blob: 5018b75f2947c2070957a2a814ee55c82c26011b [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
Kostya Serebryany40527a52013-06-03 14:49:25 +000042// MIPS requires aligned address
43static char thread_registry_placeholder[sizeof(ThreadRegistry)] ALIGNED(16);
Alexey Samsonovdef1be92013-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 Serebryanyf67ec2b2012-11-23 15:38:49 +000076 uptr PageSize = GetPageSizeCached();
77 uptr size = RoundUpTo(sizeof(AsanThread), PageSize);
Alexey Samsonova25b3462012-06-06 16:15:07 +000078 AsanThread *thread = (AsanThread*)MmapOrDie(size, __FUNCTION__);
Alexey Samsonov55cdfc62012-01-17 06:35:31 +000079 thread->start_routine_ = start_routine;
80 thread->arg_ = arg;
Alexey Samsonovdef1be92013-03-21 11:23:41 +000081 thread->context_ = 0;
Alexey Samsonov55cdfc62012-01-17 06:35:31 +000082
83 return thread;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000084}
85
Alexey Samsonovdef1be92013-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 Serebryanyf58f9982012-02-07 00:27:15 +000092}
93
Kostya Serebryanya6b52262012-01-06 19:44:11 +000094void AsanThread::Destroy() {
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000095 if (flags()->verbosity >= 1) {
Kostya Serebryanyf58f9982012-02-07 00:27:15 +000096 Report("T%d exited\n", tid());
97 }
98
Alexey Samsonovdef1be92013-03-21 11:23:41 +000099 asanThreadRegistry().FinishThread(tid());
100 FlushToAccumulatedStats(&stats_);
Kostya Serebryany1e172b42011-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 Matveev12d01ba2013-05-29 13:09:44 +0000104 ClearShadowForThreadStackAndTLS();
Alexey Samsonov55cdfc62012-01-17 06:35:31 +0000105 fake_stack().Cleanup();
Kostya Serebryanyf67ec2b2012-11-23 15:38:49 +0000106 uptr size = RoundUpTo(sizeof(AsanThread), GetPageSizeCached());
Alexey Samsonova25b3462012-06-06 16:15:07 +0000107 UnmapOrDie(this, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000108}
109
Kostya Serebryany69eca732011-12-16 19:13:35 +0000110void AsanThread::Init() {
Sergey Matveev12d01ba2013-05-29 13:09:44 +0000111 SetThreadStackAndTls();
Sergey Matveev5e719a72013-06-03 11:21:34 +0000112 lsan_disabled_ = 0;
Alexey Samsonov55cdfc62012-01-17 06:35:31 +0000113 CHECK(AddrIsInMem(stack_bottom_));
Kostya Serebryany541cfb12013-01-18 11:30:36 +0000114 CHECK(AddrIsInMem(stack_top_ - 1));
Sergey Matveev12d01ba2013-05-29 13:09:44 +0000115 ClearShadowForThreadStackAndTLS();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000116 if (flags()->verbosity >= 1) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000117 int local = 0;
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000118 Report("T%d: stack [%p,%p) size 0x%zx; local=%p\n",
Alexey Samsonov5bcca4e2012-06-06 10:46:00 +0000119 tid(), (void*)stack_bottom_, (void*)stack_top_,
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000120 stack_top_ - stack_bottom_, &local);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000121 }
Alexey Samsonov55cdfc62012-01-17 06:35:31 +0000122 fake_stack_.Init(stack_size());
Alexander Potapenko75b19eb2012-07-23 14:07:58 +0000123 AsanPlatformThreadInit();
Kostya Serebryany69eca732011-12-16 19:13:35 +0000124}
125
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000126thread_return_t AsanThread::ThreadStart(uptr os_id) {
Kostya Serebryany69eca732011-12-16 19:13:35 +0000127 Init();
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000128 asanThreadRegistry().StartThread(tid(), os_id, 0);
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000129 if (flags()->use_sigaltstack) SetAlternateSignalStack();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000130
131 if (!start_routine_) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000132 // start_routine_ == 0 if we're on the main thread or on one of the
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000133 // OS X libdispatch worker threads. But nobody is supposed to call
134 // ThreadStart() for the worker threads.
Kostya Serebryanya27bdf72013-04-05 14:40:25 +0000135 CHECK_EQ(tid(), 0);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000136 return 0;
137 }
138
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000139 thread_return_t res = start_routine_(arg_);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000140 malloc_storage().CommitBack();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000141 if (flags()->use_sigaltstack) UnsetAlternateSignalStack();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000142
Kostya Serebryanyaf344152012-01-11 02:03:16 +0000143 this->Destroy();
144
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000145 return res;
146}
147
Sergey Matveev12d01ba2013-05-29 13:09:44 +0000148void AsanThread::SetThreadStackAndTls() {
149 uptr stack_size = 0, tls_size = 0;
150 GetThreadStackAndTls(tid() == 0, &stack_bottom_, &stack_size, &tls_begin_,
151 &tls_size);
152 stack_top_ = stack_bottom_ + stack_size;
153 tls_end_ = tls_begin_ + tls_size;
154
Alexey Samsonove5931fd2012-06-07 07:13:46 +0000155 int local;
156 CHECK(AddrIsInStack((uptr)&local));
157}
158
Sergey Matveev12d01ba2013-05-29 13:09:44 +0000159void AsanThread::ClearShadowForThreadStackAndTLS() {
Alexey Samsonov55cdfc62012-01-17 06:35:31 +0000160 PoisonShadow(stack_bottom_, stack_top_ - stack_bottom_, 0);
Sergey Matveev12d01ba2013-05-29 13:09:44 +0000161 if (tls_begin_ != tls_end_)
162 PoisonShadow(tls_begin_, tls_end_ - tls_begin_, 0);
Alexey Samsonov55cdfc62012-01-17 06:35:31 +0000163}
164
Kostya Serebryany50f3daa2013-03-22 10:36:24 +0000165const char *AsanThread::GetFrameNameByAddr(uptr addr, uptr *offset,
166 uptr *frame_pc) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000167 uptr bottom = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000168 if (AddrIsInStack(addr)) {
169 bottom = stack_bottom();
170 } else {
171 bottom = fake_stack().AddrIsInFakeStack(addr);
172 CHECK(bottom);
Alexander Potapenkoe406c8c2012-11-15 15:24:42 +0000173 *offset = addr - bottom;
Kostya Serebryany50f3daa2013-03-22 10:36:24 +0000174 *frame_pc = ((uptr*)bottom)[2];
Alexander Potapenkoe406c8c2012-11-15 15:24:42 +0000175 return (const char *)((uptr*)bottom)[1];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000176 }
Kostya Serebryany5af39e52012-11-21 12:38:58 +0000177 uptr aligned_addr = addr & ~(SANITIZER_WORDSIZE/8 - 1); // align addr.
Kostya Serebryanyee392552012-05-31 15:02:07 +0000178 u8 *shadow_ptr = (u8*)MemToShadow(aligned_addr);
179 u8 *shadow_bottom = (u8*)MemToShadow(bottom);
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000180
181 while (shadow_ptr >= shadow_bottom &&
Alexander Potapenkoe406c8c2012-11-15 15:24:42 +0000182 *shadow_ptr != kAsanStackLeftRedzoneMagic) {
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000183 shadow_ptr--;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000184 }
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000185
186 while (shadow_ptr >= shadow_bottom &&
Alexander Potapenkoe406c8c2012-11-15 15:24:42 +0000187 *shadow_ptr == kAsanStackLeftRedzoneMagic) {
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000188 shadow_ptr--;
189 }
190
191 if (shadow_ptr < shadow_bottom) {
Alexander Potapenkoe406c8c2012-11-15 15:24:42 +0000192 *offset = 0;
193 return "UNKNOWN";
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000194 }
195
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000196 uptr* ptr = (uptr*)SHADOW_TO_MEM((uptr)(shadow_ptr + 1));
Alexander Potapenkoe406c8c2012-11-15 15:24:42 +0000197 CHECK(ptr[0] == kCurrentStackFrameMagic);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000198 *offset = addr - (uptr)ptr;
Kostya Serebryany50f3daa2013-03-22 10:36:24 +0000199 *frame_pc = ptr[2];
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000200 return (const char*)ptr[1];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000201}
202
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000203static bool ThreadStackContainsAddress(ThreadContextBase *tctx_base,
204 void *addr) {
205 AsanThreadContext *tctx = static_cast<AsanThreadContext*>(tctx_base);
206 AsanThread *t = tctx->thread;
207 return (t && t->fake_stack().StackSize() &&
208 (t->fake_stack().AddrIsInFakeStack((uptr)addr) ||
209 t->AddrIsInStack((uptr)addr)));
210}
211
Alexey Samsonov89c13842013-03-20 09:23:28 +0000212AsanThread *GetCurrentThread() {
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000213 AsanThreadContext *context = (AsanThreadContext*)AsanTSDGet();
214 if (!context) {
215 if (SANITIZER_ANDROID) {
216 // On Android, libc constructor is called _after_ asan_init, and cleans up
217 // TSD. Try to figure out if this is still the main thread by the stack
218 // address. We are not entirely sure that we have correct main thread
Dmitry Vyukov195369b2013-03-22 07:29:59 +0000219 // limits, so only do this magic on Android, and only if the found thread
220 // is the main thread.
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000221 AsanThreadContext *tctx = GetThreadContextByTidLocked(0);
222 if (ThreadStackContainsAddress(tctx, &context)) {
223 SetCurrentThread(tctx->thread);
224 return tctx->thread;
225 }
Alexey Samsonov89c13842013-03-20 09:23:28 +0000226 }
Alexey Samsonov89c13842013-03-20 09:23:28 +0000227 return 0;
228 }
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000229 return context->thread;
Alexey Samsonov89c13842013-03-20 09:23:28 +0000230}
231
232void SetCurrentThread(AsanThread *t) {
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000233 CHECK(t->context());
Alexey Samsonov89c13842013-03-20 09:23:28 +0000234 if (flags()->verbosity >= 2) {
235 Report("SetCurrentThread: %p for thread %p\n",
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000236 t->context(), (void*)GetThreadSelf());
Alexey Samsonov89c13842013-03-20 09:23:28 +0000237 }
238 // Make sure we do not reset the current AsanThread.
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000239 CHECK_EQ(0, AsanTSDGet());
240 AsanTSDSet(t->context());
241 CHECK_EQ(t->context(), AsanTSDGet());
Alexey Samsonov89c13842013-03-20 09:23:28 +0000242}
243
244u32 GetCurrentTidOrInvalid() {
245 AsanThread *t = GetCurrentThread();
246 return t ? t->tid() : kInvalidTid;
247}
248
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000249AsanThread *FindThreadByStackAddress(uptr addr) {
250 asanThreadRegistry().CheckLocked();
251 AsanThreadContext *tctx = static_cast<AsanThreadContext *>(
252 asanThreadRegistry().FindThreadContextLocked(ThreadStackContainsAddress,
253 (void *)addr));
254 return tctx ? tctx->thread : 0;
255}
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000256} // namespace __asan
Sergey Matveevf1ac1a42013-05-21 13:40:13 +0000257
258// --- Implementation of LSan-specific functions --- {{{1
259namespace __lsan {
260bool GetThreadRangesLocked(uptr os_id, uptr *stack_begin, uptr *stack_end,
261 uptr *tls_begin, uptr *tls_end,
262 uptr *cache_begin, uptr *cache_end) {
Sergey Matveev12d01ba2013-05-29 13:09:44 +0000263 __asan::AsanThreadContext *context = static_cast<__asan::AsanThreadContext *>(
264 __asan::asanThreadRegistry().FindThreadContextByOsIDLocked(os_id));
265 if (!context) return false;
266 __asan::AsanThread *t = context->thread;
267 if (!t) return false;
268 *stack_begin = t->stack_bottom();
269 *stack_end = t->stack_top();
270 *tls_begin = t->tls_begin();
271 *tls_end = t->tls_end();
272 // ASan doesn't keep allocator caches in TLS, so these are unused.
273 *cache_begin = 0;
274 *cache_end = 0;
275 return true;
Sergey Matveevf1ac1a42013-05-21 13:40:13 +0000276}
277
278void LockThreadRegistry() {
279 __asan::asanThreadRegistry().Lock();
280}
281
282void UnlockThreadRegistry() {
283 __asan::asanThreadRegistry().Unlock();
284}
285} // namespace __lsan