blob: 1e7086c7d3f3a7d89a8e0aa2cf8f808d617b803b [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);
157}
158
Christopher Ferris4da25032018-03-07 13:38:48 -0800159static bool VerifyPointer(const void* pointer, const char* function_name) {
160 if (g_debug->HeaderEnabled()) {
161 Header* header = g_debug->GetHeader(pointer);
162 if (header->tag != DEBUG_TAG) {
163 std::string error_str;
164 if (header->tag == DEBUG_FREE_TAG) {
165 error_str = std::string("USED AFTER FREE (") + function_name + ")";
166 } else {
167 error_str = android::base::StringPrintf("HAS INVALID TAG %" PRIx32 " (%s)", header->tag,
168 function_name);
169 }
170 LogError(pointer, error_str.c_str());
171 return false;
172 }
173 }
174
175 if (g_debug->TrackPointers()) {
176 if (!PointerData::Exists(pointer)) {
177 std::string error_str(std::string("UNKNOWN POINTER (") + function_name + ")");
178 LogError(pointer, error_str.c_str());
179 return false;
180 }
181 }
182 return true;
183}
184
185static size_t InternalMallocUsableSize(void* pointer) {
186 if (g_debug->HeaderEnabled()) {
187 return g_debug->GetHeader(pointer)->usable_size;
188 } else {
189 return g_dispatch->malloc_usable_size(pointer);
190 }
191}
192
Christopher Ferris63860cb2015-11-16 17:30:32 -0800193static void* InitHeader(Header* header, void* orig_pointer, size_t size) {
194 header->tag = DEBUG_TAG;
195 header->orig_pointer = orig_pointer;
196 header->size = size;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800197 header->usable_size = g_dispatch->malloc_usable_size(orig_pointer);
198 if (header->usable_size == 0) {
199 g_dispatch->free(orig_pointer);
200 return nullptr;
201 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800202 header->usable_size -= g_debug->pointer_offset() + reinterpret_cast<uintptr_t>(header) -
203 reinterpret_cast<uintptr_t>(orig_pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800204
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700205 if (g_debug->config().options() & FRONT_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800206 uint8_t* guard = g_debug->GetFrontGuard(header);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700207 memset(guard, g_debug->config().front_guard_value(), g_debug->config().front_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800208 }
209
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700210 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800211 uint8_t* guard = g_debug->GetRearGuard(header);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700212 memset(guard, g_debug->config().rear_guard_value(), g_debug->config().rear_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800213 // If the rear guard is enabled, set the usable size to the exact size
214 // of the allocation.
Christopher Ferris4da25032018-03-07 13:38:48 -0800215 header->usable_size = header->size;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800216 }
217
218 return g_debug->GetPointer(header);
219}
220
Tamas Berghammerac81fe82016-08-26 15:54:59 +0100221bool debug_initialize(const MallocDispatch* malloc_dispatch, int* malloc_zygote_child,
Christopher Ferris4da25032018-03-07 13:38:48 -0800222 const char* options) {
Tamas Berghammerac81fe82016-08-26 15:54:59 +0100223 if (malloc_zygote_child == nullptr || options == nullptr) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800224 return false;
225 }
Colin Cross7a28a3c2016-02-07 22:51:15 -0800226
227 InitAtfork();
228
Christopher Ferris63860cb2015-11-16 17:30:32 -0800229 g_malloc_zygote_child = malloc_zygote_child;
230
231 g_dispatch = malloc_dispatch;
232
233 if (!DebugDisableInitialize()) {
234 return false;
235 }
236
237 DebugData* debug = new DebugData();
Tamas Berghammerac81fe82016-08-26 15:54:59 +0100238 if (!debug->Initialize(options)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800239 delete debug;
240 DebugDisableFinalize();
241 return false;
242 }
243 g_debug = debug;
244
245 // Always enable the backtrace code since we will use it in a number
246 // of different error cases.
247 backtrace_startup();
248
249 return true;
250}
251
252void debug_finalize() {
253 if (g_debug == nullptr) {
254 return;
255 }
256
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700257 if (g_debug->config().options() & FREE_TRACK) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800258 PointerData::VerifyAllFreed();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800259 }
260
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700261 if (g_debug->config().options() & LEAK_TRACK) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800262 PointerData::LogLeaks();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800263 }
264
Christopher Ferris602b88c2017-08-04 13:04:04 -0700265 if ((g_debug->config().options() & BACKTRACE) && g_debug->config().backtrace_dump_on_exit()) {
266 ScopedDisableDebugCalls disable;
Christopher Ferris4da25032018-03-07 13:38:48 -0800267 debug_dump_heap(android::base::StringPrintf("%s.%d.exit.txt",
268 g_debug->config().backtrace_dump_prefix().c_str(),
269 getpid())
270 .c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700271 }
272
Christopher Ferris63860cb2015-11-16 17:30:32 -0800273 DebugDisableSet(true);
274
Colin Cross2c759912016-02-05 16:17:39 -0800275 backtrace_shutdown();
276
Christopher Ferris63860cb2015-11-16 17:30:32 -0800277 delete g_debug;
278 g_debug = nullptr;
279
280 DebugDisableFinalize();
281}
282
Christopher Ferris4da25032018-03-07 13:38:48 -0800283void debug_get_malloc_leak_info(uint8_t** info, size_t* overall_size, size_t* info_size,
284 size_t* total_memory, size_t* backtrace_size) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800285 ScopedDisableDebugCalls disable;
286
287 // Verify the arguments.
Christopher Ferris4da25032018-03-07 13:38:48 -0800288 if (info == nullptr || overall_size == nullptr || info_size == NULL || total_memory == nullptr ||
289 backtrace_size == nullptr) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800290 error_log("get_malloc_leak_info: At least one invalid parameter.");
291 return;
292 }
293
294 *info = nullptr;
295 *overall_size = 0;
296 *info_size = 0;
297 *total_memory = 0;
298 *backtrace_size = 0;
299
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700300 if (!(g_debug->config().options() & BACKTRACE)) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800301 error_log(
302 "get_malloc_leak_info: Allocations not being tracked, to enable "
303 "set the option 'backtrace'.");
Christopher Ferris63860cb2015-11-16 17:30:32 -0800304 return;
305 }
306
Christopher Ferris4da25032018-03-07 13:38:48 -0800307 PointerData::GetInfo(info, overall_size, info_size, total_memory, backtrace_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800308}
309
310void debug_free_malloc_leak_info(uint8_t* info) {
311 g_dispatch->free(info);
312}
313
Christopher Ferris55a89a42016-04-07 17:14:53 -0700314size_t debug_malloc_usable_size(void* pointer) {
315 if (DebugCallsDisabled() || pointer == nullptr) {
316 return g_dispatch->malloc_usable_size(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800317 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700318 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800319
Christopher Ferris4da25032018-03-07 13:38:48 -0800320 if (!VerifyPointer(pointer, "malloc_usable_size")) {
321 return 0;
322 }
323
324 return InternalMallocUsableSize(pointer);
Christopher Ferris55a89a42016-04-07 17:14:53 -0700325}
326
Christopher Ferris4da25032018-03-07 13:38:48 -0800327static void* InternalMalloc(size_t size) {
328 if ((g_debug->config().options() & BACKTRACE) && g_debug->pointer->ShouldDumpAndReset()) {
329 debug_dump_heap(android::base::StringPrintf(
330 "%s.%d.txt", g_debug->config().backtrace_dump_prefix().c_str(), getpid())
331 .c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700332 }
333
Colin Cross9567c7b2016-03-09 17:56:14 -0800334 if (size == 0) {
335 size = 1;
336 }
337
Christopher Ferris63860cb2015-11-16 17:30:32 -0800338 size_t real_size = size + g_debug->extra_bytes();
339 if (real_size < size) {
340 // Overflow.
341 errno = ENOMEM;
342 return nullptr;
343 }
344
Christopher Ferris4da25032018-03-07 13:38:48 -0800345 if (size > PointerInfoType::MaxSize()) {
346 errno = ENOMEM;
347 return nullptr;
348 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800349
Christopher Ferris4da25032018-03-07 13:38:48 -0800350 void* pointer;
351 if (g_debug->HeaderEnabled()) {
352 Header* header =
353 reinterpret_cast<Header*>(g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800354 if (header == nullptr) {
355 return nullptr;
356 }
357 pointer = InitHeader(header, header, size);
358 } else {
359 pointer = g_dispatch->malloc(real_size);
360 }
361
Christopher Ferris4da25032018-03-07 13:38:48 -0800362 if (pointer != nullptr) {
363 if (g_debug->TrackPointers()) {
364 PointerData::Add(pointer, size);
365 }
366
367 if (g_debug->config().options() & FILL_ON_ALLOC) {
368 size_t bytes = InternalMallocUsableSize(pointer);
369 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes();
370 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
371 memset(pointer, g_debug->config().fill_alloc_value(), bytes);
372 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800373 }
374 return pointer;
375}
376
Christopher Ferris55a89a42016-04-07 17:14:53 -0700377void* debug_malloc(size_t size) {
378 if (DebugCallsDisabled()) {
379 return g_dispatch->malloc(size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800380 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700381 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800382
Christopher Ferris4da25032018-03-07 13:38:48 -0800383 void* pointer = InternalMalloc(size);
Christopher Ferris7bd01782016-04-20 12:30:58 -0700384
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700385 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700386 g_debug->record->AddEntry(new MallocEntry(pointer, size));
387 }
388
389 return pointer;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700390}
391
Christopher Ferris4da25032018-03-07 13:38:48 -0800392static void InternalFree(void* pointer) {
393 if ((g_debug->config().options() & BACKTRACE) && g_debug->pointer->ShouldDumpAndReset()) {
394 debug_dump_heap(android::base::StringPrintf(
395 "%s.%d.txt", g_debug->config().backtrace_dump_prefix().c_str(), getpid())
396 .c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700397 }
398
Christopher Ferris63860cb2015-11-16 17:30:32 -0800399 void* free_pointer = pointer;
400 size_t bytes;
Christopher Ferrisd0919622016-03-15 22:39:39 -0700401 Header* header;
Christopher Ferris4da25032018-03-07 13:38:48 -0800402 if (g_debug->HeaderEnabled()) {
Christopher Ferrisd0919622016-03-15 22:39:39 -0700403 header = g_debug->GetHeader(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800404 free_pointer = header->orig_pointer;
405
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700406 if (g_debug->config().options() & FRONT_GUARD) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700407 if (!g_debug->front_guard->Valid(header)) {
408 g_debug->front_guard->LogFailure(header);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800409 }
410 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700411 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700412 if (!g_debug->rear_guard->Valid(header)) {
413 g_debug->rear_guard->LogFailure(header);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800414 }
415 }
416
Christopher Ferris7993b802016-01-28 18:35:05 -0800417 header->tag = DEBUG_FREE_TAG;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800418
419 bytes = header->usable_size;
420 } else {
421 bytes = g_dispatch->malloc_usable_size(pointer);
422 }
423
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700424 if (g_debug->config().options() & FILL_ON_FREE) {
425 size_t fill_bytes = g_debug->config().fill_on_free_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800426 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700427 memset(pointer, g_debug->config().fill_free_value(), bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800428 }
429
Christopher Ferris4da25032018-03-07 13:38:48 -0800430 if (g_debug->TrackPointers()) {
431 PointerData::Remove(pointer);
432 }
433
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700434 if (g_debug->config().options() & FREE_TRACK) {
Christopher Ferrisd0919622016-03-15 22:39:39 -0700435 // Do not add the allocation until we are done modifying the pointer
436 // itself. This avoids a race if a lot of threads are all doing
437 // frees at the same time and we wind up trying to really free this
438 // pointer from another thread, while still trying to free it in
439 // this function.
Christopher Ferris4da25032018-03-07 13:38:48 -0800440 pointer = PointerData::AddFreed(pointer);
441 if (pointer != nullptr) {
442 if (g_debug->HeaderEnabled()) {
443 pointer = g_debug->GetHeader(pointer)->orig_pointer;
444 }
445 g_dispatch->free(pointer);
446 }
Christopher Ferrisd0919622016-03-15 22:39:39 -0700447 } else {
448 g_dispatch->free(free_pointer);
449 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800450}
451
Christopher Ferris55a89a42016-04-07 17:14:53 -0700452void debug_free(void* pointer) {
453 if (DebugCallsDisabled() || pointer == nullptr) {
454 return g_dispatch->free(pointer);
455 }
456 ScopedDisableDebugCalls disable;
457
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700458 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700459 g_debug->record->AddEntry(new FreeEntry(pointer));
460 }
461
Christopher Ferris4da25032018-03-07 13:38:48 -0800462 if (!VerifyPointer(pointer, "free")) {
463 return;
464 }
465
466 InternalFree(pointer);
Christopher Ferris55a89a42016-04-07 17:14:53 -0700467}
468
Christopher Ferris63860cb2015-11-16 17:30:32 -0800469void* debug_memalign(size_t alignment, size_t bytes) {
470 if (DebugCallsDisabled()) {
471 return g_dispatch->memalign(alignment, bytes);
472 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700473 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800474
Colin Cross9567c7b2016-03-09 17:56:14 -0800475 if (bytes == 0) {
476 bytes = 1;
477 }
478
Christopher Ferris4da25032018-03-07 13:38:48 -0800479 if (bytes > PointerInfoType::MaxSize()) {
480 errno = ENOMEM;
481 return nullptr;
482 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800483
Christopher Ferris4da25032018-03-07 13:38:48 -0800484 void* pointer;
485 if (g_debug->HeaderEnabled()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800486 // Make the alignment a power of two.
487 if (!powerof2(alignment)) {
488 alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment);
489 }
Christopher Ferris72df6702016-02-11 15:51:31 -0800490 // Force the alignment to at least MINIMUM_ALIGNMENT_BYTES to guarantee
Christopher Ferris63860cb2015-11-16 17:30:32 -0800491 // that the header is aligned properly.
Christopher Ferris72df6702016-02-11 15:51:31 -0800492 if (alignment < MINIMUM_ALIGNMENT_BYTES) {
493 alignment = MINIMUM_ALIGNMENT_BYTES;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800494 }
495
496 // We don't have any idea what the natural alignment of
497 // the underlying native allocator is, so we always need to
498 // over allocate.
499 size_t real_size = alignment + bytes + g_debug->extra_bytes();
500 if (real_size < bytes) {
501 // Overflow.
502 errno = ENOMEM;
503 return nullptr;
504 }
505
506 pointer = g_dispatch->malloc(real_size);
507 if (pointer == nullptr) {
508 return nullptr;
509 }
510
511 uintptr_t value = reinterpret_cast<uintptr_t>(pointer) + g_debug->pointer_offset();
512 // Now align the pointer.
513 value += (-value % alignment);
514
515 Header* header = g_debug->GetHeader(reinterpret_cast<void*>(value));
516 pointer = InitHeader(header, pointer, bytes);
517 } else {
518 size_t real_size = bytes + g_debug->extra_bytes();
519 if (real_size < bytes) {
520 // Overflow.
521 errno = ENOMEM;
522 return nullptr;
523 }
524 pointer = g_dispatch->memalign(alignment, real_size);
525 }
526
Christopher Ferris4da25032018-03-07 13:38:48 -0800527 if (pointer != nullptr) {
528 if (g_debug->TrackPointers()) {
529 PointerData::Add(pointer, bytes);
530 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700531
Christopher Ferris4da25032018-03-07 13:38:48 -0800532 if (g_debug->config().options() & FILL_ON_ALLOC) {
533 size_t bytes = InternalMallocUsableSize(pointer);
534 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes();
535 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
536 memset(pointer, g_debug->config().fill_alloc_value(), bytes);
537 }
538
539 if (g_debug->config().options() & RECORD_ALLOCS) {
540 g_debug->record->AddEntry(new MemalignEntry(pointer, bytes, alignment));
541 }
Christopher Ferris7bd01782016-04-20 12:30:58 -0700542 }
543
Christopher Ferris63860cb2015-11-16 17:30:32 -0800544 return pointer;
545}
546
547void* debug_realloc(void* pointer, size_t bytes) {
548 if (DebugCallsDisabled()) {
549 return g_dispatch->realloc(pointer, bytes);
550 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700551 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800552
553 if (pointer == nullptr) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800554 pointer = InternalMalloc(bytes);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700555 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700556 g_debug->record->AddEntry(new ReallocEntry(pointer, bytes, nullptr));
557 }
558 return pointer;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800559 }
560
Christopher Ferris4da25032018-03-07 13:38:48 -0800561 if (!VerifyPointer(pointer, "realloc")) {
562 return nullptr;
563 }
564
Christopher Ferris63860cb2015-11-16 17:30:32 -0800565 if (bytes == 0) {
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700566 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700567 g_debug->record->AddEntry(new ReallocEntry(nullptr, bytes, pointer));
568 }
569
Christopher Ferris4da25032018-03-07 13:38:48 -0800570 InternalFree(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800571 return nullptr;
572 }
573
574 size_t real_size = bytes;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700575 if (g_debug->config().options() & EXPAND_ALLOC) {
576 real_size += g_debug->config().expand_alloc_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800577 if (real_size < bytes) {
578 // Overflow.
579 errno = ENOMEM;
580 return nullptr;
581 }
582 }
583
Christopher Ferris4da25032018-03-07 13:38:48 -0800584 if (bytes > PointerInfoType::MaxSize()) {
585 errno = ENOMEM;
586 return nullptr;
587 }
588
Christopher Ferris63860cb2015-11-16 17:30:32 -0800589 void* new_pointer;
590 size_t prev_size;
Christopher Ferris4da25032018-03-07 13:38:48 -0800591 if (g_debug->HeaderEnabled()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800592 // Same size, do nothing.
Christopher Ferris4da25032018-03-07 13:38:48 -0800593 Header* header = g_debug->GetHeader(pointer);
594 if (real_size == header->size) {
595 if (g_debug->TrackPointers()) {
596 // Remove and re-add so that the backtrace is updated.
597 PointerData::Remove(pointer);
598 PointerData::Add(pointer, real_size);
599 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800600 return pointer;
601 }
602
603 // Allocation is shrinking.
604 if (real_size < header->usable_size) {
605 header->size = real_size;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700606 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800607 // Don't bother allocating a smaller pointer in this case, simply
608 // change the header usable_size and reset the rear guard.
Christopher Ferris4da25032018-03-07 13:38:48 -0800609 header->usable_size = header->size;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700610 memset(g_debug->GetRearGuard(header), g_debug->config().rear_guard_value(),
611 g_debug->config().rear_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800612 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800613 if (g_debug->TrackPointers()) {
614 // Remove and re-add so that the backtrace is updated.
615 PointerData::Remove(pointer);
616 PointerData::Add(pointer, real_size);
617 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800618 return pointer;
619 }
620
621 // Allocate the new size.
Christopher Ferris4da25032018-03-07 13:38:48 -0800622 new_pointer = InternalMalloc(bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800623 if (new_pointer == nullptr) {
624 errno = ENOMEM;
625 return nullptr;
626 }
627
628 prev_size = header->usable_size;
629 memcpy(new_pointer, pointer, prev_size);
Christopher Ferris4da25032018-03-07 13:38:48 -0800630 InternalFree(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800631 } else {
Christopher Ferris4da25032018-03-07 13:38:48 -0800632 if (g_debug->TrackPointers()) {
633 PointerData::Remove(pointer);
634 }
635
Christopher Ferris63860cb2015-11-16 17:30:32 -0800636 prev_size = g_dispatch->malloc_usable_size(pointer);
637 new_pointer = g_dispatch->realloc(pointer, real_size);
638 if (new_pointer == nullptr) {
639 return nullptr;
640 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800641
642 if (g_debug->TrackPointers()) {
643 PointerData::Add(new_pointer, real_size);
644 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800645 }
646
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700647 if (g_debug->config().options() & FILL_ON_ALLOC) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800648 size_t bytes = InternalMallocUsableSize(new_pointer);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700649 if (bytes > g_debug->config().fill_on_alloc_bytes()) {
650 bytes = g_debug->config().fill_on_alloc_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800651 }
652 if (bytes > prev_size) {
653 memset(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(new_pointer) + prev_size),
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700654 g_debug->config().fill_alloc_value(), bytes - prev_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800655 }
656 }
657
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700658 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700659 g_debug->record->AddEntry(new ReallocEntry(new_pointer, bytes, pointer));
660 }
661
Christopher Ferris63860cb2015-11-16 17:30:32 -0800662 return new_pointer;
663}
664
665void* debug_calloc(size_t nmemb, size_t bytes) {
666 if (DebugCallsDisabled()) {
667 return g_dispatch->calloc(nmemb, bytes);
668 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700669 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800670
Colin Cross7877df62016-03-10 13:01:27 -0800671 size_t size;
672 if (__builtin_mul_overflow(nmemb, bytes, &size)) {
673 // Overflow
674 errno = ENOMEM;
675 return nullptr;
676 }
677
Colin Cross9567c7b2016-03-09 17:56:14 -0800678 if (size == 0) {
679 size = 1;
680 }
681
Colin Cross7877df62016-03-10 13:01:27 -0800682 size_t real_size;
683 if (__builtin_add_overflow(size, g_debug->extra_bytes(), &real_size)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800684 // Overflow.
685 errno = ENOMEM;
686 return nullptr;
687 }
688
Christopher Ferris4da25032018-03-07 13:38:48 -0800689 if (real_size > PointerInfoType::MaxSize()) {
690 errno = ENOMEM;
691 return nullptr;
692 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800693
Christopher Ferris4da25032018-03-07 13:38:48 -0800694 void* pointer;
695 if (g_debug->HeaderEnabled()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800696 // Need to guarantee the alignment of the header.
Christopher Ferris4da25032018-03-07 13:38:48 -0800697 Header* header =
698 reinterpret_cast<Header*>(g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800699 if (header == nullptr) {
700 return nullptr;
701 }
702 memset(header, 0, g_dispatch->malloc_usable_size(header));
Christopher Ferris7bd01782016-04-20 12:30:58 -0700703 pointer = InitHeader(header, header, size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800704 } else {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700705 pointer = g_dispatch->calloc(1, real_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800706 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800707
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 CallocEntry(pointer, bytes, nmemb));
710 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800711
712 if (pointer != nullptr && g_debug->TrackPointers()) {
713 PointerData::Add(pointer, size);
714 }
Christopher Ferris7bd01782016-04-20 12:30:58 -0700715 return pointer;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800716}
717
718struct mallinfo debug_mallinfo() {
719 return g_dispatch->mallinfo();
720}
721
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700722int debug_mallopt(int param, int value) {
723 return g_dispatch->mallopt(param, value);
724}
725
Christopher Ferriscae21a92018-02-05 18:14:55 -0800726void* debug_aligned_alloc(size_t alignment, size_t size) {
727 if (DebugCallsDisabled()) {
728 return g_dispatch->aligned_alloc(alignment, size);
729 }
730 if (!powerof2(alignment)) {
731 errno = EINVAL;
732 return nullptr;
733 }
734 return debug_memalign(alignment, size);
735}
736
Christopher Ferris63860cb2015-11-16 17:30:32 -0800737int debug_posix_memalign(void** memptr, size_t alignment, size_t size) {
738 if (DebugCallsDisabled()) {
739 return g_dispatch->posix_memalign(memptr, alignment, size);
740 }
741
742 if (!powerof2(alignment)) {
743 return EINVAL;
744 }
745 int saved_errno = errno;
746 *memptr = debug_memalign(alignment, size);
747 errno = saved_errno;
748 return (*memptr != nullptr) ? 0 : ENOMEM;
749}
750
Christopher Ferris4da25032018-03-07 13:38:48 -0800751int debug_iterate(uintptr_t base, size_t size, void (*callback)(uintptr_t, size_t, void*),
752 void* arg) {
753 if (g_debug->TrackPointers()) {
754 // Since malloc is disabled, don't bother acquiring any locks.
755 for (auto it = PointerData::begin(); it != PointerData::end(); ++it) {
756 callback(it->first, InternalMallocUsableSize(reinterpret_cast<void*>(it->first)), arg);
757 }
758 return 0;
759 }
Colin Cross869691c2016-01-29 12:48:18 -0800760
Christopher Ferris4da25032018-03-07 13:38:48 -0800761 // An option that adds a header will add pointer tracking, so no need to
762 // check if headers are enabled.
763 return g_dispatch->iterate(base, size, callback, arg);
Colin Cross869691c2016-01-29 12:48:18 -0800764}
765
766void debug_malloc_disable() {
767 g_dispatch->malloc_disable();
Christopher Ferris4da25032018-03-07 13:38:48 -0800768 if (g_debug->pointer) {
769 g_debug->pointer->PrepareFork();
Colin Cross869691c2016-01-29 12:48:18 -0800770 }
771}
772
773void debug_malloc_enable() {
Christopher Ferris4da25032018-03-07 13:38:48 -0800774 if (g_debug->pointer) {
775 g_debug->pointer->PostForkParent();
Colin Cross869691c2016-01-29 12:48:18 -0800776 }
777 g_dispatch->malloc_enable();
778}
779
Christopher Ferris4da25032018-03-07 13:38:48 -0800780ssize_t debug_malloc_backtrace(void* pointer, uintptr_t* frames, size_t max_frames) {
Colin Cross2d4721c2016-02-02 11:57:54 -0800781 if (DebugCallsDisabled() || pointer == nullptr) {
782 return 0;
783 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700784 ScopedDisableDebugCalls disable;
Colin Cross2d4721c2016-02-02 11:57:54 -0800785
Christopher Ferris4da25032018-03-07 13:38:48 -0800786 if (!(g_debug->config().options() & BACKTRACE)) {
787 return 0;
Colin Cross2d4721c2016-02-02 11:57:54 -0800788 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800789 return PointerData::GetFrames(pointer, frames, max_frames);
Colin Cross2d4721c2016-02-02 11:57:54 -0800790}
791
Christopher Ferris63860cb2015-11-16 17:30:32 -0800792#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
793void* debug_pvalloc(size_t bytes) {
794 if (DebugCallsDisabled()) {
795 return g_dispatch->pvalloc(bytes);
796 }
797
798 size_t pagesize = getpagesize();
Dan Alberta613d0d2017-10-05 16:39:33 -0700799 size_t size = __BIONIC_ALIGN(bytes, pagesize);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800800 if (size < bytes) {
801 // Overflow
802 errno = ENOMEM;
803 return nullptr;
804 }
805 return debug_memalign(pagesize, size);
806}
807
808void* debug_valloc(size_t size) {
809 if (DebugCallsDisabled()) {
810 return g_dispatch->valloc(size);
811 }
812 return debug_memalign(getpagesize(), size);
813}
814#endif
Christopher Ferris602b88c2017-08-04 13:04:04 -0700815
816static std::mutex g_dump_lock;
817
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700818static void write_dump(FILE* fp) {
819 fprintf(fp, "Android Native Heap Dump v1.2\n\n");
Christopher Ferris602b88c2017-08-04 13:04:04 -0700820
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700821 std::string fingerprint = android::base::GetProperty("ro.build.fingerprint", "unknown");
822 fprintf(fp, "Build fingerprint: '%s'\n\n", fingerprint.c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700823
Christopher Ferris4da25032018-03-07 13:38:48 -0800824 PointerData::DumpLiveToFile(fp);
Christopher Ferris602b88c2017-08-04 13:04:04 -0700825
826 fprintf(fp, "MAPS\n");
827 std::string content;
828 if (!android::base::ReadFileToString("/proc/self/maps", &content)) {
829 fprintf(fp, "Could not open /proc/self/maps\n");
830 } else {
831 fprintf(fp, "%s", content.c_str());
832 }
833 fprintf(fp, "END\n");
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700834}
835
836bool debug_write_malloc_leak_info(FILE* fp) {
837 ScopedDisableDebugCalls disable;
838
839 std::lock_guard<std::mutex> guard(g_dump_lock);
840
841 if (!(g_debug->config().options() & BACKTRACE)) {
842 return false;
843 }
844
845 write_dump(fp);
Christopher Ferris602b88c2017-08-04 13:04:04 -0700846 return true;
847}
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700848
849void debug_dump_heap(const char* file_name) {
850 ScopedDisableDebugCalls disable;
851
852 std::lock_guard<std::mutex> guard(g_dump_lock);
853
854 FILE* fp = fopen(file_name, "w+e");
855 if (fp == nullptr) {
856 error_log("Unable to create file: %s", file_name);
857 return;
858 }
859
860 error_log("Dumping to file: %s\n", file_name);
861 write_dump(fp);
862 fclose(fp);
863}