blob: f66295706180307e91106cfb6743bccb0afecd0a [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>
32#include <string.h>
33#include <sys/cdefs.h>
34#include <sys/param.h>
35#include <unistd.h>
36
Christopher Ferris602b88c2017-08-04 13:04:04 -070037#include <mutex>
Christopher Ferris63860cb2015-11-16 17:30:32 -080038#include <vector>
39
Christopher Ferris602b88c2017-08-04 13:04:04 -070040#include <android-base/file.h>
Christopher Ferris2e1a40a2018-06-13 10:46:34 -070041#include <android-base/properties.h>
Christopher Ferris602b88c2017-08-04 13:04:04 -070042#include <android-base/stringprintf.h>
Christopher Ferris63860cb2015-11-16 17:30:32 -080043#include <private/bionic_malloc_dispatch.h>
44
Christopher Ferris72df6702016-02-11 15:51:31 -080045#include "Config.h"
Christopher Ferris63860cb2015-11-16 17:30:32 -080046#include "DebugData.h"
Christopher Ferris4da25032018-03-07 13:38:48 -080047#include "backtrace.h"
Christopher Ferris63860cb2015-11-16 17:30:32 -080048#include "debug_disable.h"
49#include "debug_log.h"
50#include "malloc_debug.h"
Christopher Ferris93bdd6a2018-04-05 11:12:38 -070051#include "UnwindBacktrace.h"
Christopher Ferris63860cb2015-11-16 17:30:32 -080052
53// ------------------------------------------------------------------------
54// Global Data
55// ------------------------------------------------------------------------
56DebugData* g_debug;
57
58int* g_malloc_zygote_child;
59
60const MallocDispatch* g_dispatch;
61// ------------------------------------------------------------------------
62
63// ------------------------------------------------------------------------
64// Use C style prototypes for all exported functions. This makes it easy
65// to do dlsym lookups during libc initialization when malloc debug
66// is enabled.
67// ------------------------------------------------------------------------
68__BEGIN_DECLS
69
Tamas Berghammerac81fe82016-08-26 15:54:59 +010070bool debug_initialize(const MallocDispatch* malloc_dispatch, int* malloc_zygote_child,
Christopher Ferris4da25032018-03-07 13:38:48 -080071 const char* options);
Christopher Ferris63860cb2015-11-16 17:30:32 -080072void debug_finalize();
Christopher Ferris2e1a40a2018-06-13 10:46:34 -070073void debug_dump_heap(const char* file_name);
Christopher Ferris4da25032018-03-07 13:38:48 -080074void debug_get_malloc_leak_info(uint8_t** info, size_t* overall_size, size_t* info_size,
75 size_t* total_memory, size_t* backtrace_size);
Christopher Ferris2e1a40a2018-06-13 10:46:34 -070076bool debug_write_malloc_leak_info(FILE* fp);
Colin Cross2d4721c2016-02-02 11:57:54 -080077ssize_t debug_malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count);
Christopher Ferris63860cb2015-11-16 17:30:32 -080078void debug_free_malloc_leak_info(uint8_t* info);
79size_t debug_malloc_usable_size(void* pointer);
80void* debug_malloc(size_t size);
81void debug_free(void* pointer);
Christopher Ferriscae21a92018-02-05 18:14:55 -080082void* debug_aligned_alloc(size_t alignment, size_t size);
Christopher Ferris63860cb2015-11-16 17:30:32 -080083void* debug_memalign(size_t alignment, size_t bytes);
84void* debug_realloc(void* pointer, size_t bytes);
85void* debug_calloc(size_t nmemb, size_t bytes);
86struct mallinfo debug_mallinfo();
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -070087int debug_mallopt(int param, int value);
Christopher Ferris63860cb2015-11-16 17:30:32 -080088int debug_posix_memalign(void** memptr, size_t alignment, size_t size);
Colin Cross869691c2016-01-29 12:48:18 -080089int debug_iterate(uintptr_t base, size_t size,
Christopher Ferris4da25032018-03-07 13:38:48 -080090 void (*callback)(uintptr_t base, size_t size, void* arg), void* arg);
Colin Cross869691c2016-01-29 12:48:18 -080091void debug_malloc_disable();
92void debug_malloc_enable();
Christopher Ferris63860cb2015-11-16 17:30:32 -080093
94#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
95void* debug_pvalloc(size_t bytes);
96void* debug_valloc(size_t size);
97#endif
98
99__END_DECLS
100// ------------------------------------------------------------------------
101
Colin Cross7a28a3c2016-02-07 22:51:15 -0800102static void InitAtfork() {
103 static pthread_once_t atfork_init = PTHREAD_ONCE_INIT;
Christopher Ferris4da25032018-03-07 13:38:48 -0800104 pthread_once(&atfork_init, []() {
Colin Cross7a28a3c2016-02-07 22:51:15 -0800105 pthread_atfork(
Christopher Ferris4da25032018-03-07 13:38:48 -0800106 []() {
Colin Cross7a28a3c2016-02-07 22:51:15 -0800107 if (g_debug != nullptr) {
108 g_debug->PrepareFork();
109 }
110 },
Christopher Ferris4da25032018-03-07 13:38:48 -0800111 []() {
Colin Cross7a28a3c2016-02-07 22:51:15 -0800112 if (g_debug != nullptr) {
113 g_debug->PostForkParent();
114 }
115 },
Christopher Ferris4da25032018-03-07 13:38:48 -0800116 []() {
Colin Cross7a28a3c2016-02-07 22:51:15 -0800117 if (g_debug != nullptr) {
118 g_debug->PostForkChild();
119 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800120 });
Colin Cross7a28a3c2016-02-07 22:51:15 -0800121 });
122}
Christopher Ferrisd0919622016-03-15 22:39:39 -0700123
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700124void BacktraceAndLog() {
125 if (g_debug->config().options() & BACKTRACE_FULL) {
126 std::vector<uintptr_t> frames;
127 std::vector<unwindstack::LocalFrameData> frames_info;
128 if (!Unwind(&frames, &frames_info, 256)) {
129 error_log(" Backtrace failed to get any frames.");
130 } else {
131 UnwindLog(frames_info);
132 }
133 } else {
134 std::vector<uintptr_t> frames(256);
135 size_t num_frames = backtrace_get(frames.data(), frames.size());
136 if (num_frames == 0) {
137 error_log(" Backtrace failed to get any frames.");
138 } else {
139 backtrace_log(frames.data(), num_frames);
140 }
141 }
142}
143
Christopher Ferris4da25032018-03-07 13:38:48 -0800144static void LogError(const void* pointer, const char* error_str) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800145 error_log(LOG_DIVIDER);
Christopher Ferris4da25032018-03-07 13:38:48 -0800146 error_log("+++ ALLOCATION %p %s", pointer, error_str);
147
148 // If we are tracking already freed pointers, check to see if this is
149 // one so we can print extra information.
150 if (g_debug->config().options() & FREE_TRACK) {
151 PointerData::LogFreeBacktrace(pointer);
Christopher Ferris7993b802016-01-28 18:35:05 -0800152 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800153
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700154 error_log("Backtrace at time of failure:");
155 BacktraceAndLog();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800156 error_log(LOG_DIVIDER);
Iris Chang7f209a92019-01-16 11:17:15 +0800157 if (g_debug->config().options() & ABORT_ON_ERROR) {
158 abort();
159 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800160}
161
Christopher Ferris4da25032018-03-07 13:38:48 -0800162static bool VerifyPointer(const void* pointer, const char* function_name) {
163 if (g_debug->HeaderEnabled()) {
164 Header* header = g_debug->GetHeader(pointer);
165 if (header->tag != DEBUG_TAG) {
166 std::string error_str;
167 if (header->tag == DEBUG_FREE_TAG) {
168 error_str = std::string("USED AFTER FREE (") + function_name + ")";
169 } else {
170 error_str = android::base::StringPrintf("HAS INVALID TAG %" PRIx32 " (%s)", header->tag,
171 function_name);
172 }
173 LogError(pointer, error_str.c_str());
174 return false;
175 }
176 }
177
178 if (g_debug->TrackPointers()) {
179 if (!PointerData::Exists(pointer)) {
180 std::string error_str(std::string("UNKNOWN POINTER (") + function_name + ")");
181 LogError(pointer, error_str.c_str());
182 return false;
183 }
184 }
185 return true;
186}
187
188static size_t InternalMallocUsableSize(void* pointer) {
189 if (g_debug->HeaderEnabled()) {
190 return g_debug->GetHeader(pointer)->usable_size;
191 } else {
192 return g_dispatch->malloc_usable_size(pointer);
193 }
194}
195
Christopher Ferris63860cb2015-11-16 17:30:32 -0800196static void* InitHeader(Header* header, void* orig_pointer, size_t size) {
197 header->tag = DEBUG_TAG;
198 header->orig_pointer = orig_pointer;
199 header->size = size;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800200 header->usable_size = g_dispatch->malloc_usable_size(orig_pointer);
201 if (header->usable_size == 0) {
202 g_dispatch->free(orig_pointer);
203 return nullptr;
204 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800205 header->usable_size -= g_debug->pointer_offset() + reinterpret_cast<uintptr_t>(header) -
206 reinterpret_cast<uintptr_t>(orig_pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800207
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700208 if (g_debug->config().options() & FRONT_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800209 uint8_t* guard = g_debug->GetFrontGuard(header);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700210 memset(guard, g_debug->config().front_guard_value(), g_debug->config().front_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800211 }
212
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700213 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800214 uint8_t* guard = g_debug->GetRearGuard(header);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700215 memset(guard, g_debug->config().rear_guard_value(), g_debug->config().rear_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800216 // If the rear guard is enabled, set the usable size to the exact size
217 // of the allocation.
Christopher Ferris4da25032018-03-07 13:38:48 -0800218 header->usable_size = header->size;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800219 }
220
221 return g_debug->GetPointer(header);
222}
223
Tamas Berghammerac81fe82016-08-26 15:54:59 +0100224bool debug_initialize(const MallocDispatch* malloc_dispatch, int* malloc_zygote_child,
Christopher Ferris4da25032018-03-07 13:38:48 -0800225 const char* options) {
Tamas Berghammerac81fe82016-08-26 15:54:59 +0100226 if (malloc_zygote_child == nullptr || options == nullptr) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800227 return false;
228 }
Colin Cross7a28a3c2016-02-07 22:51:15 -0800229
230 InitAtfork();
231
Christopher Ferris63860cb2015-11-16 17:30:32 -0800232 g_malloc_zygote_child = malloc_zygote_child;
233
234 g_dispatch = malloc_dispatch;
235
236 if (!DebugDisableInitialize()) {
237 return false;
238 }
239
240 DebugData* debug = new DebugData();
Tamas Berghammerac81fe82016-08-26 15:54:59 +0100241 if (!debug->Initialize(options)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800242 delete debug;
243 DebugDisableFinalize();
244 return false;
245 }
246 g_debug = debug;
247
248 // Always enable the backtrace code since we will use it in a number
249 // of different error cases.
250 backtrace_startup();
251
252 return true;
253}
254
255void debug_finalize() {
256 if (g_debug == nullptr) {
257 return;
258 }
259
Christopher Ferris97b47472018-07-10 14:45:24 -0700260 // Turn off capturing allocations calls.
261 DebugDisableSet(true);
262
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700263 if (g_debug->config().options() & FREE_TRACK) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800264 PointerData::VerifyAllFreed();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800265 }
266
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700267 if (g_debug->config().options() & LEAK_TRACK) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800268 PointerData::LogLeaks();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800269 }
270
Christopher Ferris602b88c2017-08-04 13:04:04 -0700271 if ((g_debug->config().options() & BACKTRACE) && g_debug->config().backtrace_dump_on_exit()) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800272 debug_dump_heap(android::base::StringPrintf("%s.%d.exit.txt",
273 g_debug->config().backtrace_dump_prefix().c_str(),
Christopher Ferris97b47472018-07-10 14:45:24 -0700274 getpid()).c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700275 }
276
Colin Cross2c759912016-02-05 16:17:39 -0800277 backtrace_shutdown();
278
Christopher Ferris63860cb2015-11-16 17:30:32 -0800279 delete g_debug;
280 g_debug = nullptr;
281
282 DebugDisableFinalize();
283}
284
Christopher Ferris4da25032018-03-07 13:38:48 -0800285void debug_get_malloc_leak_info(uint8_t** info, size_t* overall_size, size_t* info_size,
286 size_t* total_memory, size_t* backtrace_size) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800287 ScopedDisableDebugCalls disable;
288
289 // Verify the arguments.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700290 if (info == nullptr || overall_size == nullptr || info_size == nullptr || total_memory == nullptr ||
Christopher Ferris4da25032018-03-07 13:38:48 -0800291 backtrace_size == nullptr) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800292 error_log("get_malloc_leak_info: At least one invalid parameter.");
293 return;
294 }
295
296 *info = nullptr;
297 *overall_size = 0;
298 *info_size = 0;
299 *total_memory = 0;
300 *backtrace_size = 0;
301
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700302 if (!(g_debug->config().options() & BACKTRACE)) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800303 error_log(
304 "get_malloc_leak_info: Allocations not being tracked, to enable "
305 "set the option 'backtrace'.");
Christopher Ferris63860cb2015-11-16 17:30:32 -0800306 return;
307 }
308
Christopher Ferris4da25032018-03-07 13:38:48 -0800309 PointerData::GetInfo(info, overall_size, info_size, total_memory, backtrace_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800310}
311
312void debug_free_malloc_leak_info(uint8_t* info) {
313 g_dispatch->free(info);
314}
315
Christopher Ferris55a89a42016-04-07 17:14:53 -0700316size_t debug_malloc_usable_size(void* pointer) {
317 if (DebugCallsDisabled() || pointer == nullptr) {
318 return g_dispatch->malloc_usable_size(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800319 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700320 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800321
Christopher Ferris4da25032018-03-07 13:38:48 -0800322 if (!VerifyPointer(pointer, "malloc_usable_size")) {
323 return 0;
324 }
325
326 return InternalMallocUsableSize(pointer);
Christopher Ferris55a89a42016-04-07 17:14:53 -0700327}
328
Christopher Ferris4da25032018-03-07 13:38:48 -0800329static void* InternalMalloc(size_t size) {
330 if ((g_debug->config().options() & BACKTRACE) && g_debug->pointer->ShouldDumpAndReset()) {
331 debug_dump_heap(android::base::StringPrintf(
332 "%s.%d.txt", g_debug->config().backtrace_dump_prefix().c_str(), getpid())
333 .c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700334 }
335
Colin Cross9567c7b2016-03-09 17:56:14 -0800336 if (size == 0) {
337 size = 1;
338 }
339
Christopher Ferris63860cb2015-11-16 17:30:32 -0800340 size_t real_size = size + g_debug->extra_bytes();
341 if (real_size < size) {
342 // Overflow.
343 errno = ENOMEM;
344 return nullptr;
345 }
346
Christopher Ferris4da25032018-03-07 13:38:48 -0800347 if (size > PointerInfoType::MaxSize()) {
348 errno = ENOMEM;
349 return nullptr;
350 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800351
Christopher Ferris4da25032018-03-07 13:38:48 -0800352 void* pointer;
353 if (g_debug->HeaderEnabled()) {
354 Header* header =
355 reinterpret_cast<Header*>(g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800356 if (header == nullptr) {
357 return nullptr;
358 }
359 pointer = InitHeader(header, header, size);
360 } else {
361 pointer = g_dispatch->malloc(real_size);
362 }
363
Christopher Ferris4da25032018-03-07 13:38:48 -0800364 if (pointer != nullptr) {
365 if (g_debug->TrackPointers()) {
366 PointerData::Add(pointer, size);
367 }
368
369 if (g_debug->config().options() & FILL_ON_ALLOC) {
370 size_t bytes = InternalMallocUsableSize(pointer);
371 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes();
372 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
373 memset(pointer, g_debug->config().fill_alloc_value(), bytes);
374 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800375 }
376 return pointer;
377}
378
Christopher Ferris55a89a42016-04-07 17:14:53 -0700379void* debug_malloc(size_t size) {
380 if (DebugCallsDisabled()) {
381 return g_dispatch->malloc(size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800382 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700383 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800384
Christopher Ferris4da25032018-03-07 13:38:48 -0800385 void* pointer = InternalMalloc(size);
Christopher Ferris7bd01782016-04-20 12:30:58 -0700386
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700387 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700388 g_debug->record->AddEntry(new MallocEntry(pointer, size));
389 }
390
391 return pointer;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700392}
393
Christopher Ferris4da25032018-03-07 13:38:48 -0800394static void InternalFree(void* pointer) {
395 if ((g_debug->config().options() & BACKTRACE) && g_debug->pointer->ShouldDumpAndReset()) {
396 debug_dump_heap(android::base::StringPrintf(
397 "%s.%d.txt", g_debug->config().backtrace_dump_prefix().c_str(), getpid())
398 .c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700399 }
400
Christopher Ferris63860cb2015-11-16 17:30:32 -0800401 void* free_pointer = pointer;
402 size_t bytes;
Christopher Ferrisd0919622016-03-15 22:39:39 -0700403 Header* header;
Christopher Ferris4da25032018-03-07 13:38:48 -0800404 if (g_debug->HeaderEnabled()) {
Christopher Ferrisd0919622016-03-15 22:39:39 -0700405 header = g_debug->GetHeader(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800406 free_pointer = header->orig_pointer;
407
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700408 if (g_debug->config().options() & FRONT_GUARD) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700409 if (!g_debug->front_guard->Valid(header)) {
410 g_debug->front_guard->LogFailure(header);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800411 }
412 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700413 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700414 if (!g_debug->rear_guard->Valid(header)) {
415 g_debug->rear_guard->LogFailure(header);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800416 }
417 }
418
Christopher Ferris7993b802016-01-28 18:35:05 -0800419 header->tag = DEBUG_FREE_TAG;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800420
421 bytes = header->usable_size;
422 } else {
423 bytes = g_dispatch->malloc_usable_size(pointer);
424 }
425
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700426 if (g_debug->config().options() & FILL_ON_FREE) {
427 size_t fill_bytes = g_debug->config().fill_on_free_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800428 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700429 memset(pointer, g_debug->config().fill_free_value(), bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800430 }
431
Christopher Ferris4da25032018-03-07 13:38:48 -0800432 if (g_debug->TrackPointers()) {
433 PointerData::Remove(pointer);
434 }
435
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700436 if (g_debug->config().options() & FREE_TRACK) {
Christopher Ferrisd0919622016-03-15 22:39:39 -0700437 // Do not add the allocation until we are done modifying the pointer
438 // itself. This avoids a race if a lot of threads are all doing
439 // frees at the same time and we wind up trying to really free this
440 // pointer from another thread, while still trying to free it in
441 // this function.
Christopher Ferris4da25032018-03-07 13:38:48 -0800442 pointer = PointerData::AddFreed(pointer);
443 if (pointer != nullptr) {
444 if (g_debug->HeaderEnabled()) {
445 pointer = g_debug->GetHeader(pointer)->orig_pointer;
446 }
447 g_dispatch->free(pointer);
448 }
Christopher Ferrisd0919622016-03-15 22:39:39 -0700449 } else {
450 g_dispatch->free(free_pointer);
451 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800452}
453
Christopher Ferris55a89a42016-04-07 17:14:53 -0700454void debug_free(void* pointer) {
455 if (DebugCallsDisabled() || pointer == nullptr) {
456 return g_dispatch->free(pointer);
457 }
458 ScopedDisableDebugCalls disable;
459
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700460 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700461 g_debug->record->AddEntry(new FreeEntry(pointer));
462 }
463
Christopher Ferris4da25032018-03-07 13:38:48 -0800464 if (!VerifyPointer(pointer, "free")) {
465 return;
466 }
467
468 InternalFree(pointer);
Christopher Ferris55a89a42016-04-07 17:14:53 -0700469}
470
Christopher Ferris63860cb2015-11-16 17:30:32 -0800471void* debug_memalign(size_t alignment, size_t bytes) {
472 if (DebugCallsDisabled()) {
473 return g_dispatch->memalign(alignment, bytes);
474 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700475 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800476
Colin Cross9567c7b2016-03-09 17:56:14 -0800477 if (bytes == 0) {
478 bytes = 1;
479 }
480
Christopher Ferris4da25032018-03-07 13:38:48 -0800481 if (bytes > PointerInfoType::MaxSize()) {
482 errno = ENOMEM;
483 return nullptr;
484 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800485
Christopher Ferris4da25032018-03-07 13:38:48 -0800486 void* pointer;
487 if (g_debug->HeaderEnabled()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800488 // Make the alignment a power of two.
489 if (!powerof2(alignment)) {
490 alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment);
491 }
Christopher Ferris72df6702016-02-11 15:51:31 -0800492 // Force the alignment to at least MINIMUM_ALIGNMENT_BYTES to guarantee
Christopher Ferris63860cb2015-11-16 17:30:32 -0800493 // that the header is aligned properly.
Christopher Ferris72df6702016-02-11 15:51:31 -0800494 if (alignment < MINIMUM_ALIGNMENT_BYTES) {
495 alignment = MINIMUM_ALIGNMENT_BYTES;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800496 }
497
498 // We don't have any idea what the natural alignment of
499 // the underlying native allocator is, so we always need to
500 // over allocate.
501 size_t real_size = alignment + bytes + g_debug->extra_bytes();
502 if (real_size < bytes) {
503 // Overflow.
504 errno = ENOMEM;
505 return nullptr;
506 }
507
508 pointer = g_dispatch->malloc(real_size);
509 if (pointer == nullptr) {
510 return nullptr;
511 }
512
513 uintptr_t value = reinterpret_cast<uintptr_t>(pointer) + g_debug->pointer_offset();
514 // Now align the pointer.
515 value += (-value % alignment);
516
517 Header* header = g_debug->GetHeader(reinterpret_cast<void*>(value));
518 pointer = InitHeader(header, pointer, bytes);
519 } else {
520 size_t real_size = bytes + g_debug->extra_bytes();
521 if (real_size < bytes) {
522 // Overflow.
523 errno = ENOMEM;
524 return nullptr;
525 }
526 pointer = g_dispatch->memalign(alignment, real_size);
527 }
528
Christopher Ferris4da25032018-03-07 13:38:48 -0800529 if (pointer != nullptr) {
530 if (g_debug->TrackPointers()) {
531 PointerData::Add(pointer, bytes);
532 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700533
Christopher Ferris4da25032018-03-07 13:38:48 -0800534 if (g_debug->config().options() & FILL_ON_ALLOC) {
535 size_t bytes = InternalMallocUsableSize(pointer);
536 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes();
537 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
538 memset(pointer, g_debug->config().fill_alloc_value(), bytes);
539 }
540
541 if (g_debug->config().options() & RECORD_ALLOCS) {
542 g_debug->record->AddEntry(new MemalignEntry(pointer, bytes, alignment));
543 }
Christopher Ferris7bd01782016-04-20 12:30:58 -0700544 }
545
Christopher Ferris63860cb2015-11-16 17:30:32 -0800546 return pointer;
547}
548
549void* debug_realloc(void* pointer, size_t bytes) {
550 if (DebugCallsDisabled()) {
551 return g_dispatch->realloc(pointer, bytes);
552 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700553 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800554
555 if (pointer == nullptr) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800556 pointer = InternalMalloc(bytes);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700557 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700558 g_debug->record->AddEntry(new ReallocEntry(pointer, bytes, nullptr));
559 }
560 return pointer;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800561 }
562
Christopher Ferris4da25032018-03-07 13:38:48 -0800563 if (!VerifyPointer(pointer, "realloc")) {
564 return nullptr;
565 }
566
Christopher Ferris63860cb2015-11-16 17:30:32 -0800567 if (bytes == 0) {
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700568 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700569 g_debug->record->AddEntry(new ReallocEntry(nullptr, bytes, pointer));
570 }
571
Christopher Ferris4da25032018-03-07 13:38:48 -0800572 InternalFree(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800573 return nullptr;
574 }
575
576 size_t real_size = bytes;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700577 if (g_debug->config().options() & EXPAND_ALLOC) {
578 real_size += g_debug->config().expand_alloc_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800579 if (real_size < bytes) {
580 // Overflow.
581 errno = ENOMEM;
582 return nullptr;
583 }
584 }
585
Christopher Ferris4da25032018-03-07 13:38:48 -0800586 if (bytes > PointerInfoType::MaxSize()) {
587 errno = ENOMEM;
588 return nullptr;
589 }
590
Christopher Ferris63860cb2015-11-16 17:30:32 -0800591 void* new_pointer;
592 size_t prev_size;
Christopher Ferris4da25032018-03-07 13:38:48 -0800593 if (g_debug->HeaderEnabled()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800594 // Same size, do nothing.
Christopher Ferris4da25032018-03-07 13:38:48 -0800595 Header* header = g_debug->GetHeader(pointer);
596 if (real_size == header->size) {
597 if (g_debug->TrackPointers()) {
598 // Remove and re-add so that the backtrace is updated.
599 PointerData::Remove(pointer);
600 PointerData::Add(pointer, real_size);
601 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800602 return pointer;
603 }
604
605 // Allocation is shrinking.
606 if (real_size < header->usable_size) {
607 header->size = real_size;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700608 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800609 // Don't bother allocating a smaller pointer in this case, simply
610 // change the header usable_size and reset the rear guard.
Christopher Ferris4da25032018-03-07 13:38:48 -0800611 header->usable_size = header->size;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700612 memset(g_debug->GetRearGuard(header), g_debug->config().rear_guard_value(),
613 g_debug->config().rear_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800614 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800615 if (g_debug->TrackPointers()) {
616 // Remove and re-add so that the backtrace is updated.
617 PointerData::Remove(pointer);
618 PointerData::Add(pointer, real_size);
619 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800620 return pointer;
621 }
622
623 // Allocate the new size.
Christopher Ferris4da25032018-03-07 13:38:48 -0800624 new_pointer = InternalMalloc(bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800625 if (new_pointer == nullptr) {
626 errno = ENOMEM;
627 return nullptr;
628 }
629
630 prev_size = header->usable_size;
631 memcpy(new_pointer, pointer, prev_size);
Christopher Ferris4da25032018-03-07 13:38:48 -0800632 InternalFree(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800633 } else {
Christopher Ferris4da25032018-03-07 13:38:48 -0800634 if (g_debug->TrackPointers()) {
635 PointerData::Remove(pointer);
636 }
637
Christopher Ferris63860cb2015-11-16 17:30:32 -0800638 prev_size = g_dispatch->malloc_usable_size(pointer);
639 new_pointer = g_dispatch->realloc(pointer, real_size);
640 if (new_pointer == nullptr) {
641 return nullptr;
642 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800643
644 if (g_debug->TrackPointers()) {
645 PointerData::Add(new_pointer, real_size);
646 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800647 }
648
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700649 if (g_debug->config().options() & FILL_ON_ALLOC) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800650 size_t bytes = InternalMallocUsableSize(new_pointer);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700651 if (bytes > g_debug->config().fill_on_alloc_bytes()) {
652 bytes = g_debug->config().fill_on_alloc_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800653 }
654 if (bytes > prev_size) {
655 memset(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(new_pointer) + prev_size),
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700656 g_debug->config().fill_alloc_value(), bytes - prev_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800657 }
658 }
659
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700660 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700661 g_debug->record->AddEntry(new ReallocEntry(new_pointer, bytes, pointer));
662 }
663
Christopher Ferris63860cb2015-11-16 17:30:32 -0800664 return new_pointer;
665}
666
667void* debug_calloc(size_t nmemb, size_t bytes) {
668 if (DebugCallsDisabled()) {
669 return g_dispatch->calloc(nmemb, bytes);
670 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700671 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800672
Colin Cross7877df62016-03-10 13:01:27 -0800673 size_t size;
674 if (__builtin_mul_overflow(nmemb, bytes, &size)) {
675 // Overflow
676 errno = ENOMEM;
677 return nullptr;
678 }
679
Colin Cross9567c7b2016-03-09 17:56:14 -0800680 if (size == 0) {
681 size = 1;
682 }
683
Colin Cross7877df62016-03-10 13:01:27 -0800684 size_t real_size;
685 if (__builtin_add_overflow(size, g_debug->extra_bytes(), &real_size)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800686 // Overflow.
687 errno = ENOMEM;
688 return nullptr;
689 }
690
Christopher Ferris4da25032018-03-07 13:38:48 -0800691 if (real_size > PointerInfoType::MaxSize()) {
692 errno = ENOMEM;
693 return nullptr;
694 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800695
Christopher Ferris4da25032018-03-07 13:38:48 -0800696 void* pointer;
697 if (g_debug->HeaderEnabled()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800698 // Need to guarantee the alignment of the header.
Christopher Ferris4da25032018-03-07 13:38:48 -0800699 Header* header =
700 reinterpret_cast<Header*>(g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800701 if (header == nullptr) {
702 return nullptr;
703 }
704 memset(header, 0, g_dispatch->malloc_usable_size(header));
Christopher Ferris7bd01782016-04-20 12:30:58 -0700705 pointer = InitHeader(header, header, size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800706 } else {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700707 pointer = g_dispatch->calloc(1, real_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800708 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800709
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700710 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700711 g_debug->record->AddEntry(new CallocEntry(pointer, bytes, nmemb));
712 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800713
714 if (pointer != nullptr && g_debug->TrackPointers()) {
715 PointerData::Add(pointer, size);
716 }
Christopher Ferris7bd01782016-04-20 12:30:58 -0700717 return pointer;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800718}
719
720struct mallinfo debug_mallinfo() {
721 return g_dispatch->mallinfo();
722}
723
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700724int debug_mallopt(int param, int value) {
725 return g_dispatch->mallopt(param, value);
726}
727
Christopher Ferriscae21a92018-02-05 18:14:55 -0800728void* debug_aligned_alloc(size_t alignment, size_t size) {
729 if (DebugCallsDisabled()) {
730 return g_dispatch->aligned_alloc(alignment, size);
731 }
Christopher Ferrisa22f5d52019-03-01 16:40:59 -0800732 if (!powerof2(alignment) || (size % alignment) != 0) {
Christopher Ferriscae21a92018-02-05 18:14:55 -0800733 errno = EINVAL;
734 return nullptr;
735 }
736 return debug_memalign(alignment, size);
737}
738
Christopher Ferris63860cb2015-11-16 17:30:32 -0800739int debug_posix_memalign(void** memptr, size_t alignment, size_t size) {
740 if (DebugCallsDisabled()) {
741 return g_dispatch->posix_memalign(memptr, alignment, size);
742 }
743
744 if (!powerof2(alignment)) {
745 return EINVAL;
746 }
747 int saved_errno = errno;
748 *memptr = debug_memalign(alignment, size);
749 errno = saved_errno;
750 return (*memptr != nullptr) ? 0 : ENOMEM;
751}
752
Christopher Ferris4da25032018-03-07 13:38:48 -0800753int debug_iterate(uintptr_t base, size_t size, void (*callback)(uintptr_t, size_t, void*),
754 void* arg) {
755 if (g_debug->TrackPointers()) {
756 // Since malloc is disabled, don't bother acquiring any locks.
757 for (auto it = PointerData::begin(); it != PointerData::end(); ++it) {
758 callback(it->first, InternalMallocUsableSize(reinterpret_cast<void*>(it->first)), arg);
759 }
760 return 0;
761 }
Colin Cross869691c2016-01-29 12:48:18 -0800762
Christopher Ferris4da25032018-03-07 13:38:48 -0800763 // An option that adds a header will add pointer tracking, so no need to
764 // check if headers are enabled.
765 return g_dispatch->iterate(base, size, callback, arg);
Colin Cross869691c2016-01-29 12:48:18 -0800766}
767
768void debug_malloc_disable() {
769 g_dispatch->malloc_disable();
Christopher Ferris4da25032018-03-07 13:38:48 -0800770 if (g_debug->pointer) {
771 g_debug->pointer->PrepareFork();
Colin Cross869691c2016-01-29 12:48:18 -0800772 }
773}
774
775void debug_malloc_enable() {
Christopher Ferris4da25032018-03-07 13:38:48 -0800776 if (g_debug->pointer) {
777 g_debug->pointer->PostForkParent();
Colin Cross869691c2016-01-29 12:48:18 -0800778 }
779 g_dispatch->malloc_enable();
780}
781
Christopher Ferris4da25032018-03-07 13:38:48 -0800782ssize_t debug_malloc_backtrace(void* pointer, uintptr_t* frames, size_t max_frames) {
Colin Cross2d4721c2016-02-02 11:57:54 -0800783 if (DebugCallsDisabled() || pointer == nullptr) {
784 return 0;
785 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700786 ScopedDisableDebugCalls disable;
Colin Cross2d4721c2016-02-02 11:57:54 -0800787
Christopher Ferris4da25032018-03-07 13:38:48 -0800788 if (!(g_debug->config().options() & BACKTRACE)) {
789 return 0;
Colin Cross2d4721c2016-02-02 11:57:54 -0800790 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800791 return PointerData::GetFrames(pointer, frames, max_frames);
Colin Cross2d4721c2016-02-02 11:57:54 -0800792}
793
Christopher Ferris63860cb2015-11-16 17:30:32 -0800794#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
795void* debug_pvalloc(size_t bytes) {
796 if (DebugCallsDisabled()) {
797 return g_dispatch->pvalloc(bytes);
798 }
799
800 size_t pagesize = getpagesize();
Dan Alberta613d0d2017-10-05 16:39:33 -0700801 size_t size = __BIONIC_ALIGN(bytes, pagesize);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800802 if (size < bytes) {
803 // Overflow
804 errno = ENOMEM;
805 return nullptr;
806 }
807 return debug_memalign(pagesize, size);
808}
809
810void* debug_valloc(size_t size) {
811 if (DebugCallsDisabled()) {
812 return g_dispatch->valloc(size);
813 }
814 return debug_memalign(getpagesize(), size);
815}
816#endif
Christopher Ferris602b88c2017-08-04 13:04:04 -0700817
818static std::mutex g_dump_lock;
819
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700820static void write_dump(FILE* fp) {
821 fprintf(fp, "Android Native Heap Dump v1.2\n\n");
Christopher Ferris602b88c2017-08-04 13:04:04 -0700822
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700823 std::string fingerprint = android::base::GetProperty("ro.build.fingerprint", "unknown");
824 fprintf(fp, "Build fingerprint: '%s'\n\n", fingerprint.c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700825
Christopher Ferris4da25032018-03-07 13:38:48 -0800826 PointerData::DumpLiveToFile(fp);
Christopher Ferris602b88c2017-08-04 13:04:04 -0700827
828 fprintf(fp, "MAPS\n");
829 std::string content;
830 if (!android::base::ReadFileToString("/proc/self/maps", &content)) {
831 fprintf(fp, "Could not open /proc/self/maps\n");
832 } else {
833 fprintf(fp, "%s", content.c_str());
834 }
835 fprintf(fp, "END\n");
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700836}
837
838bool debug_write_malloc_leak_info(FILE* fp) {
839 ScopedDisableDebugCalls disable;
840
841 std::lock_guard<std::mutex> guard(g_dump_lock);
842
843 if (!(g_debug->config().options() & BACKTRACE)) {
844 return false;
845 }
846
847 write_dump(fp);
Christopher Ferris602b88c2017-08-04 13:04:04 -0700848 return true;
849}
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700850
851void debug_dump_heap(const char* file_name) {
852 ScopedDisableDebugCalls disable;
853
854 std::lock_guard<std::mutex> guard(g_dump_lock);
855
856 FILE* fp = fopen(file_name, "w+e");
857 if (fp == nullptr) {
858 error_log("Unable to create file: %s", file_name);
859 return;
860 }
861
862 error_log("Dumping to file: %s\n", file_name);
863 write_dump(fp);
864 fclose(fp);
865}