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" |
Kostya Serebryany | 41ffe3d | 2012-12-17 07:54:29 +0000 | [diff] [blame] | 20 | #include "sanitizer_common/sanitizer_list.h" |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 21 | |
Kostya Serebryany | 14282a9 | 2012-12-10 13:52:55 +0000 | [diff] [blame] | 22 | // We are in the process of transitioning from the old allocator (version 1) |
| 23 | // to a new one (version 2). The change is quite intrusive so both allocators |
| 24 | // will co-exist in the source base for a while. The actual allocator is chosen |
Dmitry Vyukov | 9de857a | 2013-01-14 09:03:24 +0000 | [diff] [blame] | 25 | // at build time by redefining this macro. |
Kostya Serebryany | d8b1f78 | 2013-01-15 13:57:39 +0000 | [diff] [blame] | 26 | #ifndef ASAN_ALLOCATOR_VERSION |
Timur Iskhodzhanov | 167f9e4 | 2013-02-08 12:02:00 +0000 | [diff] [blame^] | 27 | # if (ASAN_LINUX && !ASAN_ANDROID) || ASAN_MAC || ASAN_WINDOWS |
Kostya Serebryany | d8b1f78 | 2013-01-15 13:57:39 +0000 | [diff] [blame] | 28 | # define ASAN_ALLOCATOR_VERSION 2 |
| 29 | # else |
| 30 | # define ASAN_ALLOCATOR_VERSION 1 |
| 31 | # endif |
| 32 | #endif // ASAN_ALLOCATOR_VERSION |
Kostya Serebryany | 14282a9 | 2012-12-10 13:52:55 +0000 | [diff] [blame] | 33 | |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 34 | namespace __asan { |
| 35 | |
Kostya Serebryany | 3674c6b | 2012-12-21 08:53:59 +0000 | [diff] [blame] | 36 | enum AllocType { |
| 37 | FROM_MALLOC = 1, // Memory block came from malloc, calloc, realloc, etc. |
| 38 | FROM_NEW = 2, // Memory block came from operator new. |
| 39 | FROM_NEW_BR = 3 // Memory block came from operator new [ ] |
| 40 | }; |
| 41 | |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 42 | static const uptr kNumberOfSizeClasses = 255; |
Kostya Serebryany | 9d1eee9 | 2011-11-30 17:33:13 +0000 | [diff] [blame] | 43 | struct AsanChunk; |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 44 | |
Kostya Serebryany | 61761f1 | 2013-01-28 08:05:47 +0000 | [diff] [blame] | 45 | void InitializeAllocator(); |
| 46 | |
Alexey Samsonov | 8661465 | 2012-09-18 07:38:10 +0000 | [diff] [blame] | 47 | class AsanChunkView { |
| 48 | public: |
| 49 | explicit AsanChunkView(AsanChunk *chunk) : chunk_(chunk) {} |
| 50 | bool IsValid() { return chunk_ != 0; } |
| 51 | uptr Beg(); // first byte of user memory. |
| 52 | uptr End(); // last byte of user memory. |
| 53 | uptr UsedSize(); // size requested by the user. |
| 54 | uptr AllocTid(); |
| 55 | uptr FreeTid(); |
| 56 | void GetAllocStack(StackTrace *stack); |
| 57 | void GetFreeStack(StackTrace *stack); |
Evgeniy Stepanov | 1bc7298 | 2013-02-05 14:32:03 +0000 | [diff] [blame] | 58 | bool AddrIsInside(uptr addr, uptr access_size, sptr *offset) { |
Kostya Serebryany | 5e2a7ac | 2012-12-11 09:02:36 +0000 | [diff] [blame] | 59 | if (addr >= Beg() && (addr + access_size) <= End()) { |
| 60 | *offset = addr - Beg(); |
| 61 | return true; |
| 62 | } |
| 63 | return false; |
| 64 | } |
Evgeniy Stepanov | 1bc7298 | 2013-02-05 14:32:03 +0000 | [diff] [blame] | 65 | bool AddrIsAtLeft(uptr addr, uptr access_size, sptr *offset) { |
Alexander Potapenko | 602a09f | 2012-12-12 12:32:57 +0000 | [diff] [blame] | 66 | (void)access_size; |
Kostya Serebryany | 5e2a7ac | 2012-12-11 09:02:36 +0000 | [diff] [blame] | 67 | if (addr < Beg()) { |
| 68 | *offset = Beg() - addr; |
| 69 | return true; |
| 70 | } |
| 71 | return false; |
| 72 | } |
Evgeniy Stepanov | 1bc7298 | 2013-02-05 14:32:03 +0000 | [diff] [blame] | 73 | bool AddrIsAtRight(uptr addr, uptr access_size, sptr *offset) { |
Kostya Serebryany | 5e2a7ac | 2012-12-11 09:02:36 +0000 | [diff] [blame] | 74 | if (addr + access_size >= End()) { |
Evgeniy Stepanov | 1bc7298 | 2013-02-05 14:32:03 +0000 | [diff] [blame] | 75 | *offset = addr - End(); |
Kostya Serebryany | 5e2a7ac | 2012-12-11 09:02:36 +0000 | [diff] [blame] | 76 | return true; |
| 77 | } |
| 78 | return false; |
| 79 | } |
| 80 | |
Alexey Samsonov | 8661465 | 2012-09-18 07:38:10 +0000 | [diff] [blame] | 81 | private: |
| 82 | AsanChunk *const chunk_; |
| 83 | }; |
| 84 | |
| 85 | AsanChunkView FindHeapChunkByAddress(uptr address); |
| 86 | |
Kostya Serebryany | 41ffe3d | 2012-12-17 07:54:29 +0000 | [diff] [blame] | 87 | // List of AsanChunks with total size. |
| 88 | class AsanChunkFifoList: public IntrusiveList<AsanChunk> { |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 89 | public: |
| 90 | explicit AsanChunkFifoList(LinkerInitialized) { } |
| 91 | AsanChunkFifoList() { clear(); } |
| 92 | void Push(AsanChunk *n); |
| 93 | void PushList(AsanChunkFifoList *q); |
| 94 | AsanChunk *Pop(); |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 95 | uptr size() { return size_; } |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 96 | void clear() { |
Kostya Serebryany | 41ffe3d | 2012-12-17 07:54:29 +0000 | [diff] [blame] | 97 | IntrusiveList<AsanChunk>::clear(); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 98 | size_ = 0; |
| 99 | } |
| 100 | private: |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 101 | uptr size_; |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | struct AsanThreadLocalMallocStorage { |
| 105 | explicit AsanThreadLocalMallocStorage(LinkerInitialized x) |
Dmitry Vyukov | db0cf87 | 2013-01-11 08:07:43 +0000 | [diff] [blame] | 106 | #if ASAN_ALLOCATOR_VERSION == 1 |
| 107 | : quarantine_(x) |
| 108 | #endif |
| 109 | { } |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 110 | AsanThreadLocalMallocStorage() { |
Alexey Samsonov | e725478 | 2012-02-08 13:45:31 +0000 | [diff] [blame] | 111 | CHECK(REAL(memset)); |
| 112 | REAL(memset)(this, 0, sizeof(AsanThreadLocalMallocStorage)); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Kostya Serebryany | 4a42cf6 | 2012-12-27 14:09:19 +0000 | [diff] [blame] | 115 | #if ASAN_ALLOCATOR_VERSION == 1 |
Dmitry Vyukov | db0cf87 | 2013-01-11 08:07:43 +0000 | [diff] [blame] | 116 | AsanChunkFifoList quarantine_; |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 117 | AsanChunk *free_lists_[kNumberOfSizeClasses]; |
Kostya Serebryany | 4a42cf6 | 2012-12-27 14:09:19 +0000 | [diff] [blame] | 118 | #else |
Dmitry Vyukov | db0cf87 | 2013-01-11 08:07:43 +0000 | [diff] [blame] | 119 | uptr quarantine_cache[16]; |
Dmitry Vyukov | 0d46b2b | 2013-01-15 10:45:18 +0000 | [diff] [blame] | 120 | uptr allocator2_cache[96 * (512 * 8 + 16)]; // Opaque. |
Kostya Serebryany | ec339f7 | 2012-12-17 13:43:47 +0000 | [diff] [blame] | 121 | #endif |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 122 | void CommitBack(); |
| 123 | }; |
| 124 | |
| 125 | // Fake stack frame contains local variables of one function. |
| 126 | // This struct should fit into a stack redzone (32 bytes). |
| 127 | struct FakeFrame { |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 128 | uptr magic; // Modified by the instrumented code. |
| 129 | uptr descr; // Modified by the instrumented code. |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 130 | FakeFrame *next; |
Kostya Serebryany | 1d35d15 | 2012-05-31 15:02:07 +0000 | [diff] [blame] | 131 | u64 real_stack : 48; |
| 132 | u64 size_minus_one : 16; |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | struct FakeFrameFifo { |
| 136 | public: |
| 137 | void FifoPush(FakeFrame *node); |
| 138 | FakeFrame *FifoPop(); |
| 139 | private: |
| 140 | FakeFrame *first_, *last_; |
| 141 | }; |
| 142 | |
| 143 | class FakeFrameLifo { |
| 144 | public: |
| 145 | void LifoPush(FakeFrame *node) { |
| 146 | node->next = top_; |
| 147 | top_ = node; |
| 148 | } |
| 149 | void LifoPop() { |
| 150 | CHECK(top_); |
| 151 | top_ = top_->next; |
| 152 | } |
| 153 | FakeFrame *top() { return top_; } |
| 154 | private: |
| 155 | FakeFrame *top_; |
| 156 | }; |
| 157 | |
| 158 | // For each thread we create a fake stack and place stack objects on this fake |
| 159 | // stack instead of the real stack. The fake stack is not really a stack but |
| 160 | // a fast malloc-like allocator so that when a function exits the fake stack |
| 161 | // is not poped but remains there for quite some time until gets used again. |
| 162 | // So, we poison the objects on the fake stack when function returns. |
| 163 | // It helps us find use-after-return bugs. |
| 164 | // We can not rely on __asan_stack_free being called on every function exit, |
| 165 | // so we maintain a lifo list of all current fake frames and update it on every |
| 166 | // call to __asan_stack_malloc. |
| 167 | class FakeStack { |
| 168 | public: |
| 169 | FakeStack(); |
| 170 | explicit FakeStack(LinkerInitialized) {} |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 171 | void Init(uptr stack_size); |
Kostya Serebryany | 72fde37 | 2011-12-09 01:49:31 +0000 | [diff] [blame] | 172 | void StopUsingFakeStack() { alive_ = false; } |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 173 | void Cleanup(); |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 174 | uptr AllocateStack(uptr size, uptr real_stack); |
| 175 | static void OnFree(uptr ptr, uptr size, uptr real_stack); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 176 | // Return the bottom of the maped region. |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 177 | uptr AddrIsInFakeStack(uptr addr); |
Alexander Potapenko | 0be25d5 | 2012-02-21 08:45:41 +0000 | [diff] [blame] | 178 | bool StackSize() { return stack_size_; } |
Alexey Samsonov | c3a8119 | 2012-08-30 14:22:21 +0000 | [diff] [blame] | 179 | |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 180 | private: |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 181 | static const uptr kMinStackFrameSizeLog = 9; // Min frame is 512B. |
| 182 | static const uptr kMaxStackFrameSizeLog = 16; // Max stack frame is 64K. |
| 183 | static const uptr kMaxStackMallocSize = 1 << kMaxStackFrameSizeLog; |
| 184 | static const uptr kNumberOfSizeClasses = |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 185 | kMaxStackFrameSizeLog - kMinStackFrameSizeLog + 1; |
| 186 | |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 187 | bool AddrIsInSizeClass(uptr addr, uptr size_class); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 188 | |
| 189 | // Each size class should be large enough to hold all frames. |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 190 | uptr ClassMmapSize(uptr size_class); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 191 | |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 192 | uptr ClassSize(uptr size_class) { |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 193 | return 1UL << (size_class + kMinStackFrameSizeLog); |
| 194 | } |
| 195 | |
| 196 | void DeallocateFrame(FakeFrame *fake_frame); |
| 197 | |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 198 | uptr ComputeSizeClass(uptr alloc_size); |
| 199 | void AllocateOneSizeClass(uptr size_class); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 200 | |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 201 | uptr stack_size_; |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 202 | bool alive_; |
| 203 | |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 204 | uptr allocated_size_classes_[kNumberOfSizeClasses]; |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 205 | FakeFrameFifo size_classes_[kNumberOfSizeClasses]; |
| 206 | FakeFrameLifo call_stack_; |
| 207 | }; |
| 208 | |
Kostya Serebryany | 3674c6b | 2012-12-21 08:53:59 +0000 | [diff] [blame] | 209 | void *asan_memalign(uptr alignment, uptr size, StackTrace *stack, |
| 210 | AllocType alloc_type); |
| 211 | void asan_free(void *ptr, StackTrace *stack, AllocType alloc_type); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 212 | |
Kostya Serebryany | 6b0d775 | 2012-08-28 11:54:30 +0000 | [diff] [blame] | 213 | void *asan_malloc(uptr size, StackTrace *stack); |
| 214 | void *asan_calloc(uptr nmemb, uptr size, StackTrace *stack); |
| 215 | void *asan_realloc(void *p, uptr size, StackTrace *stack); |
| 216 | void *asan_valloc(uptr size, StackTrace *stack); |
| 217 | void *asan_pvalloc(uptr size, StackTrace *stack); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 218 | |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 219 | int asan_posix_memalign(void **memptr, uptr alignment, uptr size, |
Kostya Serebryany | 6b0d775 | 2012-08-28 11:54:30 +0000 | [diff] [blame] | 220 | StackTrace *stack); |
| 221 | uptr asan_malloc_usable_size(void *ptr, StackTrace *stack); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 222 | |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 223 | uptr asan_mz_size(const void *ptr); |
Alexey Samsonov | 209c514 | 2012-01-17 06:39:10 +0000 | [diff] [blame] | 224 | void asan_mz_force_lock(); |
| 225 | void asan_mz_force_unlock(); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 226 | |
Kostya Serebryany | 4a42cf6 | 2012-12-27 14:09:19 +0000 | [diff] [blame] | 227 | void PrintInternalAllocatorStats(); |
| 228 | |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 229 | } // namespace __asan |
| 230 | #endif // ASAN_ALLOCATOR_H |