blob: 680184f266622a3991b318e33d1b4dc89581de25 [file] [log] [blame]
Alexey Samsonov298dd7c2012-06-05 07:46:31 +00001//===-- sanitizer_win.cc --------------------------------------------------===//
Alexey Samsonovdde1f112012-06-05 07:05:10 +00002//
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
Alexey Samsonov201aa362012-06-06 09:43:32 +000017#include "sanitizer_common.h"
Alexey Samsonovdde1f112012-06-05 07:05:10 +000018#include "sanitizer_libc.h"
19
Alexey Samsonovdde1f112012-06-05 07:05:10 +000020namespace __sanitizer {
21
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000022// --------------------- sanitizer_common.h
Alexey Samsonovee072902012-06-06 09:26:25 +000023int GetPid() {
24 return GetProcessId(GetCurrentProcess());
25}
26
Alexey Samsonov70afb912012-06-15 06:37:34 +000027uptr GetThreadSelf() {
28 return GetCurrentThreadId();
29}
30
Alexey Samsonovcf4d3a02012-06-07 07:32:00 +000031void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000032 uptr *stack_bottom) {
33 CHECK(stack_top);
34 CHECK(stack_bottom);
35 MEMORY_BASIC_INFORMATION mbi;
Kostya Serebryany98390d02012-06-20 15:19:17 +000036 CHECK_NE(VirtualQuery(&mbi /* on stack */, &mbi, sizeof(mbi)), 0);
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000037 // 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
Alexey Samsonov40d5b772012-06-06 16:15:07 +000045void *MmapOrDie(uptr size, const char *mem_type) {
Alexey Samsonovee072902012-06-06 09:26:25 +000046 void *rv = VirtualAlloc(0, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
Alexey Samsonov40d5b772012-06-06 16:15:07 +000047 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 }
Alexey Samsonovee072902012-06-06 09:26:25 +000052 return rv;
53}
54
55void UnmapOrDie(void *addr, uptr size) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +000056 if (VirtualFree(addr, size, MEM_DECOMMIT) == 0) {
Alexey Samsonov40d5b772012-06-06 16:15:07 +000057 Report("ERROR: Failed to deallocate 0x%zx (%zd) bytes at address %p\n",
58 size, size, addr);
59 CHECK("unable to unmap" && 0);
Alexey Samsonove95e29c2012-06-06 15:47:40 +000060 }
Alexey Samsonovee072902012-06-06 09:26:25 +000061}
62
Alexey Samsonovc70d1082012-06-14 14:42:58 +000063void *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
Alexey Samsonov40e51282012-06-15 07:29:14 +000073bool MemoryRangeIsAvailable(uptr range_start, uptr range_end) {
74 // FIXME: shall we do anything here on Windows?
75 return true;
76}
77
Alexey Samsonov961276a2012-07-03 08:24:14 +000078void *MapFileToMemory(const char *file_name, uptr *buff_size) {
79 UNIMPLEMENTED();
Alexey Samsonov28d8be22012-08-27 14:08:53 +000080 return 0;
Alexey Samsonov961276a2012-07-03 08:24:14 +000081}
82
Alexey Samsonov0c53a382012-06-14 14:07:21 +000083const 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
Alexey Samsonov58a3c582012-06-18 08:44:30 +000099const char *GetPwd() {
100 UNIMPLEMENTED();
Dmitry Vyukov3c5c9e72012-06-29 18:37:45 +0000101 return 0;
Alexey Samsonov58a3c582012-06-18 08:44:30 +0000102}
103
Alexey Samsonovae1e1712012-06-15 06:08:19 +0000104void DumpProcessMap() {
105 UNIMPLEMENTED();
106}
107
108void DisableCoreDumper() {
109 UNIMPLEMENTED();
110}
111
Alexey Samsonov97ca3062012-09-17 09:12:39 +0000112void ReExec() {
113 UNIMPLEMENTED();
114}
115
116bool StackSizeIsUnlimited() {
117 UNIMPLEMENTED();
118 return false;
119}
120
121void SetStackSizeLimitInBytes(uptr limit) {
122 UNIMPLEMENTED();
123}
124
Alexey Samsonov70afb912012-06-15 06:37:34 +0000125void SleepForSeconds(int seconds) {
126 Sleep(seconds * 1000);
127}
128
Alexey Samsonov58a3c582012-06-18 08:44:30 +0000129void SleepForMillis(int millis) {
130 Sleep(millis);
131}
132
Alexey Samsonov70afb912012-06-15 06:37:34 +0000133void Exit(int exitcode) {
134 _exit(exitcode);
135}
136
137void Abort() {
138 abort();
139 _exit(-1); // abort is not NORETURN on Windows.
140}
141
142int Atexit(void (*function)(void)) {
143 return atexit(function);
144}
145
Alexey Samsonov4b1f1032012-06-07 07:13:46 +0000146// ------------------ sanitizer_libc.h
Alexey Samsonovdde1f112012-06-05 07:05:10 +0000147void *internal_mmap(void *addr, uptr length, int prot, int flags,
148 int fd, u64 offset) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000149 UNIMPLEMENTED();
Dmitry Vyukov3c5c9e72012-06-29 18:37:45 +0000150 return 0;
Alexey Samsonovdde1f112012-06-05 07:05:10 +0000151}
152
Alexey Samsonov7ac77d62012-06-05 09:49:25 +0000153int internal_munmap(void *addr, uptr length) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000154 UNIMPLEMENTED();
Dmitry Vyukov3c5c9e72012-06-29 18:37:45 +0000155 return 0;
Alexey Samsonov7ac77d62012-06-05 09:49:25 +0000156}
157
Alexey Samsonov03c8b842012-06-05 08:32:53 +0000158int internal_close(fd_t fd) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000159 UNIMPLEMENTED();
Dmitry Vyukov3c5c9e72012-06-29 18:37:45 +0000160 return 0;
Alexey Samsonov03c8b842012-06-05 08:32:53 +0000161}
162
Alexey Samsonovdde1f112012-06-05 07:05:10 +0000163fd_t internal_open(const char *filename, bool write) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000164 UNIMPLEMENTED();
Dmitry Vyukov3c5c9e72012-06-29 18:37:45 +0000165 return 0;
Alexey Samsonovdde1f112012-06-05 07:05:10 +0000166}
167
Alexey Samsonov03c8b842012-06-05 08:32:53 +0000168uptr internal_read(fd_t fd, void *buf, uptr count) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000169 UNIMPLEMENTED();
Dmitry Vyukov3c5c9e72012-06-29 18:37:45 +0000170 return 0;
Alexey Samsonov03c8b842012-06-05 08:32:53 +0000171}
172
173uptr internal_write(fd_t fd, const void *buf, uptr count) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000174 if (fd != 2)
175 UNIMPLEMENTED();
Alexey Samsonov03c8b842012-06-05 08:32:53 +0000176 HANDLE err = GetStdHandle(STD_ERROR_HANDLE);
177 if (err == 0)
178 return 0; // FIXME: this might not work on some apps.
179 DWORD ret;
180 if (!WriteFile(err, buf, count, &ret, 0))
181 return 0;
182 return ret;
183}
184
Alexey Samsonovca2b5d72012-06-06 07:30:33 +0000185uptr internal_filesize(fd_t fd) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000186 UNIMPLEMENTED();
Dmitry Vyukov3c5c9e72012-06-29 18:37:45 +0000187 return 0;
Alexey Samsonovca2b5d72012-06-06 07:30:33 +0000188}
189
190int internal_dup2(int oldfd, int newfd) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000191 UNIMPLEMENTED();
Dmitry Vyukov3c5c9e72012-06-29 18:37:45 +0000192 return 0;
Alexey Samsonovca2b5d72012-06-06 07:30:33 +0000193}
194
Alexey Samsonovf6d21252012-09-05 14:48:24 +0000195uptr internal_readlink(const char *path, char *buf, uptr bufsize) {
196 UNIMPLEMENTED();
197 return 0;
198}
199
Alexey Samsonov58a3c582012-06-18 08:44:30 +0000200int internal_sched_yield() {
201 UNIMPLEMENTED();
Dmitry Vyukov3c5c9e72012-06-29 18:37:45 +0000202 return 0;
Alexey Samsonov58a3c582012-06-18 08:44:30 +0000203}
204
Alexey Samsonovdde1f112012-06-05 07:05:10 +0000205} // namespace __sanitizer
206
207#endif // _WIN32