| Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 1 | //===-- asan_win.cc -------------------------------------------------------===// |
| 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 a part of AddressSanitizer, an address sanity checker. |
| 11 | // |
| 12 | // Windows-specific details. |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | #ifdef _WIN32 |
| 15 | #include <windows.h> |
| 16 | |
| 17 | #include <dbghelp.h> |
| 18 | #include <stdio.h> // FIXME: get rid of this. |
| Alexey Samsonov | b823e3c | 2012-02-22 14:07:06 +0000 | [diff] [blame] | 19 | #include <stdlib.h> |
| Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 20 | |
| 21 | #include <new> // FIXME: temporarily needed for placement new in AsanLock. |
| 22 | |
| 23 | #include "asan_interceptors.h" |
| 24 | #include "asan_internal.h" |
| 25 | #include "asan_lock.h" |
| 26 | #include "asan_procmaps.h" |
| 27 | #include "asan_thread.h" |
| 28 | |
| Kostya Serebryany | 25c7178 | 2012-03-10 01:30:01 +0000 | [diff] [blame^] | 29 | // Should not add dependency on libstdc++, |
| 30 | // since most of the stuff here is inlinable. |
| 31 | #include <algorithm> |
| 32 | |
| Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 33 | namespace __asan { |
| 34 | |
| 35 | // ---------------------- Memory management ---------------- {{{1 |
| 36 | void *AsanMmapFixedNoReserve(uintptr_t fixed_addr, size_t size) { |
| 37 | return VirtualAlloc((LPVOID)fixed_addr, size, |
| 38 | MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); |
| 39 | } |
| 40 | |
| 41 | void *AsanMmapSomewhereOrDie(size_t size, const char *mem_type) { |
| 42 | void *rv = VirtualAlloc(NULL, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); |
| 43 | if (rv == NULL) |
| 44 | OutOfMemoryMessageAndDie(mem_type, size); |
| 45 | return rv; |
| 46 | } |
| 47 | |
| 48 | void *AsanMprotect(uintptr_t fixed_addr, size_t size) { |
| 49 | return VirtualAlloc((LPVOID)fixed_addr, size, |
| 50 | MEM_RESERVE | MEM_COMMIT, PAGE_NOACCESS); |
| 51 | } |
| 52 | |
| 53 | void AsanUnmapOrDie(void *addr, size_t size) { |
| Timur Iskhodzhanov | 600972e | 2012-02-24 15:28:43 +0000 | [diff] [blame] | 54 | CHECK(VirtualFree(addr, size, MEM_DECOMMIT)); |
| Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 55 | } |
| Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 56 | |
| 57 | // ---------------------- IO ---------------- {{{1 |
| 58 | size_t AsanWrite(int fd, const void *buf, size_t count) { |
| 59 | if (fd != 2) |
| 60 | UNIMPLEMENTED(); |
| 61 | |
| 62 | // FIXME: use WriteFile instead? |
| 63 | return fwrite(buf, 1, count, stderr); |
| 64 | } |
| 65 | |
| 66 | // FIXME: Looks like these functions are not needed and are linked in by the |
| 67 | // code unreachable on Windows. We should clean this up. |
| 68 | int AsanOpenReadonly(const char* filename) { |
| 69 | UNIMPLEMENTED(); |
| 70 | return -1; |
| 71 | } |
| 72 | |
| 73 | size_t AsanRead(int fd, void *buf, size_t count) { |
| 74 | UNIMPLEMENTED(); |
| 75 | return -1; |
| 76 | } |
| 77 | |
| 78 | int AsanClose(int fd) { |
| 79 | UNIMPLEMENTED(); |
| 80 | return -1; |
| 81 | } |
| Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 82 | |
| 83 | // ---------------------- Stacktraces, symbols, etc. ---------------- {{{1 |
| 84 | static AsanLock dbghelp_lock(LINKER_INITIALIZED); |
| 85 | static bool dbghelp_initialized = false; |
| 86 | #pragma comment(lib, "dbghelp.lib") |
| 87 | |
| 88 | void AsanThread::SetThreadStackTopAndBottom() { |
| 89 | MEMORY_BASIC_INFORMATION mbi; |
| 90 | CHECK(VirtualQuery(&mbi /* on stack */, |
| 91 | &mbi, sizeof(mbi)) != 0); |
| 92 | // FIXME: is it possible for the stack to not be a single allocation? |
| 93 | // Are these values what ASan expects to get (reserved, not committed; |
| 94 | // including stack guard page) ? |
| 95 | stack_top_ = (uintptr_t)mbi.BaseAddress + mbi.RegionSize; |
| 96 | stack_bottom_ = (uintptr_t)mbi.AllocationBase; |
| 97 | } |
| 98 | |
| 99 | void AsanStackTrace::GetStackTrace(size_t max_s, uintptr_t pc, uintptr_t bp) { |
| 100 | max_size = max_s; |
| 101 | void *tmp[kStackTraceMax]; |
| 102 | |
| 103 | // FIXME: CaptureStackBackTrace might be too slow for us. |
| 104 | // FIXME: Compare with StackWalk64. |
| 105 | // FIXME: Look at LLVMUnhandledExceptionFilter in Signals.inc |
| 106 | size_t cs_ret = CaptureStackBackTrace(1, max_size, tmp, NULL), |
| 107 | offset = 0; |
| 108 | // Skip the RTL frames by searching for the PC in the stacktrace. |
| 109 | // FIXME: this doesn't work well for the malloc/free stacks yet. |
| 110 | for (size_t i = 0; i < cs_ret; i++) { |
| 111 | if (pc != (uintptr_t)tmp[i]) |
| 112 | continue; |
| 113 | offset = i; |
| 114 | break; |
| 115 | } |
| 116 | |
| 117 | size = cs_ret - offset; |
| 118 | for (size_t i = 0; i < size; i++) |
| 119 | trace[i] = (uintptr_t)tmp[i + offset]; |
| 120 | } |
| 121 | |
| 122 | bool WinSymbolize(const void *addr, char *out_buffer, int buffer_size) { |
| 123 | ScopedLock lock(&dbghelp_lock); |
| 124 | if (!dbghelp_initialized) { |
| 125 | SymSetOptions(SYMOPT_DEFERRED_LOADS | |
| 126 | SYMOPT_UNDNAME | |
| 127 | SYMOPT_LOAD_LINES); |
| 128 | CHECK(SymInitialize(GetCurrentProcess(), NULL, TRUE)); |
| 129 | // FIXME: We don't call SymCleanup() on exit yet - should we? |
| 130 | dbghelp_initialized = true; |
| 131 | } |
| 132 | |
| 133 | // See http://msdn.microsoft.com/en-us/library/ms680578(VS.85).aspx |
| 134 | char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(CHAR)]; |
| 135 | PSYMBOL_INFO symbol = (PSYMBOL_INFO)buffer; |
| 136 | symbol->SizeOfStruct = sizeof(SYMBOL_INFO); |
| 137 | symbol->MaxNameLen = MAX_SYM_NAME; |
| 138 | DWORD64 offset = 0; |
| 139 | BOOL got_objname = SymFromAddr(GetCurrentProcess(), |
| 140 | (DWORD64)addr, &offset, symbol); |
| 141 | if (!got_objname) |
| 142 | return false; |
| 143 | |
| 144 | DWORD unused; |
| 145 | IMAGEHLP_LINE64 info; |
| 146 | info.SizeOfStruct = sizeof(IMAGEHLP_LINE64); |
| 147 | BOOL got_fileline = SymGetLineFromAddr64(GetCurrentProcess(), |
| 148 | (DWORD64)addr, &unused, &info); |
| 149 | int written = 0; |
| 150 | out_buffer[0] = '\0'; |
| 151 | // FIXME: it might be useful to print out 'obj' or 'obj+offset' info too. |
| 152 | if (got_fileline) { |
| 153 | written += SNPrintf(out_buffer + written, buffer_size - written, |
| 154 | " %s %s:%d", symbol->Name, |
| 155 | info.FileName, info.LineNumber); |
| 156 | } else { |
| 157 | written += SNPrintf(out_buffer + written, buffer_size - written, |
| 158 | " %s+0x%p", symbol->Name, offset); |
| 159 | } |
| 160 | return true; |
| 161 | } |
| Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 162 | |
| 163 | // ---------------------- AsanLock ---------------- {{{1 |
| 164 | enum LockState { |
| 165 | LOCK_UNINITIALIZED = 0, |
| 166 | LOCK_READY = -1, |
| 167 | }; |
| 168 | |
| 169 | AsanLock::AsanLock(LinkerInitialized li) { |
| 170 | // FIXME: see comments in AsanLock::Lock() for the details. |
| 171 | CHECK(li == LINKER_INITIALIZED || owner_ == LOCK_UNINITIALIZED); |
| 172 | |
| 173 | CHECK(sizeof(CRITICAL_SECTION) <= sizeof(opaque_storage_)); |
| 174 | InitializeCriticalSection((LPCRITICAL_SECTION)opaque_storage_); |
| 175 | owner_ = LOCK_READY; |
| 176 | } |
| 177 | |
| 178 | void AsanLock::Lock() { |
| 179 | if (owner_ == LOCK_UNINITIALIZED) { |
| 180 | // FIXME: hm, global AsanLock objects are not initialized?!? |
| 181 | // This might be a side effect of the clang+cl+link Frankenbuild... |
| 182 | new(this) AsanLock((LinkerInitialized)(LINKER_INITIALIZED + 1)); |
| 183 | |
| 184 | // FIXME: If it turns out the linker doesn't invoke our |
| 185 | // constructors, we should probably manually Lock/Unlock all the global |
| 186 | // locks while we're starting in one thread to avoid double-init races. |
| 187 | } |
| 188 | EnterCriticalSection((LPCRITICAL_SECTION)opaque_storage_); |
| 189 | CHECK(owner_ == LOCK_READY); |
| 190 | owner_ = GetThreadSelf(); |
| 191 | } |
| 192 | |
| 193 | void AsanLock::Unlock() { |
| 194 | CHECK(owner_ == GetThreadSelf()); |
| 195 | owner_ = LOCK_READY; |
| 196 | LeaveCriticalSection((LPCRITICAL_SECTION)opaque_storage_); |
| 197 | } |
| Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 198 | |
| 199 | // ---------------------- TSD ---------------- {{{1 |
| 200 | static bool tsd_key_inited = false; |
| 201 | |
| 202 | // FIXME: is __declspec enough? |
| 203 | static __declspec(thread) void *fake_tsd = NULL; |
| 204 | |
| 205 | void AsanTSDInit(void (*destructor)(void *tsd)) { |
| 206 | // FIXME: we're ignoring the destructor for now. |
| 207 | tsd_key_inited = true; |
| 208 | } |
| 209 | |
| 210 | void *AsanTSDGet() { |
| 211 | CHECK(tsd_key_inited); |
| 212 | return fake_tsd; |
| 213 | } |
| 214 | |
| 215 | void AsanTSDSet(void *tsd) { |
| 216 | CHECK(tsd_key_inited); |
| 217 | fake_tsd = tsd; |
| 218 | } |
| Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 219 | |
| 220 | // ---------------------- Various stuff ---------------- {{{1 |
| 221 | void *AsanDoesNotSupportStaticLinkage() { |
| Timur Iskhodzhanov | 07bb9f1 | 2012-02-22 13:59:49 +0000 | [diff] [blame] | 222 | #if !defined(_DLL) || defined(_DEBUG) |
| 223 | #error Please build the runtime with /MD |
| 224 | #endif |
| Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 225 | return NULL; |
| 226 | } |
| 227 | |
| Alexander Potapenko | f73a6a3 | 2012-02-13 17:09:40 +0000 | [diff] [blame] | 228 | bool AsanShadowRangeIsAvailable() { |
| 229 | // FIXME: shall we do anything here on Windows? |
| 230 | return true; |
| 231 | } |
| 232 | |
| Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 233 | int AtomicInc(int *a) { |
| 234 | return InterlockedExchangeAdd((LONG*)a, 1) + 1; |
| 235 | } |
| 236 | |
| 237 | const char* AsanGetEnv(const char* name) { |
| 238 | // FIXME: implement. |
| 239 | return NULL; |
| 240 | } |
| 241 | |
| Alexander Potapenko | 99d17eb | 2012-02-22 09:11:55 +0000 | [diff] [blame] | 242 | void AsanDumpProcessMap() { |
| 243 | UNIMPLEMENTED(); |
| 244 | } |
| 245 | |
| Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 246 | int GetPid() { |
| 247 | return GetProcessId(GetCurrentProcess()); |
| 248 | } |
| 249 | |
| 250 | uintptr_t GetThreadSelf() { |
| 251 | return GetCurrentThreadId(); |
| 252 | } |
| 253 | |
| 254 | void InstallSignalHandlers() { |
| 255 | // FIXME: Decide what to do on Windows. |
| 256 | } |
| 257 | |
| 258 | void AsanDisableCoreDumper() { |
| 259 | UNIMPLEMENTED(); |
| 260 | } |
| 261 | |
| Kostya Serebryany | e1fe0fd | 2012-02-13 21:24:29 +0000 | [diff] [blame] | 262 | void SleepForSeconds(int seconds) { |
| 263 | Sleep(seconds * 1000); |
| Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 264 | } |
| Kostya Serebryany | e1fe0fd | 2012-02-13 21:24:29 +0000 | [diff] [blame] | 265 | |
| 266 | void Exit(int exitcode) { |
| 267 | _exit(exitcode); |
| 268 | } |
| Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 269 | |
| Alexey Samsonov | b823e3c | 2012-02-22 14:07:06 +0000 | [diff] [blame] | 270 | int Atexit(void (*function)(void)) { |
| 271 | return atexit(function); |
| 272 | } |
| 273 | |
| Kostya Serebryany | 25c7178 | 2012-03-10 01:30:01 +0000 | [diff] [blame^] | 274 | void SortArray(uintptr_t *array, size_t size) { |
| 275 | std::sort(array, array + size); |
| 276 | } |
| 277 | |
| Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 278 | } // namespace __asan |
| 279 | |
| 280 | #endif // _WIN32 |