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