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 | 70afb91 | 2012-06-15 06:37:34 +0000 | [diff] [blame] | 27 | uptr GetThreadSelf() { |
| 28 | return GetCurrentThreadId(); |
| 29 | } |
| 30 | |
Alexey Samsonov | cf4d3a0 | 2012-06-07 07:32:00 +0000 | [diff] [blame] | 31 | void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top, |
Alexey Samsonov | 4b1f103 | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 32 | uptr *stack_bottom) { |
| 33 | CHECK(stack_top); |
| 34 | CHECK(stack_bottom); |
| 35 | MEMORY_BASIC_INFORMATION mbi; |
| 36 | CHECK(VirtualQuery(&mbi /* on stack */, &mbi, sizeof(mbi)) != 0); |
| 37 | // FIXME: is it possible for the stack to not be a single allocation? |
| 38 | // Are these values what ASan expects to get (reserved, not committed; |
| 39 | // including stack guard page) ? |
| 40 | *stack_top = (uptr)mbi.BaseAddress + mbi.RegionSize; |
| 41 | *stack_bottom = (uptr)mbi.AllocationBase; |
| 42 | } |
| 43 | |
| 44 | |
Alexey Samsonov | 40d5b77 | 2012-06-06 16:15:07 +0000 | [diff] [blame] | 45 | void *MmapOrDie(uptr size, const char *mem_type) { |
Alexey Samsonov | ee07290 | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 46 | void *rv = VirtualAlloc(0, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); |
Alexey Samsonov | 40d5b77 | 2012-06-06 16:15:07 +0000 | [diff] [blame] | 47 | if (rv == 0) { |
| 48 | Report("ERROR: Failed to allocate 0x%zx (%zd) bytes of %s\n", |
| 49 | size, size, mem_type); |
| 50 | CHECK("unable to mmap" && 0); |
| 51 | } |
Alexey Samsonov | ee07290 | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 52 | return rv; |
| 53 | } |
| 54 | |
| 55 | void UnmapOrDie(void *addr, uptr size) { |
Alexey Samsonov | e95e29c | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 56 | if (VirtualFree(addr, size, MEM_DECOMMIT) == 0) { |
Alexey Samsonov | 40d5b77 | 2012-06-06 16:15:07 +0000 | [diff] [blame] | 57 | Report("ERROR: Failed to deallocate 0x%zx (%zd) bytes at address %p\n", |
| 58 | size, size, addr); |
| 59 | CHECK("unable to unmap" && 0); |
Alexey Samsonov | e95e29c | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 60 | } |
Alexey Samsonov | ee07290 | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Alexey Samsonov | c70d108 | 2012-06-14 14:42:58 +0000 | [diff] [blame] | 63 | void *MmapFixedNoReserve(uptr fixed_addr, uptr size) { |
| 64 | return VirtualAlloc((LPVOID)fixed_addr, size, |
| 65 | MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); |
| 66 | } |
| 67 | |
| 68 | void *Mprotect(uptr fixed_addr, uptr size) { |
| 69 | return VirtualAlloc((LPVOID)fixed_addr, size, |
| 70 | MEM_RESERVE | MEM_COMMIT, PAGE_NOACCESS); |
| 71 | } |
| 72 | |
Alexey Samsonov | 40e5128 | 2012-06-15 07:29:14 +0000 | [diff] [blame^] | 73 | bool MemoryRangeIsAvailable(uptr range_start, uptr range_end) { |
| 74 | // FIXME: shall we do anything here on Windows? |
| 75 | return true; |
| 76 | } |
| 77 | |
Alexey Samsonov | 0c53a38 | 2012-06-14 14:07:21 +0000 | [diff] [blame] | 78 | const char *GetEnv(const char *name) { |
| 79 | static char env_buffer[32767] = {}; |
| 80 | |
| 81 | // Note: this implementation stores the result in a static buffer so we only |
| 82 | // allow it to be called just once. |
| 83 | static bool called_once = false; |
| 84 | if (called_once) |
| 85 | UNIMPLEMENTED(); |
| 86 | called_once = true; |
| 87 | |
| 88 | DWORD rv = GetEnvironmentVariableA(name, env_buffer, sizeof(env_buffer)); |
| 89 | if (rv > 0 && rv < sizeof(env_buffer)) |
| 90 | return env_buffer; |
| 91 | return 0; |
| 92 | } |
| 93 | |
Alexey Samsonov | ae1e171 | 2012-06-15 06:08:19 +0000 | [diff] [blame] | 94 | void DumpProcessMap() { |
| 95 | UNIMPLEMENTED(); |
| 96 | } |
| 97 | |
| 98 | void DisableCoreDumper() { |
| 99 | UNIMPLEMENTED(); |
| 100 | } |
| 101 | |
Alexey Samsonov | 70afb91 | 2012-06-15 06:37:34 +0000 | [diff] [blame] | 102 | void SleepForSeconds(int seconds) { |
| 103 | Sleep(seconds * 1000); |
| 104 | } |
| 105 | |
| 106 | void Exit(int exitcode) { |
| 107 | _exit(exitcode); |
| 108 | } |
| 109 | |
| 110 | void Abort() { |
| 111 | abort(); |
| 112 | _exit(-1); // abort is not NORETURN on Windows. |
| 113 | } |
| 114 | |
| 115 | int Atexit(void (*function)(void)) { |
| 116 | return atexit(function); |
| 117 | } |
| 118 | |
Alexey Samsonov | e4a8898 | 2012-06-15 07:00:31 +0000 | [diff] [blame] | 119 | int AtomicInc(int *a) { |
| 120 | return InterlockedExchangeAdd((LONG*)a, 1) + 1; |
| 121 | } |
| 122 | |
| 123 | u16 AtomicExchange(u16 *a, u16 new_val) { |
| 124 | // InterlockedExchange16 seems unavailable on some MSVS installations. |
| 125 | // Everybody stand back, I pretend to know inline assembly! |
| 126 | // FIXME: I assume VC is smart enough to save/restore eax/ecx? |
| 127 | __asm { |
| 128 | mov eax, a |
| 129 | mov cx, new_val |
| 130 | xchg [eax], cx ; NOLINT |
| 131 | mov new_val, cx |
| 132 | } |
| 133 | return new_val; |
| 134 | } |
| 135 | |
| 136 | u8 AtomicExchange(u8 *a, u8 new_val) { |
| 137 | // FIXME: can we do this with a proper xchg intrinsic? |
| 138 | u8 t = *a; |
| 139 | *a = new_val; |
| 140 | return t; |
| 141 | } |
| 142 | |
Alexey Samsonov | 4b1f103 | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 143 | // ------------------ sanitizer_libc.h |
Alexey Samsonov | dde1f11 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 144 | void *internal_mmap(void *addr, uptr length, int prot, int flags, |
| 145 | int fd, u64 offset) { |
Alexey Samsonov | e95e29c | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 146 | UNIMPLEMENTED(); |
Alexey Samsonov | dde1f11 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Alexey Samsonov | 7ac77d6 | 2012-06-05 09:49:25 +0000 | [diff] [blame] | 149 | int internal_munmap(void *addr, uptr length) { |
Alexey Samsonov | e95e29c | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 150 | UNIMPLEMENTED(); |
Alexey Samsonov | 7ac77d6 | 2012-06-05 09:49:25 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Alexey Samsonov | 03c8b84 | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 153 | int internal_close(fd_t fd) { |
Alexey Samsonov | e95e29c | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 154 | UNIMPLEMENTED(); |
Alexey Samsonov | 03c8b84 | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Alexey Samsonov | dde1f11 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 157 | fd_t internal_open(const char *filename, bool write) { |
Alexey Samsonov | e95e29c | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 158 | UNIMPLEMENTED(); |
Alexey Samsonov | dde1f11 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Alexey Samsonov | 03c8b84 | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 161 | uptr internal_read(fd_t fd, void *buf, uptr count) { |
Alexey Samsonov | e95e29c | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 162 | UNIMPLEMENTED(); |
Alexey Samsonov | 03c8b84 | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | uptr internal_write(fd_t fd, const void *buf, uptr count) { |
Alexey Samsonov | e95e29c | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 166 | if (fd != 2) |
| 167 | UNIMPLEMENTED(); |
Alexey Samsonov | 03c8b84 | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 168 | HANDLE err = GetStdHandle(STD_ERROR_HANDLE); |
| 169 | if (err == 0) |
| 170 | return 0; // FIXME: this might not work on some apps. |
| 171 | DWORD ret; |
| 172 | if (!WriteFile(err, buf, count, &ret, 0)) |
| 173 | return 0; |
| 174 | return ret; |
| 175 | } |
| 176 | |
Alexey Samsonov | ca2b5d7 | 2012-06-06 07:30:33 +0000 | [diff] [blame] | 177 | uptr internal_filesize(fd_t fd) { |
Alexey Samsonov | e95e29c | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 178 | UNIMPLEMENTED(); |
Alexey Samsonov | ca2b5d7 | 2012-06-06 07:30:33 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | int internal_dup2(int oldfd, int newfd) { |
Alexey Samsonov | e95e29c | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 182 | UNIMPLEMENTED(); |
Alexey Samsonov | ca2b5d7 | 2012-06-06 07:30:33 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Alexey Samsonov | 7ac77d6 | 2012-06-05 09:49:25 +0000 | [diff] [blame] | 185 | int internal_sscanf(const char *str, const char *format, ...) { |
Alexey Samsonov | e95e29c | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 186 | UNIMPLEMENTED(); |
Alexey Samsonov | 7ac77d6 | 2012-06-05 09:49:25 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Alexey Samsonov | dde1f11 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 189 | } // namespace __sanitizer |
| 190 | |
| 191 | #endif // _WIN32 |