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> |
Christopher Ferris | d269fcc | 2019-05-06 19:03:59 -0700 | [diff] [blame] | 32 | #include <pthread.h> |
Christopher Ferris | 9bf7817 | 2020-05-20 15:37:30 -0700 | [diff] [blame^] | 33 | #include <signal.h> |
Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 34 | #include <stdio.h> |
Christopher Ferris | c328e44 | 2019-04-01 19:31:26 -0700 | [diff] [blame] | 35 | #include <stdlib.h> |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 36 | #include <string.h> |
| 37 | #include <sys/cdefs.h> |
| 38 | #include <sys/param.h> |
Christopher Ferris | 9bf7817 | 2020-05-20 15:37:30 -0700 | [diff] [blame^] | 39 | #include <sys/syscall.h> |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 40 | #include <unistd.h> |
| 41 | |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 42 | #include <mutex> |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 43 | #include <vector> |
| 44 | |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 45 | #include <android-base/file.h> |
Christopher Ferris | 2e1a40a | 2018-06-13 10:46:34 -0700 | [diff] [blame] | 46 | #include <android-base/properties.h> |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 47 | #include <android-base/stringprintf.h> |
Mitch Phillips | 3b21ada | 2020-01-07 15:47:47 -0800 | [diff] [blame] | 48 | #include <bionic/malloc_tagged_pointers.h> |
Christopher Ferris | 9bf7817 | 2020-05-20 15:37:30 -0700 | [diff] [blame^] | 49 | #include <platform/bionic/reserved_signals.h> |
Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 50 | #include <private/MallocXmlElem.h> |
Christopher Ferris | 9bf7817 | 2020-05-20 15:37:30 -0700 | [diff] [blame^] | 51 | #include <private/bionic_malloc_dispatch.h> |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 52 | |
Christopher Ferris | 72df670 | 2016-02-11 15:51:31 -0800 | [diff] [blame] | 53 | #include "Config.h" |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 54 | #include "DebugData.h" |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 55 | #include "backtrace.h" |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 56 | #include "debug_disable.h" |
| 57 | #include "debug_log.h" |
| 58 | #include "malloc_debug.h" |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 59 | #include "UnwindBacktrace.h" |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 60 | |
| 61 | // ------------------------------------------------------------------------ |
| 62 | // Global Data |
| 63 | // ------------------------------------------------------------------------ |
| 64 | DebugData* g_debug; |
| 65 | |
Christopher Ferris | 8189e77 | 2019-04-09 16:37:23 -0700 | [diff] [blame] | 66 | bool* g_zygote_child; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 67 | |
| 68 | const MallocDispatch* g_dispatch; |
| 69 | // ------------------------------------------------------------------------ |
| 70 | |
| 71 | // ------------------------------------------------------------------------ |
| 72 | // Use C style prototypes for all exported functions. This makes it easy |
| 73 | // to do dlsym lookups during libc initialization when malloc debug |
| 74 | // is enabled. |
| 75 | // ------------------------------------------------------------------------ |
| 76 | __BEGIN_DECLS |
| 77 | |
Christopher Ferris | 8189e77 | 2019-04-09 16:37:23 -0700 | [diff] [blame] | 78 | bool debug_initialize(const MallocDispatch* malloc_dispatch, bool* malloc_zygote_child, |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 79 | const char* options); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 80 | void debug_finalize(); |
Christopher Ferris | 2e1a40a | 2018-06-13 10:46:34 -0700 | [diff] [blame] | 81 | void debug_dump_heap(const char* file_name); |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 82 | void debug_get_malloc_leak_info(uint8_t** info, size_t* overall_size, size_t* info_size, |
| 83 | size_t* total_memory, size_t* backtrace_size); |
Christopher Ferris | 2e1a40a | 2018-06-13 10:46:34 -0700 | [diff] [blame] | 84 | bool debug_write_malloc_leak_info(FILE* fp); |
Colin Cross | 2d4721c | 2016-02-02 11:57:54 -0800 | [diff] [blame] | 85 | 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] | 86 | void debug_free_malloc_leak_info(uint8_t* info); |
| 87 | size_t debug_malloc_usable_size(void* pointer); |
| 88 | void* debug_malloc(size_t size); |
| 89 | void debug_free(void* pointer); |
Christopher Ferris | cae21a9 | 2018-02-05 18:14:55 -0800 | [diff] [blame] | 90 | void* debug_aligned_alloc(size_t alignment, size_t size); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 91 | void* debug_memalign(size_t alignment, size_t bytes); |
| 92 | void* debug_realloc(void* pointer, size_t bytes); |
| 93 | void* debug_calloc(size_t nmemb, size_t bytes); |
| 94 | struct mallinfo debug_mallinfo(); |
Christopher Ferris | a1c0d2f | 2017-05-15 15:50:19 -0700 | [diff] [blame] | 95 | int debug_mallopt(int param, int value); |
Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 96 | int debug_malloc_info(int options, FILE* fp); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 97 | int debug_posix_memalign(void** memptr, size_t alignment, size_t size); |
Christopher Ferris | 6f517cd | 2019-11-08 11:28:38 -0800 | [diff] [blame] | 98 | int debug_malloc_iterate(uintptr_t base, size_t size, |
| 99 | void (*callback)(uintptr_t base, size_t size, void* arg), void* arg); |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 100 | void debug_malloc_disable(); |
| 101 | void debug_malloc_enable(); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 102 | |
| 103 | #if defined(HAVE_DEPRECATED_MALLOC_FUNCS) |
| 104 | void* debug_pvalloc(size_t bytes); |
| 105 | void* debug_valloc(size_t size); |
| 106 | #endif |
| 107 | |
| 108 | __END_DECLS |
| 109 | // ------------------------------------------------------------------------ |
| 110 | |
Christopher Ferris | d269fcc | 2019-05-06 19:03:59 -0700 | [diff] [blame] | 111 | class ScopedConcurrentLock { |
| 112 | public: |
| 113 | ScopedConcurrentLock() { |
| 114 | pthread_rwlock_rdlock(&lock_); |
| 115 | } |
| 116 | ~ScopedConcurrentLock() { |
| 117 | pthread_rwlock_unlock(&lock_); |
| 118 | } |
| 119 | |
| 120 | static void Init() { |
| 121 | pthread_rwlockattr_t attr; |
| 122 | // Set the attribute so that when a write lock is pending, read locks are no |
| 123 | // longer granted. |
| 124 | pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP); |
| 125 | pthread_rwlock_init(&lock_, &attr); |
| 126 | } |
| 127 | |
| 128 | static void BlockAllOperations() { |
| 129 | pthread_rwlock_wrlock(&lock_); |
| 130 | } |
| 131 | |
| 132 | private: |
| 133 | static pthread_rwlock_t lock_; |
| 134 | }; |
| 135 | pthread_rwlock_t ScopedConcurrentLock::lock_; |
| 136 | |
Christopher Ferris | 9bf7817 | 2020-05-20 15:37:30 -0700 | [diff] [blame^] | 137 | // Use this because the sigprocmask* functions filter out the reserved bionic |
| 138 | // signals including the signal this code blocks. |
| 139 | static inline int __rt_sigprocmask(int how, const sigset64_t* new_set, sigset64_t* old_set, |
| 140 | size_t sigset_size) { |
| 141 | return syscall(SYS_rt_sigprocmask, how, new_set, old_set, sigset_size); |
| 142 | } |
| 143 | |
| 144 | // Need to block the backtrace signal while in malloc debug routines |
| 145 | // otherwise there is a chance of a deadlock and timeout when unwinding. |
| 146 | // This can occur if a thread is paused while owning a malloc debug |
| 147 | // internal lock. |
| 148 | class ScopedBacktraceSignalBlocker { |
| 149 | public: |
| 150 | ScopedBacktraceSignalBlocker() { |
| 151 | sigemptyset64(&backtrace_set_); |
| 152 | sigaddset64(&backtrace_set_, BIONIC_SIGNAL_BACKTRACE); |
| 153 | sigset64_t old_set; |
| 154 | __rt_sigprocmask(SIG_BLOCK, &backtrace_set_, &old_set, sizeof(backtrace_set_)); |
| 155 | if (sigismember64(&old_set, BIONIC_SIGNAL_BACKTRACE)) { |
| 156 | unblock_ = false; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | ~ScopedBacktraceSignalBlocker() { |
| 161 | if (unblock_) { |
| 162 | __rt_sigprocmask(SIG_UNBLOCK, &backtrace_set_, nullptr, sizeof(backtrace_set_)); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | private: |
| 167 | bool unblock_ = true; |
| 168 | sigset64_t backtrace_set_; |
| 169 | }; |
| 170 | |
Colin Cross | 7a28a3c | 2016-02-07 22:51:15 -0800 | [diff] [blame] | 171 | static void InitAtfork() { |
| 172 | static pthread_once_t atfork_init = PTHREAD_ONCE_INIT; |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 173 | pthread_once(&atfork_init, []() { |
Colin Cross | 7a28a3c | 2016-02-07 22:51:15 -0800 | [diff] [blame] | 174 | pthread_atfork( |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 175 | []() { |
Colin Cross | 7a28a3c | 2016-02-07 22:51:15 -0800 | [diff] [blame] | 176 | if (g_debug != nullptr) { |
| 177 | g_debug->PrepareFork(); |
| 178 | } |
| 179 | }, |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 180 | []() { |
Colin Cross | 7a28a3c | 2016-02-07 22:51:15 -0800 | [diff] [blame] | 181 | if (g_debug != nullptr) { |
| 182 | g_debug->PostForkParent(); |
| 183 | } |
| 184 | }, |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 185 | []() { |
Colin Cross | 7a28a3c | 2016-02-07 22:51:15 -0800 | [diff] [blame] | 186 | if (g_debug != nullptr) { |
| 187 | g_debug->PostForkChild(); |
| 188 | } |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 189 | }); |
Colin Cross | 7a28a3c | 2016-02-07 22:51:15 -0800 | [diff] [blame] | 190 | }); |
| 191 | } |
Christopher Ferris | d091962 | 2016-03-15 22:39:39 -0700 | [diff] [blame] | 192 | |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 193 | void BacktraceAndLog() { |
| 194 | if (g_debug->config().options() & BACKTRACE_FULL) { |
| 195 | std::vector<uintptr_t> frames; |
| 196 | std::vector<unwindstack::LocalFrameData> frames_info; |
| 197 | if (!Unwind(&frames, &frames_info, 256)) { |
| 198 | error_log(" Backtrace failed to get any frames."); |
| 199 | } else { |
| 200 | UnwindLog(frames_info); |
| 201 | } |
| 202 | } else { |
| 203 | std::vector<uintptr_t> frames(256); |
| 204 | size_t num_frames = backtrace_get(frames.data(), frames.size()); |
| 205 | if (num_frames == 0) { |
| 206 | error_log(" Backtrace failed to get any frames."); |
| 207 | } else { |
| 208 | backtrace_log(frames.data(), num_frames); |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 213 | static void LogError(const void* pointer, const char* error_str) { |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 214 | error_log(LOG_DIVIDER); |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 215 | error_log("+++ ALLOCATION %p %s", pointer, error_str); |
| 216 | |
| 217 | // If we are tracking already freed pointers, check to see if this is |
| 218 | // one so we can print extra information. |
| 219 | if (g_debug->config().options() & FREE_TRACK) { |
| 220 | PointerData::LogFreeBacktrace(pointer); |
Christopher Ferris | 7993b80 | 2016-01-28 18:35:05 -0800 | [diff] [blame] | 221 | } |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 222 | |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 223 | error_log("Backtrace at time of failure:"); |
| 224 | BacktraceAndLog(); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 225 | error_log(LOG_DIVIDER); |
Iris Chang | 7f209a9 | 2019-01-16 11:17:15 +0800 | [diff] [blame] | 226 | if (g_debug->config().options() & ABORT_ON_ERROR) { |
| 227 | abort(); |
| 228 | } |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 229 | } |
| 230 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 231 | static bool VerifyPointer(const void* pointer, const char* function_name) { |
| 232 | if (g_debug->HeaderEnabled()) { |
| 233 | Header* header = g_debug->GetHeader(pointer); |
| 234 | if (header->tag != DEBUG_TAG) { |
| 235 | std::string error_str; |
| 236 | if (header->tag == DEBUG_FREE_TAG) { |
| 237 | error_str = std::string("USED AFTER FREE (") + function_name + ")"; |
| 238 | } else { |
| 239 | error_str = android::base::StringPrintf("HAS INVALID TAG %" PRIx32 " (%s)", header->tag, |
| 240 | function_name); |
| 241 | } |
| 242 | LogError(pointer, error_str.c_str()); |
| 243 | return false; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | if (g_debug->TrackPointers()) { |
| 248 | if (!PointerData::Exists(pointer)) { |
| 249 | std::string error_str(std::string("UNKNOWN POINTER (") + function_name + ")"); |
| 250 | LogError(pointer, error_str.c_str()); |
| 251 | return false; |
| 252 | } |
| 253 | } |
| 254 | return true; |
| 255 | } |
| 256 | |
| 257 | static size_t InternalMallocUsableSize(void* pointer) { |
| 258 | if (g_debug->HeaderEnabled()) { |
| 259 | return g_debug->GetHeader(pointer)->usable_size; |
| 260 | } else { |
| 261 | return g_dispatch->malloc_usable_size(pointer); |
| 262 | } |
| 263 | } |
| 264 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 265 | static void* InitHeader(Header* header, void* orig_pointer, size_t size) { |
| 266 | header->tag = DEBUG_TAG; |
| 267 | header->orig_pointer = orig_pointer; |
| 268 | header->size = size; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 269 | header->usable_size = g_dispatch->malloc_usable_size(orig_pointer); |
| 270 | if (header->usable_size == 0) { |
| 271 | g_dispatch->free(orig_pointer); |
| 272 | return nullptr; |
| 273 | } |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 274 | header->usable_size -= g_debug->pointer_offset() + reinterpret_cast<uintptr_t>(header) - |
| 275 | reinterpret_cast<uintptr_t>(orig_pointer); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 276 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 277 | if (g_debug->config().options() & FRONT_GUARD) { |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 278 | uint8_t* guard = g_debug->GetFrontGuard(header); |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 279 | 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] | 280 | } |
| 281 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 282 | if (g_debug->config().options() & REAR_GUARD) { |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 283 | uint8_t* guard = g_debug->GetRearGuard(header); |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 284 | 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] | 285 | // If the rear guard is enabled, set the usable size to the exact size |
| 286 | // of the allocation. |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 287 | header->usable_size = header->size; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | return g_debug->GetPointer(header); |
| 291 | } |
| 292 | |
Christopher Ferris | 705de3c | 2019-05-22 13:39:57 -0700 | [diff] [blame] | 293 | extern "C" void __asan_init() __attribute__((weak)); |
| 294 | |
Christopher Ferris | 8189e77 | 2019-04-09 16:37:23 -0700 | [diff] [blame] | 295 | bool debug_initialize(const MallocDispatch* malloc_dispatch, bool* zygote_child, |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 296 | const char* options) { |
Christopher Ferris | 8189e77 | 2019-04-09 16:37:23 -0700 | [diff] [blame] | 297 | if (zygote_child == nullptr || options == nullptr) { |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 298 | return false; |
| 299 | } |
Colin Cross | 7a28a3c | 2016-02-07 22:51:15 -0800 | [diff] [blame] | 300 | |
Christopher Ferris | 705de3c | 2019-05-22 13:39:57 -0700 | [diff] [blame] | 301 | if (__asan_init != 0) { |
| 302 | error_log("malloc debug cannot be enabled alongside ASAN"); |
| 303 | return false; |
| 304 | } |
| 305 | |
Colin Cross | 7a28a3c | 2016-02-07 22:51:15 -0800 | [diff] [blame] | 306 | InitAtfork(); |
| 307 | |
Christopher Ferris | 8189e77 | 2019-04-09 16:37:23 -0700 | [diff] [blame] | 308 | g_zygote_child = zygote_child; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 309 | |
| 310 | g_dispatch = malloc_dispatch; |
| 311 | |
| 312 | if (!DebugDisableInitialize()) { |
| 313 | return false; |
| 314 | } |
| 315 | |
| 316 | DebugData* debug = new DebugData(); |
Tamas Berghammer | ac81fe8 | 2016-08-26 15:54:59 +0100 | [diff] [blame] | 317 | if (!debug->Initialize(options)) { |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 318 | delete debug; |
| 319 | DebugDisableFinalize(); |
| 320 | return false; |
| 321 | } |
| 322 | g_debug = debug; |
| 323 | |
| 324 | // Always enable the backtrace code since we will use it in a number |
| 325 | // of different error cases. |
| 326 | backtrace_startup(); |
| 327 | |
Christopher Ferris | c328e44 | 2019-04-01 19:31:26 -0700 | [diff] [blame] | 328 | if (g_debug->config().options() & VERBOSE) { |
| 329 | info_log("%s: malloc debug enabled", getprogname()); |
| 330 | } |
| 331 | |
Christopher Ferris | d269fcc | 2019-05-06 19:03:59 -0700 | [diff] [blame] | 332 | ScopedConcurrentLock::Init(); |
| 333 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 334 | return true; |
| 335 | } |
| 336 | |
| 337 | void debug_finalize() { |
| 338 | if (g_debug == nullptr) { |
| 339 | return; |
| 340 | } |
| 341 | |
Christopher Ferris | d269fcc | 2019-05-06 19:03:59 -0700 | [diff] [blame] | 342 | // Make sure that there are no other threads doing debug allocations |
| 343 | // before we kill everything. |
| 344 | ScopedConcurrentLock::BlockAllOperations(); |
| 345 | |
Christopher Ferris | 97b4747 | 2018-07-10 14:45:24 -0700 | [diff] [blame] | 346 | // Turn off capturing allocations calls. |
| 347 | DebugDisableSet(true); |
| 348 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 349 | if (g_debug->config().options() & FREE_TRACK) { |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 350 | PointerData::VerifyAllFreed(); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 351 | } |
| 352 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 353 | if (g_debug->config().options() & LEAK_TRACK) { |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 354 | PointerData::LogLeaks(); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 355 | } |
| 356 | |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 357 | if ((g_debug->config().options() & BACKTRACE) && g_debug->config().backtrace_dump_on_exit()) { |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 358 | debug_dump_heap(android::base::StringPrintf("%s.%d.exit.txt", |
| 359 | g_debug->config().backtrace_dump_prefix().c_str(), |
Christopher Ferris | 97b4747 | 2018-07-10 14:45:24 -0700 | [diff] [blame] | 360 | getpid()).c_str()); |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Colin Cross | 2c75991 | 2016-02-05 16:17:39 -0800 | [diff] [blame] | 363 | backtrace_shutdown(); |
| 364 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 365 | delete g_debug; |
| 366 | g_debug = nullptr; |
| 367 | |
| 368 | DebugDisableFinalize(); |
| 369 | } |
| 370 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 371 | void debug_get_malloc_leak_info(uint8_t** info, size_t* overall_size, size_t* info_size, |
| 372 | size_t* total_memory, size_t* backtrace_size) { |
Christopher Ferris | d269fcc | 2019-05-06 19:03:59 -0700 | [diff] [blame] | 373 | ScopedConcurrentLock lock; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 374 | ScopedDisableDebugCalls disable; |
Christopher Ferris | 9bf7817 | 2020-05-20 15:37:30 -0700 | [diff] [blame^] | 375 | ScopedBacktraceSignalBlocker blocked; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 376 | |
| 377 | // Verify the arguments. |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 378 | if (info == nullptr || overall_size == nullptr || info_size == nullptr || total_memory == nullptr || |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 379 | backtrace_size == nullptr) { |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 380 | error_log("get_malloc_leak_info: At least one invalid parameter."); |
| 381 | return; |
| 382 | } |
| 383 | |
| 384 | *info = nullptr; |
| 385 | *overall_size = 0; |
| 386 | *info_size = 0; |
| 387 | *total_memory = 0; |
| 388 | *backtrace_size = 0; |
| 389 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 390 | if (!(g_debug->config().options() & BACKTRACE)) { |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 391 | error_log( |
| 392 | "get_malloc_leak_info: Allocations not being tracked, to enable " |
| 393 | "set the option 'backtrace'."); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 394 | return; |
| 395 | } |
| 396 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 397 | PointerData::GetInfo(info, overall_size, info_size, total_memory, backtrace_size); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | void debug_free_malloc_leak_info(uint8_t* info) { |
| 401 | g_dispatch->free(info); |
| 402 | } |
| 403 | |
Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 404 | size_t debug_malloc_usable_size(void* pointer) { |
| 405 | if (DebugCallsDisabled() || pointer == nullptr) { |
| 406 | return g_dispatch->malloc_usable_size(pointer); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 407 | } |
Christopher Ferris | d269fcc | 2019-05-06 19:03:59 -0700 | [diff] [blame] | 408 | ScopedConcurrentLock lock; |
Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 409 | ScopedDisableDebugCalls disable; |
Christopher Ferris | 9bf7817 | 2020-05-20 15:37:30 -0700 | [diff] [blame^] | 410 | ScopedBacktraceSignalBlocker blocked; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 411 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 412 | if (!VerifyPointer(pointer, "malloc_usable_size")) { |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | return InternalMallocUsableSize(pointer); |
Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 419 | static void* InternalMalloc(size_t size) { |
| 420 | if ((g_debug->config().options() & BACKTRACE) && g_debug->pointer->ShouldDumpAndReset()) { |
| 421 | debug_dump_heap(android::base::StringPrintf( |
| 422 | "%s.%d.txt", g_debug->config().backtrace_dump_prefix().c_str(), getpid()) |
| 423 | .c_str()); |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 424 | } |
| 425 | |
Colin Cross | 9567c7b | 2016-03-09 17:56:14 -0800 | [diff] [blame] | 426 | if (size == 0) { |
| 427 | size = 1; |
| 428 | } |
| 429 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 430 | size_t real_size = size + g_debug->extra_bytes(); |
| 431 | if (real_size < size) { |
| 432 | // Overflow. |
| 433 | errno = ENOMEM; |
| 434 | return nullptr; |
| 435 | } |
| 436 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 437 | if (size > PointerInfoType::MaxSize()) { |
| 438 | errno = ENOMEM; |
| 439 | return nullptr; |
| 440 | } |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 441 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 442 | void* pointer; |
| 443 | if (g_debug->HeaderEnabled()) { |
| 444 | Header* header = |
| 445 | reinterpret_cast<Header*>(g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size)); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 446 | if (header == nullptr) { |
| 447 | return nullptr; |
| 448 | } |
| 449 | pointer = InitHeader(header, header, size); |
| 450 | } else { |
| 451 | pointer = g_dispatch->malloc(real_size); |
| 452 | } |
| 453 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 454 | if (pointer != nullptr) { |
| 455 | if (g_debug->TrackPointers()) { |
| 456 | PointerData::Add(pointer, size); |
| 457 | } |
| 458 | |
| 459 | if (g_debug->config().options() & FILL_ON_ALLOC) { |
| 460 | size_t bytes = InternalMallocUsableSize(pointer); |
| 461 | size_t fill_bytes = g_debug->config().fill_on_alloc_bytes(); |
| 462 | bytes = (bytes < fill_bytes) ? bytes : fill_bytes; |
| 463 | memset(pointer, g_debug->config().fill_alloc_value(), bytes); |
| 464 | } |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 465 | } |
| 466 | return pointer; |
| 467 | } |
| 468 | |
Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 469 | void* debug_malloc(size_t size) { |
| 470 | if (DebugCallsDisabled()) { |
| 471 | return g_dispatch->malloc(size); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 472 | } |
Christopher Ferris | d269fcc | 2019-05-06 19:03:59 -0700 | [diff] [blame] | 473 | ScopedConcurrentLock lock; |
Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 474 | ScopedDisableDebugCalls disable; |
Christopher Ferris | 9bf7817 | 2020-05-20 15:37:30 -0700 | [diff] [blame^] | 475 | ScopedBacktraceSignalBlocker blocked; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 476 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 477 | void* pointer = InternalMalloc(size); |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 478 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 479 | if (g_debug->config().options() & RECORD_ALLOCS) { |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 480 | g_debug->record->AddEntry(new MallocEntry(pointer, size)); |
| 481 | } |
| 482 | |
| 483 | return pointer; |
Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 486 | static void InternalFree(void* pointer) { |
| 487 | if ((g_debug->config().options() & BACKTRACE) && g_debug->pointer->ShouldDumpAndReset()) { |
| 488 | debug_dump_heap(android::base::StringPrintf( |
| 489 | "%s.%d.txt", g_debug->config().backtrace_dump_prefix().c_str(), getpid()) |
| 490 | .c_str()); |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 491 | } |
| 492 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 493 | void* free_pointer = pointer; |
| 494 | size_t bytes; |
Christopher Ferris | d091962 | 2016-03-15 22:39:39 -0700 | [diff] [blame] | 495 | Header* header; |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 496 | if (g_debug->HeaderEnabled()) { |
Christopher Ferris | d091962 | 2016-03-15 22:39:39 -0700 | [diff] [blame] | 497 | header = g_debug->GetHeader(pointer); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 498 | free_pointer = header->orig_pointer; |
| 499 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 500 | if (g_debug->config().options() & FRONT_GUARD) { |
Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 501 | if (!g_debug->front_guard->Valid(header)) { |
| 502 | g_debug->front_guard->LogFailure(header); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 503 | } |
| 504 | } |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 505 | if (g_debug->config().options() & REAR_GUARD) { |
Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 506 | if (!g_debug->rear_guard->Valid(header)) { |
| 507 | g_debug->rear_guard->LogFailure(header); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | |
Christopher Ferris | 7993b80 | 2016-01-28 18:35:05 -0800 | [diff] [blame] | 511 | header->tag = DEBUG_FREE_TAG; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 512 | |
| 513 | bytes = header->usable_size; |
| 514 | } else { |
| 515 | bytes = g_dispatch->malloc_usable_size(pointer); |
| 516 | } |
| 517 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 518 | if (g_debug->config().options() & FILL_ON_FREE) { |
| 519 | size_t fill_bytes = g_debug->config().fill_on_free_bytes(); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 520 | bytes = (bytes < fill_bytes) ? bytes : fill_bytes; |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 521 | memset(pointer, g_debug->config().fill_free_value(), bytes); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 522 | } |
| 523 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 524 | if (g_debug->TrackPointers()) { |
| 525 | PointerData::Remove(pointer); |
| 526 | } |
| 527 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 528 | if (g_debug->config().options() & FREE_TRACK) { |
Christopher Ferris | d091962 | 2016-03-15 22:39:39 -0700 | [diff] [blame] | 529 | // Do not add the allocation until we are done modifying the pointer |
| 530 | // itself. This avoids a race if a lot of threads are all doing |
| 531 | // frees at the same time and we wind up trying to really free this |
| 532 | // pointer from another thread, while still trying to free it in |
| 533 | // this function. |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 534 | pointer = PointerData::AddFreed(pointer); |
| 535 | if (pointer != nullptr) { |
| 536 | if (g_debug->HeaderEnabled()) { |
| 537 | pointer = g_debug->GetHeader(pointer)->orig_pointer; |
| 538 | } |
| 539 | g_dispatch->free(pointer); |
| 540 | } |
Christopher Ferris | d091962 | 2016-03-15 22:39:39 -0700 | [diff] [blame] | 541 | } else { |
| 542 | g_dispatch->free(free_pointer); |
| 543 | } |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 544 | } |
| 545 | |
Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 546 | void debug_free(void* pointer) { |
| 547 | if (DebugCallsDisabled() || pointer == nullptr) { |
| 548 | return g_dispatch->free(pointer); |
| 549 | } |
Christopher Ferris | d269fcc | 2019-05-06 19:03:59 -0700 | [diff] [blame] | 550 | ScopedConcurrentLock lock; |
Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 551 | ScopedDisableDebugCalls disable; |
Christopher Ferris | 9bf7817 | 2020-05-20 15:37:30 -0700 | [diff] [blame^] | 552 | ScopedBacktraceSignalBlocker blocked; |
Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 553 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 554 | if (g_debug->config().options() & RECORD_ALLOCS) { |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 555 | g_debug->record->AddEntry(new FreeEntry(pointer)); |
| 556 | } |
| 557 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 558 | if (!VerifyPointer(pointer, "free")) { |
| 559 | return; |
| 560 | } |
| 561 | |
| 562 | InternalFree(pointer); |
Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 563 | } |
| 564 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 565 | void* debug_memalign(size_t alignment, size_t bytes) { |
| 566 | if (DebugCallsDisabled()) { |
| 567 | return g_dispatch->memalign(alignment, bytes); |
| 568 | } |
Christopher Ferris | d269fcc | 2019-05-06 19:03:59 -0700 | [diff] [blame] | 569 | ScopedConcurrentLock lock; |
Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 570 | ScopedDisableDebugCalls disable; |
Christopher Ferris | 9bf7817 | 2020-05-20 15:37:30 -0700 | [diff] [blame^] | 571 | ScopedBacktraceSignalBlocker blocked; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 572 | |
Colin Cross | 9567c7b | 2016-03-09 17:56:14 -0800 | [diff] [blame] | 573 | if (bytes == 0) { |
| 574 | bytes = 1; |
| 575 | } |
| 576 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 577 | if (bytes > PointerInfoType::MaxSize()) { |
| 578 | errno = ENOMEM; |
| 579 | return nullptr; |
| 580 | } |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 581 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 582 | void* pointer; |
| 583 | if (g_debug->HeaderEnabled()) { |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 584 | // Make the alignment a power of two. |
| 585 | if (!powerof2(alignment)) { |
| 586 | alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment); |
| 587 | } |
Christopher Ferris | 72df670 | 2016-02-11 15:51:31 -0800 | [diff] [blame] | 588 | // Force the alignment to at least MINIMUM_ALIGNMENT_BYTES to guarantee |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 589 | // that the header is aligned properly. |
Christopher Ferris | 72df670 | 2016-02-11 15:51:31 -0800 | [diff] [blame] | 590 | if (alignment < MINIMUM_ALIGNMENT_BYTES) { |
| 591 | alignment = MINIMUM_ALIGNMENT_BYTES; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | // We don't have any idea what the natural alignment of |
| 595 | // the underlying native allocator is, so we always need to |
| 596 | // over allocate. |
| 597 | size_t real_size = alignment + bytes + g_debug->extra_bytes(); |
| 598 | if (real_size < bytes) { |
| 599 | // Overflow. |
| 600 | errno = ENOMEM; |
| 601 | return nullptr; |
| 602 | } |
| 603 | |
| 604 | pointer = g_dispatch->malloc(real_size); |
| 605 | if (pointer == nullptr) { |
| 606 | return nullptr; |
| 607 | } |
| 608 | |
| 609 | uintptr_t value = reinterpret_cast<uintptr_t>(pointer) + g_debug->pointer_offset(); |
| 610 | // Now align the pointer. |
| 611 | value += (-value % alignment); |
| 612 | |
| 613 | Header* header = g_debug->GetHeader(reinterpret_cast<void*>(value)); |
| 614 | pointer = InitHeader(header, pointer, bytes); |
| 615 | } else { |
| 616 | size_t real_size = bytes + g_debug->extra_bytes(); |
| 617 | if (real_size < bytes) { |
| 618 | // Overflow. |
| 619 | errno = ENOMEM; |
| 620 | return nullptr; |
| 621 | } |
| 622 | pointer = g_dispatch->memalign(alignment, real_size); |
| 623 | } |
| 624 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 625 | if (pointer != nullptr) { |
| 626 | if (g_debug->TrackPointers()) { |
| 627 | PointerData::Add(pointer, bytes); |
| 628 | } |
Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 629 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 630 | if (g_debug->config().options() & FILL_ON_ALLOC) { |
| 631 | size_t bytes = InternalMallocUsableSize(pointer); |
| 632 | size_t fill_bytes = g_debug->config().fill_on_alloc_bytes(); |
| 633 | bytes = (bytes < fill_bytes) ? bytes : fill_bytes; |
| 634 | memset(pointer, g_debug->config().fill_alloc_value(), bytes); |
| 635 | } |
| 636 | |
| 637 | if (g_debug->config().options() & RECORD_ALLOCS) { |
| 638 | g_debug->record->AddEntry(new MemalignEntry(pointer, bytes, alignment)); |
| 639 | } |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 640 | } |
| 641 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 642 | return pointer; |
| 643 | } |
| 644 | |
| 645 | void* debug_realloc(void* pointer, size_t bytes) { |
| 646 | if (DebugCallsDisabled()) { |
| 647 | return g_dispatch->realloc(pointer, bytes); |
| 648 | } |
Christopher Ferris | d269fcc | 2019-05-06 19:03:59 -0700 | [diff] [blame] | 649 | ScopedConcurrentLock lock; |
Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 650 | ScopedDisableDebugCalls disable; |
Christopher Ferris | 9bf7817 | 2020-05-20 15:37:30 -0700 | [diff] [blame^] | 651 | ScopedBacktraceSignalBlocker blocked; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 652 | |
| 653 | if (pointer == nullptr) { |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 654 | pointer = InternalMalloc(bytes); |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 655 | if (g_debug->config().options() & RECORD_ALLOCS) { |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 656 | g_debug->record->AddEntry(new ReallocEntry(pointer, bytes, nullptr)); |
| 657 | } |
| 658 | return pointer; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 659 | } |
| 660 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 661 | if (!VerifyPointer(pointer, "realloc")) { |
| 662 | return nullptr; |
| 663 | } |
| 664 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 665 | if (bytes == 0) { |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 666 | if (g_debug->config().options() & RECORD_ALLOCS) { |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 667 | g_debug->record->AddEntry(new ReallocEntry(nullptr, bytes, pointer)); |
| 668 | } |
| 669 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 670 | InternalFree(pointer); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 671 | return nullptr; |
| 672 | } |
| 673 | |
| 674 | size_t real_size = bytes; |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 675 | if (g_debug->config().options() & EXPAND_ALLOC) { |
| 676 | real_size += g_debug->config().expand_alloc_bytes(); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 677 | if (real_size < bytes) { |
| 678 | // Overflow. |
| 679 | errno = ENOMEM; |
| 680 | return nullptr; |
| 681 | } |
| 682 | } |
| 683 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 684 | if (bytes > PointerInfoType::MaxSize()) { |
| 685 | errno = ENOMEM; |
| 686 | return nullptr; |
| 687 | } |
| 688 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 689 | void* new_pointer; |
| 690 | size_t prev_size; |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 691 | if (g_debug->HeaderEnabled()) { |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 692 | // Same size, do nothing. |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 693 | Header* header = g_debug->GetHeader(pointer); |
| 694 | if (real_size == header->size) { |
| 695 | if (g_debug->TrackPointers()) { |
| 696 | // Remove and re-add so that the backtrace is updated. |
| 697 | PointerData::Remove(pointer); |
| 698 | PointerData::Add(pointer, real_size); |
| 699 | } |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 700 | return pointer; |
| 701 | } |
| 702 | |
| 703 | // Allocation is shrinking. |
| 704 | if (real_size < header->usable_size) { |
| 705 | header->size = real_size; |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 706 | if (g_debug->config().options() & REAR_GUARD) { |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 707 | // Don't bother allocating a smaller pointer in this case, simply |
| 708 | // change the header usable_size and reset the rear guard. |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 709 | header->usable_size = header->size; |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 710 | memset(g_debug->GetRearGuard(header), g_debug->config().rear_guard_value(), |
| 711 | g_debug->config().rear_guard_bytes()); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 712 | } |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 713 | if (g_debug->TrackPointers()) { |
| 714 | // Remove and re-add so that the backtrace is updated. |
| 715 | PointerData::Remove(pointer); |
| 716 | PointerData::Add(pointer, real_size); |
| 717 | } |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 718 | return pointer; |
| 719 | } |
| 720 | |
| 721 | // Allocate the new size. |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 722 | new_pointer = InternalMalloc(bytes); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 723 | if (new_pointer == nullptr) { |
| 724 | errno = ENOMEM; |
| 725 | return nullptr; |
| 726 | } |
| 727 | |
| 728 | prev_size = header->usable_size; |
| 729 | memcpy(new_pointer, pointer, prev_size); |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 730 | InternalFree(pointer); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 731 | } else { |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 732 | if (g_debug->TrackPointers()) { |
| 733 | PointerData::Remove(pointer); |
| 734 | } |
| 735 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 736 | prev_size = g_dispatch->malloc_usable_size(pointer); |
| 737 | new_pointer = g_dispatch->realloc(pointer, real_size); |
| 738 | if (new_pointer == nullptr) { |
| 739 | return nullptr; |
| 740 | } |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 741 | |
| 742 | if (g_debug->TrackPointers()) { |
| 743 | PointerData::Add(new_pointer, real_size); |
| 744 | } |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 745 | } |
| 746 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 747 | if (g_debug->config().options() & FILL_ON_ALLOC) { |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 748 | size_t bytes = InternalMallocUsableSize(new_pointer); |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 749 | if (bytes > g_debug->config().fill_on_alloc_bytes()) { |
| 750 | bytes = g_debug->config().fill_on_alloc_bytes(); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 751 | } |
| 752 | if (bytes > prev_size) { |
| 753 | memset(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(new_pointer) + prev_size), |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 754 | g_debug->config().fill_alloc_value(), bytes - prev_size); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 755 | } |
| 756 | } |
| 757 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 758 | if (g_debug->config().options() & RECORD_ALLOCS) { |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 759 | g_debug->record->AddEntry(new ReallocEntry(new_pointer, bytes, pointer)); |
| 760 | } |
| 761 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 762 | return new_pointer; |
| 763 | } |
| 764 | |
| 765 | void* debug_calloc(size_t nmemb, size_t bytes) { |
| 766 | if (DebugCallsDisabled()) { |
| 767 | return g_dispatch->calloc(nmemb, bytes); |
| 768 | } |
Christopher Ferris | d269fcc | 2019-05-06 19:03:59 -0700 | [diff] [blame] | 769 | ScopedConcurrentLock lock; |
Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 770 | ScopedDisableDebugCalls disable; |
Christopher Ferris | 9bf7817 | 2020-05-20 15:37:30 -0700 | [diff] [blame^] | 771 | ScopedBacktraceSignalBlocker blocked; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 772 | |
Colin Cross | 7877df6 | 2016-03-10 13:01:27 -0800 | [diff] [blame] | 773 | size_t size; |
| 774 | if (__builtin_mul_overflow(nmemb, bytes, &size)) { |
| 775 | // Overflow |
| 776 | errno = ENOMEM; |
| 777 | return nullptr; |
| 778 | } |
| 779 | |
Colin Cross | 9567c7b | 2016-03-09 17:56:14 -0800 | [diff] [blame] | 780 | if (size == 0) { |
| 781 | size = 1; |
| 782 | } |
| 783 | |
Colin Cross | 7877df6 | 2016-03-10 13:01:27 -0800 | [diff] [blame] | 784 | size_t real_size; |
| 785 | if (__builtin_add_overflow(size, g_debug->extra_bytes(), &real_size)) { |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 786 | // Overflow. |
| 787 | errno = ENOMEM; |
| 788 | return nullptr; |
| 789 | } |
| 790 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 791 | if (real_size > PointerInfoType::MaxSize()) { |
| 792 | errno = ENOMEM; |
| 793 | return nullptr; |
| 794 | } |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 795 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 796 | void* pointer; |
| 797 | if (g_debug->HeaderEnabled()) { |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 798 | // Need to guarantee the alignment of the header. |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 799 | Header* header = |
| 800 | reinterpret_cast<Header*>(g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size)); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 801 | if (header == nullptr) { |
| 802 | return nullptr; |
| 803 | } |
| 804 | memset(header, 0, g_dispatch->malloc_usable_size(header)); |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 805 | pointer = InitHeader(header, header, size); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 806 | } else { |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 807 | pointer = g_dispatch->calloc(1, real_size); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 808 | } |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 809 | |
Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 810 | if (g_debug->config().options() & RECORD_ALLOCS) { |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 811 | g_debug->record->AddEntry(new CallocEntry(pointer, bytes, nmemb)); |
| 812 | } |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 813 | |
| 814 | if (pointer != nullptr && g_debug->TrackPointers()) { |
| 815 | PointerData::Add(pointer, size); |
| 816 | } |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 817 | return pointer; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | struct mallinfo debug_mallinfo() { |
| 821 | return g_dispatch->mallinfo(); |
| 822 | } |
| 823 | |
Christopher Ferris | a1c0d2f | 2017-05-15 15:50:19 -0700 | [diff] [blame] | 824 | int debug_mallopt(int param, int value) { |
| 825 | return g_dispatch->mallopt(param, value); |
| 826 | } |
| 827 | |
Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 828 | int debug_malloc_info(int options, FILE* fp) { |
| 829 | if (DebugCallsDisabled() || !g_debug->TrackPointers()) { |
| 830 | return g_dispatch->malloc_info(options, fp); |
| 831 | } |
Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 832 | |
| 833 | // Make sure any pending output is written to the file. |
| 834 | fflush(fp); |
| 835 | |
Christopher Ferris | d269fcc | 2019-05-06 19:03:59 -0700 | [diff] [blame] | 836 | ScopedConcurrentLock lock; |
| 837 | ScopedDisableDebugCalls disable; |
Christopher Ferris | 9bf7817 | 2020-05-20 15:37:30 -0700 | [diff] [blame^] | 838 | ScopedBacktraceSignalBlocker blocked; |
Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 839 | |
Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 840 | // Avoid any issues where allocations are made that will be freed |
| 841 | // in the fclose. |
| 842 | int fd = fileno(fp); |
| 843 | MallocXmlElem root(fd, "malloc", "version=\"debug-malloc-1\""); |
Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 844 | std::vector<ListInfoType> list; |
| 845 | PointerData::GetAllocList(&list); |
| 846 | |
| 847 | size_t alloc_num = 0; |
| 848 | for (size_t i = 0; i < list.size(); i++) { |
Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 849 | MallocXmlElem alloc(fd, "allocation", "nr=\"%zu\"", alloc_num); |
Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 850 | |
| 851 | size_t total = 1; |
| 852 | size_t size = list[i].size; |
| 853 | while (i < list.size() - 1 && list[i + 1].size == size) { |
| 854 | i++; |
| 855 | total++; |
| 856 | } |
Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 857 | MallocXmlElem(fd, "size").Contents("%zu", list[i].size); |
| 858 | MallocXmlElem(fd, "total").Contents("%zu", total); |
Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 859 | alloc_num++; |
| 860 | } |
| 861 | return 0; |
| 862 | } |
| 863 | |
Christopher Ferris | cae21a9 | 2018-02-05 18:14:55 -0800 | [diff] [blame] | 864 | void* debug_aligned_alloc(size_t alignment, size_t size) { |
| 865 | if (DebugCallsDisabled()) { |
| 866 | return g_dispatch->aligned_alloc(alignment, size); |
| 867 | } |
Christopher Ferris | a22f5d5 | 2019-03-01 16:40:59 -0800 | [diff] [blame] | 868 | if (!powerof2(alignment) || (size % alignment) != 0) { |
Christopher Ferris | cae21a9 | 2018-02-05 18:14:55 -0800 | [diff] [blame] | 869 | errno = EINVAL; |
| 870 | return nullptr; |
| 871 | } |
| 872 | return debug_memalign(alignment, size); |
| 873 | } |
| 874 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 875 | int debug_posix_memalign(void** memptr, size_t alignment, size_t size) { |
| 876 | if (DebugCallsDisabled()) { |
| 877 | return g_dispatch->posix_memalign(memptr, alignment, size); |
| 878 | } |
| 879 | |
Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 880 | if (alignment < sizeof(void*) || !powerof2(alignment)) { |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 881 | return EINVAL; |
| 882 | } |
| 883 | int saved_errno = errno; |
| 884 | *memptr = debug_memalign(alignment, size); |
| 885 | errno = saved_errno; |
| 886 | return (*memptr != nullptr) ? 0 : ENOMEM; |
| 887 | } |
| 888 | |
Christopher Ferris | 6f517cd | 2019-11-08 11:28:38 -0800 | [diff] [blame] | 889 | int debug_malloc_iterate(uintptr_t base, size_t size, void (*callback)(uintptr_t, size_t, void*), |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 890 | void* arg) { |
Christopher Ferris | d269fcc | 2019-05-06 19:03:59 -0700 | [diff] [blame] | 891 | ScopedConcurrentLock lock; |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 892 | if (g_debug->TrackPointers()) { |
| 893 | // Since malloc is disabled, don't bother acquiring any locks. |
| 894 | for (auto it = PointerData::begin(); it != PointerData::end(); ++it) { |
| 895 | callback(it->first, InternalMallocUsableSize(reinterpret_cast<void*>(it->first)), arg); |
| 896 | } |
| 897 | return 0; |
| 898 | } |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 899 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 900 | // An option that adds a header will add pointer tracking, so no need to |
| 901 | // check if headers are enabled. |
Christopher Ferris | 6f517cd | 2019-11-08 11:28:38 -0800 | [diff] [blame] | 902 | return g_dispatch->malloc_iterate(base, size, callback, arg); |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | void debug_malloc_disable() { |
Christopher Ferris | d269fcc | 2019-05-06 19:03:59 -0700 | [diff] [blame] | 906 | ScopedConcurrentLock lock; |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 907 | g_dispatch->malloc_disable(); |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 908 | if (g_debug->pointer) { |
| 909 | g_debug->pointer->PrepareFork(); |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 910 | } |
| 911 | } |
| 912 | |
| 913 | void debug_malloc_enable() { |
Christopher Ferris | d269fcc | 2019-05-06 19:03:59 -0700 | [diff] [blame] | 914 | ScopedConcurrentLock lock; |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 915 | if (g_debug->pointer) { |
| 916 | g_debug->pointer->PostForkParent(); |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 917 | } |
| 918 | g_dispatch->malloc_enable(); |
| 919 | } |
| 920 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 921 | ssize_t debug_malloc_backtrace(void* pointer, uintptr_t* frames, size_t max_frames) { |
Colin Cross | 2d4721c | 2016-02-02 11:57:54 -0800 | [diff] [blame] | 922 | if (DebugCallsDisabled() || pointer == nullptr) { |
| 923 | return 0; |
| 924 | } |
Christopher Ferris | d269fcc | 2019-05-06 19:03:59 -0700 | [diff] [blame] | 925 | ScopedConcurrentLock lock; |
Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 926 | ScopedDisableDebugCalls disable; |
Christopher Ferris | 9bf7817 | 2020-05-20 15:37:30 -0700 | [diff] [blame^] | 927 | ScopedBacktraceSignalBlocker blocked; |
Colin Cross | 2d4721c | 2016-02-02 11:57:54 -0800 | [diff] [blame] | 928 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 929 | if (!(g_debug->config().options() & BACKTRACE)) { |
| 930 | return 0; |
Colin Cross | 2d4721c | 2016-02-02 11:57:54 -0800 | [diff] [blame] | 931 | } |
Mitch Phillips | 3b21ada | 2020-01-07 15:47:47 -0800 | [diff] [blame] | 932 | pointer = UntagPointer(pointer); |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 933 | return PointerData::GetFrames(pointer, frames, max_frames); |
Colin Cross | 2d4721c | 2016-02-02 11:57:54 -0800 | [diff] [blame] | 934 | } |
| 935 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 936 | #if defined(HAVE_DEPRECATED_MALLOC_FUNCS) |
| 937 | void* debug_pvalloc(size_t bytes) { |
| 938 | if (DebugCallsDisabled()) { |
| 939 | return g_dispatch->pvalloc(bytes); |
| 940 | } |
| 941 | |
| 942 | size_t pagesize = getpagesize(); |
Dan Albert | a613d0d | 2017-10-05 16:39:33 -0700 | [diff] [blame] | 943 | size_t size = __BIONIC_ALIGN(bytes, pagesize); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 944 | if (size < bytes) { |
| 945 | // Overflow |
| 946 | errno = ENOMEM; |
| 947 | return nullptr; |
| 948 | } |
| 949 | return debug_memalign(pagesize, size); |
| 950 | } |
| 951 | |
| 952 | void* debug_valloc(size_t size) { |
| 953 | if (DebugCallsDisabled()) { |
| 954 | return g_dispatch->valloc(size); |
| 955 | } |
| 956 | return debug_memalign(getpagesize(), size); |
| 957 | } |
| 958 | #endif |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 959 | |
| 960 | static std::mutex g_dump_lock; |
| 961 | |
Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 962 | static void write_dump(int fd) { |
| 963 | dprintf(fd, "Android Native Heap Dump v1.2\n\n"); |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 964 | |
Christopher Ferris | 2e1a40a | 2018-06-13 10:46:34 -0700 | [diff] [blame] | 965 | std::string fingerprint = android::base::GetProperty("ro.build.fingerprint", "unknown"); |
Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 966 | dprintf(fd, "Build fingerprint: '%s'\n\n", fingerprint.c_str()); |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 967 | |
Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 968 | PointerData::DumpLiveToFile(fd); |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 969 | |
Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 970 | dprintf(fd, "MAPS\n"); |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 971 | std::string content; |
| 972 | if (!android::base::ReadFileToString("/proc/self/maps", &content)) { |
Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 973 | dprintf(fd, "Could not open /proc/self/maps\n"); |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 974 | } else { |
Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 975 | dprintf(fd, "%s", content.c_str()); |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 976 | } |
Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 977 | dprintf(fd, "END\n"); |
Christopher Ferris | 2e1a40a | 2018-06-13 10:46:34 -0700 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | bool debug_write_malloc_leak_info(FILE* fp) { |
Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 981 | // Make sure any pending output is written to the file. |
| 982 | fflush(fp); |
| 983 | |
Christopher Ferris | d269fcc | 2019-05-06 19:03:59 -0700 | [diff] [blame] | 984 | ScopedConcurrentLock lock; |
Christopher Ferris | 2e1a40a | 2018-06-13 10:46:34 -0700 | [diff] [blame] | 985 | ScopedDisableDebugCalls disable; |
Christopher Ferris | 9bf7817 | 2020-05-20 15:37:30 -0700 | [diff] [blame^] | 986 | ScopedBacktraceSignalBlocker blocked; |
Christopher Ferris | 2e1a40a | 2018-06-13 10:46:34 -0700 | [diff] [blame] | 987 | |
| 988 | std::lock_guard<std::mutex> guard(g_dump_lock); |
| 989 | |
| 990 | if (!(g_debug->config().options() & BACKTRACE)) { |
| 991 | return false; |
| 992 | } |
| 993 | |
Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 994 | write_dump(fileno(fp)); |
| 995 | |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 996 | return true; |
| 997 | } |
Christopher Ferris | 2e1a40a | 2018-06-13 10:46:34 -0700 | [diff] [blame] | 998 | |
| 999 | void debug_dump_heap(const char* file_name) { |
Christopher Ferris | d269fcc | 2019-05-06 19:03:59 -0700 | [diff] [blame] | 1000 | ScopedConcurrentLock lock; |
Christopher Ferris | 2e1a40a | 2018-06-13 10:46:34 -0700 | [diff] [blame] | 1001 | ScopedDisableDebugCalls disable; |
Christopher Ferris | 9bf7817 | 2020-05-20 15:37:30 -0700 | [diff] [blame^] | 1002 | ScopedBacktraceSignalBlocker blocked; |
Christopher Ferris | 2e1a40a | 2018-06-13 10:46:34 -0700 | [diff] [blame] | 1003 | |
| 1004 | std::lock_guard<std::mutex> guard(g_dump_lock); |
| 1005 | |
Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 1006 | int fd = open(file_name, O_RDWR | O_CREAT | O_NOFOLLOW | O_TRUNC | O_CLOEXEC, 0644); |
| 1007 | if (fd == -1) { |
Christopher Ferris | 2e1a40a | 2018-06-13 10:46:34 -0700 | [diff] [blame] | 1008 | error_log("Unable to create file: %s", file_name); |
| 1009 | return; |
| 1010 | } |
| 1011 | |
| 1012 | error_log("Dumping to file: %s\n", file_name); |
Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 1013 | write_dump(fd); |
| 1014 | close(fd); |
Christopher Ferris | 2e1a40a | 2018-06-13 10:46:34 -0700 | [diff] [blame] | 1015 | } |