Skip to content

Commit c0b612c

Browse files
Remove repeated call to DllMain (#112285)
1 parent 61b9cb8 commit c0b612c

File tree

1 file changed

+36
-29
lines changed

1 file changed

+36
-29
lines changed

src/coreclr/pal/src/loader/module.cpp

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ static bool LOADConvertLibraryPathWideStringToMultibyteString(
9494
INT *multibyteLibraryPathLengthRef);
9595
static BOOL LOADValidateModule(MODSTRUCT *module);
9696
static LPWSTR LOADGetModuleFileName(MODSTRUCT *module);
97-
static MODSTRUCT *LOADAddModule(NATIVE_LIBRARY_HANDLE dl_handle, LPCSTR libraryNameOrPath);
97+
static MODSTRUCT *LOADAddModule(NATIVE_LIBRARY_HANDLE dl_handle, LPCSTR libraryNameOrPath, /*OUT*/ BOOL* pIsAlreadyLoaded);
9898
static NATIVE_LIBRARY_HANDLE LOADLoadLibraryDirect(LPCSTR libraryNameOrPath);
9999
static BOOL LOADFreeLibrary(MODSTRUCT *module, BOOL fCallDllMain);
100100
static HMODULE LOADRegisterLibraryDirect(NATIVE_LIBRARY_HANDLE dl_handle, LPCSTR libraryNameOrPath, BOOL fDynamic);
@@ -669,7 +669,8 @@ PAL_RegisterModule(
669669
if (dl_handle)
670670
{
671671
// This only creates/adds the module handle and doesn't call DllMain
672-
hinstance = LOADAddModule(dl_handle, lpLibFileName);
672+
BOOL unused;
673+
hinstance = LOADAddModule(dl_handle, lpLibFileName, &unused);
673674
}
674675

675676
UnlockModuleList();
@@ -1592,10 +1593,11 @@ static MODSTRUCT *LOADAllocModule(NATIVE_LIBRARY_HANDLE dl_handle, LPCSTR name)
15921593
Return value:
15931594
PAL handle to the loaded library, or nullptr upon failure (error is set via SetLastError()).
15941595
*/
1595-
static MODSTRUCT *LOADAddModule(NATIVE_LIBRARY_HANDLE dl_handle, LPCSTR libraryNameOrPath)
1596+
static MODSTRUCT *LOADAddModule(NATIVE_LIBRARY_HANDLE dl_handle, LPCSTR libraryNameOrPath, /*OUT*/ BOOL* pIsAlreadyLoaded)
15961597
{
15971598
_ASSERTE(dl_handle != nullptr);
15981599
_ASSERTE(g_running_in_exe || (libraryNameOrPath != nullptr && libraryNameOrPath[0] != '\0'));
1600+
*pIsAlreadyLoaded = FALSE;
15991601

16001602
#if !RETURNS_NEW_HANDLES_ON_REPEAT_DLOPEN
16011603
/* search module list for a match. */
@@ -1614,6 +1616,7 @@ static MODSTRUCT *LOADAddModule(NATIVE_LIBRARY_HANDLE dl_handle, LPCSTR libraryN
16141616
module->refcount++;
16151617
}
16161618
dlclose(dl_handle);
1619+
*pIsAlreadyLoaded = TRUE;
16171620
return module;
16181621
}
16191622
module = module->next;
@@ -1665,7 +1668,8 @@ Return value:
16651668
*/
16661669
static HMODULE LOADRegisterLibraryDirect(NATIVE_LIBRARY_HANDLE dl_handle, LPCSTR libraryNameOrPath, BOOL fDynamic)
16671670
{
1668-
MODSTRUCT *module = LOADAddModule(dl_handle, libraryNameOrPath);
1671+
BOOL isAlreadyLoaded;
1672+
MODSTRUCT *module = LOADAddModule(dl_handle, libraryNameOrPath, &isAlreadyLoaded);
16691673
if (module == nullptr)
16701674
{
16711675
return nullptr;
@@ -1674,36 +1678,39 @@ static HMODULE LOADRegisterLibraryDirect(NATIVE_LIBRARY_HANDLE dl_handle, LPCSTR
16741678
/* If the module contains a DllMain, call it. */
16751679
if (module->pDllMain)
16761680
{
1677-
TRACE("Calling DllMain (%p) for module %S\n",
1678-
module->pDllMain,
1679-
module->lib_name ? module->lib_name : W16_NULLSTRING);
1680-
1681-
if (nullptr == module->hinstance)
1681+
if (!isAlreadyLoaded)
16821682
{
1683-
PREGISTER_MODULE registerModule = (PREGISTER_MODULE)dlsym(module->dl_handle, "PAL_RegisterModule");
1684-
if (registerModule != nullptr)
1685-
{
1686-
module->hinstance = registerModule(libraryNameOrPath);
1687-
}
1688-
else
1683+
TRACE("Calling DllMain (%p) for module %S\n",
1684+
module->pDllMain,
1685+
module->lib_name ? module->lib_name : W16_NULLSTRING);
1686+
1687+
if (nullptr == module->hinstance)
16891688
{
1690-
// If the target module doesn't have the PAL_RegisterModule export, then use this PAL's
1691-
// module handle assuming that the target module is referencing this PAL's exported
1692-
// functions on said handle.
1693-
module->hinstance = (HINSTANCE)module;
1689+
PREGISTER_MODULE registerModule = (PREGISTER_MODULE)dlsym(module->dl_handle, "PAL_RegisterModule");
1690+
if (registerModule != nullptr)
1691+
{
1692+
module->hinstance = registerModule(libraryNameOrPath);
1693+
}
1694+
else
1695+
{
1696+
// If the target module doesn't have the PAL_RegisterModule export, then use this PAL's
1697+
// module handle assuming that the target module is referencing this PAL's exported
1698+
// functions on said handle.
1699+
module->hinstance = (HINSTANCE)module;
1700+
}
16941701
}
1695-
}
16961702

1697-
BOOL dllMainRetVal = LOADCallDllMainSafe(module, DLL_PROCESS_ATTACH, fDynamic ? nullptr : (LPVOID)-1);
1703+
BOOL dllMainRetVal = LOADCallDllMainSafe(module, DLL_PROCESS_ATTACH, fDynamic ? nullptr : (LPVOID)-1);
16981704

1699-
// If DlMain(DLL_PROCESS_ATTACH) returns FALSE, we must immediately unload the module
1700-
if (!dllMainRetVal)
1701-
{
1702-
ERROR("DllMain returned FALSE; unloading module.\n");
1703-
module->pDllMain = nullptr;
1704-
FreeLibrary((HMODULE)module);
1705-
SetLastError(ERROR_DLL_INIT_FAILED);
1706-
module = nullptr;
1705+
// If DlMain(DLL_PROCESS_ATTACH) returns FALSE, we must immediately unload the module
1706+
if (!dllMainRetVal)
1707+
{
1708+
ERROR("DllMain returned FALSE; unloading module.\n");
1709+
module->pDllMain = nullptr;
1710+
FreeLibrary((HMODULE)module);
1711+
SetLastError(ERROR_DLL_INIT_FAILED);
1712+
module = nullptr;
1713+
}
17071714
}
17081715
}
17091716
else

0 commit comments

Comments
 (0)