blob: 328ac2fcd3982612a8f30cbc526d37496dc662ab [file] [log] [blame]
Alexey Samsonov485d3dc2012-06-04 13:50:10 +00001//===-- asan_thread.cc ----------------------------------------------------===//
Kostya Serebryany019b76f2011-11-30 01:07:02 +00002//
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"
Alexey Samsonova88c60b2013-03-28 15:42:43 +000016#include "asan_poisoning.h"
Alexey Samsonov2d3a67b2012-01-17 06:35:31 +000017#include "asan_stack.h"
Kostya Serebryany019b76f2011-11-30 01:07:02 +000018#include "asan_thread.h"
Kostya Serebryany019b76f2011-11-30 01:07:02 +000019#include "asan_mapping.h"
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000020#include "sanitizer_common/sanitizer_common.h"
Alexey Samsonov54afba82013-03-21 11:23:41 +000021#include "sanitizer_common/sanitizer_placement_new.h"
Kostya Serebryany96288392013-10-18 14:50:44 +000022#include "sanitizer_common/sanitizer_stackdepot.h"
Sergey Matveev65dd62a2013-05-21 13:40:13 +000023#include "lsan/lsan_common.h"
Kostya Serebryany019b76f2011-11-30 01:07:02 +000024
Kostya Serebryany019b76f2011-11-30 01:07:02 +000025namespace __asan {
26
Alexey Samsonov54afba82013-03-21 11:23:41 +000027// AsanThreadContext implementation.
Kostya Serebryany019b76f2011-11-30 01:07:02 +000028
Alexey Samsonov54afba82013-03-21 11:23:41 +000029void AsanThreadContext::OnCreated(void *arg) {
30 CreateThreadContextArgs *args = static_cast<CreateThreadContextArgs*>(arg);
Kostya Serebryany96288392013-10-18 14:50:44 +000031 if (args->stack)
32 stack_id = StackDepotPut(args->stack->trace, args->stack->size);
Alexey Samsonov54afba82013-03-21 11:23:41 +000033 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
Kostya Serebryanyc1aa0e82013-06-03 14:49:25 +000042// MIPS requires aligned address
Timur Iskhodzhanovbaf90cc2013-06-04 08:25:17 +000043static ALIGNED(16) char thread_registry_placeholder[sizeof(ThreadRegistry)];
Alexey Samsonov54afba82013-03-21 11:23:41 +000044static ThreadRegistry *asan_thread_registry;
45
Kostya Serebryanyf11e4852013-10-18 15:07:07 +000046static BlockingMutex mu_for_thread_context(LINKER_INITIALIZED);
47static LowLevelAllocator allocator_for_thread_context;
48
Alexey Samsonov54afba82013-03-21 11:23:41 +000049static ThreadContextBase *GetAsanThreadContext(u32 tid) {
Kostya Serebryanyf11e4852013-10-18 15:07:07 +000050 BlockingMutexLock lock(&mu_for_thread_context);
Peter Collingbourne50cb32e2013-10-24 06:23:39 +000051 return new(allocator_for_thread_context) AsanThreadContext(tid);
Alexey Samsonov54afba82013-03-21 11:23:41 +000052}
53
54ThreadRegistry &asanThreadRegistry() {
55 static bool initialized;
56 // Don't worry about thread_safety - this should be called when there is
57 // a single thread.
58 if (!initialized) {
59 // Never reuse ASan threads: we store pointer to AsanThreadContext
60 // in TSD and can't reliably tell when no more TSD destructors will
61 // be called. It would be wrong to reuse AsanThreadContext for another
62 // thread before all TSD destructors will be called for it.
63 asan_thread_registry = new(thread_registry_placeholder) ThreadRegistry(
64 GetAsanThreadContext, kMaxNumberOfThreads, kMaxNumberOfThreads);
65 initialized = true;
66 }
67 return *asan_thread_registry;
68}
69
70AsanThreadContext *GetThreadContextByTidLocked(u32 tid) {
71 return static_cast<AsanThreadContext *>(
72 asanThreadRegistry().GetThreadLocked(tid));
73}
74
75// AsanThread implementation.
76
77AsanThread *AsanThread::Create(thread_callback_t start_routine,
78 void *arg) {
Kostya Serebryanyf22c6972012-11-23 15:38:49 +000079 uptr PageSize = GetPageSizeCached();
80 uptr size = RoundUpTo(sizeof(AsanThread), PageSize);
Alexey Samsonov40d5b772012-06-06 16:15:07 +000081 AsanThread *thread = (AsanThread*)MmapOrDie(size, __FUNCTION__);
Alexey Samsonov2d3a67b2012-01-17 06:35:31 +000082 thread->start_routine_ = start_routine;
83 thread->arg_ = arg;
Alexey Samsonov54afba82013-03-21 11:23:41 +000084 thread->context_ = 0;
Alexey Samsonov2d3a67b2012-01-17 06:35:31 +000085
86 return thread;
Kostya Serebryany019b76f2011-11-30 01:07:02 +000087}
88
Alexey Samsonov54afba82013-03-21 11:23:41 +000089void AsanThread::TSDDtor(void *tsd) {
90 AsanThreadContext *context = (AsanThreadContext*)tsd;
Dmitry Vyukov52ca74e2013-10-15 13:28:51 +000091 if (common_flags()->verbosity >= 1)
Alexey Samsonov54afba82013-03-21 11:23:41 +000092 Report("T%d TSDDtor\n", context->tid);
93 if (context->thread)
94 context->thread->Destroy();
Kostya Serebryanyb5eb5a72012-02-07 00:27:15 +000095}
96
Kostya Serebryany3f4b9bb2012-01-06 19:44:11 +000097void AsanThread::Destroy() {
Dmitry Vyukov52ca74e2013-10-15 13:28:51 +000098 if (common_flags()->verbosity >= 1) {
Kostya Serebryanyb5eb5a72012-02-07 00:27:15 +000099 Report("T%d exited\n", tid());
100 }
101
Kostya Serebryany04a17672013-11-13 13:27:44 +0000102 malloc_storage().CommitBack();
103 if (flags()->use_sigaltstack) UnsetAlternateSignalStack();
Alexey Samsonov54afba82013-03-21 11:23:41 +0000104 asanThreadRegistry().FinishThread(tid());
Alexey Samsonov4b168852013-09-02 08:39:07 +0000105 FlushToDeadThreadStats(&stats_);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000106 // We also clear the shadow on thread destruction because
107 // some code may still be executing in later TSD destructors
108 // and we don't want it to have any poisoned stack.
Sergey Matveev09886cd2013-05-29 13:09:44 +0000109 ClearShadowForThreadStackAndTLS();
Kostya Serebryany6a068a72013-06-26 12:16:05 +0000110 DeleteFakeStack();
Kostya Serebryanyf22c6972012-11-23 15:38:49 +0000111 uptr size = RoundUpTo(sizeof(AsanThread), GetPageSizeCached());
Alexey Samsonov40d5b772012-06-06 16:15:07 +0000112 UnmapOrDie(this, size);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000113}
114
Kostya Serebryany628cda72013-09-12 08:34:50 +0000115// We want to create the FakeStack lazyly on the first use, but not eralier
116// than the stack size is known and the procedure has to be async-signal safe.
117FakeStack *AsanThread::AsyncSignalSafeLazyInitFakeStack() {
118 uptr stack_size = this->stack_size();
119 if (stack_size == 0) // stack_size is not yet available, don't use FakeStack.
120 return 0;
121 uptr old_val = 0;
122 // fake_stack_ has 3 states:
123 // 0 -- not initialized
124 // 1 -- being initialized
125 // ptr -- initialized
126 // This CAS checks if the state was 0 and if so changes it to state 1,
127 // if that was successfull, it initilizes the pointer.
128 if (atomic_compare_exchange_strong(
129 reinterpret_cast<atomic_uintptr_t *>(&fake_stack_), &old_val, 1UL,
Kostya Serebryany43c44932013-09-13 06:32:26 +0000130 memory_order_relaxed)) {
Kostya Serebryanyc3d43ca2013-09-18 10:35:12 +0000131 uptr stack_size_log = Log2(RoundUpToPowerOfTwo(stack_size));
Kostya Serebryany3c42ce12013-09-27 11:37:23 +0000132 if (flags()->uar_stack_size_log)
133 stack_size_log = static_cast<uptr>(flags()->uar_stack_size_log);
Kostya Serebryanyc3d43ca2013-09-18 10:35:12 +0000134 fake_stack_ = FakeStack::Create(stack_size_log);
Kostya Serebryany43c44932013-09-13 06:32:26 +0000135 SetTLSFakeStack(fake_stack_);
136 return fake_stack_;
137 }
Kostya Serebryany628cda72013-09-12 08:34:50 +0000138 return 0;
139}
140
Kostya Serebryany6bb2f1d2011-12-16 19:13:35 +0000141void AsanThread::Init() {
Sergey Matveev09886cd2013-05-29 13:09:44 +0000142 SetThreadStackAndTls();
Alexey Samsonov2d3a67b2012-01-17 06:35:31 +0000143 CHECK(AddrIsInMem(stack_bottom_));
Kostya Serebryany63c36bb2013-01-18 11:30:36 +0000144 CHECK(AddrIsInMem(stack_top_ - 1));
Sergey Matveev09886cd2013-05-29 13:09:44 +0000145 ClearShadowForThreadStackAndTLS();
Dmitry Vyukov52ca74e2013-10-15 13:28:51 +0000146 if (common_flags()->verbosity >= 1) {
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000147 int local = 0;
Evgeniy Stepanov823085a2012-03-21 11:32:46 +0000148 Report("T%d: stack [%p,%p) size 0x%zx; local=%p\n",
Alexey Samsonov461820a2012-06-06 10:46:00 +0000149 tid(), (void*)stack_bottom_, (void*)stack_top_,
Kostya Serebryany5be458c2012-01-09 19:18:27 +0000150 stack_top_ - stack_bottom_, &local);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000151 }
Kostya Serebryany6a068a72013-06-26 12:16:05 +0000152 fake_stack_ = 0; // Will be initialized lazily if needed.
Alexander Potapenko51e64882012-07-23 14:07:58 +0000153 AsanPlatformThreadInit();
Kostya Serebryany6bb2f1d2011-12-16 19:13:35 +0000154}
155
Alexey Samsonov54afba82013-03-21 11:23:41 +0000156thread_return_t AsanThread::ThreadStart(uptr os_id) {
Kostya Serebryany6bb2f1d2011-12-16 19:13:35 +0000157 Init();
Alexey Samsonov54afba82013-03-21 11:23:41 +0000158 asanThreadRegistry().StartThread(tid(), os_id, 0);
Alexey Samsonov34efb8e2012-07-09 14:36:04 +0000159 if (flags()->use_sigaltstack) SetAlternateSignalStack();
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000160
161 if (!start_routine_) {
Kostya Serebryany8d032042012-05-31 14:35:53 +0000162 // start_routine_ == 0 if we're on the main thread or on one of the
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000163 // OS X libdispatch worker threads. But nobody is supposed to call
164 // ThreadStart() for the worker threads.
Kostya Serebryany5b4267f2013-04-05 14:40:25 +0000165 CHECK_EQ(tid(), 0);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000166 return 0;
167 }
168
Timur Iskhodzhanov0f9c9a52012-02-24 15:28:43 +0000169 thread_return_t res = start_routine_(arg_);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000170
Sergey Matveevda9f5e72013-10-14 12:01:05 +0000171 // On POSIX systems we defer this to the TSD destructor. LSan will consider
172 // the thread's memory as non-live from the moment we call Destroy(), even
173 // though that memory might contain pointers to heap objects which will be
174 // cleaned up by a user-defined TSD destructor. Thus, calling Destroy() before
175 // the TSD destructors have run might cause false positives in LSan.
176 if (!SANITIZER_POSIX)
177 this->Destroy();
Kostya Serebryany332923b2012-01-11 02:03:16 +0000178
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000179 return res;
180}
181
Sergey Matveev09886cd2013-05-29 13:09:44 +0000182void AsanThread::SetThreadStackAndTls() {
Kostya Serebryanyf8bbdfa2013-09-19 14:59:52 +0000183 uptr tls_size = 0;
184 GetThreadStackAndTls(tid() == 0, &stack_bottom_, &stack_size_, &tls_begin_,
Sergey Matveev09886cd2013-05-29 13:09:44 +0000185 &tls_size);
Kostya Serebryanyf8bbdfa2013-09-19 14:59:52 +0000186 stack_top_ = stack_bottom_ + stack_size_;
Sergey Matveev09886cd2013-05-29 13:09:44 +0000187 tls_end_ = tls_begin_ + tls_size;
188
Alexey Samsonov4b1f1032012-06-07 07:13:46 +0000189 int local;
190 CHECK(AddrIsInStack((uptr)&local));
191}
192
Sergey Matveev09886cd2013-05-29 13:09:44 +0000193void AsanThread::ClearShadowForThreadStackAndTLS() {
Alexey Samsonov2d3a67b2012-01-17 06:35:31 +0000194 PoisonShadow(stack_bottom_, stack_top_ - stack_bottom_, 0);
Sergey Matveev09886cd2013-05-29 13:09:44 +0000195 if (tls_begin_ != tls_end_)
196 PoisonShadow(tls_begin_, tls_end_ - tls_begin_, 0);
Alexey Samsonov2d3a67b2012-01-17 06:35:31 +0000197}
198
Kostya Serebryany667a34a2013-03-22 10:36:24 +0000199const char *AsanThread::GetFrameNameByAddr(uptr addr, uptr *offset,
200 uptr *frame_pc) {
Kostya Serebryany8d032042012-05-31 14:35:53 +0000201 uptr bottom = 0;
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000202 if (AddrIsInStack(addr)) {
203 bottom = stack_bottom();
Kostya Serebryany736bd082013-09-12 08:43:44 +0000204 } else if (has_fake_stack()) {
Kostya Serebryany6a068a72013-06-26 12:16:05 +0000205 bottom = fake_stack()->AddrIsInFakeStack(addr);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000206 CHECK(bottom);
Alexander Potapenkobcc00a42012-11-15 15:24:42 +0000207 *offset = addr - bottom;
Kostya Serebryany667a34a2013-03-22 10:36:24 +0000208 *frame_pc = ((uptr*)bottom)[2];
Alexander Potapenkobcc00a42012-11-15 15:24:42 +0000209 return (const char *)((uptr*)bottom)[1];
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000210 }
Kostya Serebryany734f1eb2012-11-21 12:38:58 +0000211 uptr aligned_addr = addr & ~(SANITIZER_WORDSIZE/8 - 1); // align addr.
Kostya Serebryany1d35d152012-05-31 15:02:07 +0000212 u8 *shadow_ptr = (u8*)MemToShadow(aligned_addr);
213 u8 *shadow_bottom = (u8*)MemToShadow(bottom);
Evgeniy Stepanovd989be12012-05-12 12:33:10 +0000214
215 while (shadow_ptr >= shadow_bottom &&
Alexander Potapenkobcc00a42012-11-15 15:24:42 +0000216 *shadow_ptr != kAsanStackLeftRedzoneMagic) {
Evgeniy Stepanovd989be12012-05-12 12:33:10 +0000217 shadow_ptr--;
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000218 }
Evgeniy Stepanovd989be12012-05-12 12:33:10 +0000219
220 while (shadow_ptr >= shadow_bottom &&
Alexander Potapenkobcc00a42012-11-15 15:24:42 +0000221 *shadow_ptr == kAsanStackLeftRedzoneMagic) {
Evgeniy Stepanovd989be12012-05-12 12:33:10 +0000222 shadow_ptr--;
223 }
224
225 if (shadow_ptr < shadow_bottom) {
Alexander Potapenkobcc00a42012-11-15 15:24:42 +0000226 *offset = 0;
227 return "UNKNOWN";
Evgeniy Stepanovd989be12012-05-12 12:33:10 +0000228 }
229
Kostya Serebryany8d032042012-05-31 14:35:53 +0000230 uptr* ptr = (uptr*)SHADOW_TO_MEM((uptr)(shadow_ptr + 1));
Alexander Potapenkobcc00a42012-11-15 15:24:42 +0000231 CHECK(ptr[0] == kCurrentStackFrameMagic);
Kostya Serebryany8d032042012-05-31 14:35:53 +0000232 *offset = addr - (uptr)ptr;
Kostya Serebryany667a34a2013-03-22 10:36:24 +0000233 *frame_pc = ptr[2];
Evgeniy Stepanovd989be12012-05-12 12:33:10 +0000234 return (const char*)ptr[1];
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000235}
236
Alexey Samsonov54afba82013-03-21 11:23:41 +0000237static bool ThreadStackContainsAddress(ThreadContextBase *tctx_base,
238 void *addr) {
239 AsanThreadContext *tctx = static_cast<AsanThreadContext*>(tctx_base);
240 AsanThread *t = tctx->thread;
Kostya Serebryany6a068a72013-06-26 12:16:05 +0000241 if (!t) return false;
242 if (t->AddrIsInStack((uptr)addr)) return true;
Kostya Serebryany44441cc2013-09-12 08:47:00 +0000243 if (t->has_fake_stack() && t->fake_stack()->AddrIsInFakeStack((uptr)addr))
Kostya Serebryany6a068a72013-06-26 12:16:05 +0000244 return true;
245 return false;
Alexey Samsonov54afba82013-03-21 11:23:41 +0000246}
247
Alexey Samsonovcf025cb2013-03-20 09:23:28 +0000248AsanThread *GetCurrentThread() {
Sergey Matveevbdeff952013-07-08 12:57:24 +0000249 AsanThreadContext *context =
250 reinterpret_cast<AsanThreadContext *>(AsanTSDGet());
Alexey Samsonov54afba82013-03-21 11:23:41 +0000251 if (!context) {
252 if (SANITIZER_ANDROID) {
253 // On Android, libc constructor is called _after_ asan_init, and cleans up
254 // TSD. Try to figure out if this is still the main thread by the stack
255 // address. We are not entirely sure that we have correct main thread
Dmitry Vyukova7e42b52013-03-22 07:29:59 +0000256 // limits, so only do this magic on Android, and only if the found thread
257 // is the main thread.
Alexey Samsonov54afba82013-03-21 11:23:41 +0000258 AsanThreadContext *tctx = GetThreadContextByTidLocked(0);
259 if (ThreadStackContainsAddress(tctx, &context)) {
260 SetCurrentThread(tctx->thread);
261 return tctx->thread;
262 }
Alexey Samsonovcf025cb2013-03-20 09:23:28 +0000263 }
Alexey Samsonovcf025cb2013-03-20 09:23:28 +0000264 return 0;
265 }
Alexey Samsonov54afba82013-03-21 11:23:41 +0000266 return context->thread;
Alexey Samsonovcf025cb2013-03-20 09:23:28 +0000267}
268
269void SetCurrentThread(AsanThread *t) {
Alexey Samsonov54afba82013-03-21 11:23:41 +0000270 CHECK(t->context());
Dmitry Vyukov52ca74e2013-10-15 13:28:51 +0000271 if (common_flags()->verbosity >= 2) {
Alexey Samsonovcf025cb2013-03-20 09:23:28 +0000272 Report("SetCurrentThread: %p for thread %p\n",
Alexey Samsonov54afba82013-03-21 11:23:41 +0000273 t->context(), (void*)GetThreadSelf());
Alexey Samsonovcf025cb2013-03-20 09:23:28 +0000274 }
275 // Make sure we do not reset the current AsanThread.
Alexey Samsonov54afba82013-03-21 11:23:41 +0000276 CHECK_EQ(0, AsanTSDGet());
277 AsanTSDSet(t->context());
278 CHECK_EQ(t->context(), AsanTSDGet());
Alexey Samsonovcf025cb2013-03-20 09:23:28 +0000279}
280
281u32 GetCurrentTidOrInvalid() {
282 AsanThread *t = GetCurrentThread();
283 return t ? t->tid() : kInvalidTid;
284}
285
Alexey Samsonov54afba82013-03-21 11:23:41 +0000286AsanThread *FindThreadByStackAddress(uptr addr) {
287 asanThreadRegistry().CheckLocked();
288 AsanThreadContext *tctx = static_cast<AsanThreadContext *>(
289 asanThreadRegistry().FindThreadContextLocked(ThreadStackContainsAddress,
290 (void *)addr));
291 return tctx ? tctx->thread : 0;
292}
Sergey Matveevbdeff952013-07-08 12:57:24 +0000293
294void EnsureMainThreadIDIsCorrect() {
295 AsanThreadContext *context =
296 reinterpret_cast<AsanThreadContext *>(AsanTSDGet());
297 if (context && (context->tid == 0))
298 context->os_id = GetTid();
299}
Sergey Matveev43d90cb2013-10-14 14:04:50 +0000300
301__asan::AsanThread *GetAsanThreadByOsIDLocked(uptr os_id) {
302 __asan::AsanThreadContext *context = static_cast<__asan::AsanThreadContext *>(
303 __asan::asanThreadRegistry().FindThreadContextByOsIDLocked(os_id));
304 if (!context) return 0;
305 return context->thread;
306}
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000307} // namespace __asan
Sergey Matveev65dd62a2013-05-21 13:40:13 +0000308
309// --- Implementation of LSan-specific functions --- {{{1
310namespace __lsan {
311bool GetThreadRangesLocked(uptr os_id, uptr *stack_begin, uptr *stack_end,
312 uptr *tls_begin, uptr *tls_end,
313 uptr *cache_begin, uptr *cache_end) {
Sergey Matveev43d90cb2013-10-14 14:04:50 +0000314 __asan::AsanThread *t = __asan::GetAsanThreadByOsIDLocked(os_id);
Sergey Matveev09886cd2013-05-29 13:09:44 +0000315 if (!t) return false;
316 *stack_begin = t->stack_bottom();
317 *stack_end = t->stack_top();
318 *tls_begin = t->tls_begin();
319 *tls_end = t->tls_end();
320 // ASan doesn't keep allocator caches in TLS, so these are unused.
321 *cache_begin = 0;
322 *cache_end = 0;
323 return true;
Sergey Matveev65dd62a2013-05-21 13:40:13 +0000324}
325
Sergey Matveev43d90cb2013-10-14 14:04:50 +0000326void ForEachExtraStackRange(uptr os_id, RangeIteratorCallback callback,
327 void *arg) {
328 __asan::AsanThread *t = __asan::GetAsanThreadByOsIDLocked(os_id);
329 if (t && t->has_fake_stack())
330 t->fake_stack()->ForEachFakeFrame(callback, arg);
331}
332
Sergey Matveev65dd62a2013-05-21 13:40:13 +0000333void LockThreadRegistry() {
334 __asan::asanThreadRegistry().Lock();
335}
336
337void UnlockThreadRegistry() {
338 __asan::asanThreadRegistry().Unlock();
339}
Sergey Matveevbdeff952013-07-08 12:57:24 +0000340
341void EnsureMainThreadIDIsCorrect() {
342 __asan::EnsureMainThreadIDIsCorrect();
343}
Sergey Matveev65dd62a2013-05-21 13:40:13 +0000344} // namespace __lsan