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. 42 43 #include <pthread.h> 44 45 #include <private/bionic_config.h> 46 #include <private/bionic_globals.h> 47 #include <private/bionic_malloc_dispatch.h> 48 49 #include "jemalloc.h" 50 #define Malloc(function) je_ ## function 51 52 static constexpr MallocDispatch __libc_malloc_default_dispatch 53 __attribute__((unused)) = { 54 Malloc(calloc), 55 Malloc(free), 56 Malloc(mallinfo), 57 Malloc(malloc), 58 Malloc(malloc_usable_size), 59 Malloc(memalign), 60 Malloc(posix_memalign), 61 #if defined(HAVE_DEPRECATED_MALLOC_FUNCS) 62 Malloc(pvalloc), 63 #endif 64 Malloc(realloc), 65 #if defined(HAVE_DEPRECATED_MALLOC_FUNCS) 66 Malloc(valloc), 67 #endif 68 Malloc(iterate), 69 Malloc(malloc_disable), 70 Malloc(malloc_enable), 71 }; 72 73 // In a VM process, this is set to 1 after fork()ing out of zygote. 74 int gMallocLeakZygoteChild = 0; 75 76 // ============================================================================= 77 // Allocation functions 78 // ============================================================================= 79 extern "C" void* calloc(size_t n_elements, size_t elem_size) { 80 auto _calloc = __libc_globals->malloc_dispatch.calloc; 81 if (__predict_false(_calloc != nullptr)) { 82 return _calloc(n_elements, elem_size); 83 } 84 return Malloc(calloc)(n_elements, elem_size); 85 } 86 87 extern "C" void free(void* mem) { 88 auto _free = __libc_globals->malloc_dispatch.free; 89 if (__predict_false(_free != nullptr)) { 90 _free(mem); 91 } else { 92 Malloc(free)(mem); 93 } 94 } 95 96 extern "C" struct mallinfo mallinfo() { 97 auto _mallinfo = __libc_globals->malloc_dispatch.mallinfo; 98 if (__predict_false(_mallinfo != nullptr)) { 99 return _mallinfo(); 100 } 101 return Malloc(mallinfo)(); 102 } 103 104 extern "C" void* malloc(size_t bytes) { 105 auto _malloc = __libc_globals->malloc_dispatch.malloc; 106 if (__predict_false(_malloc != nullptr)) { 107 return _malloc(bytes); 108 } 109 return Malloc(malloc)(bytes); 110 } 111 112 extern "C" size_t malloc_usable_size(const void* mem) { 113 auto _malloc_usable_size = __libc_globals->malloc_dispatch.malloc_usable_size; 114 if (__predict_false(_malloc_usable_size != nullptr)) { 115 return _malloc_usable_size(mem); 116 } 117 return Malloc(malloc_usable_size)(mem); 118 } 119 120 extern "C" void* memalign(size_t alignment, size_t bytes) { 121 auto _memalign = __libc_globals->malloc_dispatch.memalign; 122 if (__predict_false(_memalign != nullptr)) { 123 return _memalign(alignment, bytes); 124 } 125 return Malloc(memalign)(alignment, bytes); 126 } 127 128 extern "C" int posix_memalign(void** memptr, size_t alignment, size_t size) { 129 auto _posix_memalign = __libc_globals->malloc_dispatch.posix_memalign; 130 if (__predict_false(_posix_memalign != nullptr)) { 131 return _posix_memalign(memptr, alignment, size); 132 } 133 return Malloc(posix_memalign)(memptr, alignment, size); 134 } 135 136 extern "C" void* realloc(void* old_mem, size_t bytes) { 137 auto _realloc = __libc_globals->malloc_dispatch.realloc; 138 if (__predict_false(_realloc != nullptr)) { 139 return _realloc(old_mem, bytes); 140 } 141 return Malloc(realloc)(old_mem, bytes); 142 } 143 144 #if defined(HAVE_DEPRECATED_MALLOC_FUNCS) 145 extern "C" void* pvalloc(size_t bytes) { 146 auto _pvalloc = __libc_globals->malloc_dispatch.pvalloc; 147 if (__predict_false(_pvalloc != nullptr)) { 148 return _pvalloc(bytes); 149 } 150 return Malloc(pvalloc)(bytes); 151 } 152 153 extern "C" void* valloc(size_t bytes) { 154 auto _valloc = __libc_globals->malloc_dispatch.valloc; 155 if (__predict_false(_valloc != nullptr)) { 156 return _valloc(bytes); 157 } 158 return Malloc(valloc)(bytes); 159 } 160 #endif 161 162 // We implement malloc debugging only in libc.so, so the code below 163 // must be excluded if we compile this file for static libc.a 164 #if !defined(LIBC_STATIC) 165 166 #include <dlfcn.h> 167 #include <stdio.h> 168 #include <stdlib.h> 169 170 #include <private/libc_logging.h> 171 #include <sys/system_properties.h> 172 173 extern "C" int __cxa_atexit(void (*func)(void *), void *arg, void *dso); 174 175 static const char* DEBUG_SHARED_LIB = "libc_malloc_debug.so"; 176 static const char* DEBUG_MALLOC_PROPERTY_OPTIONS = "libc.debug.malloc.options"; 177 static const char* DEBUG_MALLOC_PROPERTY_PROGRAM = "libc.debug.malloc.program"; 178 static const char* DEBUG_MALLOC_PROPERTY_ENV_ENABLED = "libc.debug.malloc.env_enabled"; 179 static const char* DEBUG_MALLOC_ENV_ENABLE = "LIBC_DEBUG_MALLOC_ENABLE"; 180 181 static void* libc_malloc_impl_handle = nullptr; 182 183 static void (*g_debug_finalize_func)(); 184 static void (*g_debug_get_malloc_leak_info_func)(uint8_t**, size_t*, size_t*, size_t*, size_t*); 185 static void (*g_debug_free_malloc_leak_info_func)(uint8_t*); 186 static ssize_t (*g_debug_malloc_backtrace_func)(void*, uintptr_t*, size_t); 187 188 // ============================================================================= 189 // Log functions 190 // ============================================================================= 191 #define error_log(format, ...) \ 192 __libc_format_log(ANDROID_LOG_ERROR, "libc", (format), ##__VA_ARGS__ ) 193 #define info_log(format, ...) \ 194 __libc_format_log(ANDROID_LOG_INFO, "libc", (format), ##__VA_ARGS__ ) 195 // ============================================================================= 196 197 // ============================================================================= 198 // Exported for use by ddms. 199 // ============================================================================= 200 201 // Retrieve native heap information. 202 // 203 // "*info" is set to a buffer we allocate 204 // "*overall_size" is set to the size of the "info" buffer 205 // "*info_size" is set to the size of a single entry 206 // "*total_memory" is set to the sum of all allocations we're tracking; does 207 // not include heap overhead 208 // "*backtrace_size" is set to the maximum number of entries in the back trace 209 extern "C" void get_malloc_leak_info(uint8_t** info, size_t* overall_size, 210 size_t* info_size, size_t* total_memory, size_t* backtrace_size) { 211 if (g_debug_get_malloc_leak_info_func == nullptr) { 212 return; 213 } 214 g_debug_get_malloc_leak_info_func(info, overall_size, info_size, total_memory, backtrace_size); 215 } 216 217 extern "C" void free_malloc_leak_info(uint8_t* info) { 218 if (g_debug_free_malloc_leak_info_func == nullptr) { 219 return; 220 } 221 g_debug_free_malloc_leak_info_func(info); 222 } 223 224 // ============================================================================= 225 226 template<typename FunctionType> 227 static bool InitMallocFunction(void* malloc_impl_handler, FunctionType* func, const char* prefix, const char* suffix) { 228 char symbol[128]; 229 snprintf(symbol, sizeof(symbol), "%s_%s", prefix, suffix); 230 *func = reinterpret_cast<FunctionType>(dlsym(malloc_impl_handler, symbol)); 231 if (*func == nullptr) { 232 error_log("%s: dlsym(\"%s\") failed", getprogname(), symbol); 233 return false; 234 } 235 return true; 236 } 237 238 static bool InitMalloc(void* malloc_impl_handler, MallocDispatch* table, const char* prefix) { 239 if (!InitMallocFunction<MallocCalloc>(malloc_impl_handler, &table->calloc, 240 prefix, "calloc")) { 241 return false; 242 } 243 if (!InitMallocFunction<MallocFree>(malloc_impl_handler, &table->free, 244 prefix, "free")) { 245 return false; 246 } 247 if (!InitMallocFunction<MallocMallinfo>(malloc_impl_handler, &table->mallinfo, 248 prefix, "mallinfo")) { 249 return false; 250 } 251 if (!InitMallocFunction<MallocMalloc>(malloc_impl_handler, &table->malloc, 252 prefix, "malloc")) { 253 return false; 254 } 255 if (!InitMallocFunction<MallocMallocUsableSize>( 256 malloc_impl_handler, &table->malloc_usable_size, prefix, "malloc_usable_size")) { 257 return false; 258 } 259 if (!InitMallocFunction<MallocMemalign>(malloc_impl_handler, &table->memalign, 260 prefix, "memalign")) { 261 return false; 262 } 263 if (!InitMallocFunction<MallocPosixMemalign>(malloc_impl_handler, &table->posix_memalign, 264 prefix, "posix_memalign")) { 265 return false; 266 } 267 if (!InitMallocFunction<MallocRealloc>(malloc_impl_handler, &table->realloc, 268 prefix, "realloc")) { 269 return false; 270 } 271 if (!InitMallocFunction<MallocIterate>(malloc_impl_handler, &table->iterate, 272 prefix, "iterate")) { 273 return false; 274 } 275 if (!InitMallocFunction<MallocMallocDisable>(malloc_impl_handler, &table->malloc_disable, 276 prefix, "malloc_disable")) { 277 return false; 278 } 279 if (!InitMallocFunction<MallocMallocEnable>(malloc_impl_handler, &table->malloc_enable, 280 prefix, "malloc_enable")) { 281 return false; 282 } 283 #if defined(HAVE_DEPRECATED_MALLOC_FUNCS) 284 if (!InitMallocFunction<MallocPvalloc>(malloc_impl_handler, &table->pvalloc, 285 prefix, "pvalloc")) { 286 return false; 287 } 288 if (!InitMallocFunction<MallocValloc>(malloc_impl_handler, &table->valloc, 289 prefix, "valloc")) { 290 return false; 291 } 292 #endif 293 294 return true; 295 } 296 297 static void malloc_fini_impl(void*) { 298 // Our BSD stdio implementation doesn't close the standard streams, 299 // it only flushes them. Other unclosed FILE*s will show up as 300 // malloc leaks, but to avoid the standard streams showing up in 301 // leak reports, close them here. 302 fclose(stdin); 303 fclose(stdout); 304 fclose(stderr); 305 306 g_debug_finalize_func(); 307 } 308 309 // Initializes memory allocation framework once per process. 310 static void malloc_init_impl(libc_globals* globals) { 311 char value[PROP_VALUE_MAX]; 312 if (__system_property_get(DEBUG_MALLOC_PROPERTY_OPTIONS, value) == 0 || value[0] == '\0') { 313 return; 314 } 315 316 // Check to see if only a specific program should have debug malloc enabled. 317 if (__system_property_get(DEBUG_MALLOC_PROPERTY_PROGRAM, value) != 0 && 318 strstr(getprogname(), value) == nullptr) { 319 return; 320 } 321 322 // Check for the special environment variable instead. 323 if (__system_property_get(DEBUG_MALLOC_PROPERTY_ENV_ENABLED, value) != 0 324 && value[0] != '\0' && getenv(DEBUG_MALLOC_ENV_ENABLE) == nullptr) { 325 return; 326 } 327 328 // Load the debug malloc shared library. 329 void* malloc_impl_handle = dlopen(DEBUG_SHARED_LIB, RTLD_NOW | RTLD_LOCAL); 330 if (malloc_impl_handle == nullptr) { 331 error_log("%s: Unable to open debug malloc shared library %s: %s", 332 getprogname(), DEBUG_SHARED_LIB, dlerror()); 333 return; 334 } 335 336 // Initialize malloc debugging in the loaded module. 337 auto init_func = reinterpret_cast<bool (*)(const MallocDispatch*, int*)>( 338 dlsym(malloc_impl_handle, "debug_initialize")); 339 if (init_func == nullptr) { 340 error_log("%s: debug_initialize routine not found in %s", getprogname(), DEBUG_SHARED_LIB); 341 dlclose(malloc_impl_handle); 342 return; 343 } 344 345 // Get the syms for the external functions. 346 void* finalize_sym = dlsym(malloc_impl_handle, "debug_finalize"); 347 if (finalize_sym == nullptr) { 348 error_log("%s: debug_finalize routine not found in %s", getprogname(), DEBUG_SHARED_LIB); 349 dlclose(malloc_impl_handle); 350 return; 351 } 352 353 void* get_leak_info_sym = dlsym(malloc_impl_handle, "debug_get_malloc_leak_info"); 354 if (get_leak_info_sym == nullptr) { 355 error_log("%s: debug_get_malloc_leak_info routine not found in %s", getprogname(), 356 DEBUG_SHARED_LIB); 357 dlclose(malloc_impl_handle); 358 return; 359 } 360 361 void* free_leak_info_sym = dlsym(malloc_impl_handle, "debug_free_malloc_leak_info"); 362 if (free_leak_info_sym == nullptr) { 363 error_log("%s: debug_free_malloc_leak_info routine not found in %s", getprogname(), 364 DEBUG_SHARED_LIB); 365 dlclose(malloc_impl_handle); 366 return; 367 } 368 369 void* malloc_backtrace_sym = dlsym(malloc_impl_handle, "debug_malloc_backtrace"); 370 if (malloc_backtrace_sym == nullptr) { 371 error_log("%s: debug_malloc_backtrace routine not found in %s", getprogname(), 372 DEBUG_SHARED_LIB); 373 dlclose(malloc_impl_handle); 374 return; 375 } 376 377 if (!init_func(&__libc_malloc_default_dispatch, &gMallocLeakZygoteChild)) { 378 dlclose(malloc_impl_handle); 379 return; 380 } 381 382 MallocDispatch malloc_dispatch_table; 383 if (!InitMalloc(malloc_impl_handle, &malloc_dispatch_table, "debug")) { 384 auto finalize_func = reinterpret_cast<void (*)()>(finalize_sym); 385 finalize_func(); 386 dlclose(malloc_impl_handle); 387 return; 388 } 389 390 g_debug_finalize_func = reinterpret_cast<void (*)()>(finalize_sym); 391 g_debug_get_malloc_leak_info_func = reinterpret_cast<void (*)( 392 uint8_t**, size_t*, size_t*, size_t*, size_t*)>(get_leak_info_sym); 393 g_debug_free_malloc_leak_info_func = reinterpret_cast<void (*)(uint8_t*)>(free_leak_info_sym); 394 g_debug_malloc_backtrace_func = reinterpret_cast<ssize_t (*)( 395 void*, uintptr_t*, size_t)>(malloc_backtrace_sym); 396 397 globals->malloc_dispatch = malloc_dispatch_table; 398 libc_malloc_impl_handle = malloc_impl_handle; 399 400 info_log("%s: malloc debug enabled", getprogname()); 401 402 // Use atexit to trigger the cleanup function. This avoids a problem 403 // where another atexit function is used to cleanup allocated memory, 404 // but the finalize function was already called. This particular error 405 // seems to be triggered by a zygote spawned process calling exit. 406 int ret_value = __cxa_atexit(malloc_fini_impl, nullptr, nullptr); 407 if (ret_value != 0) { 408 error_log("failed to set atexit cleanup function: %d", ret_value); 409 } 410 } 411 412 // Initializes memory allocation framework. 413 // This routine is called from __libc_init routines in libc_init_dynamic.cpp. 414 __LIBC_HIDDEN__ void __libc_init_malloc(libc_globals* globals) { 415 malloc_init_impl(globals); 416 } 417 #endif // !LIBC_STATIC 418 419 // ============================================================================= 420 // Exported for use by libmemunreachable. 421 // ============================================================================= 422 423 // Calls callback for every allocation in the anonymous heap mapping 424 // [base, base+size). Must be called between malloc_disable and malloc_enable. 425 extern "C" int malloc_iterate(uintptr_t base, size_t size, 426 void (*callback)(uintptr_t base, size_t size, void* arg), void* arg) { 427 auto _iterate = __libc_globals->malloc_dispatch.iterate; 428 if (__predict_false(_iterate != nullptr)) { 429 return _iterate(base, size, callback, arg); 430 } 431 return Malloc(iterate)(base, size, callback, arg); 432 } 433 434 // Disable calls to malloc so malloc_iterate gets a consistent view of 435 // allocated memory. 436 extern "C" void malloc_disable() { 437 auto _malloc_disable = __libc_globals->malloc_dispatch.malloc_disable; 438 if (__predict_false(_malloc_disable != nullptr)) { 439 return _malloc_disable(); 440 } 441 return Malloc(malloc_disable)(); 442 } 443 444 // Re-enable calls to malloc after a previous call to malloc_disable. 445 extern "C" void malloc_enable() { 446 auto _malloc_enable = __libc_globals->malloc_dispatch.malloc_enable; 447 if (__predict_false(_malloc_enable != nullptr)) { 448 return _malloc_enable(); 449 } 450 return Malloc(malloc_enable)(); 451 } 452 453 #ifndef LIBC_STATIC 454 extern "C" ssize_t malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count) { 455 if (g_debug_malloc_backtrace_func == nullptr) { 456 return 0; 457 } 458 return g_debug_malloc_backtrace_func(pointer, frames, frame_count); 459 } 460 #else 461 extern "C" ssize_t malloc_backtrace(void*, uintptr_t*, size_t) { 462 return 0; 463 } 464 #endif 465