Alexey Samsonov | e5f5895 | 2012-06-04 13:50:10 +0000 | [diff] [blame] | 1 | //===-- asan_thread.cc ----------------------------------------------------===// |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 2 | // |
| 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 Samsonov | 7e84349 | 2013-03-28 15:42:43 +0000 | [diff] [blame^] | 16 | #include "asan_poisoning.h" |
Alexey Samsonov | 55cdfc6 | 2012-01-17 06:35:31 +0000 | [diff] [blame] | 17 | #include "asan_stack.h" |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 18 | #include "asan_thread.h" |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 19 | #include "asan_mapping.h" |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 20 | #include "sanitizer_common/sanitizer_common.h" |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 21 | #include "sanitizer_common/sanitizer_placement_new.h" |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 22 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 23 | namespace __asan { |
| 24 | |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 25 | // AsanThreadContext implementation. |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 26 | |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 27 | void 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 | |
| 36 | void AsanThreadContext::OnFinished() { |
| 37 | // Drop the link to the AsanThread object. |
| 38 | thread = 0; |
| 39 | } |
| 40 | |
| 41 | static char thread_registry_placeholder[sizeof(ThreadRegistry)]; |
| 42 | static ThreadRegistry *asan_thread_registry; |
| 43 | |
| 44 | static ThreadContextBase *GetAsanThreadContext(u32 tid) { |
| 45 | void *mem = MmapOrDie(sizeof(AsanThreadContext), "AsanThreadContext"); |
| 46 | return new(mem) AsanThreadContext(tid); |
| 47 | } |
| 48 | |
| 49 | ThreadRegistry &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 | |
| 65 | AsanThreadContext *GetThreadContextByTidLocked(u32 tid) { |
| 66 | return static_cast<AsanThreadContext *>( |
| 67 | asanThreadRegistry().GetThreadLocked(tid)); |
| 68 | } |
| 69 | |
| 70 | // AsanThread implementation. |
| 71 | |
| 72 | AsanThread *AsanThread::Create(thread_callback_t start_routine, |
| 73 | void *arg) { |
Kostya Serebryany | f67ec2b | 2012-11-23 15:38:49 +0000 | [diff] [blame] | 74 | uptr PageSize = GetPageSizeCached(); |
| 75 | uptr size = RoundUpTo(sizeof(AsanThread), PageSize); |
Alexey Samsonov | a25b346 | 2012-06-06 16:15:07 +0000 | [diff] [blame] | 76 | AsanThread *thread = (AsanThread*)MmapOrDie(size, __FUNCTION__); |
Alexey Samsonov | 55cdfc6 | 2012-01-17 06:35:31 +0000 | [diff] [blame] | 77 | thread->start_routine_ = start_routine; |
| 78 | thread->arg_ = arg; |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 79 | thread->context_ = 0; |
Alexey Samsonov | 55cdfc6 | 2012-01-17 06:35:31 +0000 | [diff] [blame] | 80 | |
| 81 | return thread; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 84 | void 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 Serebryany | f58f998 | 2012-02-07 00:27:15 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Kostya Serebryany | a6b5226 | 2012-01-06 19:44:11 +0000 | [diff] [blame] | 92 | void AsanThread::Destroy() { |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 93 | if (flags()->verbosity >= 1) { |
Kostya Serebryany | f58f998 | 2012-02-07 00:27:15 +0000 | [diff] [blame] | 94 | Report("T%d exited\n", tid()); |
| 95 | } |
| 96 | |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 97 | asanThreadRegistry().FinishThread(tid()); |
| 98 | FlushToAccumulatedStats(&stats_); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 99 | // 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 Samsonov | 55cdfc6 | 2012-01-17 06:35:31 +0000 | [diff] [blame] | 103 | fake_stack().Cleanup(); |
Kostya Serebryany | f67ec2b | 2012-11-23 15:38:49 +0000 | [diff] [blame] | 104 | uptr size = RoundUpTo(sizeof(AsanThread), GetPageSizeCached()); |
Alexey Samsonov | a25b346 | 2012-06-06 16:15:07 +0000 | [diff] [blame] | 105 | UnmapOrDie(this, size); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Kostya Serebryany | 69eca73 | 2011-12-16 19:13:35 +0000 | [diff] [blame] | 108 | void AsanThread::Init() { |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 109 | SetThreadStackTopAndBottom(); |
Alexey Samsonov | 55cdfc6 | 2012-01-17 06:35:31 +0000 | [diff] [blame] | 110 | CHECK(AddrIsInMem(stack_bottom_)); |
Kostya Serebryany | 541cfb1 | 2013-01-18 11:30:36 +0000 | [diff] [blame] | 111 | CHECK(AddrIsInMem(stack_top_ - 1)); |
Alexey Samsonov | 55cdfc6 | 2012-01-17 06:35:31 +0000 | [diff] [blame] | 112 | ClearShadowForThreadStack(); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 113 | if (flags()->verbosity >= 1) { |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 114 | int local = 0; |
Evgeniy Stepanov | 739eb79 | 2012-03-21 11:32:46 +0000 | [diff] [blame] | 115 | Report("T%d: stack [%p,%p) size 0x%zx; local=%p\n", |
Alexey Samsonov | 5bcca4e | 2012-06-06 10:46:00 +0000 | [diff] [blame] | 116 | tid(), (void*)stack_bottom_, (void*)stack_top_, |
Kostya Serebryany | a7e760a | 2012-01-09 19:18:27 +0000 | [diff] [blame] | 117 | stack_top_ - stack_bottom_, &local); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 118 | } |
Alexey Samsonov | 55cdfc6 | 2012-01-17 06:35:31 +0000 | [diff] [blame] | 119 | fake_stack_.Init(stack_size()); |
Alexander Potapenko | 75b19eb | 2012-07-23 14:07:58 +0000 | [diff] [blame] | 120 | AsanPlatformThreadInit(); |
Kostya Serebryany | 69eca73 | 2011-12-16 19:13:35 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 123 | thread_return_t AsanThread::ThreadStart(uptr os_id) { |
Kostya Serebryany | 69eca73 | 2011-12-16 19:13:35 +0000 | [diff] [blame] | 124 | Init(); |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 125 | asanThreadRegistry().StartThread(tid(), os_id, 0); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 126 | if (flags()->use_sigaltstack) SetAlternateSignalStack(); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 127 | |
| 128 | if (!start_routine_) { |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 129 | // start_routine_ == 0 if we're on the main thread or on one of the |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 130 | // OS X libdispatch worker threads. But nobody is supposed to call |
| 131 | // ThreadStart() for the worker threads. |
| 132 | CHECK(tid() == 0); |
| 133 | return 0; |
| 134 | } |
| 135 | |
Timur Iskhodzhanov | 600972e | 2012-02-24 15:28:43 +0000 | [diff] [blame] | 136 | thread_return_t res = start_routine_(arg_); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 137 | malloc_storage().CommitBack(); |
Alexey Samsonov | cb8c4dc | 2012-07-09 14:36:04 +0000 | [diff] [blame] | 138 | if (flags()->use_sigaltstack) UnsetAlternateSignalStack(); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 139 | |
Kostya Serebryany | af34415 | 2012-01-11 02:03:16 +0000 | [diff] [blame] | 140 | this->Destroy(); |
| 141 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 142 | return res; |
| 143 | } |
| 144 | |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 145 | void AsanThread::SetThreadStackTopAndBottom() { |
| 146 | GetThreadStackTopAndBottom(tid() == 0, &stack_top_, &stack_bottom_); |
| 147 | int local; |
| 148 | CHECK(AddrIsInStack((uptr)&local)); |
| 149 | } |
| 150 | |
Alexey Samsonov | 55cdfc6 | 2012-01-17 06:35:31 +0000 | [diff] [blame] | 151 | void AsanThread::ClearShadowForThreadStack() { |
| 152 | PoisonShadow(stack_bottom_, stack_top_ - stack_bottom_, 0); |
| 153 | } |
| 154 | |
Kostya Serebryany | 50f3daa | 2013-03-22 10:36:24 +0000 | [diff] [blame] | 155 | const char *AsanThread::GetFrameNameByAddr(uptr addr, uptr *offset, |
| 156 | uptr *frame_pc) { |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 157 | uptr bottom = 0; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 158 | if (AddrIsInStack(addr)) { |
| 159 | bottom = stack_bottom(); |
| 160 | } else { |
| 161 | bottom = fake_stack().AddrIsInFakeStack(addr); |
| 162 | CHECK(bottom); |
Alexander Potapenko | e406c8c | 2012-11-15 15:24:42 +0000 | [diff] [blame] | 163 | *offset = addr - bottom; |
Kostya Serebryany | 50f3daa | 2013-03-22 10:36:24 +0000 | [diff] [blame] | 164 | *frame_pc = ((uptr*)bottom)[2]; |
Alexander Potapenko | e406c8c | 2012-11-15 15:24:42 +0000 | [diff] [blame] | 165 | return (const char *)((uptr*)bottom)[1]; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 166 | } |
Kostya Serebryany | 5af39e5 | 2012-11-21 12:38:58 +0000 | [diff] [blame] | 167 | uptr aligned_addr = addr & ~(SANITIZER_WORDSIZE/8 - 1); // align addr. |
Kostya Serebryany | ee39255 | 2012-05-31 15:02:07 +0000 | [diff] [blame] | 168 | u8 *shadow_ptr = (u8*)MemToShadow(aligned_addr); |
| 169 | u8 *shadow_bottom = (u8*)MemToShadow(bottom); |
Evgeniy Stepanov | 3972ea0 | 2012-05-12 12:33:10 +0000 | [diff] [blame] | 170 | |
| 171 | while (shadow_ptr >= shadow_bottom && |
Alexander Potapenko | e406c8c | 2012-11-15 15:24:42 +0000 | [diff] [blame] | 172 | *shadow_ptr != kAsanStackLeftRedzoneMagic) { |
Evgeniy Stepanov | 3972ea0 | 2012-05-12 12:33:10 +0000 | [diff] [blame] | 173 | shadow_ptr--; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 174 | } |
Evgeniy Stepanov | 3972ea0 | 2012-05-12 12:33:10 +0000 | [diff] [blame] | 175 | |
| 176 | while (shadow_ptr >= shadow_bottom && |
Alexander Potapenko | e406c8c | 2012-11-15 15:24:42 +0000 | [diff] [blame] | 177 | *shadow_ptr == kAsanStackLeftRedzoneMagic) { |
Evgeniy Stepanov | 3972ea0 | 2012-05-12 12:33:10 +0000 | [diff] [blame] | 178 | shadow_ptr--; |
| 179 | } |
| 180 | |
| 181 | if (shadow_ptr < shadow_bottom) { |
Alexander Potapenko | e406c8c | 2012-11-15 15:24:42 +0000 | [diff] [blame] | 182 | *offset = 0; |
| 183 | return "UNKNOWN"; |
Evgeniy Stepanov | 3972ea0 | 2012-05-12 12:33:10 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 186 | uptr* ptr = (uptr*)SHADOW_TO_MEM((uptr)(shadow_ptr + 1)); |
Alexander Potapenko | e406c8c | 2012-11-15 15:24:42 +0000 | [diff] [blame] | 187 | CHECK(ptr[0] == kCurrentStackFrameMagic); |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 188 | *offset = addr - (uptr)ptr; |
Kostya Serebryany | 50f3daa | 2013-03-22 10:36:24 +0000 | [diff] [blame] | 189 | *frame_pc = ptr[2]; |
Evgeniy Stepanov | 3972ea0 | 2012-05-12 12:33:10 +0000 | [diff] [blame] | 190 | return (const char*)ptr[1]; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 193 | static 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 Samsonov | 89c1384 | 2013-03-20 09:23:28 +0000 | [diff] [blame] | 202 | AsanThread *GetCurrentThread() { |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 203 | 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 Vyukov | 195369b | 2013-03-22 07:29:59 +0000 | [diff] [blame] | 209 | // limits, so only do this magic on Android, and only if the found thread |
| 210 | // is the main thread. |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 211 | AsanThreadContext *tctx = GetThreadContextByTidLocked(0); |
| 212 | if (ThreadStackContainsAddress(tctx, &context)) { |
| 213 | SetCurrentThread(tctx->thread); |
| 214 | return tctx->thread; |
| 215 | } |
Alexey Samsonov | 89c1384 | 2013-03-20 09:23:28 +0000 | [diff] [blame] | 216 | } |
Alexey Samsonov | 89c1384 | 2013-03-20 09:23:28 +0000 | [diff] [blame] | 217 | return 0; |
| 218 | } |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 219 | return context->thread; |
Alexey Samsonov | 89c1384 | 2013-03-20 09:23:28 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | void SetCurrentThread(AsanThread *t) { |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 223 | CHECK(t->context()); |
Alexey Samsonov | 89c1384 | 2013-03-20 09:23:28 +0000 | [diff] [blame] | 224 | if (flags()->verbosity >= 2) { |
| 225 | Report("SetCurrentThread: %p for thread %p\n", |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 226 | t->context(), (void*)GetThreadSelf()); |
Alexey Samsonov | 89c1384 | 2013-03-20 09:23:28 +0000 | [diff] [blame] | 227 | } |
| 228 | // Make sure we do not reset the current AsanThread. |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 229 | CHECK_EQ(0, AsanTSDGet()); |
| 230 | AsanTSDSet(t->context()); |
| 231 | CHECK_EQ(t->context(), AsanTSDGet()); |
Alexey Samsonov | 89c1384 | 2013-03-20 09:23:28 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | u32 GetCurrentTidOrInvalid() { |
| 235 | AsanThread *t = GetCurrentThread(); |
| 236 | return t ? t->tid() : kInvalidTid; |
| 237 | } |
| 238 | |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 239 | AsanThread *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 Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 247 | } // namespace __asan |