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