blob: c13b520aec8b1246867353064d7a9fa285e83457 [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// =============================================================================
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070078
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080079struct AllocationEntry {
80 HashEntry* entry;
81 uint32_t guard;
82};
83
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070084static AllocationEntry* to_header(void* mem) {
85 return reinterpret_cast<AllocationEntry*>(mem) - 1;
86}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080087
88// =============================================================================
89// Hash Table functions
90// =============================================================================
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070091
92static uint32_t get_hash(intptr_t* backtrace, size_t numEntries) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080093 if (backtrace == NULL) return 0;
94
95 int hash = 0;
96 size_t i;
97 for (i = 0 ; i < numEntries ; i++) {
98 hash = (hash * 33) + (backtrace[i] >> 2);
99 }
100
101 return hash;
102}
103
104static HashEntry* find_entry(HashTable* table, int slot,
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700105 intptr_t* backtrace, size_t numEntries, size_t size) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800106 HashEntry* entry = table->slots[slot];
107 while (entry != NULL) {
108 //debug_log("backtrace: %p, entry: %p entry->backtrace: %p\n",
109 // backtrace, entry, (entry != NULL) ? entry->backtrace : NULL);
110 /*
111 * See if the entry matches exactly. We compare the "size" field,
112 * including the flag bits.
113 */
114 if (entry->size == size && entry->numEntries == numEntries &&
115 !memcmp(backtrace, entry->backtrace, numEntries * sizeof(intptr_t))) {
116 return entry;
117 }
118
119 entry = entry->next;
120 }
121
122 return NULL;
123}
124
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700125static HashEntry* record_backtrace(intptr_t* backtrace, size_t numEntries, size_t size) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800126 size_t hash = get_hash(backtrace, numEntries);
127 size_t slot = hash % HASHTABLE_SIZE;
128
129 if (size & SIZE_FLAG_MASK) {
130 debug_log("malloc_debug: allocation %zx exceeds bit width\n", size);
131 abort();
132 }
133
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700134 if (gMallocLeakZygoteChild) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800135 size |= SIZE_FLAG_ZYGOTE_CHILD;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700136 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800137
138 HashEntry* entry = find_entry(&gHashTable, slot, backtrace, numEntries, size);
139
140 if (entry != NULL) {
141 entry->allocations++;
142 } else {
143 // create a new entry
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700144 entry = static_cast<HashEntry*>(dlmalloc(sizeof(HashEntry) + numEntries*sizeof(intptr_t)));
145 if (!entry) {
André Goddard Rosa5751c542010-02-05 16:03:09 -0200146 return NULL;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700147 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800148 entry->allocations = 1;
149 entry->slot = slot;
150 entry->prev = NULL;
151 entry->next = gHashTable.slots[slot];
152 entry->numEntries = numEntries;
153 entry->size = size;
154
155 memcpy(entry->backtrace, backtrace, numEntries * sizeof(intptr_t));
156
157 gHashTable.slots[slot] = entry;
158
159 if (entry->next != NULL) {
160 entry->next->prev = entry;
161 }
162
163 // we just added an entry, increase the size of the hashtable
164 gHashTable.count++;
165 }
166
167 return entry;
168}
169
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700170static int is_valid_entry(HashEntry* entry) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800171 if (entry != NULL) {
172 int i;
173 for (i = 0 ; i < HASHTABLE_SIZE ; i++) {
174 HashEntry* e1 = gHashTable.slots[i];
175
176 while (e1 != NULL) {
177 if (e1 == entry) {
178 return 1;
179 }
180
181 e1 = e1->next;
182 }
183 }
184 }
185
186 return 0;
187}
188
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700189static void remove_entry(HashEntry* entry) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800190 HashEntry* prev = entry->prev;
191 HashEntry* next = entry->next;
192
193 if (prev != NULL) entry->prev->next = next;
194 if (next != NULL) entry->next->prev = prev;
195
196 if (prev == NULL) {
197 // we are the head of the list. set the head to be next
198 gHashTable.slots[entry->slot] = entry->next;
199 }
200
201 // we just removed and entry, decrease the size of the hashtable
202 gHashTable.count--;
203}
204
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800205// =============================================================================
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700206// malloc fill functions
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800207// =============================================================================
208
209#define CHK_FILL_FREE 0xef
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700210#define CHK_SENTINEL_VALUE 0xeb
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800211
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700212extern "C" void* fill_calloc(size_t n_elements, size_t elem_size) {
213 return dlcalloc(n_elements, elem_size);
214}
215
216extern "C" void* fill_malloc(size_t bytes) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800217 void* buffer = dlmalloc(bytes);
218 if (buffer) {
219 memset(buffer, CHK_SENTINEL_VALUE, bytes);
220 }
221 return buffer;
222}
223
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700224extern "C" void fill_free(void* mem) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800225 size_t bytes = dlmalloc_usable_size(mem);
226 memset(mem, CHK_FILL_FREE, bytes);
227 dlfree(mem);
228}
229
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700230extern "C" void* fill_realloc(void* mem, size_t bytes) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800231 void* buffer = fill_malloc(bytes);
232 if (mem == NULL) {
233 return buffer;
234 }
235 if (buffer) {
236 size_t old_size = dlmalloc_usable_size(mem);
237 size_t size = (bytes < old_size)?(bytes):(old_size);
238 memcpy(buffer, mem, size);
239 fill_free(mem);
240 }
241 return buffer;
242}
243
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700244extern "C" void* fill_memalign(size_t alignment, size_t bytes) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800245 void* buffer = dlmemalign(alignment, bytes);
246 if (buffer) {
247 memset(buffer, CHK_SENTINEL_VALUE, bytes);
248 }
249 return buffer;
250}
251
252// =============================================================================
253// malloc leak functions
254// =============================================================================
255
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700256static void* MEMALIGN_GUARD = reinterpret_cast<void*>(0xA1A41520);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800257
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700258extern __LIBC_HIDDEN__ int get_backtrace(intptr_t* addrs, size_t max_entries);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700259
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700260extern "C" void* leak_malloc(size_t bytes) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800261 // allocate enough space infront of the allocation to store the pointer for
262 // the alloc structure. This will making free'ing the structer really fast!
263
264 // 1. allocate enough memory and include our header
265 // 2. set the base pointer to be right after our header
266
Xi Wang7f5aa4f2012-03-14 02:48:39 -0400267 size_t size = bytes + sizeof(AllocationEntry);
268 if (size < bytes) { // Overflow.
269 return NULL;
270 }
271
272 void* base = dlmalloc(size);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800273 if (base != NULL) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700274 ScopedPthreadMutexLocker locker(&gAllocationsMutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800275
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700276 intptr_t backtrace[BACKTRACE_SIZE];
277 size_t numEntries = get_backtrace(backtrace, BACKTRACE_SIZE);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800278
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700279 AllocationEntry* header = reinterpret_cast<AllocationEntry*>(base);
280 header->entry = record_backtrace(backtrace, numEntries, bytes);
281 header->guard = GUARD;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800282
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700283 // now increment base to point to after our header.
284 // this should just work since our header is 8 bytes.
285 base = reinterpret_cast<AllocationEntry*>(base) + 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800286 }
287
288 return base;
289}
290
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700291extern "C" void leak_free(void* mem) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800292 if (mem != NULL) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700293 ScopedPthreadMutexLocker locker(&gAllocationsMutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800294
295 // check the guard to make sure it is valid
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700296 AllocationEntry* header = to_header(mem);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800297
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800298 if (header->guard != GUARD) {
299 // could be a memaligned block
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700300 if (reinterpret_cast<void**>(mem)[-1] == MEMALIGN_GUARD) {
301 mem = reinterpret_cast<void**>(mem)[-2];
302 header = to_header(mem);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800303 }
304 }
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800305
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800306 if (header->guard == GUARD || is_valid_entry(header->entry)) {
307 // decrement the allocations
308 HashEntry* entry = header->entry;
309 entry->allocations--;
310 if (entry->allocations <= 0) {
311 remove_entry(entry);
312 dlfree(entry);
313 }
314
315 // now free the memory!
316 dlfree(header);
317 } else {
318 debug_log("WARNING bad header guard: '0x%x'! and invalid entry: %p\n",
319 header->guard, header->entry);
320 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800321 }
322}
323
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700324extern "C" void* leak_calloc(size_t n_elements, size_t elem_size) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800325 /* Fail on overflow - just to be safe even though this code runs only
326 * within the debugging C library, not the production one */
327 if (n_elements && MAX_SIZE_T / n_elements < elem_size) {
328 return NULL;
329 }
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700330 size_t size = n_elements * elem_size;
331 void* ptr = leak_malloc(size);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800332 if (ptr != NULL) {
333 memset(ptr, 0, size);
334 }
335 return ptr;
336}
337
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700338extern "C" void* leak_realloc(void* oldMem, size_t bytes) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800339 if (oldMem == NULL) {
340 return leak_malloc(bytes);
341 }
342 void* newMem = NULL;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700343 AllocationEntry* header = to_header(oldMem);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800344 if (header && header->guard == GUARD) {
345 size_t oldSize = header->entry->size & ~SIZE_FLAG_MASK;
346 newMem = leak_malloc(bytes);
347 if (newMem != NULL) {
348 size_t copySize = (oldSize <= bytes) ? oldSize : bytes;
349 memcpy(newMem, oldMem, copySize);
350 leak_free(oldMem);
351 }
352 } else {
353 newMem = dlrealloc(oldMem, bytes);
354 }
355 return newMem;
356}
357
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700358extern "C" void* leak_memalign(size_t alignment, size_t bytes) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800359 // we can just use malloc
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700360 if (alignment <= MALLOC_ALIGNMENT) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800361 return leak_malloc(bytes);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700362 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800363
364 // need to make sure it's a power of two
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700365 if (alignment & (alignment-1)) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800366 alignment = 1L << (31 - __builtin_clz(alignment));
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700367 }
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800368
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800369 // here, aligment is at least MALLOC_ALIGNMENT<<1 bytes
370 // we will align by at least MALLOC_ALIGNMENT bytes
371 // and at most alignment-MALLOC_ALIGNMENT bytes
372 size_t size = (alignment-MALLOC_ALIGNMENT) + bytes;
Xi Wang7f5aa4f2012-03-14 02:48:39 -0400373 if (size < bytes) { // Overflow.
374 return NULL;
375 }
376
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800377 void* base = leak_malloc(size);
378 if (base != NULL) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700379 intptr_t ptr = reinterpret_cast<intptr_t>(base);
380 if ((ptr % alignment) == 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800381 return base;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700382 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800383
384 // align the pointer
385 ptr += ((-ptr) % alignment);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800386
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800387 // there is always enough space for the base pointer and the guard
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700388 reinterpret_cast<void**>(ptr)[-1] = MEMALIGN_GUARD;
389 reinterpret_cast<void**>(ptr)[-2] = base;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800390
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700391 return reinterpret_cast<void*>(ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800392 }
393 return base;
394}