1 //===-- asan_win.cc -------------------------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is a part of AddressSanitizer, an address sanity checker.
10 // Windows-specific details.
11 //===----------------------------------------------------------------------===//
13 #include "sanitizer_common/sanitizer_platform.h"
19 #include "asan_interceptors.h"
20 #include "asan_internal.h"
21 #include "asan_report.h"
22 #include "asan_stack.h"
23 #include "asan_thread.h"
24 #include "sanitizer_common/sanitizer_libc.h"
25 #include "sanitizer_common/sanitizer_mutex.h"
27 using namespace __asan
; // NOLINT
30 SANITIZER_INTERFACE_ATTRIBUTE
31 int __asan_should_detect_stack_use_after_return() {
33 return __asan_option_detect_stack_use_after_return
;
36 // -------------------- A workaround for the abscence of weak symbols ----- {{{
37 // We don't have a direct equivalent of weak symbols when using MSVC, but we can
38 // use the /alternatename directive to tell the linker to default a specific
39 // symbol to a specific value, which works nicely for allocator hooks and
40 // __asan_default_options().
41 void __sanitizer_default_malloc_hook(void *ptr
, uptr size
) { }
42 void __sanitizer_default_free_hook(void *ptr
) { }
43 const char* __asan_default_default_options() { return ""; }
44 const char* __asan_default_default_suppressions() { return ""; }
45 void __asan_default_on_error() {}
46 #pragma comment(linker, "/alternatename:___sanitizer_malloc_hook=___sanitizer_default_malloc_hook") // NOLINT
47 #pragma comment(linker, "/alternatename:___sanitizer_free_hook=___sanitizer_default_free_hook") // NOLINT
48 #pragma comment(linker, "/alternatename:___asan_default_options=___asan_default_default_options") // NOLINT
49 #pragma comment(linker, "/alternatename:___asan_default_suppressions=___asan_default_default_suppressions") // NOLINT
50 #pragma comment(linker, "/alternatename:___asan_on_error=___asan_default_on_error") // NOLINT
54 // ---------------------- Windows-specific inteceptors ---------------- {{{
55 INTERCEPTOR_WINAPI(void, RaiseException
, void *a
, void *b
, void *c
, void *d
) {
56 CHECK(REAL(RaiseException
));
57 __asan_handle_no_return();
58 REAL(RaiseException
)(a
, b
, c
, d
);
61 INTERCEPTOR(int, _except_handler3
, void *a
, void *b
, void *c
, void *d
) {
62 CHECK(REAL(_except_handler3
));
63 __asan_handle_no_return();
64 return REAL(_except_handler3
)(a
, b
, c
, d
);
68 // This handler is named differently in -MT and -MD CRTs.
69 #define _except_handler4 _except_handler4_common
71 INTERCEPTOR(int, _except_handler4
, void *a
, void *b
, void *c
, void *d
) {
72 CHECK(REAL(_except_handler4
));
73 __asan_handle_no_return();
74 return REAL(_except_handler4
)(a
, b
, c
, d
);
77 static thread_return_t THREAD_CALLING_CONV
asan_thread_start(void *arg
) {
78 AsanThread
*t
= (AsanThread
*)arg
;
80 return t
->ThreadStart(GetTid(), /* signal_thread_is_registered */ nullptr);
83 INTERCEPTOR_WINAPI(DWORD
, CreateThread
,
84 void* security
, uptr stack_size
,
85 DWORD (__stdcall
*start_routine
)(void*), void* arg
,
86 DWORD thr_flags
, void* tid
) {
87 // Strict init-order checking is thread-hostile.
88 if (flags()->strict_init_order
)
89 StopInitOrderChecking();
90 GET_STACK_TRACE_THREAD
;
91 // FIXME: The CreateThread interceptor is not the same as a pthread_create
92 // one. This is a bandaid fix for PR22025.
93 bool detached
= false; // FIXME: how can we determine it on Windows?
94 u32 current_tid
= GetCurrentTidOrInvalid();
96 AsanThread::Create(start_routine
, arg
, current_tid
, &stack
, detached
);
97 return REAL(CreateThread
)(security
, stack_size
,
98 asan_thread_start
, t
, thr_flags
, tid
);
102 BlockingMutex
mu_for_thread_tracking(LINKER_INITIALIZED
);
104 void EnsureWorkerThreadRegistered() {
105 // FIXME: GetCurrentThread relies on TSD, which might not play well with
106 // system thread pools. We might want to use something like reference
107 // counting to zero out GetCurrentThread() underlying storage when the last
108 // work item finishes? Or can we disable reclaiming of threads in the pool?
109 BlockingMutexLock
l(&mu_for_thread_tracking
);
110 if (__asan::GetCurrentThread())
113 AsanThread
*t
= AsanThread::Create(
114 /* start_routine */ nullptr, /* arg */ nullptr,
115 /* parent_tid */ -1, /* stack */ nullptr, /* detached */ true);
117 asanThreadRegistry().StartThread(t
->tid(), 0, 0);
122 INTERCEPTOR_WINAPI(DWORD
, NtWaitForWorkViaWorkerFactory
, DWORD a
, DWORD b
) {
123 // NtWaitForWorkViaWorkerFactory is called from system worker pool threads to
124 // query work scheduled by BindIoCompletionCallback, QueueUserWorkItem, etc.
125 // System worker pool threads are created at arbitraty point in time and
126 // without using CreateThread, so we wrap NtWaitForWorkViaWorkerFactory
127 // instead and don't register a specific parent_tid/stack.
128 EnsureWorkerThreadRegistered();
129 return REAL(NtWaitForWorkViaWorkerFactory
)(a
, b
);
136 void InitializePlatformInterceptors() {
137 ASAN_INTERCEPT_FUNC(CreateThread
);
138 ASAN_INTERCEPT_FUNC(RaiseException
);
139 ASAN_INTERCEPT_FUNC(_except_handler3
);
140 ASAN_INTERCEPT_FUNC(_except_handler4
);
142 // NtWaitForWorkViaWorkerFactory is always linked dynamically.
143 CHECK(::__interception::OverrideFunction(
144 "NtWaitForWorkViaWorkerFactory",
145 (uptr
)WRAP(NtWaitForWorkViaWorkerFactory
),
146 (uptr
*)&REAL(NtWaitForWorkViaWorkerFactory
)));
149 // ---------------------- TSD ---------------- {{{
150 static bool tsd_key_inited
= false;
152 static __declspec(thread
) void *fake_tsd
= 0;
154 void AsanTSDInit(void (*destructor
)(void *tsd
)) {
155 // FIXME: we're ignoring the destructor for now.
156 tsd_key_inited
= true;
160 CHECK(tsd_key_inited
);
164 void AsanTSDSet(void *tsd
) {
165 CHECK(tsd_key_inited
);
169 void PlatformTSDDtor(void *tsd
) {
170 AsanThread::TSDDtor(tsd
);
174 // ---------------------- Various stuff ---------------- {{{
175 void DisableReexec() {
176 // No need to re-exec on Windows.
180 // No need to re-exec on Windows.
183 void *AsanDoesNotSupportStaticLinkage() {
185 #error Please build the runtime with a non-debug CRT: /MD or /MT
190 void AsanCheckDynamicRTPrereqs() {}
192 void AsanCheckIncompatibleRT() {}
194 void ReadContextStack(void *context
, uptr
*stack
, uptr
*ssize
) {
198 void AsanOnDeadlySignal(int, void *siginfo
, void *context
) {
202 static LPTOP_LEVEL_EXCEPTION_FILTER default_seh_handler
;
204 static long WINAPI
SEHHandler(EXCEPTION_POINTERS
*info
) {
205 EXCEPTION_RECORD
*exception_record
= info
->ExceptionRecord
;
206 CONTEXT
*context
= info
->ContextRecord
;
208 if (exception_record
->ExceptionCode
== EXCEPTION_ACCESS_VIOLATION
||
209 exception_record
->ExceptionCode
== EXCEPTION_IN_PAGE_ERROR
) {
210 const char *description
=
211 (exception_record
->ExceptionCode
== EXCEPTION_ACCESS_VIOLATION
)
214 SignalContext sig
= SignalContext::Create(exception_record
, context
);
215 ReportDeadlySignal(description
, sig
);
218 // FIXME: Handle EXCEPTION_STACK_OVERFLOW here.
220 return default_seh_handler(info
);
223 // We want to install our own exception handler (EH) to print helpful reports
224 // on access violations and whatnot. Unfortunately, the CRT initializers assume
225 // they are run before any user code and drop any previously-installed EHs on
226 // the floor, so we can't install our handler inside __asan_init.
227 // (See crt0dat.c in the CRT sources for the details)
229 // Things get even more complicated with the dynamic runtime, as it finishes its
230 // initialization before the .exe module CRT begins to initialize.
232 // For the static runtime (-MT), it's enough to put a callback to
233 // __asan_set_seh_filter in the last section for C initializers.
235 // For the dynamic runtime (-MD), we want link the same
236 // asan_dynamic_runtime_thunk.lib to all the modules, thus __asan_set_seh_filter
237 // will be called for each instrumented module. This ensures that at least one
238 // __asan_set_seh_filter call happens after the .exe module CRT is initialized.
239 extern "C" SANITIZER_INTERFACE_ATTRIBUTE
240 int __asan_set_seh_filter() {
241 // We should only store the previous handler if it's not our own handler in
242 // order to avoid loops in the EH chain.
243 auto prev_seh_handler
= SetUnhandledExceptionFilter(SEHHandler
);
244 if (prev_seh_handler
!= &SEHHandler
)
245 default_seh_handler
= prev_seh_handler
;
250 // Put a pointer to __asan_set_seh_filter at the end of the global list
251 // of C initializers, after the default EH is set by the CRT.
252 #pragma section(".CRT$XIZ", long, read) // NOLINT
253 __declspec(allocate(".CRT$XIZ"))
254 int (*__intercept_seh
)() = __asan_set_seh_filter
;
257 } // namespace __asan