1 //===-- asan_thread.cc ----------------------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is a part of AddressSanitizer, an address sanity checker.
10 // Thread-related code.
11 //===----------------------------------------------------------------------===//
12 #include "asan_allocator.h"
13 #include "asan_interceptors.h"
14 #include "asan_poisoning.h"
15 #include "asan_stack.h"
16 #include "asan_thread.h"
17 #include "asan_mapping.h"
18 #include "sanitizer_common/sanitizer_common.h"
19 #include "sanitizer_common/sanitizer_placement_new.h"
20 #include "sanitizer_common/sanitizer_stackdepot.h"
21 #include "sanitizer_common/sanitizer_tls_get_addr.h"
22 #include "lsan/lsan_common.h"
26 // AsanThreadContext implementation.
28 struct CreateThreadContextArgs
{
33 void AsanThreadContext::OnCreated(void *arg
) {
34 CreateThreadContextArgs
*args
= static_cast<CreateThreadContextArgs
*>(arg
);
36 stack_id
= StackDepotPut(*args
->stack
);
37 thread
= args
->thread
;
38 thread
->set_context(this);
41 void AsanThreadContext::OnFinished() {
42 // Drop the link to the AsanThread object.
46 // MIPS requires aligned address
47 static ALIGNED(16) char thread_registry_placeholder
[sizeof(ThreadRegistry
)];
48 static ThreadRegistry
*asan_thread_registry
;
50 static BlockingMutex
mu_for_thread_context(LINKER_INITIALIZED
);
51 static LowLevelAllocator allocator_for_thread_context
;
53 static ThreadContextBase
*GetAsanThreadContext(u32 tid
) {
54 BlockingMutexLock
lock(&mu_for_thread_context
);
55 return new(allocator_for_thread_context
) AsanThreadContext(tid
);
58 ThreadRegistry
&asanThreadRegistry() {
59 static bool initialized
;
60 // Don't worry about thread_safety - this should be called when there is
63 // Never reuse ASan threads: we store pointer to AsanThreadContext
64 // in TSD and can't reliably tell when no more TSD destructors will
65 // be called. It would be wrong to reuse AsanThreadContext for another
66 // thread before all TSD destructors will be called for it.
67 asan_thread_registry
= new(thread_registry_placeholder
) ThreadRegistry(
68 GetAsanThreadContext
, kMaxNumberOfThreads
, kMaxNumberOfThreads
);
71 return *asan_thread_registry
;
74 AsanThreadContext
*GetThreadContextByTidLocked(u32 tid
) {
75 return static_cast<AsanThreadContext
*>(
76 asanThreadRegistry().GetThreadLocked(tid
));
79 // AsanThread implementation.
81 AsanThread
*AsanThread::Create(thread_callback_t start_routine
, void *arg
,
82 u32 parent_tid
, StackTrace
*stack
,
84 uptr PageSize
= GetPageSizeCached();
85 uptr size
= RoundUpTo(sizeof(AsanThread
), PageSize
);
86 AsanThread
*thread
= (AsanThread
*)MmapOrDie(size
, __func__
);
87 thread
->start_routine_
= start_routine
;
89 CreateThreadContextArgs args
= { thread
, stack
};
90 asanThreadRegistry().CreateThread(*reinterpret_cast<uptr
*>(thread
), detached
,
96 void AsanThread::TSDDtor(void *tsd
) {
97 AsanThreadContext
*context
= (AsanThreadContext
*)tsd
;
98 VReport(1, "T%d TSDDtor\n", context
->tid
);
100 context
->thread
->Destroy();
103 void AsanThread::Destroy() {
104 int tid
= this->tid();
105 VReport(1, "T%d exited\n", tid
);
107 malloc_storage().CommitBack();
108 if (common_flags()->use_sigaltstack
) UnsetAlternateSignalStack();
109 asanThreadRegistry().FinishThread(tid
);
110 FlushToDeadThreadStats(&stats_
);
111 // We also clear the shadow on thread destruction because
112 // some code may still be executing in later TSD destructors
113 // and we don't want it to have any poisoned stack.
114 ClearShadowForThreadStackAndTLS();
115 DeleteFakeStack(tid
);
116 uptr size
= RoundUpTo(sizeof(AsanThread
), GetPageSizeCached());
117 UnmapOrDie(this, size
);
121 void AsanThread::StartSwitchFiber(FakeStack
**fake_stack_save
, uptr bottom
,
123 if (atomic_load(&stack_switching_
, memory_order_relaxed
)) {
124 Report("ERROR: starting fiber switch while in fiber switch\n");
128 next_stack_bottom_
= bottom
;
129 next_stack_top_
= bottom
+ size
;
130 atomic_store(&stack_switching_
, 1, memory_order_release
);
132 FakeStack
*current_fake_stack
= fake_stack_
;
134 *fake_stack_save
= fake_stack_
;
135 fake_stack_
= nullptr;
136 SetTLSFakeStack(nullptr);
137 // if fake_stack_save is null, the fiber will die, delete the fakestack
138 if (!fake_stack_save
&& current_fake_stack
)
139 current_fake_stack
->Destroy(this->tid());
142 void AsanThread::FinishSwitchFiber(FakeStack
*fake_stack_save
,
145 if (!atomic_load(&stack_switching_
, memory_order_relaxed
)) {
146 Report("ERROR: finishing a fiber switch that has not started\n");
150 if (fake_stack_save
) {
151 SetTLSFakeStack(fake_stack_save
);
152 fake_stack_
= fake_stack_save
;
156 *bottom_old
= stack_bottom_
;
158 *size_old
= stack_top_
- stack_bottom_
;
159 stack_bottom_
= next_stack_bottom_
;
160 stack_top_
= next_stack_top_
;
161 atomic_store(&stack_switching_
, 0, memory_order_release
);
163 next_stack_bottom_
= 0;
166 inline AsanThread::StackBounds
AsanThread::GetStackBounds() const {
167 if (!atomic_load(&stack_switching_
, memory_order_acquire
))
168 return StackBounds
{stack_bottom_
, stack_top_
}; // NOLINT
170 const uptr cur_stack
= (uptr
)&local
;
171 // Note: need to check next stack first, because FinishSwitchFiber
172 // may be in process of overwriting stack_top_/bottom_. But in such case
173 // we are already on the next stack.
174 if (cur_stack
>= next_stack_bottom_
&& cur_stack
< next_stack_top_
)
175 return StackBounds
{next_stack_bottom_
, next_stack_top_
}; // NOLINT
176 return StackBounds
{stack_bottom_
, stack_top_
}; // NOLINT
179 uptr
AsanThread::stack_top() {
180 return GetStackBounds().top
;
183 uptr
AsanThread::stack_bottom() {
184 return GetStackBounds().bottom
;
187 uptr
AsanThread::stack_size() {
188 const auto bounds
= GetStackBounds();
189 return bounds
.top
- bounds
.bottom
;
192 // We want to create the FakeStack lazyly on the first use, but not eralier
193 // than the stack size is known and the procedure has to be async-signal safe.
194 FakeStack
*AsanThread::AsyncSignalSafeLazyInitFakeStack() {
195 uptr stack_size
= this->stack_size();
196 if (stack_size
== 0) // stack_size is not yet available, don't use FakeStack.
199 // fake_stack_ has 3 states:
200 // 0 -- not initialized
201 // 1 -- being initialized
202 // ptr -- initialized
203 // This CAS checks if the state was 0 and if so changes it to state 1,
204 // if that was successful, it initializes the pointer.
205 if (atomic_compare_exchange_strong(
206 reinterpret_cast<atomic_uintptr_t
*>(&fake_stack_
), &old_val
, 1UL,
207 memory_order_relaxed
)) {
208 uptr stack_size_log
= Log2(RoundUpToPowerOfTwo(stack_size
));
209 CHECK_LE(flags()->min_uar_stack_size_log
, flags()->max_uar_stack_size_log
);
211 Min(stack_size_log
, static_cast<uptr
>(flags()->max_uar_stack_size_log
));
213 Max(stack_size_log
, static_cast<uptr
>(flags()->min_uar_stack_size_log
));
214 fake_stack_
= FakeStack::Create(stack_size_log
);
215 SetTLSFakeStack(fake_stack_
);
221 void AsanThread::Init() {
222 next_stack_top_
= next_stack_bottom_
= 0;
223 atomic_store(&stack_switching_
, false, memory_order_release
);
224 fake_stack_
= nullptr; // Will be initialized lazily if needed.
225 CHECK_EQ(this->stack_size(), 0U);
226 SetThreadStackAndTls();
227 CHECK_GT(this->stack_size(), 0U);
228 CHECK(AddrIsInMem(stack_bottom_
));
229 CHECK(AddrIsInMem(stack_top_
- 1));
230 ClearShadowForThreadStackAndTLS();
232 VReport(1, "T%d: stack [%p,%p) size 0x%zx; local=%p\n", tid(),
233 (void *)stack_bottom_
, (void *)stack_top_
, stack_top_
- stack_bottom_
,
237 thread_return_t
AsanThread::ThreadStart(
238 uptr os_id
, atomic_uintptr_t
*signal_thread_is_registered
) {
240 asanThreadRegistry().StartThread(tid(), os_id
, nullptr);
241 if (signal_thread_is_registered
)
242 atomic_store(signal_thread_is_registered
, 1, memory_order_release
);
244 if (common_flags()->use_sigaltstack
) SetAlternateSignalStack();
246 if (!start_routine_
) {
247 // start_routine_ == 0 if we're on the main thread or on one of the
248 // OS X libdispatch worker threads. But nobody is supposed to call
249 // ThreadStart() for the worker threads.
254 thread_return_t res
= start_routine_(arg_
);
256 // On POSIX systems we defer this to the TSD destructor. LSan will consider
257 // the thread's memory as non-live from the moment we call Destroy(), even
258 // though that memory might contain pointers to heap objects which will be
259 // cleaned up by a user-defined TSD destructor. Thus, calling Destroy() before
260 // the TSD destructors have run might cause false positives in LSan.
261 if (!SANITIZER_POSIX
)
267 void AsanThread::SetThreadStackAndTls() {
270 GetThreadStackAndTls(tid() == 0, const_cast<uptr
*>(&stack_bottom_
),
271 const_cast<uptr
*>(&stack_size
), &tls_begin_
, &tls_size
);
272 stack_top_
= stack_bottom_
+ stack_size
;
273 tls_end_
= tls_begin_
+ tls_size
;
277 CHECK(AddrIsInStack((uptr
)&local
));
280 void AsanThread::ClearShadowForThreadStackAndTLS() {
281 PoisonShadow(stack_bottom_
, stack_top_
- stack_bottom_
, 0);
282 if (tls_begin_
!= tls_end_
)
283 PoisonShadow(tls_begin_
, tls_end_
- tls_begin_
, 0);
286 bool AsanThread::GetStackFrameAccessByAddr(uptr addr
,
287 StackFrameAccess
*access
) {
289 if (AddrIsInStack(addr
)) {
290 bottom
= stack_bottom();
291 } else if (has_fake_stack()) {
292 bottom
= fake_stack()->AddrIsInFakeStack(addr
);
294 access
->offset
= addr
- bottom
;
295 access
->frame_pc
= ((uptr
*)bottom
)[2];
296 access
->frame_descr
= (const char *)((uptr
*)bottom
)[1];
299 uptr aligned_addr
= addr
& ~(SANITIZER_WORDSIZE
/8 - 1); // align addr.
300 u8
*shadow_ptr
= (u8
*)MemToShadow(aligned_addr
);
301 u8
*shadow_bottom
= (u8
*)MemToShadow(bottom
);
303 while (shadow_ptr
>= shadow_bottom
&&
304 *shadow_ptr
!= kAsanStackLeftRedzoneMagic
) {
308 while (shadow_ptr
>= shadow_bottom
&&
309 *shadow_ptr
== kAsanStackLeftRedzoneMagic
) {
313 if (shadow_ptr
< shadow_bottom
) {
317 uptr
* ptr
= (uptr
*)SHADOW_TO_MEM((uptr
)(shadow_ptr
+ 1));
318 CHECK(ptr
[0] == kCurrentStackFrameMagic
);
319 access
->offset
= addr
- (uptr
)ptr
;
320 access
->frame_pc
= ptr
[2];
321 access
->frame_descr
= (const char*)ptr
[1];
325 bool AsanThread::AddrIsInStack(uptr addr
) {
326 const auto bounds
= GetStackBounds();
327 return addr
>= bounds
.bottom
&& addr
< bounds
.top
;
330 static bool ThreadStackContainsAddress(ThreadContextBase
*tctx_base
,
332 AsanThreadContext
*tctx
= static_cast<AsanThreadContext
*>(tctx_base
);
333 AsanThread
*t
= tctx
->thread
;
334 if (!t
) return false;
335 if (t
->AddrIsInStack((uptr
)addr
)) return true;
336 if (t
->has_fake_stack() && t
->fake_stack()->AddrIsInFakeStack((uptr
)addr
))
341 AsanThread
*GetCurrentThread() {
342 AsanThreadContext
*context
=
343 reinterpret_cast<AsanThreadContext
*>(AsanTSDGet());
345 if (SANITIZER_ANDROID
) {
346 // On Android, libc constructor is called _after_ asan_init, and cleans up
347 // TSD. Try to figure out if this is still the main thread by the stack
348 // address. We are not entirely sure that we have correct main thread
349 // limits, so only do this magic on Android, and only if the found thread
350 // is the main thread.
351 AsanThreadContext
*tctx
= GetThreadContextByTidLocked(0);
352 if (tctx
&& ThreadStackContainsAddress(tctx
, &context
)) {
353 SetCurrentThread(tctx
->thread
);
359 return context
->thread
;
362 void SetCurrentThread(AsanThread
*t
) {
364 VReport(2, "SetCurrentThread: %p for thread %p\n", t
->context(),
365 (void *)GetThreadSelf());
366 // Make sure we do not reset the current AsanThread.
367 CHECK_EQ(0, AsanTSDGet());
368 AsanTSDSet(t
->context());
369 CHECK_EQ(t
->context(), AsanTSDGet());
372 u32
GetCurrentTidOrInvalid() {
373 AsanThread
*t
= GetCurrentThread();
374 return t
? t
->tid() : kInvalidTid
;
377 AsanThread
*FindThreadByStackAddress(uptr addr
) {
378 asanThreadRegistry().CheckLocked();
379 AsanThreadContext
*tctx
= static_cast<AsanThreadContext
*>(
380 asanThreadRegistry().FindThreadContextLocked(ThreadStackContainsAddress
,
382 return tctx
? tctx
->thread
: nullptr;
385 void EnsureMainThreadIDIsCorrect() {
386 AsanThreadContext
*context
=
387 reinterpret_cast<AsanThreadContext
*>(AsanTSDGet());
388 if (context
&& (context
->tid
== 0))
389 context
->os_id
= GetTid();
392 __asan::AsanThread
*GetAsanThreadByOsIDLocked(uptr os_id
) {
393 __asan::AsanThreadContext
*context
= static_cast<__asan::AsanThreadContext
*>(
394 __asan::asanThreadRegistry().FindThreadContextByOsIDLocked(os_id
));
395 if (!context
) return nullptr;
396 return context
->thread
;
398 } // namespace __asan
400 // --- Implementation of LSan-specific functions --- {{{1
402 bool GetThreadRangesLocked(uptr os_id
, uptr
*stack_begin
, uptr
*stack_end
,
403 uptr
*tls_begin
, uptr
*tls_end
, uptr
*cache_begin
,
404 uptr
*cache_end
, DTLS
**dtls
) {
405 __asan::AsanThread
*t
= __asan::GetAsanThreadByOsIDLocked(os_id
);
406 if (!t
) return false;
407 *stack_begin
= t
->stack_bottom();
408 *stack_end
= t
->stack_top();
409 *tls_begin
= t
->tls_begin();
410 *tls_end
= t
->tls_end();
411 // ASan doesn't keep allocator caches in TLS, so these are unused.
418 void ForEachExtraStackRange(uptr os_id
, RangeIteratorCallback callback
,
420 __asan::AsanThread
*t
= __asan::GetAsanThreadByOsIDLocked(os_id
);
421 if (t
&& t
->has_fake_stack())
422 t
->fake_stack()->ForEachFakeFrame(callback
, arg
);
425 void LockThreadRegistry() {
426 __asan::asanThreadRegistry().Lock();
429 void UnlockThreadRegistry() {
430 __asan::asanThreadRegistry().Unlock();
433 void EnsureMainThreadIDIsCorrect() {
434 __asan::EnsureMainThreadIDIsCorrect();
436 } // namespace __lsan
438 // ---------------------- Interface ---------------- {{{1
439 using namespace __asan
; // NOLINT
442 SANITIZER_INTERFACE_ATTRIBUTE
443 void __sanitizer_start_switch_fiber(void **fakestacksave
, const void *bottom
,
445 AsanThread
*t
= GetCurrentThread();
447 VReport(1, "__asan_start_switch_fiber called from unknown thread\n");
450 t
->StartSwitchFiber((FakeStack
**)fakestacksave
, (uptr
)bottom
, size
);
453 SANITIZER_INTERFACE_ATTRIBUTE
454 void __sanitizer_finish_switch_fiber(void* fakestack
,
455 const void **bottom_old
,
457 AsanThread
*t
= GetCurrentThread();
459 VReport(1, "__asan_finish_switch_fiber called from unknown thread\n");
462 t
->FinishSwitchFiber((FakeStack
*)fakestack
,