blob: f117f692584ca31077518989de2555b6152de495 [file] [log] [blame]
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001//===-- 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 Serebryanyc88059c2012-12-17 07:54:29 +000020#include "sanitizer_common/sanitizer_list.h"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000021
Kostya Serebryany8b0a7ce2012-12-10 13:52:55 +000022// 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
25// at build time by redefining this macrozz.
26#define ASAN_ALLOCATOR_VERSION 1
27
Kostya Serebryany1e172b42011-11-30 01:07:02 +000028namespace __asan {
29
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +000030enum 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 Serebryany3f4c3872012-05-31 14:35:53 +000036static const uptr kNumberOfSizeClasses = 255;
Kostya Serebryanycbab9112011-11-30 17:33:13 +000037struct AsanChunk;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000038
Alexey Samsonov5c153fa2012-09-18 07:38:10 +000039class 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 Serebryany321e1252012-12-11 09:02:36 +000050 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 Potapenko2ca12222012-12-12 12:32:57 +000058 (void)access_size;
Kostya Serebryany321e1252012-12-11 09:02:36 +000059 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 Samsonov5c153fa2012-09-18 07:38:10 +000076 private:
77 AsanChunk *const chunk_;
78};
79
80AsanChunkView FindHeapChunkByAddress(uptr address);
81
Kostya Serebryanyc88059c2012-12-17 07:54:29 +000082// List of AsanChunks with total size.
83class AsanChunkFifoList: public IntrusiveList<AsanChunk> {
Kostya Serebryany1e172b42011-11-30 01:07:02 +000084 public:
85 explicit AsanChunkFifoList(LinkerInitialized) { }
86 AsanChunkFifoList() { clear(); }
87 void Push(AsanChunk *n);
88 void PushList(AsanChunkFifoList *q);
89 AsanChunk *Pop();
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000090 uptr size() { return size_; }
Kostya Serebryany1e172b42011-11-30 01:07:02 +000091 void clear() {
Kostya Serebryanyc88059c2012-12-17 07:54:29 +000092 IntrusiveList<AsanChunk>::clear();
Kostya Serebryany1e172b42011-11-30 01:07:02 +000093 size_ = 0;
94 }
95 private:
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000096 uptr size_;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000097};
98
99struct AsanThreadLocalMallocStorage {
100 explicit AsanThreadLocalMallocStorage(LinkerInitialized x)
101 : quarantine_(x) { }
102 AsanThreadLocalMallocStorage() {
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000103 CHECK(REAL(memset));
104 REAL(memset)(this, 0, sizeof(AsanThreadLocalMallocStorage));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000105 }
106
107 AsanChunkFifoList quarantine_;
108 AsanChunk *free_lists_[kNumberOfSizeClasses];
Kostya Serebryanyc523d172012-12-17 13:43:47 +0000109#if ASAN_ALLOCATOR_VERSION == 2
110 uptr allocator2_cache[1024]; // Opaque.
111#endif
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000112 void CommitBack();
113};
114
115// Fake stack frame contains local variables of one function.
116// This struct should fit into a stack redzone (32 bytes).
117struct FakeFrame {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000118 uptr magic; // Modified by the instrumented code.
119 uptr descr; // Modified by the instrumented code.
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000120 FakeFrame *next;
Kostya Serebryanyee392552012-05-31 15:02:07 +0000121 u64 real_stack : 48;
122 u64 size_minus_one : 16;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000123};
124
125struct FakeFrameFifo {
126 public:
127 void FifoPush(FakeFrame *node);
128 FakeFrame *FifoPop();
129 private:
130 FakeFrame *first_, *last_;
131};
132
133class FakeFrameLifo {
134 public:
135 void LifoPush(FakeFrame *node) {
136 node->next = top_;
137 top_ = node;
138 }
139 void LifoPop() {
140 CHECK(top_);
141 top_ = top_->next;
142 }
143 FakeFrame *top() { return top_; }
144 private:
145 FakeFrame *top_;
146};
147
148// For each thread we create a fake stack and place stack objects on this fake
149// stack instead of the real stack. The fake stack is not really a stack but
150// a fast malloc-like allocator so that when a function exits the fake stack
151// is not poped but remains there for quite some time until gets used again.
152// So, we poison the objects on the fake stack when function returns.
153// It helps us find use-after-return bugs.
154// We can not rely on __asan_stack_free being called on every function exit,
155// so we maintain a lifo list of all current fake frames and update it on every
156// call to __asan_stack_malloc.
157class FakeStack {
158 public:
159 FakeStack();
160 explicit FakeStack(LinkerInitialized) {}
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000161 void Init(uptr stack_size);
Kostya Serebryanyc4b34d92011-12-09 01:49:31 +0000162 void StopUsingFakeStack() { alive_ = false; }
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000163 void Cleanup();
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000164 uptr AllocateStack(uptr size, uptr real_stack);
165 static void OnFree(uptr ptr, uptr size, uptr real_stack);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000166 // Return the bottom of the maped region.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000167 uptr AddrIsInFakeStack(uptr addr);
Alexander Potapenko60490e02012-02-21 08:45:41 +0000168 bool StackSize() { return stack_size_; }
Alexey Samsonov50926822012-08-30 14:22:21 +0000169
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000170 private:
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000171 static const uptr kMinStackFrameSizeLog = 9; // Min frame is 512B.
172 static const uptr kMaxStackFrameSizeLog = 16; // Max stack frame is 64K.
173 static const uptr kMaxStackMallocSize = 1 << kMaxStackFrameSizeLog;
174 static const uptr kNumberOfSizeClasses =
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000175 kMaxStackFrameSizeLog - kMinStackFrameSizeLog + 1;
176
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000177 bool AddrIsInSizeClass(uptr addr, uptr size_class);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000178
179 // Each size class should be large enough to hold all frames.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000180 uptr ClassMmapSize(uptr size_class);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000181
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000182 uptr ClassSize(uptr size_class) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000183 return 1UL << (size_class + kMinStackFrameSizeLog);
184 }
185
186 void DeallocateFrame(FakeFrame *fake_frame);
187
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000188 uptr ComputeSizeClass(uptr alloc_size);
189 void AllocateOneSizeClass(uptr size_class);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000190
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000191 uptr stack_size_;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000192 bool alive_;
193
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000194 uptr allocated_size_classes_[kNumberOfSizeClasses];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000195 FakeFrameFifo size_classes_[kNumberOfSizeClasses];
196 FakeFrameLifo call_stack_;
197};
198
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +0000199void *asan_memalign(uptr alignment, uptr size, StackTrace *stack,
200 AllocType alloc_type);
201void asan_free(void *ptr, StackTrace *stack, AllocType alloc_type);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000202
Kostya Serebryanyc3390df2012-08-28 11:54:30 +0000203void *asan_malloc(uptr size, StackTrace *stack);
204void *asan_calloc(uptr nmemb, uptr size, StackTrace *stack);
205void *asan_realloc(void *p, uptr size, StackTrace *stack);
206void *asan_valloc(uptr size, StackTrace *stack);
207void *asan_pvalloc(uptr size, StackTrace *stack);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000208
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000209int asan_posix_memalign(void **memptr, uptr alignment, uptr size,
Kostya Serebryanyc3390df2012-08-28 11:54:30 +0000210 StackTrace *stack);
211uptr asan_malloc_usable_size(void *ptr, StackTrace *stack);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000212
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000213uptr asan_mz_size(const void *ptr);
Alexey Samsonov4fd95f12012-01-17 06:39:10 +0000214void asan_mz_force_lock();
215void asan_mz_force_unlock();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000216
Kostya Serebryany2679f192012-12-10 14:19:15 +0000217// Log2 and RoundUpToPowerOfTwo should be inlined for performance.
Kostya Serebryany956ad472012-12-11 07:27:59 +0000218#if defined(_WIN32) && !defined(__clang__)
Timur Iskhodzhanovd923f2b2012-12-11 12:23:00 +0000219extern "C" {
Timur Iskhodzhanov8416e212012-12-11 12:24:41 +0000220unsigned char _BitScanForward(unsigned long *index, unsigned long mask); // NOLINT
221unsigned char _BitScanReverse(unsigned long *index, unsigned long mask); // NOLINT
Timur Iskhodzhanov4e773522012-12-11 12:03:06 +0000222#if defined(_WIN64)
Timur Iskhodzhanov8416e212012-12-11 12:24:41 +0000223unsigned char _BitScanForward64(unsigned long *index, unsigned __int64 mask); // NOLINT
224unsigned char _BitScanReverse64(unsigned long *index, unsigned __int64 mask); // NOLINT
Timur Iskhodzhanov4e773522012-12-11 12:03:06 +0000225#endif
Timur Iskhodzhanovd923f2b2012-12-11 12:23:00 +0000226}
Kostya Serebryany956ad472012-12-11 07:27:59 +0000227#endif
Kostya Serebryany2679f192012-12-10 14:19:15 +0000228
229static inline uptr Log2(uptr x) {
230 CHECK(IsPowerOfTwo(x));
231#if !defined(_WIN32) || defined(__clang__)
232 return __builtin_ctzl(x);
233#elif defined(_WIN64)
234 unsigned long ret; // NOLINT
235 _BitScanForward64(&ret, x);
236 return ret;
237#else
238 unsigned long ret; // NOLINT
239 _BitScanForward(&ret, x);
240 return ret;
241#endif
242}
243
244static inline uptr RoundUpToPowerOfTwo(uptr size) {
245 CHECK(size);
246 if (IsPowerOfTwo(size)) return size;
247
248 unsigned long up; // NOLINT
249#if !defined(_WIN32) || defined(__clang__)
250 up = SANITIZER_WORDSIZE - 1 - __builtin_clzl(size);
251#elif defined(_WIN64)
252 _BitScanReverse64(&up, size);
253#else
254 _BitScanReverse(&up, size);
255#endif
256 CHECK(size < (1ULL << (up + 1)));
257 CHECK(size > (1ULL << up));
258 return 1UL << (up + 1);
259}
260
261
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000262} // namespace __asan
263#endif // ASAN_ALLOCATOR_H