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