168d75effSDimitry Andric //===-- asan_win.cpp ------------------------------------------------------===// 268d75effSDimitry Andric // 368d75effSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 468d75effSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 568d75effSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 668d75effSDimitry Andric // 768d75effSDimitry Andric //===----------------------------------------------------------------------===// 868d75effSDimitry Andric // 968d75effSDimitry Andric // This file is a part of AddressSanitizer, an address sanity checker. 1068d75effSDimitry Andric // 1168d75effSDimitry Andric // Windows-specific details. 1268d75effSDimitry Andric //===----------------------------------------------------------------------===// 1368d75effSDimitry Andric 1468d75effSDimitry Andric #include "sanitizer_common/sanitizer_platform.h" 1568d75effSDimitry Andric #if SANITIZER_WINDOWS 1668d75effSDimitry Andric #define WIN32_LEAN_AND_MEAN 1768d75effSDimitry Andric #include <windows.h> 1868d75effSDimitry Andric 1968d75effSDimitry Andric #include <stdlib.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 5268d75effSDimitry Andric extern "C" SANITIZER_INTERFACE_ATTRIBUTE 5368d75effSDimitry Andric long __asan_unhandled_exception_filter(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 13468d75effSDimitry Andric static thread_return_t THREAD_CALLING_CONV asan_thread_start(void *arg) { 13568d75effSDimitry Andric AsanThread *t = (AsanThread *)arg; 13668d75effSDimitry Andric SetCurrentThread(t); 13768d75effSDimitry Andric return t->ThreadStart(GetTid(), /* signal_thread_is_registered */ nullptr); 13868d75effSDimitry Andric } 13968d75effSDimitry Andric 14068d75effSDimitry Andric INTERCEPTOR_WINAPI(HANDLE, CreateThread, LPSECURITY_ATTRIBUTES security, 14168d75effSDimitry Andric SIZE_T stack_size, LPTHREAD_START_ROUTINE start_routine, 14268d75effSDimitry Andric void *arg, DWORD thr_flags, DWORD *tid) { 14368d75effSDimitry Andric // Strict init-order checking is thread-hostile. 14468d75effSDimitry Andric if (flags()->strict_init_order) 14568d75effSDimitry Andric StopInitOrderChecking(); 14668d75effSDimitry Andric GET_STACK_TRACE_THREAD; 14768d75effSDimitry Andric // FIXME: The CreateThread interceptor is not the same as a pthread_create 14868d75effSDimitry Andric // one. This is a bandaid fix for PR22025. 14968d75effSDimitry Andric bool detached = false; // FIXME: how can we determine it on Windows? 15068d75effSDimitry Andric u32 current_tid = GetCurrentTidOrInvalid(); 15168d75effSDimitry Andric AsanThread *t = 15268d75effSDimitry Andric AsanThread::Create(start_routine, arg, current_tid, &stack, detached); 15368d75effSDimitry Andric return REAL(CreateThread)(security, stack_size, asan_thread_start, t, 15468d75effSDimitry Andric thr_flags, tid); 15568d75effSDimitry Andric } 15668d75effSDimitry Andric 15768d75effSDimitry Andric // }}} 15868d75effSDimitry Andric 15968d75effSDimitry Andric namespace __asan { 16068d75effSDimitry Andric 16168d75effSDimitry Andric void InitializePlatformInterceptors() { 16268d75effSDimitry Andric // The interceptors were not designed to be removable, so we have to keep this 16368d75effSDimitry Andric // module alive for the life of the process. 16468d75effSDimitry Andric HMODULE pinned; 16568d75effSDimitry Andric CHECK(GetModuleHandleExW( 16668d75effSDimitry Andric GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_PIN, 16768d75effSDimitry Andric (LPCWSTR)&InitializePlatformInterceptors, &pinned)); 16868d75effSDimitry Andric 16968d75effSDimitry Andric ASAN_INTERCEPT_FUNC(CreateThread); 17068d75effSDimitry Andric ASAN_INTERCEPT_FUNC(SetUnhandledExceptionFilter); 17168d75effSDimitry Andric 17268d75effSDimitry Andric #ifdef _WIN64 17368d75effSDimitry Andric ASAN_INTERCEPT_FUNC(__C_specific_handler); 17468d75effSDimitry Andric #else 17568d75effSDimitry Andric ASAN_INTERCEPT_FUNC(_except_handler3); 17668d75effSDimitry Andric ASAN_INTERCEPT_FUNC(_except_handler4); 17768d75effSDimitry Andric #endif 17868d75effSDimitry Andric 17968d75effSDimitry Andric // Try to intercept kernel32!RaiseException, and if that fails, intercept 18068d75effSDimitry Andric // ntdll!RtlRaiseException instead. 18168d75effSDimitry Andric if (!::__interception::OverrideFunction("RaiseException", 18268d75effSDimitry Andric (uptr)WRAP(RaiseException), 18368d75effSDimitry Andric (uptr *)&REAL(RaiseException))) { 18468d75effSDimitry Andric CHECK(::__interception::OverrideFunction("RtlRaiseException", 18568d75effSDimitry Andric (uptr)WRAP(RtlRaiseException), 18668d75effSDimitry Andric (uptr *)&REAL(RtlRaiseException))); 18768d75effSDimitry Andric } 18868d75effSDimitry Andric } 18968d75effSDimitry Andric 19068d75effSDimitry Andric void AsanApplyToGlobals(globals_op_fptr op, const void *needle) { 19168d75effSDimitry Andric UNIMPLEMENTED(); 19268d75effSDimitry Andric } 19368d75effSDimitry Andric 19468d75effSDimitry Andric // ---------------------- TSD ---------------- {{{ 19568d75effSDimitry Andric static bool tsd_key_inited = false; 19668d75effSDimitry Andric 19768d75effSDimitry Andric static __declspec(thread) void *fake_tsd = 0; 19868d75effSDimitry Andric 19968d75effSDimitry Andric // https://docs.microsoft.com/en-us/windows/desktop/api/winternl/ns-winternl-_teb 20068d75effSDimitry Andric // "[This structure may be altered in future versions of Windows. Applications 20168d75effSDimitry Andric // should use the alternate functions listed in this topic.]" 20268d75effSDimitry Andric typedef struct _TEB { 20368d75effSDimitry Andric PVOID Reserved1[12]; 20468d75effSDimitry Andric // PVOID ThreadLocalStoragePointer; is here, at the last field in Reserved1. 20568d75effSDimitry Andric PVOID ProcessEnvironmentBlock; 20668d75effSDimitry Andric PVOID Reserved2[399]; 20768d75effSDimitry Andric BYTE Reserved3[1952]; 20868d75effSDimitry Andric PVOID TlsSlots[64]; 20968d75effSDimitry Andric BYTE Reserved4[8]; 21068d75effSDimitry Andric PVOID Reserved5[26]; 21168d75effSDimitry Andric PVOID ReservedForOle; 21268d75effSDimitry Andric PVOID Reserved6[4]; 21368d75effSDimitry Andric PVOID TlsExpansionSlots; 21468d75effSDimitry Andric } TEB, *PTEB; 21568d75effSDimitry Andric 21668d75effSDimitry Andric constexpr size_t TEB_RESERVED_FIELDS_THREAD_LOCAL_STORAGE_OFFSET = 11; 21768d75effSDimitry Andric BOOL IsTlsInitialized() { 21868d75effSDimitry Andric PTEB teb = (PTEB)NtCurrentTeb(); 21968d75effSDimitry Andric return teb->Reserved1[TEB_RESERVED_FIELDS_THREAD_LOCAL_STORAGE_OFFSET] != 22068d75effSDimitry Andric nullptr; 22168d75effSDimitry Andric } 22268d75effSDimitry Andric 22368d75effSDimitry Andric void AsanTSDInit(void (*destructor)(void *tsd)) { 22468d75effSDimitry Andric // FIXME: we're ignoring the destructor for now. 22568d75effSDimitry Andric tsd_key_inited = true; 22668d75effSDimitry Andric } 22768d75effSDimitry Andric 22868d75effSDimitry Andric void *AsanTSDGet() { 22968d75effSDimitry Andric CHECK(tsd_key_inited); 23068d75effSDimitry Andric return IsTlsInitialized() ? fake_tsd : nullptr; 23168d75effSDimitry Andric } 23268d75effSDimitry Andric 23368d75effSDimitry Andric void AsanTSDSet(void *tsd) { 23468d75effSDimitry Andric CHECK(tsd_key_inited); 23568d75effSDimitry Andric fake_tsd = tsd; 23668d75effSDimitry Andric } 23768d75effSDimitry Andric 23868d75effSDimitry Andric void PlatformTSDDtor(void *tsd) { AsanThread::TSDDtor(tsd); } 23968d75effSDimitry Andric // }}} 24068d75effSDimitry Andric 24168d75effSDimitry Andric // ---------------------- Various stuff ---------------- {{{ 24268d75effSDimitry Andric void *AsanDoesNotSupportStaticLinkage() { 24368d75effSDimitry Andric #if defined(_DEBUG) 24468d75effSDimitry Andric #error Please build the runtime with a non-debug CRT: /MD or /MT 24568d75effSDimitry Andric #endif 24668d75effSDimitry Andric return 0; 24768d75effSDimitry Andric } 24868d75effSDimitry Andric 24968d75effSDimitry Andric uptr FindDynamicShadowStart() { 25068d75effSDimitry Andric uptr granularity = GetMmapGranularity(); 25168d75effSDimitry Andric uptr alignment = 8 * granularity; 25268d75effSDimitry Andric uptr left_padding = granularity; 25368d75effSDimitry Andric uptr space_size = kHighShadowEnd + left_padding; 25468d75effSDimitry Andric uptr shadow_start = FindAvailableMemoryRange(space_size, alignment, 25568d75effSDimitry Andric granularity, nullptr, nullptr); 25668d75effSDimitry Andric CHECK_NE((uptr)0, shadow_start); 25768d75effSDimitry Andric CHECK(IsAligned(shadow_start, alignment)); 25868d75effSDimitry Andric return shadow_start; 25968d75effSDimitry Andric } 26068d75effSDimitry Andric 26168d75effSDimitry Andric void AsanCheckDynamicRTPrereqs() {} 26268d75effSDimitry Andric 26368d75effSDimitry Andric void AsanCheckIncompatibleRT() {} 26468d75effSDimitry Andric 26568d75effSDimitry Andric void ReadContextStack(void *context, uptr *stack, uptr *ssize) { 26668d75effSDimitry Andric UNIMPLEMENTED(); 26768d75effSDimitry Andric } 26868d75effSDimitry Andric 26968d75effSDimitry Andric void AsanOnDeadlySignal(int, void *siginfo, void *context) { UNIMPLEMENTED(); } 27068d75effSDimitry Andric 271*5ffd83dbSDimitry Andric bool PlatformUnpoisonStacks() { return false; } 272*5ffd83dbSDimitry Andric 27368d75effSDimitry Andric #if SANITIZER_WINDOWS64 27468d75effSDimitry Andric // Exception handler for dealing with shadow memory. 27568d75effSDimitry Andric static LONG CALLBACK 27668d75effSDimitry Andric ShadowExceptionHandler(PEXCEPTION_POINTERS exception_pointers) { 27768d75effSDimitry Andric uptr page_size = GetPageSizeCached(); 27868d75effSDimitry Andric // Only handle access violations. 27968d75effSDimitry Andric if (exception_pointers->ExceptionRecord->ExceptionCode != 28068d75effSDimitry Andric EXCEPTION_ACCESS_VIOLATION || 28168d75effSDimitry Andric exception_pointers->ExceptionRecord->NumberParameters < 2) { 28268d75effSDimitry Andric __asan_handle_no_return(); 28368d75effSDimitry Andric return EXCEPTION_CONTINUE_SEARCH; 28468d75effSDimitry Andric } 28568d75effSDimitry Andric 28668d75effSDimitry Andric // Only handle access violations that land within the shadow memory. 28768d75effSDimitry Andric uptr addr = 28868d75effSDimitry Andric (uptr)(exception_pointers->ExceptionRecord->ExceptionInformation[1]); 28968d75effSDimitry Andric 29068d75effSDimitry Andric // Check valid shadow range. 29168d75effSDimitry Andric if (!AddrIsInShadow(addr)) { 29268d75effSDimitry Andric __asan_handle_no_return(); 29368d75effSDimitry Andric return EXCEPTION_CONTINUE_SEARCH; 29468d75effSDimitry Andric } 29568d75effSDimitry Andric 29668d75effSDimitry Andric // This is an access violation while trying to read from the shadow. Commit 29768d75effSDimitry Andric // the relevant page and let execution continue. 29868d75effSDimitry Andric 29968d75effSDimitry Andric // Determine the address of the page that is being accessed. 30068d75effSDimitry Andric uptr page = RoundDownTo(addr, page_size); 30168d75effSDimitry Andric 30268d75effSDimitry Andric // Commit the page. 30368d75effSDimitry Andric uptr result = 30468d75effSDimitry Andric (uptr)::VirtualAlloc((LPVOID)page, page_size, MEM_COMMIT, PAGE_READWRITE); 30568d75effSDimitry Andric if (result != page) 30668d75effSDimitry Andric return EXCEPTION_CONTINUE_SEARCH; 30768d75effSDimitry Andric 30868d75effSDimitry Andric // The page mapping succeeded, so continue execution as usual. 30968d75effSDimitry Andric return EXCEPTION_CONTINUE_EXECUTION; 31068d75effSDimitry Andric } 31168d75effSDimitry Andric 31268d75effSDimitry Andric #endif 31368d75effSDimitry Andric 31468d75effSDimitry Andric void InitializePlatformExceptionHandlers() { 31568d75effSDimitry Andric #if SANITIZER_WINDOWS64 31668d75effSDimitry Andric // On Win64, we map memory on demand with access violation handler. 31768d75effSDimitry Andric // Install our exception handler. 31868d75effSDimitry Andric CHECK(AddVectoredExceptionHandler(TRUE, &ShadowExceptionHandler)); 31968d75effSDimitry Andric #endif 32068d75effSDimitry Andric } 32168d75effSDimitry Andric 32268d75effSDimitry Andric bool IsSystemHeapAddress(uptr addr) { 32368d75effSDimitry Andric return ::HeapValidate(GetProcessHeap(), 0, (void *)addr) != FALSE; 32468d75effSDimitry Andric } 32568d75effSDimitry Andric 32668d75effSDimitry Andric // We want to install our own exception handler (EH) to print helpful reports 32768d75effSDimitry Andric // on access violations and whatnot. Unfortunately, the CRT initializers assume 32868d75effSDimitry Andric // they are run before any user code and drop any previously-installed EHs on 32968d75effSDimitry Andric // the floor, so we can't install our handler inside __asan_init. 33068d75effSDimitry Andric // (See crt0dat.c in the CRT sources for the details) 33168d75effSDimitry Andric // 33268d75effSDimitry Andric // Things get even more complicated with the dynamic runtime, as it finishes its 33368d75effSDimitry Andric // initialization before the .exe module CRT begins to initialize. 33468d75effSDimitry Andric // 33568d75effSDimitry Andric // For the static runtime (-MT), it's enough to put a callback to 33668d75effSDimitry Andric // __asan_set_seh_filter in the last section for C initializers. 33768d75effSDimitry Andric // 33868d75effSDimitry Andric // For the dynamic runtime (-MD), we want link the same 33968d75effSDimitry Andric // asan_dynamic_runtime_thunk.lib to all the modules, thus __asan_set_seh_filter 34068d75effSDimitry Andric // will be called for each instrumented module. This ensures that at least one 34168d75effSDimitry Andric // __asan_set_seh_filter call happens after the .exe module CRT is initialized. 34268d75effSDimitry Andric extern "C" SANITIZER_INTERFACE_ATTRIBUTE int __asan_set_seh_filter() { 34368d75effSDimitry Andric // We should only store the previous handler if it's not our own handler in 34468d75effSDimitry Andric // order to avoid loops in the EH chain. 34568d75effSDimitry Andric auto prev_seh_handler = SetUnhandledExceptionFilter(SEHHandler); 34668d75effSDimitry Andric if (prev_seh_handler != &SEHHandler) 34768d75effSDimitry Andric default_seh_handler = prev_seh_handler; 34868d75effSDimitry Andric return 0; 34968d75effSDimitry Andric } 35068d75effSDimitry Andric 35168d75effSDimitry Andric bool HandleDlopenInit() { 35268d75effSDimitry Andric // Not supported on this platform. 35368d75effSDimitry Andric static_assert(!SANITIZER_SUPPORTS_INIT_FOR_DLOPEN, 35468d75effSDimitry Andric "Expected SANITIZER_SUPPORTS_INIT_FOR_DLOPEN to be false"); 35568d75effSDimitry Andric return false; 35668d75effSDimitry Andric } 35768d75effSDimitry Andric 35868d75effSDimitry Andric #if !ASAN_DYNAMIC 35968d75effSDimitry Andric // The CRT runs initializers in this order: 36068d75effSDimitry Andric // - C initializers, from XIA to XIZ 36168d75effSDimitry Andric // - C++ initializers, from XCA to XCZ 36268d75effSDimitry Andric // Prior to 2015, the CRT set the unhandled exception filter at priority XIY, 36368d75effSDimitry Andric // near the end of C initialization. Starting in 2015, it was moved to the 36468d75effSDimitry Andric // beginning of C++ initialization. We set our priority to XCAB to run 36568d75effSDimitry Andric // immediately after the CRT runs. This way, our exception filter is called 36668d75effSDimitry Andric // first and we can delegate to their filter if appropriate. 36768d75effSDimitry Andric #pragma section(".CRT$XCAB", long, read) 36868d75effSDimitry Andric __declspec(allocate(".CRT$XCAB")) int (*__intercept_seh)() = 36968d75effSDimitry Andric __asan_set_seh_filter; 37068d75effSDimitry Andric 37168d75effSDimitry Andric // Piggyback on the TLS initialization callback directory to initialize asan as 37268d75effSDimitry Andric // early as possible. Initializers in .CRT$XL* are called directly by ntdll, 37368d75effSDimitry Andric // which run before the CRT. Users also add code to .CRT$XLC, so it's important 37468d75effSDimitry Andric // to run our initializers first. 37568d75effSDimitry Andric static void NTAPI asan_thread_init(void *module, DWORD reason, void *reserved) { 37668d75effSDimitry Andric if (reason == DLL_PROCESS_ATTACH) 37768d75effSDimitry Andric __asan_init(); 37868d75effSDimitry Andric } 37968d75effSDimitry Andric 38068d75effSDimitry Andric #pragma section(".CRT$XLAB", long, read) 38168d75effSDimitry Andric __declspec(allocate(".CRT$XLAB")) void(NTAPI *__asan_tls_init)( 38268d75effSDimitry Andric void *, unsigned long, void *) = asan_thread_init; 38368d75effSDimitry Andric #endif 38468d75effSDimitry Andric 38568d75effSDimitry Andric static void NTAPI asan_thread_exit(void *module, DWORD reason, void *reserved) { 38668d75effSDimitry Andric if (reason == DLL_THREAD_DETACH) { 38768d75effSDimitry Andric // Unpoison the thread's stack because the memory may be re-used. 38868d75effSDimitry Andric NT_TIB *tib = (NT_TIB *)NtCurrentTeb(); 38968d75effSDimitry Andric uptr stackSize = (uptr)tib->StackBase - (uptr)tib->StackLimit; 39068d75effSDimitry Andric __asan_unpoison_memory_region(tib->StackLimit, stackSize); 39168d75effSDimitry Andric } 39268d75effSDimitry Andric } 39368d75effSDimitry Andric 39468d75effSDimitry Andric #pragma section(".CRT$XLY", long, read) 39568d75effSDimitry Andric __declspec(allocate(".CRT$XLY")) void(NTAPI *__asan_tls_exit)( 39668d75effSDimitry Andric void *, unsigned long, void *) = asan_thread_exit; 39768d75effSDimitry Andric 39868d75effSDimitry Andric WIN_FORCE_LINK(__asan_dso_reg_hook) 39968d75effSDimitry Andric 40068d75effSDimitry Andric // }}} 40168d75effSDimitry Andric } // namespace __asan 40268d75effSDimitry Andric 40368d75effSDimitry Andric #endif // SANITIZER_WINDOWS 404