blob: 6166f150b22bbc7310fcf4886806fc4523223cea [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 Samsonov0c53a382012-06-14 14:07:21 +000059const char *GetEnv(const char *name) {
60 static char env_buffer[32767] = {};
61
62 // Note: this implementation stores the result in a static buffer so we only
63 // allow it to be called just once.
64 static bool called_once = false;
65 if (called_once)
66 UNIMPLEMENTED();
67 called_once = true;
68
69 DWORD rv = GetEnvironmentVariableA(name, env_buffer, sizeof(env_buffer));
70 if (rv > 0 && rv < sizeof(env_buffer))
71 return env_buffer;
72 return 0;
73}
74
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000075// ------------------ sanitizer_libc.h
Alexey Samsonovdde1f112012-06-05 07:05:10 +000076void *internal_mmap(void *addr, uptr length, int prot, int flags,
77 int fd, u64 offset) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +000078 UNIMPLEMENTED();
Alexey Samsonovdde1f112012-06-05 07:05:10 +000079}
80
Alexey Samsonov7ac77d62012-06-05 09:49:25 +000081int internal_munmap(void *addr, uptr length) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +000082 UNIMPLEMENTED();
Alexey Samsonov7ac77d62012-06-05 09:49:25 +000083}
84
Alexey Samsonov03c8b842012-06-05 08:32:53 +000085int internal_close(fd_t fd) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +000086 UNIMPLEMENTED();
Alexey Samsonov03c8b842012-06-05 08:32:53 +000087}
88
Alexey Samsonovdde1f112012-06-05 07:05:10 +000089fd_t internal_open(const char *filename, bool write) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +000090 UNIMPLEMENTED();
Alexey Samsonovdde1f112012-06-05 07:05:10 +000091}
92
Alexey Samsonov03c8b842012-06-05 08:32:53 +000093uptr internal_read(fd_t fd, void *buf, uptr count) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +000094 UNIMPLEMENTED();
Alexey Samsonov03c8b842012-06-05 08:32:53 +000095}
96
97uptr internal_write(fd_t fd, const void *buf, uptr count) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +000098 if (fd != 2)
99 UNIMPLEMENTED();
Alexey Samsonov03c8b842012-06-05 08:32:53 +0000100 HANDLE err = GetStdHandle(STD_ERROR_HANDLE);
101 if (err == 0)
102 return 0; // FIXME: this might not work on some apps.
103 DWORD ret;
104 if (!WriteFile(err, buf, count, &ret, 0))
105 return 0;
106 return ret;
107}
108
Alexey Samsonovca2b5d72012-06-06 07:30:33 +0000109uptr internal_filesize(fd_t fd) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000110 UNIMPLEMENTED();
Alexey Samsonovca2b5d72012-06-06 07:30:33 +0000111}
112
113int internal_dup2(int oldfd, int newfd) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000114 UNIMPLEMENTED();
Alexey Samsonovca2b5d72012-06-06 07:30:33 +0000115}
116
Alexey Samsonov7ac77d62012-06-05 09:49:25 +0000117int internal_sscanf(const char *str, const char *format, ...) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000118 UNIMPLEMENTED();
Alexey Samsonov7ac77d62012-06-05 09:49:25 +0000119}
120
Alexey Samsonovdde1f112012-06-05 07:05:10 +0000121} // namespace __sanitizer
122
123#endif // _WIN32