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