blob: 9e63a863fcdaae8ab5359e5041590af78017797b [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
Elliott Hughes3b297c42012-10-11 16:08:51 -070029#include <arpa/inet.h>
Xi Wang7f5aa4f2012-03-14 02:48:39 -040030#include <dlfcn.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080031#include <errno.h>
Xi Wang7f5aa4f2012-03-14 02:48:39 -040032#include <fcntl.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080033#include <pthread.h>
Xi Wang7f5aa4f2012-03-14 02:48:39 -040034#include <stdarg.h>
35#include <stddef.h>
36#include <stdint.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080037#include <stdio.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080038#include <stdlib.h>
39#include <string.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080040#include <sys/select.h>
Xi Wang7f5aa4f2012-03-14 02:48:39 -040041#include <sys/socket.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080042#include <sys/system_properties.h>
Xi Wang7f5aa4f2012-03-14 02:48:39 -040043#include <sys/types.h>
44#include <sys/un.h>
Elliott Hughes3b297c42012-10-11 16:08:51 -070045#include <unistd.h>
46#include <unwind.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080047
Elliott Hughes1e980b62013-01-17 18:36:06 -080048#include "debug_stacktrace.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080049#include "dlmalloc.h"
50#include "logd.h"
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080051#include "malloc_debug_common.h"
Elliott Hughes3b297c42012-10-11 16:08:51 -070052#include "ScopedPthreadMutexLocker.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080053
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080054// This file should be included into the build only when
55// MALLOC_LEAK_CHECK, or MALLOC_QEMU_INSTRUMENT, or both
56// macros are defined.
57#ifndef MALLOC_LEAK_CHECK
58#error MALLOC_LEAK_CHECK is not defined.
59#endif // !MALLOC_LEAK_CHECK
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080060
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080061// Global variables defined in malloc_debug_common.c
62extern int gMallocLeakZygoteChild;
63extern pthread_mutex_t gAllocationsMutex;
64extern HashTable gHashTable;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080065
66// =============================================================================
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070067// stack trace functions
Andy McFadden39f37452009-07-21 15:25:23 -070068// =============================================================================
69
Jin Wei9862f5e2012-08-01 14:48:57 +080070#ifndef MALLOC_ALIGNMENT
71#define MALLOC_ALIGNMENT ((size_t)8U)
72#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080073#define GUARD 0x48151642
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080074#define DEBUG 0
75
76// =============================================================================
77// Structures
78// =============================================================================
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070079
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080080struct AllocationEntry {
81 HashEntry* entry;
82 uint32_t guard;
83};
84
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070085static AllocationEntry* to_header(void* mem) {
86 return reinterpret_cast<AllocationEntry*>(mem) - 1;
87}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080088
89// =============================================================================
90// Hash Table functions
91// =============================================================================
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070092
Elliott Hughes239e7a02013-01-25 17:13:45 -080093static uint32_t get_hash(uintptr_t* backtrace, size_t numEntries) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080094 if (backtrace == NULL) return 0;
95
96 int hash = 0;
97 size_t i;
98 for (i = 0 ; i < numEntries ; i++) {
99 hash = (hash * 33) + (backtrace[i] >> 2);
100 }
101
102 return hash;
103}
104
105static HashEntry* find_entry(HashTable* table, int slot,
Elliott Hughes239e7a02013-01-25 17:13:45 -0800106 uintptr_t* backtrace, size_t numEntries, size_t size) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800107 HashEntry* entry = table->slots[slot];
108 while (entry != NULL) {
109 //debug_log("backtrace: %p, entry: %p entry->backtrace: %p\n",
110 // backtrace, entry, (entry != NULL) ? entry->backtrace : NULL);
111 /*
112 * See if the entry matches exactly. We compare the "size" field,
113 * including the flag bits.
114 */
115 if (entry->size == size && entry->numEntries == numEntries &&
Elliott Hughes239e7a02013-01-25 17:13:45 -0800116 !memcmp(backtrace, entry->backtrace, numEntries * sizeof(uintptr_t))) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800117 return entry;
118 }
119
120 entry = entry->next;
121 }
122
123 return NULL;
124}
125
Elliott Hughes239e7a02013-01-25 17:13:45 -0800126static HashEntry* record_backtrace(uintptr_t* backtrace, size_t numEntries, size_t size) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800127 size_t hash = get_hash(backtrace, numEntries);
128 size_t slot = hash % HASHTABLE_SIZE;
129
130 if (size & SIZE_FLAG_MASK) {
131 debug_log("malloc_debug: allocation %zx exceeds bit width\n", size);
132 abort();
133 }
134
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700135 if (gMallocLeakZygoteChild) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800136 size |= SIZE_FLAG_ZYGOTE_CHILD;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700137 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800138
139 HashEntry* entry = find_entry(&gHashTable, slot, backtrace, numEntries, size);
140
141 if (entry != NULL) {
142 entry->allocations++;
143 } else {
144 // create a new entry
Elliott Hughes239e7a02013-01-25 17:13:45 -0800145 entry = static_cast<HashEntry*>(dlmalloc(sizeof(HashEntry) + numEntries*sizeof(uintptr_t)));
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700146 if (!entry) {
André Goddard Rosa5751c542010-02-05 16:03:09 -0200147 return NULL;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700148 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800149 entry->allocations = 1;
150 entry->slot = slot;
151 entry->prev = NULL;
152 entry->next = gHashTable.slots[slot];
153 entry->numEntries = numEntries;
154 entry->size = size;
155
Elliott Hughes239e7a02013-01-25 17:13:45 -0800156 memcpy(entry->backtrace, backtrace, numEntries * sizeof(uintptr_t));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800157
158 gHashTable.slots[slot] = entry;
159
160 if (entry->next != NULL) {
161 entry->next->prev = entry;
162 }
163
164 // we just added an entry, increase the size of the hashtable
165 gHashTable.count++;
166 }
167
168 return entry;
169}
170
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700171static int is_valid_entry(HashEntry* entry) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800172 if (entry != NULL) {
173 int i;
174 for (i = 0 ; i < HASHTABLE_SIZE ; i++) {
175 HashEntry* e1 = gHashTable.slots[i];
176
177 while (e1 != NULL) {
178 if (e1 == entry) {
179 return 1;
180 }
181
182 e1 = e1->next;
183 }
184 }
185 }
186
187 return 0;
188}
189
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700190static void remove_entry(HashEntry* entry) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800191 HashEntry* prev = entry->prev;
192 HashEntry* next = entry->next;
193
194 if (prev != NULL) entry->prev->next = next;
195 if (next != NULL) entry->next->prev = prev;
196
197 if (prev == NULL) {
198 // we are the head of the list. set the head to be next
199 gHashTable.slots[entry->slot] = entry->next;
200 }
201
202 // we just removed and entry, decrease the size of the hashtable
203 gHashTable.count--;
204}
205
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800206// =============================================================================
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700207// malloc fill functions
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800208// =============================================================================
209
210#define CHK_FILL_FREE 0xef
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700211#define CHK_SENTINEL_VALUE 0xeb
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800212
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700213extern "C" void* fill_calloc(size_t n_elements, size_t elem_size) {
214 return dlcalloc(n_elements, elem_size);
215}
216
217extern "C" void* fill_malloc(size_t bytes) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800218 void* buffer = dlmalloc(bytes);
219 if (buffer) {
220 memset(buffer, CHK_SENTINEL_VALUE, bytes);
221 }
222 return buffer;
223}
224
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700225extern "C" void fill_free(void* mem) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800226 size_t bytes = dlmalloc_usable_size(mem);
227 memset(mem, CHK_FILL_FREE, bytes);
228 dlfree(mem);
229}
230
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700231extern "C" void* fill_realloc(void* mem, size_t bytes) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800232 void* buffer = fill_malloc(bytes);
233 if (mem == NULL) {
234 return buffer;
235 }
236 if (buffer) {
237 size_t old_size = dlmalloc_usable_size(mem);
238 size_t size = (bytes < old_size)?(bytes):(old_size);
239 memcpy(buffer, mem, size);
240 fill_free(mem);
241 }
242 return buffer;
243}
244
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700245extern "C" void* fill_memalign(size_t alignment, size_t bytes) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800246 void* buffer = dlmemalign(alignment, bytes);
247 if (buffer) {
248 memset(buffer, CHK_SENTINEL_VALUE, bytes);
249 }
250 return buffer;
251}
252
253// =============================================================================
254// malloc leak functions
255// =============================================================================
256
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700257static void* MEMALIGN_GUARD = reinterpret_cast<void*>(0xA1A41520);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800258
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700259extern "C" void* leak_malloc(size_t bytes) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800260 // 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) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700273 ScopedPthreadMutexLocker locker(&gAllocationsMutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800274
Elliott Hughes239e7a02013-01-25 17:13:45 -0800275 uintptr_t backtrace[BACKTRACE_SIZE];
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700276 size_t numEntries = get_backtrace(backtrace, BACKTRACE_SIZE);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800277
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700278 AllocationEntry* header = reinterpret_cast<AllocationEntry*>(base);
279 header->entry = record_backtrace(backtrace, numEntries, bytes);
280 header->guard = GUARD;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800281
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700282 // now increment base to point to after our header.
283 // this should just work since our header is 8 bytes.
284 base = reinterpret_cast<AllocationEntry*>(base) + 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800285 }
286
287 return base;
288}
289
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700290extern "C" void leak_free(void* mem) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800291 if (mem != NULL) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700292 ScopedPthreadMutexLocker locker(&gAllocationsMutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800293
294 // check the guard to make sure it is valid
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700295 AllocationEntry* header = to_header(mem);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800296
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800297 if (header->guard != GUARD) {
298 // could be a memaligned block
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700299 if (reinterpret_cast<void**>(mem)[-1] == MEMALIGN_GUARD) {
300 mem = reinterpret_cast<void**>(mem)[-2];
301 header = to_header(mem);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800302 }
303 }
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800304
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800305 if (header->guard == GUARD || is_valid_entry(header->entry)) {
306 // decrement the allocations
307 HashEntry* entry = header->entry;
308 entry->allocations--;
309 if (entry->allocations <= 0) {
310 remove_entry(entry);
311 dlfree(entry);
312 }
313
314 // now free the memory!
315 dlfree(header);
316 } else {
317 debug_log("WARNING bad header guard: '0x%x'! and invalid entry: %p\n",
318 header->guard, header->entry);
319 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800320 }
321}
322
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700323extern "C" void* leak_calloc(size_t n_elements, size_t elem_size) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800324 /* Fail on overflow - just to be safe even though this code runs only
325 * within the debugging C library, not the production one */
326 if (n_elements && MAX_SIZE_T / n_elements < elem_size) {
327 return NULL;
328 }
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700329 size_t size = n_elements * elem_size;
330 void* ptr = leak_malloc(size);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800331 if (ptr != NULL) {
332 memset(ptr, 0, size);
333 }
334 return ptr;
335}
336
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700337extern "C" void* leak_realloc(void* oldMem, size_t bytes) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800338 if (oldMem == NULL) {
339 return leak_malloc(bytes);
340 }
341 void* newMem = NULL;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700342 AllocationEntry* header = to_header(oldMem);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800343 if (header && header->guard == GUARD) {
344 size_t oldSize = header->entry->size & ~SIZE_FLAG_MASK;
345 newMem = leak_malloc(bytes);
346 if (newMem != NULL) {
347 size_t copySize = (oldSize <= bytes) ? oldSize : bytes;
348 memcpy(newMem, oldMem, copySize);
349 leak_free(oldMem);
350 }
351 } else {
352 newMem = dlrealloc(oldMem, bytes);
353 }
354 return newMem;
355}
356
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700357extern "C" void* leak_memalign(size_t alignment, size_t bytes) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800358 // we can just use malloc
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700359 if (alignment <= MALLOC_ALIGNMENT) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800360 return leak_malloc(bytes);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700361 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800362
363 // need to make sure it's a power of two
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700364 if (alignment & (alignment-1)) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800365 alignment = 1L << (31 - __builtin_clz(alignment));
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700366 }
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800367
Elliott Hughese5d5f7f2012-10-09 17:23:09 -0700368 // here, alignment is at least MALLOC_ALIGNMENT<<1 bytes
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800369 // we will align by at least MALLOC_ALIGNMENT bytes
370 // and at most alignment-MALLOC_ALIGNMENT bytes
371 size_t size = (alignment-MALLOC_ALIGNMENT) + bytes;
Xi Wang7f5aa4f2012-03-14 02:48:39 -0400372 if (size < bytes) { // Overflow.
373 return NULL;
374 }
375
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800376 void* base = leak_malloc(size);
377 if (base != NULL) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700378 intptr_t ptr = reinterpret_cast<intptr_t>(base);
379 if ((ptr % alignment) == 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800380 return base;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700381 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800382
383 // align the pointer
384 ptr += ((-ptr) % alignment);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800385
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800386 // there is always enough space for the base pointer and the guard
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700387 reinterpret_cast<void**>(ptr)[-1] = MEMALIGN_GUARD;
388 reinterpret_cast<void**>(ptr)[-2] = base;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800389
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700390 return reinterpret_cast<void*>(ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800391 }
392 return base;
393}