blob: 0b186458feb517da59945d06b4eb3b1c34b766b1 [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 */
28#include <errno.h>
29#include <pthread.h>
30#include <stdio.h>
31#include <arpa/inet.h>
32#include <sys/socket.h>
33#include <stdlib.h>
34#include <string.h>
35#include <unistd.h>
36#include <errno.h>
37#include <stddef.h>
38#include <stdarg.h>
39#include <fcntl.h>
40#include <unwind.h>
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080041#include <dlfcn.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080042
43#include <sys/socket.h>
44#include <sys/un.h>
45#include <sys/select.h>
46#include <sys/types.h>
47#include <sys/system_properties.h>
48
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;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080064
65// =============================================================================
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070066// stack trace functions
Andy McFadden39f37452009-07-21 15:25:23 -070067// =============================================================================
68
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080069#define MALLOC_ALIGNMENT 8
70#define GUARD 0x48151642
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080071#define DEBUG 0
72
73// =============================================================================
74// Structures
75// =============================================================================
76typedef struct AllocationEntry AllocationEntry;
77struct AllocationEntry {
78 HashEntry* entry;
79 uint32_t guard;
80};
81
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080082
83// =============================================================================
84// Hash Table functions
85// =============================================================================
86static uint32_t get_hash(intptr_t* backtrace, size_t numEntries)
87{
88 if (backtrace == NULL) return 0;
89
90 int hash = 0;
91 size_t i;
92 for (i = 0 ; i < numEntries ; i++) {
93 hash = (hash * 33) + (backtrace[i] >> 2);
94 }
95
96 return hash;
97}
98
99static HashEntry* find_entry(HashTable* table, int slot,
100 intptr_t* backtrace, size_t numEntries, size_t size)
101{
102 HashEntry* entry = table->slots[slot];
103 while (entry != NULL) {
104 //debug_log("backtrace: %p, entry: %p entry->backtrace: %p\n",
105 // backtrace, entry, (entry != NULL) ? entry->backtrace : NULL);
106 /*
107 * See if the entry matches exactly. We compare the "size" field,
108 * including the flag bits.
109 */
110 if (entry->size == size && entry->numEntries == numEntries &&
111 !memcmp(backtrace, entry->backtrace, numEntries * sizeof(intptr_t))) {
112 return entry;
113 }
114
115 entry = entry->next;
116 }
117
118 return NULL;
119}
120
121static HashEntry* record_backtrace(intptr_t* backtrace, size_t numEntries, size_t size)
122{
123 size_t hash = get_hash(backtrace, numEntries);
124 size_t slot = hash % HASHTABLE_SIZE;
125
126 if (size & SIZE_FLAG_MASK) {
127 debug_log("malloc_debug: allocation %zx exceeds bit width\n", size);
128 abort();
129 }
130
131 if (gMallocLeakZygoteChild)
132 size |= SIZE_FLAG_ZYGOTE_CHILD;
133
134 HashEntry* entry = find_entry(&gHashTable, slot, backtrace, numEntries, size);
135
136 if (entry != NULL) {
137 entry->allocations++;
138 } else {
139 // create a new entry
140 entry = (HashEntry*)dlmalloc(sizeof(HashEntry) + numEntries*sizeof(intptr_t));
André Goddard Rosa5751c542010-02-05 16:03:09 -0200141 if (!entry)
142 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800143 entry->allocations = 1;
144 entry->slot = slot;
145 entry->prev = NULL;
146 entry->next = gHashTable.slots[slot];
147 entry->numEntries = numEntries;
148 entry->size = size;
149
150 memcpy(entry->backtrace, backtrace, numEntries * sizeof(intptr_t));
151
152 gHashTable.slots[slot] = entry;
153
154 if (entry->next != NULL) {
155 entry->next->prev = entry;
156 }
157
158 // we just added an entry, increase the size of the hashtable
159 gHashTable.count++;
160 }
161
162 return entry;
163}
164
165static int is_valid_entry(HashEntry* entry)
166{
167 if (entry != NULL) {
168 int i;
169 for (i = 0 ; i < HASHTABLE_SIZE ; i++) {
170 HashEntry* e1 = gHashTable.slots[i];
171
172 while (e1 != NULL) {
173 if (e1 == entry) {
174 return 1;
175 }
176
177 e1 = e1->next;
178 }
179 }
180 }
181
182 return 0;
183}
184
185static void remove_entry(HashEntry* entry)
186{
187 HashEntry* prev = entry->prev;
188 HashEntry* next = entry->next;
189
190 if (prev != NULL) entry->prev->next = next;
191 if (next != NULL) entry->next->prev = prev;
192
193 if (prev == NULL) {
194 // we are the head of the list. set the head to be next
195 gHashTable.slots[entry->slot] = entry->next;
196 }
197
198 // we just removed and entry, decrease the size of the hashtable
199 gHashTable.count--;
200}
201
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800202// =============================================================================
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700203// malloc fill functions
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800204// =============================================================================
205
206#define CHK_FILL_FREE 0xef
Bruce Beare89d3fdc2011-09-21 12:44:05 +0200207#define CHK_SENTINEL_VALUE (char)0xeb
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800208
209void* fill_malloc(size_t bytes)
210{
211 void* buffer = dlmalloc(bytes);
212 if (buffer) {
213 memset(buffer, CHK_SENTINEL_VALUE, bytes);
214 }
215 return buffer;
216}
217
218void fill_free(void* mem)
219{
220 size_t bytes = dlmalloc_usable_size(mem);
221 memset(mem, CHK_FILL_FREE, bytes);
222 dlfree(mem);
223}
224
225void* fill_realloc(void* mem, size_t bytes)
226{
227 void* buffer = fill_malloc(bytes);
228 if (mem == NULL) {
229 return buffer;
230 }
231 if (buffer) {
232 size_t old_size = dlmalloc_usable_size(mem);
233 size_t size = (bytes < old_size)?(bytes):(old_size);
234 memcpy(buffer, mem, size);
235 fill_free(mem);
236 }
237 return buffer;
238}
239
240void* fill_memalign(size_t alignment, size_t bytes)
241{
242 void* buffer = dlmemalign(alignment, bytes);
243 if (buffer) {
244 memset(buffer, CHK_SENTINEL_VALUE, bytes);
245 }
246 return buffer;
247}
248
249// =============================================================================
250// malloc leak functions
251// =============================================================================
252
253#define MEMALIGN_GUARD ((void*)0xA1A41520)
254
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700255extern __LIBC_HIDDEN__
256int get_backtrace(intptr_t* addrs, size_t max_entries);
257
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800258void* leak_malloc(size_t bytes)
259{
260 // allocate enough space infront of the allocation to store the pointer for
261 // the alloc structure. This will making free'ing the structer really fast!
262
263 // 1. allocate enough memory and include our header
264 // 2. set the base pointer to be right after our header
265
266 void* base = dlmalloc(bytes + sizeof(AllocationEntry));
267 if (base != NULL) {
268 pthread_mutex_lock(&gAllocationsMutex);
269
270 intptr_t backtrace[BACKTRACE_SIZE];
271 size_t numEntries = get_backtrace(backtrace, BACKTRACE_SIZE);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800272
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800273 AllocationEntry* header = (AllocationEntry*)base;
274 header->entry = record_backtrace(backtrace, numEntries, bytes);
275 header->guard = GUARD;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800276
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800277 // now increment base to point to after our header.
278 // this should just work since our header is 8 bytes.
279 base = (AllocationEntry*)base + 1;
280
281 pthread_mutex_unlock(&gAllocationsMutex);
282 }
283
284 return base;
285}
286
287void leak_free(void* mem)
288{
289 if (mem != NULL) {
290 pthread_mutex_lock(&gAllocationsMutex);
291
292 // check the guard to make sure it is valid
293 AllocationEntry* header = (AllocationEntry*)mem - 1;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800294
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800295 if (header->guard != GUARD) {
296 // could be a memaligned block
297 if (((void**)mem)[-1] == MEMALIGN_GUARD) {
298 mem = ((void**)mem)[-2];
299 header = (AllocationEntry*)mem - 1;
300 }
301 }
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800302
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800303 if (header->guard == GUARD || is_valid_entry(header->entry)) {
304 // decrement the allocations
305 HashEntry* entry = header->entry;
306 entry->allocations--;
307 if (entry->allocations <= 0) {
308 remove_entry(entry);
309 dlfree(entry);
310 }
311
312 // now free the memory!
313 dlfree(header);
314 } else {
315 debug_log("WARNING bad header guard: '0x%x'! and invalid entry: %p\n",
316 header->guard, header->entry);
317 }
318
319 pthread_mutex_unlock(&gAllocationsMutex);
320 }
321}
322
323void* leak_calloc(size_t n_elements, size_t elem_size)
324{
325 size_t size;
326 void* ptr;
327
328 /* Fail on overflow - just to be safe even though this code runs only
329 * within the debugging C library, not the production one */
330 if (n_elements && MAX_SIZE_T / n_elements < elem_size) {
331 return NULL;
332 }
333 size = n_elements * elem_size;
334 ptr = leak_malloc(size);
335 if (ptr != NULL) {
336 memset(ptr, 0, size);
337 }
338 return ptr;
339}
340
341void* leak_realloc(void* oldMem, size_t bytes)
342{
343 if (oldMem == NULL) {
344 return leak_malloc(bytes);
345 }
346 void* newMem = NULL;
347 AllocationEntry* header = (AllocationEntry*)oldMem - 1;
348 if (header && header->guard == GUARD) {
349 size_t oldSize = header->entry->size & ~SIZE_FLAG_MASK;
350 newMem = leak_malloc(bytes);
351 if (newMem != NULL) {
352 size_t copySize = (oldSize <= bytes) ? oldSize : bytes;
353 memcpy(newMem, oldMem, copySize);
354 leak_free(oldMem);
355 }
356 } else {
357 newMem = dlrealloc(oldMem, bytes);
358 }
359 return newMem;
360}
361
362void* leak_memalign(size_t alignment, size_t bytes)
363{
364 // we can just use malloc
365 if (alignment <= MALLOC_ALIGNMENT)
366 return leak_malloc(bytes);
367
368 // need to make sure it's a power of two
369 if (alignment & (alignment-1))
370 alignment = 1L << (31 - __builtin_clz(alignment));
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800371
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800372 // here, aligment is at least MALLOC_ALIGNMENT<<1 bytes
373 // we will align by at least MALLOC_ALIGNMENT bytes
374 // and at most alignment-MALLOC_ALIGNMENT bytes
375 size_t size = (alignment-MALLOC_ALIGNMENT) + bytes;
376 void* base = leak_malloc(size);
377 if (base != NULL) {
378 intptr_t ptr = (intptr_t)base;
379 if ((ptr % alignment) == 0)
380 return base;
381
382 // align the pointer
383 ptr += ((-ptr) % alignment);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800384
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800385 // there is always enough space for the base pointer and the guard
386 ((void**)ptr)[-1] = MEMALIGN_GUARD;
387 ((void**)ptr)[-2] = base;
388
389 return (void*)ptr;
390 }
391 return base;
392}