asan_thread.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //===-- asan_thread.h -------------------------------------------*- C++ -*-===//
  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. // ASan-private header for asan_thread.cc.
  11. //===----------------------------------------------------------------------===//
  12. #ifndef ASAN_THREAD_H
  13. #define ASAN_THREAD_H
  14. #include "asan_allocator.h"
  15. #include "asan_internal.h"
  16. #include "asan_fake_stack.h"
  17. #include "asan_stats.h"
  18. #include "sanitizer_common/sanitizer_common.h"
  19. #include "sanitizer_common/sanitizer_libc.h"
  20. #include "sanitizer_common/sanitizer_thread_registry.h"
  21. namespace __asan {
  22. const u32 kInvalidTid = 0xffffff; // Must fit into 24 bits.
  23. const u32 kMaxNumberOfThreads = (1 << 22); // 4M
  24. class AsanThread;
  25. // These objects are created for every thread and are never deleted,
  26. // so we can find them by tid even if the thread is long dead.
  27. class AsanThreadContext : public ThreadContextBase {
  28. public:
  29. explicit AsanThreadContext(int tid)
  30. : ThreadContextBase(tid),
  31. announced(false),
  32. destructor_iterations(kPthreadDestructorIterations),
  33. stack_id(0),
  34. thread(0) {
  35. }
  36. bool announced;
  37. u8 destructor_iterations;
  38. u32 stack_id;
  39. AsanThread *thread;
  40. void OnCreated(void *arg);
  41. void OnFinished();
  42. };
  43. // AsanThreadContext objects are never freed, so we need many of them.
  44. COMPILER_CHECK(sizeof(AsanThreadContext) <= 256);
  45. // AsanThread are stored in TSD and destroyed when the thread dies.
  46. class AsanThread {
  47. public:
  48. static AsanThread *Create(thread_callback_t start_routine, void *arg);
  49. static void TSDDtor(void *tsd);
  50. void Destroy();
  51. void Init(); // Should be called from the thread itself.
  52. thread_return_t ThreadStart(uptr os_id);
  53. uptr stack_top() { return stack_top_; }
  54. uptr stack_bottom() { return stack_bottom_; }
  55. uptr stack_size() { return stack_size_; }
  56. uptr tls_begin() { return tls_begin_; }
  57. uptr tls_end() { return tls_end_; }
  58. u32 tid() { return context_->tid; }
  59. AsanThreadContext *context() { return context_; }
  60. void set_context(AsanThreadContext *context) { context_ = context; }
  61. struct StackFrameAccess {
  62. uptr offset;
  63. uptr frame_pc;
  64. const char *frame_descr;
  65. };
  66. bool GetStackFrameAccessByAddr(uptr addr, StackFrameAccess *access);
  67. bool AddrIsInStack(uptr addr) {
  68. return addr >= stack_bottom_ && addr < stack_top_;
  69. }
  70. void DeleteFakeStack(int tid) {
  71. if (!fake_stack_) return;
  72. FakeStack *t = fake_stack_;
  73. fake_stack_ = 0;
  74. SetTLSFakeStack(0);
  75. t->Destroy(tid);
  76. }
  77. bool has_fake_stack() {
  78. return (reinterpret_cast<uptr>(fake_stack_) > 1);
  79. }
  80. FakeStack *fake_stack() {
  81. if (!__asan_option_detect_stack_use_after_return)
  82. return 0;
  83. if (!has_fake_stack())
  84. return AsyncSignalSafeLazyInitFakeStack();
  85. return fake_stack_;
  86. }
  87. // True is this thread is currently unwinding stack (i.e. collecting a stack
  88. // trace). Used to prevent deadlocks on platforms where libc unwinder calls
  89. // malloc internally. See PR17116 for more details.
  90. bool isUnwinding() const { return unwinding_; }
  91. void setUnwinding(bool b) { unwinding_ = b; }
  92. // True if we are in a deadly signal handler.
  93. bool isInDeadlySignal() const { return in_deadly_signal_; }
  94. void setInDeadlySignal(bool b) { in_deadly_signal_ = b; }
  95. AsanThreadLocalMallocStorage &malloc_storage() { return malloc_storage_; }
  96. AsanStats &stats() { return stats_; }
  97. private:
  98. // NOTE: There is no AsanThread constructor. It is allocated
  99. // via mmap() and *must* be valid in zero-initialized state.
  100. void SetThreadStackAndTls();
  101. void ClearShadowForThreadStackAndTLS();
  102. FakeStack *AsyncSignalSafeLazyInitFakeStack();
  103. AsanThreadContext *context_;
  104. thread_callback_t start_routine_;
  105. void *arg_;
  106. uptr stack_top_;
  107. uptr stack_bottom_;
  108. // stack_size_ == stack_top_ - stack_bottom_;
  109. // It needs to be set in a async-signal-safe manner.
  110. uptr stack_size_;
  111. uptr tls_begin_;
  112. uptr tls_end_;
  113. FakeStack *fake_stack_;
  114. AsanThreadLocalMallocStorage malloc_storage_;
  115. AsanStats stats_;
  116. bool unwinding_;
  117. bool in_deadly_signal_;
  118. };
  119. // ScopedUnwinding is a scope for stacktracing member of a context
  120. class ScopedUnwinding {
  121. public:
  122. explicit ScopedUnwinding(AsanThread *t) : thread(t) {
  123. t->setUnwinding(true);
  124. }
  125. ~ScopedUnwinding() { thread->setUnwinding(false); }
  126. private:
  127. AsanThread *thread;
  128. };
  129. // ScopedDeadlySignal is a scope for handling deadly signals.
  130. class ScopedDeadlySignal {
  131. public:
  132. explicit ScopedDeadlySignal(AsanThread *t) : thread(t) {
  133. if (thread) thread->setInDeadlySignal(true);
  134. }
  135. ~ScopedDeadlySignal() {
  136. if (thread) thread->setInDeadlySignal(false);
  137. }
  138. private:
  139. AsanThread *thread;
  140. };
  141. struct CreateThreadContextArgs {
  142. AsanThread *thread;
  143. StackTrace *stack;
  144. };
  145. // Returns a single instance of registry.
  146. ThreadRegistry &asanThreadRegistry();
  147. // Must be called under ThreadRegistryLock.
  148. AsanThreadContext *GetThreadContextByTidLocked(u32 tid);
  149. // Get the current thread. May return 0.
  150. AsanThread *GetCurrentThread();
  151. void SetCurrentThread(AsanThread *t);
  152. u32 GetCurrentTidOrInvalid();
  153. AsanThread *FindThreadByStackAddress(uptr addr);
  154. // Used to handle fork().
  155. void EnsureMainThreadIDIsCorrect();
  156. } // namespace __asan
  157. #endif // ASAN_THREAD_H