blob: 64d70d65cbc7f3d517f2e304a6f2c505c19c41a1 [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 Serebryany1e172b42011-11-30 01:07:02 +000022
Kostya Serebryany1e172b42011-11-30 01:07:02 +000023namespace __asan {
24
Alexey Samsonovdef1be92013-03-21 11:23:41 +000025// AsanThreadContext implementation.
Kostya Serebryany1e172b42011-11-30 01:07:02 +000026
Alexey Samsonovdef1be92013-03-21 11:23:41 +000027void AsanThreadContext::OnCreated(void *arg) {
28 CreateThreadContextArgs *args = static_cast<CreateThreadContextArgs*>(arg);
29 if (args->stack) {
30 internal_memcpy(&stack, args->stack, sizeof(stack));
31 }
32 thread = args->thread;
33 thread->set_context(this);
34}
35
36void AsanThreadContext::OnFinished() {
37 // Drop the link to the AsanThread object.
38 thread = 0;
39}
40
41static char thread_registry_placeholder[sizeof(ThreadRegistry)];
42static ThreadRegistry *asan_thread_registry;
43
44static ThreadContextBase *GetAsanThreadContext(u32 tid) {
45 void *mem = MmapOrDie(sizeof(AsanThreadContext), "AsanThreadContext");
46 return new(mem) AsanThreadContext(tid);
47}
48
49ThreadRegistry &asanThreadRegistry() {
50 static bool initialized;
51 // Don't worry about thread_safety - this should be called when there is
52 // a single thread.
53 if (!initialized) {
54 // Never reuse ASan threads: we store pointer to AsanThreadContext
55 // in TSD and can't reliably tell when no more TSD destructors will
56 // be called. It would be wrong to reuse AsanThreadContext for another
57 // thread before all TSD destructors will be called for it.
58 asan_thread_registry = new(thread_registry_placeholder) ThreadRegistry(
59 GetAsanThreadContext, kMaxNumberOfThreads, kMaxNumberOfThreads);
60 initialized = true;
61 }
62 return *asan_thread_registry;
63}
64
65AsanThreadContext *GetThreadContextByTidLocked(u32 tid) {
66 return static_cast<AsanThreadContext *>(
67 asanThreadRegistry().GetThreadLocked(tid));
68}
69
70// AsanThread implementation.
71
72AsanThread *AsanThread::Create(thread_callback_t start_routine,
73 void *arg) {
Kostya Serebryanyf67ec2b2012-11-23 15:38:49 +000074 uptr PageSize = GetPageSizeCached();
75 uptr size = RoundUpTo(sizeof(AsanThread), PageSize);
Alexey Samsonova25b3462012-06-06 16:15:07 +000076 AsanThread *thread = (AsanThread*)MmapOrDie(size, __FUNCTION__);
Alexey Samsonov55cdfc62012-01-17 06:35:31 +000077 thread->start_routine_ = start_routine;
78 thread->arg_ = arg;
Alexey Samsonovdef1be92013-03-21 11:23:41 +000079 thread->context_ = 0;
Alexey Samsonov55cdfc62012-01-17 06:35:31 +000080
81 return thread;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000082}
83
Alexey Samsonovdef1be92013-03-21 11:23:41 +000084void AsanThread::TSDDtor(void *tsd) {
85 AsanThreadContext *context = (AsanThreadContext*)tsd;
86 if (flags()->verbosity >= 1)
87 Report("T%d TSDDtor\n", context->tid);
88 if (context->thread)
89 context->thread->Destroy();
Kostya Serebryanyf58f9982012-02-07 00:27:15 +000090}
91
Kostya Serebryanya6b52262012-01-06 19:44:11 +000092void AsanThread::Destroy() {
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000093 if (flags()->verbosity >= 1) {
Kostya Serebryanyf58f9982012-02-07 00:27:15 +000094 Report("T%d exited\n", tid());
95 }
96
Alexey Samsonovdef1be92013-03-21 11:23:41 +000097 asanThreadRegistry().FinishThread(tid());
98 FlushToAccumulatedStats(&stats_);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000099 // We also clear the shadow on thread destruction because
100 // some code may still be executing in later TSD destructors
101 // and we don't want it to have any poisoned stack.
102 ClearShadowForThreadStack();
Alexey Samsonov55cdfc62012-01-17 06:35:31 +0000103 fake_stack().Cleanup();
Kostya Serebryanyf67ec2b2012-11-23 15:38:49 +0000104 uptr size = RoundUpTo(sizeof(AsanThread), GetPageSizeCached());
Alexey Samsonova25b3462012-06-06 16:15:07 +0000105 UnmapOrDie(this, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000106}
107
Kostya Serebryany69eca732011-12-16 19:13:35 +0000108void AsanThread::Init() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000109 SetThreadStackTopAndBottom();
Alexey Samsonov55cdfc62012-01-17 06:35:31 +0000110 CHECK(AddrIsInMem(stack_bottom_));
Kostya Serebryany541cfb12013-01-18 11:30:36 +0000111 CHECK(AddrIsInMem(stack_top_ - 1));
Alexey Samsonov55cdfc62012-01-17 06:35:31 +0000112 ClearShadowForThreadStack();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000113 if (flags()->verbosity >= 1) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000114 int local = 0;
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000115 Report("T%d: stack [%p,%p) size 0x%zx; local=%p\n",
Alexey Samsonov5bcca4e2012-06-06 10:46:00 +0000116 tid(), (void*)stack_bottom_, (void*)stack_top_,
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000117 stack_top_ - stack_bottom_, &local);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000118 }
Alexey Samsonov55cdfc62012-01-17 06:35:31 +0000119 fake_stack_.Init(stack_size());
Alexander Potapenko75b19eb2012-07-23 14:07:58 +0000120 AsanPlatformThreadInit();
Kostya Serebryany69eca732011-12-16 19:13:35 +0000121}
122
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000123thread_return_t AsanThread::ThreadStart(uptr os_id) {
Kostya Serebryany69eca732011-12-16 19:13:35 +0000124 Init();
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000125 asanThreadRegistry().StartThread(tid(), os_id, 0);
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000126 if (flags()->use_sigaltstack) SetAlternateSignalStack();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000127
128 if (!start_routine_) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000129 // start_routine_ == 0 if we're on the main thread or on one of the
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000130 // OS X libdispatch worker threads. But nobody is supposed to call
131 // ThreadStart() for the worker threads.
Kostya Serebryanya27bdf72013-04-05 14:40:25 +0000132 CHECK_EQ(tid(), 0);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000133 return 0;
134 }
135
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000136 thread_return_t res = start_routine_(arg_);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000137 malloc_storage().CommitBack();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000138 if (flags()->use_sigaltstack) UnsetAlternateSignalStack();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000139
Kostya Serebryanyaf344152012-01-11 02:03:16 +0000140 this->Destroy();
141
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000142 return res;
143}
144
Alexey Samsonove5931fd2012-06-07 07:13:46 +0000145void AsanThread::SetThreadStackTopAndBottom() {
146 GetThreadStackTopAndBottom(tid() == 0, &stack_top_, &stack_bottom_);
147 int local;
148 CHECK(AddrIsInStack((uptr)&local));
149}
150
Alexey Samsonov55cdfc62012-01-17 06:35:31 +0000151void AsanThread::ClearShadowForThreadStack() {
152 PoisonShadow(stack_bottom_, stack_top_ - stack_bottom_, 0);
153}
154
Kostya Serebryany50f3daa2013-03-22 10:36:24 +0000155const char *AsanThread::GetFrameNameByAddr(uptr addr, uptr *offset,
156 uptr *frame_pc) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000157 uptr bottom = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000158 if (AddrIsInStack(addr)) {
159 bottom = stack_bottom();
160 } else {
161 bottom = fake_stack().AddrIsInFakeStack(addr);
162 CHECK(bottom);
Alexander Potapenkoe406c8c2012-11-15 15:24:42 +0000163 *offset = addr - bottom;
Kostya Serebryany50f3daa2013-03-22 10:36:24 +0000164 *frame_pc = ((uptr*)bottom)[2];
Alexander Potapenkoe406c8c2012-11-15 15:24:42 +0000165 return (const char *)((uptr*)bottom)[1];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000166 }
Kostya Serebryany5af39e52012-11-21 12:38:58 +0000167 uptr aligned_addr = addr & ~(SANITIZER_WORDSIZE/8 - 1); // align addr.
Kostya Serebryanyee392552012-05-31 15:02:07 +0000168 u8 *shadow_ptr = (u8*)MemToShadow(aligned_addr);
169 u8 *shadow_bottom = (u8*)MemToShadow(bottom);
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000170
171 while (shadow_ptr >= shadow_bottom &&
Alexander Potapenkoe406c8c2012-11-15 15:24:42 +0000172 *shadow_ptr != kAsanStackLeftRedzoneMagic) {
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000173 shadow_ptr--;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000174 }
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000175
176 while (shadow_ptr >= shadow_bottom &&
Alexander Potapenkoe406c8c2012-11-15 15:24:42 +0000177 *shadow_ptr == kAsanStackLeftRedzoneMagic) {
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000178 shadow_ptr--;
179 }
180
181 if (shadow_ptr < shadow_bottom) {
Alexander Potapenkoe406c8c2012-11-15 15:24:42 +0000182 *offset = 0;
183 return "UNKNOWN";
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000184 }
185
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000186 uptr* ptr = (uptr*)SHADOW_TO_MEM((uptr)(shadow_ptr + 1));
Alexander Potapenkoe406c8c2012-11-15 15:24:42 +0000187 CHECK(ptr[0] == kCurrentStackFrameMagic);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000188 *offset = addr - (uptr)ptr;
Kostya Serebryany50f3daa2013-03-22 10:36:24 +0000189 *frame_pc = ptr[2];
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000190 return (const char*)ptr[1];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000191}
192
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000193static bool ThreadStackContainsAddress(ThreadContextBase *tctx_base,
194 void *addr) {
195 AsanThreadContext *tctx = static_cast<AsanThreadContext*>(tctx_base);
196 AsanThread *t = tctx->thread;
197 return (t && t->fake_stack().StackSize() &&
198 (t->fake_stack().AddrIsInFakeStack((uptr)addr) ||
199 t->AddrIsInStack((uptr)addr)));
200}
201
Alexey Samsonov89c13842013-03-20 09:23:28 +0000202AsanThread *GetCurrentThread() {
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000203 AsanThreadContext *context = (AsanThreadContext*)AsanTSDGet();
204 if (!context) {
205 if (SANITIZER_ANDROID) {
206 // On Android, libc constructor is called _after_ asan_init, and cleans up
207 // TSD. Try to figure out if this is still the main thread by the stack
208 // address. We are not entirely sure that we have correct main thread
Dmitry Vyukov195369b2013-03-22 07:29:59 +0000209 // limits, so only do this magic on Android, and only if the found thread
210 // is the main thread.
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000211 AsanThreadContext *tctx = GetThreadContextByTidLocked(0);
212 if (ThreadStackContainsAddress(tctx, &context)) {
213 SetCurrentThread(tctx->thread);
214 return tctx->thread;
215 }
Alexey Samsonov89c13842013-03-20 09:23:28 +0000216 }
Alexey Samsonov89c13842013-03-20 09:23:28 +0000217 return 0;
218 }
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000219 return context->thread;
Alexey Samsonov89c13842013-03-20 09:23:28 +0000220}
221
222void SetCurrentThread(AsanThread *t) {
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000223 CHECK(t->context());
Alexey Samsonov89c13842013-03-20 09:23:28 +0000224 if (flags()->verbosity >= 2) {
225 Report("SetCurrentThread: %p for thread %p\n",
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000226 t->context(), (void*)GetThreadSelf());
Alexey Samsonov89c13842013-03-20 09:23:28 +0000227 }
228 // Make sure we do not reset the current AsanThread.
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000229 CHECK_EQ(0, AsanTSDGet());
230 AsanTSDSet(t->context());
231 CHECK_EQ(t->context(), AsanTSDGet());
Alexey Samsonov89c13842013-03-20 09:23:28 +0000232}
233
234u32 GetCurrentTidOrInvalid() {
235 AsanThread *t = GetCurrentThread();
236 return t ? t->tid() : kInvalidTid;
237}
238
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000239AsanThread *FindThreadByStackAddress(uptr addr) {
240 asanThreadRegistry().CheckLocked();
241 AsanThreadContext *tctx = static_cast<AsanThreadContext *>(
242 asanThreadRegistry().FindThreadContextLocked(ThreadStackContainsAddress,
243 (void *)addr));
244 return tctx ? tctx->thread : 0;
245}
246
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000247} // namespace __asan