1 /*
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>
49 #include <errno.h>
50 #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 
58 #include <android/dlext.h>
59 
60 #include <platform/bionic/malloc.h>
61 #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 "gwp_asan_wrappers.h"
68 #include "heap_tagging.h"
69 #include "malloc_common.h"
70 #include "malloc_common_dynamic.h"
71 #include "malloc_heapprofd.h"
72 #include "malloc_limit.h"
73 
74 // =============================================================================
75 // Global variables instantations.
76 // =============================================================================
77 pthread_mutex_t gGlobalsMutateLock = PTHREAD_MUTEX_INITIALIZER;
78 
79 _Atomic bool gGlobalsMutating = false;
80 
81 static bool gZygoteChild = false;
82 
83 // In a Zygote child process, this is set to true if profiling of this process
84 // is allowed. Note that this is set at a later time than gZygoteChild. The
85 // latter is set during the fork (while still in zygote's SELinux domain). While
86 // this bit is set after the child is specialized (and has transferred SELinux
87 // domains if applicable). These two flags are read by the
88 // BIONIC_SIGNAL_PROFILER handler, which does nothing if the process is not
89 // profileable.
90 static _Atomic bool gZygoteChildProfileable = false;
91 
92 // =============================================================================
93 
94 static constexpr char kHooksSharedLib[] = "libc_malloc_hooks.so";
95 static constexpr char kHooksPrefix[] = "hooks";
96 static constexpr char kHooksPropertyEnable[] = "libc.debug.hooks.enable";
97 static constexpr char kHooksEnvEnable[] = "LIBC_HOOKS_ENABLE";
98 
99 static constexpr char kDebugSharedLib[] = "libc_malloc_debug.so";
100 static constexpr char kDebugPrefix[] = "debug";
101 static constexpr char kDebugPropertyOptions[] = "libc.debug.malloc.options";
102 static constexpr char kDebugPropertyProgram[] = "libc.debug.malloc.program";
103 static constexpr char kDebugEnvOptions[] = "LIBC_DEBUG_MALLOC_OPTIONS";
104 
105 typedef void (*finalize_func_t)();
106 typedef bool (*init_func_t)(const MallocDispatch*, bool*, const char*);
107 typedef void (*get_malloc_leak_info_func_t)(uint8_t**, size_t*, size_t*, size_t*, size_t*);
108 typedef void (*free_malloc_leak_info_func_t)(uint8_t*);
109 typedef bool (*write_malloc_leak_info_func_t)(FILE*);
110 typedef ssize_t (*malloc_backtrace_func_t)(void*, uintptr_t*, size_t);
111 
112 enum FunctionEnum : uint8_t {
113   FUNC_INITIALIZE,
114   FUNC_FINALIZE,
115   FUNC_GET_MALLOC_LEAK_INFO,
116   FUNC_FREE_MALLOC_LEAK_INFO,
117   FUNC_MALLOC_BACKTRACE,
118   FUNC_WRITE_LEAK_INFO,
119   FUNC_LAST,
120 };
121 static void* gFunctions[FUNC_LAST];
122 
123 extern "C" int __cxa_atexit(void (*func)(void *), void *arg, void *dso);
124 
125 template<typename FunctionType>
InitMallocFunction(void * malloc_impl_handler,FunctionType * func,const char * prefix,const char * suffix)126 static bool InitMallocFunction(void* malloc_impl_handler, FunctionType* func, const char* prefix, const char* suffix) {
127   char symbol[128];
128   snprintf(symbol, sizeof(symbol), "%s_%s", prefix, suffix);
129   *func = reinterpret_cast<FunctionType>(dlsym(malloc_impl_handler, symbol));
130   if (*func == nullptr) {
131     error_log("%s: dlsym(\"%s\") failed", getprogname(), symbol);
132     return false;
133   }
134   return true;
135 }
136 
InitMallocFunctions(void * impl_handler,MallocDispatch * table,const char * prefix)137 static bool InitMallocFunctions(void* impl_handler, MallocDispatch* table, const char* prefix) {
138   if (!InitMallocFunction<MallocFree>(impl_handler, &table->free, prefix, "free")) {
139     return false;
140   }
141   if (!InitMallocFunction<MallocCalloc>(impl_handler, &table->calloc, prefix, "calloc")) {
142     return false;
143   }
144   if (!InitMallocFunction<MallocMallinfo>(impl_handler, &table->mallinfo, prefix, "mallinfo")) {
145     return false;
146   }
147   if (!InitMallocFunction<MallocMallopt>(impl_handler, &table->mallopt, prefix, "mallopt")) {
148     return false;
149   }
150   if (!InitMallocFunction<MallocMalloc>(impl_handler, &table->malloc, prefix, "malloc")) {
151     return false;
152   }
153   if (!InitMallocFunction<MallocMallocInfo>(impl_handler, &table->malloc_info, prefix,
154                                                 "malloc_info")) {
155     return false;
156   }
157   if (!InitMallocFunction<MallocMallocUsableSize>(impl_handler, &table->malloc_usable_size, prefix,
158                                                   "malloc_usable_size")) {
159     return false;
160   }
161   if (!InitMallocFunction<MallocMemalign>(impl_handler, &table->memalign, prefix, "memalign")) {
162     return false;
163   }
164   if (!InitMallocFunction<MallocPosixMemalign>(impl_handler, &table->posix_memalign, prefix,
165                                                "posix_memalign")) {
166     return false;
167   }
168   if (!InitMallocFunction<MallocAlignedAlloc>(impl_handler, &table->aligned_alloc,
169                                               prefix, "aligned_alloc")) {
170     return false;
171   }
172   if (!InitMallocFunction<MallocRealloc>(impl_handler, &table->realloc, prefix, "realloc")) {
173     return false;
174   }
175   if (!InitMallocFunction<MallocIterate>(impl_handler, &table->malloc_iterate, prefix,
176                                          "malloc_iterate")) {
177     return false;
178   }
179   if (!InitMallocFunction<MallocMallocDisable>(impl_handler, &table->malloc_disable, prefix,
180                                                "malloc_disable")) {
181     return false;
182   }
183   if (!InitMallocFunction<MallocMallocEnable>(impl_handler, &table->malloc_enable, prefix,
184                                               "malloc_enable")) {
185     return false;
186   }
187 #if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
188   if (!InitMallocFunction<MallocPvalloc>(impl_handler, &table->pvalloc, prefix, "pvalloc")) {
189     return false;
190   }
191   if (!InitMallocFunction<MallocValloc>(impl_handler, &table->valloc, prefix, "valloc")) {
192     return false;
193   }
194 #endif
195 
196   return true;
197 }
198 
MallocFiniImpl(void *)199 static void MallocFiniImpl(void*) {
200   // Our BSD stdio implementation doesn't close the standard streams,
201   // it only flushes them. Other unclosed FILE*s will show up as
202   // malloc leaks, but to avoid the standard streams showing up in
203   // leak reports, close them here.
204   fclose(stdin);
205   fclose(stdout);
206   fclose(stderr);
207 
208   reinterpret_cast<finalize_func_t>(gFunctions[FUNC_FINALIZE])();
209 }
210 
CheckLoadMallocHooks(char ** options)211 static bool CheckLoadMallocHooks(char** options) {
212   char* env = getenv(kHooksEnvEnable);
213   if ((env == nullptr || env[0] == '\0' || env[0] == '0') &&
214     (__system_property_get(kHooksPropertyEnable, *options) == 0 || *options[0] == '\0' || *options[0] == '0')) {
215     return false;
216   }
217   *options = nullptr;
218   return true;
219 }
220 
CheckLoadMallocDebug(char ** options)221 static bool CheckLoadMallocDebug(char** options) {
222   // If kDebugMallocEnvOptions is set then it overrides the system properties.
223   char* env = getenv(kDebugEnvOptions);
224   if (env == nullptr || env[0] == '\0') {
225     if (__system_property_get(kDebugPropertyOptions, *options) == 0 || *options[0] == '\0') {
226       return false;
227     }
228 
229     // Check to see if only a specific program should have debug malloc enabled.
230     char program[PROP_VALUE_MAX];
231     if (__system_property_get(kDebugPropertyProgram, program) != 0 &&
232         strstr(getprogname(), program) == nullptr) {
233       return false;
234     }
235   } else {
236     *options = env;
237   }
238   return true;
239 }
240 
SetGlobalFunctions(void * functions[])241 void SetGlobalFunctions(void* functions[]) {
242   for (size_t i = 0; i < FUNC_LAST; i++) {
243     gFunctions[i] = functions[i];
244   }
245 }
246 
ClearGlobalFunctions()247 static void ClearGlobalFunctions() {
248   for (size_t i = 0; i < FUNC_LAST; i++) {
249     gFunctions[i] = nullptr;
250   }
251 }
252 
InitSharedLibrary(void * impl_handle,const char * shared_lib,const char * prefix,MallocDispatch * dispatch_table)253 bool InitSharedLibrary(void* impl_handle, const char* shared_lib, const char* prefix, MallocDispatch* dispatch_table) {
254   static constexpr const char* names[] = {
255     "initialize",
256     "finalize",
257     "get_malloc_leak_info",
258     "free_malloc_leak_info",
259     "malloc_backtrace",
260     "write_malloc_leak_info",
261   };
262   for (size_t i = 0; i < FUNC_LAST; i++) {
263     char symbol[128];
264     snprintf(symbol, sizeof(symbol), "%s_%s", prefix, names[i]);
265     gFunctions[i] = dlsym(impl_handle, symbol);
266     if (gFunctions[i] == nullptr) {
267       error_log("%s: %s routine not found in %s", getprogname(), symbol, shared_lib);
268       ClearGlobalFunctions();
269       return false;
270     }
271   }
272 
273   if (!InitMallocFunctions(impl_handle, dispatch_table, prefix)) {
274     ClearGlobalFunctions();
275     return false;
276   }
277   return true;
278 }
279 
280 extern "C" struct android_namespace_t* android_get_exported_namespace(const char* name);
281 
LoadSharedLibrary(const char * shared_lib,const char * prefix,MallocDispatch * dispatch_table)282 void* LoadSharedLibrary(const char* shared_lib, const char* prefix, MallocDispatch* dispatch_table) {
283   void* impl_handle = nullptr;
284   // Try to load the libc_malloc_* libs from the "runtime" namespace and then
285   // fall back to dlopen() to load them from the default namespace.
286   //
287   // The libraries are packaged in the runtime APEX together with libc.so.
288   // However, since the libc.so is searched via the symlink in the system
289   // partition (/system/lib/libc.so -> /apex/com.android.runtime/bionic/libc.so)
290   // libc.so is loaded into the default namespace. If we just dlopen() here, the
291   // linker will load the libs found in /system/lib which might be incompatible
292   // with libc.so in the runtime APEX. Use android_dlopen_ext to explicitly load
293   // the ones in the runtime APEX.
294   struct android_namespace_t* runtime_ns = android_get_exported_namespace("com_android_runtime");
295   if (runtime_ns != nullptr) {
296     const android_dlextinfo dlextinfo = {
297       .flags = ANDROID_DLEXT_USE_NAMESPACE,
298       .library_namespace = runtime_ns,
299     };
300     impl_handle = android_dlopen_ext(shared_lib, RTLD_NOW | RTLD_LOCAL, &dlextinfo);
301   }
302 
303   if (impl_handle == nullptr) {
304     impl_handle = dlopen(shared_lib, RTLD_NOW | RTLD_LOCAL);
305   }
306 
307   if (impl_handle == nullptr) {
308     error_log("%s: Unable to open shared library %s: %s", getprogname(), shared_lib, dlerror());
309     return nullptr;
310   }
311 
312   if (!InitSharedLibrary(impl_handle, shared_lib, prefix, dispatch_table)) {
313     dlclose(impl_handle);
314     impl_handle = nullptr;
315   }
316 
317   return impl_handle;
318 }
319 
FinishInstallHooks(libc_globals * globals,const char * options,const char * prefix)320 bool FinishInstallHooks(libc_globals* globals, const char* options, const char* prefix) {
321   init_func_t init_func = reinterpret_cast<init_func_t>(gFunctions[FUNC_INITIALIZE]);
322 
323   // If GWP-ASan was initialised, we should use it as the dispatch table for
324   // heapprofd/malloc_debug/malloc_debug.
325   const MallocDispatch* prev_dispatch = GetDefaultDispatchTable();
326   if (prev_dispatch == nullptr) {
327     prev_dispatch = NativeAllocatorDispatch();
328   }
329 
330   if (!init_func(prev_dispatch, &gZygoteChild, options)) {
331     error_log("%s: failed to enable malloc %s", getprogname(), prefix);
332     ClearGlobalFunctions();
333     return false;
334   }
335 
336   // Do a pointer swap so that all of the functions become valid at once to
337   // avoid any initialization order problems.
338   atomic_store(&globals->default_dispatch_table, &globals->malloc_dispatch_table);
339   if (!MallocLimitInstalled()) {
340     atomic_store(&globals->current_dispatch_table, &globals->malloc_dispatch_table);
341   }
342 
343   // Use atexit to trigger the cleanup function. This avoids a problem
344   // where another atexit function is used to cleanup allocated memory,
345   // but the finalize function was already called. This particular error
346   // seems to be triggered by a zygote spawned process calling exit.
347   int ret_value = __cxa_atexit(MallocFiniImpl, nullptr, nullptr);
348   if (ret_value != 0) {
349     // We don't consider this a fatal error.
350     warning_log("failed to set atexit cleanup function: %d", ret_value);
351   }
352   return true;
353 }
354 
InstallHooks(libc_globals * globals,const char * options,const char * prefix,const char * shared_lib)355 static bool InstallHooks(libc_globals* globals, const char* options, const char* prefix,
356                          const char* shared_lib) {
357   void* impl_handle = LoadSharedLibrary(shared_lib, prefix, &globals->malloc_dispatch_table);
358   if (impl_handle == nullptr) {
359     return false;
360   }
361 
362   if (!FinishInstallHooks(globals, options, prefix)) {
363     dlclose(impl_handle);
364     return false;
365   }
366   return true;
367 }
368 
369 extern "C" const char* __scudo_get_stack_depot_addr();
370 extern "C" const char* __scudo_get_region_info_addr();
371 
372 // Initializes memory allocation framework once per process.
MallocInitImpl(libc_globals * globals)373 static void MallocInitImpl(libc_globals* globals) {
374   char prop[PROP_VALUE_MAX];
375   char* options = prop;
376 
377   MaybeInitGwpAsanFromLibc(globals);
378 
379 #if defined(USE_SCUDO)
380   __libc_shared_globals()->scudo_stack_depot = __scudo_get_stack_depot_addr();
381   __libc_shared_globals()->scudo_region_info = __scudo_get_region_info_addr();
382 #endif
383 
384   // Prefer malloc debug since it existed first and is a more complete
385   // malloc interceptor than the hooks.
386   bool hook_installed = false;
387   if (CheckLoadMallocDebug(&options)) {
388     hook_installed = InstallHooks(globals, options, kDebugPrefix, kDebugSharedLib);
389   } else if (CheckLoadMallocHooks(&options)) {
390     hook_installed = InstallHooks(globals, options, kHooksPrefix, kHooksSharedLib);
391   }
392 
393   if (!hook_installed) {
394     if (HeapprofdShouldLoad()) {
395       HeapprofdInstallHooksAtInit(globals);
396     }
397   } else {
398     // Record the fact that incompatible hooks are active, to skip any later
399     // heapprofd signal handler invocations.
400     HeapprofdRememberHookConflict();
401   }
402 }
403 
404 // Initializes memory allocation framework.
405 // This routine is called from __libc_init routines in libc_init_dynamic.cpp.
406 __BIONIC_WEAK_FOR_NATIVE_BRIDGE
__libc_init_malloc(libc_globals * globals)407 __LIBC_HIDDEN__ void __libc_init_malloc(libc_globals* globals) {
408   MallocInitImpl(globals);
409 }
410 
411 // =============================================================================
412 // Functions to support dumping of native heap allocations using malloc debug.
413 // =============================================================================
GetMallocLeakInfo(android_mallopt_leak_info_t * leak_info)414 bool GetMallocLeakInfo(android_mallopt_leak_info_t* leak_info) {
415   void* func = gFunctions[FUNC_GET_MALLOC_LEAK_INFO];
416   if (func == nullptr) {
417     errno = ENOTSUP;
418     return false;
419   }
420   reinterpret_cast<get_malloc_leak_info_func_t>(func)(
421       &leak_info->buffer, &leak_info->overall_size, &leak_info->info_size,
422       &leak_info->total_memory, &leak_info->backtrace_size);
423   return true;
424 }
425 
FreeMallocLeakInfo(android_mallopt_leak_info_t * leak_info)426 bool FreeMallocLeakInfo(android_mallopt_leak_info_t* leak_info) {
427   void* func = gFunctions[FUNC_FREE_MALLOC_LEAK_INFO];
428   if (func == nullptr) {
429     errno = ENOTSUP;
430     return false;
431   }
432   reinterpret_cast<free_malloc_leak_info_func_t>(func)(leak_info->buffer);
433   return true;
434 }
435 
WriteMallocLeakInfo(FILE * fp)436 bool WriteMallocLeakInfo(FILE* fp) {
437   void* func = gFunctions[FUNC_WRITE_LEAK_INFO];
438   bool written = false;
439   if (func != nullptr) {
440     written = reinterpret_cast<write_malloc_leak_info_func_t>(func)(fp);
441   }
442 
443   if (!written) {
444     fprintf(fp, "Native heap dump not available. To enable, run these commands (requires root):\n");
445     fprintf(fp, "# adb shell stop\n");
446     fprintf(fp, "# adb shell setprop libc.debug.malloc.options backtrace\n");
447     fprintf(fp, "# adb shell start\n");
448     errno = ENOTSUP;
449   }
450   return written;
451 }
452 // =============================================================================
453 
454 // =============================================================================
455 // Exported for use by libmemunreachable.
456 // =============================================================================
malloc_backtrace(void * pointer,uintptr_t * frames,size_t frame_count)457 extern "C" ssize_t malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count) {
458   void* func = gFunctions[FUNC_MALLOC_BACKTRACE];
459   if (func == nullptr) {
460     return 0;
461   }
462   return reinterpret_cast<malloc_backtrace_func_t>(func)(pointer, frames, frame_count);
463 }
464 // =============================================================================
465 
466 // =============================================================================
467 // Platform-internal mallopt variant.
468 // =============================================================================
469 __BIONIC_WEAK_FOR_NATIVE_BRIDGE
android_mallopt(int opcode,void * arg,size_t arg_size)470 extern "C" bool android_mallopt(int opcode, void* arg, size_t arg_size) {
471   if (opcode == M_SET_ZYGOTE_CHILD) {
472     if (arg != nullptr || arg_size != 0) {
473       errno = EINVAL;
474       return false;
475     }
476     gZygoteChild = true;
477     return true;
478   }
479   if (opcode == M_INIT_ZYGOTE_CHILD_PROFILING) {
480     if (arg != nullptr || arg_size != 0) {
481       errno = EINVAL;
482       return false;
483     }
484     atomic_store_explicit(&gZygoteChildProfileable, true, memory_order_release);
485     // Also check if heapprofd should start profiling from app startup.
486     HeapprofdInitZygoteChildProfiling();
487     return true;
488   }
489   if (opcode == M_GET_PROCESS_PROFILEABLE) {
490     if (arg == nullptr || arg_size != sizeof(bool)) {
491       errno = EINVAL;
492       return false;
493     }
494     // Native processes are considered profileable. Zygote children are considered
495     // profileable only when appropriately tagged.
496     *reinterpret_cast<bool*>(arg) =
497         !gZygoteChild || atomic_load_explicit(&gZygoteChildProfileable, memory_order_acquire);
498     return true;
499   }
500   if (opcode == M_SET_ALLOCATION_LIMIT_BYTES) {
501     return LimitEnable(arg, arg_size);
502   }
503   if (opcode == M_WRITE_MALLOC_LEAK_INFO_TO_FILE) {
504     if (arg == nullptr || arg_size != sizeof(FILE*)) {
505       errno = EINVAL;
506       return false;
507     }
508     return WriteMallocLeakInfo(reinterpret_cast<FILE*>(arg));
509   }
510   if (opcode == M_GET_MALLOC_LEAK_INFO) {
511     if (arg == nullptr || arg_size != sizeof(android_mallopt_leak_info_t)) {
512       errno = EINVAL;
513       return false;
514     }
515     return GetMallocLeakInfo(reinterpret_cast<android_mallopt_leak_info_t*>(arg));
516   }
517   if (opcode == M_FREE_MALLOC_LEAK_INFO) {
518     if (arg == nullptr || arg_size != sizeof(android_mallopt_leak_info_t)) {
519       errno = EINVAL;
520       return false;
521     }
522     return FreeMallocLeakInfo(reinterpret_cast<android_mallopt_leak_info_t*>(arg));
523   }
524   if (opcode == M_SET_HEAP_TAGGING_LEVEL) {
525     return SetHeapTaggingLevel(arg, arg_size);
526   }
527   if (opcode == M_INITIALIZE_GWP_ASAN) {
528     if (arg == nullptr || arg_size != sizeof(bool)) {
529       errno = EINVAL;
530       return false;
531     }
532     __libc_globals.mutate([&](libc_globals* globals) {
533       return MaybeInitGwpAsan(globals, *reinterpret_cast<bool*>(arg));
534     });
535   }
536   // Try heapprofd's mallopt, as it handles options not covered here.
537   return HeapprofdMallopt(opcode, arg, arg_size);
538 }
539 // =============================================================================
540 
541 #if !defined(__LP64__) && defined(__arm__)
542 // =============================================================================
543 // Old platform only functions that some old 32 bit apps are still using.
544 // See b/132175052.
545 // Only compile the functions for 32 bit arm, so that new apps do not use
546 // these functions.
547 // =============================================================================
get_malloc_leak_info(uint8_t ** info,size_t * overall_size,size_t * info_size,size_t * total_memory,size_t * backtrace_size)548 extern "C" void get_malloc_leak_info(uint8_t** info, size_t* overall_size, size_t* info_size,
549                                      size_t* total_memory, size_t* backtrace_size) {
550   if (info == nullptr || overall_size == nullptr || info_size == nullptr ||
551       total_memory == nullptr || backtrace_size == nullptr) {
552     return;
553   }
554 
555   *info = nullptr;
556   *overall_size = 0;
557   *info_size = 0;
558   *total_memory = 0;
559   *backtrace_size = 0;
560 
561   android_mallopt_leak_info_t leak_info = {};
562   if (android_mallopt(M_GET_MALLOC_LEAK_INFO, &leak_info, sizeof(leak_info))) {
563     *info = leak_info.buffer;
564     *overall_size = leak_info.overall_size;
565     *info_size = leak_info.info_size;
566     *total_memory = leak_info.total_memory;
567     *backtrace_size = leak_info.backtrace_size;
568   }
569 }
570 
free_malloc_leak_info(uint8_t * info)571 extern "C" void free_malloc_leak_info(uint8_t* info) {
572   android_mallopt_leak_info_t leak_info = { .buffer = info };
573   android_mallopt(M_FREE_MALLOC_LEAK_INFO, &leak_info, sizeof(leak_info));
574 }
575 // =============================================================================
576 #endif
577