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
20namespace __sanitizer {
21
22// --------------------- sanitizer_common.h
23int GetPid() {
24  return GetProcessId(GetCurrentProcess());
25}
26
27uptr GetThreadSelf() {
28  return GetCurrentThreadId();
29}
30
31void 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
45void *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
55void 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
63void *MmapFixedNoReserve(uptr fixed_addr, uptr size) {
64  return VirtualAlloc((LPVOID)fixed_addr, size,
65                      MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
66}
67
68void *Mprotect(uptr fixed_addr, uptr size) {
69  return VirtualAlloc((LPVOID)fixed_addr, size,
70                      MEM_RESERVE | MEM_COMMIT, PAGE_NOACCESS);
71}
72
73bool MemoryRangeIsAvailable(uptr range_start, uptr range_end) {
74  // FIXME: shall we do anything here on Windows?
75  return true;
76}
77
78void *MapFileToMemory(const char *file_name, uptr *buff_size) {
79  UNIMPLEMENTED();
80  return 0;
81}
82
83const 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
99const char *GetPwd() {
100  UNIMPLEMENTED();
101  return 0;
102}
103
104void DumpProcessMap() {
105  UNIMPLEMENTED();
106}
107
108void DisableCoreDumper() {
109  UNIMPLEMENTED();
110}
111
112void SleepForSeconds(int seconds) {
113  Sleep(seconds * 1000);
114}
115
116void SleepForMillis(int millis) {
117  Sleep(millis);
118}
119
120void Exit(int exitcode) {
121  _exit(exitcode);
122}
123
124void Abort() {
125  abort();
126  _exit(-1);  // abort is not NORETURN on Windows.
127}
128
129int Atexit(void (*function)(void)) {
130  return atexit(function);
131}
132
133// ------------------ sanitizer_libc.h
134void *internal_mmap(void *addr, uptr length, int prot, int flags,
135                    int fd, u64 offset) {
136  UNIMPLEMENTED();
137  return 0;
138}
139
140int internal_munmap(void *addr, uptr length) {
141  UNIMPLEMENTED();
142  return 0;
143}
144
145int internal_close(fd_t fd) {
146  UNIMPLEMENTED();
147  return 0;
148}
149
150fd_t internal_open(const char *filename, bool write) {
151  UNIMPLEMENTED();
152  return 0;
153}
154
155uptr internal_read(fd_t fd, void *buf, uptr count) {
156  UNIMPLEMENTED();
157  return 0;
158}
159
160uptr 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
172uptr internal_filesize(fd_t fd) {
173  UNIMPLEMENTED();
174  return 0;
175}
176
177int internal_dup2(int oldfd, int newfd) {
178  UNIMPLEMENTED();
179  return 0;
180}
181
182uptr internal_readlink(const char *path, char *buf, uptr bufsize) {
183  UNIMPLEMENTED();
184  return 0;
185}
186
187int internal_sched_yield() {
188  UNIMPLEMENTED();
189  return 0;
190}
191
192}  // namespace __sanitizer
193
194#endif  // _WIN32
195