blob: 1d45573b8a1d7c0eeb4561353b66a85cc7b1969e [file] [log] [blame]
Alexey Samsonove5f58952012-06-04 13:50:10 +00001//===-- asan_thread.cc ----------------------------------------------------===//
Kostya Serebryany1e172b42011-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 Samsonov7e843492013-03-28 15:42:43 +000016#include "asan_poisoning.h"
Alexey Samsonov55cdfc62012-01-17 06:35:31 +000017#include "asan_stack.h"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000018#include "asan_thread.h"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000019#include "asan_mapping.h"
Alexey Samsonove5931fd2012-06-07 07:13:46 +000020#include "sanitizer_common/sanitizer_common.h"
Alexey Samsonovdef1be92013-03-21 11:23:41 +000021#include "sanitizer_common/sanitizer_placement_new.h"
Kostya Serebryany6d958692013-10-18 14:50:44 +000022#include "sanitizer_common/sanitizer_stackdepot.h"
Stephen Hines2d1fdb22014-05-28 23:58:16 -070023#include "sanitizer_common/sanitizer_tls_get_addr.h"
Sergey Matveevf1ac1a42013-05-21 13:40:13 +000024#include "lsan/lsan_common.h"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000025
Kostya Serebryany1e172b42011-11-30 01:07:02 +000026namespace __asan {
27
Alexey Samsonovdef1be92013-03-21 11:23:41 +000028// AsanThreadContext implementation.
Kostya Serebryany1e172b42011-11-30 01:07:02 +000029
Alexey Samsonovdef1be92013-03-21 11:23:41 +000030void AsanThreadContext::OnCreated(void *arg) {
31 CreateThreadContextArgs *args = static_cast<CreateThreadContextArgs*>(arg);
Kostya Serebryany6d958692013-10-18 14:50:44 +000032 if (args->stack)
33 stack_id = StackDepotPut(args->stack->trace, args->stack->size);
Alexey Samsonovdef1be92013-03-21 11:23:41 +000034 thread = args->thread;
35 thread->set_context(this);
36}
37
38void AsanThreadContext::OnFinished() {
39 // Drop the link to the AsanThread object.
40 thread = 0;
41}
42
Kostya Serebryany40527a52013-06-03 14:49:25 +000043// MIPS requires aligned address
Timur Iskhodzhanov7c9ffde2013-06-04 08:25:17 +000044static ALIGNED(16) char thread_registry_placeholder[sizeof(ThreadRegistry)];
Alexey Samsonovdef1be92013-03-21 11:23:41 +000045static ThreadRegistry *asan_thread_registry;
46
Kostya Serebryany1fe68b82013-10-18 15:07:07 +000047static BlockingMutex mu_for_thread_context(LINKER_INITIALIZED);
48static LowLevelAllocator allocator_for_thread_context;
49
Alexey Samsonovdef1be92013-03-21 11:23:41 +000050static ThreadContextBase *GetAsanThreadContext(u32 tid) {
Kostya Serebryany1fe68b82013-10-18 15:07:07 +000051 BlockingMutexLock lock(&mu_for_thread_context);
Peter Collingbourne53177242013-10-24 06:23:39 +000052 return new(allocator_for_thread_context) AsanThreadContext(tid);
Alexey Samsonovdef1be92013-03-21 11:23:41 +000053}
54
55ThreadRegistry &asanThreadRegistry() {
56 static bool initialized;
57 // Don't worry about thread_safety - this should be called when there is
58 // a single thread.
59 if (!initialized) {
60 // Never reuse ASan threads: we store pointer to AsanThreadContext
61 // in TSD and can't reliably tell when no more TSD destructors will
62 // be called. It would be wrong to reuse AsanThreadContext for another
63 // thread before all TSD destructors will be called for it.
64 asan_thread_registry = new(thread_registry_placeholder) ThreadRegistry(
65 GetAsanThreadContext, kMaxNumberOfThreads, kMaxNumberOfThreads);
66 initialized = true;
67 }
68 return *asan_thread_registry;
69}
70
71AsanThreadContext *GetThreadContextByTidLocked(u32 tid) {
72 return static_cast<AsanThreadContext *>(
73 asanThreadRegistry().GetThreadLocked(tid));
74}
75
76// AsanThread implementation.
77
78AsanThread *AsanThread::Create(thread_callback_t start_routine,
79 void *arg) {
Kostya Serebryanyf67ec2b2012-11-23 15:38:49 +000080 uptr PageSize = GetPageSizeCached();
81 uptr size = RoundUpTo(sizeof(AsanThread), PageSize);
Stephen Hines2d1fdb22014-05-28 23:58:16 -070082 AsanThread *thread = (AsanThread*)MmapOrDie(size, __func__);
Alexey Samsonov55cdfc62012-01-17 06:35:31 +000083 thread->start_routine_ = start_routine;
84 thread->arg_ = arg;
Alexey Samsonov55cdfc62012-01-17 06:35:31 +000085
86 return thread;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000087}
88
Alexey Samsonovdef1be92013-03-21 11:23:41 +000089void AsanThread::TSDDtor(void *tsd) {
90 AsanThreadContext *context = (AsanThreadContext*)tsd;
Stephen Hines2d1fdb22014-05-28 23:58:16 -070091 VReport(1, "T%d TSDDtor\n", context->tid);
Alexey Samsonovdef1be92013-03-21 11:23:41 +000092 if (context->thread)
93 context->thread->Destroy();
Kostya Serebryanyf58f9982012-02-07 00:27:15 +000094}
95
Kostya Serebryanya6b52262012-01-06 19:44:11 +000096void AsanThread::Destroy() {
Stephen Hines2d1fdb22014-05-28 23:58:16 -070097 int tid = this->tid();
98 VReport(1, "T%d exited\n", tid);
Kostya Serebryanyf58f9982012-02-07 00:27:15 +000099
Kostya Serebryanyae914e22013-11-13 13:27:44 +0000100 malloc_storage().CommitBack();
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700101 if (common_flags()->use_sigaltstack) UnsetAlternateSignalStack();
102 asanThreadRegistry().FinishThread(tid);
Alexey Samsonov717ece52013-09-02 08:39:07 +0000103 FlushToDeadThreadStats(&stats_);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000104 // We also clear the shadow on thread destruction because
105 // some code may still be executing in later TSD destructors
106 // and we don't want it to have any poisoned stack.
Sergey Matveev12d01ba2013-05-29 13:09:44 +0000107 ClearShadowForThreadStackAndTLS();
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700108 DeleteFakeStack(tid);
Kostya Serebryanyf67ec2b2012-11-23 15:38:49 +0000109 uptr size = RoundUpTo(sizeof(AsanThread), GetPageSizeCached());
Alexey Samsonova25b3462012-06-06 16:15:07 +0000110 UnmapOrDie(this, size);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700111 DTLS_Destroy();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000112}
113
Kostya Serebryanyc98fc1f2013-09-12 08:34:50 +0000114// We want to create the FakeStack lazyly on the first use, but not eralier
115// than the stack size is known and the procedure has to be async-signal safe.
116FakeStack *AsanThread::AsyncSignalSafeLazyInitFakeStack() {
117 uptr stack_size = this->stack_size();
118 if (stack_size == 0) // stack_size is not yet available, don't use FakeStack.
119 return 0;
120 uptr old_val = 0;
121 // fake_stack_ has 3 states:
122 // 0 -- not initialized
123 // 1 -- being initialized
124 // ptr -- initialized
125 // This CAS checks if the state was 0 and if so changes it to state 1,
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700126 // if that was successful, it initializes the pointer.
Kostya Serebryanyc98fc1f2013-09-12 08:34:50 +0000127 if (atomic_compare_exchange_strong(
128 reinterpret_cast<atomic_uintptr_t *>(&fake_stack_), &old_val, 1UL,
Kostya Serebryany9433af32013-09-13 06:32:26 +0000129 memory_order_relaxed)) {
Kostya Serebryany230e52f2013-09-18 10:35:12 +0000130 uptr stack_size_log = Log2(RoundUpToPowerOfTwo(stack_size));
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700131 CHECK_LE(flags()->min_uar_stack_size_log, flags()->max_uar_stack_size_log);
132 stack_size_log =
133 Min(stack_size_log, static_cast<uptr>(flags()->max_uar_stack_size_log));
134 stack_size_log =
135 Max(stack_size_log, static_cast<uptr>(flags()->min_uar_stack_size_log));
Kostya Serebryany230e52f2013-09-18 10:35:12 +0000136 fake_stack_ = FakeStack::Create(stack_size_log);
Kostya Serebryany9433af32013-09-13 06:32:26 +0000137 SetTLSFakeStack(fake_stack_);
138 return fake_stack_;
139 }
Kostya Serebryanyc98fc1f2013-09-12 08:34:50 +0000140 return 0;
141}
142
Kostya Serebryany69eca732011-12-16 19:13:35 +0000143void AsanThread::Init() {
Sergey Matveev12d01ba2013-05-29 13:09:44 +0000144 SetThreadStackAndTls();
Alexey Samsonov55cdfc62012-01-17 06:35:31 +0000145 CHECK(AddrIsInMem(stack_bottom_));
Kostya Serebryany541cfb12013-01-18 11:30:36 +0000146 CHECK(AddrIsInMem(stack_top_ - 1));
Sergey Matveev12d01ba2013-05-29 13:09:44 +0000147 ClearShadowForThreadStackAndTLS();
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700148 int local = 0;
149 VReport(1, "T%d: stack [%p,%p) size 0x%zx; local=%p\n", tid(),
150 (void *)stack_bottom_, (void *)stack_top_, stack_top_ - stack_bottom_,
151 &local);
Kostya Serebryany7a0bba42013-06-26 12:16:05 +0000152 fake_stack_ = 0; // Will be initialized lazily if needed.
Alexander Potapenko75b19eb2012-07-23 14:07:58 +0000153 AsanPlatformThreadInit();
Kostya Serebryany69eca732011-12-16 19:13:35 +0000154}
155
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000156thread_return_t AsanThread::ThreadStart(uptr os_id) {
Kostya Serebryany69eca732011-12-16 19:13:35 +0000157 Init();
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000158 asanThreadRegistry().StartThread(tid(), os_id, 0);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700159 if (common_flags()->use_sigaltstack) SetAlternateSignalStack();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000160
161 if (!start_routine_) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000162 // start_routine_ == 0 if we're on the main thread or on one of the
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000163 // OS X libdispatch worker threads. But nobody is supposed to call
164 // ThreadStart() for the worker threads.
Kostya Serebryanya27bdf72013-04-05 14:40:25 +0000165 CHECK_EQ(tid(), 0);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000166 return 0;
167 }
168
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000169 thread_return_t res = start_routine_(arg_);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000170
Sergey Matveeve86e35f2013-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 Serebryanyaf344152012-01-11 02:03:16 +0000178
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000179 return res;
180}
181
Sergey Matveev12d01ba2013-05-29 13:09:44 +0000182void AsanThread::SetThreadStackAndTls() {
Kostya Serebryany621770a2013-09-19 14:59:52 +0000183 uptr tls_size = 0;
184 GetThreadStackAndTls(tid() == 0, &stack_bottom_, &stack_size_, &tls_begin_,
Sergey Matveev12d01ba2013-05-29 13:09:44 +0000185 &tls_size);
Kostya Serebryany621770a2013-09-19 14:59:52 +0000186 stack_top_ = stack_bottom_ + stack_size_;
Sergey Matveev12d01ba2013-05-29 13:09:44 +0000187 tls_end_ = tls_begin_ + tls_size;
188
Alexey Samsonove5931fd2012-06-07 07:13:46 +0000189 int local;
190 CHECK(AddrIsInStack((uptr)&local));
191}
192
Sergey Matveev12d01ba2013-05-29 13:09:44 +0000193void AsanThread::ClearShadowForThreadStackAndTLS() {
Alexey Samsonov55cdfc62012-01-17 06:35:31 +0000194 PoisonShadow(stack_bottom_, stack_top_ - stack_bottom_, 0);
Sergey Matveev12d01ba2013-05-29 13:09:44 +0000195 if (tls_begin_ != tls_end_)
196 PoisonShadow(tls_begin_, tls_end_ - tls_begin_, 0);
Alexey Samsonov55cdfc62012-01-17 06:35:31 +0000197}
198
Kostya Serebryany50f3daa2013-03-22 10:36:24 +0000199const char *AsanThread::GetFrameNameByAddr(uptr addr, uptr *offset,
200 uptr *frame_pc) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000201 uptr bottom = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000202 if (AddrIsInStack(addr)) {
203 bottom = stack_bottom();
Kostya Serebryanydcf98bf2013-09-12 08:43:44 +0000204 } else if (has_fake_stack()) {
Kostya Serebryany7a0bba42013-06-26 12:16:05 +0000205 bottom = fake_stack()->AddrIsInFakeStack(addr);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000206 CHECK(bottom);
Alexander Potapenkoe406c8c2012-11-15 15:24:42 +0000207 *offset = addr - bottom;
Kostya Serebryany50f3daa2013-03-22 10:36:24 +0000208 *frame_pc = ((uptr*)bottom)[2];
Alexander Potapenkoe406c8c2012-11-15 15:24:42 +0000209 return (const char *)((uptr*)bottom)[1];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000210 }
Kostya Serebryany5af39e52012-11-21 12:38:58 +0000211 uptr aligned_addr = addr & ~(SANITIZER_WORDSIZE/8 - 1); // align addr.
Kostya Serebryanyee392552012-05-31 15:02:07 +0000212 u8 *shadow_ptr = (u8*)MemToShadow(aligned_addr);
213 u8 *shadow_bottom = (u8*)MemToShadow(bottom);
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000214
215 while (shadow_ptr >= shadow_bottom &&
Alexander Potapenkoe406c8c2012-11-15 15:24:42 +0000216 *shadow_ptr != kAsanStackLeftRedzoneMagic) {
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000217 shadow_ptr--;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000218 }
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000219
220 while (shadow_ptr >= shadow_bottom &&
Alexander Potapenkoe406c8c2012-11-15 15:24:42 +0000221 *shadow_ptr == kAsanStackLeftRedzoneMagic) {
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000222 shadow_ptr--;
223 }
224
225 if (shadow_ptr < shadow_bottom) {
Alexander Potapenkoe406c8c2012-11-15 15:24:42 +0000226 *offset = 0;
227 return "UNKNOWN";
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000228 }
229
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000230 uptr* ptr = (uptr*)SHADOW_TO_MEM((uptr)(shadow_ptr + 1));
Alexander Potapenkoe406c8c2012-11-15 15:24:42 +0000231 CHECK(ptr[0] == kCurrentStackFrameMagic);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000232 *offset = addr - (uptr)ptr;
Kostya Serebryany50f3daa2013-03-22 10:36:24 +0000233 *frame_pc = ptr[2];
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000234 return (const char*)ptr[1];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000235}
236
Alexey Samsonovdef1be92013-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 Serebryany7a0bba42013-06-26 12:16:05 +0000241 if (!t) return false;
242 if (t->AddrIsInStack((uptr)addr)) return true;
Kostya Serebryanyb39a6042013-09-12 08:47:00 +0000243 if (t->has_fake_stack() && t->fake_stack()->AddrIsInFakeStack((uptr)addr))
Kostya Serebryany7a0bba42013-06-26 12:16:05 +0000244 return true;
245 return false;
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000246}
247
Alexey Samsonov89c13842013-03-20 09:23:28 +0000248AsanThread *GetCurrentThread() {
Sergey Matveevc6ac98d2013-07-08 12:57:24 +0000249 AsanThreadContext *context =
250 reinterpret_cast<AsanThreadContext *>(AsanTSDGet());
Alexey Samsonovdef1be92013-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 Vyukov195369b2013-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 Samsonovdef1be92013-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 Samsonov89c13842013-03-20 09:23:28 +0000263 }
Alexey Samsonov89c13842013-03-20 09:23:28 +0000264 return 0;
265 }
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000266 return context->thread;
Alexey Samsonov89c13842013-03-20 09:23:28 +0000267}
268
269void SetCurrentThread(AsanThread *t) {
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000270 CHECK(t->context());
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700271 VReport(2, "SetCurrentThread: %p for thread %p\n", t->context(),
272 (void *)GetThreadSelf());
Alexey Samsonov89c13842013-03-20 09:23:28 +0000273 // Make sure we do not reset the current AsanThread.
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000274 CHECK_EQ(0, AsanTSDGet());
275 AsanTSDSet(t->context());
276 CHECK_EQ(t->context(), AsanTSDGet());
Alexey Samsonov89c13842013-03-20 09:23:28 +0000277}
278
279u32 GetCurrentTidOrInvalid() {
280 AsanThread *t = GetCurrentThread();
281 return t ? t->tid() : kInvalidTid;
282}
283
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000284AsanThread *FindThreadByStackAddress(uptr addr) {
285 asanThreadRegistry().CheckLocked();
286 AsanThreadContext *tctx = static_cast<AsanThreadContext *>(
287 asanThreadRegistry().FindThreadContextLocked(ThreadStackContainsAddress,
288 (void *)addr));
289 return tctx ? tctx->thread : 0;
290}
Sergey Matveevc6ac98d2013-07-08 12:57:24 +0000291
292void EnsureMainThreadIDIsCorrect() {
293 AsanThreadContext *context =
294 reinterpret_cast<AsanThreadContext *>(AsanTSDGet());
295 if (context && (context->tid == 0))
296 context->os_id = GetTid();
297}
Sergey Matveevc5193352013-10-14 14:04:50 +0000298
299__asan::AsanThread *GetAsanThreadByOsIDLocked(uptr os_id) {
300 __asan::AsanThreadContext *context = static_cast<__asan::AsanThreadContext *>(
301 __asan::asanThreadRegistry().FindThreadContextByOsIDLocked(os_id));
302 if (!context) return 0;
303 return context->thread;
304}
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000305} // namespace __asan
Sergey Matveevf1ac1a42013-05-21 13:40:13 +0000306
307// --- Implementation of LSan-specific functions --- {{{1
308namespace __lsan {
309bool GetThreadRangesLocked(uptr os_id, uptr *stack_begin, uptr *stack_end,
310 uptr *tls_begin, uptr *tls_end,
311 uptr *cache_begin, uptr *cache_end) {
Sergey Matveevc5193352013-10-14 14:04:50 +0000312 __asan::AsanThread *t = __asan::GetAsanThreadByOsIDLocked(os_id);
Sergey Matveev12d01ba2013-05-29 13:09:44 +0000313 if (!t) return false;
314 *stack_begin = t->stack_bottom();
315 *stack_end = t->stack_top();
316 *tls_begin = t->tls_begin();
317 *tls_end = t->tls_end();
318 // ASan doesn't keep allocator caches in TLS, so these are unused.
319 *cache_begin = 0;
320 *cache_end = 0;
321 return true;
Sergey Matveevf1ac1a42013-05-21 13:40:13 +0000322}
323
Sergey Matveevc5193352013-10-14 14:04:50 +0000324void ForEachExtraStackRange(uptr os_id, RangeIteratorCallback callback,
325 void *arg) {
326 __asan::AsanThread *t = __asan::GetAsanThreadByOsIDLocked(os_id);
327 if (t && t->has_fake_stack())
328 t->fake_stack()->ForEachFakeFrame(callback, arg);
329}
330
Sergey Matveevf1ac1a42013-05-21 13:40:13 +0000331void LockThreadRegistry() {
332 __asan::asanThreadRegistry().Lock();
333}
334
335void UnlockThreadRegistry() {
336 __asan::asanThreadRegistry().Unlock();
337}
Sergey Matveevc6ac98d2013-07-08 12:57:24 +0000338
339void EnsureMainThreadIDIsCorrect() {
340 __asan::EnsureMainThreadIDIsCorrect();
341}
Sergey Matveevf1ac1a42013-05-21 13:40:13 +0000342} // namespace __lsan