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_stats.h" |
Sergey Matveev | e86e35f | 2013-10-14 12:01:05 +0000 | [diff] [blame] | 21 | #include "sanitizer_common/sanitizer_common.h" |
Kostya Serebryany | b134ffa | 2012-07-17 07:20:13 +0000 | [diff] [blame] | 22 | #include "sanitizer_common/sanitizer_libc.h" |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 23 | #include "sanitizer_common/sanitizer_thread_registry.h" |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 24 | |
| 25 | namespace __asan { |
| 26 | |
Kostya Serebryany | e0cff0b | 2012-06-06 15:06:58 +0000 | [diff] [blame] | 27 | const u32 kInvalidTid = 0xffffff; // Must fit into 24 bits. |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 28 | const u32 kMaxNumberOfThreads = (1 << 22); // 4M |
Kostya Serebryany | e0cff0b | 2012-06-06 15:06:58 +0000 | [diff] [blame] | 29 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 30 | class AsanThread; |
| 31 | |
| 32 | // These objects are created for every thread and are never deleted, |
| 33 | // 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] | 34 | class AsanThreadContext : public ThreadContextBase { |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 35 | public: |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 36 | explicit AsanThreadContext(int tid) |
| 37 | : ThreadContextBase(tid), |
| 38 | announced(false), |
Sergey Matveev | e86e35f | 2013-10-14 12:01:05 +0000 | [diff] [blame] | 39 | destructor_iterations(kPthreadDestructorIterations), |
Kostya Serebryany | 6d95869 | 2013-10-18 14:50:44 +0000 | [diff] [blame] | 40 | stack_id(0), |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 41 | thread(0) { |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 42 | } |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 43 | bool announced; |
Kostya Serebryany | 6d95869 | 2013-10-18 14:50:44 +0000 | [diff] [blame] | 44 | u8 destructor_iterations; |
| 45 | u32 stack_id; |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 46 | AsanThread *thread; |
Kostya Serebryany | f58f998 | 2012-02-07 00:27:15 +0000 | [diff] [blame] | 47 | |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame^] | 48 | void OnCreated(void *arg) override; |
| 49 | void OnFinished() override; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 52 | // AsanThreadContext objects are never freed, so we need many of them. |
Kostya Serebryany | 6d95869 | 2013-10-18 14:50:44 +0000 | [diff] [blame] | 53 | COMPILER_CHECK(sizeof(AsanThreadContext) <= 256); |
Kostya Serebryany | 716e2f2 | 2012-12-07 15:15:01 +0000 | [diff] [blame] | 54 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 55 | // AsanThread are stored in TSD and destroyed when the thread dies. |
| 56 | class AsanThread { |
| 57 | public: |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 58 | static AsanThread *Create(thread_callback_t start_routine, void *arg, |
| 59 | u32 parent_tid, StackTrace *stack, bool detached); |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 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. |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 64 | thread_return_t ThreadStart(uptr os_id, |
| 65 | atomic_uintptr_t *signal_thread_is_registered); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 66 | |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 67 | uptr stack_top() { return stack_top_; } |
| 68 | uptr stack_bottom() { return stack_bottom_; } |
Kostya Serebryany | 621770a | 2013-09-19 14:59:52 +0000 | [diff] [blame] | 69 | uptr stack_size() { return stack_size_; } |
Sergey Matveev | 12d01ba | 2013-05-29 13:09:44 +0000 | [diff] [blame] | 70 | uptr tls_begin() { return tls_begin_; } |
| 71 | uptr tls_end() { return tls_end_; } |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 72 | u32 tid() { return context_->tid; } |
| 73 | AsanThreadContext *context() { return context_; } |
| 74 | void set_context(AsanThreadContext *context) { context_ = context; } |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 75 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 76 | struct StackFrameAccess { |
| 77 | uptr offset; |
| 78 | uptr frame_pc; |
| 79 | const char *frame_descr; |
| 80 | }; |
| 81 | bool GetStackFrameAccessByAddr(uptr addr, StackFrameAccess *access); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 82 | |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 83 | bool AddrIsInStack(uptr addr) { |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 84 | return addr >= stack_bottom_ && addr < stack_top_; |
| 85 | } |
| 86 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 87 | void DeleteFakeStack(int tid) { |
Kostya Serebryany | 7a0bba4 | 2013-06-26 12:16:05 +0000 | [diff] [blame] | 88 | if (!fake_stack_) return; |
Kostya Serebryany | 9433af3 | 2013-09-13 06:32:26 +0000 | [diff] [blame] | 89 | FakeStack *t = fake_stack_; |
| 90 | fake_stack_ = 0; |
| 91 | SetTLSFakeStack(0); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 92 | t->Destroy(tid); |
Kostya Serebryany | 7a0bba4 | 2013-06-26 12:16:05 +0000 | [diff] [blame] | 93 | } |
Kostya Serebryany | ac3ae5d | 2013-09-12 07:11:58 +0000 | [diff] [blame] | 94 | |
Kostya Serebryany | dcf98bf | 2013-09-12 08:43:44 +0000 | [diff] [blame] | 95 | bool has_fake_stack() { |
| 96 | return (reinterpret_cast<uptr>(fake_stack_) > 1); |
| 97 | } |
| 98 | |
Kostya Serebryany | ac3ae5d | 2013-09-12 07:11:58 +0000 | [diff] [blame] | 99 | FakeStack *fake_stack() { |
Kostya Serebryany | 230e52f | 2013-09-18 10:35:12 +0000 | [diff] [blame] | 100 | if (!__asan_option_detect_stack_use_after_return) |
| 101 | return 0; |
Kostya Serebryany | dcf98bf | 2013-09-12 08:43:44 +0000 | [diff] [blame] | 102 | if (!has_fake_stack()) |
Kostya Serebryany | c98fc1f | 2013-09-12 08:34:50 +0000 | [diff] [blame] | 103 | return AsyncSignalSafeLazyInitFakeStack(); |
Kostya Serebryany | ac3ae5d | 2013-09-12 07:11:58 +0000 | [diff] [blame] | 104 | return fake_stack_; |
| 105 | } |
Kostya Serebryany | 7a0bba4 | 2013-06-26 12:16:05 +0000 | [diff] [blame] | 106 | |
Evgeniy Stepanov | 96a575f | 2013-09-12 08:16:28 +0000 | [diff] [blame] | 107 | // True is this thread is currently unwinding stack (i.e. collecting a stack |
| 108 | // trace). Used to prevent deadlocks on platforms where libc unwinder calls |
| 109 | // malloc internally. See PR17116 for more details. |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 110 | bool isUnwinding() const { return unwinding_; } |
| 111 | void setUnwinding(bool b) { unwinding_ = b; } |
Evgeniy Stepanov | 96a575f | 2013-09-12 08:16:28 +0000 | [diff] [blame] | 112 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 113 | // True if we are in a deadly signal handler. |
| 114 | bool isInDeadlySignal() const { return in_deadly_signal_; } |
| 115 | void setInDeadlySignal(bool b) { in_deadly_signal_ = b; } |
| 116 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 117 | AsanThreadLocalMallocStorage &malloc_storage() { return malloc_storage_; } |
| 118 | AsanStats &stats() { return stats_; } |
| 119 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 120 | private: |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 121 | // NOTE: There is no AsanThread constructor. It is allocated |
| 122 | // via mmap() and *must* be valid in zero-initialized state. |
Sergey Matveev | 12d01ba | 2013-05-29 13:09:44 +0000 | [diff] [blame] | 123 | void SetThreadStackAndTls(); |
| 124 | void ClearShadowForThreadStackAndTLS(); |
Kostya Serebryany | c98fc1f | 2013-09-12 08:34:50 +0000 | [diff] [blame] | 125 | FakeStack *AsyncSignalSafeLazyInitFakeStack(); |
| 126 | |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 127 | AsanThreadContext *context_; |
Timur Iskhodzhanov | 600972e | 2012-02-24 15:28:43 +0000 | [diff] [blame] | 128 | thread_callback_t start_routine_; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 129 | void *arg_; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 130 | uptr stack_top_; |
| 131 | uptr stack_bottom_; |
Kostya Serebryany | 621770a | 2013-09-19 14:59:52 +0000 | [diff] [blame] | 132 | // stack_size_ == stack_top_ - stack_bottom_; |
| 133 | // It needs to be set in a async-signal-safe manner. |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 134 | uptr stack_size_; |
Sergey Matveev | 12d01ba | 2013-05-29 13:09:44 +0000 | [diff] [blame] | 135 | uptr tls_begin_; |
| 136 | uptr tls_end_; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 137 | |
Kostya Serebryany | 7a0bba4 | 2013-06-26 12:16:05 +0000 | [diff] [blame] | 138 | FakeStack *fake_stack_; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 139 | AsanThreadLocalMallocStorage malloc_storage_; |
| 140 | AsanStats stats_; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 141 | bool unwinding_; |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 142 | bool in_deadly_signal_; |
Evgeniy Stepanov | 96a575f | 2013-09-12 08:16:28 +0000 | [diff] [blame] | 143 | }; |
| 144 | |
| 145 | // ScopedUnwinding is a scope for stacktracing member of a context |
| 146 | class ScopedUnwinding { |
| 147 | public: |
| 148 | explicit ScopedUnwinding(AsanThread *t) : thread(t) { |
| 149 | t->setUnwinding(true); |
| 150 | } |
| 151 | ~ScopedUnwinding() { thread->setUnwinding(false); } |
| 152 | |
| 153 | private: |
| 154 | AsanThread *thread; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 157 | // ScopedDeadlySignal is a scope for handling deadly signals. |
| 158 | class ScopedDeadlySignal { |
| 159 | public: |
| 160 | explicit ScopedDeadlySignal(AsanThread *t) : thread(t) { |
| 161 | if (thread) thread->setInDeadlySignal(true); |
| 162 | } |
| 163 | ~ScopedDeadlySignal() { |
| 164 | if (thread) thread->setInDeadlySignal(false); |
| 165 | } |
| 166 | |
| 167 | private: |
| 168 | AsanThread *thread; |
| 169 | }; |
| 170 | |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 171 | // Returns a single instance of registry. |
| 172 | ThreadRegistry &asanThreadRegistry(); |
| 173 | |
| 174 | // Must be called under ThreadRegistryLock. |
| 175 | AsanThreadContext *GetThreadContextByTidLocked(u32 tid); |
| 176 | |
Alexey Samsonov | 89c1384 | 2013-03-20 09:23:28 +0000 | [diff] [blame] | 177 | // Get the current thread. May return 0. |
| 178 | AsanThread *GetCurrentThread(); |
| 179 | void SetCurrentThread(AsanThread *t); |
| 180 | u32 GetCurrentTidOrInvalid(); |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 181 | AsanThread *FindThreadByStackAddress(uptr addr); |
Alexey Samsonov | 89c1384 | 2013-03-20 09:23:28 +0000 | [diff] [blame] | 182 | |
Sergey Matveev | c6ac98d | 2013-07-08 12:57:24 +0000 | [diff] [blame] | 183 | // Used to handle fork(). |
| 184 | void EnsureMainThreadIDIsCorrect(); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 185 | } // namespace __asan |
| 186 | |
| 187 | #endif // ASAN_THREAD_H |