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