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