blob: 5df110330b633f538154cf1c20e5b5343d302697 [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
62 // FIXME: use WriteFile instead?
63 return fwrite(buf, 1, count, stderr);
64}
65
66// FIXME: Looks like these functions are not needed and are linked in by the
67// code unreachable on Windows. We should clean this up.
68int AsanOpenReadonly(const char* filename) {
69 UNIMPLEMENTED();
70 return -1;
71}
72
73size_t AsanRead(int fd, void *buf, size_t count) {
74 UNIMPLEMENTED();
75 return -1;
76}
77
78int AsanClose(int fd) {
79 UNIMPLEMENTED();
80 return -1;
81}
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +000082
83// ---------------------- Stacktraces, symbols, etc. ---------------- {{{1
84static AsanLock dbghelp_lock(LINKER_INITIALIZED);
85static bool dbghelp_initialized = false;
86#pragma comment(lib, "dbghelp.lib")
87
88void AsanThread::SetThreadStackTopAndBottom() {
89 MEMORY_BASIC_INFORMATION mbi;
90 CHECK(VirtualQuery(&mbi /* on stack */,
91 &mbi, sizeof(mbi)) != 0);
92 // FIXME: is it possible for the stack to not be a single allocation?
93 // Are these values what ASan expects to get (reserved, not committed;
94 // including stack guard page) ?
95 stack_top_ = (uintptr_t)mbi.BaseAddress + mbi.RegionSize;
96 stack_bottom_ = (uintptr_t)mbi.AllocationBase;
97}
98
99void AsanStackTrace::GetStackTrace(size_t max_s, uintptr_t pc, uintptr_t bp) {
100 max_size = max_s;
101 void *tmp[kStackTraceMax];
102
103 // FIXME: CaptureStackBackTrace might be too slow for us.
104 // FIXME: Compare with StackWalk64.
105 // FIXME: Look at LLVMUnhandledExceptionFilter in Signals.inc
106 size_t cs_ret = CaptureStackBackTrace(1, max_size, tmp, NULL),
107 offset = 0;
108 // Skip the RTL frames by searching for the PC in the stacktrace.
109 // FIXME: this doesn't work well for the malloc/free stacks yet.
110 for (size_t i = 0; i < cs_ret; i++) {
111 if (pc != (uintptr_t)tmp[i])
112 continue;
113 offset = i;
114 break;
115 }
116
117 size = cs_ret - offset;
118 for (size_t i = 0; i < size; i++)
119 trace[i] = (uintptr_t)tmp[i + offset];
120}
121
122bool WinSymbolize(const void *addr, char *out_buffer, int buffer_size) {
123 ScopedLock lock(&dbghelp_lock);
124 if (!dbghelp_initialized) {
125 SymSetOptions(SYMOPT_DEFERRED_LOADS |
126 SYMOPT_UNDNAME |
127 SYMOPT_LOAD_LINES);
128 CHECK(SymInitialize(GetCurrentProcess(), NULL, TRUE));
129 // FIXME: We don't call SymCleanup() on exit yet - should we?
130 dbghelp_initialized = true;
131 }
132
133 // See http://msdn.microsoft.com/en-us/library/ms680578(VS.85).aspx
134 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(CHAR)];
135 PSYMBOL_INFO symbol = (PSYMBOL_INFO)buffer;
136 symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
137 symbol->MaxNameLen = MAX_SYM_NAME;
138 DWORD64 offset = 0;
139 BOOL got_objname = SymFromAddr(GetCurrentProcess(),
140 (DWORD64)addr, &offset, symbol);
141 if (!got_objname)
142 return false;
143
144 DWORD unused;
145 IMAGEHLP_LINE64 info;
146 info.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
147 BOOL got_fileline = SymGetLineFromAddr64(GetCurrentProcess(),
148 (DWORD64)addr, &unused, &info);
149 int written = 0;
150 out_buffer[0] = '\0';
151 // FIXME: it might be useful to print out 'obj' or 'obj+offset' info too.
152 if (got_fileline) {
153 written += SNPrintf(out_buffer + written, buffer_size - written,
154 " %s %s:%d", symbol->Name,
155 info.FileName, info.LineNumber);
156 } else {
157 written += SNPrintf(out_buffer + written, buffer_size - written,
158 " %s+0x%p", symbol->Name, offset);
159 }
160 return true;
161}
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000162
163// ---------------------- AsanLock ---------------- {{{1
164enum LockState {
165 LOCK_UNINITIALIZED = 0,
166 LOCK_READY = -1,
167};
168
169AsanLock::AsanLock(LinkerInitialized li) {
170 // FIXME: see comments in AsanLock::Lock() for the details.
171 CHECK(li == LINKER_INITIALIZED || owner_ == LOCK_UNINITIALIZED);
172
173 CHECK(sizeof(CRITICAL_SECTION) <= sizeof(opaque_storage_));
174 InitializeCriticalSection((LPCRITICAL_SECTION)opaque_storage_);
175 owner_ = LOCK_READY;
176}
177
178void AsanLock::Lock() {
179 if (owner_ == LOCK_UNINITIALIZED) {
180 // FIXME: hm, global AsanLock objects are not initialized?!?
181 // This might be a side effect of the clang+cl+link Frankenbuild...
182 new(this) AsanLock((LinkerInitialized)(LINKER_INITIALIZED + 1));
183
184 // FIXME: If it turns out the linker doesn't invoke our
185 // constructors, we should probably manually Lock/Unlock all the global
186 // locks while we're starting in one thread to avoid double-init races.
187 }
188 EnterCriticalSection((LPCRITICAL_SECTION)opaque_storage_);
189 CHECK(owner_ == LOCK_READY);
190 owner_ = GetThreadSelf();
191}
192
193void AsanLock::Unlock() {
194 CHECK(owner_ == GetThreadSelf());
195 owner_ = LOCK_READY;
196 LeaveCriticalSection((LPCRITICAL_SECTION)opaque_storage_);
197}
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000198
199// ---------------------- TSD ---------------- {{{1
200static bool tsd_key_inited = false;
201
202// FIXME: is __declspec enough?
203static __declspec(thread) void *fake_tsd = NULL;
204
205void AsanTSDInit(void (*destructor)(void *tsd)) {
206 // FIXME: we're ignoring the destructor for now.
207 tsd_key_inited = true;
208}
209
210void *AsanTSDGet() {
211 CHECK(tsd_key_inited);
212 return fake_tsd;
213}
214
215void AsanTSDSet(void *tsd) {
216 CHECK(tsd_key_inited);
217 fake_tsd = tsd;
218}
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000219
220// ---------------------- Various stuff ---------------- {{{1
221void *AsanDoesNotSupportStaticLinkage() {
Timur Iskhodzhanov07bb9f12012-02-22 13:59:49 +0000222#if !defined(_DLL) || defined(_DEBUG)
223#error Please build the runtime with /MD
224#endif
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000225 return NULL;
226}
227
Alexander Potapenkof73a6a32012-02-13 17:09:40 +0000228bool AsanShadowRangeIsAvailable() {
229 // FIXME: shall we do anything here on Windows?
230 return true;
231}
232
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000233int AtomicInc(int *a) {
234 return InterlockedExchangeAdd((LONG*)a, 1) + 1;
235}
236
237const char* AsanGetEnv(const char* name) {
238 // FIXME: implement.
239 return NULL;
240}
241
Alexander Potapenko99d17eb2012-02-22 09:11:55 +0000242void AsanDumpProcessMap() {
243 UNIMPLEMENTED();
244}
245
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000246int GetPid() {
247 return GetProcessId(GetCurrentProcess());
248}
249
250uintptr_t GetThreadSelf() {
251 return GetCurrentThreadId();
252}
253
254void InstallSignalHandlers() {
255 // FIXME: Decide what to do on Windows.
256}
257
258void AsanDisableCoreDumper() {
259 UNIMPLEMENTED();
260}
261
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +0000262void SleepForSeconds(int seconds) {
263 Sleep(seconds * 1000);
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000264}
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +0000265
266void Exit(int exitcode) {
267 _exit(exitcode);
268}
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000269
Alexey Samsonovb823e3c2012-02-22 14:07:06 +0000270int Atexit(void (*function)(void)) {
271 return atexit(function);
272}
273
Kostya Serebryany25c71782012-03-10 01:30:01 +0000274void SortArray(uintptr_t *array, size_t size) {
275 std::sort(array, array + size);
276}
277
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000278} // namespace __asan
279
280#endif // _WIN32