blob: ce53bea753e504a6d4d39e3a25416b64e6794e4a [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"
Kostya Serebryany71788fa2014-01-29 09:29:16 +000023#include "sanitizer_common/sanitizer_tls_get_addr.h"
Sergey Matveev65dd62a2013-05-21 13:40:13 +000024#include "lsan/lsan_common.h"
Kostya Serebryany019b76f2011-11-30 01:07:02 +000025
Kostya Serebryany019b76f2011-11-30 01:07:02 +000026namespace __asan {
27
Alexey Samsonov54afba82013-03-21 11:23:41 +000028// AsanThreadContext implementation.
Kostya Serebryany019b76f2011-11-30 01:07:02 +000029
Alexey Samsonov54afba82013-03-21 11:23:41 +000030void AsanThreadContext::OnCreated(void *arg) {
31 CreateThreadContextArgs *args = static_cast<CreateThreadContextArgs*>(arg);
Kostya Serebryany96288392013-10-18 14:50:44 +000032 if (args->stack)
Alexey Samsonov3741ab82014-10-26 06:23:07 +000033 stack_id = StackDepotPut(*args->stack);
Alexey Samsonov54afba82013-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 Serebryanyc1aa0e82013-06-03 14:49:25 +000043// MIPS requires aligned address
Timur Iskhodzhanovbaf90cc2013-06-04 08:25:17 +000044static ALIGNED(16) char thread_registry_placeholder[sizeof(ThreadRegistry)];
Alexey Samsonov54afba82013-03-21 11:23:41 +000045static ThreadRegistry *asan_thread_registry;
46
Kostya Serebryanyf11e4852013-10-18 15:07:07 +000047static BlockingMutex mu_for_thread_context(LINKER_INITIALIZED);
48static LowLevelAllocator allocator_for_thread_context;
49
Alexey Samsonov54afba82013-03-21 11:23:41 +000050static ThreadContextBase *GetAsanThreadContext(u32 tid) {
Kostya Serebryanyf11e4852013-10-18 15:07:07 +000051 BlockingMutexLock lock(&mu_for_thread_context);
Peter Collingbourne50cb32e2013-10-24 06:23:39 +000052 return new(allocator_for_thread_context) AsanThreadContext(tid);
Alexey Samsonov54afba82013-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 Serebryanyf22c6972012-11-23 15:38:49 +000080 uptr PageSize = GetPageSizeCached();
81 uptr size = RoundUpTo(sizeof(AsanThread), PageSize);
Joerg Sonnenberger9d09e2f2014-02-26 20:33:22 +000082 AsanThread *thread = (AsanThread*)MmapOrDie(size, __func__);
Alexey Samsonov2d3a67b2012-01-17 06:35:31 +000083 thread->start_routine_ = start_routine;
84 thread->arg_ = arg;
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;
Sergey Matveev9be70fb2013-12-05 12:04:51 +000091 VReport(1, "T%d TSDDtor\n", context->tid);
Alexey Samsonov54afba82013-03-21 11:23:41 +000092 if (context->thread)
93 context->thread->Destroy();
Kostya Serebryanyb5eb5a72012-02-07 00:27:15 +000094}
95
Kostya Serebryany3f4b9bb2012-01-06 19:44:11 +000096void AsanThread::Destroy() {
Kostya Serebryany7a3a93f2013-12-11 13:54:01 +000097 int tid = this->tid();
98 VReport(1, "T%d exited\n", tid);
Kostya Serebryanyb5eb5a72012-02-07 00:27:15 +000099
Kostya Serebryany04a17672013-11-13 13:27:44 +0000100 malloc_storage().CommitBack();
Alexander Potapenkocf4bef32014-01-28 09:28:57 +0000101 if (common_flags()->use_sigaltstack) UnsetAlternateSignalStack();
Kostya Serebryany7a3a93f2013-12-11 13:54:01 +0000102 asanThreadRegistry().FinishThread(tid);
Alexey Samsonov4b168852013-09-02 08:39:07 +0000103 FlushToDeadThreadStats(&stats_);
Kostya Serebryany019b76f2011-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 Matveev09886cd2013-05-29 13:09:44 +0000107 ClearShadowForThreadStackAndTLS();
Kostya Serebryany7a3a93f2013-12-11 13:54:01 +0000108 DeleteFakeStack(tid);
Kostya Serebryanyf22c6972012-11-23 15:38:49 +0000109 uptr size = RoundUpTo(sizeof(AsanThread), GetPageSizeCached());
Alexey Samsonov40d5b772012-06-06 16:15:07 +0000110 UnmapOrDie(this, size);
Kostya Serebryany71788fa2014-01-29 09:29:16 +0000111 DTLS_Destroy();
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000112}
113
Kostya Serebryany628cda72013-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,
Alp Toker1ee7fc72014-05-15 02:22:34 +0000126 // if that was successful, it initializes the pointer.
Kostya Serebryany628cda72013-09-12 08:34:50 +0000127 if (atomic_compare_exchange_strong(
128 reinterpret_cast<atomic_uintptr_t *>(&fake_stack_), &old_val, 1UL,
Kostya Serebryany43c44932013-09-13 06:32:26 +0000129 memory_order_relaxed)) {
Kostya Serebryanyc3d43ca2013-09-18 10:35:12 +0000130 uptr stack_size_log = Log2(RoundUpToPowerOfTwo(stack_size));
Kostya Serebryany1aedf6c2013-12-16 08:42:08 +0000131 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 Serebryanyc3d43ca2013-09-18 10:35:12 +0000136 fake_stack_ = FakeStack::Create(stack_size_log);
Kostya Serebryany43c44932013-09-13 06:32:26 +0000137 SetTLSFakeStack(fake_stack_);
138 return fake_stack_;
139 }
Kostya Serebryany628cda72013-09-12 08:34:50 +0000140 return 0;
141}
142
Kostya Serebryany6bb2f1d2011-12-16 19:13:35 +0000143void AsanThread::Init() {
Kostya Serebryany558b3362014-06-06 07:35:35 +0000144 fake_stack_ = 0; // Will be initialized lazily if needed.
145 CHECK_EQ(this->stack_size(), 0U);
Sergey Matveev09886cd2013-05-29 13:09:44 +0000146 SetThreadStackAndTls();
Kostya Serebryany558b3362014-06-06 07:35:35 +0000147 CHECK_GT(this->stack_size(), 0U);
Alexey Samsonov2d3a67b2012-01-17 06:35:31 +0000148 CHECK(AddrIsInMem(stack_bottom_));
Kostya Serebryany63c36bb2013-01-18 11:30:36 +0000149 CHECK(AddrIsInMem(stack_top_ - 1));
Sergey Matveev09886cd2013-05-29 13:09:44 +0000150 ClearShadowForThreadStackAndTLS();
Sergey Matveev9be70fb2013-12-05 12:04:51 +0000151 int local = 0;
152 VReport(1, "T%d: stack [%p,%p) size 0x%zx; local=%p\n", tid(),
153 (void *)stack_bottom_, (void *)stack_top_, stack_top_ - stack_bottom_,
154 &local);
Alexander Potapenko51e64882012-07-23 14:07:58 +0000155 AsanPlatformThreadInit();
Kostya Serebryany6bb2f1d2011-12-16 19:13:35 +0000156}
157
Alexey Samsonov54afba82013-03-21 11:23:41 +0000158thread_return_t AsanThread::ThreadStart(uptr os_id) {
Kostya Serebryany6bb2f1d2011-12-16 19:13:35 +0000159 Init();
Alexey Samsonov54afba82013-03-21 11:23:41 +0000160 asanThreadRegistry().StartThread(tid(), os_id, 0);
Alexander Potapenkocf4bef32014-01-28 09:28:57 +0000161 if (common_flags()->use_sigaltstack) SetAlternateSignalStack();
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000162
163 if (!start_routine_) {
Kostya Serebryany8d032042012-05-31 14:35:53 +0000164 // start_routine_ == 0 if we're on the main thread or on one of the
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000165 // OS X libdispatch worker threads. But nobody is supposed to call
166 // ThreadStart() for the worker threads.
Kostya Serebryany5b4267f2013-04-05 14:40:25 +0000167 CHECK_EQ(tid(), 0);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000168 return 0;
169 }
170
Timur Iskhodzhanov0f9c9a52012-02-24 15:28:43 +0000171 thread_return_t res = start_routine_(arg_);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000172
Sergey Matveevda9f5e72013-10-14 12:01:05 +0000173 // On POSIX systems we defer this to the TSD destructor. LSan will consider
174 // the thread's memory as non-live from the moment we call Destroy(), even
175 // though that memory might contain pointers to heap objects which will be
176 // cleaned up by a user-defined TSD destructor. Thus, calling Destroy() before
177 // the TSD destructors have run might cause false positives in LSan.
178 if (!SANITIZER_POSIX)
179 this->Destroy();
Kostya Serebryany332923b2012-01-11 02:03:16 +0000180
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000181 return res;
182}
183
Sergey Matveev09886cd2013-05-29 13:09:44 +0000184void AsanThread::SetThreadStackAndTls() {
Kostya Serebryanyf8bbdfa2013-09-19 14:59:52 +0000185 uptr tls_size = 0;
186 GetThreadStackAndTls(tid() == 0, &stack_bottom_, &stack_size_, &tls_begin_,
Sergey Matveev09886cd2013-05-29 13:09:44 +0000187 &tls_size);
Kostya Serebryanyf8bbdfa2013-09-19 14:59:52 +0000188 stack_top_ = stack_bottom_ + stack_size_;
Sergey Matveev09886cd2013-05-29 13:09:44 +0000189 tls_end_ = tls_begin_ + tls_size;
190
Alexey Samsonov4b1f1032012-06-07 07:13:46 +0000191 int local;
192 CHECK(AddrIsInStack((uptr)&local));
193}
194
Sergey Matveev09886cd2013-05-29 13:09:44 +0000195void AsanThread::ClearShadowForThreadStackAndTLS() {
Alexey Samsonov2d3a67b2012-01-17 06:35:31 +0000196 PoisonShadow(stack_bottom_, stack_top_ - stack_bottom_, 0);
Sergey Matveev09886cd2013-05-29 13:09:44 +0000197 if (tls_begin_ != tls_end_)
198 PoisonShadow(tls_begin_, tls_end_ - tls_begin_, 0);
Alexey Samsonov2d3a67b2012-01-17 06:35:31 +0000199}
200
Alexey Samsonov0470e242014-10-01 21:13:00 +0000201bool AsanThread::GetStackFrameAccessByAddr(uptr addr,
202 StackFrameAccess *access) {
Kostya Serebryany8d032042012-05-31 14:35:53 +0000203 uptr bottom = 0;
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000204 if (AddrIsInStack(addr)) {
205 bottom = stack_bottom();
Kostya Serebryany736bd082013-09-12 08:43:44 +0000206 } else if (has_fake_stack()) {
Kostya Serebryany6a068a72013-06-26 12:16:05 +0000207 bottom = fake_stack()->AddrIsInFakeStack(addr);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000208 CHECK(bottom);
Alexey Samsonov0470e242014-10-01 21:13:00 +0000209 access->offset = addr - bottom;
210 access->frame_pc = ((uptr*)bottom)[2];
211 access->frame_descr = (const char *)((uptr*)bottom)[1];
212 return true;
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000213 }
Kostya Serebryany734f1eb2012-11-21 12:38:58 +0000214 uptr aligned_addr = addr & ~(SANITIZER_WORDSIZE/8 - 1); // align addr.
Kostya Serebryany1d35d152012-05-31 15:02:07 +0000215 u8 *shadow_ptr = (u8*)MemToShadow(aligned_addr);
216 u8 *shadow_bottom = (u8*)MemToShadow(bottom);
Evgeniy Stepanovd989be12012-05-12 12:33:10 +0000217
218 while (shadow_ptr >= shadow_bottom &&
Alexander Potapenkobcc00a42012-11-15 15:24:42 +0000219 *shadow_ptr != kAsanStackLeftRedzoneMagic) {
Evgeniy Stepanovd989be12012-05-12 12:33:10 +0000220 shadow_ptr--;
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000221 }
Evgeniy Stepanovd989be12012-05-12 12:33:10 +0000222
223 while (shadow_ptr >= shadow_bottom &&
Alexander Potapenkobcc00a42012-11-15 15:24:42 +0000224 *shadow_ptr == kAsanStackLeftRedzoneMagic) {
Evgeniy Stepanovd989be12012-05-12 12:33:10 +0000225 shadow_ptr--;
226 }
227
228 if (shadow_ptr < shadow_bottom) {
Alexey Samsonov0470e242014-10-01 21:13:00 +0000229 return false;
Evgeniy Stepanovd989be12012-05-12 12:33:10 +0000230 }
231
Kostya Serebryany8d032042012-05-31 14:35:53 +0000232 uptr* ptr = (uptr*)SHADOW_TO_MEM((uptr)(shadow_ptr + 1));
Alexander Potapenkobcc00a42012-11-15 15:24:42 +0000233 CHECK(ptr[0] == kCurrentStackFrameMagic);
Alexey Samsonov0470e242014-10-01 21:13:00 +0000234 access->offset = addr - (uptr)ptr;
235 access->frame_pc = ptr[2];
236 access->frame_descr = (const char*)ptr[1];
237 return true;
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000238}
239
Alexey Samsonov54afba82013-03-21 11:23:41 +0000240static bool ThreadStackContainsAddress(ThreadContextBase *tctx_base,
241 void *addr) {
242 AsanThreadContext *tctx = static_cast<AsanThreadContext*>(tctx_base);
243 AsanThread *t = tctx->thread;
Kostya Serebryany6a068a72013-06-26 12:16:05 +0000244 if (!t) return false;
245 if (t->AddrIsInStack((uptr)addr)) return true;
Kostya Serebryany44441cc2013-09-12 08:47:00 +0000246 if (t->has_fake_stack() && t->fake_stack()->AddrIsInFakeStack((uptr)addr))
Kostya Serebryany6a068a72013-06-26 12:16:05 +0000247 return true;
248 return false;
Alexey Samsonov54afba82013-03-21 11:23:41 +0000249}
250
Alexey Samsonovcf025cb2013-03-20 09:23:28 +0000251AsanThread *GetCurrentThread() {
Sergey Matveevbdeff952013-07-08 12:57:24 +0000252 AsanThreadContext *context =
253 reinterpret_cast<AsanThreadContext *>(AsanTSDGet());
Alexey Samsonov54afba82013-03-21 11:23:41 +0000254 if (!context) {
255 if (SANITIZER_ANDROID) {
256 // On Android, libc constructor is called _after_ asan_init, and cleans up
257 // TSD. Try to figure out if this is still the main thread by the stack
258 // address. We are not entirely sure that we have correct main thread
Dmitry Vyukova7e42b52013-03-22 07:29:59 +0000259 // limits, so only do this magic on Android, and only if the found thread
260 // is the main thread.
Alexey Samsonov54afba82013-03-21 11:23:41 +0000261 AsanThreadContext *tctx = GetThreadContextByTidLocked(0);
262 if (ThreadStackContainsAddress(tctx, &context)) {
263 SetCurrentThread(tctx->thread);
264 return tctx->thread;
265 }
Alexey Samsonovcf025cb2013-03-20 09:23:28 +0000266 }
Alexey Samsonovcf025cb2013-03-20 09:23:28 +0000267 return 0;
268 }
Alexey Samsonov54afba82013-03-21 11:23:41 +0000269 return context->thread;
Alexey Samsonovcf025cb2013-03-20 09:23:28 +0000270}
271
272void SetCurrentThread(AsanThread *t) {
Alexey Samsonov54afba82013-03-21 11:23:41 +0000273 CHECK(t->context());
Sergey Matveev9be70fb2013-12-05 12:04:51 +0000274 VReport(2, "SetCurrentThread: %p for thread %p\n", t->context(),
275 (void *)GetThreadSelf());
Alexey Samsonovcf025cb2013-03-20 09:23:28 +0000276 // Make sure we do not reset the current AsanThread.
Alexey Samsonov54afba82013-03-21 11:23:41 +0000277 CHECK_EQ(0, AsanTSDGet());
278 AsanTSDSet(t->context());
279 CHECK_EQ(t->context(), AsanTSDGet());
Alexey Samsonovcf025cb2013-03-20 09:23:28 +0000280}
281
282u32 GetCurrentTidOrInvalid() {
283 AsanThread *t = GetCurrentThread();
284 return t ? t->tid() : kInvalidTid;
285}
286
Alexey Samsonov54afba82013-03-21 11:23:41 +0000287AsanThread *FindThreadByStackAddress(uptr addr) {
288 asanThreadRegistry().CheckLocked();
289 AsanThreadContext *tctx = static_cast<AsanThreadContext *>(
290 asanThreadRegistry().FindThreadContextLocked(ThreadStackContainsAddress,
291 (void *)addr));
292 return tctx ? tctx->thread : 0;
293}
Sergey Matveevbdeff952013-07-08 12:57:24 +0000294
295void EnsureMainThreadIDIsCorrect() {
296 AsanThreadContext *context =
297 reinterpret_cast<AsanThreadContext *>(AsanTSDGet());
298 if (context && (context->tid == 0))
299 context->os_id = GetTid();
300}
Sergey Matveev43d90cb2013-10-14 14:04:50 +0000301
302__asan::AsanThread *GetAsanThreadByOsIDLocked(uptr os_id) {
303 __asan::AsanThreadContext *context = static_cast<__asan::AsanThreadContext *>(
304 __asan::asanThreadRegistry().FindThreadContextByOsIDLocked(os_id));
305 if (!context) return 0;
306 return context->thread;
307}
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000308} // namespace __asan
Sergey Matveev65dd62a2013-05-21 13:40:13 +0000309
310// --- Implementation of LSan-specific functions --- {{{1
311namespace __lsan {
312bool GetThreadRangesLocked(uptr os_id, uptr *stack_begin, uptr *stack_end,
313 uptr *tls_begin, uptr *tls_end,
314 uptr *cache_begin, uptr *cache_end) {
Sergey Matveev43d90cb2013-10-14 14:04:50 +0000315 __asan::AsanThread *t = __asan::GetAsanThreadByOsIDLocked(os_id);
Sergey Matveev09886cd2013-05-29 13:09:44 +0000316 if (!t) return false;
317 *stack_begin = t->stack_bottom();
318 *stack_end = t->stack_top();
319 *tls_begin = t->tls_begin();
320 *tls_end = t->tls_end();
321 // ASan doesn't keep allocator caches in TLS, so these are unused.
322 *cache_begin = 0;
323 *cache_end = 0;
324 return true;
Sergey Matveev65dd62a2013-05-21 13:40:13 +0000325}
326
Sergey Matveev43d90cb2013-10-14 14:04:50 +0000327void ForEachExtraStackRange(uptr os_id, RangeIteratorCallback callback,
328 void *arg) {
329 __asan::AsanThread *t = __asan::GetAsanThreadByOsIDLocked(os_id);
330 if (t && t->has_fake_stack())
331 t->fake_stack()->ForEachFakeFrame(callback, arg);
332}
333
Sergey Matveev65dd62a2013-05-21 13:40:13 +0000334void LockThreadRegistry() {
335 __asan::asanThreadRegistry().Lock();
336}
337
338void UnlockThreadRegistry() {
339 __asan::asanThreadRegistry().Unlock();
340}
Sergey Matveevbdeff952013-07-08 12:57:24 +0000341
342void EnsureMainThreadIDIsCorrect() {
343 __asan::EnsureMainThreadIDIsCorrect();
344}
Sergey Matveev65dd62a2013-05-21 13:40:13 +0000345} // namespace __lsan