Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 1 | //===-- asan_malloc_linux.cc ------------------------------------*- C++ -*-===// |
| 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 | |
| 23 | #include <malloc.h> |
| 24 | |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 25 | #ifdef ANDROID |
| 26 | struct MallocDebug { |
| 27 | void* (*malloc)(size_t bytes); |
| 28 | void (*free)(void* mem); |
| 29 | void* (*calloc)(size_t n_elements, size_t elem_size); |
| 30 | void* (*realloc)(void* oldMem, size_t bytes); |
| 31 | void* (*memalign)(size_t alignment, size_t bytes); |
| 32 | }; |
| 33 | |
Alexey Samsonov | 23e3b90 | 2012-02-03 08:37:19 +0000 | [diff] [blame] | 34 | const MallocDebug asan_malloc_dispatch ALIGNED(32) = { |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 35 | malloc, free, calloc, realloc, memalign |
| 36 | }; |
| 37 | |
| 38 | extern "C" const MallocDebug* __libc_malloc_dispatch; |
| 39 | |
| 40 | namespace __asan { |
| 41 | void ReplaceSystemMalloc() { |
| 42 | __libc_malloc_dispatch = &asan_malloc_dispatch; |
| 43 | } |
| 44 | } // namespace __asan |
| 45 | |
| 46 | #else // ANDROID |
| 47 | |
| 48 | namespace __asan { |
| 49 | void ReplaceSystemMalloc() { |
| 50 | } |
| 51 | } // namespace __asan |
| 52 | #endif // ANDROID |
| 53 | |
| 54 | // ---------------------- Replacement functions ---------------- {{{1 |
| 55 | using namespace __asan; // NOLINT |
| 56 | |
Alexey Samsonov | 15965f9 | 2012-02-02 10:39:40 +0000 | [diff] [blame] | 57 | INTERCEPTOR(void, free, void *ptr) { |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 58 | GET_STACK_TRACE_HERE_FOR_FREE(ptr); |
| 59 | asan_free(ptr, &stack); |
| 60 | } |
| 61 | |
Alexey Samsonov | 15965f9 | 2012-02-02 10:39:40 +0000 | [diff] [blame] | 62 | INTERCEPTOR(void, cfree, void *ptr) { |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 63 | GET_STACK_TRACE_HERE_FOR_FREE(ptr); |
| 64 | asan_free(ptr, &stack); |
| 65 | } |
| 66 | |
Alexey Samsonov | 15965f9 | 2012-02-02 10:39:40 +0000 | [diff] [blame] | 67 | INTERCEPTOR(void*, malloc, size_t size) { |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 68 | GET_STACK_TRACE_HERE_FOR_MALLOC; |
| 69 | return asan_malloc(size, &stack); |
| 70 | } |
| 71 | |
Alexey Samsonov | 15965f9 | 2012-02-02 10:39:40 +0000 | [diff] [blame] | 72 | INTERCEPTOR(void*, calloc, size_t nmemb, size_t size) { |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 73 | if (!asan_inited) { |
Alexey Samsonov | e725478 | 2012-02-08 13:45:31 +0000 | [diff] [blame] | 74 | // Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym. |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 75 | const size_t kCallocPoolSize = 1024; |
| 76 | static uintptr_t calloc_memory_for_dlsym[kCallocPoolSize]; |
| 77 | static size_t allocated; |
| 78 | size_t size_in_words = ((nmemb * size) + kWordSize - 1) / kWordSize; |
| 79 | void *mem = (void*)&calloc_memory_for_dlsym[allocated]; |
| 80 | allocated += size_in_words; |
| 81 | CHECK(allocated < kCallocPoolSize); |
| 82 | return mem; |
| 83 | } |
| 84 | GET_STACK_TRACE_HERE_FOR_MALLOC; |
| 85 | return asan_calloc(nmemb, size, &stack); |
| 86 | } |
| 87 | |
Alexey Samsonov | 15965f9 | 2012-02-02 10:39:40 +0000 | [diff] [blame] | 88 | INTERCEPTOR(void*, realloc, void *ptr, size_t size) { |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 89 | GET_STACK_TRACE_HERE_FOR_MALLOC; |
| 90 | return asan_realloc(ptr, size, &stack); |
| 91 | } |
| 92 | |
Alexey Samsonov | 15965f9 | 2012-02-02 10:39:40 +0000 | [diff] [blame] | 93 | INTERCEPTOR(void*, memalign, size_t boundary, size_t size) { |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 94 | GET_STACK_TRACE_HERE_FOR_MALLOC; |
| 95 | return asan_memalign(boundary, size, &stack); |
| 96 | } |
| 97 | |
Alexey Samsonov | 15965f9 | 2012-02-02 10:39:40 +0000 | [diff] [blame] | 98 | INTERCEPTOR(void*, __libc_memalign, size_t align, size_t s) |
Alexey Samsonov | 23e3b90 | 2012-02-03 08:37:19 +0000 | [diff] [blame] | 99 | ALIAS("memalign"); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 100 | |
Alexey Samsonov | 15965f9 | 2012-02-02 10:39:40 +0000 | [diff] [blame] | 101 | INTERCEPTOR(size_t, malloc_usable_size, void *ptr) { |
Alexey Samsonov | 209c514 | 2012-01-17 06:39:10 +0000 | [diff] [blame] | 102 | GET_STACK_TRACE_HERE_FOR_MALLOC; |
| 103 | return asan_malloc_usable_size(ptr, &stack); |
| 104 | } |
| 105 | |
Alexey Samsonov | 15965f9 | 2012-02-02 10:39:40 +0000 | [diff] [blame] | 106 | INTERCEPTOR(struct mallinfo, mallinfo) { |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 107 | struct mallinfo res; |
Alexey Samsonov | e725478 | 2012-02-08 13:45:31 +0000 | [diff] [blame] | 108 | REAL(memset)(&res, 0, sizeof(res)); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 109 | return res; |
| 110 | } |
| 111 | |
Alexey Samsonov | 15965f9 | 2012-02-02 10:39:40 +0000 | [diff] [blame] | 112 | INTERCEPTOR(int, mallopt, int cmd, int value) { |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 113 | return -1; |
| 114 | } |
| 115 | |
Alexey Samsonov | 15965f9 | 2012-02-02 10:39:40 +0000 | [diff] [blame] | 116 | INTERCEPTOR(int, posix_memalign, void **memptr, size_t alignment, size_t size) { |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 117 | GET_STACK_TRACE_HERE_FOR_MALLOC; |
Evgeniy Stepanov | 823085a | 2012-03-21 11:32:46 +0000 | [diff] [blame^] | 118 | // Printf("posix_memalign: %zx %zu\n", alignment, size); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 119 | return asan_posix_memalign(memptr, alignment, size, &stack); |
| 120 | } |
| 121 | |
Alexey Samsonov | 15965f9 | 2012-02-02 10:39:40 +0000 | [diff] [blame] | 122 | INTERCEPTOR(void*, valloc, size_t size) { |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 123 | GET_STACK_TRACE_HERE_FOR_MALLOC; |
| 124 | return asan_valloc(size, &stack); |
| 125 | } |
| 126 | |
Alexey Samsonov | 15965f9 | 2012-02-02 10:39:40 +0000 | [diff] [blame] | 127 | INTERCEPTOR(void*, pvalloc, size_t size) { |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 128 | GET_STACK_TRACE_HERE_FOR_MALLOC; |
| 129 | return asan_pvalloc(size, &stack); |
| 130 | } |
Kostya Serebryany | 5dfa4da | 2011-12-01 21:40:52 +0000 | [diff] [blame] | 131 | |
| 132 | #endif // __linux__ |