* include/bits/allocator.h (operator==, operator!=): Add exception
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_win.cc
blobc48274e36426786e22e4485ac9e4d4f77379ab67
1 //===-- sanitizer_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 shared between AddressSanitizer and ThreadSanitizer
9 // run-time libraries and implements windows-specific functions from
10 // sanitizer_libc.h.
11 //===----------------------------------------------------------------------===//
13 #include "sanitizer_platform.h"
14 #if SANITIZER_WINDOWS
16 #define WIN32_LEAN_AND_MEAN
17 #define NOGDI
18 #include <stdlib.h>
19 #include <io.h>
20 #include <windows.h>
22 #include "sanitizer_common.h"
23 #include "sanitizer_libc.h"
24 #include "sanitizer_mutex.h"
25 #include "sanitizer_placement_new.h"
26 #include "sanitizer_stacktrace.h"
28 namespace __sanitizer {
30 #include "sanitizer_syscall_generic.inc"
32 // --------------------- sanitizer_common.h
33 uptr GetPageSize() {
34 return 1U << 14; // FIXME: is this configurable?
37 uptr GetMmapGranularity() {
38 return 1U << 16; // FIXME: is this configurable?
41 uptr GetMaxVirtualAddress() {
42 SYSTEM_INFO si;
43 GetSystemInfo(&si);
44 return (uptr)si.lpMaximumApplicationAddress;
47 bool FileExists(const char *filename) {
48 UNIMPLEMENTED();
51 uptr internal_getpid() {
52 return GetProcessId(GetCurrentProcess());
55 // In contrast to POSIX, on Windows GetCurrentThreadId()
56 // returns a system-unique identifier.
57 uptr GetTid() {
58 return GetCurrentThreadId();
61 uptr GetThreadSelf() {
62 return GetTid();
65 void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
66 uptr *stack_bottom) {
67 CHECK(stack_top);
68 CHECK(stack_bottom);
69 MEMORY_BASIC_INFORMATION mbi;
70 CHECK_NE(VirtualQuery(&mbi /* on stack */, &mbi, sizeof(mbi)), 0);
71 // FIXME: is it possible for the stack to not be a single allocation?
72 // Are these values what ASan expects to get (reserved, not committed;
73 // including stack guard page) ?
74 *stack_top = (uptr)mbi.BaseAddress + mbi.RegionSize;
75 *stack_bottom = (uptr)mbi.AllocationBase;
78 void *MmapOrDie(uptr size, const char *mem_type) {
79 void *rv = VirtualAlloc(0, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
80 if (rv == 0) {
81 Report("ERROR: Failed to allocate 0x%zx (%zd) bytes of %s\n",
82 size, size, mem_type);
83 CHECK("unable to mmap" && 0);
85 return rv;
88 void UnmapOrDie(void *addr, uptr size) {
89 if (VirtualFree(addr, size, MEM_DECOMMIT) == 0) {
90 Report("ERROR: Failed to deallocate 0x%zx (%zd) bytes at address %p\n",
91 size, size, addr);
92 CHECK("unable to unmap" && 0);
96 void *MmapFixedNoReserve(uptr fixed_addr, uptr size) {
97 // FIXME: is this really "NoReserve"? On Win32 this does not matter much,
98 // but on Win64 it does.
99 void *p = VirtualAlloc((LPVOID)fixed_addr, size,
100 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
101 if (p == 0)
102 Report("ERROR: Failed to allocate 0x%zx (%zd) bytes at %p (%d)\n",
103 size, size, fixed_addr, GetLastError());
104 return p;
107 void *MmapFixedOrDie(uptr fixed_addr, uptr size) {
108 return MmapFixedNoReserve(fixed_addr, size);
111 void *Mprotect(uptr fixed_addr, uptr size) {
112 return VirtualAlloc((LPVOID)fixed_addr, size,
113 MEM_RESERVE | MEM_COMMIT, PAGE_NOACCESS);
116 void FlushUnneededShadowMemory(uptr addr, uptr size) {
117 // This is almost useless on 32-bits.
118 // FIXME: add madvice-analog when we move to 64-bits.
121 bool MemoryRangeIsAvailable(uptr range_start, uptr range_end) {
122 // FIXME: shall we do anything here on Windows?
123 return true;
126 void *MapFileToMemory(const char *file_name, uptr *buff_size) {
127 UNIMPLEMENTED();
130 static const int kMaxEnvNameLength = 128;
131 static const DWORD kMaxEnvValueLength = 32767;
133 namespace {
135 struct EnvVariable {
136 char name[kMaxEnvNameLength];
137 char value[kMaxEnvValueLength];
140 } // namespace
142 static const int kEnvVariables = 5;
143 static EnvVariable env_vars[kEnvVariables];
144 static int num_env_vars;
146 const char *GetEnv(const char *name) {
147 // Note: this implementation caches the values of the environment variables
148 // and limits their quantity.
149 for (int i = 0; i < num_env_vars; i++) {
150 if (0 == internal_strcmp(name, env_vars[i].name))
151 return env_vars[i].value;
153 CHECK_LT(num_env_vars, kEnvVariables);
154 DWORD rv = GetEnvironmentVariableA(name, env_vars[num_env_vars].value,
155 kMaxEnvValueLength);
156 if (rv > 0 && rv < kMaxEnvValueLength) {
157 CHECK_LT(internal_strlen(name), kMaxEnvNameLength);
158 internal_strncpy(env_vars[num_env_vars].name, name, kMaxEnvNameLength);
159 num_env_vars++;
160 return env_vars[num_env_vars - 1].value;
162 return 0;
165 const char *GetPwd() {
166 UNIMPLEMENTED();
169 u32 GetUid() {
170 UNIMPLEMENTED();
173 void DumpProcessMap() {
174 UNIMPLEMENTED();
177 void DisableCoreDumper() {
178 UNIMPLEMENTED();
181 void ReExec() {
182 UNIMPLEMENTED();
185 void PrepareForSandboxing() {
186 // Nothing here for now.
189 bool StackSizeIsUnlimited() {
190 UNIMPLEMENTED();
193 void SetStackSizeLimitInBytes(uptr limit) {
194 UNIMPLEMENTED();
197 char *FindPathToBinary(const char *name) {
198 // Nothing here for now.
199 return 0;
202 void SleepForSeconds(int seconds) {
203 Sleep(seconds * 1000);
206 void SleepForMillis(int millis) {
207 Sleep(millis);
210 u64 NanoTime() {
211 return 0;
214 void Abort() {
215 abort();
216 internal__exit(-1); // abort is not NORETURN on Windows.
219 uptr GetListOfModules(LoadedModule *modules, uptr max_modules,
220 string_predicate_t filter) {
221 UNIMPLEMENTED();
224 #ifndef SANITIZER_GO
225 int Atexit(void (*function)(void)) {
226 return atexit(function);
228 #endif
230 // ------------------ sanitizer_libc.h
231 uptr internal_mmap(void *addr, uptr length, int prot, int flags,
232 int fd, u64 offset) {
233 UNIMPLEMENTED();
236 uptr internal_munmap(void *addr, uptr length) {
237 UNIMPLEMENTED();
240 uptr internal_close(fd_t fd) {
241 UNIMPLEMENTED();
244 int internal_isatty(fd_t fd) {
245 return _isatty(fd);
248 uptr internal_open(const char *filename, int flags) {
249 UNIMPLEMENTED();
252 uptr internal_open(const char *filename, int flags, u32 mode) {
253 UNIMPLEMENTED();
256 uptr OpenFile(const char *filename, bool write) {
257 UNIMPLEMENTED();
260 uptr internal_read(fd_t fd, void *buf, uptr count) {
261 UNIMPLEMENTED();
264 uptr internal_write(fd_t fd, const void *buf, uptr count) {
265 if (fd != kStderrFd)
266 UNIMPLEMENTED();
267 HANDLE err = GetStdHandle(STD_ERROR_HANDLE);
268 if (err == 0)
269 return 0; // FIXME: this might not work on some apps.
270 DWORD ret;
271 if (!WriteFile(err, buf, count, &ret, 0))
272 return 0;
273 return ret;
276 uptr internal_stat(const char *path, void *buf) {
277 UNIMPLEMENTED();
280 uptr internal_lstat(const char *path, void *buf) {
281 UNIMPLEMENTED();
284 uptr internal_fstat(fd_t fd, void *buf) {
285 UNIMPLEMENTED();
288 uptr internal_filesize(fd_t fd) {
289 UNIMPLEMENTED();
292 uptr internal_dup2(int oldfd, int newfd) {
293 UNIMPLEMENTED();
296 uptr internal_readlink(const char *path, char *buf, uptr bufsize) {
297 UNIMPLEMENTED();
300 uptr internal_sched_yield() {
301 Sleep(0);
302 return 0;
305 void internal__exit(int exitcode) {
306 ExitProcess(exitcode);
309 // ---------------------- BlockingMutex ---------------- {{{1
310 const uptr LOCK_UNINITIALIZED = 0;
311 const uptr LOCK_READY = (uptr)-1;
313 BlockingMutex::BlockingMutex(LinkerInitialized li) {
314 // FIXME: see comments in BlockingMutex::Lock() for the details.
315 CHECK(li == LINKER_INITIALIZED || owner_ == LOCK_UNINITIALIZED);
317 CHECK(sizeof(CRITICAL_SECTION) <= sizeof(opaque_storage_));
318 InitializeCriticalSection((LPCRITICAL_SECTION)opaque_storage_);
319 owner_ = LOCK_READY;
322 BlockingMutex::BlockingMutex() {
323 CHECK(sizeof(CRITICAL_SECTION) <= sizeof(opaque_storage_));
324 InitializeCriticalSection((LPCRITICAL_SECTION)opaque_storage_);
325 owner_ = LOCK_READY;
328 void BlockingMutex::Lock() {
329 if (owner_ == LOCK_UNINITIALIZED) {
330 // FIXME: hm, global BlockingMutex objects are not initialized?!?
331 // This might be a side effect of the clang+cl+link Frankenbuild...
332 new(this) BlockingMutex((LinkerInitialized)(LINKER_INITIALIZED + 1));
334 // FIXME: If it turns out the linker doesn't invoke our
335 // constructors, we should probably manually Lock/Unlock all the global
336 // locks while we're starting in one thread to avoid double-init races.
338 EnterCriticalSection((LPCRITICAL_SECTION)opaque_storage_);
339 CHECK_EQ(owner_, LOCK_READY);
340 owner_ = GetThreadSelf();
343 void BlockingMutex::Unlock() {
344 CHECK_EQ(owner_, GetThreadSelf());
345 owner_ = LOCK_READY;
346 LeaveCriticalSection((LPCRITICAL_SECTION)opaque_storage_);
349 void BlockingMutex::CheckLocked() {
350 CHECK_EQ(owner_, GetThreadSelf());
353 uptr GetTlsSize() {
354 return 0;
357 void InitTlsSize() {
360 void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size,
361 uptr *tls_addr, uptr *tls_size) {
362 #ifdef SANITIZER_GO
363 *stk_addr = 0;
364 *stk_size = 0;
365 *tls_addr = 0;
366 *tls_size = 0;
367 #else
368 uptr stack_top, stack_bottom;
369 GetThreadStackTopAndBottom(main, &stack_top, &stack_bottom);
370 *stk_addr = stack_bottom;
371 *stk_size = stack_top - stack_bottom;
372 *tls_addr = 0;
373 *tls_size = 0;
374 #endif
377 void StackTrace::SlowUnwindStack(uptr pc, uptr max_depth) {
378 // FIXME: CaptureStackBackTrace might be too slow for us.
379 // FIXME: Compare with StackWalk64.
380 // FIXME: Look at LLVMUnhandledExceptionFilter in Signals.inc
381 size = CaptureStackBackTrace(2, Min(max_depth, kStackTraceMax),
382 (void**)trace, 0);
383 // Skip the RTL frames by searching for the PC in the stacktrace.
384 uptr pc_location = LocatePcInTrace(pc);
385 PopStackFrames(pc_location);
388 void MaybeOpenReportFile() {
389 // Windows doesn't have native fork, and we don't support Cygwin or other
390 // environments that try to fake it, so the initial report_fd will always be
391 // correct.
394 void RawWrite(const char *buffer) {
395 static const char *kRawWriteError =
396 "RawWrite can't output requested buffer!\n";
397 uptr length = (uptr)internal_strlen(buffer);
398 if (length != internal_write(report_fd, buffer, length)) {
399 // stderr may be closed, but we may be able to print to the debugger
400 // instead. This is the case when launching a program from Visual Studio,
401 // and the following routine should write to its console.
402 OutputDebugStringA(buffer);
406 } // namespace __sanitizer
408 #endif // _WIN32