Alexey Samsonov | 7f9c5a2 | 2012-06-05 07:46:31 +0000 | [diff] [blame] | 1 | //===-- sanitizer_win.cc --------------------------------------------------===// |
Alexey Samsonov | c5d4651 | 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 |
Dmitry Vyukov | 33b2a94 | 2012-11-06 13:19:59 +0000 | [diff] [blame] | 15 | #define WIN32_LEAN_AND_MEAN |
| 16 | #define NOGDI |
Alexey Samsonov | e43542a | 2012-11-08 13:08:41 +0000 | [diff] [blame] | 17 | #include <stdlib.h> |
Alexey Samsonov | c5d4651 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 18 | #include <windows.h> |
| 19 | |
Alexey Samsonov | 9929ffd | 2012-06-06 09:43:32 +0000 | [diff] [blame] | 20 | #include "sanitizer_common.h" |
Alexey Samsonov | c5d4651 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 21 | #include "sanitizer_libc.h" |
| 22 | |
Alexey Samsonov | c5d4651 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 23 | namespace __sanitizer { |
| 24 | |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 25 | // --------------------- sanitizer_common.h |
Kostya Serebryany | f67ec2b | 2012-11-23 15:38:49 +0000 | [diff] [blame] | 26 | uptr GetPageSize() { |
| 27 | return 1U << 14; // FIXME: is this configurable? |
| 28 | } |
| 29 | |
| 30 | uptr GetMmapGranularity() { |
| 31 | return 1U << 16; // FIXME: is this configurable? |
| 32 | } |
| 33 | |
Alexey Samsonov | 93b4caf | 2012-11-09 14:45:30 +0000 | [diff] [blame] | 34 | bool FileExists(const char *filename) { |
| 35 | UNIMPLEMENTED(); |
| 36 | } |
| 37 | |
Alexey Samsonov | 230c3be | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 38 | int GetPid() { |
| 39 | return GetProcessId(GetCurrentProcess()); |
| 40 | } |
| 41 | |
Alexey Samsonov | fa3daaf | 2012-06-15 06:37:34 +0000 | [diff] [blame] | 42 | uptr GetThreadSelf() { |
| 43 | return GetCurrentThreadId(); |
| 44 | } |
| 45 | |
Alexey Samsonov | ed996f7 | 2012-06-07 07:32:00 +0000 | [diff] [blame] | 46 | void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top, |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 47 | uptr *stack_bottom) { |
| 48 | CHECK(stack_top); |
| 49 | CHECK(stack_bottom); |
| 50 | MEMORY_BASIC_INFORMATION mbi; |
Kostya Serebryany | 6985085 | 2012-06-20 15:19:17 +0000 | [diff] [blame] | 51 | CHECK_NE(VirtualQuery(&mbi /* on stack */, &mbi, sizeof(mbi)), 0); |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 52 | // FIXME: is it possible for the stack to not be a single allocation? |
| 53 | // Are these values what ASan expects to get (reserved, not committed; |
| 54 | // including stack guard page) ? |
| 55 | *stack_top = (uptr)mbi.BaseAddress + mbi.RegionSize; |
| 56 | *stack_bottom = (uptr)mbi.AllocationBase; |
| 57 | } |
| 58 | |
Alexey Samsonov | a25b346 | 2012-06-06 16:15:07 +0000 | [diff] [blame] | 59 | void *MmapOrDie(uptr size, const char *mem_type) { |
Alexey Samsonov | 230c3be | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 60 | void *rv = VirtualAlloc(0, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); |
Alexey Samsonov | a25b346 | 2012-06-06 16:15:07 +0000 | [diff] [blame] | 61 | if (rv == 0) { |
| 62 | Report("ERROR: Failed to allocate 0x%zx (%zd) bytes of %s\n", |
| 63 | size, size, mem_type); |
| 64 | CHECK("unable to mmap" && 0); |
| 65 | } |
Alexey Samsonov | 230c3be | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 66 | return rv; |
| 67 | } |
| 68 | |
| 69 | void UnmapOrDie(void *addr, uptr size) { |
Alexey Samsonov | 8c53e54 | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 70 | if (VirtualFree(addr, size, MEM_DECOMMIT) == 0) { |
Alexey Samsonov | a25b346 | 2012-06-06 16:15:07 +0000 | [diff] [blame] | 71 | Report("ERROR: Failed to deallocate 0x%zx (%zd) bytes at address %p\n", |
| 72 | size, size, addr); |
| 73 | CHECK("unable to unmap" && 0); |
Alexey Samsonov | 8c53e54 | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 74 | } |
Alexey Samsonov | 230c3be | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Alexey Samsonov | f607fc1 | 2012-06-14 14:42:58 +0000 | [diff] [blame] | 77 | void *MmapFixedNoReserve(uptr fixed_addr, uptr size) { |
Kostya Serebryany | 9bfe78f | 2012-12-13 05:36:00 +0000 | [diff] [blame^] | 78 | // FIXME: is this really "NoReserve"? On Win32 this does not matter much, |
| 79 | // but on Win64 it does. |
Dmitry Vyukov | 33b2a94 | 2012-11-06 13:19:59 +0000 | [diff] [blame] | 80 | void *p = VirtualAlloc((LPVOID)fixed_addr, size, |
| 81 | MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); |
| 82 | if (p == 0) |
| 83 | Report("ERROR: Failed to allocate 0x%zx (%zd) bytes at %p (%d)\n", |
| 84 | size, size, fixed_addr, GetLastError()); |
| 85 | return p; |
Alexey Samsonov | f607fc1 | 2012-06-14 14:42:58 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Kostya Serebryany | 9bfe78f | 2012-12-13 05:36:00 +0000 | [diff] [blame^] | 88 | void *MmapFixedOrDie(uptr fixed_addr, uptr size) { |
| 89 | return MmapFixedOrDie(fixed_addr, size); |
| 90 | } |
| 91 | |
Alexey Samsonov | f607fc1 | 2012-06-14 14:42:58 +0000 | [diff] [blame] | 92 | void *Mprotect(uptr fixed_addr, uptr size) { |
| 93 | return VirtualAlloc((LPVOID)fixed_addr, size, |
| 94 | MEM_RESERVE | MEM_COMMIT, PAGE_NOACCESS); |
| 95 | } |
| 96 | |
Alexey Samsonov | dd3a911 | 2012-06-15 07:29:14 +0000 | [diff] [blame] | 97 | bool MemoryRangeIsAvailable(uptr range_start, uptr range_end) { |
| 98 | // FIXME: shall we do anything here on Windows? |
| 99 | return true; |
| 100 | } |
| 101 | |
Alexey Samsonov | a68633f | 2012-07-03 08:24:14 +0000 | [diff] [blame] | 102 | void *MapFileToMemory(const char *file_name, uptr *buff_size) { |
| 103 | UNIMPLEMENTED(); |
| 104 | } |
| 105 | |
Alexey Samsonov | 3dbeabb | 2012-06-14 14:07:21 +0000 | [diff] [blame] | 106 | const char *GetEnv(const char *name) { |
| 107 | static char env_buffer[32767] = {}; |
| 108 | |
| 109 | // Note: this implementation stores the result in a static buffer so we only |
| 110 | // allow it to be called just once. |
| 111 | static bool called_once = false; |
| 112 | if (called_once) |
| 113 | UNIMPLEMENTED(); |
| 114 | called_once = true; |
| 115 | |
| 116 | DWORD rv = GetEnvironmentVariableA(name, env_buffer, sizeof(env_buffer)); |
| 117 | if (rv > 0 && rv < sizeof(env_buffer)) |
| 118 | return env_buffer; |
| 119 | return 0; |
| 120 | } |
| 121 | |
Alexey Samsonov | 0969bcf | 2012-06-18 08:44:30 +0000 | [diff] [blame] | 122 | const char *GetPwd() { |
| 123 | UNIMPLEMENTED(); |
| 124 | } |
| 125 | |
Alexey Samsonov | be7420c | 2012-06-15 06:08:19 +0000 | [diff] [blame] | 126 | void DumpProcessMap() { |
| 127 | UNIMPLEMENTED(); |
| 128 | } |
| 129 | |
| 130 | void DisableCoreDumper() { |
| 131 | UNIMPLEMENTED(); |
| 132 | } |
| 133 | |
Alexey Samsonov | d7e5bb4 | 2012-09-17 09:12:39 +0000 | [diff] [blame] | 134 | void ReExec() { |
| 135 | UNIMPLEMENTED(); |
| 136 | } |
| 137 | |
Alexander Potapenko | 2574257 | 2012-12-10 13:10:40 +0000 | [diff] [blame] | 138 | void PrepareForSandboxing() { |
| 139 | // Nothing here for now. |
| 140 | } |
| 141 | |
Alexey Samsonov | d7e5bb4 | 2012-09-17 09:12:39 +0000 | [diff] [blame] | 142 | bool StackSizeIsUnlimited() { |
| 143 | UNIMPLEMENTED(); |
Alexey Samsonov | d7e5bb4 | 2012-09-17 09:12:39 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | void SetStackSizeLimitInBytes(uptr limit) { |
| 147 | UNIMPLEMENTED(); |
| 148 | } |
| 149 | |
Alexey Samsonov | fa3daaf | 2012-06-15 06:37:34 +0000 | [diff] [blame] | 150 | void SleepForSeconds(int seconds) { |
| 151 | Sleep(seconds * 1000); |
| 152 | } |
| 153 | |
Alexey Samsonov | 0969bcf | 2012-06-18 08:44:30 +0000 | [diff] [blame] | 154 | void SleepForMillis(int millis) { |
| 155 | Sleep(millis); |
| 156 | } |
| 157 | |
Alexey Samsonov | fa3daaf | 2012-06-15 06:37:34 +0000 | [diff] [blame] | 158 | void Exit(int exitcode) { |
| 159 | _exit(exitcode); |
| 160 | } |
| 161 | |
| 162 | void Abort() { |
| 163 | abort(); |
| 164 | _exit(-1); // abort is not NORETURN on Windows. |
| 165 | } |
| 166 | |
Dmitry Vyukov | 33b2a94 | 2012-11-06 13:19:59 +0000 | [diff] [blame] | 167 | #ifndef SANITIZER_GO |
Alexey Samsonov | fa3daaf | 2012-06-15 06:37:34 +0000 | [diff] [blame] | 168 | int Atexit(void (*function)(void)) { |
| 169 | return atexit(function); |
| 170 | } |
Dmitry Vyukov | 33b2a94 | 2012-11-06 13:19:59 +0000 | [diff] [blame] | 171 | #endif |
Alexey Samsonov | fa3daaf | 2012-06-15 06:37:34 +0000 | [diff] [blame] | 172 | |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 173 | // ------------------ sanitizer_libc.h |
Alexey Samsonov | c5d4651 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 174 | void *internal_mmap(void *addr, uptr length, int prot, int flags, |
| 175 | int fd, u64 offset) { |
Alexey Samsonov | 8c53e54 | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 176 | UNIMPLEMENTED(); |
Alexey Samsonov | c5d4651 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Alexey Samsonov | 1f11d31 | 2012-06-05 09:49:25 +0000 | [diff] [blame] | 179 | int internal_munmap(void *addr, uptr length) { |
Alexey Samsonov | 8c53e54 | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 180 | UNIMPLEMENTED(); |
Alexey Samsonov | 1f11d31 | 2012-06-05 09:49:25 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Alexey Samsonov | a56aefd | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 183 | int internal_close(fd_t fd) { |
Alexey Samsonov | 8c53e54 | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 184 | UNIMPLEMENTED(); |
Alexey Samsonov | a56aefd | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Alexey Samsonov | 84d57b4 | 2012-11-02 15:18:34 +0000 | [diff] [blame] | 187 | int internal_isatty(fd_t fd) { |
| 188 | UNIMPLEMENTED(); |
| 189 | } |
| 190 | |
Alexey Samsonov | c5d4651 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 191 | fd_t internal_open(const char *filename, bool write) { |
Alexey Samsonov | 8c53e54 | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 192 | UNIMPLEMENTED(); |
Alexey Samsonov | c5d4651 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Alexey Samsonov | a56aefd | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 195 | uptr internal_read(fd_t fd, void *buf, uptr count) { |
Alexey Samsonov | 8c53e54 | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 196 | UNIMPLEMENTED(); |
Alexey Samsonov | a56aefd | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | uptr internal_write(fd_t fd, const void *buf, uptr count) { |
Alexey Samsonov | f3457cb | 2012-11-02 09:38:47 +0000 | [diff] [blame] | 200 | if (fd != kStderrFd) |
Alexey Samsonov | 8c53e54 | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 201 | UNIMPLEMENTED(); |
Alexey Samsonov | a56aefd | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 202 | HANDLE err = GetStdHandle(STD_ERROR_HANDLE); |
| 203 | if (err == 0) |
| 204 | return 0; // FIXME: this might not work on some apps. |
| 205 | DWORD ret; |
| 206 | if (!WriteFile(err, buf, count, &ret, 0)) |
| 207 | return 0; |
| 208 | return ret; |
| 209 | } |
| 210 | |
Alexey Samsonov | 8e820fc | 2012-06-06 07:30:33 +0000 | [diff] [blame] | 211 | uptr internal_filesize(fd_t fd) { |
Alexey Samsonov | 8c53e54 | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 212 | UNIMPLEMENTED(); |
Alexey Samsonov | 8e820fc | 2012-06-06 07:30:33 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | int internal_dup2(int oldfd, int newfd) { |
Alexey Samsonov | 8c53e54 | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 216 | UNIMPLEMENTED(); |
Alexey Samsonov | 8e820fc | 2012-06-06 07:30:33 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Alexey Samsonov | d1b8f58 | 2012-09-05 14:48:24 +0000 | [diff] [blame] | 219 | uptr internal_readlink(const char *path, char *buf, uptr bufsize) { |
| 220 | UNIMPLEMENTED(); |
Alexey Samsonov | d1b8f58 | 2012-09-05 14:48:24 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Alexey Samsonov | 0969bcf | 2012-06-18 08:44:30 +0000 | [diff] [blame] | 223 | int internal_sched_yield() { |
Dmitry Vyukov | 33b2a94 | 2012-11-06 13:19:59 +0000 | [diff] [blame] | 224 | Sleep(0); |
| 225 | return 0; |
Alexey Samsonov | 0969bcf | 2012-06-18 08:44:30 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Alexey Samsonov | c5d4651 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 228 | } // namespace __sanitizer |
| 229 | |
| 230 | #endif // _WIN32 |