Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -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 | /* |
Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 30 | * Contains definition of structures, global variables, and implementation of |
| 31 | * routines that are used by malloc leak detection code and other components in |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 32 | * the system. The trick is that some components expect these data and |
| 33 | * routines to be defined / implemented in libc.so library, regardless |
| 34 | * whether or not MALLOC_LEAK_CHECK macro is defined. To make things even |
| 35 | * more tricky, malloc leak detection code, implemented in |
| 36 | * libc_malloc_debug.so also requires access to these variables and routines |
| 37 | * (to fill allocation entry hash table, for example). So, all relevant |
| 38 | * variables and routines are defined / implemented here and exported |
| 39 | * to all, leak detection code and other components via dynamic (libc.so), |
| 40 | * or static (libc.a) linking. |
| 41 | */ |
| 42 | |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 43 | #include "malloc_debug_common.h" |
| 44 | |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame^] | 45 | #include <pthread.h> |
| 46 | #include <stdlib.h> |
| 47 | #include <unistd.h> |
| 48 | |
| 49 | #include "dlmalloc.h" |
| 50 | #include "ScopedPthreadMutexLocker.h" |
| 51 | |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 52 | /* |
| 53 | * In a VM process, this is set to 1 after fork()ing out of zygote. |
| 54 | */ |
| 55 | int gMallocLeakZygoteChild = 0; |
| 56 | |
| 57 | pthread_mutex_t gAllocationsMutex = PTHREAD_MUTEX_INITIALIZER; |
| 58 | HashTable gHashTable; |
| 59 | |
| 60 | // ============================================================================= |
| 61 | // output functions |
| 62 | // ============================================================================= |
| 63 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 64 | static int hash_entry_compare(const void* arg1, const void* arg2) { |
Christopher Tate | 52e7d3d | 2010-08-09 13:43:46 -0700 | [diff] [blame] | 65 | int result; |
| 66 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 67 | const HashEntry* e1 = *static_cast<HashEntry* const*>(arg1); |
| 68 | const HashEntry* e2 = *static_cast<HashEntry* const*>(arg2); |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 69 | |
Christopher Tate | 52e7d3d | 2010-08-09 13:43:46 -0700 | [diff] [blame] | 70 | // if one or both arg pointers are null, deal gracefully |
| 71 | if (e1 == NULL) { |
| 72 | result = (e2 == NULL) ? 0 : 1; |
| 73 | } else if (e2 == NULL) { |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 74 | result = -1; |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 75 | } else { |
Christopher Tate | 52e7d3d | 2010-08-09 13:43:46 -0700 | [diff] [blame] | 76 | size_t nbAlloc1 = e1->allocations; |
| 77 | size_t nbAlloc2 = e2->allocations; |
| 78 | size_t size1 = e1->size & ~SIZE_FLAG_MASK; |
| 79 | size_t size2 = e2->size & ~SIZE_FLAG_MASK; |
| 80 | size_t alloc1 = nbAlloc1 * size1; |
| 81 | size_t alloc2 = nbAlloc2 * size2; |
| 82 | |
| 83 | // sort in descending order by: |
| 84 | // 1) total size |
| 85 | // 2) number of allocations |
| 86 | // |
| 87 | // This is used for sorting, not determination of equality, so we don't |
| 88 | // need to compare the bit flags. |
Christopher Tate | 52e7d3d | 2010-08-09 13:43:46 -0700 | [diff] [blame] | 89 | if (alloc1 > alloc2) { |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 90 | result = -1; |
Christopher Tate | 52e7d3d | 2010-08-09 13:43:46 -0700 | [diff] [blame] | 91 | } else if (alloc1 < alloc2) { |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 92 | result = 1; |
| 93 | } else { |
Christopher Tate | 52e7d3d | 2010-08-09 13:43:46 -0700 | [diff] [blame] | 94 | if (nbAlloc1 > nbAlloc2) { |
| 95 | result = -1; |
| 96 | } else if (nbAlloc1 < nbAlloc2) { |
| 97 | result = 1; |
| 98 | } else { |
| 99 | result = 0; |
| 100 | } |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | return result; |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * Retrieve native heap information. |
| 108 | * |
| 109 | * "*info" is set to a buffer we allocate |
| 110 | * "*overallSize" is set to the size of the "info" buffer |
| 111 | * "*infoSize" is set to the size of a single entry |
| 112 | * "*totalMemory" is set to the sum of all allocations we're tracking; does |
| 113 | * not include heap overhead |
| 114 | * "*backtraceSize" is set to the maximum number of entries in the back trace |
| 115 | */ |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 116 | extern "C" void get_malloc_leak_info(uint8_t** info, size_t* overallSize, |
| 117 | size_t* infoSize, size_t* totalMemory, size_t* backtraceSize) { |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 118 | // don't do anything if we have invalid arguments |
| 119 | if (info == NULL || overallSize == NULL || infoSize == NULL || |
| 120 | totalMemory == NULL || backtraceSize == NULL) { |
| 121 | return; |
| 122 | } |
tedbo | 9d8be54 | 2010-10-05 13:06:06 -0700 | [diff] [blame] | 123 | *totalMemory = 0; |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 124 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 125 | ScopedPthreadMutexLocker locker(&gAllocationsMutex); |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 126 | |
| 127 | if (gHashTable.count == 0) { |
| 128 | *info = NULL; |
| 129 | *overallSize = 0; |
| 130 | *infoSize = 0; |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 131 | *backtraceSize = 0; |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 132 | return; |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 135 | HashEntry** list = static_cast<HashEntry**>(dlmalloc(sizeof(void*) * gHashTable.count)); |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 136 | |
| 137 | // get the entries into an array to be sorted |
| 138 | int index = 0; |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 139 | for (size_t i = 0 ; i < HASHTABLE_SIZE ; ++i) { |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 140 | HashEntry* entry = gHashTable.slots[i]; |
| 141 | while (entry != NULL) { |
| 142 | list[index] = entry; |
| 143 | *totalMemory = *totalMemory + |
| 144 | ((entry->size & ~SIZE_FLAG_MASK) * entry->allocations); |
| 145 | index++; |
| 146 | entry = entry->next; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // XXX: the protocol doesn't allow variable size for the stack trace (yet) |
| 151 | *infoSize = (sizeof(size_t) * 2) + (sizeof(intptr_t) * BACKTRACE_SIZE); |
| 152 | *overallSize = *infoSize * gHashTable.count; |
| 153 | *backtraceSize = BACKTRACE_SIZE; |
| 154 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 155 | // now get a byte array big enough for this |
| 156 | *info = static_cast<uint8_t*>(dlmalloc(*overallSize)); |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 157 | |
| 158 | if (*info == NULL) { |
| 159 | *overallSize = 0; |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 160 | dlfree(list); |
| 161 | return; |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 164 | qsort(list, gHashTable.count, sizeof(void*), hash_entry_compare); |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 165 | |
| 166 | uint8_t* head = *info; |
| 167 | const int count = gHashTable.count; |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 168 | for (int i = 0 ; i < count ; ++i) { |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 169 | HashEntry* entry = list[i]; |
| 170 | size_t entrySize = (sizeof(size_t) * 2) + (sizeof(intptr_t) * entry->numEntries); |
| 171 | if (entrySize < *infoSize) { |
| 172 | /* we're writing less than a full entry, clear out the rest */ |
The Android Open Source Project | 95faece | 2010-04-08 11:11:53 -0700 | [diff] [blame] | 173 | memset(head + entrySize, 0, *infoSize - entrySize); |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 174 | } else { |
| 175 | /* make sure the amount we're copying doesn't exceed the limit */ |
| 176 | entrySize = *infoSize; |
| 177 | } |
| 178 | memcpy(head, &(entry->size), entrySize); |
| 179 | head += *infoSize; |
| 180 | } |
| 181 | |
| 182 | dlfree(list); |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 185 | extern "C" void free_malloc_leak_info(uint8_t* info) { |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 186 | dlfree(info); |
| 187 | } |
| 188 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 189 | extern "C" struct mallinfo mallinfo() { |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 190 | return dlmallinfo(); |
| 191 | } |
| 192 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 193 | extern "C" size_t malloc_usable_size(void* mem) { |
Ian Rogers | 9990891 | 2012-08-17 17:28:15 -0700 | [diff] [blame] | 194 | return dlmalloc_usable_size(mem); |
| 195 | } |
| 196 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 197 | extern "C" void* valloc(size_t bytes) { |
Ian Rogers | 9990891 | 2012-08-17 17:28:15 -0700 | [diff] [blame] | 198 | return dlvalloc(bytes); |
| 199 | } |
| 200 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 201 | extern "C" void* pvalloc(size_t bytes) { |
Ian Rogers | 9990891 | 2012-08-17 17:28:15 -0700 | [diff] [blame] | 202 | return dlpvalloc(bytes); |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 203 | } |
| 204 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 205 | extern "C" int posix_memalign(void** memptr, size_t alignment, size_t size) { |
Brian Carlstrom | bfc1d97 | 2012-08-20 18:28:20 -0700 | [diff] [blame] | 206 | return dlposix_memalign(memptr, alignment, size); |
| 207 | } |
| 208 | |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 209 | /* Support for malloc debugging. |
| 210 | * Note that if USE_DL_PREFIX is not defined, it's assumed that memory |
| 211 | * allocation routines are implemented somewhere else, so all our custom |
| 212 | * malloc routines should not be compiled at all. |
| 213 | */ |
| 214 | #ifdef USE_DL_PREFIX |
| 215 | |
| 216 | /* Table for dispatching malloc calls, initialized with default dispatchers. */ |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 217 | extern const MallocDebug __libc_malloc_default_dispatch; |
| 218 | const MallocDebug __libc_malloc_default_dispatch __attribute__((aligned(32))) = { |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 219 | dlmalloc, dlfree, dlcalloc, dlrealloc, dlmemalign |
| 220 | }; |
| 221 | |
| 222 | /* Selector of dispatch table to use for dispatching malloc calls. */ |
| 223 | const MallocDebug* __libc_malloc_dispatch = &__libc_malloc_default_dispatch; |
| 224 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 225 | extern "C" void* malloc(size_t bytes) { |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 226 | return __libc_malloc_dispatch->malloc(bytes); |
| 227 | } |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 228 | |
| 229 | extern "C" void free(void* mem) { |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 230 | __libc_malloc_dispatch->free(mem); |
| 231 | } |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 232 | |
| 233 | extern "C" void* calloc(size_t n_elements, size_t elem_size) { |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 234 | return __libc_malloc_dispatch->calloc(n_elements, elem_size); |
| 235 | } |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 236 | |
| 237 | extern "C" void* realloc(void* oldMem, size_t bytes) { |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 238 | return __libc_malloc_dispatch->realloc(oldMem, bytes); |
| 239 | } |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 240 | |
| 241 | extern "C" void* memalign(size_t alignment, size_t bytes) { |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 242 | return __libc_malloc_dispatch->memalign(alignment, bytes); |
| 243 | } |
| 244 | |
| 245 | /* We implement malloc debugging only in libc.so, so code bellow |
| 246 | * must be excluded if we compile this file for static libc.a |
| 247 | */ |
| 248 | #ifndef LIBC_STATIC |
| 249 | #include <sys/system_properties.h> |
| 250 | #include <dlfcn.h> |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 251 | #include <stdio.h> |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 252 | #include "logd.h" |
| 253 | |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 254 | /* Table for dispatching malloc calls, depending on environment. */ |
| 255 | static MallocDebug gMallocUse __attribute__((aligned(32))) = { |
| 256 | dlmalloc, dlfree, dlcalloc, dlrealloc, dlmemalign |
| 257 | }; |
| 258 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 259 | extern char* __progname; |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 260 | |
| 261 | /* Handle to shared library where actual memory allocation is implemented. |
| 262 | * This library is loaded and memory allocation calls are redirected there |
| 263 | * when libc.debug.malloc environment variable contains value other than |
| 264 | * zero: |
| 265 | * 1 - For memory leak detections. |
| 266 | * 5 - For filling allocated / freed memory with patterns defined by |
| 267 | * CHK_SENTINEL_VALUE, and CHK_FILL_FREE macros. |
| 268 | * 10 - For adding pre-, and post- allocation stubs in order to detect |
| 269 | * buffer overruns. |
Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 270 | * Note that emulator's memory allocation instrumentation is not controlled by |
| 271 | * libc.debug.malloc value, but rather by emulator, started with -memcheck |
| 272 | * option. Note also, that if emulator has started with -memcheck option, |
| 273 | * emulator's instrumented memory allocation will take over value saved in |
| 274 | * libc.debug.malloc. In other words, if emulator has started with -memcheck |
| 275 | * option, libc.debug.malloc value is ignored. |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 276 | * Actual functionality for debug levels 1-10 is implemented in |
Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 277 | * libc_malloc_debug_leak.so, while functionality for emultor's instrumented |
| 278 | * allocations is implemented in libc_malloc_debug_qemu.so and can be run inside |
| 279 | * the emulator only. |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 280 | */ |
| 281 | static void* libc_malloc_impl_handle = NULL; |
| 282 | |
Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 283 | /* Make sure we have MALLOC_ALIGNMENT that matches the one that is |
| 284 | * used in dlmalloc. Emulator's memchecker needs this value to properly |
| 285 | * align its guarding zones. |
| 286 | */ |
| 287 | #ifndef MALLOC_ALIGNMENT |
| 288 | #define MALLOC_ALIGNMENT ((size_t)8U) |
| 289 | #endif /* MALLOC_ALIGNMENT */ |
| 290 | |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 291 | /* This variable is set to the value of property libc.debug.malloc.backlog, |
| 292 | * when the value of libc.debug.malloc = 10. It determines the size of the |
| 293 | * backlog we use to detect multiple frees. If the property is not set, the |
| 294 | * backlog length defaults to an internal constant defined in |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 295 | * malloc_debug_check.cpp. |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 296 | */ |
| 297 | unsigned int malloc_double_free_backlog; |
| 298 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 299 | static void InitMalloc(MallocDebug* table, int debug_level, const char* prefix) { |
| 300 | __libc_android_log_print(ANDROID_LOG_INFO, "libc", "%s: using libc.debug.malloc %d (%s)\n", |
| 301 | __progname, debug_level, prefix); |
| 302 | |
| 303 | char symbol[128]; |
| 304 | |
| 305 | snprintf(symbol, sizeof(symbol), "%s_malloc", prefix); |
| 306 | table->malloc = reinterpret_cast<MallocDebugMalloc>(dlsym(libc_malloc_impl_handle, symbol)); |
| 307 | if (table->malloc == NULL) { |
| 308 | error_log("%s: dlsym(\"%s\") failed", __progname, symbol); |
| 309 | } |
| 310 | |
| 311 | snprintf(symbol, sizeof(symbol), "%s_free", prefix); |
| 312 | table->free = reinterpret_cast<MallocDebugFree>(dlsym(libc_malloc_impl_handle, symbol)); |
| 313 | if (table->free == NULL) { |
| 314 | error_log("%s: dlsym(\"%s\") failed", __progname, symbol); |
| 315 | } |
| 316 | |
| 317 | snprintf(symbol, sizeof(symbol), "%s_calloc", prefix); |
| 318 | table->calloc = reinterpret_cast<MallocDebugCalloc>(dlsym(libc_malloc_impl_handle, symbol)); |
| 319 | if (table->calloc == NULL) { |
| 320 | error_log("%s: dlsym(\"%s\") failed", __progname, symbol); |
| 321 | } |
| 322 | |
| 323 | snprintf(symbol, sizeof(symbol), "%s_realloc", prefix); |
| 324 | table->realloc = reinterpret_cast<MallocDebugRealloc>(dlsym(libc_malloc_impl_handle, symbol)); |
| 325 | if (table->realloc == NULL) { |
| 326 | error_log("%s: dlsym(\"%s\") failed", __progname, symbol); |
| 327 | } |
| 328 | |
| 329 | snprintf(symbol, sizeof(symbol), "%s_memalign", prefix); |
| 330 | table->memalign = reinterpret_cast<MallocDebugMemalign>(dlsym(libc_malloc_impl_handle, symbol)); |
| 331 | if (table->memalign == NULL) { |
| 332 | error_log("%s: dlsym(\"%s\") failed", __progname, symbol); |
| 333 | } |
| 334 | } |
| 335 | |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 336 | /* Initializes memory allocation framework once per process. */ |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 337 | static void malloc_init_impl() { |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 338 | const char* so_name = NULL; |
Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 339 | MallocDebugInit malloc_debug_initialize = NULL; |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 340 | unsigned int qemu_running = 0; |
Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 341 | unsigned int debug_level = 0; |
| 342 | unsigned int memcheck_enabled = 0; |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 343 | char env[PROP_VALUE_MAX]; |
Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 344 | char memcheck_tracing[PROP_VALUE_MAX]; |
Iliyan Malchev | 7d2e24e | 2012-05-29 16:46:17 -0700 | [diff] [blame] | 345 | char debug_program[PROP_VALUE_MAX]; |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 346 | |
Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 347 | /* Get custom malloc debug level. Note that emulator started with |
| 348 | * memory checking option will have priority over debug level set in |
| 349 | * libc.debug.malloc system property. */ |
| 350 | if (__system_property_get("ro.kernel.qemu", env) && atoi(env)) { |
| 351 | qemu_running = 1; |
| 352 | if (__system_property_get("ro.kernel.memcheck", memcheck_tracing)) { |
| 353 | if (memcheck_tracing[0] != '0') { |
| 354 | // Emulator has started with memory tracing enabled. Enforce it. |
| 355 | debug_level = 20; |
| 356 | memcheck_enabled = 1; |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | /* If debug level has not been set by memcheck option in the emulator, |
| 362 | * lets grab it from libc.debug.malloc system property. */ |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 363 | if (debug_level == 0 && __system_property_get("libc.debug.malloc", env)) { |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 364 | debug_level = atoi(env); |
| 365 | } |
| 366 | |
| 367 | /* Debug level 0 means that we should use dlxxx allocation |
Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 368 | * routines (default). */ |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 369 | if (debug_level == 0) { |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 370 | return; |
| 371 | } |
| 372 | |
Iliyan Malchev | 7d2e24e | 2012-05-29 16:46:17 -0700 | [diff] [blame] | 373 | /* If libc.debug.malloc.program is set and is not a substring of progname, |
| 374 | * then exit. |
| 375 | */ |
| 376 | if (__system_property_get("libc.debug.malloc.program", debug_program)) { |
| 377 | if (!strstr(__progname, debug_program)) { |
| 378 | return; |
| 379 | } |
| 380 | } |
| 381 | |
Elliott Hughes | e5d5f7f | 2012-10-09 17:23:09 -0700 | [diff] [blame] | 382 | // Choose the appropriate .so for the requested debug level. |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 383 | switch (debug_level) { |
| 384 | case 1: |
| 385 | case 5: |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 386 | case 10: { |
| 387 | char debug_backlog[PROP_VALUE_MAX]; |
| 388 | if (__system_property_get("libc.debug.malloc.backlog", debug_backlog)) { |
| 389 | malloc_double_free_backlog = atoi(debug_backlog); |
| 390 | info_log("%s: setting backlog length to %d\n", |
| 391 | __progname, malloc_double_free_backlog); |
| 392 | } |
| 393 | |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 394 | so_name = "/system/lib/libc_malloc_debug_leak.so"; |
| 395 | break; |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 396 | } |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 397 | case 20: |
Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 398 | // Quick check: debug level 20 can only be handled in emulator. |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 399 | if (!qemu_running) { |
Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 400 | error_log("%s: Debug level %d can only be set in emulator\n", |
| 401 | __progname, debug_level); |
| 402 | return; |
| 403 | } |
| 404 | // Make sure that memory checking has been enabled in emulator. |
| 405 | if (!memcheck_enabled) { |
| 406 | error_log("%s: Memory checking is not enabled in the emulator\n", |
| 407 | __progname); |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 408 | return; |
| 409 | } |
| 410 | so_name = "/system/lib/libc_malloc_debug_qemu.so"; |
| 411 | break; |
| 412 | default: |
Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 413 | error_log("%s: Debug level %d is unknown\n", |
| 414 | __progname, debug_level); |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 415 | return; |
| 416 | } |
| 417 | |
| 418 | // Load .so that implements the required malloc debugging functionality. |
| 419 | libc_malloc_impl_handle = dlopen(so_name, RTLD_LAZY); |
| 420 | if (libc_malloc_impl_handle == NULL) { |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 421 | error_log("%s: Missing module %s required for malloc debug level %d: %s", |
| 422 | __progname, so_name, debug_level, dlerror()); |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 423 | return; |
| 424 | } |
| 425 | |
Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 426 | // Initialize malloc debugging in the loaded module. |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 427 | malloc_debug_initialize = reinterpret_cast<MallocDebugInit>(dlsym(libc_malloc_impl_handle, |
| 428 | "malloc_debug_initialize")); |
Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 429 | if (malloc_debug_initialize == NULL) { |
| 430 | error_log("%s: Initialization routine is not found in %s\n", |
| 431 | __progname, so_name); |
| 432 | dlclose(libc_malloc_impl_handle); |
| 433 | return; |
| 434 | } |
| 435 | if (malloc_debug_initialize()) { |
| 436 | dlclose(libc_malloc_impl_handle); |
| 437 | return; |
| 438 | } |
| 439 | |
| 440 | if (debug_level == 20) { |
| 441 | // For memory checker we need to do extra initialization. |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 442 | typedef int (*MemCheckInit)(int, const char*); |
| 443 | MemCheckInit memcheck_initialize = |
| 444 | reinterpret_cast<MemCheckInit>(dlsym(libc_malloc_impl_handle, |
| 445 | "memcheck_initialize")); |
Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 446 | if (memcheck_initialize == NULL) { |
| 447 | error_log("%s: memcheck_initialize routine is not found in %s\n", |
| 448 | __progname, so_name); |
| 449 | dlclose(libc_malloc_impl_handle); |
| 450 | return; |
| 451 | } |
| 452 | if (memcheck_initialize(MALLOC_ALIGNMENT, memcheck_tracing)) { |
| 453 | dlclose(libc_malloc_impl_handle); |
| 454 | return; |
| 455 | } |
| 456 | } |
| 457 | |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 458 | // Initialize malloc dispatch table with appropriate routines. |
| 459 | switch (debug_level) { |
| 460 | case 1: |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 461 | InitMalloc(&gMallocUse, debug_level, "leak"); |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 462 | break; |
| 463 | case 5: |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 464 | InitMalloc(&gMallocUse, debug_level, "fill"); |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 465 | break; |
| 466 | case 10: |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 467 | InitMalloc(&gMallocUse, debug_level, "chk"); |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 468 | break; |
| 469 | case 20: |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 470 | InitMalloc(&gMallocUse, debug_level, "qemu_instrumented"); |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 471 | break; |
| 472 | default: |
| 473 | break; |
| 474 | } |
| 475 | |
| 476 | // Make sure dispatch table is initialized |
| 477 | if ((gMallocUse.malloc == NULL) || |
| 478 | (gMallocUse.free == NULL) || |
| 479 | (gMallocUse.calloc == NULL) || |
| 480 | (gMallocUse.realloc == NULL) || |
| 481 | (gMallocUse.memalign == NULL)) { |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 482 | error_log("%s: some symbols for libc.debug.malloc level %d were not found (see above)", |
| 483 | __progname, debug_level); |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 484 | dlclose(libc_malloc_impl_handle); |
| 485 | libc_malloc_impl_handle = NULL; |
| 486 | } else { |
| 487 | __libc_malloc_dispatch = &gMallocUse; |
| 488 | } |
| 489 | } |
| 490 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 491 | static void malloc_fini_impl() { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 492 | if (libc_malloc_impl_handle) { |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 493 | MallocDebugFini malloc_debug_finalize = |
| 494 | reinterpret_cast<MallocDebugFini>(dlsym(libc_malloc_impl_handle, |
| 495 | "malloc_debug_finalize")); |
| 496 | if (malloc_debug_finalize) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 497 | malloc_debug_finalize(); |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 498 | } |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 499 | } |
| 500 | } |
| 501 | |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 502 | static pthread_once_t malloc_init_once_ctl = PTHREAD_ONCE_INIT; |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 503 | static pthread_once_t malloc_fini_once_ctl = PTHREAD_ONCE_INIT; |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 504 | |
| 505 | #endif // !LIBC_STATIC |
| 506 | #endif // USE_DL_PREFIX |
| 507 | |
| 508 | /* Initializes memory allocation framework. |
| 509 | * This routine is called from __libc_init routines implemented |
| 510 | * in libc_init_static.c and libc_init_dynamic.c files. |
| 511 | */ |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 512 | extern "C" void malloc_debug_init() { |
Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 513 | /* We need to initialize malloc iff we implement here custom |
| 514 | * malloc routines (i.e. USE_DL_PREFIX is defined) for libc.so */ |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 515 | #if defined(USE_DL_PREFIX) && !defined(LIBC_STATIC) |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 516 | if (pthread_once(&malloc_init_once_ctl, malloc_init_impl)) { |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 517 | error_log("Unable to initialize malloc_debug component."); |
| 518 | } |
| 519 | #endif // USE_DL_PREFIX && !LIBC_STATIC |
| 520 | } |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 521 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 522 | extern "C" void malloc_debug_fini() { |
| 523 | /* We need to finalize malloc iff we implement here custom |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 524 | * malloc routines (i.e. USE_DL_PREFIX is defined) for libc.so */ |
| 525 | #if defined(USE_DL_PREFIX) && !defined(LIBC_STATIC) |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 526 | if (pthread_once(&malloc_fini_once_ctl, malloc_fini_impl)) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 527 | error_log("Unable to finalize malloc_debug component."); |
| 528 | } |
| 529 | #endif // USE_DL_PREFIX && !LIBC_STATIC |
| 530 | } |