blob: 6c8ed41ab56978f3d9cafd877453bb73d81bf26c [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 Samsonov4b1f1032012-06-07 07:13:46 +000027void GetThreadStackTopAndBottom(bool is_main_thread, uptr *stack_top,
28 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 Samsonov4b1f1032012-06-07 07:13:46 +000059// ------------------ sanitizer_libc.h
Alexey Samsonovdde1f112012-06-05 07:05:10 +000060void *internal_mmap(void *addr, uptr length, int prot, int flags,
61 int fd, u64 offset) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +000062 UNIMPLEMENTED();
Alexey Samsonovdde1f112012-06-05 07:05:10 +000063}
64
Alexey Samsonov7ac77d62012-06-05 09:49:25 +000065int internal_munmap(void *addr, uptr length) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +000066 UNIMPLEMENTED();
Alexey Samsonov7ac77d62012-06-05 09:49:25 +000067}
68
Alexey Samsonov03c8b842012-06-05 08:32:53 +000069int internal_close(fd_t fd) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +000070 UNIMPLEMENTED();
Alexey Samsonov03c8b842012-06-05 08:32:53 +000071}
72
Alexey Samsonovdde1f112012-06-05 07:05:10 +000073fd_t internal_open(const char *filename, bool write) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +000074 UNIMPLEMENTED();
Alexey Samsonovdde1f112012-06-05 07:05:10 +000075}
76
Alexey Samsonov03c8b842012-06-05 08:32:53 +000077uptr internal_read(fd_t fd, void *buf, uptr count) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +000078 UNIMPLEMENTED();
Alexey Samsonov03c8b842012-06-05 08:32:53 +000079}
80
81uptr internal_write(fd_t fd, const void *buf, uptr count) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +000082 if (fd != 2)
83 UNIMPLEMENTED();
Alexey Samsonov03c8b842012-06-05 08:32:53 +000084 HANDLE err = GetStdHandle(STD_ERROR_HANDLE);
85 if (err == 0)
86 return 0; // FIXME: this might not work on some apps.
87 DWORD ret;
88 if (!WriteFile(err, buf, count, &ret, 0))
89 return 0;
90 return ret;
91}
92
Alexey Samsonovca2b5d72012-06-06 07:30:33 +000093uptr internal_filesize(fd_t fd) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +000094 UNIMPLEMENTED();
Alexey Samsonovca2b5d72012-06-06 07:30:33 +000095}
96
97int internal_dup2(int oldfd, int newfd) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +000098 UNIMPLEMENTED();
Alexey Samsonovca2b5d72012-06-06 07:30:33 +000099}
100
Alexey Samsonov7ac77d62012-06-05 09:49:25 +0000101int internal_sscanf(const char *str, const char *format, ...) {
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000102 UNIMPLEMENTED();
Alexey Samsonov7ac77d62012-06-05 09:49:25 +0000103}
104
Alexey Samsonovdde1f112012-06-05 07:05:10 +0000105} // namespace __sanitizer
106
107#endif // _WIN32