blob: 297e3d2dfa121c2bb7d4b0682b9d12c1a81e614f [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
Dmitry Vyukov0ff6d2d2012-11-06 13:19:59 +000015#define WIN32_LEAN_AND_MEAN
16#define NOGDI
Alexey Samsonovdde1f112012-06-05 07:05:10 +000017#include <windows.h>
18
Alexey Samsonov201aa362012-06-06 09:43:32 +000019#include "sanitizer_common.h"
Alexey Samsonovdde1f112012-06-05 07:05:10 +000020#include "sanitizer_libc.h"
21
Alexey Samsonovdde1f112012-06-05 07:05:10 +000022namespace __sanitizer {
23
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000024// --------------------- sanitizer_common.h
Alexey Samsonovee072902012-06-06 09:26:25 +000025int GetPid() {
26 return GetProcessId(GetCurrentProcess());
27}
28
Alexey Samsonov70afb912012-06-15 06:37:34 +000029uptr GetThreadSelf() {
30 return GetCurrentThreadId();
31}
32
Alexey Samsonovcf4d3a02012-06-07 07:32:00 +000033void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000034 uptr *stack_bottom) {
35 CHECK(stack_top);
36 CHECK(stack_bottom);
37 MEMORY_BASIC_INFORMATION mbi;
Kostya Serebryany98390d02012-06-20 15:19:17 +000038 CHECK_NE(VirtualQuery(&mbi /* on stack */, &mbi, sizeof(mbi)), 0);
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000039 // FIXME: is it possible for the stack to not be a single allocation?
40 // Are these values what ASan expects to get (reserved, not committed;
41 // including stack guard page) ?
42 *stack_top = (uptr)mbi.BaseAddress + mbi.RegionSize;
43 *stack_bottom = (uptr)mbi.AllocationBase;
44}
45
Alexey Samsonov40d5b772012-06-06 16:15:07 +000046void *MmapOrDie(uptr size, const char *mem_type) {
Alexey Samsonovee072902012-06-06 09:26:25 +000047 void *rv = VirtualAlloc(0, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
Alexey Samsonov40d5b772012-06-06 16:15:07 +000048 if (rv == 0) {
49 Report("ERROR: Failed to allocate 0x%zx (%zd) bytes of %s\n",
50 size, size, mem_type);
51 CHECK("unable to mmap" && 0);
52 }
Alexey Samsonovee072902012-06-06 09:26:25 +000053 return rv;
54}
55
56void UnmapOrDie(void *addr, uptr size) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +000057 if (VirtualFree(addr, size, MEM_DECOMMIT) == 0) {
Alexey Samsonov40d5b772012-06-06 16:15:07 +000058 Report("ERROR: Failed to deallocate 0x%zx (%zd) bytes at address %p\n",
59 size, size, addr);
60 CHECK("unable to unmap" && 0);
Alexey Samsonove95e29c2012-06-06 15:47:40 +000061 }
Alexey Samsonovee072902012-06-06 09:26:25 +000062}
63
Alexey Samsonovc70d1082012-06-14 14:42:58 +000064void *MmapFixedNoReserve(uptr fixed_addr, uptr size) {
Dmitry Vyukov0ff6d2d2012-11-06 13:19:59 +000065 void *p = VirtualAlloc((LPVOID)fixed_addr, size,
66 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
67 if (p == 0)
68 Report("ERROR: Failed to allocate 0x%zx (%zd) bytes at %p (%d)\n",
69 size, size, fixed_addr, GetLastError());
70 return p;
Alexey Samsonovc70d1082012-06-14 14:42:58 +000071}
72
73void *Mprotect(uptr fixed_addr, uptr size) {
74 return VirtualAlloc((LPVOID)fixed_addr, size,
75 MEM_RESERVE | MEM_COMMIT, PAGE_NOACCESS);
76}
77
Alexey Samsonov40e51282012-06-15 07:29:14 +000078bool MemoryRangeIsAvailable(uptr range_start, uptr range_end) {
79 // FIXME: shall we do anything here on Windows?
80 return true;
81}
82
Alexey Samsonov961276a2012-07-03 08:24:14 +000083void *MapFileToMemory(const char *file_name, uptr *buff_size) {
84 UNIMPLEMENTED();
85}
86
Alexey Samsonov0c53a382012-06-14 14:07:21 +000087const char *GetEnv(const char *name) {
88 static char env_buffer[32767] = {};
89
90 // Note: this implementation stores the result in a static buffer so we only
91 // allow it to be called just once.
92 static bool called_once = false;
93 if (called_once)
94 UNIMPLEMENTED();
95 called_once = true;
96
97 DWORD rv = GetEnvironmentVariableA(name, env_buffer, sizeof(env_buffer));
98 if (rv > 0 && rv < sizeof(env_buffer))
99 return env_buffer;
100 return 0;
101}
102
Alexey Samsonov58a3c582012-06-18 08:44:30 +0000103const char *GetPwd() {
104 UNIMPLEMENTED();
105}
106
Alexey Samsonovae1e1712012-06-15 06:08:19 +0000107void DumpProcessMap() {
108 UNIMPLEMENTED();
109}
110
111void DisableCoreDumper() {
112 UNIMPLEMENTED();
113}
114
Alexey Samsonov97ca3062012-09-17 09:12:39 +0000115void ReExec() {
116 UNIMPLEMENTED();
117}
118
119bool StackSizeIsUnlimited() {
120 UNIMPLEMENTED();
Alexey Samsonov97ca3062012-09-17 09:12:39 +0000121}
122
123void SetStackSizeLimitInBytes(uptr limit) {
124 UNIMPLEMENTED();
125}
126
Alexey Samsonov70afb912012-06-15 06:37:34 +0000127void SleepForSeconds(int seconds) {
128 Sleep(seconds * 1000);
129}
130
Alexey Samsonov58a3c582012-06-18 08:44:30 +0000131void SleepForMillis(int millis) {
132 Sleep(millis);
133}
134
Alexey Samsonov70afb912012-06-15 06:37:34 +0000135void Exit(int exitcode) {
136 _exit(exitcode);
137}
138
139void Abort() {
140 abort();
141 _exit(-1); // abort is not NORETURN on Windows.
142}
143
Dmitry Vyukov0ff6d2d2012-11-06 13:19:59 +0000144#ifndef SANITIZER_GO
Alexey Samsonov70afb912012-06-15 06:37:34 +0000145int Atexit(void (*function)(void)) {
146 return atexit(function);
147}
Dmitry Vyukov0ff6d2d2012-11-06 13:19:59 +0000148#endif
Alexey Samsonov70afb912012-06-15 06:37:34 +0000149
Alexey Samsonov4b1f1032012-06-07 07:13:46 +0000150// ------------------ sanitizer_libc.h
Alexey Samsonovdde1f112012-06-05 07:05:10 +0000151void *internal_mmap(void *addr, uptr length, int prot, int flags,
152 int fd, u64 offset) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000153 UNIMPLEMENTED();
Alexey Samsonovdde1f112012-06-05 07:05:10 +0000154}
155
Alexey Samsonov7ac77d62012-06-05 09:49:25 +0000156int internal_munmap(void *addr, uptr length) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000157 UNIMPLEMENTED();
Alexey Samsonov7ac77d62012-06-05 09:49:25 +0000158}
159
Alexey Samsonov03c8b842012-06-05 08:32:53 +0000160int internal_close(fd_t fd) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000161 UNIMPLEMENTED();
Alexey Samsonov03c8b842012-06-05 08:32:53 +0000162}
163
Alexey Samsonov58358892012-11-02 15:18:34 +0000164int internal_isatty(fd_t fd) {
165 UNIMPLEMENTED();
166}
167
Alexey Samsonovdde1f112012-06-05 07:05:10 +0000168fd_t internal_open(const char *filename, bool write) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000169 UNIMPLEMENTED();
Alexey Samsonovdde1f112012-06-05 07:05:10 +0000170}
171
Alexey Samsonov03c8b842012-06-05 08:32:53 +0000172uptr internal_read(fd_t fd, void *buf, uptr count) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000173 UNIMPLEMENTED();
Alexey Samsonov03c8b842012-06-05 08:32:53 +0000174}
175
176uptr internal_write(fd_t fd, const void *buf, uptr count) {
Alexey Samsonov20ba98f2012-11-02 09:38:47 +0000177 if (fd != kStderrFd)
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000178 UNIMPLEMENTED();
Alexey Samsonov03c8b842012-06-05 08:32:53 +0000179 HANDLE err = GetStdHandle(STD_ERROR_HANDLE);
180 if (err == 0)
181 return 0; // FIXME: this might not work on some apps.
182 DWORD ret;
183 if (!WriteFile(err, buf, count, &ret, 0))
184 return 0;
185 return ret;
186}
187
Alexey Samsonovca2b5d72012-06-06 07:30:33 +0000188uptr internal_filesize(fd_t fd) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000189 UNIMPLEMENTED();
Alexey Samsonovca2b5d72012-06-06 07:30:33 +0000190}
191
192int internal_dup2(int oldfd, int newfd) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000193 UNIMPLEMENTED();
Alexey Samsonovca2b5d72012-06-06 07:30:33 +0000194}
195
Alexey Samsonovf6d21252012-09-05 14:48:24 +0000196uptr internal_readlink(const char *path, char *buf, uptr bufsize) {
197 UNIMPLEMENTED();
Alexey Samsonovf6d21252012-09-05 14:48:24 +0000198}
199
Alexey Samsonov58a3c582012-06-18 08:44:30 +0000200int internal_sched_yield() {
Dmitry Vyukov0ff6d2d2012-11-06 13:19:59 +0000201 Sleep(0);
202 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