blob: 316d5fe2122fceed6a0a07ce2f3384c960336a52 [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
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
Xi Wang7f5aa4f2012-03-14 02:48:39 -0400266 size_t size = bytes + sizeof(AllocationEntry);
267 if (size < bytes) { // Overflow.
268 return NULL;
269 }
270
271 void* base = dlmalloc(size);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800272 if (base != NULL) {
273 pthread_mutex_lock(&gAllocationsMutex);
274
275 intptr_t backtrace[BACKTRACE_SIZE];
276 size_t numEntries = get_backtrace(backtrace, BACKTRACE_SIZE);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800277
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800278 AllocationEntry* header = (AllocationEntry*)base;
279 header->entry = record_backtrace(backtrace, numEntries, bytes);
280 header->guard = GUARD;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800281
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800282 // now increment base to point to after our header.
283 // this should just work since our header is 8 bytes.
284 base = (AllocationEntry*)base + 1;
285
286 pthread_mutex_unlock(&gAllocationsMutex);
287 }
288
289 return base;
290}
291
292void leak_free(void* mem)
293{
294 if (mem != NULL) {
295 pthread_mutex_lock(&gAllocationsMutex);
296
297 // check the guard to make sure it is valid
298 AllocationEntry* header = (AllocationEntry*)mem - 1;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800299
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800300 if (header->guard != GUARD) {
301 // could be a memaligned block
302 if (((void**)mem)[-1] == MEMALIGN_GUARD) {
303 mem = ((void**)mem)[-2];
304 header = (AllocationEntry*)mem - 1;
305 }
306 }
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800307
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800308 if (header->guard == GUARD || is_valid_entry(header->entry)) {
309 // decrement the allocations
310 HashEntry* entry = header->entry;
311 entry->allocations--;
312 if (entry->allocations <= 0) {
313 remove_entry(entry);
314 dlfree(entry);
315 }
316
317 // now free the memory!
318 dlfree(header);
319 } else {
320 debug_log("WARNING bad header guard: '0x%x'! and invalid entry: %p\n",
321 header->guard, header->entry);
322 }
323
324 pthread_mutex_unlock(&gAllocationsMutex);
325 }
326}
327
328void* leak_calloc(size_t n_elements, size_t elem_size)
329{
330 size_t size;
331 void* ptr;
332
333 /* Fail on overflow - just to be safe even though this code runs only
334 * within the debugging C library, not the production one */
335 if (n_elements && MAX_SIZE_T / n_elements < elem_size) {
336 return NULL;
337 }
338 size = n_elements * elem_size;
339 ptr = leak_malloc(size);
340 if (ptr != NULL) {
341 memset(ptr, 0, size);
342 }
343 return ptr;
344}
345
346void* leak_realloc(void* oldMem, size_t bytes)
347{
348 if (oldMem == NULL) {
349 return leak_malloc(bytes);
350 }
351 void* newMem = NULL;
352 AllocationEntry* header = (AllocationEntry*)oldMem - 1;
353 if (header && header->guard == GUARD) {
354 size_t oldSize = header->entry->size & ~SIZE_FLAG_MASK;
355 newMem = leak_malloc(bytes);
356 if (newMem != NULL) {
357 size_t copySize = (oldSize <= bytes) ? oldSize : bytes;
358 memcpy(newMem, oldMem, copySize);
359 leak_free(oldMem);
360 }
361 } else {
362 newMem = dlrealloc(oldMem, bytes);
363 }
364 return newMem;
365}
366
367void* leak_memalign(size_t alignment, size_t bytes)
368{
369 // we can just use malloc
370 if (alignment <= MALLOC_ALIGNMENT)
371 return leak_malloc(bytes);
372
373 // need to make sure it's a power of two
374 if (alignment & (alignment-1))
375 alignment = 1L << (31 - __builtin_clz(alignment));
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800376
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800377 // here, aligment is at least MALLOC_ALIGNMENT<<1 bytes
378 // we will align by at least MALLOC_ALIGNMENT bytes
379 // and at most alignment-MALLOC_ALIGNMENT bytes
380 size_t size = (alignment-MALLOC_ALIGNMENT) + bytes;
Xi Wang7f5aa4f2012-03-14 02:48:39 -0400381 if (size < bytes) { // Overflow.
382 return NULL;
383 }
384
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800385 void* base = leak_malloc(size);
386 if (base != NULL) {
387 intptr_t ptr = (intptr_t)base;
388 if ((ptr % alignment) == 0)
389 return base;
390
391 // align the pointer
392 ptr += ((-ptr) % alignment);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800393
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800394 // there is always enough space for the base pointer and the guard
395 ((void**)ptr)[-1] = MEMALIGN_GUARD;
396 ((void**)ptr)[-2] = base;
397
398 return (void*)ptr;
399 }
400 return base;
401}