| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2012 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 | */ | 
|  | 28 |  | 
|  | 29 | #include <errno.h> | 
|  | 30 | #include <inttypes.h> | 
|  | 31 | #include <malloc.h> | 
|  | 32 | #include <string.h> | 
|  | 33 | #include <sys/cdefs.h> | 
|  | 34 | #include <sys/param.h> | 
|  | 35 | #include <unistd.h> | 
|  | 36 |  | 
|  | 37 | #include <vector> | 
|  | 38 |  | 
|  | 39 | #include <private/bionic_malloc_dispatch.h> | 
|  | 40 |  | 
|  | 41 | #include "backtrace.h" | 
| Christopher Ferris | 72df670 | 2016-02-11 15:51:31 -0800 | [diff] [blame] | 42 | #include "Config.h" | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 43 | #include "DebugData.h" | 
|  | 44 | #include "debug_disable.h" | 
|  | 45 | #include "debug_log.h" | 
|  | 46 | #include "malloc_debug.h" | 
|  | 47 |  | 
|  | 48 | // ------------------------------------------------------------------------ | 
|  | 49 | // Global Data | 
|  | 50 | // ------------------------------------------------------------------------ | 
|  | 51 | DebugData* g_debug; | 
|  | 52 |  | 
|  | 53 | int* g_malloc_zygote_child; | 
|  | 54 |  | 
|  | 55 | const MallocDispatch* g_dispatch; | 
|  | 56 | // ------------------------------------------------------------------------ | 
|  | 57 |  | 
|  | 58 | // ------------------------------------------------------------------------ | 
|  | 59 | // Use C style prototypes for all exported functions. This makes it easy | 
|  | 60 | // to do dlsym lookups during libc initialization when malloc debug | 
|  | 61 | // is enabled. | 
|  | 62 | // ------------------------------------------------------------------------ | 
|  | 63 | __BEGIN_DECLS | 
|  | 64 |  | 
|  | 65 | bool debug_initialize(const MallocDispatch* malloc_dispatch, int* malloc_zygote_child); | 
|  | 66 | void debug_finalize(); | 
|  | 67 | void debug_get_malloc_leak_info( | 
|  | 68 | uint8_t** info, size_t* overall_size, size_t* info_size, size_t* total_memory, | 
|  | 69 | size_t* backtrace_size); | 
| Colin Cross | 2d4721c | 2016-02-02 11:57:54 -0800 | [diff] [blame] | 70 | ssize_t debug_malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 71 | void debug_free_malloc_leak_info(uint8_t* info); | 
|  | 72 | size_t debug_malloc_usable_size(void* pointer); | 
|  | 73 | void* debug_malloc(size_t size); | 
|  | 74 | void debug_free(void* pointer); | 
|  | 75 | void* debug_memalign(size_t alignment, size_t bytes); | 
|  | 76 | void* debug_realloc(void* pointer, size_t bytes); | 
|  | 77 | void* debug_calloc(size_t nmemb, size_t bytes); | 
|  | 78 | struct mallinfo debug_mallinfo(); | 
|  | 79 | int debug_posix_memalign(void** memptr, size_t alignment, size_t size); | 
| Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 80 | int debug_iterate(uintptr_t base, size_t size, | 
|  | 81 | void (*callback)(uintptr_t base, size_t size, void* arg), void* arg); | 
|  | 82 | void debug_malloc_disable(); | 
|  | 83 | void debug_malloc_enable(); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 84 |  | 
|  | 85 | #if defined(HAVE_DEPRECATED_MALLOC_FUNCS) | 
|  | 86 | void* debug_pvalloc(size_t bytes); | 
|  | 87 | void* debug_valloc(size_t size); | 
|  | 88 | #endif | 
|  | 89 |  | 
|  | 90 | __END_DECLS | 
|  | 91 | // ------------------------------------------------------------------------ | 
|  | 92 |  | 
| Colin Cross | 7a28a3c | 2016-02-07 22:51:15 -0800 | [diff] [blame] | 93 | static void InitAtfork() { | 
|  | 94 | static pthread_once_t atfork_init = PTHREAD_ONCE_INIT; | 
|  | 95 | pthread_once(&atfork_init, [](){ | 
|  | 96 | pthread_atfork( | 
|  | 97 | [](){ | 
|  | 98 | if (g_debug != nullptr) { | 
|  | 99 | g_debug->PrepareFork(); | 
|  | 100 | } | 
|  | 101 | }, | 
|  | 102 | [](){ | 
|  | 103 | if (g_debug != nullptr) { | 
|  | 104 | g_debug->PostForkParent(); | 
|  | 105 | } | 
|  | 106 | }, | 
|  | 107 | [](){ | 
|  | 108 | if (g_debug != nullptr) { | 
|  | 109 | g_debug->PostForkChild(); | 
|  | 110 | } | 
|  | 111 | } | 
|  | 112 | ); | 
|  | 113 | }); | 
|  | 114 | } | 
| Christopher Ferris | d091962 | 2016-03-15 22:39:39 -0700 | [diff] [blame] | 115 |  | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 116 | static void LogTagError(const Header* header, const void* pointer, const char* name) { | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 117 | error_log(LOG_DIVIDER); | 
| Christopher Ferris | 7993b80 | 2016-01-28 18:35:05 -0800 | [diff] [blame] | 118 | if (header->tag == DEBUG_FREE_TAG) { | 
|  | 119 | error_log("+++ ALLOCATION %p USED AFTER FREE (%s)", pointer, name); | 
|  | 120 | if (g_debug->config().options & FREE_TRACK) { | 
|  | 121 | g_debug->free_track->LogBacktrace(header); | 
|  | 122 | } | 
|  | 123 | } else { | 
|  | 124 | error_log("+++ ALLOCATION %p HAS INVALID TAG %" PRIx32 " (%s)", pointer, header->tag, name); | 
|  | 125 | } | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 126 | error_log("Backtrace at time of failure:"); | 
|  | 127 | std::vector<uintptr_t> frames(64); | 
|  | 128 | size_t frame_num = backtrace_get(frames.data(), frames.size()); | 
|  | 129 | frames.resize(frame_num); | 
|  | 130 | backtrace_log(frames.data(), frames.size()); | 
|  | 131 | error_log(LOG_DIVIDER); | 
|  | 132 | } | 
|  | 133 |  | 
|  | 134 | static void* InitHeader(Header* header, void* orig_pointer, size_t size) { | 
|  | 135 | header->tag = DEBUG_TAG; | 
|  | 136 | header->orig_pointer = orig_pointer; | 
|  | 137 | header->size = size; | 
|  | 138 | if (*g_malloc_zygote_child) { | 
|  | 139 | header->set_zygote(); | 
|  | 140 | } | 
|  | 141 | header->usable_size = g_dispatch->malloc_usable_size(orig_pointer); | 
|  | 142 | if (header->usable_size == 0) { | 
|  | 143 | g_dispatch->free(orig_pointer); | 
|  | 144 | return nullptr; | 
|  | 145 | } | 
|  | 146 | header->usable_size -= g_debug->pointer_offset() + | 
| Christopher Ferris | d091962 | 2016-03-15 22:39:39 -0700 | [diff] [blame] | 147 | reinterpret_cast<uintptr_t>(header) - reinterpret_cast<uintptr_t>(orig_pointer); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 148 |  | 
|  | 149 | if (g_debug->config().options & FRONT_GUARD) { | 
|  | 150 | uint8_t* guard = g_debug->GetFrontGuard(header); | 
|  | 151 | memset(guard, g_debug->config().front_guard_value, g_debug->config().front_guard_bytes); | 
|  | 152 | } | 
|  | 153 |  | 
|  | 154 | if (g_debug->config().options & REAR_GUARD) { | 
|  | 155 | uint8_t* guard = g_debug->GetRearGuard(header); | 
|  | 156 | memset(guard, g_debug->config().rear_guard_value, g_debug->config().rear_guard_bytes); | 
|  | 157 | // If the rear guard is enabled, set the usable size to the exact size | 
|  | 158 | // of the allocation. | 
|  | 159 | header->usable_size = header->real_size(); | 
|  | 160 | } | 
|  | 161 |  | 
|  | 162 | bool backtrace_found = false; | 
|  | 163 | if (g_debug->config().options & BACKTRACE) { | 
|  | 164 | BacktraceHeader* back_header = g_debug->GetAllocBacktrace(header); | 
|  | 165 | if (g_debug->backtrace->enabled()) { | 
| Christopher Ferris | 7993b80 | 2016-01-28 18:35:05 -0800 | [diff] [blame] | 166 | back_header->num_frames = backtrace_get( | 
|  | 167 | &back_header->frames[0], g_debug->config().backtrace_frames); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 168 | backtrace_found = back_header->num_frames > 0; | 
|  | 169 | } else { | 
|  | 170 | back_header->num_frames = 0; | 
|  | 171 | } | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 172 | } | 
|  | 173 |  | 
|  | 174 | if (g_debug->config().options & TRACK_ALLOCS) { | 
|  | 175 | g_debug->track->Add(header, backtrace_found); | 
|  | 176 | } | 
|  | 177 |  | 
|  | 178 | return g_debug->GetPointer(header); | 
|  | 179 | } | 
|  | 180 |  | 
|  | 181 | bool debug_initialize(const MallocDispatch* malloc_dispatch, int* malloc_zygote_child) { | 
|  | 182 | if (malloc_zygote_child == nullptr) { | 
|  | 183 | return false; | 
|  | 184 | } | 
| Colin Cross | 7a28a3c | 2016-02-07 22:51:15 -0800 | [diff] [blame] | 185 |  | 
|  | 186 | InitAtfork(); | 
|  | 187 |  | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 188 | g_malloc_zygote_child = malloc_zygote_child; | 
|  | 189 |  | 
|  | 190 | g_dispatch = malloc_dispatch; | 
|  | 191 |  | 
|  | 192 | if (!DebugDisableInitialize()) { | 
|  | 193 | return false; | 
|  | 194 | } | 
|  | 195 |  | 
|  | 196 | DebugData* debug = new DebugData(); | 
|  | 197 | if (!debug->Initialize()) { | 
|  | 198 | delete debug; | 
|  | 199 | DebugDisableFinalize(); | 
|  | 200 | return false; | 
|  | 201 | } | 
|  | 202 | g_debug = debug; | 
|  | 203 |  | 
|  | 204 | // Always enable the backtrace code since we will use it in a number | 
|  | 205 | // of different error cases. | 
|  | 206 | backtrace_startup(); | 
|  | 207 |  | 
|  | 208 | return true; | 
|  | 209 | } | 
|  | 210 |  | 
|  | 211 | void debug_finalize() { | 
|  | 212 | if (g_debug == nullptr) { | 
|  | 213 | return; | 
|  | 214 | } | 
|  | 215 |  | 
|  | 216 | if (g_debug->config().options & FREE_TRACK) { | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 217 | g_debug->free_track->VerifyAll(); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 218 | } | 
|  | 219 |  | 
|  | 220 | if (g_debug->config().options & LEAK_TRACK) { | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 221 | g_debug->track->DisplayLeaks(); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 222 | } | 
|  | 223 |  | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 224 | DebugDisableSet(true); | 
|  | 225 |  | 
| Colin Cross | 2c75991 | 2016-02-05 16:17:39 -0800 | [diff] [blame] | 226 | backtrace_shutdown(); | 
|  | 227 |  | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 228 | delete g_debug; | 
|  | 229 | g_debug = nullptr; | 
|  | 230 |  | 
|  | 231 | DebugDisableFinalize(); | 
|  | 232 | } | 
|  | 233 |  | 
|  | 234 | void debug_get_malloc_leak_info(uint8_t** info, size_t* overall_size, | 
|  | 235 | size_t* info_size, size_t* total_memory, size_t* backtrace_size) { | 
|  | 236 | ScopedDisableDebugCalls disable; | 
|  | 237 |  | 
|  | 238 | // Verify the arguments. | 
|  | 239 | if (info == nullptr || overall_size == nullptr || info_size == NULL || | 
|  | 240 | total_memory == nullptr || backtrace_size == nullptr) { | 
|  | 241 | error_log("get_malloc_leak_info: At least one invalid parameter."); | 
|  | 242 | return; | 
|  | 243 | } | 
|  | 244 |  | 
|  | 245 | *info = nullptr; | 
|  | 246 | *overall_size = 0; | 
|  | 247 | *info_size = 0; | 
|  | 248 | *total_memory = 0; | 
|  | 249 | *backtrace_size = 0; | 
|  | 250 |  | 
|  | 251 | if (!(g_debug->config().options & BACKTRACE)) { | 
|  | 252 | error_log("get_malloc_leak_info: Allocations not being tracked, to enable " | 
|  | 253 | "set the option 'backtrace'."); | 
|  | 254 | return; | 
|  | 255 | } | 
|  | 256 |  | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 257 | g_debug->track->GetInfo(info, overall_size, info_size, total_memory, backtrace_size); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 258 | } | 
|  | 259 |  | 
|  | 260 | void debug_free_malloc_leak_info(uint8_t* info) { | 
|  | 261 | g_dispatch->free(info); | 
|  | 262 | } | 
|  | 263 |  | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 264 | static size_t internal_malloc_usable_size(void* pointer) { | 
|  | 265 | if (g_debug->need_header()) { | 
|  | 266 | Header* header = g_debug->GetHeader(pointer); | 
|  | 267 | if (header->tag != DEBUG_TAG) { | 
|  | 268 | LogTagError(header, pointer, "malloc_usable_size"); | 
|  | 269 | return 0; | 
|  | 270 | } | 
|  | 271 |  | 
|  | 272 | return header->usable_size; | 
|  | 273 | } else { | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 274 | return g_dispatch->malloc_usable_size(pointer); | 
|  | 275 | } | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 276 | } | 
|  | 277 |  | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 278 | size_t debug_malloc_usable_size(void* pointer) { | 
|  | 279 | if (DebugCallsDisabled() || pointer == nullptr) { | 
|  | 280 | return g_dispatch->malloc_usable_size(pointer); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 281 | } | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 282 | ScopedDisableDebugCalls disable; | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 283 |  | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 284 | return internal_malloc_usable_size(pointer); | 
|  | 285 | } | 
|  | 286 |  | 
|  | 287 | static void *internal_malloc(size_t size) { | 
| Colin Cross | 9567c7b | 2016-03-09 17:56:14 -0800 | [diff] [blame] | 288 | if (size == 0) { | 
|  | 289 | size = 1; | 
|  | 290 | } | 
|  | 291 |  | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 292 | size_t real_size = size + g_debug->extra_bytes(); | 
|  | 293 | if (real_size < size) { | 
|  | 294 | // Overflow. | 
|  | 295 | errno = ENOMEM; | 
|  | 296 | return nullptr; | 
|  | 297 | } | 
|  | 298 |  | 
|  | 299 | void* pointer; | 
|  | 300 | if (g_debug->need_header()) { | 
|  | 301 | if (size > Header::max_size()) { | 
|  | 302 | errno = ENOMEM; | 
|  | 303 | return nullptr; | 
|  | 304 | } | 
|  | 305 |  | 
| Christopher Ferris | 72df670 | 2016-02-11 15:51:31 -0800 | [diff] [blame] | 306 | Header* header = reinterpret_cast<Header*>( | 
|  | 307 | g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size)); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 308 | if (header == nullptr) { | 
|  | 309 | return nullptr; | 
|  | 310 | } | 
|  | 311 | pointer = InitHeader(header, header, size); | 
|  | 312 | } else { | 
|  | 313 | pointer = g_dispatch->malloc(real_size); | 
|  | 314 | } | 
|  | 315 |  | 
|  | 316 | if (pointer != nullptr && g_debug->config().options & FILL_ON_ALLOC) { | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 317 | size_t bytes = internal_malloc_usable_size(pointer); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 318 | size_t fill_bytes = g_debug->config().fill_on_alloc_bytes; | 
|  | 319 | bytes = (bytes < fill_bytes) ? bytes : fill_bytes; | 
|  | 320 | memset(pointer, g_debug->config().fill_alloc_value, bytes); | 
|  | 321 | } | 
|  | 322 | return pointer; | 
|  | 323 | } | 
|  | 324 |  | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 325 | void* debug_malloc(size_t size) { | 
|  | 326 | if (DebugCallsDisabled()) { | 
|  | 327 | return g_dispatch->malloc(size); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 328 | } | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 329 | ScopedDisableDebugCalls disable; | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 330 |  | 
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 331 | void* pointer = internal_malloc(size); | 
|  | 332 |  | 
|  | 333 | if (g_debug->config().options & RECORD_ALLOCS) { | 
|  | 334 | g_debug->record->AddEntry(new MallocEntry(pointer, size)); | 
|  | 335 | } | 
|  | 336 |  | 
|  | 337 | return pointer; | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 338 | } | 
|  | 339 |  | 
|  | 340 | static void internal_free(void* pointer) { | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 341 | void* free_pointer = pointer; | 
|  | 342 | size_t bytes; | 
| Christopher Ferris | d091962 | 2016-03-15 22:39:39 -0700 | [diff] [blame] | 343 | Header* header; | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 344 | if (g_debug->need_header()) { | 
| Christopher Ferris | d091962 | 2016-03-15 22:39:39 -0700 | [diff] [blame] | 345 | header = g_debug->GetHeader(pointer); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 346 | if (header->tag != DEBUG_TAG) { | 
|  | 347 | LogTagError(header, pointer, "free"); | 
|  | 348 | return; | 
|  | 349 | } | 
|  | 350 | free_pointer = header->orig_pointer; | 
|  | 351 |  | 
|  | 352 | if (g_debug->config().options & FRONT_GUARD) { | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 353 | if (!g_debug->front_guard->Valid(header)) { | 
|  | 354 | g_debug->front_guard->LogFailure(header); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 355 | } | 
|  | 356 | } | 
|  | 357 | if (g_debug->config().options & REAR_GUARD) { | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 358 | if (!g_debug->rear_guard->Valid(header)) { | 
|  | 359 | g_debug->rear_guard->LogFailure(header); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 360 | } | 
|  | 361 | } | 
|  | 362 |  | 
|  | 363 | if (g_debug->config().options & TRACK_ALLOCS) { | 
|  | 364 | bool backtrace_found = false; | 
|  | 365 | if (g_debug->config().options & BACKTRACE) { | 
|  | 366 | BacktraceHeader* back_header = g_debug->GetAllocBacktrace(header); | 
|  | 367 | backtrace_found = back_header->num_frames > 0; | 
|  | 368 | } | 
|  | 369 | g_debug->track->Remove(header, backtrace_found); | 
|  | 370 | } | 
| Christopher Ferris | 7993b80 | 2016-01-28 18:35:05 -0800 | [diff] [blame] | 371 | header->tag = DEBUG_FREE_TAG; | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 372 |  | 
|  | 373 | bytes = header->usable_size; | 
|  | 374 | } else { | 
|  | 375 | bytes = g_dispatch->malloc_usable_size(pointer); | 
|  | 376 | } | 
|  | 377 |  | 
|  | 378 | if (g_debug->config().options & FILL_ON_FREE) { | 
|  | 379 | size_t fill_bytes = g_debug->config().fill_on_free_bytes; | 
|  | 380 | bytes = (bytes < fill_bytes) ? bytes : fill_bytes; | 
|  | 381 | memset(pointer, g_debug->config().fill_free_value, bytes); | 
|  | 382 | } | 
|  | 383 |  | 
| Christopher Ferris | d091962 | 2016-03-15 22:39:39 -0700 | [diff] [blame] | 384 | if (g_debug->config().options & FREE_TRACK) { | 
|  | 385 | // Do not add the allocation until we are done modifying the pointer | 
|  | 386 | // itself. This avoids a race if a lot of threads are all doing | 
|  | 387 | // frees at the same time and we wind up trying to really free this | 
|  | 388 | // pointer from another thread, while still trying to free it in | 
|  | 389 | // this function. | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 390 | g_debug->free_track->Add(header); | 
| Christopher Ferris | d091962 | 2016-03-15 22:39:39 -0700 | [diff] [blame] | 391 | } else { | 
|  | 392 | g_dispatch->free(free_pointer); | 
|  | 393 | } | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 394 | } | 
|  | 395 |  | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 396 | void debug_free(void* pointer) { | 
|  | 397 | if (DebugCallsDisabled() || pointer == nullptr) { | 
|  | 398 | return g_dispatch->free(pointer); | 
|  | 399 | } | 
|  | 400 | ScopedDisableDebugCalls disable; | 
|  | 401 |  | 
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 402 | if (g_debug->config().options & RECORD_ALLOCS) { | 
|  | 403 | g_debug->record->AddEntry(new FreeEntry(pointer)); | 
|  | 404 | } | 
|  | 405 |  | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 406 | internal_free(pointer); | 
|  | 407 | } | 
|  | 408 |  | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 409 | void* debug_memalign(size_t alignment, size_t bytes) { | 
|  | 410 | if (DebugCallsDisabled()) { | 
|  | 411 | return g_dispatch->memalign(alignment, bytes); | 
|  | 412 | } | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 413 | ScopedDisableDebugCalls disable; | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 414 |  | 
| Colin Cross | 9567c7b | 2016-03-09 17:56:14 -0800 | [diff] [blame] | 415 | if (bytes == 0) { | 
|  | 416 | bytes = 1; | 
|  | 417 | } | 
|  | 418 |  | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 419 | void* pointer; | 
|  | 420 | if (g_debug->need_header()) { | 
|  | 421 | if (bytes > Header::max_size()) { | 
|  | 422 | errno = ENOMEM; | 
|  | 423 | return nullptr; | 
|  | 424 | } | 
|  | 425 |  | 
|  | 426 | // Make the alignment a power of two. | 
|  | 427 | if (!powerof2(alignment)) { | 
|  | 428 | alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment); | 
|  | 429 | } | 
| Christopher Ferris | 72df670 | 2016-02-11 15:51:31 -0800 | [diff] [blame] | 430 | // Force the alignment to at least MINIMUM_ALIGNMENT_BYTES to guarantee | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 431 | // that the header is aligned properly. | 
| Christopher Ferris | 72df670 | 2016-02-11 15:51:31 -0800 | [diff] [blame] | 432 | if (alignment < MINIMUM_ALIGNMENT_BYTES) { | 
|  | 433 | alignment = MINIMUM_ALIGNMENT_BYTES; | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 434 | } | 
|  | 435 |  | 
|  | 436 | // We don't have any idea what the natural alignment of | 
|  | 437 | // the underlying native allocator is, so we always need to | 
|  | 438 | // over allocate. | 
|  | 439 | size_t real_size = alignment + bytes + g_debug->extra_bytes(); | 
|  | 440 | if (real_size < bytes) { | 
|  | 441 | // Overflow. | 
|  | 442 | errno = ENOMEM; | 
|  | 443 | return nullptr; | 
|  | 444 | } | 
|  | 445 |  | 
|  | 446 | pointer = g_dispatch->malloc(real_size); | 
|  | 447 | if (pointer == nullptr) { | 
|  | 448 | return nullptr; | 
|  | 449 | } | 
|  | 450 |  | 
|  | 451 | uintptr_t value = reinterpret_cast<uintptr_t>(pointer) + g_debug->pointer_offset(); | 
|  | 452 | // Now align the pointer. | 
|  | 453 | value += (-value % alignment); | 
|  | 454 |  | 
|  | 455 | Header* header = g_debug->GetHeader(reinterpret_cast<void*>(value)); | 
|  | 456 | pointer = InitHeader(header, pointer, bytes); | 
|  | 457 | } else { | 
|  | 458 | size_t real_size = bytes + g_debug->extra_bytes(); | 
|  | 459 | if (real_size < bytes) { | 
|  | 460 | // Overflow. | 
|  | 461 | errno = ENOMEM; | 
|  | 462 | return nullptr; | 
|  | 463 | } | 
|  | 464 | pointer = g_dispatch->memalign(alignment, real_size); | 
|  | 465 | } | 
|  | 466 |  | 
|  | 467 | if (pointer != nullptr && g_debug->config().options & FILL_ON_ALLOC) { | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 468 | size_t bytes = internal_malloc_usable_size(pointer); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 469 | size_t fill_bytes = g_debug->config().fill_on_alloc_bytes; | 
|  | 470 | bytes = (bytes < fill_bytes) ? bytes : fill_bytes; | 
|  | 471 | memset(pointer, g_debug->config().fill_alloc_value, bytes); | 
|  | 472 | } | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 473 |  | 
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 474 | if (g_debug->config().options & RECORD_ALLOCS) { | 
|  | 475 | g_debug->record->AddEntry(new MemalignEntry(pointer, bytes, alignment)); | 
|  | 476 | } | 
|  | 477 |  | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 478 | return pointer; | 
|  | 479 | } | 
|  | 480 |  | 
|  | 481 | void* debug_realloc(void* pointer, size_t bytes) { | 
|  | 482 | if (DebugCallsDisabled()) { | 
|  | 483 | return g_dispatch->realloc(pointer, bytes); | 
|  | 484 | } | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 485 | ScopedDisableDebugCalls disable; | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 486 |  | 
|  | 487 | if (pointer == nullptr) { | 
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 488 | pointer = internal_malloc(bytes); | 
|  | 489 | if (g_debug->config().options & RECORD_ALLOCS) { | 
|  | 490 | g_debug->record->AddEntry(new ReallocEntry(pointer, bytes, nullptr)); | 
|  | 491 | } | 
|  | 492 | return pointer; | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 493 | } | 
|  | 494 |  | 
|  | 495 | if (bytes == 0) { | 
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 496 | if (g_debug->config().options & RECORD_ALLOCS) { | 
|  | 497 | g_debug->record->AddEntry(new ReallocEntry(nullptr, bytes, pointer)); | 
|  | 498 | } | 
|  | 499 |  | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 500 | internal_free(pointer); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 501 | return nullptr; | 
|  | 502 | } | 
|  | 503 |  | 
|  | 504 | size_t real_size = bytes; | 
|  | 505 | if (g_debug->config().options & EXPAND_ALLOC) { | 
|  | 506 | real_size += g_debug->config().expand_alloc_bytes; | 
|  | 507 | if (real_size < bytes) { | 
|  | 508 | // Overflow. | 
|  | 509 | errno = ENOMEM; | 
|  | 510 | return nullptr; | 
|  | 511 | } | 
|  | 512 | } | 
|  | 513 |  | 
|  | 514 | void* new_pointer; | 
|  | 515 | size_t prev_size; | 
|  | 516 | if (g_debug->need_header()) { | 
|  | 517 | if (bytes > Header::max_size()) { | 
|  | 518 | errno = ENOMEM; | 
|  | 519 | return nullptr; | 
|  | 520 | } | 
|  | 521 |  | 
|  | 522 | Header* header = g_debug->GetHeader(pointer); | 
|  | 523 | if (header->tag != DEBUG_TAG) { | 
|  | 524 | LogTagError(header, pointer, "realloc"); | 
|  | 525 | return nullptr; | 
|  | 526 | } | 
|  | 527 |  | 
|  | 528 | // Same size, do nothing. | 
|  | 529 | if (real_size == header->real_size()) { | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 530 | // Do not bother recording, this is essentially a nop. | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 531 | return pointer; | 
|  | 532 | } | 
|  | 533 |  | 
|  | 534 | // Allocation is shrinking. | 
|  | 535 | if (real_size < header->usable_size) { | 
|  | 536 | header->size = real_size; | 
|  | 537 | if (*g_malloc_zygote_child) { | 
|  | 538 | header->set_zygote(); | 
|  | 539 | } | 
|  | 540 | if (g_debug->config().options & REAR_GUARD) { | 
|  | 541 | // Don't bother allocating a smaller pointer in this case, simply | 
|  | 542 | // change the header usable_size and reset the rear guard. | 
|  | 543 | header->usable_size = header->real_size(); | 
|  | 544 | memset(g_debug->GetRearGuard(header), g_debug->config().rear_guard_value, | 
|  | 545 | g_debug->config().rear_guard_bytes); | 
|  | 546 | } | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 547 | // Do not bother recording, this is essentially a nop. | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 548 | return pointer; | 
|  | 549 | } | 
|  | 550 |  | 
|  | 551 | // Allocate the new size. | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 552 | new_pointer = internal_malloc(bytes); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 553 | if (new_pointer == nullptr) { | 
|  | 554 | errno = ENOMEM; | 
|  | 555 | return nullptr; | 
|  | 556 | } | 
|  | 557 |  | 
|  | 558 | prev_size = header->usable_size; | 
|  | 559 | memcpy(new_pointer, pointer, prev_size); | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 560 | internal_free(pointer); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 561 | } else { | 
|  | 562 | prev_size = g_dispatch->malloc_usable_size(pointer); | 
|  | 563 | new_pointer = g_dispatch->realloc(pointer, real_size); | 
|  | 564 | if (new_pointer == nullptr) { | 
|  | 565 | return nullptr; | 
|  | 566 | } | 
|  | 567 | } | 
|  | 568 |  | 
|  | 569 | if (g_debug->config().options & FILL_ON_ALLOC) { | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 570 | size_t bytes = internal_malloc_usable_size(new_pointer); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 571 | if (bytes > g_debug->config().fill_on_alloc_bytes) { | 
|  | 572 | bytes = g_debug->config().fill_on_alloc_bytes; | 
|  | 573 | } | 
|  | 574 | if (bytes > prev_size) { | 
|  | 575 | memset(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(new_pointer) + prev_size), | 
|  | 576 | g_debug->config().fill_alloc_value, bytes - prev_size); | 
|  | 577 | } | 
|  | 578 | } | 
|  | 579 |  | 
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 580 | if (g_debug->config().options & RECORD_ALLOCS) { | 
|  | 581 | g_debug->record->AddEntry(new ReallocEntry(new_pointer, bytes, pointer)); | 
|  | 582 | } | 
|  | 583 |  | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 584 | return new_pointer; | 
|  | 585 | } | 
|  | 586 |  | 
|  | 587 | void* debug_calloc(size_t nmemb, size_t bytes) { | 
|  | 588 | if (DebugCallsDisabled()) { | 
|  | 589 | return g_dispatch->calloc(nmemb, bytes); | 
|  | 590 | } | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 591 | ScopedDisableDebugCalls disable; | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 592 |  | 
| Colin Cross | 7877df6 | 2016-03-10 13:01:27 -0800 | [diff] [blame] | 593 | size_t size; | 
|  | 594 | if (__builtin_mul_overflow(nmemb, bytes, &size)) { | 
|  | 595 | // Overflow | 
|  | 596 | errno = ENOMEM; | 
|  | 597 | return nullptr; | 
|  | 598 | } | 
|  | 599 |  | 
| Colin Cross | 9567c7b | 2016-03-09 17:56:14 -0800 | [diff] [blame] | 600 | if (size == 0) { | 
|  | 601 | size = 1; | 
|  | 602 | } | 
|  | 603 |  | 
| Colin Cross | 7877df6 | 2016-03-10 13:01:27 -0800 | [diff] [blame] | 604 | size_t real_size; | 
|  | 605 | if (__builtin_add_overflow(size, g_debug->extra_bytes(), &real_size)) { | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 606 | // Overflow. | 
|  | 607 | errno = ENOMEM; | 
|  | 608 | return nullptr; | 
|  | 609 | } | 
|  | 610 |  | 
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 611 | void* pointer; | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 612 | if (g_debug->need_header()) { | 
|  | 613 | // The above check will guarantee the multiply will not overflow. | 
| Colin Cross | 9567c7b | 2016-03-09 17:56:14 -0800 | [diff] [blame] | 614 | if (size > Header::max_size()) { | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 615 | errno = ENOMEM; | 
|  | 616 | return nullptr; | 
|  | 617 | } | 
|  | 618 |  | 
|  | 619 | // Need to guarantee the alignment of the header. | 
| Christopher Ferris | 72df670 | 2016-02-11 15:51:31 -0800 | [diff] [blame] | 620 | Header* header = reinterpret_cast<Header*>( | 
|  | 621 | g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size)); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 622 | if (header == nullptr) { | 
|  | 623 | return nullptr; | 
|  | 624 | } | 
|  | 625 | memset(header, 0, g_dispatch->malloc_usable_size(header)); | 
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 626 | pointer = InitHeader(header, header, size); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 627 | } else { | 
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 628 | pointer = g_dispatch->calloc(1, real_size); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 629 | } | 
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 630 | if (g_debug->config().options & RECORD_ALLOCS) { | 
|  | 631 | g_debug->record->AddEntry(new CallocEntry(pointer, bytes, nmemb)); | 
|  | 632 | } | 
|  | 633 | return pointer; | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 634 | } | 
|  | 635 |  | 
|  | 636 | struct mallinfo debug_mallinfo() { | 
|  | 637 | return g_dispatch->mallinfo(); | 
|  | 638 | } | 
|  | 639 |  | 
|  | 640 | int debug_posix_memalign(void** memptr, size_t alignment, size_t size) { | 
|  | 641 | if (DebugCallsDisabled()) { | 
|  | 642 | return g_dispatch->posix_memalign(memptr, alignment, size); | 
|  | 643 | } | 
|  | 644 |  | 
|  | 645 | if (!powerof2(alignment)) { | 
|  | 646 | return EINVAL; | 
|  | 647 | } | 
|  | 648 | int saved_errno = errno; | 
|  | 649 | *memptr = debug_memalign(alignment, size); | 
|  | 650 | errno = saved_errno; | 
|  | 651 | return (*memptr != nullptr) ? 0 : ENOMEM; | 
|  | 652 | } | 
|  | 653 |  | 
| Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 654 | int debug_iterate(uintptr_t base, size_t size, | 
|  | 655 | void (*callback)(uintptr_t base, size_t size, void* arg), void* arg) { | 
|  | 656 | // Can't allocate, malloc is disabled | 
|  | 657 | // Manual capture of the arguments to pass to the lambda below as void* arg | 
|  | 658 | struct iterate_ctx { | 
|  | 659 | decltype(callback) callback; | 
|  | 660 | decltype(arg) arg; | 
|  | 661 | } ctx = { callback, arg }; | 
|  | 662 |  | 
|  | 663 | return g_dispatch->iterate(base, size, | 
|  | 664 | [](uintptr_t base, size_t size, void* arg) { | 
|  | 665 | const iterate_ctx* ctx = reinterpret_cast<iterate_ctx*>(arg); | 
|  | 666 | const void* pointer = reinterpret_cast<void*>(base); | 
|  | 667 | if (g_debug->need_header()) { | 
|  | 668 | const Header* header = reinterpret_cast<const Header*>(pointer); | 
|  | 669 | if (g_debug->config().options & TRACK_ALLOCS) { | 
|  | 670 | if (g_debug->track->Contains(header)) { | 
|  | 671 | // Return just the body of the allocation if we're sure the header exists | 
|  | 672 | ctx->callback(reinterpret_cast<uintptr_t>(g_debug->GetPointer(header)), | 
| Colin Cross | baa7c6f | 2016-03-09 16:33:44 -0800 | [diff] [blame] | 673 | header->usable_size, ctx->arg); | 
| Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 674 | return; | 
|  | 675 | } | 
|  | 676 | } | 
|  | 677 | } | 
|  | 678 | // Fall back to returning the whole allocation | 
|  | 679 | ctx->callback(base, size, ctx->arg); | 
|  | 680 | }, &ctx); | 
|  | 681 | } | 
|  | 682 |  | 
|  | 683 | void debug_malloc_disable() { | 
|  | 684 | g_dispatch->malloc_disable(); | 
|  | 685 | if (g_debug->track) { | 
|  | 686 | g_debug->track->PrepareFork(); | 
|  | 687 | } | 
|  | 688 | } | 
|  | 689 |  | 
|  | 690 | void debug_malloc_enable() { | 
|  | 691 | if (g_debug->track) { | 
|  | 692 | g_debug->track->PostForkParent(); | 
|  | 693 | } | 
|  | 694 | g_dispatch->malloc_enable(); | 
|  | 695 | } | 
|  | 696 |  | 
| Colin Cross | 2d4721c | 2016-02-02 11:57:54 -0800 | [diff] [blame] | 697 | ssize_t debug_malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count) { | 
|  | 698 | if (DebugCallsDisabled() || pointer == nullptr) { | 
|  | 699 | return 0; | 
|  | 700 | } | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 701 | ScopedDisableDebugCalls disable; | 
| Colin Cross | 2d4721c | 2016-02-02 11:57:54 -0800 | [diff] [blame] | 702 |  | 
|  | 703 | if (g_debug->need_header()) { | 
|  | 704 | Header* header; | 
|  | 705 | if (g_debug->config().options & TRACK_ALLOCS) { | 
|  | 706 | header = g_debug->GetHeader(pointer); | 
|  | 707 | if (!g_debug->track->Contains(header)) { | 
|  | 708 | return 0; | 
|  | 709 | } | 
|  | 710 | } else { | 
|  | 711 | header = reinterpret_cast<Header*>(pointer); | 
|  | 712 | } | 
|  | 713 | if (header->tag != DEBUG_TAG) { | 
|  | 714 | return 0; | 
|  | 715 | } | 
|  | 716 | if (g_debug->config().options & BACKTRACE) { | 
|  | 717 | BacktraceHeader* back_header = g_debug->GetAllocBacktrace(header); | 
|  | 718 | if (back_header->num_frames > 0) { | 
|  | 719 | if (frame_count > back_header->num_frames) { | 
|  | 720 | frame_count = back_header->num_frames; | 
|  | 721 | } | 
|  | 722 | memcpy(frames, &back_header->frames[0], frame_count * sizeof(uintptr_t)); | 
|  | 723 | return frame_count; | 
|  | 724 | } | 
|  | 725 | } | 
|  | 726 | } | 
|  | 727 |  | 
|  | 728 | return 0; | 
|  | 729 | } | 
|  | 730 |  | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 731 | #if defined(HAVE_DEPRECATED_MALLOC_FUNCS) | 
|  | 732 | void* debug_pvalloc(size_t bytes) { | 
|  | 733 | if (DebugCallsDisabled()) { | 
|  | 734 | return g_dispatch->pvalloc(bytes); | 
|  | 735 | } | 
|  | 736 |  | 
|  | 737 | size_t pagesize = getpagesize(); | 
|  | 738 | size_t size = BIONIC_ALIGN(bytes, pagesize); | 
|  | 739 | if (size < bytes) { | 
|  | 740 | // Overflow | 
|  | 741 | errno = ENOMEM; | 
|  | 742 | return nullptr; | 
|  | 743 | } | 
|  | 744 | return debug_memalign(pagesize, size); | 
|  | 745 | } | 
|  | 746 |  | 
|  | 747 | void* debug_valloc(size_t size) { | 
|  | 748 | if (DebugCallsDisabled()) { | 
|  | 749 | return g_dispatch->valloc(size); | 
|  | 750 | } | 
|  | 751 | return debug_memalign(getpagesize(), size); | 
|  | 752 | } | 
|  | 753 | #endif |