malloc_common.cpp revision c40577f740ae4f66cdba4b2137668fb3114bb99d
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 52static 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. 74int gMallocLeakZygoteChild = 0; 75 76// ============================================================================= 77// Allocation functions 78// ============================================================================= 79extern "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 87extern "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 96extern "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 104extern "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 112extern "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 120extern "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 128extern "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 136extern "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) 145extern "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 153extern "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 173extern "C" int __cxa_atexit(void (*func)(void *), void *arg, void *dso); 174 175static const char* DEBUG_SHARED_LIB = "libc_malloc_debug.so"; 176static const char* DEBUG_MALLOC_PROPERTY_OPTIONS = "libc.debug.malloc.options"; 177static const char* DEBUG_MALLOC_PROPERTY_PROGRAM = "libc.debug.malloc.program"; 178static const char* DEBUG_MALLOC_PROPERTY_ENV_ENABLED = "libc.debug.malloc.env_enabled"; 179static const char* DEBUG_MALLOC_ENV_ENABLE = "LIBC_DEBUG_MALLOC_ENABLE"; 180 181static void* libc_malloc_impl_handle = nullptr; 182 183static void (*g_debug_finalize_func)(); 184static void (*g_debug_get_malloc_leak_info_func)(uint8_t**, size_t*, size_t*, size_t*, size_t*); 185static void (*g_debug_free_malloc_leak_info_func)(uint8_t*); 186 187// ============================================================================= 188// Log functions 189// ============================================================================= 190#define error_log(format, ...) \ 191 __libc_format_log(ANDROID_LOG_ERROR, "libc", (format), ##__VA_ARGS__ ) 192#define info_log(format, ...) \ 193 __libc_format_log(ANDROID_LOG_INFO, "libc", (format), ##__VA_ARGS__ ) 194// ============================================================================= 195 196// ============================================================================= 197// Exported for use by ddms. 198// ============================================================================= 199 200// Retrieve native heap information. 201// 202// "*info" is set to a buffer we allocate 203// "*overall_size" is set to the size of the "info" buffer 204// "*info_size" is set to the size of a single entry 205// "*total_memory" is set to the sum of all allocations we're tracking; does 206// not include heap overhead 207// "*backtrace_size" is set to the maximum number of entries in the back trace 208extern "C" void get_malloc_leak_info(uint8_t** info, size_t* overall_size, 209 size_t* info_size, size_t* total_memory, size_t* backtrace_size) { 210 if (g_debug_get_malloc_leak_info_func == nullptr) { 211 return; 212 } 213 g_debug_get_malloc_leak_info_func(info, overall_size, info_size, total_memory, backtrace_size); 214} 215 216extern "C" void free_malloc_leak_info(uint8_t* info) { 217 if (g_debug_free_malloc_leak_info_func == nullptr) { 218 return; 219 } 220 g_debug_free_malloc_leak_info_func(info); 221} 222 223// ============================================================================= 224 225template<typename FunctionType> 226static bool InitMallocFunction(void* malloc_impl_handler, FunctionType* func, const char* prefix, const char* suffix) { 227 char symbol[128]; 228 snprintf(symbol, sizeof(symbol), "%s_%s", prefix, suffix); 229 *func = reinterpret_cast<FunctionType>(dlsym(malloc_impl_handler, symbol)); 230 if (*func == nullptr) { 231 error_log("%s: dlsym(\"%s\") failed", getprogname(), symbol); 232 return false; 233 } 234 return true; 235} 236 237static bool InitMalloc(void* malloc_impl_handler, MallocDispatch* table, const char* prefix) { 238 if (!InitMallocFunction<MallocCalloc>(malloc_impl_handler, &table->calloc, 239 prefix, "calloc")) { 240 return false; 241 } 242 if (!InitMallocFunction<MallocFree>(malloc_impl_handler, &table->free, 243 prefix, "free")) { 244 return false; 245 } 246 if (!InitMallocFunction<MallocMallinfo>(malloc_impl_handler, &table->mallinfo, 247 prefix, "mallinfo")) { 248 return false; 249 } 250 if (!InitMallocFunction<MallocMalloc>(malloc_impl_handler, &table->malloc, 251 prefix, "malloc")) { 252 return false; 253 } 254 if (!InitMallocFunction<MallocMallocUsableSize>( 255 malloc_impl_handler, &table->malloc_usable_size, prefix, "malloc_usable_size")) { 256 return false; 257 } 258 if (!InitMallocFunction<MallocMemalign>(malloc_impl_handler, &table->memalign, 259 prefix, "memalign")) { 260 return false; 261 } 262 if (!InitMallocFunction<MallocPosixMemalign>(malloc_impl_handler, &table->posix_memalign, 263 prefix, "posix_memalign")) { 264 return false; 265 } 266 if (!InitMallocFunction<MallocRealloc>(malloc_impl_handler, &table->realloc, 267 prefix, "realloc")) { 268 return false; 269 } 270 if (!InitMallocFunction<MallocIterate>(malloc_impl_handler, &table->iterate, 271 prefix, "iterate")) { 272 return false; 273 } 274 if (!InitMallocFunction<MallocMallocDisable>(malloc_impl_handler, &table->malloc_disable, 275 prefix, "malloc_disable")) { 276 return false; 277 } 278 if (!InitMallocFunction<MallocMallocEnable>(malloc_impl_handler, &table->malloc_enable, 279 prefix, "malloc_enable")) { 280 return false; 281 } 282#if defined(HAVE_DEPRECATED_MALLOC_FUNCS) 283 if (!InitMallocFunction<MallocPvalloc>(malloc_impl_handler, &table->pvalloc, 284 prefix, "pvalloc")) { 285 return false; 286 } 287 if (!InitMallocFunction<MallocValloc>(malloc_impl_handler, &table->valloc, 288 prefix, "valloc")) { 289 return false; 290 } 291#endif 292 293 return true; 294} 295 296static void malloc_fini_impl(void*) { 297 // Our BSD stdio implementation doesn't close the standard streams, 298 // it only flushes them. Other unclosed FILE*s will show up as 299 // malloc leaks, but to avoid the standard streams showing up in 300 // leak reports, close them here. 301 fclose(stdin); 302 fclose(stdout); 303 fclose(stderr); 304 305 g_debug_finalize_func(); 306} 307 308// Initializes memory allocation framework once per process. 309static void malloc_init_impl(libc_globals* globals) { 310 char value[PROP_VALUE_MAX]; 311 if (__system_property_get(DEBUG_MALLOC_PROPERTY_OPTIONS, value) == 0 || value[0] == '\0') { 312 return; 313 } 314 315 // Check to see if only a specific program should have debug malloc enabled. 316 if (__system_property_get(DEBUG_MALLOC_PROPERTY_PROGRAM, value) != 0 && 317 strstr(getprogname(), value) == nullptr) { 318 return; 319 } 320 321 // Check for the special environment variable instead. 322 if (__system_property_get(DEBUG_MALLOC_PROPERTY_ENV_ENABLED, value) != 0 323 && value[0] != '\0' && getenv(DEBUG_MALLOC_ENV_ENABLE) == nullptr) { 324 return; 325 } 326 327 // Load the debug malloc shared library. 328 void* malloc_impl_handle = dlopen(DEBUG_SHARED_LIB, RTLD_NOW | RTLD_LOCAL); 329 if (malloc_impl_handle == nullptr) { 330 error_log("%s: Unable to open debug malloc shared library %s: %s", 331 getprogname(), DEBUG_SHARED_LIB, dlerror()); 332 return; 333 } 334 335 // Initialize malloc debugging in the loaded module. 336 void* sym = dlsym(malloc_impl_handle, "debug_initialize"); 337 auto init_func = reinterpret_cast<bool (*)(const MallocDispatch*, int*)>(sym); 338 if (init_func == nullptr) { 339 error_log("%s: debug_initialize routine not found in %s", getprogname(), DEBUG_SHARED_LIB); 340 dlclose(malloc_impl_handle); 341 return; 342 } 343 344 // Get the syms for the external functions. 345 sym = dlsym(malloc_impl_handle, "debug_finalize"); 346 if (sym == nullptr) { 347 error_log("%s: debug_finalize routine not found in %s", getprogname(), DEBUG_SHARED_LIB); 348 dlclose(malloc_impl_handle); 349 return; 350 } 351 g_debug_finalize_func = reinterpret_cast<void (*)()>(sym); 352 353 sym = dlsym(malloc_impl_handle, "debug_get_malloc_leak_info"); 354 if (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 g_debug_get_malloc_leak_info_func = reinterpret_cast<void (*)(uint8_t**, size_t*, size_t*, 361 size_t*, size_t*)>(sym); 362 363 sym = dlsym(malloc_impl_handle, "debug_free_malloc_leak_info"); 364 if (sym == nullptr) { 365 error_log("%s: debug_free_malloc_leak_info routine not found in %s", getprogname(), 366 DEBUG_SHARED_LIB); 367 dlclose(malloc_impl_handle); 368 return; 369 } 370 g_debug_free_malloc_leak_info_func = reinterpret_cast<void (*)(uint8_t*)>(sym); 371 372 if (!init_func(&__libc_malloc_default_dispatch, &gMallocLeakZygoteChild)) { 373 dlclose(malloc_impl_handle); 374 return; 375 } 376 377 MallocDispatch malloc_dispatch_table; 378 if (!InitMalloc(malloc_impl_handle, &malloc_dispatch_table, "debug")) { 379 g_debug_finalize_func(); 380 dlclose(malloc_impl_handle); 381 return; 382 } 383 384 globals->malloc_dispatch = malloc_dispatch_table; 385 libc_malloc_impl_handle = malloc_impl_handle; 386 387 info_log("%s: malloc debug enabled", getprogname()); 388 389 // Use atexit to trigger the cleanup function. This avoids a problem 390 // where another atexit function is used to cleanup allocated memory, 391 // but the finalize function was already called. This particular error 392 // seems to be triggered by a zygote spawned process calling exit. 393 int ret_value = __cxa_atexit(malloc_fini_impl, nullptr, nullptr); 394 if (ret_value != 0) { 395 error_log("failed to set atexit cleanup function: %d", ret_value); 396 } 397} 398 399// Initializes memory allocation framework. 400// This routine is called from __libc_init routines in libc_init_dynamic.cpp. 401__LIBC_HIDDEN__ void __libc_init_malloc(libc_globals* globals) { 402 malloc_init_impl(globals); 403} 404#endif // !LIBC_STATIC 405 406// ============================================================================= 407// Exported for use by libmemunreachable. 408// ============================================================================= 409 410// Calls callback for every allocation in the anonymous heap mapping 411// [base, base+size). Must be called between malloc_disable and malloc_enable. 412extern "C" int malloc_iterate(uintptr_t base, size_t size, 413 void (*callback)(uintptr_t base, size_t size, void* arg), void* arg) { 414 auto _iterate = __libc_globals->malloc_dispatch.iterate; 415 if (__predict_false(_iterate != nullptr)) { 416 return _iterate(base, size, callback, arg); 417 } 418 return Malloc(iterate)(base, size, callback, arg); 419} 420 421// Disable calls to malloc so malloc_iterate gets a consistent view of 422// allocated memory. 423extern "C" void malloc_disable() { 424 auto _malloc_disable = __libc_globals->malloc_dispatch.malloc_disable; 425 if (__predict_false(_malloc_disable != nullptr)) { 426 return _malloc_disable(); 427 } 428 return Malloc(malloc_disable)(); 429} 430 431// Re-enable calls to malloc after a previous call to malloc_disable. 432extern "C" void malloc_enable() { 433 auto _malloc_enable = __libc_globals->malloc_dispatch.malloc_enable; 434 if (__predict_false(_malloc_enable != nullptr)) { 435 return _malloc_enable(); 436 } 437 return Malloc(malloc_enable)(); 438} 439