blob: 4f8657969798a2fb3fc264d372db31208b3ae6eb [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"
42#include "DebugData.h"
43#include "debug_disable.h"
44#include "debug_log.h"
45#include "malloc_debug.h"
46
47// ------------------------------------------------------------------------
48// Global Data
49// ------------------------------------------------------------------------
50DebugData* g_debug;
51
52int* g_malloc_zygote_child;
53
54const MallocDispatch* g_dispatch;
55// ------------------------------------------------------------------------
56
57// ------------------------------------------------------------------------
58// Use C style prototypes for all exported functions. This makes it easy
59// to do dlsym lookups during libc initialization when malloc debug
60// is enabled.
61// ------------------------------------------------------------------------
62__BEGIN_DECLS
63
64bool debug_initialize(const MallocDispatch* malloc_dispatch, int* malloc_zygote_child);
65void debug_finalize();
66void debug_get_malloc_leak_info(
67 uint8_t** info, size_t* overall_size, size_t* info_size, size_t* total_memory,
68 size_t* backtrace_size);
69void debug_free_malloc_leak_info(uint8_t* info);
70size_t debug_malloc_usable_size(void* pointer);
71void* debug_malloc(size_t size);
72void debug_free(void* pointer);
73void* debug_memalign(size_t alignment, size_t bytes);
74void* debug_realloc(void* pointer, size_t bytes);
75void* debug_calloc(size_t nmemb, size_t bytes);
76struct mallinfo debug_mallinfo();
77int debug_posix_memalign(void** memptr, size_t alignment, size_t size);
78
79#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
80void* debug_pvalloc(size_t bytes);
81void* debug_valloc(size_t size);
82#endif
83
84__END_DECLS
85// ------------------------------------------------------------------------
86
87static void LogTagError(const Header* header, const void* pointer, const char* name) {
88 ScopedDisableDebugCalls disable;
89
90 error_log(LOG_DIVIDER);
Christopher Ferris7993b802016-01-28 18:35:05 -080091 if (header->tag == DEBUG_FREE_TAG) {
92 error_log("+++ ALLOCATION %p USED AFTER FREE (%s)", pointer, name);
93 if (g_debug->config().options & FREE_TRACK) {
94 g_debug->free_track->LogBacktrace(header);
95 }
96 } else {
97 error_log("+++ ALLOCATION %p HAS INVALID TAG %" PRIx32 " (%s)", pointer, header->tag, name);
98 }
Christopher Ferris63860cb2015-11-16 17:30:32 -080099 error_log("Backtrace at time of failure:");
100 std::vector<uintptr_t> frames(64);
101 size_t frame_num = backtrace_get(frames.data(), frames.size());
102 frames.resize(frame_num);
103 backtrace_log(frames.data(), frames.size());
104 error_log(LOG_DIVIDER);
105}
106
107static void* InitHeader(Header* header, void* orig_pointer, size_t size) {
108 header->tag = DEBUG_TAG;
109 header->orig_pointer = orig_pointer;
110 header->size = size;
111 if (*g_malloc_zygote_child) {
112 header->set_zygote();
113 }
114 header->usable_size = g_dispatch->malloc_usable_size(orig_pointer);
115 if (header->usable_size == 0) {
116 g_dispatch->free(orig_pointer);
117 return nullptr;
118 }
119 header->usable_size -= g_debug->pointer_offset() +
120 reinterpret_cast<uintptr_t>(orig_pointer) - reinterpret_cast<uintptr_t>(header);
121
122 if (g_debug->config().options & FRONT_GUARD) {
123 uint8_t* guard = g_debug->GetFrontGuard(header);
124 memset(guard, g_debug->config().front_guard_value, g_debug->config().front_guard_bytes);
125 }
126
127 if (g_debug->config().options & REAR_GUARD) {
128 uint8_t* guard = g_debug->GetRearGuard(header);
129 memset(guard, g_debug->config().rear_guard_value, g_debug->config().rear_guard_bytes);
130 // If the rear guard is enabled, set the usable size to the exact size
131 // of the allocation.
132 header->usable_size = header->real_size();
133 }
134
135 bool backtrace_found = false;
136 if (g_debug->config().options & BACKTRACE) {
137 BacktraceHeader* back_header = g_debug->GetAllocBacktrace(header);
138 if (g_debug->backtrace->enabled()) {
Christopher Ferris7993b802016-01-28 18:35:05 -0800139 back_header->num_frames = backtrace_get(
140 &back_header->frames[0], g_debug->config().backtrace_frames);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800141 backtrace_found = back_header->num_frames > 0;
142 } else {
143 back_header->num_frames = 0;
144 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800145 }
146
147 if (g_debug->config().options & TRACK_ALLOCS) {
148 g_debug->track->Add(header, backtrace_found);
149 }
150
151 return g_debug->GetPointer(header);
152}
153
154bool debug_initialize(const MallocDispatch* malloc_dispatch, int* malloc_zygote_child) {
155 if (malloc_zygote_child == nullptr) {
156 return false;
157 }
158 g_malloc_zygote_child = malloc_zygote_child;
159
160 g_dispatch = malloc_dispatch;
161
162 if (!DebugDisableInitialize()) {
163 return false;
164 }
165
166 DebugData* debug = new DebugData();
167 if (!debug->Initialize()) {
168 delete debug;
169 DebugDisableFinalize();
170 return false;
171 }
172 g_debug = debug;
173
174 // Always enable the backtrace code since we will use it in a number
175 // of different error cases.
176 backtrace_startup();
177
178 return true;
179}
180
181void debug_finalize() {
182 if (g_debug == nullptr) {
183 return;
184 }
185
186 if (g_debug->config().options & FREE_TRACK) {
187 g_debug->free_track->VerifyAll(*g_debug);
188 }
189
190 if (g_debug->config().options & LEAK_TRACK) {
191 g_debug->track->DisplayLeaks(*g_debug);
192 }
193
194 backtrace_shutdown();
195
196 DebugDisableSet(true);
197
198 delete g_debug;
199 g_debug = nullptr;
200
201 DebugDisableFinalize();
202}
203
204void debug_get_malloc_leak_info(uint8_t** info, size_t* overall_size,
205 size_t* info_size, size_t* total_memory, size_t* backtrace_size) {
206 ScopedDisableDebugCalls disable;
207
208 // Verify the arguments.
209 if (info == nullptr || overall_size == nullptr || info_size == NULL ||
210 total_memory == nullptr || backtrace_size == nullptr) {
211 error_log("get_malloc_leak_info: At least one invalid parameter.");
212 return;
213 }
214
215 *info = nullptr;
216 *overall_size = 0;
217 *info_size = 0;
218 *total_memory = 0;
219 *backtrace_size = 0;
220
221 if (!(g_debug->config().options & BACKTRACE)) {
222 error_log("get_malloc_leak_info: Allocations not being tracked, to enable "
223 "set the option 'backtrace'.");
224 return;
225 }
226
227 g_debug->track->GetInfo(*g_debug, info, overall_size, info_size, total_memory, backtrace_size);
228}
229
230void debug_free_malloc_leak_info(uint8_t* info) {
231 g_dispatch->free(info);
232}
233
234size_t debug_malloc_usable_size(void* pointer) {
235 if (DebugCallsDisabled() || !g_debug->need_header() || pointer == nullptr) {
236 return g_dispatch->malloc_usable_size(pointer);
237 }
238
239 Header* header = g_debug->GetHeader(pointer);
240 if (header->tag != DEBUG_TAG) {
241 LogTagError(header, pointer, "malloc_usable_size");
242 return 0;
243 }
244
245 return header->usable_size;
246}
247
248void* debug_malloc(size_t size) {
249 if (DebugCallsDisabled()) {
250 return g_dispatch->malloc(size);
251 }
252
253 size_t real_size = size + g_debug->extra_bytes();
254 if (real_size < size) {
255 // Overflow.
256 errno = ENOMEM;
257 return nullptr;
258 }
259
260 void* pointer;
261 if (g_debug->need_header()) {
262 if (size > Header::max_size()) {
263 errno = ENOMEM;
264 return nullptr;
265 }
266
267 Header* header = reinterpret_cast<Header*>(g_dispatch->memalign(sizeof(uintptr_t), real_size));
268 if (header == nullptr) {
269 return nullptr;
270 }
271 pointer = InitHeader(header, header, size);
272 } else {
273 pointer = g_dispatch->malloc(real_size);
274 }
275
276 if (pointer != nullptr && g_debug->config().options & FILL_ON_ALLOC) {
277 size_t bytes = debug_malloc_usable_size(pointer);
278 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes;
279 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
280 memset(pointer, g_debug->config().fill_alloc_value, bytes);
281 }
282 return pointer;
283}
284
285void debug_free(void* pointer) {
286 if (DebugCallsDisabled() || pointer == nullptr) {
287 return g_dispatch->free(pointer);
288 }
289
290 void* free_pointer = pointer;
291 size_t bytes;
292 if (g_debug->need_header()) {
293 Header* header = g_debug->GetHeader(pointer);
294 if (header->tag != DEBUG_TAG) {
295 LogTagError(header, pointer, "free");
296 return;
297 }
298 free_pointer = header->orig_pointer;
299
300 if (g_debug->config().options & FRONT_GUARD) {
301 if (!g_debug->front_guard->Valid(*g_debug, header)) {
302 g_debug->front_guard->LogFailure(*g_debug, header);
303 }
304 }
305 if (g_debug->config().options & REAR_GUARD) {
306 if (!g_debug->rear_guard->Valid(*g_debug, header)) {
307 g_debug->rear_guard->LogFailure(*g_debug, header);
308 }
309 }
310
311 if (g_debug->config().options & TRACK_ALLOCS) {
312 bool backtrace_found = false;
313 if (g_debug->config().options & BACKTRACE) {
314 BacktraceHeader* back_header = g_debug->GetAllocBacktrace(header);
315 backtrace_found = back_header->num_frames > 0;
316 }
317 g_debug->track->Remove(header, backtrace_found);
318 }
319
320 if (g_debug->config().options & FREE_TRACK) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800321 g_debug->free_track->Add(*g_debug, header);
322
323 // Do not free this pointer just yet.
324 free_pointer = nullptr;
325 }
Christopher Ferris7993b802016-01-28 18:35:05 -0800326 header->tag = DEBUG_FREE_TAG;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800327
328 bytes = header->usable_size;
329 } else {
330 bytes = g_dispatch->malloc_usable_size(pointer);
331 }
332
333 if (g_debug->config().options & FILL_ON_FREE) {
334 size_t fill_bytes = g_debug->config().fill_on_free_bytes;
335 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
336 memset(pointer, g_debug->config().fill_free_value, bytes);
337 }
338
339 g_dispatch->free(free_pointer);
340}
341
342void* debug_memalign(size_t alignment, size_t bytes) {
343 if (DebugCallsDisabled()) {
344 return g_dispatch->memalign(alignment, bytes);
345 }
346
347 void* pointer;
348 if (g_debug->need_header()) {
349 if (bytes > Header::max_size()) {
350 errno = ENOMEM;
351 return nullptr;
352 }
353
354 // Make the alignment a power of two.
355 if (!powerof2(alignment)) {
356 alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment);
357 }
358 // Force the alignment to at least sizeof(uintptr_t) to guarantee
359 // that the header is aligned properly.
360 if (alignment < sizeof(uintptr_t)) {
361 alignment = sizeof(uintptr_t);
362 }
363
364 // We don't have any idea what the natural alignment of
365 // the underlying native allocator is, so we always need to
366 // over allocate.
367 size_t real_size = alignment + bytes + g_debug->extra_bytes();
368 if (real_size < bytes) {
369 // Overflow.
370 errno = ENOMEM;
371 return nullptr;
372 }
373
374 pointer = g_dispatch->malloc(real_size);
375 if (pointer == nullptr) {
376 return nullptr;
377 }
378
379 uintptr_t value = reinterpret_cast<uintptr_t>(pointer) + g_debug->pointer_offset();
380 // Now align the pointer.
381 value += (-value % alignment);
382
383 Header* header = g_debug->GetHeader(reinterpret_cast<void*>(value));
384 pointer = InitHeader(header, pointer, bytes);
385 } else {
386 size_t real_size = bytes + g_debug->extra_bytes();
387 if (real_size < bytes) {
388 // Overflow.
389 errno = ENOMEM;
390 return nullptr;
391 }
392 pointer = g_dispatch->memalign(alignment, real_size);
393 }
394
395 if (pointer != nullptr && g_debug->config().options & FILL_ON_ALLOC) {
396 size_t bytes = debug_malloc_usable_size(pointer);
397 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes;
398 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
399 memset(pointer, g_debug->config().fill_alloc_value, bytes);
400 }
401 return pointer;
402}
403
404void* debug_realloc(void* pointer, size_t bytes) {
405 if (DebugCallsDisabled()) {
406 return g_dispatch->realloc(pointer, bytes);
407 }
408
409 if (pointer == nullptr) {
410 return debug_malloc(bytes);
411 }
412
413 if (bytes == 0) {
414 debug_free(pointer);
415 return nullptr;
416 }
417
418 size_t real_size = bytes;
419 if (g_debug->config().options & EXPAND_ALLOC) {
420 real_size += g_debug->config().expand_alloc_bytes;
421 if (real_size < bytes) {
422 // Overflow.
423 errno = ENOMEM;
424 return nullptr;
425 }
426 }
427
428 void* new_pointer;
429 size_t prev_size;
430 if (g_debug->need_header()) {
431 if (bytes > Header::max_size()) {
432 errno = ENOMEM;
433 return nullptr;
434 }
435
436 Header* header = g_debug->GetHeader(pointer);
437 if (header->tag != DEBUG_TAG) {
438 LogTagError(header, pointer, "realloc");
439 return nullptr;
440 }
441
442 // Same size, do nothing.
443 if (real_size == header->real_size()) {
444 return pointer;
445 }
446
447 // Allocation is shrinking.
448 if (real_size < header->usable_size) {
449 header->size = real_size;
450 if (*g_malloc_zygote_child) {
451 header->set_zygote();
452 }
453 if (g_debug->config().options & REAR_GUARD) {
454 // Don't bother allocating a smaller pointer in this case, simply
455 // change the header usable_size and reset the rear guard.
456 header->usable_size = header->real_size();
457 memset(g_debug->GetRearGuard(header), g_debug->config().rear_guard_value,
458 g_debug->config().rear_guard_bytes);
459 }
460 return pointer;
461 }
462
463 // Allocate the new size.
464 new_pointer = debug_malloc(bytes);
465 if (new_pointer == nullptr) {
466 errno = ENOMEM;
467 return nullptr;
468 }
469
470 prev_size = header->usable_size;
471 memcpy(new_pointer, pointer, prev_size);
472 debug_free(pointer);
473 } else {
474 prev_size = g_dispatch->malloc_usable_size(pointer);
475 new_pointer = g_dispatch->realloc(pointer, real_size);
476 if (new_pointer == nullptr) {
477 return nullptr;
478 }
479 }
480
481 if (g_debug->config().options & FILL_ON_ALLOC) {
482 size_t bytes = debug_malloc_usable_size(new_pointer);
483 if (bytes > g_debug->config().fill_on_alloc_bytes) {
484 bytes = g_debug->config().fill_on_alloc_bytes;
485 }
486 if (bytes > prev_size) {
487 memset(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(new_pointer) + prev_size),
488 g_debug->config().fill_alloc_value, bytes - prev_size);
489 }
490 }
491
492 return new_pointer;
493}
494
495void* debug_calloc(size_t nmemb, size_t bytes) {
496 if (DebugCallsDisabled()) {
497 return g_dispatch->calloc(nmemb, bytes);
498 }
499
500 size_t real_size = nmemb * bytes + g_debug->extra_bytes();
501 if (real_size < bytes || real_size < nmemb) {
502 // Overflow.
503 errno = ENOMEM;
504 return nullptr;
505 }
506
507 if (g_debug->need_header()) {
508 // The above check will guarantee the multiply will not overflow.
509 if (bytes * nmemb > Header::max_size()) {
510 errno = ENOMEM;
511 return nullptr;
512 }
513
514 // Need to guarantee the alignment of the header.
515 Header* header = reinterpret_cast<Header*>(g_dispatch->memalign(sizeof(uintptr_t), real_size));
516 if (header == nullptr) {
517 return nullptr;
518 }
519 memset(header, 0, g_dispatch->malloc_usable_size(header));
520 return InitHeader(header, header, nmemb * bytes);
521 } else {
522 return g_dispatch->calloc(1, real_size);
523 }
524}
525
526struct mallinfo debug_mallinfo() {
527 return g_dispatch->mallinfo();
528}
529
530int debug_posix_memalign(void** memptr, size_t alignment, size_t size) {
531 if (DebugCallsDisabled()) {
532 return g_dispatch->posix_memalign(memptr, alignment, size);
533 }
534
535 if (!powerof2(alignment)) {
536 return EINVAL;
537 }
538 int saved_errno = errno;
539 *memptr = debug_memalign(alignment, size);
540 errno = saved_errno;
541 return (*memptr != nullptr) ? 0 : ENOMEM;
542}
543
544#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
545void* debug_pvalloc(size_t bytes) {
546 if (DebugCallsDisabled()) {
547 return g_dispatch->pvalloc(bytes);
548 }
549
550 size_t pagesize = getpagesize();
551 size_t size = BIONIC_ALIGN(bytes, pagesize);
552 if (size < bytes) {
553 // Overflow
554 errno = ENOMEM;
555 return nullptr;
556 }
557 return debug_memalign(pagesize, size);
558}
559
560void* debug_valloc(size_t size) {
561 if (DebugCallsDisabled()) {
562 return g_dispatch->valloc(size);
563 }
564 return debug_memalign(getpagesize(), size);
565}
566#endif