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