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