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 | */ |
Xi Wang | 7f5aa4f | 2012-03-14 02:48:39 -0400 | [diff] [blame] | 28 | |
| 29 | #include <dlfcn.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 30 | #include <errno.h> |
Xi Wang | 7f5aa4f | 2012-03-14 02:48:39 -0400 | [diff] [blame] | 31 | #include <fcntl.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 32 | #include <pthread.h> |
Xi Wang | 7f5aa4f | 2012-03-14 02:48:39 -0400 | [diff] [blame] | 33 | #include <stdarg.h> |
| 34 | #include <stddef.h> |
| 35 | #include <stdint.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 36 | #include <stdio.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 37 | #include <stdlib.h> |
| 38 | #include <string.h> |
| 39 | #include <unistd.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 40 | #include <unwind.h> |
| 41 | |
Xi Wang | 7f5aa4f | 2012-03-14 02:48:39 -0400 | [diff] [blame] | 42 | #include <arpa/inet.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 43 | #include <sys/select.h> |
Xi Wang | 7f5aa4f | 2012-03-14 02:48:39 -0400 | [diff] [blame] | 44 | #include <sys/socket.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 45 | #include <sys/system_properties.h> |
Xi Wang | 7f5aa4f | 2012-03-14 02:48:39 -0400 | [diff] [blame] | 46 | #include <sys/types.h> |
| 47 | #include <sys/un.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 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; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 64 | |
| 65 | // ============================================================================= |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 66 | // stack trace functions |
Andy McFadden | 39f3745 | 2009-07-21 15:25:23 -0700 | [diff] [blame] | 67 | // ============================================================================= |
| 68 | |
Jin Wei | 9862f5e | 2012-08-01 14:48:57 +0800 | [diff] [blame^] | 69 | #ifndef MALLOC_ALIGNMENT |
| 70 | #define MALLOC_ALIGNMENT ((size_t)8U) |
| 71 | #endif |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 72 | #define GUARD 0x48151642 |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 73 | #define DEBUG 0 |
| 74 | |
| 75 | // ============================================================================= |
| 76 | // Structures |
| 77 | // ============================================================================= |
| 78 | typedef struct AllocationEntry AllocationEntry; |
| 79 | struct AllocationEntry { |
| 80 | HashEntry* entry; |
| 81 | uint32_t guard; |
| 82 | }; |
| 83 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 84 | |
| 85 | // ============================================================================= |
| 86 | // Hash Table functions |
| 87 | // ============================================================================= |
| 88 | static uint32_t get_hash(intptr_t* backtrace, size_t numEntries) |
| 89 | { |
| 90 | if (backtrace == NULL) return 0; |
| 91 | |
| 92 | int hash = 0; |
| 93 | size_t i; |
| 94 | for (i = 0 ; i < numEntries ; i++) { |
| 95 | hash = (hash * 33) + (backtrace[i] >> 2); |
| 96 | } |
| 97 | |
| 98 | return hash; |
| 99 | } |
| 100 | |
| 101 | static HashEntry* find_entry(HashTable* table, int slot, |
| 102 | intptr_t* backtrace, size_t numEntries, size_t size) |
| 103 | { |
| 104 | HashEntry* entry = table->slots[slot]; |
| 105 | while (entry != NULL) { |
| 106 | //debug_log("backtrace: %p, entry: %p entry->backtrace: %p\n", |
| 107 | // backtrace, entry, (entry != NULL) ? entry->backtrace : NULL); |
| 108 | /* |
| 109 | * See if the entry matches exactly. We compare the "size" field, |
| 110 | * including the flag bits. |
| 111 | */ |
| 112 | if (entry->size == size && entry->numEntries == numEntries && |
| 113 | !memcmp(backtrace, entry->backtrace, numEntries * sizeof(intptr_t))) { |
| 114 | return entry; |
| 115 | } |
| 116 | |
| 117 | entry = entry->next; |
| 118 | } |
| 119 | |
| 120 | return NULL; |
| 121 | } |
| 122 | |
| 123 | static HashEntry* record_backtrace(intptr_t* backtrace, size_t numEntries, size_t size) |
| 124 | { |
| 125 | size_t hash = get_hash(backtrace, numEntries); |
| 126 | size_t slot = hash % HASHTABLE_SIZE; |
| 127 | |
| 128 | if (size & SIZE_FLAG_MASK) { |
| 129 | debug_log("malloc_debug: allocation %zx exceeds bit width\n", size); |
| 130 | abort(); |
| 131 | } |
| 132 | |
| 133 | if (gMallocLeakZygoteChild) |
| 134 | size |= SIZE_FLAG_ZYGOTE_CHILD; |
| 135 | |
| 136 | HashEntry* entry = find_entry(&gHashTable, slot, backtrace, numEntries, size); |
| 137 | |
| 138 | if (entry != NULL) { |
| 139 | entry->allocations++; |
| 140 | } else { |
| 141 | // create a new entry |
| 142 | entry = (HashEntry*)dlmalloc(sizeof(HashEntry) + numEntries*sizeof(intptr_t)); |
André Goddard Rosa | 5751c54 | 2010-02-05 16:03:09 -0200 | [diff] [blame] | 143 | if (!entry) |
| 144 | return NULL; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 145 | entry->allocations = 1; |
| 146 | entry->slot = slot; |
| 147 | entry->prev = NULL; |
| 148 | entry->next = gHashTable.slots[slot]; |
| 149 | entry->numEntries = numEntries; |
| 150 | entry->size = size; |
| 151 | |
| 152 | memcpy(entry->backtrace, backtrace, numEntries * sizeof(intptr_t)); |
| 153 | |
| 154 | gHashTable.slots[slot] = entry; |
| 155 | |
| 156 | if (entry->next != NULL) { |
| 157 | entry->next->prev = entry; |
| 158 | } |
| 159 | |
| 160 | // we just added an entry, increase the size of the hashtable |
| 161 | gHashTable.count++; |
| 162 | } |
| 163 | |
| 164 | return entry; |
| 165 | } |
| 166 | |
| 167 | static int is_valid_entry(HashEntry* entry) |
| 168 | { |
| 169 | if (entry != NULL) { |
| 170 | int i; |
| 171 | for (i = 0 ; i < HASHTABLE_SIZE ; i++) { |
| 172 | HashEntry* e1 = gHashTable.slots[i]; |
| 173 | |
| 174 | while (e1 != NULL) { |
| 175 | if (e1 == entry) { |
| 176 | return 1; |
| 177 | } |
| 178 | |
| 179 | e1 = e1->next; |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | static void remove_entry(HashEntry* entry) |
| 188 | { |
| 189 | HashEntry* prev = entry->prev; |
| 190 | HashEntry* next = entry->next; |
| 191 | |
| 192 | if (prev != NULL) entry->prev->next = next; |
| 193 | if (next != NULL) entry->next->prev = prev; |
| 194 | |
| 195 | if (prev == NULL) { |
| 196 | // we are the head of the list. set the head to be next |
| 197 | gHashTable.slots[entry->slot] = entry->next; |
| 198 | } |
| 199 | |
| 200 | // we just removed and entry, decrease the size of the hashtable |
| 201 | gHashTable.count--; |
| 202 | } |
| 203 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 204 | // ============================================================================= |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 205 | // malloc fill functions |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 206 | // ============================================================================= |
| 207 | |
| 208 | #define CHK_FILL_FREE 0xef |
Bruce Beare | 89d3fdc | 2011-09-21 12:44:05 +0200 | [diff] [blame] | 209 | #define CHK_SENTINEL_VALUE (char)0xeb |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 210 | |
| 211 | void* fill_malloc(size_t bytes) |
| 212 | { |
| 213 | void* buffer = dlmalloc(bytes); |
| 214 | if (buffer) { |
| 215 | memset(buffer, CHK_SENTINEL_VALUE, bytes); |
| 216 | } |
| 217 | return buffer; |
| 218 | } |
| 219 | |
| 220 | void fill_free(void* mem) |
| 221 | { |
| 222 | size_t bytes = dlmalloc_usable_size(mem); |
| 223 | memset(mem, CHK_FILL_FREE, bytes); |
| 224 | dlfree(mem); |
| 225 | } |
| 226 | |
| 227 | void* fill_realloc(void* mem, size_t bytes) |
| 228 | { |
| 229 | void* buffer = fill_malloc(bytes); |
| 230 | if (mem == NULL) { |
| 231 | return buffer; |
| 232 | } |
| 233 | if (buffer) { |
| 234 | size_t old_size = dlmalloc_usable_size(mem); |
| 235 | size_t size = (bytes < old_size)?(bytes):(old_size); |
| 236 | memcpy(buffer, mem, size); |
| 237 | fill_free(mem); |
| 238 | } |
| 239 | return buffer; |
| 240 | } |
| 241 | |
| 242 | void* fill_memalign(size_t alignment, size_t bytes) |
| 243 | { |
| 244 | void* buffer = dlmemalign(alignment, bytes); |
| 245 | if (buffer) { |
| 246 | memset(buffer, CHK_SENTINEL_VALUE, bytes); |
| 247 | } |
| 248 | return buffer; |
| 249 | } |
| 250 | |
| 251 | // ============================================================================= |
| 252 | // malloc leak functions |
| 253 | // ============================================================================= |
| 254 | |
| 255 | #define MEMALIGN_GUARD ((void*)0xA1A41520) |
| 256 | |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 257 | extern __LIBC_HIDDEN__ |
| 258 | int get_backtrace(intptr_t* addrs, size_t max_entries); |
| 259 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 260 | void* leak_malloc(size_t bytes) |
| 261 | { |
| 262 | // allocate enough space infront of the allocation to store the pointer for |
| 263 | // the alloc structure. This will making free'ing the structer really fast! |
| 264 | |
| 265 | // 1. allocate enough memory and include our header |
| 266 | // 2. set the base pointer to be right after our header |
| 267 | |
Xi Wang | 7f5aa4f | 2012-03-14 02:48:39 -0400 | [diff] [blame] | 268 | size_t size = bytes + sizeof(AllocationEntry); |
| 269 | if (size < bytes) { // Overflow. |
| 270 | return NULL; |
| 271 | } |
| 272 | |
| 273 | void* base = dlmalloc(size); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 274 | if (base != NULL) { |
| 275 | pthread_mutex_lock(&gAllocationsMutex); |
| 276 | |
| 277 | intptr_t backtrace[BACKTRACE_SIZE]; |
| 278 | size_t numEntries = get_backtrace(backtrace, BACKTRACE_SIZE); |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 279 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 280 | AllocationEntry* header = (AllocationEntry*)base; |
| 281 | header->entry = record_backtrace(backtrace, numEntries, bytes); |
| 282 | header->guard = GUARD; |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 283 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 284 | // now increment base to point to after our header. |
| 285 | // this should just work since our header is 8 bytes. |
| 286 | base = (AllocationEntry*)base + 1; |
| 287 | |
| 288 | pthread_mutex_unlock(&gAllocationsMutex); |
| 289 | } |
| 290 | |
| 291 | return base; |
| 292 | } |
| 293 | |
| 294 | void leak_free(void* mem) |
| 295 | { |
| 296 | if (mem != NULL) { |
| 297 | pthread_mutex_lock(&gAllocationsMutex); |
| 298 | |
| 299 | // check the guard to make sure it is valid |
| 300 | AllocationEntry* header = (AllocationEntry*)mem - 1; |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 301 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 302 | if (header->guard != GUARD) { |
| 303 | // could be a memaligned block |
| 304 | if (((void**)mem)[-1] == MEMALIGN_GUARD) { |
| 305 | mem = ((void**)mem)[-2]; |
| 306 | header = (AllocationEntry*)mem - 1; |
| 307 | } |
| 308 | } |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 309 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 310 | if (header->guard == GUARD || is_valid_entry(header->entry)) { |
| 311 | // decrement the allocations |
| 312 | HashEntry* entry = header->entry; |
| 313 | entry->allocations--; |
| 314 | if (entry->allocations <= 0) { |
| 315 | remove_entry(entry); |
| 316 | dlfree(entry); |
| 317 | } |
| 318 | |
| 319 | // now free the memory! |
| 320 | dlfree(header); |
| 321 | } else { |
| 322 | debug_log("WARNING bad header guard: '0x%x'! and invalid entry: %p\n", |
| 323 | header->guard, header->entry); |
| 324 | } |
| 325 | |
| 326 | pthread_mutex_unlock(&gAllocationsMutex); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | void* leak_calloc(size_t n_elements, size_t elem_size) |
| 331 | { |
| 332 | size_t size; |
| 333 | void* ptr; |
| 334 | |
| 335 | /* Fail on overflow - just to be safe even though this code runs only |
| 336 | * within the debugging C library, not the production one */ |
| 337 | if (n_elements && MAX_SIZE_T / n_elements < elem_size) { |
| 338 | return NULL; |
| 339 | } |
| 340 | size = n_elements * elem_size; |
| 341 | ptr = leak_malloc(size); |
| 342 | if (ptr != NULL) { |
| 343 | memset(ptr, 0, size); |
| 344 | } |
| 345 | return ptr; |
| 346 | } |
| 347 | |
| 348 | void* leak_realloc(void* oldMem, size_t bytes) |
| 349 | { |
| 350 | if (oldMem == NULL) { |
| 351 | return leak_malloc(bytes); |
| 352 | } |
| 353 | void* newMem = NULL; |
| 354 | AllocationEntry* header = (AllocationEntry*)oldMem - 1; |
| 355 | if (header && header->guard == GUARD) { |
| 356 | size_t oldSize = header->entry->size & ~SIZE_FLAG_MASK; |
| 357 | newMem = leak_malloc(bytes); |
| 358 | if (newMem != NULL) { |
| 359 | size_t copySize = (oldSize <= bytes) ? oldSize : bytes; |
| 360 | memcpy(newMem, oldMem, copySize); |
| 361 | leak_free(oldMem); |
| 362 | } |
| 363 | } else { |
| 364 | newMem = dlrealloc(oldMem, bytes); |
| 365 | } |
| 366 | return newMem; |
| 367 | } |
| 368 | |
| 369 | void* leak_memalign(size_t alignment, size_t bytes) |
| 370 | { |
| 371 | // we can just use malloc |
| 372 | if (alignment <= MALLOC_ALIGNMENT) |
| 373 | return leak_malloc(bytes); |
| 374 | |
| 375 | // need to make sure it's a power of two |
| 376 | if (alignment & (alignment-1)) |
| 377 | alignment = 1L << (31 - __builtin_clz(alignment)); |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 378 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 379 | // here, aligment is at least MALLOC_ALIGNMENT<<1 bytes |
| 380 | // we will align by at least MALLOC_ALIGNMENT bytes |
| 381 | // and at most alignment-MALLOC_ALIGNMENT bytes |
| 382 | size_t size = (alignment-MALLOC_ALIGNMENT) + bytes; |
Xi Wang | 7f5aa4f | 2012-03-14 02:48:39 -0400 | [diff] [blame] | 383 | if (size < bytes) { // Overflow. |
| 384 | return NULL; |
| 385 | } |
| 386 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 387 | void* base = leak_malloc(size); |
| 388 | if (base != NULL) { |
| 389 | intptr_t ptr = (intptr_t)base; |
| 390 | if ((ptr % alignment) == 0) |
| 391 | return base; |
| 392 | |
| 393 | // align the pointer |
| 394 | ptr += ((-ptr) % alignment); |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 395 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 396 | // there is always enough space for the base pointer and the guard |
| 397 | ((void**)ptr)[-1] = MEMALIGN_GUARD; |
| 398 | ((void**)ptr)[-2] = base; |
| 399 | |
| 400 | return (void*)ptr; |
| 401 | } |
| 402 | return base; |
| 403 | } |