blob: 2ef4b2f3c7c5ce82b8a578394e81f26111780dad [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 Samsonov55cdfc62012-01-17 06:35:31 +000016#include "asan_stack.h"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000017#include "asan_thread.h"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000018#include "asan_mapping.h"
Alexey Samsonove5931fd2012-06-07 07:13:46 +000019#include "sanitizer_common/sanitizer_common.h"
Alexey Samsonovdef1be92013-03-21 11:23:41 +000020#include "sanitizer_common/sanitizer_placement_new.h"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000021
Kostya Serebryany1e172b42011-11-30 01:07:02 +000022namespace __asan {
23
Alexey Samsonovdef1be92013-03-21 11:23:41 +000024// AsanThreadContext implementation.
Kostya Serebryany1e172b42011-11-30 01:07:02 +000025
Alexey Samsonovdef1be92013-03-21 11:23:41 +000026void AsanThreadContext::OnCreated(void *arg) {
27 CreateThreadContextArgs *args = static_cast<CreateThreadContextArgs*>(arg);
28 if (args->stack) {
29 internal_memcpy(&stack, args->stack, sizeof(stack));
30 }
31 thread = args->thread;
32 thread->set_context(this);
33}
34
35void AsanThreadContext::OnFinished() {
36 // Drop the link to the AsanThread object.
37 thread = 0;
38}
39
40static char thread_registry_placeholder[sizeof(ThreadRegistry)];
41static ThreadRegistry *asan_thread_registry;
42
43static ThreadContextBase *GetAsanThreadContext(u32 tid) {
44 void *mem = MmapOrDie(sizeof(AsanThreadContext), "AsanThreadContext");
45 return new(mem) AsanThreadContext(tid);
46}
47
48ThreadRegistry &asanThreadRegistry() {
49 static bool initialized;
50 // Don't worry about thread_safety - this should be called when there is
51 // a single thread.
52 if (!initialized) {
53 // Never reuse ASan threads: we store pointer to AsanThreadContext
54 // in TSD and can't reliably tell when no more TSD destructors will
55 // be called. It would be wrong to reuse AsanThreadContext for another
56 // thread before all TSD destructors will be called for it.
57 asan_thread_registry = new(thread_registry_placeholder) ThreadRegistry(
58 GetAsanThreadContext, kMaxNumberOfThreads, kMaxNumberOfThreads);
59 initialized = true;
60 }
61 return *asan_thread_registry;
62}
63
64AsanThreadContext *GetThreadContextByTidLocked(u32 tid) {
65 return static_cast<AsanThreadContext *>(
66 asanThreadRegistry().GetThreadLocked(tid));
67}
68
69// AsanThread implementation.
70
71AsanThread *AsanThread::Create(thread_callback_t start_routine,
72 void *arg) {
Kostya Serebryanyf67ec2b2012-11-23 15:38:49 +000073 uptr PageSize = GetPageSizeCached();
74 uptr size = RoundUpTo(sizeof(AsanThread), PageSize);
Alexey Samsonova25b3462012-06-06 16:15:07 +000075 AsanThread *thread = (AsanThread*)MmapOrDie(size, __FUNCTION__);
Alexey Samsonov55cdfc62012-01-17 06:35:31 +000076 thread->start_routine_ = start_routine;
77 thread->arg_ = arg;
Alexey Samsonovdef1be92013-03-21 11:23:41 +000078 thread->context_ = 0;
Alexey Samsonov55cdfc62012-01-17 06:35:31 +000079
80 return thread;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000081}
82
Alexey Samsonovdef1be92013-03-21 11:23:41 +000083void AsanThread::TSDDtor(void *tsd) {
84 AsanThreadContext *context = (AsanThreadContext*)tsd;
85 if (flags()->verbosity >= 1)
86 Report("T%d TSDDtor\n", context->tid);
87 if (context->thread)
88 context->thread->Destroy();
Kostya Serebryanyf58f9982012-02-07 00:27:15 +000089}
90
Kostya Serebryanya6b52262012-01-06 19:44:11 +000091void AsanThread::Destroy() {
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000092 if (flags()->verbosity >= 1) {
Kostya Serebryanyf58f9982012-02-07 00:27:15 +000093 Report("T%d exited\n", tid());
94 }
95
Alexey Samsonovdef1be92013-03-21 11:23:41 +000096 asanThreadRegistry().FinishThread(tid());
97 FlushToAccumulatedStats(&stats_);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000098 // We also clear the shadow on thread destruction because
99 // some code may still be executing in later TSD destructors
100 // and we don't want it to have any poisoned stack.
101 ClearShadowForThreadStack();
Alexey Samsonov55cdfc62012-01-17 06:35:31 +0000102 fake_stack().Cleanup();
Kostya Serebryanyf67ec2b2012-11-23 15:38:49 +0000103 uptr size = RoundUpTo(sizeof(AsanThread), GetPageSizeCached());
Alexey Samsonova25b3462012-06-06 16:15:07 +0000104 UnmapOrDie(this, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000105}
106
Kostya Serebryany69eca732011-12-16 19:13:35 +0000107void AsanThread::Init() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000108 SetThreadStackTopAndBottom();
Alexey Samsonov55cdfc62012-01-17 06:35:31 +0000109 CHECK(AddrIsInMem(stack_bottom_));
Kostya Serebryany541cfb12013-01-18 11:30:36 +0000110 CHECK(AddrIsInMem(stack_top_ - 1));
Alexey Samsonov55cdfc62012-01-17 06:35:31 +0000111 ClearShadowForThreadStack();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000112 if (flags()->verbosity >= 1) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000113 int local = 0;
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000114 Report("T%d: stack [%p,%p) size 0x%zx; local=%p\n",
Alexey Samsonov5bcca4e2012-06-06 10:46:00 +0000115 tid(), (void*)stack_bottom_, (void*)stack_top_,
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000116 stack_top_ - stack_bottom_, &local);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000117 }
Alexey Samsonov55cdfc62012-01-17 06:35:31 +0000118 fake_stack_.Init(stack_size());
Alexander Potapenko75b19eb2012-07-23 14:07:58 +0000119 AsanPlatformThreadInit();
Kostya Serebryany69eca732011-12-16 19:13:35 +0000120}
121
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000122thread_return_t AsanThread::ThreadStart(uptr os_id) {
Kostya Serebryany69eca732011-12-16 19:13:35 +0000123 Init();
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000124 asanThreadRegistry().StartThread(tid(), os_id, 0);
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000125 if (flags()->use_sigaltstack) SetAlternateSignalStack();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000126
127 if (!start_routine_) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000128 // start_routine_ == 0 if we're on the main thread or on one of the
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000129 // OS X libdispatch worker threads. But nobody is supposed to call
130 // ThreadStart() for the worker threads.
131 CHECK(tid() == 0);
132 return 0;
133 }
134
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000135 thread_return_t res = start_routine_(arg_);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000136 malloc_storage().CommitBack();
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000137 if (flags()->use_sigaltstack) UnsetAlternateSignalStack();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000138
Kostya Serebryanyaf344152012-01-11 02:03:16 +0000139 this->Destroy();
140
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000141 return res;
142}
143
Alexey Samsonove5931fd2012-06-07 07:13:46 +0000144void AsanThread::SetThreadStackTopAndBottom() {
145 GetThreadStackTopAndBottom(tid() == 0, &stack_top_, &stack_bottom_);
146 int local;
147 CHECK(AddrIsInStack((uptr)&local));
148}
149
Alexey Samsonov55cdfc62012-01-17 06:35:31 +0000150void AsanThread::ClearShadowForThreadStack() {
151 PoisonShadow(stack_bottom_, stack_top_ - stack_bottom_, 0);
152}
153
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000154const char *AsanThread::GetFrameNameByAddr(uptr addr, uptr *offset) {
155 uptr bottom = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000156 if (AddrIsInStack(addr)) {
157 bottom = stack_bottom();
158 } else {
159 bottom = fake_stack().AddrIsInFakeStack(addr);
160 CHECK(bottom);
Alexander Potapenkoe406c8c2012-11-15 15:24:42 +0000161 *offset = addr - bottom;
162 return (const char *)((uptr*)bottom)[1];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000163 }
Kostya Serebryany5af39e52012-11-21 12:38:58 +0000164 uptr aligned_addr = addr & ~(SANITIZER_WORDSIZE/8 - 1); // align addr.
Kostya Serebryanyee392552012-05-31 15:02:07 +0000165 u8 *shadow_ptr = (u8*)MemToShadow(aligned_addr);
166 u8 *shadow_bottom = (u8*)MemToShadow(bottom);
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000167
168 while (shadow_ptr >= shadow_bottom &&
Alexander Potapenkoe406c8c2012-11-15 15:24:42 +0000169 *shadow_ptr != kAsanStackLeftRedzoneMagic) {
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000170 shadow_ptr--;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000171 }
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000172
173 while (shadow_ptr >= shadow_bottom &&
Alexander Potapenkoe406c8c2012-11-15 15:24:42 +0000174 *shadow_ptr == kAsanStackLeftRedzoneMagic) {
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000175 shadow_ptr--;
176 }
177
178 if (shadow_ptr < shadow_bottom) {
Alexander Potapenkoe406c8c2012-11-15 15:24:42 +0000179 *offset = 0;
180 return "UNKNOWN";
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000181 }
182
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000183 uptr* ptr = (uptr*)SHADOW_TO_MEM((uptr)(shadow_ptr + 1));
Alexander Potapenkoe406c8c2012-11-15 15:24:42 +0000184 CHECK(ptr[0] == kCurrentStackFrameMagic);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000185 *offset = addr - (uptr)ptr;
Evgeniy Stepanov3972ea02012-05-12 12:33:10 +0000186 return (const char*)ptr[1];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000187}
188
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000189static bool ThreadStackContainsAddress(ThreadContextBase *tctx_base,
190 void *addr) {
191 AsanThreadContext *tctx = static_cast<AsanThreadContext*>(tctx_base);
192 AsanThread *t = tctx->thread;
193 return (t && t->fake_stack().StackSize() &&
194 (t->fake_stack().AddrIsInFakeStack((uptr)addr) ||
195 t->AddrIsInStack((uptr)addr)));
196}
197
Alexey Samsonov89c13842013-03-20 09:23:28 +0000198AsanThread *GetCurrentThread() {
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000199 AsanThreadContext *context = (AsanThreadContext*)AsanTSDGet();
200 if (!context) {
201 if (SANITIZER_ANDROID) {
202 // On Android, libc constructor is called _after_ asan_init, and cleans up
203 // TSD. Try to figure out if this is still the main thread by the stack
204 // address. We are not entirely sure that we have correct main thread
Dmitry Vyukov195369b2013-03-22 07:29:59 +0000205 // limits, so only do this magic on Android, and only if the found thread
206 // is the main thread.
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000207 AsanThreadContext *tctx = GetThreadContextByTidLocked(0);
208 if (ThreadStackContainsAddress(tctx, &context)) {
209 SetCurrentThread(tctx->thread);
210 return tctx->thread;
211 }
Alexey Samsonov89c13842013-03-20 09:23:28 +0000212 }
Alexey Samsonov89c13842013-03-20 09:23:28 +0000213 return 0;
214 }
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000215 return context->thread;
Alexey Samsonov89c13842013-03-20 09:23:28 +0000216}
217
218void SetCurrentThread(AsanThread *t) {
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000219 CHECK(t->context());
Alexey Samsonov89c13842013-03-20 09:23:28 +0000220 if (flags()->verbosity >= 2) {
221 Report("SetCurrentThread: %p for thread %p\n",
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000222 t->context(), (void*)GetThreadSelf());
Alexey Samsonov89c13842013-03-20 09:23:28 +0000223 }
224 // Make sure we do not reset the current AsanThread.
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000225 CHECK_EQ(0, AsanTSDGet());
226 AsanTSDSet(t->context());
227 CHECK_EQ(t->context(), AsanTSDGet());
Alexey Samsonov89c13842013-03-20 09:23:28 +0000228}
229
230u32 GetCurrentTidOrInvalid() {
231 AsanThread *t = GetCurrentThread();
232 return t ? t->tid() : kInvalidTid;
233}
234
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000235AsanThread *FindThreadByStackAddress(uptr addr) {
236 asanThreadRegistry().CheckLocked();
237 AsanThreadContext *tctx = static_cast<AsanThreadContext *>(
238 asanThreadRegistry().FindThreadContextLocked(ThreadStackContainsAddress,
239 (void *)addr));
240 return tctx ? tctx->thread : 0;
241}
242
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000243} // namespace __asan