| 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" | 
| Elliott Hughes | eb847bc | 2013-10-09 15:50:50 -0700 | [diff] [blame] | 50 | #include "private/ScopedPthreadMutexLocker.h" | 
| Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 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) | 
| Elliott Hughes | 239e7a0 | 2013-01-25 17:13:45 -0800 | [diff] [blame] | 151 | *infoSize = (sizeof(size_t) * 2) + (sizeof(uintptr_t) * BACKTRACE_SIZE); | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 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]; | 
| Elliott Hughes | 239e7a0 | 2013-01-25 17:13:45 -0800 | [diff] [blame] | 170 | size_t entrySize = (sizeof(size_t) * 2) + (sizeof(uintptr_t) * entry->numEntries); | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 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" void* valloc(size_t bytes) { | 
| Ian Rogers | 9990891 | 2012-08-17 17:28:15 -0700 | [diff] [blame] | 194 | return dlvalloc(bytes); | 
|  | 195 | } | 
|  | 196 |  | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 197 | extern "C" void* pvalloc(size_t bytes) { | 
| Ian Rogers | 9990891 | 2012-08-17 17:28:15 -0700 | [diff] [blame] | 198 | return dlpvalloc(bytes); | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 199 | } | 
|  | 200 |  | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 201 | 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] | 202 | return dlposix_memalign(memptr, alignment, size); | 
|  | 203 | } | 
|  | 204 |  | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 205 | /* Support for malloc debugging. | 
|  | 206 | * Note that if USE_DL_PREFIX is not defined, it's assumed that memory | 
|  | 207 | * allocation routines are implemented somewhere else, so all our custom | 
|  | 208 | * malloc routines should not be compiled at all. | 
|  | 209 | */ | 
|  | 210 | #ifdef USE_DL_PREFIX | 
|  | 211 |  | 
|  | 212 | /* Table for dispatching malloc calls, initialized with default dispatchers. */ | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 213 | extern const MallocDebug __libc_malloc_default_dispatch; | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 214 | const MallocDebug __libc_malloc_default_dispatch __attribute__((aligned(32))) = | 
|  | 215 | { | 
|  | 216 | dlmalloc, dlfree, dlcalloc, dlrealloc, dlmemalign, dlmalloc_usable_size, | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 217 | }; | 
|  | 218 |  | 
|  | 219 | /* Selector of dispatch table to use for dispatching malloc calls. */ | 
|  | 220 | const MallocDebug* __libc_malloc_dispatch = &__libc_malloc_default_dispatch; | 
|  | 221 |  | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 222 | extern "C" void* malloc(size_t bytes) { | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 223 | return __libc_malloc_dispatch->malloc(bytes); | 
|  | 224 | } | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 225 |  | 
|  | 226 | extern "C" void free(void* mem) { | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 227 | __libc_malloc_dispatch->free(mem); | 
|  | 228 | } | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 229 |  | 
|  | 230 | extern "C" void* calloc(size_t n_elements, size_t elem_size) { | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 231 | return __libc_malloc_dispatch->calloc(n_elements, elem_size); | 
|  | 232 | } | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 233 |  | 
|  | 234 | extern "C" void* realloc(void* oldMem, size_t bytes) { | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 235 | return __libc_malloc_dispatch->realloc(oldMem, bytes); | 
|  | 236 | } | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 237 |  | 
|  | 238 | extern "C" void* memalign(size_t alignment, size_t bytes) { | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 239 | return __libc_malloc_dispatch->memalign(alignment, bytes); | 
|  | 240 | } | 
|  | 241 |  | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 242 | extern "C" size_t malloc_usable_size(const void* mem) { | 
|  | 243 | return __libc_malloc_dispatch->malloc_usable_size(mem); | 
|  | 244 | } | 
|  | 245 |  | 
| Elliott Hughes | db492b3 | 2013-01-03 15:44:03 -0800 | [diff] [blame] | 246 | /* We implement malloc debugging only in libc.so, so code below | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 247 | * must be excluded if we compile this file for static libc.a | 
|  | 248 | */ | 
|  | 249 | #ifndef LIBC_STATIC | 
|  | 250 | #include <sys/system_properties.h> | 
|  | 251 | #include <dlfcn.h> | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 252 | #include <stdio.h> | 
| Elliott Hughes | eb847bc | 2013-10-09 15:50:50 -0700 | [diff] [blame] | 253 | #include "private/libc_logging.h" | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 254 |  | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 255 | /* Table for dispatching malloc calls, depending on environment. */ | 
|  | 256 | static MallocDebug gMallocUse __attribute__((aligned(32))) = { | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 257 | dlmalloc, dlfree, dlcalloc, dlrealloc, dlmemalign, dlmalloc_usable_size | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 258 | }; | 
|  | 259 |  | 
| Elliott Hughes | e4ccf5a | 2013-02-07 12:06:44 -0800 | [diff] [blame] | 260 | extern const char* __progname; | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 261 |  | 
|  | 262 | /* Handle to shared library where actual memory allocation is implemented. | 
|  | 263 | * This library is loaded and memory allocation calls are redirected there | 
|  | 264 | * when libc.debug.malloc environment variable contains value other than | 
|  | 265 | * zero: | 
|  | 266 | * 1  - For memory leak detections. | 
|  | 267 | * 5  - For filling allocated / freed memory with patterns defined by | 
|  | 268 | *      CHK_SENTINEL_VALUE, and CHK_FILL_FREE macros. | 
|  | 269 | * 10 - For adding pre-, and post- allocation stubs in order to detect | 
|  | 270 | *      buffer overruns. | 
| Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 271 | * Note that emulator's memory allocation instrumentation is not controlled by | 
|  | 272 | * libc.debug.malloc value, but rather by emulator, started with -memcheck | 
|  | 273 | * option. Note also, that if emulator has started with -memcheck option, | 
|  | 274 | * emulator's instrumented memory allocation will take over value saved in | 
|  | 275 | * libc.debug.malloc. In other words, if emulator has started with -memcheck | 
|  | 276 | * option, libc.debug.malloc value is ignored. | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 277 | * Actual functionality for debug levels 1-10 is implemented in | 
| Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 278 | * libc_malloc_debug_leak.so, while functionality for emultor's instrumented | 
|  | 279 | * allocations is implemented in libc_malloc_debug_qemu.so and can be run inside | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 280 | * the emulator only. | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 281 | */ | 
|  | 282 | static void* libc_malloc_impl_handle = NULL; | 
|  | 283 |  | 
| Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 284 | /* This variable is set to the value of property libc.debug.malloc.backlog, | 
|  | 285 | * when the value of libc.debug.malloc = 10.  It determines the size of the | 
|  | 286 | * backlog we use to detect multiple frees.  If the property is not set, the | 
| Elliott Hughes | 9c81892 | 2013-02-01 17:07:40 -0800 | [diff] [blame] | 287 | * backlog length defaults to BACKLOG_DEFAULT_LEN. | 
| Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 288 | */ | 
| Elliott Hughes | 9c81892 | 2013-02-01 17:07:40 -0800 | [diff] [blame] | 289 | unsigned int gMallocDebugBacklog; | 
|  | 290 | #define BACKLOG_DEFAULT_LEN 100 | 
| Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 291 |  | 
| Elliott Hughes | 9c81892 | 2013-02-01 17:07:40 -0800 | [diff] [blame] | 292 | /* The value of libc.debug.malloc. */ | 
|  | 293 | int gMallocDebugLevel; | 
|  | 294 |  | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 295 | template<typename FunctionType> | 
| Nick Kralevich | 35c1862 | 2013-10-03 14:59:05 -0700 | [diff] [blame] | 296 | static void InitMallocFunction(void* malloc_impl_handler, FunctionType* func, const char* prefix, const char* suffix) { | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 297 | char symbol[128]; | 
|  | 298 | snprintf(symbol, sizeof(symbol), "%s_%s", prefix, suffix); | 
|  | 299 | *func = reinterpret_cast<FunctionType>(dlsym(malloc_impl_handler, symbol)); | 
|  | 300 | if (*func == NULL) { | 
|  | 301 | error_log("%s: dlsym(\"%s\") failed", __progname, symbol); | 
|  | 302 | } | 
|  | 303 | } | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 304 |  | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 305 | static void InitMalloc(void* malloc_impl_handler, MallocDebug* table, const char* prefix) { | 
|  | 306 | __libc_format_log(ANDROID_LOG_INFO, "libc", "%s: using libc.debug.malloc %d (%s)\n", | 
|  | 307 | __progname, gMallocDebugLevel, prefix); | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 308 |  | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 309 | InitMallocFunction<MallocDebugMalloc>(malloc_impl_handler, &table->malloc, prefix, "malloc"); | 
|  | 310 | InitMallocFunction<MallocDebugFree>(malloc_impl_handler, &table->free, prefix, "free"); | 
|  | 311 | InitMallocFunction<MallocDebugCalloc>(malloc_impl_handler, &table->calloc, prefix, "calloc"); | 
|  | 312 | InitMallocFunction<MallocDebugRealloc>(malloc_impl_handler, &table->realloc, prefix, "realloc"); | 
|  | 313 | InitMallocFunction<MallocDebugMemalign>(malloc_impl_handler, &table->memalign, prefix, "memalign"); | 
|  | 314 | InitMallocFunction<MallocDebugMallocUsableSize>(malloc_impl_handler, &table->malloc_usable_size, prefix, "malloc_usable_size"); | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 315 | } | 
|  | 316 |  | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 317 | /* Initializes memory allocation framework once per process. */ | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 318 | static void malloc_init_impl() { | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 319 | const char* so_name = NULL; | 
| Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 320 | MallocDebugInit malloc_debug_initialize = NULL; | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 321 | unsigned int qemu_running = 0; | 
| Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 322 | unsigned int memcheck_enabled = 0; | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 323 | char env[PROP_VALUE_MAX]; | 
| Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 324 | char memcheck_tracing[PROP_VALUE_MAX]; | 
| Iliyan Malchev | 7d2e24e | 2012-05-29 16:46:17 -0700 | [diff] [blame] | 325 | char debug_program[PROP_VALUE_MAX]; | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 326 |  | 
| Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 327 | /* Get custom malloc debug level. Note that emulator started with | 
|  | 328 | * memory checking option will have priority over debug level set in | 
|  | 329 | * libc.debug.malloc system property. */ | 
|  | 330 | if (__system_property_get("ro.kernel.qemu", env) && atoi(env)) { | 
|  | 331 | qemu_running = 1; | 
|  | 332 | if (__system_property_get("ro.kernel.memcheck", memcheck_tracing)) { | 
|  | 333 | if (memcheck_tracing[0] != '0') { | 
|  | 334 | // Emulator has started with memory tracing enabled. Enforce it. | 
| Elliott Hughes | 9c81892 | 2013-02-01 17:07:40 -0800 | [diff] [blame] | 335 | gMallocDebugLevel = 20; | 
| Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 336 | memcheck_enabled = 1; | 
|  | 337 | } | 
|  | 338 | } | 
|  | 339 | } | 
|  | 340 |  | 
|  | 341 | /* If debug level has not been set by memcheck option in the emulator, | 
|  | 342 | * lets grab it from libc.debug.malloc system property. */ | 
| Elliott Hughes | 9c81892 | 2013-02-01 17:07:40 -0800 | [diff] [blame] | 343 | if (gMallocDebugLevel == 0 && __system_property_get("libc.debug.malloc", env)) { | 
|  | 344 | gMallocDebugLevel = atoi(env); | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 345 | } | 
|  | 346 |  | 
|  | 347 | /* Debug level 0 means that we should use dlxxx allocation | 
| Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 348 | * routines (default). */ | 
| Elliott Hughes | 9c81892 | 2013-02-01 17:07:40 -0800 | [diff] [blame] | 349 | if (gMallocDebugLevel == 0) { | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 350 | return; | 
|  | 351 | } | 
|  | 352 |  | 
| Iliyan Malchev | 7d2e24e | 2012-05-29 16:46:17 -0700 | [diff] [blame] | 353 | /* If libc.debug.malloc.program is set and is not a substring of progname, | 
|  | 354 | * then exit. | 
|  | 355 | */ | 
|  | 356 | if (__system_property_get("libc.debug.malloc.program", debug_program)) { | 
|  | 357 | if (!strstr(__progname, debug_program)) { | 
|  | 358 | return; | 
|  | 359 | } | 
|  | 360 | } | 
|  | 361 |  | 
| Elliott Hughes | 84f8b5f | 2013-01-22 18:35:14 -0800 | [diff] [blame] | 362 | // mksh is way too leaky. http://b/7291287. | 
| Elliott Hughes | 9c81892 | 2013-02-01 17:07:40 -0800 | [diff] [blame] | 363 | if (gMallocDebugLevel >= 10) { | 
| Elliott Hughes | 239e7a0 | 2013-01-25 17:13:45 -0800 | [diff] [blame] | 364 | if (strcmp(__progname, "sh") == 0 || strcmp(__progname, "/system/bin/sh") == 0) { | 
|  | 365 | return; | 
|  | 366 | } | 
| Elliott Hughes | 84f8b5f | 2013-01-22 18:35:14 -0800 | [diff] [blame] | 367 | } | 
|  | 368 |  | 
| Elliott Hughes | e5d5f7f | 2012-10-09 17:23:09 -0700 | [diff] [blame] | 369 | // Choose the appropriate .so for the requested debug level. | 
| Elliott Hughes | 9c81892 | 2013-02-01 17:07:40 -0800 | [diff] [blame] | 370 | switch (gMallocDebugLevel) { | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 371 | case 1: | 
|  | 372 | case 5: | 
| Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 373 | case 10: { | 
|  | 374 | char debug_backlog[PROP_VALUE_MAX]; | 
|  | 375 | if (__system_property_get("libc.debug.malloc.backlog", debug_backlog)) { | 
| Elliott Hughes | 9c81892 | 2013-02-01 17:07:40 -0800 | [diff] [blame] | 376 | gMallocDebugBacklog = atoi(debug_backlog); | 
|  | 377 | info_log("%s: setting backlog length to %d\n", __progname, gMallocDebugBacklog); | 
| Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 378 | } | 
| Elliott Hughes | 9c81892 | 2013-02-01 17:07:40 -0800 | [diff] [blame] | 379 | if (gMallocDebugBacklog == 0) { | 
|  | 380 | gMallocDebugBacklog = BACKLOG_DEFAULT_LEN; | 
|  | 381 | } | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 382 | so_name = "/system/lib/libc_malloc_debug_leak.so"; | 
|  | 383 | break; | 
| Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 384 | } | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 385 | case 20: | 
| Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 386 | // Quick check: debug level 20 can only be handled in emulator. | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 387 | if (!qemu_running) { | 
| Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 388 | error_log("%s: Debug level %d can only be set in emulator\n", | 
| Elliott Hughes | 9c81892 | 2013-02-01 17:07:40 -0800 | [diff] [blame] | 389 | __progname, gMallocDebugLevel); | 
| Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 390 | return; | 
|  | 391 | } | 
|  | 392 | // Make sure that memory checking has been enabled in emulator. | 
|  | 393 | if (!memcheck_enabled) { | 
|  | 394 | error_log("%s: Memory checking is not enabled in the emulator\n", | 
|  | 395 | __progname); | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 396 | return; | 
|  | 397 | } | 
|  | 398 | so_name = "/system/lib/libc_malloc_debug_qemu.so"; | 
|  | 399 | break; | 
|  | 400 | default: | 
| Elliott Hughes | 9c81892 | 2013-02-01 17:07:40 -0800 | [diff] [blame] | 401 | error_log("%s: Debug level %d is unknown\n", __progname, gMallocDebugLevel); | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 402 | return; | 
|  | 403 | } | 
|  | 404 |  | 
|  | 405 | // Load .so that implements the required malloc debugging functionality. | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 406 | void* malloc_impl_handle = dlopen(so_name, RTLD_LAZY); | 
|  | 407 | if (malloc_impl_handle == NULL) { | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 408 | error_log("%s: Missing module %s required for malloc debug level %d: %s", | 
| Elliott Hughes | 9c81892 | 2013-02-01 17:07:40 -0800 | [diff] [blame] | 409 | __progname, so_name, gMallocDebugLevel, dlerror()); | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 410 | return; | 
|  | 411 | } | 
|  | 412 |  | 
| Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 413 | // Initialize malloc debugging in the loaded module. | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 414 | malloc_debug_initialize = reinterpret_cast<MallocDebugInit>(dlsym(malloc_impl_handle, | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 415 | "malloc_debug_initialize")); | 
| Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 416 | if (malloc_debug_initialize == NULL) { | 
|  | 417 | error_log("%s: Initialization routine is not found in %s\n", | 
|  | 418 | __progname, so_name); | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 419 | dlclose(malloc_impl_handle); | 
| Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 420 | return; | 
|  | 421 | } | 
| Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 422 | if (malloc_debug_initialize() == -1) { | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 423 | dlclose(malloc_impl_handle); | 
| Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 424 | return; | 
|  | 425 | } | 
|  | 426 |  | 
| Elliott Hughes | 9c81892 | 2013-02-01 17:07:40 -0800 | [diff] [blame] | 427 | if (gMallocDebugLevel == 20) { | 
| Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 428 | // For memory checker we need to do extra initialization. | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 429 | typedef int (*MemCheckInit)(int, const char*); | 
|  | 430 | MemCheckInit memcheck_initialize = | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 431 | reinterpret_cast<MemCheckInit>(dlsym(malloc_impl_handle, | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 432 | "memcheck_initialize")); | 
| Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 433 | if (memcheck_initialize == NULL) { | 
|  | 434 | error_log("%s: memcheck_initialize routine is not found in %s\n", | 
|  | 435 | __progname, so_name); | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 436 | dlclose(malloc_impl_handle); | 
| Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 437 | return; | 
|  | 438 | } | 
| Elliott Hughes | db492b3 | 2013-01-03 15:44:03 -0800 | [diff] [blame] | 439 |  | 
| Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 440 | if (memcheck_initialize(MALLOC_ALIGNMENT, memcheck_tracing)) { | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 441 | dlclose(malloc_impl_handle); | 
| Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 442 | return; | 
|  | 443 | } | 
|  | 444 | } | 
|  | 445 |  | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 446 |  | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 447 | // Initialize malloc dispatch table with appropriate routines. | 
| Elliott Hughes | 9c81892 | 2013-02-01 17:07:40 -0800 | [diff] [blame] | 448 | switch (gMallocDebugLevel) { | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 449 | case 1: | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 450 | InitMalloc(malloc_impl_handle, &gMallocUse, "leak"); | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 451 | break; | 
|  | 452 | case 5: | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 453 | InitMalloc(malloc_impl_handle, &gMallocUse, "fill"); | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 454 | break; | 
|  | 455 | case 10: | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 456 | InitMalloc(malloc_impl_handle, &gMallocUse, "chk"); | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 457 | break; | 
|  | 458 | case 20: | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 459 | InitMalloc(malloc_impl_handle, &gMallocUse, "qemu_instrumented"); | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 460 | break; | 
|  | 461 | default: | 
|  | 462 | break; | 
|  | 463 | } | 
|  | 464 |  | 
|  | 465 | // Make sure dispatch table is initialized | 
|  | 466 | if ((gMallocUse.malloc == NULL) || | 
|  | 467 | (gMallocUse.free == NULL) || | 
|  | 468 | (gMallocUse.calloc == NULL) || | 
|  | 469 | (gMallocUse.realloc == NULL) || | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 470 | (gMallocUse.memalign == NULL) || | 
|  | 471 | (gMallocUse.malloc_usable_size == NULL)) { | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 472 | error_log("%s: some symbols for libc.debug.malloc level %d were not found (see above)", | 
| Elliott Hughes | 9c81892 | 2013-02-01 17:07:40 -0800 | [diff] [blame] | 473 | __progname, gMallocDebugLevel); | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 474 | dlclose(malloc_impl_handle); | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 475 | } else { | 
|  | 476 | __libc_malloc_dispatch = &gMallocUse; | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 477 | libc_malloc_impl_handle = malloc_impl_handle; | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 478 | } | 
|  | 479 | } | 
|  | 480 |  | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 481 | static void malloc_fini_impl() { | 
| Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 482 | // Our BSD stdio implementation doesn't close the standard streams, it only flushes them. | 
|  | 483 | // And it doesn't do that until its atexit handler (_cleanup) is run, and we run first! | 
|  | 484 | // It's great that other unclosed FILE*s show up as malloc leaks, but we need to manually | 
|  | 485 | // clean up the standard streams ourselves. | 
|  | 486 | fclose(stdin); | 
|  | 487 | fclose(stdout); | 
|  | 488 | fclose(stderr); | 
|  | 489 |  | 
|  | 490 | if (libc_malloc_impl_handle != NULL) { | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 491 | MallocDebugFini malloc_debug_finalize = | 
|  | 492 | reinterpret_cast<MallocDebugFini>(dlsym(libc_malloc_impl_handle, | 
|  | 493 | "malloc_debug_finalize")); | 
| Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 494 | if (malloc_debug_finalize != NULL) { | 
| Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 495 | malloc_debug_finalize(); | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 496 | } | 
| Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 497 | } | 
|  | 498 | } | 
|  | 499 |  | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 500 | static pthread_once_t  malloc_init_once_ctl = PTHREAD_ONCE_INIT; | 
| Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 501 | static pthread_once_t  malloc_fini_once_ctl = PTHREAD_ONCE_INIT; | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 502 |  | 
|  | 503 | #endif  // !LIBC_STATIC | 
|  | 504 | #endif  // USE_DL_PREFIX | 
|  | 505 |  | 
|  | 506 | /* Initializes memory allocation framework. | 
|  | 507 | * This routine is called from __libc_init routines implemented | 
|  | 508 | * in libc_init_static.c and libc_init_dynamic.c files. | 
|  | 509 | */ | 
| Kito Cheng | ea48974 | 2013-04-12 16:13:34 +0800 | [diff] [blame] | 510 | extern "C" __LIBC_HIDDEN__ void malloc_debug_init() { | 
| Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 511 | /* We need to initialize malloc iff we implement here custom | 
|  | 512 | * malloc routines (i.e. USE_DL_PREFIX is defined) for libc.so */ | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 513 | #if defined(USE_DL_PREFIX) && !defined(LIBC_STATIC) | 
| Elliott Hughes | db492b3 | 2013-01-03 15:44:03 -0800 | [diff] [blame] | 514 | if (pthread_once(&malloc_init_once_ctl, malloc_init_impl)) { | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 515 | error_log("Unable to initialize malloc_debug component."); | 
|  | 516 | } | 
|  | 517 | #endif  // USE_DL_PREFIX && !LIBC_STATIC | 
|  | 518 | } | 
| Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 519 |  | 
| Kito Cheng | ea48974 | 2013-04-12 16:13:34 +0800 | [diff] [blame] | 520 | extern "C" __LIBC_HIDDEN__ void malloc_debug_fini() { | 
| Elliott Hughes | db492b3 | 2013-01-03 15:44:03 -0800 | [diff] [blame] | 521 | /* We need to finalize malloc iff we implement here custom | 
| Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 522 | * malloc routines (i.e. USE_DL_PREFIX is defined) for libc.so */ | 
|  | 523 | #if defined(USE_DL_PREFIX) && !defined(LIBC_STATIC) | 
| Elliott Hughes | db492b3 | 2013-01-03 15:44:03 -0800 | [diff] [blame] | 524 | if (pthread_once(&malloc_fini_once_ctl, malloc_fini_impl)) { | 
| Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 525 | error_log("Unable to finalize malloc_debug component."); | 
|  | 526 | } | 
|  | 527 | #endif  // USE_DL_PREFIX && !LIBC_STATIC | 
|  | 528 | } |