asan_win.cc revision 0ec37a6e84b37ef4785b9863e98d0339c5a10190
1//===-- asan_win.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// Windows-specific details. 13//===----------------------------------------------------------------------===// 14#ifdef _WIN32 15#include <windows.h> 16 17#include <dbghelp.h> 18#include <stdlib.h> 19 20#include <new> // FIXME: temporarily needed for placement new in AsanLock. 21 22#include "asan_interceptors.h" 23#include "asan_internal.h" 24#include "asan_lock.h" 25#include "asan_thread.h" 26#include "sanitizer_common/sanitizer_libc.h" 27 28namespace __asan { 29 30// ---------------------- Stacktraces, symbols, etc. ---------------- {{{1 31static AsanLock dbghelp_lock(LINKER_INITIALIZED); 32static bool dbghelp_initialized = false; 33#pragma comment(lib, "dbghelp.lib") 34 35void GetStackTrace(StackTrace *stack, uptr max_s, uptr pc, uptr bp) { 36 stack->max_size = max_s; 37 void *tmp[kStackTraceMax]; 38 39 // FIXME: CaptureStackBackTrace might be too slow for us. 40 // FIXME: Compare with StackWalk64. 41 // FIXME: Look at LLVMUnhandledExceptionFilter in Signals.inc 42 uptr cs_ret = CaptureStackBackTrace(1, stack->max_size, tmp, 0); 43 uptr offset = 0; 44 // Skip the RTL frames by searching for the PC in the stacktrace. 45 // FIXME: this doesn't work well for the malloc/free stacks yet. 46 for (uptr i = 0; i < cs_ret; i++) { 47 if (pc != (uptr)tmp[i]) 48 continue; 49 offset = i; 50 break; 51 } 52 53 stack->size = cs_ret - offset; 54 for (uptr i = 0; i < stack->size; i++) 55 stack->trace[i] = (uptr)tmp[i + offset]; 56} 57 58// ---------------------- AsanLock ---------------- {{{1 59enum LockState { 60 LOCK_UNINITIALIZED = 0, 61 LOCK_READY = -1, 62}; 63 64AsanLock::AsanLock(LinkerInitialized li) { 65 // FIXME: see comments in AsanLock::Lock() for the details. 66 CHECK(li == LINKER_INITIALIZED || owner_ == LOCK_UNINITIALIZED); 67 68 CHECK(sizeof(CRITICAL_SECTION) <= sizeof(opaque_storage_)); 69 InitializeCriticalSection((LPCRITICAL_SECTION)opaque_storage_); 70 owner_ = LOCK_READY; 71} 72 73void AsanLock::Lock() { 74 if (owner_ == LOCK_UNINITIALIZED) { 75 // FIXME: hm, global AsanLock objects are not initialized?!? 76 // This might be a side effect of the clang+cl+link Frankenbuild... 77 new(this) AsanLock((LinkerInitialized)(LINKER_INITIALIZED + 1)); 78 79 // FIXME: If it turns out the linker doesn't invoke our 80 // constructors, we should probably manually Lock/Unlock all the global 81 // locks while we're starting in one thread to avoid double-init races. 82 } 83 EnterCriticalSection((LPCRITICAL_SECTION)opaque_storage_); 84 CHECK(owner_ == LOCK_READY); 85 owner_ = GetThreadSelf(); 86} 87 88void AsanLock::Unlock() { 89 CHECK(owner_ == GetThreadSelf()); 90 owner_ = LOCK_READY; 91 LeaveCriticalSection((LPCRITICAL_SECTION)opaque_storage_); 92} 93 94// ---------------------- TSD ---------------- {{{1 95static bool tsd_key_inited = false; 96 97static __declspec(thread) void *fake_tsd = 0; 98 99void AsanTSDInit(void (*destructor)(void *tsd)) { 100 // FIXME: we're ignoring the destructor for now. 101 tsd_key_inited = true; 102} 103 104void *AsanTSDGet() { 105 CHECK(tsd_key_inited); 106 return fake_tsd; 107} 108 109void AsanTSDSet(void *tsd) { 110 CHECK(tsd_key_inited); 111 fake_tsd = tsd; 112} 113 114// ---------------------- Various stuff ---------------- {{{1 115void MaybeReexec() { 116 // No need to re-exec on Windows. 117} 118 119void *AsanDoesNotSupportStaticLinkage() { 120#if defined(_DEBUG) 121#error Please build the runtime with a non-debug CRT: /MD or /MT 122#endif 123 return 0; 124} 125 126void SetAlternateSignalStack() { 127 // FIXME: Decide what to do on Windows. 128} 129 130void UnsetAlternateSignalStack() { 131 // FIXME: Decide what to do on Windows. 132} 133 134void InstallSignalHandlers() { 135 // FIXME: Decide what to do on Windows. 136} 137 138void AsanPlatformThreadInit() { 139 // Nothing here for now. 140} 141 142} // namespace __asan 143 144// ---------------------- Interface ---------------- {{{1 145using namespace __asan; // NOLINT 146 147extern "C" { 148SANITIZER_INTERFACE_ATTRIBUTE NOINLINE 149bool __asan_symbolize(const void *addr, char *out_buffer, int buffer_size) { 150 ScopedLock lock(&dbghelp_lock); 151 if (!dbghelp_initialized) { 152 SymSetOptions(SYMOPT_DEFERRED_LOADS | 153 SYMOPT_UNDNAME | 154 SYMOPT_LOAD_LINES); 155 CHECK(SymInitialize(GetCurrentProcess(), 0, TRUE)); 156 // FIXME: We don't call SymCleanup() on exit yet - should we? 157 dbghelp_initialized = true; 158 } 159 160 // See http://msdn.microsoft.com/en-us/library/ms680578(VS.85).aspx 161 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(CHAR)]; 162 PSYMBOL_INFO symbol = (PSYMBOL_INFO)buffer; 163 symbol->SizeOfStruct = sizeof(SYMBOL_INFO); 164 symbol->MaxNameLen = MAX_SYM_NAME; 165 DWORD64 offset = 0; 166 BOOL got_objname = SymFromAddr(GetCurrentProcess(), 167 (DWORD64)addr, &offset, symbol); 168 if (!got_objname) 169 return false; 170 171 DWORD unused; 172 IMAGEHLP_LINE64 info; 173 info.SizeOfStruct = sizeof(IMAGEHLP_LINE64); 174 BOOL got_fileline = SymGetLineFromAddr64(GetCurrentProcess(), 175 (DWORD64)addr, &unused, &info); 176 int written = 0; 177 out_buffer[0] = '\0'; 178 // FIXME: it might be useful to print out 'obj' or 'obj+offset' info too. 179 if (got_fileline) { 180 written += internal_snprintf(out_buffer + written, buffer_size - written, 181 " %s %s:%d", symbol->Name, 182 info.FileName, info.LineNumber); 183 } else { 184 written += internal_snprintf(out_buffer + written, buffer_size - written, 185 " %s+0x%p", symbol->Name, offset); 186 } 187 return true; 188} 189} // extern "C" 190 191 192#endif // _WIN32 193