asan_thread.cc revision 200afbd8ba4904241c1ebcef4fa79d739ca01f73
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 "lsan/lsan_common.h" 23 24namespace __asan { 25 26// AsanThreadContext implementation. 27 28void AsanThreadContext::OnCreated(void *arg) { 29 CreateThreadContextArgs *args = static_cast<CreateThreadContextArgs*>(arg); 30 if (args->stack) { 31 internal_memcpy(&stack, args->stack, sizeof(stack)); 32 } 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 ThreadContextBase *GetAsanThreadContext(u32 tid) { 47 void *mem = MmapOrDie(sizeof(AsanThreadContext), "AsanThreadContext"); 48 return new(mem) AsanThreadContext(tid); 49} 50 51ThreadRegistry &asanThreadRegistry() { 52 static bool initialized; 53 // Don't worry about thread_safety - this should be called when there is 54 // a single thread. 55 if (!initialized) { 56 // Never reuse ASan threads: we store pointer to AsanThreadContext 57 // in TSD and can't reliably tell when no more TSD destructors will 58 // be called. It would be wrong to reuse AsanThreadContext for another 59 // thread before all TSD destructors will be called for it. 60 asan_thread_registry = new(thread_registry_placeholder) ThreadRegistry( 61 GetAsanThreadContext, kMaxNumberOfThreads, kMaxNumberOfThreads); 62 initialized = true; 63 } 64 return *asan_thread_registry; 65} 66 67AsanThreadContext *GetThreadContextByTidLocked(u32 tid) { 68 return static_cast<AsanThreadContext *>( 69 asanThreadRegistry().GetThreadLocked(tid)); 70} 71 72// AsanThread implementation. 73 74AsanThread *AsanThread::Create(thread_callback_t start_routine, 75 void *arg) { 76 uptr PageSize = GetPageSizeCached(); 77 uptr size = RoundUpTo(sizeof(AsanThread), PageSize); 78 AsanThread *thread = (AsanThread*)MmapOrDie(size, __FUNCTION__); 79 thread->start_routine_ = start_routine; 80 thread->arg_ = arg; 81 thread->context_ = 0; 82 83 return thread; 84} 85 86void AsanThread::TSDDtor(void *tsd) { 87 AsanThreadContext *context = (AsanThreadContext*)tsd; 88 if (flags()->verbosity >= 1) 89 Report("T%d TSDDtor\n", context->tid); 90 if (context->thread) 91 context->thread->Destroy(); 92} 93 94void AsanThread::Destroy() { 95 if (flags()->verbosity >= 1) { 96 Report("T%d exited\n", tid()); 97 } 98 99 asanThreadRegistry().FinishThread(tid()); 100 FlushToAccumulatedStats(&stats_); 101 // We also clear the shadow on thread destruction because 102 // some code may still be executing in later TSD destructors 103 // and we don't want it to have any poisoned stack. 104 ClearShadowForThreadStackAndTLS(); 105 fake_stack().Cleanup(); 106 uptr size = RoundUpTo(sizeof(AsanThread), GetPageSizeCached()); 107 UnmapOrDie(this, size); 108} 109 110void AsanThread::Init() { 111 SetThreadStackAndTls(); 112 CHECK(AddrIsInMem(stack_bottom_)); 113 CHECK(AddrIsInMem(stack_top_ - 1)); 114 ClearShadowForThreadStackAndTLS(); 115 if (flags()->verbosity >= 1) { 116 int local = 0; 117 Report("T%d: stack [%p,%p) size 0x%zx; local=%p\n", 118 tid(), (void*)stack_bottom_, (void*)stack_top_, 119 stack_top_ - stack_bottom_, &local); 120 } 121 fake_stack_.Init(stack_size()); 122 AsanPlatformThreadInit(); 123} 124 125thread_return_t AsanThread::ThreadStart(uptr os_id) { 126 Init(); 127 asanThreadRegistry().StartThread(tid(), os_id, 0); 128 if (flags()->use_sigaltstack) SetAlternateSignalStack(); 129 130 if (!start_routine_) { 131 // start_routine_ == 0 if we're on the main thread or on one of the 132 // OS X libdispatch worker threads. But nobody is supposed to call 133 // ThreadStart() for the worker threads. 134 CHECK_EQ(tid(), 0); 135 return 0; 136 } 137 138 thread_return_t res = start_routine_(arg_); 139 malloc_storage().CommitBack(); 140 if (flags()->use_sigaltstack) UnsetAlternateSignalStack(); 141 142 this->Destroy(); 143 144 return res; 145} 146 147void AsanThread::SetThreadStackAndTls() { 148 uptr stack_size = 0, tls_size = 0; 149 GetThreadStackAndTls(tid() == 0, &stack_bottom_, &stack_size, &tls_begin_, 150 &tls_size); 151 stack_top_ = stack_bottom_ + stack_size; 152 tls_end_ = tls_begin_ + tls_size; 153 154 int local; 155 CHECK(AddrIsInStack((uptr)&local)); 156} 157 158void AsanThread::ClearShadowForThreadStackAndTLS() { 159 PoisonShadow(stack_bottom_, stack_top_ - stack_bottom_, 0); 160 if (tls_begin_ != tls_end_) 161 PoisonShadow(tls_begin_, tls_end_ - tls_begin_, 0); 162} 163 164const char *AsanThread::GetFrameNameByAddr(uptr addr, uptr *offset, 165 uptr *frame_pc) { 166 uptr bottom = 0; 167 if (AddrIsInStack(addr)) { 168 bottom = stack_bottom(); 169 } else { 170 bottom = fake_stack().AddrIsInFakeStack(addr); 171 CHECK(bottom); 172 *offset = addr - bottom; 173 *frame_pc = ((uptr*)bottom)[2]; 174 return (const char *)((uptr*)bottom)[1]; 175 } 176 uptr aligned_addr = addr & ~(SANITIZER_WORDSIZE/8 - 1); // align addr. 177 u8 *shadow_ptr = (u8*)MemToShadow(aligned_addr); 178 u8 *shadow_bottom = (u8*)MemToShadow(bottom); 179 180 while (shadow_ptr >= shadow_bottom && 181 *shadow_ptr != kAsanStackLeftRedzoneMagic) { 182 shadow_ptr--; 183 } 184 185 while (shadow_ptr >= shadow_bottom && 186 *shadow_ptr == kAsanStackLeftRedzoneMagic) { 187 shadow_ptr--; 188 } 189 190 if (shadow_ptr < shadow_bottom) { 191 *offset = 0; 192 return "UNKNOWN"; 193 } 194 195 uptr* ptr = (uptr*)SHADOW_TO_MEM((uptr)(shadow_ptr + 1)); 196 CHECK(ptr[0] == kCurrentStackFrameMagic); 197 *offset = addr - (uptr)ptr; 198 *frame_pc = ptr[2]; 199 return (const char*)ptr[1]; 200} 201 202static bool ThreadStackContainsAddress(ThreadContextBase *tctx_base, 203 void *addr) { 204 AsanThreadContext *tctx = static_cast<AsanThreadContext*>(tctx_base); 205 AsanThread *t = tctx->thread; 206 return (t && t->fake_stack().StackSize() && 207 (t->fake_stack().AddrIsInFakeStack((uptr)addr) || 208 t->AddrIsInStack((uptr)addr))); 209} 210 211AsanThread *GetCurrentThread() { 212 AsanThreadContext *context = (AsanThreadContext*)AsanTSDGet(); 213 if (!context) { 214 if (SANITIZER_ANDROID) { 215 // On Android, libc constructor is called _after_ asan_init, and cleans up 216 // TSD. Try to figure out if this is still the main thread by the stack 217 // address. We are not entirely sure that we have correct main thread 218 // limits, so only do this magic on Android, and only if the found thread 219 // is the main thread. 220 AsanThreadContext *tctx = GetThreadContextByTidLocked(0); 221 if (ThreadStackContainsAddress(tctx, &context)) { 222 SetCurrentThread(tctx->thread); 223 return tctx->thread; 224 } 225 } 226 return 0; 227 } 228 return context->thread; 229} 230 231void SetCurrentThread(AsanThread *t) { 232 CHECK(t->context()); 233 if (flags()->verbosity >= 2) { 234 Report("SetCurrentThread: %p for thread %p\n", 235 t->context(), (void*)GetThreadSelf()); 236 } 237 // Make sure we do not reset the current AsanThread. 238 CHECK_EQ(0, AsanTSDGet()); 239 AsanTSDSet(t->context()); 240 CHECK_EQ(t->context(), AsanTSDGet()); 241} 242 243u32 GetCurrentTidOrInvalid() { 244 AsanThread *t = GetCurrentThread(); 245 return t ? t->tid() : kInvalidTid; 246} 247 248AsanThread *FindThreadByStackAddress(uptr addr) { 249 asanThreadRegistry().CheckLocked(); 250 AsanThreadContext *tctx = static_cast<AsanThreadContext *>( 251 asanThreadRegistry().FindThreadContextLocked(ThreadStackContainsAddress, 252 (void *)addr)); 253 return tctx ? tctx->thread : 0; 254} 255} // namespace __asan 256 257// --- Implementation of LSan-specific functions --- {{{1 258namespace __lsan { 259bool GetThreadRangesLocked(uptr os_id, uptr *stack_begin, uptr *stack_end, 260 uptr *tls_begin, uptr *tls_end, 261 uptr *cache_begin, uptr *cache_end) { 262 __asan::AsanThreadContext *context = static_cast<__asan::AsanThreadContext *>( 263 __asan::asanThreadRegistry().FindThreadContextByOsIDLocked(os_id)); 264 if (!context) return false; 265 __asan::AsanThread *t = context->thread; 266 if (!t) return false; 267 *stack_begin = t->stack_bottom(); 268 *stack_end = t->stack_top(); 269 *tls_begin = t->tls_begin(); 270 *tls_end = t->tls_end(); 271 // ASan doesn't keep allocator caches in TLS, so these are unused. 272 *cache_begin = 0; 273 *cache_end = 0; 274 return true; 275} 276 277void LockThreadRegistry() { 278 __asan::asanThreadRegistry().Lock(); 279} 280 281void UnlockThreadRegistry() { 282 __asan::asanThreadRegistry().Unlock(); 283} 284} // namespace __lsan 285