blob: e5845023d4e993b19f9d4ae65922a458d77350d4 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
2 * Copyright (C) 2008 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 */
Xi Wang7f5aa4f2012-03-14 02:48:39 -040028
29#include <dlfcn.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080030#include <errno.h>
Xi Wang7f5aa4f2012-03-14 02:48:39 -040031#include <fcntl.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080032#include <pthread.h>
Xi Wang7f5aa4f2012-03-14 02:48:39 -040033#include <stdarg.h>
34#include <stddef.h>
35#include <stdint.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080036#include <stdio.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080037#include <stdlib.h>
38#include <string.h>
39#include <unistd.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080040#include <unwind.h>
41
Xi Wang7f5aa4f2012-03-14 02:48:39 -040042#include <arpa/inet.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080043#include <sys/select.h>
Xi Wang7f5aa4f2012-03-14 02:48:39 -040044#include <sys/socket.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080045#include <sys/system_properties.h>
Xi Wang7f5aa4f2012-03-14 02:48:39 -040046#include <sys/types.h>
47#include <sys/un.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080048
49#include "dlmalloc.h"
50#include "logd.h"
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080051#include "malloc_debug_common.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080052
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080053// This file should be included into the build only when
54// MALLOC_LEAK_CHECK, or MALLOC_QEMU_INSTRUMENT, or both
55// macros are defined.
56#ifndef MALLOC_LEAK_CHECK
57#error MALLOC_LEAK_CHECK is not defined.
58#endif // !MALLOC_LEAK_CHECK
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080059
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080060// Global variables defined in malloc_debug_common.c
61extern int gMallocLeakZygoteChild;
62extern pthread_mutex_t gAllocationsMutex;
63extern HashTable gHashTable;
64extern const MallocDebug __libc_malloc_default_dispatch;
65extern const MallocDebug* __libc_malloc_dispatch;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080066
67// =============================================================================
Andy McFadden39f37452009-07-21 15:25:23 -070068// log functions
69// =============================================================================
70
71#define debug_log(format, ...) \
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080072 __libc_android_log_print(ANDROID_LOG_DEBUG, "malloc_leak_check", (format), ##__VA_ARGS__ )
73#define error_log(format, ...) \
74 __libc_android_log_print(ANDROID_LOG_ERROR, "malloc_leak_check", (format), ##__VA_ARGS__ )
75#define info_log(format, ...) \
76 __libc_android_log_print(ANDROID_LOG_INFO, "malloc_leak_check", (format), ##__VA_ARGS__ )
Andy McFadden39f37452009-07-21 15:25:23 -070077
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080078static int gTrapOnError = 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080079
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080080#define MALLOC_ALIGNMENT 8
81#define GUARD 0x48151642
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080082#define DEBUG 0
83
84// =============================================================================
85// Structures
86// =============================================================================
87typedef struct AllocationEntry AllocationEntry;
88struct AllocationEntry {
89 HashEntry* entry;
90 uint32_t guard;
91};
92
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080093
94// =============================================================================
95// Hash Table functions
96// =============================================================================
97static uint32_t get_hash(intptr_t* backtrace, size_t numEntries)
98{
99 if (backtrace == NULL) return 0;
100
101 int hash = 0;
102 size_t i;
103 for (i = 0 ; i < numEntries ; i++) {
104 hash = (hash * 33) + (backtrace[i] >> 2);
105 }
106
107 return hash;
108}
109
110static HashEntry* find_entry(HashTable* table, int slot,
111 intptr_t* backtrace, size_t numEntries, size_t size)
112{
113 HashEntry* entry = table->slots[slot];
114 while (entry != NULL) {
115 //debug_log("backtrace: %p, entry: %p entry->backtrace: %p\n",
116 // backtrace, entry, (entry != NULL) ? entry->backtrace : NULL);
117 /*
118 * See if the entry matches exactly. We compare the "size" field,
119 * including the flag bits.
120 */
121 if (entry->size == size && entry->numEntries == numEntries &&
122 !memcmp(backtrace, entry->backtrace, numEntries * sizeof(intptr_t))) {
123 return entry;
124 }
125
126 entry = entry->next;
127 }
128
129 return NULL;
130}
131
132static HashEntry* record_backtrace(intptr_t* backtrace, size_t numEntries, size_t size)
133{
134 size_t hash = get_hash(backtrace, numEntries);
135 size_t slot = hash % HASHTABLE_SIZE;
136
137 if (size & SIZE_FLAG_MASK) {
138 debug_log("malloc_debug: allocation %zx exceeds bit width\n", size);
139 abort();
140 }
141
142 if (gMallocLeakZygoteChild)
143 size |= SIZE_FLAG_ZYGOTE_CHILD;
144
145 HashEntry* entry = find_entry(&gHashTable, slot, backtrace, numEntries, size);
146
147 if (entry != NULL) {
148 entry->allocations++;
149 } else {
150 // create a new entry
151 entry = (HashEntry*)dlmalloc(sizeof(HashEntry) + numEntries*sizeof(intptr_t));
André Goddard Rosa5751c542010-02-05 16:03:09 -0200152 if (!entry)
153 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800154 entry->allocations = 1;
155 entry->slot = slot;
156 entry->prev = NULL;
157 entry->next = gHashTable.slots[slot];
158 entry->numEntries = numEntries;
159 entry->size = size;
160
161 memcpy(entry->backtrace, backtrace, numEntries * sizeof(intptr_t));
162
163 gHashTable.slots[slot] = entry;
164
165 if (entry->next != NULL) {
166 entry->next->prev = entry;
167 }
168
169 // we just added an entry, increase the size of the hashtable
170 gHashTable.count++;
171 }
172
173 return entry;
174}
175
176static int is_valid_entry(HashEntry* entry)
177{
178 if (entry != NULL) {
179 int i;
180 for (i = 0 ; i < HASHTABLE_SIZE ; i++) {
181 HashEntry* e1 = gHashTable.slots[i];
182
183 while (e1 != NULL) {
184 if (e1 == entry) {
185 return 1;
186 }
187
188 e1 = e1->next;
189 }
190 }
191 }
192
193 return 0;
194}
195
196static void remove_entry(HashEntry* entry)
197{
198 HashEntry* prev = entry->prev;
199 HashEntry* next = entry->next;
200
201 if (prev != NULL) entry->prev->next = next;
202 if (next != NULL) entry->next->prev = prev;
203
204 if (prev == NULL) {
205 // we are the head of the list. set the head to be next
206 gHashTable.slots[entry->slot] = entry->next;
207 }
208
209 // we just removed and entry, decrease the size of the hashtable
210 gHashTable.count--;
211}
212
213
214// =============================================================================
215// stack trace functions
216// =============================================================================
217
218typedef struct
219{
220 size_t count;
221 intptr_t* addrs;
222} stack_crawl_state_t;
223
224
225/* depends how the system includes define this */
226#ifdef HAVE_UNWIND_CONTEXT_STRUCT
227typedef struct _Unwind_Context __unwind_context;
228#else
229typedef _Unwind_Context __unwind_context;
230#endif
231
232static _Unwind_Reason_Code trace_function(__unwind_context *context, void *arg)
233{
234 stack_crawl_state_t* state = (stack_crawl_state_t*)arg;
235 if (state->count) {
236 intptr_t ip = (intptr_t)_Unwind_GetIP(context);
237 if (ip) {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800238 state->addrs[0] = ip;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800239 state->addrs++;
240 state->count--;
241 return _URC_NO_REASON;
242 }
243 }
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800244 /*
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800245 * If we run out of space to record the address or 0 has been seen, stop
246 * unwinding the stack.
247 */
248 return _URC_END_OF_STACK;
249}
250
251static inline
252int get_backtrace(intptr_t* addrs, size_t max_entries)
253{
254 stack_crawl_state_t state;
255 state.count = max_entries;
256 state.addrs = (intptr_t*)addrs;
257 _Unwind_Backtrace(trace_function, (void*)&state);
258 return max_entries - state.count;
259}
260
261// =============================================================================
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800262// malloc check functions
263// =============================================================================
264
265#define CHK_FILL_FREE 0xef
Bruce Beare89d3fdc2011-09-21 12:44:05 +0200266#define CHK_SENTINEL_VALUE (char)0xeb
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800267#define CHK_SENTINEL_HEAD_SIZE 16
268#define CHK_SENTINEL_TAIL_SIZE 16
269#define CHK_OVERHEAD_SIZE ( CHK_SENTINEL_HEAD_SIZE + \
270 CHK_SENTINEL_TAIL_SIZE + \
271 sizeof(size_t) )
272
273static void dump_stack_trace()
274{
275 intptr_t addrs[20];
276 int c = get_backtrace(addrs, 20);
277 char buf[16];
278 char tmp[16*20];
279 int i;
280
281 tmp[0] = 0; // Need to initialize tmp[0] for the first strcat
282 for (i=0 ; i<c; i++) {
David 'Digit' Turnerc4eee372009-07-08 14:22:41 +0200283 snprintf(buf, sizeof buf, "%2d: %08x\n", i, addrs[i]);
284 strlcat(tmp, buf, sizeof tmp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800285 }
286 __libc_android_log_print(ANDROID_LOG_ERROR, "libc", "call stack:\n%s", tmp);
287}
288
289static int is_valid_malloc_pointer(void* addr)
290{
291 return 1;
292}
293
David 'Digit' Turnerc4eee372009-07-08 14:22:41 +0200294static void assert_log_message(const char* format, ...)
295{
296 va_list args;
297
298 pthread_mutex_lock(&gAllocationsMutex);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800299 {
300 const MallocDebug* current_dispatch = __libc_malloc_dispatch;
301 __libc_malloc_dispatch = &__libc_malloc_default_dispatch;
David 'Digit' Turnerc4eee372009-07-08 14:22:41 +0200302 va_start(args, format);
303 __libc_android_log_vprint(ANDROID_LOG_ERROR, "libc",
304 format, args);
305 va_end(args);
306 dump_stack_trace();
307 if (gTrapOnError) {
308 __builtin_trap();
309 }
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800310 __libc_malloc_dispatch = current_dispatch;
311 }
David 'Digit' Turnerc4eee372009-07-08 14:22:41 +0200312 pthread_mutex_unlock(&gAllocationsMutex);
313}
314
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800315static void assert_valid_malloc_pointer(void* mem)
316{
317 if (mem && !is_valid_malloc_pointer(mem)) {
David 'Digit' Turnerc4eee372009-07-08 14:22:41 +0200318 assert_log_message(
319 "*** MALLOC CHECK: buffer %p, is not a valid "
320 "malloc pointer (are you mixing up new/delete "
321 "and malloc/free?)", mem);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800322 }
323}
324
David 'Digit' Turnerc4eee372009-07-08 14:22:41 +0200325/* Check that a given address corresponds to a guarded block,
326 * and returns its original allocation size in '*allocated'.
327 * 'func' is the capitalized name of the caller function.
328 * Returns 0 on success, or -1 on failure.
329 * NOTE: Does not return if gTrapOnError is set.
330 */
331static int chk_mem_check(void* mem,
332 size_t* allocated,
333 const char* func)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800334{
David 'Digit' Turnerc4eee372009-07-08 14:22:41 +0200335 char* buffer;
336 size_t offset, bytes;
337 int i;
338 char* buf;
339
340 /* first check the bytes in the sentinel header */
341 buf = (char*)mem - CHK_SENTINEL_HEAD_SIZE;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800342 for (i=0 ; i<CHK_SENTINEL_HEAD_SIZE ; i++) {
343 if (buf[i] != CHK_SENTINEL_VALUE) {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800344 assert_log_message(
David 'Digit' Turnerc4eee372009-07-08 14:22:41 +0200345 "*** %s CHECK: buffer %p "
346 "corrupted %d bytes before allocation",
347 func, mem, CHK_SENTINEL_HEAD_SIZE-i);
348 return -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800349 }
350 }
David 'Digit' Turnerc4eee372009-07-08 14:22:41 +0200351
352 /* then the ones in the sentinel trailer */
353 buffer = (char*)mem - CHK_SENTINEL_HEAD_SIZE;
354 offset = dlmalloc_usable_size(buffer) - sizeof(size_t);
355 bytes = *(size_t *)(buffer + offset);
356
357 buf = (char*)mem + bytes;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800358 for (i=CHK_SENTINEL_TAIL_SIZE-1 ; i>=0 ; i--) {
359 if (buf[i] != CHK_SENTINEL_VALUE) {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800360 assert_log_message(
David 'Digit' Turnerc4eee372009-07-08 14:22:41 +0200361 "*** %s CHECK: buffer %p, size=%lu, "
362 "corrupted %d bytes after allocation",
363 func, buffer, bytes, i+1);
364 return -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800365 }
366 }
David 'Digit' Turnerc4eee372009-07-08 14:22:41 +0200367
368 *allocated = bytes;
369 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800370}
371
David 'Digit' Turnerc4eee372009-07-08 14:22:41 +0200372
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800373void* chk_malloc(size_t bytes)
374{
Xi Wang7f5aa4f2012-03-14 02:48:39 -0400375 size_t size = bytes + CHK_OVERHEAD_SIZE;
376 if (size < bytes) { // Overflow.
377 return NULL;
378 }
379 uint8_t* buffer = (uint8_t*) dlmalloc(size);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800380 if (buffer) {
David 'Digit' Turnerc4eee372009-07-08 14:22:41 +0200381 memset(buffer, CHK_SENTINEL_VALUE, bytes + CHK_OVERHEAD_SIZE);
382 size_t offset = dlmalloc_usable_size(buffer) - sizeof(size_t);
383 *(size_t *)(buffer + offset) = bytes;
384 buffer += CHK_SENTINEL_HEAD_SIZE;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800385 }
386 return buffer;
387}
388
389void chk_free(void* mem)
390{
391 assert_valid_malloc_pointer(mem);
392 if (mem) {
David 'Digit' Turnerc4eee372009-07-08 14:22:41 +0200393 size_t size;
394 char* buffer;
395
396 if (chk_mem_check(mem, &size, "FREE") == 0) {
397 buffer = (char*)mem - CHK_SENTINEL_HEAD_SIZE;
398 memset(buffer, CHK_FILL_FREE, size + CHK_OVERHEAD_SIZE);
399 dlfree(buffer);
400 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800401 }
402}
403
404void* chk_calloc(size_t n_elements, size_t elem_size)
405{
406 size_t size;
407 void* ptr;
408
409 /* Fail on overflow - just to be safe even though this code runs only
410 * within the debugging C library, not the production one */
411 if (n_elements && MAX_SIZE_T / n_elements < elem_size) {
412 return NULL;
413 }
414 size = n_elements * elem_size;
415 ptr = chk_malloc(size);
416 if (ptr != NULL) {
417 memset(ptr, 0, size);
418 }
419 return ptr;
420}
421
422void* chk_realloc(void* mem, size_t bytes)
423{
David 'Digit' Turnerc4eee372009-07-08 14:22:41 +0200424 char* buffer;
425 int ret;
426 size_t old_bytes = 0;
427
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800428 assert_valid_malloc_pointer(mem);
David 'Digit' Turnerc4eee372009-07-08 14:22:41 +0200429
430 if (mem != NULL && chk_mem_check(mem, &old_bytes, "REALLOC") < 0)
431 return NULL;
432
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800433 char* new_buffer = chk_malloc(bytes);
434 if (mem == NULL) {
435 return new_buffer;
436 }
437
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800438 if (new_buffer) {
André Goddard Rosa291100c2010-02-05 16:32:56 -0200439 if (bytes > old_bytes)
440 bytes = old_bytes;
441 memcpy(new_buffer, mem, bytes);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800442 chk_free(mem);
443 }
444
445 return new_buffer;
446}
447
448void* chk_memalign(size_t alignment, size_t bytes)
449{
450 // XXX: it's better to use malloc, than being wrong
451 return chk_malloc(bytes);
452}
453
454// =============================================================================
455// malloc fill functions
456// =============================================================================
457
458void* fill_malloc(size_t bytes)
459{
460 void* buffer = dlmalloc(bytes);
461 if (buffer) {
462 memset(buffer, CHK_SENTINEL_VALUE, bytes);
463 }
464 return buffer;
465}
466
467void fill_free(void* mem)
468{
469 size_t bytes = dlmalloc_usable_size(mem);
470 memset(mem, CHK_FILL_FREE, bytes);
471 dlfree(mem);
472}
473
474void* fill_realloc(void* mem, size_t bytes)
475{
476 void* buffer = fill_malloc(bytes);
477 if (mem == NULL) {
478 return buffer;
479 }
480 if (buffer) {
481 size_t old_size = dlmalloc_usable_size(mem);
482 size_t size = (bytes < old_size)?(bytes):(old_size);
483 memcpy(buffer, mem, size);
484 fill_free(mem);
485 }
486 return buffer;
487}
488
489void* fill_memalign(size_t alignment, size_t bytes)
490{
491 void* buffer = dlmemalign(alignment, bytes);
492 if (buffer) {
493 memset(buffer, CHK_SENTINEL_VALUE, bytes);
494 }
495 return buffer;
496}
497
498// =============================================================================
499// malloc leak functions
500// =============================================================================
501
502#define MEMALIGN_GUARD ((void*)0xA1A41520)
503
504void* leak_malloc(size_t bytes)
505{
506 // allocate enough space infront of the allocation to store the pointer for
507 // the alloc structure. This will making free'ing the structer really fast!
508
509 // 1. allocate enough memory and include our header
510 // 2. set the base pointer to be right after our header
511
Xi Wang7f5aa4f2012-03-14 02:48:39 -0400512 size_t size = bytes + sizeof(AllocationEntry);
513 if (size < bytes) { // Overflow.
514 return NULL;
515 }
516
517 void* base = dlmalloc(size);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800518 if (base != NULL) {
519 pthread_mutex_lock(&gAllocationsMutex);
520
521 intptr_t backtrace[BACKTRACE_SIZE];
522 size_t numEntries = get_backtrace(backtrace, BACKTRACE_SIZE);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800523
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800524 AllocationEntry* header = (AllocationEntry*)base;
525 header->entry = record_backtrace(backtrace, numEntries, bytes);
526 header->guard = GUARD;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800527
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800528 // now increment base to point to after our header.
529 // this should just work since our header is 8 bytes.
530 base = (AllocationEntry*)base + 1;
531
532 pthread_mutex_unlock(&gAllocationsMutex);
533 }
534
535 return base;
536}
537
538void leak_free(void* mem)
539{
540 if (mem != NULL) {
541 pthread_mutex_lock(&gAllocationsMutex);
542
543 // check the guard to make sure it is valid
544 AllocationEntry* header = (AllocationEntry*)mem - 1;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800545
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800546 if (header->guard != GUARD) {
547 // could be a memaligned block
548 if (((void**)mem)[-1] == MEMALIGN_GUARD) {
549 mem = ((void**)mem)[-2];
550 header = (AllocationEntry*)mem - 1;
551 }
552 }
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800553
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800554 if (header->guard == GUARD || is_valid_entry(header->entry)) {
555 // decrement the allocations
556 HashEntry* entry = header->entry;
557 entry->allocations--;
558 if (entry->allocations <= 0) {
559 remove_entry(entry);
560 dlfree(entry);
561 }
562
563 // now free the memory!
564 dlfree(header);
565 } else {
566 debug_log("WARNING bad header guard: '0x%x'! and invalid entry: %p\n",
567 header->guard, header->entry);
568 }
569
570 pthread_mutex_unlock(&gAllocationsMutex);
571 }
572}
573
574void* leak_calloc(size_t n_elements, size_t elem_size)
575{
576 size_t size;
577 void* ptr;
578
579 /* Fail on overflow - just to be safe even though this code runs only
580 * within the debugging C library, not the production one */
581 if (n_elements && MAX_SIZE_T / n_elements < elem_size) {
582 return NULL;
583 }
584 size = n_elements * elem_size;
585 ptr = leak_malloc(size);
586 if (ptr != NULL) {
587 memset(ptr, 0, size);
588 }
589 return ptr;
590}
591
592void* leak_realloc(void* oldMem, size_t bytes)
593{
594 if (oldMem == NULL) {
595 return leak_malloc(bytes);
596 }
597 void* newMem = NULL;
598 AllocationEntry* header = (AllocationEntry*)oldMem - 1;
599 if (header && header->guard == GUARD) {
600 size_t oldSize = header->entry->size & ~SIZE_FLAG_MASK;
601 newMem = leak_malloc(bytes);
602 if (newMem != NULL) {
603 size_t copySize = (oldSize <= bytes) ? oldSize : bytes;
604 memcpy(newMem, oldMem, copySize);
605 leak_free(oldMem);
606 }
607 } else {
608 newMem = dlrealloc(oldMem, bytes);
609 }
610 return newMem;
611}
612
613void* leak_memalign(size_t alignment, size_t bytes)
614{
615 // we can just use malloc
616 if (alignment <= MALLOC_ALIGNMENT)
617 return leak_malloc(bytes);
618
619 // need to make sure it's a power of two
620 if (alignment & (alignment-1))
621 alignment = 1L << (31 - __builtin_clz(alignment));
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800622
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800623 // here, aligment is at least MALLOC_ALIGNMENT<<1 bytes
624 // we will align by at least MALLOC_ALIGNMENT bytes
625 // and at most alignment-MALLOC_ALIGNMENT bytes
626 size_t size = (alignment-MALLOC_ALIGNMENT) + bytes;
Xi Wang7f5aa4f2012-03-14 02:48:39 -0400627 if (size < bytes) { // Overflow.
628 return NULL;
629 }
630
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800631 void* base = leak_malloc(size);
632 if (base != NULL) {
633 intptr_t ptr = (intptr_t)base;
634 if ((ptr % alignment) == 0)
635 return base;
636
637 // align the pointer
638 ptr += ((-ptr) % alignment);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800639
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800640 // there is always enough space for the base pointer and the guard
641 ((void**)ptr)[-1] = MEMALIGN_GUARD;
642 ((void**)ptr)[-2] = base;
643
644 return (void*)ptr;
645 }
646 return base;
647}
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800648
649/* Initializes malloc debugging framework.
650 * See comments on MallocDebugInit in malloc_debug_common.h
651 */
652int malloc_debug_initialize(void)
653{
654 // We don't really have anything that requires initialization here.
655 return 0;
656}