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 | //===----------------------------------------------------------------------===// |
Evgeniy Stepanov | 24e1372 | 2013-03-19 14:33:38 +0000 | [diff] [blame] | 14 | |
| 15 | #include "sanitizer_platform.h" |
| 16 | #if SANITIZER_WINDOWS |
| 17 | |
Dmitry Vyukov | 33b2a94 | 2012-11-06 13:19:59 +0000 | [diff] [blame] | 18 | #define WIN32_LEAN_AND_MEAN |
| 19 | #define NOGDI |
Alexey Samsonov | c5d4651 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 20 | #include <windows.h> |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 21 | #include <dbghelp.h> |
| 22 | #include <io.h> |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame^] | 23 | #include <psapi.h> |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 24 | #include <stdlib.h> |
Alexey Samsonov | c5d4651 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 25 | |
Alexey Samsonov | 9929ffd | 2012-06-06 09:43:32 +0000 | [diff] [blame] | 26 | #include "sanitizer_common.h" |
Alexey Samsonov | c5d4651 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 27 | #include "sanitizer_libc.h" |
Dmitry Vyukov | 53833ea | 2013-01-14 14:28:06 +0000 | [diff] [blame] | 28 | #include "sanitizer_mutex.h" |
Sergey Matveev | 736cf49 | 2013-05-08 12:45:55 +0000 | [diff] [blame] | 29 | #include "sanitizer_placement_new.h" |
| 30 | #include "sanitizer_stacktrace.h" |
Alexey Samsonov | c5d4651 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 31 | |
Alexey Samsonov | c5d4651 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 32 | namespace __sanitizer { |
| 33 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 34 | #include "sanitizer_syscall_generic.inc" |
| 35 | |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 36 | // --------------------- sanitizer_common.h |
Kostya Serebryany | f67ec2b | 2012-11-23 15:38:49 +0000 | [diff] [blame] | 37 | uptr GetPageSize() { |
| 38 | return 1U << 14; // FIXME: is this configurable? |
| 39 | } |
| 40 | |
| 41 | uptr GetMmapGranularity() { |
| 42 | return 1U << 16; // FIXME: is this configurable? |
| 43 | } |
| 44 | |
Timur Iskhodzhanov | bb7f2d8 | 2013-07-16 09:47:39 +0000 | [diff] [blame] | 45 | uptr GetMaxVirtualAddress() { |
| 46 | SYSTEM_INFO si; |
| 47 | GetSystemInfo(&si); |
| 48 | return (uptr)si.lpMaximumApplicationAddress; |
| 49 | } |
| 50 | |
Alexey Samsonov | 93b4caf | 2012-11-09 14:45:30 +0000 | [diff] [blame] | 51 | bool FileExists(const char *filename) { |
| 52 | UNIMPLEMENTED(); |
| 53 | } |
| 54 | |
Peter Collingbourne | 0b694fc | 2013-05-17 16:56:53 +0000 | [diff] [blame] | 55 | uptr internal_getpid() { |
Alexey Samsonov | 230c3be | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 56 | return GetProcessId(GetCurrentProcess()); |
| 57 | } |
| 58 | |
Timur Iskhodzhanov | 2c96987 | 2013-03-25 22:04:29 +0000 | [diff] [blame] | 59 | // In contrast to POSIX, on Windows GetCurrentThreadId() |
| 60 | // returns a system-unique identifier. |
| 61 | uptr GetTid() { |
Alexey Samsonov | fa3daaf | 2012-06-15 06:37:34 +0000 | [diff] [blame] | 62 | return GetCurrentThreadId(); |
| 63 | } |
| 64 | |
Timur Iskhodzhanov | 2c96987 | 2013-03-25 22:04:29 +0000 | [diff] [blame] | 65 | uptr GetThreadSelf() { |
| 66 | return GetTid(); |
| 67 | } |
| 68 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 69 | #if !SANITIZER_GO |
Alexey Samsonov | ed996f7 | 2012-06-07 07:32:00 +0000 | [diff] [blame] | 70 | void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top, |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 71 | uptr *stack_bottom) { |
| 72 | CHECK(stack_top); |
| 73 | CHECK(stack_bottom); |
| 74 | MEMORY_BASIC_INFORMATION mbi; |
Kostya Serebryany | 6985085 | 2012-06-20 15:19:17 +0000 | [diff] [blame] | 75 | CHECK_NE(VirtualQuery(&mbi /* on stack */, &mbi, sizeof(mbi)), 0); |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 76 | // FIXME: is it possible for the stack to not be a single allocation? |
| 77 | // Are these values what ASan expects to get (reserved, not committed; |
| 78 | // including stack guard page) ? |
| 79 | *stack_top = (uptr)mbi.BaseAddress + mbi.RegionSize; |
| 80 | *stack_bottom = (uptr)mbi.AllocationBase; |
| 81 | } |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 82 | #endif // #if !SANITIZER_GO |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 83 | |
Alexey Samsonov | a25b346 | 2012-06-06 16:15:07 +0000 | [diff] [blame] | 84 | void *MmapOrDie(uptr size, const char *mem_type) { |
Alexey Samsonov | 230c3be | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 85 | void *rv = VirtualAlloc(0, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); |
Alexey Samsonov | a25b346 | 2012-06-06 16:15:07 +0000 | [diff] [blame] | 86 | if (rv == 0) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 87 | Report("ERROR: %s failed to " |
| 88 | "allocate 0x%zx (%zd) bytes of %s (error code: %d)\n", |
| 89 | SanitizerToolName, size, size, mem_type, GetLastError()); |
Alexey Samsonov | a25b346 | 2012-06-06 16:15:07 +0000 | [diff] [blame] | 90 | CHECK("unable to mmap" && 0); |
| 91 | } |
Alexey Samsonov | 230c3be | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 92 | return rv; |
| 93 | } |
| 94 | |
| 95 | void UnmapOrDie(void *addr, uptr size) { |
Alexey Samsonov | 8c53e54 | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 96 | if (VirtualFree(addr, size, MEM_DECOMMIT) == 0) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 97 | Report("ERROR: %s failed to " |
| 98 | "deallocate 0x%zx (%zd) bytes at address %p (error code: %d)\n", |
| 99 | SanitizerToolName, size, size, addr, GetLastError()); |
Alexey Samsonov | a25b346 | 2012-06-06 16:15:07 +0000 | [diff] [blame] | 100 | CHECK("unable to unmap" && 0); |
Alexey Samsonov | 8c53e54 | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 101 | } |
Alexey Samsonov | 230c3be | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Alexey Samsonov | f607fc1 | 2012-06-14 14:42:58 +0000 | [diff] [blame] | 104 | void *MmapFixedNoReserve(uptr fixed_addr, uptr size) { |
Kostya Serebryany | 9bfe78f | 2012-12-13 05:36:00 +0000 | [diff] [blame] | 105 | // FIXME: is this really "NoReserve"? On Win32 this does not matter much, |
| 106 | // but on Win64 it does. |
Dmitry Vyukov | 33b2a94 | 2012-11-06 13:19:59 +0000 | [diff] [blame] | 107 | void *p = VirtualAlloc((LPVOID)fixed_addr, size, |
| 108 | MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); |
| 109 | if (p == 0) |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 110 | Report("ERROR: %s failed to " |
| 111 | "allocate %p (%zd) bytes at %p (error code: %d)\n", |
| 112 | SanitizerToolName, size, size, fixed_addr, GetLastError()); |
Dmitry Vyukov | 33b2a94 | 2012-11-06 13:19:59 +0000 | [diff] [blame] | 113 | return p; |
Alexey Samsonov | f607fc1 | 2012-06-14 14:42:58 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Kostya Serebryany | 9bfe78f | 2012-12-13 05:36:00 +0000 | [diff] [blame] | 116 | void *MmapFixedOrDie(uptr fixed_addr, uptr size) { |
Kostya Serebryany | 8c58aa7 | 2012-12-13 05:51:02 +0000 | [diff] [blame] | 117 | return MmapFixedNoReserve(fixed_addr, size); |
Kostya Serebryany | 9bfe78f | 2012-12-13 05:36:00 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 120 | void *MmapNoReserveOrDie(uptr size, const char *mem_type) { |
| 121 | // FIXME: make this really NoReserve? |
| 122 | return MmapOrDie(size, mem_type); |
| 123 | } |
| 124 | |
Alexey Samsonov | f607fc1 | 2012-06-14 14:42:58 +0000 | [diff] [blame] | 125 | void *Mprotect(uptr fixed_addr, uptr size) { |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame^] | 126 | void *res = VirtualAlloc((LPVOID)fixed_addr, size, |
| 127 | MEM_RESERVE | MEM_COMMIT, PAGE_NOACCESS); |
| 128 | if (res == 0) |
| 129 | Report("WARNING: %s failed to " |
| 130 | "mprotect %p (%zd) bytes at %p (error code: %d)\n", |
| 131 | SanitizerToolName, size, size, fixed_addr, GetLastError()); |
| 132 | return res; |
Alexey Samsonov | f607fc1 | 2012-06-14 14:42:58 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Timur Iskhodzhanov | 2b10d39 | 2013-02-08 12:02:00 +0000 | [diff] [blame] | 135 | void FlushUnneededShadowMemory(uptr addr, uptr size) { |
| 136 | // This is almost useless on 32-bits. |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame^] | 137 | // FIXME: add madvise-analog when we move to 64-bits. |
| 138 | } |
| 139 | |
| 140 | void NoHugePagesInRegion(uptr addr, uptr size) { |
| 141 | // FIXME: probably similar to FlushUnneededShadowMemory. |
| 142 | } |
| 143 | |
| 144 | void DontDumpShadowMemory(uptr addr, uptr length) { |
| 145 | // This is almost useless on 32-bits. |
| 146 | // FIXME: add madvise-analog when we move to 64-bits. |
Timur Iskhodzhanov | 2b10d39 | 2013-02-08 12:02:00 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Alexey Samsonov | dd3a911 | 2012-06-15 07:29:14 +0000 | [diff] [blame] | 149 | bool MemoryRangeIsAvailable(uptr range_start, uptr range_end) { |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame^] | 150 | MEMORY_BASIC_INFORMATION mbi; |
| 151 | CHECK(VirtualQuery((void *)range_start, &mbi, sizeof(mbi))); |
| 152 | return mbi.Protect == PAGE_NOACCESS && |
| 153 | (uptr)mbi.BaseAddress + mbi.RegionSize >= range_end; |
Alexey Samsonov | dd3a911 | 2012-06-15 07:29:14 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Alexey Samsonov | a68633f | 2012-07-03 08:24:14 +0000 | [diff] [blame] | 156 | void *MapFileToMemory(const char *file_name, uptr *buff_size) { |
| 157 | UNIMPLEMENTED(); |
| 158 | } |
| 159 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 160 | void *MapWritableFileToMemory(void *addr, uptr size, uptr fd, uptr offset) { |
| 161 | UNIMPLEMENTED(); |
| 162 | } |
| 163 | |
Alexey Samsonov | a3ab192 | 2013-03-14 11:29:06 +0000 | [diff] [blame] | 164 | static const int kMaxEnvNameLength = 128; |
Dmitry Vyukov | c528867 | 2013-06-10 10:02:02 +0000 | [diff] [blame] | 165 | static const DWORD kMaxEnvValueLength = 32767; |
Alexey Samsonov | 1ef94b1 | 2013-03-14 11:10:23 +0000 | [diff] [blame] | 166 | |
| 167 | namespace { |
| 168 | |
| 169 | struct EnvVariable { |
| 170 | char name[kMaxEnvNameLength]; |
| 171 | char value[kMaxEnvValueLength]; |
| 172 | }; |
| 173 | |
| 174 | } // namespace |
| 175 | |
| 176 | static const int kEnvVariables = 5; |
| 177 | static EnvVariable env_vars[kEnvVariables]; |
| 178 | static int num_env_vars; |
| 179 | |
Alexey Samsonov | 3dbeabb | 2012-06-14 14:07:21 +0000 | [diff] [blame] | 180 | const char *GetEnv(const char *name) { |
Alexey Samsonov | 1ef94b1 | 2013-03-14 11:10:23 +0000 | [diff] [blame] | 181 | // Note: this implementation caches the values of the environment variables |
| 182 | // and limits their quantity. |
| 183 | for (int i = 0; i < num_env_vars; i++) { |
| 184 | if (0 == internal_strcmp(name, env_vars[i].name)) |
| 185 | return env_vars[i].value; |
| 186 | } |
| 187 | CHECK_LT(num_env_vars, kEnvVariables); |
| 188 | DWORD rv = GetEnvironmentVariableA(name, env_vars[num_env_vars].value, |
| 189 | kMaxEnvValueLength); |
| 190 | if (rv > 0 && rv < kMaxEnvValueLength) { |
| 191 | CHECK_LT(internal_strlen(name), kMaxEnvNameLength); |
| 192 | internal_strncpy(env_vars[num_env_vars].name, name, kMaxEnvNameLength); |
| 193 | num_env_vars++; |
| 194 | return env_vars[num_env_vars - 1].value; |
| 195 | } |
Alexey Samsonov | 3dbeabb | 2012-06-14 14:07:21 +0000 | [diff] [blame] | 196 | return 0; |
| 197 | } |
| 198 | |
Alexey Samsonov | 0969bcf | 2012-06-18 08:44:30 +0000 | [diff] [blame] | 199 | const char *GetPwd() { |
| 200 | UNIMPLEMENTED(); |
| 201 | } |
| 202 | |
Alexey Samsonov | 0fa691b | 2013-02-18 07:17:12 +0000 | [diff] [blame] | 203 | u32 GetUid() { |
| 204 | UNIMPLEMENTED(); |
| 205 | } |
| 206 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame^] | 207 | namespace { |
| 208 | struct ModuleInfo { |
| 209 | HMODULE handle; |
| 210 | uptr base_address; |
| 211 | uptr end_address; |
| 212 | }; |
| 213 | |
| 214 | int CompareModulesBase(const void *pl, const void *pr) { |
| 215 | const ModuleInfo &l = *(ModuleInfo *)pl, &r = *(ModuleInfo *)pr; |
| 216 | if (l.base_address < r.base_address) |
| 217 | return -1; |
| 218 | return l.base_address > r.base_address; |
Alexey Samsonov | be7420c | 2012-06-15 06:08:19 +0000 | [diff] [blame] | 219 | } |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame^] | 220 | } // namespace |
| 221 | |
| 222 | #ifndef SANITIZER_GO |
| 223 | void DumpProcessMap() { |
| 224 | Report("Dumping process modules:\n"); |
| 225 | HANDLE cur_process = GetCurrentProcess(); |
| 226 | |
| 227 | // Query the list of modules. Start by assuming there are no more than 256 |
| 228 | // modules and retry if that's not sufficient. |
| 229 | ModuleInfo *modules; |
| 230 | size_t num_modules; |
| 231 | { |
| 232 | HMODULE *hmodules = 0; |
| 233 | uptr modules_buffer_size = sizeof(HMODULE) * 256; |
| 234 | DWORD bytes_required; |
| 235 | while (!hmodules) { |
| 236 | hmodules = (HMODULE *)MmapOrDie(modules_buffer_size, __FUNCTION__); |
| 237 | CHECK(EnumProcessModules(cur_process, hmodules, modules_buffer_size, |
| 238 | &bytes_required)); |
| 239 | if (bytes_required > modules_buffer_size) { |
| 240 | // Either there turned out to be more than 256 hmodules, or new hmodules |
| 241 | // could have loaded since the last try. Retry. |
| 242 | UnmapOrDie(hmodules, modules_buffer_size); |
| 243 | hmodules = 0; |
| 244 | modules_buffer_size = bytes_required; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | num_modules = bytes_required / sizeof(HMODULE); |
| 249 | modules = |
| 250 | (ModuleInfo *)MmapOrDie(num_modules * sizeof(ModuleInfo), __FUNCTION__); |
| 251 | for (size_t i = 0; i < num_modules; ++i) { |
| 252 | modules[i].handle = hmodules[i]; |
| 253 | MODULEINFO mi; |
| 254 | if (!GetModuleInformation(cur_process, hmodules[i], &mi, sizeof(mi))) |
| 255 | continue; |
| 256 | modules[i].base_address = (uptr)mi.lpBaseOfDll; |
| 257 | modules[i].end_address = (uptr)mi.lpBaseOfDll + mi.SizeOfImage; |
| 258 | } |
| 259 | UnmapOrDie(hmodules, modules_buffer_size); |
| 260 | } |
| 261 | |
| 262 | qsort(modules, num_modules, sizeof(ModuleInfo), CompareModulesBase); |
| 263 | |
| 264 | for (size_t i = 0; i < num_modules; ++i) { |
| 265 | const ModuleInfo &mi = modules[i]; |
| 266 | char module_name[MAX_PATH]; |
| 267 | bool got_module_name = GetModuleFileNameA( |
| 268 | mi.handle, module_name, sizeof(module_name)); |
| 269 | if (mi.end_address != 0) { |
| 270 | Printf("\t%p-%p %s\n", mi.base_address, mi.end_address, |
| 271 | got_module_name ? module_name : "[no name]"); |
| 272 | } else if (got_module_name) { |
| 273 | Printf("\t??\?-??? %s\n", module_name); |
| 274 | } else { |
| 275 | Printf("\t???\n"); |
| 276 | } |
| 277 | } |
| 278 | UnmapOrDie(modules, num_modules * sizeof(ModuleInfo)); |
| 279 | } |
| 280 | #endif |
Alexey Samsonov | be7420c | 2012-06-15 06:08:19 +0000 | [diff] [blame] | 281 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 282 | void DisableCoreDumperIfNecessary() { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 283 | // Do nothing. |
Alexey Samsonov | be7420c | 2012-06-15 06:08:19 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Alexey Samsonov | d7e5bb4 | 2012-09-17 09:12:39 +0000 | [diff] [blame] | 286 | void ReExec() { |
| 287 | UNIMPLEMENTED(); |
| 288 | } |
| 289 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 290 | void PrepareForSandboxing(__sanitizer_sandbox_arguments *args) { |
| 291 | (void)args; |
Alexander Potapenko | 2574257 | 2012-12-10 13:10:40 +0000 | [diff] [blame] | 292 | // Nothing here for now. |
| 293 | } |
| 294 | |
Alexey Samsonov | d7e5bb4 | 2012-09-17 09:12:39 +0000 | [diff] [blame] | 295 | bool StackSizeIsUnlimited() { |
| 296 | UNIMPLEMENTED(); |
Alexey Samsonov | d7e5bb4 | 2012-09-17 09:12:39 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | void SetStackSizeLimitInBytes(uptr limit) { |
| 300 | UNIMPLEMENTED(); |
| 301 | } |
| 302 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 303 | bool AddressSpaceIsUnlimited() { |
| 304 | UNIMPLEMENTED(); |
| 305 | } |
| 306 | |
| 307 | void SetAddressSpaceUnlimited() { |
| 308 | UNIMPLEMENTED(); |
| 309 | } |
| 310 | |
Alexey Samsonov | 1dcd1d9 | 2013-09-03 13:20:48 +0000 | [diff] [blame] | 311 | char *FindPathToBinary(const char *name) { |
Timur Iskhodzhanov | 5a58ef4 | 2013-09-03 15:50:13 +0000 | [diff] [blame] | 312 | // Nothing here for now. |
| 313 | return 0; |
Alexey Samsonov | 1dcd1d9 | 2013-09-03 13:20:48 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Alexey Samsonov | fa3daaf | 2012-06-15 06:37:34 +0000 | [diff] [blame] | 316 | void SleepForSeconds(int seconds) { |
| 317 | Sleep(seconds * 1000); |
| 318 | } |
| 319 | |
Alexey Samsonov | 0969bcf | 2012-06-18 08:44:30 +0000 | [diff] [blame] | 320 | void SleepForMillis(int millis) { |
| 321 | Sleep(millis); |
| 322 | } |
| 323 | |
Dmitry Vyukov | c528867 | 2013-06-10 10:02:02 +0000 | [diff] [blame] | 324 | u64 NanoTime() { |
| 325 | return 0; |
| 326 | } |
| 327 | |
Alexey Samsonov | fa3daaf | 2012-06-15 06:37:34 +0000 | [diff] [blame] | 328 | void Abort() { |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame^] | 329 | if (::IsDebuggerPresent()) |
| 330 | __debugbreak(); |
| 331 | internal__exit(3); |
Alexey Samsonov | fa3daaf | 2012-06-15 06:37:34 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Alexey Samsonov | 7847d77 | 2013-09-10 14:36:16 +0000 | [diff] [blame] | 334 | uptr GetListOfModules(LoadedModule *modules, uptr max_modules, |
| 335 | string_predicate_t filter) { |
| 336 | UNIMPLEMENTED(); |
| 337 | }; |
| 338 | |
Dmitry Vyukov | 33b2a94 | 2012-11-06 13:19:59 +0000 | [diff] [blame] | 339 | #ifndef SANITIZER_GO |
Alexey Samsonov | fa3daaf | 2012-06-15 06:37:34 +0000 | [diff] [blame] | 340 | int Atexit(void (*function)(void)) { |
| 341 | return atexit(function); |
| 342 | } |
Dmitry Vyukov | 33b2a94 | 2012-11-06 13:19:59 +0000 | [diff] [blame] | 343 | #endif |
Alexey Samsonov | fa3daaf | 2012-06-15 06:37:34 +0000 | [diff] [blame] | 344 | |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 345 | // ------------------ sanitizer_libc.h |
Peter Collingbourne | 6712833 | 2013-05-08 18:15:01 +0000 | [diff] [blame] | 346 | uptr internal_mmap(void *addr, uptr length, int prot, int flags, |
| 347 | int fd, u64 offset) { |
Alexey Samsonov | 8c53e54 | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 348 | UNIMPLEMENTED(); |
Alexey Samsonov | c5d4651 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Peter Collingbourne | 6712833 | 2013-05-08 18:15:01 +0000 | [diff] [blame] | 351 | uptr internal_munmap(void *addr, uptr length) { |
Alexey Samsonov | 8c53e54 | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 352 | UNIMPLEMENTED(); |
Alexey Samsonov | 1f11d31 | 2012-06-05 09:49:25 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 355 | uptr internal_close(fd_t fd) { |
Alexey Samsonov | 8c53e54 | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 356 | UNIMPLEMENTED(); |
Alexey Samsonov | a56aefd | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Alexey Samsonov | 84d57b4 | 2012-11-02 15:18:34 +0000 | [diff] [blame] | 359 | int internal_isatty(fd_t fd) { |
Alexey Samsonov | 2bac661 | 2012-12-19 07:57:42 +0000 | [diff] [blame] | 360 | return _isatty(fd); |
Alexey Samsonov | 84d57b4 | 2012-11-02 15:18:34 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Peter Collingbourne | 6712833 | 2013-05-08 18:15:01 +0000 | [diff] [blame] | 363 | uptr internal_open(const char *filename, int flags) { |
Alexey Samsonov | ee7cc44 | 2013-02-01 15:58:46 +0000 | [diff] [blame] | 364 | UNIMPLEMENTED(); |
| 365 | } |
| 366 | |
Peter Collingbourne | 6712833 | 2013-05-08 18:15:01 +0000 | [diff] [blame] | 367 | uptr internal_open(const char *filename, int flags, u32 mode) { |
Alexey Samsonov | ee7cc44 | 2013-02-01 15:58:46 +0000 | [diff] [blame] | 368 | UNIMPLEMENTED(); |
| 369 | } |
| 370 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 371 | uptr OpenFile(const char *filename, bool write) { |
Alexey Samsonov | 8c53e54 | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 372 | UNIMPLEMENTED(); |
Alexey Samsonov | c5d4651 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Alexey Samsonov | a56aefd | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 375 | uptr internal_read(fd_t fd, void *buf, uptr count) { |
Alexey Samsonov | 8c53e54 | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 376 | UNIMPLEMENTED(); |
Alexey Samsonov | a56aefd | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | uptr internal_write(fd_t fd, const void *buf, uptr count) { |
Alexey Samsonov | f3457cb | 2012-11-02 09:38:47 +0000 | [diff] [blame] | 380 | if (fd != kStderrFd) |
Alexey Samsonov | 8c53e54 | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 381 | UNIMPLEMENTED(); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 382 | |
| 383 | static HANDLE output_stream = 0; |
| 384 | // Abort immediately if we know printing is not possible. |
| 385 | if (output_stream == INVALID_HANDLE_VALUE) |
Alexey Samsonov | a56aefd | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 386 | return 0; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 387 | |
| 388 | // If called for the first time, try to use stderr to output stuff, |
| 389 | // falling back to stdout if anything goes wrong. |
| 390 | bool fallback_to_stdout = false; |
| 391 | if (output_stream == 0) { |
| 392 | output_stream = GetStdHandle(STD_ERROR_HANDLE); |
| 393 | // We don't distinguish "no such handle" from error. |
| 394 | if (output_stream == 0) |
| 395 | output_stream = INVALID_HANDLE_VALUE; |
| 396 | |
| 397 | if (output_stream == INVALID_HANDLE_VALUE) { |
| 398 | // Retry with stdout? |
| 399 | output_stream = GetStdHandle(STD_OUTPUT_HANDLE); |
| 400 | if (output_stream == 0) |
| 401 | output_stream = INVALID_HANDLE_VALUE; |
| 402 | if (output_stream == INVALID_HANDLE_VALUE) |
| 403 | return 0; |
| 404 | } else { |
| 405 | // Successfully got an stderr handle. However, if WriteFile() fails, |
| 406 | // we can still try to fallback to stdout. |
| 407 | fallback_to_stdout = true; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | DWORD ret; |
| 412 | if (WriteFile(output_stream, buf, count, &ret, 0)) |
| 413 | return ret; |
| 414 | |
| 415 | // Re-try with stdout if using a valid stderr handle fails. |
| 416 | if (fallback_to_stdout) { |
| 417 | output_stream = GetStdHandle(STD_OUTPUT_HANDLE); |
| 418 | if (output_stream == 0) |
| 419 | output_stream = INVALID_HANDLE_VALUE; |
| 420 | if (output_stream != INVALID_HANDLE_VALUE) |
| 421 | return internal_write(fd, buf, count); |
| 422 | } |
| 423 | return 0; |
Alexey Samsonov | a56aefd | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 426 | uptr internal_stat(const char *path, void *buf) { |
Alexey Samsonov | 4c9317a | 2013-02-04 10:16:50 +0000 | [diff] [blame] | 427 | UNIMPLEMENTED(); |
| 428 | } |
| 429 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 430 | uptr internal_lstat(const char *path, void *buf) { |
Alexey Samsonov | 4c9317a | 2013-02-04 10:16:50 +0000 | [diff] [blame] | 431 | UNIMPLEMENTED(); |
| 432 | } |
| 433 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 434 | uptr internal_fstat(fd_t fd, void *buf) { |
Alexey Samsonov | 4c9317a | 2013-02-04 10:16:50 +0000 | [diff] [blame] | 435 | UNIMPLEMENTED(); |
| 436 | } |
| 437 | |
Alexey Samsonov | 8e820fc | 2012-06-06 07:30:33 +0000 | [diff] [blame] | 438 | uptr internal_filesize(fd_t fd) { |
Alexey Samsonov | 8c53e54 | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 439 | UNIMPLEMENTED(); |
Alexey Samsonov | 8e820fc | 2012-06-06 07:30:33 +0000 | [diff] [blame] | 440 | } |
| 441 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 442 | uptr internal_dup2(int oldfd, int newfd) { |
Alexey Samsonov | 8c53e54 | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 443 | UNIMPLEMENTED(); |
Alexey Samsonov | 8e820fc | 2012-06-06 07:30:33 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Alexey Samsonov | d1b8f58 | 2012-09-05 14:48:24 +0000 | [diff] [blame] | 446 | uptr internal_readlink(const char *path, char *buf, uptr bufsize) { |
| 447 | UNIMPLEMENTED(); |
Alexey Samsonov | d1b8f58 | 2012-09-05 14:48:24 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 450 | uptr internal_sched_yield() { |
Dmitry Vyukov | 33b2a94 | 2012-11-06 13:19:59 +0000 | [diff] [blame] | 451 | Sleep(0); |
| 452 | return 0; |
Alexey Samsonov | 0969bcf | 2012-06-18 08:44:30 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Alexey Samsonov | f882247 | 2013-02-20 13:54:32 +0000 | [diff] [blame] | 455 | void internal__exit(int exitcode) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 456 | ExitProcess(exitcode); |
Alexey Samsonov | f882247 | 2013-02-20 13:54:32 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 459 | uptr internal_ftruncate(fd_t fd, uptr size) { |
| 460 | UNIMPLEMENTED(); |
| 461 | } |
| 462 | |
| 463 | uptr internal_rename(const char *oldpath, const char *newpath) { |
| 464 | UNIMPLEMENTED(); |
| 465 | } |
| 466 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame^] | 467 | uptr GetRSS() { |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | void *internal_start_thread(void (*func)(void *arg), void *arg) { return 0; } |
| 472 | void internal_join_thread(void *th) { } |
| 473 | |
Dmitry Vyukov | f4f51f2 | 2013-01-14 07:51:39 +0000 | [diff] [blame] | 474 | // ---------------------- BlockingMutex ---------------- {{{1 |
Dmitry Vyukov | cf533b6 | 2013-02-04 10:42:38 +0000 | [diff] [blame] | 475 | const uptr LOCK_UNINITIALIZED = 0; |
| 476 | const uptr LOCK_READY = (uptr)-1; |
Dmitry Vyukov | f4f51f2 | 2013-01-14 07:51:39 +0000 | [diff] [blame] | 477 | |
| 478 | BlockingMutex::BlockingMutex(LinkerInitialized li) { |
| 479 | // FIXME: see comments in BlockingMutex::Lock() for the details. |
| 480 | CHECK(li == LINKER_INITIALIZED || owner_ == LOCK_UNINITIALIZED); |
| 481 | |
| 482 | CHECK(sizeof(CRITICAL_SECTION) <= sizeof(opaque_storage_)); |
| 483 | InitializeCriticalSection((LPCRITICAL_SECTION)opaque_storage_); |
| 484 | owner_ = LOCK_READY; |
| 485 | } |
| 486 | |
Alexey Samsonov | 93af594 | 2013-03-14 13:30:56 +0000 | [diff] [blame] | 487 | BlockingMutex::BlockingMutex() { |
| 488 | CHECK(sizeof(CRITICAL_SECTION) <= sizeof(opaque_storage_)); |
| 489 | InitializeCriticalSection((LPCRITICAL_SECTION)opaque_storage_); |
| 490 | owner_ = LOCK_READY; |
| 491 | } |
| 492 | |
Dmitry Vyukov | f4f51f2 | 2013-01-14 07:51:39 +0000 | [diff] [blame] | 493 | void BlockingMutex::Lock() { |
| 494 | if (owner_ == LOCK_UNINITIALIZED) { |
| 495 | // FIXME: hm, global BlockingMutex objects are not initialized?!? |
| 496 | // This might be a side effect of the clang+cl+link Frankenbuild... |
| 497 | new(this) BlockingMutex((LinkerInitialized)(LINKER_INITIALIZED + 1)); |
| 498 | |
| 499 | // FIXME: If it turns out the linker doesn't invoke our |
| 500 | // constructors, we should probably manually Lock/Unlock all the global |
| 501 | // locks while we're starting in one thread to avoid double-init races. |
| 502 | } |
| 503 | EnterCriticalSection((LPCRITICAL_SECTION)opaque_storage_); |
Dmitry Vyukov | 8ceeec4 | 2013-02-04 08:07:45 +0000 | [diff] [blame] | 504 | CHECK_EQ(owner_, LOCK_READY); |
Dmitry Vyukov | f4f51f2 | 2013-01-14 07:51:39 +0000 | [diff] [blame] | 505 | owner_ = GetThreadSelf(); |
| 506 | } |
| 507 | |
| 508 | void BlockingMutex::Unlock() { |
Dmitry Vyukov | 8ceeec4 | 2013-02-04 08:07:45 +0000 | [diff] [blame] | 509 | CHECK_EQ(owner_, GetThreadSelf()); |
Dmitry Vyukov | f4f51f2 | 2013-01-14 07:51:39 +0000 | [diff] [blame] | 510 | owner_ = LOCK_READY; |
| 511 | LeaveCriticalSection((LPCRITICAL_SECTION)opaque_storage_); |
| 512 | } |
| 513 | |
Alexey Samsonov | ce70097 | 2013-03-11 15:45:20 +0000 | [diff] [blame] | 514 | void BlockingMutex::CheckLocked() { |
| 515 | CHECK_EQ(owner_, GetThreadSelf()); |
| 516 | } |
| 517 | |
Evgeniy Stepanov | b114ed8 | 2013-03-13 08:19:53 +0000 | [diff] [blame] | 518 | uptr GetTlsSize() { |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | void InitTlsSize() { |
| 523 | } |
| 524 | |
Sergey Matveev | 24323de | 2013-05-07 14:41:43 +0000 | [diff] [blame] | 525 | void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size, |
| 526 | uptr *tls_addr, uptr *tls_size) { |
Dmitry Vyukov | 21b3b1c | 2013-06-10 10:30:54 +0000 | [diff] [blame] | 527 | #ifdef SANITIZER_GO |
| 528 | *stk_addr = 0; |
| 529 | *stk_size = 0; |
| 530 | *tls_addr = 0; |
| 531 | *tls_size = 0; |
| 532 | #else |
Sergey Matveev | 24323de | 2013-05-07 14:41:43 +0000 | [diff] [blame] | 533 | uptr stack_top, stack_bottom; |
| 534 | GetThreadStackTopAndBottom(main, &stack_top, &stack_bottom); |
| 535 | *stk_addr = stack_bottom; |
| 536 | *stk_size = stack_top - stack_bottom; |
| 537 | *tls_addr = 0; |
| 538 | *tls_size = 0; |
Dmitry Vyukov | 21b3b1c | 2013-06-10 10:30:54 +0000 | [diff] [blame] | 539 | #endif |
Sergey Matveev | 24323de | 2013-05-07 14:41:43 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 542 | #if !SANITIZER_GO |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame^] | 543 | void BufferedStackTrace::SlowUnwindStack(uptr pc, u32 max_depth) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 544 | CHECK_GE(max_depth, 2); |
Sergey Matveev | 736cf49 | 2013-05-08 12:45:55 +0000 | [diff] [blame] | 545 | // FIXME: CaptureStackBackTrace might be too slow for us. |
| 546 | // FIXME: Compare with StackWalk64. |
| 547 | // FIXME: Look at LLVMUnhandledExceptionFilter in Signals.inc |
Timur Iskhodzhanov | 7177d2b | 2013-11-09 13:59:12 +0000 | [diff] [blame] | 548 | size = CaptureStackBackTrace(2, Min(max_depth, kStackTraceMax), |
| 549 | (void**)trace, 0); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 550 | if (size == 0) |
| 551 | return; |
| 552 | |
Sergey Matveev | 736cf49 | 2013-05-08 12:45:55 +0000 | [diff] [blame] | 553 | // Skip the RTL frames by searching for the PC in the stacktrace. |
Timur Iskhodzhanov | 7177d2b | 2013-11-09 13:59:12 +0000 | [diff] [blame] | 554 | uptr pc_location = LocatePcInTrace(pc); |
| 555 | PopStackFrames(pc_location); |
Sergey Matveev | 736cf49 | 2013-05-08 12:45:55 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 558 | void BufferedStackTrace::SlowUnwindStackWithContext(uptr pc, void *context, |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame^] | 559 | u32 max_depth) { |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 560 | CONTEXT ctx = *(CONTEXT *)context; |
| 561 | STACKFRAME64 stack_frame; |
| 562 | memset(&stack_frame, 0, sizeof(stack_frame)); |
| 563 | size = 0; |
| 564 | #if defined(_WIN64) |
| 565 | int machine_type = IMAGE_FILE_MACHINE_AMD64; |
| 566 | stack_frame.AddrPC.Offset = ctx.Rip; |
| 567 | stack_frame.AddrFrame.Offset = ctx.Rbp; |
| 568 | stack_frame.AddrStack.Offset = ctx.Rsp; |
| 569 | #else |
| 570 | int machine_type = IMAGE_FILE_MACHINE_I386; |
| 571 | stack_frame.AddrPC.Offset = ctx.Eip; |
| 572 | stack_frame.AddrFrame.Offset = ctx.Ebp; |
| 573 | stack_frame.AddrStack.Offset = ctx.Esp; |
| 574 | #endif |
| 575 | stack_frame.AddrPC.Mode = AddrModeFlat; |
| 576 | stack_frame.AddrFrame.Mode = AddrModeFlat; |
| 577 | stack_frame.AddrStack.Mode = AddrModeFlat; |
| 578 | while (StackWalk64(machine_type, GetCurrentProcess(), GetCurrentThread(), |
| 579 | &stack_frame, &ctx, NULL, &SymFunctionTableAccess64, |
| 580 | &SymGetModuleBase64, NULL) && |
| 581 | size < Min(max_depth, kStackTraceMax)) { |
| 582 | trace_buffer[size++] = (uptr)stack_frame.AddrPC.Offset; |
| 583 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 584 | } |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 585 | #endif // #if !SANITIZER_GO |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 586 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame^] | 587 | void ReportFile::Write(const char *buffer, uptr length) { |
| 588 | SpinMutexLock l(mu); |
| 589 | ReopenIfNecessary(); |
| 590 | if (length != internal_write(fd, buffer, length)) { |
Reid Kleckner | 923bac7 | 2013-09-05 03:19:57 +0000 | [diff] [blame] | 591 | // stderr may be closed, but we may be able to print to the debugger |
| 592 | // instead. This is the case when launching a program from Visual Studio, |
| 593 | // and the following routine should write to its console. |
| 594 | OutputDebugStringA(buffer); |
| 595 | } |
| 596 | } |
| 597 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 598 | void SetAlternateSignalStack() { |
| 599 | // FIXME: Decide what to do on Windows. |
| 600 | } |
| 601 | |
| 602 | void UnsetAlternateSignalStack() { |
| 603 | // FIXME: Decide what to do on Windows. |
| 604 | } |
| 605 | |
| 606 | void InstallDeadlySignalHandlers(SignalHandlerType handler) { |
| 607 | (void)handler; |
| 608 | // FIXME: Decide what to do on Windows. |
| 609 | } |
| 610 | |
| 611 | bool IsDeadlySignal(int signum) { |
| 612 | // FIXME: Decide what to do on Windows. |
| 613 | return false; |
| 614 | } |
| 615 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 616 | bool IsAccessibleMemoryRange(uptr beg, uptr size) { |
| 617 | // FIXME: Actually implement this function. |
| 618 | return true; |
| 619 | } |
| 620 | |
Alexey Samsonov | c5d4651 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 621 | } // namespace __sanitizer |
| 622 | |
| 623 | #endif // _WIN32 |