blob: 35cda838cbb6fcd0caedd4eccf075d0b1636ce70 [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 Ferrisb42e8b42022-05-09 14:00:47 -070056#include "Unreachable.h"
57#include "UnwindBacktrace.h"
Christopher Ferris4da25032018-03-07 13:38:48 -080058#include "backtrace.h"
Christopher Ferris63860cb2015-11-16 17:30:32 -080059#include "debug_disable.h"
60#include "debug_log.h"
61#include "malloc_debug.h"
62
63// ------------------------------------------------------------------------
64// Global Data
65// ------------------------------------------------------------------------
66DebugData* g_debug;
67
Christopher Ferris8189e772019-04-09 16:37:23 -070068bool* g_zygote_child;
Christopher Ferris63860cb2015-11-16 17:30:32 -080069
70const MallocDispatch* g_dispatch;
71// ------------------------------------------------------------------------
72
73// ------------------------------------------------------------------------
74// Use C style prototypes for all exported functions. This makes it easy
75// to do dlsym lookups during libc initialization when malloc debug
76// is enabled.
77// ------------------------------------------------------------------------
78__BEGIN_DECLS
79
Christopher Ferris8189e772019-04-09 16:37:23 -070080bool debug_initialize(const MallocDispatch* malloc_dispatch, bool* malloc_zygote_child,
Christopher Ferris4da25032018-03-07 13:38:48 -080081 const char* options);
Christopher Ferris63860cb2015-11-16 17:30:32 -080082void debug_finalize();
Christopher Ferris2e1a40a2018-06-13 10:46:34 -070083void debug_dump_heap(const char* file_name);
Christopher Ferris4da25032018-03-07 13:38:48 -080084void debug_get_malloc_leak_info(uint8_t** info, size_t* overall_size, size_t* info_size,
85 size_t* total_memory, size_t* backtrace_size);
Christopher Ferris2e1a40a2018-06-13 10:46:34 -070086bool debug_write_malloc_leak_info(FILE* fp);
Colin Cross2d4721c2016-02-02 11:57:54 -080087ssize_t debug_malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count);
Christopher Ferris63860cb2015-11-16 17:30:32 -080088void debug_free_malloc_leak_info(uint8_t* info);
89size_t debug_malloc_usable_size(void* pointer);
90void* debug_malloc(size_t size);
91void debug_free(void* pointer);
Christopher Ferriscae21a92018-02-05 18:14:55 -080092void* debug_aligned_alloc(size_t alignment, size_t size);
Christopher Ferris63860cb2015-11-16 17:30:32 -080093void* debug_memalign(size_t alignment, size_t bytes);
94void* debug_realloc(void* pointer, size_t bytes);
95void* debug_calloc(size_t nmemb, size_t bytes);
96struct mallinfo debug_mallinfo();
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -070097int debug_mallopt(int param, int value);
Christopher Ferris6c619a02019-03-01 17:59:51 -080098int debug_malloc_info(int options, FILE* fp);
Christopher Ferris63860cb2015-11-16 17:30:32 -080099int debug_posix_memalign(void** memptr, size_t alignment, size_t size);
Christopher Ferris6f517cd2019-11-08 11:28:38 -0800100int debug_malloc_iterate(uintptr_t base, size_t size,
101 void (*callback)(uintptr_t base, size_t size, void* arg), void* arg);
Colin Cross869691c2016-01-29 12:48:18 -0800102void debug_malloc_disable();
103void debug_malloc_enable();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800104
105#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
106void* debug_pvalloc(size_t bytes);
107void* debug_valloc(size_t size);
108#endif
109
110__END_DECLS
111// ------------------------------------------------------------------------
112
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700113class ScopedConcurrentLock {
114 public:
115 ScopedConcurrentLock() {
116 pthread_rwlock_rdlock(&lock_);
117 }
118 ~ScopedConcurrentLock() {
119 pthread_rwlock_unlock(&lock_);
120 }
121
122 static void Init() {
123 pthread_rwlockattr_t attr;
124 // Set the attribute so that when a write lock is pending, read locks are no
125 // longer granted.
126 pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
127 pthread_rwlock_init(&lock_, &attr);
128 }
129
130 static void BlockAllOperations() {
131 pthread_rwlock_wrlock(&lock_);
132 }
133
134 private:
135 static pthread_rwlock_t lock_;
136};
137pthread_rwlock_t ScopedConcurrentLock::lock_;
138
Christopher Ferris9bf78172020-05-20 15:37:30 -0700139// Use this because the sigprocmask* functions filter out the reserved bionic
140// signals including the signal this code blocks.
141static inline int __rt_sigprocmask(int how, const sigset64_t* new_set, sigset64_t* old_set,
142 size_t sigset_size) {
143 return syscall(SYS_rt_sigprocmask, how, new_set, old_set, sigset_size);
144}
145
146// Need to block the backtrace signal while in malloc debug routines
147// otherwise there is a chance of a deadlock and timeout when unwinding.
148// This can occur if a thread is paused while owning a malloc debug
149// internal lock.
150class ScopedBacktraceSignalBlocker {
151 public:
152 ScopedBacktraceSignalBlocker() {
153 sigemptyset64(&backtrace_set_);
154 sigaddset64(&backtrace_set_, BIONIC_SIGNAL_BACKTRACE);
155 sigset64_t old_set;
156 __rt_sigprocmask(SIG_BLOCK, &backtrace_set_, &old_set, sizeof(backtrace_set_));
157 if (sigismember64(&old_set, BIONIC_SIGNAL_BACKTRACE)) {
158 unblock_ = false;
159 }
160 }
161
162 ~ScopedBacktraceSignalBlocker() {
163 if (unblock_) {
164 __rt_sigprocmask(SIG_UNBLOCK, &backtrace_set_, nullptr, sizeof(backtrace_set_));
165 }
166 }
167
168 private:
169 bool unblock_ = true;
170 sigset64_t backtrace_set_;
171};
172
Colin Cross7a28a3c2016-02-07 22:51:15 -0800173static void InitAtfork() {
174 static pthread_once_t atfork_init = PTHREAD_ONCE_INIT;
Christopher Ferris4da25032018-03-07 13:38:48 -0800175 pthread_once(&atfork_init, []() {
Colin Cross7a28a3c2016-02-07 22:51:15 -0800176 pthread_atfork(
Christopher Ferris4da25032018-03-07 13:38:48 -0800177 []() {
Colin Cross7a28a3c2016-02-07 22:51:15 -0800178 if (g_debug != nullptr) {
179 g_debug->PrepareFork();
180 }
181 },
Christopher Ferris4da25032018-03-07 13:38:48 -0800182 []() {
Colin Cross7a28a3c2016-02-07 22:51:15 -0800183 if (g_debug != nullptr) {
184 g_debug->PostForkParent();
185 }
186 },
Christopher Ferris4da25032018-03-07 13:38:48 -0800187 []() {
Colin Cross7a28a3c2016-02-07 22:51:15 -0800188 if (g_debug != nullptr) {
189 g_debug->PostForkChild();
190 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800191 });
Colin Cross7a28a3c2016-02-07 22:51:15 -0800192 });
193}
Christopher Ferrisd0919622016-03-15 22:39:39 -0700194
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700195void BacktraceAndLog() {
196 if (g_debug->config().options() & BACKTRACE_FULL) {
197 std::vector<uintptr_t> frames;
Christopher Ferris459eecb2022-01-07 13:38:10 -0800198 std::vector<unwindstack::FrameData> frames_info;
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700199 if (!Unwind(&frames, &frames_info, 256)) {
200 error_log(" Backtrace failed to get any frames.");
201 } else {
202 UnwindLog(frames_info);
203 }
204 } else {
205 std::vector<uintptr_t> frames(256);
206 size_t num_frames = backtrace_get(frames.data(), frames.size());
207 if (num_frames == 0) {
208 error_log(" Backtrace failed to get any frames.");
209 } else {
210 backtrace_log(frames.data(), num_frames);
211 }
212 }
213}
214
Christopher Ferris4da25032018-03-07 13:38:48 -0800215static void LogError(const void* pointer, const char* error_str) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800216 error_log(LOG_DIVIDER);
Christopher Ferris4da25032018-03-07 13:38:48 -0800217 error_log("+++ ALLOCATION %p %s", pointer, error_str);
218
219 // If we are tracking already freed pointers, check to see if this is
220 // one so we can print extra information.
221 if (g_debug->config().options() & FREE_TRACK) {
222 PointerData::LogFreeBacktrace(pointer);
Christopher Ferris7993b802016-01-28 18:35:05 -0800223 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800224
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700225 error_log("Backtrace at time of failure:");
226 BacktraceAndLog();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800227 error_log(LOG_DIVIDER);
Iris Chang7f209a92019-01-16 11:17:15 +0800228 if (g_debug->config().options() & ABORT_ON_ERROR) {
229 abort();
230 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800231}
232
Christopher Ferris4da25032018-03-07 13:38:48 -0800233static bool VerifyPointer(const void* pointer, const char* function_name) {
234 if (g_debug->HeaderEnabled()) {
235 Header* header = g_debug->GetHeader(pointer);
236 if (header->tag != DEBUG_TAG) {
237 std::string error_str;
238 if (header->tag == DEBUG_FREE_TAG) {
239 error_str = std::string("USED AFTER FREE (") + function_name + ")";
240 } else {
241 error_str = android::base::StringPrintf("HAS INVALID TAG %" PRIx32 " (%s)", header->tag,
242 function_name);
243 }
244 LogError(pointer, error_str.c_str());
245 return false;
246 }
247 }
248
249 if (g_debug->TrackPointers()) {
250 if (!PointerData::Exists(pointer)) {
251 std::string error_str(std::string("UNKNOWN POINTER (") + function_name + ")");
252 LogError(pointer, error_str.c_str());
253 return false;
254 }
255 }
256 return true;
257}
258
259static size_t InternalMallocUsableSize(void* pointer) {
260 if (g_debug->HeaderEnabled()) {
261 return g_debug->GetHeader(pointer)->usable_size;
262 } else {
263 return g_dispatch->malloc_usable_size(pointer);
264 }
265}
266
Christopher Ferris63860cb2015-11-16 17:30:32 -0800267static void* InitHeader(Header* header, void* orig_pointer, size_t size) {
268 header->tag = DEBUG_TAG;
269 header->orig_pointer = orig_pointer;
270 header->size = size;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800271 header->usable_size = g_dispatch->malloc_usable_size(orig_pointer);
272 if (header->usable_size == 0) {
273 g_dispatch->free(orig_pointer);
274 return nullptr;
275 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800276 header->usable_size -= g_debug->pointer_offset() + reinterpret_cast<uintptr_t>(header) -
277 reinterpret_cast<uintptr_t>(orig_pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800278
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700279 if (g_debug->config().options() & FRONT_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800280 uint8_t* guard = g_debug->GetFrontGuard(header);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700281 memset(guard, g_debug->config().front_guard_value(), g_debug->config().front_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800282 }
283
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700284 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800285 uint8_t* guard = g_debug->GetRearGuard(header);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700286 memset(guard, g_debug->config().rear_guard_value(), g_debug->config().rear_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800287 // If the rear guard is enabled, set the usable size to the exact size
288 // of the allocation.
Christopher Ferris4da25032018-03-07 13:38:48 -0800289 header->usable_size = header->size;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800290 }
291
292 return g_debug->GetPointer(header);
293}
294
Christopher Ferris705de3c2019-05-22 13:39:57 -0700295extern "C" void __asan_init() __attribute__((weak));
296
Christopher Ferris8189e772019-04-09 16:37:23 -0700297bool debug_initialize(const MallocDispatch* malloc_dispatch, bool* zygote_child,
Christopher Ferris4da25032018-03-07 13:38:48 -0800298 const char* options) {
Christopher Ferris8189e772019-04-09 16:37:23 -0700299 if (zygote_child == nullptr || options == nullptr) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800300 return false;
301 }
Colin Cross7a28a3c2016-02-07 22:51:15 -0800302
Christopher Ferris705de3c2019-05-22 13:39:57 -0700303 if (__asan_init != 0) {
304 error_log("malloc debug cannot be enabled alongside ASAN");
305 return false;
306 }
307
Colin Cross7a28a3c2016-02-07 22:51:15 -0800308 InitAtfork();
309
Christopher Ferris8189e772019-04-09 16:37:23 -0700310 g_zygote_child = zygote_child;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800311
312 g_dispatch = malloc_dispatch;
313
314 if (!DebugDisableInitialize()) {
315 return false;
316 }
317
318 DebugData* debug = new DebugData();
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700319 if (!debug->Initialize(options) || !Unreachable::Initialize(debug->config())) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800320 delete debug;
321 DebugDisableFinalize();
322 return false;
323 }
324 g_debug = debug;
325
326 // Always enable the backtrace code since we will use it in a number
327 // of different error cases.
328 backtrace_startup();
329
Christopher Ferrisc328e442019-04-01 19:31:26 -0700330 if (g_debug->config().options() & VERBOSE) {
331 info_log("%s: malloc debug enabled", getprogname());
332 }
333
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700334 ScopedConcurrentLock::Init();
335
Christopher Ferris63860cb2015-11-16 17:30:32 -0800336 return true;
337}
338
339void debug_finalize() {
340 if (g_debug == nullptr) {
341 return;
342 }
343
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700344 // Make sure that there are no other threads doing debug allocations
345 // before we kill everything.
346 ScopedConcurrentLock::BlockAllOperations();
347
Christopher Ferris97b47472018-07-10 14:45:24 -0700348 // Turn off capturing allocations calls.
349 DebugDisableSet(true);
350
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700351 if (g_debug->config().options() & FREE_TRACK) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800352 PointerData::VerifyAllFreed();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800353 }
354
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700355 if (g_debug->config().options() & LEAK_TRACK) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800356 PointerData::LogLeaks();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800357 }
358
Christopher Ferris602b88c2017-08-04 13:04:04 -0700359 if ((g_debug->config().options() & BACKTRACE) && g_debug->config().backtrace_dump_on_exit()) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800360 debug_dump_heap(android::base::StringPrintf("%s.%d.exit.txt",
361 g_debug->config().backtrace_dump_prefix().c_str(),
Christopher Ferris97b47472018-07-10 14:45:24 -0700362 getpid()).c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700363 }
364
Colin Cross2c759912016-02-05 16:17:39 -0800365 backtrace_shutdown();
366
Christopher Ferris33d73372021-07-02 15:46:18 -0700367 // In order to prevent any issues of threads freeing previous pointers
368 // after the main thread calls this code, simply leak the g_debug pointer
369 // and do not destroy the debug disable pthread key.
Christopher Ferris63860cb2015-11-16 17:30:32 -0800370}
371
Christopher Ferris4da25032018-03-07 13:38:48 -0800372void debug_get_malloc_leak_info(uint8_t** info, size_t* overall_size, size_t* info_size,
373 size_t* total_memory, size_t* backtrace_size) {
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700374 ScopedConcurrentLock lock;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800375 ScopedDisableDebugCalls disable;
Christopher Ferris9bf78172020-05-20 15:37:30 -0700376 ScopedBacktraceSignalBlocker blocked;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800377
378 // Verify the arguments.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700379 if (info == nullptr || overall_size == nullptr || info_size == nullptr || total_memory == nullptr ||
Christopher Ferris4da25032018-03-07 13:38:48 -0800380 backtrace_size == nullptr) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800381 error_log("get_malloc_leak_info: At least one invalid parameter.");
382 return;
383 }
384
385 *info = nullptr;
386 *overall_size = 0;
387 *info_size = 0;
388 *total_memory = 0;
389 *backtrace_size = 0;
390
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700391 if (!(g_debug->config().options() & BACKTRACE)) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800392 error_log(
393 "get_malloc_leak_info: Allocations not being tracked, to enable "
394 "set the option 'backtrace'.");
Christopher Ferris63860cb2015-11-16 17:30:32 -0800395 return;
396 }
397
Christopher Ferris4da25032018-03-07 13:38:48 -0800398 PointerData::GetInfo(info, overall_size, info_size, total_memory, backtrace_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800399}
400
401void debug_free_malloc_leak_info(uint8_t* info) {
402 g_dispatch->free(info);
403}
404
Christopher Ferris55a89a42016-04-07 17:14:53 -0700405size_t debug_malloc_usable_size(void* pointer) {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700406 Unreachable::CheckIfRequested(g_debug->config());
407
Christopher Ferris55a89a42016-04-07 17:14:53 -0700408 if (DebugCallsDisabled() || pointer == nullptr) {
409 return g_dispatch->malloc_usable_size(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800410 }
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700411 ScopedConcurrentLock lock;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700412 ScopedDisableDebugCalls disable;
Christopher Ferris9bf78172020-05-20 15:37:30 -0700413 ScopedBacktraceSignalBlocker blocked;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800414
Christopher Ferris4da25032018-03-07 13:38:48 -0800415 if (!VerifyPointer(pointer, "malloc_usable_size")) {
416 return 0;
417 }
418
419 return InternalMallocUsableSize(pointer);
Christopher Ferris55a89a42016-04-07 17:14:53 -0700420}
421
Christopher Ferris4da25032018-03-07 13:38:48 -0800422static void* InternalMalloc(size_t size) {
423 if ((g_debug->config().options() & BACKTRACE) && g_debug->pointer->ShouldDumpAndReset()) {
424 debug_dump_heap(android::base::StringPrintf(
425 "%s.%d.txt", g_debug->config().backtrace_dump_prefix().c_str(), getpid())
426 .c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700427 }
428
Colin Cross9567c7b2016-03-09 17:56:14 -0800429 if (size == 0) {
430 size = 1;
431 }
432
Christopher Ferris63860cb2015-11-16 17:30:32 -0800433 size_t real_size = size + g_debug->extra_bytes();
434 if (real_size < size) {
435 // Overflow.
436 errno = ENOMEM;
437 return nullptr;
438 }
439
Christopher Ferris4da25032018-03-07 13:38:48 -0800440 if (size > PointerInfoType::MaxSize()) {
441 errno = ENOMEM;
442 return nullptr;
443 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800444
Christopher Ferris4da25032018-03-07 13:38:48 -0800445 void* pointer;
446 if (g_debug->HeaderEnabled()) {
447 Header* header =
448 reinterpret_cast<Header*>(g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800449 if (header == nullptr) {
450 return nullptr;
451 }
452 pointer = InitHeader(header, header, size);
453 } else {
454 pointer = g_dispatch->malloc(real_size);
455 }
456
Christopher Ferris4da25032018-03-07 13:38:48 -0800457 if (pointer != nullptr) {
458 if (g_debug->TrackPointers()) {
459 PointerData::Add(pointer, size);
460 }
461
462 if (g_debug->config().options() & FILL_ON_ALLOC) {
463 size_t bytes = InternalMallocUsableSize(pointer);
464 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes();
465 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
466 memset(pointer, g_debug->config().fill_alloc_value(), bytes);
467 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800468 }
469 return pointer;
470}
471
Christopher Ferris55a89a42016-04-07 17:14:53 -0700472void* debug_malloc(size_t size) {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700473 Unreachable::CheckIfRequested(g_debug->config());
474
Christopher Ferris55a89a42016-04-07 17:14:53 -0700475 if (DebugCallsDisabled()) {
476 return g_dispatch->malloc(size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800477 }
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700478 ScopedConcurrentLock lock;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700479 ScopedDisableDebugCalls disable;
Christopher Ferris9bf78172020-05-20 15:37:30 -0700480 ScopedBacktraceSignalBlocker blocked;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800481
Christopher Ferris4da25032018-03-07 13:38:48 -0800482 void* pointer = InternalMalloc(size);
Christopher Ferris7bd01782016-04-20 12:30:58 -0700483
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700484 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700485 g_debug->record->AddEntry(new MallocEntry(pointer, size));
486 }
487
488 return pointer;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700489}
490
Christopher Ferris4da25032018-03-07 13:38:48 -0800491static void InternalFree(void* pointer) {
492 if ((g_debug->config().options() & BACKTRACE) && g_debug->pointer->ShouldDumpAndReset()) {
493 debug_dump_heap(android::base::StringPrintf(
494 "%s.%d.txt", g_debug->config().backtrace_dump_prefix().c_str(), getpid())
495 .c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700496 }
497
Christopher Ferris63860cb2015-11-16 17:30:32 -0800498 void* free_pointer = pointer;
499 size_t bytes;
Christopher Ferrisd0919622016-03-15 22:39:39 -0700500 Header* header;
Christopher Ferris4da25032018-03-07 13:38:48 -0800501 if (g_debug->HeaderEnabled()) {
Christopher Ferrisd0919622016-03-15 22:39:39 -0700502 header = g_debug->GetHeader(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800503 free_pointer = header->orig_pointer;
504
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700505 if (g_debug->config().options() & FRONT_GUARD) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700506 if (!g_debug->front_guard->Valid(header)) {
507 g_debug->front_guard->LogFailure(header);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800508 }
509 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700510 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700511 if (!g_debug->rear_guard->Valid(header)) {
512 g_debug->rear_guard->LogFailure(header);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800513 }
514 }
515
Christopher Ferris7993b802016-01-28 18:35:05 -0800516 header->tag = DEBUG_FREE_TAG;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800517
518 bytes = header->usable_size;
519 } else {
520 bytes = g_dispatch->malloc_usable_size(pointer);
521 }
522
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700523 if (g_debug->config().options() & FILL_ON_FREE) {
524 size_t fill_bytes = g_debug->config().fill_on_free_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800525 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700526 memset(pointer, g_debug->config().fill_free_value(), bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800527 }
528
Christopher Ferris4da25032018-03-07 13:38:48 -0800529 if (g_debug->TrackPointers()) {
530 PointerData::Remove(pointer);
531 }
532
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700533 if (g_debug->config().options() & FREE_TRACK) {
Christopher Ferrisd0919622016-03-15 22:39:39 -0700534 // Do not add the allocation until we are done modifying the pointer
535 // itself. This avoids a race if a lot of threads are all doing
536 // frees at the same time and we wind up trying to really free this
537 // pointer from another thread, while still trying to free it in
538 // this function.
Christopher Ferris4da25032018-03-07 13:38:48 -0800539 pointer = PointerData::AddFreed(pointer);
540 if (pointer != nullptr) {
541 if (g_debug->HeaderEnabled()) {
542 pointer = g_debug->GetHeader(pointer)->orig_pointer;
543 }
544 g_dispatch->free(pointer);
545 }
Christopher Ferrisd0919622016-03-15 22:39:39 -0700546 } else {
547 g_dispatch->free(free_pointer);
548 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800549}
550
Christopher Ferris55a89a42016-04-07 17:14:53 -0700551void debug_free(void* pointer) {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700552 Unreachable::CheckIfRequested(g_debug->config());
553
Christopher Ferris55a89a42016-04-07 17:14:53 -0700554 if (DebugCallsDisabled() || pointer == nullptr) {
555 return g_dispatch->free(pointer);
556 }
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700557 ScopedConcurrentLock lock;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700558 ScopedDisableDebugCalls disable;
Christopher Ferris9bf78172020-05-20 15:37:30 -0700559 ScopedBacktraceSignalBlocker blocked;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700560
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700561 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700562 g_debug->record->AddEntry(new FreeEntry(pointer));
563 }
564
Christopher Ferris4da25032018-03-07 13:38:48 -0800565 if (!VerifyPointer(pointer, "free")) {
566 return;
567 }
568
569 InternalFree(pointer);
Christopher Ferris55a89a42016-04-07 17:14:53 -0700570}
571
Christopher Ferris63860cb2015-11-16 17:30:32 -0800572void* debug_memalign(size_t alignment, size_t bytes) {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700573 Unreachable::CheckIfRequested(g_debug->config());
574
Christopher Ferris63860cb2015-11-16 17:30:32 -0800575 if (DebugCallsDisabled()) {
576 return g_dispatch->memalign(alignment, bytes);
577 }
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700578 ScopedConcurrentLock lock;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700579 ScopedDisableDebugCalls disable;
Christopher Ferris9bf78172020-05-20 15:37:30 -0700580 ScopedBacktraceSignalBlocker blocked;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800581
Colin Cross9567c7b2016-03-09 17:56:14 -0800582 if (bytes == 0) {
583 bytes = 1;
584 }
585
Christopher Ferris4da25032018-03-07 13:38:48 -0800586 if (bytes > PointerInfoType::MaxSize()) {
587 errno = ENOMEM;
588 return nullptr;
589 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800590
Christopher Ferris4da25032018-03-07 13:38:48 -0800591 void* pointer;
592 if (g_debug->HeaderEnabled()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800593 // Make the alignment a power of two.
594 if (!powerof2(alignment)) {
595 alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment);
596 }
Christopher Ferris72df6702016-02-11 15:51:31 -0800597 // Force the alignment to at least MINIMUM_ALIGNMENT_BYTES to guarantee
Christopher Ferris63860cb2015-11-16 17:30:32 -0800598 // that the header is aligned properly.
Christopher Ferris72df6702016-02-11 15:51:31 -0800599 if (alignment < MINIMUM_ALIGNMENT_BYTES) {
600 alignment = MINIMUM_ALIGNMENT_BYTES;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800601 }
602
603 // We don't have any idea what the natural alignment of
604 // the underlying native allocator is, so we always need to
605 // over allocate.
606 size_t real_size = alignment + bytes + g_debug->extra_bytes();
607 if (real_size < bytes) {
608 // Overflow.
609 errno = ENOMEM;
610 return nullptr;
611 }
612
613 pointer = g_dispatch->malloc(real_size);
614 if (pointer == nullptr) {
615 return nullptr;
616 }
617
618 uintptr_t value = reinterpret_cast<uintptr_t>(pointer) + g_debug->pointer_offset();
619 // Now align the pointer.
620 value += (-value % alignment);
621
622 Header* header = g_debug->GetHeader(reinterpret_cast<void*>(value));
623 pointer = InitHeader(header, pointer, bytes);
624 } else {
625 size_t real_size = bytes + g_debug->extra_bytes();
626 if (real_size < bytes) {
627 // Overflow.
628 errno = ENOMEM;
629 return nullptr;
630 }
631 pointer = g_dispatch->memalign(alignment, real_size);
632 }
633
Christopher Ferris4da25032018-03-07 13:38:48 -0800634 if (pointer != nullptr) {
635 if (g_debug->TrackPointers()) {
636 PointerData::Add(pointer, bytes);
637 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700638
Christopher Ferris4da25032018-03-07 13:38:48 -0800639 if (g_debug->config().options() & FILL_ON_ALLOC) {
640 size_t bytes = InternalMallocUsableSize(pointer);
641 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes();
642 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
643 memset(pointer, g_debug->config().fill_alloc_value(), bytes);
644 }
645
646 if (g_debug->config().options() & RECORD_ALLOCS) {
647 g_debug->record->AddEntry(new MemalignEntry(pointer, bytes, alignment));
648 }
Christopher Ferris7bd01782016-04-20 12:30:58 -0700649 }
650
Christopher Ferris63860cb2015-11-16 17:30:32 -0800651 return pointer;
652}
653
654void* debug_realloc(void* pointer, size_t bytes) {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700655 Unreachable::CheckIfRequested(g_debug->config());
656
Christopher Ferris63860cb2015-11-16 17:30:32 -0800657 if (DebugCallsDisabled()) {
658 return g_dispatch->realloc(pointer, bytes);
659 }
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700660 ScopedConcurrentLock lock;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700661 ScopedDisableDebugCalls disable;
Christopher Ferris9bf78172020-05-20 15:37:30 -0700662 ScopedBacktraceSignalBlocker blocked;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800663
664 if (pointer == nullptr) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800665 pointer = InternalMalloc(bytes);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700666 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700667 g_debug->record->AddEntry(new ReallocEntry(pointer, bytes, nullptr));
668 }
669 return pointer;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800670 }
671
Christopher Ferris4da25032018-03-07 13:38:48 -0800672 if (!VerifyPointer(pointer, "realloc")) {
673 return nullptr;
674 }
675
Christopher Ferris63860cb2015-11-16 17:30:32 -0800676 if (bytes == 0) {
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700677 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700678 g_debug->record->AddEntry(new ReallocEntry(nullptr, bytes, pointer));
679 }
680
Christopher Ferris4da25032018-03-07 13:38:48 -0800681 InternalFree(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800682 return nullptr;
683 }
684
685 size_t real_size = bytes;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700686 if (g_debug->config().options() & EXPAND_ALLOC) {
687 real_size += g_debug->config().expand_alloc_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800688 if (real_size < bytes) {
689 // Overflow.
690 errno = ENOMEM;
691 return nullptr;
692 }
693 }
694
Christopher Ferris4da25032018-03-07 13:38:48 -0800695 if (bytes > PointerInfoType::MaxSize()) {
696 errno = ENOMEM;
697 return nullptr;
698 }
699
Christopher Ferris63860cb2015-11-16 17:30:32 -0800700 void* new_pointer;
701 size_t prev_size;
Christopher Ferris4da25032018-03-07 13:38:48 -0800702 if (g_debug->HeaderEnabled()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800703 // Same size, do nothing.
Christopher Ferris4da25032018-03-07 13:38:48 -0800704 Header* header = g_debug->GetHeader(pointer);
705 if (real_size == header->size) {
706 if (g_debug->TrackPointers()) {
707 // Remove and re-add so that the backtrace is updated.
708 PointerData::Remove(pointer);
709 PointerData::Add(pointer, real_size);
710 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800711 return pointer;
712 }
713
714 // Allocation is shrinking.
715 if (real_size < header->usable_size) {
716 header->size = real_size;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700717 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800718 // Don't bother allocating a smaller pointer in this case, simply
719 // change the header usable_size and reset the rear guard.
Christopher Ferris4da25032018-03-07 13:38:48 -0800720 header->usable_size = header->size;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700721 memset(g_debug->GetRearGuard(header), g_debug->config().rear_guard_value(),
722 g_debug->config().rear_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800723 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800724 if (g_debug->TrackPointers()) {
725 // Remove and re-add so that the backtrace is updated.
726 PointerData::Remove(pointer);
727 PointerData::Add(pointer, real_size);
728 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800729 return pointer;
730 }
731
732 // Allocate the new size.
Christopher Ferris4da25032018-03-07 13:38:48 -0800733 new_pointer = InternalMalloc(bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800734 if (new_pointer == nullptr) {
735 errno = ENOMEM;
736 return nullptr;
737 }
738
739 prev_size = header->usable_size;
740 memcpy(new_pointer, pointer, prev_size);
Christopher Ferris4da25032018-03-07 13:38:48 -0800741 InternalFree(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800742 } else {
Christopher Ferris4da25032018-03-07 13:38:48 -0800743 if (g_debug->TrackPointers()) {
744 PointerData::Remove(pointer);
745 }
746
Christopher Ferris63860cb2015-11-16 17:30:32 -0800747 prev_size = g_dispatch->malloc_usable_size(pointer);
748 new_pointer = g_dispatch->realloc(pointer, real_size);
749 if (new_pointer == nullptr) {
750 return nullptr;
751 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800752
753 if (g_debug->TrackPointers()) {
754 PointerData::Add(new_pointer, real_size);
755 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800756 }
757
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700758 if (g_debug->config().options() & FILL_ON_ALLOC) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800759 size_t bytes = InternalMallocUsableSize(new_pointer);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700760 if (bytes > g_debug->config().fill_on_alloc_bytes()) {
761 bytes = g_debug->config().fill_on_alloc_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800762 }
763 if (bytes > prev_size) {
764 memset(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(new_pointer) + prev_size),
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700765 g_debug->config().fill_alloc_value(), bytes - prev_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800766 }
767 }
768
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700769 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700770 g_debug->record->AddEntry(new ReallocEntry(new_pointer, bytes, pointer));
771 }
772
Christopher Ferris63860cb2015-11-16 17:30:32 -0800773 return new_pointer;
774}
775
776void* debug_calloc(size_t nmemb, size_t bytes) {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700777 Unreachable::CheckIfRequested(g_debug->config());
778
Christopher Ferris63860cb2015-11-16 17:30:32 -0800779 if (DebugCallsDisabled()) {
780 return g_dispatch->calloc(nmemb, bytes);
781 }
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700782 ScopedConcurrentLock lock;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700783 ScopedDisableDebugCalls disable;
Christopher Ferris9bf78172020-05-20 15:37:30 -0700784 ScopedBacktraceSignalBlocker blocked;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800785
Colin Cross7877df62016-03-10 13:01:27 -0800786 size_t size;
787 if (__builtin_mul_overflow(nmemb, bytes, &size)) {
788 // Overflow
789 errno = ENOMEM;
790 return nullptr;
791 }
792
Colin Cross9567c7b2016-03-09 17:56:14 -0800793 if (size == 0) {
794 size = 1;
795 }
796
Colin Cross7877df62016-03-10 13:01:27 -0800797 size_t real_size;
798 if (__builtin_add_overflow(size, g_debug->extra_bytes(), &real_size)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800799 // Overflow.
800 errno = ENOMEM;
801 return nullptr;
802 }
803
Christopher Ferris4da25032018-03-07 13:38:48 -0800804 if (real_size > PointerInfoType::MaxSize()) {
805 errno = ENOMEM;
806 return nullptr;
807 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800808
Christopher Ferris4da25032018-03-07 13:38:48 -0800809 void* pointer;
810 if (g_debug->HeaderEnabled()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800811 // Need to guarantee the alignment of the header.
Christopher Ferris4da25032018-03-07 13:38:48 -0800812 Header* header =
813 reinterpret_cast<Header*>(g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800814 if (header == nullptr) {
815 return nullptr;
816 }
817 memset(header, 0, g_dispatch->malloc_usable_size(header));
Christopher Ferris7bd01782016-04-20 12:30:58 -0700818 pointer = InitHeader(header, header, size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800819 } else {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700820 pointer = g_dispatch->calloc(1, real_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800821 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800822
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700823 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700824 g_debug->record->AddEntry(new CallocEntry(pointer, bytes, nmemb));
825 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800826
827 if (pointer != nullptr && g_debug->TrackPointers()) {
828 PointerData::Add(pointer, size);
829 }
Christopher Ferris7bd01782016-04-20 12:30:58 -0700830 return pointer;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800831}
832
833struct mallinfo debug_mallinfo() {
834 return g_dispatch->mallinfo();
835}
836
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700837int debug_mallopt(int param, int value) {
838 return g_dispatch->mallopt(param, value);
839}
840
Christopher Ferris6c619a02019-03-01 17:59:51 -0800841int debug_malloc_info(int options, FILE* fp) {
842 if (DebugCallsDisabled() || !g_debug->TrackPointers()) {
843 return g_dispatch->malloc_info(options, fp);
844 }
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800845
846 // Make sure any pending output is written to the file.
847 fflush(fp);
848
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700849 ScopedConcurrentLock lock;
850 ScopedDisableDebugCalls disable;
Christopher Ferris9bf78172020-05-20 15:37:30 -0700851 ScopedBacktraceSignalBlocker blocked;
Christopher Ferris6c619a02019-03-01 17:59:51 -0800852
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800853 // Avoid any issues where allocations are made that will be freed
854 // in the fclose.
855 int fd = fileno(fp);
856 MallocXmlElem root(fd, "malloc", "version=\"debug-malloc-1\"");
Christopher Ferris6c619a02019-03-01 17:59:51 -0800857 std::vector<ListInfoType> list;
858 PointerData::GetAllocList(&list);
859
860 size_t alloc_num = 0;
861 for (size_t i = 0; i < list.size(); i++) {
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800862 MallocXmlElem alloc(fd, "allocation", "nr=\"%zu\"", alloc_num);
Christopher Ferris6c619a02019-03-01 17:59:51 -0800863
864 size_t total = 1;
865 size_t size = list[i].size;
866 while (i < list.size() - 1 && list[i + 1].size == size) {
867 i++;
868 total++;
869 }
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800870 MallocXmlElem(fd, "size").Contents("%zu", list[i].size);
871 MallocXmlElem(fd, "total").Contents("%zu", total);
Christopher Ferris6c619a02019-03-01 17:59:51 -0800872 alloc_num++;
873 }
874 return 0;
875}
876
Christopher Ferriscae21a92018-02-05 18:14:55 -0800877void* debug_aligned_alloc(size_t alignment, size_t size) {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700878 Unreachable::CheckIfRequested(g_debug->config());
879
Christopher Ferriscae21a92018-02-05 18:14:55 -0800880 if (DebugCallsDisabled()) {
881 return g_dispatch->aligned_alloc(alignment, size);
882 }
Christopher Ferrisa22f5d52019-03-01 16:40:59 -0800883 if (!powerof2(alignment) || (size % alignment) != 0) {
Christopher Ferriscae21a92018-02-05 18:14:55 -0800884 errno = EINVAL;
885 return nullptr;
886 }
887 return debug_memalign(alignment, size);
888}
889
Christopher Ferris63860cb2015-11-16 17:30:32 -0800890int debug_posix_memalign(void** memptr, size_t alignment, size_t size) {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700891 Unreachable::CheckIfRequested(g_debug->config());
892
Christopher Ferris63860cb2015-11-16 17:30:32 -0800893 if (DebugCallsDisabled()) {
894 return g_dispatch->posix_memalign(memptr, alignment, size);
895 }
896
Christopher Ferris6c619a02019-03-01 17:59:51 -0800897 if (alignment < sizeof(void*) || !powerof2(alignment)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800898 return EINVAL;
899 }
900 int saved_errno = errno;
901 *memptr = debug_memalign(alignment, size);
902 errno = saved_errno;
903 return (*memptr != nullptr) ? 0 : ENOMEM;
904}
905
Christopher Ferris6f517cd2019-11-08 11:28:38 -0800906int debug_malloc_iterate(uintptr_t base, size_t size, void (*callback)(uintptr_t, size_t, void*),
Christopher Ferris4da25032018-03-07 13:38:48 -0800907 void* arg) {
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700908 ScopedConcurrentLock lock;
Christopher Ferris4da25032018-03-07 13:38:48 -0800909 if (g_debug->TrackPointers()) {
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700910 PointerData::IteratePointers([&callback, &arg](uintptr_t pointer) {
911 callback(pointer, InternalMallocUsableSize(reinterpret_cast<void*>(pointer)), arg);
912 });
Christopher Ferris4da25032018-03-07 13:38:48 -0800913 return 0;
914 }
Colin Cross869691c2016-01-29 12:48:18 -0800915
Christopher Ferris4da25032018-03-07 13:38:48 -0800916 // An option that adds a header will add pointer tracking, so no need to
917 // check if headers are enabled.
Christopher Ferris6f517cd2019-11-08 11:28:38 -0800918 return g_dispatch->malloc_iterate(base, size, callback, arg);
Colin Cross869691c2016-01-29 12:48:18 -0800919}
920
921void debug_malloc_disable() {
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700922 ScopedConcurrentLock lock;
Colin Cross869691c2016-01-29 12:48:18 -0800923 g_dispatch->malloc_disable();
Christopher Ferris4da25032018-03-07 13:38:48 -0800924 if (g_debug->pointer) {
925 g_debug->pointer->PrepareFork();
Colin Cross869691c2016-01-29 12:48:18 -0800926 }
927}
928
929void debug_malloc_enable() {
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700930 ScopedConcurrentLock lock;
Christopher Ferris4da25032018-03-07 13:38:48 -0800931 if (g_debug->pointer) {
932 g_debug->pointer->PostForkParent();
Colin Cross869691c2016-01-29 12:48:18 -0800933 }
934 g_dispatch->malloc_enable();
935}
936
Christopher Ferris4da25032018-03-07 13:38:48 -0800937ssize_t debug_malloc_backtrace(void* pointer, uintptr_t* frames, size_t max_frames) {
Colin Cross2d4721c2016-02-02 11:57:54 -0800938 if (DebugCallsDisabled() || pointer == nullptr) {
939 return 0;
940 }
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700941 ScopedConcurrentLock lock;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700942 ScopedDisableDebugCalls disable;
Christopher Ferris9bf78172020-05-20 15:37:30 -0700943 ScopedBacktraceSignalBlocker blocked;
Colin Cross2d4721c2016-02-02 11:57:54 -0800944
Christopher Ferris4da25032018-03-07 13:38:48 -0800945 if (!(g_debug->config().options() & BACKTRACE)) {
946 return 0;
Colin Cross2d4721c2016-02-02 11:57:54 -0800947 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800948 pointer = UntagPointer(pointer);
Christopher Ferris4da25032018-03-07 13:38:48 -0800949 return PointerData::GetFrames(pointer, frames, max_frames);
Colin Cross2d4721c2016-02-02 11:57:54 -0800950}
951
Christopher Ferris63860cb2015-11-16 17:30:32 -0800952#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
953void* debug_pvalloc(size_t bytes) {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700954 Unreachable::CheckIfRequested(g_debug->config());
955
Christopher Ferris63860cb2015-11-16 17:30:32 -0800956 if (DebugCallsDisabled()) {
957 return g_dispatch->pvalloc(bytes);
958 }
959
960 size_t pagesize = getpagesize();
Dan Alberta613d0d2017-10-05 16:39:33 -0700961 size_t size = __BIONIC_ALIGN(bytes, pagesize);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800962 if (size < bytes) {
963 // Overflow
964 errno = ENOMEM;
965 return nullptr;
966 }
967 return debug_memalign(pagesize, size);
968}
969
970void* debug_valloc(size_t size) {
Christopher Ferrisb42e8b42022-05-09 14:00:47 -0700971 Unreachable::CheckIfRequested(g_debug->config());
972
Christopher Ferris63860cb2015-11-16 17:30:32 -0800973 if (DebugCallsDisabled()) {
974 return g_dispatch->valloc(size);
975 }
976 return debug_memalign(getpagesize(), size);
977}
978#endif
Christopher Ferris602b88c2017-08-04 13:04:04 -0700979
980static std::mutex g_dump_lock;
981
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800982static void write_dump(int fd) {
983 dprintf(fd, "Android Native Heap Dump v1.2\n\n");
Christopher Ferris602b88c2017-08-04 13:04:04 -0700984
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700985 std::string fingerprint = android::base::GetProperty("ro.build.fingerprint", "unknown");
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800986 dprintf(fd, "Build fingerprint: '%s'\n\n", fingerprint.c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700987
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800988 PointerData::DumpLiveToFile(fd);
Christopher Ferris602b88c2017-08-04 13:04:04 -0700989
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800990 dprintf(fd, "MAPS\n");
Christopher Ferris602b88c2017-08-04 13:04:04 -0700991 std::string content;
992 if (!android::base::ReadFileToString("/proc/self/maps", &content)) {
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800993 dprintf(fd, "Could not open /proc/self/maps\n");
Christopher Ferris602b88c2017-08-04 13:04:04 -0700994 } else {
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800995 dprintf(fd, "%s", content.c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700996 }
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800997 dprintf(fd, "END\n");
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700998}
999
1000bool debug_write_malloc_leak_info(FILE* fp) {
Christopher Ferrisff88fb02019-11-04 18:40:00 -08001001 // Make sure any pending output is written to the file.
1002 fflush(fp);
1003
Christopher Ferrisd269fcc2019-05-06 19:03:59 -07001004 ScopedConcurrentLock lock;
Christopher Ferris2e1a40a2018-06-13 10:46:34 -07001005 ScopedDisableDebugCalls disable;
Christopher Ferris9bf78172020-05-20 15:37:30 -07001006 ScopedBacktraceSignalBlocker blocked;
Christopher Ferris2e1a40a2018-06-13 10:46:34 -07001007
1008 std::lock_guard<std::mutex> guard(g_dump_lock);
1009
1010 if (!(g_debug->config().options() & BACKTRACE)) {
1011 return false;
1012 }
1013
Christopher Ferrisff88fb02019-11-04 18:40:00 -08001014 write_dump(fileno(fp));
1015
Christopher Ferris602b88c2017-08-04 13:04:04 -07001016 return true;
1017}
Christopher Ferris2e1a40a2018-06-13 10:46:34 -07001018
1019void debug_dump_heap(const char* file_name) {
Christopher Ferrisd269fcc2019-05-06 19:03:59 -07001020 ScopedConcurrentLock lock;
Christopher Ferris2e1a40a2018-06-13 10:46:34 -07001021 ScopedDisableDebugCalls disable;
Christopher Ferris9bf78172020-05-20 15:37:30 -07001022 ScopedBacktraceSignalBlocker blocked;
Christopher Ferris2e1a40a2018-06-13 10:46:34 -07001023
1024 std::lock_guard<std::mutex> guard(g_dump_lock);
1025
Christopher Ferrisff88fb02019-11-04 18:40:00 -08001026 int fd = open(file_name, O_RDWR | O_CREAT | O_NOFOLLOW | O_TRUNC | O_CLOEXEC, 0644);
1027 if (fd == -1) {
Christopher Ferris2e1a40a2018-06-13 10:46:34 -07001028 error_log("Unable to create file: %s", file_name);
1029 return;
1030 }
1031
1032 error_log("Dumping to file: %s\n", file_name);
Christopher Ferrisff88fb02019-11-04 18:40:00 -08001033 write_dump(fd);
1034 close(fd);
Christopher Ferris2e1a40a2018-06-13 10:46:34 -07001035}