blob: 31ae7f1e6957e63594a10fb877bf4af7c880b542 [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 Samsonovc463f302012-11-08 13:08:41 +000017#include <stdlib.h>
Alexey Samsonovdde1f112012-06-05 07:05:10 +000018#include <windows.h>
19
Alexey Samsonov201aa362012-06-06 09:43:32 +000020#include "sanitizer_common.h"
Alexey Samsonovdde1f112012-06-05 07:05:10 +000021#include "sanitizer_libc.h"
22
Alexey Samsonovdde1f112012-06-05 07:05:10 +000023namespace __sanitizer {
24
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000025// --------------------- sanitizer_common.h
Kostya Serebryanyf22c6972012-11-23 15:38:49 +000026uptr GetPageSize() {
27 return 1U << 14; // FIXME: is this configurable?
28}
29
30uptr GetMmapGranularity() {
31 return 1U << 16; // FIXME: is this configurable?
32}
33
Alexey Samsonovae9b18b2012-11-09 14:45:30 +000034bool FileExists(const char *filename) {
35 UNIMPLEMENTED();
36}
37
Alexey Samsonovee072902012-06-06 09:26:25 +000038int GetPid() {
39 return GetProcessId(GetCurrentProcess());
40}
41
Alexey Samsonov70afb912012-06-15 06:37:34 +000042uptr GetThreadSelf() {
43 return GetCurrentThreadId();
44}
45
Alexey Samsonovcf4d3a02012-06-07 07:32:00 +000046void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000047 uptr *stack_bottom) {
48 CHECK(stack_top);
49 CHECK(stack_bottom);
50 MEMORY_BASIC_INFORMATION mbi;
Kostya Serebryany98390d02012-06-20 15:19:17 +000051 CHECK_NE(VirtualQuery(&mbi /* on stack */, &mbi, sizeof(mbi)), 0);
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000052 // FIXME: is it possible for the stack to not be a single allocation?
53 // Are these values what ASan expects to get (reserved, not committed;
54 // including stack guard page) ?
55 *stack_top = (uptr)mbi.BaseAddress + mbi.RegionSize;
56 *stack_bottom = (uptr)mbi.AllocationBase;
57}
58
Alexey Samsonov40d5b772012-06-06 16:15:07 +000059void *MmapOrDie(uptr size, const char *mem_type) {
Alexey Samsonovee072902012-06-06 09:26:25 +000060 void *rv = VirtualAlloc(0, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
Alexey Samsonov40d5b772012-06-06 16:15:07 +000061 if (rv == 0) {
62 Report("ERROR: Failed to allocate 0x%zx (%zd) bytes of %s\n",
63 size, size, mem_type);
64 CHECK("unable to mmap" && 0);
65 }
Alexey Samsonovee072902012-06-06 09:26:25 +000066 return rv;
67}
68
69void UnmapOrDie(void *addr, uptr size) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +000070 if (VirtualFree(addr, size, MEM_DECOMMIT) == 0) {
Alexey Samsonov40d5b772012-06-06 16:15:07 +000071 Report("ERROR: Failed to deallocate 0x%zx (%zd) bytes at address %p\n",
72 size, size, addr);
73 CHECK("unable to unmap" && 0);
Alexey Samsonove95e29c2012-06-06 15:47:40 +000074 }
Alexey Samsonovee072902012-06-06 09:26:25 +000075}
76
Alexey Samsonovc70d1082012-06-14 14:42:58 +000077void *MmapFixedNoReserve(uptr fixed_addr, uptr size) {
Dmitry Vyukov0ff6d2d2012-11-06 13:19:59 +000078 void *p = VirtualAlloc((LPVOID)fixed_addr, size,
79 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
80 if (p == 0)
81 Report("ERROR: Failed to allocate 0x%zx (%zd) bytes at %p (%d)\n",
82 size, size, fixed_addr, GetLastError());
83 return p;
Alexey Samsonovc70d1082012-06-14 14:42:58 +000084}
85
86void *Mprotect(uptr fixed_addr, uptr size) {
87 return VirtualAlloc((LPVOID)fixed_addr, size,
88 MEM_RESERVE | MEM_COMMIT, PAGE_NOACCESS);
89}
90
Alexey Samsonov40e51282012-06-15 07:29:14 +000091bool MemoryRangeIsAvailable(uptr range_start, uptr range_end) {
92 // FIXME: shall we do anything here on Windows?
93 return true;
94}
95
Alexey Samsonov961276a2012-07-03 08:24:14 +000096void *MapFileToMemory(const char *file_name, uptr *buff_size) {
97 UNIMPLEMENTED();
98}
99
Alexey Samsonov0c53a382012-06-14 14:07:21 +0000100const char *GetEnv(const char *name) {
101 static char env_buffer[32767] = {};
102
103 // Note: this implementation stores the result in a static buffer so we only
104 // allow it to be called just once.
105 static bool called_once = false;
106 if (called_once)
107 UNIMPLEMENTED();
108 called_once = true;
109
110 DWORD rv = GetEnvironmentVariableA(name, env_buffer, sizeof(env_buffer));
111 if (rv > 0 && rv < sizeof(env_buffer))
112 return env_buffer;
113 return 0;
114}
115
Alexey Samsonov58a3c582012-06-18 08:44:30 +0000116const char *GetPwd() {
117 UNIMPLEMENTED();
118}
119
Alexey Samsonovae1e1712012-06-15 06:08:19 +0000120void DumpProcessMap() {
121 UNIMPLEMENTED();
122}
123
124void DisableCoreDumper() {
125 UNIMPLEMENTED();
126}
127
Alexey Samsonov97ca3062012-09-17 09:12:39 +0000128void ReExec() {
129 UNIMPLEMENTED();
130}
131
132bool StackSizeIsUnlimited() {
133 UNIMPLEMENTED();
Alexey Samsonov97ca3062012-09-17 09:12:39 +0000134}
135
136void SetStackSizeLimitInBytes(uptr limit) {
137 UNIMPLEMENTED();
138}
139
Alexey Samsonov70afb912012-06-15 06:37:34 +0000140void SleepForSeconds(int seconds) {
141 Sleep(seconds * 1000);
142}
143
Alexey Samsonov58a3c582012-06-18 08:44:30 +0000144void SleepForMillis(int millis) {
145 Sleep(millis);
146}
147
Alexey Samsonov70afb912012-06-15 06:37:34 +0000148void Exit(int exitcode) {
149 _exit(exitcode);
150}
151
152void Abort() {
153 abort();
154 _exit(-1); // abort is not NORETURN on Windows.
155}
156
Dmitry Vyukov0ff6d2d2012-11-06 13:19:59 +0000157#ifndef SANITIZER_GO
Alexey Samsonov70afb912012-06-15 06:37:34 +0000158int Atexit(void (*function)(void)) {
159 return atexit(function);
160}
Dmitry Vyukov0ff6d2d2012-11-06 13:19:59 +0000161#endif
Alexey Samsonov70afb912012-06-15 06:37:34 +0000162
Alexey Samsonov4b1f1032012-06-07 07:13:46 +0000163// ------------------ sanitizer_libc.h
Alexey Samsonovdde1f112012-06-05 07:05:10 +0000164void *internal_mmap(void *addr, uptr length, int prot, int flags,
165 int fd, u64 offset) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000166 UNIMPLEMENTED();
Alexey Samsonovdde1f112012-06-05 07:05:10 +0000167}
168
Alexey Samsonov7ac77d62012-06-05 09:49:25 +0000169int internal_munmap(void *addr, uptr length) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000170 UNIMPLEMENTED();
Alexey Samsonov7ac77d62012-06-05 09:49:25 +0000171}
172
Alexey Samsonov03c8b842012-06-05 08:32:53 +0000173int internal_close(fd_t fd) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000174 UNIMPLEMENTED();
Alexey Samsonov03c8b842012-06-05 08:32:53 +0000175}
176
Alexey Samsonov58358892012-11-02 15:18:34 +0000177int internal_isatty(fd_t fd) {
178 UNIMPLEMENTED();
179}
180
Alexey Samsonovdde1f112012-06-05 07:05:10 +0000181fd_t internal_open(const char *filename, bool write) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000182 UNIMPLEMENTED();
Alexey Samsonovdde1f112012-06-05 07:05:10 +0000183}
184
Alexey Samsonov03c8b842012-06-05 08:32:53 +0000185uptr internal_read(fd_t fd, void *buf, uptr count) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000186 UNIMPLEMENTED();
Alexey Samsonov03c8b842012-06-05 08:32:53 +0000187}
188
189uptr internal_write(fd_t fd, const void *buf, uptr count) {
Alexey Samsonov20ba98f2012-11-02 09:38:47 +0000190 if (fd != kStderrFd)
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000191 UNIMPLEMENTED();
Alexey Samsonov03c8b842012-06-05 08:32:53 +0000192 HANDLE err = GetStdHandle(STD_ERROR_HANDLE);
193 if (err == 0)
194 return 0; // FIXME: this might not work on some apps.
195 DWORD ret;
196 if (!WriteFile(err, buf, count, &ret, 0))
197 return 0;
198 return ret;
199}
200
Alexey Samsonovca2b5d72012-06-06 07:30:33 +0000201uptr internal_filesize(fd_t fd) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000202 UNIMPLEMENTED();
Alexey Samsonovca2b5d72012-06-06 07:30:33 +0000203}
204
205int internal_dup2(int oldfd, int newfd) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000206 UNIMPLEMENTED();
Alexey Samsonovca2b5d72012-06-06 07:30:33 +0000207}
208
Alexey Samsonovf6d21252012-09-05 14:48:24 +0000209uptr internal_readlink(const char *path, char *buf, uptr bufsize) {
210 UNIMPLEMENTED();
Alexey Samsonovf6d21252012-09-05 14:48:24 +0000211}
212
Alexey Samsonov58a3c582012-06-18 08:44:30 +0000213int internal_sched_yield() {
Dmitry Vyukov0ff6d2d2012-11-06 13:19:59 +0000214 Sleep(0);
215 return 0;
Alexey Samsonov58a3c582012-06-18 08:44:30 +0000216}
217
Alexey Samsonovdde1f112012-06-05 07:05:10 +0000218} // namespace __sanitizer
219
220#endif // _WIN32