blob: fce6c244e47f77643584058e140ea066db1c5a0c [file] [log] [blame]
Christopher Ferris63860cb2015-11-16 17:30:32 -08001/*
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 Ferrisd269fcc2019-05-06 19:03:59 -070032#include <pthread.h>
Christopher Ferris9bf78172020-05-20 15:37:30 -070033#include <signal.h>
Christopher Ferris6c619a02019-03-01 17:59:51 -080034#include <stdio.h>
Christopher Ferrisc328e442019-04-01 19:31:26 -070035#include <stdlib.h>
Christopher Ferris63860cb2015-11-16 17:30:32 -080036#include <string.h>
37#include <sys/cdefs.h>
38#include <sys/param.h>
Christopher Ferris9bf78172020-05-20 15:37:30 -070039#include <sys/syscall.h>
Christopher Ferris63860cb2015-11-16 17:30:32 -080040#include <unistd.h>
41
Christopher Ferris602b88c2017-08-04 13:04:04 -070042#include <mutex>
Christopher Ferris63860cb2015-11-16 17:30:32 -080043#include <vector>
44
Christopher Ferris602b88c2017-08-04 13:04:04 -070045#include <android-base/file.h>
Christopher Ferris2e1a40a2018-06-13 10:46:34 -070046#include <android-base/properties.h>
Christopher Ferris602b88c2017-08-04 13:04:04 -070047#include <android-base/stringprintf.h>
Mitch Phillips3b21ada2020-01-07 15:47:47 -080048#include <bionic/malloc_tagged_pointers.h>
Christopher Ferris9bf78172020-05-20 15:37:30 -070049#include <platform/bionic/reserved_signals.h>
Christopher Ferris6c619a02019-03-01 17:59:51 -080050#include <private/MallocXmlElem.h>
Christopher Ferris9bf78172020-05-20 15:37:30 -070051#include <private/bionic_malloc_dispatch.h>
Christopher Ferris459eecb2022-01-07 13:38:10 -080052#include <unwindstack/Unwinder.h>
Christopher Ferris63860cb2015-11-16 17:30:32 -080053
Christopher Ferris72df6702016-02-11 15:51:31 -080054#include "Config.h"
Christopher Ferris63860cb2015-11-16 17:30:32 -080055#include "DebugData.h"
Christopher Ferris5610d5a2023-11-14 15:04:50 -080056#include "LogAllocatorStats.h"
Christopher Ferrise39602c2024-11-02 05:02:43 +000057#include "Nanotime.h"
Christopher Ferrisb42e8b42022-05-09 14:00:47 -070058#include "Unreachable.h"
59#include "UnwindBacktrace.h"
Christopher Ferris4da25032018-03-07 13:38:48 -080060#include "backtrace.h"
Christopher Ferris63860cb2015-11-16 17:30:32 -080061#include "debug_disable.h"
62#include "debug_log.h"
63#include "malloc_debug.h"
64
65// ------------------------------------------------------------------------
66// Global Data
67// ------------------------------------------------------------------------
68DebugData* g_debug;
69
Christopher Ferris8189e772019-04-09 16:37:23 -070070bool* g_zygote_child;
Christopher Ferris63860cb2015-11-16 17:30:32 -080071
72const MallocDispatch* g_dispatch;
Chia-hung Duanf7e8b172022-11-01 21:37:56 +000073
Chia-hung Duanf7e8b172022-11-01 21:37:56 +000074namespace {
75// A TimedResult contains the result of from malloc end_ns al. functions and the
76// start/end timestamps.
77struct TimedResult {
78 uint64_t start_ns = 0;
79 uint64_t end_ns = 0;
80 union {
81 size_t s;
82 int i;
83 void* p;
84 } v;
85
86 uint64_t GetStartTimeNS() const { return start_ns; }
87 uint64_t GetEndTimeNS() const { return end_ns; }
88 void SetStartTimeNS(uint64_t t) { start_ns = t; }
89 void SetEndTimeNS(uint64_t t) { end_ns = t; }
90
91 template <typename T>
92 void setValue(T);
93 template <>
94 void setValue(size_t s) {
95 v.s = s;
96 }
97 template <>
98 void setValue(int i) {
99 v.i = i;
100 }
101 template <>
102 void setValue(void* p) {
103 v.p = p;
104 }
105
106 template <typename T>
107 T getValue() const;
108 template <>
109 size_t getValue<size_t>() const {
110 return v.s;
111 }
112 template <>
113 int getValue<int>() const {
114 return v.i;
115 }
116 template <>
117 void* getValue<void*>() const {
118 return v.p;
119 }
120};
121
122class ScopedTimer {
123 public:
124 ScopedTimer(TimedResult& res) : res_(res) { res_.start_ns = Nanotime(); }
125
126 ~ScopedTimer() { res_.end_ns = Nanotime(); }
127
128 private:
129 TimedResult& res_;
130};
131
132} // namespace
133
134template <typename MallocFn, typename... Args>
135static TimedResult TimerCall(MallocFn fn, Args... args) {
136 TimedResult ret;
137 decltype((g_dispatch->*fn)(args...)) r;
138 if (g_debug->config().options() & RECORD_ALLOCS) {
139 ScopedTimer t(ret);
140 r = (g_dispatch->*fn)(args...);
141 } else {
142 r = (g_dispatch->*fn)(args...);
143 }
144 ret.setValue<decltype(r)>(r);
145 return ret;
146}
147
148template <typename MallocFn, typename... Args>
149static TimedResult TimerCallVoid(MallocFn fn, Args... args) {
150 TimedResult ret;
151 {
152 ScopedTimer t(ret);
153 (g_dispatch->*fn)(args...);
154 }
155 return ret;
156}
157
158#define TCALL(FUNC, ...) TimerCall(&MallocDispatch::FUNC, __VA_ARGS__);
159#define TCALLVOID(FUNC, ...) TimerCallVoid(&MallocDispatch::FUNC, __VA_ARGS__);
160
Christopher Ferris63860cb2015-11-16 17:30:32 -0800161// ------------------------------------------------------------------------
162
163// ------------------------------------------------------------------------
164// Use C style prototypes for all exported functions. This makes it easy
165// to do dlsym lookups during libc initialization when malloc debug
166// is enabled.
167// ------------------------------------------------------------------------
168__BEGIN_DECLS
169
Christopher Ferris8189e772019-04-09 16:37:23 -0700170bool debug_initialize(const MallocDispatch* malloc_dispatch, bool* malloc_zygote_child,
Christopher Ferris4da25032018-03-07 13:38:48 -0800171 const char* options);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800172void debug_finalize();
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700173void debug_dump_heap(const char* file_name);
Christopher Ferris4da25032018-03-07 13:38:48 -0800174void debug_get_malloc_leak_info(uint8_t** info, size_t* overall_size, size_t* info_size,
175 size_t* total_memory, size_t* backtrace_size);
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700176bool debug_write_malloc_leak_info(FILE* fp);
Colin Cross2d4721c2016-02-02 11:57:54 -0800177ssize_t debug_malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800178void debug_free_malloc_leak_info(uint8_t* info);
179size_t debug_malloc_usable_size(void* pointer);
180void* debug_malloc(size_t size);
181void debug_free(void* pointer);
Christopher Ferriscae21a92018-02-05 18:14:55 -0800182void* debug_aligned_alloc(size_t alignment, size_t size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800183void* debug_memalign(size_t alignment, size_t bytes);
184void* debug_realloc(void* pointer, size_t bytes);
185void* debug_calloc(size_t nmemb, size_t bytes);
186struct mallinfo debug_mallinfo();
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700187int debug_mallopt(int param, int value);
Christopher Ferris6c619a02019-03-01 17:59:51 -0800188int debug_malloc_info(int options, FILE* fp);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800189int debug_posix_memalign(void** memptr, size_t alignment, size_t size);
Christopher Ferris6f517cd2019-11-08 11:28:38 -0800190int debug_malloc_iterate(uintptr_t base, size_t size,
191 void (*callback)(uintptr_t base, size_t size, void* arg), void* arg);
Colin Cross869691c2016-01-29 12:48:18 -0800192void debug_malloc_disable();
193void debug_malloc_enable();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800194
195#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
196void* debug_pvalloc(size_t bytes);
197void* debug_valloc(size_t size);
198#endif
199
200__END_DECLS
201// ------------------------------------------------------------------------
202
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700203class ScopedConcurrentLock {
204 public:
205 ScopedConcurrentLock() {
206 pthread_rwlock_rdlock(&lock_);
207 }
208 ~ScopedConcurrentLock() {
209 pthread_rwlock_unlock(&lock_);
210 }
211
212 static void Init() {
213 pthread_rwlockattr_t attr;
214 // Set the attribute so that when a write lock is pending, read locks are no
215 // longer granted.
216 pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
217 pthread_rwlock_init(&lock_, &attr);
218 }
219
220 static void BlockAllOperations() {
221 pthread_rwlock_wrlock(&lock_);
222 }
223
224 private:
225 static pthread_rwlock_t lock_;
226};
227pthread_rwlock_t ScopedConcurrentLock::lock_;
228
Christopher Ferris9bf78172020-05-20 15:37:30 -0700229// Use this because the sigprocmask* functions filter out the reserved bionic
230// signals including the signal this code blocks.
231static inline int __rt_sigprocmask(int how, const sigset64_t* new_set, sigset64_t* old_set,
232 size_t sigset_size) {
233 return syscall(SYS_rt_sigprocmask, how, new_set, old_set, sigset_size);
234}
235
236// Need to block the backtrace signal while in malloc debug routines
237// otherwise there is a chance of a deadlock and timeout when unwinding.
238// This can occur if a thread is paused while owning a malloc debug
239// internal lock.
240class ScopedBacktraceSignalBlocker {
241 public:
242 ScopedBacktraceSignalBlocker() {
243 sigemptyset64(&backtrace_set_);
244 sigaddset64(&backtrace_set_, BIONIC_SIGNAL_BACKTRACE);
245 sigset64_t old_set;
246 __rt_sigprocmask(SIG_BLOCK, &backtrace_set_, &old_set, sizeof(backtrace_set_));
247 if (sigismember64(&old_set, BIONIC_SIGNAL_BACKTRACE)) {
248 unblock_ = false;
249 }
250 }
251
252 ~ScopedBacktraceSignalBlocker() {
253 if (unblock_) {
254 __rt_sigprocmask(SIG_UNBLOCK, &backtrace_set_, nullptr, sizeof(backtrace_set_));
255 }
256 }
257
258 private:
259 bool unblock_ = true;
260 sigset64_t backtrace_set_;
261};
262
Colin Cross7a28a3c2016-02-07 22:51:15 -0800263static void InitAtfork() {
264 static pthread_once_t atfork_init = PTHREAD_ONCE_INIT;
Christopher Ferris4da25032018-03-07 13:38:48 -0800265 pthread_once(&atfork_init, []() {
Colin Cross7a28a3c2016-02-07 22:51:15 -0800266 pthread_atfork(
Christopher Ferris4da25032018-03-07 13:38:48 -0800267 []() {
Colin Cross7a28a3c2016-02-07 22:51:15 -0800268 if (g_debug != nullptr) {
269 g_debug->PrepareFork();
270 }
271 },
Christopher Ferris4da25032018-03-07 13:38:48 -0800272 []() {
Colin Cross7a28a3c2016-02-07 22:51:15 -0800273 if (g_debug != nullptr) {
274 g_debug->PostForkParent();
275 }
276 },
Christopher Ferris4da25032018-03-07 13:38:48 -0800277 []() {
Colin Cross7a28a3c2016-02-07 22:51:15 -0800278 if (g_debug != nullptr) {
279 g_debug->PostForkChild();
280 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800281 });
Colin Cross7a28a3c2016-02-07 22:51:15 -0800282 });
283}
Christopher Ferrisd0919622016-03-15 22:39:39 -0700284
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700285void BacktraceAndLog() {
286 if (g_debug->config().options() & BACKTRACE_FULL) {
287 std::vector<uintptr_t> frames;
Christopher Ferris459eecb2022-01-07 13:38:10 -0800288 std::vector<unwindstack::FrameData> frames_info;
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700289 if (!Unwind(&frames, &frames_info, 256)) {
290 error_log(" Backtrace failed to get any frames.");
291 } else {
292 UnwindLog(frames_info);
293 }
294 } else {
295 std::vector<uintptr_t> frames(256);
296 size_t num_frames = backtrace_get(frames.data(), frames.size());
297 if (num_frames == 0) {
298 error_log(" Backtrace failed to get any frames.");
299 } else {
300 backtrace_log(frames.data(), num_frames);
301 }
302 }
303}
304
Christopher Ferris4da25032018-03-07 13:38:48 -0800305static void LogError(const void* pointer, const char* error_str) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800306 error_log(LOG_DIVIDER);
Christopher Ferris4da25032018-03-07 13:38:48 -0800307 error_log("+++ ALLOCATION %p %s", pointer, error_str);
308
309 // If we are tracking already freed pointers, check to see if this is
310 // one so we can print extra information.
311 if (g_debug->config().options() & FREE_TRACK) {
312 PointerData::LogFreeBacktrace(pointer);
Christopher Ferris7993b802016-01-28 18:35:05 -0800313 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800314
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700315 error_log("Backtrace at time of failure:");
316 BacktraceAndLog();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800317 error_log(LOG_DIVIDER);
Iris Chang7f209a92019-01-16 11:17:15 +0800318 if (g_debug->config().options() & ABORT_ON_ERROR) {
319 abort();
320 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800321}
322
Christopher Ferris4da25032018-03-07 13:38:48 -0800323static bool VerifyPointer(const void* pointer, const char* function_name) {
324 if (g_debug->HeaderEnabled()) {
325 Header* header = g_debug->GetHeader(pointer);
326 if (header->tag != DEBUG_TAG) {
327 std::string error_str;
328 if (header->tag == DEBUG_FREE_TAG) {
329 error_str = std::string("USED AFTER FREE (") + function_name + ")";
330 } else {
331 error_str = android::base::StringPrintf("HAS INVALID TAG %" PRIx32 " (%s)", header->tag,
332 function_name);
333 }
334 LogError(pointer, error_str.c_str());
335 return false;
336 }
337 }
338
339 if (g_debug->TrackPointers()) {
340 if (!PointerData::Exists(pointer)) {
341 std::string error_str(std::string("UNKNOWN POINTER (") + function_name + ")");
342 LogError(pointer, error_str.c_str());
343 return false;
344 }
345 }
346 return true;
347}
348
349static size_t InternalMallocUsableSize(void* pointer) {
350 if (g_debug->HeaderEnabled()) {
351 return g_debug->GetHeader(pointer)->usable_size;
352 } else {
353 return g_dispatch->malloc_usable_size(pointer);
354 }
355}
356
Christopher Ferris63860cb2015-11-16 17:30:32 -0800357static void* InitHeader(Header* header, void* orig_pointer, size_t size) {
358 header->tag = DEBUG_TAG;
359 header->orig_pointer = orig_pointer;
360 header->size = size;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800361 header->usable_size = g_dispatch->malloc_usable_size(orig_pointer);
362 if (header->usable_size == 0) {
363 g_dispatch->free(orig_pointer);
364 return nullptr;
365 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800366 header->usable_size -= g_debug->pointer_offset() + reinterpret_cast<uintptr_t>(header) -
367 reinterpret_cast<uintptr_t>(orig_pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800368
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700369 if (g_debug->config().options() & FRONT_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800370 uint8_t* guard = g_debug->GetFrontGuard(header);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700371 memset(guard, g_debug->config().front_guard_value(), g_debug->config().front_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800372 }
373
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700374 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800375 uint8_t* guard = g_debug->GetRearGuard(header);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700376 memset(guard, g_debug->config().rear_guard_value(), g_debug->config().rear_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800377 // If the rear guard is enabled, set the usable size to the exact size
378 // of the allocation.
Christopher Ferris4da25032018-03-07 13:38:48 -0800379 header->usable_size = header->size;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800380 }
381
382 return g_debug->GetPointer(header);
383}
384
Christopher Ferris705de3c2019-05-22 13:39:57 -0700385extern "C" void __asan_init() __attribute__((weak));
386
Christopher Ferris8189e772019-04-09 16:37:23 -0700387bool debug_initialize(const MallocDispatch* malloc_dispatch, bool* zygote_child,
Christopher Ferris4da25032018-03-07 13:38:48 -0800388 const char* options) {
Christopher Ferris8189e772019-04-09 16:37:23 -0700389 if (zygote_child == nullptr || options == nullptr) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800390 return false;
391 }
Colin Cross7a28a3c2016-02-07 22:51:15 -0800392
Christopher Ferris705de3c2019-05-22 13:39:57 -0700393 if (__asan_init != 0) {
394 error_log("malloc debug cannot be enabled alongside ASAN");
395 return false;
396 }
397
Colin Cross7a28a3c2016-02-07 22:51:15 -0800398 InitAtfork();
399
Christopher Ferris8189e772019-04-09 16:37:23 -0700400 g_zygote_child = zygote_child;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800401
402 g_dispatch = malloc_dispatch;
403
404 if (!DebugDisableInitialize()) {
405 return false;
406 }
407
408 DebugData* debug = new DebugData();
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700409 if (!debug->Initialize(options) || !Unreachable::Initialize(debug->config())) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800410 delete debug;
411 DebugDisableFinalize();
412 return false;
413 }
414 g_debug = debug;
415
416 // Always enable the backtrace code since we will use it in a number
417 // of different error cases.
418 backtrace_startup();
419
Christopher Ferrisc328e442019-04-01 19:31:26 -0700420 if (g_debug->config().options() & VERBOSE) {
421 info_log("%s: malloc debug enabled", getprogname());
422 }
423
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700424 ScopedConcurrentLock::Init();
425
Christopher Ferris63860cb2015-11-16 17:30:32 -0800426 return true;
427}
428
429void debug_finalize() {
430 if (g_debug == nullptr) {
431 return;
432 }
433
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700434 // Make sure that there are no other threads doing debug allocations
435 // before we kill everything.
436 ScopedConcurrentLock::BlockAllOperations();
437
Christopher Ferris97b47472018-07-10 14:45:24 -0700438 // Turn off capturing allocations calls.
439 DebugDisableSet(true);
440
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700441 if (g_debug->config().options() & FREE_TRACK) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800442 PointerData::VerifyAllFreed();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800443 }
444
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700445 if (g_debug->config().options() & LEAK_TRACK) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800446 PointerData::LogLeaks();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800447 }
448
Christopher Ferris9b88d0a2024-06-13 13:22:02 -0700449 if ((g_debug->config().options() & RECORD_ALLOCS) && g_debug->config().record_allocs_on_exit()) {
450 RecordData::WriteEntriesOnExit();
451 }
452
Christopher Ferris602b88c2017-08-04 13:04:04 -0700453 if ((g_debug->config().options() & BACKTRACE) && g_debug->config().backtrace_dump_on_exit()) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800454 debug_dump_heap(android::base::StringPrintf("%s.%d.exit.txt",
455 g_debug->config().backtrace_dump_prefix().c_str(),
Christopher Ferris97b47472018-07-10 14:45:24 -0700456 getpid()).c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700457 }
458
Christopher Ferrisc1341e12024-07-11 19:48:08 -0700459 if (g_debug->config().options() & LOG_ALLOCATOR_STATS_ON_EXIT) {
460 LogAllocatorStats::Log();
461 }
462
Colin Cross2c759912016-02-05 16:17:39 -0800463 backtrace_shutdown();
464
Christopher Ferris33d73372021-07-02 15:46:18 -0700465 // In order to prevent any issues of threads freeing previous pointers
466 // after the main thread calls this code, simply leak the g_debug pointer
467 // and do not destroy the debug disable pthread key.
Christopher Ferris63860cb2015-11-16 17:30:32 -0800468}
469
Christopher Ferris4da25032018-03-07 13:38:48 -0800470void debug_get_malloc_leak_info(uint8_t** info, size_t* overall_size, size_t* info_size,
471 size_t* total_memory, size_t* backtrace_size) {
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700472 ScopedConcurrentLock lock;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800473 ScopedDisableDebugCalls disable;
Christopher Ferris9bf78172020-05-20 15:37:30 -0700474 ScopedBacktraceSignalBlocker blocked;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800475
476 // Verify the arguments.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700477 if (info == nullptr || overall_size == nullptr || info_size == nullptr || total_memory == nullptr ||
Christopher Ferris4da25032018-03-07 13:38:48 -0800478 backtrace_size == nullptr) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800479 error_log("get_malloc_leak_info: At least one invalid parameter.");
480 return;
481 }
482
483 *info = nullptr;
484 *overall_size = 0;
485 *info_size = 0;
486 *total_memory = 0;
487 *backtrace_size = 0;
488
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700489 if (!(g_debug->config().options() & BACKTRACE)) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800490 error_log(
491 "get_malloc_leak_info: Allocations not being tracked, to enable "
492 "set the option 'backtrace'.");
Christopher Ferris63860cb2015-11-16 17:30:32 -0800493 return;
494 }
495
Christopher Ferris4da25032018-03-07 13:38:48 -0800496 PointerData::GetInfo(info, overall_size, info_size, total_memory, backtrace_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800497}
498
499void debug_free_malloc_leak_info(uint8_t* info) {
500 g_dispatch->free(info);
Christopher Ferrisaa3e5742023-01-31 01:31:52 +0000501 // Purge the memory that was freed since a significant amount of
502 // memory could have been allocated and freed.
Christopher Ferrisf1ab9c42023-03-22 13:47:27 -0700503 g_dispatch->mallopt(M_PURGE_ALL, 0);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800504}
505
Christopher Ferris55a89a42016-04-07 17:14:53 -0700506size_t debug_malloc_usable_size(void* pointer) {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700507 Unreachable::CheckIfRequested(g_debug->config());
508
Christopher Ferris55a89a42016-04-07 17:14:53 -0700509 if (DebugCallsDisabled() || pointer == nullptr) {
510 return g_dispatch->malloc_usable_size(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800511 }
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700512 ScopedConcurrentLock lock;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700513 ScopedDisableDebugCalls disable;
Christopher Ferris9bf78172020-05-20 15:37:30 -0700514 ScopedBacktraceSignalBlocker blocked;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800515
Christopher Ferris4da25032018-03-07 13:38:48 -0800516 if (!VerifyPointer(pointer, "malloc_usable_size")) {
517 return 0;
518 }
519
520 return InternalMallocUsableSize(pointer);
Christopher Ferris55a89a42016-04-07 17:14:53 -0700521}
522
Chia-hung Duanf7e8b172022-11-01 21:37:56 +0000523static TimedResult InternalMalloc(size_t size) {
Christopher Ferris5610d5a2023-11-14 15:04:50 -0800524 uint64_t options = g_debug->config().options();
525 if ((options & BACKTRACE) && g_debug->pointer->ShouldDumpAndReset()) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800526 debug_dump_heap(android::base::StringPrintf(
527 "%s.%d.txt", g_debug->config().backtrace_dump_prefix().c_str(), getpid())
528 .c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700529 }
Christopher Ferris5610d5a2023-11-14 15:04:50 -0800530 if (options & LOG_ALLOCATOR_STATS_ON_SIGNAL) {
531 LogAllocatorStats::CheckIfShouldLog();
532 }
Christopher Ferris602b88c2017-08-04 13:04:04 -0700533
Colin Cross9567c7b2016-03-09 17:56:14 -0800534 if (size == 0) {
535 size = 1;
536 }
537
Chia-hung Duanf7e8b172022-11-01 21:37:56 +0000538 TimedResult result;
539
Christopher Ferris63860cb2015-11-16 17:30:32 -0800540 size_t real_size = size + g_debug->extra_bytes();
541 if (real_size < size) {
542 // Overflow.
543 errno = ENOMEM;
Chia-hung Duanf7e8b172022-11-01 21:37:56 +0000544 result.setValue<void*>(nullptr);
545 return result;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800546 }
547
Christopher Ferris4da25032018-03-07 13:38:48 -0800548 if (size > PointerInfoType::MaxSize()) {
549 errno = ENOMEM;
Chia-hung Duanf7e8b172022-11-01 21:37:56 +0000550 result.setValue<void*>(nullptr);
551 return result;
Christopher Ferris4da25032018-03-07 13:38:48 -0800552 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800553
Christopher Ferris4da25032018-03-07 13:38:48 -0800554 if (g_debug->HeaderEnabled()) {
Chia-hung Duanf7e8b172022-11-01 21:37:56 +0000555 result = TCALL(memalign, MINIMUM_ALIGNMENT_BYTES, real_size);
556 Header* header = reinterpret_cast<Header*>(result.getValue<void*>());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800557 if (header == nullptr) {
Chia-hung Duanf7e8b172022-11-01 21:37:56 +0000558 return result;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800559 }
Chia-hung Duanf7e8b172022-11-01 21:37:56 +0000560 result.setValue<void*>(InitHeader(header, header, size));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800561 } else {
Chia-hung Duanf7e8b172022-11-01 21:37:56 +0000562 result = TCALL(malloc, real_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800563 }
564
Chia-hung Duanf7e8b172022-11-01 21:37:56 +0000565 void* pointer = result.getValue<void*>();
566
Christopher Ferris4da25032018-03-07 13:38:48 -0800567 if (pointer != nullptr) {
568 if (g_debug->TrackPointers()) {
569 PointerData::Add(pointer, size);
570 }
571
572 if (g_debug->config().options() & FILL_ON_ALLOC) {
573 size_t bytes = InternalMallocUsableSize(pointer);
574 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes();
575 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
576 memset(pointer, g_debug->config().fill_alloc_value(), bytes);
577 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800578 }
Chia-hung Duanf7e8b172022-11-01 21:37:56 +0000579
580 return result;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800581}
582
Christopher Ferris55a89a42016-04-07 17:14:53 -0700583void* debug_malloc(size_t size) {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700584 Unreachable::CheckIfRequested(g_debug->config());
585
Christopher Ferris55a89a42016-04-07 17:14:53 -0700586 if (DebugCallsDisabled()) {
587 return g_dispatch->malloc(size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800588 }
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700589 ScopedConcurrentLock lock;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700590 ScopedDisableDebugCalls disable;
Christopher Ferris9bf78172020-05-20 15:37:30 -0700591 ScopedBacktraceSignalBlocker blocked;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800592
Christopher Ferrisf756e4c2024-11-19 04:28:20 +0000593 memory_trace::Entry* entry = nullptr;
594 if (g_debug->config().options() & RECORD_ALLOCS) {
595 // In order to preserve the order of operations, reserve the entry before
596 // performing the operation.
597 entry = g_debug->record->ReserveEntry();
598 }
599
Chia-hung Duanf7e8b172022-11-01 21:37:56 +0000600 TimedResult result = InternalMalloc(size);
Christopher Ferris7bd01782016-04-20 12:30:58 -0700601
Christopher Ferrisf756e4c2024-11-19 04:28:20 +0000602 if (entry != nullptr) {
603 *entry = memory_trace::Entry{.tid = gettid(),
604 .type = memory_trace::MALLOC,
605 .ptr = reinterpret_cast<uint64_t>(result.getValue<void*>()),
606 .size = size,
607 .start_ns = result.GetStartTimeNS(),
608 .end_ns = result.GetEndTimeNS()};
Christopher Ferris7bd01782016-04-20 12:30:58 -0700609 }
610
Chia-hung Duanf7e8b172022-11-01 21:37:56 +0000611 return result.getValue<void*>();
Christopher Ferris55a89a42016-04-07 17:14:53 -0700612}
613
Chia-hung Duanf7e8b172022-11-01 21:37:56 +0000614static TimedResult InternalFree(void* pointer) {
Christopher Ferris5610d5a2023-11-14 15:04:50 -0800615 uint64_t options = g_debug->config().options();
616 if ((options & BACKTRACE) && g_debug->pointer->ShouldDumpAndReset()) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800617 debug_dump_heap(android::base::StringPrintf(
618 "%s.%d.txt", g_debug->config().backtrace_dump_prefix().c_str(), getpid())
619 .c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700620 }
Christopher Ferris5610d5a2023-11-14 15:04:50 -0800621 if (options & LOG_ALLOCATOR_STATS_ON_SIGNAL) {
622 LogAllocatorStats::CheckIfShouldLog();
623 }
Christopher Ferris602b88c2017-08-04 13:04:04 -0700624
Christopher Ferris63860cb2015-11-16 17:30:32 -0800625 void* free_pointer = pointer;
626 size_t bytes;
Christopher Ferrisd0919622016-03-15 22:39:39 -0700627 Header* header;
Christopher Ferris4da25032018-03-07 13:38:48 -0800628 if (g_debug->HeaderEnabled()) {
Christopher Ferrisd0919622016-03-15 22:39:39 -0700629 header = g_debug->GetHeader(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800630 free_pointer = header->orig_pointer;
631
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700632 if (g_debug->config().options() & FRONT_GUARD) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700633 if (!g_debug->front_guard->Valid(header)) {
634 g_debug->front_guard->LogFailure(header);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800635 }
636 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700637 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700638 if (!g_debug->rear_guard->Valid(header)) {
639 g_debug->rear_guard->LogFailure(header);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800640 }
641 }
642
Christopher Ferris7993b802016-01-28 18:35:05 -0800643 header->tag = DEBUG_FREE_TAG;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800644
645 bytes = header->usable_size;
646 } else {
647 bytes = g_dispatch->malloc_usable_size(pointer);
648 }
649
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700650 if (g_debug->config().options() & FILL_ON_FREE) {
651 size_t fill_bytes = g_debug->config().fill_on_free_bytes();
Christopher Ferrisa3836482022-05-13 12:09:39 -0700652 fill_bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
653 memset(pointer, g_debug->config().fill_free_value(), fill_bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800654 }
655
Christopher Ferris4da25032018-03-07 13:38:48 -0800656 if (g_debug->TrackPointers()) {
657 PointerData::Remove(pointer);
658 }
659
Chia-hung Duanf7e8b172022-11-01 21:37:56 +0000660 TimedResult result;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700661 if (g_debug->config().options() & FREE_TRACK) {
Christopher Ferrisd0919622016-03-15 22:39:39 -0700662 // Do not add the allocation until we are done modifying the pointer
663 // itself. This avoids a race if a lot of threads are all doing
664 // frees at the same time and we wind up trying to really free this
665 // pointer from another thread, while still trying to free it in
666 // this function.
Christopher Ferrisa3836482022-05-13 12:09:39 -0700667 pointer = PointerData::AddFreed(pointer, bytes);
Chia-hung Duanf7e8b172022-11-01 21:37:56 +0000668 if (pointer != nullptr && g_debug->HeaderEnabled()) {
669 pointer = g_debug->GetHeader(pointer)->orig_pointer;
Christopher Ferris4da25032018-03-07 13:38:48 -0800670 }
Chia-hung Duanf7e8b172022-11-01 21:37:56 +0000671 result = TCALLVOID(free, pointer);
Christopher Ferrisd0919622016-03-15 22:39:39 -0700672 } else {
Chia-hung Duanf7e8b172022-11-01 21:37:56 +0000673 result = TCALLVOID(free, free_pointer);
Christopher Ferrisd0919622016-03-15 22:39:39 -0700674 }
Chia-hung Duanf7e8b172022-11-01 21:37:56 +0000675
676 return result;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800677}
678
Christopher Ferris55a89a42016-04-07 17:14:53 -0700679void debug_free(void* pointer) {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700680 Unreachable::CheckIfRequested(g_debug->config());
681
Christopher Ferris55a89a42016-04-07 17:14:53 -0700682 if (DebugCallsDisabled() || pointer == nullptr) {
683 return g_dispatch->free(pointer);
684 }
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700685 ScopedConcurrentLock lock;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700686 ScopedDisableDebugCalls disable;
Christopher Ferris9bf78172020-05-20 15:37:30 -0700687 ScopedBacktraceSignalBlocker blocked;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700688
Christopher Ferris4da25032018-03-07 13:38:48 -0800689 if (!VerifyPointer(pointer, "free")) {
690 return;
691 }
692
Christopher Ferrisf756e4c2024-11-19 04:28:20 +0000693 memory_trace::Entry* entry = nullptr;
694 if (g_debug->config().options() & RECORD_ALLOCS) {
695 // In order to preserve the order of operations, reserve the entry before
696 // performing the operation.
697 entry = g_debug->record->ReserveEntry();
698 }
699
Chia-hung Duanf7e8b172022-11-01 21:37:56 +0000700 TimedResult result = InternalFree(pointer);
701
Christopher Ferrisf756e4c2024-11-19 04:28:20 +0000702 if (entry != nullptr) {
703 *entry = memory_trace::Entry{.tid = gettid(),
704 .type = memory_trace::FREE,
705 .ptr = reinterpret_cast<uint64_t>(pointer),
706 .start_ns = result.GetStartTimeNS(),
707 .end_ns = result.GetEndTimeNS()};
Chia-hung Duanf7e8b172022-11-01 21:37:56 +0000708 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700709}
710
Christopher Ferris63860cb2015-11-16 17:30:32 -0800711void* debug_memalign(size_t alignment, size_t bytes) {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700712 Unreachable::CheckIfRequested(g_debug->config());
713
Christopher Ferris63860cb2015-11-16 17:30:32 -0800714 if (DebugCallsDisabled()) {
715 return g_dispatch->memalign(alignment, bytes);
716 }
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700717 ScopedConcurrentLock lock;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700718 ScopedDisableDebugCalls disable;
Christopher Ferris9bf78172020-05-20 15:37:30 -0700719 ScopedBacktraceSignalBlocker blocked;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800720
Colin Cross9567c7b2016-03-09 17:56:14 -0800721 if (bytes == 0) {
722 bytes = 1;
723 }
724
Christopher Ferris4da25032018-03-07 13:38:48 -0800725 if (bytes > PointerInfoType::MaxSize()) {
726 errno = ENOMEM;
727 return nullptr;
728 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800729
Christopher Ferrisf756e4c2024-11-19 04:28:20 +0000730 memory_trace::Entry* entry = nullptr;
731 if (g_debug->config().options() & RECORD_ALLOCS) {
732 // In order to preserve the order of operations, reserve the entry before
733 // performing the operation.
734 entry = g_debug->record->ReserveEntry();
735 }
736
Chia-hung Duanf7e8b172022-11-01 21:37:56 +0000737 TimedResult result;
Christopher Ferris4da25032018-03-07 13:38:48 -0800738 void* pointer;
739 if (g_debug->HeaderEnabled()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800740 // Make the alignment a power of two.
741 if (!powerof2(alignment)) {
742 alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment);
743 }
Christopher Ferris72df6702016-02-11 15:51:31 -0800744 // Force the alignment to at least MINIMUM_ALIGNMENT_BYTES to guarantee
Christopher Ferris63860cb2015-11-16 17:30:32 -0800745 // that the header is aligned properly.
Christopher Ferris72df6702016-02-11 15:51:31 -0800746 if (alignment < MINIMUM_ALIGNMENT_BYTES) {
747 alignment = MINIMUM_ALIGNMENT_BYTES;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800748 }
749
750 // We don't have any idea what the natural alignment of
751 // the underlying native allocator is, so we always need to
752 // over allocate.
753 size_t real_size = alignment + bytes + g_debug->extra_bytes();
754 if (real_size < bytes) {
755 // Overflow.
756 errno = ENOMEM;
757 return nullptr;
758 }
759
Chia-hung Duanf7e8b172022-11-01 21:37:56 +0000760 result = TCALL(malloc, real_size);
761 pointer = result.getValue<void*>();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800762 if (pointer == nullptr) {
763 return nullptr;
764 }
765
766 uintptr_t value = reinterpret_cast<uintptr_t>(pointer) + g_debug->pointer_offset();
767 // Now align the pointer.
768 value += (-value % alignment);
769
770 Header* header = g_debug->GetHeader(reinterpret_cast<void*>(value));
Chia-hung Duanf7e8b172022-11-01 21:37:56 +0000771 // Don't need to update `result` here because we only need the timestamps.
Christopher Ferris63860cb2015-11-16 17:30:32 -0800772 pointer = InitHeader(header, pointer, bytes);
773 } else {
774 size_t real_size = bytes + g_debug->extra_bytes();
775 if (real_size < bytes) {
776 // Overflow.
777 errno = ENOMEM;
778 return nullptr;
779 }
Chia-hung Duanf7e8b172022-11-01 21:37:56 +0000780 result = TCALL(memalign, alignment, real_size);
781 pointer = result.getValue<void*>();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800782 }
783
Christopher Ferrisf756e4c2024-11-19 04:28:20 +0000784 if (pointer == nullptr) {
785 return nullptr;
786 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700787
Christopher Ferrisf756e4c2024-11-19 04:28:20 +0000788 if (g_debug->TrackPointers()) {
789 PointerData::Add(pointer, bytes);
790 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800791
Christopher Ferrisf756e4c2024-11-19 04:28:20 +0000792 if (g_debug->config().options() & FILL_ON_ALLOC) {
793 size_t bytes = InternalMallocUsableSize(pointer);
794 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes();
795 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
796 memset(pointer, g_debug->config().fill_alloc_value(), bytes);
797 }
798
799 if (entry != nullptr) {
800 *entry = memory_trace::Entry{.tid = gettid(),
801 .type = memory_trace::MEMALIGN,
802 .ptr = reinterpret_cast<uint64_t>(pointer),
803 .size = bytes,
804 .u.align = alignment,
805 .start_ns = result.GetStartTimeNS(),
806 .end_ns = result.GetEndTimeNS()};
Christopher Ferris7bd01782016-04-20 12:30:58 -0700807 }
808
Christopher Ferris63860cb2015-11-16 17:30:32 -0800809 return pointer;
810}
811
812void* debug_realloc(void* pointer, size_t bytes) {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700813 Unreachable::CheckIfRequested(g_debug->config());
814
Christopher Ferris63860cb2015-11-16 17:30:32 -0800815 if (DebugCallsDisabled()) {
816 return g_dispatch->realloc(pointer, bytes);
817 }
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700818 ScopedConcurrentLock lock;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700819 ScopedDisableDebugCalls disable;
Christopher Ferris9bf78172020-05-20 15:37:30 -0700820 ScopedBacktraceSignalBlocker blocked;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800821
Christopher Ferrisf756e4c2024-11-19 04:28:20 +0000822 memory_trace::Entry* entry = nullptr;
823 if (g_debug->config().options() & RECORD_ALLOCS) {
824 // In order to preserve the order of operations, reserve the entry before
825 // performing the operation.
826 entry = g_debug->record->ReserveEntry();
827 }
828
Christopher Ferris63860cb2015-11-16 17:30:32 -0800829 if (pointer == nullptr) {
Chia-hung Duanf7e8b172022-11-01 21:37:56 +0000830 TimedResult result = InternalMalloc(bytes);
Chia-hung Duanf7e8b172022-11-01 21:37:56 +0000831 pointer = result.getValue<void*>();
Christopher Ferrisf756e4c2024-11-19 04:28:20 +0000832 if (entry != nullptr) {
833 *entry = memory_trace::Entry{.tid = gettid(),
834 .type = memory_trace::REALLOC,
835 .ptr = reinterpret_cast<uint64_t>(pointer),
836 .size = bytes,
837 .u.old_ptr = 0,
838 .start_ns = result.GetStartTimeNS(),
839 .end_ns = result.GetEndTimeNS()};
Christopher Ferrise39602c2024-11-02 05:02:43 +0000840 }
Christopher Ferris7bd01782016-04-20 12:30:58 -0700841 return pointer;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800842 }
843
Christopher Ferris4da25032018-03-07 13:38:48 -0800844 if (!VerifyPointer(pointer, "realloc")) {
845 return nullptr;
846 }
847
Christopher Ferris63860cb2015-11-16 17:30:32 -0800848 if (bytes == 0) {
Chia-hung Duanf7e8b172022-11-01 21:37:56 +0000849 TimedResult result = InternalFree(pointer);
850
Christopher Ferrisf756e4c2024-11-19 04:28:20 +0000851 if (entry != nullptr) {
852 *entry = memory_trace::Entry{.tid = gettid(),
853 .type = memory_trace::REALLOC,
854 .ptr = 0,
855 .size = 0,
856 .u.old_ptr = reinterpret_cast<uint64_t>(pointer),
857 .start_ns = result.GetStartTimeNS(),
858 .end_ns = result.GetEndTimeNS()};
Christopher Ferris7bd01782016-04-20 12:30:58 -0700859 }
860
Christopher Ferris63860cb2015-11-16 17:30:32 -0800861 return nullptr;
862 }
863
864 size_t real_size = bytes;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700865 if (g_debug->config().options() & EXPAND_ALLOC) {
866 real_size += g_debug->config().expand_alloc_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800867 if (real_size < bytes) {
868 // Overflow.
869 errno = ENOMEM;
870 return nullptr;
871 }
872 }
873
Christopher Ferris4da25032018-03-07 13:38:48 -0800874 if (bytes > PointerInfoType::MaxSize()) {
875 errno = ENOMEM;
876 return nullptr;
877 }
878
Chia-hung Duanf7e8b172022-11-01 21:37:56 +0000879 TimedResult result;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800880 void* new_pointer;
881 size_t prev_size;
Christopher Ferris4da25032018-03-07 13:38:48 -0800882 if (g_debug->HeaderEnabled()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800883 // Same size, do nothing.
Christopher Ferris4da25032018-03-07 13:38:48 -0800884 Header* header = g_debug->GetHeader(pointer);
885 if (real_size == header->size) {
886 if (g_debug->TrackPointers()) {
887 // Remove and re-add so that the backtrace is updated.
888 PointerData::Remove(pointer);
889 PointerData::Add(pointer, real_size);
890 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800891 return pointer;
892 }
893
894 // Allocation is shrinking.
895 if (real_size < header->usable_size) {
896 header->size = real_size;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700897 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800898 // Don't bother allocating a smaller pointer in this case, simply
899 // change the header usable_size and reset the rear guard.
Christopher Ferris4da25032018-03-07 13:38:48 -0800900 header->usable_size = header->size;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700901 memset(g_debug->GetRearGuard(header), g_debug->config().rear_guard_value(),
902 g_debug->config().rear_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800903 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800904 if (g_debug->TrackPointers()) {
905 // Remove and re-add so that the backtrace is updated.
906 PointerData::Remove(pointer);
907 PointerData::Add(pointer, real_size);
908 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800909 return pointer;
910 }
911
912 // Allocate the new size.
Chia-hung Duanf7e8b172022-11-01 21:37:56 +0000913 result = InternalMalloc(bytes);
914 new_pointer = result.getValue<void*>();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800915 if (new_pointer == nullptr) {
916 errno = ENOMEM;
917 return nullptr;
918 }
919
920 prev_size = header->usable_size;
921 memcpy(new_pointer, pointer, prev_size);
Chia-hung Duanf7e8b172022-11-01 21:37:56 +0000922 TimedResult free_time = InternalFree(pointer);
923 // `realloc` is split into two steps, update the end time to the finish time
924 // of the second operation.
925 result.SetEndTimeNS(free_time.GetEndTimeNS());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800926 } else {
Christopher Ferris4da25032018-03-07 13:38:48 -0800927 if (g_debug->TrackPointers()) {
928 PointerData::Remove(pointer);
929 }
930
Christopher Ferris63860cb2015-11-16 17:30:32 -0800931 prev_size = g_dispatch->malloc_usable_size(pointer);
Chia-hung Duanf7e8b172022-11-01 21:37:56 +0000932 result = TCALL(realloc, pointer, real_size);
933 new_pointer = result.getValue<void*>();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800934 if (new_pointer == nullptr) {
935 return nullptr;
936 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800937
938 if (g_debug->TrackPointers()) {
939 PointerData::Add(new_pointer, real_size);
940 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800941 }
942
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700943 if (g_debug->config().options() & FILL_ON_ALLOC) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800944 size_t bytes = InternalMallocUsableSize(new_pointer);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700945 if (bytes > g_debug->config().fill_on_alloc_bytes()) {
946 bytes = g_debug->config().fill_on_alloc_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800947 }
948 if (bytes > prev_size) {
949 memset(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(new_pointer) + prev_size),
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700950 g_debug->config().fill_alloc_value(), bytes - prev_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800951 }
952 }
953
Christopher Ferrisf756e4c2024-11-19 04:28:20 +0000954 if (entry != nullptr) {
955 *entry = memory_trace::Entry{.tid = gettid(),
956 .type = memory_trace::REALLOC,
957 .ptr = reinterpret_cast<uint64_t>(new_pointer),
958 .size = bytes,
959 .u.old_ptr = reinterpret_cast<uint64_t>(pointer),
960 .start_ns = result.GetStartTimeNS(),
961 .end_ns = result.GetEndTimeNS()};
Christopher Ferris7bd01782016-04-20 12:30:58 -0700962 }
963
Christopher Ferris63860cb2015-11-16 17:30:32 -0800964 return new_pointer;
965}
966
967void* debug_calloc(size_t nmemb, size_t bytes) {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700968 Unreachable::CheckIfRequested(g_debug->config());
969
Christopher Ferris63860cb2015-11-16 17:30:32 -0800970 if (DebugCallsDisabled()) {
971 return g_dispatch->calloc(nmemb, bytes);
972 }
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700973 ScopedConcurrentLock lock;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700974 ScopedDisableDebugCalls disable;
Christopher Ferris9bf78172020-05-20 15:37:30 -0700975 ScopedBacktraceSignalBlocker blocked;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800976
Colin Cross7877df62016-03-10 13:01:27 -0800977 size_t size;
978 if (__builtin_mul_overflow(nmemb, bytes, &size)) {
979 // Overflow
980 errno = ENOMEM;
981 return nullptr;
982 }
983
Colin Cross9567c7b2016-03-09 17:56:14 -0800984 if (size == 0) {
985 size = 1;
986 }
987
Colin Cross7877df62016-03-10 13:01:27 -0800988 size_t real_size;
989 if (__builtin_add_overflow(size, g_debug->extra_bytes(), &real_size)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800990 // Overflow.
991 errno = ENOMEM;
992 return nullptr;
993 }
994
Christopher Ferris4da25032018-03-07 13:38:48 -0800995 if (real_size > PointerInfoType::MaxSize()) {
996 errno = ENOMEM;
997 return nullptr;
998 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800999
Christopher Ferrisf756e4c2024-11-19 04:28:20 +00001000 memory_trace::Entry* entry = nullptr;
1001 if (g_debug->config().options() & RECORD_ALLOCS) {
1002 // In order to preserve the order of operations, reserve the entry before
1003 // performing the operation.
1004 entry = g_debug->record->ReserveEntry();
1005 }
1006
Christopher Ferris4da25032018-03-07 13:38:48 -08001007 void* pointer;
Chia-hung Duanf7e8b172022-11-01 21:37:56 +00001008 TimedResult result;
Christopher Ferris4da25032018-03-07 13:38:48 -08001009 if (g_debug->HeaderEnabled()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -08001010 // Need to guarantee the alignment of the header.
Chia-hung Duanf7e8b172022-11-01 21:37:56 +00001011 result = TCALL(memalign, MINIMUM_ALIGNMENT_BYTES, real_size);
1012 Header* header = reinterpret_cast<Header*>(result.getValue<void*>());
Christopher Ferris63860cb2015-11-16 17:30:32 -08001013 if (header == nullptr) {
1014 return nullptr;
1015 }
1016 memset(header, 0, g_dispatch->malloc_usable_size(header));
Christopher Ferris7bd01782016-04-20 12:30:58 -07001017 pointer = InitHeader(header, header, size);
Christopher Ferris63860cb2015-11-16 17:30:32 -08001018 } else {
Chia-hung Duanf7e8b172022-11-01 21:37:56 +00001019 result = TCALL(calloc, 1, real_size);
1020 pointer = result.getValue<void*>();
Christopher Ferris63860cb2015-11-16 17:30:32 -08001021 }
Christopher Ferris4da25032018-03-07 13:38:48 -08001022
Christopher Ferrisf756e4c2024-11-19 04:28:20 +00001023 if (entry != nullptr) {
1024 *entry = memory_trace::Entry{.tid = gettid(),
1025 .type = memory_trace::CALLOC,
1026 .ptr = reinterpret_cast<uint64_t>(pointer),
1027 .size = bytes,
1028 .u.n_elements = nmemb,
1029 .start_ns = result.GetStartTimeNS(),
1030 .end_ns = result.GetEndTimeNS()};
Christopher Ferris7bd01782016-04-20 12:30:58 -07001031 }
Christopher Ferris4da25032018-03-07 13:38:48 -08001032
1033 if (pointer != nullptr && g_debug->TrackPointers()) {
1034 PointerData::Add(pointer, size);
1035 }
Christopher Ferris7bd01782016-04-20 12:30:58 -07001036 return pointer;
Christopher Ferris63860cb2015-11-16 17:30:32 -08001037}
1038
1039struct mallinfo debug_mallinfo() {
1040 return g_dispatch->mallinfo();
1041}
1042
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -07001043int debug_mallopt(int param, int value) {
1044 return g_dispatch->mallopt(param, value);
1045}
1046
Christopher Ferris6c619a02019-03-01 17:59:51 -08001047int debug_malloc_info(int options, FILE* fp) {
1048 if (DebugCallsDisabled() || !g_debug->TrackPointers()) {
1049 return g_dispatch->malloc_info(options, fp);
1050 }
Christopher Ferrisff88fb02019-11-04 18:40:00 -08001051
1052 // Make sure any pending output is written to the file.
1053 fflush(fp);
1054
Christopher Ferrisd269fcc2019-05-06 19:03:59 -07001055 ScopedConcurrentLock lock;
1056 ScopedDisableDebugCalls disable;
Christopher Ferris9bf78172020-05-20 15:37:30 -07001057 ScopedBacktraceSignalBlocker blocked;
Christopher Ferris6c619a02019-03-01 17:59:51 -08001058
Christopher Ferrisff88fb02019-11-04 18:40:00 -08001059 // Avoid any issues where allocations are made that will be freed
1060 // in the fclose.
1061 int fd = fileno(fp);
1062 MallocXmlElem root(fd, "malloc", "version=\"debug-malloc-1\"");
Christopher Ferris6c619a02019-03-01 17:59:51 -08001063 std::vector<ListInfoType> list;
1064 PointerData::GetAllocList(&list);
1065
1066 size_t alloc_num = 0;
1067 for (size_t i = 0; i < list.size(); i++) {
Christopher Ferrisff88fb02019-11-04 18:40:00 -08001068 MallocXmlElem alloc(fd, "allocation", "nr=\"%zu\"", alloc_num);
Christopher Ferris6c619a02019-03-01 17:59:51 -08001069
1070 size_t total = 1;
1071 size_t size = list[i].size;
1072 while (i < list.size() - 1 && list[i + 1].size == size) {
1073 i++;
1074 total++;
1075 }
Christopher Ferrisff88fb02019-11-04 18:40:00 -08001076 MallocXmlElem(fd, "size").Contents("%zu", list[i].size);
1077 MallocXmlElem(fd, "total").Contents("%zu", total);
Christopher Ferris6c619a02019-03-01 17:59:51 -08001078 alloc_num++;
1079 }
1080 return 0;
1081}
1082
Christopher Ferriscae21a92018-02-05 18:14:55 -08001083void* debug_aligned_alloc(size_t alignment, size_t size) {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -07001084 Unreachable::CheckIfRequested(g_debug->config());
1085
Christopher Ferriscae21a92018-02-05 18:14:55 -08001086 if (DebugCallsDisabled()) {
1087 return g_dispatch->aligned_alloc(alignment, size);
1088 }
Christopher Ferrisa22f5d52019-03-01 16:40:59 -08001089 if (!powerof2(alignment) || (size % alignment) != 0) {
Christopher Ferriscae21a92018-02-05 18:14:55 -08001090 errno = EINVAL;
1091 return nullptr;
1092 }
1093 return debug_memalign(alignment, size);
1094}
1095
Christopher Ferris63860cb2015-11-16 17:30:32 -08001096int debug_posix_memalign(void** memptr, size_t alignment, size_t size) {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -07001097 Unreachable::CheckIfRequested(g_debug->config());
1098
Christopher Ferris63860cb2015-11-16 17:30:32 -08001099 if (DebugCallsDisabled()) {
1100 return g_dispatch->posix_memalign(memptr, alignment, size);
1101 }
1102
Christopher Ferris6c619a02019-03-01 17:59:51 -08001103 if (alignment < sizeof(void*) || !powerof2(alignment)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -08001104 return EINVAL;
1105 }
1106 int saved_errno = errno;
1107 *memptr = debug_memalign(alignment, size);
1108 errno = saved_errno;
1109 return (*memptr != nullptr) ? 0 : ENOMEM;
1110}
1111
Christopher Ferris6f517cd2019-11-08 11:28:38 -08001112int debug_malloc_iterate(uintptr_t base, size_t size, void (*callback)(uintptr_t, size_t, void*),
Christopher Ferris4da25032018-03-07 13:38:48 -08001113 void* arg) {
Christopher Ferrisd269fcc2019-05-06 19:03:59 -07001114 ScopedConcurrentLock lock;
Christopher Ferris4da25032018-03-07 13:38:48 -08001115 if (g_debug->TrackPointers()) {
Christopher Ferrisf78486f2022-05-04 14:08:54 -07001116 PointerData::IteratePointers([&callback, &arg](uintptr_t pointer) {
1117 callback(pointer, InternalMallocUsableSize(reinterpret_cast<void*>(pointer)), arg);
1118 });
Christopher Ferris4da25032018-03-07 13:38:48 -08001119 return 0;
1120 }
Colin Cross869691c2016-01-29 12:48:18 -08001121
Christopher Ferris4da25032018-03-07 13:38:48 -08001122 // An option that adds a header will add pointer tracking, so no need to
1123 // check if headers are enabled.
Christopher Ferris6f517cd2019-11-08 11:28:38 -08001124 return g_dispatch->malloc_iterate(base, size, callback, arg);
Colin Cross869691c2016-01-29 12:48:18 -08001125}
1126
1127void debug_malloc_disable() {
Christopher Ferrisd269fcc2019-05-06 19:03:59 -07001128 ScopedConcurrentLock lock;
Christopher Ferris4da25032018-03-07 13:38:48 -08001129 if (g_debug->pointer) {
Abdelrahman Daim862bd502024-11-14 08:09:42 -08001130 // Acquire the pointer locks first, otherwise, the code can be holding
1131 // the allocation lock and deadlock trying to acquire a pointer lock.
Christopher Ferris4da25032018-03-07 13:38:48 -08001132 g_debug->pointer->PrepareFork();
Colin Cross869691c2016-01-29 12:48:18 -08001133 }
Abdelrahman Daim862bd502024-11-14 08:09:42 -08001134 g_dispatch->malloc_disable();
Colin Cross869691c2016-01-29 12:48:18 -08001135}
1136
1137void debug_malloc_enable() {
Christopher Ferrisd269fcc2019-05-06 19:03:59 -07001138 ScopedConcurrentLock lock;
Abdelrahman Daim862bd502024-11-14 08:09:42 -08001139 g_dispatch->malloc_enable();
Christopher Ferris4da25032018-03-07 13:38:48 -08001140 if (g_debug->pointer) {
1141 g_debug->pointer->PostForkParent();
Colin Cross869691c2016-01-29 12:48:18 -08001142 }
Colin Cross869691c2016-01-29 12:48:18 -08001143}
1144
Christopher Ferris4da25032018-03-07 13:38:48 -08001145ssize_t debug_malloc_backtrace(void* pointer, uintptr_t* frames, size_t max_frames) {
Colin Cross2d4721c2016-02-02 11:57:54 -08001146 if (DebugCallsDisabled() || pointer == nullptr) {
1147 return 0;
1148 }
Christopher Ferrisd269fcc2019-05-06 19:03:59 -07001149 ScopedConcurrentLock lock;
Christopher Ferris55a89a42016-04-07 17:14:53 -07001150 ScopedDisableDebugCalls disable;
Christopher Ferris9bf78172020-05-20 15:37:30 -07001151 ScopedBacktraceSignalBlocker blocked;
Colin Cross2d4721c2016-02-02 11:57:54 -08001152
Christopher Ferris4da25032018-03-07 13:38:48 -08001153 if (!(g_debug->config().options() & BACKTRACE)) {
1154 return 0;
Colin Cross2d4721c2016-02-02 11:57:54 -08001155 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -08001156 pointer = UntagPointer(pointer);
Christopher Ferris4da25032018-03-07 13:38:48 -08001157 return PointerData::GetFrames(pointer, frames, max_frames);
Colin Cross2d4721c2016-02-02 11:57:54 -08001158}
1159
Christopher Ferris63860cb2015-11-16 17:30:32 -08001160#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
1161void* debug_pvalloc(size_t bytes) {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -07001162 Unreachable::CheckIfRequested(g_debug->config());
1163
Christopher Ferris63860cb2015-11-16 17:30:32 -08001164 if (DebugCallsDisabled()) {
1165 return g_dispatch->pvalloc(bytes);
1166 }
1167
1168 size_t pagesize = getpagesize();
Dan Alberta613d0d2017-10-05 16:39:33 -07001169 size_t size = __BIONIC_ALIGN(bytes, pagesize);
Christopher Ferris63860cb2015-11-16 17:30:32 -08001170 if (size < bytes) {
1171 // Overflow
1172 errno = ENOMEM;
1173 return nullptr;
1174 }
1175 return debug_memalign(pagesize, size);
1176}
1177
1178void* debug_valloc(size_t size) {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -07001179 Unreachable::CheckIfRequested(g_debug->config());
1180
Christopher Ferris63860cb2015-11-16 17:30:32 -08001181 if (DebugCallsDisabled()) {
1182 return g_dispatch->valloc(size);
1183 }
1184 return debug_memalign(getpagesize(), size);
1185}
1186#endif
Christopher Ferris602b88c2017-08-04 13:04:04 -07001187
1188static std::mutex g_dump_lock;
1189
Christopher Ferrisff88fb02019-11-04 18:40:00 -08001190static void write_dump(int fd) {
1191 dprintf(fd, "Android Native Heap Dump v1.2\n\n");
Christopher Ferris602b88c2017-08-04 13:04:04 -07001192
Christopher Ferris2e1a40a2018-06-13 10:46:34 -07001193 std::string fingerprint = android::base::GetProperty("ro.build.fingerprint", "unknown");
Christopher Ferrisff88fb02019-11-04 18:40:00 -08001194 dprintf(fd, "Build fingerprint: '%s'\n\n", fingerprint.c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -07001195
Christopher Ferrisff88fb02019-11-04 18:40:00 -08001196 PointerData::DumpLiveToFile(fd);
Christopher Ferris602b88c2017-08-04 13:04:04 -07001197
Christopher Ferrisff88fb02019-11-04 18:40:00 -08001198 dprintf(fd, "MAPS\n");
Christopher Ferris602b88c2017-08-04 13:04:04 -07001199 std::string content;
1200 if (!android::base::ReadFileToString("/proc/self/maps", &content)) {
Christopher Ferrisff88fb02019-11-04 18:40:00 -08001201 dprintf(fd, "Could not open /proc/self/maps\n");
Christopher Ferris602b88c2017-08-04 13:04:04 -07001202 } else {
Christopher Ferrisff88fb02019-11-04 18:40:00 -08001203 dprintf(fd, "%s", content.c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -07001204 }
Christopher Ferrisff88fb02019-11-04 18:40:00 -08001205 dprintf(fd, "END\n");
Christopher Ferrisaa3e5742023-01-31 01:31:52 +00001206
1207 // Purge the memory that was allocated and freed during this operation
1208 // since it can be large enough to expand the RSS significantly.
Christopher Ferrisf1ab9c42023-03-22 13:47:27 -07001209 g_dispatch->mallopt(M_PURGE_ALL, 0);
Christopher Ferris2e1a40a2018-06-13 10:46:34 -07001210}
1211
1212bool debug_write_malloc_leak_info(FILE* fp) {
Christopher Ferrisff88fb02019-11-04 18:40:00 -08001213 // Make sure any pending output is written to the file.
1214 fflush(fp);
1215
Christopher Ferrisd269fcc2019-05-06 19:03:59 -07001216 ScopedConcurrentLock lock;
Christopher Ferris2e1a40a2018-06-13 10:46:34 -07001217 ScopedDisableDebugCalls disable;
Christopher Ferris9bf78172020-05-20 15:37:30 -07001218 ScopedBacktraceSignalBlocker blocked;
Christopher Ferris2e1a40a2018-06-13 10:46:34 -07001219
1220 std::lock_guard<std::mutex> guard(g_dump_lock);
1221
1222 if (!(g_debug->config().options() & BACKTRACE)) {
1223 return false;
1224 }
1225
Christopher Ferrisff88fb02019-11-04 18:40:00 -08001226 write_dump(fileno(fp));
1227
Christopher Ferris602b88c2017-08-04 13:04:04 -07001228 return true;
1229}
Christopher Ferris2e1a40a2018-06-13 10:46:34 -07001230
1231void debug_dump_heap(const char* file_name) {
Christopher Ferrisd269fcc2019-05-06 19:03:59 -07001232 ScopedConcurrentLock lock;
Christopher Ferris2e1a40a2018-06-13 10:46:34 -07001233 ScopedDisableDebugCalls disable;
Christopher Ferris9bf78172020-05-20 15:37:30 -07001234 ScopedBacktraceSignalBlocker blocked;
Christopher Ferris2e1a40a2018-06-13 10:46:34 -07001235
1236 std::lock_guard<std::mutex> guard(g_dump_lock);
1237
Christopher Ferrisff88fb02019-11-04 18:40:00 -08001238 int fd = open(file_name, O_RDWR | O_CREAT | O_NOFOLLOW | O_TRUNC | O_CLOEXEC, 0644);
1239 if (fd == -1) {
Christopher Ferris2e1a40a2018-06-13 10:46:34 -07001240 error_log("Unable to create file: %s", file_name);
1241 return;
1242 }
1243
1244 error_log("Dumping to file: %s\n", file_name);
Christopher Ferrisff88fb02019-11-04 18:40:00 -08001245 write_dump(fd);
1246 close(fd);
Christopher Ferris2e1a40a2018-06-13 10:46:34 -07001247}