asan_thread.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. //===-- asan_thread.cpp ---------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file is a part of AddressSanitizer, an address sanity checker.
  10. //
  11. // Thread-related code.
  12. //===----------------------------------------------------------------------===//
  13. #include "asan_thread.h"
  14. #include "asan_allocator.h"
  15. #include "asan_interceptors.h"
  16. #include "asan_mapping.h"
  17. #include "asan_poisoning.h"
  18. #include "asan_stack.h"
  19. #include "lsan/lsan_common.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. namespace __asan {
  25. // AsanThreadContext implementation.
  26. void AsanThreadContext::OnCreated(void *arg) {
  27. CreateThreadContextArgs *args = static_cast<CreateThreadContextArgs *>(arg);
  28. if (args->stack)
  29. stack_id = StackDepotPut(*args->stack);
  30. thread = args->thread;
  31. thread->set_context(this);
  32. }
  33. void AsanThreadContext::OnFinished() {
  34. // Drop the link to the AsanThread object.
  35. thread = nullptr;
  36. }
  37. static ThreadRegistry *asan_thread_registry;
  38. static ThreadArgRetval *thread_data;
  39. static Mutex mu_for_thread_context;
  40. static ThreadContextBase *GetAsanThreadContext(u32 tid) {
  41. Lock lock(&mu_for_thread_context);
  42. return new (GetGlobalLowLevelAllocator()) AsanThreadContext(tid);
  43. }
  44. static void InitThreads() {
  45. static bool initialized;
  46. // Don't worry about thread_safety - this should be called when there is
  47. // a single thread.
  48. if (LIKELY(initialized))
  49. return;
  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. // MIPS requires aligned address
  55. static ALIGNED(alignof(
  56. ThreadRegistry)) char thread_registry_placeholder[sizeof(ThreadRegistry)];
  57. static ALIGNED(alignof(
  58. ThreadArgRetval)) char thread_data_placeholder[sizeof(ThreadArgRetval)];
  59. asan_thread_registry =
  60. new (thread_registry_placeholder) ThreadRegistry(GetAsanThreadContext);
  61. thread_data = new (thread_data_placeholder) ThreadArgRetval();
  62. initialized = true;
  63. }
  64. ThreadRegistry &asanThreadRegistry() {
  65. InitThreads();
  66. return *asan_thread_registry;
  67. }
  68. ThreadArgRetval &asanThreadArgRetval() {
  69. InitThreads();
  70. return *thread_data;
  71. }
  72. AsanThreadContext *GetThreadContextByTidLocked(u32 tid) {
  73. return static_cast<AsanThreadContext *>(
  74. asanThreadRegistry().GetThreadLocked(tid));
  75. }
  76. // AsanThread implementation.
  77. AsanThread *AsanThread::Create(const void *start_data, uptr data_size,
  78. u32 parent_tid, StackTrace *stack,
  79. bool detached) {
  80. uptr PageSize = GetPageSizeCached();
  81. uptr size = RoundUpTo(sizeof(AsanThread), PageSize);
  82. AsanThread *thread = (AsanThread *)MmapOrDie(size, __func__);
  83. if (data_size) {
  84. uptr availible_size = (uptr)thread + size - (uptr)(thread->start_data_);
  85. CHECK_LE(data_size, availible_size);
  86. internal_memcpy(thread->start_data_, start_data, data_size);
  87. }
  88. AsanThreadContext::CreateThreadContextArgs args = {thread, stack};
  89. asanThreadRegistry().CreateThread(0, detached, parent_tid, &args);
  90. return thread;
  91. }
  92. void AsanThread::GetStartData(void *out, uptr out_size) const {
  93. internal_memcpy(out, start_data_, out_size);
  94. }
  95. void AsanThread::TSDDtor(void *tsd) {
  96. AsanThreadContext *context = (AsanThreadContext *)tsd;
  97. VReport(1, "T%d TSDDtor\n", context->tid);
  98. if (context->thread)
  99. context->thread->Destroy();
  100. }
  101. void AsanThread::Destroy() {
  102. int tid = this->tid();
  103. VReport(1, "T%d exited\n", tid);
  104. bool was_running =
  105. (asanThreadRegistry().FinishThread(tid) == ThreadStatusRunning);
  106. if (was_running) {
  107. if (AsanThread *thread = GetCurrentThread())
  108. CHECK_EQ(this, thread);
  109. malloc_storage().CommitBack();
  110. if (common_flags()->use_sigaltstack)
  111. UnsetAlternateSignalStack();
  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. } else {
  119. CHECK_NE(this, GetCurrentThread());
  120. }
  121. uptr size = RoundUpTo(sizeof(AsanThread), GetPageSizeCached());
  122. UnmapOrDie(this, size);
  123. if (was_running)
  124. DTLS_Destroy();
  125. }
  126. void AsanThread::StartSwitchFiber(FakeStack **fake_stack_save, uptr bottom,
  127. uptr size) {
  128. if (atomic_load(&stack_switching_, memory_order_relaxed)) {
  129. Report("ERROR: starting fiber switch while in fiber switch\n");
  130. Die();
  131. }
  132. next_stack_bottom_ = bottom;
  133. next_stack_top_ = bottom + size;
  134. atomic_store(&stack_switching_, 1, memory_order_release);
  135. FakeStack *current_fake_stack = fake_stack_;
  136. if (fake_stack_save)
  137. *fake_stack_save = fake_stack_;
  138. fake_stack_ = nullptr;
  139. SetTLSFakeStack(nullptr);
  140. // if fake_stack_save is null, the fiber will die, delete the fakestack
  141. if (!fake_stack_save && current_fake_stack)
  142. current_fake_stack->Destroy(this->tid());
  143. }
  144. void AsanThread::FinishSwitchFiber(FakeStack *fake_stack_save, uptr *bottom_old,
  145. uptr *size_old) {
  146. if (!atomic_load(&stack_switching_, memory_order_relaxed)) {
  147. Report("ERROR: finishing a fiber switch that has not started\n");
  148. Die();
  149. }
  150. if (fake_stack_save) {
  151. SetTLSFakeStack(fake_stack_save);
  152. fake_stack_ = fake_stack_save;
  153. }
  154. if (bottom_old)
  155. *bottom_old = stack_bottom_;
  156. if (size_old)
  157. *size_old = stack_top_ - stack_bottom_;
  158. stack_bottom_ = next_stack_bottom_;
  159. stack_top_ = next_stack_top_;
  160. atomic_store(&stack_switching_, 0, memory_order_release);
  161. next_stack_top_ = 0;
  162. next_stack_bottom_ = 0;
  163. }
  164. inline AsanThread::StackBounds AsanThread::GetStackBounds() const {
  165. if (!atomic_load(&stack_switching_, memory_order_acquire)) {
  166. // Make sure the stack bounds are fully initialized.
  167. if (stack_bottom_ >= stack_top_)
  168. return {0, 0};
  169. return {stack_bottom_, stack_top_};
  170. }
  171. char local;
  172. const uptr cur_stack = (uptr)&local;
  173. // Note: need to check next stack first, because FinishSwitchFiber
  174. // may be in process of overwriting stack_top_/bottom_. But in such case
  175. // we are already on the next stack.
  176. if (cur_stack >= next_stack_bottom_ && cur_stack < next_stack_top_)
  177. return {next_stack_bottom_, next_stack_top_};
  178. return {stack_bottom_, stack_top_};
  179. }
  180. uptr AsanThread::stack_top() { return GetStackBounds().top; }
  181. uptr AsanThread::stack_bottom() { return GetStackBounds().bottom; }
  182. uptr AsanThread::stack_size() {
  183. const auto bounds = GetStackBounds();
  184. return bounds.top - bounds.bottom;
  185. }
  186. // We want to create the FakeStack lazily on the first use, but not earlier
  187. // than the stack size is known and the procedure has to be async-signal safe.
  188. FakeStack *AsanThread::AsyncSignalSafeLazyInitFakeStack() {
  189. uptr stack_size = this->stack_size();
  190. if (stack_size == 0) // stack_size is not yet available, don't use FakeStack.
  191. return nullptr;
  192. uptr old_val = 0;
  193. // fake_stack_ has 3 states:
  194. // 0 -- not initialized
  195. // 1 -- being initialized
  196. // ptr -- initialized
  197. // This CAS checks if the state was 0 and if so changes it to state 1,
  198. // if that was successful, it initializes the pointer.
  199. if (atomic_compare_exchange_strong(
  200. reinterpret_cast<atomic_uintptr_t *>(&fake_stack_), &old_val, 1UL,
  201. memory_order_relaxed)) {
  202. uptr stack_size_log = Log2(RoundUpToPowerOfTwo(stack_size));
  203. CHECK_LE(flags()->min_uar_stack_size_log, flags()->max_uar_stack_size_log);
  204. stack_size_log =
  205. Min(stack_size_log, static_cast<uptr>(flags()->max_uar_stack_size_log));
  206. stack_size_log =
  207. Max(stack_size_log, static_cast<uptr>(flags()->min_uar_stack_size_log));
  208. fake_stack_ = FakeStack::Create(stack_size_log);
  209. DCHECK_EQ(GetCurrentThread(), this);
  210. SetTLSFakeStack(fake_stack_);
  211. return fake_stack_;
  212. }
  213. return nullptr;
  214. }
  215. void AsanThread::Init(const InitOptions *options) {
  216. DCHECK_NE(tid(), kInvalidTid);
  217. next_stack_top_ = next_stack_bottom_ = 0;
  218. atomic_store(&stack_switching_, false, memory_order_release);
  219. CHECK_EQ(this->stack_size(), 0U);
  220. SetThreadStackAndTls(options);
  221. if (stack_top_ != stack_bottom_) {
  222. CHECK_GT(this->stack_size(), 0U);
  223. CHECK(AddrIsInMem(stack_bottom_));
  224. CHECK(AddrIsInMem(stack_top_ - 1));
  225. }
  226. ClearShadowForThreadStackAndTLS();
  227. fake_stack_ = nullptr;
  228. if (__asan_option_detect_stack_use_after_return &&
  229. tid() == GetCurrentTidOrInvalid()) {
  230. // AsyncSignalSafeLazyInitFakeStack makes use of threadlocals and must be
  231. // called from the context of the thread it is initializing, not its parent.
  232. // Most platforms call AsanThread::Init on the newly-spawned thread, but
  233. // Fuchsia calls this function from the parent thread. To support that
  234. // approach, we avoid calling AsyncSignalSafeLazyInitFakeStack here; it will
  235. // be called by the new thread when it first attempts to access the fake
  236. // stack.
  237. AsyncSignalSafeLazyInitFakeStack();
  238. }
  239. int local = 0;
  240. VReport(1, "T%d: stack [%p,%p) size 0x%zx; local=%p\n", tid(),
  241. (void *)stack_bottom_, (void *)stack_top_, stack_top_ - stack_bottom_,
  242. (void *)&local);
  243. }
  244. // Fuchsia doesn't use ThreadStart.
  245. // asan_fuchsia.c definies CreateMainThread and SetThreadStackAndTls.
  246. #if !SANITIZER_FUCHSIA
  247. void AsanThread::ThreadStart(tid_t os_id) {
  248. Init();
  249. asanThreadRegistry().StartThread(tid(), os_id, ThreadType::Regular, nullptr);
  250. if (common_flags()->use_sigaltstack)
  251. SetAlternateSignalStack();
  252. }
  253. AsanThread *CreateMainThread() {
  254. AsanThread *main_thread = AsanThread::Create(
  255. /* parent_tid */ kMainTid,
  256. /* stack */ nullptr, /* detached */ true);
  257. SetCurrentThread(main_thread);
  258. main_thread->ThreadStart(internal_getpid());
  259. return main_thread;
  260. }
  261. // This implementation doesn't use the argument, which is just passed down
  262. // from the caller of Init (which see, above). It's only there to support
  263. // OS-specific implementations that need more information passed through.
  264. void AsanThread::SetThreadStackAndTls(const InitOptions *options) {
  265. DCHECK_EQ(options, nullptr);
  266. uptr tls_size = 0;
  267. uptr stack_size = 0;
  268. GetThreadStackAndTls(tid() == kMainTid, &stack_bottom_, &stack_size,
  269. &tls_begin_, &tls_size);
  270. stack_top_ = RoundDownTo(stack_bottom_ + stack_size, ASAN_SHADOW_GRANULARITY);
  271. stack_bottom_ = RoundDownTo(stack_bottom_, ASAN_SHADOW_GRANULARITY);
  272. tls_end_ = tls_begin_ + tls_size;
  273. dtls_ = DTLS_Get();
  274. if (stack_top_ != stack_bottom_) {
  275. int local;
  276. CHECK(AddrIsInStack((uptr)&local));
  277. }
  278. }
  279. #endif // !SANITIZER_FUCHSIA
  280. void AsanThread::ClearShadowForThreadStackAndTLS() {
  281. if (stack_top_ != stack_bottom_)
  282. PoisonShadow(stack_bottom_, stack_top_ - stack_bottom_, 0);
  283. if (tls_begin_ != tls_end_) {
  284. uptr tls_begin_aligned = RoundDownTo(tls_begin_, ASAN_SHADOW_GRANULARITY);
  285. uptr tls_end_aligned = RoundUpTo(tls_end_, ASAN_SHADOW_GRANULARITY);
  286. FastPoisonShadow(tls_begin_aligned, tls_end_aligned - tls_begin_aligned, 0);
  287. }
  288. }
  289. bool AsanThread::GetStackFrameAccessByAddr(uptr addr,
  290. StackFrameAccess *access) {
  291. if (stack_top_ == stack_bottom_)
  292. return false;
  293. uptr bottom = 0;
  294. if (AddrIsInStack(addr)) {
  295. bottom = stack_bottom();
  296. } else if (FakeStack *fake_stack = get_fake_stack()) {
  297. bottom = fake_stack->AddrIsInFakeStack(addr);
  298. CHECK(bottom);
  299. access->offset = addr - bottom;
  300. access->frame_pc = ((uptr *)bottom)[2];
  301. access->frame_descr = (const char *)((uptr *)bottom)[1];
  302. return true;
  303. }
  304. uptr aligned_addr = RoundDownTo(addr, SANITIZER_WORDSIZE / 8); // align addr.
  305. uptr mem_ptr = RoundDownTo(aligned_addr, ASAN_SHADOW_GRANULARITY);
  306. u8 *shadow_ptr = (u8 *)MemToShadow(aligned_addr);
  307. u8 *shadow_bottom = (u8 *)MemToShadow(bottom);
  308. while (shadow_ptr >= shadow_bottom &&
  309. *shadow_ptr != kAsanStackLeftRedzoneMagic) {
  310. shadow_ptr--;
  311. mem_ptr -= ASAN_SHADOW_GRANULARITY;
  312. }
  313. while (shadow_ptr >= shadow_bottom &&
  314. *shadow_ptr == kAsanStackLeftRedzoneMagic) {
  315. shadow_ptr--;
  316. mem_ptr -= ASAN_SHADOW_GRANULARITY;
  317. }
  318. if (shadow_ptr < shadow_bottom) {
  319. return false;
  320. }
  321. uptr *ptr = (uptr *)(mem_ptr + ASAN_SHADOW_GRANULARITY);
  322. CHECK(ptr[0] == kCurrentStackFrameMagic);
  323. access->offset = addr - (uptr)ptr;
  324. access->frame_pc = ptr[2];
  325. access->frame_descr = (const char *)ptr[1];
  326. return true;
  327. }
  328. uptr AsanThread::GetStackVariableShadowStart(uptr addr) {
  329. uptr bottom = 0;
  330. if (AddrIsInStack(addr)) {
  331. bottom = stack_bottom();
  332. } else if (FakeStack *fake_stack = get_fake_stack()) {
  333. bottom = fake_stack->AddrIsInFakeStack(addr);
  334. if (bottom == 0) {
  335. return 0;
  336. }
  337. } else {
  338. return 0;
  339. }
  340. uptr aligned_addr = RoundDownTo(addr, SANITIZER_WORDSIZE / 8); // align addr.
  341. u8 *shadow_ptr = (u8 *)MemToShadow(aligned_addr);
  342. u8 *shadow_bottom = (u8 *)MemToShadow(bottom);
  343. while (shadow_ptr >= shadow_bottom &&
  344. (*shadow_ptr != kAsanStackLeftRedzoneMagic &&
  345. *shadow_ptr != kAsanStackMidRedzoneMagic &&
  346. *shadow_ptr != kAsanStackRightRedzoneMagic))
  347. shadow_ptr--;
  348. return (uptr)shadow_ptr + 1;
  349. }
  350. bool AsanThread::AddrIsInStack(uptr addr) {
  351. const auto bounds = GetStackBounds();
  352. return addr >= bounds.bottom && addr < bounds.top;
  353. }
  354. static bool ThreadStackContainsAddress(ThreadContextBase *tctx_base,
  355. void *addr) {
  356. AsanThreadContext *tctx = static_cast<AsanThreadContext *>(tctx_base);
  357. AsanThread *t = tctx->thread;
  358. if (!t)
  359. return false;
  360. if (t->AddrIsInStack((uptr)addr))
  361. return true;
  362. FakeStack *fake_stack = t->get_fake_stack();
  363. if (!fake_stack)
  364. return false;
  365. return fake_stack->AddrIsInFakeStack((uptr)addr);
  366. }
  367. AsanThread *GetCurrentThread() {
  368. AsanThreadContext *context =
  369. reinterpret_cast<AsanThreadContext *>(AsanTSDGet());
  370. if (!context) {
  371. if (SANITIZER_ANDROID) {
  372. // On Android, libc constructor is called _after_ asan_init, and cleans up
  373. // TSD. Try to figure out if this is still the main thread by the stack
  374. // address. We are not entirely sure that we have correct main thread
  375. // limits, so only do this magic on Android, and only if the found thread
  376. // is the main thread.
  377. AsanThreadContext *tctx = GetThreadContextByTidLocked(kMainTid);
  378. if (tctx && ThreadStackContainsAddress(tctx, &context)) {
  379. SetCurrentThread(tctx->thread);
  380. return tctx->thread;
  381. }
  382. }
  383. return nullptr;
  384. }
  385. return context->thread;
  386. }
  387. void SetCurrentThread(AsanThread *t) {
  388. CHECK(t->context());
  389. VReport(2, "SetCurrentThread: %p for thread %p\n", (void *)t->context(),
  390. (void *)GetThreadSelf());
  391. // Make sure we do not reset the current AsanThread.
  392. CHECK_EQ(0, AsanTSDGet());
  393. AsanTSDSet(t->context());
  394. CHECK_EQ(t->context(), AsanTSDGet());
  395. }
  396. u32 GetCurrentTidOrInvalid() {
  397. AsanThread *t = GetCurrentThread();
  398. return t ? t->tid() : kInvalidTid;
  399. }
  400. AsanThread *FindThreadByStackAddress(uptr addr) {
  401. asanThreadRegistry().CheckLocked();
  402. AsanThreadContext *tctx = static_cast<AsanThreadContext *>(
  403. asanThreadRegistry().FindThreadContextLocked(ThreadStackContainsAddress,
  404. (void *)addr));
  405. return tctx ? tctx->thread : nullptr;
  406. }
  407. void EnsureMainThreadIDIsCorrect() {
  408. AsanThreadContext *context =
  409. reinterpret_cast<AsanThreadContext *>(AsanTSDGet());
  410. if (context && (context->tid == kMainTid))
  411. context->os_id = GetTid();
  412. }
  413. __asan::AsanThread *GetAsanThreadByOsIDLocked(tid_t os_id) {
  414. __asan::AsanThreadContext *context = static_cast<__asan::AsanThreadContext *>(
  415. __asan::asanThreadRegistry().FindThreadContextByOsIDLocked(os_id));
  416. if (!context)
  417. return nullptr;
  418. return context->thread;
  419. }
  420. } // namespace __asan
  421. // --- Implementation of LSan-specific functions --- {{{1
  422. namespace __lsan {
  423. void LockThreads() {
  424. __asan::asanThreadRegistry().Lock();
  425. __asan::asanThreadArgRetval().Lock();
  426. }
  427. void UnlockThreads() {
  428. __asan::asanThreadArgRetval().Unlock();
  429. __asan::asanThreadRegistry().Unlock();
  430. }
  431. static ThreadRegistry *GetAsanThreadRegistryLocked() {
  432. __asan::asanThreadRegistry().CheckLocked();
  433. return &__asan::asanThreadRegistry();
  434. }
  435. void EnsureMainThreadIDIsCorrect() { __asan::EnsureMainThreadIDIsCorrect(); }
  436. bool GetThreadRangesLocked(tid_t os_id, uptr *stack_begin, uptr *stack_end,
  437. uptr *tls_begin, uptr *tls_end, uptr *cache_begin,
  438. uptr *cache_end, DTLS **dtls) {
  439. __asan::AsanThread *t = __asan::GetAsanThreadByOsIDLocked(os_id);
  440. if (!t)
  441. return false;
  442. *stack_begin = t->stack_bottom();
  443. *stack_end = t->stack_top();
  444. *tls_begin = t->tls_begin();
  445. *tls_end = t->tls_end();
  446. // ASan doesn't keep allocator caches in TLS, so these are unused.
  447. *cache_begin = 0;
  448. *cache_end = 0;
  449. *dtls = t->dtls();
  450. return true;
  451. }
  452. void GetAllThreadAllocatorCachesLocked(InternalMmapVector<uptr> *caches) {}
  453. void GetThreadExtraStackRangesLocked(tid_t os_id,
  454. InternalMmapVector<Range> *ranges) {
  455. __asan::AsanThread *t = __asan::GetAsanThreadByOsIDLocked(os_id);
  456. if (!t)
  457. return;
  458. __asan::FakeStack *fake_stack = t->get_fake_stack();
  459. if (!fake_stack)
  460. return;
  461. fake_stack->ForEachFakeFrame(
  462. [](uptr begin, uptr end, void *arg) {
  463. reinterpret_cast<InternalMmapVector<Range> *>(arg)->push_back(
  464. {begin, end});
  465. },
  466. ranges);
  467. }
  468. void GetThreadExtraStackRangesLocked(InternalMmapVector<Range> *ranges) {
  469. GetAsanThreadRegistryLocked()->RunCallbackForEachThreadLocked(
  470. [](ThreadContextBase *tctx, void *arg) {
  471. GetThreadExtraStackRangesLocked(
  472. tctx->os_id, reinterpret_cast<InternalMmapVector<Range> *>(arg));
  473. },
  474. ranges);
  475. }
  476. void GetAdditionalThreadContextPtrsLocked(InternalMmapVector<uptr> *ptrs) {
  477. __asan::asanThreadArgRetval().GetAllPtrsLocked(ptrs);
  478. }
  479. void GetRunningThreadsLocked(InternalMmapVector<tid_t> *threads) {
  480. GetAsanThreadRegistryLocked()->RunCallbackForEachThreadLocked(
  481. [](ThreadContextBase *tctx, void *threads) {
  482. if (tctx->status == ThreadStatusRunning)
  483. reinterpret_cast<InternalMmapVector<tid_t> *>(threads)->push_back(
  484. tctx->os_id);
  485. },
  486. threads);
  487. }
  488. } // namespace __lsan
  489. // ---------------------- Interface ---------------- {{{1
  490. using namespace __asan;
  491. extern "C" {
  492. SANITIZER_INTERFACE_ATTRIBUTE
  493. void __sanitizer_start_switch_fiber(void **fakestacksave, const void *bottom,
  494. uptr size) {
  495. AsanThread *t = GetCurrentThread();
  496. if (!t) {
  497. VReport(1, "__asan_start_switch_fiber called from unknown thread\n");
  498. return;
  499. }
  500. t->StartSwitchFiber((FakeStack **)fakestacksave, (uptr)bottom, size);
  501. }
  502. SANITIZER_INTERFACE_ATTRIBUTE
  503. void __sanitizer_finish_switch_fiber(void *fakestack, const void **bottom_old,
  504. uptr *size_old) {
  505. AsanThread *t = GetCurrentThread();
  506. if (!t) {
  507. VReport(1, "__asan_finish_switch_fiber called from unknown thread\n");
  508. return;
  509. }
  510. t->FinishSwitchFiber((FakeStack *)fakestack, (uptr *)bottom_old,
  511. (uptr *)size_old);
  512. }
  513. }