blob: 53fcead01323e7ffec7bbfcf203e77ec034ed4c4 [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 Ferris8189e772019-04-09 16:37:23 -0700255bool debug_initialize(const MallocDispatch* malloc_dispatch, bool* zygote_child,
Christopher Ferris4da25032018-03-07 13:38:48 -0800256 const char* options) {
Christopher Ferris8189e772019-04-09 16:37:23 -0700257 if (zygote_child == nullptr || options == nullptr) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800258 return false;
259 }
Colin Cross7a28a3c2016-02-07 22:51:15 -0800260
261 InitAtfork();
262
Christopher Ferris8189e772019-04-09 16:37:23 -0700263 g_zygote_child = zygote_child;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800264
265 g_dispatch = malloc_dispatch;
266
267 if (!DebugDisableInitialize()) {
268 return false;
269 }
270
271 DebugData* debug = new DebugData();
Tamas Berghammerac81fe82016-08-26 15:54:59 +0100272 if (!debug->Initialize(options)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800273 delete debug;
274 DebugDisableFinalize();
275 return false;
276 }
277 g_debug = debug;
278
279 // Always enable the backtrace code since we will use it in a number
280 // of different error cases.
281 backtrace_startup();
282
Christopher Ferrisc328e442019-04-01 19:31:26 -0700283 if (g_debug->config().options() & VERBOSE) {
284 info_log("%s: malloc debug enabled", getprogname());
285 }
286
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700287 ScopedConcurrentLock::Init();
288
Christopher Ferris63860cb2015-11-16 17:30:32 -0800289 return true;
290}
291
292void debug_finalize() {
293 if (g_debug == nullptr) {
294 return;
295 }
296
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700297 // Make sure that there are no other threads doing debug allocations
298 // before we kill everything.
299 ScopedConcurrentLock::BlockAllOperations();
300
Christopher Ferris97b47472018-07-10 14:45:24 -0700301 // Turn off capturing allocations calls.
302 DebugDisableSet(true);
303
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700304 if (g_debug->config().options() & FREE_TRACK) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800305 PointerData::VerifyAllFreed();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800306 }
307
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700308 if (g_debug->config().options() & LEAK_TRACK) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800309 PointerData::LogLeaks();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800310 }
311
Christopher Ferris602b88c2017-08-04 13:04:04 -0700312 if ((g_debug->config().options() & BACKTRACE) && g_debug->config().backtrace_dump_on_exit()) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800313 debug_dump_heap(android::base::StringPrintf("%s.%d.exit.txt",
314 g_debug->config().backtrace_dump_prefix().c_str(),
Christopher Ferris97b47472018-07-10 14:45:24 -0700315 getpid()).c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700316 }
317
Colin Cross2c759912016-02-05 16:17:39 -0800318 backtrace_shutdown();
319
Christopher Ferris63860cb2015-11-16 17:30:32 -0800320 delete g_debug;
321 g_debug = nullptr;
322
323 DebugDisableFinalize();
324}
325
Christopher Ferris4da25032018-03-07 13:38:48 -0800326void debug_get_malloc_leak_info(uint8_t** info, size_t* overall_size, size_t* info_size,
327 size_t* total_memory, size_t* backtrace_size) {
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700328 ScopedConcurrentLock lock;
329
Christopher Ferris63860cb2015-11-16 17:30:32 -0800330 ScopedDisableDebugCalls disable;
331
332 // Verify the arguments.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700333 if (info == nullptr || overall_size == nullptr || info_size == nullptr || total_memory == nullptr ||
Christopher Ferris4da25032018-03-07 13:38:48 -0800334 backtrace_size == nullptr) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800335 error_log("get_malloc_leak_info: At least one invalid parameter.");
336 return;
337 }
338
339 *info = nullptr;
340 *overall_size = 0;
341 *info_size = 0;
342 *total_memory = 0;
343 *backtrace_size = 0;
344
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700345 if (!(g_debug->config().options() & BACKTRACE)) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800346 error_log(
347 "get_malloc_leak_info: Allocations not being tracked, to enable "
348 "set the option 'backtrace'.");
Christopher Ferris63860cb2015-11-16 17:30:32 -0800349 return;
350 }
351
Christopher Ferris4da25032018-03-07 13:38:48 -0800352 PointerData::GetInfo(info, overall_size, info_size, total_memory, backtrace_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800353}
354
355void debug_free_malloc_leak_info(uint8_t* info) {
356 g_dispatch->free(info);
357}
358
Christopher Ferris55a89a42016-04-07 17:14:53 -0700359size_t debug_malloc_usable_size(void* pointer) {
360 if (DebugCallsDisabled() || pointer == nullptr) {
361 return g_dispatch->malloc_usable_size(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800362 }
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700363 ScopedConcurrentLock lock;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700364 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800365
Christopher Ferris4da25032018-03-07 13:38:48 -0800366 if (!VerifyPointer(pointer, "malloc_usable_size")) {
367 return 0;
368 }
369
370 return InternalMallocUsableSize(pointer);
Christopher Ferris55a89a42016-04-07 17:14:53 -0700371}
372
Christopher Ferris4da25032018-03-07 13:38:48 -0800373static void* InternalMalloc(size_t size) {
374 if ((g_debug->config().options() & BACKTRACE) && g_debug->pointer->ShouldDumpAndReset()) {
375 debug_dump_heap(android::base::StringPrintf(
376 "%s.%d.txt", g_debug->config().backtrace_dump_prefix().c_str(), getpid())
377 .c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700378 }
379
Colin Cross9567c7b2016-03-09 17:56:14 -0800380 if (size == 0) {
381 size = 1;
382 }
383
Christopher Ferris63860cb2015-11-16 17:30:32 -0800384 size_t real_size = size + g_debug->extra_bytes();
385 if (real_size < size) {
386 // Overflow.
387 errno = ENOMEM;
388 return nullptr;
389 }
390
Christopher Ferris4da25032018-03-07 13:38:48 -0800391 if (size > PointerInfoType::MaxSize()) {
392 errno = ENOMEM;
393 return nullptr;
394 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800395
Christopher Ferris4da25032018-03-07 13:38:48 -0800396 void* pointer;
397 if (g_debug->HeaderEnabled()) {
398 Header* header =
399 reinterpret_cast<Header*>(g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800400 if (header == nullptr) {
401 return nullptr;
402 }
403 pointer = InitHeader(header, header, size);
404 } else {
405 pointer = g_dispatch->malloc(real_size);
406 }
407
Christopher Ferris4da25032018-03-07 13:38:48 -0800408 if (pointer != nullptr) {
409 if (g_debug->TrackPointers()) {
410 PointerData::Add(pointer, size);
411 }
412
413 if (g_debug->config().options() & FILL_ON_ALLOC) {
414 size_t bytes = InternalMallocUsableSize(pointer);
415 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes();
416 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
417 memset(pointer, g_debug->config().fill_alloc_value(), bytes);
418 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800419 }
420 return pointer;
421}
422
Christopher Ferris55a89a42016-04-07 17:14:53 -0700423void* debug_malloc(size_t size) {
424 if (DebugCallsDisabled()) {
425 return g_dispatch->malloc(size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800426 }
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700427 ScopedConcurrentLock lock;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700428 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800429
Christopher Ferris4da25032018-03-07 13:38:48 -0800430 void* pointer = InternalMalloc(size);
Christopher Ferris7bd01782016-04-20 12:30:58 -0700431
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700432 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700433 g_debug->record->AddEntry(new MallocEntry(pointer, size));
434 }
435
436 return pointer;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700437}
438
Christopher Ferris4da25032018-03-07 13:38:48 -0800439static void InternalFree(void* pointer) {
440 if ((g_debug->config().options() & BACKTRACE) && g_debug->pointer->ShouldDumpAndReset()) {
441 debug_dump_heap(android::base::StringPrintf(
442 "%s.%d.txt", g_debug->config().backtrace_dump_prefix().c_str(), getpid())
443 .c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700444 }
445
Christopher Ferris63860cb2015-11-16 17:30:32 -0800446 void* free_pointer = pointer;
447 size_t bytes;
Christopher Ferrisd0919622016-03-15 22:39:39 -0700448 Header* header;
Christopher Ferris4da25032018-03-07 13:38:48 -0800449 if (g_debug->HeaderEnabled()) {
Christopher Ferrisd0919622016-03-15 22:39:39 -0700450 header = g_debug->GetHeader(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800451 free_pointer = header->orig_pointer;
452
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700453 if (g_debug->config().options() & FRONT_GUARD) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700454 if (!g_debug->front_guard->Valid(header)) {
455 g_debug->front_guard->LogFailure(header);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800456 }
457 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700458 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700459 if (!g_debug->rear_guard->Valid(header)) {
460 g_debug->rear_guard->LogFailure(header);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800461 }
462 }
463
Christopher Ferris7993b802016-01-28 18:35:05 -0800464 header->tag = DEBUG_FREE_TAG;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800465
466 bytes = header->usable_size;
467 } else {
468 bytes = g_dispatch->malloc_usable_size(pointer);
469 }
470
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700471 if (g_debug->config().options() & FILL_ON_FREE) {
472 size_t fill_bytes = g_debug->config().fill_on_free_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800473 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700474 memset(pointer, g_debug->config().fill_free_value(), bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800475 }
476
Christopher Ferris4da25032018-03-07 13:38:48 -0800477 if (g_debug->TrackPointers()) {
478 PointerData::Remove(pointer);
479 }
480
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700481 if (g_debug->config().options() & FREE_TRACK) {
Christopher Ferrisd0919622016-03-15 22:39:39 -0700482 // Do not add the allocation until we are done modifying the pointer
483 // itself. This avoids a race if a lot of threads are all doing
484 // frees at the same time and we wind up trying to really free this
485 // pointer from another thread, while still trying to free it in
486 // this function.
Christopher Ferris4da25032018-03-07 13:38:48 -0800487 pointer = PointerData::AddFreed(pointer);
488 if (pointer != nullptr) {
489 if (g_debug->HeaderEnabled()) {
490 pointer = g_debug->GetHeader(pointer)->orig_pointer;
491 }
492 g_dispatch->free(pointer);
493 }
Christopher Ferrisd0919622016-03-15 22:39:39 -0700494 } else {
495 g_dispatch->free(free_pointer);
496 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800497}
498
Christopher Ferris55a89a42016-04-07 17:14:53 -0700499void debug_free(void* pointer) {
500 if (DebugCallsDisabled() || pointer == nullptr) {
501 return g_dispatch->free(pointer);
502 }
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700503 ScopedConcurrentLock lock;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700504 ScopedDisableDebugCalls disable;
505
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700506 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700507 g_debug->record->AddEntry(new FreeEntry(pointer));
508 }
509
Christopher Ferris4da25032018-03-07 13:38:48 -0800510 if (!VerifyPointer(pointer, "free")) {
511 return;
512 }
513
514 InternalFree(pointer);
Christopher Ferris55a89a42016-04-07 17:14:53 -0700515}
516
Christopher Ferris63860cb2015-11-16 17:30:32 -0800517void* debug_memalign(size_t alignment, size_t bytes) {
518 if (DebugCallsDisabled()) {
519 return g_dispatch->memalign(alignment, bytes);
520 }
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700521 ScopedConcurrentLock lock;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700522 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800523
Colin Cross9567c7b2016-03-09 17:56:14 -0800524 if (bytes == 0) {
525 bytes = 1;
526 }
527
Christopher Ferris4da25032018-03-07 13:38:48 -0800528 if (bytes > PointerInfoType::MaxSize()) {
529 errno = ENOMEM;
530 return nullptr;
531 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800532
Christopher Ferris4da25032018-03-07 13:38:48 -0800533 void* pointer;
534 if (g_debug->HeaderEnabled()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800535 // Make the alignment a power of two.
536 if (!powerof2(alignment)) {
537 alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment);
538 }
Christopher Ferris72df6702016-02-11 15:51:31 -0800539 // Force the alignment to at least MINIMUM_ALIGNMENT_BYTES to guarantee
Christopher Ferris63860cb2015-11-16 17:30:32 -0800540 // that the header is aligned properly.
Christopher Ferris72df6702016-02-11 15:51:31 -0800541 if (alignment < MINIMUM_ALIGNMENT_BYTES) {
542 alignment = MINIMUM_ALIGNMENT_BYTES;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800543 }
544
545 // We don't have any idea what the natural alignment of
546 // the underlying native allocator is, so we always need to
547 // over allocate.
548 size_t real_size = alignment + bytes + g_debug->extra_bytes();
549 if (real_size < bytes) {
550 // Overflow.
551 errno = ENOMEM;
552 return nullptr;
553 }
554
555 pointer = g_dispatch->malloc(real_size);
556 if (pointer == nullptr) {
557 return nullptr;
558 }
559
560 uintptr_t value = reinterpret_cast<uintptr_t>(pointer) + g_debug->pointer_offset();
561 // Now align the pointer.
562 value += (-value % alignment);
563
564 Header* header = g_debug->GetHeader(reinterpret_cast<void*>(value));
565 pointer = InitHeader(header, pointer, bytes);
566 } else {
567 size_t real_size = bytes + g_debug->extra_bytes();
568 if (real_size < bytes) {
569 // Overflow.
570 errno = ENOMEM;
571 return nullptr;
572 }
573 pointer = g_dispatch->memalign(alignment, real_size);
574 }
575
Christopher Ferris4da25032018-03-07 13:38:48 -0800576 if (pointer != nullptr) {
577 if (g_debug->TrackPointers()) {
578 PointerData::Add(pointer, bytes);
579 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700580
Christopher Ferris4da25032018-03-07 13:38:48 -0800581 if (g_debug->config().options() & FILL_ON_ALLOC) {
582 size_t bytes = InternalMallocUsableSize(pointer);
583 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes();
584 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
585 memset(pointer, g_debug->config().fill_alloc_value(), bytes);
586 }
587
588 if (g_debug->config().options() & RECORD_ALLOCS) {
589 g_debug->record->AddEntry(new MemalignEntry(pointer, bytes, alignment));
590 }
Christopher Ferris7bd01782016-04-20 12:30:58 -0700591 }
592
Christopher Ferris63860cb2015-11-16 17:30:32 -0800593 return pointer;
594}
595
596void* debug_realloc(void* pointer, size_t bytes) {
597 if (DebugCallsDisabled()) {
598 return g_dispatch->realloc(pointer, bytes);
599 }
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700600 ScopedConcurrentLock lock;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700601 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800602
603 if (pointer == nullptr) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800604 pointer = InternalMalloc(bytes);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700605 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700606 g_debug->record->AddEntry(new ReallocEntry(pointer, bytes, nullptr));
607 }
608 return pointer;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800609 }
610
Christopher Ferris4da25032018-03-07 13:38:48 -0800611 if (!VerifyPointer(pointer, "realloc")) {
612 return nullptr;
613 }
614
Christopher Ferris63860cb2015-11-16 17:30:32 -0800615 if (bytes == 0) {
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700616 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700617 g_debug->record->AddEntry(new ReallocEntry(nullptr, bytes, pointer));
618 }
619
Christopher Ferris4da25032018-03-07 13:38:48 -0800620 InternalFree(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800621 return nullptr;
622 }
623
624 size_t real_size = bytes;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700625 if (g_debug->config().options() & EXPAND_ALLOC) {
626 real_size += g_debug->config().expand_alloc_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800627 if (real_size < bytes) {
628 // Overflow.
629 errno = ENOMEM;
630 return nullptr;
631 }
632 }
633
Christopher Ferris4da25032018-03-07 13:38:48 -0800634 if (bytes > PointerInfoType::MaxSize()) {
635 errno = ENOMEM;
636 return nullptr;
637 }
638
Christopher Ferris63860cb2015-11-16 17:30:32 -0800639 void* new_pointer;
640 size_t prev_size;
Christopher Ferris4da25032018-03-07 13:38:48 -0800641 if (g_debug->HeaderEnabled()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800642 // Same size, do nothing.
Christopher Ferris4da25032018-03-07 13:38:48 -0800643 Header* header = g_debug->GetHeader(pointer);
644 if (real_size == header->size) {
645 if (g_debug->TrackPointers()) {
646 // Remove and re-add so that the backtrace is updated.
647 PointerData::Remove(pointer);
648 PointerData::Add(pointer, real_size);
649 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800650 return pointer;
651 }
652
653 // Allocation is shrinking.
654 if (real_size < header->usable_size) {
655 header->size = real_size;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700656 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800657 // Don't bother allocating a smaller pointer in this case, simply
658 // change the header usable_size and reset the rear guard.
Christopher Ferris4da25032018-03-07 13:38:48 -0800659 header->usable_size = header->size;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700660 memset(g_debug->GetRearGuard(header), g_debug->config().rear_guard_value(),
661 g_debug->config().rear_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800662 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800663 if (g_debug->TrackPointers()) {
664 // Remove and re-add so that the backtrace is updated.
665 PointerData::Remove(pointer);
666 PointerData::Add(pointer, real_size);
667 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800668 return pointer;
669 }
670
671 // Allocate the new size.
Christopher Ferris4da25032018-03-07 13:38:48 -0800672 new_pointer = InternalMalloc(bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800673 if (new_pointer == nullptr) {
674 errno = ENOMEM;
675 return nullptr;
676 }
677
678 prev_size = header->usable_size;
679 memcpy(new_pointer, pointer, prev_size);
Christopher Ferris4da25032018-03-07 13:38:48 -0800680 InternalFree(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800681 } else {
Christopher Ferris4da25032018-03-07 13:38:48 -0800682 if (g_debug->TrackPointers()) {
683 PointerData::Remove(pointer);
684 }
685
Christopher Ferris63860cb2015-11-16 17:30:32 -0800686 prev_size = g_dispatch->malloc_usable_size(pointer);
687 new_pointer = g_dispatch->realloc(pointer, real_size);
688 if (new_pointer == nullptr) {
689 return nullptr;
690 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800691
692 if (g_debug->TrackPointers()) {
693 PointerData::Add(new_pointer, real_size);
694 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800695 }
696
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700697 if (g_debug->config().options() & FILL_ON_ALLOC) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800698 size_t bytes = InternalMallocUsableSize(new_pointer);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700699 if (bytes > g_debug->config().fill_on_alloc_bytes()) {
700 bytes = g_debug->config().fill_on_alloc_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800701 }
702 if (bytes > prev_size) {
703 memset(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(new_pointer) + prev_size),
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700704 g_debug->config().fill_alloc_value(), bytes - prev_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800705 }
706 }
707
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700708 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700709 g_debug->record->AddEntry(new ReallocEntry(new_pointer, bytes, pointer));
710 }
711
Christopher Ferris63860cb2015-11-16 17:30:32 -0800712 return new_pointer;
713}
714
715void* debug_calloc(size_t nmemb, size_t bytes) {
716 if (DebugCallsDisabled()) {
717 return g_dispatch->calloc(nmemb, bytes);
718 }
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700719 ScopedConcurrentLock lock;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700720 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800721
Colin Cross7877df62016-03-10 13:01:27 -0800722 size_t size;
723 if (__builtin_mul_overflow(nmemb, bytes, &size)) {
724 // Overflow
725 errno = ENOMEM;
726 return nullptr;
727 }
728
Colin Cross9567c7b2016-03-09 17:56:14 -0800729 if (size == 0) {
730 size = 1;
731 }
732
Colin Cross7877df62016-03-10 13:01:27 -0800733 size_t real_size;
734 if (__builtin_add_overflow(size, g_debug->extra_bytes(), &real_size)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800735 // Overflow.
736 errno = ENOMEM;
737 return nullptr;
738 }
739
Christopher Ferris4da25032018-03-07 13:38:48 -0800740 if (real_size > PointerInfoType::MaxSize()) {
741 errno = ENOMEM;
742 return nullptr;
743 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800744
Christopher Ferris4da25032018-03-07 13:38:48 -0800745 void* pointer;
746 if (g_debug->HeaderEnabled()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800747 // Need to guarantee the alignment of the header.
Christopher Ferris4da25032018-03-07 13:38:48 -0800748 Header* header =
749 reinterpret_cast<Header*>(g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800750 if (header == nullptr) {
751 return nullptr;
752 }
753 memset(header, 0, g_dispatch->malloc_usable_size(header));
Christopher Ferris7bd01782016-04-20 12:30:58 -0700754 pointer = InitHeader(header, header, size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800755 } else {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700756 pointer = g_dispatch->calloc(1, real_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800757 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800758
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700759 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700760 g_debug->record->AddEntry(new CallocEntry(pointer, bytes, nmemb));
761 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800762
763 if (pointer != nullptr && g_debug->TrackPointers()) {
764 PointerData::Add(pointer, size);
765 }
Christopher Ferris7bd01782016-04-20 12:30:58 -0700766 return pointer;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800767}
768
769struct mallinfo debug_mallinfo() {
770 return g_dispatch->mallinfo();
771}
772
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700773int debug_mallopt(int param, int value) {
774 return g_dispatch->mallopt(param, value);
775}
776
Christopher Ferris6c619a02019-03-01 17:59:51 -0800777int debug_malloc_info(int options, FILE* fp) {
778 if (DebugCallsDisabled() || !g_debug->TrackPointers()) {
779 return g_dispatch->malloc_info(options, fp);
780 }
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700781 ScopedConcurrentLock lock;
782 ScopedDisableDebugCalls disable;
Christopher Ferris6c619a02019-03-01 17:59:51 -0800783
784 MallocXmlElem root(fp, "malloc", "version=\"debug-malloc-1\"");
785 std::vector<ListInfoType> list;
786 PointerData::GetAllocList(&list);
787
788 size_t alloc_num = 0;
789 for (size_t i = 0; i < list.size(); i++) {
790 MallocXmlElem alloc(fp, "allocation", "nr=\"%zu\"", alloc_num);
791
792 size_t total = 1;
793 size_t size = list[i].size;
794 while (i < list.size() - 1 && list[i + 1].size == size) {
795 i++;
796 total++;
797 }
798 MallocXmlElem(fp, "size").Contents("%zu", list[i].size);
799 MallocXmlElem(fp, "total").Contents("%zu", total);
800 alloc_num++;
801 }
802 return 0;
803}
804
Christopher Ferriscae21a92018-02-05 18:14:55 -0800805void* debug_aligned_alloc(size_t alignment, size_t size) {
806 if (DebugCallsDisabled()) {
807 return g_dispatch->aligned_alloc(alignment, size);
808 }
Christopher Ferrisa22f5d52019-03-01 16:40:59 -0800809 if (!powerof2(alignment) || (size % alignment) != 0) {
Christopher Ferriscae21a92018-02-05 18:14:55 -0800810 errno = EINVAL;
811 return nullptr;
812 }
813 return debug_memalign(alignment, size);
814}
815
Christopher Ferris63860cb2015-11-16 17:30:32 -0800816int debug_posix_memalign(void** memptr, size_t alignment, size_t size) {
817 if (DebugCallsDisabled()) {
818 return g_dispatch->posix_memalign(memptr, alignment, size);
819 }
820
Christopher Ferris6c619a02019-03-01 17:59:51 -0800821 if (alignment < sizeof(void*) || !powerof2(alignment)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800822 return EINVAL;
823 }
824 int saved_errno = errno;
825 *memptr = debug_memalign(alignment, size);
826 errno = saved_errno;
827 return (*memptr != nullptr) ? 0 : ENOMEM;
828}
829
Christopher Ferris4da25032018-03-07 13:38:48 -0800830int debug_iterate(uintptr_t base, size_t size, void (*callback)(uintptr_t, size_t, void*),
831 void* arg) {
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700832 ScopedConcurrentLock lock;
Christopher Ferris4da25032018-03-07 13:38:48 -0800833 if (g_debug->TrackPointers()) {
834 // Since malloc is disabled, don't bother acquiring any locks.
835 for (auto it = PointerData::begin(); it != PointerData::end(); ++it) {
836 callback(it->first, InternalMallocUsableSize(reinterpret_cast<void*>(it->first)), arg);
837 }
838 return 0;
839 }
Colin Cross869691c2016-01-29 12:48:18 -0800840
Christopher Ferris4da25032018-03-07 13:38:48 -0800841 // An option that adds a header will add pointer tracking, so no need to
842 // check if headers are enabled.
843 return g_dispatch->iterate(base, size, callback, arg);
Colin Cross869691c2016-01-29 12:48:18 -0800844}
845
846void debug_malloc_disable() {
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700847 ScopedConcurrentLock lock;
Colin Cross869691c2016-01-29 12:48:18 -0800848 g_dispatch->malloc_disable();
Christopher Ferris4da25032018-03-07 13:38:48 -0800849 if (g_debug->pointer) {
850 g_debug->pointer->PrepareFork();
Colin Cross869691c2016-01-29 12:48:18 -0800851 }
852}
853
854void debug_malloc_enable() {
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700855 ScopedConcurrentLock lock;
Christopher Ferris4da25032018-03-07 13:38:48 -0800856 if (g_debug->pointer) {
857 g_debug->pointer->PostForkParent();
Colin Cross869691c2016-01-29 12:48:18 -0800858 }
859 g_dispatch->malloc_enable();
860}
861
Christopher Ferris4da25032018-03-07 13:38:48 -0800862ssize_t debug_malloc_backtrace(void* pointer, uintptr_t* frames, size_t max_frames) {
Colin Cross2d4721c2016-02-02 11:57:54 -0800863 if (DebugCallsDisabled() || pointer == nullptr) {
864 return 0;
865 }
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700866 ScopedConcurrentLock lock;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700867 ScopedDisableDebugCalls disable;
Colin Cross2d4721c2016-02-02 11:57:54 -0800868
Christopher Ferris4da25032018-03-07 13:38:48 -0800869 if (!(g_debug->config().options() & BACKTRACE)) {
870 return 0;
Colin Cross2d4721c2016-02-02 11:57:54 -0800871 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800872 return PointerData::GetFrames(pointer, frames, max_frames);
Colin Cross2d4721c2016-02-02 11:57:54 -0800873}
874
Christopher Ferris63860cb2015-11-16 17:30:32 -0800875#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
876void* debug_pvalloc(size_t bytes) {
877 if (DebugCallsDisabled()) {
878 return g_dispatch->pvalloc(bytes);
879 }
880
881 size_t pagesize = getpagesize();
Dan Alberta613d0d2017-10-05 16:39:33 -0700882 size_t size = __BIONIC_ALIGN(bytes, pagesize);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800883 if (size < bytes) {
884 // Overflow
885 errno = ENOMEM;
886 return nullptr;
887 }
888 return debug_memalign(pagesize, size);
889}
890
891void* debug_valloc(size_t size) {
892 if (DebugCallsDisabled()) {
893 return g_dispatch->valloc(size);
894 }
895 return debug_memalign(getpagesize(), size);
896}
897#endif
Christopher Ferris602b88c2017-08-04 13:04:04 -0700898
899static std::mutex g_dump_lock;
900
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700901static void write_dump(FILE* fp) {
902 fprintf(fp, "Android Native Heap Dump v1.2\n\n");
Christopher Ferris602b88c2017-08-04 13:04:04 -0700903
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700904 std::string fingerprint = android::base::GetProperty("ro.build.fingerprint", "unknown");
905 fprintf(fp, "Build fingerprint: '%s'\n\n", fingerprint.c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700906
Christopher Ferris4da25032018-03-07 13:38:48 -0800907 PointerData::DumpLiveToFile(fp);
Christopher Ferris602b88c2017-08-04 13:04:04 -0700908
909 fprintf(fp, "MAPS\n");
910 std::string content;
911 if (!android::base::ReadFileToString("/proc/self/maps", &content)) {
912 fprintf(fp, "Could not open /proc/self/maps\n");
913 } else {
914 fprintf(fp, "%s", content.c_str());
915 }
916 fprintf(fp, "END\n");
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700917}
918
919bool debug_write_malloc_leak_info(FILE* fp) {
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700920 ScopedConcurrentLock lock;
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700921 ScopedDisableDebugCalls disable;
922
923 std::lock_guard<std::mutex> guard(g_dump_lock);
924
925 if (!(g_debug->config().options() & BACKTRACE)) {
926 return false;
927 }
928
929 write_dump(fp);
Christopher Ferris602b88c2017-08-04 13:04:04 -0700930 return true;
931}
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700932
933void debug_dump_heap(const char* file_name) {
Christopher Ferrisd269fcc2019-05-06 19:03:59 -0700934 ScopedConcurrentLock lock;
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700935 ScopedDisableDebugCalls disable;
936
937 std::lock_guard<std::mutex> guard(g_dump_lock);
938
939 FILE* fp = fopen(file_name, "w+e");
940 if (fp == nullptr) {
941 error_log("Unable to create file: %s", file_name);
942 return;
943 }
944
945 error_log("Dumping to file: %s\n", file_name);
946 write_dump(fp);
947 fclose(fp);
948}