blob: e079c7e66509b8036104e2f3ad56fabcc76d1b45 [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
226 backtrace_shutdown();
227
228 DebugDisableSet(true);
229
230 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
285 size_t real_size = size + g_debug->extra_bytes();
286 if (real_size < size) {
287 // Overflow.
288 errno = ENOMEM;
289 return nullptr;
290 }
291
292 void* pointer;
293 if (g_debug->need_header()) {
294 if (size > Header::max_size()) {
295 errno = ENOMEM;
296 return nullptr;
297 }
298
Christopher Ferris72df6702016-02-11 15:51:31 -0800299 Header* header = reinterpret_cast<Header*>(
300 g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800301 if (header == nullptr) {
302 return nullptr;
303 }
304 pointer = InitHeader(header, header, size);
305 } else {
306 pointer = g_dispatch->malloc(real_size);
307 }
308
309 if (pointer != nullptr && g_debug->config().options & FILL_ON_ALLOC) {
310 size_t bytes = debug_malloc_usable_size(pointer);
311 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes;
312 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
313 memset(pointer, g_debug->config().fill_alloc_value, bytes);
314 }
315 return pointer;
316}
317
318void debug_free(void* pointer) {
319 if (DebugCallsDisabled() || pointer == nullptr) {
320 return g_dispatch->free(pointer);
321 }
322
323 void* free_pointer = pointer;
324 size_t bytes;
325 if (g_debug->need_header()) {
326 Header* header = g_debug->GetHeader(pointer);
327 if (header->tag != DEBUG_TAG) {
328 LogTagError(header, pointer, "free");
329 return;
330 }
331 free_pointer = header->orig_pointer;
332
333 if (g_debug->config().options & FRONT_GUARD) {
334 if (!g_debug->front_guard->Valid(*g_debug, header)) {
335 g_debug->front_guard->LogFailure(*g_debug, header);
336 }
337 }
338 if (g_debug->config().options & REAR_GUARD) {
339 if (!g_debug->rear_guard->Valid(*g_debug, header)) {
340 g_debug->rear_guard->LogFailure(*g_debug, header);
341 }
342 }
343
344 if (g_debug->config().options & TRACK_ALLOCS) {
345 bool backtrace_found = false;
346 if (g_debug->config().options & BACKTRACE) {
347 BacktraceHeader* back_header = g_debug->GetAllocBacktrace(header);
348 backtrace_found = back_header->num_frames > 0;
349 }
350 g_debug->track->Remove(header, backtrace_found);
351 }
352
353 if (g_debug->config().options & FREE_TRACK) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800354 g_debug->free_track->Add(*g_debug, header);
355
356 // Do not free this pointer just yet.
357 free_pointer = nullptr;
358 }
Christopher Ferris7993b802016-01-28 18:35:05 -0800359 header->tag = DEBUG_FREE_TAG;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800360
361 bytes = header->usable_size;
362 } else {
363 bytes = g_dispatch->malloc_usable_size(pointer);
364 }
365
366 if (g_debug->config().options & FILL_ON_FREE) {
367 size_t fill_bytes = g_debug->config().fill_on_free_bytes;
368 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
369 memset(pointer, g_debug->config().fill_free_value, bytes);
370 }
371
372 g_dispatch->free(free_pointer);
373}
374
375void* debug_memalign(size_t alignment, size_t bytes) {
376 if (DebugCallsDisabled()) {
377 return g_dispatch->memalign(alignment, bytes);
378 }
379
380 void* pointer;
381 if (g_debug->need_header()) {
382 if (bytes > Header::max_size()) {
383 errno = ENOMEM;
384 return nullptr;
385 }
386
387 // Make the alignment a power of two.
388 if (!powerof2(alignment)) {
389 alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment);
390 }
Christopher Ferris72df6702016-02-11 15:51:31 -0800391 // Force the alignment to at least MINIMUM_ALIGNMENT_BYTES to guarantee
Christopher Ferris63860cb2015-11-16 17:30:32 -0800392 // that the header is aligned properly.
Christopher Ferris72df6702016-02-11 15:51:31 -0800393 if (alignment < MINIMUM_ALIGNMENT_BYTES) {
394 alignment = MINIMUM_ALIGNMENT_BYTES;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800395 }
396
397 // We don't have any idea what the natural alignment of
398 // the underlying native allocator is, so we always need to
399 // over allocate.
400 size_t real_size = alignment + bytes + g_debug->extra_bytes();
401 if (real_size < bytes) {
402 // Overflow.
403 errno = ENOMEM;
404 return nullptr;
405 }
406
407 pointer = g_dispatch->malloc(real_size);
408 if (pointer == nullptr) {
409 return nullptr;
410 }
411
412 uintptr_t value = reinterpret_cast<uintptr_t>(pointer) + g_debug->pointer_offset();
413 // Now align the pointer.
414 value += (-value % alignment);
415
416 Header* header = g_debug->GetHeader(reinterpret_cast<void*>(value));
417 pointer = InitHeader(header, pointer, bytes);
418 } else {
419 size_t real_size = bytes + g_debug->extra_bytes();
420 if (real_size < bytes) {
421 // Overflow.
422 errno = ENOMEM;
423 return nullptr;
424 }
425 pointer = g_dispatch->memalign(alignment, real_size);
426 }
427
428 if (pointer != nullptr && g_debug->config().options & FILL_ON_ALLOC) {
429 size_t bytes = debug_malloc_usable_size(pointer);
430 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes;
431 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
432 memset(pointer, g_debug->config().fill_alloc_value, bytes);
433 }
434 return pointer;
435}
436
437void* debug_realloc(void* pointer, size_t bytes) {
438 if (DebugCallsDisabled()) {
439 return g_dispatch->realloc(pointer, bytes);
440 }
441
442 if (pointer == nullptr) {
443 return debug_malloc(bytes);
444 }
445
446 if (bytes == 0) {
447 debug_free(pointer);
448 return nullptr;
449 }
450
451 size_t real_size = bytes;
452 if (g_debug->config().options & EXPAND_ALLOC) {
453 real_size += g_debug->config().expand_alloc_bytes;
454 if (real_size < bytes) {
455 // Overflow.
456 errno = ENOMEM;
457 return nullptr;
458 }
459 }
460
461 void* new_pointer;
462 size_t prev_size;
463 if (g_debug->need_header()) {
464 if (bytes > Header::max_size()) {
465 errno = ENOMEM;
466 return nullptr;
467 }
468
469 Header* header = g_debug->GetHeader(pointer);
470 if (header->tag != DEBUG_TAG) {
471 LogTagError(header, pointer, "realloc");
472 return nullptr;
473 }
474
475 // Same size, do nothing.
476 if (real_size == header->real_size()) {
477 return pointer;
478 }
479
480 // Allocation is shrinking.
481 if (real_size < header->usable_size) {
482 header->size = real_size;
483 if (*g_malloc_zygote_child) {
484 header->set_zygote();
485 }
486 if (g_debug->config().options & REAR_GUARD) {
487 // Don't bother allocating a smaller pointer in this case, simply
488 // change the header usable_size and reset the rear guard.
489 header->usable_size = header->real_size();
490 memset(g_debug->GetRearGuard(header), g_debug->config().rear_guard_value,
491 g_debug->config().rear_guard_bytes);
492 }
493 return pointer;
494 }
495
496 // Allocate the new size.
497 new_pointer = debug_malloc(bytes);
498 if (new_pointer == nullptr) {
499 errno = ENOMEM;
500 return nullptr;
501 }
502
503 prev_size = header->usable_size;
504 memcpy(new_pointer, pointer, prev_size);
505 debug_free(pointer);
506 } else {
507 prev_size = g_dispatch->malloc_usable_size(pointer);
508 new_pointer = g_dispatch->realloc(pointer, real_size);
509 if (new_pointer == nullptr) {
510 return nullptr;
511 }
512 }
513
514 if (g_debug->config().options & FILL_ON_ALLOC) {
515 size_t bytes = debug_malloc_usable_size(new_pointer);
516 if (bytes > g_debug->config().fill_on_alloc_bytes) {
517 bytes = g_debug->config().fill_on_alloc_bytes;
518 }
519 if (bytes > prev_size) {
520 memset(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(new_pointer) + prev_size),
521 g_debug->config().fill_alloc_value, bytes - prev_size);
522 }
523 }
524
525 return new_pointer;
526}
527
528void* debug_calloc(size_t nmemb, size_t bytes) {
529 if (DebugCallsDisabled()) {
530 return g_dispatch->calloc(nmemb, bytes);
531 }
532
533 size_t real_size = nmemb * bytes + g_debug->extra_bytes();
534 if (real_size < bytes || real_size < nmemb) {
535 // Overflow.
536 errno = ENOMEM;
537 return nullptr;
538 }
539
540 if (g_debug->need_header()) {
541 // The above check will guarantee the multiply will not overflow.
542 if (bytes * nmemb > Header::max_size()) {
543 errno = ENOMEM;
544 return nullptr;
545 }
546
547 // Need to guarantee the alignment of the header.
Christopher Ferris72df6702016-02-11 15:51:31 -0800548 Header* header = reinterpret_cast<Header*>(
549 g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800550 if (header == nullptr) {
551 return nullptr;
552 }
553 memset(header, 0, g_dispatch->malloc_usable_size(header));
554 return InitHeader(header, header, nmemb * bytes);
555 } else {
556 return g_dispatch->calloc(1, real_size);
557 }
558}
559
560struct mallinfo debug_mallinfo() {
561 return g_dispatch->mallinfo();
562}
563
564int debug_posix_memalign(void** memptr, size_t alignment, size_t size) {
565 if (DebugCallsDisabled()) {
566 return g_dispatch->posix_memalign(memptr, alignment, size);
567 }
568
569 if (!powerof2(alignment)) {
570 return EINVAL;
571 }
572 int saved_errno = errno;
573 *memptr = debug_memalign(alignment, size);
574 errno = saved_errno;
575 return (*memptr != nullptr) ? 0 : ENOMEM;
576}
577
Colin Cross869691c2016-01-29 12:48:18 -0800578int debug_iterate(uintptr_t base, size_t size,
579 void (*callback)(uintptr_t base, size_t size, void* arg), void* arg) {
580 // Can't allocate, malloc is disabled
581 // Manual capture of the arguments to pass to the lambda below as void* arg
582 struct iterate_ctx {
583 decltype(callback) callback;
584 decltype(arg) arg;
585 } ctx = { callback, arg };
586
587 return g_dispatch->iterate(base, size,
588 [](uintptr_t base, size_t size, void* arg) {
589 const iterate_ctx* ctx = reinterpret_cast<iterate_ctx*>(arg);
590 const void* pointer = reinterpret_cast<void*>(base);
591 if (g_debug->need_header()) {
592 const Header* header = reinterpret_cast<const Header*>(pointer);
593 if (g_debug->config().options & TRACK_ALLOCS) {
594 if (g_debug->track->Contains(header)) {
595 // Return just the body of the allocation if we're sure the header exists
596 ctx->callback(reinterpret_cast<uintptr_t>(g_debug->GetPointer(header)),
597 header->real_size(), ctx->arg);
598 return;
599 }
600 }
601 }
602 // Fall back to returning the whole allocation
603 ctx->callback(base, size, ctx->arg);
604 }, &ctx);
605}
606
607void debug_malloc_disable() {
608 g_dispatch->malloc_disable();
609 if (g_debug->track) {
610 g_debug->track->PrepareFork();
611 }
612}
613
614void debug_malloc_enable() {
615 if (g_debug->track) {
616 g_debug->track->PostForkParent();
617 }
618 g_dispatch->malloc_enable();
619}
620
Colin Cross2d4721c2016-02-02 11:57:54 -0800621ssize_t debug_malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count) {
622 if (DebugCallsDisabled() || pointer == nullptr) {
623 return 0;
624 }
625
626 if (g_debug->need_header()) {
627 Header* header;
628 if (g_debug->config().options & TRACK_ALLOCS) {
629 header = g_debug->GetHeader(pointer);
630 if (!g_debug->track->Contains(header)) {
631 return 0;
632 }
633 } else {
634 header = reinterpret_cast<Header*>(pointer);
635 }
636 if (header->tag != DEBUG_TAG) {
637 return 0;
638 }
639 if (g_debug->config().options & BACKTRACE) {
640 BacktraceHeader* back_header = g_debug->GetAllocBacktrace(header);
641 if (back_header->num_frames > 0) {
642 if (frame_count > back_header->num_frames) {
643 frame_count = back_header->num_frames;
644 }
645 memcpy(frames, &back_header->frames[0], frame_count * sizeof(uintptr_t));
646 return frame_count;
647 }
648 }
649 }
650
651 return 0;
652}
653
Christopher Ferris63860cb2015-11-16 17:30:32 -0800654#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
655void* debug_pvalloc(size_t bytes) {
656 if (DebugCallsDisabled()) {
657 return g_dispatch->pvalloc(bytes);
658 }
659
660 size_t pagesize = getpagesize();
661 size_t size = BIONIC_ALIGN(bytes, pagesize);
662 if (size < bytes) {
663 // Overflow
664 errno = ENOMEM;
665 return nullptr;
666 }
667 return debug_memalign(pagesize, size);
668}
669
670void* debug_valloc(size_t size) {
671 if (DebugCallsDisabled()) {
672 return g_dispatch->valloc(size);
673 }
674 return debug_memalign(getpagesize(), size);
675}
676#endif