blob: d2bcf996718ea7c054b38542da1d7a71fbcac88b [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 Ferris7bd01782016-04-20 12:30:58 -0700331 void* pointer = internal_malloc(size);
332
333 if (g_debug->config().options & RECORD_ALLOCS) {
334 g_debug->record->AddEntry(new MallocEntry(pointer, size));
335 }
336
337 return pointer;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700338}
339
340static void internal_free(void* pointer) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800341 void* free_pointer = pointer;
342 size_t bytes;
Christopher Ferrisd0919622016-03-15 22:39:39 -0700343 Header* header;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800344 if (g_debug->need_header()) {
Christopher Ferrisd0919622016-03-15 22:39:39 -0700345 header = g_debug->GetHeader(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800346 if (header->tag != DEBUG_TAG) {
347 LogTagError(header, pointer, "free");
348 return;
349 }
350 free_pointer = header->orig_pointer;
351
352 if (g_debug->config().options & FRONT_GUARD) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700353 if (!g_debug->front_guard->Valid(header)) {
354 g_debug->front_guard->LogFailure(header);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800355 }
356 }
357 if (g_debug->config().options & REAR_GUARD) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700358 if (!g_debug->rear_guard->Valid(header)) {
359 g_debug->rear_guard->LogFailure(header);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800360 }
361 }
362
363 if (g_debug->config().options & TRACK_ALLOCS) {
364 bool backtrace_found = false;
365 if (g_debug->config().options & BACKTRACE) {
366 BacktraceHeader* back_header = g_debug->GetAllocBacktrace(header);
367 backtrace_found = back_header->num_frames > 0;
368 }
369 g_debug->track->Remove(header, backtrace_found);
370 }
Christopher Ferris7993b802016-01-28 18:35:05 -0800371 header->tag = DEBUG_FREE_TAG;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800372
373 bytes = header->usable_size;
374 } else {
375 bytes = g_dispatch->malloc_usable_size(pointer);
376 }
377
378 if (g_debug->config().options & FILL_ON_FREE) {
379 size_t fill_bytes = g_debug->config().fill_on_free_bytes;
380 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
381 memset(pointer, g_debug->config().fill_free_value, bytes);
382 }
383
Christopher Ferrisd0919622016-03-15 22:39:39 -0700384 if (g_debug->config().options & FREE_TRACK) {
385 // Do not add the allocation until we are done modifying the pointer
386 // itself. This avoids a race if a lot of threads are all doing
387 // frees at the same time and we wind up trying to really free this
388 // pointer from another thread, while still trying to free it in
389 // this function.
Christopher Ferris55a89a42016-04-07 17:14:53 -0700390 g_debug->free_track->Add(header);
Christopher Ferrisd0919622016-03-15 22:39:39 -0700391 } else {
392 g_dispatch->free(free_pointer);
393 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800394}
395
Christopher Ferris55a89a42016-04-07 17:14:53 -0700396void debug_free(void* pointer) {
397 if (DebugCallsDisabled() || pointer == nullptr) {
398 return g_dispatch->free(pointer);
399 }
400 ScopedDisableDebugCalls disable;
401
Christopher Ferris7bd01782016-04-20 12:30:58 -0700402 if (g_debug->config().options & RECORD_ALLOCS) {
403 g_debug->record->AddEntry(new FreeEntry(pointer));
404 }
405
Christopher Ferris55a89a42016-04-07 17:14:53 -0700406 internal_free(pointer);
407}
408
Christopher Ferris63860cb2015-11-16 17:30:32 -0800409void* debug_memalign(size_t alignment, size_t bytes) {
410 if (DebugCallsDisabled()) {
411 return g_dispatch->memalign(alignment, bytes);
412 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700413 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800414
Colin Cross9567c7b2016-03-09 17:56:14 -0800415 if (bytes == 0) {
416 bytes = 1;
417 }
418
Christopher Ferris63860cb2015-11-16 17:30:32 -0800419 void* pointer;
420 if (g_debug->need_header()) {
421 if (bytes > Header::max_size()) {
422 errno = ENOMEM;
423 return nullptr;
424 }
425
426 // Make the alignment a power of two.
427 if (!powerof2(alignment)) {
428 alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment);
429 }
Christopher Ferris72df6702016-02-11 15:51:31 -0800430 // Force the alignment to at least MINIMUM_ALIGNMENT_BYTES to guarantee
Christopher Ferris63860cb2015-11-16 17:30:32 -0800431 // that the header is aligned properly.
Christopher Ferris72df6702016-02-11 15:51:31 -0800432 if (alignment < MINIMUM_ALIGNMENT_BYTES) {
433 alignment = MINIMUM_ALIGNMENT_BYTES;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800434 }
435
436 // We don't have any idea what the natural alignment of
437 // the underlying native allocator is, so we always need to
438 // over allocate.
439 size_t real_size = alignment + bytes + g_debug->extra_bytes();
440 if (real_size < bytes) {
441 // Overflow.
442 errno = ENOMEM;
443 return nullptr;
444 }
445
446 pointer = g_dispatch->malloc(real_size);
447 if (pointer == nullptr) {
448 return nullptr;
449 }
450
451 uintptr_t value = reinterpret_cast<uintptr_t>(pointer) + g_debug->pointer_offset();
452 // Now align the pointer.
453 value += (-value % alignment);
454
455 Header* header = g_debug->GetHeader(reinterpret_cast<void*>(value));
456 pointer = InitHeader(header, pointer, bytes);
457 } else {
458 size_t real_size = bytes + g_debug->extra_bytes();
459 if (real_size < bytes) {
460 // Overflow.
461 errno = ENOMEM;
462 return nullptr;
463 }
464 pointer = g_dispatch->memalign(alignment, real_size);
465 }
466
467 if (pointer != nullptr && g_debug->config().options & FILL_ON_ALLOC) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700468 size_t bytes = internal_malloc_usable_size(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800469 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes;
470 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
471 memset(pointer, g_debug->config().fill_alloc_value, bytes);
472 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700473
Christopher Ferris7bd01782016-04-20 12:30:58 -0700474 if (g_debug->config().options & RECORD_ALLOCS) {
475 g_debug->record->AddEntry(new MemalignEntry(pointer, bytes, alignment));
476 }
477
Christopher Ferris63860cb2015-11-16 17:30:32 -0800478 return pointer;
479}
480
481void* debug_realloc(void* pointer, size_t bytes) {
482 if (DebugCallsDisabled()) {
483 return g_dispatch->realloc(pointer, bytes);
484 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700485 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800486
487 if (pointer == nullptr) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700488 pointer = internal_malloc(bytes);
489 if (g_debug->config().options & RECORD_ALLOCS) {
490 g_debug->record->AddEntry(new ReallocEntry(pointer, bytes, nullptr));
491 }
492 return pointer;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800493 }
494
495 if (bytes == 0) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700496 if (g_debug->config().options & RECORD_ALLOCS) {
497 g_debug->record->AddEntry(new ReallocEntry(nullptr, bytes, pointer));
498 }
499
Christopher Ferris55a89a42016-04-07 17:14:53 -0700500 internal_free(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800501 return nullptr;
502 }
503
504 size_t real_size = bytes;
505 if (g_debug->config().options & EXPAND_ALLOC) {
506 real_size += g_debug->config().expand_alloc_bytes;
507 if (real_size < bytes) {
508 // Overflow.
509 errno = ENOMEM;
510 return nullptr;
511 }
512 }
513
514 void* new_pointer;
515 size_t prev_size;
516 if (g_debug->need_header()) {
517 if (bytes > Header::max_size()) {
518 errno = ENOMEM;
519 return nullptr;
520 }
521
522 Header* header = g_debug->GetHeader(pointer);
523 if (header->tag != DEBUG_TAG) {
524 LogTagError(header, pointer, "realloc");
525 return nullptr;
526 }
527
528 // Same size, do nothing.
529 if (real_size == header->real_size()) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700530 // Do not bother recording, this is essentially a nop.
Christopher Ferris63860cb2015-11-16 17:30:32 -0800531 return pointer;
532 }
533
534 // Allocation is shrinking.
535 if (real_size < header->usable_size) {
536 header->size = real_size;
537 if (*g_malloc_zygote_child) {
538 header->set_zygote();
539 }
540 if (g_debug->config().options & REAR_GUARD) {
541 // Don't bother allocating a smaller pointer in this case, simply
542 // change the header usable_size and reset the rear guard.
543 header->usable_size = header->real_size();
544 memset(g_debug->GetRearGuard(header), g_debug->config().rear_guard_value,
545 g_debug->config().rear_guard_bytes);
546 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700547 // Do not bother recording, this is essentially a nop.
Christopher Ferris63860cb2015-11-16 17:30:32 -0800548 return pointer;
549 }
550
551 // Allocate the new size.
Christopher Ferris55a89a42016-04-07 17:14:53 -0700552 new_pointer = internal_malloc(bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800553 if (new_pointer == nullptr) {
554 errno = ENOMEM;
555 return nullptr;
556 }
557
558 prev_size = header->usable_size;
559 memcpy(new_pointer, pointer, prev_size);
Christopher Ferris55a89a42016-04-07 17:14:53 -0700560 internal_free(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800561 } else {
562 prev_size = g_dispatch->malloc_usable_size(pointer);
563 new_pointer = g_dispatch->realloc(pointer, real_size);
564 if (new_pointer == nullptr) {
565 return nullptr;
566 }
567 }
568
569 if (g_debug->config().options & FILL_ON_ALLOC) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700570 size_t bytes = internal_malloc_usable_size(new_pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800571 if (bytes > g_debug->config().fill_on_alloc_bytes) {
572 bytes = g_debug->config().fill_on_alloc_bytes;
573 }
574 if (bytes > prev_size) {
575 memset(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(new_pointer) + prev_size),
576 g_debug->config().fill_alloc_value, bytes - prev_size);
577 }
578 }
579
Christopher Ferris7bd01782016-04-20 12:30:58 -0700580 if (g_debug->config().options & RECORD_ALLOCS) {
581 g_debug->record->AddEntry(new ReallocEntry(new_pointer, bytes, pointer));
582 }
583
Christopher Ferris63860cb2015-11-16 17:30:32 -0800584 return new_pointer;
585}
586
587void* debug_calloc(size_t nmemb, size_t bytes) {
588 if (DebugCallsDisabled()) {
589 return g_dispatch->calloc(nmemb, bytes);
590 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700591 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800592
Colin Cross7877df62016-03-10 13:01:27 -0800593 size_t size;
594 if (__builtin_mul_overflow(nmemb, bytes, &size)) {
595 // Overflow
596 errno = ENOMEM;
597 return nullptr;
598 }
599
Colin Cross9567c7b2016-03-09 17:56:14 -0800600 if (size == 0) {
601 size = 1;
602 }
603
Colin Cross7877df62016-03-10 13:01:27 -0800604 size_t real_size;
605 if (__builtin_add_overflow(size, g_debug->extra_bytes(), &real_size)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800606 // Overflow.
607 errno = ENOMEM;
608 return nullptr;
609 }
610
Christopher Ferris7bd01782016-04-20 12:30:58 -0700611 void* pointer;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800612 if (g_debug->need_header()) {
613 // The above check will guarantee the multiply will not overflow.
Colin Cross9567c7b2016-03-09 17:56:14 -0800614 if (size > Header::max_size()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800615 errno = ENOMEM;
616 return nullptr;
617 }
618
619 // Need to guarantee the alignment of the header.
Christopher Ferris72df6702016-02-11 15:51:31 -0800620 Header* header = reinterpret_cast<Header*>(
621 g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800622 if (header == nullptr) {
623 return nullptr;
624 }
625 memset(header, 0, g_dispatch->malloc_usable_size(header));
Christopher Ferris7bd01782016-04-20 12:30:58 -0700626 pointer = InitHeader(header, header, size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800627 } else {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700628 pointer = g_dispatch->calloc(1, real_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800629 }
Christopher Ferris7bd01782016-04-20 12:30:58 -0700630 if (g_debug->config().options & RECORD_ALLOCS) {
631 g_debug->record->AddEntry(new CallocEntry(pointer, bytes, nmemb));
632 }
633 return pointer;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800634}
635
636struct mallinfo debug_mallinfo() {
637 return g_dispatch->mallinfo();
638}
639
640int debug_posix_memalign(void** memptr, size_t alignment, size_t size) {
641 if (DebugCallsDisabled()) {
642 return g_dispatch->posix_memalign(memptr, alignment, size);
643 }
644
645 if (!powerof2(alignment)) {
646 return EINVAL;
647 }
648 int saved_errno = errno;
649 *memptr = debug_memalign(alignment, size);
650 errno = saved_errno;
651 return (*memptr != nullptr) ? 0 : ENOMEM;
652}
653
Colin Cross869691c2016-01-29 12:48:18 -0800654int debug_iterate(uintptr_t base, size_t size,
655 void (*callback)(uintptr_t base, size_t size, void* arg), void* arg) {
656 // Can't allocate, malloc is disabled
657 // Manual capture of the arguments to pass to the lambda below as void* arg
658 struct iterate_ctx {
659 decltype(callback) callback;
660 decltype(arg) arg;
661 } ctx = { callback, arg };
662
663 return g_dispatch->iterate(base, size,
664 [](uintptr_t base, size_t size, void* arg) {
665 const iterate_ctx* ctx = reinterpret_cast<iterate_ctx*>(arg);
666 const void* pointer = reinterpret_cast<void*>(base);
667 if (g_debug->need_header()) {
668 const Header* header = reinterpret_cast<const Header*>(pointer);
669 if (g_debug->config().options & TRACK_ALLOCS) {
670 if (g_debug->track->Contains(header)) {
671 // Return just the body of the allocation if we're sure the header exists
672 ctx->callback(reinterpret_cast<uintptr_t>(g_debug->GetPointer(header)),
Colin Crossbaa7c6f2016-03-09 16:33:44 -0800673 header->usable_size, ctx->arg);
Colin Cross869691c2016-01-29 12:48:18 -0800674 return;
675 }
676 }
677 }
678 // Fall back to returning the whole allocation
679 ctx->callback(base, size, ctx->arg);
680 }, &ctx);
681}
682
683void debug_malloc_disable() {
684 g_dispatch->malloc_disable();
685 if (g_debug->track) {
686 g_debug->track->PrepareFork();
687 }
688}
689
690void debug_malloc_enable() {
691 if (g_debug->track) {
692 g_debug->track->PostForkParent();
693 }
694 g_dispatch->malloc_enable();
695}
696
Colin Cross2d4721c2016-02-02 11:57:54 -0800697ssize_t debug_malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count) {
698 if (DebugCallsDisabled() || pointer == nullptr) {
699 return 0;
700 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700701 ScopedDisableDebugCalls disable;
Colin Cross2d4721c2016-02-02 11:57:54 -0800702
703 if (g_debug->need_header()) {
704 Header* header;
705 if (g_debug->config().options & TRACK_ALLOCS) {
706 header = g_debug->GetHeader(pointer);
707 if (!g_debug->track->Contains(header)) {
708 return 0;
709 }
710 } else {
711 header = reinterpret_cast<Header*>(pointer);
712 }
713 if (header->tag != DEBUG_TAG) {
714 return 0;
715 }
716 if (g_debug->config().options & BACKTRACE) {
717 BacktraceHeader* back_header = g_debug->GetAllocBacktrace(header);
718 if (back_header->num_frames > 0) {
719 if (frame_count > back_header->num_frames) {
720 frame_count = back_header->num_frames;
721 }
722 memcpy(frames, &back_header->frames[0], frame_count * sizeof(uintptr_t));
723 return frame_count;
724 }
725 }
726 }
727
728 return 0;
729}
730
Christopher Ferris63860cb2015-11-16 17:30:32 -0800731#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
732void* debug_pvalloc(size_t bytes) {
733 if (DebugCallsDisabled()) {
734 return g_dispatch->pvalloc(bytes);
735 }
736
737 size_t pagesize = getpagesize();
738 size_t size = BIONIC_ALIGN(bytes, pagesize);
739 if (size < bytes) {
740 // Overflow
741 errno = ENOMEM;
742 return nullptr;
743 }
744 return debug_memalign(pagesize, size);
745}
746
747void* debug_valloc(size_t size) {
748 if (DebugCallsDisabled()) {
749 return g_dispatch->valloc(size);
750 }
751 return debug_memalign(getpagesize(), size);
752}
753#endif