2012-10-29 Wei Mi <wmi@google.com>
[official-gcc.git] / libasan / sanitizer_common / sanitizer_win.cc
blob314852304d837279a4535b4773cb2b57fa887ddb
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 //===----------------------------------------------------------------------===//
12 #ifdef _WIN32
13 #include <windows.h>
15 #include "sanitizer_common.h"
16 #include "sanitizer_libc.h"
18 namespace __sanitizer {
20 // --------------------- sanitizer_common.h
21 int GetPid() {
22 return GetProcessId(GetCurrentProcess());
25 uptr GetThreadSelf() {
26 return GetCurrentThreadId();
29 void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
30 uptr *stack_bottom) {
31 CHECK(stack_top);
32 CHECK(stack_bottom);
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);
45 if (rv == 0) {
46 Report("ERROR: Failed to allocate 0x%zx (%zd) bytes of %s\n",
47 size, size, mem_type);
48 CHECK("unable to mmap" && 0);
50 return rv;
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",
56 size, size, addr);
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?
73 return true;
76 void *MapFileToMemory(const char *file_name, uptr *buff_size) {
77 UNIMPLEMENTED();
78 return 0;
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;
87 if (called_once)
88 UNIMPLEMENTED();
89 called_once = true;
91 DWORD rv = GetEnvironmentVariableA(name, env_buffer, sizeof(env_buffer));
92 if (rv > 0 && rv < sizeof(env_buffer))
93 return env_buffer;
94 return 0;
97 const char *GetPwd() {
98 UNIMPLEMENTED();
99 return 0;
102 void DumpProcessMap() {
103 UNIMPLEMENTED();
106 void DisableCoreDumper() {
107 UNIMPLEMENTED();
110 void ReExec() {
111 UNIMPLEMENTED();
114 bool StackSizeIsUnlimited() {
115 UNIMPLEMENTED();
116 return false;
119 void SetStackSizeLimitInBytes(uptr limit) {
120 UNIMPLEMENTED();
123 void SleepForSeconds(int seconds) {
124 Sleep(seconds * 1000);
127 void SleepForMillis(int millis) {
128 Sleep(millis);
131 void Exit(int exitcode) {
132 _exit(exitcode);
135 void Abort() {
136 abort();
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) {
147 UNIMPLEMENTED();
148 return 0;
151 int internal_munmap(void *addr, uptr length) {
152 UNIMPLEMENTED();
153 return 0;
156 int internal_close(fd_t fd) {
157 UNIMPLEMENTED();
158 return 0;
161 fd_t internal_open(const char *filename, bool write) {
162 UNIMPLEMENTED();
163 return 0;
166 uptr internal_read(fd_t fd, void *buf, uptr count) {
167 UNIMPLEMENTED();
168 return 0;
171 uptr internal_write(fd_t fd, const void *buf, uptr count) {
172 if (fd != 2)
173 UNIMPLEMENTED();
174 HANDLE err = GetStdHandle(STD_ERROR_HANDLE);
175 if (err == 0)
176 return 0; // FIXME: this might not work on some apps.
177 DWORD ret;
178 if (!WriteFile(err, buf, count, &ret, 0))
179 return 0;
180 return ret;
183 uptr internal_filesize(fd_t fd) {
184 UNIMPLEMENTED();
185 return 0;
188 int internal_dup2(int oldfd, int newfd) {
189 UNIMPLEMENTED();
190 return 0;
193 uptr internal_readlink(const char *path, char *buf, uptr bufsize) {
194 UNIMPLEMENTED();
195 return 0;
198 int internal_sched_yield() {
199 UNIMPLEMENTED();
200 return 0;
203 } // namespace __sanitizer
205 #endif // _WIN32