Alexey Samsonov | e5f5895 | 2012-06-04 13:50:10 +0000 | [diff] [blame] | 1 | //===-- asan_thread.cc ----------------------------------------------------===// |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 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 | // Thread-related code. |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | #include "asan_allocator.h" |
| 15 | #include "asan_interceptors.h" |
Kostya Serebryany | df499b4 | 2012-01-05 00:44:33 +0000 | [diff] [blame] | 16 | #include "asan_procmaps.h" |
Alexey Samsonov | 55cdfc6 | 2012-01-17 06:35:31 +0000 | [diff] [blame] | 17 | #include "asan_stack.h" |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 18 | #include "asan_thread.h" |
Kostya Serebryany | af34415 | 2012-01-11 02:03:16 +0000 | [diff] [blame] | 19 | #include "asan_thread_registry.h" |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 20 | #include "asan_mapping.h" |
| 21 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 22 | namespace __asan { |
| 23 | |
| 24 | AsanThread::AsanThread(LinkerInitialized x) |
| 25 | : fake_stack_(x), |
| 26 | malloc_storage_(x), |
| 27 | stats_(x) { } |
| 28 | |
Kostya Serebryany | e0cff0b | 2012-06-06 15:06:58 +0000 | [diff] [blame] | 29 | AsanThread *AsanThread::Create(u32 parent_tid, thread_callback_t start_routine, |
Alexey Samsonov | 55cdfc6 | 2012-01-17 06:35:31 +0000 | [diff] [blame] | 30 | void *arg, AsanStackTrace *stack) { |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 31 | uptr size = RoundUpTo(sizeof(AsanThread), kPageSize); |
Alexey Samsonov | a25b346 | 2012-06-06 16:15:07 +0000 | [diff] [blame^] | 32 | AsanThread *thread = (AsanThread*)MmapOrDie(size, __FUNCTION__); |
Alexey Samsonov | 55cdfc6 | 2012-01-17 06:35:31 +0000 | [diff] [blame] | 33 | thread->start_routine_ = start_routine; |
| 34 | thread->arg_ = arg; |
| 35 | |
| 36 | AsanThreadSummary *summary = new AsanThreadSummary(parent_tid, stack); |
| 37 | summary->set_thread(thread); |
| 38 | thread->set_summary(summary); |
| 39 | |
| 40 | return thread; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Kostya Serebryany | f58f998 | 2012-02-07 00:27:15 +0000 | [diff] [blame] | 43 | void AsanThreadSummary::TSDDtor(void *tsd) { |
| 44 | AsanThreadSummary *summary = (AsanThreadSummary*)tsd; |
| 45 | if (FLAG_v >= 1) { |
| 46 | Report("T%d TSDDtor\n", summary->tid()); |
| 47 | } |
| 48 | if (summary->thread()) { |
| 49 | summary->thread()->Destroy(); |
| 50 | } |
| 51 | } |
| 52 | |
Kostya Serebryany | a6b5226 | 2012-01-06 19:44:11 +0000 | [diff] [blame] | 53 | void AsanThread::Destroy() { |
Kostya Serebryany | f58f998 | 2012-02-07 00:27:15 +0000 | [diff] [blame] | 54 | if (FLAG_v >= 1) { |
| 55 | Report("T%d exited\n", tid()); |
| 56 | } |
| 57 | |
| 58 | asanThreadRegistry().UnregisterThread(this); |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 59 | CHECK(summary()->thread() == 0); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 60 | // We also clear the shadow on thread destruction because |
| 61 | // some code may still be executing in later TSD destructors |
| 62 | // and we don't want it to have any poisoned stack. |
| 63 | ClearShadowForThreadStack(); |
Alexey Samsonov | 55cdfc6 | 2012-01-17 06:35:31 +0000 | [diff] [blame] | 64 | fake_stack().Cleanup(); |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 65 | uptr size = RoundUpTo(sizeof(AsanThread), kPageSize); |
Alexey Samsonov | a25b346 | 2012-06-06 16:15:07 +0000 | [diff] [blame^] | 66 | UnmapOrDie(this, size); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Kostya Serebryany | 69eca73 | 2011-12-16 19:13:35 +0000 | [diff] [blame] | 69 | void AsanThread::Init() { |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 70 | SetThreadStackTopAndBottom(); |
Alexey Samsonov | 55cdfc6 | 2012-01-17 06:35:31 +0000 | [diff] [blame] | 71 | CHECK(AddrIsInMem(stack_bottom_)); |
| 72 | CHECK(AddrIsInMem(stack_top_)); |
| 73 | ClearShadowForThreadStack(); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 74 | if (FLAG_v >= 1) { |
| 75 | int local = 0; |
Evgeniy Stepanov | 739eb79 | 2012-03-21 11:32:46 +0000 | [diff] [blame] | 76 | Report("T%d: stack [%p,%p) size 0x%zx; local=%p\n", |
Alexey Samsonov | 5bcca4e | 2012-06-06 10:46:00 +0000 | [diff] [blame] | 77 | tid(), (void*)stack_bottom_, (void*)stack_top_, |
Kostya Serebryany | a7e760a | 2012-01-09 19:18:27 +0000 | [diff] [blame] | 78 | stack_top_ - stack_bottom_, &local); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 79 | } |
Alexey Samsonov | 55cdfc6 | 2012-01-17 06:35:31 +0000 | [diff] [blame] | 80 | fake_stack_.Init(stack_size()); |
Kostya Serebryany | 69eca73 | 2011-12-16 19:13:35 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Timur Iskhodzhanov | 600972e | 2012-02-24 15:28:43 +0000 | [diff] [blame] | 83 | thread_return_t AsanThread::ThreadStart() { |
Kostya Serebryany | 69eca73 | 2011-12-16 19:13:35 +0000 | [diff] [blame] | 84 | Init(); |
Alexander Potapenko | f03d8af | 2012-04-05 10:54:52 +0000 | [diff] [blame] | 85 | if (FLAG_use_sigaltstack) SetAlternateSignalStack(); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 86 | |
| 87 | if (!start_routine_) { |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 88 | // start_routine_ == 0 if we're on the main thread or on one of the |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 89 | // OS X libdispatch worker threads. But nobody is supposed to call |
| 90 | // ThreadStart() for the worker threads. |
| 91 | CHECK(tid() == 0); |
| 92 | return 0; |
| 93 | } |
| 94 | |
Timur Iskhodzhanov | 600972e | 2012-02-24 15:28:43 +0000 | [diff] [blame] | 95 | thread_return_t res = start_routine_(arg_); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 96 | malloc_storage().CommitBack(); |
Alexander Potapenko | f03d8af | 2012-04-05 10:54:52 +0000 | [diff] [blame] | 97 | if (FLAG_use_sigaltstack) UnsetAlternateSignalStack(); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 98 | |
Kostya Serebryany | af34415 | 2012-01-11 02:03:16 +0000 | [diff] [blame] | 99 | this->Destroy(); |
| 100 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 101 | return res; |
| 102 | } |
| 103 | |
Alexey Samsonov | 55cdfc6 | 2012-01-17 06:35:31 +0000 | [diff] [blame] | 104 | void AsanThread::ClearShadowForThreadStack() { |
| 105 | PoisonShadow(stack_bottom_, stack_top_ - stack_bottom_, 0); |
| 106 | } |
| 107 | |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 108 | const char *AsanThread::GetFrameNameByAddr(uptr addr, uptr *offset) { |
| 109 | uptr bottom = 0; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 110 | bool is_fake_stack = false; |
| 111 | if (AddrIsInStack(addr)) { |
| 112 | bottom = stack_bottom(); |
| 113 | } else { |
| 114 | bottom = fake_stack().AddrIsInFakeStack(addr); |
| 115 | CHECK(bottom); |
| 116 | is_fake_stack = true; |
| 117 | } |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 118 | uptr aligned_addr = addr & ~(__WORDSIZE/8 - 1); // align addr. |
Kostya Serebryany | ee39255 | 2012-05-31 15:02:07 +0000 | [diff] [blame] | 119 | u8 *shadow_ptr = (u8*)MemToShadow(aligned_addr); |
| 120 | u8 *shadow_bottom = (u8*)MemToShadow(bottom); |
Evgeniy Stepanov | 3972ea0 | 2012-05-12 12:33:10 +0000 | [diff] [blame] | 121 | |
| 122 | while (shadow_ptr >= shadow_bottom && |
| 123 | *shadow_ptr != kAsanStackLeftRedzoneMagic) { |
| 124 | shadow_ptr--; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 125 | } |
Evgeniy Stepanov | 3972ea0 | 2012-05-12 12:33:10 +0000 | [diff] [blame] | 126 | |
| 127 | while (shadow_ptr >= shadow_bottom && |
| 128 | *shadow_ptr == kAsanStackLeftRedzoneMagic) { |
| 129 | shadow_ptr--; |
| 130 | } |
| 131 | |
| 132 | if (shadow_ptr < shadow_bottom) { |
| 133 | *offset = 0; |
| 134 | return "UNKNOWN"; |
| 135 | } |
| 136 | |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 137 | uptr* ptr = (uptr*)SHADOW_TO_MEM((uptr)(shadow_ptr + 1)); |
Evgeniy Stepanov | 3972ea0 | 2012-05-12 12:33:10 +0000 | [diff] [blame] | 138 | CHECK((ptr[0] == kCurrentStackFrameMagic) || |
| 139 | (is_fake_stack && ptr[0] == kRetiredStackFrameMagic)); |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 140 | *offset = addr - (uptr)ptr; |
Evgeniy Stepanov | 3972ea0 | 2012-05-12 12:33:10 +0000 | [diff] [blame] | 141 | return (const char*)ptr[1]; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 144 | } // namespace __asan |