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 | 7e84349 | 2013-03-28 15:42:43 +0000 | [diff] [blame] | 16 | #include "asan_poisoning.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 | 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" |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 21 | #include "sanitizer_common/sanitizer_placement_new.h" |
Kostya Serebryany | 6d95869 | 2013-10-18 14:50:44 +0000 | [diff] [blame] | 22 | #include "sanitizer_common/sanitizer_stackdepot.h" |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame^] | 23 | #include "sanitizer_common/sanitizer_tls_get_addr.h" |
Sergey Matveev | f1ac1a4 | 2013-05-21 13:40:13 +0000 | [diff] [blame] | 24 | #include "lsan/lsan_common.h" |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 25 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 26 | namespace __asan { |
| 27 | |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 28 | // AsanThreadContext implementation. |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 29 | |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 30 | void AsanThreadContext::OnCreated(void *arg) { |
| 31 | CreateThreadContextArgs *args = static_cast<CreateThreadContextArgs*>(arg); |
Kostya Serebryany | 6d95869 | 2013-10-18 14:50:44 +0000 | [diff] [blame] | 32 | if (args->stack) |
| 33 | stack_id = StackDepotPut(args->stack->trace, args->stack->size); |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 34 | thread = args->thread; |
| 35 | thread->set_context(this); |
| 36 | } |
| 37 | |
| 38 | void AsanThreadContext::OnFinished() { |
| 39 | // Drop the link to the AsanThread object. |
| 40 | thread = 0; |
| 41 | } |
| 42 | |
Kostya Serebryany | 40527a5 | 2013-06-03 14:49:25 +0000 | [diff] [blame] | 43 | // MIPS requires aligned address |
Timur Iskhodzhanov | 7c9ffde | 2013-06-04 08:25:17 +0000 | [diff] [blame] | 44 | static ALIGNED(16) char thread_registry_placeholder[sizeof(ThreadRegistry)]; |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 45 | static ThreadRegistry *asan_thread_registry; |
| 46 | |
Kostya Serebryany | 1fe68b8 | 2013-10-18 15:07:07 +0000 | [diff] [blame] | 47 | static BlockingMutex mu_for_thread_context(LINKER_INITIALIZED); |
| 48 | static LowLevelAllocator allocator_for_thread_context; |
| 49 | |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 50 | static ThreadContextBase *GetAsanThreadContext(u32 tid) { |
Kostya Serebryany | 1fe68b8 | 2013-10-18 15:07:07 +0000 | [diff] [blame] | 51 | BlockingMutexLock lock(&mu_for_thread_context); |
Peter Collingbourne | 5317724 | 2013-10-24 06:23:39 +0000 | [diff] [blame] | 52 | return new(allocator_for_thread_context) AsanThreadContext(tid); |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | ThreadRegistry &asanThreadRegistry() { |
| 56 | static bool initialized; |
| 57 | // Don't worry about thread_safety - this should be called when there is |
| 58 | // a single thread. |
| 59 | if (!initialized) { |
| 60 | // Never reuse ASan threads: we store pointer to AsanThreadContext |
| 61 | // in TSD and can't reliably tell when no more TSD destructors will |
| 62 | // be called. It would be wrong to reuse AsanThreadContext for another |
| 63 | // thread before all TSD destructors will be called for it. |
| 64 | asan_thread_registry = new(thread_registry_placeholder) ThreadRegistry( |
| 65 | GetAsanThreadContext, kMaxNumberOfThreads, kMaxNumberOfThreads); |
| 66 | initialized = true; |
| 67 | } |
| 68 | return *asan_thread_registry; |
| 69 | } |
| 70 | |
| 71 | AsanThreadContext *GetThreadContextByTidLocked(u32 tid) { |
| 72 | return static_cast<AsanThreadContext *>( |
| 73 | asanThreadRegistry().GetThreadLocked(tid)); |
| 74 | } |
| 75 | |
| 76 | // AsanThread implementation. |
| 77 | |
| 78 | AsanThread *AsanThread::Create(thread_callback_t start_routine, |
| 79 | void *arg) { |
Kostya Serebryany | f67ec2b | 2012-11-23 15:38:49 +0000 | [diff] [blame] | 80 | uptr PageSize = GetPageSizeCached(); |
| 81 | uptr size = RoundUpTo(sizeof(AsanThread), PageSize); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame^] | 82 | AsanThread *thread = (AsanThread*)MmapOrDie(size, __func__); |
Alexey Samsonov | 55cdfc6 | 2012-01-17 06:35:31 +0000 | [diff] [blame] | 83 | thread->start_routine_ = start_routine; |
| 84 | thread->arg_ = arg; |
Alexey Samsonov | 55cdfc6 | 2012-01-17 06:35:31 +0000 | [diff] [blame] | 85 | |
| 86 | return thread; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 89 | void AsanThread::TSDDtor(void *tsd) { |
| 90 | AsanThreadContext *context = (AsanThreadContext*)tsd; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame^] | 91 | VReport(1, "T%d TSDDtor\n", context->tid); |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 92 | if (context->thread) |
| 93 | context->thread->Destroy(); |
Kostya Serebryany | f58f998 | 2012-02-07 00:27:15 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Kostya Serebryany | a6b5226 | 2012-01-06 19:44:11 +0000 | [diff] [blame] | 96 | void AsanThread::Destroy() { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame^] | 97 | int tid = this->tid(); |
| 98 | VReport(1, "T%d exited\n", tid); |
Kostya Serebryany | f58f998 | 2012-02-07 00:27:15 +0000 | [diff] [blame] | 99 | |
Kostya Serebryany | ae914e2 | 2013-11-13 13:27:44 +0000 | [diff] [blame] | 100 | malloc_storage().CommitBack(); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame^] | 101 | if (common_flags()->use_sigaltstack) UnsetAlternateSignalStack(); |
| 102 | asanThreadRegistry().FinishThread(tid); |
Alexey Samsonov | 717ece5 | 2013-09-02 08:39:07 +0000 | [diff] [blame] | 103 | FlushToDeadThreadStats(&stats_); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 104 | // We also clear the shadow on thread destruction because |
| 105 | // some code may still be executing in later TSD destructors |
| 106 | // and we don't want it to have any poisoned stack. |
Sergey Matveev | 12d01ba | 2013-05-29 13:09:44 +0000 | [diff] [blame] | 107 | ClearShadowForThreadStackAndTLS(); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame^] | 108 | DeleteFakeStack(tid); |
Kostya Serebryany | f67ec2b | 2012-11-23 15:38:49 +0000 | [diff] [blame] | 109 | uptr size = RoundUpTo(sizeof(AsanThread), GetPageSizeCached()); |
Alexey Samsonov | a25b346 | 2012-06-06 16:15:07 +0000 | [diff] [blame] | 110 | UnmapOrDie(this, size); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame^] | 111 | DTLS_Destroy(); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Kostya Serebryany | c98fc1f | 2013-09-12 08:34:50 +0000 | [diff] [blame] | 114 | // We want to create the FakeStack lazyly on the first use, but not eralier |
| 115 | // than the stack size is known and the procedure has to be async-signal safe. |
| 116 | FakeStack *AsanThread::AsyncSignalSafeLazyInitFakeStack() { |
| 117 | uptr stack_size = this->stack_size(); |
| 118 | if (stack_size == 0) // stack_size is not yet available, don't use FakeStack. |
| 119 | return 0; |
| 120 | uptr old_val = 0; |
| 121 | // fake_stack_ has 3 states: |
| 122 | // 0 -- not initialized |
| 123 | // 1 -- being initialized |
| 124 | // ptr -- initialized |
| 125 | // This CAS checks if the state was 0 and if so changes it to state 1, |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame^] | 126 | // if that was successful, it initializes the pointer. |
Kostya Serebryany | c98fc1f | 2013-09-12 08:34:50 +0000 | [diff] [blame] | 127 | if (atomic_compare_exchange_strong( |
| 128 | reinterpret_cast<atomic_uintptr_t *>(&fake_stack_), &old_val, 1UL, |
Kostya Serebryany | 9433af3 | 2013-09-13 06:32:26 +0000 | [diff] [blame] | 129 | memory_order_relaxed)) { |
Kostya Serebryany | 230e52f | 2013-09-18 10:35:12 +0000 | [diff] [blame] | 130 | uptr stack_size_log = Log2(RoundUpToPowerOfTwo(stack_size)); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame^] | 131 | CHECK_LE(flags()->min_uar_stack_size_log, flags()->max_uar_stack_size_log); |
| 132 | stack_size_log = |
| 133 | Min(stack_size_log, static_cast<uptr>(flags()->max_uar_stack_size_log)); |
| 134 | stack_size_log = |
| 135 | Max(stack_size_log, static_cast<uptr>(flags()->min_uar_stack_size_log)); |
Kostya Serebryany | 230e52f | 2013-09-18 10:35:12 +0000 | [diff] [blame] | 136 | fake_stack_ = FakeStack::Create(stack_size_log); |
Kostya Serebryany | 9433af3 | 2013-09-13 06:32:26 +0000 | [diff] [blame] | 137 | SetTLSFakeStack(fake_stack_); |
| 138 | return fake_stack_; |
| 139 | } |
Kostya Serebryany | c98fc1f | 2013-09-12 08:34:50 +0000 | [diff] [blame] | 140 | return 0; |
| 141 | } |
| 142 | |
Kostya Serebryany | 69eca73 | 2011-12-16 19:13:35 +0000 | [diff] [blame] | 143 | void AsanThread::Init() { |
Sergey Matveev | 12d01ba | 2013-05-29 13:09:44 +0000 | [diff] [blame] | 144 | SetThreadStackAndTls(); |
Alexey Samsonov | 55cdfc6 | 2012-01-17 06:35:31 +0000 | [diff] [blame] | 145 | CHECK(AddrIsInMem(stack_bottom_)); |
Kostya Serebryany | 541cfb1 | 2013-01-18 11:30:36 +0000 | [diff] [blame] | 146 | CHECK(AddrIsInMem(stack_top_ - 1)); |
Sergey Matveev | 12d01ba | 2013-05-29 13:09:44 +0000 | [diff] [blame] | 147 | ClearShadowForThreadStackAndTLS(); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame^] | 148 | int local = 0; |
| 149 | VReport(1, "T%d: stack [%p,%p) size 0x%zx; local=%p\n", tid(), |
| 150 | (void *)stack_bottom_, (void *)stack_top_, stack_top_ - stack_bottom_, |
| 151 | &local); |
Kostya Serebryany | 7a0bba4 | 2013-06-26 12:16:05 +0000 | [diff] [blame] | 152 | fake_stack_ = 0; // Will be initialized lazily if needed. |
Alexander Potapenko | 75b19eb | 2012-07-23 14:07:58 +0000 | [diff] [blame] | 153 | AsanPlatformThreadInit(); |
Kostya Serebryany | 69eca73 | 2011-12-16 19:13:35 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 156 | thread_return_t AsanThread::ThreadStart(uptr os_id) { |
Kostya Serebryany | 69eca73 | 2011-12-16 19:13:35 +0000 | [diff] [blame] | 157 | Init(); |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 158 | asanThreadRegistry().StartThread(tid(), os_id, 0); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame^] | 159 | if (common_flags()->use_sigaltstack) SetAlternateSignalStack(); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 160 | |
| 161 | if (!start_routine_) { |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 162 | // 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] | 163 | // OS X libdispatch worker threads. But nobody is supposed to call |
| 164 | // ThreadStart() for the worker threads. |
Kostya Serebryany | a27bdf7 | 2013-04-05 14:40:25 +0000 | [diff] [blame] | 165 | CHECK_EQ(tid(), 0); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 166 | return 0; |
| 167 | } |
| 168 | |
Timur Iskhodzhanov | 600972e | 2012-02-24 15:28:43 +0000 | [diff] [blame] | 169 | thread_return_t res = start_routine_(arg_); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 170 | |
Sergey Matveev | e86e35f | 2013-10-14 12:01:05 +0000 | [diff] [blame] | 171 | // On POSIX systems we defer this to the TSD destructor. LSan will consider |
| 172 | // the thread's memory as non-live from the moment we call Destroy(), even |
| 173 | // though that memory might contain pointers to heap objects which will be |
| 174 | // cleaned up by a user-defined TSD destructor. Thus, calling Destroy() before |
| 175 | // the TSD destructors have run might cause false positives in LSan. |
| 176 | if (!SANITIZER_POSIX) |
| 177 | this->Destroy(); |
Kostya Serebryany | af34415 | 2012-01-11 02:03:16 +0000 | [diff] [blame] | 178 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 179 | return res; |
| 180 | } |
| 181 | |
Sergey Matveev | 12d01ba | 2013-05-29 13:09:44 +0000 | [diff] [blame] | 182 | void AsanThread::SetThreadStackAndTls() { |
Kostya Serebryany | 621770a | 2013-09-19 14:59:52 +0000 | [diff] [blame] | 183 | uptr tls_size = 0; |
| 184 | GetThreadStackAndTls(tid() == 0, &stack_bottom_, &stack_size_, &tls_begin_, |
Sergey Matveev | 12d01ba | 2013-05-29 13:09:44 +0000 | [diff] [blame] | 185 | &tls_size); |
Kostya Serebryany | 621770a | 2013-09-19 14:59:52 +0000 | [diff] [blame] | 186 | stack_top_ = stack_bottom_ + stack_size_; |
Sergey Matveev | 12d01ba | 2013-05-29 13:09:44 +0000 | [diff] [blame] | 187 | tls_end_ = tls_begin_ + tls_size; |
| 188 | |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 189 | int local; |
| 190 | CHECK(AddrIsInStack((uptr)&local)); |
| 191 | } |
| 192 | |
Sergey Matveev | 12d01ba | 2013-05-29 13:09:44 +0000 | [diff] [blame] | 193 | void AsanThread::ClearShadowForThreadStackAndTLS() { |
Alexey Samsonov | 55cdfc6 | 2012-01-17 06:35:31 +0000 | [diff] [blame] | 194 | PoisonShadow(stack_bottom_, stack_top_ - stack_bottom_, 0); |
Sergey Matveev | 12d01ba | 2013-05-29 13:09:44 +0000 | [diff] [blame] | 195 | if (tls_begin_ != tls_end_) |
| 196 | PoisonShadow(tls_begin_, tls_end_ - tls_begin_, 0); |
Alexey Samsonov | 55cdfc6 | 2012-01-17 06:35:31 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Kostya Serebryany | 50f3daa | 2013-03-22 10:36:24 +0000 | [diff] [blame] | 199 | const char *AsanThread::GetFrameNameByAddr(uptr addr, uptr *offset, |
| 200 | uptr *frame_pc) { |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 201 | uptr bottom = 0; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 202 | if (AddrIsInStack(addr)) { |
| 203 | bottom = stack_bottom(); |
Kostya Serebryany | dcf98bf | 2013-09-12 08:43:44 +0000 | [diff] [blame] | 204 | } else if (has_fake_stack()) { |
Kostya Serebryany | 7a0bba4 | 2013-06-26 12:16:05 +0000 | [diff] [blame] | 205 | bottom = fake_stack()->AddrIsInFakeStack(addr); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 206 | CHECK(bottom); |
Alexander Potapenko | e406c8c | 2012-11-15 15:24:42 +0000 | [diff] [blame] | 207 | *offset = addr - bottom; |
Kostya Serebryany | 50f3daa | 2013-03-22 10:36:24 +0000 | [diff] [blame] | 208 | *frame_pc = ((uptr*)bottom)[2]; |
Alexander Potapenko | e406c8c | 2012-11-15 15:24:42 +0000 | [diff] [blame] | 209 | return (const char *)((uptr*)bottom)[1]; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 210 | } |
Kostya Serebryany | 5af39e5 | 2012-11-21 12:38:58 +0000 | [diff] [blame] | 211 | uptr aligned_addr = addr & ~(SANITIZER_WORDSIZE/8 - 1); // align addr. |
Kostya Serebryany | ee39255 | 2012-05-31 15:02:07 +0000 | [diff] [blame] | 212 | u8 *shadow_ptr = (u8*)MemToShadow(aligned_addr); |
| 213 | u8 *shadow_bottom = (u8*)MemToShadow(bottom); |
Evgeniy Stepanov | 3972ea0 | 2012-05-12 12:33:10 +0000 | [diff] [blame] | 214 | |
| 215 | while (shadow_ptr >= shadow_bottom && |
Alexander Potapenko | e406c8c | 2012-11-15 15:24:42 +0000 | [diff] [blame] | 216 | *shadow_ptr != kAsanStackLeftRedzoneMagic) { |
Evgeniy Stepanov | 3972ea0 | 2012-05-12 12:33:10 +0000 | [diff] [blame] | 217 | shadow_ptr--; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 218 | } |
Evgeniy Stepanov | 3972ea0 | 2012-05-12 12:33:10 +0000 | [diff] [blame] | 219 | |
| 220 | while (shadow_ptr >= shadow_bottom && |
Alexander Potapenko | e406c8c | 2012-11-15 15:24:42 +0000 | [diff] [blame] | 221 | *shadow_ptr == kAsanStackLeftRedzoneMagic) { |
Evgeniy Stepanov | 3972ea0 | 2012-05-12 12:33:10 +0000 | [diff] [blame] | 222 | shadow_ptr--; |
| 223 | } |
| 224 | |
| 225 | if (shadow_ptr < shadow_bottom) { |
Alexander Potapenko | e406c8c | 2012-11-15 15:24:42 +0000 | [diff] [blame] | 226 | *offset = 0; |
| 227 | return "UNKNOWN"; |
Evgeniy Stepanov | 3972ea0 | 2012-05-12 12:33:10 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 230 | uptr* ptr = (uptr*)SHADOW_TO_MEM((uptr)(shadow_ptr + 1)); |
Alexander Potapenko | e406c8c | 2012-11-15 15:24:42 +0000 | [diff] [blame] | 231 | CHECK(ptr[0] == kCurrentStackFrameMagic); |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 232 | *offset = addr - (uptr)ptr; |
Kostya Serebryany | 50f3daa | 2013-03-22 10:36:24 +0000 | [diff] [blame] | 233 | *frame_pc = ptr[2]; |
Evgeniy Stepanov | 3972ea0 | 2012-05-12 12:33:10 +0000 | [diff] [blame] | 234 | return (const char*)ptr[1]; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 237 | static bool ThreadStackContainsAddress(ThreadContextBase *tctx_base, |
| 238 | void *addr) { |
| 239 | AsanThreadContext *tctx = static_cast<AsanThreadContext*>(tctx_base); |
| 240 | AsanThread *t = tctx->thread; |
Kostya Serebryany | 7a0bba4 | 2013-06-26 12:16:05 +0000 | [diff] [blame] | 241 | if (!t) return false; |
| 242 | if (t->AddrIsInStack((uptr)addr)) return true; |
Kostya Serebryany | b39a604 | 2013-09-12 08:47:00 +0000 | [diff] [blame] | 243 | if (t->has_fake_stack() && t->fake_stack()->AddrIsInFakeStack((uptr)addr)) |
Kostya Serebryany | 7a0bba4 | 2013-06-26 12:16:05 +0000 | [diff] [blame] | 244 | return true; |
| 245 | return false; |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Alexey Samsonov | 89c1384 | 2013-03-20 09:23:28 +0000 | [diff] [blame] | 248 | AsanThread *GetCurrentThread() { |
Sergey Matveev | c6ac98d | 2013-07-08 12:57:24 +0000 | [diff] [blame] | 249 | AsanThreadContext *context = |
| 250 | reinterpret_cast<AsanThreadContext *>(AsanTSDGet()); |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 251 | if (!context) { |
| 252 | if (SANITIZER_ANDROID) { |
| 253 | // On Android, libc constructor is called _after_ asan_init, and cleans up |
| 254 | // TSD. Try to figure out if this is still the main thread by the stack |
| 255 | // address. We are not entirely sure that we have correct main thread |
Dmitry Vyukov | 195369b | 2013-03-22 07:29:59 +0000 | [diff] [blame] | 256 | // limits, so only do this magic on Android, and only if the found thread |
| 257 | // is the main thread. |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 258 | AsanThreadContext *tctx = GetThreadContextByTidLocked(0); |
| 259 | if (ThreadStackContainsAddress(tctx, &context)) { |
| 260 | SetCurrentThread(tctx->thread); |
| 261 | return tctx->thread; |
| 262 | } |
Alexey Samsonov | 89c1384 | 2013-03-20 09:23:28 +0000 | [diff] [blame] | 263 | } |
Alexey Samsonov | 89c1384 | 2013-03-20 09:23:28 +0000 | [diff] [blame] | 264 | return 0; |
| 265 | } |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 266 | return context->thread; |
Alexey Samsonov | 89c1384 | 2013-03-20 09:23:28 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | void SetCurrentThread(AsanThread *t) { |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 270 | CHECK(t->context()); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame^] | 271 | VReport(2, "SetCurrentThread: %p for thread %p\n", t->context(), |
| 272 | (void *)GetThreadSelf()); |
Alexey Samsonov | 89c1384 | 2013-03-20 09:23:28 +0000 | [diff] [blame] | 273 | // Make sure we do not reset the current AsanThread. |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 274 | CHECK_EQ(0, AsanTSDGet()); |
| 275 | AsanTSDSet(t->context()); |
| 276 | CHECK_EQ(t->context(), AsanTSDGet()); |
Alexey Samsonov | 89c1384 | 2013-03-20 09:23:28 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | u32 GetCurrentTidOrInvalid() { |
| 280 | AsanThread *t = GetCurrentThread(); |
| 281 | return t ? t->tid() : kInvalidTid; |
| 282 | } |
| 283 | |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 284 | AsanThread *FindThreadByStackAddress(uptr addr) { |
| 285 | asanThreadRegistry().CheckLocked(); |
| 286 | AsanThreadContext *tctx = static_cast<AsanThreadContext *>( |
| 287 | asanThreadRegistry().FindThreadContextLocked(ThreadStackContainsAddress, |
| 288 | (void *)addr)); |
| 289 | return tctx ? tctx->thread : 0; |
| 290 | } |
Sergey Matveev | c6ac98d | 2013-07-08 12:57:24 +0000 | [diff] [blame] | 291 | |
| 292 | void EnsureMainThreadIDIsCorrect() { |
| 293 | AsanThreadContext *context = |
| 294 | reinterpret_cast<AsanThreadContext *>(AsanTSDGet()); |
| 295 | if (context && (context->tid == 0)) |
| 296 | context->os_id = GetTid(); |
| 297 | } |
Sergey Matveev | c519335 | 2013-10-14 14:04:50 +0000 | [diff] [blame] | 298 | |
| 299 | __asan::AsanThread *GetAsanThreadByOsIDLocked(uptr os_id) { |
| 300 | __asan::AsanThreadContext *context = static_cast<__asan::AsanThreadContext *>( |
| 301 | __asan::asanThreadRegistry().FindThreadContextByOsIDLocked(os_id)); |
| 302 | if (!context) return 0; |
| 303 | return context->thread; |
| 304 | } |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 305 | } // namespace __asan |
Sergey Matveev | f1ac1a4 | 2013-05-21 13:40:13 +0000 | [diff] [blame] | 306 | |
| 307 | // --- Implementation of LSan-specific functions --- {{{1 |
| 308 | namespace __lsan { |
| 309 | bool GetThreadRangesLocked(uptr os_id, uptr *stack_begin, uptr *stack_end, |
| 310 | uptr *tls_begin, uptr *tls_end, |
| 311 | uptr *cache_begin, uptr *cache_end) { |
Sergey Matveev | c519335 | 2013-10-14 14:04:50 +0000 | [diff] [blame] | 312 | __asan::AsanThread *t = __asan::GetAsanThreadByOsIDLocked(os_id); |
Sergey Matveev | 12d01ba | 2013-05-29 13:09:44 +0000 | [diff] [blame] | 313 | if (!t) return false; |
| 314 | *stack_begin = t->stack_bottom(); |
| 315 | *stack_end = t->stack_top(); |
| 316 | *tls_begin = t->tls_begin(); |
| 317 | *tls_end = t->tls_end(); |
| 318 | // ASan doesn't keep allocator caches in TLS, so these are unused. |
| 319 | *cache_begin = 0; |
| 320 | *cache_end = 0; |
| 321 | return true; |
Sergey Matveev | f1ac1a4 | 2013-05-21 13:40:13 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Sergey Matveev | c519335 | 2013-10-14 14:04:50 +0000 | [diff] [blame] | 324 | void ForEachExtraStackRange(uptr os_id, RangeIteratorCallback callback, |
| 325 | void *arg) { |
| 326 | __asan::AsanThread *t = __asan::GetAsanThreadByOsIDLocked(os_id); |
| 327 | if (t && t->has_fake_stack()) |
| 328 | t->fake_stack()->ForEachFakeFrame(callback, arg); |
| 329 | } |
| 330 | |
Sergey Matveev | f1ac1a4 | 2013-05-21 13:40:13 +0000 | [diff] [blame] | 331 | void LockThreadRegistry() { |
| 332 | __asan::asanThreadRegistry().Lock(); |
| 333 | } |
| 334 | |
| 335 | void UnlockThreadRegistry() { |
| 336 | __asan::asanThreadRegistry().Unlock(); |
| 337 | } |
Sergey Matveev | c6ac98d | 2013-07-08 12:57:24 +0000 | [diff] [blame] | 338 | |
| 339 | void EnsureMainThreadIDIsCorrect() { |
| 340 | __asan::EnsureMainThreadIDIsCorrect(); |
| 341 | } |
Sergey Matveev | f1ac1a4 | 2013-05-21 13:40:13 +0000 | [diff] [blame] | 342 | } // namespace __lsan |