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