1 //===-- sanitizer_win.cc --------------------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is shared between AddressSanitizer and ThreadSanitizer
9 // run-time libraries and implements windows-specific functions from
11 //===----------------------------------------------------------------------===//
13 #include "sanitizer_platform.h"
16 #define WIN32_LEAN_AND_MEAN
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
34 return 1U << 14; // FIXME: is this configurable?
37 uptr
GetMmapGranularity() {
38 return 1U << 16; // FIXME: is this configurable?
41 uptr
GetMaxVirtualAddress() {
44 return (uptr
)si
.lpMaximumApplicationAddress
;
47 bool FileExists(const char *filename
) {
51 uptr
internal_getpid() {
52 return GetProcessId(GetCurrentProcess());
55 // In contrast to POSIX, on Windows GetCurrentThreadId()
56 // returns a system-unique identifier.
58 return GetCurrentThreadId();
61 uptr
GetThreadSelf() {
65 void GetThreadStackTopAndBottom(bool at_initialization
, uptr
*stack_top
,
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
);
81 Report("ERROR: Failed to allocate 0x%zx (%zd) bytes of %s\n",
82 size
, size
, mem_type
);
83 CHECK("unable to mmap" && 0);
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",
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
);
102 Report("ERROR: Failed to allocate 0x%zx (%zd) bytes at %p (%d)\n",
103 size
, size
, fixed_addr
, GetLastError());
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?
126 void *MapFileToMemory(const char *file_name
, uptr
*buff_size
) {
130 static const int kMaxEnvNameLength
= 128;
131 static const DWORD kMaxEnvValueLength
= 32767;
136 char name
[kMaxEnvNameLength
];
137 char value
[kMaxEnvValueLength
];
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
,
156 if (rv
> 0 && rv
< kMaxEnvValueLength
) {
157 CHECK_LT(internal_strlen(name
), kMaxEnvNameLength
);
158 internal_strncpy(env_vars
[num_env_vars
].name
, name
, kMaxEnvNameLength
);
160 return env_vars
[num_env_vars
- 1].value
;
165 const char *GetPwd() {
173 void DumpProcessMap() {
177 void DisableCoreDumper() {
185 void PrepareForSandboxing() {
186 // Nothing here for now.
189 bool StackSizeIsUnlimited() {
193 void SetStackSizeLimitInBytes(uptr limit
) {
197 char *FindPathToBinary(const char *name
) {
198 // Nothing here for now.
202 void SleepForSeconds(int seconds
) {
203 Sleep(seconds
* 1000);
206 void SleepForMillis(int millis
) {
216 internal__exit(-1); // abort is not NORETURN on Windows.
219 uptr
GetListOfModules(LoadedModule
*modules
, uptr max_modules
,
220 string_predicate_t filter
) {
225 int Atexit(void (*function
)(void)) {
226 return atexit(function
);
230 // ------------------ sanitizer_libc.h
231 uptr
internal_mmap(void *addr
, uptr length
, int prot
, int flags
,
232 int fd
, u64 offset
) {
236 uptr
internal_munmap(void *addr
, uptr length
) {
240 uptr
internal_close(fd_t fd
) {
244 int internal_isatty(fd_t fd
) {
248 uptr
internal_open(const char *filename
, int flags
) {
252 uptr
internal_open(const char *filename
, int flags
, u32 mode
) {
256 uptr
OpenFile(const char *filename
, bool write
) {
260 uptr
internal_read(fd_t fd
, void *buf
, uptr count
) {
264 uptr
internal_write(fd_t fd
, const void *buf
, uptr count
) {
267 HANDLE err
= GetStdHandle(STD_ERROR_HANDLE
);
269 return 0; // FIXME: this might not work on some apps.
271 if (!WriteFile(err
, buf
, count
, &ret
, 0))
276 uptr
internal_stat(const char *path
, void *buf
) {
280 uptr
internal_lstat(const char *path
, void *buf
) {
284 uptr
internal_fstat(fd_t fd
, void *buf
) {
288 uptr
internal_filesize(fd_t fd
) {
292 uptr
internal_dup2(int oldfd
, int newfd
) {
296 uptr
internal_readlink(const char *path
, char *buf
, uptr bufsize
) {
300 uptr
internal_sched_yield() {
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_
);
322 BlockingMutex::BlockingMutex() {
323 CHECK(sizeof(CRITICAL_SECTION
) <= sizeof(opaque_storage_
));
324 InitializeCriticalSection((LPCRITICAL_SECTION
)opaque_storage_
);
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());
346 LeaveCriticalSection((LPCRITICAL_SECTION
)opaque_storage_
);
349 void BlockingMutex::CheckLocked() {
350 CHECK_EQ(owner_
, GetThreadSelf());
360 void GetThreadStackAndTls(bool main
, uptr
*stk_addr
, uptr
*stk_size
,
361 uptr
*tls_addr
, uptr
*tls_size
) {
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
;
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
),
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
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