| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2008 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 | #include <errno.h> | 
|  | 29 | #include <pthread.h> | 
|  | 30 | #include <stdio.h> | 
|  | 31 | #include <arpa/inet.h> | 
|  | 32 | #include <sys/socket.h> | 
|  | 33 | #include <stdlib.h> | 
|  | 34 | #include <string.h> | 
|  | 35 | #include <unistd.h> | 
|  | 36 | #include <errno.h> | 
|  | 37 | #include <stddef.h> | 
|  | 38 | #include <stdarg.h> | 
|  | 39 | #include <fcntl.h> | 
|  | 40 | #include <unwind.h> | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 41 | #include <dlfcn.h> | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 42 |  | 
|  | 43 | #include <sys/socket.h> | 
|  | 44 | #include <sys/un.h> | 
|  | 45 | #include <sys/select.h> | 
|  | 46 | #include <sys/types.h> | 
|  | 47 | #include <sys/system_properties.h> | 
|  | 48 |  | 
|  | 49 | #include "dlmalloc.h" | 
|  | 50 | #include "logd.h" | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 51 | #include "malloc_debug_common.h" | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 52 |  | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 53 | // This file should be included into the build only when | 
|  | 54 | // MALLOC_LEAK_CHECK, or MALLOC_QEMU_INSTRUMENT, or both | 
|  | 55 | // macros are defined. | 
|  | 56 | #ifndef MALLOC_LEAK_CHECK | 
|  | 57 | #error MALLOC_LEAK_CHECK is not defined. | 
|  | 58 | #endif  // !MALLOC_LEAK_CHECK | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 59 |  | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 60 | // Global variables defined in malloc_debug_common.c | 
|  | 61 | extern int gMallocLeakZygoteChild; | 
|  | 62 | extern pthread_mutex_t gAllocationsMutex; | 
|  | 63 | extern HashTable gHashTable; | 
|  | 64 | extern const MallocDebug __libc_malloc_default_dispatch; | 
|  | 65 | extern const MallocDebug* __libc_malloc_dispatch; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 66 |  | 
|  | 67 | // ============================================================================= | 
| Andy McFadden | 39f3745 | 2009-07-21 15:25:23 -0700 | [diff] [blame] | 68 | // log functions | 
|  | 69 | // ============================================================================= | 
|  | 70 |  | 
|  | 71 | #define debug_log(format, ...)  \ | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 72 | __libc_android_log_print(ANDROID_LOG_DEBUG, "malloc_leak_check", (format), ##__VA_ARGS__ ) | 
|  | 73 | #define error_log(format, ...)  \ | 
|  | 74 | __libc_android_log_print(ANDROID_LOG_ERROR, "malloc_leak_check", (format), ##__VA_ARGS__ ) | 
|  | 75 | #define info_log(format, ...)  \ | 
|  | 76 | __libc_android_log_print(ANDROID_LOG_INFO, "malloc_leak_check", (format), ##__VA_ARGS__ ) | 
| Andy McFadden | 39f3745 | 2009-07-21 15:25:23 -0700 | [diff] [blame] | 77 |  | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 78 | static int gTrapOnError = 1; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 79 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 80 | #define MALLOC_ALIGNMENT    8 | 
|  | 81 | #define GUARD               0x48151642 | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 82 | #define DEBUG               0 | 
|  | 83 |  | 
|  | 84 | // ============================================================================= | 
|  | 85 | // Structures | 
|  | 86 | // ============================================================================= | 
|  | 87 | typedef struct AllocationEntry AllocationEntry; | 
|  | 88 | struct AllocationEntry { | 
|  | 89 | HashEntry* entry; | 
|  | 90 | uint32_t guard; | 
|  | 91 | }; | 
|  | 92 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 93 |  | 
|  | 94 | // ============================================================================= | 
|  | 95 | // Hash Table functions | 
|  | 96 | // ============================================================================= | 
|  | 97 | static uint32_t get_hash(intptr_t* backtrace, size_t numEntries) | 
|  | 98 | { | 
|  | 99 | if (backtrace == NULL) return 0; | 
|  | 100 |  | 
|  | 101 | int hash = 0; | 
|  | 102 | size_t i; | 
|  | 103 | for (i = 0 ; i < numEntries ; i++) { | 
|  | 104 | hash = (hash * 33) + (backtrace[i] >> 2); | 
|  | 105 | } | 
|  | 106 |  | 
|  | 107 | return hash; | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | static HashEntry* find_entry(HashTable* table, int slot, | 
|  | 111 | intptr_t* backtrace, size_t numEntries, size_t size) | 
|  | 112 | { | 
|  | 113 | HashEntry* entry = table->slots[slot]; | 
|  | 114 | while (entry != NULL) { | 
|  | 115 | //debug_log("backtrace: %p, entry: %p entry->backtrace: %p\n", | 
|  | 116 | //        backtrace, entry, (entry != NULL) ? entry->backtrace : NULL); | 
|  | 117 | /* | 
|  | 118 | * See if the entry matches exactly.  We compare the "size" field, | 
|  | 119 | * including the flag bits. | 
|  | 120 | */ | 
|  | 121 | if (entry->size == size && entry->numEntries == numEntries && | 
|  | 122 | !memcmp(backtrace, entry->backtrace, numEntries * sizeof(intptr_t))) { | 
|  | 123 | return entry; | 
|  | 124 | } | 
|  | 125 |  | 
|  | 126 | entry = entry->next; | 
|  | 127 | } | 
|  | 128 |  | 
|  | 129 | return NULL; | 
|  | 130 | } | 
|  | 131 |  | 
|  | 132 | static HashEntry* record_backtrace(intptr_t* backtrace, size_t numEntries, size_t size) | 
|  | 133 | { | 
|  | 134 | size_t hash = get_hash(backtrace, numEntries); | 
|  | 135 | size_t slot = hash % HASHTABLE_SIZE; | 
|  | 136 |  | 
|  | 137 | if (size & SIZE_FLAG_MASK) { | 
|  | 138 | debug_log("malloc_debug: allocation %zx exceeds bit width\n", size); | 
|  | 139 | abort(); | 
|  | 140 | } | 
|  | 141 |  | 
|  | 142 | if (gMallocLeakZygoteChild) | 
|  | 143 | size |= SIZE_FLAG_ZYGOTE_CHILD; | 
|  | 144 |  | 
|  | 145 | HashEntry* entry = find_entry(&gHashTable, slot, backtrace, numEntries, size); | 
|  | 146 |  | 
|  | 147 | if (entry != NULL) { | 
|  | 148 | entry->allocations++; | 
|  | 149 | } else { | 
|  | 150 | // create a new entry | 
|  | 151 | entry = (HashEntry*)dlmalloc(sizeof(HashEntry) + numEntries*sizeof(intptr_t)); | 
|  | 152 | entry->allocations = 1; | 
|  | 153 | entry->slot = slot; | 
|  | 154 | entry->prev = NULL; | 
|  | 155 | entry->next = gHashTable.slots[slot]; | 
|  | 156 | entry->numEntries = numEntries; | 
|  | 157 | entry->size = size; | 
|  | 158 |  | 
|  | 159 | memcpy(entry->backtrace, backtrace, numEntries * sizeof(intptr_t)); | 
|  | 160 |  | 
|  | 161 | gHashTable.slots[slot] = entry; | 
|  | 162 |  | 
|  | 163 | if (entry->next != NULL) { | 
|  | 164 | entry->next->prev = entry; | 
|  | 165 | } | 
|  | 166 |  | 
|  | 167 | // we just added an entry, increase the size of the hashtable | 
|  | 168 | gHashTable.count++; | 
|  | 169 | } | 
|  | 170 |  | 
|  | 171 | return entry; | 
|  | 172 | } | 
|  | 173 |  | 
|  | 174 | static int is_valid_entry(HashEntry* entry) | 
|  | 175 | { | 
|  | 176 | if (entry != NULL) { | 
|  | 177 | int i; | 
|  | 178 | for (i = 0 ; i < HASHTABLE_SIZE ; i++) { | 
|  | 179 | HashEntry* e1 = gHashTable.slots[i]; | 
|  | 180 |  | 
|  | 181 | while (e1 != NULL) { | 
|  | 182 | if (e1 == entry) { | 
|  | 183 | return 1; | 
|  | 184 | } | 
|  | 185 |  | 
|  | 186 | e1 = e1->next; | 
|  | 187 | } | 
|  | 188 | } | 
|  | 189 | } | 
|  | 190 |  | 
|  | 191 | return 0; | 
|  | 192 | } | 
|  | 193 |  | 
|  | 194 | static void remove_entry(HashEntry* entry) | 
|  | 195 | { | 
|  | 196 | HashEntry* prev = entry->prev; | 
|  | 197 | HashEntry* next = entry->next; | 
|  | 198 |  | 
|  | 199 | if (prev != NULL) entry->prev->next = next; | 
|  | 200 | if (next != NULL) entry->next->prev = prev; | 
|  | 201 |  | 
|  | 202 | if (prev == NULL) { | 
|  | 203 | // we are the head of the list. set the head to be next | 
|  | 204 | gHashTable.slots[entry->slot] = entry->next; | 
|  | 205 | } | 
|  | 206 |  | 
|  | 207 | // we just removed and entry, decrease the size of the hashtable | 
|  | 208 | gHashTable.count--; | 
|  | 209 | } | 
|  | 210 |  | 
|  | 211 |  | 
|  | 212 | // ============================================================================= | 
|  | 213 | // stack trace functions | 
|  | 214 | // ============================================================================= | 
|  | 215 |  | 
|  | 216 | typedef struct | 
|  | 217 | { | 
|  | 218 | size_t count; | 
|  | 219 | intptr_t* addrs; | 
|  | 220 | } stack_crawl_state_t; | 
|  | 221 |  | 
|  | 222 |  | 
|  | 223 | /* depends how the system includes define this */ | 
|  | 224 | #ifdef HAVE_UNWIND_CONTEXT_STRUCT | 
|  | 225 | typedef struct _Unwind_Context __unwind_context; | 
|  | 226 | #else | 
|  | 227 | typedef _Unwind_Context __unwind_context; | 
|  | 228 | #endif | 
|  | 229 |  | 
|  | 230 | static _Unwind_Reason_Code trace_function(__unwind_context *context, void *arg) | 
|  | 231 | { | 
|  | 232 | stack_crawl_state_t* state = (stack_crawl_state_t*)arg; | 
|  | 233 | if (state->count) { | 
|  | 234 | intptr_t ip = (intptr_t)_Unwind_GetIP(context); | 
|  | 235 | if (ip) { | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 236 | state->addrs[0] = ip; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 237 | state->addrs++; | 
|  | 238 | state->count--; | 
|  | 239 | return _URC_NO_REASON; | 
|  | 240 | } | 
|  | 241 | } | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 242 | /* | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 243 | * If we run out of space to record the address or 0 has been seen, stop | 
|  | 244 | * unwinding the stack. | 
|  | 245 | */ | 
|  | 246 | return _URC_END_OF_STACK; | 
|  | 247 | } | 
|  | 248 |  | 
|  | 249 | static inline | 
|  | 250 | int get_backtrace(intptr_t* addrs, size_t max_entries) | 
|  | 251 | { | 
|  | 252 | stack_crawl_state_t state; | 
|  | 253 | state.count = max_entries; | 
|  | 254 | state.addrs = (intptr_t*)addrs; | 
|  | 255 | _Unwind_Backtrace(trace_function, (void*)&state); | 
|  | 256 | return max_entries - state.count; | 
|  | 257 | } | 
|  | 258 |  | 
|  | 259 | // ============================================================================= | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 260 | // malloc check functions | 
|  | 261 | // ============================================================================= | 
|  | 262 |  | 
|  | 263 | #define CHK_FILL_FREE           0xef | 
|  | 264 | #define CHK_SENTINEL_VALUE      0xeb | 
|  | 265 | #define CHK_SENTINEL_HEAD_SIZE  16 | 
|  | 266 | #define CHK_SENTINEL_TAIL_SIZE  16 | 
|  | 267 | #define CHK_OVERHEAD_SIZE       (   CHK_SENTINEL_HEAD_SIZE +    \ | 
|  | 268 | CHK_SENTINEL_TAIL_SIZE +    \ | 
|  | 269 | sizeof(size_t) ) | 
|  | 270 |  | 
|  | 271 | static void dump_stack_trace() | 
|  | 272 | { | 
|  | 273 | intptr_t addrs[20]; | 
|  | 274 | int c = get_backtrace(addrs, 20); | 
|  | 275 | char buf[16]; | 
|  | 276 | char tmp[16*20]; | 
|  | 277 | int i; | 
|  | 278 |  | 
|  | 279 | tmp[0] = 0; // Need to initialize tmp[0] for the first strcat | 
|  | 280 | for (i=0 ; i<c; i++) { | 
| David 'Digit' Turner | c4eee37 | 2009-07-08 14:22:41 +0200 | [diff] [blame] | 281 | snprintf(buf, sizeof buf, "%2d: %08x\n", i, addrs[i]); | 
|  | 282 | strlcat(tmp, buf, sizeof tmp); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 283 | } | 
|  | 284 | __libc_android_log_print(ANDROID_LOG_ERROR, "libc", "call stack:\n%s", tmp); | 
|  | 285 | } | 
|  | 286 |  | 
|  | 287 | static int is_valid_malloc_pointer(void* addr) | 
|  | 288 | { | 
|  | 289 | return 1; | 
|  | 290 | } | 
|  | 291 |  | 
| David 'Digit' Turner | c4eee37 | 2009-07-08 14:22:41 +0200 | [diff] [blame] | 292 | static void assert_log_message(const char* format, ...) | 
|  | 293 | { | 
|  | 294 | va_list  args; | 
|  | 295 |  | 
|  | 296 | pthread_mutex_lock(&gAllocationsMutex); | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 297 | { | 
|  | 298 | const MallocDebug* current_dispatch = __libc_malloc_dispatch; | 
|  | 299 | __libc_malloc_dispatch = &__libc_malloc_default_dispatch; | 
| David 'Digit' Turner | c4eee37 | 2009-07-08 14:22:41 +0200 | [diff] [blame] | 300 | va_start(args, format); | 
|  | 301 | __libc_android_log_vprint(ANDROID_LOG_ERROR, "libc", | 
|  | 302 | format, args); | 
|  | 303 | va_end(args); | 
|  | 304 | dump_stack_trace(); | 
|  | 305 | if (gTrapOnError) { | 
|  | 306 | __builtin_trap(); | 
|  | 307 | } | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 308 | __libc_malloc_dispatch = current_dispatch; | 
|  | 309 | } | 
| David 'Digit' Turner | c4eee37 | 2009-07-08 14:22:41 +0200 | [diff] [blame] | 310 | pthread_mutex_unlock(&gAllocationsMutex); | 
|  | 311 | } | 
|  | 312 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 313 | static void assert_valid_malloc_pointer(void* mem) | 
|  | 314 | { | 
|  | 315 | if (mem && !is_valid_malloc_pointer(mem)) { | 
| David 'Digit' Turner | c4eee37 | 2009-07-08 14:22:41 +0200 | [diff] [blame] | 316 | assert_log_message( | 
|  | 317 | "*** MALLOC CHECK: buffer %p, is not a valid " | 
|  | 318 | "malloc pointer (are you mixing up new/delete " | 
|  | 319 | "and malloc/free?)", mem); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 320 | } | 
|  | 321 | } | 
|  | 322 |  | 
| David 'Digit' Turner | c4eee37 | 2009-07-08 14:22:41 +0200 | [diff] [blame] | 323 | /* Check that a given address corresponds to a guarded block, | 
|  | 324 | * and returns its original allocation size in '*allocated'. | 
|  | 325 | * 'func' is the capitalized name of the caller function. | 
|  | 326 | * Returns 0 on success, or -1 on failure. | 
|  | 327 | * NOTE: Does not return if gTrapOnError is set. | 
|  | 328 | */ | 
|  | 329 | static int chk_mem_check(void*       mem, | 
|  | 330 | size_t*     allocated, | 
|  | 331 | const char* func) | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 332 | { | 
| David 'Digit' Turner | c4eee37 | 2009-07-08 14:22:41 +0200 | [diff] [blame] | 333 | char*  buffer; | 
|  | 334 | size_t offset, bytes; | 
|  | 335 | int    i; | 
|  | 336 | char*  buf; | 
|  | 337 |  | 
|  | 338 | /* first check the bytes in the sentinel header */ | 
|  | 339 | buf = (char*)mem - CHK_SENTINEL_HEAD_SIZE; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 340 | for (i=0 ; i<CHK_SENTINEL_HEAD_SIZE ; i++) { | 
|  | 341 | if (buf[i] != CHK_SENTINEL_VALUE) { | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 342 | assert_log_message( | 
| David 'Digit' Turner | c4eee37 | 2009-07-08 14:22:41 +0200 | [diff] [blame] | 343 | "*** %s CHECK: buffer %p " | 
|  | 344 | "corrupted %d bytes before allocation", | 
|  | 345 | func, mem, CHK_SENTINEL_HEAD_SIZE-i); | 
|  | 346 | return -1; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 347 | } | 
|  | 348 | } | 
| David 'Digit' Turner | c4eee37 | 2009-07-08 14:22:41 +0200 | [diff] [blame] | 349 |  | 
|  | 350 | /* then the ones in the sentinel trailer */ | 
|  | 351 | buffer = (char*)mem - CHK_SENTINEL_HEAD_SIZE; | 
|  | 352 | offset = dlmalloc_usable_size(buffer) - sizeof(size_t); | 
|  | 353 | bytes  = *(size_t *)(buffer + offset); | 
|  | 354 |  | 
|  | 355 | buf = (char*)mem + bytes; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 356 | for (i=CHK_SENTINEL_TAIL_SIZE-1 ; i>=0 ; i--) { | 
|  | 357 | if (buf[i] != CHK_SENTINEL_VALUE) { | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 358 | assert_log_message( | 
| David 'Digit' Turner | c4eee37 | 2009-07-08 14:22:41 +0200 | [diff] [blame] | 359 | "*** %s CHECK: buffer %p, size=%lu, " | 
|  | 360 | "corrupted %d bytes after allocation", | 
|  | 361 | func, buffer, bytes, i+1); | 
|  | 362 | return -1; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 363 | } | 
|  | 364 | } | 
| David 'Digit' Turner | c4eee37 | 2009-07-08 14:22:41 +0200 | [diff] [blame] | 365 |  | 
|  | 366 | *allocated = bytes; | 
|  | 367 | return 0; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 368 | } | 
|  | 369 |  | 
| David 'Digit' Turner | c4eee37 | 2009-07-08 14:22:41 +0200 | [diff] [blame] | 370 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 371 | void* chk_malloc(size_t bytes) | 
|  | 372 | { | 
|  | 373 | char* buffer = (char*)dlmalloc(bytes + CHK_OVERHEAD_SIZE); | 
|  | 374 | if (buffer) { | 
| David 'Digit' Turner | c4eee37 | 2009-07-08 14:22:41 +0200 | [diff] [blame] | 375 | memset(buffer, CHK_SENTINEL_VALUE, bytes + CHK_OVERHEAD_SIZE); | 
|  | 376 | size_t offset = dlmalloc_usable_size(buffer) - sizeof(size_t); | 
|  | 377 | *(size_t *)(buffer + offset) = bytes; | 
|  | 378 | buffer += CHK_SENTINEL_HEAD_SIZE; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 379 | } | 
|  | 380 | return buffer; | 
|  | 381 | } | 
|  | 382 |  | 
|  | 383 | void  chk_free(void* mem) | 
|  | 384 | { | 
|  | 385 | assert_valid_malloc_pointer(mem); | 
|  | 386 | if (mem) { | 
| David 'Digit' Turner | c4eee37 | 2009-07-08 14:22:41 +0200 | [diff] [blame] | 387 | size_t  size; | 
|  | 388 | char*   buffer; | 
|  | 389 |  | 
|  | 390 | if (chk_mem_check(mem, &size, "FREE") == 0) { | 
|  | 391 | buffer = (char*)mem - CHK_SENTINEL_HEAD_SIZE; | 
|  | 392 | memset(buffer, CHK_FILL_FREE, size + CHK_OVERHEAD_SIZE); | 
|  | 393 | dlfree(buffer); | 
|  | 394 | } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 395 | } | 
|  | 396 | } | 
|  | 397 |  | 
|  | 398 | void* chk_calloc(size_t n_elements, size_t elem_size) | 
|  | 399 | { | 
|  | 400 | size_t  size; | 
|  | 401 | void*   ptr; | 
|  | 402 |  | 
|  | 403 | /* Fail on overflow - just to be safe even though this code runs only | 
|  | 404 | * within the debugging C library, not the production one */ | 
|  | 405 | if (n_elements && MAX_SIZE_T / n_elements < elem_size) { | 
|  | 406 | return NULL; | 
|  | 407 | } | 
|  | 408 | size = n_elements * elem_size; | 
|  | 409 | ptr  = chk_malloc(size); | 
|  | 410 | if (ptr != NULL) { | 
|  | 411 | memset(ptr, 0, size); | 
|  | 412 | } | 
|  | 413 | return ptr; | 
|  | 414 | } | 
|  | 415 |  | 
|  | 416 | void* chk_realloc(void* mem, size_t bytes) | 
|  | 417 | { | 
| David 'Digit' Turner | c4eee37 | 2009-07-08 14:22:41 +0200 | [diff] [blame] | 418 | char*   buffer; | 
|  | 419 | int     ret; | 
|  | 420 | size_t  old_bytes = 0; | 
|  | 421 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 422 | assert_valid_malloc_pointer(mem); | 
| David 'Digit' Turner | c4eee37 | 2009-07-08 14:22:41 +0200 | [diff] [blame] | 423 |  | 
|  | 424 | if (mem != NULL && chk_mem_check(mem, &old_bytes, "REALLOC") < 0) | 
|  | 425 | return NULL; | 
|  | 426 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 427 | char* new_buffer = chk_malloc(bytes); | 
|  | 428 | if (mem == NULL) { | 
|  | 429 | return new_buffer; | 
|  | 430 | } | 
|  | 431 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 432 | if (new_buffer) { | 
| André Goddard Rosa | 291100c | 2010-02-05 16:32:56 -0200 | [diff] [blame] | 433 | if (bytes > old_bytes) | 
|  | 434 | bytes = old_bytes; | 
|  | 435 | memcpy(new_buffer, mem, bytes); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 436 | chk_free(mem); | 
|  | 437 | } | 
|  | 438 |  | 
|  | 439 | return new_buffer; | 
|  | 440 | } | 
|  | 441 |  | 
|  | 442 | void* chk_memalign(size_t alignment, size_t bytes) | 
|  | 443 | { | 
|  | 444 | // XXX: it's better to use malloc, than being wrong | 
|  | 445 | return chk_malloc(bytes); | 
|  | 446 | } | 
|  | 447 |  | 
|  | 448 | // ============================================================================= | 
|  | 449 | // malloc fill functions | 
|  | 450 | // ============================================================================= | 
|  | 451 |  | 
|  | 452 | void* fill_malloc(size_t bytes) | 
|  | 453 | { | 
|  | 454 | void* buffer = dlmalloc(bytes); | 
|  | 455 | if (buffer) { | 
|  | 456 | memset(buffer, CHK_SENTINEL_VALUE, bytes); | 
|  | 457 | } | 
|  | 458 | return buffer; | 
|  | 459 | } | 
|  | 460 |  | 
|  | 461 | void  fill_free(void* mem) | 
|  | 462 | { | 
|  | 463 | size_t bytes = dlmalloc_usable_size(mem); | 
|  | 464 | memset(mem, CHK_FILL_FREE, bytes); | 
|  | 465 | dlfree(mem); | 
|  | 466 | } | 
|  | 467 |  | 
|  | 468 | void* fill_realloc(void* mem, size_t bytes) | 
|  | 469 | { | 
|  | 470 | void* buffer = fill_malloc(bytes); | 
|  | 471 | if (mem == NULL) { | 
|  | 472 | return buffer; | 
|  | 473 | } | 
|  | 474 | if (buffer) { | 
|  | 475 | size_t old_size = dlmalloc_usable_size(mem); | 
|  | 476 | size_t size = (bytes < old_size)?(bytes):(old_size); | 
|  | 477 | memcpy(buffer, mem, size); | 
|  | 478 | fill_free(mem); | 
|  | 479 | } | 
|  | 480 | return buffer; | 
|  | 481 | } | 
|  | 482 |  | 
|  | 483 | void* fill_memalign(size_t alignment, size_t bytes) | 
|  | 484 | { | 
|  | 485 | void* buffer = dlmemalign(alignment, bytes); | 
|  | 486 | if (buffer) { | 
|  | 487 | memset(buffer, CHK_SENTINEL_VALUE, bytes); | 
|  | 488 | } | 
|  | 489 | return buffer; | 
|  | 490 | } | 
|  | 491 |  | 
|  | 492 | // ============================================================================= | 
|  | 493 | // malloc leak functions | 
|  | 494 | // ============================================================================= | 
|  | 495 |  | 
|  | 496 | #define MEMALIGN_GUARD  ((void*)0xA1A41520) | 
|  | 497 |  | 
|  | 498 | void* leak_malloc(size_t bytes) | 
|  | 499 | { | 
|  | 500 | // allocate enough space infront of the allocation to store the pointer for | 
|  | 501 | // the alloc structure. This will making free'ing the structer really fast! | 
|  | 502 |  | 
|  | 503 | // 1. allocate enough memory and include our header | 
|  | 504 | // 2. set the base pointer to be right after our header | 
|  | 505 |  | 
|  | 506 | void* base = dlmalloc(bytes + sizeof(AllocationEntry)); | 
|  | 507 | if (base != NULL) { | 
|  | 508 | pthread_mutex_lock(&gAllocationsMutex); | 
|  | 509 |  | 
|  | 510 | intptr_t backtrace[BACKTRACE_SIZE]; | 
|  | 511 | size_t numEntries = get_backtrace(backtrace, BACKTRACE_SIZE); | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 512 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 513 | AllocationEntry* header = (AllocationEntry*)base; | 
|  | 514 | header->entry = record_backtrace(backtrace, numEntries, bytes); | 
|  | 515 | header->guard = GUARD; | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 516 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 517 | // now increment base to point to after our header. | 
|  | 518 | // this should just work since our header is 8 bytes. | 
|  | 519 | base = (AllocationEntry*)base + 1; | 
|  | 520 |  | 
|  | 521 | pthread_mutex_unlock(&gAllocationsMutex); | 
|  | 522 | } | 
|  | 523 |  | 
|  | 524 | return base; | 
|  | 525 | } | 
|  | 526 |  | 
|  | 527 | void leak_free(void* mem) | 
|  | 528 | { | 
|  | 529 | if (mem != NULL) { | 
|  | 530 | pthread_mutex_lock(&gAllocationsMutex); | 
|  | 531 |  | 
|  | 532 | // check the guard to make sure it is valid | 
|  | 533 | AllocationEntry* header = (AllocationEntry*)mem - 1; | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 534 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 535 | if (header->guard != GUARD) { | 
|  | 536 | // could be a memaligned block | 
|  | 537 | if (((void**)mem)[-1] == MEMALIGN_GUARD) { | 
|  | 538 | mem = ((void**)mem)[-2]; | 
|  | 539 | header = (AllocationEntry*)mem - 1; | 
|  | 540 | } | 
|  | 541 | } | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 542 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 543 | if (header->guard == GUARD || is_valid_entry(header->entry)) { | 
|  | 544 | // decrement the allocations | 
|  | 545 | HashEntry* entry = header->entry; | 
|  | 546 | entry->allocations--; | 
|  | 547 | if (entry->allocations <= 0) { | 
|  | 548 | remove_entry(entry); | 
|  | 549 | dlfree(entry); | 
|  | 550 | } | 
|  | 551 |  | 
|  | 552 | // now free the memory! | 
|  | 553 | dlfree(header); | 
|  | 554 | } else { | 
|  | 555 | debug_log("WARNING bad header guard: '0x%x'! and invalid entry: %p\n", | 
|  | 556 | header->guard, header->entry); | 
|  | 557 | } | 
|  | 558 |  | 
|  | 559 | pthread_mutex_unlock(&gAllocationsMutex); | 
|  | 560 | } | 
|  | 561 | } | 
|  | 562 |  | 
|  | 563 | void* leak_calloc(size_t n_elements, size_t elem_size) | 
|  | 564 | { | 
|  | 565 | size_t  size; | 
|  | 566 | void*   ptr; | 
|  | 567 |  | 
|  | 568 | /* Fail on overflow - just to be safe even though this code runs only | 
|  | 569 | * within the debugging C library, not the production one */ | 
|  | 570 | if (n_elements && MAX_SIZE_T / n_elements < elem_size) { | 
|  | 571 | return NULL; | 
|  | 572 | } | 
|  | 573 | size = n_elements * elem_size; | 
|  | 574 | ptr  = leak_malloc(size); | 
|  | 575 | if (ptr != NULL) { | 
|  | 576 | memset(ptr, 0, size); | 
|  | 577 | } | 
|  | 578 | return ptr; | 
|  | 579 | } | 
|  | 580 |  | 
|  | 581 | void* leak_realloc(void* oldMem, size_t bytes) | 
|  | 582 | { | 
|  | 583 | if (oldMem == NULL) { | 
|  | 584 | return leak_malloc(bytes); | 
|  | 585 | } | 
|  | 586 | void* newMem = NULL; | 
|  | 587 | AllocationEntry* header = (AllocationEntry*)oldMem - 1; | 
|  | 588 | if (header && header->guard == GUARD) { | 
|  | 589 | size_t oldSize = header->entry->size & ~SIZE_FLAG_MASK; | 
|  | 590 | newMem = leak_malloc(bytes); | 
|  | 591 | if (newMem != NULL) { | 
|  | 592 | size_t copySize = (oldSize <= bytes) ? oldSize : bytes; | 
|  | 593 | memcpy(newMem, oldMem, copySize); | 
|  | 594 | leak_free(oldMem); | 
|  | 595 | } | 
|  | 596 | } else { | 
|  | 597 | newMem = dlrealloc(oldMem, bytes); | 
|  | 598 | } | 
|  | 599 | return newMem; | 
|  | 600 | } | 
|  | 601 |  | 
|  | 602 | void* leak_memalign(size_t alignment, size_t bytes) | 
|  | 603 | { | 
|  | 604 | // we can just use malloc | 
|  | 605 | if (alignment <= MALLOC_ALIGNMENT) | 
|  | 606 | return leak_malloc(bytes); | 
|  | 607 |  | 
|  | 608 | // need to make sure it's a power of two | 
|  | 609 | if (alignment & (alignment-1)) | 
|  | 610 | alignment = 1L << (31 - __builtin_clz(alignment)); | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 611 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 612 | // here, aligment is at least MALLOC_ALIGNMENT<<1 bytes | 
|  | 613 | // we will align by at least MALLOC_ALIGNMENT bytes | 
|  | 614 | // and at most alignment-MALLOC_ALIGNMENT bytes | 
|  | 615 | size_t size = (alignment-MALLOC_ALIGNMENT) + bytes; | 
|  | 616 | void* base = leak_malloc(size); | 
|  | 617 | if (base != NULL) { | 
|  | 618 | intptr_t ptr = (intptr_t)base; | 
|  | 619 | if ((ptr % alignment) == 0) | 
|  | 620 | return base; | 
|  | 621 |  | 
|  | 622 | // align the pointer | 
|  | 623 | ptr += ((-ptr) % alignment); | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 624 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 625 | // there is always enough space for the base pointer and the guard | 
|  | 626 | ((void**)ptr)[-1] = MEMALIGN_GUARD; | 
|  | 627 | ((void**)ptr)[-2] = base; | 
|  | 628 |  | 
|  | 629 | return (void*)ptr; | 
|  | 630 | } | 
|  | 631 | return base; | 
|  | 632 | } | 
| Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 633 |  | 
|  | 634 | /* Initializes malloc debugging framework. | 
|  | 635 | * See comments on MallocDebugInit in malloc_debug_common.h | 
|  | 636 | */ | 
|  | 637 | int malloc_debug_initialize(void) | 
|  | 638 | { | 
|  | 639 | // We don't really have anything that requires initialization here. | 
|  | 640 | return 0; | 
|  | 641 | } |