blob: b35aa27117eb2529cdcf47615f492b0d1c30baeb [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
Colin Cross869691c2016-01-29 12:48:18 -080044#include <pthread.h>
Florian Mayerf7f71e32018-08-31 15:36:48 -070045#include <stdatomic.h>
Colin Cross869691c2016-01-29 12:48:18 -080046
dimitry5332af62018-12-04 14:03:13 +010047#include <private/bionic_defs.h>
Christopher Ferris63860cb2015-11-16 17:30:32 -080048#include <private/bionic_config.h>
49#include <private/bionic_globals.h>
Ryan Savitskiecc37e32018-12-14 15:57:21 +000050#include <private/bionic_malloc.h>
Christopher Ferris63860cb2015-11-16 17:30:32 -080051#include <private/bionic_malloc_dispatch.h>
52
Evgenii Stepanovbe551f52018-08-13 16:46:15 -070053#if __has_feature(hwaddress_sanitizer)
54// FIXME: implement these in HWASan allocator.
55extern "C" int __sanitizer_iterate(uintptr_t base __unused, size_t size __unused,
56 void (*callback)(uintptr_t base, size_t size, void* arg) __unused,
57 void* arg __unused) {
58 return 0;
59}
60
61extern "C" void __sanitizer_malloc_disable() {
62}
63
64extern "C" void __sanitizer_malloc_enable() {
65}
66#include <sanitizer/hwasan_interface.h>
67#define Malloc(function) __sanitizer_ ## function
68
69#else // __has_feature(hwaddress_sanitizer)
Christopher Ferris63860cb2015-11-16 17:30:32 -080070#include "jemalloc.h"
71#define Malloc(function) je_ ## function
Evgenii Stepanovbe551f52018-08-13 16:46:15 -070072#endif
Christopher Ferris63860cb2015-11-16 17:30:32 -080073
Florian Mayerf7f71e32018-08-31 15:36:48 -070074template <typename T>
75static T* RemoveConst(const T* x) {
76 return const_cast<T*>(x);
77}
78
79// RemoveConst is a workaround for bug in current libcxx. Fix in
80// https://reviews.llvm.org/D47613
81#define atomic_load_explicit_const(obj, order) atomic_load_explicit(RemoveConst(obj), order)
82
Christopher Ferris63860cb2015-11-16 17:30:32 -080083static constexpr MallocDispatch __libc_malloc_default_dispatch
84 __attribute__((unused)) = {
85 Malloc(calloc),
86 Malloc(free),
87 Malloc(mallinfo),
88 Malloc(malloc),
89 Malloc(malloc_usable_size),
90 Malloc(memalign),
91 Malloc(posix_memalign),
92#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
93 Malloc(pvalloc),
94#endif
95 Malloc(realloc),
96#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
97 Malloc(valloc),
98#endif
Colin Cross869691c2016-01-29 12:48:18 -080099 Malloc(iterate),
100 Malloc(malloc_disable),
101 Malloc(malloc_enable),
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700102 Malloc(mallopt),
Christopher Ferriscae21a92018-02-05 18:14:55 -0800103 Malloc(aligned_alloc),
Christopher Ferris63860cb2015-11-16 17:30:32 -0800104 };
105
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800106// Malloc hooks.
107void* (*volatile __malloc_hook)(size_t, const void*);
108void* (*volatile __realloc_hook)(void*, size_t, const void*);
109void (*volatile __free_hook)(void*, const void*);
110void* (*volatile __memalign_hook)(size_t, size_t, const void*);
111
Christopher Ferris63860cb2015-11-16 17:30:32 -0800112// In a VM process, this is set to 1 after fork()ing out of zygote.
113int gMallocLeakZygoteChild = 0;
114
115// =============================================================================
116// Allocation functions
117// =============================================================================
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800118static inline const MallocDispatch* GetDispatchTable() {
119 return atomic_load_explicit_const(&__libc_globals->current_dispatch_table,
120 memory_order_acquire);
121}
122
Christopher Ferris63860cb2015-11-16 17:30:32 -0800123extern "C" void* calloc(size_t n_elements, size_t elem_size) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800124 auto dispatch_table = GetDispatchTable();
125 if (__predict_false(dispatch_table != nullptr)) {
126 return dispatch_table->calloc(n_elements, elem_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800127 }
128 return Malloc(calloc)(n_elements, elem_size);
129}
130
131extern "C" void free(void* mem) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800132 auto dispatch_table = GetDispatchTable();
133 if (__predict_false(dispatch_table != nullptr)) {
134 dispatch_table->free(mem);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800135 } else {
136 Malloc(free)(mem);
137 }
138}
139
140extern "C" struct mallinfo mallinfo() {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800141 auto dispatch_table = GetDispatchTable();
142 if (__predict_false(dispatch_table != nullptr)) {
143 return dispatch_table->mallinfo();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800144 }
145 return Malloc(mallinfo)();
146}
147
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700148extern "C" int mallopt(int param, int value) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800149 auto dispatch_table = GetDispatchTable();
150 if (__predict_false(dispatch_table != nullptr)) {
151 return dispatch_table->mallopt(param, value);
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700152 }
153 return Malloc(mallopt)(param, value);
154}
155
Christopher Ferris63860cb2015-11-16 17:30:32 -0800156extern "C" void* malloc(size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800157 auto dispatch_table = GetDispatchTable();
158 if (__predict_false(dispatch_table != nullptr)) {
159 return dispatch_table->malloc(bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800160 }
161 return Malloc(malloc)(bytes);
162}
163
164extern "C" size_t malloc_usable_size(const void* mem) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800165 auto dispatch_table = GetDispatchTable();
166 if (__predict_false(dispatch_table != nullptr)) {
167 return dispatch_table->malloc_usable_size(mem);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800168 }
169 return Malloc(malloc_usable_size)(mem);
170}
171
172extern "C" void* memalign(size_t alignment, size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800173 auto dispatch_table = GetDispatchTable();
174 if (__predict_false(dispatch_table != nullptr)) {
175 return dispatch_table->memalign(alignment, bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800176 }
177 return Malloc(memalign)(alignment, bytes);
178}
179
180extern "C" int posix_memalign(void** memptr, size_t alignment, size_t size) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800181 auto dispatch_table = GetDispatchTable();
182 if (__predict_false(dispatch_table != nullptr)) {
183 return dispatch_table->posix_memalign(memptr, alignment, size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800184 }
185 return Malloc(posix_memalign)(memptr, alignment, size);
186}
187
Christopher Ferriscae21a92018-02-05 18:14:55 -0800188extern "C" void* aligned_alloc(size_t alignment, size_t size) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800189 auto dispatch_table = GetDispatchTable();
190 if (__predict_false(dispatch_table != nullptr)) {
191 return dispatch_table->aligned_alloc(alignment, size);
Christopher Ferriscae21a92018-02-05 18:14:55 -0800192 }
193 return Malloc(aligned_alloc)(alignment, size);
194}
195
Christopher Ferris63860cb2015-11-16 17:30:32 -0800196extern "C" void* realloc(void* old_mem, size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800197 auto dispatch_table = GetDispatchTable();
198 if (__predict_false(dispatch_table != nullptr)) {
199 return dispatch_table->realloc(old_mem, bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800200 }
201 return Malloc(realloc)(old_mem, bytes);
202}
203
Elliott Hughesb1770852018-09-18 12:52:42 -0700204extern "C" void* reallocarray(void* old_mem, size_t item_count, size_t item_size) {
205 size_t new_size;
206 if (__builtin_mul_overflow(item_count, item_size, &new_size)) {
207 errno = ENOMEM;
208 return nullptr;
209 }
210 return realloc(old_mem, new_size);
211}
212
Christopher Ferris63860cb2015-11-16 17:30:32 -0800213#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
214extern "C" void* pvalloc(size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800215 auto dispatch_table = GetDispatchTable();
216 if (__predict_false(dispatch_table != nullptr)) {
217 return dispatch_table->pvalloc(bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800218 }
219 return Malloc(pvalloc)(bytes);
220}
221
222extern "C" void* valloc(size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800223 auto dispatch_table = GetDispatchTable();
224 if (__predict_false(dispatch_table != nullptr)) {
225 return dispatch_table->valloc(bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800226 }
227 return Malloc(valloc)(bytes);
228}
229#endif
230
231// We implement malloc debugging only in libc.so, so the code below
232// must be excluded if we compile this file for static libc.a
233#if !defined(LIBC_STATIC)
234
235#include <dlfcn.h>
Florian Mayer4e28ea12018-11-22 17:34:34 +0000236#include <fcntl.h>
Christopher Ferris63860cb2015-11-16 17:30:32 -0800237#include <stdio.h>
238#include <stdlib.h>
Florian Mayer4e28ea12018-11-22 17:34:34 +0000239#include <unistd.h>
Christopher Ferris63860cb2015-11-16 17:30:32 -0800240
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700241#include <async_safe/log.h>
Christopher Ferris63860cb2015-11-16 17:30:32 -0800242#include <sys/system_properties.h>
243
244extern "C" int __cxa_atexit(void (*func)(void *), void *arg, void *dso);
245
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800246static const char* HOOKS_SHARED_LIB = "libc_malloc_hooks.so";
247static const char* HOOKS_PROPERTY_ENABLE = "libc.debug.hooks.enable";
248static const char* HOOKS_ENV_ENABLE = "LIBC_HOOKS_ENABLE";
249
Christopher Ferris63860cb2015-11-16 17:30:32 -0800250static const char* DEBUG_SHARED_LIB = "libc_malloc_debug.so";
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800251static const char* DEBUG_PROPERTY_OPTIONS = "libc.debug.malloc.options";
252static const char* DEBUG_PROPERTY_PROGRAM = "libc.debug.malloc.program";
253static const char* DEBUG_ENV_OPTIONS = "LIBC_DEBUG_MALLOC_OPTIONS";
Christopher Ferris63860cb2015-11-16 17:30:32 -0800254
Florian Mayerf7f71e32018-08-31 15:36:48 -0700255static const char* HEAPPROFD_SHARED_LIB = "heapprofd_client.so";
256static const char* HEAPPROFD_PREFIX = "heapprofd";
Florian Mayer0dbe6d12018-11-08 11:25:49 +0000257static const char* HEAPPROFD_PROPERTY_ENABLE = "heapprofd.enable";
Florian Mayerf7f71e32018-08-31 15:36:48 -0700258static const int HEAPPROFD_SIGNAL = __SIGRTMIN + 4;
259
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800260enum FunctionEnum : uint8_t {
261 FUNC_INITIALIZE,
262 FUNC_FINALIZE,
263 FUNC_GET_MALLOC_LEAK_INFO,
264 FUNC_FREE_MALLOC_LEAK_INFO,
265 FUNC_MALLOC_BACKTRACE,
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700266 FUNC_WRITE_LEAK_INFO,
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800267 FUNC_LAST,
268};
269static void* g_functions[FUNC_LAST];
Christopher Ferris63860cb2015-11-16 17:30:32 -0800270
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800271typedef void (*finalize_func_t)();
272typedef bool (*init_func_t)(const MallocDispatch*, int*, const char*);
273typedef void (*get_malloc_leak_info_func_t)(uint8_t**, size_t*, size_t*, size_t*, size_t*);
274typedef void (*free_malloc_leak_info_func_t)(uint8_t*);
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700275typedef bool (*write_malloc_leak_info_func_t)(FILE*);
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800276typedef ssize_t (*malloc_backtrace_func_t)(void*, uintptr_t*, size_t);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800277
278// =============================================================================
279// Log functions
280// =============================================================================
281#define error_log(format, ...) \
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700282 async_safe_format_log(ANDROID_LOG_ERROR, "libc", (format), ##__VA_ARGS__ )
Christopher Ferris63860cb2015-11-16 17:30:32 -0800283#define info_log(format, ...) \
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700284 async_safe_format_log(ANDROID_LOG_INFO, "libc", (format), ##__VA_ARGS__ )
Christopher Ferris63860cb2015-11-16 17:30:32 -0800285// =============================================================================
286
Ryan Savitskiecc37e32018-12-14 15:57:21 +0000287// In a Zygote child process, this is set to true if profiling of this process
288// is allowed. Note that this set at a later time than the above
289// gMallocLeakZygoteChild. The latter is set during the fork (while still in
290// zygote's SELinux domain). While this bit is set after the child is
291// specialized (and has transferred SELinux domains if applicable).
292static _Atomic bool gMallocZygoteChildProfileable = false;
293
Christopher Ferris63860cb2015-11-16 17:30:32 -0800294// =============================================================================
295// Exported for use by ddms.
296// =============================================================================
297
298// Retrieve native heap information.
299//
300// "*info" is set to a buffer we allocate
301// "*overall_size" is set to the size of the "info" buffer
302// "*info_size" is set to the size of a single entry
303// "*total_memory" is set to the sum of all allocations we're tracking; does
304// not include heap overhead
305// "*backtrace_size" is set to the maximum number of entries in the back trace
306extern "C" void get_malloc_leak_info(uint8_t** info, size_t* overall_size,
307 size_t* info_size, size_t* total_memory, size_t* backtrace_size) {
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800308 void* func = g_functions[FUNC_GET_MALLOC_LEAK_INFO];
309 if (func == nullptr) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800310 return;
311 }
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800312 reinterpret_cast<get_malloc_leak_info_func_t>(func)(info, overall_size, info_size, total_memory,
313 backtrace_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800314}
315
316extern "C" void free_malloc_leak_info(uint8_t* info) {
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800317 void* func = g_functions[FUNC_FREE_MALLOC_LEAK_INFO];
318 if (func == nullptr) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800319 return;
320 }
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800321 reinterpret_cast<free_malloc_leak_info_func_t>(func)(info);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800322}
Colin Cross869691c2016-01-29 12:48:18 -0800323
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700324extern "C" void write_malloc_leak_info(FILE* fp) {
325 if (fp == nullptr) {
326 error_log("write_malloc_leak_info called with a nullptr");
327 return;
328 }
329
330 void* func = g_functions[FUNC_WRITE_LEAK_INFO];
331 bool written = false;
332 if (func != nullptr) {
333 written = reinterpret_cast<write_malloc_leak_info_func_t>(func)(fp);
334 }
335
336 if (!written) {
337 fprintf(fp, "Native heap dump not available. To enable, run these commands (requires root):\n");
338 fprintf(fp, "# adb shell stop\n");
339 fprintf(fp, "# adb shell setprop libc.debug.malloc.options backtrace\n");
340 fprintf(fp, "# adb shell start\n");
341 }
342}
343
Christopher Ferris63860cb2015-11-16 17:30:32 -0800344// =============================================================================
345
346template<typename FunctionType>
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800347static bool InitMallocFunction(void* malloc_impl_handler, FunctionType* func, const char* prefix, const char* suffix) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800348 char symbol[128];
349 snprintf(symbol, sizeof(symbol), "%s_%s", prefix, suffix);
350 *func = reinterpret_cast<FunctionType>(dlsym(malloc_impl_handler, symbol));
351 if (*func == nullptr) {
352 error_log("%s: dlsym(\"%s\") failed", getprogname(), symbol);
353 return false;
354 }
355 return true;
356}
357
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800358static bool InitMallocFunctions(void* impl_handler, MallocDispatch* table, const char* prefix) {
Florian Mayerf7f71e32018-08-31 15:36:48 -0700359 if (!InitMallocFunction<MallocFree>(impl_handler, &table->free, prefix, "free")) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800360 return false;
361 }
Florian Mayerf7f71e32018-08-31 15:36:48 -0700362 if (!InitMallocFunction<MallocCalloc>(impl_handler, &table->calloc, prefix, "calloc")) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800363 return false;
364 }
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800365 if (!InitMallocFunction<MallocMallinfo>(impl_handler, &table->mallinfo, prefix, "mallinfo")) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800366 return false;
367 }
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800368 if (!InitMallocFunction<MallocMallopt>(impl_handler, &table->mallopt, prefix, "mallopt")) {
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700369 return false;
370 }
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800371 if (!InitMallocFunction<MallocMalloc>(impl_handler, &table->malloc, prefix, "malloc")) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800372 return false;
373 }
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800374 if (!InitMallocFunction<MallocMallocUsableSize>(impl_handler, &table->malloc_usable_size, prefix,
375 "malloc_usable_size")) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800376 return false;
377 }
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800378 if (!InitMallocFunction<MallocMemalign>(impl_handler, &table->memalign, prefix, "memalign")) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800379 return false;
380 }
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800381 if (!InitMallocFunction<MallocPosixMemalign>(impl_handler, &table->posix_memalign, prefix,
382 "posix_memalign")) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800383 return false;
384 }
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800385 if (!InitMallocFunction<MallocAlignedAlloc>(impl_handler, &table->aligned_alloc,
Christopher Ferriscae21a92018-02-05 18:14:55 -0800386 prefix, "aligned_alloc")) {
387 return false;
388 }
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800389 if (!InitMallocFunction<MallocRealloc>(impl_handler, &table->realloc, prefix, "realloc")) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800390 return false;
391 }
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800392 if (!InitMallocFunction<MallocIterate>(impl_handler, &table->iterate, prefix, "iterate")) {
Colin Cross869691c2016-01-29 12:48:18 -0800393 return false;
394 }
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800395 if (!InitMallocFunction<MallocMallocDisable>(impl_handler, &table->malloc_disable, prefix,
396 "malloc_disable")) {
Colin Cross869691c2016-01-29 12:48:18 -0800397 return false;
398 }
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800399 if (!InitMallocFunction<MallocMallocEnable>(impl_handler, &table->malloc_enable, prefix,
400 "malloc_enable")) {
Colin Cross869691c2016-01-29 12:48:18 -0800401 return false;
402 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800403#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800404 if (!InitMallocFunction<MallocPvalloc>(impl_handler, &table->pvalloc, prefix, "pvalloc")) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800405 return false;
406 }
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800407 if (!InitMallocFunction<MallocValloc>(impl_handler, &table->valloc, prefix, "valloc")) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800408 return false;
409 }
410#endif
411
412 return true;
413}
414
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800415static void MallocFiniImpl(void*) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800416 // Our BSD stdio implementation doesn't close the standard streams,
417 // it only flushes them. Other unclosed FILE*s will show up as
418 // malloc leaks, but to avoid the standard streams showing up in
419 // leak reports, close them here.
420 fclose(stdin);
421 fclose(stdout);
422 fclose(stderr);
423
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800424 reinterpret_cast<finalize_func_t>(g_functions[FUNC_FINALIZE])();
425}
426
427static bool CheckLoadMallocHooks(char** options) {
428 char* env = getenv(HOOKS_ENV_ENABLE);
429 if ((env == nullptr || env[0] == '\0' || env[0] == '0') &&
430 (__system_property_get(HOOKS_PROPERTY_ENABLE, *options) == 0 || *options[0] == '\0' || *options[0] == '0')) {
431 return false;
432 }
433 *options = nullptr;
434 return true;
435}
436
437static bool CheckLoadMallocDebug(char** options) {
438 // If DEBUG_MALLOC_ENV_OPTIONS is set then it overrides the system properties.
439 char* env = getenv(DEBUG_ENV_OPTIONS);
440 if (env == nullptr || env[0] == '\0') {
441 if (__system_property_get(DEBUG_PROPERTY_OPTIONS, *options) == 0 || *options[0] == '\0') {
442 return false;
443 }
444
445 // Check to see if only a specific program should have debug malloc enabled.
446 char program[PROP_VALUE_MAX];
447 if (__system_property_get(DEBUG_PROPERTY_PROGRAM, program) != 0 &&
448 strstr(getprogname(), program) == nullptr) {
449 return false;
450 }
451 } else {
452 *options = env;
453 }
454 return true;
455}
456
Florian Mayer4e28ea12018-11-22 17:34:34 +0000457static bool GetHeapprofdProgramProperty(char* data, size_t size) {
458 constexpr char prefix[] = "heapprofd.enable.";
459 // - 1 to skip nullbyte, which we will write later.
460 constexpr size_t prefix_size = sizeof(prefix) - 1;
461 if (size < prefix_size) {
462 error_log("%s: Overflow constructing heapprofd property", getprogname());
463 return false;
464 }
465 memcpy(data, prefix, prefix_size);
466
467 int fd = open("/proc/self/cmdline", O_RDONLY | O_CLOEXEC);
468 if (fd == -1) {
469 error_log("%s: Failed to open /proc/self/cmdline", getprogname());
470 return false;
471 }
472 char cmdline[128];
473 ssize_t rd = read(fd, cmdline, sizeof(cmdline) - 1);
474 close(fd);
475 if (rd == -1) {
476 error_log("%s: Failed to read /proc/self/cmdline", getprogname());
477 return false;
478 }
479 cmdline[rd] = '\0';
480 char* first_arg = static_cast<char*>(memchr(cmdline, '\0', rd));
481 if (first_arg == nullptr || first_arg == cmdline + size - 1) {
482 error_log("%s: Overflow reading cmdline", getprogname());
483 return false;
484 }
485 // For consistency with what we do with Java app cmdlines, trim everything
486 // after the @ sign of the first arg.
487 char* first_at = static_cast<char*>(memchr(cmdline, '@', rd));
488 if (first_at != nullptr && first_at < first_arg) {
489 *first_at = '\0';
490 first_arg = first_at;
491 }
492
493 char* start = static_cast<char*>(memrchr(cmdline, '/', first_arg - cmdline));
494 if (start == first_arg) {
495 // The first argument ended in a slash.
496 error_log("%s: cmdline ends in /", getprogname());
497 return false;
498 } else if (start == nullptr) {
499 start = cmdline;
500 } else {
501 // Skip the /.
502 start++;
503 }
504
505 size_t name_size = static_cast<size_t>(first_arg - start);
506 if (name_size >= size - prefix_size) {
507 error_log("%s: overflow constructing heapprofd property.", getprogname());
508 return false;
509 }
510 // + 1 to also copy the trailing null byte.
511 memcpy(data + prefix_size, start, name_size + 1);
512 return true;
513}
514
Florian Mayer0dbe6d12018-11-08 11:25:49 +0000515static bool CheckLoadHeapprofd() {
516 // First check for heapprofd.enable. If it is set to "all", enable
517 // heapprofd for all processes. Otherwise, check heapprofd.enable.${prog},
518 // if it is set and not 0, enable heap profiling for this process.
519 char property_value[PROP_VALUE_MAX];
520 if (__system_property_get(HEAPPROFD_PROPERTY_ENABLE, property_value) == 0) {
521 return false;
522 }
523 if (strcmp(property_value, "all") == 0) {
524 return true;
525 }
526
527 char program_property[128];
Florian Mayer4e28ea12018-11-22 17:34:34 +0000528 if (!GetHeapprofdProgramProperty(program_property,
529 sizeof(program_property))) {
Florian Mayer0dbe6d12018-11-08 11:25:49 +0000530 return false;
531 }
Florian Mayer0dbe6d12018-11-08 11:25:49 +0000532 if (__system_property_get(program_property, property_value) == 0) {
533 return false;
534 }
Florian Mayer0dbe6d12018-11-08 11:25:49 +0000535 return program_property[0] != '\0';
536}
537
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800538static void ClearGlobalFunctions() {
539 for (size_t i = 0; i < FUNC_LAST; i++) {
540 g_functions[i] = nullptr;
541 }
542}
543
Florian Mayerdb59b892018-11-27 17:06:54 +0000544static bool InitSharedLibrary(void* impl_handle, const char* shared_lib, const char* prefix, MallocDispatch* dispatch_table) {
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800545 static constexpr const char* names[] = {
546 "initialize",
547 "finalize",
548 "get_malloc_leak_info",
549 "free_malloc_leak_info",
550 "malloc_backtrace",
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700551 "write_malloc_leak_info",
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800552 };
553 for (size_t i = 0; i < FUNC_LAST; i++) {
554 char symbol[128];
555 snprintf(symbol, sizeof(symbol), "%s_%s", prefix, names[i]);
556 g_functions[i] = dlsym(impl_handle, symbol);
557 if (g_functions[i] == nullptr) {
558 error_log("%s: %s routine not found in %s", getprogname(), symbol, shared_lib);
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800559 ClearGlobalFunctions();
Florian Mayerdb59b892018-11-27 17:06:54 +0000560 return false;
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800561 }
562 }
563
564 if (!InitMallocFunctions(impl_handle, dispatch_table, prefix)) {
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800565 ClearGlobalFunctions();
Florian Mayerdb59b892018-11-27 17:06:54 +0000566 return false;
567 }
568 return true;
569}
570
571static void* LoadSharedLibrary(const char* shared_lib, const char* prefix, MallocDispatch* dispatch_table) {
572 void* impl_handle = dlopen(shared_lib, RTLD_NOW | RTLD_LOCAL);
573 if (impl_handle == nullptr) {
574 error_log("%s: Unable to open shared library %s: %s", getprogname(), shared_lib, dlerror());
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800575 return nullptr;
576 }
577
Florian Mayerdb59b892018-11-27 17:06:54 +0000578 if (!InitSharedLibrary(impl_handle, shared_lib, prefix, dispatch_table)) {
579 dlclose(impl_handle);
580 impl_handle = nullptr;
581 }
582
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800583 return impl_handle;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800584}
585
Florian Mayerf671e032019-01-28 18:32:14 +0000586// The handle returned by dlopen when previously loading the heapprofd
587// hooks. nullptr if they had not been loaded before.
Florian Mayerdb59b892018-11-27 17:06:54 +0000588static _Atomic (void*) g_heapprofd_handle = nullptr;
Florian Mayer176a4752018-10-23 11:48:34 +0100589
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800590static void InstallHooks(libc_globals* globals, const char* options,
Florian Mayerf7f71e32018-08-31 15:36:48 -0700591 const char* prefix, const char* shared_lib) {
Florian Mayerdb59b892018-11-27 17:06:54 +0000592 void* impl_handle = atomic_load(&g_heapprofd_handle);
Florian Mayerf671e032019-01-28 18:32:14 +0000593 bool reusing_handle = impl_handle != nullptr;
594 if (reusing_handle) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800595 if (!InitSharedLibrary(impl_handle, shared_lib, prefix, &globals->malloc_dispatch_table)) {
Florian Mayerdb59b892018-11-27 17:06:54 +0000596 return;
597 }
598 } else {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800599 impl_handle = LoadSharedLibrary(shared_lib, prefix, &globals->malloc_dispatch_table);
Florian Mayerdb59b892018-11-27 17:06:54 +0000600 if (impl_handle == nullptr) {
601 return;
602 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800603 }
Florian Mayerdb59b892018-11-27 17:06:54 +0000604 init_func_t init_func = reinterpret_cast<init_func_t>(g_functions[FUNC_INITIALIZE]);
Tamas Berghammerac81fe82016-08-26 15:54:59 +0100605 if (!init_func(&__libc_malloc_default_dispatch, &gMallocLeakZygoteChild, options)) {
Florian Mayerdb59b892018-11-27 17:06:54 +0000606 error_log("%s: failed to enable malloc %s", getprogname(), prefix);
Florian Mayerf671e032019-01-28 18:32:14 +0000607 if (!reusing_handle) {
608 // We should not close if we are re-using an old handle, as we cannot be
609 // sure other threads are not currently in the hooks.
610 dlclose(impl_handle);
611 }
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800612 ClearGlobalFunctions();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800613 return;
614 }
615
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800616 // Do a pointer swap so that all of the functions become valid at once to
617 // avoid any initialization order problems.
618 atomic_store(&globals->current_dispatch_table, &globals->malloc_dispatch_table);
619
Florian Mayerdb59b892018-11-27 17:06:54 +0000620 atomic_store(&g_heapprofd_handle, impl_handle);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800621
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800622 info_log("%s: malloc %s enabled", getprogname(), prefix);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800623
624 // Use atexit to trigger the cleanup function. This avoids a problem
625 // where another atexit function is used to cleanup allocated memory,
626 // but the finalize function was already called. This particular error
627 // seems to be triggered by a zygote spawned process calling exit.
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800628 int ret_value = __cxa_atexit(MallocFiniImpl, nullptr, nullptr);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800629 if (ret_value != 0) {
630 error_log("failed to set atexit cleanup function: %d", ret_value);
631 }
632}
633
Ryan Savitskiecc37e32018-12-14 15:57:21 +0000634// The logic for triggering heapprofd (at runtime) is as follows:
635// 1. HEAPPROFD_SIGNAL is received by the process, entering the
636// MaybeInstallInitHeapprofdHook signal handler.
637// 2. If the initialization is not already in flight
638// (g_heapprofd_init_in_progress is false), the malloc hook is set to
639// point at InitHeapprofdHook, and g_heapprofd_init_in_progress is set to
640// true.
641// 3. The next malloc call enters InitHeapprofdHook, which removes the malloc
642// hook, and spawns a detached pthread to run the InitHeapprofd task.
643// (g_heapprofd_init_hook_installed atomic is used to perform this once.)
644// 4. InitHeapprofd, on a dedicated pthread, loads the heapprofd client library,
645// installs the full set of heapprofd hooks, and invokes the client's
646// initializer. The dedicated pthread then terminates.
Florian Mayer176a4752018-10-23 11:48:34 +0100647// 5. g_heapprofd_init_in_progress and g_heapprofd_init_hook_installed are
Ryan Savitskiecc37e32018-12-14 15:57:21 +0000648// reset to false such that heapprofd can be reinitialized. Reinitialization
649// means that a new profiling session is started, and any still active is
Florian Mayer176a4752018-10-23 11:48:34 +0100650// torn down.
Florian Mayerf7f71e32018-08-31 15:36:48 -0700651//
Ryan Savitskiecc37e32018-12-14 15:57:21 +0000652// The incremental hooking and a dedicated task thread are used since we cannot
653// do heavy work within a signal handler, or when blocking a malloc invocation.
Florian Mayerf7f71e32018-08-31 15:36:48 -0700654
655static _Atomic bool g_heapprofd_init_in_progress = false;
Florian Mayer176a4752018-10-23 11:48:34 +0100656static _Atomic bool g_heapprofd_init_hook_installed = false;
Florian Mayerf7f71e32018-08-31 15:36:48 -0700657
Ryan Savitskiecc37e32018-12-14 15:57:21 +0000658extern "C" void MaybeInstallInitHeapprofdHook(int);
Florian Mayer3a538a42018-12-20 11:23:50 +0000659
660// Initializes memory allocation framework once per process.
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800661static void MallocInitImpl(libc_globals* globals) {
Florian Mayer3a538a42018-12-20 11:23:50 +0000662 struct sigaction action = {};
Ryan Savitskiecc37e32018-12-14 15:57:21 +0000663 action.sa_handler = MaybeInstallInitHeapprofdHook;
Florian Mayer3a538a42018-12-20 11:23:50 +0000664 sigaction(HEAPPROFD_SIGNAL, &action, nullptr);
665
666 const char* prefix;
667 const char* shared_lib;
668 char prop[PROP_VALUE_MAX];
669 char* options = prop;
670 // Prefer malloc debug since it existed first and is a more complete
671 // malloc interceptor than the hooks.
672 if (CheckLoadMallocDebug(&options)) {
673 prefix = "debug";
674 shared_lib = DEBUG_SHARED_LIB;
675 } else if (CheckLoadMallocHooks(&options)) {
676 prefix = "hooks";
677 shared_lib = HOOKS_SHARED_LIB;
678 } else if (CheckLoadHeapprofd()) {
679 prefix = "heapprofd";
680 shared_lib = HEAPPROFD_SHARED_LIB;
681 } else {
682 return;
683 }
684 if (!atomic_exchange(&g_heapprofd_init_in_progress, true)) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800685 InstallHooks(globals, options, prefix, shared_lib);
Florian Mayer3a538a42018-12-20 11:23:50 +0000686 atomic_store(&g_heapprofd_init_in_progress, false);
687 }
688}
689
690// Initializes memory allocation framework.
691// This routine is called from __libc_init routines in libc_init_dynamic.cpp.
692__BIONIC_WEAK_FOR_NATIVE_BRIDGE
693__LIBC_HIDDEN__ void __libc_init_malloc(libc_globals* globals) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800694 MallocInitImpl(globals);
Florian Mayer3a538a42018-12-20 11:23:50 +0000695}
696
Florian Mayerf7f71e32018-08-31 15:36:48 -0700697static void* InitHeapprofd(void*) {
698 __libc_globals.mutate([](libc_globals* globals) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800699 InstallHooks(globals, nullptr, HEAPPROFD_PREFIX, HEAPPROFD_SHARED_LIB);
Florian Mayerf7f71e32018-08-31 15:36:48 -0700700 });
701 atomic_store(&g_heapprofd_init_in_progress, false);
Florian Mayer176a4752018-10-23 11:48:34 +0100702 // Allow to install hook again to re-initialize heap profiling after the
703 // current session finished.
704 atomic_store(&g_heapprofd_init_hook_installed, false);
Florian Mayerf7f71e32018-08-31 15:36:48 -0700705 return nullptr;
706}
707
708static void* InitHeapprofdHook(size_t bytes) {
Florian Mayer176a4752018-10-23 11:48:34 +0100709 if (!atomic_exchange(&g_heapprofd_init_hook_installed, true)) {
Florian Mayerf7f71e32018-08-31 15:36:48 -0700710 __libc_globals.mutate([](libc_globals* globals) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800711 atomic_store(&globals->current_dispatch_table, nullptr);
Florian Mayerf7f71e32018-08-31 15:36:48 -0700712 });
713
714 pthread_t thread_id;
715 if (pthread_create(&thread_id, nullptr, InitHeapprofd, nullptr) == -1)
716 error_log("%s: heapprofd: failed to pthread_create.", getprogname());
717 else if (pthread_detach(thread_id) == -1)
718 error_log("%s: heapprofd: failed to pthread_detach", getprogname());
719 if (pthread_setname_np(thread_id, "heapprofdinit") == -1)
720 error_log("%s: heapprod: failed to pthread_setname_np", getprogname());
721 }
722 return Malloc(malloc)(bytes);
723}
724
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800725static constexpr MallocDispatch __heapprofd_dispatch
726 __attribute__((unused)) = {
727 Malloc(calloc),
728 Malloc(free),
729 Malloc(mallinfo),
730 InitHeapprofdHook,
731 Malloc(malloc_usable_size),
732 Malloc(memalign),
733 Malloc(posix_memalign),
734#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
735 Malloc(pvalloc),
736#endif
737 Malloc(realloc),
738#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
739 Malloc(valloc),
740#endif
741 Malloc(iterate),
742 Malloc(malloc_disable),
743 Malloc(malloc_enable),
744 Malloc(mallopt),
745 Malloc(aligned_alloc),
746 };
747
Ryan Savitskiecc37e32018-12-14 15:57:21 +0000748extern "C" void MaybeInstallInitHeapprofdHook(int) {
749 // Zygote child processes must be marked profileable.
750 if (gMallocLeakZygoteChild &&
751 !atomic_load_explicit_const(&gMallocZygoteChildProfileable, memory_order_acquire)) {
752 return;
753 }
754
Florian Mayerf7f71e32018-08-31 15:36:48 -0700755 if (!atomic_exchange(&g_heapprofd_init_in_progress, true)) {
756 __libc_globals.mutate([](libc_globals* globals) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800757 atomic_store(&globals->current_dispatch_table, &__heapprofd_dispatch);
Florian Mayerf7f71e32018-08-31 15:36:48 -0700758 });
759 }
760}
761
Christopher Ferris63860cb2015-11-16 17:30:32 -0800762#endif // !LIBC_STATIC
Colin Cross869691c2016-01-29 12:48:18 -0800763
764// =============================================================================
Ryan Savitskiecc37e32018-12-14 15:57:21 +0000765// Platform-internal mallopt variant.
766// =============================================================================
767
768#if !defined(LIBC_STATIC)
Florian Mayerdb59b892018-11-27 17:06:54 +0000769bool MallocDispatchReset() {
770 if (!atomic_exchange(&g_heapprofd_init_in_progress, true)) {
771 __libc_globals.mutate([](libc_globals* globals) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800772 atomic_store(&globals->current_dispatch_table, nullptr);
Florian Mayerdb59b892018-11-27 17:06:54 +0000773 });
774 atomic_store(&g_heapprofd_init_in_progress, false);
775 return true;
776 }
777 errno = EAGAIN;
778 return false;
779}
780
Ryan Savitskiecc37e32018-12-14 15:57:21 +0000781// Marks this process as a profileable zygote child.
782bool HandleInitZygoteChildProfiling() {
783 atomic_store_explicit(&gMallocZygoteChildProfileable, true,
784 memory_order_release);
785
786 // Conditionally start "from startup" profiling.
787 if (CheckLoadHeapprofd()) {
788 // Directly call the signal handler (will correctly guard against
789 // concurrent signal delivery).
790 MaybeInstallInitHeapprofdHook(HEAPPROFD_SIGNAL);
791 }
792 return true;
793}
794
795#else
796
Florian Mayerdb59b892018-11-27 17:06:54 +0000797bool MallocDispatchReset() {
798 return true;
799}
800
Ryan Savitskiecc37e32018-12-14 15:57:21 +0000801bool HandleInitZygoteChildProfiling() {
802 return true;
803}
804
805#endif // !defined(LIBC_STATIC)
806
807bool android_mallopt(int opcode, void* arg, size_t arg_size) {
808 if (opcode == M_INIT_ZYGOTE_CHILD_PROFILING) {
809 if (arg != nullptr || arg_size != 0) {
810 errno = EINVAL;
811 return false;
812 }
813 return HandleInitZygoteChildProfiling();
814 }
Florian Mayerdb59b892018-11-27 17:06:54 +0000815 if (opcode == M_RESET_HOOKS) {
816 if (arg != nullptr || arg_size != 0) {
817 errno = EINVAL;
818 return false;
819 }
820 return MallocDispatchReset();
821 }
Ryan Savitskiecc37e32018-12-14 15:57:21 +0000822
823 errno = ENOTSUP;
824 return false;
825}
826
827// =============================================================================
Colin Cross869691c2016-01-29 12:48:18 -0800828// Exported for use by libmemunreachable.
829// =============================================================================
830
831// Calls callback for every allocation in the anonymous heap mapping
832// [base, base+size). Must be called between malloc_disable and malloc_enable.
833extern "C" int malloc_iterate(uintptr_t base, size_t size,
834 void (*callback)(uintptr_t base, size_t size, void* arg), void* arg) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800835 auto dispatch_table = GetDispatchTable();
836 if (__predict_false(dispatch_table != nullptr)) {
837 return dispatch_table->iterate(base, size, callback, arg);
Colin Cross869691c2016-01-29 12:48:18 -0800838 }
839 return Malloc(iterate)(base, size, callback, arg);
840}
841
842// Disable calls to malloc so malloc_iterate gets a consistent view of
843// allocated memory.
844extern "C" void malloc_disable() {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800845 auto dispatch_table = GetDispatchTable();
846 if (__predict_false(dispatch_table != nullptr)) {
847 return dispatch_table->malloc_disable();
Colin Cross869691c2016-01-29 12:48:18 -0800848 }
849 return Malloc(malloc_disable)();
850}
851
852// Re-enable calls to malloc after a previous call to malloc_disable.
853extern "C" void malloc_enable() {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800854 auto dispatch_table = GetDispatchTable();
855 if (__predict_false(dispatch_table != nullptr)) {
856 return dispatch_table->malloc_enable();
Colin Cross869691c2016-01-29 12:48:18 -0800857 }
858 return Malloc(malloc_enable)();
859}
Colin Cross2d4721c2016-02-02 11:57:54 -0800860
861#ifndef LIBC_STATIC
862extern "C" ssize_t malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count) {
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800863 void* func = g_functions[FUNC_MALLOC_BACKTRACE];
864 if (func == nullptr) {
Colin Cross2d4721c2016-02-02 11:57:54 -0800865 return 0;
866 }
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800867 return reinterpret_cast<malloc_backtrace_func_t>(func)(pointer, frames, frame_count);
Colin Cross2d4721c2016-02-02 11:57:54 -0800868}
869#else
870extern "C" ssize_t malloc_backtrace(void*, uintptr_t*, size_t) {
871 return 0;
872}
873#endif