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