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