asan_thread.cc 11 KB

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