blob: eb33882a522e9cd63859671ebafcdd6cc5e30c99 [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"
25#include "asan_procmaps.h"
26#include "asan_thread.h"
27
Kostya Serebryany25c71782012-03-10 01:30:01 +000028// Should not add dependency on libstdc++,
29// since most of the stuff here is inlinable.
30#include <algorithm>
31
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +000032namespace __asan {
33
34// ---------------------- Memory management ---------------- {{{1
Kostya Serebryany45c8d1b2012-05-31 16:06:05 +000035void *AsanMmapFixedNoReserve(uptr fixed_addr, uptr size) {
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +000036 return VirtualAlloc((LPVOID)fixed_addr, size,
37 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
38}
39
Kostya Serebryany45c8d1b2012-05-31 16:06:05 +000040void *AsanMmapSomewhereOrDie(uptr size, const char *mem_type) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000041 void *rv = VirtualAlloc(0, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
42 if (rv == 0)
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +000043 OutOfMemoryMessageAndDie(mem_type, size);
44 return rv;
45}
46
Kostya Serebryany45c8d1b2012-05-31 16:06:05 +000047void *AsanMprotect(uptr fixed_addr, uptr size) {
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +000048 return VirtualAlloc((LPVOID)fixed_addr, size,
49 MEM_RESERVE | MEM_COMMIT, PAGE_NOACCESS);
50}
51
Kostya Serebryany45c8d1b2012-05-31 16:06:05 +000052void AsanUnmapOrDie(void *addr, uptr size) {
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +000053 CHECK(VirtualFree(addr, size, MEM_DECOMMIT));
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +000054}
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +000055
56// ---------------------- IO ---------------- {{{1
Kostya Serebryany45c8d1b2012-05-31 16:06:05 +000057uptr AsanWrite(int fd, const void *buf, uptr count) {
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +000058 if (fd != 2)
59 UNIMPLEMENTED();
60
Timur Iskhodzhanov27a78002012-03-11 12:45:12 +000061 HANDLE err = GetStdHandle(STD_ERROR_HANDLE);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000062 if (err == 0)
Timur Iskhodzhanov27a78002012-03-11 12:45:12 +000063 return 0; // FIXME: this might not work on some apps.
64 DWORD ret;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000065 if (!WriteFile(err, buf, count, &ret, 0))
Timur Iskhodzhanov27a78002012-03-11 12:45:12 +000066 return 0;
67 return ret;
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +000068}
69
70// FIXME: Looks like these functions are not needed and are linked in by the
71// code unreachable on Windows. We should clean this up.
72int AsanOpenReadonly(const char* filename) {
73 UNIMPLEMENTED();
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +000074}
75
Kostya Serebryany45c8d1b2012-05-31 16:06:05 +000076uptr AsanRead(int fd, void *buf, uptr count) {
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +000077 UNIMPLEMENTED();
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +000078}
79
80int AsanClose(int fd) {
81 UNIMPLEMENTED();
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +000082}
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +000083
84// ---------------------- Stacktraces, symbols, etc. ---------------- {{{1
85static AsanLock dbghelp_lock(LINKER_INITIALIZED);
86static bool dbghelp_initialized = false;
87#pragma comment(lib, "dbghelp.lib")
88
89void AsanThread::SetThreadStackTopAndBottom() {
90 MEMORY_BASIC_INFORMATION mbi;
91 CHECK(VirtualQuery(&mbi /* on stack */,
92 &mbi, sizeof(mbi)) != 0);
93 // FIXME: is it possible for the stack to not be a single allocation?
94 // Are these values what ASan expects to get (reserved, not committed;
95 // including stack guard page) ?
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000096 stack_top_ = (uptr)mbi.BaseAddress + mbi.RegionSize;
97 stack_bottom_ = (uptr)mbi.AllocationBase;
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +000098}
99
Kostya Serebryany45c8d1b2012-05-31 16:06:05 +0000100void AsanStackTrace::GetStackTrace(uptr max_s, uptr pc, uptr bp) {
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000101 max_size = max_s;
102 void *tmp[kStackTraceMax];
103
104 // FIXME: CaptureStackBackTrace might be too slow for us.
105 // FIXME: Compare with StackWalk64.
106 // FIXME: Look at LLVMUnhandledExceptionFilter in Signals.inc
Kostya Serebryany45c8d1b2012-05-31 16:06:05 +0000107 uptr cs_ret = CaptureStackBackTrace(1, max_size, tmp, 0),
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000108 offset = 0;
109 // Skip the RTL frames by searching for the PC in the stacktrace.
110 // FIXME: this doesn't work well for the malloc/free stacks yet.
Kostya Serebryany45c8d1b2012-05-31 16:06:05 +0000111 for (uptr i = 0; i < cs_ret; i++) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000112 if (pc != (uptr)tmp[i])
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000113 continue;
114 offset = i;
115 break;
116 }
117
118 size = cs_ret - offset;
Kostya Serebryany45c8d1b2012-05-31 16:06:05 +0000119 for (uptr i = 0; i < size; i++)
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000120 trace[i] = (uptr)tmp[i + offset];
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000121}
122
Timur Iskhodzhanov8c505ef2012-05-21 14:25:36 +0000123bool __asan_WinSymbolize(const void *addr, char *out_buffer, int buffer_size) {
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000124 ScopedLock lock(&dbghelp_lock);
125 if (!dbghelp_initialized) {
126 SymSetOptions(SYMOPT_DEFERRED_LOADS |
127 SYMOPT_UNDNAME |
128 SYMOPT_LOAD_LINES);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000129 CHECK(SymInitialize(GetCurrentProcess(), 0, TRUE));
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000130 // FIXME: We don't call SymCleanup() on exit yet - should we?
131 dbghelp_initialized = true;
132 }
133
134 // See http://msdn.microsoft.com/en-us/library/ms680578(VS.85).aspx
135 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(CHAR)];
136 PSYMBOL_INFO symbol = (PSYMBOL_INFO)buffer;
137 symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
138 symbol->MaxNameLen = MAX_SYM_NAME;
139 DWORD64 offset = 0;
140 BOOL got_objname = SymFromAddr(GetCurrentProcess(),
141 (DWORD64)addr, &offset, symbol);
142 if (!got_objname)
143 return false;
144
145 DWORD unused;
146 IMAGEHLP_LINE64 info;
147 info.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
148 BOOL got_fileline = SymGetLineFromAddr64(GetCurrentProcess(),
149 (DWORD64)addr, &unused, &info);
150 int written = 0;
151 out_buffer[0] = '\0';
152 // FIXME: it might be useful to print out 'obj' or 'obj+offset' info too.
153 if (got_fileline) {
154 written += SNPrintf(out_buffer + written, buffer_size - written,
155 " %s %s:%d", symbol->Name,
156 info.FileName, info.LineNumber);
157 } else {
158 written += SNPrintf(out_buffer + written, buffer_size - written,
159 " %s+0x%p", symbol->Name, offset);
160 }
161 return true;
162}
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000163
164// ---------------------- AsanLock ---------------- {{{1
165enum LockState {
166 LOCK_UNINITIALIZED = 0,
167 LOCK_READY = -1,
168};
169
170AsanLock::AsanLock(LinkerInitialized li) {
171 // FIXME: see comments in AsanLock::Lock() for the details.
172 CHECK(li == LINKER_INITIALIZED || owner_ == LOCK_UNINITIALIZED);
173
174 CHECK(sizeof(CRITICAL_SECTION) <= sizeof(opaque_storage_));
175 InitializeCriticalSection((LPCRITICAL_SECTION)opaque_storage_);
176 owner_ = LOCK_READY;
177}
178
179void AsanLock::Lock() {
180 if (owner_ == LOCK_UNINITIALIZED) {
181 // FIXME: hm, global AsanLock objects are not initialized?!?
182 // This might be a side effect of the clang+cl+link Frankenbuild...
183 new(this) AsanLock((LinkerInitialized)(LINKER_INITIALIZED + 1));
184
185 // FIXME: If it turns out the linker doesn't invoke our
186 // constructors, we should probably manually Lock/Unlock all the global
187 // locks while we're starting in one thread to avoid double-init races.
188 }
189 EnterCriticalSection((LPCRITICAL_SECTION)opaque_storage_);
190 CHECK(owner_ == LOCK_READY);
191 owner_ = GetThreadSelf();
192}
193
194void AsanLock::Unlock() {
195 CHECK(owner_ == GetThreadSelf());
196 owner_ = LOCK_READY;
197 LeaveCriticalSection((LPCRITICAL_SECTION)opaque_storage_);
198}
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000199
200// ---------------------- TSD ---------------- {{{1
201static bool tsd_key_inited = false;
202
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000203static __declspec(thread) void *fake_tsd = 0;
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000204
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 Iskhodzhanov2716a612012-03-12 11:45:09 +0000222#if defined(_DEBUG)
223#error Please build the runtime with a non-debug CRT: /MD or /MT
Timur Iskhodzhanov07bb9f12012-02-22 13:59:49 +0000224#endif
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000225 return 0;
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000226}
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
Kostya Serebryanyee392552012-05-31 15:02:07 +0000237u16 AtomicExchange(u16 *a, u16 new_val) {
Timur Iskhodzhanov8a5d8c42012-04-05 17:16:32 +0000238 // InterlockedExchange16 seems unavailable on some MSVS installations.
239 // Everybody stand back, I pretend to know inline assembly!
240 // FIXME: I assume VC is smart enough to save/restore eax/ecx?
241 __asm {
242 mov eax, a
243 mov cx, new_val
Timur Iskhodzhanov00152bf2012-04-05 18:31:50 +0000244 xchg [eax], cx ; NOLINT
Timur Iskhodzhanov8a5d8c42012-04-05 17:16:32 +0000245 mov new_val, cx
246 }
247 return new_val;
Kostya Serebryanyf1e82b82012-04-05 15:55:09 +0000248}
249
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000250const char* AsanGetEnv(const char* name) {
Timur Iskhodzhanov23bd2bb2012-03-13 16:12:03 +0000251 static char env_buffer[32767] = {};
252
253 // Note: this implementation stores the result in a static buffer so we only
254 // allow it to be called just once.
255 static bool called_once = false;
256 if (called_once)
257 UNIMPLEMENTED();
258 called_once = true;
259
260 DWORD rv = GetEnvironmentVariableA(name, env_buffer, sizeof(env_buffer));
261 if (rv > 0 && rv < sizeof(env_buffer))
262 return env_buffer;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000263 return 0;
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000264}
265
Alexander Potapenko99d17eb2012-02-22 09:11:55 +0000266void AsanDumpProcessMap() {
267 UNIMPLEMENTED();
268}
269
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000270int GetPid() {
271 return GetProcessId(GetCurrentProcess());
272}
273
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000274uptr GetThreadSelf() {
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000275 return GetCurrentThreadId();
276}
277
Alexander Potapenkof03d8af2012-04-05 10:54:52 +0000278void SetAlternateSignalStack() {
279 // FIXME: Decide what to do on Windows.
280}
281
282void UnsetAlternateSignalStack() {
283 // FIXME: Decide what to do on Windows.
284}
285
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000286void InstallSignalHandlers() {
287 // FIXME: Decide what to do on Windows.
288}
289
290void AsanDisableCoreDumper() {
291 UNIMPLEMENTED();
292}
293
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +0000294void SleepForSeconds(int seconds) {
295 Sleep(seconds * 1000);
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000296}
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +0000297
298void Exit(int exitcode) {
299 _exit(exitcode);
300}
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000301
Kostya Serebryanyf8e6fee2012-04-06 01:27:11 +0000302void Abort() {
303 abort();
Timur Iskhodzhanov8c505ef2012-05-21 14:25:36 +0000304 _exit(-1); // abort is not NORETURN on Windows.
Kostya Serebryanyf8e6fee2012-04-06 01:27:11 +0000305}
306
Alexey Samsonovb823e3c2012-02-22 14:07:06 +0000307int Atexit(void (*function)(void)) {
308 return atexit(function);
309}
310
Kostya Serebryany45c8d1b2012-05-31 16:06:05 +0000311void SortArray(uptr *array, uptr size) {
Kostya Serebryany25c71782012-03-10 01:30:01 +0000312 std::sort(array, array + size);
313}
314
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000315} // namespace __asan
316
317#endif // _WIN32