Alexey Samsonov | 485d3dc | 2012-06-04 13:50:10 +0000 | [diff] [blame] | 1 | //===-- asan_malloc_linux.cc ----------------------------------------------===// |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file is a part of AddressSanitizer, an address sanity checker. |
| 11 | // |
| 12 | // Linux-specific malloc interception. |
| 13 | // We simply define functions like malloc, free, realloc, etc. |
| 14 | // They will replace the corresponding libc functions automagically. |
| 15 | //===----------------------------------------------------------------------===// |
Kostya Serebryany | 5dfa4da | 2011-12-01 21:40:52 +0000 | [diff] [blame] | 16 | #ifdef __linux__ |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 17 | |
| 18 | #include "asan_allocator.h" |
| 19 | #include "asan_interceptors.h" |
| 20 | #include "asan_internal.h" |
| 21 | #include "asan_stack.h" |
| 22 | |
Evgeniy Stepanov | be01398 | 2012-08-27 11:15:55 +0000 | [diff] [blame^] | 23 | #if ASAN_ANDROID |
Alexey Samsonov | 941a6ec | 2012-08-06 15:13:22 +0000 | [diff] [blame] | 24 | DECLARE_REAL_AND_INTERCEPTOR(void*, malloc, uptr size); |
| 25 | DECLARE_REAL_AND_INTERCEPTOR(void, free, void *ptr); |
| 26 | DECLARE_REAL_AND_INTERCEPTOR(void*, calloc, uptr nmemb, uptr size); |
| 27 | DECLARE_REAL_AND_INTERCEPTOR(void*, realloc, void *ptr, uptr size); |
| 28 | DECLARE_REAL_AND_INTERCEPTOR(void*, memalign, uptr boundary, uptr size); |
| 29 | |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 30 | struct MallocDebug { |
Kostya Serebryany | 0e0832b | 2012-06-25 09:58:29 +0000 | [diff] [blame] | 31 | void* (*malloc)(uptr bytes); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 32 | void (*free)(void* mem); |
Kostya Serebryany | 0e0832b | 2012-06-25 09:58:29 +0000 | [diff] [blame] | 33 | void* (*calloc)(uptr n_elements, uptr elem_size); |
| 34 | void* (*realloc)(void* oldMem, uptr bytes); |
| 35 | void* (*memalign)(uptr alignment, uptr bytes); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
Alexey Samsonov | 23e3b90 | 2012-02-03 08:37:19 +0000 | [diff] [blame] | 38 | const MallocDebug asan_malloc_dispatch ALIGNED(32) = { |
Alexey Samsonov | 941a6ec | 2012-08-06 15:13:22 +0000 | [diff] [blame] | 39 | WRAP(malloc), WRAP(free), WRAP(calloc), WRAP(realloc), WRAP(memalign) |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | extern "C" const MallocDebug* __libc_malloc_dispatch; |
| 43 | |
| 44 | namespace __asan { |
| 45 | void ReplaceSystemMalloc() { |
| 46 | __libc_malloc_dispatch = &asan_malloc_dispatch; |
| 47 | } |
| 48 | } // namespace __asan |
| 49 | |
| 50 | #else // ANDROID |
| 51 | |
| 52 | namespace __asan { |
| 53 | void ReplaceSystemMalloc() { |
| 54 | } |
| 55 | } // namespace __asan |
| 56 | #endif // ANDROID |
| 57 | |
| 58 | // ---------------------- Replacement functions ---------------- {{{1 |
| 59 | using namespace __asan; // NOLINT |
| 60 | |
Alexey Samsonov | 15965f9 | 2012-02-02 10:39:40 +0000 | [diff] [blame] | 61 | INTERCEPTOR(void, free, void *ptr) { |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 62 | GET_STACK_TRACE_HERE_FOR_FREE(ptr); |
| 63 | asan_free(ptr, &stack); |
| 64 | } |
| 65 | |
Alexey Samsonov | 15965f9 | 2012-02-02 10:39:40 +0000 | [diff] [blame] | 66 | INTERCEPTOR(void, cfree, void *ptr) { |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 67 | GET_STACK_TRACE_HERE_FOR_FREE(ptr); |
| 68 | asan_free(ptr, &stack); |
| 69 | } |
| 70 | |
Kostya Serebryany | 0e0832b | 2012-06-25 09:58:29 +0000 | [diff] [blame] | 71 | INTERCEPTOR(void*, malloc, uptr size) { |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 72 | GET_STACK_TRACE_HERE_FOR_MALLOC; |
| 73 | return asan_malloc(size, &stack); |
| 74 | } |
| 75 | |
Kostya Serebryany | 0e0832b | 2012-06-25 09:58:29 +0000 | [diff] [blame] | 76 | INTERCEPTOR(void*, calloc, uptr nmemb, uptr size) { |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 77 | if (!asan_inited) { |
Alexey Samsonov | e725478 | 2012-02-08 13:45:31 +0000 | [diff] [blame] | 78 | // Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym. |
Kostya Serebryany | 0e0832b | 2012-06-25 09:58:29 +0000 | [diff] [blame] | 79 | const uptr kCallocPoolSize = 1024; |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 80 | static uptr calloc_memory_for_dlsym[kCallocPoolSize]; |
Kostya Serebryany | 0e0832b | 2012-06-25 09:58:29 +0000 | [diff] [blame] | 81 | static uptr allocated; |
| 82 | uptr size_in_words = ((nmemb * size) + kWordSize - 1) / kWordSize; |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 83 | void *mem = (void*)&calloc_memory_for_dlsym[allocated]; |
| 84 | allocated += size_in_words; |
| 85 | CHECK(allocated < kCallocPoolSize); |
| 86 | return mem; |
| 87 | } |
| 88 | GET_STACK_TRACE_HERE_FOR_MALLOC; |
| 89 | return asan_calloc(nmemb, size, &stack); |
| 90 | } |
| 91 | |
Kostya Serebryany | 0e0832b | 2012-06-25 09:58:29 +0000 | [diff] [blame] | 92 | INTERCEPTOR(void*, realloc, void *ptr, uptr size) { |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 93 | GET_STACK_TRACE_HERE_FOR_MALLOC; |
| 94 | return asan_realloc(ptr, size, &stack); |
| 95 | } |
| 96 | |
Kostya Serebryany | 0e0832b | 2012-06-25 09:58:29 +0000 | [diff] [blame] | 97 | INTERCEPTOR(void*, memalign, uptr boundary, uptr size) { |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 98 | GET_STACK_TRACE_HERE_FOR_MALLOC; |
| 99 | return asan_memalign(boundary, size, &stack); |
| 100 | } |
| 101 | |
Kostya Serebryany | 0e0832b | 2012-06-25 09:58:29 +0000 | [diff] [blame] | 102 | INTERCEPTOR(void*, __libc_memalign, uptr align, uptr s) |
Alexey Samsonov | 23e3b90 | 2012-02-03 08:37:19 +0000 | [diff] [blame] | 103 | ALIAS("memalign"); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 104 | |
Kostya Serebryany | 0e0832b | 2012-06-25 09:58:29 +0000 | [diff] [blame] | 105 | INTERCEPTOR(uptr, malloc_usable_size, void *ptr) { |
Alexey Samsonov | 209c514 | 2012-01-17 06:39:10 +0000 | [diff] [blame] | 106 | GET_STACK_TRACE_HERE_FOR_MALLOC; |
| 107 | return asan_malloc_usable_size(ptr, &stack); |
| 108 | } |
| 109 | |
Kostya Serebryany | 0e0832b | 2012-06-25 09:58:29 +0000 | [diff] [blame] | 110 | // We avoid including malloc.h for portability reasons. |
| 111 | // man mallinfo says the fields are "long", but the implementation uses int. |
| 112 | // It doesn't matter much -- we just need to make sure that the libc's mallinfo |
| 113 | // is not called. |
| 114 | struct fake_mallinfo { |
| 115 | int x[10]; |
| 116 | }; |
| 117 | |
| 118 | INTERCEPTOR(struct fake_mallinfo, mallinfo, void) { |
| 119 | struct fake_mallinfo res; |
Alexey Samsonov | e725478 | 2012-02-08 13:45:31 +0000 | [diff] [blame] | 120 | REAL(memset)(&res, 0, sizeof(res)); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 121 | return res; |
| 122 | } |
| 123 | |
Alexey Samsonov | 15965f9 | 2012-02-02 10:39:40 +0000 | [diff] [blame] | 124 | INTERCEPTOR(int, mallopt, int cmd, int value) { |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 125 | return -1; |
| 126 | } |
| 127 | |
Kostya Serebryany | 0e0832b | 2012-06-25 09:58:29 +0000 | [diff] [blame] | 128 | INTERCEPTOR(int, posix_memalign, void **memptr, uptr alignment, uptr size) { |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 129 | GET_STACK_TRACE_HERE_FOR_MALLOC; |
Evgeniy Stepanov | 823085a | 2012-03-21 11:32:46 +0000 | [diff] [blame] | 130 | // Printf("posix_memalign: %zx %zu\n", alignment, size); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 131 | return asan_posix_memalign(memptr, alignment, size, &stack); |
| 132 | } |
| 133 | |
Kostya Serebryany | 0e0832b | 2012-06-25 09:58:29 +0000 | [diff] [blame] | 134 | INTERCEPTOR(void*, valloc, uptr size) { |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 135 | GET_STACK_TRACE_HERE_FOR_MALLOC; |
| 136 | return asan_valloc(size, &stack); |
| 137 | } |
| 138 | |
Kostya Serebryany | 0e0832b | 2012-06-25 09:58:29 +0000 | [diff] [blame] | 139 | INTERCEPTOR(void*, pvalloc, uptr size) { |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 140 | GET_STACK_TRACE_HERE_FOR_MALLOC; |
| 141 | return asan_pvalloc(size, &stack); |
| 142 | } |
Kostya Serebryany | 5dfa4da | 2011-12-01 21:40:52 +0000 | [diff] [blame] | 143 | |
| 144 | #endif // __linux__ |