1 //===-- sanitizer_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 shared between AddressSanitizer and ThreadSanitizer
11 // run-time libraries and implements windows-specific functions from
12 // sanitizer_libc.h.
13 //===----------------------------------------------------------------------===//
14 #ifdef _WIN32
15 #include <windows.h>
16
17 #include "sanitizer_common.h"
18 #include "sanitizer_libc.h"
19
20 namespace __sanitizer {
21
22 // --------------------- sanitizer_common.h
GetPid()23 int GetPid() {
24 return GetProcessId(GetCurrentProcess());
25 }
26
GetThreadSelf()27 uptr GetThreadSelf() {
28 return GetCurrentThreadId();
29 }
30
GetThreadStackTopAndBottom(bool at_initialization,uptr * stack_top,uptr * stack_bottom)31 void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
32 uptr *stack_bottom) {
33 CHECK(stack_top);
34 CHECK(stack_bottom);
35 MEMORY_BASIC_INFORMATION mbi;
36 CHECK_NE(VirtualQuery(&mbi /* on stack */, &mbi, sizeof(mbi)), 0);
37 // FIXME: is it possible for the stack to not be a single allocation?
38 // Are these values what ASan expects to get (reserved, not committed;
39 // including stack guard page) ?
40 *stack_top = (uptr)mbi.BaseAddress + mbi.RegionSize;
41 *stack_bottom = (uptr)mbi.AllocationBase;
42 }
43
44
MmapOrDie(uptr size,const char * mem_type)45 void *MmapOrDie(uptr size, const char *mem_type) {
46 void *rv = VirtualAlloc(0, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
47 if (rv == 0) {
48 Report("ERROR: Failed to allocate 0x%zx (%zd) bytes of %s\n",
49 size, size, mem_type);
50 CHECK("unable to mmap" && 0);
51 }
52 return rv;
53 }
54
UnmapOrDie(void * addr,uptr size)55 void UnmapOrDie(void *addr, uptr size) {
56 if (VirtualFree(addr, size, MEM_DECOMMIT) == 0) {
57 Report("ERROR: Failed to deallocate 0x%zx (%zd) bytes at address %p\n",
58 size, size, addr);
59 CHECK("unable to unmap" && 0);
60 }
61 }
62
MmapFixedNoReserve(uptr fixed_addr,uptr size)63 void *MmapFixedNoReserve(uptr fixed_addr, uptr size) {
64 return VirtualAlloc((LPVOID)fixed_addr, size,
65 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
66 }
67
Mprotect(uptr fixed_addr,uptr size)68 void *Mprotect(uptr fixed_addr, uptr size) {
69 return VirtualAlloc((LPVOID)fixed_addr, size,
70 MEM_RESERVE | MEM_COMMIT, PAGE_NOACCESS);
71 }
72
MemoryRangeIsAvailable(uptr range_start,uptr range_end)73 bool MemoryRangeIsAvailable(uptr range_start, uptr range_end) {
74 // FIXME: shall we do anything here on Windows?
75 return true;
76 }
77
MapFileToMemory(const char * file_name,uptr * buff_size)78 void *MapFileToMemory(const char *file_name, uptr *buff_size) {
79 UNIMPLEMENTED();
80 return 0;
81 }
82
GetEnv(const char * name)83 const char *GetEnv(const char *name) {
84 static char env_buffer[32767] = {};
85
86 // Note: this implementation stores the result in a static buffer so we only
87 // allow it to be called just once.
88 static bool called_once = false;
89 if (called_once)
90 UNIMPLEMENTED();
91 called_once = true;
92
93 DWORD rv = GetEnvironmentVariableA(name, env_buffer, sizeof(env_buffer));
94 if (rv > 0 && rv < sizeof(env_buffer))
95 return env_buffer;
96 return 0;
97 }
98
GetPwd()99 const char *GetPwd() {
100 UNIMPLEMENTED();
101 return 0;
102 }
103
DumpProcessMap()104 void DumpProcessMap() {
105 UNIMPLEMENTED();
106 }
107
DisableCoreDumper()108 void DisableCoreDumper() {
109 UNIMPLEMENTED();
110 }
111
SleepForSeconds(int seconds)112 void SleepForSeconds(int seconds) {
113 Sleep(seconds * 1000);
114 }
115
SleepForMillis(int millis)116 void SleepForMillis(int millis) {
117 Sleep(millis);
118 }
119
Exit(int exitcode)120 void Exit(int exitcode) {
121 _exit(exitcode);
122 }
123
Abort()124 void Abort() {
125 abort();
126 _exit(-1); // abort is not NORETURN on Windows.
127 }
128
Atexit(void (* function)(void))129 int Atexit(void (*function)(void)) {
130 return atexit(function);
131 }
132
133 // ------------------ sanitizer_libc.h
internal_mmap(void * addr,uptr length,int prot,int flags,int fd,u64 offset)134 void *internal_mmap(void *addr, uptr length, int prot, int flags,
135 int fd, u64 offset) {
136 UNIMPLEMENTED();
137 return 0;
138 }
139
internal_munmap(void * addr,uptr length)140 int internal_munmap(void *addr, uptr length) {
141 UNIMPLEMENTED();
142 return 0;
143 }
144
internal_close(fd_t fd)145 int internal_close(fd_t fd) {
146 UNIMPLEMENTED();
147 return 0;
148 }
149
internal_open(const char * filename,bool write)150 fd_t internal_open(const char *filename, bool write) {
151 UNIMPLEMENTED();
152 return 0;
153 }
154
internal_read(fd_t fd,void * buf,uptr count)155 uptr internal_read(fd_t fd, void *buf, uptr count) {
156 UNIMPLEMENTED();
157 return 0;
158 }
159
internal_write(fd_t fd,const void * buf,uptr count)160 uptr internal_write(fd_t fd, const void *buf, uptr count) {
161 if (fd != 2)
162 UNIMPLEMENTED();
163 HANDLE err = GetStdHandle(STD_ERROR_HANDLE);
164 if (err == 0)
165 return 0; // FIXME: this might not work on some apps.
166 DWORD ret;
167 if (!WriteFile(err, buf, count, &ret, 0))
168 return 0;
169 return ret;
170 }
171
internal_filesize(fd_t fd)172 uptr internal_filesize(fd_t fd) {
173 UNIMPLEMENTED();
174 return 0;
175 }
176
internal_dup2(int oldfd,int newfd)177 int internal_dup2(int oldfd, int newfd) {
178 UNIMPLEMENTED();
179 return 0;
180 }
181
internal_readlink(const char * path,char * buf,uptr bufsize)182 uptr internal_readlink(const char *path, char *buf, uptr bufsize) {
183 UNIMPLEMENTED();
184 return 0;
185 }
186
internal_sched_yield()187 int internal_sched_yield() {
188 UNIMPLEMENTED();
189 return 0;
190 }
191
192 } // namespace __sanitizer
193
194 #endif // _WIN32
195