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" |
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), |
| 39 | thread(0) { |
| 40 | internal_memset(&stack, 0, sizeof(stack)); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 41 | } |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 42 | bool announced; |
| 43 | StackTrace stack; |
| 44 | AsanThread *thread; |
Kostya Serebryany | f58f998 | 2012-02-07 00:27:15 +0000 | [diff] [blame] | 45 | |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 46 | void OnCreated(void *arg); |
| 47 | void OnFinished(); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 48 | }; |
| 49 | |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 50 | // AsanThreadContext objects are never freed, so we need many of them. |
| 51 | COMPILER_CHECK(sizeof(AsanThreadContext) <= 4096); |
Kostya Serebryany | 716e2f2 | 2012-12-07 15:15:01 +0000 | [diff] [blame] | 52 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 53 | // AsanThread are stored in TSD and destroyed when the thread dies. |
| 54 | class AsanThread { |
| 55 | public: |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 56 | static AsanThread *Create(thread_callback_t start_routine, void *arg); |
| 57 | static void TSDDtor(void *tsd); |
Kostya Serebryany | a6b5226 | 2012-01-06 19:44:11 +0000 | [diff] [blame] | 58 | void Destroy(); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 59 | |
Kostya Serebryany | 69eca73 | 2011-12-16 19:13:35 +0000 | [diff] [blame] | 60 | void Init(); // Should be called from the thread itself. |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 61 | thread_return_t ThreadStart(uptr os_id); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 62 | |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 63 | uptr stack_top() { return stack_top_; } |
| 64 | uptr stack_bottom() { return stack_bottom_; } |
| 65 | uptr stack_size() { return stack_top_ - stack_bottom_; } |
Sergey Matveev | 12d01ba | 2013-05-29 13:09:44 +0000 | [diff] [blame] | 66 | uptr tls_begin() { return tls_begin_; } |
| 67 | uptr tls_end() { return tls_end_; } |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 68 | u32 tid() { return context_->tid; } |
| 69 | AsanThreadContext *context() { return context_; } |
| 70 | void set_context(AsanThreadContext *context) { context_ = context; } |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 71 | |
Kostya Serebryany | 50f3daa | 2013-03-22 10:36:24 +0000 | [diff] [blame] | 72 | const char *GetFrameNameByAddr(uptr addr, uptr *offset, uptr *frame_pc); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 73 | |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 74 | bool AddrIsInStack(uptr addr) { |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 75 | return addr >= stack_bottom_ && addr < stack_top_; |
| 76 | } |
| 77 | |
Kostya Serebryany | 7a0bba4 | 2013-06-26 12:16:05 +0000 | [diff] [blame] | 78 | void DeleteFakeStack() { |
| 79 | if (!fake_stack_) return; |
Kostya Serebryany | ac3ae5d | 2013-09-12 07:11:58 +0000 | [diff] [blame] | 80 | fake_stack_->PoisonAll(0); |
| 81 | fake_stack_->Destroy(); |
Kostya Serebryany | 7a0bba4 | 2013-06-26 12:16:05 +0000 | [diff] [blame] | 82 | } |
Kostya Serebryany | ac3ae5d | 2013-09-12 07:11:58 +0000 | [diff] [blame] | 83 | |
| 84 | FakeStack *fake_stack() { |
| 85 | if (!fake_stack_) // FIXME: lazy init is not async-signal safe. |
| 86 | fake_stack_ = FakeStack::Create(Log2(RoundUpToPowerOfTwo(stack_size()))); |
| 87 | return fake_stack_; |
| 88 | } |
Kostya Serebryany | 7a0bba4 | 2013-06-26 12:16:05 +0000 | [diff] [blame] | 89 | |
Evgeniy Stepanov | 96a575f | 2013-09-12 08:16:28 +0000 | [diff] [blame^] | 90 | // True is this thread is currently unwinding stack (i.e. collecting a stack |
| 91 | // trace). Used to prevent deadlocks on platforms where libc unwinder calls |
| 92 | // malloc internally. See PR17116 for more details. |
| 93 | bool isUnwinding() const { return unwinding; } |
| 94 | void setUnwinding(bool b) { unwinding = b; } |
| 95 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 96 | AsanThreadLocalMallocStorage &malloc_storage() { return malloc_storage_; } |
| 97 | AsanStats &stats() { return stats_; } |
| 98 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 99 | private: |
Evgeniy Stepanov | 96a575f | 2013-09-12 08:16:28 +0000 | [diff] [blame^] | 100 | AsanThread() : unwinding(false) {} |
Sergey Matveev | 12d01ba | 2013-05-29 13:09:44 +0000 | [diff] [blame] | 101 | void SetThreadStackAndTls(); |
| 102 | void ClearShadowForThreadStackAndTLS(); |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 103 | AsanThreadContext *context_; |
Timur Iskhodzhanov | 600972e | 2012-02-24 15:28:43 +0000 | [diff] [blame] | 104 | thread_callback_t start_routine_; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 105 | void *arg_; |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 106 | uptr stack_top_; |
| 107 | uptr stack_bottom_; |
Sergey Matveev | 12d01ba | 2013-05-29 13:09:44 +0000 | [diff] [blame] | 108 | uptr tls_begin_; |
| 109 | uptr tls_end_; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 110 | |
Kostya Serebryany | 7a0bba4 | 2013-06-26 12:16:05 +0000 | [diff] [blame] | 111 | FakeStack *fake_stack_; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 112 | AsanThreadLocalMallocStorage malloc_storage_; |
| 113 | AsanStats stats_; |
Evgeniy Stepanov | 96a575f | 2013-09-12 08:16:28 +0000 | [diff] [blame^] | 114 | bool unwinding; |
| 115 | }; |
| 116 | |
| 117 | // ScopedUnwinding is a scope for stacktracing member of a context |
| 118 | class ScopedUnwinding { |
| 119 | public: |
| 120 | explicit ScopedUnwinding(AsanThread *t) : thread(t) { |
| 121 | t->setUnwinding(true); |
| 122 | } |
| 123 | ~ScopedUnwinding() { thread->setUnwinding(false); } |
| 124 | |
| 125 | private: |
| 126 | AsanThread *thread; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 127 | }; |
| 128 | |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 129 | struct CreateThreadContextArgs { |
| 130 | AsanThread *thread; |
| 131 | StackTrace *stack; |
| 132 | }; |
| 133 | |
| 134 | // Returns a single instance of registry. |
| 135 | ThreadRegistry &asanThreadRegistry(); |
| 136 | |
| 137 | // Must be called under ThreadRegistryLock. |
| 138 | AsanThreadContext *GetThreadContextByTidLocked(u32 tid); |
| 139 | |
Alexey Samsonov | 89c1384 | 2013-03-20 09:23:28 +0000 | [diff] [blame] | 140 | // Get the current thread. May return 0. |
| 141 | AsanThread *GetCurrentThread(); |
| 142 | void SetCurrentThread(AsanThread *t); |
| 143 | u32 GetCurrentTidOrInvalid(); |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 144 | AsanThread *FindThreadByStackAddress(uptr addr); |
Alexey Samsonov | 89c1384 | 2013-03-20 09:23:28 +0000 | [diff] [blame] | 145 | |
Sergey Matveev | c6ac98d | 2013-07-08 12:57:24 +0000 | [diff] [blame] | 146 | // Used to handle fork(). |
| 147 | void EnsureMainThreadIDIsCorrect(); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 148 | } // namespace __asan |
| 149 | |
| 150 | #endif // ASAN_THREAD_H |