Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 1 | //===-- asan_allocator.h ----------------------------------------*- 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 | // ASan-private header for asan_allocator.cc. |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef ASAN_ALLOCATOR_H |
| 16 | #define ASAN_ALLOCATOR_H |
| 17 | |
| 18 | #include "asan_internal.h" |
| 19 | #include "asan_interceptors.h" |
| 20 | |
| 21 | namespace __asan { |
| 22 | |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 23 | static const uptr kNumberOfSizeClasses = 255; |
Kostya Serebryany | 9d1eee9 | 2011-11-30 17:33:13 +0000 | [diff] [blame] | 24 | struct AsanChunk; |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 25 | |
| 26 | class AsanChunkFifoList { |
| 27 | public: |
| 28 | explicit AsanChunkFifoList(LinkerInitialized) { } |
| 29 | AsanChunkFifoList() { clear(); } |
| 30 | void Push(AsanChunk *n); |
| 31 | void PushList(AsanChunkFifoList *q); |
| 32 | AsanChunk *Pop(); |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 33 | uptr size() { return size_; } |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 34 | void clear() { |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 35 | first_ = last_ = 0; |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 36 | size_ = 0; |
| 37 | } |
| 38 | private: |
| 39 | AsanChunk *first_; |
| 40 | AsanChunk *last_; |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 41 | uptr size_; |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | struct AsanThreadLocalMallocStorage { |
| 45 | explicit AsanThreadLocalMallocStorage(LinkerInitialized x) |
| 46 | : quarantine_(x) { } |
| 47 | AsanThreadLocalMallocStorage() { |
Alexey Samsonov | e725478 | 2012-02-08 13:45:31 +0000 | [diff] [blame] | 48 | CHECK(REAL(memset)); |
| 49 | REAL(memset)(this, 0, sizeof(AsanThreadLocalMallocStorage)); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | AsanChunkFifoList quarantine_; |
| 53 | AsanChunk *free_lists_[kNumberOfSizeClasses]; |
| 54 | void CommitBack(); |
| 55 | }; |
| 56 | |
| 57 | // Fake stack frame contains local variables of one function. |
| 58 | // This struct should fit into a stack redzone (32 bytes). |
| 59 | struct FakeFrame { |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 60 | uptr magic; // Modified by the instrumented code. |
| 61 | uptr descr; // Modified by the instrumented code. |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 62 | FakeFrame *next; |
Kostya Serebryany | 1d35d15 | 2012-05-31 15:02:07 +0000 | [diff] [blame] | 63 | u64 real_stack : 48; |
| 64 | u64 size_minus_one : 16; |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | struct FakeFrameFifo { |
| 68 | public: |
| 69 | void FifoPush(FakeFrame *node); |
| 70 | FakeFrame *FifoPop(); |
| 71 | private: |
| 72 | FakeFrame *first_, *last_; |
| 73 | }; |
| 74 | |
| 75 | class FakeFrameLifo { |
| 76 | public: |
| 77 | void LifoPush(FakeFrame *node) { |
| 78 | node->next = top_; |
| 79 | top_ = node; |
| 80 | } |
| 81 | void LifoPop() { |
| 82 | CHECK(top_); |
| 83 | top_ = top_->next; |
| 84 | } |
| 85 | FakeFrame *top() { return top_; } |
| 86 | private: |
| 87 | FakeFrame *top_; |
| 88 | }; |
| 89 | |
| 90 | // For each thread we create a fake stack and place stack objects on this fake |
| 91 | // stack instead of the real stack. The fake stack is not really a stack but |
| 92 | // a fast malloc-like allocator so that when a function exits the fake stack |
| 93 | // is not poped but remains there for quite some time until gets used again. |
| 94 | // So, we poison the objects on the fake stack when function returns. |
| 95 | // It helps us find use-after-return bugs. |
| 96 | // We can not rely on __asan_stack_free being called on every function exit, |
| 97 | // so we maintain a lifo list of all current fake frames and update it on every |
| 98 | // call to __asan_stack_malloc. |
| 99 | class FakeStack { |
| 100 | public: |
| 101 | FakeStack(); |
| 102 | explicit FakeStack(LinkerInitialized) {} |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 103 | void Init(uptr stack_size); |
Kostya Serebryany | 72fde37 | 2011-12-09 01:49:31 +0000 | [diff] [blame] | 104 | void StopUsingFakeStack() { alive_ = false; } |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 105 | void Cleanup(); |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 106 | uptr AllocateStack(uptr size, uptr real_stack); |
| 107 | static void OnFree(uptr ptr, uptr size, uptr real_stack); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 108 | // Return the bottom of the maped region. |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 109 | uptr AddrIsInFakeStack(uptr addr); |
Alexander Potapenko | 0be25d5 | 2012-02-21 08:45:41 +0000 | [diff] [blame] | 110 | bool StackSize() { return stack_size_; } |
Alexey Samsonov | c3a8119 | 2012-08-30 14:22:21 +0000 | [diff] [blame] | 111 | |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 112 | private: |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 113 | static const uptr kMinStackFrameSizeLog = 9; // Min frame is 512B. |
| 114 | static const uptr kMaxStackFrameSizeLog = 16; // Max stack frame is 64K. |
| 115 | static const uptr kMaxStackMallocSize = 1 << kMaxStackFrameSizeLog; |
| 116 | static const uptr kNumberOfSizeClasses = |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 117 | kMaxStackFrameSizeLog - kMinStackFrameSizeLog + 1; |
| 118 | |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 119 | bool AddrIsInSizeClass(uptr addr, uptr size_class); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 120 | |
| 121 | // Each size class should be large enough to hold all frames. |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 122 | uptr ClassMmapSize(uptr size_class); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 123 | |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 124 | uptr ClassSize(uptr size_class) { |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 125 | return 1UL << (size_class + kMinStackFrameSizeLog); |
| 126 | } |
| 127 | |
| 128 | void DeallocateFrame(FakeFrame *fake_frame); |
| 129 | |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 130 | uptr ComputeSizeClass(uptr alloc_size); |
| 131 | void AllocateOneSizeClass(uptr size_class); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 132 | |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 133 | uptr stack_size_; |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 134 | bool alive_; |
| 135 | |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 136 | uptr allocated_size_classes_[kNumberOfSizeClasses]; |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 137 | FakeFrameFifo size_classes_[kNumberOfSizeClasses]; |
| 138 | FakeFrameLifo call_stack_; |
| 139 | }; |
| 140 | |
Kostya Serebryany | 6b0d775 | 2012-08-28 11:54:30 +0000 | [diff] [blame] | 141 | void *asan_memalign(uptr alignment, uptr size, StackTrace *stack); |
| 142 | void asan_free(void *ptr, StackTrace *stack); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 143 | |
Kostya Serebryany | 6b0d775 | 2012-08-28 11:54:30 +0000 | [diff] [blame] | 144 | void *asan_malloc(uptr size, StackTrace *stack); |
| 145 | void *asan_calloc(uptr nmemb, uptr size, StackTrace *stack); |
| 146 | void *asan_realloc(void *p, uptr size, StackTrace *stack); |
| 147 | void *asan_valloc(uptr size, StackTrace *stack); |
| 148 | void *asan_pvalloc(uptr size, StackTrace *stack); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 149 | |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 150 | int asan_posix_memalign(void **memptr, uptr alignment, uptr size, |
Kostya Serebryany | 6b0d775 | 2012-08-28 11:54:30 +0000 | [diff] [blame] | 151 | StackTrace *stack); |
| 152 | uptr asan_malloc_usable_size(void *ptr, StackTrace *stack); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 153 | |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 154 | uptr asan_mz_size(const void *ptr); |
Alexey Samsonov | 209c514 | 2012-01-17 06:39:10 +0000 | [diff] [blame] | 155 | void asan_mz_force_lock(); |
| 156 | void asan_mz_force_unlock(); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 157 | |
| 158 | } // namespace __asan |
| 159 | #endif // ASAN_ALLOCATOR_H |