asan_thread.cc revision c3390df6670cb166119b961eb27a033fb9073496
1//===-- asan_thread.cc ----------------------------------------------------===// 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" 16#include "asan_stack.h" 17#include "asan_thread.h" 18#include "asan_thread_registry.h" 19#include "asan_mapping.h" 20#include "sanitizer_common/sanitizer_common.h" 21 22namespace __asan { 23 24AsanThread::AsanThread(LinkerInitialized x) 25 : fake_stack_(x), 26 malloc_storage_(x), 27 stats_(x) { } 28 29static AsanLock mu_for_thread_summary(LINKER_INITIALIZED); 30static LowLevelAllocator allocator_for_thread_summary; 31 32AsanThread *AsanThread::Create(u32 parent_tid, thread_callback_t start_routine, 33 void *arg, StackTrace *stack) { 34 uptr size = RoundUpTo(sizeof(AsanThread), kPageSize); 35 AsanThread *thread = (AsanThread*)MmapOrDie(size, __FUNCTION__); 36 thread->start_routine_ = start_routine; 37 thread->arg_ = arg; 38 39 const uptr kSummaryAllocSize = 1024; 40 CHECK_LE(sizeof(AsanThreadSummary), kSummaryAllocSize); 41 AsanThreadSummary *summary; 42 { 43 ScopedLock lock(&mu_for_thread_summary); 44 summary = (AsanThreadSummary*) 45 allocator_for_thread_summary.Allocate(kSummaryAllocSize); 46 } 47 summary->Init(parent_tid, stack); 48 summary->set_thread(thread); 49 thread->set_summary(summary); 50 51 return thread; 52} 53 54void AsanThreadSummary::TSDDtor(void *tsd) { 55 AsanThreadSummary *summary = (AsanThreadSummary*)tsd; 56 if (flags()->verbosity >= 1) { 57 Report("T%d TSDDtor\n", summary->tid()); 58 } 59 if (summary->thread()) { 60 summary->thread()->Destroy(); 61 } 62} 63 64void AsanThread::Destroy() { 65 if (flags()->verbosity >= 1) { 66 Report("T%d exited\n", tid()); 67 } 68 69 asanThreadRegistry().UnregisterThread(this); 70 CHECK(summary()->thread() == 0); 71 // We also clear the shadow on thread destruction because 72 // some code may still be executing in later TSD destructors 73 // and we don't want it to have any poisoned stack. 74 ClearShadowForThreadStack(); 75 fake_stack().Cleanup(); 76 uptr size = RoundUpTo(sizeof(AsanThread), kPageSize); 77 UnmapOrDie(this, size); 78} 79 80void AsanThread::Init() { 81 SetThreadStackTopAndBottom(); 82 CHECK(AddrIsInMem(stack_bottom_)); 83 CHECK(AddrIsInMem(stack_top_)); 84 ClearShadowForThreadStack(); 85 if (flags()->verbosity >= 1) { 86 int local = 0; 87 Report("T%d: stack [%p,%p) size 0x%zx; local=%p\n", 88 tid(), (void*)stack_bottom_, (void*)stack_top_, 89 stack_top_ - stack_bottom_, &local); 90 } 91 fake_stack_.Init(stack_size()); 92 AsanPlatformThreadInit(); 93} 94 95thread_return_t AsanThread::ThreadStart() { 96 Init(); 97 if (flags()->use_sigaltstack) SetAlternateSignalStack(); 98 99 if (!start_routine_) { 100 // start_routine_ == 0 if we're on the main thread or on one of the 101 // OS X libdispatch worker threads. But nobody is supposed to call 102 // ThreadStart() for the worker threads. 103 CHECK(tid() == 0); 104 return 0; 105 } 106 107 thread_return_t res = start_routine_(arg_); 108 malloc_storage().CommitBack(); 109 if (flags()->use_sigaltstack) UnsetAlternateSignalStack(); 110 111 this->Destroy(); 112 113 return res; 114} 115 116void AsanThread::SetThreadStackTopAndBottom() { 117 GetThreadStackTopAndBottom(tid() == 0, &stack_top_, &stack_bottom_); 118 int local; 119 CHECK(AddrIsInStack((uptr)&local)); 120} 121 122void AsanThread::ClearShadowForThreadStack() { 123 PoisonShadow(stack_bottom_, stack_top_ - stack_bottom_, 0); 124} 125 126const char *AsanThread::GetFrameNameByAddr(uptr addr, uptr *offset) { 127 uptr bottom = 0; 128 bool is_fake_stack = false; 129 if (AddrIsInStack(addr)) { 130 bottom = stack_bottom(); 131 } else { 132 bottom = fake_stack().AddrIsInFakeStack(addr); 133 CHECK(bottom); 134 is_fake_stack = true; 135 } 136 uptr aligned_addr = addr & ~(__WORDSIZE/8 - 1); // align addr. 137 u8 *shadow_ptr = (u8*)MemToShadow(aligned_addr); 138 u8 *shadow_bottom = (u8*)MemToShadow(bottom); 139 140 while (shadow_ptr >= shadow_bottom && 141 *shadow_ptr != kAsanStackLeftRedzoneMagic) { 142 shadow_ptr--; 143 } 144 145 while (shadow_ptr >= shadow_bottom && 146 *shadow_ptr == kAsanStackLeftRedzoneMagic) { 147 shadow_ptr--; 148 } 149 150 if (shadow_ptr < shadow_bottom) { 151 *offset = 0; 152 return "UNKNOWN"; 153 } 154 155 uptr* ptr = (uptr*)SHADOW_TO_MEM((uptr)(shadow_ptr + 1)); 156 CHECK((ptr[0] == kCurrentStackFrameMagic) || 157 (is_fake_stack && ptr[0] == kRetiredStackFrameMagic)); 158 *offset = addr - (uptr)ptr; 159 return (const char*)ptr[1]; 160} 161 162} // namespace __asan 163