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