blob: 014d3855ef014509c283018beec1c0551840021d [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
Tamas Berghammerac81fe82016-08-26 15:54:59 +010065bool debug_initialize(const MallocDispatch* malloc_dispatch, int* malloc_zygote_child,
66 const char* options);
Christopher Ferris63860cb2015-11-16 17:30:32 -080067void debug_finalize();
68void debug_get_malloc_leak_info(
69 uint8_t** info, size_t* overall_size, size_t* info_size, size_t* total_memory,
70 size_t* backtrace_size);
Colin Cross2d4721c2016-02-02 11:57:54 -080071ssize_t debug_malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count);
Christopher Ferris63860cb2015-11-16 17:30:32 -080072void debug_free_malloc_leak_info(uint8_t* info);
73size_t debug_malloc_usable_size(void* pointer);
74void* debug_malloc(size_t size);
75void debug_free(void* pointer);
76void* debug_memalign(size_t alignment, size_t bytes);
77void* debug_realloc(void* pointer, size_t bytes);
78void* debug_calloc(size_t nmemb, size_t bytes);
79struct mallinfo debug_mallinfo();
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -070080int debug_mallopt(int param, int value);
Christopher Ferris63860cb2015-11-16 17:30:32 -080081int debug_posix_memalign(void** memptr, size_t alignment, size_t size);
Colin Cross869691c2016-01-29 12:48:18 -080082int debug_iterate(uintptr_t base, size_t size,
83 void (*callback)(uintptr_t base, size_t size, void* arg), void* arg);
84void debug_malloc_disable();
85void debug_malloc_enable();
Christopher Ferris63860cb2015-11-16 17:30:32 -080086
87#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
88void* debug_pvalloc(size_t bytes);
89void* debug_valloc(size_t size);
90#endif
91
92__END_DECLS
93// ------------------------------------------------------------------------
94
Colin Cross7a28a3c2016-02-07 22:51:15 -080095static void InitAtfork() {
96 static pthread_once_t atfork_init = PTHREAD_ONCE_INIT;
97 pthread_once(&atfork_init, [](){
98 pthread_atfork(
99 [](){
100 if (g_debug != nullptr) {
101 g_debug->PrepareFork();
102 }
103 },
104 [](){
105 if (g_debug != nullptr) {
106 g_debug->PostForkParent();
107 }
108 },
109 [](){
110 if (g_debug != nullptr) {
111 g_debug->PostForkChild();
112 }
113 }
114 );
115 });
116}
Christopher Ferrisd0919622016-03-15 22:39:39 -0700117
Christopher Ferris63860cb2015-11-16 17:30:32 -0800118static void LogTagError(const Header* header, const void* pointer, const char* name) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800119 error_log(LOG_DIVIDER);
Christopher Ferris7993b802016-01-28 18:35:05 -0800120 if (header->tag == DEBUG_FREE_TAG) {
121 error_log("+++ ALLOCATION %p USED AFTER FREE (%s)", pointer, name);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700122 if (g_debug->config().options() & FREE_TRACK) {
Christopher Ferris7993b802016-01-28 18:35:05 -0800123 g_debug->free_track->LogBacktrace(header);
124 }
125 } else {
126 error_log("+++ ALLOCATION %p HAS INVALID TAG %" PRIx32 " (%s)", pointer, header->tag, name);
127 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800128 error_log("Backtrace at time of failure:");
129 std::vector<uintptr_t> frames(64);
130 size_t frame_num = backtrace_get(frames.data(), frames.size());
131 frames.resize(frame_num);
132 backtrace_log(frames.data(), frames.size());
133 error_log(LOG_DIVIDER);
134}
135
136static void* InitHeader(Header* header, void* orig_pointer, size_t size) {
137 header->tag = DEBUG_TAG;
138 header->orig_pointer = orig_pointer;
139 header->size = size;
140 if (*g_malloc_zygote_child) {
141 header->set_zygote();
142 }
143 header->usable_size = g_dispatch->malloc_usable_size(orig_pointer);
144 if (header->usable_size == 0) {
145 g_dispatch->free(orig_pointer);
146 return nullptr;
147 }
148 header->usable_size -= g_debug->pointer_offset() +
Christopher Ferrisd0919622016-03-15 22:39:39 -0700149 reinterpret_cast<uintptr_t>(header) - reinterpret_cast<uintptr_t>(orig_pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800150
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700151 if (g_debug->config().options() & FRONT_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800152 uint8_t* guard = g_debug->GetFrontGuard(header);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700153 memset(guard, g_debug->config().front_guard_value(), g_debug->config().front_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800154 }
155
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700156 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800157 uint8_t* guard = g_debug->GetRearGuard(header);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700158 memset(guard, g_debug->config().rear_guard_value(), g_debug->config().rear_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800159 // If the rear guard is enabled, set the usable size to the exact size
160 // of the allocation.
161 header->usable_size = header->real_size();
162 }
163
164 bool backtrace_found = false;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700165 if (g_debug->config().options() & BACKTRACE) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800166 BacktraceHeader* back_header = g_debug->GetAllocBacktrace(header);
167 if (g_debug->backtrace->enabled()) {
Christopher Ferris7993b802016-01-28 18:35:05 -0800168 back_header->num_frames = backtrace_get(
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700169 &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
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700176 if (g_debug->config().options() & TRACK_ALLOCS) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800177 g_debug->track->Add(header, backtrace_found);
178 }
179
180 return g_debug->GetPointer(header);
181}
182
Tamas Berghammerac81fe82016-08-26 15:54:59 +0100183bool debug_initialize(const MallocDispatch* malloc_dispatch, int* malloc_zygote_child,
184 const char* options) {
185 if (malloc_zygote_child == nullptr || options == nullptr) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800186 return false;
187 }
Colin Cross7a28a3c2016-02-07 22:51:15 -0800188
189 InitAtfork();
190
Christopher Ferris63860cb2015-11-16 17:30:32 -0800191 g_malloc_zygote_child = malloc_zygote_child;
192
193 g_dispatch = malloc_dispatch;
194
195 if (!DebugDisableInitialize()) {
196 return false;
197 }
198
199 DebugData* debug = new DebugData();
Tamas Berghammerac81fe82016-08-26 15:54:59 +0100200 if (!debug->Initialize(options)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800201 delete debug;
202 DebugDisableFinalize();
203 return false;
204 }
205 g_debug = debug;
206
207 // Always enable the backtrace code since we will use it in a number
208 // of different error cases.
209 backtrace_startup();
210
211 return true;
212}
213
214void debug_finalize() {
215 if (g_debug == nullptr) {
216 return;
217 }
218
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700219 if (g_debug->config().options() & FREE_TRACK) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700220 g_debug->free_track->VerifyAll();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800221 }
222
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700223 if (g_debug->config().options() & LEAK_TRACK) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700224 g_debug->track->DisplayLeaks();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800225 }
226
Christopher Ferris63860cb2015-11-16 17:30:32 -0800227 DebugDisableSet(true);
228
Colin Cross2c759912016-02-05 16:17:39 -0800229 backtrace_shutdown();
230
Christopher Ferris63860cb2015-11-16 17:30:32 -0800231 delete g_debug;
232 g_debug = nullptr;
233
234 DebugDisableFinalize();
235}
236
237void debug_get_malloc_leak_info(uint8_t** info, size_t* overall_size,
238 size_t* info_size, size_t* total_memory, size_t* backtrace_size) {
239 ScopedDisableDebugCalls disable;
240
241 // Verify the arguments.
242 if (info == nullptr || overall_size == nullptr || info_size == NULL ||
243 total_memory == nullptr || backtrace_size == nullptr) {
244 error_log("get_malloc_leak_info: At least one invalid parameter.");
245 return;
246 }
247
248 *info = nullptr;
249 *overall_size = 0;
250 *info_size = 0;
251 *total_memory = 0;
252 *backtrace_size = 0;
253
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700254 if (!(g_debug->config().options() & BACKTRACE)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800255 error_log("get_malloc_leak_info: Allocations not being tracked, to enable "
256 "set the option 'backtrace'.");
257 return;
258 }
259
Christopher Ferris55a89a42016-04-07 17:14:53 -0700260 g_debug->track->GetInfo(info, overall_size, info_size, total_memory, backtrace_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800261}
262
263void debug_free_malloc_leak_info(uint8_t* info) {
264 g_dispatch->free(info);
265}
266
Christopher Ferris55a89a42016-04-07 17:14:53 -0700267static size_t internal_malloc_usable_size(void* pointer) {
268 if (g_debug->need_header()) {
269 Header* header = g_debug->GetHeader(pointer);
270 if (header->tag != DEBUG_TAG) {
271 LogTagError(header, pointer, "malloc_usable_size");
272 return 0;
273 }
274
275 return header->usable_size;
276 } else {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800277 return g_dispatch->malloc_usable_size(pointer);
278 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800279}
280
Christopher Ferris55a89a42016-04-07 17:14:53 -0700281size_t debug_malloc_usable_size(void* pointer) {
282 if (DebugCallsDisabled() || pointer == nullptr) {
283 return g_dispatch->malloc_usable_size(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800284 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700285 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800286
Christopher Ferris55a89a42016-04-07 17:14:53 -0700287 return internal_malloc_usable_size(pointer);
288}
289
290static void *internal_malloc(size_t size) {
Colin Cross9567c7b2016-03-09 17:56:14 -0800291 if (size == 0) {
292 size = 1;
293 }
294
Christopher Ferris63860cb2015-11-16 17:30:32 -0800295 size_t real_size = size + g_debug->extra_bytes();
296 if (real_size < size) {
297 // Overflow.
298 errno = ENOMEM;
299 return nullptr;
300 }
301
302 void* pointer;
303 if (g_debug->need_header()) {
304 if (size > Header::max_size()) {
305 errno = ENOMEM;
306 return nullptr;
307 }
308
Christopher Ferris72df6702016-02-11 15:51:31 -0800309 Header* header = reinterpret_cast<Header*>(
310 g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800311 if (header == nullptr) {
312 return nullptr;
313 }
314 pointer = InitHeader(header, header, size);
315 } else {
316 pointer = g_dispatch->malloc(real_size);
317 }
318
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700319 if (pointer != nullptr && g_debug->config().options() & FILL_ON_ALLOC) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700320 size_t bytes = internal_malloc_usable_size(pointer);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700321 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800322 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700323 memset(pointer, g_debug->config().fill_alloc_value(), bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800324 }
325 return pointer;
326}
327
Christopher Ferris55a89a42016-04-07 17:14:53 -0700328void* debug_malloc(size_t size) {
329 if (DebugCallsDisabled()) {
330 return g_dispatch->malloc(size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800331 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700332 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800333
Christopher Ferris7bd01782016-04-20 12:30:58 -0700334 void* pointer = internal_malloc(size);
335
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700336 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700337 g_debug->record->AddEntry(new MallocEntry(pointer, size));
338 }
339
340 return pointer;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700341}
342
343static void internal_free(void* pointer) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800344 void* free_pointer = pointer;
345 size_t bytes;
Christopher Ferrisd0919622016-03-15 22:39:39 -0700346 Header* header;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800347 if (g_debug->need_header()) {
Christopher Ferrisd0919622016-03-15 22:39:39 -0700348 header = g_debug->GetHeader(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800349 if (header->tag != DEBUG_TAG) {
350 LogTagError(header, pointer, "free");
351 return;
352 }
353 free_pointer = header->orig_pointer;
354
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700355 if (g_debug->config().options() & FRONT_GUARD) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700356 if (!g_debug->front_guard->Valid(header)) {
357 g_debug->front_guard->LogFailure(header);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800358 }
359 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700360 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700361 if (!g_debug->rear_guard->Valid(header)) {
362 g_debug->rear_guard->LogFailure(header);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800363 }
364 }
365
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700366 if (g_debug->config().options() & TRACK_ALLOCS) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800367 bool backtrace_found = false;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700368 if (g_debug->config().options() & BACKTRACE) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800369 BacktraceHeader* back_header = g_debug->GetAllocBacktrace(header);
370 backtrace_found = back_header->num_frames > 0;
371 }
372 g_debug->track->Remove(header, backtrace_found);
373 }
Christopher Ferris7993b802016-01-28 18:35:05 -0800374 header->tag = DEBUG_FREE_TAG;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800375
376 bytes = header->usable_size;
377 } else {
378 bytes = g_dispatch->malloc_usable_size(pointer);
379 }
380
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700381 if (g_debug->config().options() & FILL_ON_FREE) {
382 size_t fill_bytes = g_debug->config().fill_on_free_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800383 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700384 memset(pointer, g_debug->config().fill_free_value(), bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800385 }
386
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700387 if (g_debug->config().options() & FREE_TRACK) {
Christopher Ferrisd0919622016-03-15 22:39:39 -0700388 // Do not add the allocation until we are done modifying the pointer
389 // itself. This avoids a race if a lot of threads are all doing
390 // frees at the same time and we wind up trying to really free this
391 // pointer from another thread, while still trying to free it in
392 // this function.
Christopher Ferris55a89a42016-04-07 17:14:53 -0700393 g_debug->free_track->Add(header);
Christopher Ferrisd0919622016-03-15 22:39:39 -0700394 } else {
395 g_dispatch->free(free_pointer);
396 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800397}
398
Christopher Ferris55a89a42016-04-07 17:14:53 -0700399void debug_free(void* pointer) {
400 if (DebugCallsDisabled() || pointer == nullptr) {
401 return g_dispatch->free(pointer);
402 }
403 ScopedDisableDebugCalls disable;
404
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700405 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700406 g_debug->record->AddEntry(new FreeEntry(pointer));
407 }
408
Christopher Ferris55a89a42016-04-07 17:14:53 -0700409 internal_free(pointer);
410}
411
Christopher Ferris63860cb2015-11-16 17:30:32 -0800412void* debug_memalign(size_t alignment, size_t bytes) {
413 if (DebugCallsDisabled()) {
414 return g_dispatch->memalign(alignment, bytes);
415 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700416 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800417
Colin Cross9567c7b2016-03-09 17:56:14 -0800418 if (bytes == 0) {
419 bytes = 1;
420 }
421
Christopher Ferris63860cb2015-11-16 17:30:32 -0800422 void* pointer;
423 if (g_debug->need_header()) {
424 if (bytes > Header::max_size()) {
425 errno = ENOMEM;
426 return nullptr;
427 }
428
429 // Make the alignment a power of two.
430 if (!powerof2(alignment)) {
431 alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment);
432 }
Christopher Ferris72df6702016-02-11 15:51:31 -0800433 // Force the alignment to at least MINIMUM_ALIGNMENT_BYTES to guarantee
Christopher Ferris63860cb2015-11-16 17:30:32 -0800434 // that the header is aligned properly.
Christopher Ferris72df6702016-02-11 15:51:31 -0800435 if (alignment < MINIMUM_ALIGNMENT_BYTES) {
436 alignment = MINIMUM_ALIGNMENT_BYTES;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800437 }
438
439 // We don't have any idea what the natural alignment of
440 // the underlying native allocator is, so we always need to
441 // over allocate.
442 size_t real_size = alignment + bytes + g_debug->extra_bytes();
443 if (real_size < bytes) {
444 // Overflow.
445 errno = ENOMEM;
446 return nullptr;
447 }
448
449 pointer = g_dispatch->malloc(real_size);
450 if (pointer == nullptr) {
451 return nullptr;
452 }
453
454 uintptr_t value = reinterpret_cast<uintptr_t>(pointer) + g_debug->pointer_offset();
455 // Now align the pointer.
456 value += (-value % alignment);
457
458 Header* header = g_debug->GetHeader(reinterpret_cast<void*>(value));
459 pointer = InitHeader(header, pointer, bytes);
460 } else {
461 size_t real_size = bytes + g_debug->extra_bytes();
462 if (real_size < bytes) {
463 // Overflow.
464 errno = ENOMEM;
465 return nullptr;
466 }
467 pointer = g_dispatch->memalign(alignment, real_size);
468 }
469
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700470 if (pointer != nullptr && g_debug->config().options() & FILL_ON_ALLOC) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700471 size_t bytes = internal_malloc_usable_size(pointer);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700472 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800473 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700474 memset(pointer, g_debug->config().fill_alloc_value(), bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800475 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700476
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700477 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700478 g_debug->record->AddEntry(new MemalignEntry(pointer, bytes, alignment));
479 }
480
Christopher Ferris63860cb2015-11-16 17:30:32 -0800481 return pointer;
482}
483
484void* debug_realloc(void* pointer, size_t bytes) {
485 if (DebugCallsDisabled()) {
486 return g_dispatch->realloc(pointer, bytes);
487 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700488 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800489
490 if (pointer == nullptr) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700491 pointer = internal_malloc(bytes);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700492 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700493 g_debug->record->AddEntry(new ReallocEntry(pointer, bytes, nullptr));
494 }
495 return pointer;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800496 }
497
498 if (bytes == 0) {
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700499 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700500 g_debug->record->AddEntry(new ReallocEntry(nullptr, bytes, pointer));
501 }
502
Christopher Ferris55a89a42016-04-07 17:14:53 -0700503 internal_free(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800504 return nullptr;
505 }
506
507 size_t real_size = bytes;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700508 if (g_debug->config().options() & EXPAND_ALLOC) {
509 real_size += g_debug->config().expand_alloc_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800510 if (real_size < bytes) {
511 // Overflow.
512 errno = ENOMEM;
513 return nullptr;
514 }
515 }
516
517 void* new_pointer;
518 size_t prev_size;
519 if (g_debug->need_header()) {
520 if (bytes > Header::max_size()) {
521 errno = ENOMEM;
522 return nullptr;
523 }
524
525 Header* header = g_debug->GetHeader(pointer);
526 if (header->tag != DEBUG_TAG) {
527 LogTagError(header, pointer, "realloc");
528 return nullptr;
529 }
530
531 // Same size, do nothing.
532 if (real_size == header->real_size()) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700533 // Do not bother recording, this is essentially a nop.
Christopher Ferris63860cb2015-11-16 17:30:32 -0800534 return pointer;
535 }
536
537 // Allocation is shrinking.
538 if (real_size < header->usable_size) {
539 header->size = real_size;
540 if (*g_malloc_zygote_child) {
541 header->set_zygote();
542 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700543 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800544 // Don't bother allocating a smaller pointer in this case, simply
545 // change the header usable_size and reset the rear guard.
546 header->usable_size = header->real_size();
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700547 memset(g_debug->GetRearGuard(header), g_debug->config().rear_guard_value(),
548 g_debug->config().rear_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800549 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700550 // Do not bother recording, this is essentially a nop.
Christopher Ferris63860cb2015-11-16 17:30:32 -0800551 return pointer;
552 }
553
554 // Allocate the new size.
Christopher Ferris55a89a42016-04-07 17:14:53 -0700555 new_pointer = internal_malloc(bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800556 if (new_pointer == nullptr) {
557 errno = ENOMEM;
558 return nullptr;
559 }
560
561 prev_size = header->usable_size;
562 memcpy(new_pointer, pointer, prev_size);
Christopher Ferris55a89a42016-04-07 17:14:53 -0700563 internal_free(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800564 } else {
565 prev_size = g_dispatch->malloc_usable_size(pointer);
566 new_pointer = g_dispatch->realloc(pointer, real_size);
567 if (new_pointer == nullptr) {
568 return nullptr;
569 }
570 }
571
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700572 if (g_debug->config().options() & FILL_ON_ALLOC) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700573 size_t bytes = internal_malloc_usable_size(new_pointer);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700574 if (bytes > g_debug->config().fill_on_alloc_bytes()) {
575 bytes = g_debug->config().fill_on_alloc_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800576 }
577 if (bytes > prev_size) {
578 memset(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(new_pointer) + prev_size),
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700579 g_debug->config().fill_alloc_value(), bytes - prev_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800580 }
581 }
582
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700583 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700584 g_debug->record->AddEntry(new ReallocEntry(new_pointer, bytes, pointer));
585 }
586
Christopher Ferris63860cb2015-11-16 17:30:32 -0800587 return new_pointer;
588}
589
590void* debug_calloc(size_t nmemb, size_t bytes) {
591 if (DebugCallsDisabled()) {
592 return g_dispatch->calloc(nmemb, bytes);
593 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700594 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800595
Colin Cross7877df62016-03-10 13:01:27 -0800596 size_t size;
597 if (__builtin_mul_overflow(nmemb, bytes, &size)) {
598 // Overflow
599 errno = ENOMEM;
600 return nullptr;
601 }
602
Colin Cross9567c7b2016-03-09 17:56:14 -0800603 if (size == 0) {
604 size = 1;
605 }
606
Colin Cross7877df62016-03-10 13:01:27 -0800607 size_t real_size;
608 if (__builtin_add_overflow(size, g_debug->extra_bytes(), &real_size)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800609 // Overflow.
610 errno = ENOMEM;
611 return nullptr;
612 }
613
Christopher Ferris7bd01782016-04-20 12:30:58 -0700614 void* pointer;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800615 if (g_debug->need_header()) {
616 // The above check will guarantee the multiply will not overflow.
Colin Cross9567c7b2016-03-09 17:56:14 -0800617 if (size > Header::max_size()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800618 errno = ENOMEM;
619 return nullptr;
620 }
621
622 // Need to guarantee the alignment of the header.
Christopher Ferris72df6702016-02-11 15:51:31 -0800623 Header* header = reinterpret_cast<Header*>(
624 g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800625 if (header == nullptr) {
626 return nullptr;
627 }
628 memset(header, 0, g_dispatch->malloc_usable_size(header));
Christopher Ferris7bd01782016-04-20 12:30:58 -0700629 pointer = InitHeader(header, header, size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800630 } else {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700631 pointer = g_dispatch->calloc(1, real_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800632 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700633 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700634 g_debug->record->AddEntry(new CallocEntry(pointer, bytes, nmemb));
635 }
636 return pointer;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800637}
638
639struct mallinfo debug_mallinfo() {
640 return g_dispatch->mallinfo();
641}
642
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700643int debug_mallopt(int param, int value) {
644 return g_dispatch->mallopt(param, value);
645}
646
Christopher Ferris63860cb2015-11-16 17:30:32 -0800647int debug_posix_memalign(void** memptr, size_t alignment, size_t size) {
648 if (DebugCallsDisabled()) {
649 return g_dispatch->posix_memalign(memptr, alignment, size);
650 }
651
652 if (!powerof2(alignment)) {
653 return EINVAL;
654 }
655 int saved_errno = errno;
656 *memptr = debug_memalign(alignment, size);
657 errno = saved_errno;
658 return (*memptr != nullptr) ? 0 : ENOMEM;
659}
660
Colin Cross869691c2016-01-29 12:48:18 -0800661int debug_iterate(uintptr_t base, size_t size,
662 void (*callback)(uintptr_t base, size_t size, void* arg), void* arg) {
663 // Can't allocate, malloc is disabled
664 // Manual capture of the arguments to pass to the lambda below as void* arg
665 struct iterate_ctx {
666 decltype(callback) callback;
667 decltype(arg) arg;
668 } ctx = { callback, arg };
669
670 return g_dispatch->iterate(base, size,
671 [](uintptr_t base, size_t size, void* arg) {
672 const iterate_ctx* ctx = reinterpret_cast<iterate_ctx*>(arg);
673 const void* pointer = reinterpret_cast<void*>(base);
674 if (g_debug->need_header()) {
675 const Header* header = reinterpret_cast<const Header*>(pointer);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700676 if (g_debug->config().options() & TRACK_ALLOCS) {
Colin Cross869691c2016-01-29 12:48:18 -0800677 if (g_debug->track->Contains(header)) {
678 // Return just the body of the allocation if we're sure the header exists
679 ctx->callback(reinterpret_cast<uintptr_t>(g_debug->GetPointer(header)),
Colin Crossbaa7c6f2016-03-09 16:33:44 -0800680 header->usable_size, ctx->arg);
Colin Cross869691c2016-01-29 12:48:18 -0800681 return;
682 }
683 }
684 }
685 // Fall back to returning the whole allocation
686 ctx->callback(base, size, ctx->arg);
687 }, &ctx);
688}
689
690void debug_malloc_disable() {
691 g_dispatch->malloc_disable();
692 if (g_debug->track) {
693 g_debug->track->PrepareFork();
694 }
695}
696
697void debug_malloc_enable() {
698 if (g_debug->track) {
699 g_debug->track->PostForkParent();
700 }
701 g_dispatch->malloc_enable();
702}
703
Colin Cross2d4721c2016-02-02 11:57:54 -0800704ssize_t debug_malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count) {
705 if (DebugCallsDisabled() || pointer == nullptr) {
706 return 0;
707 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700708 ScopedDisableDebugCalls disable;
Colin Cross2d4721c2016-02-02 11:57:54 -0800709
710 if (g_debug->need_header()) {
711 Header* header;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700712 if (g_debug->config().options() & TRACK_ALLOCS) {
Colin Cross2d4721c2016-02-02 11:57:54 -0800713 header = g_debug->GetHeader(pointer);
714 if (!g_debug->track->Contains(header)) {
715 return 0;
716 }
717 } else {
718 header = reinterpret_cast<Header*>(pointer);
719 }
720 if (header->tag != DEBUG_TAG) {
721 return 0;
722 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700723 if (g_debug->config().options() & BACKTRACE) {
Colin Cross2d4721c2016-02-02 11:57:54 -0800724 BacktraceHeader* back_header = g_debug->GetAllocBacktrace(header);
725 if (back_header->num_frames > 0) {
726 if (frame_count > back_header->num_frames) {
727 frame_count = back_header->num_frames;
728 }
729 memcpy(frames, &back_header->frames[0], frame_count * sizeof(uintptr_t));
730 return frame_count;
731 }
732 }
733 }
734
735 return 0;
736}
737
Christopher Ferris63860cb2015-11-16 17:30:32 -0800738#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
739void* debug_pvalloc(size_t bytes) {
740 if (DebugCallsDisabled()) {
741 return g_dispatch->pvalloc(bytes);
742 }
743
744 size_t pagesize = getpagesize();
745 size_t size = BIONIC_ALIGN(bytes, pagesize);
746 if (size < bytes) {
747 // Overflow
748 errno = ENOMEM;
749 return nullptr;
750 }
751 return debug_memalign(pagesize, size);
752}
753
754void* debug_valloc(size_t size) {
755 if (DebugCallsDisabled()) {
756 return g_dispatch->valloc(size);
757 }
758 return debug_memalign(getpagesize(), size);
759}
760#endif