blob: bb3aadea1f4853487c6916180044d079b941b339 [file] [log] [blame]
Christopher Ferris63860cb2015-11-16 17:30:32 -08001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29// Contains a thin layer that calls whatever real native allocator
30// has been defined. For the libc shared library, this allows the
31// implementation of a debug malloc that can intercept all of the allocation
32// calls and add special debugging code to attempt to catch allocation
33// errors. All of the debugging code is implemented in a separate shared
34// library that is only loaded when the property "libc.debug.malloc.options"
35// is set to a non-zero value. There are two functions exported to
36// allow ddms, or other external users to get information from the debug
37// allocation.
38// get_malloc_leak_info: Returns information about all of the known native
39// allocations that are currently in use.
40// free_malloc_leak_info: Frees the data allocated by the call to
41// get_malloc_leak_info.
Christopher Ferris2e1a40a2018-06-13 10:46:34 -070042// write_malloc_leak_info: Writes the leak info data to a file.
Christopher Ferris63860cb2015-11-16 17:30:32 -080043
Christopher Ferrisfa10a3a2019-03-08 10:56:17 -080044#include <errno.h>
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080045#include <stdint.h>
Christopher Ferris6c619a02019-03-01 17:59:51 -080046#include <stdio.h>
Colin Cross869691c2016-01-29 12:48:18 -080047
Christopher Ferris63860cb2015-11-16 17:30:32 -080048#include <private/bionic_config.h>
Christopher Ferris63860cb2015-11-16 17:30:32 -080049
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080050#include "malloc_common.h"
Evgenii Stepanovbe551f52018-08-13 16:46:15 -070051
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080052// =============================================================================
53// Global variables instantations.
54// =============================================================================
Evgenii Stepanovbe551f52018-08-13 16:46:15 -070055
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080056// Malloc hooks globals.
Christopher Ferrisdb478a62018-02-07 18:42:14 -080057void* (*volatile __malloc_hook)(size_t, const void*);
58void* (*volatile __realloc_hook)(void*, size_t, const void*);
59void (*volatile __free_hook)(void*, const void*);
60void* (*volatile __memalign_hook)(size_t, size_t, const void*);
61
Christopher Ferris63860cb2015-11-16 17:30:32 -080062// In a VM process, this is set to 1 after fork()ing out of zygote.
63int gMallocLeakZygoteChild = 0;
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080064// =============================================================================
Christopher Ferris63860cb2015-11-16 17:30:32 -080065
66// =============================================================================
67// Allocation functions
68// =============================================================================
69extern "C" void* calloc(size_t n_elements, size_t elem_size) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -080070 auto dispatch_table = GetDispatchTable();
71 if (__predict_false(dispatch_table != nullptr)) {
72 return dispatch_table->calloc(n_elements, elem_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -080073 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -080074 void* result = Malloc(calloc)(n_elements, elem_size);
75 if (__predict_false(result == nullptr)) {
76 warning_log("calloc(%zu, %zu) failed: returning null pointer", n_elements, elem_size);
77 }
78 return result;
Christopher Ferris63860cb2015-11-16 17:30:32 -080079}
80
81extern "C" void free(void* mem) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -080082 auto dispatch_table = GetDispatchTable();
83 if (__predict_false(dispatch_table != nullptr)) {
84 dispatch_table->free(mem);
Christopher Ferris63860cb2015-11-16 17:30:32 -080085 } else {
86 Malloc(free)(mem);
87 }
88}
89
90extern "C" struct mallinfo mallinfo() {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -080091 auto dispatch_table = GetDispatchTable();
92 if (__predict_false(dispatch_table != nullptr)) {
93 return dispatch_table->mallinfo();
Christopher Ferris63860cb2015-11-16 17:30:32 -080094 }
95 return Malloc(mallinfo)();
96}
97
Christopher Ferris6c619a02019-03-01 17:59:51 -080098extern "C" int malloc_info(int options, FILE* fp) {
99 auto dispatch_table = GetDispatchTable();
100 if (__predict_false(dispatch_table != nullptr)) {
101 return dispatch_table->malloc_info(options, fp);
102 }
103 return Malloc(malloc_info)(options, fp);
104}
105
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700106extern "C" int mallopt(int param, int value) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800107 auto dispatch_table = GetDispatchTable();
108 if (__predict_false(dispatch_table != nullptr)) {
109 return dispatch_table->mallopt(param, value);
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700110 }
111 return Malloc(mallopt)(param, value);
112}
113
Christopher Ferris63860cb2015-11-16 17:30:32 -0800114extern "C" void* malloc(size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800115 auto dispatch_table = GetDispatchTable();
116 if (__predict_false(dispatch_table != nullptr)) {
117 return dispatch_table->malloc(bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800118 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800119 void* result = Malloc(malloc)(bytes);
120 if (__predict_false(result == nullptr)) {
121 warning_log("malloc(%zu) failed: returning null pointer", bytes);
122 }
123 return result;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800124}
125
126extern "C" size_t malloc_usable_size(const void* mem) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800127 auto dispatch_table = GetDispatchTable();
128 if (__predict_false(dispatch_table != nullptr)) {
129 return dispatch_table->malloc_usable_size(mem);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800130 }
131 return Malloc(malloc_usable_size)(mem);
132}
133
134extern "C" void* memalign(size_t alignment, size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800135 auto dispatch_table = GetDispatchTable();
136 if (__predict_false(dispatch_table != nullptr)) {
137 return dispatch_table->memalign(alignment, bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800138 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800139 void* result = Malloc(memalign)(alignment, bytes);
140 if (__predict_false(result == nullptr)) {
141 warning_log("memalign(%zu, %zu) failed: returning null pointer", alignment, bytes);
142 }
143 return result;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800144}
145
146extern "C" int posix_memalign(void** memptr, size_t alignment, size_t size) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800147 auto dispatch_table = GetDispatchTable();
148 if (__predict_false(dispatch_table != nullptr)) {
149 return dispatch_table->posix_memalign(memptr, alignment, size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800150 }
151 return Malloc(posix_memalign)(memptr, alignment, size);
152}
153
Christopher Ferriscae21a92018-02-05 18:14:55 -0800154extern "C" void* aligned_alloc(size_t alignment, size_t size) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800155 auto dispatch_table = GetDispatchTable();
156 if (__predict_false(dispatch_table != nullptr)) {
157 return dispatch_table->aligned_alloc(alignment, size);
Christopher Ferriscae21a92018-02-05 18:14:55 -0800158 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800159 void* result = Malloc(aligned_alloc)(alignment, size);
160 if (__predict_false(result == nullptr)) {
161 warning_log("aligned_alloc(%zu, %zu) failed: returning null pointer", alignment, size);
162 }
163 return result;
Christopher Ferriscae21a92018-02-05 18:14:55 -0800164}
165
Christopher Ferris63860cb2015-11-16 17:30:32 -0800166extern "C" void* realloc(void* old_mem, size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800167 auto dispatch_table = GetDispatchTable();
168 if (__predict_false(dispatch_table != nullptr)) {
169 return dispatch_table->realloc(old_mem, bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800170 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800171 void* result = Malloc(realloc)(old_mem, bytes);
172 if (__predict_false(result == nullptr && bytes != 0)) {
173 warning_log("realloc(%p, %zu) failed: returning null pointer", old_mem, bytes);
174 }
175 return result;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800176}
177
Elliott Hughesb1770852018-09-18 12:52:42 -0700178extern "C" void* reallocarray(void* old_mem, size_t item_count, size_t item_size) {
179 size_t new_size;
180 if (__builtin_mul_overflow(item_count, item_size, &new_size)) {
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800181 warning_log("reallocaray(%p, %zu, %zu) failed: returning null pointer",
182 old_mem, item_count, item_size);
Elliott Hughesb1770852018-09-18 12:52:42 -0700183 errno = ENOMEM;
184 return nullptr;
185 }
186 return realloc(old_mem, new_size);
187}
188
Christopher Ferris63860cb2015-11-16 17:30:32 -0800189#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
190extern "C" void* pvalloc(size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800191 auto dispatch_table = GetDispatchTable();
192 if (__predict_false(dispatch_table != nullptr)) {
193 return dispatch_table->pvalloc(bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800194 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800195 void* result = Malloc(pvalloc)(bytes);
196 if (__predict_false(result == nullptr)) {
197 warning_log("pvalloc(%zu) failed: returning null pointer", bytes);
198 }
199 return result;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800200}
201
202extern "C" void* valloc(size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800203 auto dispatch_table = GetDispatchTable();
204 if (__predict_false(dispatch_table != nullptr)) {
205 return dispatch_table->valloc(bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800206 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800207 void* result = Malloc(valloc)(bytes);
208 if (__predict_false(result == nullptr)) {
209 warning_log("valloc(%zu) failed: returning null pointer", bytes);
210 }
211 return result;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800212}
213#endif
Christopher Ferris63860cb2015-11-16 17:30:32 -0800214// =============================================================================
Ryan Savitskiecc37e32018-12-14 15:57:21 +0000215
216// =============================================================================
Colin Cross869691c2016-01-29 12:48:18 -0800217// Exported for use by libmemunreachable.
218// =============================================================================
219
220// Calls callback for every allocation in the anonymous heap mapping
221// [base, base+size). Must be called between malloc_disable and malloc_enable.
222extern "C" int malloc_iterate(uintptr_t base, size_t size,
223 void (*callback)(uintptr_t base, size_t size, void* arg), void* arg) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800224 auto dispatch_table = GetDispatchTable();
225 if (__predict_false(dispatch_table != nullptr)) {
226 return dispatch_table->iterate(base, size, callback, arg);
Colin Cross869691c2016-01-29 12:48:18 -0800227 }
228 return Malloc(iterate)(base, size, callback, arg);
229}
230
231// Disable calls to malloc so malloc_iterate gets a consistent view of
232// allocated memory.
233extern "C" void malloc_disable() {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800234 auto dispatch_table = GetDispatchTable();
235 if (__predict_false(dispatch_table != nullptr)) {
236 return dispatch_table->malloc_disable();
Colin Cross869691c2016-01-29 12:48:18 -0800237 }
238 return Malloc(malloc_disable)();
239}
240
241// Re-enable calls to malloc after a previous call to malloc_disable.
242extern "C" void malloc_enable() {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800243 auto dispatch_table = GetDispatchTable();
244 if (__predict_false(dispatch_table != nullptr)) {
245 return dispatch_table->malloc_enable();
Colin Cross869691c2016-01-29 12:48:18 -0800246 }
247 return Malloc(malloc_enable)();
248}
Colin Cross2d4721c2016-02-02 11:57:54 -0800249
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800250#if defined(LIBC_STATIC)
Colin Cross2d4721c2016-02-02 11:57:54 -0800251extern "C" ssize_t malloc_backtrace(void*, uintptr_t*, size_t) {
252 return 0;
253}
254#endif
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800255
256#if __has_feature(hwaddress_sanitizer)
257// FIXME: implement these in HWASan allocator.
258extern "C" int __sanitizer_iterate(uintptr_t base __unused, size_t size __unused,
259 void (*callback)(uintptr_t base, size_t size, void* arg) __unused,
260 void* arg __unused) {
261 return 0;
262}
263
264extern "C" void __sanitizer_malloc_disable() {
265}
266
267extern "C" void __sanitizer_malloc_enable() {
268}
Christopher Ferrisfa10a3a2019-03-08 10:56:17 -0800269
270extern "C" int __sanitizer_malloc_info(int, FILE*) {
271 errno = ENOTSUP;
272 return -1;
273}
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800274#endif
275// =============================================================================
276
277// =============================================================================
278// Platform-internal mallopt variant.
279// =============================================================================
280#if defined(LIBC_STATIC)
281extern "C" bool android_mallopt(int, void*, size_t) {
282 // There are no options supported on static executables.
283 errno = ENOTSUP;
284 return false;
285}
286#endif
287// =============================================================================