asan_thread.h 5.4 KB

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