blob: 6f841cab57a46d7d0f07673c93fd1c68ad7b5f4c [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"
50
51// ------------------------------------------------------------------------
52// Global Data
53// ------------------------------------------------------------------------
54DebugData* g_debug;
55
56int* g_malloc_zygote_child;
57
58const MallocDispatch* g_dispatch;
59// ------------------------------------------------------------------------
60
61// ------------------------------------------------------------------------
62// Use C style prototypes for all exported functions. This makes it easy
63// to do dlsym lookups during libc initialization when malloc debug
64// is enabled.
65// ------------------------------------------------------------------------
66__BEGIN_DECLS
67
Tamas Berghammerac81fe82016-08-26 15:54:59 +010068bool debug_initialize(const MallocDispatch* malloc_dispatch, int* malloc_zygote_child,
Christopher Ferris4da25032018-03-07 13:38:48 -080069 const char* options);
Christopher Ferris63860cb2015-11-16 17:30:32 -080070void debug_finalize();
Christopher Ferris602b88c2017-08-04 13:04:04 -070071bool debug_dump_heap(const char* file_name);
Christopher Ferris4da25032018-03-07 13:38:48 -080072void debug_get_malloc_leak_info(uint8_t** info, size_t* overall_size, size_t* info_size,
73 size_t* total_memory, size_t* backtrace_size);
Colin Cross2d4721c2016-02-02 11:57:54 -080074ssize_t debug_malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count);
Christopher Ferris63860cb2015-11-16 17:30:32 -080075void debug_free_malloc_leak_info(uint8_t* info);
76size_t debug_malloc_usable_size(void* pointer);
77void* debug_malloc(size_t size);
78void debug_free(void* pointer);
Christopher Ferriscae21a92018-02-05 18:14:55 -080079void* debug_aligned_alloc(size_t alignment, size_t size);
Christopher Ferris63860cb2015-11-16 17:30:32 -080080void* debug_memalign(size_t alignment, size_t bytes);
81void* debug_realloc(void* pointer, size_t bytes);
82void* debug_calloc(size_t nmemb, size_t bytes);
83struct mallinfo debug_mallinfo();
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -070084int debug_mallopt(int param, int value);
Christopher Ferris63860cb2015-11-16 17:30:32 -080085int debug_posix_memalign(void** memptr, size_t alignment, size_t size);
Colin Cross869691c2016-01-29 12:48:18 -080086int debug_iterate(uintptr_t base, size_t size,
Christopher Ferris4da25032018-03-07 13:38:48 -080087 void (*callback)(uintptr_t base, size_t size, void* arg), void* arg);
Colin Cross869691c2016-01-29 12:48:18 -080088void debug_malloc_disable();
89void debug_malloc_enable();
Christopher Ferris63860cb2015-11-16 17:30:32 -080090
91#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
92void* debug_pvalloc(size_t bytes);
93void* debug_valloc(size_t size);
94#endif
95
96__END_DECLS
97// ------------------------------------------------------------------------
98
Colin Cross7a28a3c2016-02-07 22:51:15 -080099static void InitAtfork() {
100 static pthread_once_t atfork_init = PTHREAD_ONCE_INIT;
Christopher Ferris4da25032018-03-07 13:38:48 -0800101 pthread_once(&atfork_init, []() {
Colin Cross7a28a3c2016-02-07 22:51:15 -0800102 pthread_atfork(
Christopher Ferris4da25032018-03-07 13:38:48 -0800103 []() {
Colin Cross7a28a3c2016-02-07 22:51:15 -0800104 if (g_debug != nullptr) {
105 g_debug->PrepareFork();
106 }
107 },
Christopher Ferris4da25032018-03-07 13:38:48 -0800108 []() {
Colin Cross7a28a3c2016-02-07 22:51:15 -0800109 if (g_debug != nullptr) {
110 g_debug->PostForkParent();
111 }
112 },
Christopher Ferris4da25032018-03-07 13:38:48 -0800113 []() {
Colin Cross7a28a3c2016-02-07 22:51:15 -0800114 if (g_debug != nullptr) {
115 g_debug->PostForkChild();
116 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800117 });
Colin Cross7a28a3c2016-02-07 22:51:15 -0800118 });
119}
Christopher Ferrisd0919622016-03-15 22:39:39 -0700120
Christopher Ferris4da25032018-03-07 13:38:48 -0800121static void LogError(const void* pointer, const char* error_str) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800122 error_log(LOG_DIVIDER);
Christopher Ferris4da25032018-03-07 13:38:48 -0800123 error_log("+++ ALLOCATION %p %s", pointer, error_str);
124
125 // If we are tracking already freed pointers, check to see if this is
126 // one so we can print extra information.
127 if (g_debug->config().options() & FREE_TRACK) {
128 PointerData::LogFreeBacktrace(pointer);
Christopher Ferris7993b802016-01-28 18:35:05 -0800129 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800130
131 std::vector<uintptr_t> frames(128);
132 size_t num_frames = backtrace_get(frames.data(), frames.size());
133 if (num_frames == 0) {
134 error_log("Backtrace failed to get any frames.");
135 } else {
136 error_log("Backtrace at time of failure:");
137 backtrace_log(frames.data(), num_frames);
138 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800139 error_log(LOG_DIVIDER);
140}
141
Christopher Ferris4da25032018-03-07 13:38:48 -0800142static bool VerifyPointer(const void* pointer, const char* function_name) {
143 if (g_debug->HeaderEnabled()) {
144 Header* header = g_debug->GetHeader(pointer);
145 if (header->tag != DEBUG_TAG) {
146 std::string error_str;
147 if (header->tag == DEBUG_FREE_TAG) {
148 error_str = std::string("USED AFTER FREE (") + function_name + ")";
149 } else {
150 error_str = android::base::StringPrintf("HAS INVALID TAG %" PRIx32 " (%s)", header->tag,
151 function_name);
152 }
153 LogError(pointer, error_str.c_str());
154 return false;
155 }
156 }
157
158 if (g_debug->TrackPointers()) {
159 if (!PointerData::Exists(pointer)) {
160 std::string error_str(std::string("UNKNOWN POINTER (") + function_name + ")");
161 LogError(pointer, error_str.c_str());
162 return false;
163 }
164 }
165 return true;
166}
167
168static size_t InternalMallocUsableSize(void* pointer) {
169 if (g_debug->HeaderEnabled()) {
170 return g_debug->GetHeader(pointer)->usable_size;
171 } else {
172 return g_dispatch->malloc_usable_size(pointer);
173 }
174}
175
Christopher Ferris63860cb2015-11-16 17:30:32 -0800176static void* InitHeader(Header* header, void* orig_pointer, size_t size) {
177 header->tag = DEBUG_TAG;
178 header->orig_pointer = orig_pointer;
179 header->size = size;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800180 header->usable_size = g_dispatch->malloc_usable_size(orig_pointer);
181 if (header->usable_size == 0) {
182 g_dispatch->free(orig_pointer);
183 return nullptr;
184 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800185 header->usable_size -= g_debug->pointer_offset() + reinterpret_cast<uintptr_t>(header) -
186 reinterpret_cast<uintptr_t>(orig_pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800187
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700188 if (g_debug->config().options() & FRONT_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800189 uint8_t* guard = g_debug->GetFrontGuard(header);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700190 memset(guard, g_debug->config().front_guard_value(), g_debug->config().front_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800191 }
192
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700193 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800194 uint8_t* guard = g_debug->GetRearGuard(header);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700195 memset(guard, g_debug->config().rear_guard_value(), g_debug->config().rear_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800196 // If the rear guard is enabled, set the usable size to the exact size
197 // of the allocation.
Christopher Ferris4da25032018-03-07 13:38:48 -0800198 header->usable_size = header->size;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800199 }
200
201 return g_debug->GetPointer(header);
202}
203
Tamas Berghammerac81fe82016-08-26 15:54:59 +0100204bool debug_initialize(const MallocDispatch* malloc_dispatch, int* malloc_zygote_child,
Christopher Ferris4da25032018-03-07 13:38:48 -0800205 const char* options) {
Tamas Berghammerac81fe82016-08-26 15:54:59 +0100206 if (malloc_zygote_child == nullptr || options == nullptr) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800207 return false;
208 }
Colin Cross7a28a3c2016-02-07 22:51:15 -0800209
210 InitAtfork();
211
Christopher Ferris63860cb2015-11-16 17:30:32 -0800212 g_malloc_zygote_child = malloc_zygote_child;
213
214 g_dispatch = malloc_dispatch;
215
216 if (!DebugDisableInitialize()) {
217 return false;
218 }
219
220 DebugData* debug = new DebugData();
Tamas Berghammerac81fe82016-08-26 15:54:59 +0100221 if (!debug->Initialize(options)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800222 delete debug;
223 DebugDisableFinalize();
224 return false;
225 }
226 g_debug = debug;
227
228 // Always enable the backtrace code since we will use it in a number
229 // of different error cases.
230 backtrace_startup();
231
232 return true;
233}
234
235void debug_finalize() {
236 if (g_debug == nullptr) {
237 return;
238 }
239
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700240 if (g_debug->config().options() & FREE_TRACK) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800241 PointerData::VerifyAllFreed();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800242 }
243
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700244 if (g_debug->config().options() & LEAK_TRACK) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800245 PointerData::LogLeaks();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800246 }
247
Christopher Ferris602b88c2017-08-04 13:04:04 -0700248 if ((g_debug->config().options() & BACKTRACE) && g_debug->config().backtrace_dump_on_exit()) {
249 ScopedDisableDebugCalls disable;
Christopher Ferris4da25032018-03-07 13:38:48 -0800250 debug_dump_heap(android::base::StringPrintf("%s.%d.exit.txt",
251 g_debug->config().backtrace_dump_prefix().c_str(),
252 getpid())
253 .c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700254 }
255
Christopher Ferris63860cb2015-11-16 17:30:32 -0800256 DebugDisableSet(true);
257
Colin Cross2c759912016-02-05 16:17:39 -0800258 backtrace_shutdown();
259
Christopher Ferris63860cb2015-11-16 17:30:32 -0800260 delete g_debug;
261 g_debug = nullptr;
262
263 DebugDisableFinalize();
264}
265
Christopher Ferris4da25032018-03-07 13:38:48 -0800266void debug_get_malloc_leak_info(uint8_t** info, size_t* overall_size, size_t* info_size,
267 size_t* total_memory, size_t* backtrace_size) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800268 ScopedDisableDebugCalls disable;
269
270 // Verify the arguments.
Christopher Ferris4da25032018-03-07 13:38:48 -0800271 if (info == nullptr || overall_size == nullptr || info_size == NULL || total_memory == nullptr ||
272 backtrace_size == nullptr) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800273 error_log("get_malloc_leak_info: At least one invalid parameter.");
274 return;
275 }
276
277 *info = nullptr;
278 *overall_size = 0;
279 *info_size = 0;
280 *total_memory = 0;
281 *backtrace_size = 0;
282
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700283 if (!(g_debug->config().options() & BACKTRACE)) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800284 error_log(
285 "get_malloc_leak_info: Allocations not being tracked, to enable "
286 "set the option 'backtrace'.");
Christopher Ferris63860cb2015-11-16 17:30:32 -0800287 return;
288 }
289
Christopher Ferris4da25032018-03-07 13:38:48 -0800290 PointerData::GetInfo(info, overall_size, info_size, total_memory, backtrace_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800291}
292
293void debug_free_malloc_leak_info(uint8_t* info) {
294 g_dispatch->free(info);
295}
296
Christopher Ferris55a89a42016-04-07 17:14:53 -0700297size_t debug_malloc_usable_size(void* pointer) {
298 if (DebugCallsDisabled() || pointer == nullptr) {
299 return g_dispatch->malloc_usable_size(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800300 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700301 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800302
Christopher Ferris4da25032018-03-07 13:38:48 -0800303 if (!VerifyPointer(pointer, "malloc_usable_size")) {
304 return 0;
305 }
306
307 return InternalMallocUsableSize(pointer);
Christopher Ferris55a89a42016-04-07 17:14:53 -0700308}
309
Christopher Ferris4da25032018-03-07 13:38:48 -0800310static void* InternalMalloc(size_t size) {
311 if ((g_debug->config().options() & BACKTRACE) && g_debug->pointer->ShouldDumpAndReset()) {
312 debug_dump_heap(android::base::StringPrintf(
313 "%s.%d.txt", g_debug->config().backtrace_dump_prefix().c_str(), getpid())
314 .c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700315 }
316
Colin Cross9567c7b2016-03-09 17:56:14 -0800317 if (size == 0) {
318 size = 1;
319 }
320
Christopher Ferris63860cb2015-11-16 17:30:32 -0800321 size_t real_size = size + g_debug->extra_bytes();
322 if (real_size < size) {
323 // Overflow.
324 errno = ENOMEM;
325 return nullptr;
326 }
327
Christopher Ferris4da25032018-03-07 13:38:48 -0800328 if (size > PointerInfoType::MaxSize()) {
329 errno = ENOMEM;
330 return nullptr;
331 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800332
Christopher Ferris4da25032018-03-07 13:38:48 -0800333 void* pointer;
334 if (g_debug->HeaderEnabled()) {
335 Header* header =
336 reinterpret_cast<Header*>(g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800337 if (header == nullptr) {
338 return nullptr;
339 }
340 pointer = InitHeader(header, header, size);
341 } else {
342 pointer = g_dispatch->malloc(real_size);
343 }
344
Christopher Ferris4da25032018-03-07 13:38:48 -0800345 if (pointer != nullptr) {
346 if (g_debug->TrackPointers()) {
347 PointerData::Add(pointer, size);
348 }
349
350 if (g_debug->config().options() & FILL_ON_ALLOC) {
351 size_t bytes = InternalMallocUsableSize(pointer);
352 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes();
353 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
354 memset(pointer, g_debug->config().fill_alloc_value(), bytes);
355 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800356 }
357 return pointer;
358}
359
Christopher Ferris55a89a42016-04-07 17:14:53 -0700360void* debug_malloc(size_t size) {
361 if (DebugCallsDisabled()) {
362 return g_dispatch->malloc(size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800363 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700364 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800365
Christopher Ferris4da25032018-03-07 13:38:48 -0800366 void* pointer = InternalMalloc(size);
Christopher Ferris7bd01782016-04-20 12:30:58 -0700367
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700368 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700369 g_debug->record->AddEntry(new MallocEntry(pointer, size));
370 }
371
372 return pointer;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700373}
374
Christopher Ferris4da25032018-03-07 13:38:48 -0800375static void InternalFree(void* pointer) {
376 if ((g_debug->config().options() & BACKTRACE) && g_debug->pointer->ShouldDumpAndReset()) {
377 debug_dump_heap(android::base::StringPrintf(
378 "%s.%d.txt", g_debug->config().backtrace_dump_prefix().c_str(), getpid())
379 .c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700380 }
381
Christopher Ferris63860cb2015-11-16 17:30:32 -0800382 void* free_pointer = pointer;
383 size_t bytes;
Christopher Ferrisd0919622016-03-15 22:39:39 -0700384 Header* header;
Christopher Ferris4da25032018-03-07 13:38:48 -0800385 if (g_debug->HeaderEnabled()) {
Christopher Ferrisd0919622016-03-15 22:39:39 -0700386 header = g_debug->GetHeader(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800387 free_pointer = header->orig_pointer;
388
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700389 if (g_debug->config().options() & FRONT_GUARD) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700390 if (!g_debug->front_guard->Valid(header)) {
391 g_debug->front_guard->LogFailure(header);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800392 }
393 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700394 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700395 if (!g_debug->rear_guard->Valid(header)) {
396 g_debug->rear_guard->LogFailure(header);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800397 }
398 }
399
Christopher Ferris7993b802016-01-28 18:35:05 -0800400 header->tag = DEBUG_FREE_TAG;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800401
402 bytes = header->usable_size;
403 } else {
404 bytes = g_dispatch->malloc_usable_size(pointer);
405 }
406
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700407 if (g_debug->config().options() & FILL_ON_FREE) {
408 size_t fill_bytes = g_debug->config().fill_on_free_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800409 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700410 memset(pointer, g_debug->config().fill_free_value(), bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800411 }
412
Christopher Ferris4da25032018-03-07 13:38:48 -0800413 if (g_debug->TrackPointers()) {
414 PointerData::Remove(pointer);
415 }
416
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700417 if (g_debug->config().options() & FREE_TRACK) {
Christopher Ferrisd0919622016-03-15 22:39:39 -0700418 // Do not add the allocation until we are done modifying the pointer
419 // itself. This avoids a race if a lot of threads are all doing
420 // frees at the same time and we wind up trying to really free this
421 // pointer from another thread, while still trying to free it in
422 // this function.
Christopher Ferris4da25032018-03-07 13:38:48 -0800423 pointer = PointerData::AddFreed(pointer);
424 if (pointer != nullptr) {
425 if (g_debug->HeaderEnabled()) {
426 pointer = g_debug->GetHeader(pointer)->orig_pointer;
427 }
428 g_dispatch->free(pointer);
429 }
Christopher Ferrisd0919622016-03-15 22:39:39 -0700430 } else {
431 g_dispatch->free(free_pointer);
432 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800433}
434
Christopher Ferris55a89a42016-04-07 17:14:53 -0700435void debug_free(void* pointer) {
436 if (DebugCallsDisabled() || pointer == nullptr) {
437 return g_dispatch->free(pointer);
438 }
439 ScopedDisableDebugCalls disable;
440
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700441 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700442 g_debug->record->AddEntry(new FreeEntry(pointer));
443 }
444
Christopher Ferris4da25032018-03-07 13:38:48 -0800445 if (!VerifyPointer(pointer, "free")) {
446 return;
447 }
448
449 InternalFree(pointer);
Christopher Ferris55a89a42016-04-07 17:14:53 -0700450}
451
Christopher Ferris63860cb2015-11-16 17:30:32 -0800452void* debug_memalign(size_t alignment, size_t bytes) {
453 if (DebugCallsDisabled()) {
454 return g_dispatch->memalign(alignment, bytes);
455 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700456 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800457
Colin Cross9567c7b2016-03-09 17:56:14 -0800458 if (bytes == 0) {
459 bytes = 1;
460 }
461
Christopher Ferris4da25032018-03-07 13:38:48 -0800462 if (bytes > PointerInfoType::MaxSize()) {
463 errno = ENOMEM;
464 return nullptr;
465 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800466
Christopher Ferris4da25032018-03-07 13:38:48 -0800467 void* pointer;
468 if (g_debug->HeaderEnabled()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800469 // Make the alignment a power of two.
470 if (!powerof2(alignment)) {
471 alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment);
472 }
Christopher Ferris72df6702016-02-11 15:51:31 -0800473 // Force the alignment to at least MINIMUM_ALIGNMENT_BYTES to guarantee
Christopher Ferris63860cb2015-11-16 17:30:32 -0800474 // that the header is aligned properly.
Christopher Ferris72df6702016-02-11 15:51:31 -0800475 if (alignment < MINIMUM_ALIGNMENT_BYTES) {
476 alignment = MINIMUM_ALIGNMENT_BYTES;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800477 }
478
479 // We don't have any idea what the natural alignment of
480 // the underlying native allocator is, so we always need to
481 // over allocate.
482 size_t real_size = alignment + bytes + g_debug->extra_bytes();
483 if (real_size < bytes) {
484 // Overflow.
485 errno = ENOMEM;
486 return nullptr;
487 }
488
489 pointer = g_dispatch->malloc(real_size);
490 if (pointer == nullptr) {
491 return nullptr;
492 }
493
494 uintptr_t value = reinterpret_cast<uintptr_t>(pointer) + g_debug->pointer_offset();
495 // Now align the pointer.
496 value += (-value % alignment);
497
498 Header* header = g_debug->GetHeader(reinterpret_cast<void*>(value));
499 pointer = InitHeader(header, pointer, bytes);
500 } else {
501 size_t real_size = bytes + g_debug->extra_bytes();
502 if (real_size < bytes) {
503 // Overflow.
504 errno = ENOMEM;
505 return nullptr;
506 }
507 pointer = g_dispatch->memalign(alignment, real_size);
508 }
509
Christopher Ferris4da25032018-03-07 13:38:48 -0800510 if (pointer != nullptr) {
511 if (g_debug->TrackPointers()) {
512 PointerData::Add(pointer, bytes);
513 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700514
Christopher Ferris4da25032018-03-07 13:38:48 -0800515 if (g_debug->config().options() & FILL_ON_ALLOC) {
516 size_t bytes = InternalMallocUsableSize(pointer);
517 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes();
518 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
519 memset(pointer, g_debug->config().fill_alloc_value(), bytes);
520 }
521
522 if (g_debug->config().options() & RECORD_ALLOCS) {
523 g_debug->record->AddEntry(new MemalignEntry(pointer, bytes, alignment));
524 }
Christopher Ferris7bd01782016-04-20 12:30:58 -0700525 }
526
Christopher Ferris63860cb2015-11-16 17:30:32 -0800527 return pointer;
528}
529
530void* debug_realloc(void* pointer, size_t bytes) {
531 if (DebugCallsDisabled()) {
532 return g_dispatch->realloc(pointer, bytes);
533 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700534 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800535
536 if (pointer == nullptr) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800537 pointer = InternalMalloc(bytes);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700538 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700539 g_debug->record->AddEntry(new ReallocEntry(pointer, bytes, nullptr));
540 }
541 return pointer;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800542 }
543
Christopher Ferris4da25032018-03-07 13:38:48 -0800544 if (!VerifyPointer(pointer, "realloc")) {
545 return nullptr;
546 }
547
Christopher Ferris63860cb2015-11-16 17:30:32 -0800548 if (bytes == 0) {
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700549 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700550 g_debug->record->AddEntry(new ReallocEntry(nullptr, bytes, pointer));
551 }
552
Christopher Ferris4da25032018-03-07 13:38:48 -0800553 InternalFree(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800554 return nullptr;
555 }
556
557 size_t real_size = bytes;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700558 if (g_debug->config().options() & EXPAND_ALLOC) {
559 real_size += g_debug->config().expand_alloc_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800560 if (real_size < bytes) {
561 // Overflow.
562 errno = ENOMEM;
563 return nullptr;
564 }
565 }
566
Christopher Ferris4da25032018-03-07 13:38:48 -0800567 if (bytes > PointerInfoType::MaxSize()) {
568 errno = ENOMEM;
569 return nullptr;
570 }
571
Christopher Ferris63860cb2015-11-16 17:30:32 -0800572 void* new_pointer;
573 size_t prev_size;
Christopher Ferris4da25032018-03-07 13:38:48 -0800574 if (g_debug->HeaderEnabled()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800575 // Same size, do nothing.
Christopher Ferris4da25032018-03-07 13:38:48 -0800576 Header* header = g_debug->GetHeader(pointer);
577 if (real_size == header->size) {
578 if (g_debug->TrackPointers()) {
579 // Remove and re-add so that the backtrace is updated.
580 PointerData::Remove(pointer);
581 PointerData::Add(pointer, real_size);
582 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800583 return pointer;
584 }
585
586 // Allocation is shrinking.
587 if (real_size < header->usable_size) {
588 header->size = real_size;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700589 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800590 // Don't bother allocating a smaller pointer in this case, simply
591 // change the header usable_size and reset the rear guard.
Christopher Ferris4da25032018-03-07 13:38:48 -0800592 header->usable_size = header->size;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700593 memset(g_debug->GetRearGuard(header), g_debug->config().rear_guard_value(),
594 g_debug->config().rear_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800595 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800596 if (g_debug->TrackPointers()) {
597 // Remove and re-add so that the backtrace is updated.
598 PointerData::Remove(pointer);
599 PointerData::Add(pointer, real_size);
600 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800601 return pointer;
602 }
603
604 // Allocate the new size.
Christopher Ferris4da25032018-03-07 13:38:48 -0800605 new_pointer = InternalMalloc(bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800606 if (new_pointer == nullptr) {
607 errno = ENOMEM;
608 return nullptr;
609 }
610
611 prev_size = header->usable_size;
612 memcpy(new_pointer, pointer, prev_size);
Christopher Ferris4da25032018-03-07 13:38:48 -0800613 InternalFree(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800614 } else {
Christopher Ferris4da25032018-03-07 13:38:48 -0800615 if (g_debug->TrackPointers()) {
616 PointerData::Remove(pointer);
617 }
618
Christopher Ferris63860cb2015-11-16 17:30:32 -0800619 prev_size = g_dispatch->malloc_usable_size(pointer);
620 new_pointer = g_dispatch->realloc(pointer, real_size);
621 if (new_pointer == nullptr) {
622 return nullptr;
623 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800624
625 if (g_debug->TrackPointers()) {
626 PointerData::Add(new_pointer, real_size);
627 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800628 }
629
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700630 if (g_debug->config().options() & FILL_ON_ALLOC) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800631 size_t bytes = InternalMallocUsableSize(new_pointer);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700632 if (bytes > g_debug->config().fill_on_alloc_bytes()) {
633 bytes = g_debug->config().fill_on_alloc_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800634 }
635 if (bytes > prev_size) {
636 memset(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(new_pointer) + prev_size),
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700637 g_debug->config().fill_alloc_value(), bytes - prev_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800638 }
639 }
640
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700641 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700642 g_debug->record->AddEntry(new ReallocEntry(new_pointer, bytes, pointer));
643 }
644
Christopher Ferris63860cb2015-11-16 17:30:32 -0800645 return new_pointer;
646}
647
648void* debug_calloc(size_t nmemb, size_t bytes) {
649 if (DebugCallsDisabled()) {
650 return g_dispatch->calloc(nmemb, bytes);
651 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700652 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800653
Colin Cross7877df62016-03-10 13:01:27 -0800654 size_t size;
655 if (__builtin_mul_overflow(nmemb, bytes, &size)) {
656 // Overflow
657 errno = ENOMEM;
658 return nullptr;
659 }
660
Colin Cross9567c7b2016-03-09 17:56:14 -0800661 if (size == 0) {
662 size = 1;
663 }
664
Colin Cross7877df62016-03-10 13:01:27 -0800665 size_t real_size;
666 if (__builtin_add_overflow(size, g_debug->extra_bytes(), &real_size)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800667 // Overflow.
668 errno = ENOMEM;
669 return nullptr;
670 }
671
Christopher Ferris4da25032018-03-07 13:38:48 -0800672 if (real_size > PointerInfoType::MaxSize()) {
673 errno = ENOMEM;
674 return nullptr;
675 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800676
Christopher Ferris4da25032018-03-07 13:38:48 -0800677 void* pointer;
678 if (g_debug->HeaderEnabled()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800679 // Need to guarantee the alignment of the header.
Christopher Ferris4da25032018-03-07 13:38:48 -0800680 Header* header =
681 reinterpret_cast<Header*>(g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800682 if (header == nullptr) {
683 return nullptr;
684 }
685 memset(header, 0, g_dispatch->malloc_usable_size(header));
Christopher Ferris7bd01782016-04-20 12:30:58 -0700686 pointer = InitHeader(header, header, size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800687 } else {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700688 pointer = g_dispatch->calloc(1, real_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800689 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800690
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700691 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700692 g_debug->record->AddEntry(new CallocEntry(pointer, bytes, nmemb));
693 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800694
695 if (pointer != nullptr && g_debug->TrackPointers()) {
696 PointerData::Add(pointer, size);
697 }
Christopher Ferris7bd01782016-04-20 12:30:58 -0700698 return pointer;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800699}
700
701struct mallinfo debug_mallinfo() {
702 return g_dispatch->mallinfo();
703}
704
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700705int debug_mallopt(int param, int value) {
706 return g_dispatch->mallopt(param, value);
707}
708
Christopher Ferriscae21a92018-02-05 18:14:55 -0800709void* debug_aligned_alloc(size_t alignment, size_t size) {
710 if (DebugCallsDisabled()) {
711 return g_dispatch->aligned_alloc(alignment, size);
712 }
713 if (!powerof2(alignment)) {
714 errno = EINVAL;
715 return nullptr;
716 }
717 return debug_memalign(alignment, size);
718}
719
Christopher Ferris63860cb2015-11-16 17:30:32 -0800720int debug_posix_memalign(void** memptr, size_t alignment, size_t size) {
721 if (DebugCallsDisabled()) {
722 return g_dispatch->posix_memalign(memptr, alignment, size);
723 }
724
725 if (!powerof2(alignment)) {
726 return EINVAL;
727 }
728 int saved_errno = errno;
729 *memptr = debug_memalign(alignment, size);
730 errno = saved_errno;
731 return (*memptr != nullptr) ? 0 : ENOMEM;
732}
733
Christopher Ferris4da25032018-03-07 13:38:48 -0800734int debug_iterate(uintptr_t base, size_t size, void (*callback)(uintptr_t, size_t, void*),
735 void* arg) {
736 if (g_debug->TrackPointers()) {
737 // Since malloc is disabled, don't bother acquiring any locks.
738 for (auto it = PointerData::begin(); it != PointerData::end(); ++it) {
739 callback(it->first, InternalMallocUsableSize(reinterpret_cast<void*>(it->first)), arg);
740 }
741 return 0;
742 }
Colin Cross869691c2016-01-29 12:48:18 -0800743
Christopher Ferris4da25032018-03-07 13:38:48 -0800744 // An option that adds a header will add pointer tracking, so no need to
745 // check if headers are enabled.
746 return g_dispatch->iterate(base, size, callback, arg);
Colin Cross869691c2016-01-29 12:48:18 -0800747}
748
749void debug_malloc_disable() {
750 g_dispatch->malloc_disable();
Christopher Ferris4da25032018-03-07 13:38:48 -0800751 if (g_debug->pointer) {
752 g_debug->pointer->PrepareFork();
Colin Cross869691c2016-01-29 12:48:18 -0800753 }
754}
755
756void debug_malloc_enable() {
Christopher Ferris4da25032018-03-07 13:38:48 -0800757 if (g_debug->pointer) {
758 g_debug->pointer->PostForkParent();
Colin Cross869691c2016-01-29 12:48:18 -0800759 }
760 g_dispatch->malloc_enable();
761}
762
Christopher Ferris4da25032018-03-07 13:38:48 -0800763ssize_t debug_malloc_backtrace(void* pointer, uintptr_t* frames, size_t max_frames) {
Colin Cross2d4721c2016-02-02 11:57:54 -0800764 if (DebugCallsDisabled() || pointer == nullptr) {
765 return 0;
766 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700767 ScopedDisableDebugCalls disable;
Colin Cross2d4721c2016-02-02 11:57:54 -0800768
Christopher Ferris4da25032018-03-07 13:38:48 -0800769 if (!(g_debug->config().options() & BACKTRACE)) {
770 return 0;
Colin Cross2d4721c2016-02-02 11:57:54 -0800771 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800772 return PointerData::GetFrames(pointer, frames, max_frames);
Colin Cross2d4721c2016-02-02 11:57:54 -0800773}
774
Christopher Ferris63860cb2015-11-16 17:30:32 -0800775#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
776void* debug_pvalloc(size_t bytes) {
777 if (DebugCallsDisabled()) {
778 return g_dispatch->pvalloc(bytes);
779 }
780
781 size_t pagesize = getpagesize();
Dan Alberta613d0d2017-10-05 16:39:33 -0700782 size_t size = __BIONIC_ALIGN(bytes, pagesize);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800783 if (size < bytes) {
784 // Overflow
785 errno = ENOMEM;
786 return nullptr;
787 }
788 return debug_memalign(pagesize, size);
789}
790
791void* debug_valloc(size_t size) {
792 if (DebugCallsDisabled()) {
793 return g_dispatch->valloc(size);
794 }
795 return debug_memalign(getpagesize(), size);
796}
797#endif
Christopher Ferris602b88c2017-08-04 13:04:04 -0700798
799static std::mutex g_dump_lock;
800
801bool debug_dump_heap(const char* file_name) {
802 ScopedDisableDebugCalls disable;
803
804 std::lock_guard<std::mutex> guard(g_dump_lock);
805
806 FILE* fp = fopen(file_name, "w+e");
807 if (fp == nullptr) {
808 error_log("Unable to create file: %s", file_name);
809 return false;
810 }
811 error_log("Dumping to file: %s\n", file_name);
812
813 if (!(g_debug->config().options() & BACKTRACE)) {
814 fprintf(fp, "Native heap dump not available. To enable, run these commands (requires root):\n");
815 fprintf(fp, "# adb shell stop\n");
816 fprintf(fp, "# adb shell setprop libc.debug.malloc.options backtrace\n");
817 fprintf(fp, "# adb shell start\n");
818 fclose(fp);
819 return false;
820 }
821
822 fprintf(fp, "Android Native Heap Dump v1.0\n\n");
823
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");
834 fclose(fp);
835 return true;
836}