blob: a03be508f1707c669e60d866e80e47148e4b2848 [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
Peter Collingbourne1e110fb2020-01-09 10:48:22 -080067#include "heap_tagging.h"
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080068#include "malloc_common.h"
69#include "malloc_common_dynamic.h"
70#include "malloc_heapprofd.h"
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -080071#include "malloc_limit.h"
72
73// =============================================================================
74// Global variables instantations.
75// =============================================================================
76pthread_mutex_t gGlobalsMutateLock = PTHREAD_MUTEX_INITIALIZER;
77
Christopher Ferris8189e772019-04-09 16:37:23 -070078bool gZygoteChild = false;
79
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -080080_Atomic bool gGlobalsMutating = false;
81// =============================================================================
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080082
83static 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
Christopher Ferris6f517cd2019-11-08 11:28:38 -080099 Malloc(malloc_iterate),
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800100 Malloc(malloc_disable),
101 Malloc(malloc_enable),
102 Malloc(mallopt),
103 Malloc(aligned_alloc),
Christopher Ferris6c619a02019-03-01 17:59:51 -0800104 Malloc(malloc_info),
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800105 };
106
107static constexpr char kHooksSharedLib[] = "libc_malloc_hooks.so";
108static constexpr char kHooksPrefix[] = "hooks";
109static constexpr char kHooksPropertyEnable[] = "libc.debug.hooks.enable";
110static constexpr char kHooksEnvEnable[] = "LIBC_HOOKS_ENABLE";
111
112static constexpr char kDebugSharedLib[] = "libc_malloc_debug.so";
113static constexpr char kDebugPrefix[] = "debug";
114static constexpr char kDebugPropertyOptions[] = "libc.debug.malloc.options";
115static constexpr char kDebugPropertyProgram[] = "libc.debug.malloc.program";
116static constexpr char kDebugEnvOptions[] = "LIBC_DEBUG_MALLOC_OPTIONS";
117
118typedef void (*finalize_func_t)();
Christopher Ferris8189e772019-04-09 16:37:23 -0700119typedef bool (*init_func_t)(const MallocDispatch*, bool*, const char*);
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800120typedef void (*get_malloc_leak_info_func_t)(uint8_t**, size_t*, size_t*, size_t*, size_t*);
121typedef void (*free_malloc_leak_info_func_t)(uint8_t*);
122typedef bool (*write_malloc_leak_info_func_t)(FILE*);
123typedef ssize_t (*malloc_backtrace_func_t)(void*, uintptr_t*, size_t);
124
125enum FunctionEnum : uint8_t {
126 FUNC_INITIALIZE,
127 FUNC_FINALIZE,
128 FUNC_GET_MALLOC_LEAK_INFO,
129 FUNC_FREE_MALLOC_LEAK_INFO,
130 FUNC_MALLOC_BACKTRACE,
131 FUNC_WRITE_LEAK_INFO,
132 FUNC_LAST,
133};
134static void* gFunctions[FUNC_LAST];
135
136extern "C" int __cxa_atexit(void (*func)(void *), void *arg, void *dso);
137
138template<typename FunctionType>
139static bool InitMallocFunction(void* malloc_impl_handler, FunctionType* func, const char* prefix, const char* suffix) {
140 char symbol[128];
141 snprintf(symbol, sizeof(symbol), "%s_%s", prefix, suffix);
142 *func = reinterpret_cast<FunctionType>(dlsym(malloc_impl_handler, symbol));
143 if (*func == nullptr) {
144 error_log("%s: dlsym(\"%s\") failed", getprogname(), symbol);
145 return false;
146 }
147 return true;
148}
149
150static bool InitMallocFunctions(void* impl_handler, MallocDispatch* table, const char* prefix) {
151 if (!InitMallocFunction<MallocFree>(impl_handler, &table->free, prefix, "free")) {
152 return false;
153 }
154 if (!InitMallocFunction<MallocCalloc>(impl_handler, &table->calloc, prefix, "calloc")) {
155 return false;
156 }
157 if (!InitMallocFunction<MallocMallinfo>(impl_handler, &table->mallinfo, prefix, "mallinfo")) {
158 return false;
159 }
160 if (!InitMallocFunction<MallocMallopt>(impl_handler, &table->mallopt, prefix, "mallopt")) {
161 return false;
162 }
163 if (!InitMallocFunction<MallocMalloc>(impl_handler, &table->malloc, prefix, "malloc")) {
164 return false;
165 }
Christopher Ferris6c619a02019-03-01 17:59:51 -0800166 if (!InitMallocFunction<MallocMallocInfo>(impl_handler, &table->malloc_info, prefix,
167 "malloc_info")) {
168 return false;
169 }
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800170 if (!InitMallocFunction<MallocMallocUsableSize>(impl_handler, &table->malloc_usable_size, prefix,
171 "malloc_usable_size")) {
172 return false;
173 }
174 if (!InitMallocFunction<MallocMemalign>(impl_handler, &table->memalign, prefix, "memalign")) {
175 return false;
176 }
177 if (!InitMallocFunction<MallocPosixMemalign>(impl_handler, &table->posix_memalign, prefix,
178 "posix_memalign")) {
179 return false;
180 }
181 if (!InitMallocFunction<MallocAlignedAlloc>(impl_handler, &table->aligned_alloc,
182 prefix, "aligned_alloc")) {
183 return false;
184 }
185 if (!InitMallocFunction<MallocRealloc>(impl_handler, &table->realloc, prefix, "realloc")) {
186 return false;
187 }
Christopher Ferris6f517cd2019-11-08 11:28:38 -0800188 if (!InitMallocFunction<MallocIterate>(impl_handler, &table->malloc_iterate, prefix,
189 "malloc_iterate")) {
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800190 return false;
191 }
192 if (!InitMallocFunction<MallocMallocDisable>(impl_handler, &table->malloc_disable, prefix,
193 "malloc_disable")) {
194 return false;
195 }
196 if (!InitMallocFunction<MallocMallocEnable>(impl_handler, &table->malloc_enable, prefix,
197 "malloc_enable")) {
198 return false;
199 }
200#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
201 if (!InitMallocFunction<MallocPvalloc>(impl_handler, &table->pvalloc, prefix, "pvalloc")) {
202 return false;
203 }
204 if (!InitMallocFunction<MallocValloc>(impl_handler, &table->valloc, prefix, "valloc")) {
205 return false;
206 }
207#endif
208
209 return true;
210}
211
212static void MallocFiniImpl(void*) {
213 // Our BSD stdio implementation doesn't close the standard streams,
214 // it only flushes them. Other unclosed FILE*s will show up as
215 // malloc leaks, but to avoid the standard streams showing up in
216 // leak reports, close them here.
217 fclose(stdin);
218 fclose(stdout);
219 fclose(stderr);
220
221 reinterpret_cast<finalize_func_t>(gFunctions[FUNC_FINALIZE])();
222}
223
224static bool CheckLoadMallocHooks(char** options) {
225 char* env = getenv(kHooksEnvEnable);
226 if ((env == nullptr || env[0] == '\0' || env[0] == '0') &&
227 (__system_property_get(kHooksPropertyEnable, *options) == 0 || *options[0] == '\0' || *options[0] == '0')) {
228 return false;
229 }
230 *options = nullptr;
231 return true;
232}
233
234static bool CheckLoadMallocDebug(char** options) {
235 // If kDebugMallocEnvOptions is set then it overrides the system properties.
236 char* env = getenv(kDebugEnvOptions);
237 if (env == nullptr || env[0] == '\0') {
238 if (__system_property_get(kDebugPropertyOptions, *options) == 0 || *options[0] == '\0') {
239 return false;
240 }
241
242 // Check to see if only a specific program should have debug malloc enabled.
243 char program[PROP_VALUE_MAX];
244 if (__system_property_get(kDebugPropertyProgram, program) != 0 &&
245 strstr(getprogname(), program) == nullptr) {
246 return false;
247 }
248 } else {
249 *options = env;
250 }
251 return true;
252}
253
254static void ClearGlobalFunctions() {
255 for (size_t i = 0; i < FUNC_LAST; i++) {
256 gFunctions[i] = nullptr;
257 }
258}
259
260bool InitSharedLibrary(void* impl_handle, const char* shared_lib, const char* prefix, MallocDispatch* dispatch_table) {
261 static constexpr const char* names[] = {
262 "initialize",
263 "finalize",
264 "get_malloc_leak_info",
265 "free_malloc_leak_info",
266 "malloc_backtrace",
267 "write_malloc_leak_info",
268 };
269 for (size_t i = 0; i < FUNC_LAST; i++) {
270 char symbol[128];
271 snprintf(symbol, sizeof(symbol), "%s_%s", prefix, names[i]);
272 gFunctions[i] = dlsym(impl_handle, symbol);
273 if (gFunctions[i] == nullptr) {
274 error_log("%s: %s routine not found in %s", getprogname(), symbol, shared_lib);
275 ClearGlobalFunctions();
276 return false;
277 }
278 }
279
280 if (!InitMallocFunctions(impl_handle, dispatch_table, prefix)) {
281 ClearGlobalFunctions();
282 return false;
283 }
284 return true;
285}
286
Jiyong Park3ff116a2019-04-02 23:04:52 +0900287extern "C" struct android_namespace_t* android_get_exported_namespace(const char* name);
288
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800289void* LoadSharedLibrary(const char* shared_lib, const char* prefix, MallocDispatch* dispatch_table) {
Jiyong Park3ff116a2019-04-02 23:04:52 +0900290 void* impl_handle = nullptr;
291 // Try to load the libc_malloc_* libs from the "runtime" namespace and then
292 // fall back to dlopen() to load them from the default namespace.
293 //
294 // The libraries are packaged in the runtime APEX together with libc.so.
295 // However, since the libc.so is searched via the symlink in the system
296 // partition (/system/lib/libc.so -> /apex/com.android.runtime/bionic.libc.so)
297 // libc.so is loaded into the default namespace. If we just dlopen() here, the
298 // linker will load the libs found in /system/lib which might be incompatible
299 // with libc.so in the runtime APEX. Use android_dlopen_ext to explicitly load
300 // the ones in the runtime APEX.
301 struct android_namespace_t* runtime_ns = android_get_exported_namespace("runtime");
302 if (runtime_ns != nullptr) {
303 const android_dlextinfo dlextinfo = {
304 .flags = ANDROID_DLEXT_USE_NAMESPACE,
305 .library_namespace = runtime_ns,
306 };
307 impl_handle = android_dlopen_ext(shared_lib, RTLD_NOW | RTLD_LOCAL, &dlextinfo);
308 }
309
310 if (impl_handle == nullptr) {
311 impl_handle = dlopen(shared_lib, RTLD_NOW | RTLD_LOCAL);
312 }
313
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800314 if (impl_handle == nullptr) {
315 error_log("%s: Unable to open shared library %s: %s", getprogname(), shared_lib, dlerror());
316 return nullptr;
317 }
318
319 if (!InitSharedLibrary(impl_handle, shared_lib, prefix, dispatch_table)) {
320 dlclose(impl_handle);
321 impl_handle = nullptr;
322 }
323
324 return impl_handle;
325}
326
327bool FinishInstallHooks(libc_globals* globals, const char* options, const char* prefix) {
328 init_func_t init_func = reinterpret_cast<init_func_t>(gFunctions[FUNC_INITIALIZE]);
Christopher Ferris8189e772019-04-09 16:37:23 -0700329 if (!init_func(&__libc_malloc_default_dispatch, &gZygoteChild, options)) {
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800330 error_log("%s: failed to enable malloc %s", getprogname(), prefix);
331 ClearGlobalFunctions();
332 return false;
333 }
334
335 // Do a pointer swap so that all of the functions become valid at once to
336 // avoid any initialization order problems.
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -0800337 atomic_store(&globals->default_dispatch_table, &globals->malloc_dispatch_table);
338 if (GetDispatchTable() == nullptr) {
339 atomic_store(&globals->current_dispatch_table, &globals->malloc_dispatch_table);
340 }
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800341
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800342 // Use atexit to trigger the cleanup function. This avoids a problem
343 // where another atexit function is used to cleanup allocated memory,
344 // but the finalize function was already called. This particular error
345 // seems to be triggered by a zygote spawned process calling exit.
346 int ret_value = __cxa_atexit(MallocFiniImpl, nullptr, nullptr);
347 if (ret_value != 0) {
348 // We don't consider this a fatal error.
Christopher Ferrisc328e442019-04-01 19:31:26 -0700349 warning_log("failed to set atexit cleanup function: %d", ret_value);
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800350 }
351 return true;
352}
353
Christopher Ferris28228562019-02-14 10:23:58 -0800354static bool InstallHooks(libc_globals* globals, const char* options, const char* prefix,
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800355 const char* shared_lib) {
356 void* impl_handle = LoadSharedLibrary(shared_lib, prefix, &globals->malloc_dispatch_table);
357 if (impl_handle == nullptr) {
Christopher Ferris28228562019-02-14 10:23:58 -0800358 return false;
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800359 }
360
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800361 if (!FinishInstallHooks(globals, options, prefix)) {
362 dlclose(impl_handle);
Christopher Ferris28228562019-02-14 10:23:58 -0800363 return false;
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800364 }
Christopher Ferris28228562019-02-14 10:23:58 -0800365 return true;
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800366}
367
368// Initializes memory allocation framework once per process.
369static void MallocInitImpl(libc_globals* globals) {
370 char prop[PROP_VALUE_MAX];
371 char* options = prop;
372
373 // Prefer malloc debug since it existed first and is a more complete
374 // malloc interceptor than the hooks.
Christopher Ferris28228562019-02-14 10:23:58 -0800375 bool hook_installed = false;
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800376 if (CheckLoadMallocDebug(&options)) {
Christopher Ferris28228562019-02-14 10:23:58 -0800377 hook_installed = InstallHooks(globals, options, kDebugPrefix, kDebugSharedLib);
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800378 } else if (CheckLoadMallocHooks(&options)) {
Christopher Ferris28228562019-02-14 10:23:58 -0800379 hook_installed = InstallHooks(globals, options, kHooksPrefix, kHooksSharedLib);
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800380 }
381
Christopher Ferris28228562019-02-14 10:23:58 -0800382 if (!hook_installed) {
383 if (HeapprofdShouldLoad()) {
384 HeapprofdInstallHooksAtInit(globals);
385 }
386
387 // Install this last to avoid as many race conditions as possible.
388 HeapprofdInstallSignalHandler();
389 } else {
390 // Install a signal handler that prints an error since we don't support
391 // heapprofd and any other hook to be installed at the same time.
392 HeapprofdInstallErrorSignalHandler();
393 }
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800394}
395
396// Initializes memory allocation framework.
397// This routine is called from __libc_init routines in libc_init_dynamic.cpp.
398__BIONIC_WEAK_FOR_NATIVE_BRIDGE
399__LIBC_HIDDEN__ void __libc_init_malloc(libc_globals* globals) {
400 MallocInitImpl(globals);
401}
402
403// =============================================================================
404// Functions to support dumping of native heap allocations using malloc debug.
405// =============================================================================
Christopher Ferris30659fd2019-04-15 19:01:08 -0700406bool GetMallocLeakInfo(android_mallopt_leak_info_t* leak_info) {
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800407 void* func = gFunctions[FUNC_GET_MALLOC_LEAK_INFO];
408 if (func == nullptr) {
Christopher Ferris30659fd2019-04-15 19:01:08 -0700409 errno = ENOTSUP;
410 return false;
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800411 }
Christopher Ferris30659fd2019-04-15 19:01:08 -0700412 reinterpret_cast<get_malloc_leak_info_func_t>(func)(
413 &leak_info->buffer, &leak_info->overall_size, &leak_info->info_size,
414 &leak_info->total_memory, &leak_info->backtrace_size);
415 return true;
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800416}
417
Christopher Ferris30659fd2019-04-15 19:01:08 -0700418bool FreeMallocLeakInfo(android_mallopt_leak_info_t* leak_info) {
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800419 void* func = gFunctions[FUNC_FREE_MALLOC_LEAK_INFO];
420 if (func == nullptr) {
Christopher Ferris30659fd2019-04-15 19:01:08 -0700421 errno = ENOTSUP;
422 return false;
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800423 }
Christopher Ferris30659fd2019-04-15 19:01:08 -0700424 reinterpret_cast<free_malloc_leak_info_func_t>(func)(leak_info->buffer);
425 return true;
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800426}
427
Christopher Ferris30659fd2019-04-15 19:01:08 -0700428bool WriteMallocLeakInfo(FILE* fp) {
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800429 void* func = gFunctions[FUNC_WRITE_LEAK_INFO];
430 bool written = false;
431 if (func != nullptr) {
432 written = reinterpret_cast<write_malloc_leak_info_func_t>(func)(fp);
433 }
434
435 if (!written) {
436 fprintf(fp, "Native heap dump not available. To enable, run these commands (requires root):\n");
437 fprintf(fp, "# adb shell stop\n");
438 fprintf(fp, "# adb shell setprop libc.debug.malloc.options backtrace\n");
439 fprintf(fp, "# adb shell start\n");
Christopher Ferris30659fd2019-04-15 19:01:08 -0700440 errno = ENOTSUP;
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800441 }
Christopher Ferris30659fd2019-04-15 19:01:08 -0700442 return written;
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800443}
444// =============================================================================
445
446// =============================================================================
447// Exported for use by libmemunreachable.
448// =============================================================================
449extern "C" ssize_t malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count) {
450 void* func = gFunctions[FUNC_MALLOC_BACKTRACE];
451 if (func == nullptr) {
452 return 0;
453 }
454 return reinterpret_cast<malloc_backtrace_func_t>(func)(pointer, frames, frame_count);
455}
456// =============================================================================
457
458// =============================================================================
459// Platform-internal mallopt variant.
460// =============================================================================
Evgeny Eltsinedbc9e22019-12-16 16:37:44 +0100461__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800462extern "C" bool android_mallopt(int opcode, void* arg, size_t arg_size) {
Christopher Ferris8189e772019-04-09 16:37:23 -0700463 if (opcode == M_SET_ZYGOTE_CHILD) {
464 if (arg != nullptr || arg_size != 0) {
465 errno = EINVAL;
466 return false;
467 }
468 gZygoteChild = true;
469 return true;
470 }
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -0800471 if (opcode == M_SET_ALLOCATION_LIMIT_BYTES) {
472 return LimitEnable(arg, arg_size);
473 }
Christopher Ferris30659fd2019-04-15 19:01:08 -0700474 if (opcode == M_WRITE_MALLOC_LEAK_INFO_TO_FILE) {
475 if (arg == nullptr || arg_size != sizeof(FILE*)) {
476 errno = EINVAL;
477 return false;
478 }
479 return WriteMallocLeakInfo(reinterpret_cast<FILE*>(arg));
480 }
481 if (opcode == M_GET_MALLOC_LEAK_INFO) {
482 if (arg == nullptr || arg_size != sizeof(android_mallopt_leak_info_t)) {
483 errno = EINVAL;
484 return false;
485 }
486 return GetMallocLeakInfo(reinterpret_cast<android_mallopt_leak_info_t*>(arg));
487 }
488 if (opcode == M_FREE_MALLOC_LEAK_INFO) {
489 if (arg == nullptr || arg_size != sizeof(android_mallopt_leak_info_t)) {
490 errno = EINVAL;
491 return false;
492 }
493 return FreeMallocLeakInfo(reinterpret_cast<android_mallopt_leak_info_t*>(arg));
494 }
Peter Collingbourne1e110fb2020-01-09 10:48:22 -0800495 if (opcode == M_SET_HEAP_TAGGING_LEVEL) {
496 return SetHeapTaggingLevel(arg, arg_size);
497 }
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800498 return HeapprofdMallopt(opcode, arg, arg_size);
499}
500// =============================================================================
Christopher Ferris23c056d2019-05-07 16:02:49 -0700501
502#if !defined(__LP64__) && defined(__arm__)
503// =============================================================================
504// Old platform only functions that some old 32 bit apps are still using.
505// See b/132175052.
506// Only compile the functions for 32 bit arm, so that new apps do not use
507// these functions.
508// =============================================================================
509extern "C" void get_malloc_leak_info(uint8_t** info, size_t* overall_size, size_t* info_size,
510 size_t* total_memory, size_t* backtrace_size) {
511 if (info == nullptr || overall_size == nullptr || info_size == nullptr ||
512 total_memory == nullptr || backtrace_size == nullptr) {
513 return;
514 }
515
516 *info = nullptr;
517 *overall_size = 0;
518 *info_size = 0;
519 *total_memory = 0;
520 *backtrace_size = 0;
521
522 android_mallopt_leak_info_t leak_info = {};
523 if (android_mallopt(M_GET_MALLOC_LEAK_INFO, &leak_info, sizeof(leak_info))) {
524 *info = leak_info.buffer;
525 *overall_size = leak_info.overall_size;
526 *info_size = leak_info.info_size;
527 *total_memory = leak_info.total_memory;
528 *backtrace_size = leak_info.backtrace_size;
529 }
530}
531
532extern "C" void free_malloc_leak_info(uint8_t* info) {
533 android_mallopt_leak_info_t leak_info = { .buffer = info };
534 android_mallopt(M_FREE_MALLOC_LEAK_INFO, &leak_info, sizeof(leak_info));
535}
536// =============================================================================
537#endif