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