1 //===-- asan_win.cc -------------------------------------------------------===// 2 // 3 // This file is distributed under the University of Illinois Open Source 4 // License. See LICENSE.TXT for details. 5 // 6 //===----------------------------------------------------------------------===// 7 // 8 // This file is a part of AddressSanitizer, an address sanity checker. 9 // 10 // Windows-specific details. 11 //===----------------------------------------------------------------------===// 12 #ifdef _WIN32 13 #include <windows.h> 14 15 #include <dbghelp.h> 16 #include <stdlib.h> 17 18 #include "asan_interceptors.h" 19 #include "asan_internal.h" 20 #include "asan_thread.h" 21 #include "sanitizer_common/sanitizer_libc.h" 22 #include "sanitizer_common/sanitizer_mutex.h" 23 24 namespace __asan { 25 26 // ---------------------- Stacktraces, symbols, etc. ---------------- {{{1 27 static BlockingMutex dbghelp_lock(LINKER_INITIALIZED); 28 static bool dbghelp_initialized = false; 29 #pragma comment(lib, "dbghelp.lib") 30 31 void GetStackTrace(StackTrace *stack, uptr max_s, uptr pc, uptr bp, bool fast) { 32 (void)fast; 33 stack->max_size = max_s; 34 void *tmp[kStackTraceMax]; 35 36 // FIXME: CaptureStackBackTrace might be too slow for us. 37 // FIXME: Compare with StackWalk64. 38 // FIXME: Look at LLVMUnhandledExceptionFilter in Signals.inc 39 uptr cs_ret = CaptureStackBackTrace(1, stack->max_size, tmp, 0); 40 uptr offset = 0; 41 // Skip the RTL frames by searching for the PC in the stacktrace. 42 // FIXME: this doesn't work well for the malloc/free stacks yet. 43 for (uptr i = 0; i < cs_ret; i++) { 44 if (pc != (uptr)tmp[i]) 45 continue; 46 offset = i; 47 break; 48 } 49 50 stack->size = cs_ret - offset; 51 for (uptr i = 0; i < stack->size; i++) 52 stack->trace[i] = (uptr)tmp[i + offset]; 53 } 54 55 // ---------------------- TSD ---------------- {{{1 56 static bool tsd_key_inited = false; 57 58 static __declspec(thread) void *fake_tsd = 0; 59 60 void AsanTSDInit(void (*destructor)(void *tsd)) { 61 // FIXME: we're ignoring the destructor for now. 62 tsd_key_inited = true; 63 } 64 65 void *AsanTSDGet() { 66 CHECK(tsd_key_inited); 67 return fake_tsd; 68 } 69 70 void AsanTSDSet(void *tsd) { 71 CHECK(tsd_key_inited); 72 fake_tsd = tsd; 73 } 74 75 // ---------------------- Various stuff ---------------- {{{1 76 void MaybeReexec() { 77 // No need to re-exec on Windows. 78 } 79 80 void *AsanDoesNotSupportStaticLinkage() { 81 #if defined(_DEBUG) 82 #error Please build the runtime with a non-debug CRT: /MD or /MT 83 #endif 84 return 0; 85 } 86 87 void SetAlternateSignalStack() { 88 // FIXME: Decide what to do on Windows. 89 } 90 91 void UnsetAlternateSignalStack() { 92 // FIXME: Decide what to do on Windows. 93 } 94 95 void InstallSignalHandlers() { 96 // FIXME: Decide what to do on Windows. 97 } 98 99 void AsanPlatformThreadInit() { 100 // Nothing here for now. 101 } 102 103 void ReadContextStack(void *context, uptr *stack, uptr *ssize) { 104 UNIMPLEMENTED(); 105 } 106 107 } // namespace __asan 108 109 // ---------------------- Interface ---------------- {{{1 110 using namespace __asan; // NOLINT 111 112 extern "C" { 113 SANITIZER_INTERFACE_ATTRIBUTE NOINLINE 114 bool __asan_symbolize(const void *addr, char *out_buffer, int buffer_size) { 115 BlockingMutexLock lock(&dbghelp_lock); 116 if (!dbghelp_initialized) { 117 SymSetOptions(SYMOPT_DEFERRED_LOADS | 118 SYMOPT_UNDNAME | 119 SYMOPT_LOAD_LINES); 120 CHECK(SymInitialize(GetCurrentProcess(), 0, TRUE)); 121 // FIXME: We don't call SymCleanup() on exit yet - should we? 122 dbghelp_initialized = true; 123 } 124 125 // See http://msdn.microsoft.com/en-us/library/ms680578(VS.85).aspx 126 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(CHAR)]; 127 PSYMBOL_INFO symbol = (PSYMBOL_INFO)buffer; 128 symbol->SizeOfStruct = sizeof(SYMBOL_INFO); 129 symbol->MaxNameLen = MAX_SYM_NAME; 130 DWORD64 offset = 0; 131 BOOL got_objname = SymFromAddr(GetCurrentProcess(), 132 (DWORD64)addr, &offset, symbol); 133 if (!got_objname) 134 return false; 135 136 DWORD unused; 137 IMAGEHLP_LINE64 info; 138 info.SizeOfStruct = sizeof(IMAGEHLP_LINE64); 139 BOOL got_fileline = SymGetLineFromAddr64(GetCurrentProcess(), 140 (DWORD64)addr, &unused, &info); 141 int written = 0; 142 out_buffer[0] = '\0'; 143 // FIXME: it might be useful to print out 'obj' or 'obj+offset' info too. 144 if (got_fileline) { 145 written += internal_snprintf(out_buffer + written, buffer_size - written, 146 " %s %s:%d", symbol->Name, 147 info.FileName, info.LineNumber); 148 } else { 149 written += internal_snprintf(out_buffer + written, buffer_size - written, 150 " %s+0x%p", symbol->Name, offset); 151 } 152 return true; 153 } 154 } // extern "C" 155 156 157 #endif // _WIN32 158