blob: 51f32e91fad5063762bc9cf1daa1452fc1a7c441 [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>
18#include <stdio.h> // FIXME: get rid of this.
Alexey Samsonovb823e3c2012-02-22 14:07:06 +000019#include <stdlib.h>
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +000020
21#include <new> // FIXME: temporarily needed for placement new in AsanLock.
22
23#include "asan_interceptors.h"
24#include "asan_internal.h"
25#include "asan_lock.h"
26#include "asan_procmaps.h"
27#include "asan_thread.h"
28
Kostya Serebryany25c71782012-03-10 01:30:01 +000029// Should not add dependency on libstdc++,
30// since most of the stuff here is inlinable.
31#include <algorithm>
32
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +000033namespace __asan {
34
35// ---------------------- Memory management ---------------- {{{1
36void *AsanMmapFixedNoReserve(uintptr_t fixed_addr, size_t size) {
37 return VirtualAlloc((LPVOID)fixed_addr, size,
38 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
39}
40
41void *AsanMmapSomewhereOrDie(size_t size, const char *mem_type) {
42 void *rv = VirtualAlloc(NULL, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
43 if (rv == NULL)
44 OutOfMemoryMessageAndDie(mem_type, size);
45 return rv;
46}
47
48void *AsanMprotect(uintptr_t fixed_addr, size_t size) {
49 return VirtualAlloc((LPVOID)fixed_addr, size,
50 MEM_RESERVE | MEM_COMMIT, PAGE_NOACCESS);
51}
52
53void AsanUnmapOrDie(void *addr, size_t size) {
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +000054 CHECK(VirtualFree(addr, size, MEM_DECOMMIT));
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +000055}
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +000056
57// ---------------------- IO ---------------- {{{1
58size_t AsanWrite(int fd, const void *buf, size_t count) {
59 if (fd != 2)
60 UNIMPLEMENTED();
61
Timur Iskhodzhanov27a78002012-03-11 12:45:12 +000062 HANDLE err = GetStdHandle(STD_ERROR_HANDLE);
63 if (err == NULL)
64 return 0; // FIXME: this might not work on some apps.
65 DWORD ret;
66 if (!WriteFile(err, buf, count, &ret, NULL))
67 return 0;
68 return ret;
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +000069}
70
71// FIXME: Looks like these functions are not needed and are linked in by the
72// code unreachable on Windows. We should clean this up.
73int AsanOpenReadonly(const char* filename) {
74 UNIMPLEMENTED();
75 return -1;
76}
77
78size_t AsanRead(int fd, void *buf, size_t count) {
79 UNIMPLEMENTED();
80 return -1;
81}
82
83int AsanClose(int fd) {
84 UNIMPLEMENTED();
85 return -1;
86}
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +000087
88// ---------------------- Stacktraces, symbols, etc. ---------------- {{{1
89static AsanLock dbghelp_lock(LINKER_INITIALIZED);
90static bool dbghelp_initialized = false;
91#pragma comment(lib, "dbghelp.lib")
92
93void AsanThread::SetThreadStackTopAndBottom() {
94 MEMORY_BASIC_INFORMATION mbi;
95 CHECK(VirtualQuery(&mbi /* on stack */,
96 &mbi, sizeof(mbi)) != 0);
97 // FIXME: is it possible for the stack to not be a single allocation?
98 // Are these values what ASan expects to get (reserved, not committed;
99 // including stack guard page) ?
100 stack_top_ = (uintptr_t)mbi.BaseAddress + mbi.RegionSize;
101 stack_bottom_ = (uintptr_t)mbi.AllocationBase;
102}
103
104void AsanStackTrace::GetStackTrace(size_t max_s, uintptr_t pc, uintptr_t bp) {
105 max_size = max_s;
106 void *tmp[kStackTraceMax];
107
108 // FIXME: CaptureStackBackTrace might be too slow for us.
109 // FIXME: Compare with StackWalk64.
110 // FIXME: Look at LLVMUnhandledExceptionFilter in Signals.inc
111 size_t cs_ret = CaptureStackBackTrace(1, max_size, tmp, NULL),
112 offset = 0;
113 // Skip the RTL frames by searching for the PC in the stacktrace.
114 // FIXME: this doesn't work well for the malloc/free stacks yet.
115 for (size_t i = 0; i < cs_ret; i++) {
116 if (pc != (uintptr_t)tmp[i])
117 continue;
118 offset = i;
119 break;
120 }
121
122 size = cs_ret - offset;
123 for (size_t i = 0; i < size; i++)
124 trace[i] = (uintptr_t)tmp[i + offset];
125}
126
127bool WinSymbolize(const void *addr, char *out_buffer, int buffer_size) {
128 ScopedLock lock(&dbghelp_lock);
129 if (!dbghelp_initialized) {
130 SymSetOptions(SYMOPT_DEFERRED_LOADS |
131 SYMOPT_UNDNAME |
132 SYMOPT_LOAD_LINES);
133 CHECK(SymInitialize(GetCurrentProcess(), NULL, TRUE));
134 // FIXME: We don't call SymCleanup() on exit yet - should we?
135 dbghelp_initialized = true;
136 }
137
138 // See http://msdn.microsoft.com/en-us/library/ms680578(VS.85).aspx
139 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(CHAR)];
140 PSYMBOL_INFO symbol = (PSYMBOL_INFO)buffer;
141 symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
142 symbol->MaxNameLen = MAX_SYM_NAME;
143 DWORD64 offset = 0;
144 BOOL got_objname = SymFromAddr(GetCurrentProcess(),
145 (DWORD64)addr, &offset, symbol);
146 if (!got_objname)
147 return false;
148
149 DWORD unused;
150 IMAGEHLP_LINE64 info;
151 info.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
152 BOOL got_fileline = SymGetLineFromAddr64(GetCurrentProcess(),
153 (DWORD64)addr, &unused, &info);
154 int written = 0;
155 out_buffer[0] = '\0';
156 // FIXME: it might be useful to print out 'obj' or 'obj+offset' info too.
157 if (got_fileline) {
158 written += SNPrintf(out_buffer + written, buffer_size - written,
159 " %s %s:%d", symbol->Name,
160 info.FileName, info.LineNumber);
161 } else {
162 written += SNPrintf(out_buffer + written, buffer_size - written,
163 " %s+0x%p", symbol->Name, offset);
164 }
165 return true;
166}
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000167
168// ---------------------- AsanLock ---------------- {{{1
169enum LockState {
170 LOCK_UNINITIALIZED = 0,
171 LOCK_READY = -1,
172};
173
174AsanLock::AsanLock(LinkerInitialized li) {
175 // FIXME: see comments in AsanLock::Lock() for the details.
176 CHECK(li == LINKER_INITIALIZED || owner_ == LOCK_UNINITIALIZED);
177
178 CHECK(sizeof(CRITICAL_SECTION) <= sizeof(opaque_storage_));
179 InitializeCriticalSection((LPCRITICAL_SECTION)opaque_storage_);
180 owner_ = LOCK_READY;
181}
182
183void AsanLock::Lock() {
184 if (owner_ == LOCK_UNINITIALIZED) {
185 // FIXME: hm, global AsanLock objects are not initialized?!?
186 // This might be a side effect of the clang+cl+link Frankenbuild...
187 new(this) AsanLock((LinkerInitialized)(LINKER_INITIALIZED + 1));
188
189 // FIXME: If it turns out the linker doesn't invoke our
190 // constructors, we should probably manually Lock/Unlock all the global
191 // locks while we're starting in one thread to avoid double-init races.
192 }
193 EnterCriticalSection((LPCRITICAL_SECTION)opaque_storage_);
194 CHECK(owner_ == LOCK_READY);
195 owner_ = GetThreadSelf();
196}
197
198void AsanLock::Unlock() {
199 CHECK(owner_ == GetThreadSelf());
200 owner_ = LOCK_READY;
201 LeaveCriticalSection((LPCRITICAL_SECTION)opaque_storage_);
202}
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000203
204// ---------------------- TSD ---------------- {{{1
205static bool tsd_key_inited = false;
206
207// FIXME: is __declspec enough?
208static __declspec(thread) void *fake_tsd = NULL;
209
210void AsanTSDInit(void (*destructor)(void *tsd)) {
211 // FIXME: we're ignoring the destructor for now.
212 tsd_key_inited = true;
213}
214
215void *AsanTSDGet() {
216 CHECK(tsd_key_inited);
217 return fake_tsd;
218}
219
220void AsanTSDSet(void *tsd) {
221 CHECK(tsd_key_inited);
222 fake_tsd = tsd;
223}
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000224
225// ---------------------- Various stuff ---------------- {{{1
226void *AsanDoesNotSupportStaticLinkage() {
Timur Iskhodzhanov07bb9f12012-02-22 13:59:49 +0000227#if !defined(_DLL) || defined(_DEBUG)
228#error Please build the runtime with /MD
229#endif
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000230 return NULL;
231}
232
Alexander Potapenkof73a6a32012-02-13 17:09:40 +0000233bool AsanShadowRangeIsAvailable() {
234 // FIXME: shall we do anything here on Windows?
235 return true;
236}
237
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000238int AtomicInc(int *a) {
239 return InterlockedExchangeAdd((LONG*)a, 1) + 1;
240}
241
242const char* AsanGetEnv(const char* name) {
243 // FIXME: implement.
244 return NULL;
245}
246
Alexander Potapenko99d17eb2012-02-22 09:11:55 +0000247void AsanDumpProcessMap() {
248 UNIMPLEMENTED();
249}
250
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000251int GetPid() {
252 return GetProcessId(GetCurrentProcess());
253}
254
255uintptr_t GetThreadSelf() {
256 return GetCurrentThreadId();
257}
258
259void InstallSignalHandlers() {
260 // FIXME: Decide what to do on Windows.
261}
262
263void AsanDisableCoreDumper() {
264 UNIMPLEMENTED();
265}
266
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +0000267void SleepForSeconds(int seconds) {
268 Sleep(seconds * 1000);
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000269}
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +0000270
271void Exit(int exitcode) {
272 _exit(exitcode);
273}
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000274
Alexey Samsonovb823e3c2012-02-22 14:07:06 +0000275int Atexit(void (*function)(void)) {
276 return atexit(function);
277}
278
Kostya Serebryany25c71782012-03-10 01:30:01 +0000279void SortArray(uintptr_t *array, size_t size) {
280 std::sort(array, array + size);
281}
282
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000283} // namespace __asan
284
285#endif // _WIN32