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