Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 1 | //===-- asan_thread.h -------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file is a part of AddressSanitizer, an address sanity checker. |
| 11 | // |
| 12 | // ASan-private header for asan_thread.cc. |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | #ifndef ASAN_THREAD_H |
| 15 | #define ASAN_THREAD_H |
| 16 | |
| 17 | #include "asan_allocator.h" |
| 18 | #include "asan_internal.h" |
Kostya Serebryany | 244384d | 2013-04-11 11:39:19 +0000 | [diff] [blame] | 19 | #include "asan_fake_stack.h" |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 20 | #include "asan_stack.h" |
| 21 | #include "asan_stats.h" |
Sergey Matveev | e86e35f | 2013-10-14 12:01:05 +0000 | [diff] [blame] | 22 | #include "sanitizer_common/sanitizer_common.h" |
Kostya Serebryany | b134ffa | 2012-07-17 07:20:13 +0000 | [diff] [blame] | 23 | #include "sanitizer_common/sanitizer_libc.h" |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 24 | #include "sanitizer_common/sanitizer_thread_registry.h" |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 25 | |
| 26 | namespace __asan { |
| 27 | |
Kostya Serebryany | e0cff0b | 2012-06-06 15:06:58 +0000 | [diff] [blame] | 28 | const u32 kInvalidTid = 0xffffff; // Must fit into 24 bits. |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 29 | const u32 kMaxNumberOfThreads = (1 << 22); // 4M |
Kostya Serebryany | e0cff0b | 2012-06-06 15:06:58 +0000 | [diff] [blame] | 30 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 31 | class AsanThread; |
| 32 | |
| 33 | // These objects are created for every thread and are never deleted, |
| 34 | // so we can find them by tid even if the thread is long dead. |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 35 | class AsanThreadContext : public ThreadContextBase { |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 36 | public: |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 37 | explicit AsanThreadContext(int tid) |
| 38 | : ThreadContextBase(tid), |
| 39 | announced(false), |
Sergey Matveev | e86e35f | 2013-10-14 12:01:05 +0000 | [diff] [blame] | 40 | destructor_iterations(kPthreadDestructorIterations), |
Kostya Serebryany | 6d95869 | 2013-10-18 14:50:44 +0000 | [diff] [blame] | 41 | stack_id(0), |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 42 | thread(0) { |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 43 | } |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 44 | bool announced; |
Kostya Serebryany | 6d95869 | 2013-10-18 14:50:44 +0000 | [diff] [blame] | 45 | u8 destructor_iterations; |
| 46 | u32 stack_id; |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 47 | AsanThread *thread; |
Kostya Serebryany | f58f998 | 2012-02-07 00:27:15 +0000 | [diff] [blame] | 48 | |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 49 | void OnCreated(void *arg); |
| 50 | void OnFinished(); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 53 | // AsanThreadContext objects are never freed, so we need many of them. |
Kostya Serebryany | 6d95869 | 2013-10-18 14:50:44 +0000 | [diff] [blame] | 54 | COMPILER_CHECK(sizeof(AsanThreadContext) <= 256); |
Kostya Serebryany | 716e2f2 | 2012-12-07 15:15:01 +0000 | [diff] [blame] | 55 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 56 | // AsanThread are stored in TSD and destroyed when the thread dies. |
| 57 | class AsanThread { |
| 58 | public: |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 59 | static AsanThread *Create(thread_callback_t start_routine, void *arg); |
| 60 | static void TSDDtor(void *tsd); |
Kostya Serebryany | a6b5226 | 2012-01-06 19:44:11 +0000 | [diff] [blame] | 61 | void Destroy(); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 62 | |
Kostya Serebryany | 69eca73 | 2011-12-16 19:13:35 +0000 | [diff] [blame] | 63 | void Init(); // Should be called from the thread itself. |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 64 | thread_return_t ThreadStart(uptr os_id); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 65 | |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 66 | uptr stack_top() { return stack_top_; } |
| 67 | uptr stack_bottom() { return stack_bottom_; } |
Kostya Serebryany | 621770a | 2013-09-19 14:59:52 +0000 | [diff] [blame] | 68 | uptr stack_size() { return stack_size_; } |
Sergey Matveev | 12d01ba | 2013-05-29 13:09:44 +0000 | [diff] [blame] | 69 | uptr tls_begin() { return tls_begin_; } |
| 70 | uptr tls_end() { return tls_end_; } |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 71 | u32 tid() { return context_->tid; } |
| 72 | AsanThreadContext *context() { return context_; } |
| 73 | void set_context(AsanThreadContext *context) { context_ = context; } |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 74 | |
Kostya Serebryany | 50f3daa | 2013-03-22 10:36:24 +0000 | [diff] [blame] | 75 | const char *GetFrameNameByAddr(uptr addr, uptr *offset, uptr *frame_pc); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 76 | |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 77 | bool AddrIsInStack(uptr addr) { |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 78 | return addr >= stack_bottom_ && addr < stack_top_; |
| 79 | } |
| 80 | |
Kostya Serebryany | 7a0bba4 | 2013-06-26 12:16:05 +0000 | [diff] [blame] | 81 | void DeleteFakeStack() { |
| 82 | if (!fake_stack_) return; |
Kostya Serebryany | 9433af3 | 2013-09-13 06:32:26 +0000 | [diff] [blame] | 83 | FakeStack *t = fake_stack_; |
| 84 | fake_stack_ = 0; |
| 85 | SetTLSFakeStack(0); |
| 86 | t->Destroy(); |
Kostya Serebryany | 7a0bba4 | 2013-06-26 12:16:05 +0000 | [diff] [blame] | 87 | } |
Kostya Serebryany | ac3ae5d | 2013-09-12 07:11:58 +0000 | [diff] [blame] | 88 | |
Kostya Serebryany | dcf98bf | 2013-09-12 08:43:44 +0000 | [diff] [blame] | 89 | bool has_fake_stack() { |
| 90 | return (reinterpret_cast<uptr>(fake_stack_) > 1); |
| 91 | } |
| 92 | |
Kostya Serebryany | ac3ae5d | 2013-09-12 07:11:58 +0000 | [diff] [blame] | 93 | FakeStack *fake_stack() { |
Kostya Serebryany | 230e52f | 2013-09-18 10:35:12 +0000 | [diff] [blame] | 94 | if (!__asan_option_detect_stack_use_after_return) |
| 95 | return 0; |
Kostya Serebryany | dcf98bf | 2013-09-12 08:43:44 +0000 | [diff] [blame] | 96 | if (!has_fake_stack()) |
Kostya Serebryany | c98fc1f | 2013-09-12 08:34:50 +0000 | [diff] [blame] | 97 | return AsyncSignalSafeLazyInitFakeStack(); |
Kostya Serebryany | ac3ae5d | 2013-09-12 07:11:58 +0000 | [diff] [blame] | 98 | return fake_stack_; |
| 99 | } |
Kostya Serebryany | 7a0bba4 | 2013-06-26 12:16:05 +0000 | [diff] [blame] | 100 | |
Evgeniy Stepanov | 96a575f | 2013-09-12 08:16:28 +0000 | [diff] [blame] | 101 | // True is this thread is currently unwinding stack (i.e. collecting a stack |
| 102 | // trace). Used to prevent deadlocks on platforms where libc unwinder calls |
| 103 | // malloc internally. See PR17116 for more details. |
| 104 | bool isUnwinding() const { return unwinding; } |
| 105 | void setUnwinding(bool b) { unwinding = b; } |
| 106 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 107 | AsanThreadLocalMallocStorage &malloc_storage() { return malloc_storage_; } |
| 108 | AsanStats &stats() { return stats_; } |
| 109 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 110 | private: |
Evgeniy Stepanov | 96a575f | 2013-09-12 08:16:28 +0000 | [diff] [blame] | 111 | AsanThread() : unwinding(false) {} |
Sergey Matveev | 12d01ba | 2013-05-29 13:09:44 +0000 | [diff] [blame] | 112 | void SetThreadStackAndTls(); |
| 113 | void ClearShadowForThreadStackAndTLS(); |
Kostya Serebryany | c98fc1f | 2013-09-12 08:34:50 +0000 | [diff] [blame] | 114 | FakeStack *AsyncSignalSafeLazyInitFakeStack(); |
| 115 | |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 116 | AsanThreadContext *context_; |
Timur Iskhodzhanov | 600972e | 2012-02-24 15:28:43 +0000 | [diff] [blame] | 117 | thread_callback_t start_routine_; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 118 | void *arg_; |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 119 | uptr stack_top_; |
| 120 | uptr stack_bottom_; |
Kostya Serebryany | 621770a | 2013-09-19 14:59:52 +0000 | [diff] [blame] | 121 | // stack_size_ == stack_top_ - stack_bottom_; |
| 122 | // It needs to be set in a async-signal-safe manner. |
| 123 | uptr stack_size_; |
Sergey Matveev | 12d01ba | 2013-05-29 13:09:44 +0000 | [diff] [blame] | 124 | uptr tls_begin_; |
| 125 | uptr tls_end_; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 126 | |
Kostya Serebryany | 7a0bba4 | 2013-06-26 12:16:05 +0000 | [diff] [blame] | 127 | FakeStack *fake_stack_; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 128 | AsanThreadLocalMallocStorage malloc_storage_; |
| 129 | AsanStats stats_; |
Evgeniy Stepanov | 96a575f | 2013-09-12 08:16:28 +0000 | [diff] [blame] | 130 | bool unwinding; |
| 131 | }; |
| 132 | |
| 133 | // ScopedUnwinding is a scope for stacktracing member of a context |
| 134 | class ScopedUnwinding { |
| 135 | public: |
| 136 | explicit ScopedUnwinding(AsanThread *t) : thread(t) { |
| 137 | t->setUnwinding(true); |
| 138 | } |
| 139 | ~ScopedUnwinding() { thread->setUnwinding(false); } |
| 140 | |
| 141 | private: |
| 142 | AsanThread *thread; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 143 | }; |
| 144 | |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 145 | struct CreateThreadContextArgs { |
| 146 | AsanThread *thread; |
| 147 | StackTrace *stack; |
| 148 | }; |
| 149 | |
| 150 | // Returns a single instance of registry. |
| 151 | ThreadRegistry &asanThreadRegistry(); |
| 152 | |
| 153 | // Must be called under ThreadRegistryLock. |
| 154 | AsanThreadContext *GetThreadContextByTidLocked(u32 tid); |
| 155 | |
Alexey Samsonov | 89c1384 | 2013-03-20 09:23:28 +0000 | [diff] [blame] | 156 | // Get the current thread. May return 0. |
| 157 | AsanThread *GetCurrentThread(); |
| 158 | void SetCurrentThread(AsanThread *t); |
| 159 | u32 GetCurrentTidOrInvalid(); |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 160 | AsanThread *FindThreadByStackAddress(uptr addr); |
Alexey Samsonov | 89c1384 | 2013-03-20 09:23:28 +0000 | [diff] [blame] | 161 | |
Sergey Matveev | c6ac98d | 2013-07-08 12:57:24 +0000 | [diff] [blame] | 162 | // Used to handle fork(). |
| 163 | void EnsureMainThreadIDIsCorrect(); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 164 | } // namespace __asan |
| 165 | |
| 166 | #endif // ASAN_THREAD_H |