blob: 568192d69ce9279e9c718df04525d7147f0ebfec [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
37#include <vector>
38
39#include <private/bionic_malloc_dispatch.h>
40
41#include "backtrace.h"
Christopher Ferris72df6702016-02-11 15:51:31 -080042#include "Config.h"
Christopher Ferris63860cb2015-11-16 17:30:32 -080043#include "DebugData.h"
44#include "debug_disable.h"
45#include "debug_log.h"
46#include "malloc_debug.h"
47
48// ------------------------------------------------------------------------
49// Global Data
50// ------------------------------------------------------------------------
51DebugData* g_debug;
52
53int* g_malloc_zygote_child;
54
55const MallocDispatch* g_dispatch;
56// ------------------------------------------------------------------------
57
58// ------------------------------------------------------------------------
59// Use C style prototypes for all exported functions. This makes it easy
60// to do dlsym lookups during libc initialization when malloc debug
61// is enabled.
62// ------------------------------------------------------------------------
63__BEGIN_DECLS
64
65bool debug_initialize(const MallocDispatch* malloc_dispatch, int* malloc_zygote_child);
66void debug_finalize();
67void debug_get_malloc_leak_info(
68 uint8_t** info, size_t* overall_size, size_t* info_size, size_t* total_memory,
69 size_t* backtrace_size);
Colin Cross2d4721c2016-02-02 11:57:54 -080070ssize_t debug_malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count);
Christopher Ferris63860cb2015-11-16 17:30:32 -080071void debug_free_malloc_leak_info(uint8_t* info);
72size_t debug_malloc_usable_size(void* pointer);
73void* debug_malloc(size_t size);
74void debug_free(void* pointer);
75void* debug_memalign(size_t alignment, size_t bytes);
76void* debug_realloc(void* pointer, size_t bytes);
77void* debug_calloc(size_t nmemb, size_t bytes);
78struct mallinfo debug_mallinfo();
79int debug_posix_memalign(void** memptr, size_t alignment, size_t size);
Colin Cross869691c2016-01-29 12:48:18 -080080int debug_iterate(uintptr_t base, size_t size,
81 void (*callback)(uintptr_t base, size_t size, void* arg), void* arg);
82void debug_malloc_disable();
83void debug_malloc_enable();
Christopher Ferris63860cb2015-11-16 17:30:32 -080084
85#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
86void* debug_pvalloc(size_t bytes);
87void* debug_valloc(size_t size);
88#endif
89
90__END_DECLS
91// ------------------------------------------------------------------------
92
Colin Cross7a28a3c2016-02-07 22:51:15 -080093static void InitAtfork() {
94 static pthread_once_t atfork_init = PTHREAD_ONCE_INIT;
95 pthread_once(&atfork_init, [](){
96 pthread_atfork(
97 [](){
98 if (g_debug != nullptr) {
99 g_debug->PrepareFork();
100 }
101 },
102 [](){
103 if (g_debug != nullptr) {
104 g_debug->PostForkParent();
105 }
106 },
107 [](){
108 if (g_debug != nullptr) {
109 g_debug->PostForkChild();
110 }
111 }
112 );
113 });
114}
Christopher Ferris63860cb2015-11-16 17:30:32 -0800115static void LogTagError(const Header* header, const void* pointer, const char* name) {
116 ScopedDisableDebugCalls disable;
117
118 error_log(LOG_DIVIDER);
Christopher Ferris7993b802016-01-28 18:35:05 -0800119 if (header->tag == DEBUG_FREE_TAG) {
120 error_log("+++ ALLOCATION %p USED AFTER FREE (%s)", pointer, name);
121 if (g_debug->config().options & FREE_TRACK) {
122 g_debug->free_track->LogBacktrace(header);
123 }
124 } else {
125 error_log("+++ ALLOCATION %p HAS INVALID TAG %" PRIx32 " (%s)", pointer, header->tag, name);
126 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800127 error_log("Backtrace at time of failure:");
128 std::vector<uintptr_t> frames(64);
129 size_t frame_num = backtrace_get(frames.data(), frames.size());
130 frames.resize(frame_num);
131 backtrace_log(frames.data(), frames.size());
132 error_log(LOG_DIVIDER);
133}
134
135static void* InitHeader(Header* header, void* orig_pointer, size_t size) {
136 header->tag = DEBUG_TAG;
137 header->orig_pointer = orig_pointer;
138 header->size = size;
139 if (*g_malloc_zygote_child) {
140 header->set_zygote();
141 }
142 header->usable_size = g_dispatch->malloc_usable_size(orig_pointer);
143 if (header->usable_size == 0) {
144 g_dispatch->free(orig_pointer);
145 return nullptr;
146 }
147 header->usable_size -= g_debug->pointer_offset() +
148 reinterpret_cast<uintptr_t>(orig_pointer) - reinterpret_cast<uintptr_t>(header);
149
150 if (g_debug->config().options & FRONT_GUARD) {
151 uint8_t* guard = g_debug->GetFrontGuard(header);
152 memset(guard, g_debug->config().front_guard_value, g_debug->config().front_guard_bytes);
153 }
154
155 if (g_debug->config().options & REAR_GUARD) {
156 uint8_t* guard = g_debug->GetRearGuard(header);
157 memset(guard, g_debug->config().rear_guard_value, g_debug->config().rear_guard_bytes);
158 // If the rear guard is enabled, set the usable size to the exact size
159 // of the allocation.
160 header->usable_size = header->real_size();
161 }
162
163 bool backtrace_found = false;
164 if (g_debug->config().options & BACKTRACE) {
165 BacktraceHeader* back_header = g_debug->GetAllocBacktrace(header);
166 if (g_debug->backtrace->enabled()) {
Colin Cross2d4721c2016-02-02 11:57:54 -0800167 ScopedDisableDebugCalls disable;
Christopher Ferris7993b802016-01-28 18:35:05 -0800168 back_header->num_frames = backtrace_get(
169 &back_header->frames[0], g_debug->config().backtrace_frames);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800170 backtrace_found = back_header->num_frames > 0;
171 } else {
172 back_header->num_frames = 0;
173 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800174 }
175
176 if (g_debug->config().options & TRACK_ALLOCS) {
177 g_debug->track->Add(header, backtrace_found);
178 }
179
180 return g_debug->GetPointer(header);
181}
182
183bool debug_initialize(const MallocDispatch* malloc_dispatch, int* malloc_zygote_child) {
184 if (malloc_zygote_child == nullptr) {
185 return false;
186 }
Colin Cross7a28a3c2016-02-07 22:51:15 -0800187
188 InitAtfork();
189
Christopher Ferris63860cb2015-11-16 17:30:32 -0800190 g_malloc_zygote_child = malloc_zygote_child;
191
192 g_dispatch = malloc_dispatch;
193
194 if (!DebugDisableInitialize()) {
195 return false;
196 }
197
198 DebugData* debug = new DebugData();
199 if (!debug->Initialize()) {
200 delete debug;
201 DebugDisableFinalize();
202 return false;
203 }
204 g_debug = debug;
205
206 // Always enable the backtrace code since we will use it in a number
207 // of different error cases.
208 backtrace_startup();
209
210 return true;
211}
212
213void debug_finalize() {
214 if (g_debug == nullptr) {
215 return;
216 }
217
218 if (g_debug->config().options & FREE_TRACK) {
219 g_debug->free_track->VerifyAll(*g_debug);
220 }
221
222 if (g_debug->config().options & LEAK_TRACK) {
223 g_debug->track->DisplayLeaks(*g_debug);
224 }
225
Christopher Ferris63860cb2015-11-16 17:30:32 -0800226 DebugDisableSet(true);
227
Colin Cross2c759912016-02-05 16:17:39 -0800228 backtrace_shutdown();
229
Christopher Ferris63860cb2015-11-16 17:30:32 -0800230 delete g_debug;
231 g_debug = nullptr;
232
233 DebugDisableFinalize();
234}
235
236void debug_get_malloc_leak_info(uint8_t** info, size_t* overall_size,
237 size_t* info_size, size_t* total_memory, size_t* backtrace_size) {
238 ScopedDisableDebugCalls disable;
239
240 // Verify the arguments.
241 if (info == nullptr || overall_size == nullptr || info_size == NULL ||
242 total_memory == nullptr || backtrace_size == nullptr) {
243 error_log("get_malloc_leak_info: At least one invalid parameter.");
244 return;
245 }
246
247 *info = nullptr;
248 *overall_size = 0;
249 *info_size = 0;
250 *total_memory = 0;
251 *backtrace_size = 0;
252
253 if (!(g_debug->config().options & BACKTRACE)) {
254 error_log("get_malloc_leak_info: Allocations not being tracked, to enable "
255 "set the option 'backtrace'.");
256 return;
257 }
258
259 g_debug->track->GetInfo(*g_debug, info, overall_size, info_size, total_memory, backtrace_size);
260}
261
262void debug_free_malloc_leak_info(uint8_t* info) {
263 g_dispatch->free(info);
264}
265
266size_t debug_malloc_usable_size(void* pointer) {
267 if (DebugCallsDisabled() || !g_debug->need_header() || pointer == nullptr) {
268 return g_dispatch->malloc_usable_size(pointer);
269 }
270
271 Header* header = g_debug->GetHeader(pointer);
272 if (header->tag != DEBUG_TAG) {
273 LogTagError(header, pointer, "malloc_usable_size");
274 return 0;
275 }
276
277 return header->usable_size;
278}
279
280void* debug_malloc(size_t size) {
281 if (DebugCallsDisabled()) {
282 return g_dispatch->malloc(size);
283 }
284
Colin Cross9567c7b2016-03-09 17:56:14 -0800285 if (size == 0) {
286 size = 1;
287 }
288
Christopher Ferris63860cb2015-11-16 17:30:32 -0800289 size_t real_size = size + g_debug->extra_bytes();
290 if (real_size < size) {
291 // Overflow.
292 errno = ENOMEM;
293 return nullptr;
294 }
295
296 void* pointer;
297 if (g_debug->need_header()) {
298 if (size > Header::max_size()) {
299 errno = ENOMEM;
300 return nullptr;
301 }
302
Christopher Ferris72df6702016-02-11 15:51:31 -0800303 Header* header = reinterpret_cast<Header*>(
304 g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800305 if (header == nullptr) {
306 return nullptr;
307 }
308 pointer = InitHeader(header, header, size);
309 } else {
310 pointer = g_dispatch->malloc(real_size);
311 }
312
313 if (pointer != nullptr && g_debug->config().options & FILL_ON_ALLOC) {
314 size_t bytes = debug_malloc_usable_size(pointer);
315 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes;
316 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
317 memset(pointer, g_debug->config().fill_alloc_value, bytes);
318 }
319 return pointer;
320}
321
322void debug_free(void* pointer) {
323 if (DebugCallsDisabled() || pointer == nullptr) {
324 return g_dispatch->free(pointer);
325 }
326
327 void* free_pointer = pointer;
328 size_t bytes;
329 if (g_debug->need_header()) {
330 Header* header = g_debug->GetHeader(pointer);
331 if (header->tag != DEBUG_TAG) {
332 LogTagError(header, pointer, "free");
333 return;
334 }
335 free_pointer = header->orig_pointer;
336
337 if (g_debug->config().options & FRONT_GUARD) {
338 if (!g_debug->front_guard->Valid(*g_debug, header)) {
339 g_debug->front_guard->LogFailure(*g_debug, header);
340 }
341 }
342 if (g_debug->config().options & REAR_GUARD) {
343 if (!g_debug->rear_guard->Valid(*g_debug, header)) {
344 g_debug->rear_guard->LogFailure(*g_debug, header);
345 }
346 }
347
348 if (g_debug->config().options & TRACK_ALLOCS) {
349 bool backtrace_found = false;
350 if (g_debug->config().options & BACKTRACE) {
351 BacktraceHeader* back_header = g_debug->GetAllocBacktrace(header);
352 backtrace_found = back_header->num_frames > 0;
353 }
354 g_debug->track->Remove(header, backtrace_found);
355 }
356
357 if (g_debug->config().options & FREE_TRACK) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800358 g_debug->free_track->Add(*g_debug, header);
359
360 // Do not free this pointer just yet.
361 free_pointer = nullptr;
362 }
Christopher Ferris7993b802016-01-28 18:35:05 -0800363 header->tag = DEBUG_FREE_TAG;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800364
365 bytes = header->usable_size;
366 } else {
367 bytes = g_dispatch->malloc_usable_size(pointer);
368 }
369
370 if (g_debug->config().options & FILL_ON_FREE) {
371 size_t fill_bytes = g_debug->config().fill_on_free_bytes;
372 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
373 memset(pointer, g_debug->config().fill_free_value, bytes);
374 }
375
376 g_dispatch->free(free_pointer);
377}
378
379void* debug_memalign(size_t alignment, size_t bytes) {
380 if (DebugCallsDisabled()) {
381 return g_dispatch->memalign(alignment, bytes);
382 }
383
Colin Cross9567c7b2016-03-09 17:56:14 -0800384 if (bytes == 0) {
385 bytes = 1;
386 }
387
Christopher Ferris63860cb2015-11-16 17:30:32 -0800388 void* pointer;
389 if (g_debug->need_header()) {
390 if (bytes > Header::max_size()) {
391 errno = ENOMEM;
392 return nullptr;
393 }
394
395 // Make the alignment a power of two.
396 if (!powerof2(alignment)) {
397 alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment);
398 }
Christopher Ferris72df6702016-02-11 15:51:31 -0800399 // Force the alignment to at least MINIMUM_ALIGNMENT_BYTES to guarantee
Christopher Ferris63860cb2015-11-16 17:30:32 -0800400 // that the header is aligned properly.
Christopher Ferris72df6702016-02-11 15:51:31 -0800401 if (alignment < MINIMUM_ALIGNMENT_BYTES) {
402 alignment = MINIMUM_ALIGNMENT_BYTES;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800403 }
404
405 // We don't have any idea what the natural alignment of
406 // the underlying native allocator is, so we always need to
407 // over allocate.
408 size_t real_size = alignment + bytes + g_debug->extra_bytes();
409 if (real_size < bytes) {
410 // Overflow.
411 errno = ENOMEM;
412 return nullptr;
413 }
414
415 pointer = g_dispatch->malloc(real_size);
416 if (pointer == nullptr) {
417 return nullptr;
418 }
419
420 uintptr_t value = reinterpret_cast<uintptr_t>(pointer) + g_debug->pointer_offset();
421 // Now align the pointer.
422 value += (-value % alignment);
423
424 Header* header = g_debug->GetHeader(reinterpret_cast<void*>(value));
425 pointer = InitHeader(header, pointer, bytes);
426 } else {
427 size_t real_size = bytes + g_debug->extra_bytes();
428 if (real_size < bytes) {
429 // Overflow.
430 errno = ENOMEM;
431 return nullptr;
432 }
433 pointer = g_dispatch->memalign(alignment, real_size);
434 }
435
436 if (pointer != nullptr && g_debug->config().options & FILL_ON_ALLOC) {
437 size_t bytes = debug_malloc_usable_size(pointer);
438 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes;
439 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
440 memset(pointer, g_debug->config().fill_alloc_value, bytes);
441 }
442 return pointer;
443}
444
445void* debug_realloc(void* pointer, size_t bytes) {
446 if (DebugCallsDisabled()) {
447 return g_dispatch->realloc(pointer, bytes);
448 }
449
450 if (pointer == nullptr) {
451 return debug_malloc(bytes);
452 }
453
454 if (bytes == 0) {
455 debug_free(pointer);
456 return nullptr;
457 }
458
459 size_t real_size = bytes;
460 if (g_debug->config().options & EXPAND_ALLOC) {
461 real_size += g_debug->config().expand_alloc_bytes;
462 if (real_size < bytes) {
463 // Overflow.
464 errno = ENOMEM;
465 return nullptr;
466 }
467 }
468
469 void* new_pointer;
470 size_t prev_size;
471 if (g_debug->need_header()) {
472 if (bytes > Header::max_size()) {
473 errno = ENOMEM;
474 return nullptr;
475 }
476
477 Header* header = g_debug->GetHeader(pointer);
478 if (header->tag != DEBUG_TAG) {
479 LogTagError(header, pointer, "realloc");
480 return nullptr;
481 }
482
483 // Same size, do nothing.
484 if (real_size == header->real_size()) {
485 return pointer;
486 }
487
488 // Allocation is shrinking.
489 if (real_size < header->usable_size) {
490 header->size = real_size;
491 if (*g_malloc_zygote_child) {
492 header->set_zygote();
493 }
494 if (g_debug->config().options & REAR_GUARD) {
495 // Don't bother allocating a smaller pointer in this case, simply
496 // change the header usable_size and reset the rear guard.
497 header->usable_size = header->real_size();
498 memset(g_debug->GetRearGuard(header), g_debug->config().rear_guard_value,
499 g_debug->config().rear_guard_bytes);
500 }
501 return pointer;
502 }
503
504 // Allocate the new size.
505 new_pointer = debug_malloc(bytes);
506 if (new_pointer == nullptr) {
507 errno = ENOMEM;
508 return nullptr;
509 }
510
511 prev_size = header->usable_size;
512 memcpy(new_pointer, pointer, prev_size);
513 debug_free(pointer);
514 } else {
515 prev_size = g_dispatch->malloc_usable_size(pointer);
516 new_pointer = g_dispatch->realloc(pointer, real_size);
517 if (new_pointer == nullptr) {
518 return nullptr;
519 }
520 }
521
522 if (g_debug->config().options & FILL_ON_ALLOC) {
523 size_t bytes = debug_malloc_usable_size(new_pointer);
524 if (bytes > g_debug->config().fill_on_alloc_bytes) {
525 bytes = g_debug->config().fill_on_alloc_bytes;
526 }
527 if (bytes > prev_size) {
528 memset(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(new_pointer) + prev_size),
529 g_debug->config().fill_alloc_value, bytes - prev_size);
530 }
531 }
532
533 return new_pointer;
534}
535
536void* debug_calloc(size_t nmemb, size_t bytes) {
537 if (DebugCallsDisabled()) {
538 return g_dispatch->calloc(nmemb, bytes);
539 }
540
Colin Cross7877df62016-03-10 13:01:27 -0800541 size_t size;
542 if (__builtin_mul_overflow(nmemb, bytes, &size)) {
543 // Overflow
544 errno = ENOMEM;
545 return nullptr;
546 }
547
Colin Cross9567c7b2016-03-09 17:56:14 -0800548 if (size == 0) {
549 size = 1;
550 }
551
Colin Cross7877df62016-03-10 13:01:27 -0800552 size_t real_size;
553 if (__builtin_add_overflow(size, g_debug->extra_bytes(), &real_size)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800554 // Overflow.
555 errno = ENOMEM;
556 return nullptr;
557 }
558
559 if (g_debug->need_header()) {
560 // The above check will guarantee the multiply will not overflow.
Colin Cross9567c7b2016-03-09 17:56:14 -0800561 if (size > Header::max_size()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800562 errno = ENOMEM;
563 return nullptr;
564 }
565
566 // Need to guarantee the alignment of the header.
Christopher Ferris72df6702016-02-11 15:51:31 -0800567 Header* header = reinterpret_cast<Header*>(
568 g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800569 if (header == nullptr) {
570 return nullptr;
571 }
572 memset(header, 0, g_dispatch->malloc_usable_size(header));
Colin Cross9567c7b2016-03-09 17:56:14 -0800573 return InitHeader(header, header, size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800574 } else {
575 return g_dispatch->calloc(1, real_size);
576 }
577}
578
579struct mallinfo debug_mallinfo() {
580 return g_dispatch->mallinfo();
581}
582
583int debug_posix_memalign(void** memptr, size_t alignment, size_t size) {
584 if (DebugCallsDisabled()) {
585 return g_dispatch->posix_memalign(memptr, alignment, size);
586 }
587
588 if (!powerof2(alignment)) {
589 return EINVAL;
590 }
591 int saved_errno = errno;
592 *memptr = debug_memalign(alignment, size);
593 errno = saved_errno;
594 return (*memptr != nullptr) ? 0 : ENOMEM;
595}
596
Colin Cross869691c2016-01-29 12:48:18 -0800597int debug_iterate(uintptr_t base, size_t size,
598 void (*callback)(uintptr_t base, size_t size, void* arg), void* arg) {
599 // Can't allocate, malloc is disabled
600 // Manual capture of the arguments to pass to the lambda below as void* arg
601 struct iterate_ctx {
602 decltype(callback) callback;
603 decltype(arg) arg;
604 } ctx = { callback, arg };
605
606 return g_dispatch->iterate(base, size,
607 [](uintptr_t base, size_t size, void* arg) {
608 const iterate_ctx* ctx = reinterpret_cast<iterate_ctx*>(arg);
609 const void* pointer = reinterpret_cast<void*>(base);
610 if (g_debug->need_header()) {
611 const Header* header = reinterpret_cast<const Header*>(pointer);
612 if (g_debug->config().options & TRACK_ALLOCS) {
613 if (g_debug->track->Contains(header)) {
614 // Return just the body of the allocation if we're sure the header exists
615 ctx->callback(reinterpret_cast<uintptr_t>(g_debug->GetPointer(header)),
Colin Crossbaa7c6f2016-03-09 16:33:44 -0800616 header->usable_size, ctx->arg);
Colin Cross869691c2016-01-29 12:48:18 -0800617 return;
618 }
619 }
620 }
621 // Fall back to returning the whole allocation
622 ctx->callback(base, size, ctx->arg);
623 }, &ctx);
624}
625
626void debug_malloc_disable() {
627 g_dispatch->malloc_disable();
628 if (g_debug->track) {
629 g_debug->track->PrepareFork();
630 }
631}
632
633void debug_malloc_enable() {
634 if (g_debug->track) {
635 g_debug->track->PostForkParent();
636 }
637 g_dispatch->malloc_enable();
638}
639
Colin Cross2d4721c2016-02-02 11:57:54 -0800640ssize_t debug_malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count) {
641 if (DebugCallsDisabled() || pointer == nullptr) {
642 return 0;
643 }
644
645 if (g_debug->need_header()) {
646 Header* header;
647 if (g_debug->config().options & TRACK_ALLOCS) {
648 header = g_debug->GetHeader(pointer);
649 if (!g_debug->track->Contains(header)) {
650 return 0;
651 }
652 } else {
653 header = reinterpret_cast<Header*>(pointer);
654 }
655 if (header->tag != DEBUG_TAG) {
656 return 0;
657 }
658 if (g_debug->config().options & BACKTRACE) {
659 BacktraceHeader* back_header = g_debug->GetAllocBacktrace(header);
660 if (back_header->num_frames > 0) {
661 if (frame_count > back_header->num_frames) {
662 frame_count = back_header->num_frames;
663 }
664 memcpy(frames, &back_header->frames[0], frame_count * sizeof(uintptr_t));
665 return frame_count;
666 }
667 }
668 }
669
670 return 0;
671}
672
Christopher Ferris63860cb2015-11-16 17:30:32 -0800673#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
674void* debug_pvalloc(size_t bytes) {
675 if (DebugCallsDisabled()) {
676 return g_dispatch->pvalloc(bytes);
677 }
678
679 size_t pagesize = getpagesize();
680 size_t size = BIONIC_ALIGN(bytes, pagesize);
681 if (size < bytes) {
682 // Overflow
683 errno = ENOMEM;
684 return nullptr;
685 }
686 return debug_memalign(pagesize, size);
687}
688
689void* debug_valloc(size_t size) {
690 if (DebugCallsDisabled()) {
691 return g_dispatch->valloc(size);
692 }
693 return debug_memalign(getpagesize(), size);
694}
695#endif