1 //===-- asan_thread.cc ----------------------------------------------------===// 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" 16 #include "asan_poisoning.h" 17 #include "asan_stack.h" 18 #include "asan_thread.h" 19 #include "asan_mapping.h" 20 #include "sanitizer_common/sanitizer_common.h" 21 #include "sanitizer_common/sanitizer_placement_new.h" 22 #include "sanitizer_common/sanitizer_stackdepot.h" 23 #include "sanitizer_common/sanitizer_tls_get_addr.h" 24 #include "lsan/lsan_common.h" 25 26 namespace __asan { 27 28 // AsanThreadContext implementation. 29 30 struct CreateThreadContextArgs { 31 AsanThread *thread; 32 StackTrace *stack; 33 }; 34 35 void AsanThreadContext::OnCreated(void *arg) { 36 CreateThreadContextArgs *args = static_cast<CreateThreadContextArgs*>(arg); 37 if (args->stack) 38 stack_id = StackDepotPut(*args->stack); 39 thread = args->thread; 40 thread->set_context(this); 41 } 42 43 void AsanThreadContext::OnFinished() { 44 // Drop the link to the AsanThread object. 45 thread = 0; 46 } 47 48 // MIPS requires aligned address 49 static ALIGNED(16) char thread_registry_placeholder[sizeof(ThreadRegistry)]; 50 static ThreadRegistry *asan_thread_registry; 51 52 static BlockingMutex mu_for_thread_context(LINKER_INITIALIZED); 53 static LowLevelAllocator allocator_for_thread_context; 54 55 static ThreadContextBase *GetAsanThreadContext(u32 tid) { 56 BlockingMutexLock lock(&mu_for_thread_context); 57 return new(allocator_for_thread_context) AsanThreadContext(tid); 58 } 59 60 ThreadRegistry &asanThreadRegistry() { 61 static bool initialized; 62 // Don't worry about thread_safety - this should be called when there is 63 // a single thread. 64 if (!initialized) { 65 // Never reuse ASan threads: we store pointer to AsanThreadContext 66 // in TSD and can't reliably tell when no more TSD destructors will 67 // be called. It would be wrong to reuse AsanThreadContext for another 68 // thread before all TSD destructors will be called for it. 69 asan_thread_registry = new(thread_registry_placeholder) ThreadRegistry( 70 GetAsanThreadContext, kMaxNumberOfThreads, kMaxNumberOfThreads); 71 initialized = true; 72 } 73 return *asan_thread_registry; 74 } 75 76 AsanThreadContext *GetThreadContextByTidLocked(u32 tid) { 77 return static_cast<AsanThreadContext *>( 78 asanThreadRegistry().GetThreadLocked(tid)); 79 } 80 81 // AsanThread implementation. 82 83 AsanThread *AsanThread::Create(thread_callback_t start_routine, void *arg, 84 u32 parent_tid, StackTrace *stack, 85 bool detached) { 86 uptr PageSize = GetPageSizeCached(); 87 uptr size = RoundUpTo(sizeof(AsanThread), PageSize); 88 AsanThread *thread = (AsanThread*)MmapOrDie(size, __func__); 89 thread->start_routine_ = start_routine; 90 thread->arg_ = arg; 91 CreateThreadContextArgs args = { thread, stack }; 92 asanThreadRegistry().CreateThread(*reinterpret_cast<uptr *>(thread), detached, 93 parent_tid, &args); 94 95 return thread; 96 } 97 98 void AsanThread::TSDDtor(void *tsd) { 99 AsanThreadContext *context = (AsanThreadContext*)tsd; 100 VReport(1, "T%d TSDDtor\n", context->tid); 101 if (context->thread) 102 context->thread->Destroy(); 103 } 104 105 void AsanThread::Destroy() { 106 int tid = this->tid(); 107 VReport(1, "T%d exited\n", tid); 108 109 malloc_storage().CommitBack(); 110 if (common_flags()->use_sigaltstack) UnsetAlternateSignalStack(); 111 asanThreadRegistry().FinishThread(tid); 112 FlushToDeadThreadStats(&stats_); 113 // We also clear the shadow on thread destruction because 114 // some code may still be executing in later TSD destructors 115 // and we don't want it to have any poisoned stack. 116 ClearShadowForThreadStackAndTLS(); 117 DeleteFakeStack(tid); 118 uptr size = RoundUpTo(sizeof(AsanThread), GetPageSizeCached()); 119 UnmapOrDie(this, size); 120 DTLS_Destroy(); 121 } 122 123 // We want to create the FakeStack lazyly on the first use, but not eralier 124 // than the stack size is known and the procedure has to be async-signal safe. 125 FakeStack *AsanThread::AsyncSignalSafeLazyInitFakeStack() { 126 uptr stack_size = this->stack_size(); 127 if (stack_size == 0) // stack_size is not yet available, don't use FakeStack. 128 return 0; 129 uptr old_val = 0; 130 // fake_stack_ has 3 states: 131 // 0 -- not initialized 132 // 1 -- being initialized 133 // ptr -- initialized 134 // This CAS checks if the state was 0 and if so changes it to state 1, 135 // if that was successful, it initializes the pointer. 136 if (atomic_compare_exchange_strong( 137 reinterpret_cast<atomic_uintptr_t *>(&fake_stack_), &old_val, 1UL, 138 memory_order_relaxed)) { 139 uptr stack_size_log = Log2(RoundUpToPowerOfTwo(stack_size)); 140 CHECK_LE(flags()->min_uar_stack_size_log, flags()->max_uar_stack_size_log); 141 stack_size_log = 142 Min(stack_size_log, static_cast<uptr>(flags()->max_uar_stack_size_log)); 143 stack_size_log = 144 Max(stack_size_log, static_cast<uptr>(flags()->min_uar_stack_size_log)); 145 fake_stack_ = FakeStack::Create(stack_size_log); 146 SetTLSFakeStack(fake_stack_); 147 return fake_stack_; 148 } 149 return 0; 150 } 151 152 void AsanThread::Init() { 153 fake_stack_ = 0; // Will be initialized lazily if needed. 154 CHECK_EQ(this->stack_size(), 0U); 155 SetThreadStackAndTls(); 156 CHECK_GT(this->stack_size(), 0U); 157 CHECK(AddrIsInMem(stack_bottom_)); 158 CHECK(AddrIsInMem(stack_top_ - 1)); 159 ClearShadowForThreadStackAndTLS(); 160 int local = 0; 161 VReport(1, "T%d: stack [%p,%p) size 0x%zx; local=%p\n", tid(), 162 (void *)stack_bottom_, (void *)stack_top_, stack_top_ - stack_bottom_, 163 &local); 164 AsanPlatformThreadInit(); 165 } 166 167 thread_return_t AsanThread::ThreadStart( 168 uptr os_id, atomic_uintptr_t *signal_thread_is_registered) { 169 Init(); 170 asanThreadRegistry().StartThread(tid(), os_id, 0); 171 if (signal_thread_is_registered) 172 atomic_store(signal_thread_is_registered, 1, memory_order_release); 173 174 if (common_flags()->use_sigaltstack) SetAlternateSignalStack(); 175 176 if (!start_routine_) { 177 // start_routine_ == 0 if we're on the main thread or on one of the 178 // OS X libdispatch worker threads. But nobody is supposed to call 179 // ThreadStart() for the worker threads. 180 CHECK_EQ(tid(), 0); 181 return 0; 182 } 183 184 thread_return_t res = start_routine_(arg_); 185 186 // On POSIX systems we defer this to the TSD destructor. LSan will consider 187 // the thread's memory as non-live from the moment we call Destroy(), even 188 // though that memory might contain pointers to heap objects which will be 189 // cleaned up by a user-defined TSD destructor. Thus, calling Destroy() before 190 // the TSD destructors have run might cause false positives in LSan. 191 if (!SANITIZER_POSIX) 192 this->Destroy(); 193 194 return res; 195 } 196 197 void AsanThread::SetThreadStackAndTls() { 198 uptr tls_size = 0; 199 GetThreadStackAndTls(tid() == 0, &stack_bottom_, &stack_size_, &tls_begin_, 200 &tls_size); 201 stack_top_ = stack_bottom_ + stack_size_; 202 tls_end_ = tls_begin_ + tls_size; 203 204 int local; 205 CHECK(AddrIsInStack((uptr)&local)); 206 } 207 208 void AsanThread::ClearShadowForThreadStackAndTLS() { 209 PoisonShadow(stack_bottom_, stack_top_ - stack_bottom_, 0); 210 if (tls_begin_ != tls_end_) 211 PoisonShadow(tls_begin_, tls_end_ - tls_begin_, 0); 212 } 213 214 bool AsanThread::GetStackFrameAccessByAddr(uptr addr, 215 StackFrameAccess *access) { 216 uptr bottom = 0; 217 if (AddrIsInStack(addr)) { 218 bottom = stack_bottom(); 219 } else if (has_fake_stack()) { 220 bottom = fake_stack()->AddrIsInFakeStack(addr); 221 CHECK(bottom); 222 access->offset = addr - bottom; 223 access->frame_pc = ((uptr*)bottom)[2]; 224 access->frame_descr = (const char *)((uptr*)bottom)[1]; 225 return true; 226 } 227 uptr aligned_addr = addr & ~(SANITIZER_WORDSIZE/8 - 1); // align addr. 228 u8 *shadow_ptr = (u8*)MemToShadow(aligned_addr); 229 u8 *shadow_bottom = (u8*)MemToShadow(bottom); 230 231 while (shadow_ptr >= shadow_bottom && 232 *shadow_ptr != kAsanStackLeftRedzoneMagic) { 233 shadow_ptr--; 234 } 235 236 while (shadow_ptr >= shadow_bottom && 237 *shadow_ptr == kAsanStackLeftRedzoneMagic) { 238 shadow_ptr--; 239 } 240 241 if (shadow_ptr < shadow_bottom) { 242 return false; 243 } 244 245 uptr* ptr = (uptr*)SHADOW_TO_MEM((uptr)(shadow_ptr + 1)); 246 CHECK(ptr[0] == kCurrentStackFrameMagic); 247 access->offset = addr - (uptr)ptr; 248 access->frame_pc = ptr[2]; 249 access->frame_descr = (const char*)ptr[1]; 250 return true; 251 } 252 253 static bool ThreadStackContainsAddress(ThreadContextBase *tctx_base, 254 void *addr) { 255 AsanThreadContext *tctx = static_cast<AsanThreadContext*>(tctx_base); 256 AsanThread *t = tctx->thread; 257 if (!t) return false; 258 if (t->AddrIsInStack((uptr)addr)) return true; 259 if (t->has_fake_stack() && t->fake_stack()->AddrIsInFakeStack((uptr)addr)) 260 return true; 261 return false; 262 } 263 264 AsanThread *GetCurrentThread() { 265 AsanThreadContext *context = 266 reinterpret_cast<AsanThreadContext *>(AsanTSDGet()); 267 if (!context) { 268 if (SANITIZER_ANDROID) { 269 // On Android, libc constructor is called _after_ asan_init, and cleans up 270 // TSD. Try to figure out if this is still the main thread by the stack 271 // address. We are not entirely sure that we have correct main thread 272 // limits, so only do this magic on Android, and only if the found thread 273 // is the main thread. 274 AsanThreadContext *tctx = GetThreadContextByTidLocked(0); 275 if (ThreadStackContainsAddress(tctx, &context)) { 276 SetCurrentThread(tctx->thread); 277 return tctx->thread; 278 } 279 } 280 return 0; 281 } 282 return context->thread; 283 } 284 285 void SetCurrentThread(AsanThread *t) { 286 CHECK(t->context()); 287 VReport(2, "SetCurrentThread: %p for thread %p\n", t->context(), 288 (void *)GetThreadSelf()); 289 // Make sure we do not reset the current AsanThread. 290 CHECK_EQ(0, AsanTSDGet()); 291 AsanTSDSet(t->context()); 292 CHECK_EQ(t->context(), AsanTSDGet()); 293 } 294 295 u32 GetCurrentTidOrInvalid() { 296 AsanThread *t = GetCurrentThread(); 297 return t ? t->tid() : kInvalidTid; 298 } 299 300 AsanThread *FindThreadByStackAddress(uptr addr) { 301 asanThreadRegistry().CheckLocked(); 302 AsanThreadContext *tctx = static_cast<AsanThreadContext *>( 303 asanThreadRegistry().FindThreadContextLocked(ThreadStackContainsAddress, 304 (void *)addr)); 305 return tctx ? tctx->thread : 0; 306 } 307 308 void EnsureMainThreadIDIsCorrect() { 309 AsanThreadContext *context = 310 reinterpret_cast<AsanThreadContext *>(AsanTSDGet()); 311 if (context && (context->tid == 0)) 312 context->os_id = GetTid(); 313 } 314 315 __asan::AsanThread *GetAsanThreadByOsIDLocked(uptr os_id) { 316 __asan::AsanThreadContext *context = static_cast<__asan::AsanThreadContext *>( 317 __asan::asanThreadRegistry().FindThreadContextByOsIDLocked(os_id)); 318 if (!context) return 0; 319 return context->thread; 320 } 321 } // namespace __asan 322 323 // --- Implementation of LSan-specific functions --- {{{1 324 namespace __lsan { 325 bool GetThreadRangesLocked(uptr os_id, uptr *stack_begin, uptr *stack_end, 326 uptr *tls_begin, uptr *tls_end, 327 uptr *cache_begin, uptr *cache_end) { 328 __asan::AsanThread *t = __asan::GetAsanThreadByOsIDLocked(os_id); 329 if (!t) return false; 330 *stack_begin = t->stack_bottom(); 331 *stack_end = t->stack_top(); 332 *tls_begin = t->tls_begin(); 333 *tls_end = t->tls_end(); 334 // ASan doesn't keep allocator caches in TLS, so these are unused. 335 *cache_begin = 0; 336 *cache_end = 0; 337 return true; 338 } 339 340 void ForEachExtraStackRange(uptr os_id, RangeIteratorCallback callback, 341 void *arg) { 342 __asan::AsanThread *t = __asan::GetAsanThreadByOsIDLocked(os_id); 343 if (t && t->has_fake_stack()) 344 t->fake_stack()->ForEachFakeFrame(callback, arg); 345 } 346 347 void LockThreadRegistry() { 348 __asan::asanThreadRegistry().Lock(); 349 } 350 351 void UnlockThreadRegistry() { 352 __asan::asanThreadRegistry().Unlock(); 353 } 354 355 void EnsureMainThreadIDIsCorrect() { 356 __asan::EnsureMainThreadIDIsCorrect(); 357 } 358 } // namespace __lsan 359