blob: 9f6f4f022c361cb9768bd38dae8861df7f7c0192 [file] [log] [blame]
Alexey Samsonov7f9c5a22012-06-05 07:46:31 +00001//===-- sanitizer_win.cc --------------------------------------------------===//
Alexey Samsonovc5d46512012-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 Vyukov33b2a942012-11-06 13:19:59 +000015#define WIN32_LEAN_AND_MEAN
16#define NOGDI
Alexey Samsonove43542a2012-11-08 13:08:41 +000017#include <stdlib.h>
Alexey Samsonovc5d46512012-06-05 07:05:10 +000018#include <windows.h>
19
Alexey Samsonov9929ffd2012-06-06 09:43:32 +000020#include "sanitizer_common.h"
Alexey Samsonovc5d46512012-06-05 07:05:10 +000021#include "sanitizer_libc.h"
22
Alexey Samsonovc5d46512012-06-05 07:05:10 +000023namespace __sanitizer {
24
Alexey Samsonove5931fd2012-06-07 07:13:46 +000025// --------------------- sanitizer_common.h
Kostya Serebryanyf67ec2b2012-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 Samsonov93b4caf2012-11-09 14:45:30 +000034bool FileExists(const char *filename) {
35 UNIMPLEMENTED();
36}
37
Alexey Samsonov230c3be2012-06-06 09:26:25 +000038int GetPid() {
39 return GetProcessId(GetCurrentProcess());
40}
41
Alexey Samsonovfa3daaf2012-06-15 06:37:34 +000042uptr GetThreadSelf() {
43 return GetCurrentThreadId();
44}
45
Alexey Samsonoved996f72012-06-07 07:32:00 +000046void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
Alexey Samsonove5931fd2012-06-07 07:13:46 +000047 uptr *stack_bottom) {
48 CHECK(stack_top);
49 CHECK(stack_bottom);
50 MEMORY_BASIC_INFORMATION mbi;
Kostya Serebryany69850852012-06-20 15:19:17 +000051 CHECK_NE(VirtualQuery(&mbi /* on stack */, &mbi, sizeof(mbi)), 0);
Alexey Samsonove5931fd2012-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 Samsonova25b3462012-06-06 16:15:07 +000059void *MmapOrDie(uptr size, const char *mem_type) {
Alexey Samsonov230c3be2012-06-06 09:26:25 +000060 void *rv = VirtualAlloc(0, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
Alexey Samsonova25b3462012-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 Samsonov230c3be2012-06-06 09:26:25 +000066 return rv;
67}
68
69void UnmapOrDie(void *addr, uptr size) {
Alexey Samsonov8c53e542012-06-06 15:47:40 +000070 if (VirtualFree(addr, size, MEM_DECOMMIT) == 0) {
Alexey Samsonova25b3462012-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 Samsonov8c53e542012-06-06 15:47:40 +000074 }
Alexey Samsonov230c3be2012-06-06 09:26:25 +000075}
76
Alexey Samsonovf607fc12012-06-14 14:42:58 +000077void *MmapFixedNoReserve(uptr fixed_addr, uptr size) {
Kostya Serebryany9bfe78f2012-12-13 05:36:00 +000078 // FIXME: is this really "NoReserve"? On Win32 this does not matter much,
79 // but on Win64 it does.
Dmitry Vyukov33b2a942012-11-06 13:19:59 +000080 void *p = VirtualAlloc((LPVOID)fixed_addr, size,
81 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
82 if (p == 0)
83 Report("ERROR: Failed to allocate 0x%zx (%zd) bytes at %p (%d)\n",
84 size, size, fixed_addr, GetLastError());
85 return p;
Alexey Samsonovf607fc12012-06-14 14:42:58 +000086}
87
Kostya Serebryany9bfe78f2012-12-13 05:36:00 +000088void *MmapFixedOrDie(uptr fixed_addr, uptr size) {
89 return MmapFixedOrDie(fixed_addr, size);
90}
91
Alexey Samsonovf607fc12012-06-14 14:42:58 +000092void *Mprotect(uptr fixed_addr, uptr size) {
93 return VirtualAlloc((LPVOID)fixed_addr, size,
94 MEM_RESERVE | MEM_COMMIT, PAGE_NOACCESS);
95}
96
Alexey Samsonovdd3a9112012-06-15 07:29:14 +000097bool MemoryRangeIsAvailable(uptr range_start, uptr range_end) {
98 // FIXME: shall we do anything here on Windows?
99 return true;
100}
101
Alexey Samsonova68633f2012-07-03 08:24:14 +0000102void *MapFileToMemory(const char *file_name, uptr *buff_size) {
103 UNIMPLEMENTED();
104}
105
Alexey Samsonov3dbeabb2012-06-14 14:07:21 +0000106const char *GetEnv(const char *name) {
107 static char env_buffer[32767] = {};
108
109 // Note: this implementation stores the result in a static buffer so we only
110 // allow it to be called just once.
111 static bool called_once = false;
112 if (called_once)
113 UNIMPLEMENTED();
114 called_once = true;
115
116 DWORD rv = GetEnvironmentVariableA(name, env_buffer, sizeof(env_buffer));
117 if (rv > 0 && rv < sizeof(env_buffer))
118 return env_buffer;
119 return 0;
120}
121
Alexey Samsonov0969bcf2012-06-18 08:44:30 +0000122const char *GetPwd() {
123 UNIMPLEMENTED();
124}
125
Alexey Samsonovbe7420c2012-06-15 06:08:19 +0000126void DumpProcessMap() {
127 UNIMPLEMENTED();
128}
129
130void DisableCoreDumper() {
131 UNIMPLEMENTED();
132}
133
Alexey Samsonovd7e5bb42012-09-17 09:12:39 +0000134void ReExec() {
135 UNIMPLEMENTED();
136}
137
Alexander Potapenko25742572012-12-10 13:10:40 +0000138void PrepareForSandboxing() {
139 // Nothing here for now.
140}
141
Alexey Samsonovd7e5bb42012-09-17 09:12:39 +0000142bool StackSizeIsUnlimited() {
143 UNIMPLEMENTED();
Alexey Samsonovd7e5bb42012-09-17 09:12:39 +0000144}
145
146void SetStackSizeLimitInBytes(uptr limit) {
147 UNIMPLEMENTED();
148}
149
Alexey Samsonovfa3daaf2012-06-15 06:37:34 +0000150void SleepForSeconds(int seconds) {
151 Sleep(seconds * 1000);
152}
153
Alexey Samsonov0969bcf2012-06-18 08:44:30 +0000154void SleepForMillis(int millis) {
155 Sleep(millis);
156}
157
Alexey Samsonovfa3daaf2012-06-15 06:37:34 +0000158void Exit(int exitcode) {
159 _exit(exitcode);
160}
161
162void Abort() {
163 abort();
164 _exit(-1); // abort is not NORETURN on Windows.
165}
166
Dmitry Vyukov33b2a942012-11-06 13:19:59 +0000167#ifndef SANITIZER_GO
Alexey Samsonovfa3daaf2012-06-15 06:37:34 +0000168int Atexit(void (*function)(void)) {
169 return atexit(function);
170}
Dmitry Vyukov33b2a942012-11-06 13:19:59 +0000171#endif
Alexey Samsonovfa3daaf2012-06-15 06:37:34 +0000172
Alexey Samsonove5931fd2012-06-07 07:13:46 +0000173// ------------------ sanitizer_libc.h
Alexey Samsonovc5d46512012-06-05 07:05:10 +0000174void *internal_mmap(void *addr, uptr length, int prot, int flags,
175 int fd, u64 offset) {
Alexey Samsonov8c53e542012-06-06 15:47:40 +0000176 UNIMPLEMENTED();
Alexey Samsonovc5d46512012-06-05 07:05:10 +0000177}
178
Alexey Samsonov1f11d312012-06-05 09:49:25 +0000179int internal_munmap(void *addr, uptr length) {
Alexey Samsonov8c53e542012-06-06 15:47:40 +0000180 UNIMPLEMENTED();
Alexey Samsonov1f11d312012-06-05 09:49:25 +0000181}
182
Alexey Samsonova56aefd2012-06-05 08:32:53 +0000183int internal_close(fd_t fd) {
Alexey Samsonov8c53e542012-06-06 15:47:40 +0000184 UNIMPLEMENTED();
Alexey Samsonova56aefd2012-06-05 08:32:53 +0000185}
186
Alexey Samsonov84d57b42012-11-02 15:18:34 +0000187int internal_isatty(fd_t fd) {
188 UNIMPLEMENTED();
189}
190
Alexey Samsonovc5d46512012-06-05 07:05:10 +0000191fd_t internal_open(const char *filename, bool write) {
Alexey Samsonov8c53e542012-06-06 15:47:40 +0000192 UNIMPLEMENTED();
Alexey Samsonovc5d46512012-06-05 07:05:10 +0000193}
194
Alexey Samsonova56aefd2012-06-05 08:32:53 +0000195uptr internal_read(fd_t fd, void *buf, uptr count) {
Alexey Samsonov8c53e542012-06-06 15:47:40 +0000196 UNIMPLEMENTED();
Alexey Samsonova56aefd2012-06-05 08:32:53 +0000197}
198
199uptr internal_write(fd_t fd, const void *buf, uptr count) {
Alexey Samsonovf3457cb2012-11-02 09:38:47 +0000200 if (fd != kStderrFd)
Alexey Samsonov8c53e542012-06-06 15:47:40 +0000201 UNIMPLEMENTED();
Alexey Samsonova56aefd2012-06-05 08:32:53 +0000202 HANDLE err = GetStdHandle(STD_ERROR_HANDLE);
203 if (err == 0)
204 return 0; // FIXME: this might not work on some apps.
205 DWORD ret;
206 if (!WriteFile(err, buf, count, &ret, 0))
207 return 0;
208 return ret;
209}
210
Alexey Samsonov8e820fc2012-06-06 07:30:33 +0000211uptr internal_filesize(fd_t fd) {
Alexey Samsonov8c53e542012-06-06 15:47:40 +0000212 UNIMPLEMENTED();
Alexey Samsonov8e820fc2012-06-06 07:30:33 +0000213}
214
215int internal_dup2(int oldfd, int newfd) {
Alexey Samsonov8c53e542012-06-06 15:47:40 +0000216 UNIMPLEMENTED();
Alexey Samsonov8e820fc2012-06-06 07:30:33 +0000217}
218
Alexey Samsonovd1b8f582012-09-05 14:48:24 +0000219uptr internal_readlink(const char *path, char *buf, uptr bufsize) {
220 UNIMPLEMENTED();
Alexey Samsonovd1b8f582012-09-05 14:48:24 +0000221}
222
Alexey Samsonov0969bcf2012-06-18 08:44:30 +0000223int internal_sched_yield() {
Dmitry Vyukov33b2a942012-11-06 13:19:59 +0000224 Sleep(0);
225 return 0;
Alexey Samsonov0969bcf2012-06-18 08:44:30 +0000226}
227
Alexey Samsonovc5d46512012-06-05 07:05:10 +0000228} // namespace __sanitizer
229
230#endif // _WIN32