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