asan_thread.cc revision dcf98bf49d68533c7aebadbf6c4467afdd486c99
1//===-- asan_thread.cc ----------------------------------------------------===//
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// Thread-related code.
13//===----------------------------------------------------------------------===//
14#include "asan_allocator.h"
15#include "asan_interceptors.h"
16#include "asan_poisoning.h"
17#include "asan_stack.h"
18#include "asan_thread.h"
19#include "asan_mapping.h"
20#include "sanitizer_common/sanitizer_common.h"
21#include "sanitizer_common/sanitizer_placement_new.h"
22#include "lsan/lsan_common.h"
23
24namespace __asan {
25
26// AsanThreadContext implementation.
27
28void AsanThreadContext::OnCreated(void *arg) {
29  CreateThreadContextArgs *args = static_cast<CreateThreadContextArgs*>(arg);
30  if (args->stack) {
31    internal_memcpy(&stack, args->stack, sizeof(stack));
32  }
33  thread = args->thread;
34  thread->set_context(this);
35}
36
37void AsanThreadContext::OnFinished() {
38  // Drop the link to the AsanThread object.
39  thread = 0;
40}
41
42// MIPS requires aligned address
43static ALIGNED(16) char thread_registry_placeholder[sizeof(ThreadRegistry)];
44static ThreadRegistry *asan_thread_registry;
45
46static ThreadContextBase *GetAsanThreadContext(u32 tid) {
47  void *mem = MmapOrDie(sizeof(AsanThreadContext), "AsanThreadContext");
48  return new(mem) AsanThreadContext(tid);
49}
50
51ThreadRegistry &asanThreadRegistry() {
52  static bool initialized;
53  // Don't worry about thread_safety - this should be called when there is
54  // a single thread.
55  if (!initialized) {
56    // Never reuse ASan threads: we store pointer to AsanThreadContext
57    // in TSD and can't reliably tell when no more TSD destructors will
58    // be called. It would be wrong to reuse AsanThreadContext for another
59    // thread before all TSD destructors will be called for it.
60    asan_thread_registry = new(thread_registry_placeholder) ThreadRegistry(
61        GetAsanThreadContext, kMaxNumberOfThreads, kMaxNumberOfThreads);
62    initialized = true;
63  }
64  return *asan_thread_registry;
65}
66
67AsanThreadContext *GetThreadContextByTidLocked(u32 tid) {
68  return static_cast<AsanThreadContext *>(
69      asanThreadRegistry().GetThreadLocked(tid));
70}
71
72// AsanThread implementation.
73
74AsanThread *AsanThread::Create(thread_callback_t start_routine,
75                               void *arg) {
76  uptr PageSize = GetPageSizeCached();
77  uptr size = RoundUpTo(sizeof(AsanThread), PageSize);
78  AsanThread *thread = (AsanThread*)MmapOrDie(size, __FUNCTION__);
79  thread->start_routine_ = start_routine;
80  thread->arg_ = arg;
81  thread->context_ = 0;
82
83  return thread;
84}
85
86void AsanThread::TSDDtor(void *tsd) {
87  AsanThreadContext *context = (AsanThreadContext*)tsd;
88  if (flags()->verbosity >= 1)
89    Report("T%d TSDDtor\n", context->tid);
90  if (context->thread)
91    context->thread->Destroy();
92}
93
94void AsanThread::Destroy() {
95  if (flags()->verbosity >= 1) {
96    Report("T%d exited\n", tid());
97  }
98
99  asanThreadRegistry().FinishThread(tid());
100  FlushToDeadThreadStats(&stats_);
101  // We also clear the shadow on thread destruction because
102  // some code may still be executing in later TSD destructors
103  // and we don't want it to have any poisoned stack.
104  ClearShadowForThreadStackAndTLS();
105  DeleteFakeStack();
106  uptr size = RoundUpTo(sizeof(AsanThread), GetPageSizeCached());
107  UnmapOrDie(this, size);
108}
109
110// We want to create the FakeStack lazyly on the first use, but not eralier
111// than the stack size is known and the procedure has to be async-signal safe.
112FakeStack *AsanThread::AsyncSignalSafeLazyInitFakeStack() {
113  uptr stack_size = this->stack_size();
114  if (stack_size == 0)  // stack_size is not yet available, don't use FakeStack.
115    return 0;
116  uptr old_val = 0;
117  // fake_stack_ has 3 states:
118  // 0   -- not initialized
119  // 1   -- being initialized
120  // ptr -- initialized
121  // This CAS checks if the state was 0 and if so changes it to state 1,
122  // if that was successfull, it initilizes the pointer.
123  if (atomic_compare_exchange_strong(
124      reinterpret_cast<atomic_uintptr_t *>(&fake_stack_), &old_val, 1UL,
125      memory_order_relaxed))
126    return fake_stack_ =
127               FakeStack::Create(Log2(RoundUpToPowerOfTwo(stack_size)));
128  return 0;
129}
130
131void AsanThread::Init() {
132  SetThreadStackAndTls();
133  CHECK(AddrIsInMem(stack_bottom_));
134  CHECK(AddrIsInMem(stack_top_ - 1));
135  ClearShadowForThreadStackAndTLS();
136  if (flags()->verbosity >= 1) {
137    int local = 0;
138    Report("T%d: stack [%p,%p) size 0x%zx; local=%p\n",
139           tid(), (void*)stack_bottom_, (void*)stack_top_,
140           stack_top_ - stack_bottom_, &local);
141  }
142  fake_stack_ = 0;  // Will be initialized lazily if needed.
143  AsanPlatformThreadInit();
144}
145
146thread_return_t AsanThread::ThreadStart(uptr os_id) {
147  Init();
148  asanThreadRegistry().StartThread(tid(), os_id, 0);
149  if (flags()->use_sigaltstack) SetAlternateSignalStack();
150
151  if (!start_routine_) {
152    // start_routine_ == 0 if we're on the main thread or on one of the
153    // OS X libdispatch worker threads. But nobody is supposed to call
154    // ThreadStart() for the worker threads.
155    CHECK_EQ(tid(), 0);
156    return 0;
157  }
158
159  thread_return_t res = start_routine_(arg_);
160  malloc_storage().CommitBack();
161  if (flags()->use_sigaltstack) UnsetAlternateSignalStack();
162
163  this->Destroy();
164
165  return res;
166}
167
168void AsanThread::SetThreadStackAndTls() {
169  uptr stack_size = 0, tls_size = 0;
170  GetThreadStackAndTls(tid() == 0, &stack_bottom_, &stack_size, &tls_begin_,
171                       &tls_size);
172  stack_top_ = stack_bottom_ + stack_size;
173  tls_end_ = tls_begin_ + tls_size;
174
175  int local;
176  CHECK(AddrIsInStack((uptr)&local));
177}
178
179void AsanThread::ClearShadowForThreadStackAndTLS() {
180  PoisonShadow(stack_bottom_, stack_top_ - stack_bottom_, 0);
181  if (tls_begin_ != tls_end_)
182    PoisonShadow(tls_begin_, tls_end_ - tls_begin_, 0);
183}
184
185const char *AsanThread::GetFrameNameByAddr(uptr addr, uptr *offset,
186                                           uptr *frame_pc) {
187  uptr bottom = 0;
188  if (AddrIsInStack(addr)) {
189    bottom = stack_bottom();
190  } else if (has_fake_stack()) {
191    bottom = fake_stack()->AddrIsInFakeStack(addr);
192    CHECK(bottom);
193    *offset = addr - bottom;
194    *frame_pc = ((uptr*)bottom)[2];
195    return  (const char *)((uptr*)bottom)[1];
196  }
197  uptr aligned_addr = addr & ~(SANITIZER_WORDSIZE/8 - 1);  // align addr.
198  u8 *shadow_ptr = (u8*)MemToShadow(aligned_addr);
199  u8 *shadow_bottom = (u8*)MemToShadow(bottom);
200
201  while (shadow_ptr >= shadow_bottom &&
202         *shadow_ptr != kAsanStackLeftRedzoneMagic) {
203    shadow_ptr--;
204  }
205
206  while (shadow_ptr >= shadow_bottom &&
207         *shadow_ptr == kAsanStackLeftRedzoneMagic) {
208    shadow_ptr--;
209  }
210
211  if (shadow_ptr < shadow_bottom) {
212    *offset = 0;
213    return "UNKNOWN";
214  }
215
216  uptr* ptr = (uptr*)SHADOW_TO_MEM((uptr)(shadow_ptr + 1));
217  CHECK(ptr[0] == kCurrentStackFrameMagic);
218  *offset = addr - (uptr)ptr;
219  *frame_pc = ptr[2];
220  return (const char*)ptr[1];
221}
222
223static bool ThreadStackContainsAddress(ThreadContextBase *tctx_base,
224                                       void *addr) {
225  AsanThreadContext *tctx = static_cast<AsanThreadContext*>(tctx_base);
226  AsanThread *t = tctx->thread;
227  if (!t) return false;
228  if (t->AddrIsInStack((uptr)addr)) return true;
229  if (t->fake_stack() && t->fake_stack()->AddrIsInFakeStack((uptr)addr))
230    return true;
231  return false;
232}
233
234AsanThread *GetCurrentThread() {
235  AsanThreadContext *context =
236      reinterpret_cast<AsanThreadContext *>(AsanTSDGet());
237  if (!context) {
238    if (SANITIZER_ANDROID) {
239      // On Android, libc constructor is called _after_ asan_init, and cleans up
240      // TSD. Try to figure out if this is still the main thread by the stack
241      // address. We are not entirely sure that we have correct main thread
242      // limits, so only do this magic on Android, and only if the found thread
243      // is the main thread.
244      AsanThreadContext *tctx = GetThreadContextByTidLocked(0);
245      if (ThreadStackContainsAddress(tctx, &context)) {
246        SetCurrentThread(tctx->thread);
247        return tctx->thread;
248      }
249    }
250    return 0;
251  }
252  return context->thread;
253}
254
255void SetCurrentThread(AsanThread *t) {
256  CHECK(t->context());
257  if (flags()->verbosity >= 2) {
258    Report("SetCurrentThread: %p for thread %p\n",
259           t->context(), (void*)GetThreadSelf());
260  }
261  // Make sure we do not reset the current AsanThread.
262  CHECK_EQ(0, AsanTSDGet());
263  AsanTSDSet(t->context());
264  CHECK_EQ(t->context(), AsanTSDGet());
265}
266
267u32 GetCurrentTidOrInvalid() {
268  AsanThread *t = GetCurrentThread();
269  return t ? t->tid() : kInvalidTid;
270}
271
272AsanThread *FindThreadByStackAddress(uptr addr) {
273  asanThreadRegistry().CheckLocked();
274  AsanThreadContext *tctx = static_cast<AsanThreadContext *>(
275      asanThreadRegistry().FindThreadContextLocked(ThreadStackContainsAddress,
276                                                   (void *)addr));
277  return tctx ? tctx->thread : 0;
278}
279
280void EnsureMainThreadIDIsCorrect() {
281  AsanThreadContext *context =
282      reinterpret_cast<AsanThreadContext *>(AsanTSDGet());
283  if (context && (context->tid == 0))
284    context->os_id = GetTid();
285}
286}  // namespace __asan
287
288// --- Implementation of LSan-specific functions --- {{{1
289namespace __lsan {
290bool GetThreadRangesLocked(uptr os_id, uptr *stack_begin, uptr *stack_end,
291                           uptr *tls_begin, uptr *tls_end,
292                           uptr *cache_begin, uptr *cache_end) {
293  __asan::AsanThreadContext *context = static_cast<__asan::AsanThreadContext *>(
294      __asan::asanThreadRegistry().FindThreadContextByOsIDLocked(os_id));
295  if (!context) return false;
296  __asan::AsanThread *t = context->thread;
297  if (!t) return false;
298  *stack_begin = t->stack_bottom();
299  *stack_end = t->stack_top();
300  *tls_begin = t->tls_begin();
301  *tls_end = t->tls_end();
302  // ASan doesn't keep allocator caches in TLS, so these are unused.
303  *cache_begin = 0;
304  *cache_end = 0;
305  return true;
306}
307
308void LockThreadRegistry() {
309  __asan::asanThreadRegistry().Lock();
310}
311
312void UnlockThreadRegistry() {
313  __asan::asanThreadRegistry().Unlock();
314}
315
316void EnsureMainThreadIDIsCorrect() {
317  __asan::EnsureMainThreadIDIsCorrect();
318}
319}  // namespace __lsan
320