blob: 907936f7d263edbd6201a6ac4089cd58591e4e40 [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 Samsonovee072902012-06-06 09:26:25 +000026int GetPid() {
27 return GetProcessId(GetCurrentProcess());
28}
29
Alexey Samsonov70afb912012-06-15 06:37:34 +000030uptr GetThreadSelf() {
31 return GetCurrentThreadId();
32}
33
Alexey Samsonovcf4d3a02012-06-07 07:32:00 +000034void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000035 uptr *stack_bottom) {
36 CHECK(stack_top);
37 CHECK(stack_bottom);
38 MEMORY_BASIC_INFORMATION mbi;
Kostya Serebryany98390d02012-06-20 15:19:17 +000039 CHECK_NE(VirtualQuery(&mbi /* on stack */, &mbi, sizeof(mbi)), 0);
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000040 // FIXME: is it possible for the stack to not be a single allocation?
41 // Are these values what ASan expects to get (reserved, not committed;
42 // including stack guard page) ?
43 *stack_top = (uptr)mbi.BaseAddress + mbi.RegionSize;
44 *stack_bottom = (uptr)mbi.AllocationBase;
45}
46
Alexey Samsonov40d5b772012-06-06 16:15:07 +000047void *MmapOrDie(uptr size, const char *mem_type) {
Alexey Samsonovee072902012-06-06 09:26:25 +000048 void *rv = VirtualAlloc(0, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
Alexey Samsonov40d5b772012-06-06 16:15:07 +000049 if (rv == 0) {
50 Report("ERROR: Failed to allocate 0x%zx (%zd) bytes of %s\n",
51 size, size, mem_type);
52 CHECK("unable to mmap" && 0);
53 }
Alexey Samsonovee072902012-06-06 09:26:25 +000054 return rv;
55}
56
57void UnmapOrDie(void *addr, uptr size) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +000058 if (VirtualFree(addr, size, MEM_DECOMMIT) == 0) {
Alexey Samsonov40d5b772012-06-06 16:15:07 +000059 Report("ERROR: Failed to deallocate 0x%zx (%zd) bytes at address %p\n",
60 size, size, addr);
61 CHECK("unable to unmap" && 0);
Alexey Samsonove95e29c2012-06-06 15:47:40 +000062 }
Alexey Samsonovee072902012-06-06 09:26:25 +000063}
64
Alexey Samsonovc70d1082012-06-14 14:42:58 +000065void *MmapFixedNoReserve(uptr fixed_addr, uptr size) {
Dmitry Vyukov0ff6d2d2012-11-06 13:19:59 +000066 void *p = VirtualAlloc((LPVOID)fixed_addr, size,
67 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
68 if (p == 0)
69 Report("ERROR: Failed to allocate 0x%zx (%zd) bytes at %p (%d)\n",
70 size, size, fixed_addr, GetLastError());
71 return p;
Alexey Samsonovc70d1082012-06-14 14:42:58 +000072}
73
74void *Mprotect(uptr fixed_addr, uptr size) {
75 return VirtualAlloc((LPVOID)fixed_addr, size,
76 MEM_RESERVE | MEM_COMMIT, PAGE_NOACCESS);
77}
78
Alexey Samsonov40e51282012-06-15 07:29:14 +000079bool MemoryRangeIsAvailable(uptr range_start, uptr range_end) {
80 // FIXME: shall we do anything here on Windows?
81 return true;
82}
83
Alexey Samsonov961276a2012-07-03 08:24:14 +000084void *MapFileToMemory(const char *file_name, uptr *buff_size) {
85 UNIMPLEMENTED();
86}
87
Alexey Samsonov0c53a382012-06-14 14:07:21 +000088const char *GetEnv(const char *name) {
89 static char env_buffer[32767] = {};
90
91 // Note: this implementation stores the result in a static buffer so we only
92 // allow it to be called just once.
93 static bool called_once = false;
94 if (called_once)
95 UNIMPLEMENTED();
96 called_once = true;
97
98 DWORD rv = GetEnvironmentVariableA(name, env_buffer, sizeof(env_buffer));
99 if (rv > 0 && rv < sizeof(env_buffer))
100 return env_buffer;
101 return 0;
102}
103
Alexey Samsonov58a3c582012-06-18 08:44:30 +0000104const char *GetPwd() {
105 UNIMPLEMENTED();
106}
107
Alexey Samsonovae1e1712012-06-15 06:08:19 +0000108void DumpProcessMap() {
109 UNIMPLEMENTED();
110}
111
112void DisableCoreDumper() {
113 UNIMPLEMENTED();
114}
115
Alexey Samsonov97ca3062012-09-17 09:12:39 +0000116void ReExec() {
117 UNIMPLEMENTED();
118}
119
120bool StackSizeIsUnlimited() {
121 UNIMPLEMENTED();
Alexey Samsonov97ca3062012-09-17 09:12:39 +0000122}
123
124void SetStackSizeLimitInBytes(uptr limit) {
125 UNIMPLEMENTED();
126}
127
Alexey Samsonov70afb912012-06-15 06:37:34 +0000128void SleepForSeconds(int seconds) {
129 Sleep(seconds * 1000);
130}
131
Alexey Samsonov58a3c582012-06-18 08:44:30 +0000132void SleepForMillis(int millis) {
133 Sleep(millis);
134}
135
Alexey Samsonov70afb912012-06-15 06:37:34 +0000136void Exit(int exitcode) {
137 _exit(exitcode);
138}
139
140void Abort() {
141 abort();
142 _exit(-1); // abort is not NORETURN on Windows.
143}
144
Dmitry Vyukov0ff6d2d2012-11-06 13:19:59 +0000145#ifndef SANITIZER_GO
Alexey Samsonov70afb912012-06-15 06:37:34 +0000146int Atexit(void (*function)(void)) {
147 return atexit(function);
148}
Dmitry Vyukov0ff6d2d2012-11-06 13:19:59 +0000149#endif
Alexey Samsonov70afb912012-06-15 06:37:34 +0000150
Alexey Samsonov4b1f1032012-06-07 07:13:46 +0000151// ------------------ sanitizer_libc.h
Alexey Samsonovdde1f112012-06-05 07:05:10 +0000152void *internal_mmap(void *addr, uptr length, int prot, int flags,
153 int fd, u64 offset) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000154 UNIMPLEMENTED();
Alexey Samsonovdde1f112012-06-05 07:05:10 +0000155}
156
Alexey Samsonov7ac77d62012-06-05 09:49:25 +0000157int internal_munmap(void *addr, uptr length) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000158 UNIMPLEMENTED();
Alexey Samsonov7ac77d62012-06-05 09:49:25 +0000159}
160
Alexey Samsonov03c8b842012-06-05 08:32:53 +0000161int internal_close(fd_t fd) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000162 UNIMPLEMENTED();
Alexey Samsonov03c8b842012-06-05 08:32:53 +0000163}
164
Alexey Samsonov58358892012-11-02 15:18:34 +0000165int internal_isatty(fd_t fd) {
166 UNIMPLEMENTED();
167}
168
Alexey Samsonovdde1f112012-06-05 07:05:10 +0000169fd_t internal_open(const char *filename, bool write) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000170 UNIMPLEMENTED();
Alexey Samsonovdde1f112012-06-05 07:05:10 +0000171}
172
Alexey Samsonov03c8b842012-06-05 08:32:53 +0000173uptr internal_read(fd_t fd, void *buf, uptr count) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000174 UNIMPLEMENTED();
Alexey Samsonov03c8b842012-06-05 08:32:53 +0000175}
176
177uptr internal_write(fd_t fd, const void *buf, uptr count) {
Alexey Samsonov20ba98f2012-11-02 09:38:47 +0000178 if (fd != kStderrFd)
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000179 UNIMPLEMENTED();
Alexey Samsonov03c8b842012-06-05 08:32:53 +0000180 HANDLE err = GetStdHandle(STD_ERROR_HANDLE);
181 if (err == 0)
182 return 0; // FIXME: this might not work on some apps.
183 DWORD ret;
184 if (!WriteFile(err, buf, count, &ret, 0))
185 return 0;
186 return ret;
187}
188
Alexey Samsonovca2b5d72012-06-06 07:30:33 +0000189uptr internal_filesize(fd_t fd) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000190 UNIMPLEMENTED();
Alexey Samsonovca2b5d72012-06-06 07:30:33 +0000191}
192
193int internal_dup2(int oldfd, int newfd) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000194 UNIMPLEMENTED();
Alexey Samsonovca2b5d72012-06-06 07:30:33 +0000195}
196
Alexey Samsonovf6d21252012-09-05 14:48:24 +0000197uptr internal_readlink(const char *path, char *buf, uptr bufsize) {
198 UNIMPLEMENTED();
Alexey Samsonovf6d21252012-09-05 14:48:24 +0000199}
200
Alexey Samsonov58a3c582012-06-18 08:44:30 +0000201int internal_sched_yield() {
Dmitry Vyukov0ff6d2d2012-11-06 13:19:59 +0000202 Sleep(0);
203 return 0;
Alexey Samsonov58a3c582012-06-18 08:44:30 +0000204}
205
Alexey Samsonovdde1f112012-06-05 07:05:10 +0000206} // namespace __sanitizer
207
208#endif // _WIN32