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 | |
Elliott Hughes | eb847bc | 2013-10-09 15:50:50 -0700 | [diff] [blame] | 49 | #include "private/ScopedPthreadMutexLocker.h" |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 50 | |
Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 51 | // In a VM process, this is set to 1 after fork()ing out of zygote. |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 52 | int gMallocLeakZygoteChild = 0; |
| 53 | |
Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 54 | static HashTable g_hash_table; |
| 55 | |
| 56 | // Support for malloc debugging. |
| 57 | // Table for dispatching malloc calls, initialized with default dispatchers. |
| 58 | static const MallocDebug __libc_malloc_default_dispatch __attribute__((aligned(32))) = { |
| 59 | Malloc(malloc), Malloc(free), Malloc(calloc), Malloc(realloc), Malloc(memalign), Malloc(malloc_usable_size), |
| 60 | }; |
| 61 | |
| 62 | // Selector of dispatch table to use for dispatching malloc calls. |
Elliott Hughes | 14442bb | 2014-06-04 15:18:36 -0700 | [diff] [blame^] | 63 | // TODO: fix http://b/15432753 and make this static again. |
| 64 | const MallocDebug* __libc_malloc_dispatch = &__libc_malloc_default_dispatch; |
Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 65 | |
| 66 | // Handle to shared library where actual memory allocation is implemented. |
| 67 | // This library is loaded and memory allocation calls are redirected there |
| 68 | // when libc.debug.malloc environment variable contains value other than |
| 69 | // zero: |
| 70 | // 1 - For memory leak detections. |
| 71 | // 5 - For filling allocated / freed memory with patterns defined by |
| 72 | // CHK_SENTINEL_VALUE, and CHK_FILL_FREE macros. |
| 73 | // 10 - For adding pre-, and post- allocation stubs in order to detect |
| 74 | // buffer overruns. |
| 75 | // Note that emulator's memory allocation instrumentation is not controlled by |
| 76 | // libc.debug.malloc value, but rather by emulator, started with -memcheck |
| 77 | // option. Note also, that if emulator has started with -memcheck option, |
| 78 | // emulator's instrumented memory allocation will take over value saved in |
| 79 | // libc.debug.malloc. In other words, if emulator has started with -memcheck |
| 80 | // option, libc.debug.malloc value is ignored. |
| 81 | // Actual functionality for debug levels 1-10 is implemented in |
| 82 | // libc_malloc_debug_leak.so, while functionality for emulator's instrumented |
| 83 | // allocations is implemented in libc_malloc_debug_qemu.so and can be run inside |
| 84 | // the emulator only. |
| 85 | #if !defined(LIBC_STATIC) |
| 86 | static void* libc_malloc_impl_handle = NULL; |
| 87 | #endif |
| 88 | |
| 89 | |
| 90 | // The value of libc.debug.malloc. |
| 91 | #if !defined(LIBC_STATIC) |
| 92 | static int g_malloc_debug_level = 0; |
| 93 | #endif |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 94 | |
| 95 | // ============================================================================= |
| 96 | // output functions |
| 97 | // ============================================================================= |
| 98 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 99 | static int hash_entry_compare(const void* arg1, const void* arg2) { |
Christopher Tate | 52e7d3d | 2010-08-09 13:43:46 -0700 | [diff] [blame] | 100 | int result; |
| 101 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 102 | const HashEntry* e1 = *static_cast<HashEntry* const*>(arg1); |
| 103 | const HashEntry* e2 = *static_cast<HashEntry* const*>(arg2); |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 104 | |
Christopher Tate | 52e7d3d | 2010-08-09 13:43:46 -0700 | [diff] [blame] | 105 | // if one or both arg pointers are null, deal gracefully |
| 106 | if (e1 == NULL) { |
| 107 | result = (e2 == NULL) ? 0 : 1; |
| 108 | } else if (e2 == NULL) { |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 109 | result = -1; |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 110 | } else { |
Christopher Tate | 52e7d3d | 2010-08-09 13:43:46 -0700 | [diff] [blame] | 111 | size_t nbAlloc1 = e1->allocations; |
| 112 | size_t nbAlloc2 = e2->allocations; |
| 113 | size_t size1 = e1->size & ~SIZE_FLAG_MASK; |
| 114 | size_t size2 = e2->size & ~SIZE_FLAG_MASK; |
| 115 | size_t alloc1 = nbAlloc1 * size1; |
| 116 | size_t alloc2 = nbAlloc2 * size2; |
| 117 | |
| 118 | // sort in descending order by: |
| 119 | // 1) total size |
| 120 | // 2) number of allocations |
| 121 | // |
| 122 | // This is used for sorting, not determination of equality, so we don't |
| 123 | // need to compare the bit flags. |
Christopher Tate | 52e7d3d | 2010-08-09 13:43:46 -0700 | [diff] [blame] | 124 | if (alloc1 > alloc2) { |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 125 | result = -1; |
Christopher Tate | 52e7d3d | 2010-08-09 13:43:46 -0700 | [diff] [blame] | 126 | } else if (alloc1 < alloc2) { |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 127 | result = 1; |
| 128 | } else { |
Christopher Tate | 52e7d3d | 2010-08-09 13:43:46 -0700 | [diff] [blame] | 129 | if (nbAlloc1 > nbAlloc2) { |
| 130 | result = -1; |
| 131 | } else if (nbAlloc1 < nbAlloc2) { |
| 132 | result = 1; |
| 133 | } else { |
| 134 | result = 0; |
| 135 | } |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | return result; |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * Retrieve native heap information. |
| 143 | * |
| 144 | * "*info" is set to a buffer we allocate |
| 145 | * "*overallSize" is set to the size of the "info" buffer |
| 146 | * "*infoSize" is set to the size of a single entry |
| 147 | * "*totalMemory" is set to the sum of all allocations we're tracking; does |
| 148 | * not include heap overhead |
| 149 | * "*backtraceSize" is set to the maximum number of entries in the back trace |
| 150 | */ |
Elliott Hughes | 7c9923d | 2014-05-16 16:29:55 -0700 | [diff] [blame] | 151 | |
| 152 | // Exported for use by ddms. |
| 153 | extern "C" void get_malloc_leak_info(uint8_t** info, size_t* overallSize, |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 154 | size_t* infoSize, size_t* totalMemory, size_t* backtraceSize) { |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 155 | // don't do anything if we have invalid arguments |
| 156 | if (info == NULL || overallSize == NULL || infoSize == NULL || |
| 157 | totalMemory == NULL || backtraceSize == NULL) { |
| 158 | return; |
| 159 | } |
tedbo | 9d8be54 | 2010-10-05 13:06:06 -0700 | [diff] [blame] | 160 | *totalMemory = 0; |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 161 | |
Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 162 | ScopedPthreadMutexLocker locker(&g_hash_table.lock); |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 163 | |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 164 | if (g_hash_table.count == 0) { |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 165 | *info = NULL; |
| 166 | *overallSize = 0; |
| 167 | *infoSize = 0; |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 168 | *backtraceSize = 0; |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 169 | return; |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Christopher Ferris | 72bbd42 | 2014-05-08 11:14:03 -0700 | [diff] [blame] | 172 | HashEntry** list = static_cast<HashEntry**>(Malloc(malloc)(sizeof(void*) * g_hash_table.count)); |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 173 | |
| 174 | // get the entries into an array to be sorted |
| 175 | int index = 0; |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 176 | for (size_t i = 0 ; i < HASHTABLE_SIZE ; ++i) { |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 177 | HashEntry* entry = g_hash_table.slots[i]; |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 178 | while (entry != NULL) { |
| 179 | list[index] = entry; |
| 180 | *totalMemory = *totalMemory + |
| 181 | ((entry->size & ~SIZE_FLAG_MASK) * entry->allocations); |
| 182 | index++; |
| 183 | entry = entry->next; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | // 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] | 188 | *infoSize = (sizeof(size_t) * 2) + (sizeof(uintptr_t) * BACKTRACE_SIZE); |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 189 | *overallSize = *infoSize * g_hash_table.count; |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 190 | *backtraceSize = BACKTRACE_SIZE; |
| 191 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 192 | // now get a byte array big enough for this |
Christopher Ferris | 72bbd42 | 2014-05-08 11:14:03 -0700 | [diff] [blame] | 193 | *info = static_cast<uint8_t*>(Malloc(malloc)(*overallSize)); |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 194 | |
| 195 | if (*info == NULL) { |
| 196 | *overallSize = 0; |
Christopher Ferris | 72bbd42 | 2014-05-08 11:14:03 -0700 | [diff] [blame] | 197 | Malloc(free)(list); |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 198 | return; |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 199 | } |
| 200 | |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 201 | qsort(list, g_hash_table.count, sizeof(void*), hash_entry_compare); |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 202 | |
| 203 | uint8_t* head = *info; |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 204 | const int count = g_hash_table.count; |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 205 | for (int i = 0 ; i < count ; ++i) { |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 206 | HashEntry* entry = list[i]; |
Elliott Hughes | 239e7a0 | 2013-01-25 17:13:45 -0800 | [diff] [blame] | 207 | size_t entrySize = (sizeof(size_t) * 2) + (sizeof(uintptr_t) * entry->numEntries); |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 208 | if (entrySize < *infoSize) { |
| 209 | /* 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] | 210 | memset(head + entrySize, 0, *infoSize - entrySize); |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 211 | } else { |
| 212 | /* make sure the amount we're copying doesn't exceed the limit */ |
| 213 | entrySize = *infoSize; |
| 214 | } |
| 215 | memcpy(head, &(entry->size), entrySize); |
| 216 | head += *infoSize; |
| 217 | } |
| 218 | |
Christopher Ferris | 72bbd42 | 2014-05-08 11:14:03 -0700 | [diff] [blame] | 219 | Malloc(free)(list); |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 220 | } |
| 221 | |
Elliott Hughes | 7c9923d | 2014-05-16 16:29:55 -0700 | [diff] [blame] | 222 | // Exported for use by ddms. |
| 223 | extern "C" void free_malloc_leak_info(uint8_t* info) { |
Christopher Ferris | 72bbd42 | 2014-05-08 11:14:03 -0700 | [diff] [blame] | 224 | Malloc(free)(info); |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 225 | } |
| 226 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 227 | extern "C" struct mallinfo mallinfo() { |
Christopher Ferris | 72bbd42 | 2014-05-08 11:14:03 -0700 | [diff] [blame] | 228 | return Malloc(mallinfo)(); |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 229 | } |
| 230 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 231 | extern "C" void* valloc(size_t bytes) { |
Christopher Ferris | 72bbd42 | 2014-05-08 11:14:03 -0700 | [diff] [blame] | 232 | return Malloc(valloc)(bytes); |
Ian Rogers | 9990891 | 2012-08-17 17:28:15 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 235 | extern "C" void* pvalloc(size_t bytes) { |
Christopher Ferris | 72bbd42 | 2014-05-08 11:14:03 -0700 | [diff] [blame] | 236 | return Malloc(pvalloc)(bytes); |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 237 | } |
| 238 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 239 | extern "C" int posix_memalign(void** memptr, size_t alignment, size_t size) { |
Christopher Ferris | 72bbd42 | 2014-05-08 11:14:03 -0700 | [diff] [blame] | 240 | return Malloc(posix_memalign)(memptr, alignment, size); |
Brian Carlstrom | bfc1d97 | 2012-08-20 18:28:20 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 243 | extern "C" void* malloc(size_t bytes) { |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 244 | return __libc_malloc_dispatch->malloc(bytes); |
| 245 | } |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 246 | |
| 247 | extern "C" void free(void* mem) { |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 248 | __libc_malloc_dispatch->free(mem); |
| 249 | } |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 250 | |
| 251 | extern "C" void* calloc(size_t n_elements, size_t elem_size) { |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 252 | return __libc_malloc_dispatch->calloc(n_elements, elem_size); |
| 253 | } |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 254 | |
| 255 | extern "C" void* realloc(void* oldMem, size_t bytes) { |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 256 | return __libc_malloc_dispatch->realloc(oldMem, bytes); |
| 257 | } |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 258 | |
| 259 | extern "C" void* memalign(size_t alignment, size_t bytes) { |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 260 | return __libc_malloc_dispatch->memalign(alignment, bytes); |
| 261 | } |
| 262 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 263 | extern "C" size_t malloc_usable_size(const void* mem) { |
| 264 | return __libc_malloc_dispatch->malloc_usable_size(mem); |
| 265 | } |
| 266 | |
Elliott Hughes | db492b3 | 2013-01-03 15:44:03 -0800 | [diff] [blame] | 267 | /* We implement malloc debugging only in libc.so, so code below |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 268 | * must be excluded if we compile this file for static libc.a |
| 269 | */ |
| 270 | #ifndef LIBC_STATIC |
| 271 | #include <sys/system_properties.h> |
| 272 | #include <dlfcn.h> |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 273 | #include <stdio.h> |
Elliott Hughes | eb847bc | 2013-10-09 15:50:50 -0700 | [diff] [blame] | 274 | #include "private/libc_logging.h" |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 275 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 276 | template<typename FunctionType> |
Nick Kralevich | 35c1862 | 2013-10-03 14:59:05 -0700 | [diff] [blame] | 277 | 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] | 278 | char symbol[128]; |
| 279 | snprintf(symbol, sizeof(symbol), "%s_%s", prefix, suffix); |
| 280 | *func = reinterpret_cast<FunctionType>(dlsym(malloc_impl_handler, symbol)); |
| 281 | if (*func == NULL) { |
Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 282 | error_log("%s: dlsym(\"%s\") failed", getprogname(), symbol); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 283 | } |
| 284 | } |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 285 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 286 | static void InitMalloc(void* malloc_impl_handler, MallocDebug* table, const char* prefix) { |
| 287 | __libc_format_log(ANDROID_LOG_INFO, "libc", "%s: using libc.debug.malloc %d (%s)\n", |
Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 288 | getprogname(), g_malloc_debug_level, prefix); |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 289 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 290 | InitMallocFunction<MallocDebugMalloc>(malloc_impl_handler, &table->malloc, prefix, "malloc"); |
| 291 | InitMallocFunction<MallocDebugFree>(malloc_impl_handler, &table->free, prefix, "free"); |
| 292 | InitMallocFunction<MallocDebugCalloc>(malloc_impl_handler, &table->calloc, prefix, "calloc"); |
| 293 | InitMallocFunction<MallocDebugRealloc>(malloc_impl_handler, &table->realloc, prefix, "realloc"); |
| 294 | InitMallocFunction<MallocDebugMemalign>(malloc_impl_handler, &table->memalign, prefix, "memalign"); |
| 295 | 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] | 296 | } |
| 297 | |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 298 | /* Initializes memory allocation framework once per process. */ |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 299 | static void malloc_init_impl() { |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 300 | const char* so_name = NULL; |
Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 301 | MallocDebugInit malloc_debug_initialize = NULL; |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 302 | unsigned int qemu_running = 0; |
Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 303 | unsigned int memcheck_enabled = 0; |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 304 | char env[PROP_VALUE_MAX]; |
Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 305 | char memcheck_tracing[PROP_VALUE_MAX]; |
Iliyan Malchev | 7d2e24e | 2012-05-29 16:46:17 -0700 | [diff] [blame] | 306 | char debug_program[PROP_VALUE_MAX]; |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 307 | |
Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 308 | /* Get custom malloc debug level. Note that emulator started with |
| 309 | * memory checking option will have priority over debug level set in |
| 310 | * libc.debug.malloc system property. */ |
| 311 | if (__system_property_get("ro.kernel.qemu", env) && atoi(env)) { |
| 312 | qemu_running = 1; |
| 313 | if (__system_property_get("ro.kernel.memcheck", memcheck_tracing)) { |
| 314 | if (memcheck_tracing[0] != '0') { |
| 315 | // Emulator has started with memory tracing enabled. Enforce it. |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 316 | g_malloc_debug_level = 20; |
Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 317 | memcheck_enabled = 1; |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | /* If debug level has not been set by memcheck option in the emulator, |
| 323 | * lets grab it from libc.debug.malloc system property. */ |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 324 | if (g_malloc_debug_level == 0 && __system_property_get("libc.debug.malloc", env)) { |
| 325 | g_malloc_debug_level = atoi(env); |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 326 | } |
| 327 | |
Christopher Ferris | 72bbd42 | 2014-05-08 11:14:03 -0700 | [diff] [blame] | 328 | /* Debug level 0 means that we should use default allocation routines. */ |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 329 | if (g_malloc_debug_level == 0) { |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 330 | return; |
| 331 | } |
| 332 | |
Iliyan Malchev | 7d2e24e | 2012-05-29 16:46:17 -0700 | [diff] [blame] | 333 | /* If libc.debug.malloc.program is set and is not a substring of progname, |
| 334 | * then exit. |
| 335 | */ |
| 336 | if (__system_property_get("libc.debug.malloc.program", debug_program)) { |
Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 337 | if (!strstr(getprogname(), debug_program)) { |
Iliyan Malchev | 7d2e24e | 2012-05-29 16:46:17 -0700 | [diff] [blame] | 338 | return; |
| 339 | } |
| 340 | } |
| 341 | |
Elliott Hughes | 84f8b5f | 2013-01-22 18:35:14 -0800 | [diff] [blame] | 342 | // mksh is way too leaky. http://b/7291287. |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 343 | if (g_malloc_debug_level >= 10) { |
Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 344 | if (strcmp(getprogname(), "sh") == 0 || strcmp(getprogname(), "/system/bin/sh") == 0) { |
Elliott Hughes | 239e7a0 | 2013-01-25 17:13:45 -0800 | [diff] [blame] | 345 | return; |
| 346 | } |
Elliott Hughes | 84f8b5f | 2013-01-22 18:35:14 -0800 | [diff] [blame] | 347 | } |
| 348 | |
Elliott Hughes | e5d5f7f | 2012-10-09 17:23:09 -0700 | [diff] [blame] | 349 | // Choose the appropriate .so for the requested debug level. |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 350 | switch (g_malloc_debug_level) { |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 351 | case 1: |
| 352 | case 5: |
Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 353 | case 10: |
Elliott Hughes | 64b2963 | 2014-04-01 13:48:30 -0700 | [diff] [blame] | 354 | so_name = "libc_malloc_debug_leak.so"; |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 355 | break; |
| 356 | case 20: |
Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 357 | // Quick check: debug level 20 can only be handled in emulator. |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 358 | if (!qemu_running) { |
Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 359 | error_log("%s: Debug level %d can only be set in emulator\n", |
Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 360 | getprogname(), g_malloc_debug_level); |
Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 361 | return; |
| 362 | } |
| 363 | // Make sure that memory checking has been enabled in emulator. |
| 364 | if (!memcheck_enabled) { |
| 365 | error_log("%s: Memory checking is not enabled in the emulator\n", |
Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 366 | getprogname()); |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 367 | return; |
| 368 | } |
Elliott Hughes | 64b2963 | 2014-04-01 13:48:30 -0700 | [diff] [blame] | 369 | so_name = "libc_malloc_debug_qemu.so"; |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 370 | break; |
| 371 | default: |
Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 372 | error_log("%s: Debug level %d is unknown\n", getprogname(), g_malloc_debug_level); |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 373 | return; |
| 374 | } |
| 375 | |
| 376 | // Load .so that implements the required malloc debugging functionality. |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 377 | void* malloc_impl_handle = dlopen(so_name, RTLD_LAZY); |
| 378 | if (malloc_impl_handle == NULL) { |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 379 | error_log("%s: Missing module %s required for malloc debug level %d: %s", |
Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 380 | getprogname(), so_name, g_malloc_debug_level, dlerror()); |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 381 | return; |
| 382 | } |
| 383 | |
Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 384 | // Initialize malloc debugging in the loaded module. |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 385 | malloc_debug_initialize = reinterpret_cast<MallocDebugInit>(dlsym(malloc_impl_handle, |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 386 | "malloc_debug_initialize")); |
Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 387 | if (malloc_debug_initialize == NULL) { |
| 388 | error_log("%s: Initialization routine is not found in %s\n", |
Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 389 | getprogname(), so_name); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 390 | dlclose(malloc_impl_handle); |
Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 391 | return; |
| 392 | } |
Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 393 | if (malloc_debug_initialize(&g_hash_table) == -1) { |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 394 | dlclose(malloc_impl_handle); |
Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 395 | return; |
| 396 | } |
| 397 | |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 398 | if (g_malloc_debug_level == 20) { |
Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 399 | // For memory checker we need to do extra initialization. |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 400 | typedef int (*MemCheckInit)(int, const char*); |
| 401 | MemCheckInit memcheck_initialize = |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 402 | reinterpret_cast<MemCheckInit>(dlsym(malloc_impl_handle, |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 403 | "memcheck_initialize")); |
Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 404 | if (memcheck_initialize == NULL) { |
| 405 | error_log("%s: memcheck_initialize routine is not found in %s\n", |
Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 406 | getprogname(), so_name); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 407 | dlclose(malloc_impl_handle); |
Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 408 | return; |
| 409 | } |
Elliott Hughes | db492b3 | 2013-01-03 15:44:03 -0800 | [diff] [blame] | 410 | |
Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 411 | if (memcheck_initialize(MALLOC_ALIGNMENT, memcheck_tracing)) { |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 412 | dlclose(malloc_impl_handle); |
Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 413 | return; |
| 414 | } |
| 415 | } |
| 416 | |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 417 | // Initialize malloc dispatch table with appropriate routines. |
Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 418 | static MallocDebug malloc_dispatch_table __attribute__((aligned(32))) = { |
| 419 | Malloc(malloc), |
| 420 | Malloc(free), |
| 421 | Malloc(calloc), |
| 422 | Malloc(realloc), |
| 423 | Malloc(memalign), |
| 424 | Malloc(malloc_usable_size) |
| 425 | }; |
| 426 | |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 427 | switch (g_malloc_debug_level) { |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 428 | case 1: |
Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 429 | InitMalloc(malloc_impl_handle, &malloc_dispatch_table, "leak"); |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 430 | break; |
| 431 | case 5: |
Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 432 | InitMalloc(malloc_impl_handle, &malloc_dispatch_table, "fill"); |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 433 | break; |
| 434 | case 10: |
Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 435 | InitMalloc(malloc_impl_handle, &malloc_dispatch_table, "chk"); |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 436 | break; |
| 437 | case 20: |
Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 438 | InitMalloc(malloc_impl_handle, &malloc_dispatch_table, "qemu_instrumented"); |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 439 | break; |
| 440 | default: |
| 441 | break; |
| 442 | } |
| 443 | |
| 444 | // Make sure dispatch table is initialized |
Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 445 | if ((malloc_dispatch_table.malloc == NULL) || |
| 446 | (malloc_dispatch_table.free == NULL) || |
| 447 | (malloc_dispatch_table.calloc == NULL) || |
| 448 | (malloc_dispatch_table.realloc == NULL) || |
| 449 | (malloc_dispatch_table.memalign == NULL) || |
| 450 | (malloc_dispatch_table.malloc_usable_size == NULL)) { |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 451 | error_log("%s: some symbols for libc.debug.malloc level %d were not found (see above)", |
Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 452 | getprogname(), g_malloc_debug_level); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 453 | dlclose(malloc_impl_handle); |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 454 | } else { |
Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 455 | __libc_malloc_dispatch = &malloc_dispatch_table; |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 456 | libc_malloc_impl_handle = malloc_impl_handle; |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 457 | } |
| 458 | } |
| 459 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 460 | static void malloc_fini_impl() { |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 461 | // Our BSD stdio implementation doesn't close the standard streams, it only flushes them. |
Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 462 | // And it doesn't do that until its atexit handler is run, and we run first! |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 463 | // It's great that other unclosed FILE*s show up as malloc leaks, but we need to manually |
| 464 | // clean up the standard streams ourselves. |
| 465 | fclose(stdin); |
| 466 | fclose(stdout); |
| 467 | fclose(stderr); |
| 468 | |
| 469 | if (libc_malloc_impl_handle != NULL) { |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 470 | MallocDebugFini malloc_debug_finalize = |
| 471 | reinterpret_cast<MallocDebugFini>(dlsym(libc_malloc_impl_handle, |
| 472 | "malloc_debug_finalize")); |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 473 | if (malloc_debug_finalize != NULL) { |
Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 474 | malloc_debug_finalize(g_malloc_debug_level); |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 475 | } |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 476 | } |
| 477 | } |
| 478 | |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 479 | #endif // !LIBC_STATIC |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 480 | |
| 481 | /* Initializes memory allocation framework. |
| 482 | * This routine is called from __libc_init routines implemented |
| 483 | * in libc_init_static.c and libc_init_dynamic.c files. |
| 484 | */ |
Kito Cheng | ea48974 | 2013-04-12 16:13:34 +0800 | [diff] [blame] | 485 | extern "C" __LIBC_HIDDEN__ void malloc_debug_init() { |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 486 | #if defined(USE_DL_PREFIX) && !defined(LIBC_STATIC) |
Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 487 | static pthread_once_t malloc_init_once_ctl = PTHREAD_ONCE_INIT; |
| 488 | if (pthread_once(&malloc_init_once_ctl, malloc_init_impl)) { |
| 489 | error_log("Unable to initialize malloc_debug component."); |
| 490 | } |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 491 | #endif // USE_DL_PREFIX && !LIBC_STATIC |
| 492 | } |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 493 | |
Kito Cheng | ea48974 | 2013-04-12 16:13:34 +0800 | [diff] [blame] | 494 | extern "C" __LIBC_HIDDEN__ void malloc_debug_fini() { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 495 | #if defined(USE_DL_PREFIX) && !defined(LIBC_STATIC) |
Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 496 | static pthread_once_t malloc_fini_once_ctl = PTHREAD_ONCE_INIT; |
| 497 | if (pthread_once(&malloc_fini_once_ctl, malloc_fini_impl)) { |
| 498 | error_log("Unable to finalize malloc_debug component."); |
| 499 | } |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 500 | #endif // USE_DL_PREFIX && !LIBC_STATIC |
| 501 | } |