Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -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 | // Contains a thin layer that calls whatever real native allocator |
| 30 | // has been defined. For the libc shared library, this allows the |
| 31 | // implementation of a debug malloc that can intercept all of the allocation |
| 32 | // calls and add special debugging code to attempt to catch allocation |
| 33 | // errors. All of the debugging code is implemented in a separate shared |
| 34 | // library that is only loaded when the property "libc.debug.malloc.options" |
| 35 | // is set to a non-zero value. There are two functions exported to |
| 36 | // allow ddms, or other external users to get information from the debug |
| 37 | // allocation. |
| 38 | // get_malloc_leak_info: Returns information about all of the known native |
| 39 | // allocations that are currently in use. |
| 40 | // free_malloc_leak_info: Frees the data allocated by the call to |
| 41 | // get_malloc_leak_info. |
Christopher Ferris | 2e1a40a | 2018-06-13 10:46:34 -0700 | [diff] [blame] | 42 | // write_malloc_leak_info: Writes the leak info data to a file. |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 43 | |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 44 | #include <pthread.h> |
Florian Mayer | f7f71e3 | 2018-08-31 15:36:48 -0700 | [diff] [blame] | 45 | #include <stdatomic.h> |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 46 | |
dimitry | 5332af6 | 2018-12-04 14:03:13 +0100 | [diff] [blame] | 47 | #include <private/bionic_defs.h> |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 48 | #include <private/bionic_config.h> |
| 49 | #include <private/bionic_globals.h> |
Ryan Savitski | ecc37e3 | 2018-12-14 15:57:21 +0000 | [diff] [blame] | 50 | #include <private/bionic_malloc.h> |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 51 | #include <private/bionic_malloc_dispatch.h> |
| 52 | |
Evgenii Stepanov | be551f5 | 2018-08-13 16:46:15 -0700 | [diff] [blame] | 53 | #if __has_feature(hwaddress_sanitizer) |
| 54 | // FIXME: implement these in HWASan allocator. |
| 55 | extern "C" int __sanitizer_iterate(uintptr_t base __unused, size_t size __unused, |
| 56 | void (*callback)(uintptr_t base, size_t size, void* arg) __unused, |
| 57 | void* arg __unused) { |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | extern "C" void __sanitizer_malloc_disable() { |
| 62 | } |
| 63 | |
| 64 | extern "C" void __sanitizer_malloc_enable() { |
| 65 | } |
| 66 | #include <sanitizer/hwasan_interface.h> |
| 67 | #define Malloc(function) __sanitizer_ ## function |
| 68 | |
| 69 | #else // __has_feature(hwaddress_sanitizer) |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 70 | #include "jemalloc.h" |
| 71 | #define Malloc(function) je_ ## function |
Evgenii Stepanov | be551f5 | 2018-08-13 16:46:15 -0700 | [diff] [blame] | 72 | #endif |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 73 | |
Florian Mayer | f7f71e3 | 2018-08-31 15:36:48 -0700 | [diff] [blame] | 74 | template <typename T> |
| 75 | static T* RemoveConst(const T* x) { |
| 76 | return const_cast<T*>(x); |
| 77 | } |
| 78 | |
| 79 | // RemoveConst is a workaround for bug in current libcxx. Fix in |
| 80 | // https://reviews.llvm.org/D47613 |
| 81 | #define atomic_load_explicit_const(obj, order) atomic_load_explicit(RemoveConst(obj), order) |
| 82 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 83 | static constexpr MallocDispatch __libc_malloc_default_dispatch |
| 84 | __attribute__((unused)) = { |
| 85 | Malloc(calloc), |
| 86 | Malloc(free), |
| 87 | Malloc(mallinfo), |
| 88 | Malloc(malloc), |
| 89 | Malloc(malloc_usable_size), |
| 90 | Malloc(memalign), |
| 91 | Malloc(posix_memalign), |
| 92 | #if defined(HAVE_DEPRECATED_MALLOC_FUNCS) |
| 93 | Malloc(pvalloc), |
| 94 | #endif |
| 95 | Malloc(realloc), |
| 96 | #if defined(HAVE_DEPRECATED_MALLOC_FUNCS) |
| 97 | Malloc(valloc), |
| 98 | #endif |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 99 | Malloc(iterate), |
| 100 | Malloc(malloc_disable), |
| 101 | Malloc(malloc_enable), |
Christopher Ferris | a1c0d2f | 2017-05-15 15:50:19 -0700 | [diff] [blame] | 102 | Malloc(mallopt), |
Christopher Ferris | cae21a9 | 2018-02-05 18:14:55 -0800 | [diff] [blame] | 103 | Malloc(aligned_alloc), |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 104 | }; |
| 105 | |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 106 | // Malloc hooks. |
| 107 | void* (*volatile __malloc_hook)(size_t, const void*); |
| 108 | void* (*volatile __realloc_hook)(void*, size_t, const void*); |
| 109 | void (*volatile __free_hook)(void*, const void*); |
| 110 | void* (*volatile __memalign_hook)(size_t, size_t, const void*); |
| 111 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 112 | // In a VM process, this is set to 1 after fork()ing out of zygote. |
| 113 | int gMallocLeakZygoteChild = 0; |
| 114 | |
| 115 | // ============================================================================= |
| 116 | // Allocation functions |
| 117 | // ============================================================================= |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame^] | 118 | static inline const MallocDispatch* GetDispatchTable() { |
| 119 | return atomic_load_explicit_const(&__libc_globals->current_dispatch_table, |
| 120 | memory_order_acquire); |
| 121 | } |
| 122 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 123 | extern "C" void* calloc(size_t n_elements, size_t elem_size) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame^] | 124 | auto dispatch_table = GetDispatchTable(); |
| 125 | if (__predict_false(dispatch_table != nullptr)) { |
| 126 | return dispatch_table->calloc(n_elements, elem_size); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 127 | } |
| 128 | return Malloc(calloc)(n_elements, elem_size); |
| 129 | } |
| 130 | |
| 131 | extern "C" void free(void* mem) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame^] | 132 | auto dispatch_table = GetDispatchTable(); |
| 133 | if (__predict_false(dispatch_table != nullptr)) { |
| 134 | dispatch_table->free(mem); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 135 | } else { |
| 136 | Malloc(free)(mem); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | extern "C" struct mallinfo mallinfo() { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame^] | 141 | auto dispatch_table = GetDispatchTable(); |
| 142 | if (__predict_false(dispatch_table != nullptr)) { |
| 143 | return dispatch_table->mallinfo(); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 144 | } |
| 145 | return Malloc(mallinfo)(); |
| 146 | } |
| 147 | |
Christopher Ferris | a1c0d2f | 2017-05-15 15:50:19 -0700 | [diff] [blame] | 148 | extern "C" int mallopt(int param, int value) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame^] | 149 | auto dispatch_table = GetDispatchTable(); |
| 150 | if (__predict_false(dispatch_table != nullptr)) { |
| 151 | return dispatch_table->mallopt(param, value); |
Christopher Ferris | a1c0d2f | 2017-05-15 15:50:19 -0700 | [diff] [blame] | 152 | } |
| 153 | return Malloc(mallopt)(param, value); |
| 154 | } |
| 155 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 156 | extern "C" void* malloc(size_t bytes) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame^] | 157 | auto dispatch_table = GetDispatchTable(); |
| 158 | if (__predict_false(dispatch_table != nullptr)) { |
| 159 | return dispatch_table->malloc(bytes); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 160 | } |
| 161 | return Malloc(malloc)(bytes); |
| 162 | } |
| 163 | |
| 164 | extern "C" size_t malloc_usable_size(const void* mem) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame^] | 165 | auto dispatch_table = GetDispatchTable(); |
| 166 | if (__predict_false(dispatch_table != nullptr)) { |
| 167 | return dispatch_table->malloc_usable_size(mem); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 168 | } |
| 169 | return Malloc(malloc_usable_size)(mem); |
| 170 | } |
| 171 | |
| 172 | extern "C" void* memalign(size_t alignment, size_t bytes) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame^] | 173 | auto dispatch_table = GetDispatchTable(); |
| 174 | if (__predict_false(dispatch_table != nullptr)) { |
| 175 | return dispatch_table->memalign(alignment, bytes); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 176 | } |
| 177 | return Malloc(memalign)(alignment, bytes); |
| 178 | } |
| 179 | |
| 180 | extern "C" int posix_memalign(void** memptr, size_t alignment, size_t size) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame^] | 181 | auto dispatch_table = GetDispatchTable(); |
| 182 | if (__predict_false(dispatch_table != nullptr)) { |
| 183 | return dispatch_table->posix_memalign(memptr, alignment, size); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 184 | } |
| 185 | return Malloc(posix_memalign)(memptr, alignment, size); |
| 186 | } |
| 187 | |
Christopher Ferris | cae21a9 | 2018-02-05 18:14:55 -0800 | [diff] [blame] | 188 | extern "C" void* aligned_alloc(size_t alignment, size_t size) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame^] | 189 | auto dispatch_table = GetDispatchTable(); |
| 190 | if (__predict_false(dispatch_table != nullptr)) { |
| 191 | return dispatch_table->aligned_alloc(alignment, size); |
Christopher Ferris | cae21a9 | 2018-02-05 18:14:55 -0800 | [diff] [blame] | 192 | } |
| 193 | return Malloc(aligned_alloc)(alignment, size); |
| 194 | } |
| 195 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 196 | extern "C" void* realloc(void* old_mem, size_t bytes) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame^] | 197 | auto dispatch_table = GetDispatchTable(); |
| 198 | if (__predict_false(dispatch_table != nullptr)) { |
| 199 | return dispatch_table->realloc(old_mem, bytes); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 200 | } |
| 201 | return Malloc(realloc)(old_mem, bytes); |
| 202 | } |
| 203 | |
Elliott Hughes | b177085 | 2018-09-18 12:52:42 -0700 | [diff] [blame] | 204 | extern "C" void* reallocarray(void* old_mem, size_t item_count, size_t item_size) { |
| 205 | size_t new_size; |
| 206 | if (__builtin_mul_overflow(item_count, item_size, &new_size)) { |
| 207 | errno = ENOMEM; |
| 208 | return nullptr; |
| 209 | } |
| 210 | return realloc(old_mem, new_size); |
| 211 | } |
| 212 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 213 | #if defined(HAVE_DEPRECATED_MALLOC_FUNCS) |
| 214 | extern "C" void* pvalloc(size_t bytes) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame^] | 215 | auto dispatch_table = GetDispatchTable(); |
| 216 | if (__predict_false(dispatch_table != nullptr)) { |
| 217 | return dispatch_table->pvalloc(bytes); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 218 | } |
| 219 | return Malloc(pvalloc)(bytes); |
| 220 | } |
| 221 | |
| 222 | extern "C" void* valloc(size_t bytes) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame^] | 223 | auto dispatch_table = GetDispatchTable(); |
| 224 | if (__predict_false(dispatch_table != nullptr)) { |
| 225 | return dispatch_table->valloc(bytes); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 226 | } |
| 227 | return Malloc(valloc)(bytes); |
| 228 | } |
| 229 | #endif |
| 230 | |
| 231 | // We implement malloc debugging only in libc.so, so the code below |
| 232 | // must be excluded if we compile this file for static libc.a |
| 233 | #if !defined(LIBC_STATIC) |
| 234 | |
| 235 | #include <dlfcn.h> |
Florian Mayer | 4e28ea1 | 2018-11-22 17:34:34 +0000 | [diff] [blame] | 236 | #include <fcntl.h> |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 237 | #include <stdio.h> |
| 238 | #include <stdlib.h> |
Florian Mayer | 4e28ea1 | 2018-11-22 17:34:34 +0000 | [diff] [blame] | 239 | #include <unistd.h> |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 240 | |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 241 | #include <async_safe/log.h> |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 242 | #include <sys/system_properties.h> |
| 243 | |
| 244 | extern "C" int __cxa_atexit(void (*func)(void *), void *arg, void *dso); |
| 245 | |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 246 | static const char* HOOKS_SHARED_LIB = "libc_malloc_hooks.so"; |
| 247 | static const char* HOOKS_PROPERTY_ENABLE = "libc.debug.hooks.enable"; |
| 248 | static const char* HOOKS_ENV_ENABLE = "LIBC_HOOKS_ENABLE"; |
| 249 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 250 | static const char* DEBUG_SHARED_LIB = "libc_malloc_debug.so"; |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 251 | static const char* DEBUG_PROPERTY_OPTIONS = "libc.debug.malloc.options"; |
| 252 | static const char* DEBUG_PROPERTY_PROGRAM = "libc.debug.malloc.program"; |
| 253 | static const char* DEBUG_ENV_OPTIONS = "LIBC_DEBUG_MALLOC_OPTIONS"; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 254 | |
Florian Mayer | f7f71e3 | 2018-08-31 15:36:48 -0700 | [diff] [blame] | 255 | static const char* HEAPPROFD_SHARED_LIB = "heapprofd_client.so"; |
| 256 | static const char* HEAPPROFD_PREFIX = "heapprofd"; |
Florian Mayer | 0dbe6d1 | 2018-11-08 11:25:49 +0000 | [diff] [blame] | 257 | static const char* HEAPPROFD_PROPERTY_ENABLE = "heapprofd.enable"; |
Florian Mayer | f7f71e3 | 2018-08-31 15:36:48 -0700 | [diff] [blame] | 258 | static const int HEAPPROFD_SIGNAL = __SIGRTMIN + 4; |
| 259 | |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 260 | enum FunctionEnum : uint8_t { |
| 261 | FUNC_INITIALIZE, |
| 262 | FUNC_FINALIZE, |
| 263 | FUNC_GET_MALLOC_LEAK_INFO, |
| 264 | FUNC_FREE_MALLOC_LEAK_INFO, |
| 265 | FUNC_MALLOC_BACKTRACE, |
Christopher Ferris | 2e1a40a | 2018-06-13 10:46:34 -0700 | [diff] [blame] | 266 | FUNC_WRITE_LEAK_INFO, |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 267 | FUNC_LAST, |
| 268 | }; |
| 269 | static void* g_functions[FUNC_LAST]; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 270 | |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 271 | typedef void (*finalize_func_t)(); |
| 272 | typedef bool (*init_func_t)(const MallocDispatch*, int*, const char*); |
| 273 | typedef void (*get_malloc_leak_info_func_t)(uint8_t**, size_t*, size_t*, size_t*, size_t*); |
| 274 | typedef void (*free_malloc_leak_info_func_t)(uint8_t*); |
Christopher Ferris | 2e1a40a | 2018-06-13 10:46:34 -0700 | [diff] [blame] | 275 | typedef bool (*write_malloc_leak_info_func_t)(FILE*); |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 276 | typedef ssize_t (*malloc_backtrace_func_t)(void*, uintptr_t*, size_t); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 277 | |
| 278 | // ============================================================================= |
| 279 | // Log functions |
| 280 | // ============================================================================= |
| 281 | #define error_log(format, ...) \ |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 282 | async_safe_format_log(ANDROID_LOG_ERROR, "libc", (format), ##__VA_ARGS__ ) |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 283 | #define info_log(format, ...) \ |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 284 | async_safe_format_log(ANDROID_LOG_INFO, "libc", (format), ##__VA_ARGS__ ) |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 285 | // ============================================================================= |
| 286 | |
Ryan Savitski | ecc37e3 | 2018-12-14 15:57:21 +0000 | [diff] [blame] | 287 | // In a Zygote child process, this is set to true if profiling of this process |
| 288 | // is allowed. Note that this set at a later time than the above |
| 289 | // gMallocLeakZygoteChild. The latter is set during the fork (while still in |
| 290 | // zygote's SELinux domain). While this bit is set after the child is |
| 291 | // specialized (and has transferred SELinux domains if applicable). |
| 292 | static _Atomic bool gMallocZygoteChildProfileable = false; |
| 293 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 294 | // ============================================================================= |
| 295 | // Exported for use by ddms. |
| 296 | // ============================================================================= |
| 297 | |
| 298 | // Retrieve native heap information. |
| 299 | // |
| 300 | // "*info" is set to a buffer we allocate |
| 301 | // "*overall_size" is set to the size of the "info" buffer |
| 302 | // "*info_size" is set to the size of a single entry |
| 303 | // "*total_memory" is set to the sum of all allocations we're tracking; does |
| 304 | // not include heap overhead |
| 305 | // "*backtrace_size" is set to the maximum number of entries in the back trace |
| 306 | extern "C" void get_malloc_leak_info(uint8_t** info, size_t* overall_size, |
| 307 | size_t* info_size, size_t* total_memory, size_t* backtrace_size) { |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 308 | void* func = g_functions[FUNC_GET_MALLOC_LEAK_INFO]; |
| 309 | if (func == nullptr) { |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 310 | return; |
| 311 | } |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 312 | reinterpret_cast<get_malloc_leak_info_func_t>(func)(info, overall_size, info_size, total_memory, |
| 313 | backtrace_size); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | extern "C" void free_malloc_leak_info(uint8_t* info) { |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 317 | void* func = g_functions[FUNC_FREE_MALLOC_LEAK_INFO]; |
| 318 | if (func == nullptr) { |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 319 | return; |
| 320 | } |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 321 | reinterpret_cast<free_malloc_leak_info_func_t>(func)(info); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 322 | } |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 323 | |
Christopher Ferris | 2e1a40a | 2018-06-13 10:46:34 -0700 | [diff] [blame] | 324 | extern "C" void write_malloc_leak_info(FILE* fp) { |
| 325 | if (fp == nullptr) { |
| 326 | error_log("write_malloc_leak_info called with a nullptr"); |
| 327 | return; |
| 328 | } |
| 329 | |
| 330 | void* func = g_functions[FUNC_WRITE_LEAK_INFO]; |
| 331 | bool written = false; |
| 332 | if (func != nullptr) { |
| 333 | written = reinterpret_cast<write_malloc_leak_info_func_t>(func)(fp); |
| 334 | } |
| 335 | |
| 336 | if (!written) { |
| 337 | fprintf(fp, "Native heap dump not available. To enable, run these commands (requires root):\n"); |
| 338 | fprintf(fp, "# adb shell stop\n"); |
| 339 | fprintf(fp, "# adb shell setprop libc.debug.malloc.options backtrace\n"); |
| 340 | fprintf(fp, "# adb shell start\n"); |
| 341 | } |
| 342 | } |
| 343 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 344 | // ============================================================================= |
| 345 | |
| 346 | template<typename FunctionType> |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame^] | 347 | static bool InitMallocFunction(void* malloc_impl_handler, FunctionType* func, const char* prefix, const char* suffix) { |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 348 | char symbol[128]; |
| 349 | snprintf(symbol, sizeof(symbol), "%s_%s", prefix, suffix); |
| 350 | *func = reinterpret_cast<FunctionType>(dlsym(malloc_impl_handler, symbol)); |
| 351 | if (*func == nullptr) { |
| 352 | error_log("%s: dlsym(\"%s\") failed", getprogname(), symbol); |
| 353 | return false; |
| 354 | } |
| 355 | return true; |
| 356 | } |
| 357 | |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 358 | static bool InitMallocFunctions(void* impl_handler, MallocDispatch* table, const char* prefix) { |
Florian Mayer | f7f71e3 | 2018-08-31 15:36:48 -0700 | [diff] [blame] | 359 | if (!InitMallocFunction<MallocFree>(impl_handler, &table->free, prefix, "free")) { |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 360 | return false; |
| 361 | } |
Florian Mayer | f7f71e3 | 2018-08-31 15:36:48 -0700 | [diff] [blame] | 362 | if (!InitMallocFunction<MallocCalloc>(impl_handler, &table->calloc, prefix, "calloc")) { |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 363 | return false; |
| 364 | } |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 365 | if (!InitMallocFunction<MallocMallinfo>(impl_handler, &table->mallinfo, prefix, "mallinfo")) { |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 366 | return false; |
| 367 | } |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 368 | if (!InitMallocFunction<MallocMallopt>(impl_handler, &table->mallopt, prefix, "mallopt")) { |
Christopher Ferris | a1c0d2f | 2017-05-15 15:50:19 -0700 | [diff] [blame] | 369 | return false; |
| 370 | } |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 371 | if (!InitMallocFunction<MallocMalloc>(impl_handler, &table->malloc, prefix, "malloc")) { |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 372 | return false; |
| 373 | } |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 374 | if (!InitMallocFunction<MallocMallocUsableSize>(impl_handler, &table->malloc_usable_size, prefix, |
| 375 | "malloc_usable_size")) { |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 376 | return false; |
| 377 | } |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 378 | if (!InitMallocFunction<MallocMemalign>(impl_handler, &table->memalign, prefix, "memalign")) { |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 379 | return false; |
| 380 | } |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 381 | if (!InitMallocFunction<MallocPosixMemalign>(impl_handler, &table->posix_memalign, prefix, |
| 382 | "posix_memalign")) { |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 383 | return false; |
| 384 | } |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 385 | if (!InitMallocFunction<MallocAlignedAlloc>(impl_handler, &table->aligned_alloc, |
Christopher Ferris | cae21a9 | 2018-02-05 18:14:55 -0800 | [diff] [blame] | 386 | prefix, "aligned_alloc")) { |
| 387 | return false; |
| 388 | } |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 389 | if (!InitMallocFunction<MallocRealloc>(impl_handler, &table->realloc, prefix, "realloc")) { |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 390 | return false; |
| 391 | } |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 392 | if (!InitMallocFunction<MallocIterate>(impl_handler, &table->iterate, prefix, "iterate")) { |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 393 | return false; |
| 394 | } |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 395 | if (!InitMallocFunction<MallocMallocDisable>(impl_handler, &table->malloc_disable, prefix, |
| 396 | "malloc_disable")) { |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 397 | return false; |
| 398 | } |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 399 | if (!InitMallocFunction<MallocMallocEnable>(impl_handler, &table->malloc_enable, prefix, |
| 400 | "malloc_enable")) { |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 401 | return false; |
| 402 | } |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 403 | #if defined(HAVE_DEPRECATED_MALLOC_FUNCS) |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 404 | if (!InitMallocFunction<MallocPvalloc>(impl_handler, &table->pvalloc, prefix, "pvalloc")) { |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 405 | return false; |
| 406 | } |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 407 | if (!InitMallocFunction<MallocValloc>(impl_handler, &table->valloc, prefix, "valloc")) { |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 408 | return false; |
| 409 | } |
| 410 | #endif |
| 411 | |
| 412 | return true; |
| 413 | } |
| 414 | |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame^] | 415 | static void MallocFiniImpl(void*) { |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 416 | // Our BSD stdio implementation doesn't close the standard streams, |
| 417 | // it only flushes them. Other unclosed FILE*s will show up as |
| 418 | // malloc leaks, but to avoid the standard streams showing up in |
| 419 | // leak reports, close them here. |
| 420 | fclose(stdin); |
| 421 | fclose(stdout); |
| 422 | fclose(stderr); |
| 423 | |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 424 | reinterpret_cast<finalize_func_t>(g_functions[FUNC_FINALIZE])(); |
| 425 | } |
| 426 | |
| 427 | static bool CheckLoadMallocHooks(char** options) { |
| 428 | char* env = getenv(HOOKS_ENV_ENABLE); |
| 429 | if ((env == nullptr || env[0] == '\0' || env[0] == '0') && |
| 430 | (__system_property_get(HOOKS_PROPERTY_ENABLE, *options) == 0 || *options[0] == '\0' || *options[0] == '0')) { |
| 431 | return false; |
| 432 | } |
| 433 | *options = nullptr; |
| 434 | return true; |
| 435 | } |
| 436 | |
| 437 | static bool CheckLoadMallocDebug(char** options) { |
| 438 | // If DEBUG_MALLOC_ENV_OPTIONS is set then it overrides the system properties. |
| 439 | char* env = getenv(DEBUG_ENV_OPTIONS); |
| 440 | if (env == nullptr || env[0] == '\0') { |
| 441 | if (__system_property_get(DEBUG_PROPERTY_OPTIONS, *options) == 0 || *options[0] == '\0') { |
| 442 | return false; |
| 443 | } |
| 444 | |
| 445 | // Check to see if only a specific program should have debug malloc enabled. |
| 446 | char program[PROP_VALUE_MAX]; |
| 447 | if (__system_property_get(DEBUG_PROPERTY_PROGRAM, program) != 0 && |
| 448 | strstr(getprogname(), program) == nullptr) { |
| 449 | return false; |
| 450 | } |
| 451 | } else { |
| 452 | *options = env; |
| 453 | } |
| 454 | return true; |
| 455 | } |
| 456 | |
Florian Mayer | 4e28ea1 | 2018-11-22 17:34:34 +0000 | [diff] [blame] | 457 | static bool GetHeapprofdProgramProperty(char* data, size_t size) { |
| 458 | constexpr char prefix[] = "heapprofd.enable."; |
| 459 | // - 1 to skip nullbyte, which we will write later. |
| 460 | constexpr size_t prefix_size = sizeof(prefix) - 1; |
| 461 | if (size < prefix_size) { |
| 462 | error_log("%s: Overflow constructing heapprofd property", getprogname()); |
| 463 | return false; |
| 464 | } |
| 465 | memcpy(data, prefix, prefix_size); |
| 466 | |
| 467 | int fd = open("/proc/self/cmdline", O_RDONLY | O_CLOEXEC); |
| 468 | if (fd == -1) { |
| 469 | error_log("%s: Failed to open /proc/self/cmdline", getprogname()); |
| 470 | return false; |
| 471 | } |
| 472 | char cmdline[128]; |
| 473 | ssize_t rd = read(fd, cmdline, sizeof(cmdline) - 1); |
| 474 | close(fd); |
| 475 | if (rd == -1) { |
| 476 | error_log("%s: Failed to read /proc/self/cmdline", getprogname()); |
| 477 | return false; |
| 478 | } |
| 479 | cmdline[rd] = '\0'; |
| 480 | char* first_arg = static_cast<char*>(memchr(cmdline, '\0', rd)); |
| 481 | if (first_arg == nullptr || first_arg == cmdline + size - 1) { |
| 482 | error_log("%s: Overflow reading cmdline", getprogname()); |
| 483 | return false; |
| 484 | } |
| 485 | // For consistency with what we do with Java app cmdlines, trim everything |
| 486 | // after the @ sign of the first arg. |
| 487 | char* first_at = static_cast<char*>(memchr(cmdline, '@', rd)); |
| 488 | if (first_at != nullptr && first_at < first_arg) { |
| 489 | *first_at = '\0'; |
| 490 | first_arg = first_at; |
| 491 | } |
| 492 | |
| 493 | char* start = static_cast<char*>(memrchr(cmdline, '/', first_arg - cmdline)); |
| 494 | if (start == first_arg) { |
| 495 | // The first argument ended in a slash. |
| 496 | error_log("%s: cmdline ends in /", getprogname()); |
| 497 | return false; |
| 498 | } else if (start == nullptr) { |
| 499 | start = cmdline; |
| 500 | } else { |
| 501 | // Skip the /. |
| 502 | start++; |
| 503 | } |
| 504 | |
| 505 | size_t name_size = static_cast<size_t>(first_arg - start); |
| 506 | if (name_size >= size - prefix_size) { |
| 507 | error_log("%s: overflow constructing heapprofd property.", getprogname()); |
| 508 | return false; |
| 509 | } |
| 510 | // + 1 to also copy the trailing null byte. |
| 511 | memcpy(data + prefix_size, start, name_size + 1); |
| 512 | return true; |
| 513 | } |
| 514 | |
Florian Mayer | 0dbe6d1 | 2018-11-08 11:25:49 +0000 | [diff] [blame] | 515 | static bool CheckLoadHeapprofd() { |
| 516 | // First check for heapprofd.enable. If it is set to "all", enable |
| 517 | // heapprofd for all processes. Otherwise, check heapprofd.enable.${prog}, |
| 518 | // if it is set and not 0, enable heap profiling for this process. |
| 519 | char property_value[PROP_VALUE_MAX]; |
| 520 | if (__system_property_get(HEAPPROFD_PROPERTY_ENABLE, property_value) == 0) { |
| 521 | return false; |
| 522 | } |
| 523 | if (strcmp(property_value, "all") == 0) { |
| 524 | return true; |
| 525 | } |
| 526 | |
| 527 | char program_property[128]; |
Florian Mayer | 4e28ea1 | 2018-11-22 17:34:34 +0000 | [diff] [blame] | 528 | if (!GetHeapprofdProgramProperty(program_property, |
| 529 | sizeof(program_property))) { |
Florian Mayer | 0dbe6d1 | 2018-11-08 11:25:49 +0000 | [diff] [blame] | 530 | return false; |
| 531 | } |
Florian Mayer | 0dbe6d1 | 2018-11-08 11:25:49 +0000 | [diff] [blame] | 532 | if (__system_property_get(program_property, property_value) == 0) { |
| 533 | return false; |
| 534 | } |
Florian Mayer | 0dbe6d1 | 2018-11-08 11:25:49 +0000 | [diff] [blame] | 535 | return program_property[0] != '\0'; |
| 536 | } |
| 537 | |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 538 | static void ClearGlobalFunctions() { |
| 539 | for (size_t i = 0; i < FUNC_LAST; i++) { |
| 540 | g_functions[i] = nullptr; |
| 541 | } |
| 542 | } |
| 543 | |
Florian Mayer | db59b89 | 2018-11-27 17:06:54 +0000 | [diff] [blame] | 544 | static bool InitSharedLibrary(void* impl_handle, const char* shared_lib, const char* prefix, MallocDispatch* dispatch_table) { |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 545 | static constexpr const char* names[] = { |
| 546 | "initialize", |
| 547 | "finalize", |
| 548 | "get_malloc_leak_info", |
| 549 | "free_malloc_leak_info", |
| 550 | "malloc_backtrace", |
Christopher Ferris | 2e1a40a | 2018-06-13 10:46:34 -0700 | [diff] [blame] | 551 | "write_malloc_leak_info", |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 552 | }; |
| 553 | for (size_t i = 0; i < FUNC_LAST; i++) { |
| 554 | char symbol[128]; |
| 555 | snprintf(symbol, sizeof(symbol), "%s_%s", prefix, names[i]); |
| 556 | g_functions[i] = dlsym(impl_handle, symbol); |
| 557 | if (g_functions[i] == nullptr) { |
| 558 | error_log("%s: %s routine not found in %s", getprogname(), symbol, shared_lib); |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 559 | ClearGlobalFunctions(); |
Florian Mayer | db59b89 | 2018-11-27 17:06:54 +0000 | [diff] [blame] | 560 | return false; |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 561 | } |
| 562 | } |
| 563 | |
| 564 | if (!InitMallocFunctions(impl_handle, dispatch_table, prefix)) { |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 565 | ClearGlobalFunctions(); |
Florian Mayer | db59b89 | 2018-11-27 17:06:54 +0000 | [diff] [blame] | 566 | return false; |
| 567 | } |
| 568 | return true; |
| 569 | } |
| 570 | |
| 571 | static void* LoadSharedLibrary(const char* shared_lib, const char* prefix, MallocDispatch* dispatch_table) { |
| 572 | void* impl_handle = dlopen(shared_lib, RTLD_NOW | RTLD_LOCAL); |
| 573 | if (impl_handle == nullptr) { |
| 574 | error_log("%s: Unable to open shared library %s: %s", getprogname(), shared_lib, dlerror()); |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 575 | return nullptr; |
| 576 | } |
| 577 | |
Florian Mayer | db59b89 | 2018-11-27 17:06:54 +0000 | [diff] [blame] | 578 | if (!InitSharedLibrary(impl_handle, shared_lib, prefix, dispatch_table)) { |
| 579 | dlclose(impl_handle); |
| 580 | impl_handle = nullptr; |
| 581 | } |
| 582 | |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 583 | return impl_handle; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 584 | } |
| 585 | |
Florian Mayer | f671e03 | 2019-01-28 18:32:14 +0000 | [diff] [blame] | 586 | // The handle returned by dlopen when previously loading the heapprofd |
| 587 | // hooks. nullptr if they had not been loaded before. |
Florian Mayer | db59b89 | 2018-11-27 17:06:54 +0000 | [diff] [blame] | 588 | static _Atomic (void*) g_heapprofd_handle = nullptr; |
Florian Mayer | 176a475 | 2018-10-23 11:48:34 +0100 | [diff] [blame] | 589 | |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame^] | 590 | static void InstallHooks(libc_globals* globals, const char* options, |
Florian Mayer | f7f71e3 | 2018-08-31 15:36:48 -0700 | [diff] [blame] | 591 | const char* prefix, const char* shared_lib) { |
Florian Mayer | db59b89 | 2018-11-27 17:06:54 +0000 | [diff] [blame] | 592 | void* impl_handle = atomic_load(&g_heapprofd_handle); |
Florian Mayer | f671e03 | 2019-01-28 18:32:14 +0000 | [diff] [blame] | 593 | bool reusing_handle = impl_handle != nullptr; |
| 594 | if (reusing_handle) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame^] | 595 | if (!InitSharedLibrary(impl_handle, shared_lib, prefix, &globals->malloc_dispatch_table)) { |
Florian Mayer | db59b89 | 2018-11-27 17:06:54 +0000 | [diff] [blame] | 596 | return; |
| 597 | } |
| 598 | } else { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame^] | 599 | impl_handle = LoadSharedLibrary(shared_lib, prefix, &globals->malloc_dispatch_table); |
Florian Mayer | db59b89 | 2018-11-27 17:06:54 +0000 | [diff] [blame] | 600 | if (impl_handle == nullptr) { |
| 601 | return; |
| 602 | } |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 603 | } |
Florian Mayer | db59b89 | 2018-11-27 17:06:54 +0000 | [diff] [blame] | 604 | init_func_t init_func = reinterpret_cast<init_func_t>(g_functions[FUNC_INITIALIZE]); |
Tamas Berghammer | ac81fe8 | 2016-08-26 15:54:59 +0100 | [diff] [blame] | 605 | if (!init_func(&__libc_malloc_default_dispatch, &gMallocLeakZygoteChild, options)) { |
Florian Mayer | db59b89 | 2018-11-27 17:06:54 +0000 | [diff] [blame] | 606 | error_log("%s: failed to enable malloc %s", getprogname(), prefix); |
Florian Mayer | f671e03 | 2019-01-28 18:32:14 +0000 | [diff] [blame] | 607 | if (!reusing_handle) { |
| 608 | // We should not close if we are re-using an old handle, as we cannot be |
| 609 | // sure other threads are not currently in the hooks. |
| 610 | dlclose(impl_handle); |
| 611 | } |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 612 | ClearGlobalFunctions(); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 613 | return; |
| 614 | } |
| 615 | |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame^] | 616 | // Do a pointer swap so that all of the functions become valid at once to |
| 617 | // avoid any initialization order problems. |
| 618 | atomic_store(&globals->current_dispatch_table, &globals->malloc_dispatch_table); |
| 619 | |
Florian Mayer | db59b89 | 2018-11-27 17:06:54 +0000 | [diff] [blame] | 620 | atomic_store(&g_heapprofd_handle, impl_handle); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 621 | |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 622 | info_log("%s: malloc %s enabled", getprogname(), prefix); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 623 | |
| 624 | // Use atexit to trigger the cleanup function. This avoids a problem |
| 625 | // where another atexit function is used to cleanup allocated memory, |
| 626 | // but the finalize function was already called. This particular error |
| 627 | // seems to be triggered by a zygote spawned process calling exit. |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame^] | 628 | int ret_value = __cxa_atexit(MallocFiniImpl, nullptr, nullptr); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 629 | if (ret_value != 0) { |
| 630 | error_log("failed to set atexit cleanup function: %d", ret_value); |
| 631 | } |
| 632 | } |
| 633 | |
Ryan Savitski | ecc37e3 | 2018-12-14 15:57:21 +0000 | [diff] [blame] | 634 | // The logic for triggering heapprofd (at runtime) is as follows: |
| 635 | // 1. HEAPPROFD_SIGNAL is received by the process, entering the |
| 636 | // MaybeInstallInitHeapprofdHook signal handler. |
| 637 | // 2. If the initialization is not already in flight |
| 638 | // (g_heapprofd_init_in_progress is false), the malloc hook is set to |
| 639 | // point at InitHeapprofdHook, and g_heapprofd_init_in_progress is set to |
| 640 | // true. |
| 641 | // 3. The next malloc call enters InitHeapprofdHook, which removes the malloc |
| 642 | // hook, and spawns a detached pthread to run the InitHeapprofd task. |
| 643 | // (g_heapprofd_init_hook_installed atomic is used to perform this once.) |
| 644 | // 4. InitHeapprofd, on a dedicated pthread, loads the heapprofd client library, |
| 645 | // installs the full set of heapprofd hooks, and invokes the client's |
| 646 | // initializer. The dedicated pthread then terminates. |
Florian Mayer | 176a475 | 2018-10-23 11:48:34 +0100 | [diff] [blame] | 647 | // 5. g_heapprofd_init_in_progress and g_heapprofd_init_hook_installed are |
Ryan Savitski | ecc37e3 | 2018-12-14 15:57:21 +0000 | [diff] [blame] | 648 | // reset to false such that heapprofd can be reinitialized. Reinitialization |
| 649 | // means that a new profiling session is started, and any still active is |
Florian Mayer | 176a475 | 2018-10-23 11:48:34 +0100 | [diff] [blame] | 650 | // torn down. |
Florian Mayer | f7f71e3 | 2018-08-31 15:36:48 -0700 | [diff] [blame] | 651 | // |
Ryan Savitski | ecc37e3 | 2018-12-14 15:57:21 +0000 | [diff] [blame] | 652 | // The incremental hooking and a dedicated task thread are used since we cannot |
| 653 | // do heavy work within a signal handler, or when blocking a malloc invocation. |
Florian Mayer | f7f71e3 | 2018-08-31 15:36:48 -0700 | [diff] [blame] | 654 | |
| 655 | static _Atomic bool g_heapprofd_init_in_progress = false; |
Florian Mayer | 176a475 | 2018-10-23 11:48:34 +0100 | [diff] [blame] | 656 | static _Atomic bool g_heapprofd_init_hook_installed = false; |
Florian Mayer | f7f71e3 | 2018-08-31 15:36:48 -0700 | [diff] [blame] | 657 | |
Ryan Savitski | ecc37e3 | 2018-12-14 15:57:21 +0000 | [diff] [blame] | 658 | extern "C" void MaybeInstallInitHeapprofdHook(int); |
Florian Mayer | 3a538a4 | 2018-12-20 11:23:50 +0000 | [diff] [blame] | 659 | |
| 660 | // Initializes memory allocation framework once per process. |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame^] | 661 | static void MallocInitImpl(libc_globals* globals) { |
Florian Mayer | 3a538a4 | 2018-12-20 11:23:50 +0000 | [diff] [blame] | 662 | struct sigaction action = {}; |
Ryan Savitski | ecc37e3 | 2018-12-14 15:57:21 +0000 | [diff] [blame] | 663 | action.sa_handler = MaybeInstallInitHeapprofdHook; |
Florian Mayer | 3a538a4 | 2018-12-20 11:23:50 +0000 | [diff] [blame] | 664 | sigaction(HEAPPROFD_SIGNAL, &action, nullptr); |
| 665 | |
| 666 | const char* prefix; |
| 667 | const char* shared_lib; |
| 668 | char prop[PROP_VALUE_MAX]; |
| 669 | char* options = prop; |
| 670 | // Prefer malloc debug since it existed first and is a more complete |
| 671 | // malloc interceptor than the hooks. |
| 672 | if (CheckLoadMallocDebug(&options)) { |
| 673 | prefix = "debug"; |
| 674 | shared_lib = DEBUG_SHARED_LIB; |
| 675 | } else if (CheckLoadMallocHooks(&options)) { |
| 676 | prefix = "hooks"; |
| 677 | shared_lib = HOOKS_SHARED_LIB; |
| 678 | } else if (CheckLoadHeapprofd()) { |
| 679 | prefix = "heapprofd"; |
| 680 | shared_lib = HEAPPROFD_SHARED_LIB; |
| 681 | } else { |
| 682 | return; |
| 683 | } |
| 684 | if (!atomic_exchange(&g_heapprofd_init_in_progress, true)) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame^] | 685 | InstallHooks(globals, options, prefix, shared_lib); |
Florian Mayer | 3a538a4 | 2018-12-20 11:23:50 +0000 | [diff] [blame] | 686 | atomic_store(&g_heapprofd_init_in_progress, false); |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | // Initializes memory allocation framework. |
| 691 | // This routine is called from __libc_init routines in libc_init_dynamic.cpp. |
| 692 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE |
| 693 | __LIBC_HIDDEN__ void __libc_init_malloc(libc_globals* globals) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame^] | 694 | MallocInitImpl(globals); |
Florian Mayer | 3a538a4 | 2018-12-20 11:23:50 +0000 | [diff] [blame] | 695 | } |
| 696 | |
Florian Mayer | f7f71e3 | 2018-08-31 15:36:48 -0700 | [diff] [blame] | 697 | static void* InitHeapprofd(void*) { |
| 698 | __libc_globals.mutate([](libc_globals* globals) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame^] | 699 | InstallHooks(globals, nullptr, HEAPPROFD_PREFIX, HEAPPROFD_SHARED_LIB); |
Florian Mayer | f7f71e3 | 2018-08-31 15:36:48 -0700 | [diff] [blame] | 700 | }); |
| 701 | atomic_store(&g_heapprofd_init_in_progress, false); |
Florian Mayer | 176a475 | 2018-10-23 11:48:34 +0100 | [diff] [blame] | 702 | // Allow to install hook again to re-initialize heap profiling after the |
| 703 | // current session finished. |
| 704 | atomic_store(&g_heapprofd_init_hook_installed, false); |
Florian Mayer | f7f71e3 | 2018-08-31 15:36:48 -0700 | [diff] [blame] | 705 | return nullptr; |
| 706 | } |
| 707 | |
| 708 | static void* InitHeapprofdHook(size_t bytes) { |
Florian Mayer | 176a475 | 2018-10-23 11:48:34 +0100 | [diff] [blame] | 709 | if (!atomic_exchange(&g_heapprofd_init_hook_installed, true)) { |
Florian Mayer | f7f71e3 | 2018-08-31 15:36:48 -0700 | [diff] [blame] | 710 | __libc_globals.mutate([](libc_globals* globals) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame^] | 711 | atomic_store(&globals->current_dispatch_table, nullptr); |
Florian Mayer | f7f71e3 | 2018-08-31 15:36:48 -0700 | [diff] [blame] | 712 | }); |
| 713 | |
| 714 | pthread_t thread_id; |
| 715 | if (pthread_create(&thread_id, nullptr, InitHeapprofd, nullptr) == -1) |
| 716 | error_log("%s: heapprofd: failed to pthread_create.", getprogname()); |
| 717 | else if (pthread_detach(thread_id) == -1) |
| 718 | error_log("%s: heapprofd: failed to pthread_detach", getprogname()); |
| 719 | if (pthread_setname_np(thread_id, "heapprofdinit") == -1) |
| 720 | error_log("%s: heapprod: failed to pthread_setname_np", getprogname()); |
| 721 | } |
| 722 | return Malloc(malloc)(bytes); |
| 723 | } |
| 724 | |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame^] | 725 | static constexpr MallocDispatch __heapprofd_dispatch |
| 726 | __attribute__((unused)) = { |
| 727 | Malloc(calloc), |
| 728 | Malloc(free), |
| 729 | Malloc(mallinfo), |
| 730 | InitHeapprofdHook, |
| 731 | Malloc(malloc_usable_size), |
| 732 | Malloc(memalign), |
| 733 | Malloc(posix_memalign), |
| 734 | #if defined(HAVE_DEPRECATED_MALLOC_FUNCS) |
| 735 | Malloc(pvalloc), |
| 736 | #endif |
| 737 | Malloc(realloc), |
| 738 | #if defined(HAVE_DEPRECATED_MALLOC_FUNCS) |
| 739 | Malloc(valloc), |
| 740 | #endif |
| 741 | Malloc(iterate), |
| 742 | Malloc(malloc_disable), |
| 743 | Malloc(malloc_enable), |
| 744 | Malloc(mallopt), |
| 745 | Malloc(aligned_alloc), |
| 746 | }; |
| 747 | |
Ryan Savitski | ecc37e3 | 2018-12-14 15:57:21 +0000 | [diff] [blame] | 748 | extern "C" void MaybeInstallInitHeapprofdHook(int) { |
| 749 | // Zygote child processes must be marked profileable. |
| 750 | if (gMallocLeakZygoteChild && |
| 751 | !atomic_load_explicit_const(&gMallocZygoteChildProfileable, memory_order_acquire)) { |
| 752 | return; |
| 753 | } |
| 754 | |
Florian Mayer | f7f71e3 | 2018-08-31 15:36:48 -0700 | [diff] [blame] | 755 | if (!atomic_exchange(&g_heapprofd_init_in_progress, true)) { |
| 756 | __libc_globals.mutate([](libc_globals* globals) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame^] | 757 | atomic_store(&globals->current_dispatch_table, &__heapprofd_dispatch); |
Florian Mayer | f7f71e3 | 2018-08-31 15:36:48 -0700 | [diff] [blame] | 758 | }); |
| 759 | } |
| 760 | } |
| 761 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 762 | #endif // !LIBC_STATIC |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 763 | |
| 764 | // ============================================================================= |
Ryan Savitski | ecc37e3 | 2018-12-14 15:57:21 +0000 | [diff] [blame] | 765 | // Platform-internal mallopt variant. |
| 766 | // ============================================================================= |
| 767 | |
| 768 | #if !defined(LIBC_STATIC) |
Florian Mayer | db59b89 | 2018-11-27 17:06:54 +0000 | [diff] [blame] | 769 | bool MallocDispatchReset() { |
| 770 | if (!atomic_exchange(&g_heapprofd_init_in_progress, true)) { |
| 771 | __libc_globals.mutate([](libc_globals* globals) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame^] | 772 | atomic_store(&globals->current_dispatch_table, nullptr); |
Florian Mayer | db59b89 | 2018-11-27 17:06:54 +0000 | [diff] [blame] | 773 | }); |
| 774 | atomic_store(&g_heapprofd_init_in_progress, false); |
| 775 | return true; |
| 776 | } |
| 777 | errno = EAGAIN; |
| 778 | return false; |
| 779 | } |
| 780 | |
Ryan Savitski | ecc37e3 | 2018-12-14 15:57:21 +0000 | [diff] [blame] | 781 | // Marks this process as a profileable zygote child. |
| 782 | bool HandleInitZygoteChildProfiling() { |
| 783 | atomic_store_explicit(&gMallocZygoteChildProfileable, true, |
| 784 | memory_order_release); |
| 785 | |
| 786 | // Conditionally start "from startup" profiling. |
| 787 | if (CheckLoadHeapprofd()) { |
| 788 | // Directly call the signal handler (will correctly guard against |
| 789 | // concurrent signal delivery). |
| 790 | MaybeInstallInitHeapprofdHook(HEAPPROFD_SIGNAL); |
| 791 | } |
| 792 | return true; |
| 793 | } |
| 794 | |
| 795 | #else |
| 796 | |
Florian Mayer | db59b89 | 2018-11-27 17:06:54 +0000 | [diff] [blame] | 797 | bool MallocDispatchReset() { |
| 798 | return true; |
| 799 | } |
| 800 | |
Ryan Savitski | ecc37e3 | 2018-12-14 15:57:21 +0000 | [diff] [blame] | 801 | bool HandleInitZygoteChildProfiling() { |
| 802 | return true; |
| 803 | } |
| 804 | |
| 805 | #endif // !defined(LIBC_STATIC) |
| 806 | |
| 807 | bool android_mallopt(int opcode, void* arg, size_t arg_size) { |
| 808 | if (opcode == M_INIT_ZYGOTE_CHILD_PROFILING) { |
| 809 | if (arg != nullptr || arg_size != 0) { |
| 810 | errno = EINVAL; |
| 811 | return false; |
| 812 | } |
| 813 | return HandleInitZygoteChildProfiling(); |
| 814 | } |
Florian Mayer | db59b89 | 2018-11-27 17:06:54 +0000 | [diff] [blame] | 815 | if (opcode == M_RESET_HOOKS) { |
| 816 | if (arg != nullptr || arg_size != 0) { |
| 817 | errno = EINVAL; |
| 818 | return false; |
| 819 | } |
| 820 | return MallocDispatchReset(); |
| 821 | } |
Ryan Savitski | ecc37e3 | 2018-12-14 15:57:21 +0000 | [diff] [blame] | 822 | |
| 823 | errno = ENOTSUP; |
| 824 | return false; |
| 825 | } |
| 826 | |
| 827 | // ============================================================================= |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 828 | // Exported for use by libmemunreachable. |
| 829 | // ============================================================================= |
| 830 | |
| 831 | // Calls callback for every allocation in the anonymous heap mapping |
| 832 | // [base, base+size). Must be called between malloc_disable and malloc_enable. |
| 833 | extern "C" int malloc_iterate(uintptr_t base, size_t size, |
| 834 | void (*callback)(uintptr_t base, size_t size, void* arg), void* arg) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame^] | 835 | auto dispatch_table = GetDispatchTable(); |
| 836 | if (__predict_false(dispatch_table != nullptr)) { |
| 837 | return dispatch_table->iterate(base, size, callback, arg); |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 838 | } |
| 839 | return Malloc(iterate)(base, size, callback, arg); |
| 840 | } |
| 841 | |
| 842 | // Disable calls to malloc so malloc_iterate gets a consistent view of |
| 843 | // allocated memory. |
| 844 | extern "C" void malloc_disable() { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame^] | 845 | auto dispatch_table = GetDispatchTable(); |
| 846 | if (__predict_false(dispatch_table != nullptr)) { |
| 847 | return dispatch_table->malloc_disable(); |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 848 | } |
| 849 | return Malloc(malloc_disable)(); |
| 850 | } |
| 851 | |
| 852 | // Re-enable calls to malloc after a previous call to malloc_disable. |
| 853 | extern "C" void malloc_enable() { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame^] | 854 | auto dispatch_table = GetDispatchTable(); |
| 855 | if (__predict_false(dispatch_table != nullptr)) { |
| 856 | return dispatch_table->malloc_enable(); |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 857 | } |
| 858 | return Malloc(malloc_enable)(); |
| 859 | } |
Colin Cross | 2d4721c | 2016-02-02 11:57:54 -0800 | [diff] [blame] | 860 | |
| 861 | #ifndef LIBC_STATIC |
| 862 | extern "C" ssize_t malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count) { |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 863 | void* func = g_functions[FUNC_MALLOC_BACKTRACE]; |
| 864 | if (func == nullptr) { |
Colin Cross | 2d4721c | 2016-02-02 11:57:54 -0800 | [diff] [blame] | 865 | return 0; |
| 866 | } |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 867 | return reinterpret_cast<malloc_backtrace_func_t>(func)(pointer, frames, frame_count); |
Colin Cross | 2d4721c | 2016-02-02 11:57:54 -0800 | [diff] [blame] | 868 | } |
| 869 | #else |
| 870 | extern "C" ssize_t malloc_backtrace(void*, uintptr_t*, size_t) { |
| 871 | return 0; |
| 872 | } |
| 873 | #endif |