blob: d23ab15c379faa1eb2644f31a6466962981d5a00 [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 Ferris63860cb2015-11-16 17:30:32 -080052
Christopher Ferris72df6702016-02-11 15:51:31 -080053#include "Config.h"
Christopher Ferris63860cb2015-11-16 17:30:32 -080054#include "DebugData.h"
Christopher Ferris4da25032018-03-07 13:38:48 -080055#include "backtrace.h"
Christopher Ferris63860cb2015-11-16 17:30:32 -080056#include "debug_disable.h"
57#include "debug_log.h"
58#include "malloc_debug.h"
Christopher Ferris93bdd6a2018-04-05 11:12:38 -070059#include "UnwindBacktrace.h"
Christopher Ferris63860cb2015-11-16 17:30:32 -080060
61// ------------------------------------------------------------------------
62// Global Data
63// ------------------------------------------------------------------------
64DebugData* g_debug;
65
Christopher Ferris8189e772019-04-09 16:37:23 -070066bool* g_zygote_child;
Christopher Ferris63860cb2015-11-16 17:30:32 -080067
68const MallocDispatch* g_dispatch;
69// ------------------------------------------------------------------------
70
71// ------------------------------------------------------------------------
72// Use C style prototypes for all exported functions. This makes it easy
73// to do dlsym lookups during libc initialization when malloc debug
74// is enabled.
75// ------------------------------------------------------------------------
76__BEGIN_DECLS
77
Christopher Ferris8189e772019-04-09 16:37:23 -070078bool debug_initialize(const MallocDispatch* malloc_dispatch, bool* malloc_zygote_child,
Christopher Ferris4da25032018-03-07 13:38:48 -080079 const char* options);
Christopher Ferris63860cb2015-11-16 17:30:32 -080080void debug_finalize();
Christopher Ferris2e1a40a2018-06-13 10:46:34 -070081void debug_dump_heap(const char* file_name);
Christopher Ferris4da25032018-03-07 13:38:48 -080082void debug_get_malloc_leak_info(uint8_t** info, size_t* overall_size, size_t* info_size,
83 size_t* total_memory, size_t* backtrace_size);
Christopher Ferris2e1a40a2018-06-13 10:46:34 -070084bool debug_write_malloc_leak_info(FILE* fp);
Colin Cross2d4721c2016-02-02 11:57:54 -080085ssize_t debug_malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count);
Christopher Ferris63860cb2015-11-16 17:30:32 -080086void debug_free_malloc_leak_info(uint8_t* info);
87size_t debug_malloc_usable_size(void* pointer);
88void* debug_malloc(size_t size);
89void debug_free(void* pointer);
Christopher Ferriscae21a92018-02-05 18:14:55 -080090void* debug_aligned_alloc(size_t alignment, size_t size);
Christopher Ferris63860cb2015-11-16 17:30:32 -080091void* debug_memalign(size_t alignment, size_t bytes);
92void* debug_realloc(void* pointer, size_t bytes);
93void* debug_calloc(size_t nmemb, size_t bytes);
94struct mallinfo debug_mallinfo();
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -070095int debug_mallopt(int param, int value);
Christopher Ferris6c619a02019-03-01 17:59:51 -080096int debug_malloc_info(int options, FILE* fp);
Christopher Ferris63860cb2015-11-16 17:30:32 -080097int debug_posix_memalign(void** memptr, size_t alignment, size_t size);
Christopher Ferris6f517cd2019-11-08 11:28:38 -080098int debug_malloc_iterate(uintptr_t base, size_t size,
99 void (*callback)(uintptr_t base, size_t size, void* arg), void* arg);
Colin Cross869691c2016-01-29 12:48:18 -0800100void debug_malloc_disable();
101void debug_malloc_enable();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800102
103#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
104void* debug_pvalloc(size_t bytes);
105void* debug_valloc(size_t size);
106#endif
107
108__END_DECLS
109// ------------------------------------------------------------------------
110
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700111class ScopedConcurrentLock {
112 public:
113 ScopedConcurrentLock() {
114 pthread_rwlock_rdlock(&lock_);
115 }
116 ~ScopedConcurrentLock() {
117 pthread_rwlock_unlock(&lock_);
118 }
119
120 static void Init() {
121 pthread_rwlockattr_t attr;
122 // Set the attribute so that when a write lock is pending, read locks are no
123 // longer granted.
124 pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
125 pthread_rwlock_init(&lock_, &attr);
126 }
127
128 static void BlockAllOperations() {
129 pthread_rwlock_wrlock(&lock_);
130 }
131
132 private:
133 static pthread_rwlock_t lock_;
134};
135pthread_rwlock_t ScopedConcurrentLock::lock_;
136
Christopher Ferris9bf78172020-05-20 15:37:30 -0700137// Use this because the sigprocmask* functions filter out the reserved bionic
138// signals including the signal this code blocks.
139static inline int __rt_sigprocmask(int how, const sigset64_t* new_set, sigset64_t* old_set,
140 size_t sigset_size) {
141 return syscall(SYS_rt_sigprocmask, how, new_set, old_set, sigset_size);
142}
143
144// Need to block the backtrace signal while in malloc debug routines
145// otherwise there is a chance of a deadlock and timeout when unwinding.
146// This can occur if a thread is paused while owning a malloc debug
147// internal lock.
148class ScopedBacktraceSignalBlocker {
149 public:
150 ScopedBacktraceSignalBlocker() {
151 sigemptyset64(&backtrace_set_);
152 sigaddset64(&backtrace_set_, BIONIC_SIGNAL_BACKTRACE);
153 sigset64_t old_set;
154 __rt_sigprocmask(SIG_BLOCK, &backtrace_set_, &old_set, sizeof(backtrace_set_));
155 if (sigismember64(&old_set, BIONIC_SIGNAL_BACKTRACE)) {
156 unblock_ = false;
157 }
158 }
159
160 ~ScopedBacktraceSignalBlocker() {
161 if (unblock_) {
162 __rt_sigprocmask(SIG_UNBLOCK, &backtrace_set_, nullptr, sizeof(backtrace_set_));
163 }
164 }
165
166 private:
167 bool unblock_ = true;
168 sigset64_t backtrace_set_;
169};
170
Colin Cross7a28a3c2016-02-07 22:51:15 -0800171static void InitAtfork() {
172 static pthread_once_t atfork_init = PTHREAD_ONCE_INIT;
Christopher Ferris4da25032018-03-07 13:38:48 -0800173 pthread_once(&atfork_init, []() {
Colin Cross7a28a3c2016-02-07 22:51:15 -0800174 pthread_atfork(
Christopher Ferris4da25032018-03-07 13:38:48 -0800175 []() {
Colin Cross7a28a3c2016-02-07 22:51:15 -0800176 if (g_debug != nullptr) {
177 g_debug->PrepareFork();
178 }
179 },
Christopher Ferris4da25032018-03-07 13:38:48 -0800180 []() {
Colin Cross7a28a3c2016-02-07 22:51:15 -0800181 if (g_debug != nullptr) {
182 g_debug->PostForkParent();
183 }
184 },
Christopher Ferris4da25032018-03-07 13:38:48 -0800185 []() {
Colin Cross7a28a3c2016-02-07 22:51:15 -0800186 if (g_debug != nullptr) {
187 g_debug->PostForkChild();
188 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800189 });
Colin Cross7a28a3c2016-02-07 22:51:15 -0800190 });
191}
Christopher Ferrisd0919622016-03-15 22:39:39 -0700192
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700193void BacktraceAndLog() {
194 if (g_debug->config().options() & BACKTRACE_FULL) {
195 std::vector<uintptr_t> frames;
196 std::vector<unwindstack::LocalFrameData> frames_info;
197 if (!Unwind(&frames, &frames_info, 256)) {
198 error_log(" Backtrace failed to get any frames.");
199 } else {
200 UnwindLog(frames_info);
201 }
202 } else {
203 std::vector<uintptr_t> frames(256);
204 size_t num_frames = backtrace_get(frames.data(), frames.size());
205 if (num_frames == 0) {
206 error_log(" Backtrace failed to get any frames.");
207 } else {
208 backtrace_log(frames.data(), num_frames);
209 }
210 }
211}
212
Christopher Ferris4da25032018-03-07 13:38:48 -0800213static void LogError(const void* pointer, const char* error_str) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800214 error_log(LOG_DIVIDER);
Christopher Ferris4da25032018-03-07 13:38:48 -0800215 error_log("+++ ALLOCATION %p %s", pointer, error_str);
216
217 // If we are tracking already freed pointers, check to see if this is
218 // one so we can print extra information.
219 if (g_debug->config().options() & FREE_TRACK) {
220 PointerData::LogFreeBacktrace(pointer);
Christopher Ferris7993b802016-01-28 18:35:05 -0800221 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800222
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700223 error_log("Backtrace at time of failure:");
224 BacktraceAndLog();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800225 error_log(LOG_DIVIDER);
Iris Chang7f209a92019-01-16 11:17:15 +0800226 if (g_debug->config().options() & ABORT_ON_ERROR) {
227 abort();
228 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800229}
230
Christopher Ferris4da25032018-03-07 13:38:48 -0800231static bool VerifyPointer(const void* pointer, const char* function_name) {
232 if (g_debug->HeaderEnabled()) {
233 Header* header = g_debug->GetHeader(pointer);
234 if (header->tag != DEBUG_TAG) {
235 std::string error_str;
236 if (header->tag == DEBUG_FREE_TAG) {
237 error_str = std::string("USED AFTER FREE (") + function_name + ")";
238 } else {
239 error_str = android::base::StringPrintf("HAS INVALID TAG %" PRIx32 " (%s)", header->tag,
240 function_name);
241 }
242 LogError(pointer, error_str.c_str());
243 return false;
244 }
245 }
246
247 if (g_debug->TrackPointers()) {
248 if (!PointerData::Exists(pointer)) {
249 std::string error_str(std::string("UNKNOWN POINTER (") + function_name + ")");
250 LogError(pointer, error_str.c_str());
251 return false;
252 }
253 }
254 return true;
255}
256
257static size_t InternalMallocUsableSize(void* pointer) {
258 if (g_debug->HeaderEnabled()) {
259 return g_debug->GetHeader(pointer)->usable_size;
260 } else {
261 return g_dispatch->malloc_usable_size(pointer);
262 }
263}
264
Christopher Ferris63860cb2015-11-16 17:30:32 -0800265static void* InitHeader(Header* header, void* orig_pointer, size_t size) {
266 header->tag = DEBUG_TAG;
267 header->orig_pointer = orig_pointer;
268 header->size = size;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800269 header->usable_size = g_dispatch->malloc_usable_size(orig_pointer);
270 if (header->usable_size == 0) {
271 g_dispatch->free(orig_pointer);
272 return nullptr;
273 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800274 header->usable_size -= g_debug->pointer_offset() + reinterpret_cast<uintptr_t>(header) -
275 reinterpret_cast<uintptr_t>(orig_pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800276
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700277 if (g_debug->config().options() & FRONT_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800278 uint8_t* guard = g_debug->GetFrontGuard(header);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700279 memset(guard, g_debug->config().front_guard_value(), g_debug->config().front_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800280 }
281
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700282 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800283 uint8_t* guard = g_debug->GetRearGuard(header);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700284 memset(guard, g_debug->config().rear_guard_value(), g_debug->config().rear_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800285 // If the rear guard is enabled, set the usable size to the exact size
286 // of the allocation.
Christopher Ferris4da25032018-03-07 13:38:48 -0800287 header->usable_size = header->size;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800288 }
289
290 return g_debug->GetPointer(header);
291}
292
Christopher Ferris705de3c2019-05-22 13:39:57 -0700293extern "C" void __asan_init() __attribute__((weak));
294
Christopher Ferris8189e772019-04-09 16:37:23 -0700295bool debug_initialize(const MallocDispatch* malloc_dispatch, bool* zygote_child,
Christopher Ferris4da25032018-03-07 13:38:48 -0800296 const char* options) {
Christopher Ferris8189e772019-04-09 16:37:23 -0700297 if (zygote_child == nullptr || options == nullptr) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800298 return false;
299 }
Colin Cross7a28a3c2016-02-07 22:51:15 -0800300
Christopher Ferris705de3c2019-05-22 13:39:57 -0700301 if (__asan_init != 0) {
302 error_log("malloc debug cannot be enabled alongside ASAN");
303 return false;
304 }
305
Colin Cross7a28a3c2016-02-07 22:51:15 -0800306 InitAtfork();
307
Christopher Ferris8189e772019-04-09 16:37:23 -0700308 g_zygote_child = zygote_child;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800309
310 g_dispatch = malloc_dispatch;
311
312 if (!DebugDisableInitialize()) {
313 return false;
314 }
315
316 DebugData* debug = new DebugData();
Tamas Berghammerac81fe82016-08-26 15:54:59 +0100317 if (!debug->Initialize(options)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800318 delete debug;
319 DebugDisableFinalize();
320 return false;
321 }
322 g_debug = debug;
323
324 // Always enable the backtrace code since we will use it in a number
325 // of different error cases.
326 backtrace_startup();
327
Christopher Ferrisc328e442019-04-01 19:31:26 -0700328 if (g_debug->config().options() & VERBOSE) {
329 info_log("%s: malloc debug enabled", getprogname());
330 }
331
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700332 ScopedConcurrentLock::Init();
333
Christopher Ferris63860cb2015-11-16 17:30:32 -0800334 return true;
335}
336
337void debug_finalize() {
338 if (g_debug == nullptr) {
339 return;
340 }
341
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700342 // Make sure that there are no other threads doing debug allocations
343 // before we kill everything.
344 ScopedConcurrentLock::BlockAllOperations();
345
Christopher Ferris97b47472018-07-10 14:45:24 -0700346 // Turn off capturing allocations calls.
347 DebugDisableSet(true);
348
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700349 if (g_debug->config().options() & FREE_TRACK) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800350 PointerData::VerifyAllFreed();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800351 }
352
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700353 if (g_debug->config().options() & LEAK_TRACK) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800354 PointerData::LogLeaks();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800355 }
356
Christopher Ferris602b88c2017-08-04 13:04:04 -0700357 if ((g_debug->config().options() & BACKTRACE) && g_debug->config().backtrace_dump_on_exit()) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800358 debug_dump_heap(android::base::StringPrintf("%s.%d.exit.txt",
359 g_debug->config().backtrace_dump_prefix().c_str(),
Christopher Ferris97b47472018-07-10 14:45:24 -0700360 getpid()).c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700361 }
362
Colin Cross2c759912016-02-05 16:17:39 -0800363 backtrace_shutdown();
364
Christopher Ferris33d73372021-07-02 15:46:18 -0700365 // In order to prevent any issues of threads freeing previous pointers
366 // after the main thread calls this code, simply leak the g_debug pointer
367 // and do not destroy the debug disable pthread key.
Christopher Ferris63860cb2015-11-16 17:30:32 -0800368}
369
Christopher Ferris4da25032018-03-07 13:38:48 -0800370void debug_get_malloc_leak_info(uint8_t** info, size_t* overall_size, size_t* info_size,
371 size_t* total_memory, size_t* backtrace_size) {
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700372 ScopedConcurrentLock lock;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800373 ScopedDisableDebugCalls disable;
Christopher Ferris9bf78172020-05-20 15:37:30 -0700374 ScopedBacktraceSignalBlocker blocked;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800375
376 // Verify the arguments.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700377 if (info == nullptr || overall_size == nullptr || info_size == nullptr || total_memory == nullptr ||
Christopher Ferris4da25032018-03-07 13:38:48 -0800378 backtrace_size == nullptr) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800379 error_log("get_malloc_leak_info: At least one invalid parameter.");
380 return;
381 }
382
383 *info = nullptr;
384 *overall_size = 0;
385 *info_size = 0;
386 *total_memory = 0;
387 *backtrace_size = 0;
388
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700389 if (!(g_debug->config().options() & BACKTRACE)) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800390 error_log(
391 "get_malloc_leak_info: Allocations not being tracked, to enable "
392 "set the option 'backtrace'.");
Christopher Ferris63860cb2015-11-16 17:30:32 -0800393 return;
394 }
395
Christopher Ferris4da25032018-03-07 13:38:48 -0800396 PointerData::GetInfo(info, overall_size, info_size, total_memory, backtrace_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800397}
398
399void debug_free_malloc_leak_info(uint8_t* info) {
400 g_dispatch->free(info);
401}
402
Christopher Ferris55a89a42016-04-07 17:14:53 -0700403size_t debug_malloc_usable_size(void* pointer) {
404 if (DebugCallsDisabled() || pointer == nullptr) {
405 return g_dispatch->malloc_usable_size(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800406 }
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700407 ScopedConcurrentLock lock;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700408 ScopedDisableDebugCalls disable;
Christopher Ferris9bf78172020-05-20 15:37:30 -0700409 ScopedBacktraceSignalBlocker blocked;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800410
Christopher Ferris4da25032018-03-07 13:38:48 -0800411 if (!VerifyPointer(pointer, "malloc_usable_size")) {
412 return 0;
413 }
414
415 return InternalMallocUsableSize(pointer);
Christopher Ferris55a89a42016-04-07 17:14:53 -0700416}
417
Christopher Ferris4da25032018-03-07 13:38:48 -0800418static void* InternalMalloc(size_t size) {
419 if ((g_debug->config().options() & BACKTRACE) && g_debug->pointer->ShouldDumpAndReset()) {
420 debug_dump_heap(android::base::StringPrintf(
421 "%s.%d.txt", g_debug->config().backtrace_dump_prefix().c_str(), getpid())
422 .c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700423 }
424
Colin Cross9567c7b2016-03-09 17:56:14 -0800425 if (size == 0) {
426 size = 1;
427 }
428
Christopher Ferris63860cb2015-11-16 17:30:32 -0800429 size_t real_size = size + g_debug->extra_bytes();
430 if (real_size < size) {
431 // Overflow.
432 errno = ENOMEM;
433 return nullptr;
434 }
435
Christopher Ferris4da25032018-03-07 13:38:48 -0800436 if (size > PointerInfoType::MaxSize()) {
437 errno = ENOMEM;
438 return nullptr;
439 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800440
Christopher Ferris4da25032018-03-07 13:38:48 -0800441 void* pointer;
442 if (g_debug->HeaderEnabled()) {
443 Header* header =
444 reinterpret_cast<Header*>(g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800445 if (header == nullptr) {
446 return nullptr;
447 }
448 pointer = InitHeader(header, header, size);
449 } else {
450 pointer = g_dispatch->malloc(real_size);
451 }
452
Christopher Ferris4da25032018-03-07 13:38:48 -0800453 if (pointer != nullptr) {
454 if (g_debug->TrackPointers()) {
455 PointerData::Add(pointer, size);
456 }
457
458 if (g_debug->config().options() & FILL_ON_ALLOC) {
459 size_t bytes = InternalMallocUsableSize(pointer);
460 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes();
461 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
462 memset(pointer, g_debug->config().fill_alloc_value(), bytes);
463 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800464 }
465 return pointer;
466}
467
Christopher Ferris55a89a42016-04-07 17:14:53 -0700468void* debug_malloc(size_t size) {
469 if (DebugCallsDisabled()) {
470 return g_dispatch->malloc(size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800471 }
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700472 ScopedConcurrentLock lock;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700473 ScopedDisableDebugCalls disable;
Christopher Ferris9bf78172020-05-20 15:37:30 -0700474 ScopedBacktraceSignalBlocker blocked;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800475
Christopher Ferris4da25032018-03-07 13:38:48 -0800476 void* pointer = InternalMalloc(size);
Christopher Ferris7bd01782016-04-20 12:30:58 -0700477
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700478 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700479 g_debug->record->AddEntry(new MallocEntry(pointer, size));
480 }
481
482 return pointer;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700483}
484
Christopher Ferris4da25032018-03-07 13:38:48 -0800485static void InternalFree(void* pointer) {
486 if ((g_debug->config().options() & BACKTRACE) && g_debug->pointer->ShouldDumpAndReset()) {
487 debug_dump_heap(android::base::StringPrintf(
488 "%s.%d.txt", g_debug->config().backtrace_dump_prefix().c_str(), getpid())
489 .c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700490 }
491
Christopher Ferris63860cb2015-11-16 17:30:32 -0800492 void* free_pointer = pointer;
493 size_t bytes;
Christopher Ferrisd0919622016-03-15 22:39:39 -0700494 Header* header;
Christopher Ferris4da25032018-03-07 13:38:48 -0800495 if (g_debug->HeaderEnabled()) {
Christopher Ferrisd0919622016-03-15 22:39:39 -0700496 header = g_debug->GetHeader(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800497 free_pointer = header->orig_pointer;
498
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700499 if (g_debug->config().options() & FRONT_GUARD) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700500 if (!g_debug->front_guard->Valid(header)) {
501 g_debug->front_guard->LogFailure(header);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800502 }
503 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700504 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700505 if (!g_debug->rear_guard->Valid(header)) {
506 g_debug->rear_guard->LogFailure(header);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800507 }
508 }
509
Christopher Ferris7993b802016-01-28 18:35:05 -0800510 header->tag = DEBUG_FREE_TAG;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800511
512 bytes = header->usable_size;
513 } else {
514 bytes = g_dispatch->malloc_usable_size(pointer);
515 }
516
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700517 if (g_debug->config().options() & FILL_ON_FREE) {
518 size_t fill_bytes = g_debug->config().fill_on_free_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800519 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700520 memset(pointer, g_debug->config().fill_free_value(), bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800521 }
522
Christopher Ferris4da25032018-03-07 13:38:48 -0800523 if (g_debug->TrackPointers()) {
524 PointerData::Remove(pointer);
525 }
526
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700527 if (g_debug->config().options() & FREE_TRACK) {
Christopher Ferrisd0919622016-03-15 22:39:39 -0700528 // Do not add the allocation until we are done modifying the pointer
529 // itself. This avoids a race if a lot of threads are all doing
530 // frees at the same time and we wind up trying to really free this
531 // pointer from another thread, while still trying to free it in
532 // this function.
Christopher Ferris4da25032018-03-07 13:38:48 -0800533 pointer = PointerData::AddFreed(pointer);
534 if (pointer != nullptr) {
535 if (g_debug->HeaderEnabled()) {
536 pointer = g_debug->GetHeader(pointer)->orig_pointer;
537 }
538 g_dispatch->free(pointer);
539 }
Christopher Ferrisd0919622016-03-15 22:39:39 -0700540 } else {
541 g_dispatch->free(free_pointer);
542 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800543}
544
Christopher Ferris55a89a42016-04-07 17:14:53 -0700545void debug_free(void* pointer) {
546 if (DebugCallsDisabled() || pointer == nullptr) {
547 return g_dispatch->free(pointer);
548 }
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700549 ScopedConcurrentLock lock;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700550 ScopedDisableDebugCalls disable;
Christopher Ferris9bf78172020-05-20 15:37:30 -0700551 ScopedBacktraceSignalBlocker blocked;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700552
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700553 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700554 g_debug->record->AddEntry(new FreeEntry(pointer));
555 }
556
Christopher Ferris4da25032018-03-07 13:38:48 -0800557 if (!VerifyPointer(pointer, "free")) {
558 return;
559 }
560
561 InternalFree(pointer);
Christopher Ferris55a89a42016-04-07 17:14:53 -0700562}
563
Christopher Ferris63860cb2015-11-16 17:30:32 -0800564void* debug_memalign(size_t alignment, size_t bytes) {
565 if (DebugCallsDisabled()) {
566 return g_dispatch->memalign(alignment, bytes);
567 }
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700568 ScopedConcurrentLock lock;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700569 ScopedDisableDebugCalls disable;
Christopher Ferris9bf78172020-05-20 15:37:30 -0700570 ScopedBacktraceSignalBlocker blocked;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800571
Colin Cross9567c7b2016-03-09 17:56:14 -0800572 if (bytes == 0) {
573 bytes = 1;
574 }
575
Christopher Ferris4da25032018-03-07 13:38:48 -0800576 if (bytes > PointerInfoType::MaxSize()) {
577 errno = ENOMEM;
578 return nullptr;
579 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800580
Christopher Ferris4da25032018-03-07 13:38:48 -0800581 void* pointer;
582 if (g_debug->HeaderEnabled()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800583 // Make the alignment a power of two.
584 if (!powerof2(alignment)) {
585 alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment);
586 }
Christopher Ferris72df6702016-02-11 15:51:31 -0800587 // Force the alignment to at least MINIMUM_ALIGNMENT_BYTES to guarantee
Christopher Ferris63860cb2015-11-16 17:30:32 -0800588 // that the header is aligned properly.
Christopher Ferris72df6702016-02-11 15:51:31 -0800589 if (alignment < MINIMUM_ALIGNMENT_BYTES) {
590 alignment = MINIMUM_ALIGNMENT_BYTES;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800591 }
592
593 // We don't have any idea what the natural alignment of
594 // the underlying native allocator is, so we always need to
595 // over allocate.
596 size_t real_size = alignment + bytes + g_debug->extra_bytes();
597 if (real_size < bytes) {
598 // Overflow.
599 errno = ENOMEM;
600 return nullptr;
601 }
602
603 pointer = g_dispatch->malloc(real_size);
604 if (pointer == nullptr) {
605 return nullptr;
606 }
607
608 uintptr_t value = reinterpret_cast<uintptr_t>(pointer) + g_debug->pointer_offset();
609 // Now align the pointer.
610 value += (-value % alignment);
611
612 Header* header = g_debug->GetHeader(reinterpret_cast<void*>(value));
613 pointer = InitHeader(header, pointer, bytes);
614 } else {
615 size_t real_size = bytes + g_debug->extra_bytes();
616 if (real_size < bytes) {
617 // Overflow.
618 errno = ENOMEM;
619 return nullptr;
620 }
621 pointer = g_dispatch->memalign(alignment, real_size);
622 }
623
Christopher Ferris4da25032018-03-07 13:38:48 -0800624 if (pointer != nullptr) {
625 if (g_debug->TrackPointers()) {
626 PointerData::Add(pointer, bytes);
627 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700628
Christopher Ferris4da25032018-03-07 13:38:48 -0800629 if (g_debug->config().options() & FILL_ON_ALLOC) {
630 size_t bytes = InternalMallocUsableSize(pointer);
631 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes();
632 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
633 memset(pointer, g_debug->config().fill_alloc_value(), bytes);
634 }
635
636 if (g_debug->config().options() & RECORD_ALLOCS) {
637 g_debug->record->AddEntry(new MemalignEntry(pointer, bytes, alignment));
638 }
Christopher Ferris7bd01782016-04-20 12:30:58 -0700639 }
640
Christopher Ferris63860cb2015-11-16 17:30:32 -0800641 return pointer;
642}
643
644void* debug_realloc(void* pointer, size_t bytes) {
645 if (DebugCallsDisabled()) {
646 return g_dispatch->realloc(pointer, bytes);
647 }
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700648 ScopedConcurrentLock lock;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700649 ScopedDisableDebugCalls disable;
Christopher Ferris9bf78172020-05-20 15:37:30 -0700650 ScopedBacktraceSignalBlocker blocked;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800651
652 if (pointer == nullptr) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800653 pointer = InternalMalloc(bytes);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700654 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700655 g_debug->record->AddEntry(new ReallocEntry(pointer, bytes, nullptr));
656 }
657 return pointer;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800658 }
659
Christopher Ferris4da25032018-03-07 13:38:48 -0800660 if (!VerifyPointer(pointer, "realloc")) {
661 return nullptr;
662 }
663
Christopher Ferris63860cb2015-11-16 17:30:32 -0800664 if (bytes == 0) {
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700665 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700666 g_debug->record->AddEntry(new ReallocEntry(nullptr, bytes, pointer));
667 }
668
Christopher Ferris4da25032018-03-07 13:38:48 -0800669 InternalFree(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800670 return nullptr;
671 }
672
673 size_t real_size = bytes;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700674 if (g_debug->config().options() & EXPAND_ALLOC) {
675 real_size += g_debug->config().expand_alloc_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800676 if (real_size < bytes) {
677 // Overflow.
678 errno = ENOMEM;
679 return nullptr;
680 }
681 }
682
Christopher Ferris4da25032018-03-07 13:38:48 -0800683 if (bytes > PointerInfoType::MaxSize()) {
684 errno = ENOMEM;
685 return nullptr;
686 }
687
Christopher Ferris63860cb2015-11-16 17:30:32 -0800688 void* new_pointer;
689 size_t prev_size;
Christopher Ferris4da25032018-03-07 13:38:48 -0800690 if (g_debug->HeaderEnabled()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800691 // Same size, do nothing.
Christopher Ferris4da25032018-03-07 13:38:48 -0800692 Header* header = g_debug->GetHeader(pointer);
693 if (real_size == header->size) {
694 if (g_debug->TrackPointers()) {
695 // Remove and re-add so that the backtrace is updated.
696 PointerData::Remove(pointer);
697 PointerData::Add(pointer, real_size);
698 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800699 return pointer;
700 }
701
702 // Allocation is shrinking.
703 if (real_size < header->usable_size) {
704 header->size = real_size;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700705 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800706 // Don't bother allocating a smaller pointer in this case, simply
707 // change the header usable_size and reset the rear guard.
Christopher Ferris4da25032018-03-07 13:38:48 -0800708 header->usable_size = header->size;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700709 memset(g_debug->GetRearGuard(header), g_debug->config().rear_guard_value(),
710 g_debug->config().rear_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800711 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800712 if (g_debug->TrackPointers()) {
713 // Remove and re-add so that the backtrace is updated.
714 PointerData::Remove(pointer);
715 PointerData::Add(pointer, real_size);
716 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800717 return pointer;
718 }
719
720 // Allocate the new size.
Christopher Ferris4da25032018-03-07 13:38:48 -0800721 new_pointer = InternalMalloc(bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800722 if (new_pointer == nullptr) {
723 errno = ENOMEM;
724 return nullptr;
725 }
726
727 prev_size = header->usable_size;
728 memcpy(new_pointer, pointer, prev_size);
Christopher Ferris4da25032018-03-07 13:38:48 -0800729 InternalFree(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800730 } else {
Christopher Ferris4da25032018-03-07 13:38:48 -0800731 if (g_debug->TrackPointers()) {
732 PointerData::Remove(pointer);
733 }
734
Christopher Ferris63860cb2015-11-16 17:30:32 -0800735 prev_size = g_dispatch->malloc_usable_size(pointer);
736 new_pointer = g_dispatch->realloc(pointer, real_size);
737 if (new_pointer == nullptr) {
738 return nullptr;
739 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800740
741 if (g_debug->TrackPointers()) {
742 PointerData::Add(new_pointer, real_size);
743 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800744 }
745
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700746 if (g_debug->config().options() & FILL_ON_ALLOC) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800747 size_t bytes = InternalMallocUsableSize(new_pointer);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700748 if (bytes > g_debug->config().fill_on_alloc_bytes()) {
749 bytes = g_debug->config().fill_on_alloc_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800750 }
751 if (bytes > prev_size) {
752 memset(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(new_pointer) + prev_size),
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700753 g_debug->config().fill_alloc_value(), bytes - prev_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800754 }
755 }
756
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700757 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700758 g_debug->record->AddEntry(new ReallocEntry(new_pointer, bytes, pointer));
759 }
760
Christopher Ferris63860cb2015-11-16 17:30:32 -0800761 return new_pointer;
762}
763
764void* debug_calloc(size_t nmemb, size_t bytes) {
765 if (DebugCallsDisabled()) {
766 return g_dispatch->calloc(nmemb, bytes);
767 }
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700768 ScopedConcurrentLock lock;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700769 ScopedDisableDebugCalls disable;
Christopher Ferris9bf78172020-05-20 15:37:30 -0700770 ScopedBacktraceSignalBlocker blocked;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800771
Colin Cross7877df62016-03-10 13:01:27 -0800772 size_t size;
773 if (__builtin_mul_overflow(nmemb, bytes, &size)) {
774 // Overflow
775 errno = ENOMEM;
776 return nullptr;
777 }
778
Colin Cross9567c7b2016-03-09 17:56:14 -0800779 if (size == 0) {
780 size = 1;
781 }
782
Colin Cross7877df62016-03-10 13:01:27 -0800783 size_t real_size;
784 if (__builtin_add_overflow(size, g_debug->extra_bytes(), &real_size)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800785 // Overflow.
786 errno = ENOMEM;
787 return nullptr;
788 }
789
Christopher Ferris4da25032018-03-07 13:38:48 -0800790 if (real_size > PointerInfoType::MaxSize()) {
791 errno = ENOMEM;
792 return nullptr;
793 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800794
Christopher Ferris4da25032018-03-07 13:38:48 -0800795 void* pointer;
796 if (g_debug->HeaderEnabled()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800797 // Need to guarantee the alignment of the header.
Christopher Ferris4da25032018-03-07 13:38:48 -0800798 Header* header =
799 reinterpret_cast<Header*>(g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800800 if (header == nullptr) {
801 return nullptr;
802 }
803 memset(header, 0, g_dispatch->malloc_usable_size(header));
Christopher Ferris7bd01782016-04-20 12:30:58 -0700804 pointer = InitHeader(header, header, size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800805 } else {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700806 pointer = g_dispatch->calloc(1, real_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800807 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800808
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700809 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700810 g_debug->record->AddEntry(new CallocEntry(pointer, bytes, nmemb));
811 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800812
813 if (pointer != nullptr && g_debug->TrackPointers()) {
814 PointerData::Add(pointer, size);
815 }
Christopher Ferris7bd01782016-04-20 12:30:58 -0700816 return pointer;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800817}
818
819struct mallinfo debug_mallinfo() {
820 return g_dispatch->mallinfo();
821}
822
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700823int debug_mallopt(int param, int value) {
824 return g_dispatch->mallopt(param, value);
825}
826
Christopher Ferris6c619a02019-03-01 17:59:51 -0800827int debug_malloc_info(int options, FILE* fp) {
828 if (DebugCallsDisabled() || !g_debug->TrackPointers()) {
829 return g_dispatch->malloc_info(options, fp);
830 }
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800831
832 // Make sure any pending output is written to the file.
833 fflush(fp);
834
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700835 ScopedConcurrentLock lock;
836 ScopedDisableDebugCalls disable;
Christopher Ferris9bf78172020-05-20 15:37:30 -0700837 ScopedBacktraceSignalBlocker blocked;
Christopher Ferris6c619a02019-03-01 17:59:51 -0800838
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800839 // Avoid any issues where allocations are made that will be freed
840 // in the fclose.
841 int fd = fileno(fp);
842 MallocXmlElem root(fd, "malloc", "version=\"debug-malloc-1\"");
Christopher Ferris6c619a02019-03-01 17:59:51 -0800843 std::vector<ListInfoType> list;
844 PointerData::GetAllocList(&list);
845
846 size_t alloc_num = 0;
847 for (size_t i = 0; i < list.size(); i++) {
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800848 MallocXmlElem alloc(fd, "allocation", "nr=\"%zu\"", alloc_num);
Christopher Ferris6c619a02019-03-01 17:59:51 -0800849
850 size_t total = 1;
851 size_t size = list[i].size;
852 while (i < list.size() - 1 && list[i + 1].size == size) {
853 i++;
854 total++;
855 }
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800856 MallocXmlElem(fd, "size").Contents("%zu", list[i].size);
857 MallocXmlElem(fd, "total").Contents("%zu", total);
Christopher Ferris6c619a02019-03-01 17:59:51 -0800858 alloc_num++;
859 }
860 return 0;
861}
862
Christopher Ferriscae21a92018-02-05 18:14:55 -0800863void* debug_aligned_alloc(size_t alignment, size_t size) {
864 if (DebugCallsDisabled()) {
865 return g_dispatch->aligned_alloc(alignment, size);
866 }
Christopher Ferrisa22f5d52019-03-01 16:40:59 -0800867 if (!powerof2(alignment) || (size % alignment) != 0) {
Christopher Ferriscae21a92018-02-05 18:14:55 -0800868 errno = EINVAL;
869 return nullptr;
870 }
871 return debug_memalign(alignment, size);
872}
873
Christopher Ferris63860cb2015-11-16 17:30:32 -0800874int debug_posix_memalign(void** memptr, size_t alignment, size_t size) {
875 if (DebugCallsDisabled()) {
876 return g_dispatch->posix_memalign(memptr, alignment, size);
877 }
878
Christopher Ferris6c619a02019-03-01 17:59:51 -0800879 if (alignment < sizeof(void*) || !powerof2(alignment)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800880 return EINVAL;
881 }
882 int saved_errno = errno;
883 *memptr = debug_memalign(alignment, size);
884 errno = saved_errno;
885 return (*memptr != nullptr) ? 0 : ENOMEM;
886}
887
Christopher Ferris6f517cd2019-11-08 11:28:38 -0800888int debug_malloc_iterate(uintptr_t base, size_t size, void (*callback)(uintptr_t, size_t, void*),
Christopher Ferris4da25032018-03-07 13:38:48 -0800889 void* arg) {
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700890 ScopedConcurrentLock lock;
Christopher Ferris4da25032018-03-07 13:38:48 -0800891 if (g_debug->TrackPointers()) {
892 // Since malloc is disabled, don't bother acquiring any locks.
893 for (auto it = PointerData::begin(); it != PointerData::end(); ++it) {
894 callback(it->first, InternalMallocUsableSize(reinterpret_cast<void*>(it->first)), arg);
895 }
896 return 0;
897 }
Colin Cross869691c2016-01-29 12:48:18 -0800898
Christopher Ferris4da25032018-03-07 13:38:48 -0800899 // An option that adds a header will add pointer tracking, so no need to
900 // check if headers are enabled.
Christopher Ferris6f517cd2019-11-08 11:28:38 -0800901 return g_dispatch->malloc_iterate(base, size, callback, arg);
Colin Cross869691c2016-01-29 12:48:18 -0800902}
903
904void debug_malloc_disable() {
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700905 ScopedConcurrentLock lock;
Colin Cross869691c2016-01-29 12:48:18 -0800906 g_dispatch->malloc_disable();
Christopher Ferris4da25032018-03-07 13:38:48 -0800907 if (g_debug->pointer) {
908 g_debug->pointer->PrepareFork();
Colin Cross869691c2016-01-29 12:48:18 -0800909 }
910}
911
912void debug_malloc_enable() {
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700913 ScopedConcurrentLock lock;
Christopher Ferris4da25032018-03-07 13:38:48 -0800914 if (g_debug->pointer) {
915 g_debug->pointer->PostForkParent();
Colin Cross869691c2016-01-29 12:48:18 -0800916 }
917 g_dispatch->malloc_enable();
918}
919
Christopher Ferris4da25032018-03-07 13:38:48 -0800920ssize_t debug_malloc_backtrace(void* pointer, uintptr_t* frames, size_t max_frames) {
Colin Cross2d4721c2016-02-02 11:57:54 -0800921 if (DebugCallsDisabled() || pointer == nullptr) {
922 return 0;
923 }
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700924 ScopedConcurrentLock lock;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700925 ScopedDisableDebugCalls disable;
Christopher Ferris9bf78172020-05-20 15:37:30 -0700926 ScopedBacktraceSignalBlocker blocked;
Colin Cross2d4721c2016-02-02 11:57:54 -0800927
Christopher Ferris4da25032018-03-07 13:38:48 -0800928 if (!(g_debug->config().options() & BACKTRACE)) {
929 return 0;
Colin Cross2d4721c2016-02-02 11:57:54 -0800930 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800931 pointer = UntagPointer(pointer);
Christopher Ferris4da25032018-03-07 13:38:48 -0800932 return PointerData::GetFrames(pointer, frames, max_frames);
Colin Cross2d4721c2016-02-02 11:57:54 -0800933}
934
Christopher Ferris63860cb2015-11-16 17:30:32 -0800935#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
936void* debug_pvalloc(size_t bytes) {
937 if (DebugCallsDisabled()) {
938 return g_dispatch->pvalloc(bytes);
939 }
940
941 size_t pagesize = getpagesize();
Dan Alberta613d0d2017-10-05 16:39:33 -0700942 size_t size = __BIONIC_ALIGN(bytes, pagesize);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800943 if (size < bytes) {
944 // Overflow
945 errno = ENOMEM;
946 return nullptr;
947 }
948 return debug_memalign(pagesize, size);
949}
950
951void* debug_valloc(size_t size) {
952 if (DebugCallsDisabled()) {
953 return g_dispatch->valloc(size);
954 }
955 return debug_memalign(getpagesize(), size);
956}
957#endif
Christopher Ferris602b88c2017-08-04 13:04:04 -0700958
959static std::mutex g_dump_lock;
960
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800961static void write_dump(int fd) {
962 dprintf(fd, "Android Native Heap Dump v1.2\n\n");
Christopher Ferris602b88c2017-08-04 13:04:04 -0700963
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700964 std::string fingerprint = android::base::GetProperty("ro.build.fingerprint", "unknown");
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800965 dprintf(fd, "Build fingerprint: '%s'\n\n", fingerprint.c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700966
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800967 PointerData::DumpLiveToFile(fd);
Christopher Ferris602b88c2017-08-04 13:04:04 -0700968
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800969 dprintf(fd, "MAPS\n");
Christopher Ferris602b88c2017-08-04 13:04:04 -0700970 std::string content;
971 if (!android::base::ReadFileToString("/proc/self/maps", &content)) {
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800972 dprintf(fd, "Could not open /proc/self/maps\n");
Christopher Ferris602b88c2017-08-04 13:04:04 -0700973 } else {
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800974 dprintf(fd, "%s", content.c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700975 }
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800976 dprintf(fd, "END\n");
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700977}
978
979bool debug_write_malloc_leak_info(FILE* fp) {
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800980 // Make sure any pending output is written to the file.
981 fflush(fp);
982
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700983 ScopedConcurrentLock lock;
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700984 ScopedDisableDebugCalls disable;
Christopher Ferris9bf78172020-05-20 15:37:30 -0700985 ScopedBacktraceSignalBlocker blocked;
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700986
987 std::lock_guard<std::mutex> guard(g_dump_lock);
988
989 if (!(g_debug->config().options() & BACKTRACE)) {
990 return false;
991 }
992
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800993 write_dump(fileno(fp));
994
Christopher Ferris602b88c2017-08-04 13:04:04 -0700995 return true;
996}
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700997
998void debug_dump_heap(const char* file_name) {
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700999 ScopedConcurrentLock lock;
Christopher Ferris2e1a40a2018-06-13 10:46:34 -07001000 ScopedDisableDebugCalls disable;
Christopher Ferris9bf78172020-05-20 15:37:30 -07001001 ScopedBacktraceSignalBlocker blocked;
Christopher Ferris2e1a40a2018-06-13 10:46:34 -07001002
1003 std::lock_guard<std::mutex> guard(g_dump_lock);
1004
Christopher Ferrisff88fb02019-11-04 18:40:00 -08001005 int fd = open(file_name, O_RDWR | O_CREAT | O_NOFOLLOW | O_TRUNC | O_CLOEXEC, 0644);
1006 if (fd == -1) {
Christopher Ferris2e1a40a2018-06-13 10:46:34 -07001007 error_log("Unable to create file: %s", file_name);
1008 return;
1009 }
1010
1011 error_log("Dumping to file: %s\n", file_name);
Christopher Ferrisff88fb02019-11-04 18:40:00 -08001012 write_dump(fd);
1013 close(fd);
Christopher Ferris2e1a40a2018-06-13 10:46:34 -07001014}