Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 | [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" |
| 19 | #include "asan_stack.h" |
| 20 | #include "asan_stats.h" |
| 21 | |
| 22 | namespace __asan { |
| 23 | |
| 24 | const size_t kMaxThreadStackSize = 16 * (1 << 20); // 16M |
| 25 | |
| 26 | class AsanThread; |
| 27 | |
| 28 | // These objects are created for every thread and are never deleted, |
| 29 | // so we can find them by tid even if the thread is long dead. |
| 30 | class AsanThreadSummary { |
| 31 | public: |
| 32 | explicit AsanThreadSummary(LinkerInitialized) { } // for T0. |
Alexey Samsonov | 2d3a67b | 2012-01-17 06:35:31 | [diff] [blame^] | 33 | AsanThreadSummary(int parent_tid, AsanStackTrace *stack) |
| 34 | : parent_tid_(parent_tid), |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 | [diff] [blame] | 35 | announced_(false) { |
Alexey Samsonov | 2d3a67b | 2012-01-17 06:35:31 | [diff] [blame^] | 36 | tid_ = -1; |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 | [diff] [blame] | 37 | if (stack) { |
| 38 | stack_ = *stack; |
| 39 | } |
| 40 | thread_ = 0; |
| 41 | } |
| 42 | void Announce() { |
| 43 | if (tid_ == 0) return; // no need to announce the main thread. |
| 44 | if (!announced_) { |
| 45 | announced_ = true; |
| 46 | Printf("Thread T%d created by T%d here:\n", tid_, parent_tid_); |
| 47 | stack_.PrintStack(); |
| 48 | } |
| 49 | } |
| 50 | int tid() { return tid_; } |
Alexey Samsonov | 2d3a67b | 2012-01-17 06:35:31 | [diff] [blame^] | 51 | void set_tid(int tid) { tid_ = tid; } |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 | [diff] [blame] | 52 | AsanThread *thread() { return thread_; } |
| 53 | void set_thread(AsanThread *thread) { thread_ = thread; } |
| 54 | private: |
| 55 | int tid_; |
| 56 | int parent_tid_; |
| 57 | bool announced_; |
| 58 | AsanStackTrace stack_; |
| 59 | AsanThread *thread_; |
| 60 | }; |
| 61 | |
| 62 | // AsanThread are stored in TSD and destroyed when the thread dies. |
| 63 | class AsanThread { |
| 64 | public: |
| 65 | explicit AsanThread(LinkerInitialized); // for T0. |
Kostya Serebryany | 3f4b9bb | 2012-01-06 19:44:11 | [diff] [blame] | 66 | static AsanThread *Create(int parent_tid, void *(*start_routine) (void *), |
Alexey Samsonov | 2d3a67b | 2012-01-17 06:35:31 | [diff] [blame^] | 67 | void *arg, AsanStackTrace *stack); |
Kostya Serebryany | 3f4b9bb | 2012-01-06 19:44:11 | [diff] [blame] | 68 | void Destroy(); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 | [diff] [blame] | 69 | |
Kostya Serebryany | 6bb2f1d | 2011-12-16 19:13:35 | [diff] [blame] | 70 | void Init(); // Should be called from the thread itself. |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 | [diff] [blame] | 71 | void *ThreadStart(); |
| 72 | |
| 73 | uintptr_t stack_top() { return stack_top_; } |
| 74 | uintptr_t stack_bottom() { return stack_bottom_; } |
| 75 | size_t stack_size() { return stack_top_ - stack_bottom_; } |
| 76 | int tid() { return summary_->tid(); } |
| 77 | AsanThreadSummary *summary() { return summary_; } |
| 78 | void set_summary(AsanThreadSummary *summary) { summary_ = summary; } |
| 79 | |
| 80 | const char *GetFrameNameByAddr(uintptr_t addr, uintptr_t *offset); |
| 81 | |
| 82 | bool AddrIsInStack(uintptr_t addr) { |
| 83 | return addr >= stack_bottom_ && addr < stack_top_; |
| 84 | } |
| 85 | |
| 86 | FakeStack &fake_stack() { return fake_stack_; } |
| 87 | AsanThreadLocalMallocStorage &malloc_storage() { return malloc_storage_; } |
| 88 | AsanStats &stats() { return stats_; } |
| 89 | |
| 90 | static const int kInvalidTid = -1; |
| 91 | |
| 92 | private: |
| 93 | |
| 94 | void SetThreadStackTopAndBottom(); |
| 95 | void ClearShadowForThreadStack(); |
| 96 | AsanThreadSummary *summary_; |
| 97 | void *(*start_routine_) (void *param); |
| 98 | void *arg_; |
| 99 | uintptr_t stack_top_; |
| 100 | uintptr_t stack_bottom_; |
| 101 | |
| 102 | FakeStack fake_stack_; |
| 103 | AsanThreadLocalMallocStorage malloc_storage_; |
| 104 | AsanStats stats_; |
| 105 | }; |
| 106 | |
| 107 | } // namespace __asan |
| 108 | |
| 109 | #endif // ASAN_THREAD_H |