blob: c68a1fee4068fd6afd58793c9481a03c7b0d46b9 [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"
Alexey Samsonov961276a2012-07-03 08:24:14 +000019#include "sanitizer_symbolizer.h"
Alexey Samsonovdde1f112012-06-05 07:05:10 +000020
Alexey Samsonovdde1f112012-06-05 07:05:10 +000021namespace __sanitizer {
22
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000023// --------------------- sanitizer_common.h
Alexey Samsonovee072902012-06-06 09:26:25 +000024int GetPid() {
25 return GetProcessId(GetCurrentProcess());
26}
27
Alexey Samsonov70afb912012-06-15 06:37:34 +000028uptr GetThreadSelf() {
29 return GetCurrentThreadId();
30}
31
Alexey Samsonovcf4d3a02012-06-07 07:32:00 +000032void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000033 uptr *stack_bottom) {
34 CHECK(stack_top);
35 CHECK(stack_bottom);
36 MEMORY_BASIC_INFORMATION mbi;
Kostya Serebryany98390d02012-06-20 15:19:17 +000037 CHECK_NE(VirtualQuery(&mbi /* on stack */, &mbi, sizeof(mbi)), 0);
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000038 // FIXME: is it possible for the stack to not be a single allocation?
39 // Are these values what ASan expects to get (reserved, not committed;
40 // including stack guard page) ?
41 *stack_top = (uptr)mbi.BaseAddress + mbi.RegionSize;
42 *stack_bottom = (uptr)mbi.AllocationBase;
43}
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) {
65 return VirtualAlloc((LPVOID)fixed_addr, size,
66 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
67}
68
69void *Mprotect(uptr fixed_addr, uptr size) {
70 return VirtualAlloc((LPVOID)fixed_addr, size,
71 MEM_RESERVE | MEM_COMMIT, PAGE_NOACCESS);
72}
73
Alexey Samsonov40e51282012-06-15 07:29:14 +000074bool MemoryRangeIsAvailable(uptr range_start, uptr range_end) {
75 // FIXME: shall we do anything here on Windows?
76 return true;
77}
78
Alexey Samsonov961276a2012-07-03 08:24:14 +000079void *MapFileToMemory(const char *file_name, uptr *buff_size) {
80 UNIMPLEMENTED();
81}
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 Samsonov70afb912012-06-15 06:37:34 +0000112void SleepForSeconds(int seconds) {
113 Sleep(seconds * 1000);
114}
115
Alexey Samsonov58a3c582012-06-18 08:44:30 +0000116void SleepForMillis(int millis) {
117 Sleep(millis);
118}
119
Alexey Samsonov70afb912012-06-15 06:37:34 +0000120void 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
Alexey Samsonov961276a2012-07-03 08:24:14 +0000133// ------------------ sanitizer_symbolizer.h
134bool FindDWARFSection(uptr object_file_addr, const char *section_name,
135 DWARFSection *section) {
136 UNIMPLEMENTED();
137 return false;
138}
139
140uptr GetListOfModules(ModuleDIContext *modules, uptr max_modules) {
141 UNIMPLEMENTED();
142};
143
Alexey Samsonov4b1f1032012-06-07 07:13:46 +0000144// ------------------ sanitizer_libc.h
Alexey Samsonovdde1f112012-06-05 07:05:10 +0000145void *internal_mmap(void *addr, uptr length, int prot, int flags,
146 int fd, u64 offset) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000147 UNIMPLEMENTED();
Dmitry Vyukov3c5c9e72012-06-29 18:37:45 +0000148 return 0;
Alexey Samsonovdde1f112012-06-05 07:05:10 +0000149}
150
Alexey Samsonov7ac77d62012-06-05 09:49:25 +0000151int internal_munmap(void *addr, uptr length) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000152 UNIMPLEMENTED();
Dmitry Vyukov3c5c9e72012-06-29 18:37:45 +0000153 return 0;
Alexey Samsonov7ac77d62012-06-05 09:49:25 +0000154}
155
Alexey Samsonov03c8b842012-06-05 08:32:53 +0000156int internal_close(fd_t fd) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000157 UNIMPLEMENTED();
Dmitry Vyukov3c5c9e72012-06-29 18:37:45 +0000158 return 0;
Alexey Samsonov03c8b842012-06-05 08:32:53 +0000159}
160
Alexey Samsonovdde1f112012-06-05 07:05:10 +0000161fd_t internal_open(const char *filename, bool write) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000162 UNIMPLEMENTED();
Dmitry Vyukov3c5c9e72012-06-29 18:37:45 +0000163 return 0;
Alexey Samsonovdde1f112012-06-05 07:05:10 +0000164}
165
Alexey Samsonov03c8b842012-06-05 08:32:53 +0000166uptr internal_read(fd_t fd, void *buf, uptr count) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000167 UNIMPLEMENTED();
Dmitry Vyukov3c5c9e72012-06-29 18:37:45 +0000168 return 0;
Alexey Samsonov03c8b842012-06-05 08:32:53 +0000169}
170
171uptr internal_write(fd_t fd, const void *buf, uptr count) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000172 if (fd != 2)
173 UNIMPLEMENTED();
Alexey Samsonov03c8b842012-06-05 08:32:53 +0000174 HANDLE err = GetStdHandle(STD_ERROR_HANDLE);
175 if (err == 0)
176 return 0; // FIXME: this might not work on some apps.
177 DWORD ret;
178 if (!WriteFile(err, buf, count, &ret, 0))
179 return 0;
180 return ret;
181}
182
Alexey Samsonovca2b5d72012-06-06 07:30:33 +0000183uptr internal_filesize(fd_t fd) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000184 UNIMPLEMENTED();
Dmitry Vyukov3c5c9e72012-06-29 18:37:45 +0000185 return 0;
Alexey Samsonovca2b5d72012-06-06 07:30:33 +0000186}
187
188int internal_dup2(int oldfd, int newfd) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000189 UNIMPLEMENTED();
Dmitry Vyukov3c5c9e72012-06-29 18:37:45 +0000190 return 0;
Alexey Samsonovca2b5d72012-06-06 07:30:33 +0000191}
192
Alexey Samsonov58a3c582012-06-18 08:44:30 +0000193int internal_sched_yield() {
194 UNIMPLEMENTED();
Dmitry Vyukov3c5c9e72012-06-29 18:37:45 +0000195 return 0;
Alexey Samsonov58a3c582012-06-18 08:44:30 +0000196}
197
Alexey Samsonovdde1f112012-06-05 07:05:10 +0000198} // namespace __sanitizer
199
200#endif // _WIN32