From 07994edf1908bb1f1de8f52478c8b2ded3a1e6d9 Mon Sep 17 00:00:00 2001 From: mingkuang Date: Sat, 25 May 2024 18:03:33 +0800 Subject: [PATCH] =?UTF-8?q?Opt,=20=E4=BD=BF=E7=94=A8Tls=E9=87=8D=E5=86=99p?= =?UTF-8?q?td=5Fdownlevel=EF=BC=8C=E6=8F=90=E9=AB=98=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Directory.Build.props | 4 +- Nuget/VC-LTL.nuspec | 8 +- Sources/crt/ptd_downlevel.cpp | 76 ------------------- Sources/crt/ptd_downlevel.h | 50 +++++++++--- Sources/crt/vcruntime/ehhelpers.h | 2 +- Sources/crt/vcruntime/frame.cpp | 2 +- Sources/crt/vcruntime/risctrnsctrl.cpp | 10 +-- Sources/crt/vcruntime/trnsctrl.h | 8 +- Sources/crt/vcruntime/vcruntime_internal.h | 5 +- Sources/ucrt/misc/invalid_parameter.cpp | 6 +- config/config.props | 4 +- ucrtbase.msvcrt/localeconv.cpp | 8 +- ucrtbase.msvcrt/ucrtbase.msvcrt.vcxproj | 4 +- .../ucrtbase.msvcrt.vcxproj.filters | 6 +- vcruntime.ucrtbase/vcruntime.ucrtbase.vcxproj | 16 ++++ 15 files changed, 79 insertions(+), 130 deletions(-) delete mode 100644 Sources/crt/ptd_downlevel.cpp diff --git a/Directory.Build.props b/Directory.Build.props index 386c7de..5dd623b 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -18,8 +18,6 @@ NoExtensions 4200;%(DisableSpecificWarnings) true - - /Zc:threadSafeInit- %(AdditionalOptions) /d2FH4- %(AdditionalOptions) @@ -33,4 +31,4 @@ $([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../')) - \ No newline at end of file + diff --git a/Nuget/VC-LTL.nuspec b/Nuget/VC-LTL.nuspec index 3bff3f8..af58187 100644 --- a/Nuget/VC-LTL.nuspec +++ b/Nuget/VC-LTL.nuspec @@ -27,14 +27,10 @@ + - - + diff --git a/Sources/crt/ptd_downlevel.cpp b/Sources/crt/ptd_downlevel.cpp deleted file mode 100644 index e5ccbcc..0000000 --- a/Sources/crt/ptd_downlevel.cpp +++ /dev/null @@ -1,76 +0,0 @@ - -#include -#include "ptd_downlevel.h" - -static ptd_downlevel ___ptd_downlevel_data[0x4000]; - - -EXTERN_C ptd_downlevel * __fastcall __LTL_get_ptd_downlevel(BOOL bCanAlloc) -{ - const auto dwCurrentThreadId = GetCurrentThreadId(); - - //我们做一个桶,分成128组,每组128个元素 - auto pPTD_downlevelGroup = &___ptd_downlevel_data[(dwCurrentThreadId >> 1) & 0x7F]; - - for (int i = 0; i != 128; ++i) - { - //搜索是线程安全的,因为不可能二个线程ID是一样的 - if (pPTD_downlevelGroup[i].dwThreadId == dwCurrentThreadId) - { - return &pPTD_downlevelGroup[i]; - } - } - - if (bCanAlloc) - { - for (int i = 0; i != 128; ++i) - { - //尝试搜索一个空闲的位置 - auto dwOrgThreadId = InterlockedCompareExchange(&pPTD_downlevelGroup[i].dwThreadId, dwCurrentThreadId, 0); - - if (dwOrgThreadId == 0) - { - //成功锁定了一个新的 ptd 快,我们先将数据初始化为 0 - memset( - &pPTD_downlevelGroup[i]._thread_local_iph, - 0, - sizeof(pPTD_downlevelGroup[i]) - size_t(&((ptd_downlevel*)NULL)->_thread_local_iph)); - - - __if_exists(ptd_downlevel::_CatchStateInParent) - { - pPTD_downlevelGroup[i]._CatchStateInParent = -2;//INVALID_CATCH_SPECIFIC_STATE - } - - return &pPTD_downlevelGroup[i]; - } - } - } - - return nullptr; -} - -static VOID NTAPI ptd_downlevel_TLS_CALLBACK( - PVOID DllHandle, - DWORD Reason, - PVOID Reserved -) -{ - if (DLL_THREAD_DETACH == Reason) - { - auto ptd = __LTL_get_ptd_downlevel(FALSE); - - //释放ptd - if (ptd) - InterlockedExchange(&ptd->dwThreadId, 0); - } -} - -#pragma section(".CRT$XLB", long, read) // LTL Loader TLS Callback -__declspec(allocate(".CRT$XLB")) static PIMAGE_TLS_CALLBACK __LTL_TLS_CALLBACKs = ptd_downlevel_TLS_CALLBACK; - -#if defined(_X86_) -#pragma comment(linker, "/INCLUDE:__tls_used") -#else -#pragma comment(linker, "/INCLUDE:_tls_used") -#endif diff --git a/Sources/crt/ptd_downlevel.h b/Sources/crt/ptd_downlevel.h index d5cea6e..837c368 100644 --- a/Sources/crt/ptd_downlevel.h +++ b/Sources/crt/ptd_downlevel.h @@ -6,8 +6,8 @@ struct ptd_downlevel { - //所需线程ID - unsigned dwThreadId; + uintptr_t bInit; +#if WindowsTargetPlatformMinVersion < WindowsTargetPlatformWindows10_10240 void(__cdecl* _thread_local_iph)( wchar_t const*, wchar_t const*, @@ -15,21 +15,51 @@ struct ptd_downlevel unsigned int, uintptr_t ); -#if defined (_M_IA64) || defined (_M_AMD64) || defined (_M_ARM64) || defined (_M_ARM) - void* _pForeignException; +#endif + +#if WindowsTargetPlatformMinVersion < WindowsTargetPlatformWindowsXP + int _ProcessingThrow; /* for uncaught_exception */ +#endif + +#if WindowsTargetPlatformMinVersion < WindowsTargetPlatformWindows6 + int _cxxReThrow; +#endif + +#if defined (_M_IA64) || defined (_M_AMD64) || defined (_M_ARM64) || defined (_M_ARM) || defined _M_HYBRID +#if WindowsTargetPlatformMinVersion < WindowsTargetPlatformWindows6 uintptr_t _ImageBase; uintptr_t _ThrowImageBase; + void* _pForeignException; #endif - int _cxxReThrow; -#if defined _M_X64 || defined _M_ARM || defined _M_ARM64 || defined _M_HYBRID - int _CatchStateInParent; + +#if WindowsTargetPlatformMinVersion < __MakeVersion(10, 0, 19041) + int _CatchStateInParent; #endif + +#endif // defined (_M_IA64) || defined (_M_AMD64) || defined (_M_ARM64) || defined (_M_ARM) + #if defined(_M_IX86) - int _ProcessingThrow; /* for uncaught_exception */ + +#if WindowsTargetPlatformMinVersion < WindowsTargetPlatformWindows6 void* _pFrameInfoChain; #endif +#endif // defined(_M_IX86) }; -EXTERN_C ptd_downlevel* __fastcall __LTL_get_ptd_downlevel(BOOL bCanAlloc); +#if WindowsTargetPlatformMinVersion < __MakeVersion(10, 0, 19041) +__declspec(noinline) inline ptd_downlevel* __fastcall __LTL_get_ptd_downlevel() +{ + static thread_local ptd_downlevel s_ptd_downlevel; + if (!s_ptd_downlevel.bInit) + { + s_ptd_downlevel.bInit = 1; + __if_exists(ptd_downlevel::_CatchStateInParent) + { + s_ptd_downlevel._CatchStateInParent = -2;//INVALID_CATCH_SPECIFIC_STATE + } + } -#endif \ No newline at end of file + return &s_ptd_downlevel; +} +#endif +#endif // _LTL_ptd_downlevel diff --git a/Sources/crt/vcruntime/ehhelpers.h b/Sources/crt/vcruntime/ehhelpers.h index fd0f620..d339120 100644 --- a/Sources/crt/vcruntime/ehhelpers.h +++ b/Sources/crt/vcruntime/ehhelpers.h @@ -14,7 +14,7 @@ #else #include #define _pForeignExceptWin6 (*((EHExceptionRecord **)&(((_ptd_msvcrt_win6_shared*)RENAME_BASE_PTD(__vcrt_getptd)())->_pForeignException))) -#define _pForeignExceptdownlevel (*((EHExceptionRecord **)&(__LTL_get_ptd_downlevel(TRUE)->_pForeignException))) +#define _pForeignExceptdownlevel (*((EHExceptionRecord **)&(__LTL_get_ptd_downlevel()->_pForeignException))) #define _pForeignExcept (__LTL_GetOsMinVersion() >= MakeMiniVersion(6, 0) ? _pForeignExceptWin6 : _pForeignExceptdownlevel) #endif diff --git a/Sources/crt/vcruntime/frame.cpp b/Sources/crt/vcruntime/frame.cpp index 85deee9..1f5e5fd 100644 --- a/Sources/crt/vcruntime/frame.cpp +++ b/Sources/crt/vcruntime/frame.cpp @@ -47,7 +47,7 @@ using namespace FH4; #include #define cxxReThrowWin6 (((_ptd_msvcrt_win6_shared*)RENAME_BASE_PTD(__vcrt_getptd)())->_cxxReThrow) -#define cxxReThrowdownlevel (__LTL_get_ptd_downlevel(TRUE)->_cxxReThrow) +#define cxxReThrowdownlevel (__LTL_get_ptd_downlevel()->_cxxReThrow) #define cxxReThrow (__LTL_GetOsMinVersion() >= MakeMiniVersion(6,0) ? cxxReThrowWin6 : cxxReThrowdownlevel) #endif diff --git a/Sources/crt/vcruntime/risctrnsctrl.cpp b/Sources/crt/vcruntime/risctrnsctrl.cpp index f1c502a..f60042b 100644 --- a/Sources/crt/vcruntime/risctrnsctrl.cpp +++ b/Sources/crt/vcruntime/risctrnsctrl.cpp @@ -36,7 +36,7 @@ #else #include #define _ImageBaseWin6 (((_ptd_msvcrt_win6_shared*)__vcrt_getptd())->_ImageBase) -#define _ImageBasedownlevel (__LTL_get_ptd_downlevel(TRUE)->_ImageBase) +#define _ImageBasedownlevel (__LTL_get_ptd_downlevel()->_ImageBase) #define _ImageBase (__LTL_GetOsMinVersion() >= MakeMiniVersion(6,0) ? _ImageBaseWin6 : _ImageBasedownlevel) #endif extern "C" uintptr_t __cdecl _GetImageBase() @@ -60,7 +60,7 @@ extern "C" void __cdecl _SetImageBase(uintptr_t ImageBaseToRestore) #else #include #define _ThrowImageBaseWin6 (((_ptd_msvcrt_win6_shared*)__vcrt_getptd())->_ThrowImageBase) -#define _ThrowImageBasedownlevel (__LTL_get_ptd_downlevel(TRUE)->_ThrowImageBase) +#define _ThrowImageBasedownlevel (__LTL_get_ptd_downlevel()->_ThrowImageBase) #define _ThrowImageBase (__LTL_GetOsMinVersion() >= MakeMiniVersion(6,0) ? _ThrowImageBaseWin6 : _ThrowImageBasedownlevel) #endif @@ -76,12 +76,6 @@ extern "C" void __cdecl _SetThrowImageBase(uintptr_t NewThrowImageBase) #endif -#if 1 -#if _VCRT_BUILD_FH4 && WindowsTargetPlatformMinVersion >= WindowsTargetPlatformWindows6 -thread_local int VC_LTL_UCRT_CatchStateInParent = INVALID_CATCH_SPECIFIC_STATE; -#endif // _VCRT_BUILD_FH4 && WindowsTargetPlatformMinVersion >= WindowsTargetPlatformWindows6 -#endif // 1 - #if _EH_RELATIVE_FUNCINFO #if _VCRT_BUILD_FH4 // diff --git a/Sources/crt/vcruntime/trnsctrl.h b/Sources/crt/vcruntime/trnsctrl.h index 23016be..f62a0f9 100644 --- a/Sources/crt/vcruntime/trnsctrl.h +++ b/Sources/crt/vcruntime/trnsctrl.h @@ -262,14 +262,12 @@ extern "C" _VCRTIMP int* __cdecl __processing_throw(); #if _VCRT_BUILD_FH4 // In the satellite build, __vcrt_getptd uses satellite's PTD // In the non-DLL build, __vcrt_getptd uses the main PTD which was updated to have this field -#if WindowsTargetPlatformMinVersion >= WindowsTargetPlatformWindows6 -extern thread_local int VC_LTL_UCRT_CatchStateInParent; -#define CatchStateInParent (VC_LTL_UCRT_CatchStateInParent) +#if WindowsTargetPlatformMinVersion >= __MakeVersion(10, 0, 19041) +#define CatchStateInParent (RENAME_BASE_PTD(__vcrt_getptd)()->_CatchStateInParent) #else #include -#define CatchStateInParent (__LTL_get_ptd_downlevel(TRUE)->_CatchStateInParent) +#define CatchStateInParent (__LTL_get_ptd_downlevel()->_CatchStateInParent) #endif - #endif #pragma pack(pop) diff --git a/Sources/crt/vcruntime/vcruntime_internal.h b/Sources/crt/vcruntime/vcruntime_internal.h index c84b86e..beeb707 100644 --- a/Sources/crt/vcruntime/vcruntime_internal.h +++ b/Sources/crt/vcruntime/vcruntime_internal.h @@ -209,10 +209,11 @@ typedef struct RENAME_BASE_PTD(__vcrt_ptd) uintptr_t _ThrowImageBase; void* _pForeignException; - //2015 没有这个 - //int _CatchStateInParent; // Used to link together the catch funclet with the parent. During dispatch contains state associated +#if WindowsTargetPlatformMinVersion >= __MakeVersion(10, 0, 19041) + int _CatchStateInParent; // Used to link together the catch funclet with the parent. During dispatch contains state associated // with catch in the parent. During unwind represents the current unwind state that is resumed to // during collided unwind and used to look for handlers of the throwing dtor. +#endif #elif defined _M_IX86 void* _pFrameInfoChain; #endif diff --git a/Sources/ucrt/misc/invalid_parameter.cpp b/Sources/ucrt/misc/invalid_parameter.cpp index 9a91abc..211fe96 100644 --- a/Sources/ucrt/misc/invalid_parameter.cpp +++ b/Sources/ucrt/misc/invalid_parameter.cpp @@ -15,11 +15,9 @@ static _invalid_parameter_handler __acrt_invalid_parameter_handler = nullptr; #if WindowsTargetPlatformMinVersion >= WindowsTargetPlatformWindows10_10240 #define _thread_local_iph _get_thread_local_invalid_parameter_handler() -#elif WindowsTargetPlatformMinVersion >= WindowsTargetPlatformWindows6 -static thread_local _invalid_parameter_handler _thread_local_iph; #else #include -#define _thread_local_iph (__LTL_get_ptd_downlevel(TRUE)->_thread_local_iph) +#define _thread_local_iph (__LTL_get_ptd_downlevel()->_thread_local_iph) #endif #if defined _M_X64 && !defined _UCRT_ENCLAVE_BUILD @@ -328,4 +326,4 @@ extern "C" _invalid_parameter_handler __cdecl _get_thread_local_invalid_paramete } _LCRT_DEFINE_IAT_SYMBOL(_get_thread_local_invalid_parameter_handler); -#endif \ No newline at end of file +#endif diff --git a/config/config.props b/config/config.props index 8782622..958de5e 100644 --- a/config/config.props +++ b/config/config.props @@ -132,8 +132,10 @@ _Build_By_LTL=1;_LTL_Core_Version=$(LTL_CoreVersion);%(PreprocessorDefinitions) _ATL_XP_TARGETING=1;%(PreprocessorDefinitions) - + 5.01 diff --git a/ucrtbase.msvcrt/localeconv.cpp b/ucrtbase.msvcrt/localeconv.cpp index b1e911f..bce3d65 100644 --- a/ucrtbase.msvcrt/localeconv.cpp +++ b/ucrtbase.msvcrt/localeconv.cpp @@ -87,14 +87,8 @@ extern "C" lconv* __cdecl localeconv(void) auto _Page = ___lc_codepage_func(); -#if WindowsTargetPlatformMinVersion < WindowsTargetPlatformWindows6 - //低于Vista,那么无法安全的使用 thread_local - static lconv_with_static_buffer ThreadBuffer[0x4000]; - - auto& Current = ThreadBuffer[(GetCurrentThreadId() >> 1) % __crt_countof(ThreadBuffer)]; -#else + // 由YY-Thunks为Windows XP提供完整的thread_local支持 static thread_local lconv_with_static_buffer Current; -#endif if (Current.decimal_point != pOrglconv->decimal_point) { diff --git a/ucrtbase.msvcrt/ucrtbase.msvcrt.vcxproj b/ucrtbase.msvcrt/ucrtbase.msvcrt.vcxproj index 01f1c38..c17b0e8 100644 --- a/ucrtbase.msvcrt/ucrtbase.msvcrt.vcxproj +++ b/ucrtbase.msvcrt/ucrtbase.msvcrt.vcxproj @@ -658,9 +658,6 @@ - - true - true @@ -1296,6 +1293,7 @@ + diff --git a/ucrtbase.msvcrt/ucrtbase.msvcrt.vcxproj.filters b/ucrtbase.msvcrt/ucrtbase.msvcrt.vcxproj.filters index bfa3c9e..a56f2c5 100644 --- a/ucrtbase.msvcrt/ucrtbase.msvcrt.vcxproj.filters +++ b/ucrtbase.msvcrt/ucrtbase.msvcrt.vcxproj.filters @@ -765,9 +765,6 @@ 源文件\WinXP - - 头文件\ltl - 源文件\objs @@ -806,5 +803,8 @@ 头文件\ltl + + 头文件\ltl + \ No newline at end of file diff --git a/vcruntime.ucrtbase/vcruntime.ucrtbase.vcxproj b/vcruntime.ucrtbase/vcruntime.ucrtbase.vcxproj index 889500f..1ac2f35 100644 --- a/vcruntime.ucrtbase/vcruntime.ucrtbase.vcxproj +++ b/vcruntime.ucrtbase/vcruntime.ucrtbase.vcxproj @@ -441,6 +441,7 @@ NoExtensions 4200;%(DisableSpecificWarnings) stdcpp17 + framework.h;%(ForcedIncludeFiles) @@ -465,6 +466,7 @@ NoExtensions 4200;%(DisableSpecificWarnings) stdcpp17 + framework.h;%(ForcedIncludeFiles) @@ -490,6 +492,7 @@ MultiThreaded 4200;%(DisableSpecificWarnings) stdcpp17 + framework.h;%(ForcedIncludeFiles) @@ -513,6 +516,7 @@ MultiThreadedDebug 4200;%(DisableSpecificWarnings) stdcpp17 + framework.h;%(ForcedIncludeFiles) @@ -539,6 +543,7 @@ NoExtensions 4200;%(DisableSpecificWarnings) stdcpp17 + framework.h;%(ForcedIncludeFiles) @@ -563,6 +568,7 @@ NoExtensions 4200;%(DisableSpecificWarnings) stdcpp17 + framework.h;%(ForcedIncludeFiles) @@ -588,6 +594,7 @@ OldStyle 4200;%(DisableSpecificWarnings) stdcpp17 + framework.h;%(ForcedIncludeFiles) @@ -611,6 +618,7 @@ OldStyle 4200;%(DisableSpecificWarnings) stdcpp17 + framework.h;%(ForcedIncludeFiles) @@ -636,6 +644,7 @@ MultiThreaded 4200;%(DisableSpecificWarnings) stdcpp17 + framework.h;%(ForcedIncludeFiles) @@ -659,6 +668,7 @@ MultiThreadedDebug 4200;%(DisableSpecificWarnings) stdcpp17 + framework.h;%(ForcedIncludeFiles) @@ -684,6 +694,7 @@ MultiThreaded 4200;%(DisableSpecificWarnings) stdcpp17 + framework.h;%(ForcedIncludeFiles) @@ -707,6 +718,7 @@ MultiThreadedDebug 4200;%(DisableSpecificWarnings) stdcpp17 + framework.h;%(ForcedIncludeFiles) @@ -732,6 +744,7 @@ OldStyle 4200;%(DisableSpecificWarnings) stdcpp17 + framework.h;%(ForcedIncludeFiles) @@ -755,6 +768,7 @@ OldStyle 4200;%(DisableSpecificWarnings) stdcpp17 + framework.h;%(ForcedIncludeFiles) @@ -780,6 +794,7 @@ OldStyle 4200;%(DisableSpecificWarnings) stdcpp17 + framework.h;%(ForcedIncludeFiles) @@ -803,6 +818,7 @@ OldStyle 4200;%(DisableSpecificWarnings) stdcpp17 + framework.h;%(ForcedIncludeFiles)