blob: da28381031a5f008010b6543712a15f61263001c [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"
Sergey Matveevf1ac1a42013-05-21 13:40:13 +000022#include "lsan/lsan_common.h"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000023
Kostya Serebryany1e172b42011-11-30 01:07:02 +000024namespace __asan {
25
Alexey Samsonovdef1be92013-03-21 11:23:41 +000026// AsanThreadContext implementation.
Kostya Serebryany1e172b42011-11-30 01:07:02 +000027
Alexey Samsonovdef1be92013-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
42static char thread_registry_placeholder[sizeof(ThreadRegistry)];
43static ThreadRegistry *asan_thread_registry;
44
45static ThreadContextBase *GetAsanThreadContext(u32 tid) {
46 void *mem = MmapOrDie(sizeof(AsanThreadContext), "AsanThreadContext");
47 return new(mem) AsanThreadContext(tid);
48}
49
50ThreadRegistry &asanThreadRegistry() {
51 static bool initialized;
52 // Don't worry about thread_safety - this should be called when there is
53 // a single thread.
54 if (!initialized) {
55 // Never reuse ASan threads: we store pointer to AsanThreadContext
56 // in TSD and can't reliably tell when no more TSD destructors will
57 // be called. It would be wrong to reuse AsanThreadContext for another
58 // thread before all TSD destructors will be called for it.
59 asan_thread_registry = new(thread_registry_placeholder) ThreadRegistry(
60 GetAsanThreadContext, kMaxNumberOfThreads, kMaxNumberOfThreads);
61 initialized = true;
62 }
63 return *asan_thread_registry;
64}
65
66AsanThreadContext *GetThreadContextByTidLocked(u32 tid) {
67 return static_cast<AsanThreadContext *>(
68 asanThreadRegistry().GetThreadLocked(tid));
69}
70
71// AsanThread implementation.
72
73AsanThread *AsanThread::Create(thread_callback_t start_routine,
74 void *arg) {
Kostya Serebryanyf67ec2b2012-11-23 15:38:49 +000075 uptr PageSize = GetPageSizeCached();
76 uptr size = RoundUpTo(sizeof(AsanThread), PageSize);
Alexey Samsonova25b3462012-06-06 16:15:07 +000077 AsanThread *thread = (AsanThread*)MmapOrDie(size, __FUNCTION__);
Alexey Samsonov55cdfc62012-01-17 06:35:31 +000078 thread->start_routine_ = start_routine;
79 thread->arg_ = arg;
Alexey Samsonovdef1be92013-03-21 11:23:41 +000080 thread->context_ = 0;
Alexey Samsonov55cdfc62012-01-17 06:35:31 +000081
82 return thread;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000083}
84
Alexey Samsonovdef1be92013-03-21 11:23:41 +000085void AsanThread::TSDDtor(void *tsd) {
86 AsanThreadContext *context = (AsanThreadContext*)tsd;
87 if (flags()->verbosity >= 1)
88 Report("T%d TSDDtor\n", context->tid);
89 if (context->thread)
90 context->thread->Destroy();
Kostya Serebryanyf58f9982012-02-07 00:27:15 +000091}
92
Kostya Serebryanya6b52262012-01-06 19:44:11 +000093void AsanThread::Destroy() {
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000094 if (flags()->verbosity >= 1) {
Kostya Serebryanyf58f9982012-02-07 00:27:15 +000095 Report("T%d exited\n", tid());
96 }
97
Alexey Samsonovdef1be92013-03-21 11:23:41 +000098 asanThreadRegistry().FinishThread(tid());
99 FlushToAccumulatedStats(&stats_);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000100 // We also clear the shadow on thread destruction because
101 // some code may still be executing in later TSD destructors
102 // and we don't want it to have any poisoned stack.
103 ClearShadowForThreadStack();
Alexey Samsonov55cdfc62012-01-17 06:35:31 +0000104 fake_stack().Cleanup();
Kostya Serebryanyf67ec2b2012-11-23 15:38:49 +0000105 uptr size = RoundUpTo(sizeof(AsanThread), GetPageSizeCached());
Alexey Samsonova25b3462012-06-06 16:15:07 +0000106 UnmapOrDie(this, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000107}
108
Kostya Serebryany69eca732011-12-16 19:13:35 +0000109void AsanThread::Init() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000110 SetThreadStackTopAndBottom();
Alexey Samsonov55cdfc62012-01-17 06:35:31 +0000111 CHECK(AddrIsInMem(stack_bottom_));
Kostya Serebryany541cfb12013-01-18 11:30:36 +0000112 CHECK(AddrIsInMem(stack_top_ - 1));
Alexey Samsonov55cdfc62012-01-17 06:35:31 +0000113 ClearShadowForThreadStack();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000114 if (flags()->verbosity >= 1) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000115 int local = 0;
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000116 Report("T%d: stack [%p,%p) size 0x%zx; local=%p\n",
Alexey Samsonov5bcca4e2012-06-06 10:46:00 +0000117 tid(), (void*)stack_bottom_, (void*)stack_top_,
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000118 stack_top_ - stack_bottom_, &local);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000119 }
Alexey Samsonov55cdfc62012-01-17 06:35:31 +0000120 fake_stack_.Init(stack_size());
Alexander Potapenko75b19eb2012-07-23 14:07:58 +0000121 AsanPlatformThreadInit();
Kostya Serebryany69eca732011-12-16 19:13:35 +0000122}
123
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000124thread_return_t AsanThread::ThreadStart(uptr os_id) {
Kostya Serebryany69eca732011-12-16 19:13:35 +0000125 Init();
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000126 asanThreadRegistry().StartThread(tid(), os_id, 0);
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000127 if (flags()->use_sigaltstack) SetAlternateSignalStack();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000128
129 if (!start_routine_) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000130 // start_routine_ == 0 if we're on the main thread or on one of the
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000131 // OS X libdispatch worker threads. But nobody is supposed to call
132 // ThreadStart() for the worker threads.
Kostya Serebryanya27bdf72013-04-05 14:40:25 +0000133 CHECK_EQ(tid(), 0);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000134 return 0;
135 }
136
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000137 thread_return_t res = start_routine_(arg_);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000138 malloc_storage().CommitBack();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000139 if (flags()->use_sigaltstack) UnsetAlternateSignalStack();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000140
Kostya Serebryanyaf344152012-01-11 02:03:16 +0000141 this->Destroy();
142
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000143 return res;
144}
145
Alexey Samsonove5931fd2012-06-07 07:13:46 +0000146void AsanThread::SetThreadStackTopAndBottom() {
147 GetThreadStackTopAndBottom(tid() == 0, &stack_top_, &stack_bottom_);
148 int local;
149 CHECK(AddrIsInStack((uptr)&local));
150}
151
Alexey Samsonov55cdfc62012-01-17 06:35:31 +0000152void AsanThread::ClearShadowForThreadStack() {
153 PoisonShadow(stack_bottom_, stack_top_ - stack_bottom_, 0);
154}
155
Kostya Serebryany50f3daa2013-03-22 10:36:24 +0000156const char *AsanThread::GetFrameNameByAddr(uptr addr, uptr *offset,
157 uptr *frame_pc) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000158 uptr bottom = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000159 if (AddrIsInStack(addr)) {
160 bottom = stack_bottom();
161 } else {
162 bottom = fake_stack().AddrIsInFakeStack(addr);
163 CHECK(bottom);
Alexander Potapenkoe406c8c2012-11-15 15:24:42 +0000164 *offset = addr - bottom;
Kostya Serebryany50f3daa2013-03-22 10:36:24 +0000165 *frame_pc = ((uptr*)bottom)[2];
Alexander Potapenkoe406c8c2012-11-15 15:24:42 +0000166 return (const char *)((uptr*)bottom)[1];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000167 }
Kostya Serebryany5af39e52012-11-21 12:38:58 +0000168 uptr aligned_addr = addr & ~(SANITIZER_WORDSIZE/8 - 1); // align addr.
Kostya Serebryanyee392552012-05-31 15:02:07 +0000169 u8 *shadow_ptr = (u8*)MemToShadow(aligned_addr);
170 u8 *shadow_bottom = (u8*)MemToShadow(bottom);
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000171
172 while (shadow_ptr >= shadow_bottom &&
Alexander Potapenkoe406c8c2012-11-15 15:24:42 +0000173 *shadow_ptr != kAsanStackLeftRedzoneMagic) {
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000174 shadow_ptr--;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000175 }
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000176
177 while (shadow_ptr >= shadow_bottom &&
Alexander Potapenkoe406c8c2012-11-15 15:24:42 +0000178 *shadow_ptr == kAsanStackLeftRedzoneMagic) {
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000179 shadow_ptr--;
180 }
181
182 if (shadow_ptr < shadow_bottom) {
Alexander Potapenkoe406c8c2012-11-15 15:24:42 +0000183 *offset = 0;
184 return "UNKNOWN";
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000185 }
186
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000187 uptr* ptr = (uptr*)SHADOW_TO_MEM((uptr)(shadow_ptr + 1));
Alexander Potapenkoe406c8c2012-11-15 15:24:42 +0000188 CHECK(ptr[0] == kCurrentStackFrameMagic);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000189 *offset = addr - (uptr)ptr;
Kostya Serebryany50f3daa2013-03-22 10:36:24 +0000190 *frame_pc = ptr[2];
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000191 return (const char*)ptr[1];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000192}
193
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000194static bool ThreadStackContainsAddress(ThreadContextBase *tctx_base,
195 void *addr) {
196 AsanThreadContext *tctx = static_cast<AsanThreadContext*>(tctx_base);
197 AsanThread *t = tctx->thread;
198 return (t && t->fake_stack().StackSize() &&
199 (t->fake_stack().AddrIsInFakeStack((uptr)addr) ||
200 t->AddrIsInStack((uptr)addr)));
201}
202
Alexey Samsonov89c13842013-03-20 09:23:28 +0000203AsanThread *GetCurrentThread() {
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000204 AsanThreadContext *context = (AsanThreadContext*)AsanTSDGet();
205 if (!context) {
206 if (SANITIZER_ANDROID) {
207 // On Android, libc constructor is called _after_ asan_init, and cleans up
208 // TSD. Try to figure out if this is still the main thread by the stack
209 // address. We are not entirely sure that we have correct main thread
Dmitry Vyukov195369b2013-03-22 07:29:59 +0000210 // limits, so only do this magic on Android, and only if the found thread
211 // is the main thread.
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000212 AsanThreadContext *tctx = GetThreadContextByTidLocked(0);
213 if (ThreadStackContainsAddress(tctx, &context)) {
214 SetCurrentThread(tctx->thread);
215 return tctx->thread;
216 }
Alexey Samsonov89c13842013-03-20 09:23:28 +0000217 }
Alexey Samsonov89c13842013-03-20 09:23:28 +0000218 return 0;
219 }
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000220 return context->thread;
Alexey Samsonov89c13842013-03-20 09:23:28 +0000221}
222
223void SetCurrentThread(AsanThread *t) {
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000224 CHECK(t->context());
Alexey Samsonov89c13842013-03-20 09:23:28 +0000225 if (flags()->verbosity >= 2) {
226 Report("SetCurrentThread: %p for thread %p\n",
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000227 t->context(), (void*)GetThreadSelf());
Alexey Samsonov89c13842013-03-20 09:23:28 +0000228 }
229 // Make sure we do not reset the current AsanThread.
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000230 CHECK_EQ(0, AsanTSDGet());
231 AsanTSDSet(t->context());
232 CHECK_EQ(t->context(), AsanTSDGet());
Alexey Samsonov89c13842013-03-20 09:23:28 +0000233}
234
235u32 GetCurrentTidOrInvalid() {
236 AsanThread *t = GetCurrentThread();
237 return t ? t->tid() : kInvalidTid;
238}
239
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000240AsanThread *FindThreadByStackAddress(uptr addr) {
241 asanThreadRegistry().CheckLocked();
242 AsanThreadContext *tctx = static_cast<AsanThreadContext *>(
243 asanThreadRegistry().FindThreadContextLocked(ThreadStackContainsAddress,
244 (void *)addr));
245 return tctx ? tctx->thread : 0;
246}
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000247} // namespace __asan
Sergey Matveevf1ac1a42013-05-21 13:40:13 +0000248
249// --- Implementation of LSan-specific functions --- {{{1
250namespace __lsan {
251bool GetThreadRangesLocked(uptr os_id, uptr *stack_begin, uptr *stack_end,
252 uptr *tls_begin, uptr *tls_end,
253 uptr *cache_begin, uptr *cache_end) {
254 // FIXME: Stub.
255 return false;
256}
257
258void LockThreadRegistry() {
259 __asan::asanThreadRegistry().Lock();
260}
261
262void UnlockThreadRegistry() {
263 __asan::asanThreadRegistry().Unlock();
264}
265} // namespace __lsan