blob: 4bcf8e5b8a7722a553233fb3c27660af8fb87d41 [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;
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
Jin Wei9862f5e2012-08-01 14:48:57 +080069#ifndef MALLOC_ALIGNMENT
70#define MALLOC_ALIGNMENT ((size_t)8U)
71#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080072#define GUARD 0x48151642
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080073#define DEBUG 0
74
75// =============================================================================
76// Structures
77// =============================================================================
78typedef struct AllocationEntry AllocationEntry;
79struct AllocationEntry {
80 HashEntry* entry;
81 uint32_t guard;
82};
83
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080084
85// =============================================================================
86// Hash Table functions
87// =============================================================================
88static uint32_t get_hash(intptr_t* backtrace, size_t numEntries)
89{
90 if (backtrace == NULL) return 0;
91
92 int hash = 0;
93 size_t i;
94 for (i = 0 ; i < numEntries ; i++) {
95 hash = (hash * 33) + (backtrace[i] >> 2);
96 }
97
98 return hash;
99}
100
101static HashEntry* find_entry(HashTable* table, int slot,
102 intptr_t* backtrace, size_t numEntries, size_t size)
103{
104 HashEntry* entry = table->slots[slot];
105 while (entry != NULL) {
106 //debug_log("backtrace: %p, entry: %p entry->backtrace: %p\n",
107 // backtrace, entry, (entry != NULL) ? entry->backtrace : NULL);
108 /*
109 * See if the entry matches exactly. We compare the "size" field,
110 * including the flag bits.
111 */
112 if (entry->size == size && entry->numEntries == numEntries &&
113 !memcmp(backtrace, entry->backtrace, numEntries * sizeof(intptr_t))) {
114 return entry;
115 }
116
117 entry = entry->next;
118 }
119
120 return NULL;
121}
122
123static HashEntry* record_backtrace(intptr_t* backtrace, size_t numEntries, size_t size)
124{
125 size_t hash = get_hash(backtrace, numEntries);
126 size_t slot = hash % HASHTABLE_SIZE;
127
128 if (size & SIZE_FLAG_MASK) {
129 debug_log("malloc_debug: allocation %zx exceeds bit width\n", size);
130 abort();
131 }
132
133 if (gMallocLeakZygoteChild)
134 size |= SIZE_FLAG_ZYGOTE_CHILD;
135
136 HashEntry* entry = find_entry(&gHashTable, slot, backtrace, numEntries, size);
137
138 if (entry != NULL) {
139 entry->allocations++;
140 } else {
141 // create a new entry
142 entry = (HashEntry*)dlmalloc(sizeof(HashEntry) + numEntries*sizeof(intptr_t));
André Goddard Rosa5751c542010-02-05 16:03:09 -0200143 if (!entry)
144 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800145 entry->allocations = 1;
146 entry->slot = slot;
147 entry->prev = NULL;
148 entry->next = gHashTable.slots[slot];
149 entry->numEntries = numEntries;
150 entry->size = size;
151
152 memcpy(entry->backtrace, backtrace, numEntries * sizeof(intptr_t));
153
154 gHashTable.slots[slot] = entry;
155
156 if (entry->next != NULL) {
157 entry->next->prev = entry;
158 }
159
160 // we just added an entry, increase the size of the hashtable
161 gHashTable.count++;
162 }
163
164 return entry;
165}
166
167static int is_valid_entry(HashEntry* entry)
168{
169 if (entry != NULL) {
170 int i;
171 for (i = 0 ; i < HASHTABLE_SIZE ; i++) {
172 HashEntry* e1 = gHashTable.slots[i];
173
174 while (e1 != NULL) {
175 if (e1 == entry) {
176 return 1;
177 }
178
179 e1 = e1->next;
180 }
181 }
182 }
183
184 return 0;
185}
186
187static void remove_entry(HashEntry* entry)
188{
189 HashEntry* prev = entry->prev;
190 HashEntry* next = entry->next;
191
192 if (prev != NULL) entry->prev->next = next;
193 if (next != NULL) entry->next->prev = prev;
194
195 if (prev == NULL) {
196 // we are the head of the list. set the head to be next
197 gHashTable.slots[entry->slot] = entry->next;
198 }
199
200 // we just removed and entry, decrease the size of the hashtable
201 gHashTable.count--;
202}
203
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800204// =============================================================================
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700205// malloc fill functions
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800206// =============================================================================
207
208#define CHK_FILL_FREE 0xef
Bruce Beare89d3fdc2011-09-21 12:44:05 +0200209#define CHK_SENTINEL_VALUE (char)0xeb
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800210
211void* fill_malloc(size_t bytes)
212{
213 void* buffer = dlmalloc(bytes);
214 if (buffer) {
215 memset(buffer, CHK_SENTINEL_VALUE, bytes);
216 }
217 return buffer;
218}
219
220void fill_free(void* mem)
221{
222 size_t bytes = dlmalloc_usable_size(mem);
223 memset(mem, CHK_FILL_FREE, bytes);
224 dlfree(mem);
225}
226
227void* fill_realloc(void* mem, size_t bytes)
228{
229 void* buffer = fill_malloc(bytes);
230 if (mem == NULL) {
231 return buffer;
232 }
233 if (buffer) {
234 size_t old_size = dlmalloc_usable_size(mem);
235 size_t size = (bytes < old_size)?(bytes):(old_size);
236 memcpy(buffer, mem, size);
237 fill_free(mem);
238 }
239 return buffer;
240}
241
242void* fill_memalign(size_t alignment, size_t bytes)
243{
244 void* buffer = dlmemalign(alignment, bytes);
245 if (buffer) {
246 memset(buffer, CHK_SENTINEL_VALUE, bytes);
247 }
248 return buffer;
249}
250
251// =============================================================================
252// malloc leak functions
253// =============================================================================
254
255#define MEMALIGN_GUARD ((void*)0xA1A41520)
256
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700257extern __LIBC_HIDDEN__
258int get_backtrace(intptr_t* addrs, size_t max_entries);
259
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800260void* leak_malloc(size_t bytes)
261{
262 // allocate enough space infront of the allocation to store the pointer for
263 // the alloc structure. This will making free'ing the structer really fast!
264
265 // 1. allocate enough memory and include our header
266 // 2. set the base pointer to be right after our header
267
Xi Wang7f5aa4f2012-03-14 02:48:39 -0400268 size_t size = bytes + sizeof(AllocationEntry);
269 if (size < bytes) { // Overflow.
270 return NULL;
271 }
272
273 void* base = dlmalloc(size);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800274 if (base != NULL) {
275 pthread_mutex_lock(&gAllocationsMutex);
276
277 intptr_t backtrace[BACKTRACE_SIZE];
278 size_t numEntries = get_backtrace(backtrace, BACKTRACE_SIZE);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800279
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800280 AllocationEntry* header = (AllocationEntry*)base;
281 header->entry = record_backtrace(backtrace, numEntries, bytes);
282 header->guard = GUARD;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800283
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800284 // now increment base to point to after our header.
285 // this should just work since our header is 8 bytes.
286 base = (AllocationEntry*)base + 1;
287
288 pthread_mutex_unlock(&gAllocationsMutex);
289 }
290
291 return base;
292}
293
294void leak_free(void* mem)
295{
296 if (mem != NULL) {
297 pthread_mutex_lock(&gAllocationsMutex);
298
299 // check the guard to make sure it is valid
300 AllocationEntry* header = (AllocationEntry*)mem - 1;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800301
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800302 if (header->guard != GUARD) {
303 // could be a memaligned block
304 if (((void**)mem)[-1] == MEMALIGN_GUARD) {
305 mem = ((void**)mem)[-2];
306 header = (AllocationEntry*)mem - 1;
307 }
308 }
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800309
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800310 if (header->guard == GUARD || is_valid_entry(header->entry)) {
311 // decrement the allocations
312 HashEntry* entry = header->entry;
313 entry->allocations--;
314 if (entry->allocations <= 0) {
315 remove_entry(entry);
316 dlfree(entry);
317 }
318
319 // now free the memory!
320 dlfree(header);
321 } else {
322 debug_log("WARNING bad header guard: '0x%x'! and invalid entry: %p\n",
323 header->guard, header->entry);
324 }
325
326 pthread_mutex_unlock(&gAllocationsMutex);
327 }
328}
329
330void* leak_calloc(size_t n_elements, size_t elem_size)
331{
332 size_t size;
333 void* ptr;
334
335 /* Fail on overflow - just to be safe even though this code runs only
336 * within the debugging C library, not the production one */
337 if (n_elements && MAX_SIZE_T / n_elements < elem_size) {
338 return NULL;
339 }
340 size = n_elements * elem_size;
341 ptr = leak_malloc(size);
342 if (ptr != NULL) {
343 memset(ptr, 0, size);
344 }
345 return ptr;
346}
347
348void* leak_realloc(void* oldMem, size_t bytes)
349{
350 if (oldMem == NULL) {
351 return leak_malloc(bytes);
352 }
353 void* newMem = NULL;
354 AllocationEntry* header = (AllocationEntry*)oldMem - 1;
355 if (header && header->guard == GUARD) {
356 size_t oldSize = header->entry->size & ~SIZE_FLAG_MASK;
357 newMem = leak_malloc(bytes);
358 if (newMem != NULL) {
359 size_t copySize = (oldSize <= bytes) ? oldSize : bytes;
360 memcpy(newMem, oldMem, copySize);
361 leak_free(oldMem);
362 }
363 } else {
364 newMem = dlrealloc(oldMem, bytes);
365 }
366 return newMem;
367}
368
369void* leak_memalign(size_t alignment, size_t bytes)
370{
371 // we can just use malloc
372 if (alignment <= MALLOC_ALIGNMENT)
373 return leak_malloc(bytes);
374
375 // need to make sure it's a power of two
376 if (alignment & (alignment-1))
377 alignment = 1L << (31 - __builtin_clz(alignment));
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800378
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800379 // here, aligment is at least MALLOC_ALIGNMENT<<1 bytes
380 // we will align by at least MALLOC_ALIGNMENT bytes
381 // and at most alignment-MALLOC_ALIGNMENT bytes
382 size_t size = (alignment-MALLOC_ALIGNMENT) + bytes;
Xi Wang7f5aa4f2012-03-14 02:48:39 -0400383 if (size < bytes) { // Overflow.
384 return NULL;
385 }
386
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800387 void* base = leak_malloc(size);
388 if (base != NULL) {
389 intptr_t ptr = (intptr_t)base;
390 if ((ptr % alignment) == 0)
391 return base;
392
393 // align the pointer
394 ptr += ((-ptr) % alignment);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800395
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800396 // there is always enough space for the base pointer and the guard
397 ((void**)ptr)[-1] = MEMALIGN_GUARD;
398 ((void**)ptr)[-2] = base;
399
400 return (void*)ptr;
401 }
402 return base;
403}