blob: 5da5b88e48c31c08a8045ee84b753799da3a34df [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 Ferrisd0919622016-03-15 22:39:39 -0700115
Christopher Ferris63860cb2015-11-16 17:30:32 -0800116static void LogTagError(const Header* header, const void* pointer, const char* name) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800117 error_log(LOG_DIVIDER);
Christopher Ferris7993b802016-01-28 18:35:05 -0800118 if (header->tag == DEBUG_FREE_TAG) {
119 error_log("+++ ALLOCATION %p USED AFTER FREE (%s)", pointer, name);
120 if (g_debug->config().options & FREE_TRACK) {
121 g_debug->free_track->LogBacktrace(header);
122 }
123 } else {
124 error_log("+++ ALLOCATION %p HAS INVALID TAG %" PRIx32 " (%s)", pointer, header->tag, name);
125 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800126 error_log("Backtrace at time of failure:");
127 std::vector<uintptr_t> frames(64);
128 size_t frame_num = backtrace_get(frames.data(), frames.size());
129 frames.resize(frame_num);
130 backtrace_log(frames.data(), frames.size());
131 error_log(LOG_DIVIDER);
132}
133
134static void* InitHeader(Header* header, void* orig_pointer, size_t size) {
135 header->tag = DEBUG_TAG;
136 header->orig_pointer = orig_pointer;
137 header->size = size;
138 if (*g_malloc_zygote_child) {
139 header->set_zygote();
140 }
141 header->usable_size = g_dispatch->malloc_usable_size(orig_pointer);
142 if (header->usable_size == 0) {
143 g_dispatch->free(orig_pointer);
144 return nullptr;
145 }
146 header->usable_size -= g_debug->pointer_offset() +
Christopher Ferrisd0919622016-03-15 22:39:39 -0700147 reinterpret_cast<uintptr_t>(header) - reinterpret_cast<uintptr_t>(orig_pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800148
149 if (g_debug->config().options & FRONT_GUARD) {
150 uint8_t* guard = g_debug->GetFrontGuard(header);
151 memset(guard, g_debug->config().front_guard_value, g_debug->config().front_guard_bytes);
152 }
153
154 if (g_debug->config().options & REAR_GUARD) {
155 uint8_t* guard = g_debug->GetRearGuard(header);
156 memset(guard, g_debug->config().rear_guard_value, g_debug->config().rear_guard_bytes);
157 // If the rear guard is enabled, set the usable size to the exact size
158 // of the allocation.
159 header->usable_size = header->real_size();
160 }
161
162 bool backtrace_found = false;
163 if (g_debug->config().options & BACKTRACE) {
164 BacktraceHeader* back_header = g_debug->GetAllocBacktrace(header);
165 if (g_debug->backtrace->enabled()) {
Christopher Ferris7993b802016-01-28 18:35:05 -0800166 back_header->num_frames = backtrace_get(
167 &back_header->frames[0], g_debug->config().backtrace_frames);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800168 backtrace_found = back_header->num_frames > 0;
169 } else {
170 back_header->num_frames = 0;
171 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800172 }
173
174 if (g_debug->config().options & TRACK_ALLOCS) {
175 g_debug->track->Add(header, backtrace_found);
176 }
177
178 return g_debug->GetPointer(header);
179}
180
181bool debug_initialize(const MallocDispatch* malloc_dispatch, int* malloc_zygote_child) {
182 if (malloc_zygote_child == nullptr) {
183 return false;
184 }
Colin Cross7a28a3c2016-02-07 22:51:15 -0800185
186 InitAtfork();
187
Christopher Ferris63860cb2015-11-16 17:30:32 -0800188 g_malloc_zygote_child = malloc_zygote_child;
189
190 g_dispatch = malloc_dispatch;
191
192 if (!DebugDisableInitialize()) {
193 return false;
194 }
195
196 DebugData* debug = new DebugData();
197 if (!debug->Initialize()) {
198 delete debug;
199 DebugDisableFinalize();
200 return false;
201 }
202 g_debug = debug;
203
204 // Always enable the backtrace code since we will use it in a number
205 // of different error cases.
206 backtrace_startup();
207
208 return true;
209}
210
211void debug_finalize() {
212 if (g_debug == nullptr) {
213 return;
214 }
215
216 if (g_debug->config().options & FREE_TRACK) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700217 g_debug->free_track->VerifyAll();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800218 }
219
220 if (g_debug->config().options & LEAK_TRACK) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700221 g_debug->track->DisplayLeaks();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800222 }
223
Christopher Ferris63860cb2015-11-16 17:30:32 -0800224 DebugDisableSet(true);
225
Colin Cross2c759912016-02-05 16:17:39 -0800226 backtrace_shutdown();
227
Christopher Ferris63860cb2015-11-16 17:30:32 -0800228 delete g_debug;
229 g_debug = nullptr;
230
231 DebugDisableFinalize();
232}
233
234void debug_get_malloc_leak_info(uint8_t** info, size_t* overall_size,
235 size_t* info_size, size_t* total_memory, size_t* backtrace_size) {
236 ScopedDisableDebugCalls disable;
237
238 // Verify the arguments.
239 if (info == nullptr || overall_size == nullptr || info_size == NULL ||
240 total_memory == nullptr || backtrace_size == nullptr) {
241 error_log("get_malloc_leak_info: At least one invalid parameter.");
242 return;
243 }
244
245 *info = nullptr;
246 *overall_size = 0;
247 *info_size = 0;
248 *total_memory = 0;
249 *backtrace_size = 0;
250
251 if (!(g_debug->config().options & BACKTRACE)) {
252 error_log("get_malloc_leak_info: Allocations not being tracked, to enable "
253 "set the option 'backtrace'.");
254 return;
255 }
256
Christopher Ferris55a89a42016-04-07 17:14:53 -0700257 g_debug->track->GetInfo(info, overall_size, info_size, total_memory, backtrace_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800258}
259
260void debug_free_malloc_leak_info(uint8_t* info) {
261 g_dispatch->free(info);
262}
263
Christopher Ferris55a89a42016-04-07 17:14:53 -0700264static size_t internal_malloc_usable_size(void* pointer) {
265 if (g_debug->need_header()) {
266 Header* header = g_debug->GetHeader(pointer);
267 if (header->tag != DEBUG_TAG) {
268 LogTagError(header, pointer, "malloc_usable_size");
269 return 0;
270 }
271
272 return header->usable_size;
273 } else {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800274 return g_dispatch->malloc_usable_size(pointer);
275 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800276}
277
Christopher Ferris55a89a42016-04-07 17:14:53 -0700278size_t debug_malloc_usable_size(void* pointer) {
279 if (DebugCallsDisabled() || pointer == nullptr) {
280 return g_dispatch->malloc_usable_size(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800281 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700282 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800283
Christopher Ferris55a89a42016-04-07 17:14:53 -0700284 return internal_malloc_usable_size(pointer);
285}
286
287static void *internal_malloc(size_t size) {
Colin Cross9567c7b2016-03-09 17:56:14 -0800288 if (size == 0) {
289 size = 1;
290 }
291
Christopher Ferris63860cb2015-11-16 17:30:32 -0800292 size_t real_size = size + g_debug->extra_bytes();
293 if (real_size < size) {
294 // Overflow.
295 errno = ENOMEM;
296 return nullptr;
297 }
298
299 void* pointer;
300 if (g_debug->need_header()) {
301 if (size > Header::max_size()) {
302 errno = ENOMEM;
303 return nullptr;
304 }
305
Christopher Ferris72df6702016-02-11 15:51:31 -0800306 Header* header = reinterpret_cast<Header*>(
307 g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800308 if (header == nullptr) {
309 return nullptr;
310 }
311 pointer = InitHeader(header, header, size);
312 } else {
313 pointer = g_dispatch->malloc(real_size);
314 }
315
316 if (pointer != nullptr && g_debug->config().options & FILL_ON_ALLOC) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700317 size_t bytes = internal_malloc_usable_size(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800318 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes;
319 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
320 memset(pointer, g_debug->config().fill_alloc_value, bytes);
321 }
322 return pointer;
323}
324
Christopher Ferris55a89a42016-04-07 17:14:53 -0700325void* debug_malloc(size_t size) {
326 if (DebugCallsDisabled()) {
327 return g_dispatch->malloc(size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800328 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700329 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800330
Christopher Ferris55a89a42016-04-07 17:14:53 -0700331 return internal_malloc(size);
332}
333
334static void internal_free(void* pointer) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800335 void* free_pointer = pointer;
336 size_t bytes;
Christopher Ferrisd0919622016-03-15 22:39:39 -0700337 Header* header;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800338 if (g_debug->need_header()) {
Christopher Ferrisd0919622016-03-15 22:39:39 -0700339 header = g_debug->GetHeader(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800340 if (header->tag != DEBUG_TAG) {
341 LogTagError(header, pointer, "free");
342 return;
343 }
344 free_pointer = header->orig_pointer;
345
346 if (g_debug->config().options & FRONT_GUARD) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700347 if (!g_debug->front_guard->Valid(header)) {
348 g_debug->front_guard->LogFailure(header);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800349 }
350 }
351 if (g_debug->config().options & REAR_GUARD) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700352 if (!g_debug->rear_guard->Valid(header)) {
353 g_debug->rear_guard->LogFailure(header);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800354 }
355 }
356
357 if (g_debug->config().options & TRACK_ALLOCS) {
358 bool backtrace_found = false;
359 if (g_debug->config().options & BACKTRACE) {
360 BacktraceHeader* back_header = g_debug->GetAllocBacktrace(header);
361 backtrace_found = back_header->num_frames > 0;
362 }
363 g_debug->track->Remove(header, backtrace_found);
364 }
Christopher Ferris7993b802016-01-28 18:35:05 -0800365 header->tag = DEBUG_FREE_TAG;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800366
367 bytes = header->usable_size;
368 } else {
369 bytes = g_dispatch->malloc_usable_size(pointer);
370 }
371
372 if (g_debug->config().options & FILL_ON_FREE) {
373 size_t fill_bytes = g_debug->config().fill_on_free_bytes;
374 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
375 memset(pointer, g_debug->config().fill_free_value, bytes);
376 }
377
Christopher Ferrisd0919622016-03-15 22:39:39 -0700378 if (g_debug->config().options & FREE_TRACK) {
379 // Do not add the allocation until we are done modifying the pointer
380 // itself. This avoids a race if a lot of threads are all doing
381 // frees at the same time and we wind up trying to really free this
382 // pointer from another thread, while still trying to free it in
383 // this function.
Christopher Ferris55a89a42016-04-07 17:14:53 -0700384 g_debug->free_track->Add(header);
Christopher Ferrisd0919622016-03-15 22:39:39 -0700385 } else {
386 g_dispatch->free(free_pointer);
387 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800388}
389
Christopher Ferris55a89a42016-04-07 17:14:53 -0700390void debug_free(void* pointer) {
391 if (DebugCallsDisabled() || pointer == nullptr) {
392 return g_dispatch->free(pointer);
393 }
394 ScopedDisableDebugCalls disable;
395
396 internal_free(pointer);
397}
398
Christopher Ferris63860cb2015-11-16 17:30:32 -0800399void* debug_memalign(size_t alignment, size_t bytes) {
400 if (DebugCallsDisabled()) {
401 return g_dispatch->memalign(alignment, bytes);
402 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700403 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800404
Colin Cross9567c7b2016-03-09 17:56:14 -0800405 if (bytes == 0) {
406 bytes = 1;
407 }
408
Christopher Ferris63860cb2015-11-16 17:30:32 -0800409 void* pointer;
410 if (g_debug->need_header()) {
411 if (bytes > Header::max_size()) {
412 errno = ENOMEM;
413 return nullptr;
414 }
415
416 // Make the alignment a power of two.
417 if (!powerof2(alignment)) {
418 alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment);
419 }
Christopher Ferris72df6702016-02-11 15:51:31 -0800420 // Force the alignment to at least MINIMUM_ALIGNMENT_BYTES to guarantee
Christopher Ferris63860cb2015-11-16 17:30:32 -0800421 // that the header is aligned properly.
Christopher Ferris72df6702016-02-11 15:51:31 -0800422 if (alignment < MINIMUM_ALIGNMENT_BYTES) {
423 alignment = MINIMUM_ALIGNMENT_BYTES;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800424 }
425
426 // We don't have any idea what the natural alignment of
427 // the underlying native allocator is, so we always need to
428 // over allocate.
429 size_t real_size = alignment + bytes + g_debug->extra_bytes();
430 if (real_size < bytes) {
431 // Overflow.
432 errno = ENOMEM;
433 return nullptr;
434 }
435
436 pointer = g_dispatch->malloc(real_size);
437 if (pointer == nullptr) {
438 return nullptr;
439 }
440
441 uintptr_t value = reinterpret_cast<uintptr_t>(pointer) + g_debug->pointer_offset();
442 // Now align the pointer.
443 value += (-value % alignment);
444
445 Header* header = g_debug->GetHeader(reinterpret_cast<void*>(value));
446 pointer = InitHeader(header, pointer, bytes);
447 } else {
448 size_t real_size = bytes + g_debug->extra_bytes();
449 if (real_size < bytes) {
450 // Overflow.
451 errno = ENOMEM;
452 return nullptr;
453 }
454 pointer = g_dispatch->memalign(alignment, real_size);
455 }
456
457 if (pointer != nullptr && g_debug->config().options & FILL_ON_ALLOC) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700458 size_t bytes = internal_malloc_usable_size(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800459 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes;
460 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
461 memset(pointer, g_debug->config().fill_alloc_value, bytes);
462 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700463
Christopher Ferris63860cb2015-11-16 17:30:32 -0800464 return pointer;
465}
466
467void* debug_realloc(void* pointer, size_t bytes) {
468 if (DebugCallsDisabled()) {
469 return g_dispatch->realloc(pointer, bytes);
470 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700471 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800472
473 if (pointer == nullptr) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700474 return internal_malloc(bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800475 }
476
477 if (bytes == 0) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700478 internal_free(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800479 return nullptr;
480 }
481
482 size_t real_size = bytes;
483 if (g_debug->config().options & EXPAND_ALLOC) {
484 real_size += g_debug->config().expand_alloc_bytes;
485 if (real_size < bytes) {
486 // Overflow.
487 errno = ENOMEM;
488 return nullptr;
489 }
490 }
491
492 void* new_pointer;
493 size_t prev_size;
494 if (g_debug->need_header()) {
495 if (bytes > Header::max_size()) {
496 errno = ENOMEM;
497 return nullptr;
498 }
499
500 Header* header = g_debug->GetHeader(pointer);
501 if (header->tag != DEBUG_TAG) {
502 LogTagError(header, pointer, "realloc");
503 return nullptr;
504 }
505
506 // Same size, do nothing.
507 if (real_size == header->real_size()) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700508 // Do not bother recording, this is essentially a nop.
Christopher Ferris63860cb2015-11-16 17:30:32 -0800509 return pointer;
510 }
511
512 // Allocation is shrinking.
513 if (real_size < header->usable_size) {
514 header->size = real_size;
515 if (*g_malloc_zygote_child) {
516 header->set_zygote();
517 }
518 if (g_debug->config().options & REAR_GUARD) {
519 // Don't bother allocating a smaller pointer in this case, simply
520 // change the header usable_size and reset the rear guard.
521 header->usable_size = header->real_size();
522 memset(g_debug->GetRearGuard(header), g_debug->config().rear_guard_value,
523 g_debug->config().rear_guard_bytes);
524 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700525 // Do not bother recording, this is essentially a nop.
Christopher Ferris63860cb2015-11-16 17:30:32 -0800526 return pointer;
527 }
528
529 // Allocate the new size.
Christopher Ferris55a89a42016-04-07 17:14:53 -0700530 new_pointer = internal_malloc(bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800531 if (new_pointer == nullptr) {
532 errno = ENOMEM;
533 return nullptr;
534 }
535
536 prev_size = header->usable_size;
537 memcpy(new_pointer, pointer, prev_size);
Christopher Ferris55a89a42016-04-07 17:14:53 -0700538 internal_free(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800539 } else {
540 prev_size = g_dispatch->malloc_usable_size(pointer);
541 new_pointer = g_dispatch->realloc(pointer, real_size);
542 if (new_pointer == nullptr) {
543 return nullptr;
544 }
545 }
546
547 if (g_debug->config().options & FILL_ON_ALLOC) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700548 size_t bytes = internal_malloc_usable_size(new_pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800549 if (bytes > g_debug->config().fill_on_alloc_bytes) {
550 bytes = g_debug->config().fill_on_alloc_bytes;
551 }
552 if (bytes > prev_size) {
553 memset(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(new_pointer) + prev_size),
554 g_debug->config().fill_alloc_value, bytes - prev_size);
555 }
556 }
557
558 return new_pointer;
559}
560
561void* debug_calloc(size_t nmemb, size_t bytes) {
562 if (DebugCallsDisabled()) {
563 return g_dispatch->calloc(nmemb, bytes);
564 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700565 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800566
Colin Cross7877df62016-03-10 13:01:27 -0800567 size_t size;
568 if (__builtin_mul_overflow(nmemb, bytes, &size)) {
569 // Overflow
570 errno = ENOMEM;
571 return nullptr;
572 }
573
Colin Cross9567c7b2016-03-09 17:56:14 -0800574 if (size == 0) {
575 size = 1;
576 }
577
Colin Cross7877df62016-03-10 13:01:27 -0800578 size_t real_size;
579 if (__builtin_add_overflow(size, g_debug->extra_bytes(), &real_size)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800580 // Overflow.
581 errno = ENOMEM;
582 return nullptr;
583 }
584
585 if (g_debug->need_header()) {
586 // The above check will guarantee the multiply will not overflow.
Colin Cross9567c7b2016-03-09 17:56:14 -0800587 if (size > Header::max_size()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800588 errno = ENOMEM;
589 return nullptr;
590 }
591
592 // Need to guarantee the alignment of the header.
Christopher Ferris72df6702016-02-11 15:51:31 -0800593 Header* header = reinterpret_cast<Header*>(
594 g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800595 if (header == nullptr) {
596 return nullptr;
597 }
598 memset(header, 0, g_dispatch->malloc_usable_size(header));
Colin Cross9567c7b2016-03-09 17:56:14 -0800599 return InitHeader(header, header, size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800600 } else {
601 return g_dispatch->calloc(1, real_size);
602 }
603}
604
605struct mallinfo debug_mallinfo() {
606 return g_dispatch->mallinfo();
607}
608
609int debug_posix_memalign(void** memptr, size_t alignment, size_t size) {
610 if (DebugCallsDisabled()) {
611 return g_dispatch->posix_memalign(memptr, alignment, size);
612 }
613
614 if (!powerof2(alignment)) {
615 return EINVAL;
616 }
617 int saved_errno = errno;
618 *memptr = debug_memalign(alignment, size);
619 errno = saved_errno;
620 return (*memptr != nullptr) ? 0 : ENOMEM;
621}
622
Colin Cross869691c2016-01-29 12:48:18 -0800623int debug_iterate(uintptr_t base, size_t size,
624 void (*callback)(uintptr_t base, size_t size, void* arg), void* arg) {
625 // Can't allocate, malloc is disabled
626 // Manual capture of the arguments to pass to the lambda below as void* arg
627 struct iterate_ctx {
628 decltype(callback) callback;
629 decltype(arg) arg;
630 } ctx = { callback, arg };
631
632 return g_dispatch->iterate(base, size,
633 [](uintptr_t base, size_t size, void* arg) {
634 const iterate_ctx* ctx = reinterpret_cast<iterate_ctx*>(arg);
635 const void* pointer = reinterpret_cast<void*>(base);
636 if (g_debug->need_header()) {
637 const Header* header = reinterpret_cast<const Header*>(pointer);
638 if (g_debug->config().options & TRACK_ALLOCS) {
639 if (g_debug->track->Contains(header)) {
640 // Return just the body of the allocation if we're sure the header exists
641 ctx->callback(reinterpret_cast<uintptr_t>(g_debug->GetPointer(header)),
Colin Crossbaa7c6f2016-03-09 16:33:44 -0800642 header->usable_size, ctx->arg);
Colin Cross869691c2016-01-29 12:48:18 -0800643 return;
644 }
645 }
646 }
647 // Fall back to returning the whole allocation
648 ctx->callback(base, size, ctx->arg);
649 }, &ctx);
650}
651
652void debug_malloc_disable() {
653 g_dispatch->malloc_disable();
654 if (g_debug->track) {
655 g_debug->track->PrepareFork();
656 }
657}
658
659void debug_malloc_enable() {
660 if (g_debug->track) {
661 g_debug->track->PostForkParent();
662 }
663 g_dispatch->malloc_enable();
664}
665
Colin Cross2d4721c2016-02-02 11:57:54 -0800666ssize_t debug_malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count) {
667 if (DebugCallsDisabled() || pointer == nullptr) {
668 return 0;
669 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700670 ScopedDisableDebugCalls disable;
Colin Cross2d4721c2016-02-02 11:57:54 -0800671
672 if (g_debug->need_header()) {
673 Header* header;
674 if (g_debug->config().options & TRACK_ALLOCS) {
675 header = g_debug->GetHeader(pointer);
676 if (!g_debug->track->Contains(header)) {
677 return 0;
678 }
679 } else {
680 header = reinterpret_cast<Header*>(pointer);
681 }
682 if (header->tag != DEBUG_TAG) {
683 return 0;
684 }
685 if (g_debug->config().options & BACKTRACE) {
686 BacktraceHeader* back_header = g_debug->GetAllocBacktrace(header);
687 if (back_header->num_frames > 0) {
688 if (frame_count > back_header->num_frames) {
689 frame_count = back_header->num_frames;
690 }
691 memcpy(frames, &back_header->frames[0], frame_count * sizeof(uintptr_t));
692 return frame_count;
693 }
694 }
695 }
696
697 return 0;
698}
699
Christopher Ferris63860cb2015-11-16 17:30:32 -0800700#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
701void* debug_pvalloc(size_t bytes) {
702 if (DebugCallsDisabled()) {
703 return g_dispatch->pvalloc(bytes);
704 }
705
706 size_t pagesize = getpagesize();
707 size_t size = BIONIC_ALIGN(bytes, pagesize);
708 if (size < bytes) {
709 // Overflow
710 errno = ENOMEM;
711 return nullptr;
712 }
713 return debug_memalign(pagesize, size);
714}
715
716void* debug_valloc(size_t size) {
717 if (DebugCallsDisabled()) {
718 return g_dispatch->valloc(size);
719 }
720 return debug_memalign(getpagesize(), size);
721}
722#endif