| 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 | |
| Elliott Hughes | 731da1b | 2025-07-23 10:52:52 -0700 | [diff] [blame] | 42 | #include <bit> |
| Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 43 | #include <mutex> |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 44 | #include <vector> |
| 45 | |
| Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 46 | #include <android-base/file.h> |
| Christopher Ferris | 2e1a40a | 2018-06-13 10:46:34 -0700 | [diff] [blame] | 47 | #include <android-base/properties.h> |
| Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 48 | #include <android-base/stringprintf.h> |
| Mitch Phillips | 3b21ada | 2020-01-07 15:47:47 -0800 | [diff] [blame] | 49 | #include <bionic/malloc_tagged_pointers.h> |
| Christopher Ferris | 9bf7817 | 2020-05-20 15:37:30 -0700 | [diff] [blame] | 50 | #include <platform/bionic/reserved_signals.h> |
| Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 51 | #include <private/MallocXmlElem.h> |
| Christopher Ferris | 9bf7817 | 2020-05-20 15:37:30 -0700 | [diff] [blame] | 52 | #include <private/bionic_malloc_dispatch.h> |
| Christopher Ferris | 459eecb | 2022-01-07 13:38:10 -0800 | [diff] [blame] | 53 | #include <unwindstack/Unwinder.h> |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 54 | |
| Christopher Ferris | 72df670 | 2016-02-11 15:51:31 -0800 | [diff] [blame] | 55 | #include "Config.h" |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 56 | #include "DebugData.h" |
| Christopher Ferris | 5610d5a | 2023-11-14 15:04:50 -0800 | [diff] [blame] | 57 | #include "LogAllocatorStats.h" |
| Christopher Ferris | e39602c | 2024-11-02 05:02:43 +0000 | [diff] [blame] | 58 | #include "Nanotime.h" |
| Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame] | 59 | #include "Unreachable.h" |
| 60 | #include "UnwindBacktrace.h" |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 61 | #include "backtrace.h" |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 62 | #include "debug_disable.h" |
| 63 | #include "debug_log.h" |
| 64 | #include "malloc_debug.h" |
| 65 | |
| 66 | // ------------------------------------------------------------------------ |
| 67 | // Global Data |
| 68 | // ------------------------------------------------------------------------ |
| 69 | DebugData* g_debug; |
| 70 | |
| Christopher Ferris | 8189e77 | 2019-04-09 16:37:23 -0700 | [diff] [blame] | 71 | bool* g_zygote_child; |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 72 | |
| 73 | const MallocDispatch* g_dispatch; |
| Chia-hung Duan | f7e8b17 | 2022-11-01 21:37:56 +0000 | [diff] [blame] | 74 | |
| Chia-hung Duan | f7e8b17 | 2022-11-01 21:37:56 +0000 | [diff] [blame] | 75 | namespace { |
| 76 | // A TimedResult contains the result of from malloc end_ns al. functions and the |
| 77 | // start/end timestamps. |
| 78 | struct TimedResult { |
| 79 | uint64_t start_ns = 0; |
| 80 | uint64_t end_ns = 0; |
| 81 | union { |
| 82 | size_t s; |
| 83 | int i; |
| 84 | void* p; |
| 85 | } v; |
| 86 | |
| 87 | uint64_t GetStartTimeNS() const { return start_ns; } |
| 88 | uint64_t GetEndTimeNS() const { return end_ns; } |
| 89 | void SetStartTimeNS(uint64_t t) { start_ns = t; } |
| 90 | void SetEndTimeNS(uint64_t t) { end_ns = t; } |
| 91 | |
| 92 | template <typename T> |
| 93 | void setValue(T); |
| 94 | template <> |
| 95 | void setValue(size_t s) { |
| 96 | v.s = s; |
| 97 | } |
| 98 | template <> |
| 99 | void setValue(int i) { |
| 100 | v.i = i; |
| 101 | } |
| 102 | template <> |
| 103 | void setValue(void* p) { |
| 104 | v.p = p; |
| 105 | } |
| 106 | |
| 107 | template <typename T> |
| 108 | T getValue() const; |
| 109 | template <> |
| 110 | size_t getValue<size_t>() const { |
| 111 | return v.s; |
| 112 | } |
| 113 | template <> |
| 114 | int getValue<int>() const { |
| 115 | return v.i; |
| 116 | } |
| 117 | template <> |
| 118 | void* getValue<void*>() const { |
| 119 | return v.p; |
| 120 | } |
| 121 | }; |
| 122 | |
| 123 | class ScopedTimer { |
| 124 | public: |
| 125 | ScopedTimer(TimedResult& res) : res_(res) { res_.start_ns = Nanotime(); } |
| 126 | |
| 127 | ~ScopedTimer() { res_.end_ns = Nanotime(); } |
| 128 | |
| 129 | private: |
| 130 | TimedResult& res_; |
| 131 | }; |
| 132 | |
| 133 | } // namespace |
| 134 | |
| 135 | template <typename MallocFn, typename... Args> |
| 136 | static TimedResult TimerCall(MallocFn fn, Args... args) { |
| 137 | TimedResult ret; |
| 138 | decltype((g_dispatch->*fn)(args...)) r; |
| 139 | if (g_debug->config().options() & RECORD_ALLOCS) { |
| 140 | ScopedTimer t(ret); |
| 141 | r = (g_dispatch->*fn)(args...); |
| 142 | } else { |
| 143 | r = (g_dispatch->*fn)(args...); |
| 144 | } |
| 145 | ret.setValue<decltype(r)>(r); |
| 146 | return ret; |
| 147 | } |
| 148 | |
| 149 | template <typename MallocFn, typename... Args> |
| 150 | static TimedResult TimerCallVoid(MallocFn fn, Args... args) { |
| 151 | TimedResult ret; |
| 152 | { |
| 153 | ScopedTimer t(ret); |
| 154 | (g_dispatch->*fn)(args...); |
| 155 | } |
| 156 | return ret; |
| 157 | } |
| 158 | |
| 159 | #define TCALL(FUNC, ...) TimerCall(&MallocDispatch::FUNC, __VA_ARGS__); |
| 160 | #define TCALLVOID(FUNC, ...) TimerCallVoid(&MallocDispatch::FUNC, __VA_ARGS__); |
| 161 | |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 162 | // ------------------------------------------------------------------------ |
| 163 | |
| 164 | // ------------------------------------------------------------------------ |
| 165 | // Use C style prototypes for all exported functions. This makes it easy |
| 166 | // to do dlsym lookups during libc initialization when malloc debug |
| 167 | // is enabled. |
| 168 | // ------------------------------------------------------------------------ |
| 169 | __BEGIN_DECLS |
| 170 | |
| Christopher Ferris | 8189e77 | 2019-04-09 16:37:23 -0700 | [diff] [blame] | 171 | bool debug_initialize(const MallocDispatch* malloc_dispatch, bool* malloc_zygote_child, |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 172 | const char* options); |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 173 | void debug_finalize(); |
| Christopher Ferris | 2e1a40a | 2018-06-13 10:46:34 -0700 | [diff] [blame] | 174 | void debug_dump_heap(const char* file_name); |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 175 | void debug_get_malloc_leak_info(uint8_t** info, size_t* overall_size, size_t* info_size, |
| 176 | size_t* total_memory, size_t* backtrace_size); |
| Christopher Ferris | 2e1a40a | 2018-06-13 10:46:34 -0700 | [diff] [blame] | 177 | bool debug_write_malloc_leak_info(FILE* fp); |
| Colin Cross | 2d4721c | 2016-02-02 11:57:54 -0800 | [diff] [blame] | 178 | 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] | 179 | void debug_free_malloc_leak_info(uint8_t* info); |
| 180 | size_t debug_malloc_usable_size(void* pointer); |
| 181 | void* debug_malloc(size_t size); |
| 182 | void debug_free(void* pointer); |
| Christopher Ferris | cae21a9 | 2018-02-05 18:14:55 -0800 | [diff] [blame] | 183 | void* debug_aligned_alloc(size_t alignment, size_t size); |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 184 | void* debug_memalign(size_t alignment, size_t bytes); |
| 185 | void* debug_realloc(void* pointer, size_t bytes); |
| 186 | void* debug_calloc(size_t nmemb, size_t bytes); |
| 187 | struct mallinfo debug_mallinfo(); |
| Christopher Ferris | a1c0d2f | 2017-05-15 15:50:19 -0700 | [diff] [blame] | 188 | int debug_mallopt(int param, int value); |
| Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 189 | int debug_malloc_info(int options, FILE* fp); |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 190 | int debug_posix_memalign(void** memptr, size_t alignment, size_t size); |
| Christopher Ferris | 6f517cd | 2019-11-08 11:28:38 -0800 | [diff] [blame] | 191 | int debug_malloc_iterate(uintptr_t base, size_t size, |
| 192 | void (*callback)(uintptr_t base, size_t size, void* arg), void* arg); |
| Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 193 | void debug_malloc_disable(); |
| 194 | void debug_malloc_enable(); |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 195 | |
| 196 | #if defined(HAVE_DEPRECATED_MALLOC_FUNCS) |
| 197 | void* debug_pvalloc(size_t bytes); |
| 198 | void* debug_valloc(size_t size); |
| 199 | #endif |
| 200 | |
| 201 | __END_DECLS |
| 202 | // ------------------------------------------------------------------------ |
| 203 | |
| Christopher Ferris | d269fcc | 2019-05-06 19:03:59 -0700 | [diff] [blame] | 204 | class ScopedConcurrentLock { |
| 205 | public: |
| 206 | ScopedConcurrentLock() { |
| 207 | pthread_rwlock_rdlock(&lock_); |
| 208 | } |
| 209 | ~ScopedConcurrentLock() { |
| 210 | pthread_rwlock_unlock(&lock_); |
| 211 | } |
| 212 | |
| 213 | static void Init() { |
| 214 | pthread_rwlockattr_t attr; |
| 215 | // Set the attribute so that when a write lock is pending, read locks are no |
| 216 | // longer granted. |
| 217 | pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP); |
| 218 | pthread_rwlock_init(&lock_, &attr); |
| 219 | } |
| 220 | |
| 221 | static void BlockAllOperations() { |
| 222 | pthread_rwlock_wrlock(&lock_); |
| 223 | } |
| 224 | |
| 225 | private: |
| 226 | static pthread_rwlock_t lock_; |
| 227 | }; |
| 228 | pthread_rwlock_t ScopedConcurrentLock::lock_; |
| 229 | |
| Christopher Ferris | 9bf7817 | 2020-05-20 15:37:30 -0700 | [diff] [blame] | 230 | // Use this because the sigprocmask* functions filter out the reserved bionic |
| 231 | // signals including the signal this code blocks. |
| 232 | static inline int __rt_sigprocmask(int how, const sigset64_t* new_set, sigset64_t* old_set, |
| 233 | size_t sigset_size) { |
| 234 | return syscall(SYS_rt_sigprocmask, how, new_set, old_set, sigset_size); |
| 235 | } |
| 236 | |
| 237 | // Need to block the backtrace signal while in malloc debug routines |
| 238 | // otherwise there is a chance of a deadlock and timeout when unwinding. |
| 239 | // This can occur if a thread is paused while owning a malloc debug |
| 240 | // internal lock. |
| 241 | class ScopedBacktraceSignalBlocker { |
| 242 | public: |
| 243 | ScopedBacktraceSignalBlocker() { |
| 244 | sigemptyset64(&backtrace_set_); |
| 245 | sigaddset64(&backtrace_set_, BIONIC_SIGNAL_BACKTRACE); |
| 246 | sigset64_t old_set; |
| 247 | __rt_sigprocmask(SIG_BLOCK, &backtrace_set_, &old_set, sizeof(backtrace_set_)); |
| 248 | if (sigismember64(&old_set, BIONIC_SIGNAL_BACKTRACE)) { |
| 249 | unblock_ = false; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | ~ScopedBacktraceSignalBlocker() { |
| 254 | if (unblock_) { |
| 255 | __rt_sigprocmask(SIG_UNBLOCK, &backtrace_set_, nullptr, sizeof(backtrace_set_)); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | private: |
| 260 | bool unblock_ = true; |
| 261 | sigset64_t backtrace_set_; |
| 262 | }; |
| 263 | |
| Colin Cross | 7a28a3c | 2016-02-07 22:51:15 -0800 | [diff] [blame] | 264 | static void InitAtfork() { |
| 265 | static pthread_once_t atfork_init = PTHREAD_ONCE_INIT; |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 266 | pthread_once(&atfork_init, []() { |
| Colin Cross | 7a28a3c | 2016-02-07 22:51:15 -0800 | [diff] [blame] | 267 | pthread_atfork( |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 268 | []() { |
| Colin Cross | 7a28a3c | 2016-02-07 22:51:15 -0800 | [diff] [blame] | 269 | if (g_debug != nullptr) { |
| 270 | g_debug->PrepareFork(); |
| 271 | } |
| 272 | }, |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 273 | []() { |
| Colin Cross | 7a28a3c | 2016-02-07 22:51:15 -0800 | [diff] [blame] | 274 | if (g_debug != nullptr) { |
| 275 | g_debug->PostForkParent(); |
| 276 | } |
| 277 | }, |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 278 | []() { |
| Colin Cross | 7a28a3c | 2016-02-07 22:51:15 -0800 | [diff] [blame] | 279 | if (g_debug != nullptr) { |
| 280 | g_debug->PostForkChild(); |
| 281 | } |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 282 | }); |
| Colin Cross | 7a28a3c | 2016-02-07 22:51:15 -0800 | [diff] [blame] | 283 | }); |
| 284 | } |
| Christopher Ferris | d091962 | 2016-03-15 22:39:39 -0700 | [diff] [blame] | 285 | |
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 286 | void BacktraceAndLog() { |
| 287 | if (g_debug->config().options() & BACKTRACE_FULL) { |
| 288 | std::vector<uintptr_t> frames; |
| Christopher Ferris | 459eecb | 2022-01-07 13:38:10 -0800 | [diff] [blame] | 289 | std::vector<unwindstack::FrameData> frames_info; |
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 290 | if (!Unwind(&frames, &frames_info, 256)) { |
| 291 | error_log(" Backtrace failed to get any frames."); |
| 292 | } else { |
| 293 | UnwindLog(frames_info); |
| 294 | } |
| 295 | } else { |
| 296 | std::vector<uintptr_t> frames(256); |
| 297 | size_t num_frames = backtrace_get(frames.data(), frames.size()); |
| 298 | if (num_frames == 0) { |
| 299 | error_log(" Backtrace failed to get any frames."); |
| 300 | } else { |
| 301 | backtrace_log(frames.data(), num_frames); |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 306 | static void LogError(const void* pointer, const char* error_str) { |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 307 | error_log(LOG_DIVIDER); |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 308 | error_log("+++ ALLOCATION %p %s", pointer, error_str); |
| 309 | |
| 310 | // If we are tracking already freed pointers, check to see if this is |
| 311 | // one so we can print extra information. |
| 312 | if (g_debug->config().options() & FREE_TRACK) { |
| 313 | PointerData::LogFreeBacktrace(pointer); |
| Christopher Ferris | 7993b80 | 2016-01-28 18:35:05 -0800 | [diff] [blame] | 314 | } |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 315 | |
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 316 | error_log("Backtrace at time of failure:"); |
| 317 | BacktraceAndLog(); |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 318 | error_log(LOG_DIVIDER); |
| Iris Chang | 7f209a9 | 2019-01-16 11:17:15 +0800 | [diff] [blame] | 319 | if (g_debug->config().options() & ABORT_ON_ERROR) { |
| 320 | abort(); |
| 321 | } |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 322 | } |
| 323 | |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 324 | static bool VerifyPointer(const void* pointer, const char* function_name) { |
| 325 | if (g_debug->HeaderEnabled()) { |
| 326 | Header* header = g_debug->GetHeader(pointer); |
| 327 | if (header->tag != DEBUG_TAG) { |
| 328 | std::string error_str; |
| 329 | if (header->tag == DEBUG_FREE_TAG) { |
| 330 | error_str = std::string("USED AFTER FREE (") + function_name + ")"; |
| 331 | } else { |
| 332 | error_str = android::base::StringPrintf("HAS INVALID TAG %" PRIx32 " (%s)", header->tag, |
| 333 | function_name); |
| 334 | } |
| 335 | LogError(pointer, error_str.c_str()); |
| 336 | return false; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | if (g_debug->TrackPointers()) { |
| 341 | if (!PointerData::Exists(pointer)) { |
| 342 | std::string error_str(std::string("UNKNOWN POINTER (") + function_name + ")"); |
| 343 | LogError(pointer, error_str.c_str()); |
| 344 | return false; |
| 345 | } |
| 346 | } |
| 347 | return true; |
| 348 | } |
| 349 | |
| 350 | static size_t InternalMallocUsableSize(void* pointer) { |
| 351 | if (g_debug->HeaderEnabled()) { |
| 352 | return g_debug->GetHeader(pointer)->usable_size; |
| 353 | } else { |
| 354 | return g_dispatch->malloc_usable_size(pointer); |
| 355 | } |
| 356 | } |
| 357 | |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 358 | static void* InitHeader(Header* header, void* orig_pointer, size_t size) { |
| 359 | header->tag = DEBUG_TAG; |
| 360 | header->orig_pointer = orig_pointer; |
| 361 | header->size = size; |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 362 | header->usable_size = g_dispatch->malloc_usable_size(orig_pointer); |
| 363 | if (header->usable_size == 0) { |
| 364 | g_dispatch->free(orig_pointer); |
| 365 | return nullptr; |
| 366 | } |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 367 | header->usable_size -= g_debug->pointer_offset() + reinterpret_cast<uintptr_t>(header) - |
| 368 | reinterpret_cast<uintptr_t>(orig_pointer); |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 369 | |
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 370 | if (g_debug->config().options() & FRONT_GUARD) { |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 371 | uint8_t* guard = g_debug->GetFrontGuard(header); |
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 372 | 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] | 373 | } |
| 374 | |
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 375 | if (g_debug->config().options() & REAR_GUARD) { |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 376 | uint8_t* guard = g_debug->GetRearGuard(header); |
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 377 | 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] | 378 | // If the rear guard is enabled, set the usable size to the exact size |
| 379 | // of the allocation. |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 380 | header->usable_size = header->size; |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | return g_debug->GetPointer(header); |
| 384 | } |
| 385 | |
| Christopher Ferris | 705de3c | 2019-05-22 13:39:57 -0700 | [diff] [blame] | 386 | extern "C" void __asan_init() __attribute__((weak)); |
| 387 | |
| Christopher Ferris | 8189e77 | 2019-04-09 16:37:23 -0700 | [diff] [blame] | 388 | bool debug_initialize(const MallocDispatch* malloc_dispatch, bool* zygote_child, |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 389 | const char* options) { |
| Christopher Ferris | 8189e77 | 2019-04-09 16:37:23 -0700 | [diff] [blame] | 390 | if (zygote_child == nullptr || options == nullptr) { |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 391 | return false; |
| 392 | } |
| Colin Cross | 7a28a3c | 2016-02-07 22:51:15 -0800 | [diff] [blame] | 393 | |
| Christopher Ferris | 705de3c | 2019-05-22 13:39:57 -0700 | [diff] [blame] | 394 | if (__asan_init != 0) { |
| 395 | error_log("malloc debug cannot be enabled alongside ASAN"); |
| 396 | return false; |
| 397 | } |
| 398 | |
| Colin Cross | 7a28a3c | 2016-02-07 22:51:15 -0800 | [diff] [blame] | 399 | InitAtfork(); |
| 400 | |
| Christopher Ferris | 8189e77 | 2019-04-09 16:37:23 -0700 | [diff] [blame] | 401 | g_zygote_child = zygote_child; |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 402 | |
| 403 | g_dispatch = malloc_dispatch; |
| 404 | |
| 405 | if (!DebugDisableInitialize()) { |
| 406 | return false; |
| 407 | } |
| 408 | |
| 409 | DebugData* debug = new DebugData(); |
| Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame] | 410 | if (!debug->Initialize(options) || !Unreachable::Initialize(debug->config())) { |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 411 | delete debug; |
| 412 | DebugDisableFinalize(); |
| 413 | return false; |
| 414 | } |
| 415 | g_debug = debug; |
| 416 | |
| 417 | // Always enable the backtrace code since we will use it in a number |
| 418 | // of different error cases. |
| 419 | backtrace_startup(); |
| 420 | |
| Christopher Ferris | c328e44 | 2019-04-01 19:31:26 -0700 | [diff] [blame] | 421 | if (g_debug->config().options() & VERBOSE) { |
| 422 | info_log("%s: malloc debug enabled", getprogname()); |
| 423 | } |
| 424 | |
| Christopher Ferris | d269fcc | 2019-05-06 19:03:59 -0700 | [diff] [blame] | 425 | ScopedConcurrentLock::Init(); |
| 426 | |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 427 | return true; |
| 428 | } |
| 429 | |
| 430 | void debug_finalize() { |
| 431 | if (g_debug == nullptr) { |
| 432 | return; |
| 433 | } |
| 434 | |
| Christopher Ferris | d269fcc | 2019-05-06 19:03:59 -0700 | [diff] [blame] | 435 | // Make sure that there are no other threads doing debug allocations |
| 436 | // before we kill everything. |
| 437 | ScopedConcurrentLock::BlockAllOperations(); |
| 438 | |
| Christopher Ferris | 97b4747 | 2018-07-10 14:45:24 -0700 | [diff] [blame] | 439 | // Turn off capturing allocations calls. |
| 440 | DebugDisableSet(true); |
| 441 | |
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 442 | if (g_debug->config().options() & FREE_TRACK) { |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 443 | PointerData::VerifyAllFreed(); |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 444 | } |
| 445 | |
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 446 | if (g_debug->config().options() & LEAK_TRACK) { |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 447 | PointerData::LogLeaks(); |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 448 | } |
| 449 | |
| Christopher Ferris | 9b88d0a | 2024-06-13 13:22:02 -0700 | [diff] [blame] | 450 | if ((g_debug->config().options() & RECORD_ALLOCS) && g_debug->config().record_allocs_on_exit()) { |
| 451 | RecordData::WriteEntriesOnExit(); |
| 452 | } |
| 453 | |
| Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 454 | 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] | 455 | debug_dump_heap(android::base::StringPrintf("%s.%d.exit.txt", |
| 456 | g_debug->config().backtrace_dump_prefix().c_str(), |
| Christopher Ferris | 97b4747 | 2018-07-10 14:45:24 -0700 | [diff] [blame] | 457 | getpid()).c_str()); |
| Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 458 | } |
| 459 | |
| Christopher Ferris | c1341e1 | 2024-07-11 19:48:08 -0700 | [diff] [blame] | 460 | if (g_debug->config().options() & LOG_ALLOCATOR_STATS_ON_EXIT) { |
| 461 | LogAllocatorStats::Log(); |
| 462 | } |
| 463 | |
| Colin Cross | 2c75991 | 2016-02-05 16:17:39 -0800 | [diff] [blame] | 464 | backtrace_shutdown(); |
| 465 | |
| Christopher Ferris | 33d7337 | 2021-07-02 15:46:18 -0700 | [diff] [blame] | 466 | // In order to prevent any issues of threads freeing previous pointers |
| 467 | // after the main thread calls this code, simply leak the g_debug pointer |
| 468 | // and do not destroy the debug disable pthread key. |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 469 | } |
| 470 | |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 471 | void debug_get_malloc_leak_info(uint8_t** info, size_t* overall_size, size_t* info_size, |
| 472 | size_t* total_memory, size_t* backtrace_size) { |
| Christopher Ferris | d269fcc | 2019-05-06 19:03:59 -0700 | [diff] [blame] | 473 | ScopedConcurrentLock lock; |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [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 | |
| 477 | // Verify the arguments. |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 478 | if (info == nullptr || overall_size == nullptr || info_size == nullptr || total_memory == nullptr || |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 479 | backtrace_size == nullptr) { |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 480 | error_log("get_malloc_leak_info: At least one invalid parameter."); |
| 481 | return; |
| 482 | } |
| 483 | |
| 484 | *info = nullptr; |
| 485 | *overall_size = 0; |
| 486 | *info_size = 0; |
| 487 | *total_memory = 0; |
| 488 | *backtrace_size = 0; |
| 489 | |
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 490 | if (!(g_debug->config().options() & BACKTRACE)) { |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 491 | error_log( |
| 492 | "get_malloc_leak_info: Allocations not being tracked, to enable " |
| 493 | "set the option 'backtrace'."); |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 494 | return; |
| 495 | } |
| 496 | |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 497 | PointerData::GetInfo(info, overall_size, info_size, total_memory, backtrace_size); |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | void debug_free_malloc_leak_info(uint8_t* info) { |
| 501 | g_dispatch->free(info); |
| Christopher Ferris | aa3e574 | 2023-01-31 01:31:52 +0000 | [diff] [blame] | 502 | // Purge the memory that was freed since a significant amount of |
| 503 | // memory could have been allocated and freed. |
| Christopher Ferris | f1ab9c4 | 2023-03-22 13:47:27 -0700 | [diff] [blame] | 504 | g_dispatch->mallopt(M_PURGE_ALL, 0); |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 505 | } |
| 506 | |
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 507 | size_t debug_malloc_usable_size(void* pointer) { |
| Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame] | 508 | Unreachable::CheckIfRequested(g_debug->config()); |
| 509 | |
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 510 | if (DebugCallsDisabled() || pointer == nullptr) { |
| 511 | return g_dispatch->malloc_usable_size(pointer); |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 512 | } |
| Christopher Ferris | d269fcc | 2019-05-06 19:03:59 -0700 | [diff] [blame] | 513 | ScopedConcurrentLock lock; |
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 514 | ScopedDisableDebugCalls disable; |
| Christopher Ferris | 9bf7817 | 2020-05-20 15:37:30 -0700 | [diff] [blame] | 515 | ScopedBacktraceSignalBlocker blocked; |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 516 | |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 517 | if (!VerifyPointer(pointer, "malloc_usable_size")) { |
| 518 | return 0; |
| 519 | } |
| 520 | |
| 521 | return InternalMallocUsableSize(pointer); |
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 522 | } |
| 523 | |
| Chia-hung Duan | f7e8b17 | 2022-11-01 21:37:56 +0000 | [diff] [blame] | 524 | static TimedResult InternalMalloc(size_t size) { |
| Christopher Ferris | 5610d5a | 2023-11-14 15:04:50 -0800 | [diff] [blame] | 525 | uint64_t options = g_debug->config().options(); |
| 526 | if ((options & BACKTRACE) && g_debug->pointer->ShouldDumpAndReset()) { |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 527 | debug_dump_heap(android::base::StringPrintf( |
| 528 | "%s.%d.txt", g_debug->config().backtrace_dump_prefix().c_str(), getpid()) |
| 529 | .c_str()); |
| Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 530 | } |
| Christopher Ferris | 5610d5a | 2023-11-14 15:04:50 -0800 | [diff] [blame] | 531 | if (options & LOG_ALLOCATOR_STATS_ON_SIGNAL) { |
| 532 | LogAllocatorStats::CheckIfShouldLog(); |
| 533 | } |
| Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 534 | |
| Colin Cross | 9567c7b | 2016-03-09 17:56:14 -0800 | [diff] [blame] | 535 | if (size == 0) { |
| 536 | size = 1; |
| 537 | } |
| 538 | |
| Chia-hung Duan | f7e8b17 | 2022-11-01 21:37:56 +0000 | [diff] [blame] | 539 | TimedResult result; |
| 540 | |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 541 | size_t real_size = size + g_debug->extra_bytes(); |
| 542 | if (real_size < size) { |
| 543 | // Overflow. |
| 544 | errno = ENOMEM; |
| Chia-hung Duan | f7e8b17 | 2022-11-01 21:37:56 +0000 | [diff] [blame] | 545 | result.setValue<void*>(nullptr); |
| 546 | return result; |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 547 | } |
| 548 | |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 549 | if (size > PointerInfoType::MaxSize()) { |
| 550 | errno = ENOMEM; |
| Chia-hung Duan | f7e8b17 | 2022-11-01 21:37:56 +0000 | [diff] [blame] | 551 | result.setValue<void*>(nullptr); |
| 552 | return result; |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 553 | } |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 554 | |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 555 | if (g_debug->HeaderEnabled()) { |
| Chia-hung Duan | f7e8b17 | 2022-11-01 21:37:56 +0000 | [diff] [blame] | 556 | result = TCALL(memalign, MINIMUM_ALIGNMENT_BYTES, real_size); |
| 557 | Header* header = reinterpret_cast<Header*>(result.getValue<void*>()); |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 558 | if (header == nullptr) { |
| Chia-hung Duan | f7e8b17 | 2022-11-01 21:37:56 +0000 | [diff] [blame] | 559 | return result; |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 560 | } |
| Chia-hung Duan | f7e8b17 | 2022-11-01 21:37:56 +0000 | [diff] [blame] | 561 | result.setValue<void*>(InitHeader(header, header, size)); |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 562 | } else { |
| Chia-hung Duan | f7e8b17 | 2022-11-01 21:37:56 +0000 | [diff] [blame] | 563 | result = TCALL(malloc, real_size); |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 564 | } |
| 565 | |
| Chia-hung Duan | f7e8b17 | 2022-11-01 21:37:56 +0000 | [diff] [blame] | 566 | void* pointer = result.getValue<void*>(); |
| 567 | |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 568 | if (pointer != nullptr) { |
| 569 | if (g_debug->TrackPointers()) { |
| 570 | PointerData::Add(pointer, size); |
| 571 | } |
| 572 | |
| 573 | if (g_debug->config().options() & FILL_ON_ALLOC) { |
| 574 | size_t bytes = InternalMallocUsableSize(pointer); |
| 575 | size_t fill_bytes = g_debug->config().fill_on_alloc_bytes(); |
| 576 | bytes = (bytes < fill_bytes) ? bytes : fill_bytes; |
| 577 | memset(pointer, g_debug->config().fill_alloc_value(), bytes); |
| 578 | } |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 579 | } |
| Chia-hung Duan | f7e8b17 | 2022-11-01 21:37:56 +0000 | [diff] [blame] | 580 | |
| 581 | return result; |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 582 | } |
| 583 | |
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 584 | void* debug_malloc(size_t size) { |
| Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame] | 585 | Unreachable::CheckIfRequested(g_debug->config()); |
| 586 | |
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 587 | if (DebugCallsDisabled()) { |
| 588 | return g_dispatch->malloc(size); |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 589 | } |
| Christopher Ferris | d269fcc | 2019-05-06 19:03:59 -0700 | [diff] [blame] | 590 | ScopedConcurrentLock lock; |
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 591 | ScopedDisableDebugCalls disable; |
| Christopher Ferris | 9bf7817 | 2020-05-20 15:37:30 -0700 | [diff] [blame] | 592 | ScopedBacktraceSignalBlocker blocked; |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 593 | |
| Christopher Ferris | f756e4c | 2024-11-19 04:28:20 +0000 | [diff] [blame] | 594 | memory_trace::Entry* entry = nullptr; |
| 595 | if (g_debug->config().options() & RECORD_ALLOCS) { |
| 596 | // In order to preserve the order of operations, reserve the entry before |
| 597 | // performing the operation. |
| 598 | entry = g_debug->record->ReserveEntry(); |
| 599 | } |
| 600 | |
| Chia-hung Duan | f7e8b17 | 2022-11-01 21:37:56 +0000 | [diff] [blame] | 601 | TimedResult result = InternalMalloc(size); |
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 602 | |
| Christopher Ferris | f756e4c | 2024-11-19 04:28:20 +0000 | [diff] [blame] | 603 | if (entry != nullptr) { |
| 604 | *entry = memory_trace::Entry{.tid = gettid(), |
| 605 | .type = memory_trace::MALLOC, |
| 606 | .ptr = reinterpret_cast<uint64_t>(result.getValue<void*>()), |
| 607 | .size = size, |
| 608 | .start_ns = result.GetStartTimeNS(), |
| 609 | .end_ns = result.GetEndTimeNS()}; |
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 610 | } |
| 611 | |
| Chia-hung Duan | f7e8b17 | 2022-11-01 21:37:56 +0000 | [diff] [blame] | 612 | return result.getValue<void*>(); |
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 613 | } |
| 614 | |
| Chia-hung Duan | f7e8b17 | 2022-11-01 21:37:56 +0000 | [diff] [blame] | 615 | static TimedResult InternalFree(void* pointer) { |
| Christopher Ferris | 5610d5a | 2023-11-14 15:04:50 -0800 | [diff] [blame] | 616 | uint64_t options = g_debug->config().options(); |
| 617 | if ((options & BACKTRACE) && g_debug->pointer->ShouldDumpAndReset()) { |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 618 | debug_dump_heap(android::base::StringPrintf( |
| 619 | "%s.%d.txt", g_debug->config().backtrace_dump_prefix().c_str(), getpid()) |
| 620 | .c_str()); |
| Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 621 | } |
| Christopher Ferris | 5610d5a | 2023-11-14 15:04:50 -0800 | [diff] [blame] | 622 | if (options & LOG_ALLOCATOR_STATS_ON_SIGNAL) { |
| 623 | LogAllocatorStats::CheckIfShouldLog(); |
| 624 | } |
| Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 625 | |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 626 | void* free_pointer = pointer; |
| 627 | size_t bytes; |
| Christopher Ferris | d091962 | 2016-03-15 22:39:39 -0700 | [diff] [blame] | 628 | Header* header; |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 629 | if (g_debug->HeaderEnabled()) { |
| Christopher Ferris | d091962 | 2016-03-15 22:39:39 -0700 | [diff] [blame] | 630 | header = g_debug->GetHeader(pointer); |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 631 | free_pointer = header->orig_pointer; |
| 632 | |
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 633 | if (g_debug->config().options() & FRONT_GUARD) { |
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 634 | if (!g_debug->front_guard->Valid(header)) { |
| 635 | g_debug->front_guard->LogFailure(header); |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 636 | } |
| 637 | } |
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 638 | if (g_debug->config().options() & REAR_GUARD) { |
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 639 | if (!g_debug->rear_guard->Valid(header)) { |
| 640 | g_debug->rear_guard->LogFailure(header); |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 641 | } |
| 642 | } |
| 643 | |
| Christopher Ferris | 7993b80 | 2016-01-28 18:35:05 -0800 | [diff] [blame] | 644 | header->tag = DEBUG_FREE_TAG; |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 645 | |
| 646 | bytes = header->usable_size; |
| 647 | } else { |
| 648 | bytes = g_dispatch->malloc_usable_size(pointer); |
| 649 | } |
| 650 | |
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 651 | if (g_debug->config().options() & FILL_ON_FREE) { |
| 652 | size_t fill_bytes = g_debug->config().fill_on_free_bytes(); |
| Christopher Ferris | a383648 | 2022-05-13 12:09:39 -0700 | [diff] [blame] | 653 | fill_bytes = (bytes < fill_bytes) ? bytes : fill_bytes; |
| 654 | memset(pointer, g_debug->config().fill_free_value(), fill_bytes); |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 655 | } |
| 656 | |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 657 | if (g_debug->TrackPointers()) { |
| 658 | PointerData::Remove(pointer); |
| 659 | } |
| 660 | |
| Chia-hung Duan | f7e8b17 | 2022-11-01 21:37:56 +0000 | [diff] [blame] | 661 | TimedResult result; |
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 662 | if (g_debug->config().options() & FREE_TRACK) { |
| Christopher Ferris | d091962 | 2016-03-15 22:39:39 -0700 | [diff] [blame] | 663 | // Do not add the allocation until we are done modifying the pointer |
| 664 | // itself. This avoids a race if a lot of threads are all doing |
| 665 | // frees at the same time and we wind up trying to really free this |
| 666 | // pointer from another thread, while still trying to free it in |
| 667 | // this function. |
| Christopher Ferris | a383648 | 2022-05-13 12:09:39 -0700 | [diff] [blame] | 668 | pointer = PointerData::AddFreed(pointer, bytes); |
| Chia-hung Duan | f7e8b17 | 2022-11-01 21:37:56 +0000 | [diff] [blame] | 669 | if (pointer != nullptr && g_debug->HeaderEnabled()) { |
| 670 | pointer = g_debug->GetHeader(pointer)->orig_pointer; |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 671 | } |
| Chia-hung Duan | f7e8b17 | 2022-11-01 21:37:56 +0000 | [diff] [blame] | 672 | result = TCALLVOID(free, pointer); |
| Christopher Ferris | d091962 | 2016-03-15 22:39:39 -0700 | [diff] [blame] | 673 | } else { |
| Chia-hung Duan | f7e8b17 | 2022-11-01 21:37:56 +0000 | [diff] [blame] | 674 | result = TCALLVOID(free, free_pointer); |
| Christopher Ferris | d091962 | 2016-03-15 22:39:39 -0700 | [diff] [blame] | 675 | } |
| Chia-hung Duan | f7e8b17 | 2022-11-01 21:37:56 +0000 | [diff] [blame] | 676 | |
| 677 | return result; |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 678 | } |
| 679 | |
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 680 | void debug_free(void* pointer) { |
| Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame] | 681 | Unreachable::CheckIfRequested(g_debug->config()); |
| 682 | |
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 683 | if (DebugCallsDisabled() || pointer == nullptr) { |
| 684 | return g_dispatch->free(pointer); |
| 685 | } |
| Christopher Ferris | 31199e7 | 2024-11-08 14:44:53 -0800 | [diff] [blame] | 686 | |
| 687 | size_t size; |
| 688 | if (g_debug->config().options() & RECORD_ALLOCS) { |
| 689 | // Need to get the size before disabling debug calls. |
| 690 | size = debug_malloc_usable_size(pointer); |
| 691 | } |
| 692 | |
| Christopher Ferris | d269fcc | 2019-05-06 19:03:59 -0700 | [diff] [blame] | 693 | ScopedConcurrentLock lock; |
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 694 | ScopedDisableDebugCalls disable; |
| Christopher Ferris | 9bf7817 | 2020-05-20 15:37:30 -0700 | [diff] [blame] | 695 | ScopedBacktraceSignalBlocker blocked; |
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 696 | |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 697 | if (!VerifyPointer(pointer, "free")) { |
| 698 | return; |
| 699 | } |
| 700 | |
| Christopher Ferris | 31199e7 | 2024-11-08 14:44:53 -0800 | [diff] [blame] | 701 | int64_t present_bytes = -1; |
| Christopher Ferris | f756e4c | 2024-11-19 04:28:20 +0000 | [diff] [blame] | 702 | memory_trace::Entry* entry = nullptr; |
| 703 | if (g_debug->config().options() & RECORD_ALLOCS) { |
| 704 | // In order to preserve the order of operations, reserve the entry before |
| 705 | // performing the operation. |
| 706 | entry = g_debug->record->ReserveEntry(); |
| Christopher Ferris | 31199e7 | 2024-11-08 14:44:53 -0800 | [diff] [blame] | 707 | |
| 708 | // Need to get the present bytes before the pointer is freed in case the |
| 709 | // memory is released during the free call. |
| 710 | present_bytes = g_debug->record->GetPresentBytes(pointer, size); |
| Christopher Ferris | f756e4c | 2024-11-19 04:28:20 +0000 | [diff] [blame] | 711 | } |
| 712 | |
| Chia-hung Duan | f7e8b17 | 2022-11-01 21:37:56 +0000 | [diff] [blame] | 713 | TimedResult result = InternalFree(pointer); |
| 714 | |
| Christopher Ferris | f756e4c | 2024-11-19 04:28:20 +0000 | [diff] [blame] | 715 | if (entry != nullptr) { |
| 716 | *entry = memory_trace::Entry{.tid = gettid(), |
| 717 | .type = memory_trace::FREE, |
| 718 | .ptr = reinterpret_cast<uint64_t>(pointer), |
| Christopher Ferris | 31199e7 | 2024-11-08 14:44:53 -0800 | [diff] [blame] | 719 | .present_bytes = present_bytes, |
| Christopher Ferris | f756e4c | 2024-11-19 04:28:20 +0000 | [diff] [blame] | 720 | .start_ns = result.GetStartTimeNS(), |
| 721 | .end_ns = result.GetEndTimeNS()}; |
| Chia-hung Duan | f7e8b17 | 2022-11-01 21:37:56 +0000 | [diff] [blame] | 722 | } |
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 723 | } |
| 724 | |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 725 | void* debug_memalign(size_t alignment, size_t bytes) { |
| Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame] | 726 | Unreachable::CheckIfRequested(g_debug->config()); |
| 727 | |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 728 | if (DebugCallsDisabled()) { |
| 729 | return g_dispatch->memalign(alignment, bytes); |
| 730 | } |
| Christopher Ferris | d269fcc | 2019-05-06 19:03:59 -0700 | [diff] [blame] | 731 | ScopedConcurrentLock lock; |
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 732 | ScopedDisableDebugCalls disable; |
| Christopher Ferris | 9bf7817 | 2020-05-20 15:37:30 -0700 | [diff] [blame] | 733 | ScopedBacktraceSignalBlocker blocked; |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 734 | |
| Colin Cross | 9567c7b | 2016-03-09 17:56:14 -0800 | [diff] [blame] | 735 | if (bytes == 0) { |
| 736 | bytes = 1; |
| 737 | } |
| 738 | |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 739 | if (bytes > PointerInfoType::MaxSize()) { |
| 740 | errno = ENOMEM; |
| 741 | return nullptr; |
| 742 | } |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 743 | |
| Christopher Ferris | f756e4c | 2024-11-19 04:28:20 +0000 | [diff] [blame] | 744 | memory_trace::Entry* entry = nullptr; |
| 745 | if (g_debug->config().options() & RECORD_ALLOCS) { |
| 746 | // In order to preserve the order of operations, reserve the entry before |
| 747 | // performing the operation. |
| 748 | entry = g_debug->record->ReserveEntry(); |
| 749 | } |
| 750 | |
| Chia-hung Duan | f7e8b17 | 2022-11-01 21:37:56 +0000 | [diff] [blame] | 751 | TimedResult result; |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 752 | void* pointer; |
| 753 | if (g_debug->HeaderEnabled()) { |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 754 | // Make the alignment a power of two. |
| Elliott Hughes | 731da1b | 2025-07-23 10:52:52 -0700 | [diff] [blame] | 755 | alignment = std::bit_ceil(alignment); |
| Christopher Ferris | 72df670 | 2016-02-11 15:51:31 -0800 | [diff] [blame] | 756 | // Force the alignment to at least MINIMUM_ALIGNMENT_BYTES to guarantee |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 757 | // that the header is aligned properly. |
| Christopher Ferris | 72df670 | 2016-02-11 15:51:31 -0800 | [diff] [blame] | 758 | if (alignment < MINIMUM_ALIGNMENT_BYTES) { |
| 759 | alignment = MINIMUM_ALIGNMENT_BYTES; |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | // We don't have any idea what the natural alignment of |
| 763 | // the underlying native allocator is, so we always need to |
| 764 | // over allocate. |
| 765 | size_t real_size = alignment + bytes + g_debug->extra_bytes(); |
| 766 | if (real_size < bytes) { |
| 767 | // Overflow. |
| 768 | errno = ENOMEM; |
| 769 | return nullptr; |
| 770 | } |
| 771 | |
| Chia-hung Duan | f7e8b17 | 2022-11-01 21:37:56 +0000 | [diff] [blame] | 772 | result = TCALL(malloc, real_size); |
| 773 | pointer = result.getValue<void*>(); |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 774 | if (pointer == nullptr) { |
| 775 | return nullptr; |
| 776 | } |
| 777 | |
| 778 | uintptr_t value = reinterpret_cast<uintptr_t>(pointer) + g_debug->pointer_offset(); |
| 779 | // Now align the pointer. |
| 780 | value += (-value % alignment); |
| 781 | |
| 782 | Header* header = g_debug->GetHeader(reinterpret_cast<void*>(value)); |
| Chia-hung Duan | f7e8b17 | 2022-11-01 21:37:56 +0000 | [diff] [blame] | 783 | // Don't need to update `result` here because we only need the timestamps. |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 784 | pointer = InitHeader(header, pointer, bytes); |
| 785 | } else { |
| 786 | size_t real_size = bytes + g_debug->extra_bytes(); |
| 787 | if (real_size < bytes) { |
| 788 | // Overflow. |
| 789 | errno = ENOMEM; |
| 790 | return nullptr; |
| 791 | } |
| Chia-hung Duan | f7e8b17 | 2022-11-01 21:37:56 +0000 | [diff] [blame] | 792 | result = TCALL(memalign, alignment, real_size); |
| 793 | pointer = result.getValue<void*>(); |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 794 | } |
| 795 | |
| Christopher Ferris | f756e4c | 2024-11-19 04:28:20 +0000 | [diff] [blame] | 796 | if (pointer == nullptr) { |
| 797 | return nullptr; |
| 798 | } |
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 799 | |
| Christopher Ferris | f756e4c | 2024-11-19 04:28:20 +0000 | [diff] [blame] | 800 | if (g_debug->TrackPointers()) { |
| 801 | PointerData::Add(pointer, bytes); |
| 802 | } |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 803 | |
| Christopher Ferris | f756e4c | 2024-11-19 04:28:20 +0000 | [diff] [blame] | 804 | if (g_debug->config().options() & FILL_ON_ALLOC) { |
| 805 | size_t bytes = InternalMallocUsableSize(pointer); |
| 806 | size_t fill_bytes = g_debug->config().fill_on_alloc_bytes(); |
| 807 | bytes = (bytes < fill_bytes) ? bytes : fill_bytes; |
| 808 | memset(pointer, g_debug->config().fill_alloc_value(), bytes); |
| 809 | } |
| 810 | |
| 811 | if (entry != nullptr) { |
| 812 | *entry = memory_trace::Entry{.tid = gettid(), |
| 813 | .type = memory_trace::MEMALIGN, |
| 814 | .ptr = reinterpret_cast<uint64_t>(pointer), |
| 815 | .size = bytes, |
| 816 | .u.align = alignment, |
| 817 | .start_ns = result.GetStartTimeNS(), |
| 818 | .end_ns = result.GetEndTimeNS()}; |
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 819 | } |
| 820 | |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 821 | return pointer; |
| 822 | } |
| 823 | |
| 824 | void* debug_realloc(void* pointer, size_t bytes) { |
| Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame] | 825 | Unreachable::CheckIfRequested(g_debug->config()); |
| 826 | |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 827 | if (DebugCallsDisabled()) { |
| 828 | return g_dispatch->realloc(pointer, bytes); |
| 829 | } |
| Christopher Ferris | 31199e7 | 2024-11-08 14:44:53 -0800 | [diff] [blame] | 830 | |
| 831 | size_t old_size; |
| 832 | if (pointer != nullptr && g_debug->config().options() & RECORD_ALLOCS) { |
| 833 | // Need to get the size before disabling debug calls. |
| 834 | old_size = debug_malloc_usable_size(pointer); |
| 835 | } |
| 836 | |
| Christopher Ferris | d269fcc | 2019-05-06 19:03:59 -0700 | [diff] [blame] | 837 | ScopedConcurrentLock lock; |
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 838 | ScopedDisableDebugCalls disable; |
| Christopher Ferris | 9bf7817 | 2020-05-20 15:37:30 -0700 | [diff] [blame] | 839 | ScopedBacktraceSignalBlocker blocked; |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 840 | |
| Christopher Ferris | f756e4c | 2024-11-19 04:28:20 +0000 | [diff] [blame] | 841 | memory_trace::Entry* entry = nullptr; |
| 842 | if (g_debug->config().options() & RECORD_ALLOCS) { |
| 843 | // In order to preserve the order of operations, reserve the entry before |
| 844 | // performing the operation. |
| 845 | entry = g_debug->record->ReserveEntry(); |
| 846 | } |
| 847 | |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 848 | if (pointer == nullptr) { |
| Chia-hung Duan | f7e8b17 | 2022-11-01 21:37:56 +0000 | [diff] [blame] | 849 | TimedResult result = InternalMalloc(bytes); |
| Chia-hung Duan | f7e8b17 | 2022-11-01 21:37:56 +0000 | [diff] [blame] | 850 | pointer = result.getValue<void*>(); |
| Christopher Ferris | f756e4c | 2024-11-19 04:28:20 +0000 | [diff] [blame] | 851 | if (entry != nullptr) { |
| 852 | *entry = memory_trace::Entry{.tid = gettid(), |
| 853 | .type = memory_trace::REALLOC, |
| 854 | .ptr = reinterpret_cast<uint64_t>(pointer), |
| 855 | .size = bytes, |
| 856 | .u.old_ptr = 0, |
| 857 | .start_ns = result.GetStartTimeNS(), |
| 858 | .end_ns = result.GetEndTimeNS()}; |
| Christopher Ferris | e39602c | 2024-11-02 05:02:43 +0000 | [diff] [blame] | 859 | } |
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 860 | return pointer; |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 861 | } |
| 862 | |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 863 | if (!VerifyPointer(pointer, "realloc")) { |
| 864 | return nullptr; |
| 865 | } |
| 866 | |
| Christopher Ferris | 31199e7 | 2024-11-08 14:44:53 -0800 | [diff] [blame] | 867 | int64_t present_bytes = -1; |
| 868 | if (g_debug->config().options() & RECORD_ALLOCS) { |
| 869 | // Need to get the present bytes before the pointer is freed in case the |
| 870 | // memory is released during the free call. |
| 871 | present_bytes = g_debug->record->GetPresentBytes(pointer, old_size); |
| 872 | } |
| 873 | |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 874 | if (bytes == 0) { |
| Chia-hung Duan | f7e8b17 | 2022-11-01 21:37:56 +0000 | [diff] [blame] | 875 | TimedResult result = InternalFree(pointer); |
| 876 | |
| Christopher Ferris | f756e4c | 2024-11-19 04:28:20 +0000 | [diff] [blame] | 877 | if (entry != nullptr) { |
| 878 | *entry = memory_trace::Entry{.tid = gettid(), |
| 879 | .type = memory_trace::REALLOC, |
| 880 | .ptr = 0, |
| 881 | .size = 0, |
| 882 | .u.old_ptr = reinterpret_cast<uint64_t>(pointer), |
| Christopher Ferris | 31199e7 | 2024-11-08 14:44:53 -0800 | [diff] [blame] | 883 | .present_bytes = present_bytes, |
| Christopher Ferris | f756e4c | 2024-11-19 04:28:20 +0000 | [diff] [blame] | 884 | .start_ns = result.GetStartTimeNS(), |
| 885 | .end_ns = result.GetEndTimeNS()}; |
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 886 | } |
| 887 | |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 888 | return nullptr; |
| 889 | } |
| 890 | |
| 891 | size_t real_size = bytes; |
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 892 | if (g_debug->config().options() & EXPAND_ALLOC) { |
| 893 | real_size += g_debug->config().expand_alloc_bytes(); |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 894 | if (real_size < bytes) { |
| 895 | // Overflow. |
| 896 | errno = ENOMEM; |
| 897 | return nullptr; |
| 898 | } |
| 899 | } |
| 900 | |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 901 | if (bytes > PointerInfoType::MaxSize()) { |
| 902 | errno = ENOMEM; |
| 903 | return nullptr; |
| 904 | } |
| 905 | |
| Chia-hung Duan | f7e8b17 | 2022-11-01 21:37:56 +0000 | [diff] [blame] | 906 | TimedResult result; |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 907 | void* new_pointer; |
| 908 | size_t prev_size; |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 909 | if (g_debug->HeaderEnabled()) { |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 910 | // Same size, do nothing. |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 911 | Header* header = g_debug->GetHeader(pointer); |
| 912 | if (real_size == header->size) { |
| 913 | if (g_debug->TrackPointers()) { |
| 914 | // Remove and re-add so that the backtrace is updated. |
| 915 | PointerData::Remove(pointer); |
| 916 | PointerData::Add(pointer, real_size); |
| 917 | } |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 918 | return pointer; |
| 919 | } |
| 920 | |
| 921 | // Allocation is shrinking. |
| 922 | if (real_size < header->usable_size) { |
| 923 | header->size = real_size; |
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 924 | if (g_debug->config().options() & REAR_GUARD) { |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 925 | // Don't bother allocating a smaller pointer in this case, simply |
| 926 | // change the header usable_size and reset the rear guard. |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 927 | header->usable_size = header->size; |
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 928 | memset(g_debug->GetRearGuard(header), g_debug->config().rear_guard_value(), |
| 929 | g_debug->config().rear_guard_bytes()); |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 930 | } |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 931 | if (g_debug->TrackPointers()) { |
| 932 | // Remove and re-add so that the backtrace is updated. |
| 933 | PointerData::Remove(pointer); |
| 934 | PointerData::Add(pointer, real_size); |
| 935 | } |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 936 | return pointer; |
| 937 | } |
| 938 | |
| 939 | // Allocate the new size. |
| Chia-hung Duan | f7e8b17 | 2022-11-01 21:37:56 +0000 | [diff] [blame] | 940 | result = InternalMalloc(bytes); |
| 941 | new_pointer = result.getValue<void*>(); |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 942 | if (new_pointer == nullptr) { |
| 943 | errno = ENOMEM; |
| 944 | return nullptr; |
| 945 | } |
| 946 | |
| 947 | prev_size = header->usable_size; |
| 948 | memcpy(new_pointer, pointer, prev_size); |
| Chia-hung Duan | f7e8b17 | 2022-11-01 21:37:56 +0000 | [diff] [blame] | 949 | TimedResult free_time = InternalFree(pointer); |
| 950 | // `realloc` is split into two steps, update the end time to the finish time |
| 951 | // of the second operation. |
| 952 | result.SetEndTimeNS(free_time.GetEndTimeNS()); |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 953 | } else { |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 954 | if (g_debug->TrackPointers()) { |
| 955 | PointerData::Remove(pointer); |
| 956 | } |
| 957 | |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 958 | prev_size = g_dispatch->malloc_usable_size(pointer); |
| Chia-hung Duan | f7e8b17 | 2022-11-01 21:37:56 +0000 | [diff] [blame] | 959 | result = TCALL(realloc, pointer, real_size); |
| 960 | new_pointer = result.getValue<void*>(); |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 961 | if (new_pointer == nullptr) { |
| 962 | return nullptr; |
| 963 | } |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 964 | |
| 965 | if (g_debug->TrackPointers()) { |
| 966 | PointerData::Add(new_pointer, real_size); |
| 967 | } |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 968 | } |
| 969 | |
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 970 | if (g_debug->config().options() & FILL_ON_ALLOC) { |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 971 | size_t bytes = InternalMallocUsableSize(new_pointer); |
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 972 | if (bytes > g_debug->config().fill_on_alloc_bytes()) { |
| 973 | bytes = g_debug->config().fill_on_alloc_bytes(); |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 974 | } |
| 975 | if (bytes > prev_size) { |
| 976 | memset(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(new_pointer) + prev_size), |
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 977 | g_debug->config().fill_alloc_value(), bytes - prev_size); |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 978 | } |
| 979 | } |
| 980 | |
| Christopher Ferris | f756e4c | 2024-11-19 04:28:20 +0000 | [diff] [blame] | 981 | if (entry != nullptr) { |
| 982 | *entry = memory_trace::Entry{.tid = gettid(), |
| 983 | .type = memory_trace::REALLOC, |
| 984 | .ptr = reinterpret_cast<uint64_t>(new_pointer), |
| 985 | .size = bytes, |
| 986 | .u.old_ptr = reinterpret_cast<uint64_t>(pointer), |
| Christopher Ferris | 31199e7 | 2024-11-08 14:44:53 -0800 | [diff] [blame] | 987 | .present_bytes = present_bytes, |
| Christopher Ferris | f756e4c | 2024-11-19 04:28:20 +0000 | [diff] [blame] | 988 | .start_ns = result.GetStartTimeNS(), |
| 989 | .end_ns = result.GetEndTimeNS()}; |
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 990 | } |
| 991 | |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 992 | return new_pointer; |
| 993 | } |
| 994 | |
| 995 | void* debug_calloc(size_t nmemb, size_t bytes) { |
| Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame] | 996 | Unreachable::CheckIfRequested(g_debug->config()); |
| 997 | |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 998 | if (DebugCallsDisabled()) { |
| 999 | return g_dispatch->calloc(nmemb, bytes); |
| 1000 | } |
| Christopher Ferris | d269fcc | 2019-05-06 19:03:59 -0700 | [diff] [blame] | 1001 | ScopedConcurrentLock lock; |
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 1002 | ScopedDisableDebugCalls disable; |
| Christopher Ferris | 9bf7817 | 2020-05-20 15:37:30 -0700 | [diff] [blame] | 1003 | ScopedBacktraceSignalBlocker blocked; |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 1004 | |
| Colin Cross | 7877df6 | 2016-03-10 13:01:27 -0800 | [diff] [blame] | 1005 | size_t size; |
| 1006 | if (__builtin_mul_overflow(nmemb, bytes, &size)) { |
| 1007 | // Overflow |
| 1008 | errno = ENOMEM; |
| 1009 | return nullptr; |
| 1010 | } |
| 1011 | |
| Colin Cross | 9567c7b | 2016-03-09 17:56:14 -0800 | [diff] [blame] | 1012 | if (size == 0) { |
| 1013 | size = 1; |
| 1014 | } |
| 1015 | |
| Colin Cross | 7877df6 | 2016-03-10 13:01:27 -0800 | [diff] [blame] | 1016 | size_t real_size; |
| 1017 | if (__builtin_add_overflow(size, g_debug->extra_bytes(), &real_size)) { |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 1018 | // Overflow. |
| 1019 | errno = ENOMEM; |
| 1020 | return nullptr; |
| 1021 | } |
| 1022 | |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 1023 | if (real_size > PointerInfoType::MaxSize()) { |
| 1024 | errno = ENOMEM; |
| 1025 | return nullptr; |
| 1026 | } |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 1027 | |
| Christopher Ferris | f756e4c | 2024-11-19 04:28:20 +0000 | [diff] [blame] | 1028 | memory_trace::Entry* entry = nullptr; |
| 1029 | if (g_debug->config().options() & RECORD_ALLOCS) { |
| 1030 | // In order to preserve the order of operations, reserve the entry before |
| 1031 | // performing the operation. |
| 1032 | entry = g_debug->record->ReserveEntry(); |
| 1033 | } |
| 1034 | |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 1035 | void* pointer; |
| Chia-hung Duan | f7e8b17 | 2022-11-01 21:37:56 +0000 | [diff] [blame] | 1036 | TimedResult result; |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 1037 | if (g_debug->HeaderEnabled()) { |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 1038 | // Need to guarantee the alignment of the header. |
| Chia-hung Duan | f7e8b17 | 2022-11-01 21:37:56 +0000 | [diff] [blame] | 1039 | result = TCALL(memalign, MINIMUM_ALIGNMENT_BYTES, real_size); |
| 1040 | Header* header = reinterpret_cast<Header*>(result.getValue<void*>()); |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 1041 | if (header == nullptr) { |
| 1042 | return nullptr; |
| 1043 | } |
| 1044 | memset(header, 0, g_dispatch->malloc_usable_size(header)); |
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 1045 | pointer = InitHeader(header, header, size); |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 1046 | } else { |
| Chia-hung Duan | f7e8b17 | 2022-11-01 21:37:56 +0000 | [diff] [blame] | 1047 | result = TCALL(calloc, 1, real_size); |
| 1048 | pointer = result.getValue<void*>(); |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 1049 | } |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 1050 | |
| Christopher Ferris | f756e4c | 2024-11-19 04:28:20 +0000 | [diff] [blame] | 1051 | if (entry != nullptr) { |
| 1052 | *entry = memory_trace::Entry{.tid = gettid(), |
| 1053 | .type = memory_trace::CALLOC, |
| 1054 | .ptr = reinterpret_cast<uint64_t>(pointer), |
| 1055 | .size = bytes, |
| 1056 | .u.n_elements = nmemb, |
| 1057 | .start_ns = result.GetStartTimeNS(), |
| 1058 | .end_ns = result.GetEndTimeNS()}; |
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 1059 | } |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 1060 | |
| 1061 | if (pointer != nullptr && g_debug->TrackPointers()) { |
| 1062 | PointerData::Add(pointer, size); |
| 1063 | } |
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 1064 | return pointer; |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | struct mallinfo debug_mallinfo() { |
| 1068 | return g_dispatch->mallinfo(); |
| 1069 | } |
| 1070 | |
| Christopher Ferris | a1c0d2f | 2017-05-15 15:50:19 -0700 | [diff] [blame] | 1071 | int debug_mallopt(int param, int value) { |
| 1072 | return g_dispatch->mallopt(param, value); |
| 1073 | } |
| 1074 | |
| Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 1075 | int debug_malloc_info(int options, FILE* fp) { |
| 1076 | if (DebugCallsDisabled() || !g_debug->TrackPointers()) { |
| 1077 | return g_dispatch->malloc_info(options, fp); |
| 1078 | } |
| Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 1079 | |
| 1080 | // Make sure any pending output is written to the file. |
| 1081 | fflush(fp); |
| 1082 | |
| Christopher Ferris | d269fcc | 2019-05-06 19:03:59 -0700 | [diff] [blame] | 1083 | ScopedConcurrentLock lock; |
| 1084 | ScopedDisableDebugCalls disable; |
| Christopher Ferris | 9bf7817 | 2020-05-20 15:37:30 -0700 | [diff] [blame] | 1085 | ScopedBacktraceSignalBlocker blocked; |
| Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 1086 | |
| Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 1087 | // Avoid any issues where allocations are made that will be freed |
| 1088 | // in the fclose. |
| 1089 | int fd = fileno(fp); |
| 1090 | MallocXmlElem root(fd, "malloc", "version=\"debug-malloc-1\""); |
| Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 1091 | std::vector<ListInfoType> list; |
| 1092 | PointerData::GetAllocList(&list); |
| 1093 | |
| 1094 | size_t alloc_num = 0; |
| 1095 | for (size_t i = 0; i < list.size(); i++) { |
| Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 1096 | MallocXmlElem alloc(fd, "allocation", "nr=\"%zu\"", alloc_num); |
| Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 1097 | |
| 1098 | size_t total = 1; |
| 1099 | size_t size = list[i].size; |
| 1100 | while (i < list.size() - 1 && list[i + 1].size == size) { |
| 1101 | i++; |
| 1102 | total++; |
| 1103 | } |
| Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 1104 | MallocXmlElem(fd, "size").Contents("%zu", list[i].size); |
| 1105 | MallocXmlElem(fd, "total").Contents("%zu", total); |
| Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 1106 | alloc_num++; |
| 1107 | } |
| 1108 | return 0; |
| 1109 | } |
| 1110 | |
| Christopher Ferris | cae21a9 | 2018-02-05 18:14:55 -0800 | [diff] [blame] | 1111 | void* debug_aligned_alloc(size_t alignment, size_t size) { |
| Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame] | 1112 | Unreachable::CheckIfRequested(g_debug->config()); |
| 1113 | |
| Christopher Ferris | cae21a9 | 2018-02-05 18:14:55 -0800 | [diff] [blame] | 1114 | if (DebugCallsDisabled()) { |
| 1115 | return g_dispatch->aligned_alloc(alignment, size); |
| 1116 | } |
| Christopher Ferris | a22f5d5 | 2019-03-01 16:40:59 -0800 | [diff] [blame] | 1117 | if (!powerof2(alignment) || (size % alignment) != 0) { |
| Christopher Ferris | cae21a9 | 2018-02-05 18:14:55 -0800 | [diff] [blame] | 1118 | errno = EINVAL; |
| 1119 | return nullptr; |
| 1120 | } |
| 1121 | return debug_memalign(alignment, size); |
| 1122 | } |
| 1123 | |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 1124 | int debug_posix_memalign(void** memptr, size_t alignment, size_t size) { |
| Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame] | 1125 | Unreachable::CheckIfRequested(g_debug->config()); |
| 1126 | |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 1127 | if (DebugCallsDisabled()) { |
| 1128 | return g_dispatch->posix_memalign(memptr, alignment, size); |
| 1129 | } |
| 1130 | |
| Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 1131 | if (alignment < sizeof(void*) || !powerof2(alignment)) { |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 1132 | return EINVAL; |
| 1133 | } |
| 1134 | int saved_errno = errno; |
| 1135 | *memptr = debug_memalign(alignment, size); |
| 1136 | errno = saved_errno; |
| 1137 | return (*memptr != nullptr) ? 0 : ENOMEM; |
| 1138 | } |
| 1139 | |
| Christopher Ferris | 6f517cd | 2019-11-08 11:28:38 -0800 | [diff] [blame] | 1140 | 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] | 1141 | void* arg) { |
| Christopher Ferris | d269fcc | 2019-05-06 19:03:59 -0700 | [diff] [blame] | 1142 | ScopedConcurrentLock lock; |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 1143 | if (g_debug->TrackPointers()) { |
| Christopher Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 1144 | PointerData::IteratePointers([&callback, &arg](uintptr_t pointer) { |
| 1145 | callback(pointer, InternalMallocUsableSize(reinterpret_cast<void*>(pointer)), arg); |
| 1146 | }); |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 1147 | return 0; |
| 1148 | } |
| Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 1149 | |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 1150 | // An option that adds a header will add pointer tracking, so no need to |
| 1151 | // check if headers are enabled. |
| Christopher Ferris | 6f517cd | 2019-11-08 11:28:38 -0800 | [diff] [blame] | 1152 | return g_dispatch->malloc_iterate(base, size, callback, arg); |
| Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | void debug_malloc_disable() { |
| Christopher Ferris | d269fcc | 2019-05-06 19:03:59 -0700 | [diff] [blame] | 1156 | ScopedConcurrentLock lock; |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 1157 | if (g_debug->pointer) { |
| Abdelrahman Daim | 862bd50 | 2024-11-14 08:09:42 -0800 | [diff] [blame] | 1158 | // Acquire the pointer locks first, otherwise, the code can be holding |
| 1159 | // the allocation lock and deadlock trying to acquire a pointer lock. |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 1160 | g_debug->pointer->PrepareFork(); |
| Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 1161 | } |
| Abdelrahman Daim | 862bd50 | 2024-11-14 08:09:42 -0800 | [diff] [blame] | 1162 | g_dispatch->malloc_disable(); |
| Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 1163 | } |
| 1164 | |
| 1165 | void debug_malloc_enable() { |
| Christopher Ferris | d269fcc | 2019-05-06 19:03:59 -0700 | [diff] [blame] | 1166 | ScopedConcurrentLock lock; |
| Abdelrahman Daim | 862bd50 | 2024-11-14 08:09:42 -0800 | [diff] [blame] | 1167 | g_dispatch->malloc_enable(); |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 1168 | if (g_debug->pointer) { |
| 1169 | g_debug->pointer->PostForkParent(); |
| Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 1170 | } |
| Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 1171 | } |
| 1172 | |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 1173 | 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] | 1174 | if (DebugCallsDisabled() || pointer == nullptr) { |
| 1175 | return 0; |
| 1176 | } |
| Christopher Ferris | d269fcc | 2019-05-06 19:03:59 -0700 | [diff] [blame] | 1177 | ScopedConcurrentLock lock; |
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 1178 | ScopedDisableDebugCalls disable; |
| Christopher Ferris | 9bf7817 | 2020-05-20 15:37:30 -0700 | [diff] [blame] | 1179 | ScopedBacktraceSignalBlocker blocked; |
| Colin Cross | 2d4721c | 2016-02-02 11:57:54 -0800 | [diff] [blame] | 1180 | |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 1181 | if (!(g_debug->config().options() & BACKTRACE)) { |
| 1182 | return 0; |
| Colin Cross | 2d4721c | 2016-02-02 11:57:54 -0800 | [diff] [blame] | 1183 | } |
| Mitch Phillips | 3b21ada | 2020-01-07 15:47:47 -0800 | [diff] [blame] | 1184 | pointer = UntagPointer(pointer); |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 1185 | return PointerData::GetFrames(pointer, frames, max_frames); |
| Colin Cross | 2d4721c | 2016-02-02 11:57:54 -0800 | [diff] [blame] | 1186 | } |
| 1187 | |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 1188 | #if defined(HAVE_DEPRECATED_MALLOC_FUNCS) |
| 1189 | void* debug_pvalloc(size_t bytes) { |
| Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame] | 1190 | Unreachable::CheckIfRequested(g_debug->config()); |
| 1191 | |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 1192 | if (DebugCallsDisabled()) { |
| 1193 | return g_dispatch->pvalloc(bytes); |
| 1194 | } |
| 1195 | |
| 1196 | size_t pagesize = getpagesize(); |
| Elliott Hughes | 193b0bc | 2025-05-14 06:35:50 -0700 | [diff] [blame] | 1197 | size_t size = __builtin_align_up(bytes, pagesize); |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 1198 | if (size < bytes) { |
| 1199 | // Overflow |
| 1200 | errno = ENOMEM; |
| 1201 | return nullptr; |
| 1202 | } |
| 1203 | return debug_memalign(pagesize, size); |
| 1204 | } |
| 1205 | |
| 1206 | void* debug_valloc(size_t size) { |
| Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame] | 1207 | Unreachable::CheckIfRequested(g_debug->config()); |
| 1208 | |
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 1209 | if (DebugCallsDisabled()) { |
| 1210 | return g_dispatch->valloc(size); |
| 1211 | } |
| 1212 | return debug_memalign(getpagesize(), size); |
| 1213 | } |
| 1214 | #endif |
| Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 1215 | |
| 1216 | static std::mutex g_dump_lock; |
| 1217 | |
| Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 1218 | static void write_dump(int fd) { |
| 1219 | dprintf(fd, "Android Native Heap Dump v1.2\n\n"); |
| Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 1220 | |
| Christopher Ferris | 2e1a40a | 2018-06-13 10:46:34 -0700 | [diff] [blame] | 1221 | std::string fingerprint = android::base::GetProperty("ro.build.fingerprint", "unknown"); |
| Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 1222 | dprintf(fd, "Build fingerprint: '%s'\n\n", fingerprint.c_str()); |
| Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 1223 | |
| Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 1224 | PointerData::DumpLiveToFile(fd); |
| Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 1225 | |
| Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 1226 | dprintf(fd, "MAPS\n"); |
| Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 1227 | std::string content; |
| 1228 | if (!android::base::ReadFileToString("/proc/self/maps", &content)) { |
| Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 1229 | dprintf(fd, "Could not open /proc/self/maps\n"); |
| Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 1230 | } else { |
| Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 1231 | dprintf(fd, "%s", content.c_str()); |
| Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 1232 | } |
| Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 1233 | dprintf(fd, "END\n"); |
| Christopher Ferris | aa3e574 | 2023-01-31 01:31:52 +0000 | [diff] [blame] | 1234 | |
| 1235 | // Purge the memory that was allocated and freed during this operation |
| 1236 | // since it can be large enough to expand the RSS significantly. |
| Christopher Ferris | f1ab9c4 | 2023-03-22 13:47:27 -0700 | [diff] [blame] | 1237 | g_dispatch->mallopt(M_PURGE_ALL, 0); |
| Christopher Ferris | 2e1a40a | 2018-06-13 10:46:34 -0700 | [diff] [blame] | 1238 | } |
| 1239 | |
| 1240 | bool debug_write_malloc_leak_info(FILE* fp) { |
| Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 1241 | // Make sure any pending output is written to the file. |
| 1242 | fflush(fp); |
| 1243 | |
| Christopher Ferris | d269fcc | 2019-05-06 19:03:59 -0700 | [diff] [blame] | 1244 | ScopedConcurrentLock lock; |
| Christopher Ferris | 2e1a40a | 2018-06-13 10:46:34 -0700 | [diff] [blame] | 1245 | ScopedDisableDebugCalls disable; |
| Christopher Ferris | 9bf7817 | 2020-05-20 15:37:30 -0700 | [diff] [blame] | 1246 | ScopedBacktraceSignalBlocker blocked; |
| Christopher Ferris | 2e1a40a | 2018-06-13 10:46:34 -0700 | [diff] [blame] | 1247 | |
| 1248 | std::lock_guard<std::mutex> guard(g_dump_lock); |
| 1249 | |
| 1250 | if (!(g_debug->config().options() & BACKTRACE)) { |
| 1251 | return false; |
| 1252 | } |
| 1253 | |
| Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 1254 | write_dump(fileno(fp)); |
| 1255 | |
| Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 1256 | return true; |
| 1257 | } |
| Christopher Ferris | 2e1a40a | 2018-06-13 10:46:34 -0700 | [diff] [blame] | 1258 | |
| 1259 | void debug_dump_heap(const char* file_name) { |
| Christopher Ferris | d269fcc | 2019-05-06 19:03:59 -0700 | [diff] [blame] | 1260 | ScopedConcurrentLock lock; |
| Christopher Ferris | 2e1a40a | 2018-06-13 10:46:34 -0700 | [diff] [blame] | 1261 | ScopedDisableDebugCalls disable; |
| Christopher Ferris | 9bf7817 | 2020-05-20 15:37:30 -0700 | [diff] [blame] | 1262 | ScopedBacktraceSignalBlocker blocked; |
| Christopher Ferris | 2e1a40a | 2018-06-13 10:46:34 -0700 | [diff] [blame] | 1263 | |
| 1264 | std::lock_guard<std::mutex> guard(g_dump_lock); |
| 1265 | |
| Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 1266 | int fd = open(file_name, O_RDWR | O_CREAT | O_NOFOLLOW | O_TRUNC | O_CLOEXEC, 0644); |
| 1267 | if (fd == -1) { |
| Christopher Ferris | 2e1a40a | 2018-06-13 10:46:34 -0700 | [diff] [blame] | 1268 | error_log("Unable to create file: %s", file_name); |
| 1269 | return; |
| 1270 | } |
| 1271 | |
| 1272 | error_log("Dumping to file: %s\n", file_name); |
| Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 1273 | write_dump(fd); |
| 1274 | close(fd); |
| Christopher Ferris | 2e1a40a | 2018-06-13 10:46:34 -0700 | [diff] [blame] | 1275 | } |