Skip to content

Commit 221dc73

Browse files
authored
Avoid PAL LoadLibrary simulator for regular PInvoke (dotnet#24669)
Fixes #21009
1 parent 458d030 commit 221dc73

File tree

9 files changed

+32
-95
lines changed

9 files changed

+32
-95
lines changed

src/pal/inc/pal.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2654,13 +2654,6 @@ PALAPI
26542654
PAL_LoadLibraryDirect(
26552655
IN LPCWSTR lpLibFileName);
26562656

2657-
PALIMPORT
2658-
HMODULE
2659-
PALAPI
2660-
PAL_RegisterLibraryDirect(
2661-
IN NATIVE_LIBRARY_HANDLE dl_handle,
2662-
IN LPCWSTR lpLibFileName);
2663-
26642657
PALIMPORT
26652658
BOOL
26662659
PALAPI

src/pal/src/loader/module.cpp

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -635,60 +635,6 @@ PAL_LoadLibraryDirect(
635635
return dl_handle;
636636
}
637637

638-
/*
639-
Function:
640-
PAL_RegisterLibraryDirect
641-
642-
Registers a system handle to a loaded library with the module list.
643-
644-
Returns a PAL handle to the loaded library, or nullptr upon failure (error is set via SetLastError()).
645-
*/
646-
HMODULE
647-
PALAPI
648-
PAL_RegisterLibraryDirect(
649-
IN NATIVE_LIBRARY_HANDLE dl_handle,
650-
IN LPCWSTR lpLibFileName)
651-
{
652-
PathCharString pathstr;
653-
CHAR * lpstr = nullptr;
654-
INT name_length;
655-
HMODULE hModule = nullptr;
656-
657-
PERF_ENTRY(RegisterLibraryDirect);
658-
ENTRY("RegisterLibraryDirect (lpLibFileName=%p (%S)) \n",
659-
lpLibFileName ? lpLibFileName : W16_NULLSTRING,
660-
lpLibFileName ? lpLibFileName : W16_NULLSTRING);
661-
662-
if (!LOADVerifyLibraryPath(lpLibFileName))
663-
{
664-
goto done;
665-
}
666-
667-
lpstr = pathstr.OpenStringBuffer((PAL_wcslen(lpLibFileName)+1) * MaxWCharToAcpLength);
668-
if (nullptr == lpstr)
669-
{
670-
goto done;
671-
}
672-
if (!LOADConvertLibraryPathWideStringToMultibyteString(lpLibFileName, lpstr, &name_length))
673-
{
674-
goto done;
675-
}
676-
677-
/* do the Dos/Unix conversion on our own copy of the name */
678-
FILEDosToUnixPathA(lpstr);
679-
pathstr.CloseBuffer(name_length);
680-
681-
/* let LOADRegisterLibraryDirect call SetLastError in case of failure */
682-
LockModuleList();
683-
hModule = LOADRegisterLibraryDirect(dl_handle, lpstr, true /* fDynamic */);
684-
UnlockModuleList();
685-
686-
done:
687-
LOGEXIT("RegisterLibraryDirect returns HMODULE %p\n", hModule);
688-
PERF_EXIT(RegisterLibraryDirect);
689-
return hModule;
690-
}
691-
692638
/*
693639
Function:
694640
PAL_FreeLibraryDirect

src/vm/appdomain.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2880,7 +2880,6 @@ AppDomain::AppDomain()
28802880

28812881
m_pRefClassFactHash = NULL;
28822882

2883-
m_ReversePInvokeCanEnter=TRUE;
28842883
m_ForceTrivialWaitOperations = false;
28852884
m_Stage=STAGE_CREATING;
28862885

@@ -4702,7 +4701,7 @@ BOOL AppDomain::AddExceptionToCache(AssemblySpec* pSpec, Exception *ex)
47024701
return m_AssemblyCache.StoreException(pSpec, ex);
47034702
}
47044703

4705-
void AppDomain::AddUnmanagedImageToCache(LPCWSTR libraryName, HMODULE hMod)
4704+
void AppDomain::AddUnmanagedImageToCache(LPCWSTR libraryName, NATIVE_LIBRARY_HANDLE hMod)
47064705
{
47074706
CONTRACTL
47084707
{
@@ -4723,9 +4722,9 @@ void AppDomain::AddUnmanagedImageToCache(LPCWSTR libraryName, HMODULE hMod)
47234722
}
47244723

47254724

4726-
HMODULE AppDomain::FindUnmanagedImageInCache(LPCWSTR libraryName)
4725+
NATIVE_LIBRARY_HANDLE AppDomain::FindUnmanagedImageInCache(LPCWSTR libraryName)
47274726
{
4728-
CONTRACT(HMODULE)
4727+
CONTRACT(NATIVE_LIBRARY_HANDLE)
47294728
{
47304729
THROWS;
47314730
GC_TRIGGERS;
@@ -4739,7 +4738,7 @@ HMODULE AppDomain::FindUnmanagedImageInCache(LPCWSTR libraryName)
47394738

47404739
AssemblySpec spec;
47414740
spec.SetCodeBase(libraryName);
4742-
RETURN (HMODULE) m_UnmanagedCache.LookupEntry(&spec, 0);
4741+
RETURN (NATIVE_LIBRARY_HANDLE) m_UnmanagedCache.LookupEntry(&spec, 0);
47434742
}
47444743

47454744
BOOL AppDomain::RemoveFileFromCache(PEAssembly *pFile)

src/vm/appdomain.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2073,8 +2073,8 @@ class AppDomain : public BaseDomain
20732073
BOOL RemoveAssemblyFromCache(DomainAssembly* pAssembly);
20742074

20752075
BOOL AddExceptionToCache(AssemblySpec* pSpec, Exception *ex);
2076-
void AddUnmanagedImageToCache(LPCWSTR libraryName, HMODULE hMod);
2077-
HMODULE FindUnmanagedImageInCache(LPCWSTR libraryName);
2076+
void AddUnmanagedImageToCache(LPCWSTR libraryName, NATIVE_LIBRARY_HANDLE hMod);
2077+
NATIVE_LIBRARY_HANDLE FindUnmanagedImageInCache(LPCWSTR libraryName);
20782078
//****************************************************************************************
20792079
//
20802080
// Adds or removes an assembly to the domain.
@@ -2724,7 +2724,6 @@ class AppDomain : public BaseDomain
27242724
size_t m_MemoryPressure;
27252725

27262726
ArrayList m_NativeDllSearchDirectories;
2727-
BOOL m_ReversePInvokeCanEnter;
27282727
bool m_ForceTrivialWaitOperations;
27292728

27302729
public:

src/vm/dllimport.cpp

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5732,7 +5732,7 @@ void CreateCLRToDispatchCOMStub(
57325732
#endif // FEATURE_COMINTEROP
57335733

57345734
/*static*/
5735-
LPVOID NDirect::NDirectGetEntryPoint(NDirectMethodDesc *pMD, HINSTANCE hMod)
5735+
LPVOID NDirect::NDirectGetEntryPoint(NDirectMethodDesc *pMD, NATIVE_LIBRARY_HANDLE hMod)
57365736
{
57375737
// GetProcAddress cannot be called while preemptive GC is disabled.
57385738
// It requires the OS to take the loader lock.
@@ -6148,7 +6148,7 @@ INT_PTR NDirect::GetNativeLibraryExport(NATIVE_LIBRARY_HANDLE handle, LPCWSTR sy
61486148
if ((address == NULL) && throwOnError)
61496149
COMPlusThrow(kEntryPointNotFoundException, IDS_EE_NDIRECT_GETPROCADDR_WIN_DLL, symbolName);
61506150
#else // !FEATURE_PAL
6151-
INT_PTR address = reinterpret_cast<INT_PTR>(PAL_GetProcAddressDirect((NATIVE_LIBRARY_HANDLE)handle, lpstr));
6151+
INT_PTR address = reinterpret_cast<INT_PTR>(PAL_GetProcAddressDirect(handle, lpstr));
61526152
if ((address == NULL) && throwOnError)
61536153
COMPlusThrow(kEntryPointNotFoundException, IDS_EE_NDIRECT_GETPROCADDR_UNIX_SO, symbolName);
61546154
#endif // !FEATURE_PAL
@@ -6632,7 +6632,7 @@ NATIVE_LIBRARY_HANDLE NDirect::LoadLibraryModuleBySearch(Assembly *callingAssemb
66326632
}
66336633

66346634
// This Method returns an instance of the PAL-Registered handle
6635-
HINSTANCE NDirect::LoadLibraryModule(NDirectMethodDesc * pMD, LoadLibErrorTracker * pErrorTracker)
6635+
NATIVE_LIBRARY_HANDLE NDirect::LoadLibraryModule(NDirectMethodDesc * pMD, LoadLibErrorTracker * pErrorTracker)
66366636
{
66376637
CONTRACTL
66386638
{
@@ -6648,12 +6648,9 @@ HINSTANCE NDirect::LoadLibraryModule(NDirectMethodDesc * pMD, LoadLibErrorTracke
66486648
PREFIX_ASSUME( name != NULL );
66496649
MAKE_WIDEPTR_FROMUTF8( wszLibName, name );
66506650

6651-
ModuleHandleHolder hmod = LoadLibraryModuleViaCallback(pMD, wszLibName);
6651+
NativeLibraryHandleHolder hmod = LoadLibraryModuleViaCallback(pMD, wszLibName);
66526652
if (hmod != NULL)
66536653
{
6654-
#ifdef FEATURE_PAL
6655-
hmod = PAL_RegisterLibraryDirect(hmod, wszLibName);
6656-
#endif // FEATURE_PAL
66576654
return hmod.Extract();
66586655
}
66596656

@@ -6666,9 +6663,6 @@ HINSTANCE NDirect::LoadLibraryModule(NDirectMethodDesc * pMD, LoadLibErrorTracke
66666663
hmod = LoadLibraryModuleViaHost(pMD, wszLibName);
66676664
if (hmod != NULL)
66686665
{
6669-
#ifdef FEATURE_PAL
6670-
hmod = PAL_RegisterLibraryDirect(hmod, wszLibName);
6671-
#endif // FEATURE_PAL
66726666
return hmod.Extract();
66736667
}
66746668
}
@@ -6682,10 +6676,6 @@ HINSTANCE NDirect::LoadLibraryModule(NDirectMethodDesc * pMD, LoadLibErrorTracke
66826676
hmod = LoadLibraryModuleBySearch(pMD, pErrorTracker, wszLibName);
66836677
if (hmod != NULL)
66846678
{
6685-
#ifdef FEATURE_PAL
6686-
hmod = PAL_RegisterLibraryDirect(hmod, wszLibName);
6687-
#endif // FEATURE_PAL
6688-
66896679
// If we have a handle add it to the cache.
66906680
pDomain->AddUnmanagedImageToCache(wszLibName, hmod);
66916681
return hmod.Extract();
@@ -6696,9 +6686,6 @@ HINSTANCE NDirect::LoadLibraryModule(NDirectMethodDesc * pMD, LoadLibErrorTracke
66966686
hmod = LoadLibraryModuleViaEvent(pMD, wszLibName);
66976687
if (hmod != NULL)
66986688
{
6699-
#ifdef FEATURE_PAL
6700-
hmod = PAL_RegisterLibraryDirect(hmod, wszLibName);
6701-
#endif // FEATURE_PAL
67026689
return hmod.Extract();
67036690
}
67046691
}
@@ -6753,7 +6740,7 @@ VOID NDirect::NDirectLink(NDirectMethodDesc *pMD)
67536740
LoadLibErrorTracker errorTracker;
67546741

67556742
BOOL fSuccess = FALSE;
6756-
HINSTANCE hmod = LoadLibraryModule( pMD, &errorTracker );
6743+
NATIVE_LIBRARY_HANDLE hmod = LoadLibraryModule( pMD, &errorTracker );
67576744
if ( hmod )
67586745
{
67596746
LPVOID pvTarget = NDirectGetEntryPoint(pMD, hmod);

src/vm/dllimport.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ class NDirect
7373
//---------------------------------------------------------
7474
static HRESULT HasNAT_LAttribute(IMDInternalImport *pInternalImport, mdToken token, DWORD dwMemberAttrs);
7575

76-
static LPVOID NDirectGetEntryPoint(NDirectMethodDesc *pMD, HINSTANCE hMod);
76+
static LPVOID NDirectGetEntryPoint(NDirectMethodDesc *pMD, NATIVE_LIBRARY_HANDLE hMod);
7777
static NATIVE_LIBRARY_HANDLE LoadLibraryFromPath(LPCWSTR libraryPath, BOOL throwOnError);
7878
static NATIVE_LIBRARY_HANDLE LoadLibraryByName(LPCWSTR name, Assembly *callingAssembly,
7979
BOOL hasDllImportSearchPathFlags, DWORD dllImportSearchPathFlags,
8080
BOOL throwOnError);
81-
static HINSTANCE LoadLibraryModule(NDirectMethodDesc * pMD, LoadLibErrorTracker *pErrorTracker);
81+
static NATIVE_LIBRARY_HANDLE LoadLibraryModule(NDirectMethodDesc * pMD, LoadLibErrorTracker *pErrorTracker);
8282
static void FreeNativeLibrary(NATIVE_LIBRARY_HANDLE handle);
8383
static INT_PTR GetNativeLibraryExport(NATIVE_LIBRARY_HANDLE handle, LPCWSTR symbolName, BOOL throwOnError);
8484

src/vm/method.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5110,7 +5110,7 @@ void NDirectMethodDesc::InterlockedSetNDirectFlags(WORD wFlags)
51105110
}
51115111

51125112
#ifndef CROSSGEN_COMPILE
5113-
FARPROC NDirectMethodDesc::FindEntryPointWithMangling(HINSTANCE hMod, PTR_CUTF8 entryPointName) const
5113+
FARPROC NDirectMethodDesc::FindEntryPointWithMangling(NATIVE_LIBRARY_HANDLE hMod, PTR_CUTF8 entryPointName) const
51145114
{
51155115
CONTRACTL
51165116
{
@@ -5120,7 +5120,11 @@ FARPROC NDirectMethodDesc::FindEntryPointWithMangling(HINSTANCE hMod, PTR_CUTF8
51205120
}
51215121
CONTRACTL_END;
51225122

5123+
#ifndef FEATURE_PAL
51235124
FARPROC pFunc = GetProcAddress(hMod, entryPointName);
5125+
#else
5126+
FARPROC pFunc = PAL_GetProcAddressDirect(hMod, entryPointName);
5127+
#endif
51245128

51255129
#if defined(_TARGET_X86_)
51265130

@@ -5163,7 +5167,7 @@ FARPROC NDirectMethodDesc::FindEntryPointWithMangling(HINSTANCE hMod, PTR_CUTF8
51635167
}
51645168

51655169
//*******************************************************************************
5166-
LPVOID NDirectMethodDesc::FindEntryPoint(HINSTANCE hMod) const
5170+
LPVOID NDirectMethodDesc::FindEntryPoint(NATIVE_LIBRARY_HANDLE hMod) const
51675171
{
51685172
CONTRACTL
51695173
{

src/vm/method.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3030,10 +3030,10 @@ class NDirectMethodDesc : public MethodDesc
30303030
// Find the entry point name and function address
30313031
// based on the module and data from NDirectMethodDesc
30323032
//
3033-
LPVOID FindEntryPoint(HINSTANCE hMod) const;
3033+
LPVOID FindEntryPoint(NATIVE_LIBRARY_HANDLE hMod) const;
30343034

30353035
private:
3036-
FARPROC FindEntryPointWithMangling(HINSTANCE mod, PTR_CUTF8 entryPointName) const;
3036+
FARPROC FindEntryPointWithMangling(NATIVE_LIBRARY_HANDLE mod, PTR_CUTF8 entryPointName) const;
30373037

30383038
public:
30393039

src/vm/util.hpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -810,9 +810,18 @@ inline void UnsafeTlsFreeForHolder(DWORD* addr)
810810
typedef Holder<DWORD*, DoNothing<DWORD*>, UnsafeTlsFreeForHolder> TlsHolder;
811811

812812
// A holder for HMODULE.
813-
FORCEINLINE void VoidFreeLibrary(HMODULE h) { WRAPPER_NO_CONTRACT; CLRFreeLibrary(h); }
813+
FORCEINLINE void VoidFreeNativeLibrary(NATIVE_LIBRARY_HANDLE h)
814+
{
815+
WRAPPER_NO_CONTRACT;
816+
817+
#ifdef FEATURE_PAL
818+
PAL_FreeLibraryDirect(h);
819+
#else
820+
FreeLibrary(h);
821+
#endif
822+
}
814823

815-
typedef Wrapper<HMODULE, DoNothing<HMODULE>, VoidFreeLibrary, NULL> ModuleHandleHolder;
824+
typedef Wrapper<NATIVE_LIBRARY_HANDLE, DoNothing<NATIVE_LIBRARY_HANDLE>, VoidFreeNativeLibrary, NULL> NativeLibraryHandleHolder;
816825

817826
#ifndef FEATURE_PAL
818827

0 commit comments

Comments
 (0)