blob: edd697d5169dff35b6e2f2eb05e0ae3a3ed7f525 [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 Ferrise4cdbc42019-02-08 17:30:58 -080044#include <stdint.h>
Christopher Ferris6c619a02019-03-01 17:59:51 -080045#include <stdio.h>
Colin Cross869691c2016-01-29 12:48:18 -080046
Christopher Ferris63860cb2015-11-16 17:30:32 -080047#include <private/bionic_config.h>
Christopher Ferris63860cb2015-11-16 17:30:32 -080048
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080049#include "malloc_common.h"
Evgenii Stepanovbe551f52018-08-13 16:46:15 -070050
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080051// =============================================================================
52// Global variables instantations.
53// =============================================================================
Evgenii Stepanovbe551f52018-08-13 16:46:15 -070054
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080055// Malloc hooks globals.
Christopher Ferrisdb478a62018-02-07 18:42:14 -080056void* (*volatile __malloc_hook)(size_t, const void*);
57void* (*volatile __realloc_hook)(void*, size_t, const void*);
58void (*volatile __free_hook)(void*, const void*);
59void* (*volatile __memalign_hook)(size_t, size_t, const void*);
60
Christopher Ferris63860cb2015-11-16 17:30:32 -080061// In a VM process, this is set to 1 after fork()ing out of zygote.
62int gMallocLeakZygoteChild = 0;
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080063// =============================================================================
Christopher Ferris63860cb2015-11-16 17:30:32 -080064
65// =============================================================================
66// Allocation functions
67// =============================================================================
68extern "C" void* calloc(size_t n_elements, size_t elem_size) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -080069 auto dispatch_table = GetDispatchTable();
70 if (__predict_false(dispatch_table != nullptr)) {
71 return dispatch_table->calloc(n_elements, elem_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -080072 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -080073 void* result = Malloc(calloc)(n_elements, elem_size);
74 if (__predict_false(result == nullptr)) {
75 warning_log("calloc(%zu, %zu) failed: returning null pointer", n_elements, elem_size);
76 }
77 return result;
Christopher Ferris63860cb2015-11-16 17:30:32 -080078}
79
80extern "C" void free(void* mem) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -080081 auto dispatch_table = GetDispatchTable();
82 if (__predict_false(dispatch_table != nullptr)) {
83 dispatch_table->free(mem);
Christopher Ferris63860cb2015-11-16 17:30:32 -080084 } else {
85 Malloc(free)(mem);
86 }
87}
88
89extern "C" struct mallinfo mallinfo() {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -080090 auto dispatch_table = GetDispatchTable();
91 if (__predict_false(dispatch_table != nullptr)) {
92 return dispatch_table->mallinfo();
Christopher Ferris63860cb2015-11-16 17:30:32 -080093 }
94 return Malloc(mallinfo)();
95}
96
Christopher Ferris6c619a02019-03-01 17:59:51 -080097extern "C" int malloc_info(int options, FILE* fp) {
98 auto dispatch_table = GetDispatchTable();
99 if (__predict_false(dispatch_table != nullptr)) {
100 return dispatch_table->malloc_info(options, fp);
101 }
102 return Malloc(malloc_info)(options, fp);
103}
104
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700105extern "C" int mallopt(int param, int value) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800106 auto dispatch_table = GetDispatchTable();
107 if (__predict_false(dispatch_table != nullptr)) {
108 return dispatch_table->mallopt(param, value);
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700109 }
110 return Malloc(mallopt)(param, value);
111}
112
Christopher Ferris63860cb2015-11-16 17:30:32 -0800113extern "C" void* malloc(size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800114 auto dispatch_table = GetDispatchTable();
115 if (__predict_false(dispatch_table != nullptr)) {
116 return dispatch_table->malloc(bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800117 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800118 void* result = Malloc(malloc)(bytes);
119 if (__predict_false(result == nullptr)) {
120 warning_log("malloc(%zu) failed: returning null pointer", bytes);
121 }
122 return result;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800123}
124
125extern "C" size_t malloc_usable_size(const void* mem) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800126 auto dispatch_table = GetDispatchTable();
127 if (__predict_false(dispatch_table != nullptr)) {
128 return dispatch_table->malloc_usable_size(mem);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800129 }
130 return Malloc(malloc_usable_size)(mem);
131}
132
133extern "C" void* memalign(size_t alignment, size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800134 auto dispatch_table = GetDispatchTable();
135 if (__predict_false(dispatch_table != nullptr)) {
136 return dispatch_table->memalign(alignment, bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800137 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800138 void* result = Malloc(memalign)(alignment, bytes);
139 if (__predict_false(result == nullptr)) {
140 warning_log("memalign(%zu, %zu) failed: returning null pointer", alignment, bytes);
141 }
142 return result;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800143}
144
145extern "C" int posix_memalign(void** memptr, size_t alignment, size_t size) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800146 auto dispatch_table = GetDispatchTable();
147 if (__predict_false(dispatch_table != nullptr)) {
148 return dispatch_table->posix_memalign(memptr, alignment, size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800149 }
150 return Malloc(posix_memalign)(memptr, alignment, size);
151}
152
Christopher Ferriscae21a92018-02-05 18:14:55 -0800153extern "C" void* aligned_alloc(size_t alignment, size_t size) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800154 auto dispatch_table = GetDispatchTable();
155 if (__predict_false(dispatch_table != nullptr)) {
156 return dispatch_table->aligned_alloc(alignment, size);
Christopher Ferriscae21a92018-02-05 18:14:55 -0800157 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800158 void* result = Malloc(aligned_alloc)(alignment, size);
159 if (__predict_false(result == nullptr)) {
160 warning_log("aligned_alloc(%zu, %zu) failed: returning null pointer", alignment, size);
161 }
162 return result;
Christopher Ferriscae21a92018-02-05 18:14:55 -0800163}
164
Christopher Ferris63860cb2015-11-16 17:30:32 -0800165extern "C" void* realloc(void* old_mem, size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800166 auto dispatch_table = GetDispatchTable();
167 if (__predict_false(dispatch_table != nullptr)) {
168 return dispatch_table->realloc(old_mem, bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800169 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800170 void* result = Malloc(realloc)(old_mem, bytes);
171 if (__predict_false(result == nullptr && bytes != 0)) {
172 warning_log("realloc(%p, %zu) failed: returning null pointer", old_mem, bytes);
173 }
174 return result;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800175}
176
Elliott Hughesb1770852018-09-18 12:52:42 -0700177extern "C" void* reallocarray(void* old_mem, size_t item_count, size_t item_size) {
178 size_t new_size;
179 if (__builtin_mul_overflow(item_count, item_size, &new_size)) {
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800180 warning_log("reallocaray(%p, %zu, %zu) failed: returning null pointer",
181 old_mem, item_count, item_size);
Elliott Hughesb1770852018-09-18 12:52:42 -0700182 errno = ENOMEM;
183 return nullptr;
184 }
185 return realloc(old_mem, new_size);
186}
187
Christopher Ferris63860cb2015-11-16 17:30:32 -0800188#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
189extern "C" void* pvalloc(size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800190 auto dispatch_table = GetDispatchTable();
191 if (__predict_false(dispatch_table != nullptr)) {
192 return dispatch_table->pvalloc(bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800193 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800194 void* result = Malloc(pvalloc)(bytes);
195 if (__predict_false(result == nullptr)) {
196 warning_log("pvalloc(%zu) failed: returning null pointer", bytes);
197 }
198 return result;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800199}
200
201extern "C" void* valloc(size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800202 auto dispatch_table = GetDispatchTable();
203 if (__predict_false(dispatch_table != nullptr)) {
204 return dispatch_table->valloc(bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800205 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800206 void* result = Malloc(valloc)(bytes);
207 if (__predict_false(result == nullptr)) {
208 warning_log("valloc(%zu) failed: returning null pointer", bytes);
209 }
210 return result;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800211}
212#endif
Christopher Ferris63860cb2015-11-16 17:30:32 -0800213// =============================================================================
Ryan Savitskiecc37e32018-12-14 15:57:21 +0000214
215// =============================================================================
Colin Cross869691c2016-01-29 12:48:18 -0800216// Exported for use by libmemunreachable.
217// =============================================================================
218
219// Calls callback for every allocation in the anonymous heap mapping
220// [base, base+size). Must be called between malloc_disable and malloc_enable.
221extern "C" int malloc_iterate(uintptr_t base, size_t size,
222 void (*callback)(uintptr_t base, size_t size, void* arg), void* arg) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800223 auto dispatch_table = GetDispatchTable();
224 if (__predict_false(dispatch_table != nullptr)) {
225 return dispatch_table->iterate(base, size, callback, arg);
Colin Cross869691c2016-01-29 12:48:18 -0800226 }
227 return Malloc(iterate)(base, size, callback, arg);
228}
229
230// Disable calls to malloc so malloc_iterate gets a consistent view of
231// allocated memory.
232extern "C" void malloc_disable() {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800233 auto dispatch_table = GetDispatchTable();
234 if (__predict_false(dispatch_table != nullptr)) {
235 return dispatch_table->malloc_disable();
Colin Cross869691c2016-01-29 12:48:18 -0800236 }
237 return Malloc(malloc_disable)();
238}
239
240// Re-enable calls to malloc after a previous call to malloc_disable.
241extern "C" void malloc_enable() {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800242 auto dispatch_table = GetDispatchTable();
243 if (__predict_false(dispatch_table != nullptr)) {
244 return dispatch_table->malloc_enable();
Colin Cross869691c2016-01-29 12:48:18 -0800245 }
246 return Malloc(malloc_enable)();
247}
Colin Cross2d4721c2016-02-02 11:57:54 -0800248
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800249#if defined(LIBC_STATIC)
Colin Cross2d4721c2016-02-02 11:57:54 -0800250extern "C" ssize_t malloc_backtrace(void*, uintptr_t*, size_t) {
251 return 0;
252}
253#endif
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800254
255#if __has_feature(hwaddress_sanitizer)
256// FIXME: implement these in HWASan allocator.
257extern "C" int __sanitizer_iterate(uintptr_t base __unused, size_t size __unused,
258 void (*callback)(uintptr_t base, size_t size, void* arg) __unused,
259 void* arg __unused) {
260 return 0;
261}
262
263extern "C" void __sanitizer_malloc_disable() {
264}
265
266extern "C" void __sanitizer_malloc_enable() {
267}
268#endif
269// =============================================================================
270
271// =============================================================================
272// Platform-internal mallopt variant.
273// =============================================================================
274#if defined(LIBC_STATIC)
275extern "C" bool android_mallopt(int, void*, size_t) {
276 // There are no options supported on static executables.
277 errno = ENOTSUP;
278 return false;
279}
280#endif
281// =============================================================================