blob: 5c2289491535e07bc817fca8a451fd5019b4e905 [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"
Sergey Matveev65dd62a2013-05-21 13:40:13 +000022#include "lsan/lsan_common.h"
Kostya Serebryany019b76f2011-11-30 01:07:02 +000023
Kostya Serebryany019b76f2011-11-30 01:07:02 +000024namespace __asan {
25
Alexey Samsonov54afba82013-03-21 11:23:41 +000026// AsanThreadContext implementation.
Kostya Serebryany019b76f2011-11-30 01:07:02 +000027
Alexey Samsonov54afba82013-03-21 11:23:41 +000028void 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
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
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) {
Kostya Serebryanyf22c6972012-11-23 15:38:49 +000076 uptr PageSize = GetPageSizeCached();
77 uptr size = RoundUpTo(sizeof(AsanThread), PageSize);
Alexey Samsonov40d5b772012-06-06 16:15:07 +000078 AsanThread *thread = (AsanThread*)MmapOrDie(size, __FUNCTION__);
Alexey Samsonov2d3a67b2012-01-17 06:35:31 +000079 thread->start_routine_ = start_routine;
80 thread->arg_ = arg;
Alexey Samsonov54afba82013-03-21 11:23:41 +000081 thread->context_ = 0;
Alexey Samsonov2d3a67b2012-01-17 06:35:31 +000082
83 return thread;
Kostya Serebryany019b76f2011-11-30 01:07:02 +000084}
85
Alexey Samsonov54afba82013-03-21 11:23:41 +000086void 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();
Kostya Serebryanyb5eb5a72012-02-07 00:27:15 +000092}
93
Kostya Serebryany3f4b9bb2012-01-06 19:44:11 +000094void AsanThread::Destroy() {
Alexey Samsonov34efb8e2012-07-09 14:36:04 +000095 if (flags()->verbosity >= 1) {
Kostya Serebryanyb5eb5a72012-02-07 00:27:15 +000096 Report("T%d exited\n", tid());
97 }
98
Alexey Samsonov54afba82013-03-21 11:23:41 +000099 asanThreadRegistry().FinishThread(tid());
Alexey Samsonov4b168852013-09-02 08:39:07 +0000100 FlushToDeadThreadStats(&stats_);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000101 // 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.
Sergey Matveev09886cd2013-05-29 13:09:44 +0000104 ClearShadowForThreadStackAndTLS();
Kostya Serebryany6a068a72013-06-26 12:16:05 +0000105 DeleteFakeStack();
Kostya Serebryanyf22c6972012-11-23 15:38:49 +0000106 uptr size = RoundUpTo(sizeof(AsanThread), GetPageSizeCached());
Alexey Samsonov40d5b772012-06-06 16:15:07 +0000107 UnmapOrDie(this, size);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000108}
109
Kostya Serebryany628cda72013-09-12 08:34:50 +0000110// 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,
Kostya Serebryany43c44932013-09-13 06:32:26 +0000125 memory_order_relaxed)) {
Kostya Serebryanyc3d43ca2013-09-18 10:35:12 +0000126 uptr stack_size_log = Log2(RoundUpToPowerOfTwo(stack_size));
127 fake_stack_ = FakeStack::Create(stack_size_log);
Kostya Serebryany43c44932013-09-13 06:32:26 +0000128 SetTLSFakeStack(fake_stack_);
Kostya Serebryanyc3d43ca2013-09-18 10:35:12 +0000129 if (flags()->verbosity) {
130 u8 *p = reinterpret_cast<u8 *>(fake_stack_);
131 Report("T%d: FakeStack created: %p -- %p\n", tid(), p,
132 p + FakeStack::RequiredSize(stack_size_log));
133 }
Kostya Serebryany43c44932013-09-13 06:32:26 +0000134 return fake_stack_;
135 }
Kostya Serebryany628cda72013-09-12 08:34:50 +0000136 return 0;
137}
138
Kostya Serebryany6bb2f1d2011-12-16 19:13:35 +0000139void AsanThread::Init() {
Sergey Matveev09886cd2013-05-29 13:09:44 +0000140 SetThreadStackAndTls();
Alexey Samsonov2d3a67b2012-01-17 06:35:31 +0000141 CHECK(AddrIsInMem(stack_bottom_));
Kostya Serebryany63c36bb2013-01-18 11:30:36 +0000142 CHECK(AddrIsInMem(stack_top_ - 1));
Sergey Matveev09886cd2013-05-29 13:09:44 +0000143 ClearShadowForThreadStackAndTLS();
Alexey Samsonov34efb8e2012-07-09 14:36:04 +0000144 if (flags()->verbosity >= 1) {
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000145 int local = 0;
Evgeniy Stepanov823085a2012-03-21 11:32:46 +0000146 Report("T%d: stack [%p,%p) size 0x%zx; local=%p\n",
Alexey Samsonov461820a2012-06-06 10:46:00 +0000147 tid(), (void*)stack_bottom_, (void*)stack_top_,
Kostya Serebryany5be458c2012-01-09 19:18:27 +0000148 stack_top_ - stack_bottom_, &local);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000149 }
Kostya Serebryany6a068a72013-06-26 12:16:05 +0000150 fake_stack_ = 0; // Will be initialized lazily if needed.
Alexander Potapenko51e64882012-07-23 14:07:58 +0000151 AsanPlatformThreadInit();
Kostya Serebryany6bb2f1d2011-12-16 19:13:35 +0000152}
153
Alexey Samsonov54afba82013-03-21 11:23:41 +0000154thread_return_t AsanThread::ThreadStart(uptr os_id) {
Kostya Serebryany6bb2f1d2011-12-16 19:13:35 +0000155 Init();
Alexey Samsonov54afba82013-03-21 11:23:41 +0000156 asanThreadRegistry().StartThread(tid(), os_id, 0);
Alexey Samsonov34efb8e2012-07-09 14:36:04 +0000157 if (flags()->use_sigaltstack) SetAlternateSignalStack();
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000158
159 if (!start_routine_) {
Kostya Serebryany8d032042012-05-31 14:35:53 +0000160 // start_routine_ == 0 if we're on the main thread or on one of the
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000161 // OS X libdispatch worker threads. But nobody is supposed to call
162 // ThreadStart() for the worker threads.
Kostya Serebryany5b4267f2013-04-05 14:40:25 +0000163 CHECK_EQ(tid(), 0);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000164 return 0;
165 }
166
Timur Iskhodzhanov0f9c9a52012-02-24 15:28:43 +0000167 thread_return_t res = start_routine_(arg_);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000168 malloc_storage().CommitBack();
Alexey Samsonov34efb8e2012-07-09 14:36:04 +0000169 if (flags()->use_sigaltstack) UnsetAlternateSignalStack();
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000170
Kostya Serebryany332923b2012-01-11 02:03:16 +0000171 this->Destroy();
172
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000173 return res;
174}
175
Sergey Matveev09886cd2013-05-29 13:09:44 +0000176void AsanThread::SetThreadStackAndTls() {
177 uptr stack_size = 0, tls_size = 0;
178 GetThreadStackAndTls(tid() == 0, &stack_bottom_, &stack_size, &tls_begin_,
179 &tls_size);
180 stack_top_ = stack_bottom_ + stack_size;
181 tls_end_ = tls_begin_ + tls_size;
182
Alexey Samsonov4b1f1032012-06-07 07:13:46 +0000183 int local;
184 CHECK(AddrIsInStack((uptr)&local));
185}
186
Sergey Matveev09886cd2013-05-29 13:09:44 +0000187void AsanThread::ClearShadowForThreadStackAndTLS() {
Alexey Samsonov2d3a67b2012-01-17 06:35:31 +0000188 PoisonShadow(stack_bottom_, stack_top_ - stack_bottom_, 0);
Sergey Matveev09886cd2013-05-29 13:09:44 +0000189 if (tls_begin_ != tls_end_)
190 PoisonShadow(tls_begin_, tls_end_ - tls_begin_, 0);
Alexey Samsonov2d3a67b2012-01-17 06:35:31 +0000191}
192
Kostya Serebryany667a34a2013-03-22 10:36:24 +0000193const char *AsanThread::GetFrameNameByAddr(uptr addr, uptr *offset,
194 uptr *frame_pc) {
Kostya Serebryany8d032042012-05-31 14:35:53 +0000195 uptr bottom = 0;
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000196 if (AddrIsInStack(addr)) {
197 bottom = stack_bottom();
Kostya Serebryany736bd082013-09-12 08:43:44 +0000198 } else if (has_fake_stack()) {
Kostya Serebryany6a068a72013-06-26 12:16:05 +0000199 bottom = fake_stack()->AddrIsInFakeStack(addr);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000200 CHECK(bottom);
Alexander Potapenkobcc00a42012-11-15 15:24:42 +0000201 *offset = addr - bottom;
Kostya Serebryany667a34a2013-03-22 10:36:24 +0000202 *frame_pc = ((uptr*)bottom)[2];
Alexander Potapenkobcc00a42012-11-15 15:24:42 +0000203 return (const char *)((uptr*)bottom)[1];
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000204 }
Kostya Serebryany734f1eb2012-11-21 12:38:58 +0000205 uptr aligned_addr = addr & ~(SANITIZER_WORDSIZE/8 - 1); // align addr.
Kostya Serebryany1d35d152012-05-31 15:02:07 +0000206 u8 *shadow_ptr = (u8*)MemToShadow(aligned_addr);
207 u8 *shadow_bottom = (u8*)MemToShadow(bottom);
Evgeniy Stepanovd989be12012-05-12 12:33:10 +0000208
209 while (shadow_ptr >= shadow_bottom &&
Alexander Potapenkobcc00a42012-11-15 15:24:42 +0000210 *shadow_ptr != kAsanStackLeftRedzoneMagic) {
Evgeniy Stepanovd989be12012-05-12 12:33:10 +0000211 shadow_ptr--;
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000212 }
Evgeniy Stepanovd989be12012-05-12 12:33:10 +0000213
214 while (shadow_ptr >= shadow_bottom &&
Alexander Potapenkobcc00a42012-11-15 15:24:42 +0000215 *shadow_ptr == kAsanStackLeftRedzoneMagic) {
Evgeniy Stepanovd989be12012-05-12 12:33:10 +0000216 shadow_ptr--;
217 }
218
219 if (shadow_ptr < shadow_bottom) {
Alexander Potapenkobcc00a42012-11-15 15:24:42 +0000220 *offset = 0;
221 return "UNKNOWN";
Evgeniy Stepanovd989be12012-05-12 12:33:10 +0000222 }
223
Kostya Serebryany8d032042012-05-31 14:35:53 +0000224 uptr* ptr = (uptr*)SHADOW_TO_MEM((uptr)(shadow_ptr + 1));
Alexander Potapenkobcc00a42012-11-15 15:24:42 +0000225 CHECK(ptr[0] == kCurrentStackFrameMagic);
Kostya Serebryany8d032042012-05-31 14:35:53 +0000226 *offset = addr - (uptr)ptr;
Kostya Serebryany667a34a2013-03-22 10:36:24 +0000227 *frame_pc = ptr[2];
Evgeniy Stepanovd989be12012-05-12 12:33:10 +0000228 return (const char*)ptr[1];
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000229}
230
Alexey Samsonov54afba82013-03-21 11:23:41 +0000231static bool ThreadStackContainsAddress(ThreadContextBase *tctx_base,
232 void *addr) {
233 AsanThreadContext *tctx = static_cast<AsanThreadContext*>(tctx_base);
234 AsanThread *t = tctx->thread;
Kostya Serebryany6a068a72013-06-26 12:16:05 +0000235 if (!t) return false;
236 if (t->AddrIsInStack((uptr)addr)) return true;
Kostya Serebryany44441cc2013-09-12 08:47:00 +0000237 if (t->has_fake_stack() && t->fake_stack()->AddrIsInFakeStack((uptr)addr))
Kostya Serebryany6a068a72013-06-26 12:16:05 +0000238 return true;
239 return false;
Alexey Samsonov54afba82013-03-21 11:23:41 +0000240}
241
Alexey Samsonovcf025cb2013-03-20 09:23:28 +0000242AsanThread *GetCurrentThread() {
Sergey Matveevbdeff952013-07-08 12:57:24 +0000243 AsanThreadContext *context =
244 reinterpret_cast<AsanThreadContext *>(AsanTSDGet());
Alexey Samsonov54afba82013-03-21 11:23:41 +0000245 if (!context) {
246 if (SANITIZER_ANDROID) {
247 // On Android, libc constructor is called _after_ asan_init, and cleans up
248 // TSD. Try to figure out if this is still the main thread by the stack
249 // address. We are not entirely sure that we have correct main thread
Dmitry Vyukova7e42b52013-03-22 07:29:59 +0000250 // limits, so only do this magic on Android, and only if the found thread
251 // is the main thread.
Alexey Samsonov54afba82013-03-21 11:23:41 +0000252 AsanThreadContext *tctx = GetThreadContextByTidLocked(0);
253 if (ThreadStackContainsAddress(tctx, &context)) {
254 SetCurrentThread(tctx->thread);
255 return tctx->thread;
256 }
Alexey Samsonovcf025cb2013-03-20 09:23:28 +0000257 }
Alexey Samsonovcf025cb2013-03-20 09:23:28 +0000258 return 0;
259 }
Alexey Samsonov54afba82013-03-21 11:23:41 +0000260 return context->thread;
Alexey Samsonovcf025cb2013-03-20 09:23:28 +0000261}
262
263void SetCurrentThread(AsanThread *t) {
Alexey Samsonov54afba82013-03-21 11:23:41 +0000264 CHECK(t->context());
Alexey Samsonovcf025cb2013-03-20 09:23:28 +0000265 if (flags()->verbosity >= 2) {
266 Report("SetCurrentThread: %p for thread %p\n",
Alexey Samsonov54afba82013-03-21 11:23:41 +0000267 t->context(), (void*)GetThreadSelf());
Alexey Samsonovcf025cb2013-03-20 09:23:28 +0000268 }
269 // Make sure we do not reset the current AsanThread.
Alexey Samsonov54afba82013-03-21 11:23:41 +0000270 CHECK_EQ(0, AsanTSDGet());
271 AsanTSDSet(t->context());
272 CHECK_EQ(t->context(), AsanTSDGet());
Alexey Samsonovcf025cb2013-03-20 09:23:28 +0000273}
274
275u32 GetCurrentTidOrInvalid() {
276 AsanThread *t = GetCurrentThread();
277 return t ? t->tid() : kInvalidTid;
278}
279
Alexey Samsonov54afba82013-03-21 11:23:41 +0000280AsanThread *FindThreadByStackAddress(uptr addr) {
281 asanThreadRegistry().CheckLocked();
282 AsanThreadContext *tctx = static_cast<AsanThreadContext *>(
283 asanThreadRegistry().FindThreadContextLocked(ThreadStackContainsAddress,
284 (void *)addr));
285 return tctx ? tctx->thread : 0;
286}
Sergey Matveevbdeff952013-07-08 12:57:24 +0000287
288void EnsureMainThreadIDIsCorrect() {
289 AsanThreadContext *context =
290 reinterpret_cast<AsanThreadContext *>(AsanTSDGet());
291 if (context && (context->tid == 0))
292 context->os_id = GetTid();
293}
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000294} // namespace __asan
Sergey Matveev65dd62a2013-05-21 13:40:13 +0000295
296// --- Implementation of LSan-specific functions --- {{{1
297namespace __lsan {
298bool GetThreadRangesLocked(uptr os_id, uptr *stack_begin, uptr *stack_end,
299 uptr *tls_begin, uptr *tls_end,
300 uptr *cache_begin, uptr *cache_end) {
Sergey Matveev09886cd2013-05-29 13:09:44 +0000301 __asan::AsanThreadContext *context = static_cast<__asan::AsanThreadContext *>(
302 __asan::asanThreadRegistry().FindThreadContextByOsIDLocked(os_id));
303 if (!context) return false;
304 __asan::AsanThread *t = context->thread;
305 if (!t) return false;
306 *stack_begin = t->stack_bottom();
307 *stack_end = t->stack_top();
308 *tls_begin = t->tls_begin();
309 *tls_end = t->tls_end();
310 // ASan doesn't keep allocator caches in TLS, so these are unused.
311 *cache_begin = 0;
312 *cache_end = 0;
313 return true;
Sergey Matveev65dd62a2013-05-21 13:40:13 +0000314}
315
316void LockThreadRegistry() {
317 __asan::asanThreadRegistry().Lock();
318}
319
320void UnlockThreadRegistry() {
321 __asan::asanThreadRegistry().Unlock();
322}
Sergey Matveevbdeff952013-07-08 12:57:24 +0000323
324void EnsureMainThreadIDIsCorrect() {
325 __asan::EnsureMainThreadIDIsCorrect();
326}
Sergey Matveev65dd62a2013-05-21 13:40:13 +0000327} // namespace __lsan