blob: e620c717b861e07e91f518df1fb06fced1c91ad6 [file] [log] [blame]
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +00001//===-- 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>
Alexey Samsonovb823e3c2012-02-22 14:07:06 +000018#include <stdlib.h>
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +000019
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"
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +000025#include "asan_thread.h"
Alexey Samsonovde08c022012-06-19 09:21:57 +000026#include "sanitizer_common/sanitizer_libc.h"
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +000027
28namespace __asan {
29
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +000030// ---------------------- Stacktraces, symbols, etc. ---------------- {{{1
31static AsanLock dbghelp_lock(LINKER_INITIALIZED);
32static bool dbghelp_initialized = false;
33#pragma comment(lib, "dbghelp.lib")
34
Kostya Serebryany69f21742012-08-28 14:14:30 +000035void GetStackTrace(StackTrace *stack, uptr max_s, uptr pc, uptr bp) {
36 stack->max_size = max_s;
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +000037 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
Kostya Serebryany69f21742012-08-28 14:14:30 +000042 uptr cs_ret = CaptureStackBackTrace(1, stack->max_size, tmp, 0);
43 uptr offset = 0;
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +000044 // 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.
Kostya Serebryany45c8d1b2012-05-31 16:06:05 +000046 for (uptr i = 0; i < cs_ret; i++) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000047 if (pc != (uptr)tmp[i])
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +000048 continue;
49 offset = i;
50 break;
51 }
52
Kostya Serebryany69f21742012-08-28 14:14:30 +000053 stack->size = cs_ret - offset;
54 for (uptr i = 0; i < stack->size; i++)
55 stack->trace[i] = (uptr)tmp[i + offset];
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +000056}
57
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +000058// ---------------------- 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}
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +000093
94// ---------------------- TSD ---------------- {{{1
95static bool tsd_key_inited = false;
96
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000097static __declspec(thread) void *fake_tsd = 0;
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +000098
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}
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000113
114// ---------------------- Various stuff ---------------- {{{1
Alexander Potapenkoeb8c46e2012-08-24 09:22:05 +0000115void MaybeReexec() {
116 // No need to re-exec on Windows.
117}
118
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000119void *AsanDoesNotSupportStaticLinkage() {
Timur Iskhodzhanov2716a612012-03-12 11:45:09 +0000120#if defined(_DEBUG)
121#error Please build the runtime with a non-debug CRT: /MD or /MT
Timur Iskhodzhanov07bb9f12012-02-22 13:59:49 +0000122#endif
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000123 return 0;
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000124}
125
Alexander Potapenkof03d8af2012-04-05 10:54:52 +0000126void SetAlternateSignalStack() {
127 // FIXME: Decide what to do on Windows.
128}
129
130void UnsetAlternateSignalStack() {
131 // FIXME: Decide what to do on Windows.
132}
133
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000134void InstallSignalHandlers() {
135 // FIXME: Decide what to do on Windows.
136}
137
Alexander Potapenko75b19eb2012-07-23 14:07:58 +0000138void AsanPlatformThreadInit() {
139 // Nothing here for now.
140}
141
Alexey Samsonov08700282012-11-23 09:46:34 +0000142void ClearShadowMemoryForContext(void *context) {
143 UNIMPLEMENTED();
144}
145
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000146} // namespace __asan
147
Alexey Samsonov1ca53572012-10-02 12:11:17 +0000148// ---------------------- Interface ---------------- {{{1
149using namespace __asan; // NOLINT
150
Alexey Samsonov0ec37a62012-10-02 12:35:42 +0000151extern "C" {
152SANITIZER_INTERFACE_ATTRIBUTE NOINLINE
Alexey Samsonov1ca53572012-10-02 12:11:17 +0000153bool __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}
Alexey Samsonov0ec37a62012-10-02 12:35:42 +0000193} // extern "C"
Alexey Samsonov1ca53572012-10-02 12:11:17 +0000194
195
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000196#endif // _WIN32