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 //===----------------------------------------------------------------------===//
15 #include "sanitizer_common.h"
16 #include "sanitizer_libc.h"
18 namespace __sanitizer
{
20 // --------------------- sanitizer_common.h
22 return GetProcessId(GetCurrentProcess());
25 uptr
GetThreadSelf() {
26 return GetCurrentThreadId();
29 void GetThreadStackTopAndBottom(bool at_initialization
, uptr
*stack_top
,
33 MEMORY_BASIC_INFORMATION mbi
;
34 CHECK_NE(VirtualQuery(&mbi
/* on stack */, &mbi
, sizeof(mbi
)), 0);
35 // FIXME: is it possible for the stack to not be a single allocation?
36 // Are these values what ASan expects to get (reserved, not committed;
37 // including stack guard page) ?
38 *stack_top
= (uptr
)mbi
.BaseAddress
+ mbi
.RegionSize
;
39 *stack_bottom
= (uptr
)mbi
.AllocationBase
;
43 void *MmapOrDie(uptr size
, const char *mem_type
) {
44 void *rv
= VirtualAlloc(0, size
, MEM_RESERVE
| MEM_COMMIT
, PAGE_READWRITE
);
46 Report("ERROR: Failed to allocate 0x%zx (%zd) bytes of %s\n",
47 size
, size
, mem_type
);
48 CHECK("unable to mmap" && 0);
53 void UnmapOrDie(void *addr
, uptr size
) {
54 if (VirtualFree(addr
, size
, MEM_DECOMMIT
) == 0) {
55 Report("ERROR: Failed to deallocate 0x%zx (%zd) bytes at address %p\n",
57 CHECK("unable to unmap" && 0);
61 void *MmapFixedNoReserve(uptr fixed_addr
, uptr size
) {
62 return VirtualAlloc((LPVOID
)fixed_addr
, size
,
63 MEM_RESERVE
| MEM_COMMIT
, PAGE_READWRITE
);
66 void *Mprotect(uptr fixed_addr
, uptr size
) {
67 return VirtualAlloc((LPVOID
)fixed_addr
, size
,
68 MEM_RESERVE
| MEM_COMMIT
, PAGE_NOACCESS
);
71 bool MemoryRangeIsAvailable(uptr range_start
, uptr range_end
) {
72 // FIXME: shall we do anything here on Windows?
76 void *MapFileToMemory(const char *file_name
, uptr
*buff_size
) {
81 const char *GetEnv(const char *name
) {
82 static char env_buffer
[32767] = {};
84 // Note: this implementation stores the result in a static buffer so we only
85 // allow it to be called just once.
86 static bool called_once
= false;
91 DWORD rv
= GetEnvironmentVariableA(name
, env_buffer
, sizeof(env_buffer
));
92 if (rv
> 0 && rv
< sizeof(env_buffer
))
97 const char *GetPwd() {
102 void DumpProcessMap() {
106 void DisableCoreDumper() {
114 bool StackSizeIsUnlimited() {
119 void SetStackSizeLimitInBytes(uptr limit
) {
123 void SleepForSeconds(int seconds
) {
124 Sleep(seconds
* 1000);
127 void SleepForMillis(int millis
) {
131 void Exit(int exitcode
) {
137 _exit(-1); // abort is not NORETURN on Windows.
140 int Atexit(void (*function
)(void)) {
141 return atexit(function
);
144 // ------------------ sanitizer_libc.h
145 void *internal_mmap(void *addr
, uptr length
, int prot
, int flags
,
146 int fd
, u64 offset
) {
151 int internal_munmap(void *addr
, uptr length
) {
156 int internal_close(fd_t fd
) {
161 fd_t
internal_open(const char *filename
, bool write
) {
166 uptr
internal_read(fd_t fd
, void *buf
, uptr count
) {
171 uptr
internal_write(fd_t fd
, const void *buf
, uptr count
) {
174 HANDLE err
= GetStdHandle(STD_ERROR_HANDLE
);
176 return 0; // FIXME: this might not work on some apps.
178 if (!WriteFile(err
, buf
, count
, &ret
, 0))
183 uptr
internal_filesize(fd_t fd
) {
188 int internal_dup2(int oldfd
, int newfd
) {
193 uptr
internal_readlink(const char *path
, char *buf
, uptr bufsize
) {
198 int internal_sched_yield() {
203 } // namespace __sanitizer