Alexey Samsonov | 298dd7c | 2012-06-05 07:46:31 +0000 | [diff] [blame] | 1 | //===-- sanitizer_win.cc --------------------------------------------------===// |
Alexey Samsonov | dde1f11 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 2 | // |
| 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 Samsonov | 201aa36 | 2012-06-06 09:43:32 +0000 | [diff] [blame] | 17 | #include "sanitizer_common.h" |
Alexey Samsonov | dde1f11 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 18 | #include "sanitizer_libc.h" |
| 19 | |
Alexey Samsonov | dde1f11 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 20 | namespace __sanitizer { |
| 21 | |
Alexey Samsonov | 4b1f103 | 2012-06-07 07:13:46 +0000 | [diff] [blame^] | 22 | // --------------------- sanitizer_common.h |
Alexey Samsonov | ee07290 | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 23 | int GetPid() { |
| 24 | return GetProcessId(GetCurrentProcess()); |
| 25 | } |
| 26 | |
Alexey Samsonov | 4b1f103 | 2012-06-07 07:13:46 +0000 | [diff] [blame^] | 27 | void 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 Samsonov | 40d5b77 | 2012-06-06 16:15:07 +0000 | [diff] [blame] | 41 | void *MmapOrDie(uptr size, const char *mem_type) { |
Alexey Samsonov | ee07290 | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 42 | void *rv = VirtualAlloc(0, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); |
Alexey Samsonov | 40d5b77 | 2012-06-06 16:15:07 +0000 | [diff] [blame] | 43 | 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 Samsonov | ee07290 | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 48 | return rv; |
| 49 | } |
| 50 | |
| 51 | void UnmapOrDie(void *addr, uptr size) { |
Alexey Samsonov | e95e29c | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 52 | if (VirtualFree(addr, size, MEM_DECOMMIT) == 0) { |
Alexey Samsonov | 40d5b77 | 2012-06-06 16:15:07 +0000 | [diff] [blame] | 53 | Report("ERROR: Failed to deallocate 0x%zx (%zd) bytes at address %p\n", |
| 54 | size, size, addr); |
| 55 | CHECK("unable to unmap" && 0); |
Alexey Samsonov | e95e29c | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 56 | } |
Alexey Samsonov | ee07290 | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Alexey Samsonov | 4b1f103 | 2012-06-07 07:13:46 +0000 | [diff] [blame^] | 59 | // ------------------ sanitizer_libc.h |
Alexey Samsonov | dde1f11 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 60 | void *internal_mmap(void *addr, uptr length, int prot, int flags, |
| 61 | int fd, u64 offset) { |
Alexey Samsonov | e95e29c | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 62 | UNIMPLEMENTED(); |
Alexey Samsonov | dde1f11 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Alexey Samsonov | 7ac77d6 | 2012-06-05 09:49:25 +0000 | [diff] [blame] | 65 | int internal_munmap(void *addr, uptr length) { |
Alexey Samsonov | e95e29c | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 66 | UNIMPLEMENTED(); |
Alexey Samsonov | 7ac77d6 | 2012-06-05 09:49:25 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Alexey Samsonov | 03c8b84 | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 69 | int internal_close(fd_t fd) { |
Alexey Samsonov | e95e29c | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 70 | UNIMPLEMENTED(); |
Alexey Samsonov | 03c8b84 | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Alexey Samsonov | dde1f11 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 73 | fd_t internal_open(const char *filename, bool write) { |
Alexey Samsonov | e95e29c | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 74 | UNIMPLEMENTED(); |
Alexey Samsonov | dde1f11 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Alexey Samsonov | 03c8b84 | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 77 | uptr internal_read(fd_t fd, void *buf, uptr count) { |
Alexey Samsonov | e95e29c | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 78 | UNIMPLEMENTED(); |
Alexey Samsonov | 03c8b84 | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | uptr internal_write(fd_t fd, const void *buf, uptr count) { |
Alexey Samsonov | e95e29c | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 82 | if (fd != 2) |
| 83 | UNIMPLEMENTED(); |
Alexey Samsonov | 03c8b84 | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 84 | 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 Samsonov | ca2b5d7 | 2012-06-06 07:30:33 +0000 | [diff] [blame] | 93 | uptr internal_filesize(fd_t fd) { |
Alexey Samsonov | e95e29c | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 94 | UNIMPLEMENTED(); |
Alexey Samsonov | ca2b5d7 | 2012-06-06 07:30:33 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | int internal_dup2(int oldfd, int newfd) { |
Alexey Samsonov | e95e29c | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 98 | UNIMPLEMENTED(); |
Alexey Samsonov | ca2b5d7 | 2012-06-06 07:30:33 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Alexey Samsonov | 7ac77d6 | 2012-06-05 09:49:25 +0000 | [diff] [blame] | 101 | int internal_sscanf(const char *str, const char *format, ...) { |
Alexey Samsonov | e95e29c | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 102 | UNIMPLEMENTED(); |
Alexey Samsonov | 7ac77d6 | 2012-06-05 09:49:25 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Alexey Samsonov | dde1f11 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 105 | } // namespace __sanitizer |
| 106 | |
| 107 | #endif // _WIN32 |