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 | //===----------------------------------------------------------------------===// |
Evgeniy Stepanov | 24e1372 | 2013-03-19 14:33:38 +0000 | [diff] [blame] | 14 | |
| 15 | #include "sanitizer_common/sanitizer_platform.h" |
Evgeniy Stepanov | 30e110e | 2013-03-19 14:54:17 +0000 | [diff] [blame] | 16 | #if SANITIZER_WINDOWS |
Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 17 | #include <windows.h> |
| 18 | |
| 19 | #include <dbghelp.h> |
Alexey Samsonov | b823e3c | 2012-02-22 14:07:06 +0000 | [diff] [blame] | 20 | #include <stdlib.h> |
Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 21 | |
Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 22 | #include "asan_interceptors.h" |
| 23 | #include "asan_internal.h" |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 24 | #include "asan_report.h" |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 25 | #include "asan_stack.h" |
Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 26 | #include "asan_thread.h" |
Alexey Samsonov | de08c02 | 2012-06-19 09:21:57 +0000 | [diff] [blame] | 27 | #include "sanitizer_common/sanitizer_libc.h" |
Dmitry Vyukov | f4f51f2 | 2013-01-14 07:51:39 +0000 | [diff] [blame] | 28 | #include "sanitizer_common/sanitizer_mutex.h" |
Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 29 | |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 30 | using namespace __asan; // NOLINT |
| 31 | |
Timur Iskhodzhanov | c11fa09 | 2013-09-23 11:05:41 +0000 | [diff] [blame] | 32 | extern "C" { |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 33 | SANITIZER_INTERFACE_ATTRIBUTE |
| 34 | int __asan_should_detect_stack_use_after_return() { |
| 35 | __asan_init(); |
| 36 | return __asan_option_detect_stack_use_after_return; |
Timur Iskhodzhanov | c11fa09 | 2013-09-23 11:05:41 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 39 | // -------------------- A workaround for the abscence of weak symbols ----- {{{ |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 40 | // We don't have a direct equivalent of weak symbols when using MSVC, but we can |
| 41 | // use the /alternatename directive to tell the linker to default a specific |
| 42 | // symbol to a specific value, which works nicely for allocator hooks and |
| 43 | // __asan_default_options(). |
| 44 | void __sanitizer_default_malloc_hook(void *ptr, uptr size) { } |
| 45 | void __sanitizer_default_free_hook(void *ptr) { } |
| 46 | const char* __asan_default_default_options() { return ""; } |
| 47 | const char* __asan_default_default_suppressions() { return ""; } |
| 48 | void __asan_default_on_error() {} |
| 49 | #pragma comment(linker, "/alternatename:___sanitizer_malloc_hook=___sanitizer_default_malloc_hook") // NOLINT |
| 50 | #pragma comment(linker, "/alternatename:___sanitizer_free_hook=___sanitizer_default_free_hook") // NOLINT |
| 51 | #pragma comment(linker, "/alternatename:___asan_default_options=___asan_default_default_options") // NOLINT |
| 52 | #pragma comment(linker, "/alternatename:___asan_default_suppressions=___asan_default_default_suppressions") // NOLINT |
| 53 | #pragma comment(linker, "/alternatename:___asan_on_error=___asan_default_on_error") // NOLINT |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 54 | // }}} |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 55 | } // extern "C" |
| 56 | |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 57 | // ---------------------- Windows-specific inteceptors ---------------- {{{ |
| 58 | INTERCEPTOR_WINAPI(void, RaiseException, void *a, void *b, void *c, void *d) { |
| 59 | CHECK(REAL(RaiseException)); |
| 60 | __asan_handle_no_return(); |
| 61 | REAL(RaiseException)(a, b, c, d); |
| 62 | } |
| 63 | |
| 64 | INTERCEPTOR(int, _except_handler3, void *a, void *b, void *c, void *d) { |
| 65 | CHECK(REAL(_except_handler3)); |
| 66 | __asan_handle_no_return(); |
| 67 | return REAL(_except_handler3)(a, b, c, d); |
| 68 | } |
| 69 | |
| 70 | #if ASAN_DYNAMIC |
| 71 | // This handler is named differently in -MT and -MD CRTs. |
| 72 | #define _except_handler4 _except_handler4_common |
| 73 | #endif |
| 74 | INTERCEPTOR(int, _except_handler4, void *a, void *b, void *c, void *d) { |
| 75 | CHECK(REAL(_except_handler4)); |
| 76 | __asan_handle_no_return(); |
| 77 | return REAL(_except_handler4)(a, b, c, d); |
| 78 | } |
| 79 | |
| 80 | static thread_return_t THREAD_CALLING_CONV asan_thread_start(void *arg) { |
| 81 | AsanThread *t = (AsanThread*)arg; |
| 82 | SetCurrentThread(t); |
| 83 | return t->ThreadStart(GetTid(), /* signal_thread_is_registered */ nullptr); |
| 84 | } |
| 85 | |
| 86 | INTERCEPTOR_WINAPI(DWORD, CreateThread, |
| 87 | void* security, uptr stack_size, |
| 88 | DWORD (__stdcall *start_routine)(void*), void* arg, |
| 89 | DWORD thr_flags, void* tid) { |
| 90 | // Strict init-order checking is thread-hostile. |
| 91 | if (flags()->strict_init_order) |
| 92 | StopInitOrderChecking(); |
| 93 | GET_STACK_TRACE_THREAD; |
| 94 | // FIXME: The CreateThread interceptor is not the same as a pthread_create |
| 95 | // one. This is a bandaid fix for PR22025. |
| 96 | bool detached = false; // FIXME: how can we determine it on Windows? |
| 97 | u32 current_tid = GetCurrentTidOrInvalid(); |
| 98 | AsanThread *t = |
| 99 | AsanThread::Create(start_routine, arg, current_tid, &stack, detached); |
| 100 | return REAL(CreateThread)(security, stack_size, |
| 101 | asan_thread_start, t, thr_flags, tid); |
| 102 | } |
| 103 | |
| 104 | namespace { |
| 105 | BlockingMutex mu_for_thread_tracking(LINKER_INITIALIZED); |
| 106 | |
| 107 | void EnsureWorkerThreadRegistered() { |
| 108 | // FIXME: GetCurrentThread relies on TSD, which might not play well with |
| 109 | // system thread pools. We might want to use something like reference |
| 110 | // counting to zero out GetCurrentThread() underlying storage when the last |
| 111 | // work item finishes? Or can we disable reclaiming of threads in the pool? |
| 112 | BlockingMutexLock l(&mu_for_thread_tracking); |
| 113 | if (__asan::GetCurrentThread()) |
| 114 | return; |
| 115 | |
| 116 | AsanThread *t = AsanThread::Create( |
| 117 | /* start_routine */ nullptr, /* arg */ nullptr, |
| 118 | /* parent_tid */ -1, /* stack */ nullptr, /* detached */ true); |
| 119 | t->Init(); |
| 120 | asanThreadRegistry().StartThread(t->tid(), 0, 0); |
| 121 | SetCurrentThread(t); |
| 122 | } |
| 123 | } // namespace |
| 124 | |
| 125 | INTERCEPTOR_WINAPI(DWORD, NtWaitForWorkViaWorkerFactory, DWORD a, DWORD b) { |
| 126 | // NtWaitForWorkViaWorkerFactory is called from system worker pool threads to |
| 127 | // query work scheduled by BindIoCompletionCallback, QueueUserWorkItem, etc. |
| 128 | // System worker pool threads are created at arbitraty point in time and |
| 129 | // without using CreateThread, so we wrap NtWaitForWorkViaWorkerFactory |
| 130 | // instead and don't register a specific parent_tid/stack. |
| 131 | EnsureWorkerThreadRegistered(); |
| 132 | return REAL(NtWaitForWorkViaWorkerFactory)(a, b); |
| 133 | } |
| 134 | |
| 135 | // }}} |
| 136 | |
Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 137 | namespace __asan { |
| 138 | |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 139 | void InitializePlatformInterceptors() { |
| 140 | ASAN_INTERCEPT_FUNC(CreateThread); |
| 141 | ASAN_INTERCEPT_FUNC(RaiseException); |
| 142 | ASAN_INTERCEPT_FUNC(_except_handler3); |
| 143 | ASAN_INTERCEPT_FUNC(_except_handler4); |
| 144 | |
| 145 | // NtWaitForWorkViaWorkerFactory is always linked dynamically. |
| 146 | CHECK(::__interception::OverrideFunction( |
| 147 | "NtWaitForWorkViaWorkerFactory", |
| 148 | (uptr)WRAP(NtWaitForWorkViaWorkerFactory), |
| 149 | (uptr *)&REAL(NtWaitForWorkViaWorkerFactory))); |
| 150 | } |
| 151 | |
| 152 | // ---------------------- TSD ---------------- {{{ |
Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 153 | static bool tsd_key_inited = false; |
| 154 | |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 155 | static __declspec(thread) void *fake_tsd = 0; |
Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 156 | |
| 157 | void AsanTSDInit(void (*destructor)(void *tsd)) { |
| 158 | // FIXME: we're ignoring the destructor for now. |
| 159 | tsd_key_inited = true; |
| 160 | } |
| 161 | |
| 162 | void *AsanTSDGet() { |
| 163 | CHECK(tsd_key_inited); |
| 164 | return fake_tsd; |
| 165 | } |
| 166 | |
| 167 | void AsanTSDSet(void *tsd) { |
| 168 | CHECK(tsd_key_inited); |
| 169 | fake_tsd = tsd; |
| 170 | } |
Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 171 | |
Sergey Matveev | e86e35f | 2013-10-14 12:01:05 +0000 | [diff] [blame] | 172 | void PlatformTSDDtor(void *tsd) { |
| 173 | AsanThread::TSDDtor(tsd); |
| 174 | } |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 175 | // }}} |
| 176 | |
| 177 | // ---------------------- Various stuff ---------------- {{{ |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 178 | void DisableReexec() { |
| 179 | // No need to re-exec on Windows. |
| 180 | } |
| 181 | |
Alexander Potapenko | eb8c46e | 2012-08-24 09:22:05 +0000 | [diff] [blame] | 182 | void MaybeReexec() { |
| 183 | // No need to re-exec on Windows. |
| 184 | } |
| 185 | |
Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 186 | void *AsanDoesNotSupportStaticLinkage() { |
Timur Iskhodzhanov | 2716a61 | 2012-03-12 11:45:09 +0000 | [diff] [blame] | 187 | #if defined(_DEBUG) |
| 188 | #error Please build the runtime with a non-debug CRT: /MD or /MT |
Timur Iskhodzhanov | 07bb9f1 | 2012-02-22 13:59:49 +0000 | [diff] [blame] | 189 | #endif |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 190 | return 0; |
Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 193 | void AsanCheckDynamicRTPrereqs() {} |
Alexander Potapenko | f03d8af | 2012-04-05 10:54:52 +0000 | [diff] [blame] | 194 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 195 | void AsanCheckIncompatibleRT() {} |
Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 196 | |
Alexander Potapenko | 75b19eb | 2012-07-23 14:07:58 +0000 | [diff] [blame] | 197 | void AsanPlatformThreadInit() { |
| 198 | // Nothing here for now. |
| 199 | } |
| 200 | |
Alexey Samsonov | 57db4ba | 2013-01-17 15:45:28 +0000 | [diff] [blame] | 201 | void ReadContextStack(void *context, uptr *stack, uptr *ssize) { |
Alexey Samsonov | 0870028 | 2012-11-23 09:46:34 +0000 | [diff] [blame] | 202 | UNIMPLEMENTED(); |
| 203 | } |
| 204 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 205 | void AsanOnSIGSEGV(int, void *siginfo, void *context) { |
| 206 | UNIMPLEMENTED(); |
Alexey Samsonov | 1ca5357 | 2012-10-02 12:11:17 +0000 | [diff] [blame] | 207 | } |
Alexey Samsonov | 1ca5357 | 2012-10-02 12:11:17 +0000 | [diff] [blame] | 208 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 209 | static LPTOP_LEVEL_EXCEPTION_FILTER default_seh_handler; |
| 210 | |
| 211 | static long WINAPI SEHHandler(EXCEPTION_POINTERS *info) { |
| 212 | EXCEPTION_RECORD *exception_record = info->ExceptionRecord; |
| 213 | CONTEXT *context = info->ContextRecord; |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 214 | |
| 215 | if (exception_record->ExceptionCode == EXCEPTION_ACCESS_VIOLATION || |
| 216 | exception_record->ExceptionCode == EXCEPTION_IN_PAGE_ERROR) { |
| 217 | const char *description = |
| 218 | (exception_record->ExceptionCode == EXCEPTION_ACCESS_VIOLATION) |
| 219 | ? "access-violation" |
| 220 | : "in-page-error"; |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 221 | SignalContext sig = SignalContext::Create(exception_record, context); |
| 222 | ReportSIGSEGV(description, sig); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | // FIXME: Handle EXCEPTION_STACK_OVERFLOW here. |
| 226 | |
| 227 | return default_seh_handler(info); |
| 228 | } |
| 229 | |
| 230 | // We want to install our own exception handler (EH) to print helpful reports |
| 231 | // on access violations and whatnot. Unfortunately, the CRT initializers assume |
| 232 | // they are run before any user code and drop any previously-installed EHs on |
| 233 | // the floor, so we can't install our handler inside __asan_init. |
| 234 | // (See crt0dat.c in the CRT sources for the details) |
| 235 | // |
| 236 | // Things get even more complicated with the dynamic runtime, as it finishes its |
| 237 | // initialization before the .exe module CRT begins to initialize. |
| 238 | // |
| 239 | // For the static runtime (-MT), it's enough to put a callback to |
| 240 | // __asan_set_seh_filter in the last section for C initializers. |
| 241 | // |
| 242 | // For the dynamic runtime (-MD), we want link the same |
| 243 | // asan_dynamic_runtime_thunk.lib to all the modules, thus __asan_set_seh_filter |
| 244 | // will be called for each instrumented module. This ensures that at least one |
| 245 | // __asan_set_seh_filter call happens after the .exe module CRT is initialized. |
| 246 | extern "C" SANITIZER_INTERFACE_ATTRIBUTE |
| 247 | int __asan_set_seh_filter() { |
| 248 | // We should only store the previous handler if it's not our own handler in |
| 249 | // order to avoid loops in the EH chain. |
| 250 | auto prev_seh_handler = SetUnhandledExceptionFilter(SEHHandler); |
| 251 | if (prev_seh_handler != &SEHHandler) |
| 252 | default_seh_handler = prev_seh_handler; |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | #if !ASAN_DYNAMIC |
| 257 | // Put a pointer to __asan_set_seh_filter at the end of the global list |
| 258 | // of C initializers, after the default EH is set by the CRT. |
| 259 | #pragma section(".CRT$XIZ", long, read) // NOLINT |
| 260 | static __declspec(allocate(".CRT$XIZ")) |
| 261 | int (*__intercept_seh)() = __asan_set_seh_filter; |
| 262 | #endif |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 263 | // }}} |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 264 | } // namespace __asan |
Alexey Samsonov | 1ca5357 | 2012-10-02 12:11:17 +0000 | [diff] [blame] | 265 | |
Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 266 | #endif // _WIN32 |