blob: a8c43ca4a3a7618feeae45da93aa7b622534d57e [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"
19
Alexey Samsonovdde1f112012-06-05 07:05:10 +000020namespace __sanitizer {
21
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000022// --------------------- sanitizer_common.h
Alexey Samsonovee072902012-06-06 09:26:25 +000023int GetPid() {
24 return GetProcessId(GetCurrentProcess());
25}
26
Alexey Samsonovcf4d3a02012-06-07 07:32:00 +000027void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000028 uptr *stack_bottom) {
29 CHECK(stack_top);
30 CHECK(stack_bottom);
31 MEMORY_BASIC_INFORMATION mbi;
32 CHECK(VirtualQuery(&mbi /* on stack */, &mbi, sizeof(mbi)) != 0);
33 // FIXME: is it possible for the stack to not be a single allocation?
34 // Are these values what ASan expects to get (reserved, not committed;
35 // including stack guard page) ?
36 *stack_top = (uptr)mbi.BaseAddress + mbi.RegionSize;
37 *stack_bottom = (uptr)mbi.AllocationBase;
38}
39
40
Alexey Samsonov40d5b772012-06-06 16:15:07 +000041void *MmapOrDie(uptr size, const char *mem_type) {
Alexey Samsonovee072902012-06-06 09:26:25 +000042 void *rv = VirtualAlloc(0, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
Alexey Samsonov40d5b772012-06-06 16:15:07 +000043 if (rv == 0) {
44 Report("ERROR: Failed to allocate 0x%zx (%zd) bytes of %s\n",
45 size, size, mem_type);
46 CHECK("unable to mmap" && 0);
47 }
Alexey Samsonovee072902012-06-06 09:26:25 +000048 return rv;
49}
50
51void UnmapOrDie(void *addr, uptr size) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +000052 if (VirtualFree(addr, size, MEM_DECOMMIT) == 0) {
Alexey Samsonov40d5b772012-06-06 16:15:07 +000053 Report("ERROR: Failed to deallocate 0x%zx (%zd) bytes at address %p\n",
54 size, size, addr);
55 CHECK("unable to unmap" && 0);
Alexey Samsonove95e29c2012-06-06 15:47:40 +000056 }
Alexey Samsonovee072902012-06-06 09:26:25 +000057}
58
Alexey Samsonovc70d1082012-06-14 14:42:58 +000059void *MmapFixedNoReserve(uptr fixed_addr, uptr size) {
60 return VirtualAlloc((LPVOID)fixed_addr, size,
61 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
62}
63
64void *Mprotect(uptr fixed_addr, uptr size) {
65 return VirtualAlloc((LPVOID)fixed_addr, size,
66 MEM_RESERVE | MEM_COMMIT, PAGE_NOACCESS);
67}
68
Alexey Samsonov0c53a382012-06-14 14:07:21 +000069const char *GetEnv(const char *name) {
70 static char env_buffer[32767] = {};
71
72 // Note: this implementation stores the result in a static buffer so we only
73 // allow it to be called just once.
74 static bool called_once = false;
75 if (called_once)
76 UNIMPLEMENTED();
77 called_once = true;
78
79 DWORD rv = GetEnvironmentVariableA(name, env_buffer, sizeof(env_buffer));
80 if (rv > 0 && rv < sizeof(env_buffer))
81 return env_buffer;
82 return 0;
83}
84
Alexey Samsonovae1e1712012-06-15 06:08:19 +000085void DumpProcessMap() {
86 UNIMPLEMENTED();
87}
88
89void DisableCoreDumper() {
90 UNIMPLEMENTED();
91}
92
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000093// ------------------ sanitizer_libc.h
Alexey Samsonovdde1f112012-06-05 07:05:10 +000094void *internal_mmap(void *addr, uptr length, int prot, int flags,
95 int fd, u64 offset) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +000096 UNIMPLEMENTED();
Alexey Samsonovdde1f112012-06-05 07:05:10 +000097}
98
Alexey Samsonov7ac77d62012-06-05 09:49:25 +000099int internal_munmap(void *addr, uptr length) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000100 UNIMPLEMENTED();
Alexey Samsonov7ac77d62012-06-05 09:49:25 +0000101}
102
Alexey Samsonov03c8b842012-06-05 08:32:53 +0000103int internal_close(fd_t fd) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000104 UNIMPLEMENTED();
Alexey Samsonov03c8b842012-06-05 08:32:53 +0000105}
106
Alexey Samsonovdde1f112012-06-05 07:05:10 +0000107fd_t internal_open(const char *filename, bool write) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000108 UNIMPLEMENTED();
Alexey Samsonovdde1f112012-06-05 07:05:10 +0000109}
110
Alexey Samsonov03c8b842012-06-05 08:32:53 +0000111uptr internal_read(fd_t fd, void *buf, uptr count) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000112 UNIMPLEMENTED();
Alexey Samsonov03c8b842012-06-05 08:32:53 +0000113}
114
115uptr internal_write(fd_t fd, const void *buf, uptr count) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000116 if (fd != 2)
117 UNIMPLEMENTED();
Alexey Samsonov03c8b842012-06-05 08:32:53 +0000118 HANDLE err = GetStdHandle(STD_ERROR_HANDLE);
119 if (err == 0)
120 return 0; // FIXME: this might not work on some apps.
121 DWORD ret;
122 if (!WriteFile(err, buf, count, &ret, 0))
123 return 0;
124 return ret;
125}
126
Alexey Samsonovca2b5d72012-06-06 07:30:33 +0000127uptr internal_filesize(fd_t fd) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000128 UNIMPLEMENTED();
Alexey Samsonovca2b5d72012-06-06 07:30:33 +0000129}
130
131int internal_dup2(int oldfd, int newfd) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000132 UNIMPLEMENTED();
Alexey Samsonovca2b5d72012-06-06 07:30:33 +0000133}
134
Alexey Samsonov7ac77d62012-06-05 09:49:25 +0000135int internal_sscanf(const char *str, const char *format, ...) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000136 UNIMPLEMENTED();
Alexey Samsonov7ac77d62012-06-05 09:49:25 +0000137}
138
Alexey Samsonovdde1f112012-06-05 07:05:10 +0000139} // namespace __sanitizer
140
141#endif // _WIN32