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