Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Evgeniy Stepanov | 24e1372 | 2013-03-19 14:33:38 +0000 | [diff] [blame] | 14 | |
| 15 | #include "sanitizer_common/sanitizer_platform.h" |
Evgeniy Stepanov | 30e110e | 2013-03-19 14:54:17 +0000 | [diff] [blame] | 16 | #if SANITIZER_WINDOWS |
Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 17 | #include <windows.h> |
| 18 | |
| 19 | #include <dbghelp.h> |
Alexey Samsonov | b823e3c | 2012-02-22 14:07:06 +0000 | [diff] [blame] | 20 | #include <stdlib.h> |
Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 21 | |
Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 22 | #include "asan_interceptors.h" |
| 23 | #include "asan_internal.h" |
Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 24 | #include "asan_thread.h" |
Alexey Samsonov | de08c02 | 2012-06-19 09:21:57 +0000 | [diff] [blame] | 25 | #include "sanitizer_common/sanitizer_libc.h" |
Dmitry Vyukov | f4f51f2 | 2013-01-14 07:51:39 +0000 | [diff] [blame] | 26 | #include "sanitizer_common/sanitizer_mutex.h" |
Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 27 | |
Timur Iskhodzhanov | c11fa09 | 2013-09-23 11:05:41 +0000 | [diff] [blame] | 28 | extern "C" { |
| 29 | SANITIZER_INTERFACE_ATTRIBUTE |
Timur Iskhodzhanov | def895b | 2013-09-23 11:19:43 +0000 | [diff] [blame] | 30 | int __asan_should_detect_stack_use_after_return() { |
Timur Iskhodzhanov | c11fa09 | 2013-09-23 11:05:41 +0000 | [diff] [blame] | 31 | __asan_init(); |
| 32 | return __asan_option_detect_stack_use_after_return; |
| 33 | } |
| 34 | } |
| 35 | |
Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 36 | namespace __asan { |
| 37 | |
Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 38 | // ---------------------- Stacktraces, symbols, etc. ---------------- {{{1 |
Dmitry Vyukov | f4f51f2 | 2013-01-14 07:51:39 +0000 | [diff] [blame] | 39 | static BlockingMutex dbghelp_lock(LINKER_INITIALIZED); |
Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 40 | static bool dbghelp_initialized = false; |
| 41 | #pragma comment(lib, "dbghelp.lib") |
| 42 | |
Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 43 | // ---------------------- TSD ---------------- {{{1 |
| 44 | static bool tsd_key_inited = false; |
| 45 | |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 46 | static __declspec(thread) void *fake_tsd = 0; |
Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 47 | |
| 48 | void AsanTSDInit(void (*destructor)(void *tsd)) { |
| 49 | // FIXME: we're ignoring the destructor for now. |
| 50 | tsd_key_inited = true; |
| 51 | } |
| 52 | |
| 53 | void *AsanTSDGet() { |
| 54 | CHECK(tsd_key_inited); |
| 55 | return fake_tsd; |
| 56 | } |
| 57 | |
| 58 | void AsanTSDSet(void *tsd) { |
| 59 | CHECK(tsd_key_inited); |
| 60 | fake_tsd = tsd; |
| 61 | } |
Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 62 | |
Sergey Matveev | e86e35f | 2013-10-14 12:01:05 +0000 | [diff] [blame] | 63 | void PlatformTSDDtor(void *tsd) { |
| 64 | AsanThread::TSDDtor(tsd); |
| 65 | } |
Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 66 | // ---------------------- Various stuff ---------------- {{{1 |
Alexander Potapenko | eb8c46e | 2012-08-24 09:22:05 +0000 | [diff] [blame] | 67 | void MaybeReexec() { |
| 68 | // No need to re-exec on Windows. |
| 69 | } |
| 70 | |
Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 71 | void *AsanDoesNotSupportStaticLinkage() { |
Timur Iskhodzhanov | 2716a61 | 2012-03-12 11:45:09 +0000 | [diff] [blame] | 72 | #if defined(_DEBUG) |
| 73 | #error Please build the runtime with a non-debug CRT: /MD or /MT |
Timur Iskhodzhanov | 07bb9f1 | 2012-02-22 13:59:49 +0000 | [diff] [blame] | 74 | #endif |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 75 | return 0; |
Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Alexander Potapenko | f03d8af | 2012-04-05 10:54:52 +0000 | [diff] [blame] | 78 | void SetAlternateSignalStack() { |
| 79 | // FIXME: Decide what to do on Windows. |
| 80 | } |
| 81 | |
| 82 | void UnsetAlternateSignalStack() { |
| 83 | // FIXME: Decide what to do on Windows. |
| 84 | } |
| 85 | |
Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 86 | void InstallSignalHandlers() { |
| 87 | // FIXME: Decide what to do on Windows. |
| 88 | } |
| 89 | |
Alexander Potapenko | 75b19eb | 2012-07-23 14:07:58 +0000 | [diff] [blame] | 90 | void AsanPlatformThreadInit() { |
| 91 | // Nothing here for now. |
| 92 | } |
| 93 | |
Alexey Samsonov | 57db4ba | 2013-01-17 15:45:28 +0000 | [diff] [blame] | 94 | void ReadContextStack(void *context, uptr *stack, uptr *ssize) { |
Alexey Samsonov | 0870028 | 2012-11-23 09:46:34 +0000 | [diff] [blame] | 95 | UNIMPLEMENTED(); |
| 96 | } |
| 97 | |
Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 98 | } // namespace __asan |
| 99 | |
Alexey Samsonov | 1ca5357 | 2012-10-02 12:11:17 +0000 | [diff] [blame] | 100 | // ---------------------- Interface ---------------- {{{1 |
| 101 | using namespace __asan; // NOLINT |
| 102 | |
Alexey Samsonov | 0ec37a6 | 2012-10-02 12:35:42 +0000 | [diff] [blame] | 103 | extern "C" { |
| 104 | SANITIZER_INTERFACE_ATTRIBUTE NOINLINE |
Alexey Samsonov | 1ca5357 | 2012-10-02 12:11:17 +0000 | [diff] [blame] | 105 | bool __asan_symbolize(const void *addr, char *out_buffer, int buffer_size) { |
Dmitry Vyukov | f4f51f2 | 2013-01-14 07:51:39 +0000 | [diff] [blame] | 106 | BlockingMutexLock lock(&dbghelp_lock); |
Alexey Samsonov | 1ca5357 | 2012-10-02 12:11:17 +0000 | [diff] [blame] | 107 | if (!dbghelp_initialized) { |
| 108 | SymSetOptions(SYMOPT_DEFERRED_LOADS | |
| 109 | SYMOPT_UNDNAME | |
| 110 | SYMOPT_LOAD_LINES); |
| 111 | CHECK(SymInitialize(GetCurrentProcess(), 0, TRUE)); |
| 112 | // FIXME: We don't call SymCleanup() on exit yet - should we? |
| 113 | dbghelp_initialized = true; |
| 114 | } |
| 115 | |
| 116 | // See http://msdn.microsoft.com/en-us/library/ms680578(VS.85).aspx |
| 117 | char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(CHAR)]; |
| 118 | PSYMBOL_INFO symbol = (PSYMBOL_INFO)buffer; |
| 119 | symbol->SizeOfStruct = sizeof(SYMBOL_INFO); |
| 120 | symbol->MaxNameLen = MAX_SYM_NAME; |
| 121 | DWORD64 offset = 0; |
| 122 | BOOL got_objname = SymFromAddr(GetCurrentProcess(), |
| 123 | (DWORD64)addr, &offset, symbol); |
| 124 | if (!got_objname) |
| 125 | return false; |
| 126 | |
| 127 | DWORD unused; |
| 128 | IMAGEHLP_LINE64 info; |
| 129 | info.SizeOfStruct = sizeof(IMAGEHLP_LINE64); |
| 130 | BOOL got_fileline = SymGetLineFromAddr64(GetCurrentProcess(), |
| 131 | (DWORD64)addr, &unused, &info); |
| 132 | int written = 0; |
| 133 | out_buffer[0] = '\0'; |
| 134 | // FIXME: it might be useful to print out 'obj' or 'obj+offset' info too. |
| 135 | if (got_fileline) { |
| 136 | written += internal_snprintf(out_buffer + written, buffer_size - written, |
| 137 | " %s %s:%d", symbol->Name, |
| 138 | info.FileName, info.LineNumber); |
| 139 | } else { |
| 140 | written += internal_snprintf(out_buffer + written, buffer_size - written, |
| 141 | " %s+0x%p", symbol->Name, offset); |
| 142 | } |
| 143 | return true; |
| 144 | } |
Alexey Samsonov | 0ec37a6 | 2012-10-02 12:35:42 +0000 | [diff] [blame] | 145 | } // extern "C" |
Alexey Samsonov | 1ca5357 | 2012-10-02 12:11:17 +0000 | [diff] [blame] | 146 | |
| 147 | |
Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 148 | #endif // _WIN32 |