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