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