blob: 90bf35a43953258e79e4b4782f37f9e5548d524a [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
Alexey Samsonovae9b18b2012-11-09 14:45:30 +000026bool FileExists(const char *filename) {
27 UNIMPLEMENTED();
28}
29
Alexey Samsonovee072902012-06-06 09:26:25 +000030int GetPid() {
31 return GetProcessId(GetCurrentProcess());
32}
33
Alexey Samsonov70afb912012-06-15 06:37:34 +000034uptr GetThreadSelf() {
35 return GetCurrentThreadId();
36}
37
Alexey Samsonovcf4d3a02012-06-07 07:32:00 +000038void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000039 uptr *stack_bottom) {
40 CHECK(stack_top);
41 CHECK(stack_bottom);
42 MEMORY_BASIC_INFORMATION mbi;
Kostya Serebryany98390d02012-06-20 15:19:17 +000043 CHECK_NE(VirtualQuery(&mbi /* on stack */, &mbi, sizeof(mbi)), 0);
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000044 // FIXME: is it possible for the stack to not be a single allocation?
45 // Are these values what ASan expects to get (reserved, not committed;
46 // including stack guard page) ?
47 *stack_top = (uptr)mbi.BaseAddress + mbi.RegionSize;
48 *stack_bottom = (uptr)mbi.AllocationBase;
49}
50
Alexey Samsonov40d5b772012-06-06 16:15:07 +000051void *MmapOrDie(uptr size, const char *mem_type) {
Alexey Samsonovee072902012-06-06 09:26:25 +000052 void *rv = VirtualAlloc(0, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
Alexey Samsonov40d5b772012-06-06 16:15:07 +000053 if (rv == 0) {
54 Report("ERROR: Failed to allocate 0x%zx (%zd) bytes of %s\n",
55 size, size, mem_type);
56 CHECK("unable to mmap" && 0);
57 }
Alexey Samsonovee072902012-06-06 09:26:25 +000058 return rv;
59}
60
61void UnmapOrDie(void *addr, uptr size) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +000062 if (VirtualFree(addr, size, MEM_DECOMMIT) == 0) {
Alexey Samsonov40d5b772012-06-06 16:15:07 +000063 Report("ERROR: Failed to deallocate 0x%zx (%zd) bytes at address %p\n",
64 size, size, addr);
65 CHECK("unable to unmap" && 0);
Alexey Samsonove95e29c2012-06-06 15:47:40 +000066 }
Alexey Samsonovee072902012-06-06 09:26:25 +000067}
68
Alexey Samsonovc70d1082012-06-14 14:42:58 +000069void *MmapFixedNoReserve(uptr fixed_addr, uptr size) {
Dmitry Vyukov0ff6d2d2012-11-06 13:19:59 +000070 void *p = VirtualAlloc((LPVOID)fixed_addr, size,
71 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
72 if (p == 0)
73 Report("ERROR: Failed to allocate 0x%zx (%zd) bytes at %p (%d)\n",
74 size, size, fixed_addr, GetLastError());
75 return p;
Alexey Samsonovc70d1082012-06-14 14:42:58 +000076}
77
78void *Mprotect(uptr fixed_addr, uptr size) {
79 return VirtualAlloc((LPVOID)fixed_addr, size,
80 MEM_RESERVE | MEM_COMMIT, PAGE_NOACCESS);
81}
82
Alexey Samsonov40e51282012-06-15 07:29:14 +000083bool MemoryRangeIsAvailable(uptr range_start, uptr range_end) {
84 // FIXME: shall we do anything here on Windows?
85 return true;
86}
87
Alexey Samsonov961276a2012-07-03 08:24:14 +000088void *MapFileToMemory(const char *file_name, uptr *buff_size) {
89 UNIMPLEMENTED();
90}
91
Alexey Samsonov0c53a382012-06-14 14:07:21 +000092const char *GetEnv(const char *name) {
93 static char env_buffer[32767] = {};
94
95 // Note: this implementation stores the result in a static buffer so we only
96 // allow it to be called just once.
97 static bool called_once = false;
98 if (called_once)
99 UNIMPLEMENTED();
100 called_once = true;
101
102 DWORD rv = GetEnvironmentVariableA(name, env_buffer, sizeof(env_buffer));
103 if (rv > 0 && rv < sizeof(env_buffer))
104 return env_buffer;
105 return 0;
106}
107
Alexey Samsonov58a3c582012-06-18 08:44:30 +0000108const char *GetPwd() {
109 UNIMPLEMENTED();
110}
111
Alexey Samsonovae1e1712012-06-15 06:08:19 +0000112void DumpProcessMap() {
113 UNIMPLEMENTED();
114}
115
116void DisableCoreDumper() {
117 UNIMPLEMENTED();
118}
119
Alexey Samsonov97ca3062012-09-17 09:12:39 +0000120void ReExec() {
121 UNIMPLEMENTED();
122}
123
124bool StackSizeIsUnlimited() {
125 UNIMPLEMENTED();
Alexey Samsonov97ca3062012-09-17 09:12:39 +0000126}
127
128void SetStackSizeLimitInBytes(uptr limit) {
129 UNIMPLEMENTED();
130}
131
Alexey Samsonov70afb912012-06-15 06:37:34 +0000132void SleepForSeconds(int seconds) {
133 Sleep(seconds * 1000);
134}
135
Alexey Samsonov58a3c582012-06-18 08:44:30 +0000136void SleepForMillis(int millis) {
137 Sleep(millis);
138}
139
Alexey Samsonov70afb912012-06-15 06:37:34 +0000140void Exit(int exitcode) {
141 _exit(exitcode);
142}
143
144void Abort() {
145 abort();
146 _exit(-1); // abort is not NORETURN on Windows.
147}
148
Dmitry Vyukov0ff6d2d2012-11-06 13:19:59 +0000149#ifndef SANITIZER_GO
Alexey Samsonov70afb912012-06-15 06:37:34 +0000150int Atexit(void (*function)(void)) {
151 return atexit(function);
152}
Dmitry Vyukov0ff6d2d2012-11-06 13:19:59 +0000153#endif
Alexey Samsonov70afb912012-06-15 06:37:34 +0000154
Alexey Samsonov4b1f1032012-06-07 07:13:46 +0000155// ------------------ sanitizer_libc.h
Alexey Samsonovdde1f112012-06-05 07:05:10 +0000156void *internal_mmap(void *addr, uptr length, int prot, int flags,
157 int fd, u64 offset) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000158 UNIMPLEMENTED();
Alexey Samsonovdde1f112012-06-05 07:05:10 +0000159}
160
Alexey Samsonov7ac77d62012-06-05 09:49:25 +0000161int internal_munmap(void *addr, uptr length) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000162 UNIMPLEMENTED();
Alexey Samsonov7ac77d62012-06-05 09:49:25 +0000163}
164
Alexey Samsonov03c8b842012-06-05 08:32:53 +0000165int internal_close(fd_t fd) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000166 UNIMPLEMENTED();
Alexey Samsonov03c8b842012-06-05 08:32:53 +0000167}
168
Alexey Samsonov58358892012-11-02 15:18:34 +0000169int internal_isatty(fd_t fd) {
170 UNIMPLEMENTED();
171}
172
Alexey Samsonovdde1f112012-06-05 07:05:10 +0000173fd_t internal_open(const char *filename, bool write) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000174 UNIMPLEMENTED();
Alexey Samsonovdde1f112012-06-05 07:05:10 +0000175}
176
Alexey Samsonov03c8b842012-06-05 08:32:53 +0000177uptr internal_read(fd_t fd, void *buf, uptr count) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000178 UNIMPLEMENTED();
Alexey Samsonov03c8b842012-06-05 08:32:53 +0000179}
180
181uptr internal_write(fd_t fd, const void *buf, uptr count) {
Alexey Samsonov20ba98f2012-11-02 09:38:47 +0000182 if (fd != kStderrFd)
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000183 UNIMPLEMENTED();
Alexey Samsonov03c8b842012-06-05 08:32:53 +0000184 HANDLE err = GetStdHandle(STD_ERROR_HANDLE);
185 if (err == 0)
186 return 0; // FIXME: this might not work on some apps.
187 DWORD ret;
188 if (!WriteFile(err, buf, count, &ret, 0))
189 return 0;
190 return ret;
191}
192
Alexey Samsonovca2b5d72012-06-06 07:30:33 +0000193uptr internal_filesize(fd_t fd) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000194 UNIMPLEMENTED();
Alexey Samsonovca2b5d72012-06-06 07:30:33 +0000195}
196
197int internal_dup2(int oldfd, int newfd) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000198 UNIMPLEMENTED();
Alexey Samsonovca2b5d72012-06-06 07:30:33 +0000199}
200
Alexey Samsonovf6d21252012-09-05 14:48:24 +0000201uptr internal_readlink(const char *path, char *buf, uptr bufsize) {
202 UNIMPLEMENTED();
Alexey Samsonovf6d21252012-09-05 14:48:24 +0000203}
204
Alexey Samsonov58a3c582012-06-18 08:44:30 +0000205int internal_sched_yield() {
Dmitry Vyukov0ff6d2d2012-11-06 13:19:59 +0000206 Sleep(0);
207 return 0;
Alexey Samsonov58a3c582012-06-18 08:44:30 +0000208}
209
Alexey Samsonovdde1f112012-06-05 07:05:10 +0000210} // namespace __sanitizer
211
212#endif // _WIN32