blob: f817281be49b5cdff6e194b97915da8a5bdd35c8 [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 Ferris1fc5ccf2019-02-15 18:06:15 -080049#include <private/bionic_malloc.h>
Christopher Ferris63860cb2015-11-16 17:30:32 -080050
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080051#include "malloc_common.h"
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -080052#include "malloc_limit.h"
Evgenii Stepanovbe551f52018-08-13 16:46:15 -070053
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080054// =============================================================================
55// Global variables instantations.
56// =============================================================================
Evgenii Stepanovbe551f52018-08-13 16:46:15 -070057
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080058// Malloc hooks globals.
Christopher Ferrisdb478a62018-02-07 18:42:14 -080059void* (*volatile __malloc_hook)(size_t, const void*);
60void* (*volatile __realloc_hook)(void*, size_t, const void*);
61void (*volatile __free_hook)(void*, const void*);
62void* (*volatile __memalign_hook)(size_t, size_t, const void*);
63
Christopher Ferris63860cb2015-11-16 17:30:32 -080064// In a VM process, this is set to 1 after fork()ing out of zygote.
65int gMallocLeakZygoteChild = 0;
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080066// =============================================================================
Christopher Ferris63860cb2015-11-16 17:30:32 -080067
68// =============================================================================
69// Allocation functions
70// =============================================================================
71extern "C" void* calloc(size_t n_elements, size_t elem_size) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -080072 auto dispatch_table = GetDispatchTable();
73 if (__predict_false(dispatch_table != nullptr)) {
74 return dispatch_table->calloc(n_elements, elem_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -080075 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -080076 void* result = Malloc(calloc)(n_elements, elem_size);
77 if (__predict_false(result == nullptr)) {
78 warning_log("calloc(%zu, %zu) failed: returning null pointer", n_elements, elem_size);
79 }
80 return result;
Christopher Ferris63860cb2015-11-16 17:30:32 -080081}
82
83extern "C" void free(void* mem) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -080084 auto dispatch_table = GetDispatchTable();
85 if (__predict_false(dispatch_table != nullptr)) {
86 dispatch_table->free(mem);
Christopher Ferris63860cb2015-11-16 17:30:32 -080087 } else {
88 Malloc(free)(mem);
89 }
90}
91
92extern "C" struct mallinfo mallinfo() {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -080093 auto dispatch_table = GetDispatchTable();
94 if (__predict_false(dispatch_table != nullptr)) {
95 return dispatch_table->mallinfo();
Christopher Ferris63860cb2015-11-16 17:30:32 -080096 }
97 return Malloc(mallinfo)();
98}
99
Christopher Ferris6c619a02019-03-01 17:59:51 -0800100extern "C" int malloc_info(int options, FILE* fp) {
101 auto dispatch_table = GetDispatchTable();
102 if (__predict_false(dispatch_table != nullptr)) {
103 return dispatch_table->malloc_info(options, fp);
104 }
105 return Malloc(malloc_info)(options, fp);
106}
107
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700108extern "C" int mallopt(int param, int value) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800109 auto dispatch_table = GetDispatchTable();
110 if (__predict_false(dispatch_table != nullptr)) {
111 return dispatch_table->mallopt(param, value);
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700112 }
113 return Malloc(mallopt)(param, value);
114}
115
Christopher Ferris63860cb2015-11-16 17:30:32 -0800116extern "C" void* malloc(size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800117 auto dispatch_table = GetDispatchTable();
118 if (__predict_false(dispatch_table != nullptr)) {
119 return dispatch_table->malloc(bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800120 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800121 void* result = Malloc(malloc)(bytes);
122 if (__predict_false(result == nullptr)) {
123 warning_log("malloc(%zu) failed: returning null pointer", bytes);
124 }
125 return result;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800126}
127
128extern "C" size_t malloc_usable_size(const void* mem) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800129 auto dispatch_table = GetDispatchTable();
130 if (__predict_false(dispatch_table != nullptr)) {
131 return dispatch_table->malloc_usable_size(mem);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800132 }
133 return Malloc(malloc_usable_size)(mem);
134}
135
136extern "C" void* memalign(size_t alignment, size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800137 auto dispatch_table = GetDispatchTable();
138 if (__predict_false(dispatch_table != nullptr)) {
139 return dispatch_table->memalign(alignment, bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800140 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800141 void* result = Malloc(memalign)(alignment, bytes);
142 if (__predict_false(result == nullptr)) {
143 warning_log("memalign(%zu, %zu) failed: returning null pointer", alignment, bytes);
144 }
145 return result;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800146}
147
148extern "C" int posix_memalign(void** memptr, size_t alignment, size_t size) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800149 auto dispatch_table = GetDispatchTable();
150 if (__predict_false(dispatch_table != nullptr)) {
151 return dispatch_table->posix_memalign(memptr, alignment, size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800152 }
153 return Malloc(posix_memalign)(memptr, alignment, size);
154}
155
Christopher Ferriscae21a92018-02-05 18:14:55 -0800156extern "C" void* aligned_alloc(size_t alignment, size_t size) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800157 auto dispatch_table = GetDispatchTable();
158 if (__predict_false(dispatch_table != nullptr)) {
159 return dispatch_table->aligned_alloc(alignment, size);
Christopher Ferriscae21a92018-02-05 18:14:55 -0800160 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800161 void* result = Malloc(aligned_alloc)(alignment, size);
162 if (__predict_false(result == nullptr)) {
163 warning_log("aligned_alloc(%zu, %zu) failed: returning null pointer", alignment, size);
164 }
165 return result;
Christopher Ferriscae21a92018-02-05 18:14:55 -0800166}
167
Christopher Ferris63860cb2015-11-16 17:30:32 -0800168extern "C" void* realloc(void* old_mem, size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800169 auto dispatch_table = GetDispatchTable();
170 if (__predict_false(dispatch_table != nullptr)) {
171 return dispatch_table->realloc(old_mem, bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800172 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800173 void* result = Malloc(realloc)(old_mem, bytes);
174 if (__predict_false(result == nullptr && bytes != 0)) {
175 warning_log("realloc(%p, %zu) failed: returning null pointer", old_mem, bytes);
176 }
177 return result;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800178}
179
Elliott Hughesb1770852018-09-18 12:52:42 -0700180extern "C" void* reallocarray(void* old_mem, size_t item_count, size_t item_size) {
181 size_t new_size;
182 if (__builtin_mul_overflow(item_count, item_size, &new_size)) {
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800183 warning_log("reallocaray(%p, %zu, %zu) failed: returning null pointer",
184 old_mem, item_count, item_size);
Elliott Hughesb1770852018-09-18 12:52:42 -0700185 errno = ENOMEM;
186 return nullptr;
187 }
188 return realloc(old_mem, new_size);
189}
190
Christopher Ferris63860cb2015-11-16 17:30:32 -0800191#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
192extern "C" void* pvalloc(size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800193 auto dispatch_table = GetDispatchTable();
194 if (__predict_false(dispatch_table != nullptr)) {
195 return dispatch_table->pvalloc(bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800196 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800197 void* result = Malloc(pvalloc)(bytes);
198 if (__predict_false(result == nullptr)) {
199 warning_log("pvalloc(%zu) failed: returning null pointer", bytes);
200 }
201 return result;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800202}
203
204extern "C" void* valloc(size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800205 auto dispatch_table = GetDispatchTable();
206 if (__predict_false(dispatch_table != nullptr)) {
207 return dispatch_table->valloc(bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800208 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800209 void* result = Malloc(valloc)(bytes);
210 if (__predict_false(result == nullptr)) {
211 warning_log("valloc(%zu) failed: returning null pointer", bytes);
212 }
213 return result;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800214}
215#endif
Christopher Ferris63860cb2015-11-16 17:30:32 -0800216// =============================================================================
Ryan Savitskiecc37e32018-12-14 15:57:21 +0000217
218// =============================================================================
Colin Cross869691c2016-01-29 12:48:18 -0800219// Exported for use by libmemunreachable.
220// =============================================================================
221
222// Calls callback for every allocation in the anonymous heap mapping
223// [base, base+size). Must be called between malloc_disable and malloc_enable.
224extern "C" int malloc_iterate(uintptr_t base, size_t size,
225 void (*callback)(uintptr_t base, size_t size, void* arg), void* arg) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800226 auto dispatch_table = GetDispatchTable();
227 if (__predict_false(dispatch_table != nullptr)) {
228 return dispatch_table->iterate(base, size, callback, arg);
Colin Cross869691c2016-01-29 12:48:18 -0800229 }
230 return Malloc(iterate)(base, size, callback, arg);
231}
232
233// Disable calls to malloc so malloc_iterate gets a consistent view of
234// allocated memory.
235extern "C" void malloc_disable() {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800236 auto dispatch_table = GetDispatchTable();
237 if (__predict_false(dispatch_table != nullptr)) {
238 return dispatch_table->malloc_disable();
Colin Cross869691c2016-01-29 12:48:18 -0800239 }
240 return Malloc(malloc_disable)();
241}
242
243// Re-enable calls to malloc after a previous call to malloc_disable.
244extern "C" void malloc_enable() {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800245 auto dispatch_table = GetDispatchTable();
246 if (__predict_false(dispatch_table != nullptr)) {
247 return dispatch_table->malloc_enable();
Colin Cross869691c2016-01-29 12:48:18 -0800248 }
249 return Malloc(malloc_enable)();
250}
Colin Cross2d4721c2016-02-02 11:57:54 -0800251
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800252#if defined(LIBC_STATIC)
Colin Cross2d4721c2016-02-02 11:57:54 -0800253extern "C" ssize_t malloc_backtrace(void*, uintptr_t*, size_t) {
254 return 0;
255}
256#endif
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800257
258#if __has_feature(hwaddress_sanitizer)
259// FIXME: implement these in HWASan allocator.
260extern "C" int __sanitizer_iterate(uintptr_t base __unused, size_t size __unused,
261 void (*callback)(uintptr_t base, size_t size, void* arg) __unused,
262 void* arg __unused) {
263 return 0;
264}
265
266extern "C" void __sanitizer_malloc_disable() {
267}
268
269extern "C" void __sanitizer_malloc_enable() {
270}
Christopher Ferrisfa10a3a2019-03-08 10:56:17 -0800271
272extern "C" int __sanitizer_malloc_info(int, FILE*) {
273 errno = ENOTSUP;
274 return -1;
275}
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800276#endif
277// =============================================================================
278
279// =============================================================================
280// Platform-internal mallopt variant.
281// =============================================================================
282#if defined(LIBC_STATIC)
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -0800283extern "C" bool android_mallopt(int opcode, void* arg, size_t arg_size) {
284 if (opcode == M_SET_ALLOCATION_LIMIT_BYTES) {
285 return LimitEnable(arg, arg_size);
286 }
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800287 errno = ENOTSUP;
288 return false;
289}
290#endif
291// =============================================================================