annotate libsanitizer/asan/asan_fake_stack.cc @ 122:fb3d53c41846

do not expand code segment
author mir3636
date Thu, 22 Mar 2018 17:37:58 +0900
parents 04ced10e8804
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 //===-- asan_fake_stack.cc ------------------------------------------------===//
kono
parents:
diff changeset
2 //
kono
parents:
diff changeset
3 // This file is distributed under the University of Illinois Open Source
kono
parents:
diff changeset
4 // License. See LICENSE.TXT for details.
kono
parents:
diff changeset
5 //
kono
parents:
diff changeset
6 //===----------------------------------------------------------------------===//
kono
parents:
diff changeset
7 //
kono
parents:
diff changeset
8 // This file is a part of AddressSanitizer, an address sanity checker.
kono
parents:
diff changeset
9 //
kono
parents:
diff changeset
10 // FakeStack is used to detect use-after-return bugs.
kono
parents:
diff changeset
11 //===----------------------------------------------------------------------===//
kono
parents:
diff changeset
12
kono
parents:
diff changeset
13 #include "asan_allocator.h"
kono
parents:
diff changeset
14 #include "asan_poisoning.h"
kono
parents:
diff changeset
15 #include "asan_thread.h"
kono
parents:
diff changeset
16
kono
parents:
diff changeset
17 namespace __asan {
kono
parents:
diff changeset
18
kono
parents:
diff changeset
19 static const u64 kMagic1 = kAsanStackAfterReturnMagic;
kono
parents:
diff changeset
20 static const u64 kMagic2 = (kMagic1 << 8) | kMagic1;
kono
parents:
diff changeset
21 static const u64 kMagic4 = (kMagic2 << 16) | kMagic2;
kono
parents:
diff changeset
22 static const u64 kMagic8 = (kMagic4 << 32) | kMagic4;
kono
parents:
diff changeset
23
kono
parents:
diff changeset
24 static const u64 kAllocaRedzoneSize = 32UL;
kono
parents:
diff changeset
25 static const u64 kAllocaRedzoneMask = 31UL;
kono
parents:
diff changeset
26
kono
parents:
diff changeset
27 // For small size classes inline PoisonShadow for better performance.
kono
parents:
diff changeset
28 ALWAYS_INLINE void SetShadow(uptr ptr, uptr size, uptr class_id, u64 magic) {
kono
parents:
diff changeset
29 CHECK_EQ(SHADOW_SCALE, 3); // This code expects SHADOW_SCALE=3.
kono
parents:
diff changeset
30 u64 *shadow = reinterpret_cast<u64*>(MemToShadow(ptr));
kono
parents:
diff changeset
31 if (class_id <= 6) {
kono
parents:
diff changeset
32 for (uptr i = 0; i < (((uptr)1) << class_id); i++) {
kono
parents:
diff changeset
33 shadow[i] = magic;
kono
parents:
diff changeset
34 // Make sure this does not become memset.
kono
parents:
diff changeset
35 SanitizerBreakOptimization(nullptr);
kono
parents:
diff changeset
36 }
kono
parents:
diff changeset
37 } else {
kono
parents:
diff changeset
38 // The size class is too big, it's cheaper to poison only size bytes.
kono
parents:
diff changeset
39 PoisonShadow(ptr, size, static_cast<u8>(magic));
kono
parents:
diff changeset
40 }
kono
parents:
diff changeset
41 }
kono
parents:
diff changeset
42
kono
parents:
diff changeset
43 FakeStack *FakeStack::Create(uptr stack_size_log) {
kono
parents:
diff changeset
44 static uptr kMinStackSizeLog = 16;
kono
parents:
diff changeset
45 static uptr kMaxStackSizeLog = FIRST_32_SECOND_64(24, 28);
kono
parents:
diff changeset
46 if (stack_size_log < kMinStackSizeLog)
kono
parents:
diff changeset
47 stack_size_log = kMinStackSizeLog;
kono
parents:
diff changeset
48 if (stack_size_log > kMaxStackSizeLog)
kono
parents:
diff changeset
49 stack_size_log = kMaxStackSizeLog;
kono
parents:
diff changeset
50 uptr size = RequiredSize(stack_size_log);
kono
parents:
diff changeset
51 FakeStack *res = reinterpret_cast<FakeStack *>(
kono
parents:
diff changeset
52 flags()->uar_noreserve ? MmapNoReserveOrDie(size, "FakeStack")
kono
parents:
diff changeset
53 : MmapOrDie(size, "FakeStack"));
kono
parents:
diff changeset
54 res->stack_size_log_ = stack_size_log;
kono
parents:
diff changeset
55 u8 *p = reinterpret_cast<u8 *>(res);
kono
parents:
diff changeset
56 VReport(1, "T%d: FakeStack created: %p -- %p stack_size_log: %zd; "
kono
parents:
diff changeset
57 "mmapped %zdK, noreserve=%d \n",
kono
parents:
diff changeset
58 GetCurrentTidOrInvalid(), p,
kono
parents:
diff changeset
59 p + FakeStack::RequiredSize(stack_size_log), stack_size_log,
kono
parents:
diff changeset
60 size >> 10, flags()->uar_noreserve);
kono
parents:
diff changeset
61 return res;
kono
parents:
diff changeset
62 }
kono
parents:
diff changeset
63
kono
parents:
diff changeset
64 void FakeStack::Destroy(int tid) {
kono
parents:
diff changeset
65 PoisonAll(0);
kono
parents:
diff changeset
66 if (Verbosity() >= 2) {
kono
parents:
diff changeset
67 InternalScopedString str(kNumberOfSizeClasses * 50);
kono
parents:
diff changeset
68 for (uptr class_id = 0; class_id < kNumberOfSizeClasses; class_id++)
kono
parents:
diff changeset
69 str.append("%zd: %zd/%zd; ", class_id, hint_position_[class_id],
kono
parents:
diff changeset
70 NumberOfFrames(stack_size_log(), class_id));
kono
parents:
diff changeset
71 Report("T%d: FakeStack destroyed: %s\n", tid, str.data());
kono
parents:
diff changeset
72 }
kono
parents:
diff changeset
73 uptr size = RequiredSize(stack_size_log_);
kono
parents:
diff changeset
74 FlushUnneededASanShadowMemory(reinterpret_cast<uptr>(this), size);
kono
parents:
diff changeset
75 UnmapOrDie(this, size);
kono
parents:
diff changeset
76 }
kono
parents:
diff changeset
77
kono
parents:
diff changeset
78 void FakeStack::PoisonAll(u8 magic) {
kono
parents:
diff changeset
79 PoisonShadow(reinterpret_cast<uptr>(this), RequiredSize(stack_size_log()),
kono
parents:
diff changeset
80 magic);
kono
parents:
diff changeset
81 }
kono
parents:
diff changeset
82
kono
parents:
diff changeset
83 #if !defined(_MSC_VER) || defined(__clang__)
kono
parents:
diff changeset
84 ALWAYS_INLINE USED
kono
parents:
diff changeset
85 #endif
kono
parents:
diff changeset
86 FakeFrame *FakeStack::Allocate(uptr stack_size_log, uptr class_id,
kono
parents:
diff changeset
87 uptr real_stack) {
kono
parents:
diff changeset
88 CHECK_LT(class_id, kNumberOfSizeClasses);
kono
parents:
diff changeset
89 if (needs_gc_)
kono
parents:
diff changeset
90 GC(real_stack);
kono
parents:
diff changeset
91 uptr &hint_position = hint_position_[class_id];
kono
parents:
diff changeset
92 const int num_iter = NumberOfFrames(stack_size_log, class_id);
kono
parents:
diff changeset
93 u8 *flags = GetFlags(stack_size_log, class_id);
kono
parents:
diff changeset
94 for (int i = 0; i < num_iter; i++) {
kono
parents:
diff changeset
95 uptr pos = ModuloNumberOfFrames(stack_size_log, class_id, hint_position++);
kono
parents:
diff changeset
96 // This part is tricky. On one hand, checking and setting flags[pos]
kono
parents:
diff changeset
97 // should be atomic to ensure async-signal safety. But on the other hand,
kono
parents:
diff changeset
98 // if the signal arrives between checking and setting flags[pos], the
kono
parents:
diff changeset
99 // signal handler's fake stack will start from a different hint_position
kono
parents:
diff changeset
100 // and so will not touch this particular byte. So, it is safe to do this
kono
parents:
diff changeset
101 // with regular non-atomic load and store (at least I was not able to make
kono
parents:
diff changeset
102 // this code crash).
kono
parents:
diff changeset
103 if (flags[pos]) continue;
kono
parents:
diff changeset
104 flags[pos] = 1;
kono
parents:
diff changeset
105 FakeFrame *res = reinterpret_cast<FakeFrame *>(
kono
parents:
diff changeset
106 GetFrame(stack_size_log, class_id, pos));
kono
parents:
diff changeset
107 res->real_stack = real_stack;
kono
parents:
diff changeset
108 *SavedFlagPtr(reinterpret_cast<uptr>(res), class_id) = &flags[pos];
kono
parents:
diff changeset
109 return res;
kono
parents:
diff changeset
110 }
kono
parents:
diff changeset
111 return nullptr; // We are out of fake stack.
kono
parents:
diff changeset
112 }
kono
parents:
diff changeset
113
kono
parents:
diff changeset
114 uptr FakeStack::AddrIsInFakeStack(uptr ptr, uptr *frame_beg, uptr *frame_end) {
kono
parents:
diff changeset
115 uptr stack_size_log = this->stack_size_log();
kono
parents:
diff changeset
116 uptr beg = reinterpret_cast<uptr>(GetFrame(stack_size_log, 0, 0));
kono
parents:
diff changeset
117 uptr end = reinterpret_cast<uptr>(this) + RequiredSize(stack_size_log);
kono
parents:
diff changeset
118 if (ptr < beg || ptr >= end) return 0;
kono
parents:
diff changeset
119 uptr class_id = (ptr - beg) >> stack_size_log;
kono
parents:
diff changeset
120 uptr base = beg + (class_id << stack_size_log);
kono
parents:
diff changeset
121 CHECK_LE(base, ptr);
kono
parents:
diff changeset
122 CHECK_LT(ptr, base + (((uptr)1) << stack_size_log));
kono
parents:
diff changeset
123 uptr pos = (ptr - base) >> (kMinStackFrameSizeLog + class_id);
kono
parents:
diff changeset
124 uptr res = base + pos * BytesInSizeClass(class_id);
kono
parents:
diff changeset
125 *frame_end = res + BytesInSizeClass(class_id);
kono
parents:
diff changeset
126 *frame_beg = res + sizeof(FakeFrame);
kono
parents:
diff changeset
127 return res;
kono
parents:
diff changeset
128 }
kono
parents:
diff changeset
129
kono
parents:
diff changeset
130 void FakeStack::HandleNoReturn() {
kono
parents:
diff changeset
131 needs_gc_ = true;
kono
parents:
diff changeset
132 }
kono
parents:
diff changeset
133
kono
parents:
diff changeset
134 // When throw, longjmp or some such happens we don't call OnFree() and
kono
parents:
diff changeset
135 // as the result may leak one or more fake frames, but the good news is that
kono
parents:
diff changeset
136 // we are notified about all such events by HandleNoReturn().
kono
parents:
diff changeset
137 // If we recently had such no-return event we need to collect garbage frames.
kono
parents:
diff changeset
138 // We do it based on their 'real_stack' values -- everything that is lower
kono
parents:
diff changeset
139 // than the current real_stack is garbage.
kono
parents:
diff changeset
140 NOINLINE void FakeStack::GC(uptr real_stack) {
kono
parents:
diff changeset
141 uptr collected = 0;
kono
parents:
diff changeset
142 for (uptr class_id = 0; class_id < kNumberOfSizeClasses; class_id++) {
kono
parents:
diff changeset
143 u8 *flags = GetFlags(stack_size_log(), class_id);
kono
parents:
diff changeset
144 for (uptr i = 0, n = NumberOfFrames(stack_size_log(), class_id); i < n;
kono
parents:
diff changeset
145 i++) {
kono
parents:
diff changeset
146 if (flags[i] == 0) continue; // not allocated.
kono
parents:
diff changeset
147 FakeFrame *ff = reinterpret_cast<FakeFrame *>(
kono
parents:
diff changeset
148 GetFrame(stack_size_log(), class_id, i));
kono
parents:
diff changeset
149 if (ff->real_stack < real_stack) {
kono
parents:
diff changeset
150 flags[i] = 0;
kono
parents:
diff changeset
151 collected++;
kono
parents:
diff changeset
152 }
kono
parents:
diff changeset
153 }
kono
parents:
diff changeset
154 }
kono
parents:
diff changeset
155 needs_gc_ = false;
kono
parents:
diff changeset
156 }
kono
parents:
diff changeset
157
kono
parents:
diff changeset
158 void FakeStack::ForEachFakeFrame(RangeIteratorCallback callback, void *arg) {
kono
parents:
diff changeset
159 for (uptr class_id = 0; class_id < kNumberOfSizeClasses; class_id++) {
kono
parents:
diff changeset
160 u8 *flags = GetFlags(stack_size_log(), class_id);
kono
parents:
diff changeset
161 for (uptr i = 0, n = NumberOfFrames(stack_size_log(), class_id); i < n;
kono
parents:
diff changeset
162 i++) {
kono
parents:
diff changeset
163 if (flags[i] == 0) continue; // not allocated.
kono
parents:
diff changeset
164 FakeFrame *ff = reinterpret_cast<FakeFrame *>(
kono
parents:
diff changeset
165 GetFrame(stack_size_log(), class_id, i));
kono
parents:
diff changeset
166 uptr begin = reinterpret_cast<uptr>(ff);
kono
parents:
diff changeset
167 callback(begin, begin + FakeStack::BytesInSizeClass(class_id), arg);
kono
parents:
diff changeset
168 }
kono
parents:
diff changeset
169 }
kono
parents:
diff changeset
170 }
kono
parents:
diff changeset
171
kono
parents:
diff changeset
172 #if (SANITIZER_LINUX && !SANITIZER_ANDROID) || SANITIZER_FUCHSIA
kono
parents:
diff changeset
173 static THREADLOCAL FakeStack *fake_stack_tls;
kono
parents:
diff changeset
174
kono
parents:
diff changeset
175 FakeStack *GetTLSFakeStack() {
kono
parents:
diff changeset
176 return fake_stack_tls;
kono
parents:
diff changeset
177 }
kono
parents:
diff changeset
178 void SetTLSFakeStack(FakeStack *fs) {
kono
parents:
diff changeset
179 fake_stack_tls = fs;
kono
parents:
diff changeset
180 }
kono
parents:
diff changeset
181 #else
kono
parents:
diff changeset
182 FakeStack *GetTLSFakeStack() { return 0; }
kono
parents:
diff changeset
183 void SetTLSFakeStack(FakeStack *fs) { }
kono
parents:
diff changeset
184 #endif // (SANITIZER_LINUX && !SANITIZER_ANDROID) || SANITIZER_FUCHSIA
kono
parents:
diff changeset
185
kono
parents:
diff changeset
186 static FakeStack *GetFakeStack() {
kono
parents:
diff changeset
187 AsanThread *t = GetCurrentThread();
kono
parents:
diff changeset
188 if (!t) return nullptr;
kono
parents:
diff changeset
189 return t->fake_stack();
kono
parents:
diff changeset
190 }
kono
parents:
diff changeset
191
kono
parents:
diff changeset
192 static FakeStack *GetFakeStackFast() {
kono
parents:
diff changeset
193 if (FakeStack *fs = GetTLSFakeStack())
kono
parents:
diff changeset
194 return fs;
kono
parents:
diff changeset
195 if (!__asan_option_detect_stack_use_after_return)
kono
parents:
diff changeset
196 return nullptr;
kono
parents:
diff changeset
197 return GetFakeStack();
kono
parents:
diff changeset
198 }
kono
parents:
diff changeset
199
kono
parents:
diff changeset
200 ALWAYS_INLINE uptr OnMalloc(uptr class_id, uptr size) {
kono
parents:
diff changeset
201 FakeStack *fs = GetFakeStackFast();
kono
parents:
diff changeset
202 if (!fs) return 0;
kono
parents:
diff changeset
203 uptr local_stack;
kono
parents:
diff changeset
204 uptr real_stack = reinterpret_cast<uptr>(&local_stack);
kono
parents:
diff changeset
205 FakeFrame *ff = fs->Allocate(fs->stack_size_log(), class_id, real_stack);
kono
parents:
diff changeset
206 if (!ff) return 0; // Out of fake stack.
kono
parents:
diff changeset
207 uptr ptr = reinterpret_cast<uptr>(ff);
kono
parents:
diff changeset
208 SetShadow(ptr, size, class_id, 0);
kono
parents:
diff changeset
209 return ptr;
kono
parents:
diff changeset
210 }
kono
parents:
diff changeset
211
kono
parents:
diff changeset
212 ALWAYS_INLINE void OnFree(uptr ptr, uptr class_id, uptr size) {
kono
parents:
diff changeset
213 FakeStack::Deallocate(ptr, class_id);
kono
parents:
diff changeset
214 SetShadow(ptr, size, class_id, kMagic8);
kono
parents:
diff changeset
215 }
kono
parents:
diff changeset
216
kono
parents:
diff changeset
217 } // namespace __asan
kono
parents:
diff changeset
218
kono
parents:
diff changeset
219 // ---------------------- Interface ---------------- {{{1
kono
parents:
diff changeset
220 using namespace __asan;
kono
parents:
diff changeset
221 #define DEFINE_STACK_MALLOC_FREE_WITH_CLASS_ID(class_id) \
kono
parents:
diff changeset
222 extern "C" SANITIZER_INTERFACE_ATTRIBUTE uptr \
kono
parents:
diff changeset
223 __asan_stack_malloc_##class_id(uptr size) { \
kono
parents:
diff changeset
224 return OnMalloc(class_id, size); \
kono
parents:
diff changeset
225 } \
kono
parents:
diff changeset
226 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void __asan_stack_free_##class_id( \
kono
parents:
diff changeset
227 uptr ptr, uptr size) { \
kono
parents:
diff changeset
228 OnFree(ptr, class_id, size); \
kono
parents:
diff changeset
229 }
kono
parents:
diff changeset
230
kono
parents:
diff changeset
231 DEFINE_STACK_MALLOC_FREE_WITH_CLASS_ID(0)
kono
parents:
diff changeset
232 DEFINE_STACK_MALLOC_FREE_WITH_CLASS_ID(1)
kono
parents:
diff changeset
233 DEFINE_STACK_MALLOC_FREE_WITH_CLASS_ID(2)
kono
parents:
diff changeset
234 DEFINE_STACK_MALLOC_FREE_WITH_CLASS_ID(3)
kono
parents:
diff changeset
235 DEFINE_STACK_MALLOC_FREE_WITH_CLASS_ID(4)
kono
parents:
diff changeset
236 DEFINE_STACK_MALLOC_FREE_WITH_CLASS_ID(5)
kono
parents:
diff changeset
237 DEFINE_STACK_MALLOC_FREE_WITH_CLASS_ID(6)
kono
parents:
diff changeset
238 DEFINE_STACK_MALLOC_FREE_WITH_CLASS_ID(7)
kono
parents:
diff changeset
239 DEFINE_STACK_MALLOC_FREE_WITH_CLASS_ID(8)
kono
parents:
diff changeset
240 DEFINE_STACK_MALLOC_FREE_WITH_CLASS_ID(9)
kono
parents:
diff changeset
241 DEFINE_STACK_MALLOC_FREE_WITH_CLASS_ID(10)
kono
parents:
diff changeset
242 extern "C" {
kono
parents:
diff changeset
243 SANITIZER_INTERFACE_ATTRIBUTE
kono
parents:
diff changeset
244 void *__asan_get_current_fake_stack() { return GetFakeStackFast(); }
kono
parents:
diff changeset
245
kono
parents:
diff changeset
246 SANITIZER_INTERFACE_ATTRIBUTE
kono
parents:
diff changeset
247 void *__asan_addr_is_in_fake_stack(void *fake_stack, void *addr, void **beg,
kono
parents:
diff changeset
248 void **end) {
kono
parents:
diff changeset
249 FakeStack *fs = reinterpret_cast<FakeStack*>(fake_stack);
kono
parents:
diff changeset
250 if (!fs) return nullptr;
kono
parents:
diff changeset
251 uptr frame_beg, frame_end;
kono
parents:
diff changeset
252 FakeFrame *frame = reinterpret_cast<FakeFrame *>(fs->AddrIsInFakeStack(
kono
parents:
diff changeset
253 reinterpret_cast<uptr>(addr), &frame_beg, &frame_end));
kono
parents:
diff changeset
254 if (!frame) return nullptr;
kono
parents:
diff changeset
255 if (frame->magic != kCurrentStackFrameMagic)
kono
parents:
diff changeset
256 return nullptr;
kono
parents:
diff changeset
257 if (beg) *beg = reinterpret_cast<void*>(frame_beg);
kono
parents:
diff changeset
258 if (end) *end = reinterpret_cast<void*>(frame_end);
kono
parents:
diff changeset
259 return reinterpret_cast<void*>(frame->real_stack);
kono
parents:
diff changeset
260 }
kono
parents:
diff changeset
261
kono
parents:
diff changeset
262 SANITIZER_INTERFACE_ATTRIBUTE
kono
parents:
diff changeset
263 void __asan_alloca_poison(uptr addr, uptr size) {
kono
parents:
diff changeset
264 uptr LeftRedzoneAddr = addr - kAllocaRedzoneSize;
kono
parents:
diff changeset
265 uptr PartialRzAddr = addr + size;
kono
parents:
diff changeset
266 uptr RightRzAddr = (PartialRzAddr + kAllocaRedzoneMask) & ~kAllocaRedzoneMask;
kono
parents:
diff changeset
267 uptr PartialRzAligned = PartialRzAddr & ~(SHADOW_GRANULARITY - 1);
kono
parents:
diff changeset
268 FastPoisonShadow(LeftRedzoneAddr, kAllocaRedzoneSize, kAsanAllocaLeftMagic);
kono
parents:
diff changeset
269 FastPoisonShadowPartialRightRedzone(
kono
parents:
diff changeset
270 PartialRzAligned, PartialRzAddr % SHADOW_GRANULARITY,
kono
parents:
diff changeset
271 RightRzAddr - PartialRzAligned, kAsanAllocaRightMagic);
kono
parents:
diff changeset
272 FastPoisonShadow(RightRzAddr, kAllocaRedzoneSize, kAsanAllocaRightMagic);
kono
parents:
diff changeset
273 }
kono
parents:
diff changeset
274
kono
parents:
diff changeset
275 SANITIZER_INTERFACE_ATTRIBUTE
kono
parents:
diff changeset
276 void __asan_allocas_unpoison(uptr top, uptr bottom) {
kono
parents:
diff changeset
277 if ((!top) || (top > bottom)) return;
kono
parents:
diff changeset
278 REAL(memset)(reinterpret_cast<void*>(MemToShadow(top)), 0,
kono
parents:
diff changeset
279 (bottom - top) / SHADOW_GRANULARITY);
kono
parents:
diff changeset
280 }
kono
parents:
diff changeset
281 } // extern "C"