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