asan_win.cc revision 219f1741dad7b8ea07851ac1d96d3b0a99198d44
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 "asan_interceptors.h" 21#include "asan_internal.h" 22#include "asan_lock.h" 23#include "asan_thread.h" 24#include "sanitizer_common/sanitizer_libc.h" 25#include "sanitizer_common/sanitizer_placement_new.h" 26 27namespace __asan { 28 29// ---------------------- Stacktraces, symbols, etc. ---------------- {{{1 30static AsanLock dbghelp_lock(LINKER_INITIALIZED); 31static bool dbghelp_initialized = false; 32#pragma comment(lib, "dbghelp.lib") 33 34void GetStackTrace(StackTrace *stack, uptr max_s, uptr pc, uptr bp, bool fast) { 35 (void)fast; 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 142void ClearShadowMemoryForContext(void *context) { 143 UNIMPLEMENTED(); 144} 145 146} // namespace __asan 147 148// ---------------------- Interface ---------------- {{{1 149using namespace __asan; // NOLINT 150 151extern "C" { 152SANITIZER_INTERFACE_ATTRIBUTE NOINLINE 153bool __asan_symbolize(const void *addr, char *out_buffer, int buffer_size) { 154 ScopedLock lock(&dbghelp_lock); 155 if (!dbghelp_initialized) { 156 SymSetOptions(SYMOPT_DEFERRED_LOADS | 157 SYMOPT_UNDNAME | 158 SYMOPT_LOAD_LINES); 159 CHECK(SymInitialize(GetCurrentProcess(), 0, TRUE)); 160 // FIXME: We don't call SymCleanup() on exit yet - should we? 161 dbghelp_initialized = true; 162 } 163 164 // See http://msdn.microsoft.com/en-us/library/ms680578(VS.85).aspx 165 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(CHAR)]; 166 PSYMBOL_INFO symbol = (PSYMBOL_INFO)buffer; 167 symbol->SizeOfStruct = sizeof(SYMBOL_INFO); 168 symbol->MaxNameLen = MAX_SYM_NAME; 169 DWORD64 offset = 0; 170 BOOL got_objname = SymFromAddr(GetCurrentProcess(), 171 (DWORD64)addr, &offset, symbol); 172 if (!got_objname) 173 return false; 174 175 DWORD unused; 176 IMAGEHLP_LINE64 info; 177 info.SizeOfStruct = sizeof(IMAGEHLP_LINE64); 178 BOOL got_fileline = SymGetLineFromAddr64(GetCurrentProcess(), 179 (DWORD64)addr, &unused, &info); 180 int written = 0; 181 out_buffer[0] = '\0'; 182 // FIXME: it might be useful to print out 'obj' or 'obj+offset' info too. 183 if (got_fileline) { 184 written += internal_snprintf(out_buffer + written, buffer_size - written, 185 " %s %s:%d", symbol->Name, 186 info.FileName, info.LineNumber); 187 } else { 188 written += internal_snprintf(out_buffer + written, buffer_size - written, 189 " %s+0x%p", symbol->Name, offset); 190 } 191 return true; 192} 193} // extern "C" 194 195 196#endif // _WIN32 197