blob: 836c33b86af18e003fce83fdfcb817282c557312 [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>
41#include <android-base/stringprintf.h>
Christopher Ferris63860cb2015-11-16 17:30:32 -080042#include <private/bionic_malloc_dispatch.h>
43
Christopher Ferris72df6702016-02-11 15:51:31 -080044#include "Config.h"
Christopher Ferris63860cb2015-11-16 17:30:32 -080045#include "DebugData.h"
Christopher Ferris4da25032018-03-07 13:38:48 -080046#include "backtrace.h"
Christopher Ferris63860cb2015-11-16 17:30:32 -080047#include "debug_disable.h"
48#include "debug_log.h"
49#include "malloc_debug.h"
Christopher Ferris93bdd6a2018-04-05 11:12:38 -070050#include "UnwindBacktrace.h"
Christopher Ferris63860cb2015-11-16 17:30:32 -080051
52// ------------------------------------------------------------------------
53// Global Data
54// ------------------------------------------------------------------------
55DebugData* g_debug;
56
57int* g_malloc_zygote_child;
58
59const MallocDispatch* g_dispatch;
60// ------------------------------------------------------------------------
61
62// ------------------------------------------------------------------------
63// Use C style prototypes for all exported functions. This makes it easy
64// to do dlsym lookups during libc initialization when malloc debug
65// is enabled.
66// ------------------------------------------------------------------------
67__BEGIN_DECLS
68
Tamas Berghammerac81fe82016-08-26 15:54:59 +010069bool debug_initialize(const MallocDispatch* malloc_dispatch, int* malloc_zygote_child,
Christopher Ferris4da25032018-03-07 13:38:48 -080070 const char* options);
Christopher Ferris63860cb2015-11-16 17:30:32 -080071void debug_finalize();
Christopher Ferris602b88c2017-08-04 13:04:04 -070072bool debug_dump_heap(const char* file_name);
Christopher Ferris4da25032018-03-07 13:38:48 -080073void debug_get_malloc_leak_info(uint8_t** info, size_t* overall_size, size_t* info_size,
74 size_t* total_memory, size_t* backtrace_size);
Colin Cross2d4721c2016-02-02 11:57:54 -080075ssize_t debug_malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count);
Christopher Ferris63860cb2015-11-16 17:30:32 -080076void debug_free_malloc_leak_info(uint8_t* info);
77size_t debug_malloc_usable_size(void* pointer);
78void* debug_malloc(size_t size);
79void debug_free(void* pointer);
Christopher Ferriscae21a92018-02-05 18:14:55 -080080void* debug_aligned_alloc(size_t alignment, size_t size);
Christopher Ferris63860cb2015-11-16 17:30:32 -080081void* debug_memalign(size_t alignment, size_t bytes);
82void* debug_realloc(void* pointer, size_t bytes);
83void* debug_calloc(size_t nmemb, size_t bytes);
84struct mallinfo debug_mallinfo();
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -070085int debug_mallopt(int param, int value);
Christopher Ferris63860cb2015-11-16 17:30:32 -080086int debug_posix_memalign(void** memptr, size_t alignment, size_t size);
Colin Cross869691c2016-01-29 12:48:18 -080087int debug_iterate(uintptr_t base, size_t size,
Christopher Ferris4da25032018-03-07 13:38:48 -080088 void (*callback)(uintptr_t base, size_t size, void* arg), void* arg);
Colin Cross869691c2016-01-29 12:48:18 -080089void debug_malloc_disable();
90void debug_malloc_enable();
Christopher Ferris63860cb2015-11-16 17:30:32 -080091
92#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
93void* debug_pvalloc(size_t bytes);
94void* debug_valloc(size_t size);
95#endif
96
97__END_DECLS
98// ------------------------------------------------------------------------
99
Colin Cross7a28a3c2016-02-07 22:51:15 -0800100static void InitAtfork() {
101 static pthread_once_t atfork_init = PTHREAD_ONCE_INIT;
Christopher Ferris4da25032018-03-07 13:38:48 -0800102 pthread_once(&atfork_init, []() {
Colin Cross7a28a3c2016-02-07 22:51:15 -0800103 pthread_atfork(
Christopher Ferris4da25032018-03-07 13:38:48 -0800104 []() {
Colin Cross7a28a3c2016-02-07 22:51:15 -0800105 if (g_debug != nullptr) {
106 g_debug->PrepareFork();
107 }
108 },
Christopher Ferris4da25032018-03-07 13:38:48 -0800109 []() {
Colin Cross7a28a3c2016-02-07 22:51:15 -0800110 if (g_debug != nullptr) {
111 g_debug->PostForkParent();
112 }
113 },
Christopher Ferris4da25032018-03-07 13:38:48 -0800114 []() {
Colin Cross7a28a3c2016-02-07 22:51:15 -0800115 if (g_debug != nullptr) {
116 g_debug->PostForkChild();
117 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800118 });
Colin Cross7a28a3c2016-02-07 22:51:15 -0800119 });
120}
Christopher Ferrisd0919622016-03-15 22:39:39 -0700121
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700122void BacktraceAndLog() {
123 if (g_debug->config().options() & BACKTRACE_FULL) {
124 std::vector<uintptr_t> frames;
125 std::vector<unwindstack::LocalFrameData> frames_info;
126 if (!Unwind(&frames, &frames_info, 256)) {
127 error_log(" Backtrace failed to get any frames.");
128 } else {
129 UnwindLog(frames_info);
130 }
131 } else {
132 std::vector<uintptr_t> frames(256);
133 size_t num_frames = backtrace_get(frames.data(), frames.size());
134 if (num_frames == 0) {
135 error_log(" Backtrace failed to get any frames.");
136 } else {
137 backtrace_log(frames.data(), num_frames);
138 }
139 }
140}
141
Christopher Ferris4da25032018-03-07 13:38:48 -0800142static void LogError(const void* pointer, const char* error_str) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800143 error_log(LOG_DIVIDER);
Christopher Ferris4da25032018-03-07 13:38:48 -0800144 error_log("+++ ALLOCATION %p %s", pointer, error_str);
145
146 // If we are tracking already freed pointers, check to see if this is
147 // one so we can print extra information.
148 if (g_debug->config().options() & FREE_TRACK) {
149 PointerData::LogFreeBacktrace(pointer);
Christopher Ferris7993b802016-01-28 18:35:05 -0800150 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800151
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700152 error_log("Backtrace at time of failure:");
153 BacktraceAndLog();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800154 error_log(LOG_DIVIDER);
155}
156
Christopher Ferris4da25032018-03-07 13:38:48 -0800157static bool VerifyPointer(const void* pointer, const char* function_name) {
158 if (g_debug->HeaderEnabled()) {
159 Header* header = g_debug->GetHeader(pointer);
160 if (header->tag != DEBUG_TAG) {
161 std::string error_str;
162 if (header->tag == DEBUG_FREE_TAG) {
163 error_str = std::string("USED AFTER FREE (") + function_name + ")";
164 } else {
165 error_str = android::base::StringPrintf("HAS INVALID TAG %" PRIx32 " (%s)", header->tag,
166 function_name);
167 }
168 LogError(pointer, error_str.c_str());
169 return false;
170 }
171 }
172
173 if (g_debug->TrackPointers()) {
174 if (!PointerData::Exists(pointer)) {
175 std::string error_str(std::string("UNKNOWN POINTER (") + function_name + ")");
176 LogError(pointer, error_str.c_str());
177 return false;
178 }
179 }
180 return true;
181}
182
183static size_t InternalMallocUsableSize(void* pointer) {
184 if (g_debug->HeaderEnabled()) {
185 return g_debug->GetHeader(pointer)->usable_size;
186 } else {
187 return g_dispatch->malloc_usable_size(pointer);
188 }
189}
190
Christopher Ferris63860cb2015-11-16 17:30:32 -0800191static void* InitHeader(Header* header, void* orig_pointer, size_t size) {
192 header->tag = DEBUG_TAG;
193 header->orig_pointer = orig_pointer;
194 header->size = size;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800195 header->usable_size = g_dispatch->malloc_usable_size(orig_pointer);
196 if (header->usable_size == 0) {
197 g_dispatch->free(orig_pointer);
198 return nullptr;
199 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800200 header->usable_size -= g_debug->pointer_offset() + reinterpret_cast<uintptr_t>(header) -
201 reinterpret_cast<uintptr_t>(orig_pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800202
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700203 if (g_debug->config().options() & FRONT_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800204 uint8_t* guard = g_debug->GetFrontGuard(header);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700205 memset(guard, g_debug->config().front_guard_value(), g_debug->config().front_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800206 }
207
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700208 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800209 uint8_t* guard = g_debug->GetRearGuard(header);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700210 memset(guard, g_debug->config().rear_guard_value(), g_debug->config().rear_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800211 // If the rear guard is enabled, set the usable size to the exact size
212 // of the allocation.
Christopher Ferris4da25032018-03-07 13:38:48 -0800213 header->usable_size = header->size;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800214 }
215
216 return g_debug->GetPointer(header);
217}
218
Tamas Berghammerac81fe82016-08-26 15:54:59 +0100219bool debug_initialize(const MallocDispatch* malloc_dispatch, int* malloc_zygote_child,
Christopher Ferris4da25032018-03-07 13:38:48 -0800220 const char* options) {
Tamas Berghammerac81fe82016-08-26 15:54:59 +0100221 if (malloc_zygote_child == nullptr || options == nullptr) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800222 return false;
223 }
Colin Cross7a28a3c2016-02-07 22:51:15 -0800224
225 InitAtfork();
226
Christopher Ferris63860cb2015-11-16 17:30:32 -0800227 g_malloc_zygote_child = malloc_zygote_child;
228
229 g_dispatch = malloc_dispatch;
230
231 if (!DebugDisableInitialize()) {
232 return false;
233 }
234
235 DebugData* debug = new DebugData();
Tamas Berghammerac81fe82016-08-26 15:54:59 +0100236 if (!debug->Initialize(options)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800237 delete debug;
238 DebugDisableFinalize();
239 return false;
240 }
241 g_debug = debug;
242
243 // Always enable the backtrace code since we will use it in a number
244 // of different error cases.
245 backtrace_startup();
246
247 return true;
248}
249
250void debug_finalize() {
251 if (g_debug == nullptr) {
252 return;
253 }
254
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700255 if (g_debug->config().options() & FREE_TRACK) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800256 PointerData::VerifyAllFreed();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800257 }
258
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700259 if (g_debug->config().options() & LEAK_TRACK) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800260 PointerData::LogLeaks();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800261 }
262
Christopher Ferris602b88c2017-08-04 13:04:04 -0700263 if ((g_debug->config().options() & BACKTRACE) && g_debug->config().backtrace_dump_on_exit()) {
264 ScopedDisableDebugCalls disable;
Christopher Ferris4da25032018-03-07 13:38:48 -0800265 debug_dump_heap(android::base::StringPrintf("%s.%d.exit.txt",
266 g_debug->config().backtrace_dump_prefix().c_str(),
267 getpid())
268 .c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700269 }
270
Christopher Ferris63860cb2015-11-16 17:30:32 -0800271 DebugDisableSet(true);
272
Colin Cross2c759912016-02-05 16:17:39 -0800273 backtrace_shutdown();
274
Christopher Ferris63860cb2015-11-16 17:30:32 -0800275 delete g_debug;
276 g_debug = nullptr;
277
278 DebugDisableFinalize();
279}
280
Christopher Ferris4da25032018-03-07 13:38:48 -0800281void debug_get_malloc_leak_info(uint8_t** info, size_t* overall_size, size_t* info_size,
282 size_t* total_memory, size_t* backtrace_size) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800283 ScopedDisableDebugCalls disable;
284
285 // Verify the arguments.
Christopher Ferris4da25032018-03-07 13:38:48 -0800286 if (info == nullptr || overall_size == nullptr || info_size == NULL || total_memory == nullptr ||
287 backtrace_size == nullptr) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800288 error_log("get_malloc_leak_info: At least one invalid parameter.");
289 return;
290 }
291
292 *info = nullptr;
293 *overall_size = 0;
294 *info_size = 0;
295 *total_memory = 0;
296 *backtrace_size = 0;
297
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700298 if (!(g_debug->config().options() & BACKTRACE)) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800299 error_log(
300 "get_malloc_leak_info: Allocations not being tracked, to enable "
301 "set the option 'backtrace'.");
Christopher Ferris63860cb2015-11-16 17:30:32 -0800302 return;
303 }
304
Christopher Ferris4da25032018-03-07 13:38:48 -0800305 PointerData::GetInfo(info, overall_size, info_size, total_memory, backtrace_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800306}
307
308void debug_free_malloc_leak_info(uint8_t* info) {
309 g_dispatch->free(info);
310}
311
Christopher Ferris55a89a42016-04-07 17:14:53 -0700312size_t debug_malloc_usable_size(void* pointer) {
313 if (DebugCallsDisabled() || pointer == nullptr) {
314 return g_dispatch->malloc_usable_size(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800315 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700316 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800317
Christopher Ferris4da25032018-03-07 13:38:48 -0800318 if (!VerifyPointer(pointer, "malloc_usable_size")) {
319 return 0;
320 }
321
322 return InternalMallocUsableSize(pointer);
Christopher Ferris55a89a42016-04-07 17:14:53 -0700323}
324
Christopher Ferris4da25032018-03-07 13:38:48 -0800325static void* InternalMalloc(size_t size) {
326 if ((g_debug->config().options() & BACKTRACE) && g_debug->pointer->ShouldDumpAndReset()) {
327 debug_dump_heap(android::base::StringPrintf(
328 "%s.%d.txt", g_debug->config().backtrace_dump_prefix().c_str(), getpid())
329 .c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700330 }
331
Colin Cross9567c7b2016-03-09 17:56:14 -0800332 if (size == 0) {
333 size = 1;
334 }
335
Christopher Ferris63860cb2015-11-16 17:30:32 -0800336 size_t real_size = size + g_debug->extra_bytes();
337 if (real_size < size) {
338 // Overflow.
339 errno = ENOMEM;
340 return nullptr;
341 }
342
Christopher Ferris4da25032018-03-07 13:38:48 -0800343 if (size > PointerInfoType::MaxSize()) {
344 errno = ENOMEM;
345 return nullptr;
346 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800347
Christopher Ferris4da25032018-03-07 13:38:48 -0800348 void* pointer;
349 if (g_debug->HeaderEnabled()) {
350 Header* header =
351 reinterpret_cast<Header*>(g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800352 if (header == nullptr) {
353 return nullptr;
354 }
355 pointer = InitHeader(header, header, size);
356 } else {
357 pointer = g_dispatch->malloc(real_size);
358 }
359
Christopher Ferris4da25032018-03-07 13:38:48 -0800360 if (pointer != nullptr) {
361 if (g_debug->TrackPointers()) {
362 PointerData::Add(pointer, size);
363 }
364
365 if (g_debug->config().options() & FILL_ON_ALLOC) {
366 size_t bytes = InternalMallocUsableSize(pointer);
367 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes();
368 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
369 memset(pointer, g_debug->config().fill_alloc_value(), bytes);
370 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800371 }
372 return pointer;
373}
374
Christopher Ferris55a89a42016-04-07 17:14:53 -0700375void* debug_malloc(size_t size) {
376 if (DebugCallsDisabled()) {
377 return g_dispatch->malloc(size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800378 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700379 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800380
Christopher Ferris4da25032018-03-07 13:38:48 -0800381 void* pointer = InternalMalloc(size);
Christopher Ferris7bd01782016-04-20 12:30:58 -0700382
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700383 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700384 g_debug->record->AddEntry(new MallocEntry(pointer, size));
385 }
386
387 return pointer;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700388}
389
Christopher Ferris4da25032018-03-07 13:38:48 -0800390static void InternalFree(void* pointer) {
391 if ((g_debug->config().options() & BACKTRACE) && g_debug->pointer->ShouldDumpAndReset()) {
392 debug_dump_heap(android::base::StringPrintf(
393 "%s.%d.txt", g_debug->config().backtrace_dump_prefix().c_str(), getpid())
394 .c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700395 }
396
Christopher Ferris63860cb2015-11-16 17:30:32 -0800397 void* free_pointer = pointer;
398 size_t bytes;
Christopher Ferrisd0919622016-03-15 22:39:39 -0700399 Header* header;
Christopher Ferris4da25032018-03-07 13:38:48 -0800400 if (g_debug->HeaderEnabled()) {
Christopher Ferrisd0919622016-03-15 22:39:39 -0700401 header = g_debug->GetHeader(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800402 free_pointer = header->orig_pointer;
403
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700404 if (g_debug->config().options() & FRONT_GUARD) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700405 if (!g_debug->front_guard->Valid(header)) {
406 g_debug->front_guard->LogFailure(header);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800407 }
408 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700409 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700410 if (!g_debug->rear_guard->Valid(header)) {
411 g_debug->rear_guard->LogFailure(header);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800412 }
413 }
414
Christopher Ferris7993b802016-01-28 18:35:05 -0800415 header->tag = DEBUG_FREE_TAG;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800416
417 bytes = header->usable_size;
418 } else {
419 bytes = g_dispatch->malloc_usable_size(pointer);
420 }
421
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700422 if (g_debug->config().options() & FILL_ON_FREE) {
423 size_t fill_bytes = g_debug->config().fill_on_free_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800424 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700425 memset(pointer, g_debug->config().fill_free_value(), bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800426 }
427
Christopher Ferris4da25032018-03-07 13:38:48 -0800428 if (g_debug->TrackPointers()) {
429 PointerData::Remove(pointer);
430 }
431
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700432 if (g_debug->config().options() & FREE_TRACK) {
Christopher Ferrisd0919622016-03-15 22:39:39 -0700433 // Do not add the allocation until we are done modifying the pointer
434 // itself. This avoids a race if a lot of threads are all doing
435 // frees at the same time and we wind up trying to really free this
436 // pointer from another thread, while still trying to free it in
437 // this function.
Christopher Ferris4da25032018-03-07 13:38:48 -0800438 pointer = PointerData::AddFreed(pointer);
439 if (pointer != nullptr) {
440 if (g_debug->HeaderEnabled()) {
441 pointer = g_debug->GetHeader(pointer)->orig_pointer;
442 }
443 g_dispatch->free(pointer);
444 }
Christopher Ferrisd0919622016-03-15 22:39:39 -0700445 } else {
446 g_dispatch->free(free_pointer);
447 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800448}
449
Christopher Ferris55a89a42016-04-07 17:14:53 -0700450void debug_free(void* pointer) {
451 if (DebugCallsDisabled() || pointer == nullptr) {
452 return g_dispatch->free(pointer);
453 }
454 ScopedDisableDebugCalls disable;
455
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700456 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700457 g_debug->record->AddEntry(new FreeEntry(pointer));
458 }
459
Christopher Ferris4da25032018-03-07 13:38:48 -0800460 if (!VerifyPointer(pointer, "free")) {
461 return;
462 }
463
464 InternalFree(pointer);
Christopher Ferris55a89a42016-04-07 17:14:53 -0700465}
466
Christopher Ferris63860cb2015-11-16 17:30:32 -0800467void* debug_memalign(size_t alignment, size_t bytes) {
468 if (DebugCallsDisabled()) {
469 return g_dispatch->memalign(alignment, bytes);
470 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700471 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800472
Colin Cross9567c7b2016-03-09 17:56:14 -0800473 if (bytes == 0) {
474 bytes = 1;
475 }
476
Christopher Ferris4da25032018-03-07 13:38:48 -0800477 if (bytes > PointerInfoType::MaxSize()) {
478 errno = ENOMEM;
479 return nullptr;
480 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800481
Christopher Ferris4da25032018-03-07 13:38:48 -0800482 void* pointer;
483 if (g_debug->HeaderEnabled()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800484 // Make the alignment a power of two.
485 if (!powerof2(alignment)) {
486 alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment);
487 }
Christopher Ferris72df6702016-02-11 15:51:31 -0800488 // Force the alignment to at least MINIMUM_ALIGNMENT_BYTES to guarantee
Christopher Ferris63860cb2015-11-16 17:30:32 -0800489 // that the header is aligned properly.
Christopher Ferris72df6702016-02-11 15:51:31 -0800490 if (alignment < MINIMUM_ALIGNMENT_BYTES) {
491 alignment = MINIMUM_ALIGNMENT_BYTES;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800492 }
493
494 // We don't have any idea what the natural alignment of
495 // the underlying native allocator is, so we always need to
496 // over allocate.
497 size_t real_size = alignment + bytes + g_debug->extra_bytes();
498 if (real_size < bytes) {
499 // Overflow.
500 errno = ENOMEM;
501 return nullptr;
502 }
503
504 pointer = g_dispatch->malloc(real_size);
505 if (pointer == nullptr) {
506 return nullptr;
507 }
508
509 uintptr_t value = reinterpret_cast<uintptr_t>(pointer) + g_debug->pointer_offset();
510 // Now align the pointer.
511 value += (-value % alignment);
512
513 Header* header = g_debug->GetHeader(reinterpret_cast<void*>(value));
514 pointer = InitHeader(header, pointer, bytes);
515 } else {
516 size_t real_size = bytes + g_debug->extra_bytes();
517 if (real_size < bytes) {
518 // Overflow.
519 errno = ENOMEM;
520 return nullptr;
521 }
522 pointer = g_dispatch->memalign(alignment, real_size);
523 }
524
Christopher Ferris4da25032018-03-07 13:38:48 -0800525 if (pointer != nullptr) {
526 if (g_debug->TrackPointers()) {
527 PointerData::Add(pointer, bytes);
528 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700529
Christopher Ferris4da25032018-03-07 13:38:48 -0800530 if (g_debug->config().options() & FILL_ON_ALLOC) {
531 size_t bytes = InternalMallocUsableSize(pointer);
532 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes();
533 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
534 memset(pointer, g_debug->config().fill_alloc_value(), bytes);
535 }
536
537 if (g_debug->config().options() & RECORD_ALLOCS) {
538 g_debug->record->AddEntry(new MemalignEntry(pointer, bytes, alignment));
539 }
Christopher Ferris7bd01782016-04-20 12:30:58 -0700540 }
541
Christopher Ferris63860cb2015-11-16 17:30:32 -0800542 return pointer;
543}
544
545void* debug_realloc(void* pointer, size_t bytes) {
546 if (DebugCallsDisabled()) {
547 return g_dispatch->realloc(pointer, bytes);
548 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700549 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800550
551 if (pointer == nullptr) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800552 pointer = InternalMalloc(bytes);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700553 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700554 g_debug->record->AddEntry(new ReallocEntry(pointer, bytes, nullptr));
555 }
556 return pointer;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800557 }
558
Christopher Ferris4da25032018-03-07 13:38:48 -0800559 if (!VerifyPointer(pointer, "realloc")) {
560 return nullptr;
561 }
562
Christopher Ferris63860cb2015-11-16 17:30:32 -0800563 if (bytes == 0) {
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700564 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700565 g_debug->record->AddEntry(new ReallocEntry(nullptr, bytes, pointer));
566 }
567
Christopher Ferris4da25032018-03-07 13:38:48 -0800568 InternalFree(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800569 return nullptr;
570 }
571
572 size_t real_size = bytes;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700573 if (g_debug->config().options() & EXPAND_ALLOC) {
574 real_size += g_debug->config().expand_alloc_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800575 if (real_size < bytes) {
576 // Overflow.
577 errno = ENOMEM;
578 return nullptr;
579 }
580 }
581
Christopher Ferris4da25032018-03-07 13:38:48 -0800582 if (bytes > PointerInfoType::MaxSize()) {
583 errno = ENOMEM;
584 return nullptr;
585 }
586
Christopher Ferris63860cb2015-11-16 17:30:32 -0800587 void* new_pointer;
588 size_t prev_size;
Christopher Ferris4da25032018-03-07 13:38:48 -0800589 if (g_debug->HeaderEnabled()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800590 // Same size, do nothing.
Christopher Ferris4da25032018-03-07 13:38:48 -0800591 Header* header = g_debug->GetHeader(pointer);
592 if (real_size == header->size) {
593 if (g_debug->TrackPointers()) {
594 // Remove and re-add so that the backtrace is updated.
595 PointerData::Remove(pointer);
596 PointerData::Add(pointer, real_size);
597 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800598 return pointer;
599 }
600
601 // Allocation is shrinking.
602 if (real_size < header->usable_size) {
603 header->size = real_size;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700604 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800605 // Don't bother allocating a smaller pointer in this case, simply
606 // change the header usable_size and reset the rear guard.
Christopher Ferris4da25032018-03-07 13:38:48 -0800607 header->usable_size = header->size;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700608 memset(g_debug->GetRearGuard(header), g_debug->config().rear_guard_value(),
609 g_debug->config().rear_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800610 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800611 if (g_debug->TrackPointers()) {
612 // Remove and re-add so that the backtrace is updated.
613 PointerData::Remove(pointer);
614 PointerData::Add(pointer, real_size);
615 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800616 return pointer;
617 }
618
619 // Allocate the new size.
Christopher Ferris4da25032018-03-07 13:38:48 -0800620 new_pointer = InternalMalloc(bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800621 if (new_pointer == nullptr) {
622 errno = ENOMEM;
623 return nullptr;
624 }
625
626 prev_size = header->usable_size;
627 memcpy(new_pointer, pointer, prev_size);
Christopher Ferris4da25032018-03-07 13:38:48 -0800628 InternalFree(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800629 } else {
Christopher Ferris4da25032018-03-07 13:38:48 -0800630 if (g_debug->TrackPointers()) {
631 PointerData::Remove(pointer);
632 }
633
Christopher Ferris63860cb2015-11-16 17:30:32 -0800634 prev_size = g_dispatch->malloc_usable_size(pointer);
635 new_pointer = g_dispatch->realloc(pointer, real_size);
636 if (new_pointer == nullptr) {
637 return nullptr;
638 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800639
640 if (g_debug->TrackPointers()) {
641 PointerData::Add(new_pointer, real_size);
642 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800643 }
644
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700645 if (g_debug->config().options() & FILL_ON_ALLOC) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800646 size_t bytes = InternalMallocUsableSize(new_pointer);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700647 if (bytes > g_debug->config().fill_on_alloc_bytes()) {
648 bytes = g_debug->config().fill_on_alloc_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800649 }
650 if (bytes > prev_size) {
651 memset(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(new_pointer) + prev_size),
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700652 g_debug->config().fill_alloc_value(), bytes - prev_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800653 }
654 }
655
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700656 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700657 g_debug->record->AddEntry(new ReallocEntry(new_pointer, bytes, pointer));
658 }
659
Christopher Ferris63860cb2015-11-16 17:30:32 -0800660 return new_pointer;
661}
662
663void* debug_calloc(size_t nmemb, size_t bytes) {
664 if (DebugCallsDisabled()) {
665 return g_dispatch->calloc(nmemb, bytes);
666 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700667 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800668
Colin Cross7877df62016-03-10 13:01:27 -0800669 size_t size;
670 if (__builtin_mul_overflow(nmemb, bytes, &size)) {
671 // Overflow
672 errno = ENOMEM;
673 return nullptr;
674 }
675
Colin Cross9567c7b2016-03-09 17:56:14 -0800676 if (size == 0) {
677 size = 1;
678 }
679
Colin Cross7877df62016-03-10 13:01:27 -0800680 size_t real_size;
681 if (__builtin_add_overflow(size, g_debug->extra_bytes(), &real_size)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800682 // Overflow.
683 errno = ENOMEM;
684 return nullptr;
685 }
686
Christopher Ferris4da25032018-03-07 13:38:48 -0800687 if (real_size > PointerInfoType::MaxSize()) {
688 errno = ENOMEM;
689 return nullptr;
690 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800691
Christopher Ferris4da25032018-03-07 13:38:48 -0800692 void* pointer;
693 if (g_debug->HeaderEnabled()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800694 // Need to guarantee the alignment of the header.
Christopher Ferris4da25032018-03-07 13:38:48 -0800695 Header* header =
696 reinterpret_cast<Header*>(g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800697 if (header == nullptr) {
698 return nullptr;
699 }
700 memset(header, 0, g_dispatch->malloc_usable_size(header));
Christopher Ferris7bd01782016-04-20 12:30:58 -0700701 pointer = InitHeader(header, header, size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800702 } else {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700703 pointer = g_dispatch->calloc(1, real_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800704 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800705
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700706 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700707 g_debug->record->AddEntry(new CallocEntry(pointer, bytes, nmemb));
708 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800709
710 if (pointer != nullptr && g_debug->TrackPointers()) {
711 PointerData::Add(pointer, size);
712 }
Christopher Ferris7bd01782016-04-20 12:30:58 -0700713 return pointer;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800714}
715
716struct mallinfo debug_mallinfo() {
717 return g_dispatch->mallinfo();
718}
719
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700720int debug_mallopt(int param, int value) {
721 return g_dispatch->mallopt(param, value);
722}
723
Christopher Ferriscae21a92018-02-05 18:14:55 -0800724void* debug_aligned_alloc(size_t alignment, size_t size) {
725 if (DebugCallsDisabled()) {
726 return g_dispatch->aligned_alloc(alignment, size);
727 }
728 if (!powerof2(alignment)) {
729 errno = EINVAL;
730 return nullptr;
731 }
732 return debug_memalign(alignment, size);
733}
734
Christopher Ferris63860cb2015-11-16 17:30:32 -0800735int debug_posix_memalign(void** memptr, size_t alignment, size_t size) {
736 if (DebugCallsDisabled()) {
737 return g_dispatch->posix_memalign(memptr, alignment, size);
738 }
739
740 if (!powerof2(alignment)) {
741 return EINVAL;
742 }
743 int saved_errno = errno;
744 *memptr = debug_memalign(alignment, size);
745 errno = saved_errno;
746 return (*memptr != nullptr) ? 0 : ENOMEM;
747}
748
Christopher Ferris4da25032018-03-07 13:38:48 -0800749int debug_iterate(uintptr_t base, size_t size, void (*callback)(uintptr_t, size_t, void*),
750 void* arg) {
751 if (g_debug->TrackPointers()) {
752 // Since malloc is disabled, don't bother acquiring any locks.
753 for (auto it = PointerData::begin(); it != PointerData::end(); ++it) {
754 callback(it->first, InternalMallocUsableSize(reinterpret_cast<void*>(it->first)), arg);
755 }
756 return 0;
757 }
Colin Cross869691c2016-01-29 12:48:18 -0800758
Christopher Ferris4da25032018-03-07 13:38:48 -0800759 // An option that adds a header will add pointer tracking, so no need to
760 // check if headers are enabled.
761 return g_dispatch->iterate(base, size, callback, arg);
Colin Cross869691c2016-01-29 12:48:18 -0800762}
763
764void debug_malloc_disable() {
765 g_dispatch->malloc_disable();
Christopher Ferris4da25032018-03-07 13:38:48 -0800766 if (g_debug->pointer) {
767 g_debug->pointer->PrepareFork();
Colin Cross869691c2016-01-29 12:48:18 -0800768 }
769}
770
771void debug_malloc_enable() {
Christopher Ferris4da25032018-03-07 13:38:48 -0800772 if (g_debug->pointer) {
773 g_debug->pointer->PostForkParent();
Colin Cross869691c2016-01-29 12:48:18 -0800774 }
775 g_dispatch->malloc_enable();
776}
777
Christopher Ferris4da25032018-03-07 13:38:48 -0800778ssize_t debug_malloc_backtrace(void* pointer, uintptr_t* frames, size_t max_frames) {
Colin Cross2d4721c2016-02-02 11:57:54 -0800779 if (DebugCallsDisabled() || pointer == nullptr) {
780 return 0;
781 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700782 ScopedDisableDebugCalls disable;
Colin Cross2d4721c2016-02-02 11:57:54 -0800783
Christopher Ferris4da25032018-03-07 13:38:48 -0800784 if (!(g_debug->config().options() & BACKTRACE)) {
785 return 0;
Colin Cross2d4721c2016-02-02 11:57:54 -0800786 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800787 return PointerData::GetFrames(pointer, frames, max_frames);
Colin Cross2d4721c2016-02-02 11:57:54 -0800788}
789
Christopher Ferris63860cb2015-11-16 17:30:32 -0800790#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
791void* debug_pvalloc(size_t bytes) {
792 if (DebugCallsDisabled()) {
793 return g_dispatch->pvalloc(bytes);
794 }
795
796 size_t pagesize = getpagesize();
Dan Alberta613d0d2017-10-05 16:39:33 -0700797 size_t size = __BIONIC_ALIGN(bytes, pagesize);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800798 if (size < bytes) {
799 // Overflow
800 errno = ENOMEM;
801 return nullptr;
802 }
803 return debug_memalign(pagesize, size);
804}
805
806void* debug_valloc(size_t size) {
807 if (DebugCallsDisabled()) {
808 return g_dispatch->valloc(size);
809 }
810 return debug_memalign(getpagesize(), size);
811}
812#endif
Christopher Ferris602b88c2017-08-04 13:04:04 -0700813
814static std::mutex g_dump_lock;
815
816bool debug_dump_heap(const char* file_name) {
817 ScopedDisableDebugCalls disable;
818
819 std::lock_guard<std::mutex> guard(g_dump_lock);
820
821 FILE* fp = fopen(file_name, "w+e");
822 if (fp == nullptr) {
823 error_log("Unable to create file: %s", file_name);
824 return false;
825 }
826 error_log("Dumping to file: %s\n", file_name);
827
828 if (!(g_debug->config().options() & BACKTRACE)) {
829 fprintf(fp, "Native heap dump not available. To enable, run these commands (requires root):\n");
830 fprintf(fp, "# adb shell stop\n");
831 fprintf(fp, "# adb shell setprop libc.debug.malloc.options backtrace\n");
832 fprintf(fp, "# adb shell start\n");
833 fclose(fp);
834 return false;
835 }
836
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700837 fprintf(fp, "Android Native Heap Dump v1.1\n\n");
Christopher Ferris602b88c2017-08-04 13:04:04 -0700838
Christopher Ferris4da25032018-03-07 13:38:48 -0800839 PointerData::DumpLiveToFile(fp);
Christopher Ferris602b88c2017-08-04 13:04:04 -0700840
841 fprintf(fp, "MAPS\n");
842 std::string content;
843 if (!android::base::ReadFileToString("/proc/self/maps", &content)) {
844 fprintf(fp, "Could not open /proc/self/maps\n");
845 } else {
846 fprintf(fp, "%s", content.c_str());
847 }
848 fprintf(fp, "END\n");
849 fclose(fp);
850 return true;
851}