blob: c030d54290ac9d33dc1c5490e041792ca1e43e4a [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 Ferris6c619a02019-03-01 17:59:51 -080033#include <stdio.h>
Christopher Ferrisc328e442019-04-01 19:31:26 -070034#include <stdlib.h>
Christopher Ferris63860cb2015-11-16 17:30:32 -080035#include <string.h>
36#include <sys/cdefs.h>
37#include <sys/param.h>
38#include <unistd.h>
39
Christopher Ferris602b88c2017-08-04 13:04:04 -070040#include <mutex>
Christopher Ferris63860cb2015-11-16 17:30:32 -080041#include <vector>
42
Christopher Ferris602b88c2017-08-04 13:04:04 -070043#include <android-base/file.h>
Christopher Ferris2e1a40a2018-06-13 10:46:34 -070044#include <android-base/properties.h>
Christopher Ferris602b88c2017-08-04 13:04:04 -070045#include <android-base/stringprintf.h>
Christopher Ferris63860cb2015-11-16 17:30:32 -080046#include <private/bionic_malloc_dispatch.h>
Christopher Ferris6c619a02019-03-01 17:59:51 -080047#include <private/MallocXmlElem.h>
Christopher Ferris63860cb2015-11-16 17:30:32 -080048
Christopher Ferris72df6702016-02-11 15:51:31 -080049#include "Config.h"
Christopher Ferris63860cb2015-11-16 17:30:32 -080050#include "DebugData.h"
Christopher Ferris4da25032018-03-07 13:38:48 -080051#include "backtrace.h"
Christopher Ferris63860cb2015-11-16 17:30:32 -080052#include "debug_disable.h"
53#include "debug_log.h"
54#include "malloc_debug.h"
Christopher Ferris93bdd6a2018-04-05 11:12:38 -070055#include "UnwindBacktrace.h"
Christopher Ferris63860cb2015-11-16 17:30:32 -080056
57// ------------------------------------------------------------------------
58// Global Data
59// ------------------------------------------------------------------------
60DebugData* g_debug;
61
Christopher Ferris8189e772019-04-09 16:37:23 -070062bool* g_zygote_child;
Christopher Ferris63860cb2015-11-16 17:30:32 -080063
64const MallocDispatch* g_dispatch;
65// ------------------------------------------------------------------------
66
67// ------------------------------------------------------------------------
68// Use C style prototypes for all exported functions. This makes it easy
69// to do dlsym lookups during libc initialization when malloc debug
70// is enabled.
71// ------------------------------------------------------------------------
72__BEGIN_DECLS
73
Christopher Ferris8189e772019-04-09 16:37:23 -070074bool debug_initialize(const MallocDispatch* malloc_dispatch, bool* malloc_zygote_child,
Christopher Ferris4da25032018-03-07 13:38:48 -080075 const char* options);
Christopher Ferris63860cb2015-11-16 17:30:32 -080076void debug_finalize();
Christopher Ferris2e1a40a2018-06-13 10:46:34 -070077void debug_dump_heap(const char* file_name);
Christopher Ferris4da25032018-03-07 13:38:48 -080078void debug_get_malloc_leak_info(uint8_t** info, size_t* overall_size, size_t* info_size,
79 size_t* total_memory, size_t* backtrace_size);
Christopher Ferris2e1a40a2018-06-13 10:46:34 -070080bool debug_write_malloc_leak_info(FILE* fp);
Colin Cross2d4721c2016-02-02 11:57:54 -080081ssize_t debug_malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count);
Christopher Ferris63860cb2015-11-16 17:30:32 -080082void debug_free_malloc_leak_info(uint8_t* info);
83size_t debug_malloc_usable_size(void* pointer);
84void* debug_malloc(size_t size);
85void debug_free(void* pointer);
Christopher Ferriscae21a92018-02-05 18:14:55 -080086void* debug_aligned_alloc(size_t alignment, size_t size);
Christopher Ferris63860cb2015-11-16 17:30:32 -080087void* debug_memalign(size_t alignment, size_t bytes);
88void* debug_realloc(void* pointer, size_t bytes);
89void* debug_calloc(size_t nmemb, size_t bytes);
90struct mallinfo debug_mallinfo();
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -070091int debug_mallopt(int param, int value);
Christopher Ferris6c619a02019-03-01 17:59:51 -080092int debug_malloc_info(int options, FILE* fp);
Christopher Ferris63860cb2015-11-16 17:30:32 -080093int debug_posix_memalign(void** memptr, size_t alignment, size_t size);
Colin Cross869691c2016-01-29 12:48:18 -080094int debug_iterate(uintptr_t base, size_t size,
Christopher Ferris4da25032018-03-07 13:38:48 -080095 void (*callback)(uintptr_t base, size_t size, void* arg), void* arg);
Colin Cross869691c2016-01-29 12:48:18 -080096void debug_malloc_disable();
97void debug_malloc_enable();
Christopher Ferris63860cb2015-11-16 17:30:32 -080098
99#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
100void* debug_pvalloc(size_t bytes);
101void* debug_valloc(size_t size);
102#endif
103
104__END_DECLS
105// ------------------------------------------------------------------------
106
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700107class ScopedConcurrentLock {
108 public:
109 ScopedConcurrentLock() {
110 pthread_rwlock_rdlock(&lock_);
111 }
112 ~ScopedConcurrentLock() {
113 pthread_rwlock_unlock(&lock_);
114 }
115
116 static void Init() {
117 pthread_rwlockattr_t attr;
118 // Set the attribute so that when a write lock is pending, read locks are no
119 // longer granted.
120 pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
121 pthread_rwlock_init(&lock_, &attr);
122 }
123
124 static void BlockAllOperations() {
125 pthread_rwlock_wrlock(&lock_);
126 }
127
128 private:
129 static pthread_rwlock_t lock_;
130};
131pthread_rwlock_t ScopedConcurrentLock::lock_;
132
Colin Cross7a28a3c2016-02-07 22:51:15 -0800133static void InitAtfork() {
134 static pthread_once_t atfork_init = PTHREAD_ONCE_INIT;
Christopher Ferris4da25032018-03-07 13:38:48 -0800135 pthread_once(&atfork_init, []() {
Colin Cross7a28a3c2016-02-07 22:51:15 -0800136 pthread_atfork(
Christopher Ferris4da25032018-03-07 13:38:48 -0800137 []() {
Colin Cross7a28a3c2016-02-07 22:51:15 -0800138 if (g_debug != nullptr) {
139 g_debug->PrepareFork();
140 }
141 },
Christopher Ferris4da25032018-03-07 13:38:48 -0800142 []() {
Colin Cross7a28a3c2016-02-07 22:51:15 -0800143 if (g_debug != nullptr) {
144 g_debug->PostForkParent();
145 }
146 },
Christopher Ferris4da25032018-03-07 13:38:48 -0800147 []() {
Colin Cross7a28a3c2016-02-07 22:51:15 -0800148 if (g_debug != nullptr) {
149 g_debug->PostForkChild();
150 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800151 });
Colin Cross7a28a3c2016-02-07 22:51:15 -0800152 });
153}
Christopher Ferrisd0919622016-03-15 22:39:39 -0700154
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700155void BacktraceAndLog() {
156 if (g_debug->config().options() & BACKTRACE_FULL) {
157 std::vector<uintptr_t> frames;
158 std::vector<unwindstack::LocalFrameData> frames_info;
159 if (!Unwind(&frames, &frames_info, 256)) {
160 error_log(" Backtrace failed to get any frames.");
161 } else {
162 UnwindLog(frames_info);
163 }
164 } else {
165 std::vector<uintptr_t> frames(256);
166 size_t num_frames = backtrace_get(frames.data(), frames.size());
167 if (num_frames == 0) {
168 error_log(" Backtrace failed to get any frames.");
169 } else {
170 backtrace_log(frames.data(), num_frames);
171 }
172 }
173}
174
Christopher Ferris4da25032018-03-07 13:38:48 -0800175static void LogError(const void* pointer, const char* error_str) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800176 error_log(LOG_DIVIDER);
Christopher Ferris4da25032018-03-07 13:38:48 -0800177 error_log("+++ ALLOCATION %p %s", pointer, error_str);
178
179 // If we are tracking already freed pointers, check to see if this is
180 // one so we can print extra information.
181 if (g_debug->config().options() & FREE_TRACK) {
182 PointerData::LogFreeBacktrace(pointer);
Christopher Ferris7993b802016-01-28 18:35:05 -0800183 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800184
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700185 error_log("Backtrace at time of failure:");
186 BacktraceAndLog();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800187 error_log(LOG_DIVIDER);
Iris Chang7f209a92019-01-16 11:17:15 +0800188 if (g_debug->config().options() & ABORT_ON_ERROR) {
189 abort();
190 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800191}
192
Christopher Ferris4da25032018-03-07 13:38:48 -0800193static bool VerifyPointer(const void* pointer, const char* function_name) {
194 if (g_debug->HeaderEnabled()) {
195 Header* header = g_debug->GetHeader(pointer);
196 if (header->tag != DEBUG_TAG) {
197 std::string error_str;
198 if (header->tag == DEBUG_FREE_TAG) {
199 error_str = std::string("USED AFTER FREE (") + function_name + ")";
200 } else {
201 error_str = android::base::StringPrintf("HAS INVALID TAG %" PRIx32 " (%s)", header->tag,
202 function_name);
203 }
204 LogError(pointer, error_str.c_str());
205 return false;
206 }
207 }
208
209 if (g_debug->TrackPointers()) {
210 if (!PointerData::Exists(pointer)) {
211 std::string error_str(std::string("UNKNOWN POINTER (") + function_name + ")");
212 LogError(pointer, error_str.c_str());
213 return false;
214 }
215 }
216 return true;
217}
218
219static size_t InternalMallocUsableSize(void* pointer) {
220 if (g_debug->HeaderEnabled()) {
221 return g_debug->GetHeader(pointer)->usable_size;
222 } else {
223 return g_dispatch->malloc_usable_size(pointer);
224 }
225}
226
Christopher Ferris63860cb2015-11-16 17:30:32 -0800227static void* InitHeader(Header* header, void* orig_pointer, size_t size) {
228 header->tag = DEBUG_TAG;
229 header->orig_pointer = orig_pointer;
230 header->size = size;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800231 header->usable_size = g_dispatch->malloc_usable_size(orig_pointer);
232 if (header->usable_size == 0) {
233 g_dispatch->free(orig_pointer);
234 return nullptr;
235 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800236 header->usable_size -= g_debug->pointer_offset() + reinterpret_cast<uintptr_t>(header) -
237 reinterpret_cast<uintptr_t>(orig_pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800238
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700239 if (g_debug->config().options() & FRONT_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800240 uint8_t* guard = g_debug->GetFrontGuard(header);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700241 memset(guard, g_debug->config().front_guard_value(), g_debug->config().front_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800242 }
243
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700244 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800245 uint8_t* guard = g_debug->GetRearGuard(header);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700246 memset(guard, g_debug->config().rear_guard_value(), g_debug->config().rear_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800247 // If the rear guard is enabled, set the usable size to the exact size
248 // of the allocation.
Christopher Ferris4da25032018-03-07 13:38:48 -0800249 header->usable_size = header->size;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800250 }
251
252 return g_debug->GetPointer(header);
253}
254
Christopher Ferris705de3c2019-05-22 13:39:57 -0700255extern "C" void __asan_init() __attribute__((weak));
256
Christopher Ferris8189e772019-04-09 16:37:23 -0700257bool debug_initialize(const MallocDispatch* malloc_dispatch, bool* zygote_child,
Christopher Ferris4da25032018-03-07 13:38:48 -0800258 const char* options) {
Christopher Ferris8189e772019-04-09 16:37:23 -0700259 if (zygote_child == nullptr || options == nullptr) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800260 return false;
261 }
Colin Cross7a28a3c2016-02-07 22:51:15 -0800262
Christopher Ferris705de3c2019-05-22 13:39:57 -0700263 if (__asan_init != 0) {
264 error_log("malloc debug cannot be enabled alongside ASAN");
265 return false;
266 }
267
Colin Cross7a28a3c2016-02-07 22:51:15 -0800268 InitAtfork();
269
Christopher Ferris8189e772019-04-09 16:37:23 -0700270 g_zygote_child = zygote_child;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800271
272 g_dispatch = malloc_dispatch;
273
274 if (!DebugDisableInitialize()) {
275 return false;
276 }
277
278 DebugData* debug = new DebugData();
Tamas Berghammerac81fe82016-08-26 15:54:59 +0100279 if (!debug->Initialize(options)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800280 delete debug;
281 DebugDisableFinalize();
282 return false;
283 }
284 g_debug = debug;
285
286 // Always enable the backtrace code since we will use it in a number
287 // of different error cases.
288 backtrace_startup();
289
Christopher Ferrisc328e442019-04-01 19:31:26 -0700290 if (g_debug->config().options() & VERBOSE) {
291 info_log("%s: malloc debug enabled", getprogname());
292 }
293
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700294 ScopedConcurrentLock::Init();
295
Christopher Ferris63860cb2015-11-16 17:30:32 -0800296 return true;
297}
298
299void debug_finalize() {
300 if (g_debug == nullptr) {
301 return;
302 }
303
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700304 // Make sure that there are no other threads doing debug allocations
305 // before we kill everything.
306 ScopedConcurrentLock::BlockAllOperations();
307
Christopher Ferris97b47472018-07-10 14:45:24 -0700308 // Turn off capturing allocations calls.
309 DebugDisableSet(true);
310
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700311 if (g_debug->config().options() & FREE_TRACK) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800312 PointerData::VerifyAllFreed();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800313 }
314
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700315 if (g_debug->config().options() & LEAK_TRACK) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800316 PointerData::LogLeaks();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800317 }
318
Christopher Ferris602b88c2017-08-04 13:04:04 -0700319 if ((g_debug->config().options() & BACKTRACE) && g_debug->config().backtrace_dump_on_exit()) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800320 debug_dump_heap(android::base::StringPrintf("%s.%d.exit.txt",
321 g_debug->config().backtrace_dump_prefix().c_str(),
Christopher Ferris97b47472018-07-10 14:45:24 -0700322 getpid()).c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700323 }
324
Colin Cross2c759912016-02-05 16:17:39 -0800325 backtrace_shutdown();
326
Christopher Ferris63860cb2015-11-16 17:30:32 -0800327 delete g_debug;
328 g_debug = nullptr;
329
330 DebugDisableFinalize();
331}
332
Christopher Ferris4da25032018-03-07 13:38:48 -0800333void debug_get_malloc_leak_info(uint8_t** info, size_t* overall_size, size_t* info_size,
334 size_t* total_memory, size_t* backtrace_size) {
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700335 ScopedConcurrentLock lock;
336
Christopher Ferris63860cb2015-11-16 17:30:32 -0800337 ScopedDisableDebugCalls disable;
338
339 // Verify the arguments.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700340 if (info == nullptr || overall_size == nullptr || info_size == nullptr || total_memory == nullptr ||
Christopher Ferris4da25032018-03-07 13:38:48 -0800341 backtrace_size == nullptr) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800342 error_log("get_malloc_leak_info: At least one invalid parameter.");
343 return;
344 }
345
346 *info = nullptr;
347 *overall_size = 0;
348 *info_size = 0;
349 *total_memory = 0;
350 *backtrace_size = 0;
351
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700352 if (!(g_debug->config().options() & BACKTRACE)) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800353 error_log(
354 "get_malloc_leak_info: Allocations not being tracked, to enable "
355 "set the option 'backtrace'.");
Christopher Ferris63860cb2015-11-16 17:30:32 -0800356 return;
357 }
358
Christopher Ferris4da25032018-03-07 13:38:48 -0800359 PointerData::GetInfo(info, overall_size, info_size, total_memory, backtrace_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800360}
361
362void debug_free_malloc_leak_info(uint8_t* info) {
363 g_dispatch->free(info);
364}
365
Christopher Ferris55a89a42016-04-07 17:14:53 -0700366size_t debug_malloc_usable_size(void* pointer) {
367 if (DebugCallsDisabled() || pointer == nullptr) {
368 return g_dispatch->malloc_usable_size(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800369 }
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700370 ScopedConcurrentLock lock;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700371 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800372
Christopher Ferris4da25032018-03-07 13:38:48 -0800373 if (!VerifyPointer(pointer, "malloc_usable_size")) {
374 return 0;
375 }
376
377 return InternalMallocUsableSize(pointer);
Christopher Ferris55a89a42016-04-07 17:14:53 -0700378}
379
Christopher Ferris4da25032018-03-07 13:38:48 -0800380static void* InternalMalloc(size_t size) {
381 if ((g_debug->config().options() & BACKTRACE) && g_debug->pointer->ShouldDumpAndReset()) {
382 debug_dump_heap(android::base::StringPrintf(
383 "%s.%d.txt", g_debug->config().backtrace_dump_prefix().c_str(), getpid())
384 .c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700385 }
386
Colin Cross9567c7b2016-03-09 17:56:14 -0800387 if (size == 0) {
388 size = 1;
389 }
390
Christopher Ferris63860cb2015-11-16 17:30:32 -0800391 size_t real_size = size + g_debug->extra_bytes();
392 if (real_size < size) {
393 // Overflow.
394 errno = ENOMEM;
395 return nullptr;
396 }
397
Christopher Ferris4da25032018-03-07 13:38:48 -0800398 if (size > PointerInfoType::MaxSize()) {
399 errno = ENOMEM;
400 return nullptr;
401 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800402
Christopher Ferris4da25032018-03-07 13:38:48 -0800403 void* pointer;
404 if (g_debug->HeaderEnabled()) {
405 Header* header =
406 reinterpret_cast<Header*>(g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800407 if (header == nullptr) {
408 return nullptr;
409 }
410 pointer = InitHeader(header, header, size);
411 } else {
412 pointer = g_dispatch->malloc(real_size);
413 }
414
Christopher Ferris4da25032018-03-07 13:38:48 -0800415 if (pointer != nullptr) {
416 if (g_debug->TrackPointers()) {
417 PointerData::Add(pointer, size);
418 }
419
420 if (g_debug->config().options() & FILL_ON_ALLOC) {
421 size_t bytes = InternalMallocUsableSize(pointer);
422 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes();
423 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
424 memset(pointer, g_debug->config().fill_alloc_value(), bytes);
425 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800426 }
427 return pointer;
428}
429
Christopher Ferris55a89a42016-04-07 17:14:53 -0700430void* debug_malloc(size_t size) {
431 if (DebugCallsDisabled()) {
432 return g_dispatch->malloc(size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800433 }
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700434 ScopedConcurrentLock lock;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700435 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800436
Christopher Ferris4da25032018-03-07 13:38:48 -0800437 void* pointer = InternalMalloc(size);
Christopher Ferris7bd01782016-04-20 12:30:58 -0700438
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700439 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700440 g_debug->record->AddEntry(new MallocEntry(pointer, size));
441 }
442
443 return pointer;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700444}
445
Christopher Ferris4da25032018-03-07 13:38:48 -0800446static void InternalFree(void* pointer) {
447 if ((g_debug->config().options() & BACKTRACE) && g_debug->pointer->ShouldDumpAndReset()) {
448 debug_dump_heap(android::base::StringPrintf(
449 "%s.%d.txt", g_debug->config().backtrace_dump_prefix().c_str(), getpid())
450 .c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700451 }
452
Christopher Ferris63860cb2015-11-16 17:30:32 -0800453 void* free_pointer = pointer;
454 size_t bytes;
Christopher Ferrisd0919622016-03-15 22:39:39 -0700455 Header* header;
Christopher Ferris4da25032018-03-07 13:38:48 -0800456 if (g_debug->HeaderEnabled()) {
Christopher Ferrisd0919622016-03-15 22:39:39 -0700457 header = g_debug->GetHeader(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800458 free_pointer = header->orig_pointer;
459
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700460 if (g_debug->config().options() & FRONT_GUARD) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700461 if (!g_debug->front_guard->Valid(header)) {
462 g_debug->front_guard->LogFailure(header);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800463 }
464 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700465 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700466 if (!g_debug->rear_guard->Valid(header)) {
467 g_debug->rear_guard->LogFailure(header);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800468 }
469 }
470
Christopher Ferris7993b802016-01-28 18:35:05 -0800471 header->tag = DEBUG_FREE_TAG;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800472
473 bytes = header->usable_size;
474 } else {
475 bytes = g_dispatch->malloc_usable_size(pointer);
476 }
477
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700478 if (g_debug->config().options() & FILL_ON_FREE) {
479 size_t fill_bytes = g_debug->config().fill_on_free_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800480 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700481 memset(pointer, g_debug->config().fill_free_value(), bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800482 }
483
Christopher Ferris4da25032018-03-07 13:38:48 -0800484 if (g_debug->TrackPointers()) {
485 PointerData::Remove(pointer);
486 }
487
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700488 if (g_debug->config().options() & FREE_TRACK) {
Christopher Ferrisd0919622016-03-15 22:39:39 -0700489 // Do not add the allocation until we are done modifying the pointer
490 // itself. This avoids a race if a lot of threads are all doing
491 // frees at the same time and we wind up trying to really free this
492 // pointer from another thread, while still trying to free it in
493 // this function.
Christopher Ferris4da25032018-03-07 13:38:48 -0800494 pointer = PointerData::AddFreed(pointer);
495 if (pointer != nullptr) {
496 if (g_debug->HeaderEnabled()) {
497 pointer = g_debug->GetHeader(pointer)->orig_pointer;
498 }
499 g_dispatch->free(pointer);
500 }
Christopher Ferrisd0919622016-03-15 22:39:39 -0700501 } else {
502 g_dispatch->free(free_pointer);
503 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800504}
505
Christopher Ferris55a89a42016-04-07 17:14:53 -0700506void debug_free(void* pointer) {
507 if (DebugCallsDisabled() || pointer == nullptr) {
508 return g_dispatch->free(pointer);
509 }
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700510 ScopedConcurrentLock lock;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700511 ScopedDisableDebugCalls disable;
512
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700513 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700514 g_debug->record->AddEntry(new FreeEntry(pointer));
515 }
516
Christopher Ferris4da25032018-03-07 13:38:48 -0800517 if (!VerifyPointer(pointer, "free")) {
518 return;
519 }
520
521 InternalFree(pointer);
Christopher Ferris55a89a42016-04-07 17:14:53 -0700522}
523
Christopher Ferris63860cb2015-11-16 17:30:32 -0800524void* debug_memalign(size_t alignment, size_t bytes) {
525 if (DebugCallsDisabled()) {
526 return g_dispatch->memalign(alignment, bytes);
527 }
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700528 ScopedConcurrentLock lock;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700529 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800530
Colin Cross9567c7b2016-03-09 17:56:14 -0800531 if (bytes == 0) {
532 bytes = 1;
533 }
534
Christopher Ferris4da25032018-03-07 13:38:48 -0800535 if (bytes > PointerInfoType::MaxSize()) {
536 errno = ENOMEM;
537 return nullptr;
538 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800539
Christopher Ferris4da25032018-03-07 13:38:48 -0800540 void* pointer;
541 if (g_debug->HeaderEnabled()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800542 // Make the alignment a power of two.
543 if (!powerof2(alignment)) {
544 alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment);
545 }
Christopher Ferris72df6702016-02-11 15:51:31 -0800546 // Force the alignment to at least MINIMUM_ALIGNMENT_BYTES to guarantee
Christopher Ferris63860cb2015-11-16 17:30:32 -0800547 // that the header is aligned properly.
Christopher Ferris72df6702016-02-11 15:51:31 -0800548 if (alignment < MINIMUM_ALIGNMENT_BYTES) {
549 alignment = MINIMUM_ALIGNMENT_BYTES;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800550 }
551
552 // We don't have any idea what the natural alignment of
553 // the underlying native allocator is, so we always need to
554 // over allocate.
555 size_t real_size = alignment + bytes + g_debug->extra_bytes();
556 if (real_size < bytes) {
557 // Overflow.
558 errno = ENOMEM;
559 return nullptr;
560 }
561
562 pointer = g_dispatch->malloc(real_size);
563 if (pointer == nullptr) {
564 return nullptr;
565 }
566
567 uintptr_t value = reinterpret_cast<uintptr_t>(pointer) + g_debug->pointer_offset();
568 // Now align the pointer.
569 value += (-value % alignment);
570
571 Header* header = g_debug->GetHeader(reinterpret_cast<void*>(value));
572 pointer = InitHeader(header, pointer, bytes);
573 } else {
574 size_t real_size = bytes + g_debug->extra_bytes();
575 if (real_size < bytes) {
576 // Overflow.
577 errno = ENOMEM;
578 return nullptr;
579 }
580 pointer = g_dispatch->memalign(alignment, real_size);
581 }
582
Christopher Ferris4da25032018-03-07 13:38:48 -0800583 if (pointer != nullptr) {
584 if (g_debug->TrackPointers()) {
585 PointerData::Add(pointer, bytes);
586 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700587
Christopher Ferris4da25032018-03-07 13:38:48 -0800588 if (g_debug->config().options() & FILL_ON_ALLOC) {
589 size_t bytes = InternalMallocUsableSize(pointer);
590 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes();
591 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
592 memset(pointer, g_debug->config().fill_alloc_value(), bytes);
593 }
594
595 if (g_debug->config().options() & RECORD_ALLOCS) {
596 g_debug->record->AddEntry(new MemalignEntry(pointer, bytes, alignment));
597 }
Christopher Ferris7bd01782016-04-20 12:30:58 -0700598 }
599
Christopher Ferris63860cb2015-11-16 17:30:32 -0800600 return pointer;
601}
602
603void* debug_realloc(void* pointer, size_t bytes) {
604 if (DebugCallsDisabled()) {
605 return g_dispatch->realloc(pointer, bytes);
606 }
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700607 ScopedConcurrentLock lock;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700608 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800609
610 if (pointer == nullptr) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800611 pointer = InternalMalloc(bytes);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700612 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700613 g_debug->record->AddEntry(new ReallocEntry(pointer, bytes, nullptr));
614 }
615 return pointer;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800616 }
617
Christopher Ferris4da25032018-03-07 13:38:48 -0800618 if (!VerifyPointer(pointer, "realloc")) {
619 return nullptr;
620 }
621
Christopher Ferris63860cb2015-11-16 17:30:32 -0800622 if (bytes == 0) {
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700623 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700624 g_debug->record->AddEntry(new ReallocEntry(nullptr, bytes, pointer));
625 }
626
Christopher Ferris4da25032018-03-07 13:38:48 -0800627 InternalFree(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800628 return nullptr;
629 }
630
631 size_t real_size = bytes;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700632 if (g_debug->config().options() & EXPAND_ALLOC) {
633 real_size += g_debug->config().expand_alloc_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800634 if (real_size < bytes) {
635 // Overflow.
636 errno = ENOMEM;
637 return nullptr;
638 }
639 }
640
Christopher Ferris4da25032018-03-07 13:38:48 -0800641 if (bytes > PointerInfoType::MaxSize()) {
642 errno = ENOMEM;
643 return nullptr;
644 }
645
Christopher Ferris63860cb2015-11-16 17:30:32 -0800646 void* new_pointer;
647 size_t prev_size;
Christopher Ferris4da25032018-03-07 13:38:48 -0800648 if (g_debug->HeaderEnabled()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800649 // Same size, do nothing.
Christopher Ferris4da25032018-03-07 13:38:48 -0800650 Header* header = g_debug->GetHeader(pointer);
651 if (real_size == header->size) {
652 if (g_debug->TrackPointers()) {
653 // Remove and re-add so that the backtrace is updated.
654 PointerData::Remove(pointer);
655 PointerData::Add(pointer, real_size);
656 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800657 return pointer;
658 }
659
660 // Allocation is shrinking.
661 if (real_size < header->usable_size) {
662 header->size = real_size;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700663 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800664 // Don't bother allocating a smaller pointer in this case, simply
665 // change the header usable_size and reset the rear guard.
Christopher Ferris4da25032018-03-07 13:38:48 -0800666 header->usable_size = header->size;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700667 memset(g_debug->GetRearGuard(header), g_debug->config().rear_guard_value(),
668 g_debug->config().rear_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800669 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800670 if (g_debug->TrackPointers()) {
671 // Remove and re-add so that the backtrace is updated.
672 PointerData::Remove(pointer);
673 PointerData::Add(pointer, real_size);
674 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800675 return pointer;
676 }
677
678 // Allocate the new size.
Christopher Ferris4da25032018-03-07 13:38:48 -0800679 new_pointer = InternalMalloc(bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800680 if (new_pointer == nullptr) {
681 errno = ENOMEM;
682 return nullptr;
683 }
684
685 prev_size = header->usable_size;
686 memcpy(new_pointer, pointer, prev_size);
Christopher Ferris4da25032018-03-07 13:38:48 -0800687 InternalFree(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800688 } else {
Christopher Ferris4da25032018-03-07 13:38:48 -0800689 if (g_debug->TrackPointers()) {
690 PointerData::Remove(pointer);
691 }
692
Christopher Ferris63860cb2015-11-16 17:30:32 -0800693 prev_size = g_dispatch->malloc_usable_size(pointer);
694 new_pointer = g_dispatch->realloc(pointer, real_size);
695 if (new_pointer == nullptr) {
696 return nullptr;
697 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800698
699 if (g_debug->TrackPointers()) {
700 PointerData::Add(new_pointer, real_size);
701 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800702 }
703
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700704 if (g_debug->config().options() & FILL_ON_ALLOC) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800705 size_t bytes = InternalMallocUsableSize(new_pointer);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700706 if (bytes > g_debug->config().fill_on_alloc_bytes()) {
707 bytes = g_debug->config().fill_on_alloc_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800708 }
709 if (bytes > prev_size) {
710 memset(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(new_pointer) + prev_size),
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700711 g_debug->config().fill_alloc_value(), bytes - prev_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800712 }
713 }
714
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700715 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700716 g_debug->record->AddEntry(new ReallocEntry(new_pointer, bytes, pointer));
717 }
718
Christopher Ferris63860cb2015-11-16 17:30:32 -0800719 return new_pointer;
720}
721
722void* debug_calloc(size_t nmemb, size_t bytes) {
723 if (DebugCallsDisabled()) {
724 return g_dispatch->calloc(nmemb, bytes);
725 }
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700726 ScopedConcurrentLock lock;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700727 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800728
Colin Cross7877df62016-03-10 13:01:27 -0800729 size_t size;
730 if (__builtin_mul_overflow(nmemb, bytes, &size)) {
731 // Overflow
732 errno = ENOMEM;
733 return nullptr;
734 }
735
Colin Cross9567c7b2016-03-09 17:56:14 -0800736 if (size == 0) {
737 size = 1;
738 }
739
Colin Cross7877df62016-03-10 13:01:27 -0800740 size_t real_size;
741 if (__builtin_add_overflow(size, g_debug->extra_bytes(), &real_size)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800742 // Overflow.
743 errno = ENOMEM;
744 return nullptr;
745 }
746
Christopher Ferris4da25032018-03-07 13:38:48 -0800747 if (real_size > PointerInfoType::MaxSize()) {
748 errno = ENOMEM;
749 return nullptr;
750 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800751
Christopher Ferris4da25032018-03-07 13:38:48 -0800752 void* pointer;
753 if (g_debug->HeaderEnabled()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800754 // Need to guarantee the alignment of the header.
Christopher Ferris4da25032018-03-07 13:38:48 -0800755 Header* header =
756 reinterpret_cast<Header*>(g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800757 if (header == nullptr) {
758 return nullptr;
759 }
760 memset(header, 0, g_dispatch->malloc_usable_size(header));
Christopher Ferris7bd01782016-04-20 12:30:58 -0700761 pointer = InitHeader(header, header, size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800762 } else {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700763 pointer = g_dispatch->calloc(1, real_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800764 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800765
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700766 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700767 g_debug->record->AddEntry(new CallocEntry(pointer, bytes, nmemb));
768 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800769
770 if (pointer != nullptr && g_debug->TrackPointers()) {
771 PointerData::Add(pointer, size);
772 }
Christopher Ferris7bd01782016-04-20 12:30:58 -0700773 return pointer;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800774}
775
776struct mallinfo debug_mallinfo() {
777 return g_dispatch->mallinfo();
778}
779
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700780int debug_mallopt(int param, int value) {
781 return g_dispatch->mallopt(param, value);
782}
783
Christopher Ferris6c619a02019-03-01 17:59:51 -0800784int debug_malloc_info(int options, FILE* fp) {
785 if (DebugCallsDisabled() || !g_debug->TrackPointers()) {
786 return g_dispatch->malloc_info(options, fp);
787 }
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700788 ScopedConcurrentLock lock;
789 ScopedDisableDebugCalls disable;
Christopher Ferris6c619a02019-03-01 17:59:51 -0800790
791 MallocXmlElem root(fp, "malloc", "version=\"debug-malloc-1\"");
792 std::vector<ListInfoType> list;
793 PointerData::GetAllocList(&list);
794
795 size_t alloc_num = 0;
796 for (size_t i = 0; i < list.size(); i++) {
797 MallocXmlElem alloc(fp, "allocation", "nr=\"%zu\"", alloc_num);
798
799 size_t total = 1;
800 size_t size = list[i].size;
801 while (i < list.size() - 1 && list[i + 1].size == size) {
802 i++;
803 total++;
804 }
805 MallocXmlElem(fp, "size").Contents("%zu", list[i].size);
806 MallocXmlElem(fp, "total").Contents("%zu", total);
807 alloc_num++;
808 }
809 return 0;
810}
811
Christopher Ferriscae21a92018-02-05 18:14:55 -0800812void* debug_aligned_alloc(size_t alignment, size_t size) {
813 if (DebugCallsDisabled()) {
814 return g_dispatch->aligned_alloc(alignment, size);
815 }
Christopher Ferrisa22f5d52019-03-01 16:40:59 -0800816 if (!powerof2(alignment) || (size % alignment) != 0) {
Christopher Ferriscae21a92018-02-05 18:14:55 -0800817 errno = EINVAL;
818 return nullptr;
819 }
820 return debug_memalign(alignment, size);
821}
822
Christopher Ferris63860cb2015-11-16 17:30:32 -0800823int debug_posix_memalign(void** memptr, size_t alignment, size_t size) {
824 if (DebugCallsDisabled()) {
825 return g_dispatch->posix_memalign(memptr, alignment, size);
826 }
827
Christopher Ferris6c619a02019-03-01 17:59:51 -0800828 if (alignment < sizeof(void*) || !powerof2(alignment)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800829 return EINVAL;
830 }
831 int saved_errno = errno;
832 *memptr = debug_memalign(alignment, size);
833 errno = saved_errno;
834 return (*memptr != nullptr) ? 0 : ENOMEM;
835}
836
Christopher Ferris4da25032018-03-07 13:38:48 -0800837int debug_iterate(uintptr_t base, size_t size, void (*callback)(uintptr_t, size_t, void*),
838 void* arg) {
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700839 ScopedConcurrentLock lock;
Christopher Ferris4da25032018-03-07 13:38:48 -0800840 if (g_debug->TrackPointers()) {
841 // Since malloc is disabled, don't bother acquiring any locks.
842 for (auto it = PointerData::begin(); it != PointerData::end(); ++it) {
843 callback(it->first, InternalMallocUsableSize(reinterpret_cast<void*>(it->first)), arg);
844 }
845 return 0;
846 }
Colin Cross869691c2016-01-29 12:48:18 -0800847
Christopher Ferris4da25032018-03-07 13:38:48 -0800848 // An option that adds a header will add pointer tracking, so no need to
849 // check if headers are enabled.
850 return g_dispatch->iterate(base, size, callback, arg);
Colin Cross869691c2016-01-29 12:48:18 -0800851}
852
853void debug_malloc_disable() {
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700854 ScopedConcurrentLock lock;
Colin Cross869691c2016-01-29 12:48:18 -0800855 g_dispatch->malloc_disable();
Christopher Ferris4da25032018-03-07 13:38:48 -0800856 if (g_debug->pointer) {
857 g_debug->pointer->PrepareFork();
Colin Cross869691c2016-01-29 12:48:18 -0800858 }
859}
860
861void debug_malloc_enable() {
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700862 ScopedConcurrentLock lock;
Christopher Ferris4da25032018-03-07 13:38:48 -0800863 if (g_debug->pointer) {
864 g_debug->pointer->PostForkParent();
Colin Cross869691c2016-01-29 12:48:18 -0800865 }
866 g_dispatch->malloc_enable();
867}
868
Christopher Ferris4da25032018-03-07 13:38:48 -0800869ssize_t debug_malloc_backtrace(void* pointer, uintptr_t* frames, size_t max_frames) {
Colin Cross2d4721c2016-02-02 11:57:54 -0800870 if (DebugCallsDisabled() || pointer == nullptr) {
871 return 0;
872 }
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700873 ScopedConcurrentLock lock;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700874 ScopedDisableDebugCalls disable;
Colin Cross2d4721c2016-02-02 11:57:54 -0800875
Christopher Ferris4da25032018-03-07 13:38:48 -0800876 if (!(g_debug->config().options() & BACKTRACE)) {
877 return 0;
Colin Cross2d4721c2016-02-02 11:57:54 -0800878 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800879 return PointerData::GetFrames(pointer, frames, max_frames);
Colin Cross2d4721c2016-02-02 11:57:54 -0800880}
881
Christopher Ferris63860cb2015-11-16 17:30:32 -0800882#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
883void* debug_pvalloc(size_t bytes) {
884 if (DebugCallsDisabled()) {
885 return g_dispatch->pvalloc(bytes);
886 }
887
888 size_t pagesize = getpagesize();
Dan Alberta613d0d2017-10-05 16:39:33 -0700889 size_t size = __BIONIC_ALIGN(bytes, pagesize);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800890 if (size < bytes) {
891 // Overflow
892 errno = ENOMEM;
893 return nullptr;
894 }
895 return debug_memalign(pagesize, size);
896}
897
898void* debug_valloc(size_t size) {
899 if (DebugCallsDisabled()) {
900 return g_dispatch->valloc(size);
901 }
902 return debug_memalign(getpagesize(), size);
903}
904#endif
Christopher Ferris602b88c2017-08-04 13:04:04 -0700905
906static std::mutex g_dump_lock;
907
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700908static void write_dump(FILE* fp) {
909 fprintf(fp, "Android Native Heap Dump v1.2\n\n");
Christopher Ferris602b88c2017-08-04 13:04:04 -0700910
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700911 std::string fingerprint = android::base::GetProperty("ro.build.fingerprint", "unknown");
912 fprintf(fp, "Build fingerprint: '%s'\n\n", fingerprint.c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700913
Christopher Ferris4da25032018-03-07 13:38:48 -0800914 PointerData::DumpLiveToFile(fp);
Christopher Ferris602b88c2017-08-04 13:04:04 -0700915
916 fprintf(fp, "MAPS\n");
917 std::string content;
918 if (!android::base::ReadFileToString("/proc/self/maps", &content)) {
919 fprintf(fp, "Could not open /proc/self/maps\n");
920 } else {
921 fprintf(fp, "%s", content.c_str());
922 }
923 fprintf(fp, "END\n");
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700924}
925
926bool debug_write_malloc_leak_info(FILE* fp) {
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700927 ScopedConcurrentLock lock;
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700928 ScopedDisableDebugCalls disable;
929
930 std::lock_guard<std::mutex> guard(g_dump_lock);
931
932 if (!(g_debug->config().options() & BACKTRACE)) {
933 return false;
934 }
935
936 write_dump(fp);
Christopher Ferris602b88c2017-08-04 13:04:04 -0700937 return true;
938}
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700939
940void debug_dump_heap(const char* file_name) {
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700941 ScopedConcurrentLock lock;
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700942 ScopedDisableDebugCalls disable;
943
944 std::lock_guard<std::mutex> guard(g_dump_lock);
945
946 FILE* fp = fopen(file_name, "w+e");
947 if (fp == nullptr) {
948 error_log("Unable to create file: %s", file_name);
949 return;
950 }
951
952 error_log("Dumping to file: %s\n", file_name);
953 write_dump(fp);
954 fclose(fp);
955}