blob: 9075a9caaf49905d8b0a2abcc3630384eb0231ad [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 Ferris97b47472018-07-10 14:45:24 -0700257 // Turn off capturing allocations calls.
258 DebugDisableSet(true);
259
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700260 if (g_debug->config().options() & FREE_TRACK) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800261 PointerData::VerifyAllFreed();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800262 }
263
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700264 if (g_debug->config().options() & LEAK_TRACK) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800265 PointerData::LogLeaks();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800266 }
267
Christopher Ferris602b88c2017-08-04 13:04:04 -0700268 if ((g_debug->config().options() & BACKTRACE) && g_debug->config().backtrace_dump_on_exit()) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800269 debug_dump_heap(android::base::StringPrintf("%s.%d.exit.txt",
270 g_debug->config().backtrace_dump_prefix().c_str(),
Christopher Ferris97b47472018-07-10 14:45:24 -0700271 getpid()).c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700272 }
273
Colin Cross2c759912016-02-05 16:17:39 -0800274 backtrace_shutdown();
275
Christopher Ferris63860cb2015-11-16 17:30:32 -0800276 delete g_debug;
277 g_debug = nullptr;
278
279 DebugDisableFinalize();
280}
281
Christopher Ferris4da25032018-03-07 13:38:48 -0800282void debug_get_malloc_leak_info(uint8_t** info, size_t* overall_size, size_t* info_size,
283 size_t* total_memory, size_t* backtrace_size) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800284 ScopedDisableDebugCalls disable;
285
286 // Verify the arguments.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700287 if (info == nullptr || overall_size == nullptr || info_size == nullptr || total_memory == nullptr ||
Christopher Ferris4da25032018-03-07 13:38:48 -0800288 backtrace_size == nullptr) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800289 error_log("get_malloc_leak_info: At least one invalid parameter.");
290 return;
291 }
292
293 *info = nullptr;
294 *overall_size = 0;
295 *info_size = 0;
296 *total_memory = 0;
297 *backtrace_size = 0;
298
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700299 if (!(g_debug->config().options() & BACKTRACE)) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800300 error_log(
301 "get_malloc_leak_info: Allocations not being tracked, to enable "
302 "set the option 'backtrace'.");
Christopher Ferris63860cb2015-11-16 17:30:32 -0800303 return;
304 }
305
Christopher Ferris4da25032018-03-07 13:38:48 -0800306 PointerData::GetInfo(info, overall_size, info_size, total_memory, backtrace_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800307}
308
309void debug_free_malloc_leak_info(uint8_t* info) {
310 g_dispatch->free(info);
311}
312
Christopher Ferris55a89a42016-04-07 17:14:53 -0700313size_t debug_malloc_usable_size(void* pointer) {
314 if (DebugCallsDisabled() || pointer == nullptr) {
315 return g_dispatch->malloc_usable_size(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800316 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700317 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800318
Christopher Ferris4da25032018-03-07 13:38:48 -0800319 if (!VerifyPointer(pointer, "malloc_usable_size")) {
320 return 0;
321 }
322
323 return InternalMallocUsableSize(pointer);
Christopher Ferris55a89a42016-04-07 17:14:53 -0700324}
325
Christopher Ferris4da25032018-03-07 13:38:48 -0800326static void* InternalMalloc(size_t size) {
327 if ((g_debug->config().options() & BACKTRACE) && g_debug->pointer->ShouldDumpAndReset()) {
328 debug_dump_heap(android::base::StringPrintf(
329 "%s.%d.txt", g_debug->config().backtrace_dump_prefix().c_str(), getpid())
330 .c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700331 }
332
Colin Cross9567c7b2016-03-09 17:56:14 -0800333 if (size == 0) {
334 size = 1;
335 }
336
Christopher Ferris63860cb2015-11-16 17:30:32 -0800337 size_t real_size = size + g_debug->extra_bytes();
338 if (real_size < size) {
339 // Overflow.
340 errno = ENOMEM;
341 return nullptr;
342 }
343
Christopher Ferris4da25032018-03-07 13:38:48 -0800344 if (size > PointerInfoType::MaxSize()) {
345 errno = ENOMEM;
346 return nullptr;
347 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800348
Christopher Ferris4da25032018-03-07 13:38:48 -0800349 void* pointer;
350 if (g_debug->HeaderEnabled()) {
351 Header* header =
352 reinterpret_cast<Header*>(g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800353 if (header == nullptr) {
354 return nullptr;
355 }
356 pointer = InitHeader(header, header, size);
357 } else {
358 pointer = g_dispatch->malloc(real_size);
359 }
360
Christopher Ferris4da25032018-03-07 13:38:48 -0800361 if (pointer != nullptr) {
362 if (g_debug->TrackPointers()) {
363 PointerData::Add(pointer, size);
364 }
365
366 if (g_debug->config().options() & FILL_ON_ALLOC) {
367 size_t bytes = InternalMallocUsableSize(pointer);
368 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes();
369 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
370 memset(pointer, g_debug->config().fill_alloc_value(), bytes);
371 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800372 }
373 return pointer;
374}
375
Christopher Ferris55a89a42016-04-07 17:14:53 -0700376void* debug_malloc(size_t size) {
377 if (DebugCallsDisabled()) {
378 return g_dispatch->malloc(size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800379 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700380 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800381
Christopher Ferris4da25032018-03-07 13:38:48 -0800382 void* pointer = InternalMalloc(size);
Christopher Ferris7bd01782016-04-20 12:30:58 -0700383
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700384 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700385 g_debug->record->AddEntry(new MallocEntry(pointer, size));
386 }
387
388 return pointer;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700389}
390
Christopher Ferris4da25032018-03-07 13:38:48 -0800391static void InternalFree(void* pointer) {
392 if ((g_debug->config().options() & BACKTRACE) && g_debug->pointer->ShouldDumpAndReset()) {
393 debug_dump_heap(android::base::StringPrintf(
394 "%s.%d.txt", g_debug->config().backtrace_dump_prefix().c_str(), getpid())
395 .c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700396 }
397
Christopher Ferris63860cb2015-11-16 17:30:32 -0800398 void* free_pointer = pointer;
399 size_t bytes;
Christopher Ferrisd0919622016-03-15 22:39:39 -0700400 Header* header;
Christopher Ferris4da25032018-03-07 13:38:48 -0800401 if (g_debug->HeaderEnabled()) {
Christopher Ferrisd0919622016-03-15 22:39:39 -0700402 header = g_debug->GetHeader(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800403 free_pointer = header->orig_pointer;
404
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700405 if (g_debug->config().options() & FRONT_GUARD) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700406 if (!g_debug->front_guard->Valid(header)) {
407 g_debug->front_guard->LogFailure(header);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800408 }
409 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700410 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700411 if (!g_debug->rear_guard->Valid(header)) {
412 g_debug->rear_guard->LogFailure(header);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800413 }
414 }
415
Christopher Ferris7993b802016-01-28 18:35:05 -0800416 header->tag = DEBUG_FREE_TAG;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800417
418 bytes = header->usable_size;
419 } else {
420 bytes = g_dispatch->malloc_usable_size(pointer);
421 }
422
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700423 if (g_debug->config().options() & FILL_ON_FREE) {
424 size_t fill_bytes = g_debug->config().fill_on_free_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800425 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700426 memset(pointer, g_debug->config().fill_free_value(), bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800427 }
428
Christopher Ferris4da25032018-03-07 13:38:48 -0800429 if (g_debug->TrackPointers()) {
430 PointerData::Remove(pointer);
431 }
432
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700433 if (g_debug->config().options() & FREE_TRACK) {
Christopher Ferrisd0919622016-03-15 22:39:39 -0700434 // Do not add the allocation until we are done modifying the pointer
435 // itself. This avoids a race if a lot of threads are all doing
436 // frees at the same time and we wind up trying to really free this
437 // pointer from another thread, while still trying to free it in
438 // this function.
Christopher Ferris4da25032018-03-07 13:38:48 -0800439 pointer = PointerData::AddFreed(pointer);
440 if (pointer != nullptr) {
441 if (g_debug->HeaderEnabled()) {
442 pointer = g_debug->GetHeader(pointer)->orig_pointer;
443 }
444 g_dispatch->free(pointer);
445 }
Christopher Ferrisd0919622016-03-15 22:39:39 -0700446 } else {
447 g_dispatch->free(free_pointer);
448 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800449}
450
Christopher Ferris55a89a42016-04-07 17:14:53 -0700451void debug_free(void* pointer) {
452 if (DebugCallsDisabled() || pointer == nullptr) {
453 return g_dispatch->free(pointer);
454 }
455 ScopedDisableDebugCalls disable;
456
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700457 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700458 g_debug->record->AddEntry(new FreeEntry(pointer));
459 }
460
Christopher Ferris4da25032018-03-07 13:38:48 -0800461 if (!VerifyPointer(pointer, "free")) {
462 return;
463 }
464
465 InternalFree(pointer);
Christopher Ferris55a89a42016-04-07 17:14:53 -0700466}
467
Christopher Ferris63860cb2015-11-16 17:30:32 -0800468void* debug_memalign(size_t alignment, size_t bytes) {
469 if (DebugCallsDisabled()) {
470 return g_dispatch->memalign(alignment, bytes);
471 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700472 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800473
Colin Cross9567c7b2016-03-09 17:56:14 -0800474 if (bytes == 0) {
475 bytes = 1;
476 }
477
Christopher Ferris4da25032018-03-07 13:38:48 -0800478 if (bytes > PointerInfoType::MaxSize()) {
479 errno = ENOMEM;
480 return nullptr;
481 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800482
Christopher Ferris4da25032018-03-07 13:38:48 -0800483 void* pointer;
484 if (g_debug->HeaderEnabled()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800485 // Make the alignment a power of two.
486 if (!powerof2(alignment)) {
487 alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment);
488 }
Christopher Ferris72df6702016-02-11 15:51:31 -0800489 // Force the alignment to at least MINIMUM_ALIGNMENT_BYTES to guarantee
Christopher Ferris63860cb2015-11-16 17:30:32 -0800490 // that the header is aligned properly.
Christopher Ferris72df6702016-02-11 15:51:31 -0800491 if (alignment < MINIMUM_ALIGNMENT_BYTES) {
492 alignment = MINIMUM_ALIGNMENT_BYTES;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800493 }
494
495 // We don't have any idea what the natural alignment of
496 // the underlying native allocator is, so we always need to
497 // over allocate.
498 size_t real_size = alignment + bytes + g_debug->extra_bytes();
499 if (real_size < bytes) {
500 // Overflow.
501 errno = ENOMEM;
502 return nullptr;
503 }
504
505 pointer = g_dispatch->malloc(real_size);
506 if (pointer == nullptr) {
507 return nullptr;
508 }
509
510 uintptr_t value = reinterpret_cast<uintptr_t>(pointer) + g_debug->pointer_offset();
511 // Now align the pointer.
512 value += (-value % alignment);
513
514 Header* header = g_debug->GetHeader(reinterpret_cast<void*>(value));
515 pointer = InitHeader(header, pointer, bytes);
516 } else {
517 size_t real_size = bytes + g_debug->extra_bytes();
518 if (real_size < bytes) {
519 // Overflow.
520 errno = ENOMEM;
521 return nullptr;
522 }
523 pointer = g_dispatch->memalign(alignment, real_size);
524 }
525
Christopher Ferris4da25032018-03-07 13:38:48 -0800526 if (pointer != nullptr) {
527 if (g_debug->TrackPointers()) {
528 PointerData::Add(pointer, bytes);
529 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700530
Christopher Ferris4da25032018-03-07 13:38:48 -0800531 if (g_debug->config().options() & FILL_ON_ALLOC) {
532 size_t bytes = InternalMallocUsableSize(pointer);
533 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes();
534 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
535 memset(pointer, g_debug->config().fill_alloc_value(), bytes);
536 }
537
538 if (g_debug->config().options() & RECORD_ALLOCS) {
539 g_debug->record->AddEntry(new MemalignEntry(pointer, bytes, alignment));
540 }
Christopher Ferris7bd01782016-04-20 12:30:58 -0700541 }
542
Christopher Ferris63860cb2015-11-16 17:30:32 -0800543 return pointer;
544}
545
546void* debug_realloc(void* pointer, size_t bytes) {
547 if (DebugCallsDisabled()) {
548 return g_dispatch->realloc(pointer, bytes);
549 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700550 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800551
552 if (pointer == nullptr) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800553 pointer = InternalMalloc(bytes);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700554 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700555 g_debug->record->AddEntry(new ReallocEntry(pointer, bytes, nullptr));
556 }
557 return pointer;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800558 }
559
Christopher Ferris4da25032018-03-07 13:38:48 -0800560 if (!VerifyPointer(pointer, "realloc")) {
561 return nullptr;
562 }
563
Christopher Ferris63860cb2015-11-16 17:30:32 -0800564 if (bytes == 0) {
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700565 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700566 g_debug->record->AddEntry(new ReallocEntry(nullptr, bytes, pointer));
567 }
568
Christopher Ferris4da25032018-03-07 13:38:48 -0800569 InternalFree(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800570 return nullptr;
571 }
572
573 size_t real_size = bytes;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700574 if (g_debug->config().options() & EXPAND_ALLOC) {
575 real_size += g_debug->config().expand_alloc_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800576 if (real_size < bytes) {
577 // Overflow.
578 errno = ENOMEM;
579 return nullptr;
580 }
581 }
582
Christopher Ferris4da25032018-03-07 13:38:48 -0800583 if (bytes > PointerInfoType::MaxSize()) {
584 errno = ENOMEM;
585 return nullptr;
586 }
587
Christopher Ferris63860cb2015-11-16 17:30:32 -0800588 void* new_pointer;
589 size_t prev_size;
Christopher Ferris4da25032018-03-07 13:38:48 -0800590 if (g_debug->HeaderEnabled()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800591 // Same size, do nothing.
Christopher Ferris4da25032018-03-07 13:38:48 -0800592 Header* header = g_debug->GetHeader(pointer);
593 if (real_size == header->size) {
594 if (g_debug->TrackPointers()) {
595 // Remove and re-add so that the backtrace is updated.
596 PointerData::Remove(pointer);
597 PointerData::Add(pointer, real_size);
598 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800599 return pointer;
600 }
601
602 // Allocation is shrinking.
603 if (real_size < header->usable_size) {
604 header->size = real_size;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700605 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800606 // Don't bother allocating a smaller pointer in this case, simply
607 // change the header usable_size and reset the rear guard.
Christopher Ferris4da25032018-03-07 13:38:48 -0800608 header->usable_size = header->size;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700609 memset(g_debug->GetRearGuard(header), g_debug->config().rear_guard_value(),
610 g_debug->config().rear_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800611 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800612 if (g_debug->TrackPointers()) {
613 // Remove and re-add so that the backtrace is updated.
614 PointerData::Remove(pointer);
615 PointerData::Add(pointer, real_size);
616 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800617 return pointer;
618 }
619
620 // Allocate the new size.
Christopher Ferris4da25032018-03-07 13:38:48 -0800621 new_pointer = InternalMalloc(bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800622 if (new_pointer == nullptr) {
623 errno = ENOMEM;
624 return nullptr;
625 }
626
627 prev_size = header->usable_size;
628 memcpy(new_pointer, pointer, prev_size);
Christopher Ferris4da25032018-03-07 13:38:48 -0800629 InternalFree(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800630 } else {
Christopher Ferris4da25032018-03-07 13:38:48 -0800631 if (g_debug->TrackPointers()) {
632 PointerData::Remove(pointer);
633 }
634
Christopher Ferris63860cb2015-11-16 17:30:32 -0800635 prev_size = g_dispatch->malloc_usable_size(pointer);
636 new_pointer = g_dispatch->realloc(pointer, real_size);
637 if (new_pointer == nullptr) {
638 return nullptr;
639 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800640
641 if (g_debug->TrackPointers()) {
642 PointerData::Add(new_pointer, real_size);
643 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800644 }
645
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700646 if (g_debug->config().options() & FILL_ON_ALLOC) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800647 size_t bytes = InternalMallocUsableSize(new_pointer);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700648 if (bytes > g_debug->config().fill_on_alloc_bytes()) {
649 bytes = g_debug->config().fill_on_alloc_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800650 }
651 if (bytes > prev_size) {
652 memset(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(new_pointer) + prev_size),
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700653 g_debug->config().fill_alloc_value(), bytes - prev_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800654 }
655 }
656
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700657 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700658 g_debug->record->AddEntry(new ReallocEntry(new_pointer, bytes, pointer));
659 }
660
Christopher Ferris63860cb2015-11-16 17:30:32 -0800661 return new_pointer;
662}
663
664void* debug_calloc(size_t nmemb, size_t bytes) {
665 if (DebugCallsDisabled()) {
666 return g_dispatch->calloc(nmemb, bytes);
667 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700668 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800669
Colin Cross7877df62016-03-10 13:01:27 -0800670 size_t size;
671 if (__builtin_mul_overflow(nmemb, bytes, &size)) {
672 // Overflow
673 errno = ENOMEM;
674 return nullptr;
675 }
676
Colin Cross9567c7b2016-03-09 17:56:14 -0800677 if (size == 0) {
678 size = 1;
679 }
680
Colin Cross7877df62016-03-10 13:01:27 -0800681 size_t real_size;
682 if (__builtin_add_overflow(size, g_debug->extra_bytes(), &real_size)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800683 // Overflow.
684 errno = ENOMEM;
685 return nullptr;
686 }
687
Christopher Ferris4da25032018-03-07 13:38:48 -0800688 if (real_size > PointerInfoType::MaxSize()) {
689 errno = ENOMEM;
690 return nullptr;
691 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800692
Christopher Ferris4da25032018-03-07 13:38:48 -0800693 void* pointer;
694 if (g_debug->HeaderEnabled()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800695 // Need to guarantee the alignment of the header.
Christopher Ferris4da25032018-03-07 13:38:48 -0800696 Header* header =
697 reinterpret_cast<Header*>(g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800698 if (header == nullptr) {
699 return nullptr;
700 }
701 memset(header, 0, g_dispatch->malloc_usable_size(header));
Christopher Ferris7bd01782016-04-20 12:30:58 -0700702 pointer = InitHeader(header, header, size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800703 } else {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700704 pointer = g_dispatch->calloc(1, real_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800705 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800706
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700707 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700708 g_debug->record->AddEntry(new CallocEntry(pointer, bytes, nmemb));
709 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800710
711 if (pointer != nullptr && g_debug->TrackPointers()) {
712 PointerData::Add(pointer, size);
713 }
Christopher Ferris7bd01782016-04-20 12:30:58 -0700714 return pointer;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800715}
716
717struct mallinfo debug_mallinfo() {
718 return g_dispatch->mallinfo();
719}
720
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700721int debug_mallopt(int param, int value) {
722 return g_dispatch->mallopt(param, value);
723}
724
Christopher Ferriscae21a92018-02-05 18:14:55 -0800725void* debug_aligned_alloc(size_t alignment, size_t size) {
726 if (DebugCallsDisabled()) {
727 return g_dispatch->aligned_alloc(alignment, size);
728 }
729 if (!powerof2(alignment)) {
730 errno = EINVAL;
731 return nullptr;
732 }
733 return debug_memalign(alignment, size);
734}
735
Christopher Ferris63860cb2015-11-16 17:30:32 -0800736int debug_posix_memalign(void** memptr, size_t alignment, size_t size) {
737 if (DebugCallsDisabled()) {
738 return g_dispatch->posix_memalign(memptr, alignment, size);
739 }
740
741 if (!powerof2(alignment)) {
742 return EINVAL;
743 }
744 int saved_errno = errno;
745 *memptr = debug_memalign(alignment, size);
746 errno = saved_errno;
747 return (*memptr != nullptr) ? 0 : ENOMEM;
748}
749
Christopher Ferris4da25032018-03-07 13:38:48 -0800750int debug_iterate(uintptr_t base, size_t size, void (*callback)(uintptr_t, size_t, void*),
751 void* arg) {
752 if (g_debug->TrackPointers()) {
753 // Since malloc is disabled, don't bother acquiring any locks.
754 for (auto it = PointerData::begin(); it != PointerData::end(); ++it) {
755 callback(it->first, InternalMallocUsableSize(reinterpret_cast<void*>(it->first)), arg);
756 }
757 return 0;
758 }
Colin Cross869691c2016-01-29 12:48:18 -0800759
Christopher Ferris4da25032018-03-07 13:38:48 -0800760 // An option that adds a header will add pointer tracking, so no need to
761 // check if headers are enabled.
762 return g_dispatch->iterate(base, size, callback, arg);
Colin Cross869691c2016-01-29 12:48:18 -0800763}
764
765void debug_malloc_disable() {
766 g_dispatch->malloc_disable();
Christopher Ferris4da25032018-03-07 13:38:48 -0800767 if (g_debug->pointer) {
768 g_debug->pointer->PrepareFork();
Colin Cross869691c2016-01-29 12:48:18 -0800769 }
770}
771
772void debug_malloc_enable() {
Christopher Ferris4da25032018-03-07 13:38:48 -0800773 if (g_debug->pointer) {
774 g_debug->pointer->PostForkParent();
Colin Cross869691c2016-01-29 12:48:18 -0800775 }
776 g_dispatch->malloc_enable();
777}
778
Christopher Ferris4da25032018-03-07 13:38:48 -0800779ssize_t debug_malloc_backtrace(void* pointer, uintptr_t* frames, size_t max_frames) {
Colin Cross2d4721c2016-02-02 11:57:54 -0800780 if (DebugCallsDisabled() || pointer == nullptr) {
781 return 0;
782 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700783 ScopedDisableDebugCalls disable;
Colin Cross2d4721c2016-02-02 11:57:54 -0800784
Christopher Ferris4da25032018-03-07 13:38:48 -0800785 if (!(g_debug->config().options() & BACKTRACE)) {
786 return 0;
Colin Cross2d4721c2016-02-02 11:57:54 -0800787 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800788 return PointerData::GetFrames(pointer, frames, max_frames);
Colin Cross2d4721c2016-02-02 11:57:54 -0800789}
790
Christopher Ferris63860cb2015-11-16 17:30:32 -0800791#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
792void* debug_pvalloc(size_t bytes) {
793 if (DebugCallsDisabled()) {
794 return g_dispatch->pvalloc(bytes);
795 }
796
797 size_t pagesize = getpagesize();
Dan Alberta613d0d2017-10-05 16:39:33 -0700798 size_t size = __BIONIC_ALIGN(bytes, pagesize);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800799 if (size < bytes) {
800 // Overflow
801 errno = ENOMEM;
802 return nullptr;
803 }
804 return debug_memalign(pagesize, size);
805}
806
807void* debug_valloc(size_t size) {
808 if (DebugCallsDisabled()) {
809 return g_dispatch->valloc(size);
810 }
811 return debug_memalign(getpagesize(), size);
812}
813#endif
Christopher Ferris602b88c2017-08-04 13:04:04 -0700814
815static std::mutex g_dump_lock;
816
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700817static void write_dump(FILE* fp) {
818 fprintf(fp, "Android Native Heap Dump v1.2\n\n");
Christopher Ferris602b88c2017-08-04 13:04:04 -0700819
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700820 std::string fingerprint = android::base::GetProperty("ro.build.fingerprint", "unknown");
821 fprintf(fp, "Build fingerprint: '%s'\n\n", fingerprint.c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700822
Christopher Ferris4da25032018-03-07 13:38:48 -0800823 PointerData::DumpLiveToFile(fp);
Christopher Ferris602b88c2017-08-04 13:04:04 -0700824
825 fprintf(fp, "MAPS\n");
826 std::string content;
827 if (!android::base::ReadFileToString("/proc/self/maps", &content)) {
828 fprintf(fp, "Could not open /proc/self/maps\n");
829 } else {
830 fprintf(fp, "%s", content.c_str());
831 }
832 fprintf(fp, "END\n");
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700833}
834
835bool debug_write_malloc_leak_info(FILE* fp) {
836 ScopedDisableDebugCalls disable;
837
838 std::lock_guard<std::mutex> guard(g_dump_lock);
839
840 if (!(g_debug->config().options() & BACKTRACE)) {
841 return false;
842 }
843
844 write_dump(fp);
Christopher Ferris602b88c2017-08-04 13:04:04 -0700845 return true;
846}
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700847
848void debug_dump_heap(const char* file_name) {
849 ScopedDisableDebugCalls disable;
850
851 std::lock_guard<std::mutex> guard(g_dump_lock);
852
853 FILE* fp = fopen(file_name, "w+e");
854 if (fp == nullptr) {
855 error_log("Unable to create file: %s", file_name);
856 return;
857 }
858
859 error_log("Dumping to file: %s\n", file_name);
860 write_dump(fp);
861 fclose(fp);
862}