blob: addb5d4b2b61ddfee414c81b58bf5e0110461c49 [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();
80int debug_posix_memalign(void** memptr, size_t alignment, size_t size);
Colin Cross869691c2016-01-29 12:48:18 -080081int debug_iterate(uintptr_t base, size_t size,
82 void (*callback)(uintptr_t base, size_t size, void* arg), void* arg);
83void debug_malloc_disable();
84void debug_malloc_enable();
Christopher Ferris63860cb2015-11-16 17:30:32 -080085
86#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
87void* debug_pvalloc(size_t bytes);
88void* debug_valloc(size_t size);
89#endif
90
91__END_DECLS
92// ------------------------------------------------------------------------
93
Colin Cross7a28a3c2016-02-07 22:51:15 -080094static void InitAtfork() {
95 static pthread_once_t atfork_init = PTHREAD_ONCE_INIT;
96 pthread_once(&atfork_init, [](){
97 pthread_atfork(
98 [](){
99 if (g_debug != nullptr) {
100 g_debug->PrepareFork();
101 }
102 },
103 [](){
104 if (g_debug != nullptr) {
105 g_debug->PostForkParent();
106 }
107 },
108 [](){
109 if (g_debug != nullptr) {
110 g_debug->PostForkChild();
111 }
112 }
113 );
114 });
115}
Christopher Ferrisd0919622016-03-15 22:39:39 -0700116
Christopher Ferris63860cb2015-11-16 17:30:32 -0800117static void LogTagError(const Header* header, const void* pointer, const char* name) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800118 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);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700121 if (g_debug->config().options() & FREE_TRACK) {
Christopher Ferris7993b802016-01-28 18:35:05 -0800122 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() +
Christopher Ferrisd0919622016-03-15 22:39:39 -0700148 reinterpret_cast<uintptr_t>(header) - reinterpret_cast<uintptr_t>(orig_pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800149
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700150 if (g_debug->config().options() & FRONT_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800151 uint8_t* guard = g_debug->GetFrontGuard(header);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700152 memset(guard, g_debug->config().front_guard_value(), g_debug->config().front_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800153 }
154
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700155 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800156 uint8_t* guard = g_debug->GetRearGuard(header);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700157 memset(guard, g_debug->config().rear_guard_value(), g_debug->config().rear_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800158 // 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;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700164 if (g_debug->config().options() & BACKTRACE) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800165 BacktraceHeader* back_header = g_debug->GetAllocBacktrace(header);
166 if (g_debug->backtrace->enabled()) {
Christopher Ferris7993b802016-01-28 18:35:05 -0800167 back_header->num_frames = backtrace_get(
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700168 &back_header->frames[0], g_debug->config().backtrace_frames());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800169 backtrace_found = back_header->num_frames > 0;
170 } else {
171 back_header->num_frames = 0;
172 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800173 }
174
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700175 if (g_debug->config().options() & TRACK_ALLOCS) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800176 g_debug->track->Add(header, backtrace_found);
177 }
178
179 return g_debug->GetPointer(header);
180}
181
Tamas Berghammerac81fe82016-08-26 15:54:59 +0100182bool debug_initialize(const MallocDispatch* malloc_dispatch, int* malloc_zygote_child,
183 const char* options) {
184 if (malloc_zygote_child == nullptr || options == nullptr) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800185 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();
Tamas Berghammerac81fe82016-08-26 15:54:59 +0100199 if (!debug->Initialize(options)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800200 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
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700218 if (g_debug->config().options() & FREE_TRACK) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700219 g_debug->free_track->VerifyAll();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800220 }
221
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700222 if (g_debug->config().options() & LEAK_TRACK) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700223 g_debug->track->DisplayLeaks();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800224 }
225
Christopher Ferris63860cb2015-11-16 17:30:32 -0800226 DebugDisableSet(true);
227
Colin Cross2c759912016-02-05 16:17:39 -0800228 backtrace_shutdown();
229
Christopher Ferris63860cb2015-11-16 17:30:32 -0800230 delete g_debug;
231 g_debug = nullptr;
232
233 DebugDisableFinalize();
234}
235
236void debug_get_malloc_leak_info(uint8_t** info, size_t* overall_size,
237 size_t* info_size, size_t* total_memory, size_t* backtrace_size) {
238 ScopedDisableDebugCalls disable;
239
240 // Verify the arguments.
241 if (info == nullptr || overall_size == nullptr || info_size == NULL ||
242 total_memory == nullptr || backtrace_size == nullptr) {
243 error_log("get_malloc_leak_info: At least one invalid parameter.");
244 return;
245 }
246
247 *info = nullptr;
248 *overall_size = 0;
249 *info_size = 0;
250 *total_memory = 0;
251 *backtrace_size = 0;
252
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700253 if (!(g_debug->config().options() & BACKTRACE)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800254 error_log("get_malloc_leak_info: Allocations not being tracked, to enable "
255 "set the option 'backtrace'.");
256 return;
257 }
258
Christopher Ferris55a89a42016-04-07 17:14:53 -0700259 g_debug->track->GetInfo(info, overall_size, info_size, total_memory, backtrace_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800260}
261
262void debug_free_malloc_leak_info(uint8_t* info) {
263 g_dispatch->free(info);
264}
265
Christopher Ferris55a89a42016-04-07 17:14:53 -0700266static size_t internal_malloc_usable_size(void* pointer) {
267 if (g_debug->need_header()) {
268 Header* header = g_debug->GetHeader(pointer);
269 if (header->tag != DEBUG_TAG) {
270 LogTagError(header, pointer, "malloc_usable_size");
271 return 0;
272 }
273
274 return header->usable_size;
275 } else {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800276 return g_dispatch->malloc_usable_size(pointer);
277 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800278}
279
Christopher Ferris55a89a42016-04-07 17:14:53 -0700280size_t debug_malloc_usable_size(void* pointer) {
281 if (DebugCallsDisabled() || pointer == nullptr) {
282 return g_dispatch->malloc_usable_size(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800283 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700284 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800285
Christopher Ferris55a89a42016-04-07 17:14:53 -0700286 return internal_malloc_usable_size(pointer);
287}
288
289static void *internal_malloc(size_t size) {
Colin Cross9567c7b2016-03-09 17:56:14 -0800290 if (size == 0) {
291 size = 1;
292 }
293
Christopher Ferris63860cb2015-11-16 17:30:32 -0800294 size_t real_size = size + g_debug->extra_bytes();
295 if (real_size < size) {
296 // Overflow.
297 errno = ENOMEM;
298 return nullptr;
299 }
300
301 void* pointer;
302 if (g_debug->need_header()) {
303 if (size > Header::max_size()) {
304 errno = ENOMEM;
305 return nullptr;
306 }
307
Christopher Ferris72df6702016-02-11 15:51:31 -0800308 Header* header = reinterpret_cast<Header*>(
309 g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800310 if (header == nullptr) {
311 return nullptr;
312 }
313 pointer = InitHeader(header, header, size);
314 } else {
315 pointer = g_dispatch->malloc(real_size);
316 }
317
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700318 if (pointer != nullptr && g_debug->config().options() & FILL_ON_ALLOC) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700319 size_t bytes = internal_malloc_usable_size(pointer);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700320 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800321 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700322 memset(pointer, g_debug->config().fill_alloc_value(), bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800323 }
324 return pointer;
325}
326
Christopher Ferris55a89a42016-04-07 17:14:53 -0700327void* debug_malloc(size_t size) {
328 if (DebugCallsDisabled()) {
329 return g_dispatch->malloc(size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800330 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700331 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800332
Christopher Ferris7bd01782016-04-20 12:30:58 -0700333 void* pointer = internal_malloc(size);
334
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700335 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700336 g_debug->record->AddEntry(new MallocEntry(pointer, size));
337 }
338
339 return pointer;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700340}
341
342static void internal_free(void* pointer) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800343 void* free_pointer = pointer;
344 size_t bytes;
Christopher Ferrisd0919622016-03-15 22:39:39 -0700345 Header* header;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800346 if (g_debug->need_header()) {
Christopher Ferrisd0919622016-03-15 22:39:39 -0700347 header = g_debug->GetHeader(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800348 if (header->tag != DEBUG_TAG) {
349 LogTagError(header, pointer, "free");
350 return;
351 }
352 free_pointer = header->orig_pointer;
353
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700354 if (g_debug->config().options() & FRONT_GUARD) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700355 if (!g_debug->front_guard->Valid(header)) {
356 g_debug->front_guard->LogFailure(header);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800357 }
358 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700359 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700360 if (!g_debug->rear_guard->Valid(header)) {
361 g_debug->rear_guard->LogFailure(header);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800362 }
363 }
364
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700365 if (g_debug->config().options() & TRACK_ALLOCS) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800366 bool backtrace_found = false;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700367 if (g_debug->config().options() & BACKTRACE) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800368 BacktraceHeader* back_header = g_debug->GetAllocBacktrace(header);
369 backtrace_found = back_header->num_frames > 0;
370 }
371 g_debug->track->Remove(header, backtrace_found);
372 }
Christopher Ferris7993b802016-01-28 18:35:05 -0800373 header->tag = DEBUG_FREE_TAG;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800374
375 bytes = header->usable_size;
376 } else {
377 bytes = g_dispatch->malloc_usable_size(pointer);
378 }
379
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700380 if (g_debug->config().options() & FILL_ON_FREE) {
381 size_t fill_bytes = g_debug->config().fill_on_free_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800382 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700383 memset(pointer, g_debug->config().fill_free_value(), bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800384 }
385
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700386 if (g_debug->config().options() & FREE_TRACK) {
Christopher Ferrisd0919622016-03-15 22:39:39 -0700387 // Do not add the allocation until we are done modifying the pointer
388 // itself. This avoids a race if a lot of threads are all doing
389 // frees at the same time and we wind up trying to really free this
390 // pointer from another thread, while still trying to free it in
391 // this function.
Christopher Ferris55a89a42016-04-07 17:14:53 -0700392 g_debug->free_track->Add(header);
Christopher Ferrisd0919622016-03-15 22:39:39 -0700393 } else {
394 g_dispatch->free(free_pointer);
395 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800396}
397
Christopher Ferris55a89a42016-04-07 17:14:53 -0700398void debug_free(void* pointer) {
399 if (DebugCallsDisabled() || pointer == nullptr) {
400 return g_dispatch->free(pointer);
401 }
402 ScopedDisableDebugCalls disable;
403
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700404 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700405 g_debug->record->AddEntry(new FreeEntry(pointer));
406 }
407
Christopher Ferris55a89a42016-04-07 17:14:53 -0700408 internal_free(pointer);
409}
410
Christopher Ferris63860cb2015-11-16 17:30:32 -0800411void* debug_memalign(size_t alignment, size_t bytes) {
412 if (DebugCallsDisabled()) {
413 return g_dispatch->memalign(alignment, bytes);
414 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700415 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800416
Colin Cross9567c7b2016-03-09 17:56:14 -0800417 if (bytes == 0) {
418 bytes = 1;
419 }
420
Christopher Ferris63860cb2015-11-16 17:30:32 -0800421 void* pointer;
422 if (g_debug->need_header()) {
423 if (bytes > Header::max_size()) {
424 errno = ENOMEM;
425 return nullptr;
426 }
427
428 // Make the alignment a power of two.
429 if (!powerof2(alignment)) {
430 alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment);
431 }
Christopher Ferris72df6702016-02-11 15:51:31 -0800432 // Force the alignment to at least MINIMUM_ALIGNMENT_BYTES to guarantee
Christopher Ferris63860cb2015-11-16 17:30:32 -0800433 // that the header is aligned properly.
Christopher Ferris72df6702016-02-11 15:51:31 -0800434 if (alignment < MINIMUM_ALIGNMENT_BYTES) {
435 alignment = MINIMUM_ALIGNMENT_BYTES;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800436 }
437
438 // We don't have any idea what the natural alignment of
439 // the underlying native allocator is, so we always need to
440 // over allocate.
441 size_t real_size = alignment + bytes + g_debug->extra_bytes();
442 if (real_size < bytes) {
443 // Overflow.
444 errno = ENOMEM;
445 return nullptr;
446 }
447
448 pointer = g_dispatch->malloc(real_size);
449 if (pointer == nullptr) {
450 return nullptr;
451 }
452
453 uintptr_t value = reinterpret_cast<uintptr_t>(pointer) + g_debug->pointer_offset();
454 // Now align the pointer.
455 value += (-value % alignment);
456
457 Header* header = g_debug->GetHeader(reinterpret_cast<void*>(value));
458 pointer = InitHeader(header, pointer, bytes);
459 } else {
460 size_t real_size = bytes + g_debug->extra_bytes();
461 if (real_size < bytes) {
462 // Overflow.
463 errno = ENOMEM;
464 return nullptr;
465 }
466 pointer = g_dispatch->memalign(alignment, real_size);
467 }
468
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700469 if (pointer != nullptr && g_debug->config().options() & FILL_ON_ALLOC) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700470 size_t bytes = internal_malloc_usable_size(pointer);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700471 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800472 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700473 memset(pointer, g_debug->config().fill_alloc_value(), bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800474 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700475
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700476 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700477 g_debug->record->AddEntry(new MemalignEntry(pointer, bytes, alignment));
478 }
479
Christopher Ferris63860cb2015-11-16 17:30:32 -0800480 return pointer;
481}
482
483void* debug_realloc(void* pointer, size_t bytes) {
484 if (DebugCallsDisabled()) {
485 return g_dispatch->realloc(pointer, bytes);
486 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700487 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800488
489 if (pointer == nullptr) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700490 pointer = internal_malloc(bytes);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700491 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700492 g_debug->record->AddEntry(new ReallocEntry(pointer, bytes, nullptr));
493 }
494 return pointer;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800495 }
496
497 if (bytes == 0) {
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700498 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700499 g_debug->record->AddEntry(new ReallocEntry(nullptr, bytes, pointer));
500 }
501
Christopher Ferris55a89a42016-04-07 17:14:53 -0700502 internal_free(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800503 return nullptr;
504 }
505
506 size_t real_size = bytes;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700507 if (g_debug->config().options() & EXPAND_ALLOC) {
508 real_size += g_debug->config().expand_alloc_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800509 if (real_size < bytes) {
510 // Overflow.
511 errno = ENOMEM;
512 return nullptr;
513 }
514 }
515
516 void* new_pointer;
517 size_t prev_size;
518 if (g_debug->need_header()) {
519 if (bytes > Header::max_size()) {
520 errno = ENOMEM;
521 return nullptr;
522 }
523
524 Header* header = g_debug->GetHeader(pointer);
525 if (header->tag != DEBUG_TAG) {
526 LogTagError(header, pointer, "realloc");
527 return nullptr;
528 }
529
530 // Same size, do nothing.
531 if (real_size == header->real_size()) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700532 // Do not bother recording, this is essentially a nop.
Christopher Ferris63860cb2015-11-16 17:30:32 -0800533 return pointer;
534 }
535
536 // Allocation is shrinking.
537 if (real_size < header->usable_size) {
538 header->size = real_size;
539 if (*g_malloc_zygote_child) {
540 header->set_zygote();
541 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700542 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800543 // Don't bother allocating a smaller pointer in this case, simply
544 // change the header usable_size and reset the rear guard.
545 header->usable_size = header->real_size();
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700546 memset(g_debug->GetRearGuard(header), g_debug->config().rear_guard_value(),
547 g_debug->config().rear_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800548 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700549 // Do not bother recording, this is essentially a nop.
Christopher Ferris63860cb2015-11-16 17:30:32 -0800550 return pointer;
551 }
552
553 // Allocate the new size.
Christopher Ferris55a89a42016-04-07 17:14:53 -0700554 new_pointer = internal_malloc(bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800555 if (new_pointer == nullptr) {
556 errno = ENOMEM;
557 return nullptr;
558 }
559
560 prev_size = header->usable_size;
561 memcpy(new_pointer, pointer, prev_size);
Christopher Ferris55a89a42016-04-07 17:14:53 -0700562 internal_free(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800563 } else {
564 prev_size = g_dispatch->malloc_usable_size(pointer);
565 new_pointer = g_dispatch->realloc(pointer, real_size);
566 if (new_pointer == nullptr) {
567 return nullptr;
568 }
569 }
570
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700571 if (g_debug->config().options() & FILL_ON_ALLOC) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700572 size_t bytes = internal_malloc_usable_size(new_pointer);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700573 if (bytes > g_debug->config().fill_on_alloc_bytes()) {
574 bytes = g_debug->config().fill_on_alloc_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800575 }
576 if (bytes > prev_size) {
577 memset(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(new_pointer) + prev_size),
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700578 g_debug->config().fill_alloc_value(), bytes - prev_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800579 }
580 }
581
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700582 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700583 g_debug->record->AddEntry(new ReallocEntry(new_pointer, bytes, pointer));
584 }
585
Christopher Ferris63860cb2015-11-16 17:30:32 -0800586 return new_pointer;
587}
588
589void* debug_calloc(size_t nmemb, size_t bytes) {
590 if (DebugCallsDisabled()) {
591 return g_dispatch->calloc(nmemb, bytes);
592 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700593 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800594
Colin Cross7877df62016-03-10 13:01:27 -0800595 size_t size;
596 if (__builtin_mul_overflow(nmemb, bytes, &size)) {
597 // Overflow
598 errno = ENOMEM;
599 return nullptr;
600 }
601
Colin Cross9567c7b2016-03-09 17:56:14 -0800602 if (size == 0) {
603 size = 1;
604 }
605
Colin Cross7877df62016-03-10 13:01:27 -0800606 size_t real_size;
607 if (__builtin_add_overflow(size, g_debug->extra_bytes(), &real_size)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800608 // Overflow.
609 errno = ENOMEM;
610 return nullptr;
611 }
612
Christopher Ferris7bd01782016-04-20 12:30:58 -0700613 void* pointer;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800614 if (g_debug->need_header()) {
615 // The above check will guarantee the multiply will not overflow.
Colin Cross9567c7b2016-03-09 17:56:14 -0800616 if (size > Header::max_size()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800617 errno = ENOMEM;
618 return nullptr;
619 }
620
621 // Need to guarantee the alignment of the header.
Christopher Ferris72df6702016-02-11 15:51:31 -0800622 Header* header = reinterpret_cast<Header*>(
623 g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800624 if (header == nullptr) {
625 return nullptr;
626 }
627 memset(header, 0, g_dispatch->malloc_usable_size(header));
Christopher Ferris7bd01782016-04-20 12:30:58 -0700628 pointer = InitHeader(header, header, size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800629 } else {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700630 pointer = g_dispatch->calloc(1, real_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800631 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700632 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700633 g_debug->record->AddEntry(new CallocEntry(pointer, bytes, nmemb));
634 }
635 return pointer;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800636}
637
638struct mallinfo debug_mallinfo() {
639 return g_dispatch->mallinfo();
640}
641
642int debug_posix_memalign(void** memptr, size_t alignment, size_t size) {
643 if (DebugCallsDisabled()) {
644 return g_dispatch->posix_memalign(memptr, alignment, size);
645 }
646
647 if (!powerof2(alignment)) {
648 return EINVAL;
649 }
650 int saved_errno = errno;
651 *memptr = debug_memalign(alignment, size);
652 errno = saved_errno;
653 return (*memptr != nullptr) ? 0 : ENOMEM;
654}
655
Colin Cross869691c2016-01-29 12:48:18 -0800656int debug_iterate(uintptr_t base, size_t size,
657 void (*callback)(uintptr_t base, size_t size, void* arg), void* arg) {
658 // Can't allocate, malloc is disabled
659 // Manual capture of the arguments to pass to the lambda below as void* arg
660 struct iterate_ctx {
661 decltype(callback) callback;
662 decltype(arg) arg;
663 } ctx = { callback, arg };
664
665 return g_dispatch->iterate(base, size,
666 [](uintptr_t base, size_t size, void* arg) {
667 const iterate_ctx* ctx = reinterpret_cast<iterate_ctx*>(arg);
668 const void* pointer = reinterpret_cast<void*>(base);
669 if (g_debug->need_header()) {
670 const Header* header = reinterpret_cast<const Header*>(pointer);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700671 if (g_debug->config().options() & TRACK_ALLOCS) {
Colin Cross869691c2016-01-29 12:48:18 -0800672 if (g_debug->track->Contains(header)) {
673 // Return just the body of the allocation if we're sure the header exists
674 ctx->callback(reinterpret_cast<uintptr_t>(g_debug->GetPointer(header)),
Colin Crossbaa7c6f2016-03-09 16:33:44 -0800675 header->usable_size, ctx->arg);
Colin Cross869691c2016-01-29 12:48:18 -0800676 return;
677 }
678 }
679 }
680 // Fall back to returning the whole allocation
681 ctx->callback(base, size, ctx->arg);
682 }, &ctx);
683}
684
685void debug_malloc_disable() {
686 g_dispatch->malloc_disable();
687 if (g_debug->track) {
688 g_debug->track->PrepareFork();
689 }
690}
691
692void debug_malloc_enable() {
693 if (g_debug->track) {
694 g_debug->track->PostForkParent();
695 }
696 g_dispatch->malloc_enable();
697}
698
Colin Cross2d4721c2016-02-02 11:57:54 -0800699ssize_t debug_malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count) {
700 if (DebugCallsDisabled() || pointer == nullptr) {
701 return 0;
702 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700703 ScopedDisableDebugCalls disable;
Colin Cross2d4721c2016-02-02 11:57:54 -0800704
705 if (g_debug->need_header()) {
706 Header* header;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700707 if (g_debug->config().options() & TRACK_ALLOCS) {
Colin Cross2d4721c2016-02-02 11:57:54 -0800708 header = g_debug->GetHeader(pointer);
709 if (!g_debug->track->Contains(header)) {
710 return 0;
711 }
712 } else {
713 header = reinterpret_cast<Header*>(pointer);
714 }
715 if (header->tag != DEBUG_TAG) {
716 return 0;
717 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700718 if (g_debug->config().options() & BACKTRACE) {
Colin Cross2d4721c2016-02-02 11:57:54 -0800719 BacktraceHeader* back_header = g_debug->GetAllocBacktrace(header);
720 if (back_header->num_frames > 0) {
721 if (frame_count > back_header->num_frames) {
722 frame_count = back_header->num_frames;
723 }
724 memcpy(frames, &back_header->frames[0], frame_count * sizeof(uintptr_t));
725 return frame_count;
726 }
727 }
728 }
729
730 return 0;
731}
732
Christopher Ferris63860cb2015-11-16 17:30:32 -0800733#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
734void* debug_pvalloc(size_t bytes) {
735 if (DebugCallsDisabled()) {
736 return g_dispatch->pvalloc(bytes);
737 }
738
739 size_t pagesize = getpagesize();
740 size_t size = BIONIC_ALIGN(bytes, pagesize);
741 if (size < bytes) {
742 // Overflow
743 errno = ENOMEM;
744 return nullptr;
745 }
746 return debug_memalign(pagesize, size);
747}
748
749void* debug_valloc(size_t size) {
750 if (DebugCallsDisabled()) {
751 return g_dispatch->valloc(size);
752 }
753 return debug_memalign(getpagesize(), size);
754}
755#endif