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