1*7c3d14c8STreehugger Robot //===-- asan_win.cc -------------------------------------------------------===//
2*7c3d14c8STreehugger Robot //
3*7c3d14c8STreehugger Robot // The LLVM Compiler Infrastructure
4*7c3d14c8STreehugger Robot //
5*7c3d14c8STreehugger Robot // This file is distributed under the University of Illinois Open Source
6*7c3d14c8STreehugger Robot // License. See LICENSE.TXT for details.
7*7c3d14c8STreehugger Robot //
8*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
9*7c3d14c8STreehugger Robot //
10*7c3d14c8STreehugger Robot // This file is a part of AddressSanitizer, an address sanity checker.
11*7c3d14c8STreehugger Robot //
12*7c3d14c8STreehugger Robot // Windows-specific details.
13*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
14*7c3d14c8STreehugger Robot
15*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_platform.h"
16*7c3d14c8STreehugger Robot #if SANITIZER_WINDOWS
17*7c3d14c8STreehugger Robot #define WIN32_LEAN_AND_MEAN
18*7c3d14c8STreehugger Robot #include <windows.h>
19*7c3d14c8STreehugger Robot
20*7c3d14c8STreehugger Robot #include <stdlib.h>
21*7c3d14c8STreehugger Robot
22*7c3d14c8STreehugger Robot #include "asan_interceptors.h"
23*7c3d14c8STreehugger Robot #include "asan_internal.h"
24*7c3d14c8STreehugger Robot #include "asan_report.h"
25*7c3d14c8STreehugger Robot #include "asan_stack.h"
26*7c3d14c8STreehugger Robot #include "asan_thread.h"
27*7c3d14c8STreehugger Robot #include "asan_mapping.h"
28*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_libc.h"
29*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_mutex.h"
30*7c3d14c8STreehugger Robot
31*7c3d14c8STreehugger Robot using namespace __asan; // NOLINT
32*7c3d14c8STreehugger Robot
33*7c3d14c8STreehugger Robot extern "C" {
34*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE
__asan_should_detect_stack_use_after_return()35*7c3d14c8STreehugger Robot int __asan_should_detect_stack_use_after_return() {
36*7c3d14c8STreehugger Robot __asan_init();
37*7c3d14c8STreehugger Robot return __asan_option_detect_stack_use_after_return;
38*7c3d14c8STreehugger Robot }
39*7c3d14c8STreehugger Robot
40*7c3d14c8STreehugger Robot // -------------------- A workaround for the abscence of weak symbols ----- {{{
41*7c3d14c8STreehugger Robot // We don't have a direct equivalent of weak symbols when using MSVC, but we can
42*7c3d14c8STreehugger Robot // use the /alternatename directive to tell the linker to default a specific
43*7c3d14c8STreehugger Robot // symbol to a specific value, which works nicely for allocator hooks and
44*7c3d14c8STreehugger Robot // __asan_default_options().
__sanitizer_default_malloc_hook(void * ptr,uptr size)45*7c3d14c8STreehugger Robot void __sanitizer_default_malloc_hook(void *ptr, uptr size) { }
__sanitizer_default_free_hook(void * ptr)46*7c3d14c8STreehugger Robot void __sanitizer_default_free_hook(void *ptr) { }
__asan_default_default_options()47*7c3d14c8STreehugger Robot const char* __asan_default_default_options() { return ""; }
__asan_default_default_suppressions()48*7c3d14c8STreehugger Robot const char* __asan_default_default_suppressions() { return ""; }
__asan_default_on_error()49*7c3d14c8STreehugger Robot void __asan_default_on_error() {}
50*7c3d14c8STreehugger Robot // 64-bit msvc will not prepend an underscore for symbols.
51*7c3d14c8STreehugger Robot #ifdef _WIN64
52*7c3d14c8STreehugger Robot #pragma comment(linker, "/alternatename:__sanitizer_malloc_hook=__sanitizer_default_malloc_hook") // NOLINT
53*7c3d14c8STreehugger Robot #pragma comment(linker, "/alternatename:__sanitizer_free_hook=__sanitizer_default_free_hook") // NOLINT
54*7c3d14c8STreehugger Robot #pragma comment(linker, "/alternatename:__asan_default_options=__asan_default_default_options") // NOLINT
55*7c3d14c8STreehugger Robot #pragma comment(linker, "/alternatename:__asan_default_suppressions=__asan_default_default_suppressions") // NOLINT
56*7c3d14c8STreehugger Robot #pragma comment(linker, "/alternatename:__asan_on_error=__asan_default_on_error") // NOLINT
57*7c3d14c8STreehugger Robot #else
58*7c3d14c8STreehugger Robot #pragma comment(linker, "/alternatename:___sanitizer_malloc_hook=___sanitizer_default_malloc_hook") // NOLINT
59*7c3d14c8STreehugger Robot #pragma comment(linker, "/alternatename:___sanitizer_free_hook=___sanitizer_default_free_hook") // NOLINT
60*7c3d14c8STreehugger Robot #pragma comment(linker, "/alternatename:___asan_default_options=___asan_default_default_options") // NOLINT
61*7c3d14c8STreehugger Robot #pragma comment(linker, "/alternatename:___asan_default_suppressions=___asan_default_default_suppressions") // NOLINT
62*7c3d14c8STreehugger Robot #pragma comment(linker, "/alternatename:___asan_on_error=___asan_default_on_error") // NOLINT
63*7c3d14c8STreehugger Robot #endif
64*7c3d14c8STreehugger Robot // }}}
65*7c3d14c8STreehugger Robot } // extern "C"
66*7c3d14c8STreehugger Robot
67*7c3d14c8STreehugger Robot // ---------------------- Windows-specific inteceptors ---------------- {{{
INTERCEPTOR_WINAPI(void,RaiseException,void * a,void * b,void * c,void * d)68*7c3d14c8STreehugger Robot INTERCEPTOR_WINAPI(void, RaiseException, void *a, void *b, void *c, void *d) {
69*7c3d14c8STreehugger Robot CHECK(REAL(RaiseException));
70*7c3d14c8STreehugger Robot __asan_handle_no_return();
71*7c3d14c8STreehugger Robot REAL(RaiseException)(a, b, c, d);
72*7c3d14c8STreehugger Robot }
73*7c3d14c8STreehugger Robot
74*7c3d14c8STreehugger Robot // TODO(wwchrome): Win64 has no _except_handler3/4.
75*7c3d14c8STreehugger Robot // Need to implement _C_specific_handler instead.
76*7c3d14c8STreehugger Robot #ifndef _WIN64
INTERCEPTOR(int,_except_handler3,void * a,void * b,void * c,void * d)77*7c3d14c8STreehugger Robot INTERCEPTOR(int, _except_handler3, void *a, void *b, void *c, void *d) {
78*7c3d14c8STreehugger Robot CHECK(REAL(_except_handler3));
79*7c3d14c8STreehugger Robot __asan_handle_no_return();
80*7c3d14c8STreehugger Robot return REAL(_except_handler3)(a, b, c, d);
81*7c3d14c8STreehugger Robot }
82*7c3d14c8STreehugger Robot
83*7c3d14c8STreehugger Robot #if ASAN_DYNAMIC
84*7c3d14c8STreehugger Robot // This handler is named differently in -MT and -MD CRTs.
85*7c3d14c8STreehugger Robot #define _except_handler4 _except_handler4_common
86*7c3d14c8STreehugger Robot #endif
INTERCEPTOR(int,_except_handler4,void * a,void * b,void * c,void * d)87*7c3d14c8STreehugger Robot INTERCEPTOR(int, _except_handler4, void *a, void *b, void *c, void *d) {
88*7c3d14c8STreehugger Robot CHECK(REAL(_except_handler4));
89*7c3d14c8STreehugger Robot __asan_handle_no_return();
90*7c3d14c8STreehugger Robot return REAL(_except_handler4)(a, b, c, d);
91*7c3d14c8STreehugger Robot }
92*7c3d14c8STreehugger Robot #endif
93*7c3d14c8STreehugger Robot
asan_thread_start(void * arg)94*7c3d14c8STreehugger Robot static thread_return_t THREAD_CALLING_CONV asan_thread_start(void *arg) {
95*7c3d14c8STreehugger Robot AsanThread *t = (AsanThread*)arg;
96*7c3d14c8STreehugger Robot SetCurrentThread(t);
97*7c3d14c8STreehugger Robot return t->ThreadStart(GetTid(), /* signal_thread_is_registered */ nullptr);
98*7c3d14c8STreehugger Robot }
99*7c3d14c8STreehugger Robot
INTERCEPTOR_WINAPI(DWORD,CreateThread,void * security,uptr stack_size,DWORD (__stdcall * start_routine)(void *),void * arg,DWORD thr_flags,void * tid)100*7c3d14c8STreehugger Robot INTERCEPTOR_WINAPI(DWORD, CreateThread,
101*7c3d14c8STreehugger Robot void* security, uptr stack_size,
102*7c3d14c8STreehugger Robot DWORD (__stdcall *start_routine)(void*), void* arg,
103*7c3d14c8STreehugger Robot DWORD thr_flags, void* tid) {
104*7c3d14c8STreehugger Robot // Strict init-order checking is thread-hostile.
105*7c3d14c8STreehugger Robot if (flags()->strict_init_order)
106*7c3d14c8STreehugger Robot StopInitOrderChecking();
107*7c3d14c8STreehugger Robot GET_STACK_TRACE_THREAD;
108*7c3d14c8STreehugger Robot // FIXME: The CreateThread interceptor is not the same as a pthread_create
109*7c3d14c8STreehugger Robot // one. This is a bandaid fix for PR22025.
110*7c3d14c8STreehugger Robot bool detached = false; // FIXME: how can we determine it on Windows?
111*7c3d14c8STreehugger Robot u32 current_tid = GetCurrentTidOrInvalid();
112*7c3d14c8STreehugger Robot AsanThread *t =
113*7c3d14c8STreehugger Robot AsanThread::Create(start_routine, arg, current_tid, &stack, detached);
114*7c3d14c8STreehugger Robot return REAL(CreateThread)(security, stack_size,
115*7c3d14c8STreehugger Robot asan_thread_start, t, thr_flags, tid);
116*7c3d14c8STreehugger Robot }
117*7c3d14c8STreehugger Robot
118*7c3d14c8STreehugger Robot namespace {
119*7c3d14c8STreehugger Robot BlockingMutex mu_for_thread_tracking(LINKER_INITIALIZED);
120*7c3d14c8STreehugger Robot
EnsureWorkerThreadRegistered()121*7c3d14c8STreehugger Robot void EnsureWorkerThreadRegistered() {
122*7c3d14c8STreehugger Robot // FIXME: GetCurrentThread relies on TSD, which might not play well with
123*7c3d14c8STreehugger Robot // system thread pools. We might want to use something like reference
124*7c3d14c8STreehugger Robot // counting to zero out GetCurrentThread() underlying storage when the last
125*7c3d14c8STreehugger Robot // work item finishes? Or can we disable reclaiming of threads in the pool?
126*7c3d14c8STreehugger Robot BlockingMutexLock l(&mu_for_thread_tracking);
127*7c3d14c8STreehugger Robot if (__asan::GetCurrentThread())
128*7c3d14c8STreehugger Robot return;
129*7c3d14c8STreehugger Robot
130*7c3d14c8STreehugger Robot AsanThread *t = AsanThread::Create(
131*7c3d14c8STreehugger Robot /* start_routine */ nullptr, /* arg */ nullptr,
132*7c3d14c8STreehugger Robot /* parent_tid */ -1, /* stack */ nullptr, /* detached */ true);
133*7c3d14c8STreehugger Robot t->Init();
134*7c3d14c8STreehugger Robot asanThreadRegistry().StartThread(t->tid(), 0, 0);
135*7c3d14c8STreehugger Robot SetCurrentThread(t);
136*7c3d14c8STreehugger Robot }
137*7c3d14c8STreehugger Robot } // namespace
138*7c3d14c8STreehugger Robot
INTERCEPTOR_WINAPI(DWORD,NtWaitForWorkViaWorkerFactory,DWORD a,DWORD b)139*7c3d14c8STreehugger Robot INTERCEPTOR_WINAPI(DWORD, NtWaitForWorkViaWorkerFactory, DWORD a, DWORD b) {
140*7c3d14c8STreehugger Robot // NtWaitForWorkViaWorkerFactory is called from system worker pool threads to
141*7c3d14c8STreehugger Robot // query work scheduled by BindIoCompletionCallback, QueueUserWorkItem, etc.
142*7c3d14c8STreehugger Robot // System worker pool threads are created at arbitraty point in time and
143*7c3d14c8STreehugger Robot // without using CreateThread, so we wrap NtWaitForWorkViaWorkerFactory
144*7c3d14c8STreehugger Robot // instead and don't register a specific parent_tid/stack.
145*7c3d14c8STreehugger Robot EnsureWorkerThreadRegistered();
146*7c3d14c8STreehugger Robot return REAL(NtWaitForWorkViaWorkerFactory)(a, b);
147*7c3d14c8STreehugger Robot }
148*7c3d14c8STreehugger Robot
149*7c3d14c8STreehugger Robot // }}}
150*7c3d14c8STreehugger Robot
151*7c3d14c8STreehugger Robot namespace __asan {
152*7c3d14c8STreehugger Robot
InitializePlatformInterceptors()153*7c3d14c8STreehugger Robot void InitializePlatformInterceptors() {
154*7c3d14c8STreehugger Robot ASAN_INTERCEPT_FUNC(CreateThread);
155*7c3d14c8STreehugger Robot ASAN_INTERCEPT_FUNC(RaiseException);
156*7c3d14c8STreehugger Robot
157*7c3d14c8STreehugger Robot // TODO(wwchrome): Win64 uses _C_specific_handler instead.
158*7c3d14c8STreehugger Robot #ifndef _WIN64
159*7c3d14c8STreehugger Robot ASAN_INTERCEPT_FUNC(_except_handler3);
160*7c3d14c8STreehugger Robot ASAN_INTERCEPT_FUNC(_except_handler4);
161*7c3d14c8STreehugger Robot #endif
162*7c3d14c8STreehugger Robot
163*7c3d14c8STreehugger Robot // NtWaitForWorkViaWorkerFactory is always linked dynamically.
164*7c3d14c8STreehugger Robot CHECK(::__interception::OverrideFunction(
165*7c3d14c8STreehugger Robot "NtWaitForWorkViaWorkerFactory",
166*7c3d14c8STreehugger Robot (uptr)WRAP(NtWaitForWorkViaWorkerFactory),
167*7c3d14c8STreehugger Robot (uptr *)&REAL(NtWaitForWorkViaWorkerFactory)));
168*7c3d14c8STreehugger Robot }
169*7c3d14c8STreehugger Robot
AsanApplyToGlobals(globals_op_fptr op,const void * needle)170*7c3d14c8STreehugger Robot void AsanApplyToGlobals(globals_op_fptr op, const void *needle) {
171*7c3d14c8STreehugger Robot UNIMPLEMENTED();
172*7c3d14c8STreehugger Robot }
173*7c3d14c8STreehugger Robot
174*7c3d14c8STreehugger Robot // ---------------------- TSD ---------------- {{{
175*7c3d14c8STreehugger Robot static bool tsd_key_inited = false;
176*7c3d14c8STreehugger Robot
177*7c3d14c8STreehugger Robot static __declspec(thread) void *fake_tsd = 0;
178*7c3d14c8STreehugger Robot
AsanTSDInit(void (* destructor)(void * tsd))179*7c3d14c8STreehugger Robot void AsanTSDInit(void (*destructor)(void *tsd)) {
180*7c3d14c8STreehugger Robot // FIXME: we're ignoring the destructor for now.
181*7c3d14c8STreehugger Robot tsd_key_inited = true;
182*7c3d14c8STreehugger Robot }
183*7c3d14c8STreehugger Robot
AsanTSDGet()184*7c3d14c8STreehugger Robot void *AsanTSDGet() {
185*7c3d14c8STreehugger Robot CHECK(tsd_key_inited);
186*7c3d14c8STreehugger Robot return fake_tsd;
187*7c3d14c8STreehugger Robot }
188*7c3d14c8STreehugger Robot
AsanTSDSet(void * tsd)189*7c3d14c8STreehugger Robot void AsanTSDSet(void *tsd) {
190*7c3d14c8STreehugger Robot CHECK(tsd_key_inited);
191*7c3d14c8STreehugger Robot fake_tsd = tsd;
192*7c3d14c8STreehugger Robot }
193*7c3d14c8STreehugger Robot
PlatformTSDDtor(void * tsd)194*7c3d14c8STreehugger Robot void PlatformTSDDtor(void *tsd) {
195*7c3d14c8STreehugger Robot AsanThread::TSDDtor(tsd);
196*7c3d14c8STreehugger Robot }
197*7c3d14c8STreehugger Robot // }}}
198*7c3d14c8STreehugger Robot
199*7c3d14c8STreehugger Robot // ---------------------- Various stuff ---------------- {{{
AsanDoesNotSupportStaticLinkage()200*7c3d14c8STreehugger Robot void *AsanDoesNotSupportStaticLinkage() {
201*7c3d14c8STreehugger Robot #if defined(_DEBUG)
202*7c3d14c8STreehugger Robot #error Please build the runtime with a non-debug CRT: /MD or /MT
203*7c3d14c8STreehugger Robot #endif
204*7c3d14c8STreehugger Robot return 0;
205*7c3d14c8STreehugger Robot }
206*7c3d14c8STreehugger Robot
AsanCheckDynamicRTPrereqs()207*7c3d14c8STreehugger Robot void AsanCheckDynamicRTPrereqs() {}
208*7c3d14c8STreehugger Robot
AsanCheckIncompatibleRT()209*7c3d14c8STreehugger Robot void AsanCheckIncompatibleRT() {}
210*7c3d14c8STreehugger Robot
ReadContextStack(void * context,uptr * stack,uptr * ssize)211*7c3d14c8STreehugger Robot void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
212*7c3d14c8STreehugger Robot UNIMPLEMENTED();
213*7c3d14c8STreehugger Robot }
214*7c3d14c8STreehugger Robot
AsanOnDeadlySignal(int,void * siginfo,void * context)215*7c3d14c8STreehugger Robot void AsanOnDeadlySignal(int, void *siginfo, void *context) {
216*7c3d14c8STreehugger Robot UNIMPLEMENTED();
217*7c3d14c8STreehugger Robot }
218*7c3d14c8STreehugger Robot
219*7c3d14c8STreehugger Robot #if SANITIZER_WINDOWS64
220*7c3d14c8STreehugger Robot // Exception handler for dealing with shadow memory.
221*7c3d14c8STreehugger Robot static LONG CALLBACK
ShadowExceptionHandler(PEXCEPTION_POINTERS exception_pointers)222*7c3d14c8STreehugger Robot ShadowExceptionHandler(PEXCEPTION_POINTERS exception_pointers) {
223*7c3d14c8STreehugger Robot static uptr page_size = GetPageSizeCached();
224*7c3d14c8STreehugger Robot static uptr alloc_granularity = GetMmapGranularity();
225*7c3d14c8STreehugger Robot // Only handle access violations.
226*7c3d14c8STreehugger Robot if (exception_pointers->ExceptionRecord->ExceptionCode !=
227*7c3d14c8STreehugger Robot EXCEPTION_ACCESS_VIOLATION) {
228*7c3d14c8STreehugger Robot return EXCEPTION_CONTINUE_SEARCH;
229*7c3d14c8STreehugger Robot }
230*7c3d14c8STreehugger Robot
231*7c3d14c8STreehugger Robot // Only handle access violations that land within the shadow memory.
232*7c3d14c8STreehugger Robot uptr addr =
233*7c3d14c8STreehugger Robot (uptr)(exception_pointers->ExceptionRecord->ExceptionInformation[1]);
234*7c3d14c8STreehugger Robot
235*7c3d14c8STreehugger Robot // Check valid shadow range.
236*7c3d14c8STreehugger Robot if (!AddrIsInShadow(addr)) return EXCEPTION_CONTINUE_SEARCH;
237*7c3d14c8STreehugger Robot
238*7c3d14c8STreehugger Robot // This is an access violation while trying to read from the shadow. Commit
239*7c3d14c8STreehugger Robot // the relevant page and let execution continue.
240*7c3d14c8STreehugger Robot
241*7c3d14c8STreehugger Robot // Determine the address of the page that is being accessed.
242*7c3d14c8STreehugger Robot uptr page = RoundDownTo(addr, page_size);
243*7c3d14c8STreehugger Robot
244*7c3d14c8STreehugger Robot // Query the existing page.
245*7c3d14c8STreehugger Robot MEMORY_BASIC_INFORMATION mem_info = {};
246*7c3d14c8STreehugger Robot if (::VirtualQuery((LPVOID)page, &mem_info, sizeof(mem_info)) == 0)
247*7c3d14c8STreehugger Robot return EXCEPTION_CONTINUE_SEARCH;
248*7c3d14c8STreehugger Robot
249*7c3d14c8STreehugger Robot // Commit the page.
250*7c3d14c8STreehugger Robot uptr result =
251*7c3d14c8STreehugger Robot (uptr)::VirtualAlloc((LPVOID)page, page_size, MEM_COMMIT, PAGE_READWRITE);
252*7c3d14c8STreehugger Robot if (result != page) return EXCEPTION_CONTINUE_SEARCH;
253*7c3d14c8STreehugger Robot
254*7c3d14c8STreehugger Robot // The page mapping succeeded, so continue execution as usual.
255*7c3d14c8STreehugger Robot return EXCEPTION_CONTINUE_EXECUTION;
256*7c3d14c8STreehugger Robot }
257*7c3d14c8STreehugger Robot
258*7c3d14c8STreehugger Robot #endif
259*7c3d14c8STreehugger Robot
InitializePlatformExceptionHandlers()260*7c3d14c8STreehugger Robot void InitializePlatformExceptionHandlers() {
261*7c3d14c8STreehugger Robot #if SANITIZER_WINDOWS64
262*7c3d14c8STreehugger Robot // On Win64, we map memory on demand with access violation handler.
263*7c3d14c8STreehugger Robot // Install our exception handler.
264*7c3d14c8STreehugger Robot CHECK(AddVectoredExceptionHandler(TRUE, &ShadowExceptionHandler));
265*7c3d14c8STreehugger Robot #endif
266*7c3d14c8STreehugger Robot }
267*7c3d14c8STreehugger Robot
268*7c3d14c8STreehugger Robot static LPTOP_LEVEL_EXCEPTION_FILTER default_seh_handler;
269*7c3d14c8STreehugger Robot
SEHHandler(EXCEPTION_POINTERS * info)270*7c3d14c8STreehugger Robot static long WINAPI SEHHandler(EXCEPTION_POINTERS *info) {
271*7c3d14c8STreehugger Robot EXCEPTION_RECORD *exception_record = info->ExceptionRecord;
272*7c3d14c8STreehugger Robot CONTEXT *context = info->ContextRecord;
273*7c3d14c8STreehugger Robot
274*7c3d14c8STreehugger Robot if (exception_record->ExceptionCode == EXCEPTION_ACCESS_VIOLATION ||
275*7c3d14c8STreehugger Robot exception_record->ExceptionCode == EXCEPTION_IN_PAGE_ERROR) {
276*7c3d14c8STreehugger Robot const char *description =
277*7c3d14c8STreehugger Robot (exception_record->ExceptionCode == EXCEPTION_ACCESS_VIOLATION)
278*7c3d14c8STreehugger Robot ? "access-violation"
279*7c3d14c8STreehugger Robot : "in-page-error";
280*7c3d14c8STreehugger Robot SignalContext sig = SignalContext::Create(exception_record, context);
281*7c3d14c8STreehugger Robot ReportDeadlySignal(description, sig);
282*7c3d14c8STreehugger Robot }
283*7c3d14c8STreehugger Robot
284*7c3d14c8STreehugger Robot // FIXME: Handle EXCEPTION_STACK_OVERFLOW here.
285*7c3d14c8STreehugger Robot
286*7c3d14c8STreehugger Robot return default_seh_handler(info);
287*7c3d14c8STreehugger Robot }
288*7c3d14c8STreehugger Robot
289*7c3d14c8STreehugger Robot // We want to install our own exception handler (EH) to print helpful reports
290*7c3d14c8STreehugger Robot // on access violations and whatnot. Unfortunately, the CRT initializers assume
291*7c3d14c8STreehugger Robot // they are run before any user code and drop any previously-installed EHs on
292*7c3d14c8STreehugger Robot // the floor, so we can't install our handler inside __asan_init.
293*7c3d14c8STreehugger Robot // (See crt0dat.c in the CRT sources for the details)
294*7c3d14c8STreehugger Robot //
295*7c3d14c8STreehugger Robot // Things get even more complicated with the dynamic runtime, as it finishes its
296*7c3d14c8STreehugger Robot // initialization before the .exe module CRT begins to initialize.
297*7c3d14c8STreehugger Robot //
298*7c3d14c8STreehugger Robot // For the static runtime (-MT), it's enough to put a callback to
299*7c3d14c8STreehugger Robot // __asan_set_seh_filter in the last section for C initializers.
300*7c3d14c8STreehugger Robot //
301*7c3d14c8STreehugger Robot // For the dynamic runtime (-MD), we want link the same
302*7c3d14c8STreehugger Robot // asan_dynamic_runtime_thunk.lib to all the modules, thus __asan_set_seh_filter
303*7c3d14c8STreehugger Robot // will be called for each instrumented module. This ensures that at least one
304*7c3d14c8STreehugger Robot // __asan_set_seh_filter call happens after the .exe module CRT is initialized.
305*7c3d14c8STreehugger Robot extern "C" SANITIZER_INTERFACE_ATTRIBUTE
__asan_set_seh_filter()306*7c3d14c8STreehugger Robot int __asan_set_seh_filter() {
307*7c3d14c8STreehugger Robot // We should only store the previous handler if it's not our own handler in
308*7c3d14c8STreehugger Robot // order to avoid loops in the EH chain.
309*7c3d14c8STreehugger Robot auto prev_seh_handler = SetUnhandledExceptionFilter(SEHHandler);
310*7c3d14c8STreehugger Robot if (prev_seh_handler != &SEHHandler)
311*7c3d14c8STreehugger Robot default_seh_handler = prev_seh_handler;
312*7c3d14c8STreehugger Robot return 0;
313*7c3d14c8STreehugger Robot }
314*7c3d14c8STreehugger Robot
315*7c3d14c8STreehugger Robot #if !ASAN_DYNAMIC
316*7c3d14c8STreehugger Robot // The CRT runs initializers in this order:
317*7c3d14c8STreehugger Robot // - C initializers, from XIA to XIZ
318*7c3d14c8STreehugger Robot // - C++ initializers, from XCA to XCZ
319*7c3d14c8STreehugger Robot // Prior to 2015, the CRT set the unhandled exception filter at priority XIY,
320*7c3d14c8STreehugger Robot // near the end of C initialization. Starting in 2015, it was moved to the
321*7c3d14c8STreehugger Robot // beginning of C++ initialization. We set our priority to XCAB to run
322*7c3d14c8STreehugger Robot // immediately after the CRT runs. This way, our exception filter is called
323*7c3d14c8STreehugger Robot // first and we can delegate to their filter if appropriate.
324*7c3d14c8STreehugger Robot #pragma section(".CRT$XCAB", long, read) // NOLINT
325*7c3d14c8STreehugger Robot __declspec(allocate(".CRT$XCAB"))
326*7c3d14c8STreehugger Robot int (*__intercept_seh)() = __asan_set_seh_filter;
327*7c3d14c8STreehugger Robot #endif
328*7c3d14c8STreehugger Robot // }}}
329*7c3d14c8STreehugger Robot } // namespace __asan
330*7c3d14c8STreehugger Robot
331*7c3d14c8STreehugger Robot #endif // _WIN32
332