181ad6265SDimitry Andric //===-- asan_win.cpp 281ad6265SDimitry Andric //------------------------------------------------------===//> 368d75effSDimitry Andric // 468d75effSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 568d75effSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 668d75effSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 768d75effSDimitry Andric // 868d75effSDimitry Andric //===----------------------------------------------------------------------===// 968d75effSDimitry Andric // 1068d75effSDimitry Andric // This file is a part of AddressSanitizer, an address sanity checker. 1168d75effSDimitry Andric // 1268d75effSDimitry Andric // Windows-specific details. 1368d75effSDimitry Andric //===----------------------------------------------------------------------===// 1468d75effSDimitry Andric 1568d75effSDimitry Andric #include "sanitizer_common/sanitizer_platform.h" 1668d75effSDimitry Andric #if SANITIZER_WINDOWS 1768d75effSDimitry Andric # define WIN32_LEAN_AND_MEAN 1868d75effSDimitry Andric # include <stdlib.h> 1981ad6265SDimitry Andric # include <windows.h> 2068d75effSDimitry Andric 2168d75effSDimitry Andric # include "asan_interceptors.h" 2268d75effSDimitry Andric # include "asan_internal.h" 2368d75effSDimitry Andric # include "asan_mapping.h" 2468d75effSDimitry Andric # include "asan_report.h" 2568d75effSDimitry Andric # include "asan_stack.h" 2668d75effSDimitry Andric # include "asan_thread.h" 2768d75effSDimitry Andric # include "sanitizer_common/sanitizer_libc.h" 2868d75effSDimitry Andric # include "sanitizer_common/sanitizer_mutex.h" 2968d75effSDimitry Andric # include "sanitizer_common/sanitizer_win.h" 3068d75effSDimitry Andric # include "sanitizer_common/sanitizer_win_defs.h" 3168d75effSDimitry Andric 3268d75effSDimitry Andric using namespace __asan; 3368d75effSDimitry Andric 3468d75effSDimitry Andric extern "C" { 3568d75effSDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE 3668d75effSDimitry Andric int __asan_should_detect_stack_use_after_return() { 3768d75effSDimitry Andric __asan_init(); 3868d75effSDimitry Andric return __asan_option_detect_stack_use_after_return; 3968d75effSDimitry Andric } 4068d75effSDimitry Andric 4168d75effSDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE 4268d75effSDimitry Andric uptr __asan_get_shadow_memory_dynamic_address() { 4368d75effSDimitry Andric __asan_init(); 4468d75effSDimitry Andric return __asan_shadow_memory_dynamic_address; 4568d75effSDimitry Andric } 4668d75effSDimitry Andric } // extern "C" 4768d75effSDimitry Andric 4868d75effSDimitry Andric // ---------------------- Windows-specific interceptors ---------------- {{{ 4968d75effSDimitry Andric static LPTOP_LEVEL_EXCEPTION_FILTER default_seh_handler; 5068d75effSDimitry Andric static LPTOP_LEVEL_EXCEPTION_FILTER user_seh_handler; 5168d75effSDimitry Andric 5281ad6265SDimitry Andric extern "C" SANITIZER_INTERFACE_ATTRIBUTE long __asan_unhandled_exception_filter( 5381ad6265SDimitry Andric EXCEPTION_POINTERS *info) { 5468d75effSDimitry Andric EXCEPTION_RECORD *exception_record = info->ExceptionRecord; 5568d75effSDimitry Andric CONTEXT *context = info->ContextRecord; 5668d75effSDimitry Andric 5768d75effSDimitry Andric // FIXME: Handle EXCEPTION_STACK_OVERFLOW here. 5868d75effSDimitry Andric 5968d75effSDimitry Andric SignalContext sig(exception_record, context); 6068d75effSDimitry Andric ReportDeadlySignal(sig); 6168d75effSDimitry Andric UNREACHABLE("returned from reporting deadly signal"); 6268d75effSDimitry Andric } 6368d75effSDimitry Andric 6468d75effSDimitry Andric // Wrapper SEH Handler. If the exception should be handled by asan, we call 6568d75effSDimitry Andric // __asan_unhandled_exception_filter, otherwise, we execute the user provided 6668d75effSDimitry Andric // exception handler or the default. 6768d75effSDimitry Andric static long WINAPI SEHHandler(EXCEPTION_POINTERS *info) { 6868d75effSDimitry Andric DWORD exception_code = info->ExceptionRecord->ExceptionCode; 6968d75effSDimitry Andric if (__sanitizer::IsHandledDeadlyException(exception_code)) 7068d75effSDimitry Andric return __asan_unhandled_exception_filter(info); 7168d75effSDimitry Andric if (user_seh_handler) 7268d75effSDimitry Andric return user_seh_handler(info); 7368d75effSDimitry Andric // Bubble out to the default exception filter. 7468d75effSDimitry Andric if (default_seh_handler) 7568d75effSDimitry Andric return default_seh_handler(info); 7668d75effSDimitry Andric return EXCEPTION_CONTINUE_SEARCH; 7768d75effSDimitry Andric } 7868d75effSDimitry Andric 7968d75effSDimitry Andric INTERCEPTOR_WINAPI(LPTOP_LEVEL_EXCEPTION_FILTER, SetUnhandledExceptionFilter, 8068d75effSDimitry Andric LPTOP_LEVEL_EXCEPTION_FILTER ExceptionFilter) { 8168d75effSDimitry Andric CHECK(REAL(SetUnhandledExceptionFilter)); 8268d75effSDimitry Andric if (ExceptionFilter == &SEHHandler) 8368d75effSDimitry Andric return REAL(SetUnhandledExceptionFilter)(ExceptionFilter); 8468d75effSDimitry Andric // We record the user provided exception handler to be called for all the 8568d75effSDimitry Andric // exceptions unhandled by asan. 8668d75effSDimitry Andric Swap(ExceptionFilter, user_seh_handler); 8768d75effSDimitry Andric return ExceptionFilter; 8868d75effSDimitry Andric } 8968d75effSDimitry Andric 9068d75effSDimitry Andric INTERCEPTOR_WINAPI(void, RtlRaiseException, EXCEPTION_RECORD *ExceptionRecord) { 9168d75effSDimitry Andric CHECK(REAL(RtlRaiseException)); 9268d75effSDimitry Andric // This is a noreturn function, unless it's one of the exceptions raised to 9368d75effSDimitry Andric // communicate with the debugger, such as the one from OutputDebugString. 9468d75effSDimitry Andric if (ExceptionRecord->ExceptionCode != DBG_PRINTEXCEPTION_C) 9568d75effSDimitry Andric __asan_handle_no_return(); 9668d75effSDimitry Andric REAL(RtlRaiseException)(ExceptionRecord); 9768d75effSDimitry Andric } 9868d75effSDimitry Andric 9968d75effSDimitry Andric INTERCEPTOR_WINAPI(void, RaiseException, void *a, void *b, void *c, void *d) { 10068d75effSDimitry Andric CHECK(REAL(RaiseException)); 10168d75effSDimitry Andric __asan_handle_no_return(); 10268d75effSDimitry Andric REAL(RaiseException)(a, b, c, d); 10368d75effSDimitry Andric } 10468d75effSDimitry Andric 10568d75effSDimitry Andric #ifdef _WIN64 10668d75effSDimitry Andric 10768d75effSDimitry Andric INTERCEPTOR_WINAPI(EXCEPTION_DISPOSITION, __C_specific_handler, 10868d75effSDimitry Andric _EXCEPTION_RECORD *a, void *b, _CONTEXT *c, 10968d75effSDimitry Andric _DISPATCHER_CONTEXT *d) { 11068d75effSDimitry Andric CHECK(REAL(__C_specific_handler)); 11168d75effSDimitry Andric __asan_handle_no_return(); 11268d75effSDimitry Andric return REAL(__C_specific_handler)(a, b, c, d); 11368d75effSDimitry Andric } 11468d75effSDimitry Andric 11568d75effSDimitry Andric #else 11668d75effSDimitry Andric 11768d75effSDimitry Andric INTERCEPTOR(int, _except_handler3, void *a, void *b, void *c, void *d) { 11868d75effSDimitry Andric CHECK(REAL(_except_handler3)); 11968d75effSDimitry Andric __asan_handle_no_return(); 12068d75effSDimitry Andric return REAL(_except_handler3)(a, b, c, d); 12168d75effSDimitry Andric } 12268d75effSDimitry Andric 12368d75effSDimitry Andric #if ASAN_DYNAMIC 12468d75effSDimitry Andric // This handler is named differently in -MT and -MD CRTs. 12568d75effSDimitry Andric #define _except_handler4 _except_handler4_common 12668d75effSDimitry Andric #endif 12768d75effSDimitry Andric INTERCEPTOR(int, _except_handler4, void *a, void *b, void *c, void *d) { 12868d75effSDimitry Andric CHECK(REAL(_except_handler4)); 12968d75effSDimitry Andric __asan_handle_no_return(); 13068d75effSDimitry Andric return REAL(_except_handler4)(a, b, c, d); 13168d75effSDimitry Andric } 13268d75effSDimitry Andric #endif 13368d75effSDimitry Andric 1345f757f3fSDimitry Andric struct ThreadStartParams { 1355f757f3fSDimitry Andric thread_callback_t start_routine; 1365f757f3fSDimitry Andric void *arg; 1375f757f3fSDimitry Andric }; 1385f757f3fSDimitry Andric 13968d75effSDimitry Andric static thread_return_t THREAD_CALLING_CONV asan_thread_start(void *arg) { 14068d75effSDimitry Andric AsanThread *t = (AsanThread *)arg; 14168d75effSDimitry Andric SetCurrentThread(t); 1425f757f3fSDimitry Andric t->ThreadStart(GetTid()); 1435f757f3fSDimitry Andric 1445f757f3fSDimitry Andric ThreadStartParams params; 1455f757f3fSDimitry Andric t->GetStartData(params); 1465f757f3fSDimitry Andric 1475f757f3fSDimitry Andric auto res = (*params.start_routine)(params.arg); 1485f757f3fSDimitry Andric t->Destroy(); // POSIX calls this from TSD destructor. 1495f757f3fSDimitry Andric return res; 15068d75effSDimitry Andric } 15168d75effSDimitry Andric 15268d75effSDimitry Andric INTERCEPTOR_WINAPI(HANDLE, CreateThread, LPSECURITY_ATTRIBUTES security, 15368d75effSDimitry Andric SIZE_T stack_size, LPTHREAD_START_ROUTINE start_routine, 15468d75effSDimitry Andric void *arg, DWORD thr_flags, DWORD *tid) { 15568d75effSDimitry Andric // Strict init-order checking is thread-hostile. 15668d75effSDimitry Andric if (flags()->strict_init_order) 15768d75effSDimitry Andric StopInitOrderChecking(); 15868d75effSDimitry Andric GET_STACK_TRACE_THREAD; 15968d75effSDimitry Andric // FIXME: The CreateThread interceptor is not the same as a pthread_create 16068d75effSDimitry Andric // one. This is a bandaid fix for PR22025. 16168d75effSDimitry Andric bool detached = false; // FIXME: how can we determine it on Windows? 16268d75effSDimitry Andric u32 current_tid = GetCurrentTidOrInvalid(); 1635f757f3fSDimitry Andric ThreadStartParams params = {start_routine, arg}; 1645f757f3fSDimitry Andric AsanThread *t = AsanThread::Create(params, current_tid, &stack, detached); 16568d75effSDimitry Andric return REAL(CreateThread)(security, stack_size, asan_thread_start, t, 16668d75effSDimitry Andric thr_flags, tid); 16768d75effSDimitry Andric } 16868d75effSDimitry Andric 16968d75effSDimitry Andric // }}} 17068d75effSDimitry Andric 17168d75effSDimitry Andric namespace __asan { 17268d75effSDimitry Andric 17368d75effSDimitry Andric void InitializePlatformInterceptors() { 17406c3fb27SDimitry Andric __interception::SetErrorReportCallback(Report); 17506c3fb27SDimitry Andric 17668d75effSDimitry Andric // The interceptors were not designed to be removable, so we have to keep this 17768d75effSDimitry Andric // module alive for the life of the process. 17868d75effSDimitry Andric HMODULE pinned; 17968d75effSDimitry Andric CHECK(GetModuleHandleExW( 18068d75effSDimitry Andric GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_PIN, 18168d75effSDimitry Andric (LPCWSTR)&InitializePlatformInterceptors, &pinned)); 18268d75effSDimitry Andric 18368d75effSDimitry Andric ASAN_INTERCEPT_FUNC(CreateThread); 18468d75effSDimitry Andric ASAN_INTERCEPT_FUNC(SetUnhandledExceptionFilter); 18568d75effSDimitry Andric 18668d75effSDimitry Andric #ifdef _WIN64 18768d75effSDimitry Andric ASAN_INTERCEPT_FUNC(__C_specific_handler); 18868d75effSDimitry Andric #else 18968d75effSDimitry Andric ASAN_INTERCEPT_FUNC(_except_handler3); 19068d75effSDimitry Andric ASAN_INTERCEPT_FUNC(_except_handler4); 19168d75effSDimitry Andric #endif 19268d75effSDimitry Andric 19368d75effSDimitry Andric // Try to intercept kernel32!RaiseException, and if that fails, intercept 19468d75effSDimitry Andric // ntdll!RtlRaiseException instead. 19568d75effSDimitry Andric if (!::__interception::OverrideFunction("RaiseException", 19668d75effSDimitry Andric (uptr)WRAP(RaiseException), 19768d75effSDimitry Andric (uptr *)&REAL(RaiseException))) { 19868d75effSDimitry Andric CHECK(::__interception::OverrideFunction("RtlRaiseException", 19968d75effSDimitry Andric (uptr)WRAP(RtlRaiseException), 20068d75effSDimitry Andric (uptr *)&REAL(RtlRaiseException))); 20168d75effSDimitry Andric } 20268d75effSDimitry Andric } 20368d75effSDimitry Andric 20481ad6265SDimitry Andric void InstallAtExitCheckLeaks() {} 20581ad6265SDimitry Andric 2065f757f3fSDimitry Andric void InstallAtForkHandler() {} 2075f757f3fSDimitry Andric 20868d75effSDimitry Andric void AsanApplyToGlobals(globals_op_fptr op, const void *needle) { 20968d75effSDimitry Andric UNIMPLEMENTED(); 21068d75effSDimitry Andric } 21168d75effSDimitry Andric 212e8d8bef9SDimitry Andric void FlushUnneededASanShadowMemory(uptr p, uptr size) { 21306c3fb27SDimitry Andric // Only asan on 64-bit Windows supports committing shadow memory on demand. 21406c3fb27SDimitry Andric #if SANITIZER_WINDOWS64 215e8d8bef9SDimitry Andric // Since asan's mapping is compacting, the shadow chunk may be 216e8d8bef9SDimitry Andric // not page-aligned, so we only flush the page-aligned portion. 217e8d8bef9SDimitry Andric ReleaseMemoryPagesToOS(MemToShadow(p), MemToShadow(p + size)); 21806c3fb27SDimitry Andric #endif 219e8d8bef9SDimitry Andric } 220e8d8bef9SDimitry Andric 22168d75effSDimitry Andric // ---------------------- TSD ---------------- {{{ 22268d75effSDimitry Andric static bool tsd_key_inited = false; 22368d75effSDimitry Andric 22468d75effSDimitry Andric static __declspec(thread) void *fake_tsd = 0; 22568d75effSDimitry Andric 22668d75effSDimitry Andric // https://docs.microsoft.com/en-us/windows/desktop/api/winternl/ns-winternl-_teb 22768d75effSDimitry Andric // "[This structure may be altered in future versions of Windows. Applications 22868d75effSDimitry Andric // should use the alternate functions listed in this topic.]" 22968d75effSDimitry Andric typedef struct _TEB { 23068d75effSDimitry Andric PVOID Reserved1[12]; 23168d75effSDimitry Andric // PVOID ThreadLocalStoragePointer; is here, at the last field in Reserved1. 23268d75effSDimitry Andric PVOID ProcessEnvironmentBlock; 23368d75effSDimitry Andric PVOID Reserved2[399]; 23468d75effSDimitry Andric BYTE Reserved3[1952]; 23568d75effSDimitry Andric PVOID TlsSlots[64]; 23668d75effSDimitry Andric BYTE Reserved4[8]; 23768d75effSDimitry Andric PVOID Reserved5[26]; 23868d75effSDimitry Andric PVOID ReservedForOle; 23968d75effSDimitry Andric PVOID Reserved6[4]; 24068d75effSDimitry Andric PVOID TlsExpansionSlots; 24168d75effSDimitry Andric } TEB, *PTEB; 24268d75effSDimitry Andric 24368d75effSDimitry Andric constexpr size_t TEB_RESERVED_FIELDS_THREAD_LOCAL_STORAGE_OFFSET = 11; 24468d75effSDimitry Andric BOOL IsTlsInitialized() { 24568d75effSDimitry Andric PTEB teb = (PTEB)NtCurrentTeb(); 24668d75effSDimitry Andric return teb->Reserved1[TEB_RESERVED_FIELDS_THREAD_LOCAL_STORAGE_OFFSET] != 24768d75effSDimitry Andric nullptr; 24868d75effSDimitry Andric } 24968d75effSDimitry Andric 25068d75effSDimitry Andric void AsanTSDInit(void (*destructor)(void *tsd)) { 25168d75effSDimitry Andric // FIXME: we're ignoring the destructor for now. 25268d75effSDimitry Andric tsd_key_inited = true; 25368d75effSDimitry Andric } 25468d75effSDimitry Andric 25568d75effSDimitry Andric void *AsanTSDGet() { 25668d75effSDimitry Andric CHECK(tsd_key_inited); 25768d75effSDimitry Andric return IsTlsInitialized() ? fake_tsd : nullptr; 25868d75effSDimitry Andric } 25968d75effSDimitry Andric 26068d75effSDimitry Andric void AsanTSDSet(void *tsd) { 26168d75effSDimitry Andric CHECK(tsd_key_inited); 26268d75effSDimitry Andric fake_tsd = tsd; 26368d75effSDimitry Andric } 26468d75effSDimitry Andric 26568d75effSDimitry Andric void PlatformTSDDtor(void *tsd) { AsanThread::TSDDtor(tsd); } 26668d75effSDimitry Andric // }}} 26768d75effSDimitry Andric 26868d75effSDimitry Andric // ---------------------- Various stuff ---------------- {{{ 26968d75effSDimitry Andric uptr FindDynamicShadowStart() { 2700eae32dcSDimitry Andric return MapDynamicShadow(MemToShadowSize(kHighMemEnd), ASAN_SHADOW_SCALE, 271*0fca6ea1SDimitry Andric /*min_shadow_base_alignment*/ 0, kHighMemEnd, 272*0fca6ea1SDimitry Andric GetMmapGranularity()); 27368d75effSDimitry Andric } 27468d75effSDimitry Andric 27568d75effSDimitry Andric void AsanCheckDynamicRTPrereqs() {} 27668d75effSDimitry Andric 27768d75effSDimitry Andric void AsanCheckIncompatibleRT() {} 27868d75effSDimitry Andric 27968d75effSDimitry Andric void AsanOnDeadlySignal(int, void *siginfo, void *context) { UNIMPLEMENTED(); } 28068d75effSDimitry Andric 2815ffd83dbSDimitry Andric bool PlatformUnpoisonStacks() { return false; } 2825ffd83dbSDimitry Andric 28368d75effSDimitry Andric #if SANITIZER_WINDOWS64 28468d75effSDimitry Andric // Exception handler for dealing with shadow memory. 28568d75effSDimitry Andric static LONG CALLBACK 28668d75effSDimitry Andric ShadowExceptionHandler(PEXCEPTION_POINTERS exception_pointers) { 28768d75effSDimitry Andric uptr page_size = GetPageSizeCached(); 28868d75effSDimitry Andric // Only handle access violations. 28968d75effSDimitry Andric if (exception_pointers->ExceptionRecord->ExceptionCode != 29068d75effSDimitry Andric EXCEPTION_ACCESS_VIOLATION || 29168d75effSDimitry Andric exception_pointers->ExceptionRecord->NumberParameters < 2) { 29268d75effSDimitry Andric __asan_handle_no_return(); 29368d75effSDimitry Andric return EXCEPTION_CONTINUE_SEARCH; 29468d75effSDimitry Andric } 29568d75effSDimitry Andric 29668d75effSDimitry Andric // Only handle access violations that land within the shadow memory. 29768d75effSDimitry Andric uptr addr = 29868d75effSDimitry Andric (uptr)(exception_pointers->ExceptionRecord->ExceptionInformation[1]); 29968d75effSDimitry Andric 30068d75effSDimitry Andric // Check valid shadow range. 30168d75effSDimitry Andric if (!AddrIsInShadow(addr)) { 30268d75effSDimitry Andric __asan_handle_no_return(); 30368d75effSDimitry Andric return EXCEPTION_CONTINUE_SEARCH; 30468d75effSDimitry Andric } 30568d75effSDimitry Andric 30668d75effSDimitry Andric // This is an access violation while trying to read from the shadow. Commit 30768d75effSDimitry Andric // the relevant page and let execution continue. 30868d75effSDimitry Andric 30968d75effSDimitry Andric // Determine the address of the page that is being accessed. 31068d75effSDimitry Andric uptr page = RoundDownTo(addr, page_size); 31168d75effSDimitry Andric 31268d75effSDimitry Andric // Commit the page. 31368d75effSDimitry Andric uptr result = 31468d75effSDimitry Andric (uptr)::VirtualAlloc((LPVOID)page, page_size, MEM_COMMIT, PAGE_READWRITE); 31568d75effSDimitry Andric if (result != page) 31668d75effSDimitry Andric return EXCEPTION_CONTINUE_SEARCH; 31768d75effSDimitry Andric 31868d75effSDimitry Andric // The page mapping succeeded, so continue execution as usual. 31968d75effSDimitry Andric return EXCEPTION_CONTINUE_EXECUTION; 32068d75effSDimitry Andric } 32168d75effSDimitry Andric 32268d75effSDimitry Andric #endif 32368d75effSDimitry Andric 32468d75effSDimitry Andric void InitializePlatformExceptionHandlers() { 32568d75effSDimitry Andric #if SANITIZER_WINDOWS64 32668d75effSDimitry Andric // On Win64, we map memory on demand with access violation handler. 32768d75effSDimitry Andric // Install our exception handler. 32868d75effSDimitry Andric CHECK(AddVectoredExceptionHandler(TRUE, &ShadowExceptionHandler)); 32968d75effSDimitry Andric #endif 33068d75effSDimitry Andric } 33168d75effSDimitry Andric 33268d75effSDimitry Andric bool IsSystemHeapAddress(uptr addr) { 33368d75effSDimitry Andric return ::HeapValidate(GetProcessHeap(), 0, (void *)addr) != FALSE; 33468d75effSDimitry Andric } 33568d75effSDimitry Andric 33668d75effSDimitry Andric // We want to install our own exception handler (EH) to print helpful reports 33768d75effSDimitry Andric // on access violations and whatnot. Unfortunately, the CRT initializers assume 33868d75effSDimitry Andric // they are run before any user code and drop any previously-installed EHs on 33968d75effSDimitry Andric // the floor, so we can't install our handler inside __asan_init. 34068d75effSDimitry Andric // (See crt0dat.c in the CRT sources for the details) 34168d75effSDimitry Andric // 34268d75effSDimitry Andric // Things get even more complicated with the dynamic runtime, as it finishes its 34368d75effSDimitry Andric // initialization before the .exe module CRT begins to initialize. 34468d75effSDimitry Andric // 34568d75effSDimitry Andric // For the static runtime (-MT), it's enough to put a callback to 34668d75effSDimitry Andric // __asan_set_seh_filter in the last section for C initializers. 34768d75effSDimitry Andric // 34868d75effSDimitry Andric // For the dynamic runtime (-MD), we want link the same 34968d75effSDimitry Andric // asan_dynamic_runtime_thunk.lib to all the modules, thus __asan_set_seh_filter 35068d75effSDimitry Andric // will be called for each instrumented module. This ensures that at least one 35168d75effSDimitry Andric // __asan_set_seh_filter call happens after the .exe module CRT is initialized. 35268d75effSDimitry Andric extern "C" SANITIZER_INTERFACE_ATTRIBUTE int __asan_set_seh_filter() { 35368d75effSDimitry Andric // We should only store the previous handler if it's not our own handler in 35468d75effSDimitry Andric // order to avoid loops in the EH chain. 35568d75effSDimitry Andric auto prev_seh_handler = SetUnhandledExceptionFilter(SEHHandler); 35668d75effSDimitry Andric if (prev_seh_handler != &SEHHandler) 35768d75effSDimitry Andric default_seh_handler = prev_seh_handler; 35868d75effSDimitry Andric return 0; 35968d75effSDimitry Andric } 36068d75effSDimitry Andric 36168d75effSDimitry Andric bool HandleDlopenInit() { 36268d75effSDimitry Andric // Not supported on this platform. 36368d75effSDimitry Andric static_assert(!SANITIZER_SUPPORTS_INIT_FOR_DLOPEN, 36468d75effSDimitry Andric "Expected SANITIZER_SUPPORTS_INIT_FOR_DLOPEN to be false"); 36568d75effSDimitry Andric return false; 36668d75effSDimitry Andric } 36768d75effSDimitry Andric 36868d75effSDimitry Andric #if !ASAN_DYNAMIC 36968d75effSDimitry Andric // The CRT runs initializers in this order: 37068d75effSDimitry Andric // - C initializers, from XIA to XIZ 37168d75effSDimitry Andric // - C++ initializers, from XCA to XCZ 37268d75effSDimitry Andric // Prior to 2015, the CRT set the unhandled exception filter at priority XIY, 37368d75effSDimitry Andric // near the end of C initialization. Starting in 2015, it was moved to the 37468d75effSDimitry Andric // beginning of C++ initialization. We set our priority to XCAB to run 37568d75effSDimitry Andric // immediately after the CRT runs. This way, our exception filter is called 37668d75effSDimitry Andric // first and we can delegate to their filter if appropriate. 37768d75effSDimitry Andric #pragma section(".CRT$XCAB", long, read) 37868d75effSDimitry Andric __declspec(allocate(".CRT$XCAB")) int (*__intercept_seh)() = 37968d75effSDimitry Andric __asan_set_seh_filter; 38068d75effSDimitry Andric 38168d75effSDimitry Andric // Piggyback on the TLS initialization callback directory to initialize asan as 38268d75effSDimitry Andric // early as possible. Initializers in .CRT$XL* are called directly by ntdll, 38368d75effSDimitry Andric // which run before the CRT. Users also add code to .CRT$XLC, so it's important 38468d75effSDimitry Andric // to run our initializers first. 38568d75effSDimitry Andric static void NTAPI asan_thread_init(void *module, DWORD reason, void *reserved) { 38668d75effSDimitry Andric if (reason == DLL_PROCESS_ATTACH) 38768d75effSDimitry Andric __asan_init(); 38868d75effSDimitry Andric } 38968d75effSDimitry Andric 39068d75effSDimitry Andric #pragma section(".CRT$XLAB", long, read) 39168d75effSDimitry Andric __declspec(allocate(".CRT$XLAB")) void(NTAPI *__asan_tls_init)( 39268d75effSDimitry Andric void *, unsigned long, void *) = asan_thread_init; 39368d75effSDimitry Andric #endif 39468d75effSDimitry Andric 39568d75effSDimitry Andric static void NTAPI asan_thread_exit(void *module, DWORD reason, void *reserved) { 39668d75effSDimitry Andric if (reason == DLL_THREAD_DETACH) { 39768d75effSDimitry Andric // Unpoison the thread's stack because the memory may be re-used. 39868d75effSDimitry Andric NT_TIB *tib = (NT_TIB *)NtCurrentTeb(); 39968d75effSDimitry Andric uptr stackSize = (uptr)tib->StackBase - (uptr)tib->StackLimit; 40068d75effSDimitry Andric __asan_unpoison_memory_region(tib->StackLimit, stackSize); 40168d75effSDimitry Andric } 40268d75effSDimitry Andric } 40368d75effSDimitry Andric 40468d75effSDimitry Andric #pragma section(".CRT$XLY", long, read) 40568d75effSDimitry Andric __declspec(allocate(".CRT$XLY")) void(NTAPI *__asan_tls_exit)( 40668d75effSDimitry Andric void *, unsigned long, void *) = asan_thread_exit; 40768d75effSDimitry Andric 40868d75effSDimitry Andric WIN_FORCE_LINK(__asan_dso_reg_hook) 40968d75effSDimitry Andric 41068d75effSDimitry Andric // }}} 41168d75effSDimitry Andric } // namespace __asan 41268d75effSDimitry Andric 41368d75effSDimitry Andric #endif // SANITIZER_WINDOWS 414