1*7c3d14c8STreehugger Robot //===-- asan_thread.cc ----------------------------------------------------===//
2*7c3d14c8STreehugger Robot //
3*7c3d14c8STreehugger Robot // The LLVM Compiler Infrastructure
4*7c3d14c8STreehugger Robot //
5*7c3d14c8STreehugger Robot // This file is distributed under the University of Illinois Open Source
6*7c3d14c8STreehugger Robot // License. See LICENSE.TXT for details.
7*7c3d14c8STreehugger Robot //
8*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
9*7c3d14c8STreehugger Robot //
10*7c3d14c8STreehugger Robot // This file is a part of AddressSanitizer, an address sanity checker.
11*7c3d14c8STreehugger Robot //
12*7c3d14c8STreehugger Robot // Thread-related code.
13*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
14*7c3d14c8STreehugger Robot #include "asan_allocator.h"
15*7c3d14c8STreehugger Robot #include "asan_interceptors.h"
16*7c3d14c8STreehugger Robot #include "asan_poisoning.h"
17*7c3d14c8STreehugger Robot #include "asan_stack.h"
18*7c3d14c8STreehugger Robot #include "asan_thread.h"
19*7c3d14c8STreehugger Robot #include "asan_mapping.h"
20*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_common.h"
21*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_placement_new.h"
22*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_stackdepot.h"
23*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_tls_get_addr.h"
24*7c3d14c8STreehugger Robot #include "lsan/lsan_common.h"
25*7c3d14c8STreehugger Robot
26*7c3d14c8STreehugger Robot namespace __asan {
27*7c3d14c8STreehugger Robot
28*7c3d14c8STreehugger Robot // AsanThreadContext implementation.
29*7c3d14c8STreehugger Robot
30*7c3d14c8STreehugger Robot struct CreateThreadContextArgs {
31*7c3d14c8STreehugger Robot AsanThread *thread;
32*7c3d14c8STreehugger Robot StackTrace *stack;
33*7c3d14c8STreehugger Robot };
34*7c3d14c8STreehugger Robot
OnCreated(void * arg)35*7c3d14c8STreehugger Robot void AsanThreadContext::OnCreated(void *arg) {
36*7c3d14c8STreehugger Robot CreateThreadContextArgs *args = static_cast<CreateThreadContextArgs*>(arg);
37*7c3d14c8STreehugger Robot if (args->stack)
38*7c3d14c8STreehugger Robot stack_id = StackDepotPut(*args->stack);
39*7c3d14c8STreehugger Robot thread = args->thread;
40*7c3d14c8STreehugger Robot thread->set_context(this);
41*7c3d14c8STreehugger Robot }
42*7c3d14c8STreehugger Robot
OnFinished()43*7c3d14c8STreehugger Robot void AsanThreadContext::OnFinished() {
44*7c3d14c8STreehugger Robot // Drop the link to the AsanThread object.
45*7c3d14c8STreehugger Robot thread = nullptr;
46*7c3d14c8STreehugger Robot }
47*7c3d14c8STreehugger Robot
48*7c3d14c8STreehugger Robot // MIPS requires aligned address
49*7c3d14c8STreehugger Robot static ALIGNED(16) char thread_registry_placeholder[sizeof(ThreadRegistry)];
50*7c3d14c8STreehugger Robot static ThreadRegistry *asan_thread_registry;
51*7c3d14c8STreehugger Robot
52*7c3d14c8STreehugger Robot static BlockingMutex mu_for_thread_context(LINKER_INITIALIZED);
53*7c3d14c8STreehugger Robot static LowLevelAllocator allocator_for_thread_context;
54*7c3d14c8STreehugger Robot
GetAsanThreadContext(u32 tid)55*7c3d14c8STreehugger Robot static ThreadContextBase *GetAsanThreadContext(u32 tid) {
56*7c3d14c8STreehugger Robot BlockingMutexLock lock(&mu_for_thread_context);
57*7c3d14c8STreehugger Robot return new(allocator_for_thread_context) AsanThreadContext(tid);
58*7c3d14c8STreehugger Robot }
59*7c3d14c8STreehugger Robot
asanThreadRegistry()60*7c3d14c8STreehugger Robot ThreadRegistry &asanThreadRegistry() {
61*7c3d14c8STreehugger Robot static bool initialized;
62*7c3d14c8STreehugger Robot // Don't worry about thread_safety - this should be called when there is
63*7c3d14c8STreehugger Robot // a single thread.
64*7c3d14c8STreehugger Robot if (!initialized) {
65*7c3d14c8STreehugger Robot // Never reuse ASan threads: we store pointer to AsanThreadContext
66*7c3d14c8STreehugger Robot // in TSD and can't reliably tell when no more TSD destructors will
67*7c3d14c8STreehugger Robot // be called. It would be wrong to reuse AsanThreadContext for another
68*7c3d14c8STreehugger Robot // thread before all TSD destructors will be called for it.
69*7c3d14c8STreehugger Robot asan_thread_registry = new(thread_registry_placeholder) ThreadRegistry(
70*7c3d14c8STreehugger Robot GetAsanThreadContext, kMaxNumberOfThreads, kMaxNumberOfThreads);
71*7c3d14c8STreehugger Robot initialized = true;
72*7c3d14c8STreehugger Robot }
73*7c3d14c8STreehugger Robot return *asan_thread_registry;
74*7c3d14c8STreehugger Robot }
75*7c3d14c8STreehugger Robot
GetThreadContextByTidLocked(u32 tid)76*7c3d14c8STreehugger Robot AsanThreadContext *GetThreadContextByTidLocked(u32 tid) {
77*7c3d14c8STreehugger Robot return static_cast<AsanThreadContext *>(
78*7c3d14c8STreehugger Robot asanThreadRegistry().GetThreadLocked(tid));
79*7c3d14c8STreehugger Robot }
80*7c3d14c8STreehugger Robot
81*7c3d14c8STreehugger Robot // AsanThread implementation.
82*7c3d14c8STreehugger Robot
Create(thread_callback_t start_routine,void * arg,u32 parent_tid,StackTrace * stack,bool detached)83*7c3d14c8STreehugger Robot AsanThread *AsanThread::Create(thread_callback_t start_routine, void *arg,
84*7c3d14c8STreehugger Robot u32 parent_tid, StackTrace *stack,
85*7c3d14c8STreehugger Robot bool detached) {
86*7c3d14c8STreehugger Robot uptr PageSize = GetPageSizeCached();
87*7c3d14c8STreehugger Robot uptr size = RoundUpTo(sizeof(AsanThread), PageSize);
88*7c3d14c8STreehugger Robot AsanThread *thread = (AsanThread*)MmapOrDie(size, __func__);
89*7c3d14c8STreehugger Robot thread->start_routine_ = start_routine;
90*7c3d14c8STreehugger Robot thread->arg_ = arg;
91*7c3d14c8STreehugger Robot CreateThreadContextArgs args = { thread, stack };
92*7c3d14c8STreehugger Robot asanThreadRegistry().CreateThread(*reinterpret_cast<uptr *>(thread), detached,
93*7c3d14c8STreehugger Robot parent_tid, &args);
94*7c3d14c8STreehugger Robot
95*7c3d14c8STreehugger Robot return thread;
96*7c3d14c8STreehugger Robot }
97*7c3d14c8STreehugger Robot
TSDDtor(void * tsd)98*7c3d14c8STreehugger Robot void AsanThread::TSDDtor(void *tsd) {
99*7c3d14c8STreehugger Robot AsanThreadContext *context = (AsanThreadContext*)tsd;
100*7c3d14c8STreehugger Robot VReport(1, "T%d TSDDtor\n", context->tid);
101*7c3d14c8STreehugger Robot if (context->thread)
102*7c3d14c8STreehugger Robot context->thread->Destroy();
103*7c3d14c8STreehugger Robot }
104*7c3d14c8STreehugger Robot
Destroy()105*7c3d14c8STreehugger Robot void AsanThread::Destroy() {
106*7c3d14c8STreehugger Robot int tid = this->tid();
107*7c3d14c8STreehugger Robot VReport(1, "T%d exited\n", tid);
108*7c3d14c8STreehugger Robot
109*7c3d14c8STreehugger Robot malloc_storage().CommitBack();
110*7c3d14c8STreehugger Robot if (common_flags()->use_sigaltstack) UnsetAlternateSignalStack();
111*7c3d14c8STreehugger Robot asanThreadRegistry().FinishThread(tid);
112*7c3d14c8STreehugger Robot FlushToDeadThreadStats(&stats_);
113*7c3d14c8STreehugger Robot // We also clear the shadow on thread destruction because
114*7c3d14c8STreehugger Robot // some code may still be executing in later TSD destructors
115*7c3d14c8STreehugger Robot // and we don't want it to have any poisoned stack.
116*7c3d14c8STreehugger Robot ClearShadowForThreadStackAndTLS();
117*7c3d14c8STreehugger Robot DeleteFakeStack(tid);
118*7c3d14c8STreehugger Robot uptr size = RoundUpTo(sizeof(AsanThread), GetPageSizeCached());
119*7c3d14c8STreehugger Robot UnmapOrDie(this, size);
120*7c3d14c8STreehugger Robot DTLS_Destroy();
121*7c3d14c8STreehugger Robot }
122*7c3d14c8STreehugger Robot
StartSwitchFiber(FakeStack ** fake_stack_save,uptr bottom,uptr size)123*7c3d14c8STreehugger Robot void AsanThread::StartSwitchFiber(FakeStack **fake_stack_save, uptr bottom,
124*7c3d14c8STreehugger Robot uptr size) {
125*7c3d14c8STreehugger Robot if (atomic_load(&stack_switching_, memory_order_relaxed)) {
126*7c3d14c8STreehugger Robot Report("ERROR: starting fiber switch while in fiber switch\n");
127*7c3d14c8STreehugger Robot Die();
128*7c3d14c8STreehugger Robot }
129*7c3d14c8STreehugger Robot
130*7c3d14c8STreehugger Robot next_stack_bottom_ = bottom;
131*7c3d14c8STreehugger Robot next_stack_top_ = bottom + size;
132*7c3d14c8STreehugger Robot atomic_store(&stack_switching_, 1, memory_order_release);
133*7c3d14c8STreehugger Robot
134*7c3d14c8STreehugger Robot FakeStack *current_fake_stack = fake_stack_;
135*7c3d14c8STreehugger Robot if (fake_stack_save)
136*7c3d14c8STreehugger Robot *fake_stack_save = fake_stack_;
137*7c3d14c8STreehugger Robot fake_stack_ = nullptr;
138*7c3d14c8STreehugger Robot SetTLSFakeStack(nullptr);
139*7c3d14c8STreehugger Robot // if fake_stack_save is null, the fiber will die, delete the fakestack
140*7c3d14c8STreehugger Robot if (!fake_stack_save && current_fake_stack)
141*7c3d14c8STreehugger Robot current_fake_stack->Destroy(this->tid());
142*7c3d14c8STreehugger Robot }
143*7c3d14c8STreehugger Robot
FinishSwitchFiber(FakeStack * fake_stack_save)144*7c3d14c8STreehugger Robot void AsanThread::FinishSwitchFiber(FakeStack *fake_stack_save) {
145*7c3d14c8STreehugger Robot if (!atomic_load(&stack_switching_, memory_order_relaxed)) {
146*7c3d14c8STreehugger Robot Report("ERROR: finishing a fiber switch that has not started\n");
147*7c3d14c8STreehugger Robot Die();
148*7c3d14c8STreehugger Robot }
149*7c3d14c8STreehugger Robot
150*7c3d14c8STreehugger Robot if (fake_stack_save) {
151*7c3d14c8STreehugger Robot SetTLSFakeStack(fake_stack_save);
152*7c3d14c8STreehugger Robot fake_stack_ = fake_stack_save;
153*7c3d14c8STreehugger Robot }
154*7c3d14c8STreehugger Robot
155*7c3d14c8STreehugger Robot stack_bottom_ = next_stack_bottom_;
156*7c3d14c8STreehugger Robot stack_top_ = next_stack_top_;
157*7c3d14c8STreehugger Robot atomic_store(&stack_switching_, 0, memory_order_release);
158*7c3d14c8STreehugger Robot next_stack_top_ = 0;
159*7c3d14c8STreehugger Robot next_stack_bottom_ = 0;
160*7c3d14c8STreehugger Robot }
161*7c3d14c8STreehugger Robot
GetStackBounds() const162*7c3d14c8STreehugger Robot inline AsanThread::StackBounds AsanThread::GetStackBounds() const {
163*7c3d14c8STreehugger Robot if (!atomic_load(&stack_switching_, memory_order_acquire))
164*7c3d14c8STreehugger Robot return StackBounds{stack_bottom_, stack_top_}; // NOLINT
165*7c3d14c8STreehugger Robot char local;
166*7c3d14c8STreehugger Robot const uptr cur_stack = (uptr)&local;
167*7c3d14c8STreehugger Robot // Note: need to check next stack first, because FinishSwitchFiber
168*7c3d14c8STreehugger Robot // may be in process of overwriting stack_top_/bottom_. But in such case
169*7c3d14c8STreehugger Robot // we are already on the next stack.
170*7c3d14c8STreehugger Robot if (cur_stack >= next_stack_bottom_ && cur_stack < next_stack_top_)
171*7c3d14c8STreehugger Robot return StackBounds{next_stack_bottom_, next_stack_top_}; // NOLINT
172*7c3d14c8STreehugger Robot return StackBounds{stack_bottom_, stack_top_}; // NOLINT
173*7c3d14c8STreehugger Robot }
174*7c3d14c8STreehugger Robot
stack_top()175*7c3d14c8STreehugger Robot uptr AsanThread::stack_top() {
176*7c3d14c8STreehugger Robot return GetStackBounds().top;
177*7c3d14c8STreehugger Robot }
178*7c3d14c8STreehugger Robot
stack_bottom()179*7c3d14c8STreehugger Robot uptr AsanThread::stack_bottom() {
180*7c3d14c8STreehugger Robot return GetStackBounds().bottom;
181*7c3d14c8STreehugger Robot }
182*7c3d14c8STreehugger Robot
stack_size()183*7c3d14c8STreehugger Robot uptr AsanThread::stack_size() {
184*7c3d14c8STreehugger Robot const auto bounds = GetStackBounds();
185*7c3d14c8STreehugger Robot return bounds.top - bounds.bottom;
186*7c3d14c8STreehugger Robot }
187*7c3d14c8STreehugger Robot
188*7c3d14c8STreehugger Robot // We want to create the FakeStack lazyly on the first use, but not eralier
189*7c3d14c8STreehugger Robot // than the stack size is known and the procedure has to be async-signal safe.
AsyncSignalSafeLazyInitFakeStack()190*7c3d14c8STreehugger Robot FakeStack *AsanThread::AsyncSignalSafeLazyInitFakeStack() {
191*7c3d14c8STreehugger Robot uptr stack_size = this->stack_size();
192*7c3d14c8STreehugger Robot if (stack_size == 0) // stack_size is not yet available, don't use FakeStack.
193*7c3d14c8STreehugger Robot return nullptr;
194*7c3d14c8STreehugger Robot uptr old_val = 0;
195*7c3d14c8STreehugger Robot // fake_stack_ has 3 states:
196*7c3d14c8STreehugger Robot // 0 -- not initialized
197*7c3d14c8STreehugger Robot // 1 -- being initialized
198*7c3d14c8STreehugger Robot // ptr -- initialized
199*7c3d14c8STreehugger Robot // This CAS checks if the state was 0 and if so changes it to state 1,
200*7c3d14c8STreehugger Robot // if that was successful, it initializes the pointer.
201*7c3d14c8STreehugger Robot if (atomic_compare_exchange_strong(
202*7c3d14c8STreehugger Robot reinterpret_cast<atomic_uintptr_t *>(&fake_stack_), &old_val, 1UL,
203*7c3d14c8STreehugger Robot memory_order_relaxed)) {
204*7c3d14c8STreehugger Robot uptr stack_size_log = Log2(RoundUpToPowerOfTwo(stack_size));
205*7c3d14c8STreehugger Robot CHECK_LE(flags()->min_uar_stack_size_log, flags()->max_uar_stack_size_log);
206*7c3d14c8STreehugger Robot stack_size_log =
207*7c3d14c8STreehugger Robot Min(stack_size_log, static_cast<uptr>(flags()->max_uar_stack_size_log));
208*7c3d14c8STreehugger Robot stack_size_log =
209*7c3d14c8STreehugger Robot Max(stack_size_log, static_cast<uptr>(flags()->min_uar_stack_size_log));
210*7c3d14c8STreehugger Robot fake_stack_ = FakeStack::Create(stack_size_log);
211*7c3d14c8STreehugger Robot SetTLSFakeStack(fake_stack_);
212*7c3d14c8STreehugger Robot return fake_stack_;
213*7c3d14c8STreehugger Robot }
214*7c3d14c8STreehugger Robot return nullptr;
215*7c3d14c8STreehugger Robot }
216*7c3d14c8STreehugger Robot
Init()217*7c3d14c8STreehugger Robot void AsanThread::Init() {
218*7c3d14c8STreehugger Robot next_stack_top_ = next_stack_bottom_ = 0;
219*7c3d14c8STreehugger Robot atomic_store(&stack_switching_, false, memory_order_release);
220*7c3d14c8STreehugger Robot fake_stack_ = nullptr; // Will be initialized lazily if needed.
221*7c3d14c8STreehugger Robot CHECK_EQ(this->stack_size(), 0U);
222*7c3d14c8STreehugger Robot SetThreadStackAndTls();
223*7c3d14c8STreehugger Robot CHECK_GT(this->stack_size(), 0U);
224*7c3d14c8STreehugger Robot CHECK(AddrIsInMem(stack_bottom_));
225*7c3d14c8STreehugger Robot CHECK(AddrIsInMem(stack_top_ - 1));
226*7c3d14c8STreehugger Robot ClearShadowForThreadStackAndTLS();
227*7c3d14c8STreehugger Robot int local = 0;
228*7c3d14c8STreehugger Robot VReport(1, "T%d: stack [%p,%p) size 0x%zx; local=%p\n", tid(),
229*7c3d14c8STreehugger Robot (void *)stack_bottom_, (void *)stack_top_, stack_top_ - stack_bottom_,
230*7c3d14c8STreehugger Robot &local);
231*7c3d14c8STreehugger Robot }
232*7c3d14c8STreehugger Robot
ThreadStart(uptr os_id,atomic_uintptr_t * signal_thread_is_registered)233*7c3d14c8STreehugger Robot thread_return_t AsanThread::ThreadStart(
234*7c3d14c8STreehugger Robot uptr os_id, atomic_uintptr_t *signal_thread_is_registered) {
235*7c3d14c8STreehugger Robot Init();
236*7c3d14c8STreehugger Robot asanThreadRegistry().StartThread(tid(), os_id, nullptr);
237*7c3d14c8STreehugger Robot if (signal_thread_is_registered)
238*7c3d14c8STreehugger Robot atomic_store(signal_thread_is_registered, 1, memory_order_release);
239*7c3d14c8STreehugger Robot
240*7c3d14c8STreehugger Robot if (common_flags()->use_sigaltstack) SetAlternateSignalStack();
241*7c3d14c8STreehugger Robot
242*7c3d14c8STreehugger Robot if (!start_routine_) {
243*7c3d14c8STreehugger Robot // start_routine_ == 0 if we're on the main thread or on one of the
244*7c3d14c8STreehugger Robot // OS X libdispatch worker threads. But nobody is supposed to call
245*7c3d14c8STreehugger Robot // ThreadStart() for the worker threads.
246*7c3d14c8STreehugger Robot CHECK_EQ(tid(), 0);
247*7c3d14c8STreehugger Robot return 0;
248*7c3d14c8STreehugger Robot }
249*7c3d14c8STreehugger Robot
250*7c3d14c8STreehugger Robot thread_return_t res = start_routine_(arg_);
251*7c3d14c8STreehugger Robot
252*7c3d14c8STreehugger Robot // On POSIX systems we defer this to the TSD destructor. LSan will consider
253*7c3d14c8STreehugger Robot // the thread's memory as non-live from the moment we call Destroy(), even
254*7c3d14c8STreehugger Robot // though that memory might contain pointers to heap objects which will be
255*7c3d14c8STreehugger Robot // cleaned up by a user-defined TSD destructor. Thus, calling Destroy() before
256*7c3d14c8STreehugger Robot // the TSD destructors have run might cause false positives in LSan.
257*7c3d14c8STreehugger Robot if (!SANITIZER_POSIX)
258*7c3d14c8STreehugger Robot this->Destroy();
259*7c3d14c8STreehugger Robot
260*7c3d14c8STreehugger Robot return res;
261*7c3d14c8STreehugger Robot }
262*7c3d14c8STreehugger Robot
SetThreadStackAndTls()263*7c3d14c8STreehugger Robot void AsanThread::SetThreadStackAndTls() {
264*7c3d14c8STreehugger Robot uptr tls_size = 0;
265*7c3d14c8STreehugger Robot uptr stack_size = 0;
266*7c3d14c8STreehugger Robot GetThreadStackAndTls(tid() == 0, const_cast<uptr *>(&stack_bottom_),
267*7c3d14c8STreehugger Robot const_cast<uptr *>(&stack_size), &tls_begin_, &tls_size);
268*7c3d14c8STreehugger Robot stack_top_ = stack_bottom_ + stack_size;
269*7c3d14c8STreehugger Robot tls_end_ = tls_begin_ + tls_size;
270*7c3d14c8STreehugger Robot dtls_ = DTLS_Get();
271*7c3d14c8STreehugger Robot
272*7c3d14c8STreehugger Robot int local;
273*7c3d14c8STreehugger Robot CHECK(AddrIsInStack((uptr)&local));
274*7c3d14c8STreehugger Robot }
275*7c3d14c8STreehugger Robot
ClearShadowForThreadStackAndTLS()276*7c3d14c8STreehugger Robot void AsanThread::ClearShadowForThreadStackAndTLS() {
277*7c3d14c8STreehugger Robot PoisonShadow(stack_bottom_, stack_top_ - stack_bottom_, 0);
278*7c3d14c8STreehugger Robot if (tls_begin_ != tls_end_)
279*7c3d14c8STreehugger Robot PoisonShadow(tls_begin_, tls_end_ - tls_begin_, 0);
280*7c3d14c8STreehugger Robot }
281*7c3d14c8STreehugger Robot
GetStackFrameAccessByAddr(uptr addr,StackFrameAccess * access)282*7c3d14c8STreehugger Robot bool AsanThread::GetStackFrameAccessByAddr(uptr addr,
283*7c3d14c8STreehugger Robot StackFrameAccess *access) {
284*7c3d14c8STreehugger Robot uptr bottom = 0;
285*7c3d14c8STreehugger Robot if (AddrIsInStack(addr)) {
286*7c3d14c8STreehugger Robot bottom = stack_bottom();
287*7c3d14c8STreehugger Robot } else if (has_fake_stack()) {
288*7c3d14c8STreehugger Robot bottom = fake_stack()->AddrIsInFakeStack(addr);
289*7c3d14c8STreehugger Robot CHECK(bottom);
290*7c3d14c8STreehugger Robot access->offset = addr - bottom;
291*7c3d14c8STreehugger Robot access->frame_pc = ((uptr*)bottom)[2];
292*7c3d14c8STreehugger Robot access->frame_descr = (const char *)((uptr*)bottom)[1];
293*7c3d14c8STreehugger Robot return true;
294*7c3d14c8STreehugger Robot }
295*7c3d14c8STreehugger Robot uptr aligned_addr = addr & ~(SANITIZER_WORDSIZE/8 - 1); // align addr.
296*7c3d14c8STreehugger Robot u8 *shadow_ptr = (u8*)MemToShadow(aligned_addr);
297*7c3d14c8STreehugger Robot u8 *shadow_bottom = (u8*)MemToShadow(bottom);
298*7c3d14c8STreehugger Robot
299*7c3d14c8STreehugger Robot while (shadow_ptr >= shadow_bottom &&
300*7c3d14c8STreehugger Robot *shadow_ptr != kAsanStackLeftRedzoneMagic) {
301*7c3d14c8STreehugger Robot shadow_ptr--;
302*7c3d14c8STreehugger Robot }
303*7c3d14c8STreehugger Robot
304*7c3d14c8STreehugger Robot while (shadow_ptr >= shadow_bottom &&
305*7c3d14c8STreehugger Robot *shadow_ptr == kAsanStackLeftRedzoneMagic) {
306*7c3d14c8STreehugger Robot shadow_ptr--;
307*7c3d14c8STreehugger Robot }
308*7c3d14c8STreehugger Robot
309*7c3d14c8STreehugger Robot if (shadow_ptr < shadow_bottom) {
310*7c3d14c8STreehugger Robot return false;
311*7c3d14c8STreehugger Robot }
312*7c3d14c8STreehugger Robot
313*7c3d14c8STreehugger Robot uptr* ptr = (uptr*)SHADOW_TO_MEM((uptr)(shadow_ptr + 1));
314*7c3d14c8STreehugger Robot CHECK(ptr[0] == kCurrentStackFrameMagic);
315*7c3d14c8STreehugger Robot access->offset = addr - (uptr)ptr;
316*7c3d14c8STreehugger Robot access->frame_pc = ptr[2];
317*7c3d14c8STreehugger Robot access->frame_descr = (const char*)ptr[1];
318*7c3d14c8STreehugger Robot return true;
319*7c3d14c8STreehugger Robot }
320*7c3d14c8STreehugger Robot
AddrIsInStack(uptr addr)321*7c3d14c8STreehugger Robot bool AsanThread::AddrIsInStack(uptr addr) {
322*7c3d14c8STreehugger Robot const auto bounds = GetStackBounds();
323*7c3d14c8STreehugger Robot return addr >= bounds.bottom && addr < bounds.top;
324*7c3d14c8STreehugger Robot }
325*7c3d14c8STreehugger Robot
ThreadStackContainsAddress(ThreadContextBase * tctx_base,void * addr)326*7c3d14c8STreehugger Robot static bool ThreadStackContainsAddress(ThreadContextBase *tctx_base,
327*7c3d14c8STreehugger Robot void *addr) {
328*7c3d14c8STreehugger Robot AsanThreadContext *tctx = static_cast<AsanThreadContext*>(tctx_base);
329*7c3d14c8STreehugger Robot AsanThread *t = tctx->thread;
330*7c3d14c8STreehugger Robot if (!t) return false;
331*7c3d14c8STreehugger Robot if (t->AddrIsInStack((uptr)addr)) return true;
332*7c3d14c8STreehugger Robot if (t->has_fake_stack() && t->fake_stack()->AddrIsInFakeStack((uptr)addr))
333*7c3d14c8STreehugger Robot return true;
334*7c3d14c8STreehugger Robot return false;
335*7c3d14c8STreehugger Robot }
336*7c3d14c8STreehugger Robot
GetCurrentThread()337*7c3d14c8STreehugger Robot AsanThread *GetCurrentThread() {
338*7c3d14c8STreehugger Robot AsanThreadContext *context =
339*7c3d14c8STreehugger Robot reinterpret_cast<AsanThreadContext *>(AsanTSDGet());
340*7c3d14c8STreehugger Robot if (!context) {
341*7c3d14c8STreehugger Robot if (SANITIZER_ANDROID) {
342*7c3d14c8STreehugger Robot // On Android, libc constructor is called _after_ asan_init, and cleans up
343*7c3d14c8STreehugger Robot // TSD. Try to figure out if this is still the main thread by the stack
344*7c3d14c8STreehugger Robot // address. We are not entirely sure that we have correct main thread
345*7c3d14c8STreehugger Robot // limits, so only do this magic on Android, and only if the found thread
346*7c3d14c8STreehugger Robot // is the main thread.
347*7c3d14c8STreehugger Robot AsanThreadContext *tctx = GetThreadContextByTidLocked(0);
348*7c3d14c8STreehugger Robot if (ThreadStackContainsAddress(tctx, &context)) {
349*7c3d14c8STreehugger Robot SetCurrentThread(tctx->thread);
350*7c3d14c8STreehugger Robot return tctx->thread;
351*7c3d14c8STreehugger Robot }
352*7c3d14c8STreehugger Robot }
353*7c3d14c8STreehugger Robot return nullptr;
354*7c3d14c8STreehugger Robot }
355*7c3d14c8STreehugger Robot return context->thread;
356*7c3d14c8STreehugger Robot }
357*7c3d14c8STreehugger Robot
SetCurrentThread(AsanThread * t)358*7c3d14c8STreehugger Robot void SetCurrentThread(AsanThread *t) {
359*7c3d14c8STreehugger Robot CHECK(t->context());
360*7c3d14c8STreehugger Robot VReport(2, "SetCurrentThread: %p for thread %p\n", t->context(),
361*7c3d14c8STreehugger Robot (void *)GetThreadSelf());
362*7c3d14c8STreehugger Robot // Make sure we do not reset the current AsanThread.
363*7c3d14c8STreehugger Robot CHECK_EQ(0, AsanTSDGet());
364*7c3d14c8STreehugger Robot AsanTSDSet(t->context());
365*7c3d14c8STreehugger Robot CHECK_EQ(t->context(), AsanTSDGet());
366*7c3d14c8STreehugger Robot }
367*7c3d14c8STreehugger Robot
GetCurrentTidOrInvalid()368*7c3d14c8STreehugger Robot u32 GetCurrentTidOrInvalid() {
369*7c3d14c8STreehugger Robot AsanThread *t = GetCurrentThread();
370*7c3d14c8STreehugger Robot return t ? t->tid() : kInvalidTid;
371*7c3d14c8STreehugger Robot }
372*7c3d14c8STreehugger Robot
FindThreadByStackAddress(uptr addr)373*7c3d14c8STreehugger Robot AsanThread *FindThreadByStackAddress(uptr addr) {
374*7c3d14c8STreehugger Robot asanThreadRegistry().CheckLocked();
375*7c3d14c8STreehugger Robot AsanThreadContext *tctx = static_cast<AsanThreadContext *>(
376*7c3d14c8STreehugger Robot asanThreadRegistry().FindThreadContextLocked(ThreadStackContainsAddress,
377*7c3d14c8STreehugger Robot (void *)addr));
378*7c3d14c8STreehugger Robot return tctx ? tctx->thread : nullptr;
379*7c3d14c8STreehugger Robot }
380*7c3d14c8STreehugger Robot
EnsureMainThreadIDIsCorrect()381*7c3d14c8STreehugger Robot void EnsureMainThreadIDIsCorrect() {
382*7c3d14c8STreehugger Robot AsanThreadContext *context =
383*7c3d14c8STreehugger Robot reinterpret_cast<AsanThreadContext *>(AsanTSDGet());
384*7c3d14c8STreehugger Robot if (context && (context->tid == 0))
385*7c3d14c8STreehugger Robot context->os_id = GetTid();
386*7c3d14c8STreehugger Robot }
387*7c3d14c8STreehugger Robot
GetAsanThreadByOsIDLocked(uptr os_id)388*7c3d14c8STreehugger Robot __asan::AsanThread *GetAsanThreadByOsIDLocked(uptr os_id) {
389*7c3d14c8STreehugger Robot __asan::AsanThreadContext *context = static_cast<__asan::AsanThreadContext *>(
390*7c3d14c8STreehugger Robot __asan::asanThreadRegistry().FindThreadContextByOsIDLocked(os_id));
391*7c3d14c8STreehugger Robot if (!context) return nullptr;
392*7c3d14c8STreehugger Robot return context->thread;
393*7c3d14c8STreehugger Robot }
394*7c3d14c8STreehugger Robot } // namespace __asan
395*7c3d14c8STreehugger Robot
396*7c3d14c8STreehugger Robot // --- Implementation of LSan-specific functions --- {{{1
397*7c3d14c8STreehugger Robot namespace __lsan {
GetThreadRangesLocked(uptr os_id,uptr * stack_begin,uptr * stack_end,uptr * tls_begin,uptr * tls_end,uptr * cache_begin,uptr * cache_end,DTLS ** dtls)398*7c3d14c8STreehugger Robot bool GetThreadRangesLocked(uptr os_id, uptr *stack_begin, uptr *stack_end,
399*7c3d14c8STreehugger Robot uptr *tls_begin, uptr *tls_end, uptr *cache_begin,
400*7c3d14c8STreehugger Robot uptr *cache_end, DTLS **dtls) {
401*7c3d14c8STreehugger Robot __asan::AsanThread *t = __asan::GetAsanThreadByOsIDLocked(os_id);
402*7c3d14c8STreehugger Robot if (!t) return false;
403*7c3d14c8STreehugger Robot *stack_begin = t->stack_bottom();
404*7c3d14c8STreehugger Robot *stack_end = t->stack_top();
405*7c3d14c8STreehugger Robot *tls_begin = t->tls_begin();
406*7c3d14c8STreehugger Robot *tls_end = t->tls_end();
407*7c3d14c8STreehugger Robot // ASan doesn't keep allocator caches in TLS, so these are unused.
408*7c3d14c8STreehugger Robot *cache_begin = 0;
409*7c3d14c8STreehugger Robot *cache_end = 0;
410*7c3d14c8STreehugger Robot *dtls = t->dtls();
411*7c3d14c8STreehugger Robot return true;
412*7c3d14c8STreehugger Robot }
413*7c3d14c8STreehugger Robot
ForEachExtraStackRange(uptr os_id,RangeIteratorCallback callback,void * arg)414*7c3d14c8STreehugger Robot void ForEachExtraStackRange(uptr os_id, RangeIteratorCallback callback,
415*7c3d14c8STreehugger Robot void *arg) {
416*7c3d14c8STreehugger Robot __asan::AsanThread *t = __asan::GetAsanThreadByOsIDLocked(os_id);
417*7c3d14c8STreehugger Robot if (t && t->has_fake_stack())
418*7c3d14c8STreehugger Robot t->fake_stack()->ForEachFakeFrame(callback, arg);
419*7c3d14c8STreehugger Robot }
420*7c3d14c8STreehugger Robot
LockThreadRegistry()421*7c3d14c8STreehugger Robot void LockThreadRegistry() {
422*7c3d14c8STreehugger Robot __asan::asanThreadRegistry().Lock();
423*7c3d14c8STreehugger Robot }
424*7c3d14c8STreehugger Robot
UnlockThreadRegistry()425*7c3d14c8STreehugger Robot void UnlockThreadRegistry() {
426*7c3d14c8STreehugger Robot __asan::asanThreadRegistry().Unlock();
427*7c3d14c8STreehugger Robot }
428*7c3d14c8STreehugger Robot
EnsureMainThreadIDIsCorrect()429*7c3d14c8STreehugger Robot void EnsureMainThreadIDIsCorrect() {
430*7c3d14c8STreehugger Robot __asan::EnsureMainThreadIDIsCorrect();
431*7c3d14c8STreehugger Robot }
432*7c3d14c8STreehugger Robot } // namespace __lsan
433*7c3d14c8STreehugger Robot
434*7c3d14c8STreehugger Robot // ---------------------- Interface ---------------- {{{1
435*7c3d14c8STreehugger Robot using namespace __asan; // NOLINT
436*7c3d14c8STreehugger Robot
437*7c3d14c8STreehugger Robot extern "C" {
438*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE
__sanitizer_start_switch_fiber(void ** fakestacksave,const void * bottom,uptr size)439*7c3d14c8STreehugger Robot void __sanitizer_start_switch_fiber(void **fakestacksave, const void *bottom,
440*7c3d14c8STreehugger Robot uptr size) {
441*7c3d14c8STreehugger Robot AsanThread *t = GetCurrentThread();
442*7c3d14c8STreehugger Robot if (!t) {
443*7c3d14c8STreehugger Robot VReport(1, "__asan_start_switch_fiber called from unknown thread\n");
444*7c3d14c8STreehugger Robot return;
445*7c3d14c8STreehugger Robot }
446*7c3d14c8STreehugger Robot t->StartSwitchFiber((FakeStack**)fakestacksave, (uptr)bottom, size);
447*7c3d14c8STreehugger Robot }
448*7c3d14c8STreehugger Robot
449*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE
__sanitizer_finish_switch_fiber(void * fakestack)450*7c3d14c8STreehugger Robot void __sanitizer_finish_switch_fiber(void* fakestack) {
451*7c3d14c8STreehugger Robot AsanThread *t = GetCurrentThread();
452*7c3d14c8STreehugger Robot if (!t) {
453*7c3d14c8STreehugger Robot VReport(1, "__asan_finish_switch_fiber called from unknown thread\n");
454*7c3d14c8STreehugger Robot return;
455*7c3d14c8STreehugger Robot }
456*7c3d14c8STreehugger Robot t->FinishSwitchFiber((FakeStack*)fakestack);
457*7c3d14c8STreehugger Robot }
458*7c3d14c8STreehugger Robot }
459