Skip to content

Commit

Permalink
Opt, 新增__YY_Thunks_Disable_Rreload_Dlls弱符号,控制预加载状态
Browse files Browse the repository at this point in the history
  • Loading branch information
mingkuang-Chuyu committed Sep 24, 2024
1 parent 3311538 commit a41b77a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Build.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ goto:eof

:: FixObj "XXX\YY_Thunks_for_Vista.obj" 1.def+2.def
:FixObj
LibMaker.exe FixObj %1 /WeakExternFix:__security_cookie=%PointType% /WeakExternFix:__acrt_atexit_table=%PointType% /WeakExternFix:__pfnDllMainCRTStartupForYY_Thunks=%PointType%
LibMaker.exe FixObj %1 /WeakExternFix:__security_cookie=%PointType% /WeakExternFix:__acrt_atexit_table=%PointType% /WeakExternFix:__pfnDllMainCRTStartupForYY_Thunks=%PointType% /WeakExternFix:__YY_Thunks_Disable_Rreload_Dlls=4
if %ErrorLevel% NEQ 0 exit /b %ErrorLevel%
if "%2"=="" goto:eof
set DEF_FILES=%2
Expand Down
28 changes: 21 additions & 7 deletions src/Thunks/YY_Thunks.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,17 @@ else
*/
EXTERN_C const UINT64 __YY_Thunks_Installed = YY_Thunks_Target;

/*
导出一个外部弱符号,指示当前是否关闭DLL预载(默认开启DLL预载)。
警告:关闭DLL预载虽然可以提升初始化速度,但是这可能带来死锁问题。目前已知的有:
1. locale锁/LoadDll锁交叉持锁产生死锁(https://github.com/Chuyu-Team/YY-Thunks/issues/7)
2. thread safe init锁/LoadDll锁交叉持锁产生死锁
// 如果需要关闭DLL预载,那么再任意位置定义如下变量即可:
EXTERN_C const BOOL __YY_Thunks_Disable_Rreload_Dlls = TRUE;
*/
EXTERN_C extern BOOL __YY_Thunks_Disable_Rreload_Dlls /* = FALSE*/;

// 从DllMain缓存RtlDllShutdownInProgress状态,规避退出时调用RtlDllShutdownInProgress。
// 0:缓存无效
// 1:模块正常卸载
Expand Down Expand Up @@ -642,14 +653,17 @@ static ThunksInitStatus __cdecl _YY_initialize_winapi_thunks(ThunksInitStatus _s
// 后续过程已经可以线程安全执行了,所以我们立即解锁,避免其他线程过于长时间的等待
InterlockedExchange((volatile LONG*)&s_eThunksStatus, LONG(_sInitStatus));

/*
* https://github.com/Chuyu-Team/YY-Thunks/issues/7
* 为了避免 try_get 系列函数竞争 DLL load锁,因此我们将所有被Thunk的API都预先加载完毕。
*/
for (auto p = (InitFunType*)__YY_THUNKS_INIT_FUN_START; p != (InitFunType*)__YY_THUNKS_INIT_FUN_END; ++p)
if (!__YY_Thunks_Disable_Rreload_Dlls)
{
if (auto pInitFun = *p)
pInitFun();
/*
* https://github.com/Chuyu-Team/YY-Thunks/issues/7
* 为了避免 try_get 系列函数竞争 DLL load锁,因此我们将所有被Thunk的API都预先加载完毕。
*/
for (auto p = (InitFunType*)__YY_THUNKS_INIT_FUN_START; p != (InitFunType*)__YY_THUNKS_INIT_FUN_END; ++p)
{
if (auto pInitFun = *p)
pInitFun();
}
}

return _sInitStatus;
Expand Down
2 changes: 2 additions & 0 deletions src/YY-Thunks.UnitTest/weak.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
const void* __acrt_atexit_table;

const void* __pfnDllMainCRTStartupForYY_Thunks;

const void* __YY_Thunks_Disable_Rreload_Dlls;

0 comments on commit a41b77a

Please sign in to comment.