blob: eec49a4d23dc89e48cb346affce5e760618d6677 [file] [log] [blame]
Christopher Ferrise4cdbc42019-02-08 17:30:58 -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#if defined(LIBC_STATIC)
30#error This file should not be compiled for static targets.
31#endif
32
33// Contains a thin layer that calls whatever real native allocator
34// has been defined. For the libc shared library, this allows the
35// implementation of a debug malloc that can intercept all of the allocation
36// calls and add special debugging code to attempt to catch allocation
37// errors. All of the debugging code is implemented in a separate shared
38// library that is only loaded when the property "libc.debug.malloc.options"
39// is set to a non-zero value. There are three functions exported to
40// allow ddms, or other external users to get information from the debug
41// allocation.
42// get_malloc_leak_info: Returns information about all of the known native
43// allocations that are currently in use.
44// free_malloc_leak_info: Frees the data allocated by the call to
45// get_malloc_leak_info.
46// write_malloc_leak_info: Writes the leak info data to a file.
47
48#include <dlfcn.h>
Christopher Ferris8189e772019-04-09 16:37:23 -070049#include <errno.h>
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080050#include <fcntl.h>
51#include <pthread.h>
52#include <stdatomic.h>
53#include <stdbool.h>
54#include <stdio.h>
55#include <stdlib.h>
56#include <unistd.h>
57
Jiyong Park3ff116a2019-04-02 23:04:52 +090058#include <android/dlext.h>
59
Christopher Ferris2b0638e2019-09-11 19:05:29 -070060#include <platform/bionic/malloc.h>
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080061#include <private/bionic_config.h>
62#include <private/bionic_defs.h>
63#include <private/bionic_malloc_dispatch.h>
64
65#include <sys/system_properties.h>
66
67#include "malloc_common.h"
68#include "malloc_common_dynamic.h"
69#include "malloc_heapprofd.h"
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -080070#include "malloc_limit.h"
71
72// =============================================================================
73// Global variables instantations.
74// =============================================================================
75pthread_mutex_t gGlobalsMutateLock = PTHREAD_MUTEX_INITIALIZER;
76
Christopher Ferris8189e772019-04-09 16:37:23 -070077bool gZygoteChild = false;
78
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -080079_Atomic bool gGlobalsMutating = false;
80// =============================================================================
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080081
82static constexpr MallocDispatch __libc_malloc_default_dispatch
83 __attribute__((unused)) = {
84 Malloc(calloc),
85 Malloc(free),
86 Malloc(mallinfo),
87 Malloc(malloc),
88 Malloc(malloc_usable_size),
89 Malloc(memalign),
90 Malloc(posix_memalign),
91#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
92 Malloc(pvalloc),
93#endif
94 Malloc(realloc),
95#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
96 Malloc(valloc),
97#endif
Christopher Ferris6f517cd2019-11-08 11:28:38 -080098 Malloc(malloc_iterate),
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080099 Malloc(malloc_disable),
100 Malloc(malloc_enable),
101 Malloc(mallopt),
102 Malloc(aligned_alloc),
Christopher Ferris6c619a02019-03-01 17:59:51 -0800103 Malloc(malloc_info),
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800104 };
105
106static constexpr char kHooksSharedLib[] = "libc_malloc_hooks.so";
107static constexpr char kHooksPrefix[] = "hooks";
108static constexpr char kHooksPropertyEnable[] = "libc.debug.hooks.enable";
109static constexpr char kHooksEnvEnable[] = "LIBC_HOOKS_ENABLE";
110
111static constexpr char kDebugSharedLib[] = "libc_malloc_debug.so";
112static constexpr char kDebugPrefix[] = "debug";
113static constexpr char kDebugPropertyOptions[] = "libc.debug.malloc.options";
114static constexpr char kDebugPropertyProgram[] = "libc.debug.malloc.program";
115static constexpr char kDebugEnvOptions[] = "LIBC_DEBUG_MALLOC_OPTIONS";
116
117typedef void (*finalize_func_t)();
Christopher Ferris8189e772019-04-09 16:37:23 -0700118typedef bool (*init_func_t)(const MallocDispatch*, bool*, const char*);
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800119typedef void (*get_malloc_leak_info_func_t)(uint8_t**, size_t*, size_t*, size_t*, size_t*);
120typedef void (*free_malloc_leak_info_func_t)(uint8_t*);
121typedef bool (*write_malloc_leak_info_func_t)(FILE*);
122typedef ssize_t (*malloc_backtrace_func_t)(void*, uintptr_t*, size_t);
123
124enum FunctionEnum : uint8_t {
125 FUNC_INITIALIZE,
126 FUNC_FINALIZE,
127 FUNC_GET_MALLOC_LEAK_INFO,
128 FUNC_FREE_MALLOC_LEAK_INFO,
129 FUNC_MALLOC_BACKTRACE,
130 FUNC_WRITE_LEAK_INFO,
131 FUNC_LAST,
132};
133static void* gFunctions[FUNC_LAST];
134
135extern "C" int __cxa_atexit(void (*func)(void *), void *arg, void *dso);
136
137template<typename FunctionType>
138static bool InitMallocFunction(void* malloc_impl_handler, FunctionType* func, const char* prefix, const char* suffix) {
139 char symbol[128];
140 snprintf(symbol, sizeof(symbol), "%s_%s", prefix, suffix);
141 *func = reinterpret_cast<FunctionType>(dlsym(malloc_impl_handler, symbol));
142 if (*func == nullptr) {
143 error_log("%s: dlsym(\"%s\") failed", getprogname(), symbol);
144 return false;
145 }
146 return true;
147}
148
149static bool InitMallocFunctions(void* impl_handler, MallocDispatch* table, const char* prefix) {
150 if (!InitMallocFunction<MallocFree>(impl_handler, &table->free, prefix, "free")) {
151 return false;
152 }
153 if (!InitMallocFunction<MallocCalloc>(impl_handler, &table->calloc, prefix, "calloc")) {
154 return false;
155 }
156 if (!InitMallocFunction<MallocMallinfo>(impl_handler, &table->mallinfo, prefix, "mallinfo")) {
157 return false;
158 }
159 if (!InitMallocFunction<MallocMallopt>(impl_handler, &table->mallopt, prefix, "mallopt")) {
160 return false;
161 }
162 if (!InitMallocFunction<MallocMalloc>(impl_handler, &table->malloc, prefix, "malloc")) {
163 return false;
164 }
Christopher Ferris6c619a02019-03-01 17:59:51 -0800165 if (!InitMallocFunction<MallocMallocInfo>(impl_handler, &table->malloc_info, prefix,
166 "malloc_info")) {
167 return false;
168 }
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800169 if (!InitMallocFunction<MallocMallocUsableSize>(impl_handler, &table->malloc_usable_size, prefix,
170 "malloc_usable_size")) {
171 return false;
172 }
173 if (!InitMallocFunction<MallocMemalign>(impl_handler, &table->memalign, prefix, "memalign")) {
174 return false;
175 }
176 if (!InitMallocFunction<MallocPosixMemalign>(impl_handler, &table->posix_memalign, prefix,
177 "posix_memalign")) {
178 return false;
179 }
180 if (!InitMallocFunction<MallocAlignedAlloc>(impl_handler, &table->aligned_alloc,
181 prefix, "aligned_alloc")) {
182 return false;
183 }
184 if (!InitMallocFunction<MallocRealloc>(impl_handler, &table->realloc, prefix, "realloc")) {
185 return false;
186 }
Christopher Ferris6f517cd2019-11-08 11:28:38 -0800187 if (!InitMallocFunction<MallocIterate>(impl_handler, &table->malloc_iterate, prefix,
188 "malloc_iterate")) {
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800189 return false;
190 }
191 if (!InitMallocFunction<MallocMallocDisable>(impl_handler, &table->malloc_disable, prefix,
192 "malloc_disable")) {
193 return false;
194 }
195 if (!InitMallocFunction<MallocMallocEnable>(impl_handler, &table->malloc_enable, prefix,
196 "malloc_enable")) {
197 return false;
198 }
199#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
200 if (!InitMallocFunction<MallocPvalloc>(impl_handler, &table->pvalloc, prefix, "pvalloc")) {
201 return false;
202 }
203 if (!InitMallocFunction<MallocValloc>(impl_handler, &table->valloc, prefix, "valloc")) {
204 return false;
205 }
206#endif
207
208 return true;
209}
210
211static void MallocFiniImpl(void*) {
212 // Our BSD stdio implementation doesn't close the standard streams,
213 // it only flushes them. Other unclosed FILE*s will show up as
214 // malloc leaks, but to avoid the standard streams showing up in
215 // leak reports, close them here.
216 fclose(stdin);
217 fclose(stdout);
218 fclose(stderr);
219
220 reinterpret_cast<finalize_func_t>(gFunctions[FUNC_FINALIZE])();
221}
222
223static bool CheckLoadMallocHooks(char** options) {
224 char* env = getenv(kHooksEnvEnable);
225 if ((env == nullptr || env[0] == '\0' || env[0] == '0') &&
226 (__system_property_get(kHooksPropertyEnable, *options) == 0 || *options[0] == '\0' || *options[0] == '0')) {
227 return false;
228 }
229 *options = nullptr;
230 return true;
231}
232
233static bool CheckLoadMallocDebug(char** options) {
234 // If kDebugMallocEnvOptions is set then it overrides the system properties.
235 char* env = getenv(kDebugEnvOptions);
236 if (env == nullptr || env[0] == '\0') {
237 if (__system_property_get(kDebugPropertyOptions, *options) == 0 || *options[0] == '\0') {
238 return false;
239 }
240
241 // Check to see if only a specific program should have debug malloc enabled.
242 char program[PROP_VALUE_MAX];
243 if (__system_property_get(kDebugPropertyProgram, program) != 0 &&
244 strstr(getprogname(), program) == nullptr) {
245 return false;
246 }
247 } else {
248 *options = env;
249 }
250 return true;
251}
252
253static void ClearGlobalFunctions() {
254 for (size_t i = 0; i < FUNC_LAST; i++) {
255 gFunctions[i] = nullptr;
256 }
257}
258
259bool InitSharedLibrary(void* impl_handle, const char* shared_lib, const char* prefix, MallocDispatch* dispatch_table) {
260 static constexpr const char* names[] = {
261 "initialize",
262 "finalize",
263 "get_malloc_leak_info",
264 "free_malloc_leak_info",
265 "malloc_backtrace",
266 "write_malloc_leak_info",
267 };
268 for (size_t i = 0; i < FUNC_LAST; i++) {
269 char symbol[128];
270 snprintf(symbol, sizeof(symbol), "%s_%s", prefix, names[i]);
271 gFunctions[i] = dlsym(impl_handle, symbol);
272 if (gFunctions[i] == nullptr) {
273 error_log("%s: %s routine not found in %s", getprogname(), symbol, shared_lib);
274 ClearGlobalFunctions();
275 return false;
276 }
277 }
278
279 if (!InitMallocFunctions(impl_handle, dispatch_table, prefix)) {
280 ClearGlobalFunctions();
281 return false;
282 }
283 return true;
284}
285
Ytai Ben-tsvi5105ece2019-12-19 19:08:58 +0000286// Note about USE_SCUDO. This file is compiled into libc.so and libc_scudo.so.
287// When compiled into libc_scudo.so, the libc_malloc_* libraries don't need
288// to be loaded from the runtime namespace since libc_scudo.so is not from
289// the runtime APEX, but is copied to any APEX that needs it.
290#ifndef USE_SCUDO
Jiyong Park3ff116a2019-04-02 23:04:52 +0900291extern "C" struct android_namespace_t* android_get_exported_namespace(const char* name);
Ytai Ben-tsvi5105ece2019-12-19 19:08:58 +0000292#endif
Jiyong Park3ff116a2019-04-02 23:04:52 +0900293
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800294void* LoadSharedLibrary(const char* shared_lib, const char* prefix, MallocDispatch* dispatch_table) {
Jiyong Park3ff116a2019-04-02 23:04:52 +0900295 void* impl_handle = nullptr;
Ytai Ben-tsvi5105ece2019-12-19 19:08:58 +0000296#ifndef USE_SCUDO
Jiyong Park3ff116a2019-04-02 23:04:52 +0900297 // Try to load the libc_malloc_* libs from the "runtime" namespace and then
298 // fall back to dlopen() to load them from the default namespace.
299 //
300 // The libraries are packaged in the runtime APEX together with libc.so.
301 // However, since the libc.so is searched via the symlink in the system
302 // partition (/system/lib/libc.so -> /apex/com.android.runtime/bionic.libc.so)
303 // libc.so is loaded into the default namespace. If we just dlopen() here, the
304 // linker will load the libs found in /system/lib which might be incompatible
305 // with libc.so in the runtime APEX. Use android_dlopen_ext to explicitly load
306 // the ones in the runtime APEX.
307 struct android_namespace_t* runtime_ns = android_get_exported_namespace("runtime");
308 if (runtime_ns != nullptr) {
309 const android_dlextinfo dlextinfo = {
310 .flags = ANDROID_DLEXT_USE_NAMESPACE,
311 .library_namespace = runtime_ns,
312 };
313 impl_handle = android_dlopen_ext(shared_lib, RTLD_NOW | RTLD_LOCAL, &dlextinfo);
314 }
Ytai Ben-tsvi5105ece2019-12-19 19:08:58 +0000315#endif
Jiyong Park3ff116a2019-04-02 23:04:52 +0900316
317 if (impl_handle == nullptr) {
318 impl_handle = dlopen(shared_lib, RTLD_NOW | RTLD_LOCAL);
319 }
320
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800321 if (impl_handle == nullptr) {
322 error_log("%s: Unable to open shared library %s: %s", getprogname(), shared_lib, dlerror());
323 return nullptr;
324 }
325
326 if (!InitSharedLibrary(impl_handle, shared_lib, prefix, dispatch_table)) {
327 dlclose(impl_handle);
328 impl_handle = nullptr;
329 }
330
331 return impl_handle;
332}
333
334bool FinishInstallHooks(libc_globals* globals, const char* options, const char* prefix) {
335 init_func_t init_func = reinterpret_cast<init_func_t>(gFunctions[FUNC_INITIALIZE]);
Christopher Ferris8189e772019-04-09 16:37:23 -0700336 if (!init_func(&__libc_malloc_default_dispatch, &gZygoteChild, options)) {
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800337 error_log("%s: failed to enable malloc %s", getprogname(), prefix);
338 ClearGlobalFunctions();
339 return false;
340 }
341
342 // Do a pointer swap so that all of the functions become valid at once to
343 // avoid any initialization order problems.
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -0800344 atomic_store(&globals->default_dispatch_table, &globals->malloc_dispatch_table);
345 if (GetDispatchTable() == nullptr) {
346 atomic_store(&globals->current_dispatch_table, &globals->malloc_dispatch_table);
347 }
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800348
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800349 // Use atexit to trigger the cleanup function. This avoids a problem
350 // where another atexit function is used to cleanup allocated memory,
351 // but the finalize function was already called. This particular error
352 // seems to be triggered by a zygote spawned process calling exit.
353 int ret_value = __cxa_atexit(MallocFiniImpl, nullptr, nullptr);
354 if (ret_value != 0) {
355 // We don't consider this a fatal error.
Christopher Ferrisc328e442019-04-01 19:31:26 -0700356 warning_log("failed to set atexit cleanup function: %d", ret_value);
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800357 }
358 return true;
359}
360
Christopher Ferris28228562019-02-14 10:23:58 -0800361static bool InstallHooks(libc_globals* globals, const char* options, const char* prefix,
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800362 const char* shared_lib) {
363 void* impl_handle = LoadSharedLibrary(shared_lib, prefix, &globals->malloc_dispatch_table);
364 if (impl_handle == nullptr) {
Christopher Ferris28228562019-02-14 10:23:58 -0800365 return false;
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800366 }
367
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800368 if (!FinishInstallHooks(globals, options, prefix)) {
369 dlclose(impl_handle);
Christopher Ferris28228562019-02-14 10:23:58 -0800370 return false;
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800371 }
Christopher Ferris28228562019-02-14 10:23:58 -0800372 return true;
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800373}
374
375// Initializes memory allocation framework once per process.
376static void MallocInitImpl(libc_globals* globals) {
377 char prop[PROP_VALUE_MAX];
378 char* options = prop;
379
380 // Prefer malloc debug since it existed first and is a more complete
381 // malloc interceptor than the hooks.
Christopher Ferris28228562019-02-14 10:23:58 -0800382 bool hook_installed = false;
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800383 if (CheckLoadMallocDebug(&options)) {
Christopher Ferris28228562019-02-14 10:23:58 -0800384 hook_installed = InstallHooks(globals, options, kDebugPrefix, kDebugSharedLib);
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800385 } else if (CheckLoadMallocHooks(&options)) {
Christopher Ferris28228562019-02-14 10:23:58 -0800386 hook_installed = InstallHooks(globals, options, kHooksPrefix, kHooksSharedLib);
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800387 }
388
Christopher Ferris28228562019-02-14 10:23:58 -0800389 if (!hook_installed) {
390 if (HeapprofdShouldLoad()) {
391 HeapprofdInstallHooksAtInit(globals);
392 }
393
394 // Install this last to avoid as many race conditions as possible.
395 HeapprofdInstallSignalHandler();
396 } else {
397 // Install a signal handler that prints an error since we don't support
398 // heapprofd and any other hook to be installed at the same time.
399 HeapprofdInstallErrorSignalHandler();
400 }
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800401}
402
403// Initializes memory allocation framework.
404// This routine is called from __libc_init routines in libc_init_dynamic.cpp.
405__BIONIC_WEAK_FOR_NATIVE_BRIDGE
406__LIBC_HIDDEN__ void __libc_init_malloc(libc_globals* globals) {
407 MallocInitImpl(globals);
408}
409
410// =============================================================================
411// Functions to support dumping of native heap allocations using malloc debug.
412// =============================================================================
Christopher Ferris30659fd2019-04-15 19:01:08 -0700413bool GetMallocLeakInfo(android_mallopt_leak_info_t* leak_info) {
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800414 void* func = gFunctions[FUNC_GET_MALLOC_LEAK_INFO];
415 if (func == nullptr) {
Christopher Ferris30659fd2019-04-15 19:01:08 -0700416 errno = ENOTSUP;
417 return false;
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800418 }
Christopher Ferris30659fd2019-04-15 19:01:08 -0700419 reinterpret_cast<get_malloc_leak_info_func_t>(func)(
420 &leak_info->buffer, &leak_info->overall_size, &leak_info->info_size,
421 &leak_info->total_memory, &leak_info->backtrace_size);
422 return true;
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800423}
424
Christopher Ferris30659fd2019-04-15 19:01:08 -0700425bool FreeMallocLeakInfo(android_mallopt_leak_info_t* leak_info) {
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800426 void* func = gFunctions[FUNC_FREE_MALLOC_LEAK_INFO];
427 if (func == nullptr) {
Christopher Ferris30659fd2019-04-15 19:01:08 -0700428 errno = ENOTSUP;
429 return false;
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800430 }
Christopher Ferris30659fd2019-04-15 19:01:08 -0700431 reinterpret_cast<free_malloc_leak_info_func_t>(func)(leak_info->buffer);
432 return true;
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800433}
434
Christopher Ferris30659fd2019-04-15 19:01:08 -0700435bool WriteMallocLeakInfo(FILE* fp) {
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800436 void* func = gFunctions[FUNC_WRITE_LEAK_INFO];
437 bool written = false;
438 if (func != nullptr) {
439 written = reinterpret_cast<write_malloc_leak_info_func_t>(func)(fp);
440 }
441
442 if (!written) {
443 fprintf(fp, "Native heap dump not available. To enable, run these commands (requires root):\n");
444 fprintf(fp, "# adb shell stop\n");
445 fprintf(fp, "# adb shell setprop libc.debug.malloc.options backtrace\n");
446 fprintf(fp, "# adb shell start\n");
Christopher Ferris30659fd2019-04-15 19:01:08 -0700447 errno = ENOTSUP;
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800448 }
Christopher Ferris30659fd2019-04-15 19:01:08 -0700449 return written;
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800450}
451// =============================================================================
452
453// =============================================================================
454// Exported for use by libmemunreachable.
455// =============================================================================
456extern "C" ssize_t malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count) {
457 void* func = gFunctions[FUNC_MALLOC_BACKTRACE];
458 if (func == nullptr) {
459 return 0;
460 }
461 return reinterpret_cast<malloc_backtrace_func_t>(func)(pointer, frames, frame_count);
462}
463// =============================================================================
464
465// =============================================================================
466// Platform-internal mallopt variant.
467// =============================================================================
Evgeny Eltsinedbc9e22019-12-16 16:37:44 +0100468__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800469extern "C" bool android_mallopt(int opcode, void* arg, size_t arg_size) {
Christopher Ferris8189e772019-04-09 16:37:23 -0700470 if (opcode == M_SET_ZYGOTE_CHILD) {
471 if (arg != nullptr || arg_size != 0) {
472 errno = EINVAL;
473 return false;
474 }
475 gZygoteChild = true;
476 return true;
477 }
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -0800478 if (opcode == M_SET_ALLOCATION_LIMIT_BYTES) {
479 return LimitEnable(arg, arg_size);
480 }
Christopher Ferris30659fd2019-04-15 19:01:08 -0700481 if (opcode == M_WRITE_MALLOC_LEAK_INFO_TO_FILE) {
482 if (arg == nullptr || arg_size != sizeof(FILE*)) {
483 errno = EINVAL;
484 return false;
485 }
486 return WriteMallocLeakInfo(reinterpret_cast<FILE*>(arg));
487 }
488 if (opcode == M_GET_MALLOC_LEAK_INFO) {
489 if (arg == nullptr || arg_size != sizeof(android_mallopt_leak_info_t)) {
490 errno = EINVAL;
491 return false;
492 }
493 return GetMallocLeakInfo(reinterpret_cast<android_mallopt_leak_info_t*>(arg));
494 }
495 if (opcode == M_FREE_MALLOC_LEAK_INFO) {
496 if (arg == nullptr || arg_size != sizeof(android_mallopt_leak_info_t)) {
497 errno = EINVAL;
498 return false;
499 }
500 return FreeMallocLeakInfo(reinterpret_cast<android_mallopt_leak_info_t*>(arg));
501 }
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800502 return HeapprofdMallopt(opcode, arg, arg_size);
503}
504// =============================================================================
Christopher Ferris23c056d2019-05-07 16:02:49 -0700505
506#if !defined(__LP64__) && defined(__arm__)
507// =============================================================================
508// Old platform only functions that some old 32 bit apps are still using.
509// See b/132175052.
510// Only compile the functions for 32 bit arm, so that new apps do not use
511// these functions.
512// =============================================================================
513extern "C" void get_malloc_leak_info(uint8_t** info, size_t* overall_size, size_t* info_size,
514 size_t* total_memory, size_t* backtrace_size) {
515 if (info == nullptr || overall_size == nullptr || info_size == nullptr ||
516 total_memory == nullptr || backtrace_size == nullptr) {
517 return;
518 }
519
520 *info = nullptr;
521 *overall_size = 0;
522 *info_size = 0;
523 *total_memory = 0;
524 *backtrace_size = 0;
525
526 android_mallopt_leak_info_t leak_info = {};
527 if (android_mallopt(M_GET_MALLOC_LEAK_INFO, &leak_info, sizeof(leak_info))) {
528 *info = leak_info.buffer;
529 *overall_size = leak_info.overall_size;
530 *info_size = leak_info.info_size;
531 *total_memory = leak_info.total_memory;
532 *backtrace_size = leak_info.backtrace_size;
533 }
534}
535
536extern "C" void free_malloc_leak_info(uint8_t* info) {
537 android_mallopt_leak_info_t leak_info = { .buffer = info };
538 android_mallopt(M_FREE_MALLOC_LEAK_INFO, &leak_info, sizeof(leak_info));
539}
540// =============================================================================
541#endif