Alexey Samsonov | 485d3dc | 2012-06-04 13:50:10 +0000 | [diff] [blame] | 1 | //===-- asan_thread.cc ----------------------------------------------------===// |
Kostya Serebryany | 019b76f | 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 | a88c60b | 2013-03-28 15:42:43 +0000 | [diff] [blame] | 16 | #include "asan_poisoning.h" |
Alexey Samsonov | 2d3a67b | 2012-01-17 06:35:31 +0000 | [diff] [blame] | 17 | #include "asan_stack.h" |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 18 | #include "asan_thread.h" |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 19 | #include "asan_mapping.h" |
Alexey Samsonov | 4b1f103 | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 20 | #include "sanitizer_common/sanitizer_common.h" |
Alexey Samsonov | 54afba8 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 21 | #include "sanitizer_common/sanitizer_placement_new.h" |
Kostya Serebryany | 9628839 | 2013-10-18 14:50:44 +0000 | [diff] [blame] | 22 | #include "sanitizer_common/sanitizer_stackdepot.h" |
Kostya Serebryany | 71788fa | 2014-01-29 09:29:16 +0000 | [diff] [blame] | 23 | #include "sanitizer_common/sanitizer_tls_get_addr.h" |
Sergey Matveev | 65dd62a | 2013-05-21 13:40:13 +0000 | [diff] [blame] | 24 | #include "lsan/lsan_common.h" |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 25 | |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 26 | namespace __asan { |
| 27 | |
Alexey Samsonov | 54afba8 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 28 | // AsanThreadContext implementation. |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 29 | |
Sergey Matveev | eba518b | 2014-12-05 17:31:13 +0000 | [diff] [blame] | 30 | struct CreateThreadContextArgs { |
| 31 | AsanThread *thread; |
| 32 | StackTrace *stack; |
| 33 | }; |
| 34 | |
Alexey Samsonov | 54afba8 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 35 | void AsanThreadContext::OnCreated(void *arg) { |
| 36 | CreateThreadContextArgs *args = static_cast<CreateThreadContextArgs*>(arg); |
Kostya Serebryany | 9628839 | 2013-10-18 14:50:44 +0000 | [diff] [blame] | 37 | if (args->stack) |
Alexey Samsonov | 3741ab8 | 2014-10-26 06:23:07 +0000 | [diff] [blame] | 38 | stack_id = StackDepotPut(*args->stack); |
Alexey Samsonov | 54afba8 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 39 | thread = args->thread; |
| 40 | thread->set_context(this); |
| 41 | } |
| 42 | |
| 43 | void AsanThreadContext::OnFinished() { |
| 44 | // Drop the link to the AsanThread object. |
Vedant Kumar | 59ba7b8 | 2015-10-01 00:22:21 +0000 | [diff] [blame] | 45 | thread = nullptr; |
Alexey Samsonov | 54afba8 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Kostya Serebryany | c1aa0e8 | 2013-06-03 14:49:25 +0000 | [diff] [blame] | 48 | // MIPS requires aligned address |
Timur Iskhodzhanov | baf90cc | 2013-06-04 08:25:17 +0000 | [diff] [blame] | 49 | static ALIGNED(16) char thread_registry_placeholder[sizeof(ThreadRegistry)]; |
Alexey Samsonov | 54afba8 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 50 | static ThreadRegistry *asan_thread_registry; |
| 51 | |
Kostya Serebryany | f11e485 | 2013-10-18 15:07:07 +0000 | [diff] [blame] | 52 | static BlockingMutex mu_for_thread_context(LINKER_INITIALIZED); |
| 53 | static LowLevelAllocator allocator_for_thread_context; |
| 54 | |
Alexey Samsonov | 54afba8 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 55 | static ThreadContextBase *GetAsanThreadContext(u32 tid) { |
Kostya Serebryany | f11e485 | 2013-10-18 15:07:07 +0000 | [diff] [blame] | 56 | BlockingMutexLock lock(&mu_for_thread_context); |
Peter Collingbourne | 50cb32e | 2013-10-24 06:23:39 +0000 | [diff] [blame] | 57 | return new(allocator_for_thread_context) AsanThreadContext(tid); |
Alexey Samsonov | 54afba8 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | ThreadRegistry &asanThreadRegistry() { |
| 61 | static bool initialized; |
| 62 | // Don't worry about thread_safety - this should be called when there is |
| 63 | // a single thread. |
| 64 | if (!initialized) { |
| 65 | // Never reuse ASan threads: we store pointer to AsanThreadContext |
| 66 | // in TSD and can't reliably tell when no more TSD destructors will |
| 67 | // be called. It would be wrong to reuse AsanThreadContext for another |
| 68 | // thread before all TSD destructors will be called for it. |
| 69 | asan_thread_registry = new(thread_registry_placeholder) ThreadRegistry( |
| 70 | GetAsanThreadContext, kMaxNumberOfThreads, kMaxNumberOfThreads); |
| 71 | initialized = true; |
| 72 | } |
| 73 | return *asan_thread_registry; |
| 74 | } |
| 75 | |
| 76 | AsanThreadContext *GetThreadContextByTidLocked(u32 tid) { |
| 77 | return static_cast<AsanThreadContext *>( |
| 78 | asanThreadRegistry().GetThreadLocked(tid)); |
| 79 | } |
| 80 | |
| 81 | // AsanThread implementation. |
| 82 | |
Sergey Matveev | eba518b | 2014-12-05 17:31:13 +0000 | [diff] [blame] | 83 | AsanThread *AsanThread::Create(thread_callback_t start_routine, void *arg, |
| 84 | u32 parent_tid, StackTrace *stack, |
| 85 | bool detached) { |
Kostya Serebryany | f22c697 | 2012-11-23 15:38:49 +0000 | [diff] [blame] | 86 | uptr PageSize = GetPageSizeCached(); |
| 87 | uptr size = RoundUpTo(sizeof(AsanThread), PageSize); |
Joerg Sonnenberger | 9d09e2f | 2014-02-26 20:33:22 +0000 | [diff] [blame] | 88 | AsanThread *thread = (AsanThread*)MmapOrDie(size, __func__); |
Alexey Samsonov | 2d3a67b | 2012-01-17 06:35:31 +0000 | [diff] [blame] | 89 | thread->start_routine_ = start_routine; |
| 90 | thread->arg_ = arg; |
Sergey Matveev | eba518b | 2014-12-05 17:31:13 +0000 | [diff] [blame] | 91 | CreateThreadContextArgs args = { thread, stack }; |
| 92 | asanThreadRegistry().CreateThread(*reinterpret_cast<uptr *>(thread), detached, |
| 93 | parent_tid, &args); |
Alexey Samsonov | 2d3a67b | 2012-01-17 06:35:31 +0000 | [diff] [blame] | 94 | |
| 95 | return thread; |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Alexey Samsonov | 54afba8 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 98 | void AsanThread::TSDDtor(void *tsd) { |
| 99 | AsanThreadContext *context = (AsanThreadContext*)tsd; |
Sergey Matveev | 9be70fb | 2013-12-05 12:04:51 +0000 | [diff] [blame] | 100 | VReport(1, "T%d TSDDtor\n", context->tid); |
Alexey Samsonov | 54afba8 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 101 | if (context->thread) |
| 102 | context->thread->Destroy(); |
Kostya Serebryany | b5eb5a7 | 2012-02-07 00:27:15 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Kostya Serebryany | 3f4b9bb | 2012-01-06 19:44:11 +0000 | [diff] [blame] | 105 | void AsanThread::Destroy() { |
Kostya Serebryany | 7a3a93f | 2013-12-11 13:54:01 +0000 | [diff] [blame] | 106 | int tid = this->tid(); |
| 107 | VReport(1, "T%d exited\n", tid); |
Kostya Serebryany | b5eb5a7 | 2012-02-07 00:27:15 +0000 | [diff] [blame] | 108 | |
Kostya Serebryany | 04a1767 | 2013-11-13 13:27:44 +0000 | [diff] [blame] | 109 | malloc_storage().CommitBack(); |
Alexander Potapenko | cf4bef3 | 2014-01-28 09:28:57 +0000 | [diff] [blame] | 110 | if (common_flags()->use_sigaltstack) UnsetAlternateSignalStack(); |
Kostya Serebryany | 7a3a93f | 2013-12-11 13:54:01 +0000 | [diff] [blame] | 111 | asanThreadRegistry().FinishThread(tid); |
Alexey Samsonov | 4b16885 | 2013-09-02 08:39:07 +0000 | [diff] [blame] | 112 | FlushToDeadThreadStats(&stats_); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 113 | // We also clear the shadow on thread destruction because |
| 114 | // some code may still be executing in later TSD destructors |
| 115 | // and we don't want it to have any poisoned stack. |
Sergey Matveev | 09886cd | 2013-05-29 13:09:44 +0000 | [diff] [blame] | 116 | ClearShadowForThreadStackAndTLS(); |
Kostya Serebryany | 7a3a93f | 2013-12-11 13:54:01 +0000 | [diff] [blame] | 117 | DeleteFakeStack(tid); |
Kostya Serebryany | f22c697 | 2012-11-23 15:38:49 +0000 | [diff] [blame] | 118 | uptr size = RoundUpTo(sizeof(AsanThread), GetPageSizeCached()); |
Alexey Samsonov | 40d5b77 | 2012-06-06 16:15:07 +0000 | [diff] [blame] | 119 | UnmapOrDie(this, size); |
Kostya Serebryany | 71788fa | 2014-01-29 09:29:16 +0000 | [diff] [blame] | 120 | DTLS_Destroy(); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Dmitry Vyukov | 47b7c5c | 2016-06-21 12:29:18 +0000 | [diff] [blame] | 123 | void AsanThread::StartSwitchFiber(FakeStack **fake_stack_save, uptr bottom, |
| 124 | uptr size) { |
| 125 | if (atomic_load(&stack_switching_, memory_order_relaxed)) { |
| 126 | Report("ERROR: starting fiber switch while in fiber switch\n"); |
| 127 | Die(); |
| 128 | } |
| 129 | |
| 130 | next_stack_bottom_ = bottom; |
| 131 | next_stack_top_ = bottom + size; |
| 132 | atomic_store(&stack_switching_, 1, memory_order_release); |
| 133 | |
| 134 | FakeStack *current_fake_stack = fake_stack_; |
| 135 | if (fake_stack_save) |
| 136 | *fake_stack_save = fake_stack_; |
| 137 | fake_stack_ = nullptr; |
| 138 | SetTLSFakeStack(nullptr); |
| 139 | // if fake_stack_save is null, the fiber will die, delete the fakestack |
| 140 | if (!fake_stack_save && current_fake_stack) |
| 141 | current_fake_stack->Destroy(this->tid()); |
| 142 | } |
| 143 | |
Dmitry Vyukov | b358783 | 2016-09-28 12:28:16 +0000 | [diff] [blame] | 144 | void AsanThread::FinishSwitchFiber(FakeStack *fake_stack_save, |
| 145 | uptr *bottom_old, |
| 146 | uptr *size_old) { |
Dmitry Vyukov | 47b7c5c | 2016-06-21 12:29:18 +0000 | [diff] [blame] | 147 | if (!atomic_load(&stack_switching_, memory_order_relaxed)) { |
| 148 | Report("ERROR: finishing a fiber switch that has not started\n"); |
| 149 | Die(); |
| 150 | } |
| 151 | |
| 152 | if (fake_stack_save) { |
| 153 | SetTLSFakeStack(fake_stack_save); |
| 154 | fake_stack_ = fake_stack_save; |
| 155 | } |
| 156 | |
Dmitry Vyukov | b358783 | 2016-09-28 12:28:16 +0000 | [diff] [blame] | 157 | if (bottom_old) |
| 158 | *bottom_old = stack_bottom_; |
| 159 | if (size_old) |
| 160 | *size_old = stack_top_ - stack_bottom_; |
Dmitry Vyukov | 47b7c5c | 2016-06-21 12:29:18 +0000 | [diff] [blame] | 161 | stack_bottom_ = next_stack_bottom_; |
| 162 | stack_top_ = next_stack_top_; |
| 163 | atomic_store(&stack_switching_, 0, memory_order_release); |
| 164 | next_stack_top_ = 0; |
| 165 | next_stack_bottom_ = 0; |
| 166 | } |
| 167 | |
| 168 | inline AsanThread::StackBounds AsanThread::GetStackBounds() const { |
| 169 | if (!atomic_load(&stack_switching_, memory_order_acquire)) |
| 170 | return StackBounds{stack_bottom_, stack_top_}; // NOLINT |
| 171 | char local; |
| 172 | const uptr cur_stack = (uptr)&local; |
| 173 | // Note: need to check next stack first, because FinishSwitchFiber |
| 174 | // may be in process of overwriting stack_top_/bottom_. But in such case |
| 175 | // we are already on the next stack. |
| 176 | if (cur_stack >= next_stack_bottom_ && cur_stack < next_stack_top_) |
| 177 | return StackBounds{next_stack_bottom_, next_stack_top_}; // NOLINT |
| 178 | return StackBounds{stack_bottom_, stack_top_}; // NOLINT |
| 179 | } |
| 180 | |
| 181 | uptr AsanThread::stack_top() { |
| 182 | return GetStackBounds().top; |
| 183 | } |
| 184 | |
| 185 | uptr AsanThread::stack_bottom() { |
| 186 | return GetStackBounds().bottom; |
| 187 | } |
| 188 | |
| 189 | uptr AsanThread::stack_size() { |
| 190 | const auto bounds = GetStackBounds(); |
| 191 | return bounds.top - bounds.bottom; |
| 192 | } |
| 193 | |
Kostya Serebryany | 628cda7 | 2013-09-12 08:34:50 +0000 | [diff] [blame] | 194 | // We want to create the FakeStack lazyly on the first use, but not eralier |
| 195 | // than the stack size is known and the procedure has to be async-signal safe. |
| 196 | FakeStack *AsanThread::AsyncSignalSafeLazyInitFakeStack() { |
| 197 | uptr stack_size = this->stack_size(); |
| 198 | if (stack_size == 0) // stack_size is not yet available, don't use FakeStack. |
Vedant Kumar | 59ba7b8 | 2015-10-01 00:22:21 +0000 | [diff] [blame] | 199 | return nullptr; |
Kostya Serebryany | 628cda7 | 2013-09-12 08:34:50 +0000 | [diff] [blame] | 200 | uptr old_val = 0; |
| 201 | // fake_stack_ has 3 states: |
| 202 | // 0 -- not initialized |
| 203 | // 1 -- being initialized |
| 204 | // ptr -- initialized |
| 205 | // This CAS checks if the state was 0 and if so changes it to state 1, |
Alp Toker | 1ee7fc7 | 2014-05-15 02:22:34 +0000 | [diff] [blame] | 206 | // if that was successful, it initializes the pointer. |
Kostya Serebryany | 628cda7 | 2013-09-12 08:34:50 +0000 | [diff] [blame] | 207 | if (atomic_compare_exchange_strong( |
| 208 | reinterpret_cast<atomic_uintptr_t *>(&fake_stack_), &old_val, 1UL, |
Kostya Serebryany | 43c4493 | 2013-09-13 06:32:26 +0000 | [diff] [blame] | 209 | memory_order_relaxed)) { |
Kostya Serebryany | c3d43ca | 2013-09-18 10:35:12 +0000 | [diff] [blame] | 210 | uptr stack_size_log = Log2(RoundUpToPowerOfTwo(stack_size)); |
Kostya Serebryany | 1aedf6c | 2013-12-16 08:42:08 +0000 | [diff] [blame] | 211 | CHECK_LE(flags()->min_uar_stack_size_log, flags()->max_uar_stack_size_log); |
| 212 | stack_size_log = |
| 213 | Min(stack_size_log, static_cast<uptr>(flags()->max_uar_stack_size_log)); |
| 214 | stack_size_log = |
| 215 | Max(stack_size_log, static_cast<uptr>(flags()->min_uar_stack_size_log)); |
Kostya Serebryany | c3d43ca | 2013-09-18 10:35:12 +0000 | [diff] [blame] | 216 | fake_stack_ = FakeStack::Create(stack_size_log); |
Kostya Serebryany | 43c4493 | 2013-09-13 06:32:26 +0000 | [diff] [blame] | 217 | SetTLSFakeStack(fake_stack_); |
| 218 | return fake_stack_; |
| 219 | } |
Vedant Kumar | 59ba7b8 | 2015-10-01 00:22:21 +0000 | [diff] [blame] | 220 | return nullptr; |
Kostya Serebryany | 628cda7 | 2013-09-12 08:34:50 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Kostya Serebryany | 6bb2f1d | 2011-12-16 19:13:35 +0000 | [diff] [blame] | 223 | void AsanThread::Init() { |
Dmitry Vyukov | 47b7c5c | 2016-06-21 12:29:18 +0000 | [diff] [blame] | 224 | next_stack_top_ = next_stack_bottom_ = 0; |
| 225 | atomic_store(&stack_switching_, false, memory_order_release); |
Vedant Kumar | 59ba7b8 | 2015-10-01 00:22:21 +0000 | [diff] [blame] | 226 | fake_stack_ = nullptr; // Will be initialized lazily if needed. |
Kostya Serebryany | 558b336 | 2014-06-06 07:35:35 +0000 | [diff] [blame] | 227 | CHECK_EQ(this->stack_size(), 0U); |
Sergey Matveev | 09886cd | 2013-05-29 13:09:44 +0000 | [diff] [blame] | 228 | SetThreadStackAndTls(); |
Kostya Serebryany | 558b336 | 2014-06-06 07:35:35 +0000 | [diff] [blame] | 229 | CHECK_GT(this->stack_size(), 0U); |
Alexey Samsonov | 2d3a67b | 2012-01-17 06:35:31 +0000 | [diff] [blame] | 230 | CHECK(AddrIsInMem(stack_bottom_)); |
Kostya Serebryany | 63c36bb | 2013-01-18 11:30:36 +0000 | [diff] [blame] | 231 | CHECK(AddrIsInMem(stack_top_ - 1)); |
Sergey Matveev | 09886cd | 2013-05-29 13:09:44 +0000 | [diff] [blame] | 232 | ClearShadowForThreadStackAndTLS(); |
Sergey Matveev | 9be70fb | 2013-12-05 12:04:51 +0000 | [diff] [blame] | 233 | int local = 0; |
| 234 | VReport(1, "T%d: stack [%p,%p) size 0x%zx; local=%p\n", tid(), |
| 235 | (void *)stack_bottom_, (void *)stack_top_, stack_top_ - stack_bottom_, |
| 236 | &local); |
Kostya Serebryany | 6bb2f1d | 2011-12-16 19:13:35 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Sergey Matveev | b029c51 | 2014-12-05 00:10:15 +0000 | [diff] [blame] | 239 | thread_return_t AsanThread::ThreadStart( |
| 240 | uptr os_id, atomic_uintptr_t *signal_thread_is_registered) { |
Kostya Serebryany | 6bb2f1d | 2011-12-16 19:13:35 +0000 | [diff] [blame] | 241 | Init(); |
Kuba Mracek | bba1d40 | 2017-02-02 12:54:21 +0000 | [diff] [blame] | 242 | asanThreadRegistry().StartThread(tid(), os_id, /*workerthread*/ false, |
| 243 | nullptr); |
Sergey Matveev | b029c51 | 2014-12-05 00:10:15 +0000 | [diff] [blame] | 244 | if (signal_thread_is_registered) |
| 245 | atomic_store(signal_thread_is_registered, 1, memory_order_release); |
| 246 | |
Alexander Potapenko | cf4bef3 | 2014-01-28 09:28:57 +0000 | [diff] [blame] | 247 | if (common_flags()->use_sigaltstack) SetAlternateSignalStack(); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 248 | |
| 249 | if (!start_routine_) { |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 250 | // start_routine_ == 0 if we're on the main thread or on one of the |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 251 | // OS X libdispatch worker threads. But nobody is supposed to call |
| 252 | // ThreadStart() for the worker threads. |
Kostya Serebryany | 5b4267f | 2013-04-05 14:40:25 +0000 | [diff] [blame] | 253 | CHECK_EQ(tid(), 0); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 254 | return 0; |
| 255 | } |
| 256 | |
Timur Iskhodzhanov | 0f9c9a5 | 2012-02-24 15:28:43 +0000 | [diff] [blame] | 257 | thread_return_t res = start_routine_(arg_); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 258 | |
Sergey Matveev | da9f5e7 | 2013-10-14 12:01:05 +0000 | [diff] [blame] | 259 | // On POSIX systems we defer this to the TSD destructor. LSan will consider |
| 260 | // the thread's memory as non-live from the moment we call Destroy(), even |
| 261 | // though that memory might contain pointers to heap objects which will be |
| 262 | // cleaned up by a user-defined TSD destructor. Thus, calling Destroy() before |
| 263 | // the TSD destructors have run might cause false positives in LSan. |
| 264 | if (!SANITIZER_POSIX) |
| 265 | this->Destroy(); |
Kostya Serebryany | 332923b | 2012-01-11 02:03:16 +0000 | [diff] [blame] | 266 | |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 267 | return res; |
| 268 | } |
| 269 | |
Sergey Matveev | 09886cd | 2013-05-29 13:09:44 +0000 | [diff] [blame] | 270 | void AsanThread::SetThreadStackAndTls() { |
Kostya Serebryany | f8bbdfa | 2013-09-19 14:59:52 +0000 | [diff] [blame] | 271 | uptr tls_size = 0; |
Dmitry Vyukov | 47b7c5c | 2016-06-21 12:29:18 +0000 | [diff] [blame] | 272 | uptr stack_size = 0; |
| 273 | GetThreadStackAndTls(tid() == 0, const_cast<uptr *>(&stack_bottom_), |
| 274 | const_cast<uptr *>(&stack_size), &tls_begin_, &tls_size); |
| 275 | stack_top_ = stack_bottom_ + stack_size; |
Sergey Matveev | 09886cd | 2013-05-29 13:09:44 +0000 | [diff] [blame] | 276 | tls_end_ = tls_begin_ + tls_size; |
Alexey Samsonov | 5535c51 | 2016-01-14 18:50:09 +0000 | [diff] [blame] | 277 | dtls_ = DTLS_Get(); |
Sergey Matveev | 09886cd | 2013-05-29 13:09:44 +0000 | [diff] [blame] | 278 | |
Alexey Samsonov | 4b1f103 | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 279 | int local; |
| 280 | CHECK(AddrIsInStack((uptr)&local)); |
| 281 | } |
| 282 | |
Sergey Matveev | 09886cd | 2013-05-29 13:09:44 +0000 | [diff] [blame] | 283 | void AsanThread::ClearShadowForThreadStackAndTLS() { |
Alexey Samsonov | 2d3a67b | 2012-01-17 06:35:31 +0000 | [diff] [blame] | 284 | PoisonShadow(stack_bottom_, stack_top_ - stack_bottom_, 0); |
Sergey Matveev | 09886cd | 2013-05-29 13:09:44 +0000 | [diff] [blame] | 285 | if (tls_begin_ != tls_end_) |
| 286 | PoisonShadow(tls_begin_, tls_end_ - tls_begin_, 0); |
Alexey Samsonov | 2d3a67b | 2012-01-17 06:35:31 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Alexey Samsonov | 0470e24 | 2014-10-01 21:13:00 +0000 | [diff] [blame] | 289 | bool AsanThread::GetStackFrameAccessByAddr(uptr addr, |
| 290 | StackFrameAccess *access) { |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 291 | uptr bottom = 0; |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 292 | if (AddrIsInStack(addr)) { |
| 293 | bottom = stack_bottom(); |
Kostya Serebryany | 736bd08 | 2013-09-12 08:43:44 +0000 | [diff] [blame] | 294 | } else if (has_fake_stack()) { |
Kostya Serebryany | 6a068a7 | 2013-06-26 12:16:05 +0000 | [diff] [blame] | 295 | bottom = fake_stack()->AddrIsInFakeStack(addr); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 296 | CHECK(bottom); |
Alexey Samsonov | 0470e24 | 2014-10-01 21:13:00 +0000 | [diff] [blame] | 297 | access->offset = addr - bottom; |
| 298 | access->frame_pc = ((uptr*)bottom)[2]; |
| 299 | access->frame_descr = (const char *)((uptr*)bottom)[1]; |
| 300 | return true; |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 301 | } |
Kostya Serebryany | 734f1eb | 2012-11-21 12:38:58 +0000 | [diff] [blame] | 302 | uptr aligned_addr = addr & ~(SANITIZER_WORDSIZE/8 - 1); // align addr. |
Kostya Serebryany | 1d35d15 | 2012-05-31 15:02:07 +0000 | [diff] [blame] | 303 | u8 *shadow_ptr = (u8*)MemToShadow(aligned_addr); |
| 304 | u8 *shadow_bottom = (u8*)MemToShadow(bottom); |
Evgeniy Stepanov | d989be1 | 2012-05-12 12:33:10 +0000 | [diff] [blame] | 305 | |
| 306 | while (shadow_ptr >= shadow_bottom && |
Alexander Potapenko | bcc00a4 | 2012-11-15 15:24:42 +0000 | [diff] [blame] | 307 | *shadow_ptr != kAsanStackLeftRedzoneMagic) { |
Evgeniy Stepanov | d989be1 | 2012-05-12 12:33:10 +0000 | [diff] [blame] | 308 | shadow_ptr--; |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 309 | } |
Evgeniy Stepanov | d989be1 | 2012-05-12 12:33:10 +0000 | [diff] [blame] | 310 | |
| 311 | while (shadow_ptr >= shadow_bottom && |
Alexander Potapenko | bcc00a4 | 2012-11-15 15:24:42 +0000 | [diff] [blame] | 312 | *shadow_ptr == kAsanStackLeftRedzoneMagic) { |
Evgeniy Stepanov | d989be1 | 2012-05-12 12:33:10 +0000 | [diff] [blame] | 313 | shadow_ptr--; |
| 314 | } |
| 315 | |
| 316 | if (shadow_ptr < shadow_bottom) { |
Alexey Samsonov | 0470e24 | 2014-10-01 21:13:00 +0000 | [diff] [blame] | 317 | return false; |
Evgeniy Stepanov | d989be1 | 2012-05-12 12:33:10 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Kostya Serebryany | 8d03204 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 320 | uptr* ptr = (uptr*)SHADOW_TO_MEM((uptr)(shadow_ptr + 1)); |
Alexander Potapenko | bcc00a4 | 2012-11-15 15:24:42 +0000 | [diff] [blame] | 321 | CHECK(ptr[0] == kCurrentStackFrameMagic); |
Alexey Samsonov | 0470e24 | 2014-10-01 21:13:00 +0000 | [diff] [blame] | 322 | access->offset = addr - (uptr)ptr; |
| 323 | access->frame_pc = ptr[2]; |
| 324 | access->frame_descr = (const char*)ptr[1]; |
| 325 | return true; |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Dmitry Vyukov | 47b7c5c | 2016-06-21 12:29:18 +0000 | [diff] [blame] | 328 | bool AsanThread::AddrIsInStack(uptr addr) { |
| 329 | const auto bounds = GetStackBounds(); |
| 330 | return addr >= bounds.bottom && addr < bounds.top; |
| 331 | } |
| 332 | |
Alexey Samsonov | 54afba8 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 333 | static bool ThreadStackContainsAddress(ThreadContextBase *tctx_base, |
| 334 | void *addr) { |
| 335 | AsanThreadContext *tctx = static_cast<AsanThreadContext*>(tctx_base); |
| 336 | AsanThread *t = tctx->thread; |
Kostya Serebryany | 6a068a7 | 2013-06-26 12:16:05 +0000 | [diff] [blame] | 337 | if (!t) return false; |
| 338 | if (t->AddrIsInStack((uptr)addr)) return true; |
Kostya Serebryany | 44441cc | 2013-09-12 08:47:00 +0000 | [diff] [blame] | 339 | if (t->has_fake_stack() && t->fake_stack()->AddrIsInFakeStack((uptr)addr)) |
Kostya Serebryany | 6a068a7 | 2013-06-26 12:16:05 +0000 | [diff] [blame] | 340 | return true; |
| 341 | return false; |
Alexey Samsonov | 54afba8 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Alexey Samsonov | cf025cb | 2013-03-20 09:23:28 +0000 | [diff] [blame] | 344 | AsanThread *GetCurrentThread() { |
Sergey Matveev | bdeff95 | 2013-07-08 12:57:24 +0000 | [diff] [blame] | 345 | AsanThreadContext *context = |
| 346 | reinterpret_cast<AsanThreadContext *>(AsanTSDGet()); |
Alexey Samsonov | 54afba8 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 347 | if (!context) { |
| 348 | if (SANITIZER_ANDROID) { |
| 349 | // On Android, libc constructor is called _after_ asan_init, and cleans up |
| 350 | // TSD. Try to figure out if this is still the main thread by the stack |
| 351 | // address. We are not entirely sure that we have correct main thread |
Dmitry Vyukov | a7e42b5 | 2013-03-22 07:29:59 +0000 | [diff] [blame] | 352 | // limits, so only do this magic on Android, and only if the found thread |
| 353 | // is the main thread. |
Alexey Samsonov | 54afba8 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 354 | AsanThreadContext *tctx = GetThreadContextByTidLocked(0); |
Evgeniy Stepanov | 3989c9f | 2016-09-07 22:57:06 +0000 | [diff] [blame] | 355 | if (tctx && ThreadStackContainsAddress(tctx, &context)) { |
Alexey Samsonov | 54afba8 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 356 | SetCurrentThread(tctx->thread); |
| 357 | return tctx->thread; |
| 358 | } |
Alexey Samsonov | cf025cb | 2013-03-20 09:23:28 +0000 | [diff] [blame] | 359 | } |
Vedant Kumar | 59ba7b8 | 2015-10-01 00:22:21 +0000 | [diff] [blame] | 360 | return nullptr; |
Alexey Samsonov | cf025cb | 2013-03-20 09:23:28 +0000 | [diff] [blame] | 361 | } |
Alexey Samsonov | 54afba8 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 362 | return context->thread; |
Alexey Samsonov | cf025cb | 2013-03-20 09:23:28 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | void SetCurrentThread(AsanThread *t) { |
Alexey Samsonov | 54afba8 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 366 | CHECK(t->context()); |
Sergey Matveev | 9be70fb | 2013-12-05 12:04:51 +0000 | [diff] [blame] | 367 | VReport(2, "SetCurrentThread: %p for thread %p\n", t->context(), |
| 368 | (void *)GetThreadSelf()); |
Alexey Samsonov | cf025cb | 2013-03-20 09:23:28 +0000 | [diff] [blame] | 369 | // Make sure we do not reset the current AsanThread. |
Alexey Samsonov | 54afba8 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 370 | CHECK_EQ(0, AsanTSDGet()); |
| 371 | AsanTSDSet(t->context()); |
| 372 | CHECK_EQ(t->context(), AsanTSDGet()); |
Alexey Samsonov | cf025cb | 2013-03-20 09:23:28 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | u32 GetCurrentTidOrInvalid() { |
| 376 | AsanThread *t = GetCurrentThread(); |
| 377 | return t ? t->tid() : kInvalidTid; |
| 378 | } |
| 379 | |
Alexey Samsonov | 54afba8 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 380 | AsanThread *FindThreadByStackAddress(uptr addr) { |
| 381 | asanThreadRegistry().CheckLocked(); |
| 382 | AsanThreadContext *tctx = static_cast<AsanThreadContext *>( |
| 383 | asanThreadRegistry().FindThreadContextLocked(ThreadStackContainsAddress, |
| 384 | (void *)addr)); |
Vedant Kumar | 59ba7b8 | 2015-10-01 00:22:21 +0000 | [diff] [blame] | 385 | return tctx ? tctx->thread : nullptr; |
Alexey Samsonov | 54afba8 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 386 | } |
Sergey Matveev | bdeff95 | 2013-07-08 12:57:24 +0000 | [diff] [blame] | 387 | |
| 388 | void EnsureMainThreadIDIsCorrect() { |
| 389 | AsanThreadContext *context = |
| 390 | reinterpret_cast<AsanThreadContext *>(AsanTSDGet()); |
| 391 | if (context && (context->tid == 0)) |
| 392 | context->os_id = GetTid(); |
| 393 | } |
Sergey Matveev | 43d90cb | 2013-10-14 14:04:50 +0000 | [diff] [blame] | 394 | |
| 395 | __asan::AsanThread *GetAsanThreadByOsIDLocked(uptr os_id) { |
| 396 | __asan::AsanThreadContext *context = static_cast<__asan::AsanThreadContext *>( |
| 397 | __asan::asanThreadRegistry().FindThreadContextByOsIDLocked(os_id)); |
Vedant Kumar | 59ba7b8 | 2015-10-01 00:22:21 +0000 | [diff] [blame] | 398 | if (!context) return nullptr; |
Sergey Matveev | 43d90cb | 2013-10-14 14:04:50 +0000 | [diff] [blame] | 399 | return context->thread; |
| 400 | } |
Vedant Kumar | 59ba7b8 | 2015-10-01 00:22:21 +0000 | [diff] [blame] | 401 | } // namespace __asan |
Sergey Matveev | 65dd62a | 2013-05-21 13:40:13 +0000 | [diff] [blame] | 402 | |
| 403 | // --- Implementation of LSan-specific functions --- {{{1 |
| 404 | namespace __lsan { |
| 405 | bool GetThreadRangesLocked(uptr os_id, uptr *stack_begin, uptr *stack_end, |
Alexey Samsonov | 5535c51 | 2016-01-14 18:50:09 +0000 | [diff] [blame] | 406 | uptr *tls_begin, uptr *tls_end, uptr *cache_begin, |
| 407 | uptr *cache_end, DTLS **dtls) { |
Sergey Matveev | 43d90cb | 2013-10-14 14:04:50 +0000 | [diff] [blame] | 408 | __asan::AsanThread *t = __asan::GetAsanThreadByOsIDLocked(os_id); |
Sergey Matveev | 09886cd | 2013-05-29 13:09:44 +0000 | [diff] [blame] | 409 | if (!t) return false; |
| 410 | *stack_begin = t->stack_bottom(); |
| 411 | *stack_end = t->stack_top(); |
| 412 | *tls_begin = t->tls_begin(); |
| 413 | *tls_end = t->tls_end(); |
| 414 | // ASan doesn't keep allocator caches in TLS, so these are unused. |
| 415 | *cache_begin = 0; |
| 416 | *cache_end = 0; |
Alexey Samsonov | 5535c51 | 2016-01-14 18:50:09 +0000 | [diff] [blame] | 417 | *dtls = t->dtls(); |
Sergey Matveev | 09886cd | 2013-05-29 13:09:44 +0000 | [diff] [blame] | 418 | return true; |
Sergey Matveev | 65dd62a | 2013-05-21 13:40:13 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Sergey Matveev | 43d90cb | 2013-10-14 14:04:50 +0000 | [diff] [blame] | 421 | void ForEachExtraStackRange(uptr os_id, RangeIteratorCallback callback, |
| 422 | void *arg) { |
| 423 | __asan::AsanThread *t = __asan::GetAsanThreadByOsIDLocked(os_id); |
| 424 | if (t && t->has_fake_stack()) |
| 425 | t->fake_stack()->ForEachFakeFrame(callback, arg); |
| 426 | } |
| 427 | |
Sergey Matveev | 65dd62a | 2013-05-21 13:40:13 +0000 | [diff] [blame] | 428 | void LockThreadRegistry() { |
| 429 | __asan::asanThreadRegistry().Lock(); |
| 430 | } |
| 431 | |
| 432 | void UnlockThreadRegistry() { |
| 433 | __asan::asanThreadRegistry().Unlock(); |
| 434 | } |
Sergey Matveev | bdeff95 | 2013-07-08 12:57:24 +0000 | [diff] [blame] | 435 | |
| 436 | void EnsureMainThreadIDIsCorrect() { |
| 437 | __asan::EnsureMainThreadIDIsCorrect(); |
| 438 | } |
Vedant Kumar | 59ba7b8 | 2015-10-01 00:22:21 +0000 | [diff] [blame] | 439 | } // namespace __lsan |
Dmitry Vyukov | 47b7c5c | 2016-06-21 12:29:18 +0000 | [diff] [blame] | 440 | |
| 441 | // ---------------------- Interface ---------------- {{{1 |
| 442 | using namespace __asan; // NOLINT |
| 443 | |
| 444 | extern "C" { |
| 445 | SANITIZER_INTERFACE_ATTRIBUTE |
| 446 | void __sanitizer_start_switch_fiber(void **fakestacksave, const void *bottom, |
| 447 | uptr size) { |
| 448 | AsanThread *t = GetCurrentThread(); |
| 449 | if (!t) { |
| 450 | VReport(1, "__asan_start_switch_fiber called from unknown thread\n"); |
| 451 | return; |
| 452 | } |
| 453 | t->StartSwitchFiber((FakeStack**)fakestacksave, (uptr)bottom, size); |
| 454 | } |
| 455 | |
| 456 | SANITIZER_INTERFACE_ATTRIBUTE |
Dmitry Vyukov | b358783 | 2016-09-28 12:28:16 +0000 | [diff] [blame] | 457 | void __sanitizer_finish_switch_fiber(void* fakestack, |
| 458 | const void **bottom_old, |
| 459 | uptr *size_old) { |
Dmitry Vyukov | 47b7c5c | 2016-06-21 12:29:18 +0000 | [diff] [blame] | 460 | AsanThread *t = GetCurrentThread(); |
| 461 | if (!t) { |
| 462 | VReport(1, "__asan_finish_switch_fiber called from unknown thread\n"); |
| 463 | return; |
| 464 | } |
Dmitry Vyukov | b358783 | 2016-09-28 12:28:16 +0000 | [diff] [blame] | 465 | t->FinishSwitchFiber((FakeStack*)fakestack, |
| 466 | (uptr*)bottom_old, |
| 467 | (uptr*)size_old); |
Dmitry Vyukov | 47b7c5c | 2016-06-21 12:29:18 +0000 | [diff] [blame] | 468 | } |
| 469 | } |