asan_thread.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //===-- asan_thread.h -------------------------------------------*- C++ -*-===//
  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. // ASan-private header for asan_thread.cpp.
  12. //===----------------------------------------------------------------------===//
  13. #ifndef ASAN_THREAD_H
  14. #define ASAN_THREAD_H
  15. #include "asan_allocator.h"
  16. #include "asan_fake_stack.h"
  17. #include "asan_internal.h"
  18. #include "asan_stats.h"
  19. #include "sanitizer_common/sanitizer_common.h"
  20. #include "sanitizer_common/sanitizer_libc.h"
  21. #include "sanitizer_common/sanitizer_thread_arg_retval.h"
  22. #include "sanitizer_common/sanitizer_thread_registry.h"
  23. namespace __sanitizer {
  24. struct DTLS;
  25. } // namespace __sanitizer
  26. namespace __asan {
  27. class AsanThread;
  28. // These objects are created for every thread and are never deleted,
  29. // so we can find them by tid even if the thread is long dead.
  30. class AsanThreadContext final : public ThreadContextBase {
  31. public:
  32. explicit AsanThreadContext(int tid)
  33. : ThreadContextBase(tid), announced(false),
  34. destructor_iterations(GetPthreadDestructorIterations()), stack_id(0),
  35. thread(nullptr) {}
  36. bool announced;
  37. u8 destructor_iterations;
  38. u32 stack_id;
  39. AsanThread *thread;
  40. void OnCreated(void *arg) override;
  41. void OnFinished() override;
  42. struct CreateThreadContextArgs {
  43. AsanThread *thread;
  44. StackTrace *stack;
  45. };
  46. };
  47. // AsanThreadContext objects are never freed, so we need many of them.
  48. COMPILER_CHECK(sizeof(AsanThreadContext) <= 256);
  49. #if defined(_MSC_VER) && !defined(__clang__)
  50. // MSVC raises a warning about a nonstandard extension being used for the 0
  51. // sized element in this array. Disable this for warn-as-error builds.
  52. # pragma warning(push)
  53. # pragma warning(disable : 4200)
  54. #endif
  55. // AsanThread are stored in TSD and destroyed when the thread dies.
  56. class AsanThread {
  57. public:
  58. template <typename T>
  59. static AsanThread *Create(const T &data, u32 parent_tid, StackTrace *stack,
  60. bool detached) {
  61. return Create(&data, sizeof(data), parent_tid, stack, detached);
  62. }
  63. static AsanThread *Create(u32 parent_tid, StackTrace *stack, bool detached) {
  64. return Create(nullptr, 0, parent_tid, stack, detached);
  65. }
  66. static void TSDDtor(void *tsd);
  67. void Destroy();
  68. struct InitOptions;
  69. void Init(const InitOptions *options = nullptr);
  70. void ThreadStart(tid_t os_id);
  71. thread_return_t RunThread();
  72. uptr stack_top();
  73. uptr stack_bottom();
  74. uptr stack_size();
  75. uptr tls_begin() { return tls_begin_; }
  76. uptr tls_end() { return tls_end_; }
  77. DTLS *dtls() { return dtls_; }
  78. u32 tid() { return context_->tid; }
  79. AsanThreadContext *context() { return context_; }
  80. void set_context(AsanThreadContext *context) { context_ = context; }
  81. struct StackFrameAccess {
  82. uptr offset;
  83. uptr frame_pc;
  84. const char *frame_descr;
  85. };
  86. bool GetStackFrameAccessByAddr(uptr addr, StackFrameAccess *access);
  87. // Returns a pointer to the start of the stack variable's shadow memory.
  88. uptr GetStackVariableShadowStart(uptr addr);
  89. bool AddrIsInStack(uptr addr);
  90. void DeleteFakeStack(int tid) {
  91. if (!fake_stack_) return;
  92. FakeStack *t = fake_stack_;
  93. fake_stack_ = nullptr;
  94. SetTLSFakeStack(nullptr);
  95. t->Destroy(tid);
  96. }
  97. void StartSwitchFiber(FakeStack **fake_stack_save, uptr bottom, uptr size);
  98. void FinishSwitchFiber(FakeStack *fake_stack_save, uptr *bottom_old,
  99. uptr *size_old);
  100. FakeStack *get_fake_stack() {
  101. if (atomic_load(&stack_switching_, memory_order_relaxed))
  102. return nullptr;
  103. if (reinterpret_cast<uptr>(fake_stack_) <= 1)
  104. return nullptr;
  105. return fake_stack_;
  106. }
  107. FakeStack *get_or_create_fake_stack() {
  108. if (atomic_load(&stack_switching_, memory_order_relaxed))
  109. return nullptr;
  110. if (reinterpret_cast<uptr>(fake_stack_) <= 1)
  111. return AsyncSignalSafeLazyInitFakeStack();
  112. return fake_stack_;
  113. }
  114. // True is this thread is currently unwinding stack (i.e. collecting a stack
  115. // trace). Used to prevent deadlocks on platforms where libc unwinder calls
  116. // malloc internally. See PR17116 for more details.
  117. bool isUnwinding() const { return unwinding_; }
  118. void setUnwinding(bool b) { unwinding_ = b; }
  119. AsanThreadLocalMallocStorage &malloc_storage() { return malloc_storage_; }
  120. AsanStats &stats() { return stats_; }
  121. void *extra_spill_area() { return &extra_spill_area_; }
  122. template <typename T>
  123. void GetStartData(T &data) const {
  124. GetStartData(&data, sizeof(data));
  125. }
  126. private:
  127. // NOTE: There is no AsanThread constructor. It is allocated
  128. // via mmap() and *must* be valid in zero-initialized state.
  129. static AsanThread *Create(const void *start_data, uptr data_size,
  130. u32 parent_tid, StackTrace *stack, bool detached);
  131. void SetThreadStackAndTls(const InitOptions *options);
  132. void ClearShadowForThreadStackAndTLS();
  133. FakeStack *AsyncSignalSafeLazyInitFakeStack();
  134. struct StackBounds {
  135. uptr bottom;
  136. uptr top;
  137. };
  138. StackBounds GetStackBounds() const;
  139. void GetStartData(void *out, uptr out_size) const;
  140. AsanThreadContext *context_;
  141. uptr stack_top_;
  142. uptr stack_bottom_;
  143. // these variables are used when the thread is about to switch stack
  144. uptr next_stack_top_;
  145. uptr next_stack_bottom_;
  146. // true if switching is in progress
  147. atomic_uint8_t stack_switching_;
  148. uptr tls_begin_;
  149. uptr tls_end_;
  150. DTLS *dtls_;
  151. FakeStack *fake_stack_;
  152. AsanThreadLocalMallocStorage malloc_storage_;
  153. AsanStats stats_;
  154. bool unwinding_;
  155. uptr extra_spill_area_;
  156. char start_data_[];
  157. };
  158. #if defined(_MSC_VER) && !defined(__clang__)
  159. # pragma warning(pop)
  160. #endif
  161. // Returns a single instance of registry.
  162. ThreadRegistry &asanThreadRegistry();
  163. ThreadArgRetval &asanThreadArgRetval();
  164. // Must be called under ThreadRegistryLock.
  165. AsanThreadContext *GetThreadContextByTidLocked(u32 tid);
  166. // Get the current thread. May return 0.
  167. AsanThread *GetCurrentThread();
  168. void SetCurrentThread(AsanThread *t);
  169. u32 GetCurrentTidOrInvalid();
  170. AsanThread *FindThreadByStackAddress(uptr addr);
  171. // Used to handle fork().
  172. void EnsureMainThreadIDIsCorrect();
  173. } // namespace __asan
  174. #endif // ASAN_THREAD_H