From a6fdbcbbd3e4cc0842a1eef287fd92ef90ed2146 Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Tue, 21 May 2019 07:48:53 -0700 Subject: [PATCH] Avoid PAL LoadLibrary simulator for regular PInvoke (dotnet/coreclr#24669) Fixes dotnet/coreclr#21009 Commit migrated from https://github.com/dotnet/coreclr/commit/221dc73878027e95b515d0c46cad0266331e538d --- src/coreclr/src/pal/inc/pal.h | 7 ---- src/coreclr/src/pal/src/loader/module.cpp | 54 ------------------------------- src/coreclr/src/vm/appdomain.cpp | 9 +++--- src/coreclr/src/vm/appdomain.hpp | 5 ++- src/coreclr/src/vm/dllimport.cpp | 23 +++---------- src/coreclr/src/vm/dllimport.h | 4 +-- src/coreclr/src/vm/method.cpp | 8 +++-- src/coreclr/src/vm/method.hpp | 4 +-- src/coreclr/src/vm/util.hpp | 13 ++++++-- 9 files changed, 32 insertions(+), 95 deletions(-) diff --git a/src/coreclr/src/pal/inc/pal.h b/src/coreclr/src/pal/inc/pal.h index 540b5c6..bcb4e8c 100644 --- a/src/coreclr/src/pal/inc/pal.h +++ b/src/coreclr/src/pal/inc/pal.h @@ -2655,13 +2655,6 @@ PAL_LoadLibraryDirect( IN LPCWSTR lpLibFileName); PALIMPORT -HMODULE -PALAPI -PAL_RegisterLibraryDirect( - IN NATIVE_LIBRARY_HANDLE dl_handle, - IN LPCWSTR lpLibFileName); - -PALIMPORT BOOL PALAPI PAL_FreeLibraryDirect( diff --git a/src/coreclr/src/pal/src/loader/module.cpp b/src/coreclr/src/pal/src/loader/module.cpp index e50ed56b..dc4c8ba 100644 --- a/src/coreclr/src/pal/src/loader/module.cpp +++ b/src/coreclr/src/pal/src/loader/module.cpp @@ -637,60 +637,6 @@ done: /* Function: - PAL_RegisterLibraryDirect - - Registers a system handle to a loaded library with the module list. - - Returns a PAL handle to the loaded library, or nullptr upon failure (error is set via SetLastError()). -*/ -HMODULE -PALAPI -PAL_RegisterLibraryDirect( - IN NATIVE_LIBRARY_HANDLE dl_handle, - IN LPCWSTR lpLibFileName) -{ - PathCharString pathstr; - CHAR * lpstr = nullptr; - INT name_length; - HMODULE hModule = nullptr; - - PERF_ENTRY(RegisterLibraryDirect); - ENTRY("RegisterLibraryDirect (lpLibFileName=%p (%S)) \n", - lpLibFileName ? lpLibFileName : W16_NULLSTRING, - lpLibFileName ? lpLibFileName : W16_NULLSTRING); - - if (!LOADVerifyLibraryPath(lpLibFileName)) - { - goto done; - } - - lpstr = pathstr.OpenStringBuffer((PAL_wcslen(lpLibFileName)+1) * MaxWCharToAcpLength); - if (nullptr == lpstr) - { - goto done; - } - if (!LOADConvertLibraryPathWideStringToMultibyteString(lpLibFileName, lpstr, &name_length)) - { - goto done; - } - - /* do the Dos/Unix conversion on our own copy of the name */ - FILEDosToUnixPathA(lpstr); - pathstr.CloseBuffer(name_length); - - /* let LOADRegisterLibraryDirect call SetLastError in case of failure */ - LockModuleList(); - hModule = LOADRegisterLibraryDirect(dl_handle, lpstr, true /* fDynamic */); - UnlockModuleList(); - -done: - LOGEXIT("RegisterLibraryDirect returns HMODULE %p\n", hModule); - PERF_EXIT(RegisterLibraryDirect); - return hModule; -} - -/* -Function: PAL_FreeLibraryDirect Free a loaded library diff --git a/src/coreclr/src/vm/appdomain.cpp b/src/coreclr/src/vm/appdomain.cpp index 2d92d3f..cfd1d75 100644 --- a/src/coreclr/src/vm/appdomain.cpp +++ b/src/coreclr/src/vm/appdomain.cpp @@ -2880,7 +2880,6 @@ AppDomain::AppDomain() m_pRefClassFactHash = NULL; - m_ReversePInvokeCanEnter=TRUE; m_ForceTrivialWaitOperations = false; m_Stage=STAGE_CREATING; @@ -4702,7 +4701,7 @@ BOOL AppDomain::AddExceptionToCache(AssemblySpec* pSpec, Exception *ex) return m_AssemblyCache.StoreException(pSpec, ex); } -void AppDomain::AddUnmanagedImageToCache(LPCWSTR libraryName, HMODULE hMod) +void AppDomain::AddUnmanagedImageToCache(LPCWSTR libraryName, NATIVE_LIBRARY_HANDLE hMod) { CONTRACTL { @@ -4723,9 +4722,9 @@ void AppDomain::AddUnmanagedImageToCache(LPCWSTR libraryName, HMODULE hMod) } -HMODULE AppDomain::FindUnmanagedImageInCache(LPCWSTR libraryName) +NATIVE_LIBRARY_HANDLE AppDomain::FindUnmanagedImageInCache(LPCWSTR libraryName) { - CONTRACT(HMODULE) + CONTRACT(NATIVE_LIBRARY_HANDLE) { THROWS; GC_TRIGGERS; @@ -4739,7 +4738,7 @@ HMODULE AppDomain::FindUnmanagedImageInCache(LPCWSTR libraryName) AssemblySpec spec; spec.SetCodeBase(libraryName); - RETURN (HMODULE) m_UnmanagedCache.LookupEntry(&spec, 0); + RETURN (NATIVE_LIBRARY_HANDLE) m_UnmanagedCache.LookupEntry(&spec, 0); } BOOL AppDomain::RemoveFileFromCache(PEAssembly *pFile) diff --git a/src/coreclr/src/vm/appdomain.hpp b/src/coreclr/src/vm/appdomain.hpp index 8944c21..9029454 100644 --- a/src/coreclr/src/vm/appdomain.hpp +++ b/src/coreclr/src/vm/appdomain.hpp @@ -2073,8 +2073,8 @@ public: BOOL RemoveAssemblyFromCache(DomainAssembly* pAssembly); BOOL AddExceptionToCache(AssemblySpec* pSpec, Exception *ex); - void AddUnmanagedImageToCache(LPCWSTR libraryName, HMODULE hMod); - HMODULE FindUnmanagedImageInCache(LPCWSTR libraryName); + void AddUnmanagedImageToCache(LPCWSTR libraryName, NATIVE_LIBRARY_HANDLE hMod); + NATIVE_LIBRARY_HANDLE FindUnmanagedImageInCache(LPCWSTR libraryName); //**************************************************************************************** // // Adds or removes an assembly to the domain. @@ -2724,7 +2724,6 @@ public: size_t m_MemoryPressure; ArrayList m_NativeDllSearchDirectories; - BOOL m_ReversePInvokeCanEnter; bool m_ForceTrivialWaitOperations; public: diff --git a/src/coreclr/src/vm/dllimport.cpp b/src/coreclr/src/vm/dllimport.cpp index 8f3458d..7fc0863 100644 --- a/src/coreclr/src/vm/dllimport.cpp +++ b/src/coreclr/src/vm/dllimport.cpp @@ -5732,7 +5732,7 @@ void CreateCLRToDispatchCOMStub( #endif // FEATURE_COMINTEROP /*static*/ -LPVOID NDirect::NDirectGetEntryPoint(NDirectMethodDesc *pMD, HINSTANCE hMod) +LPVOID NDirect::NDirectGetEntryPoint(NDirectMethodDesc *pMD, NATIVE_LIBRARY_HANDLE hMod) { // GetProcAddress cannot be called while preemptive GC is disabled. // It requires the OS to take the loader lock. @@ -6148,7 +6148,7 @@ INT_PTR NDirect::GetNativeLibraryExport(NATIVE_LIBRARY_HANDLE handle, LPCWSTR sy if ((address == NULL) && throwOnError) COMPlusThrow(kEntryPointNotFoundException, IDS_EE_NDIRECT_GETPROCADDR_WIN_DLL, symbolName); #else // !FEATURE_PAL - INT_PTR address = reinterpret_cast(PAL_GetProcAddressDirect((NATIVE_LIBRARY_HANDLE)handle, lpstr)); + INT_PTR address = reinterpret_cast(PAL_GetProcAddressDirect(handle, lpstr)); if ((address == NULL) && throwOnError) COMPlusThrow(kEntryPointNotFoundException, IDS_EE_NDIRECT_GETPROCADDR_UNIX_SO, symbolName); #endif // !FEATURE_PAL @@ -6632,7 +6632,7 @@ NATIVE_LIBRARY_HANDLE NDirect::LoadLibraryModuleBySearch(Assembly *callingAssemb } // This Method returns an instance of the PAL-Registered handle -HINSTANCE NDirect::LoadLibraryModule(NDirectMethodDesc * pMD, LoadLibErrorTracker * pErrorTracker) +NATIVE_LIBRARY_HANDLE NDirect::LoadLibraryModule(NDirectMethodDesc * pMD, LoadLibErrorTracker * pErrorTracker) { CONTRACTL { @@ -6648,12 +6648,9 @@ HINSTANCE NDirect::LoadLibraryModule(NDirectMethodDesc * pMD, LoadLibErrorTracke PREFIX_ASSUME( name != NULL ); MAKE_WIDEPTR_FROMUTF8( wszLibName, name ); - ModuleHandleHolder hmod = LoadLibraryModuleViaCallback(pMD, wszLibName); + NativeLibraryHandleHolder hmod = LoadLibraryModuleViaCallback(pMD, wszLibName); if (hmod != NULL) { -#ifdef FEATURE_PAL - hmod = PAL_RegisterLibraryDirect(hmod, wszLibName); -#endif // FEATURE_PAL return hmod.Extract(); } @@ -6666,9 +6663,6 @@ HINSTANCE NDirect::LoadLibraryModule(NDirectMethodDesc * pMD, LoadLibErrorTracke hmod = LoadLibraryModuleViaHost(pMD, wszLibName); if (hmod != NULL) { -#ifdef FEATURE_PAL - hmod = PAL_RegisterLibraryDirect(hmod, wszLibName); -#endif // FEATURE_PAL return hmod.Extract(); } } @@ -6682,10 +6676,6 @@ HINSTANCE NDirect::LoadLibraryModule(NDirectMethodDesc * pMD, LoadLibErrorTracke hmod = LoadLibraryModuleBySearch(pMD, pErrorTracker, wszLibName); if (hmod != NULL) { -#ifdef FEATURE_PAL - hmod = PAL_RegisterLibraryDirect(hmod, wszLibName); -#endif // FEATURE_PAL - // If we have a handle add it to the cache. pDomain->AddUnmanagedImageToCache(wszLibName, hmod); return hmod.Extract(); @@ -6696,9 +6686,6 @@ HINSTANCE NDirect::LoadLibraryModule(NDirectMethodDesc * pMD, LoadLibErrorTracke hmod = LoadLibraryModuleViaEvent(pMD, wszLibName); if (hmod != NULL) { -#ifdef FEATURE_PAL - hmod = PAL_RegisterLibraryDirect(hmod, wszLibName); -#endif // FEATURE_PAL return hmod.Extract(); } } @@ -6753,7 +6740,7 @@ VOID NDirect::NDirectLink(NDirectMethodDesc *pMD) LoadLibErrorTracker errorTracker; BOOL fSuccess = FALSE; - HINSTANCE hmod = LoadLibraryModule( pMD, &errorTracker ); + NATIVE_LIBRARY_HANDLE hmod = LoadLibraryModule( pMD, &errorTracker ); if ( hmod ) { LPVOID pvTarget = NDirectGetEntryPoint(pMD, hmod); diff --git a/src/coreclr/src/vm/dllimport.h b/src/coreclr/src/vm/dllimport.h index 5a3b06f..4e3b862 100644 --- a/src/coreclr/src/vm/dllimport.h +++ b/src/coreclr/src/vm/dllimport.h @@ -73,12 +73,12 @@ public: //--------------------------------------------------------- static HRESULT HasNAT_LAttribute(IMDInternalImport *pInternalImport, mdToken token, DWORD dwMemberAttrs); - static LPVOID NDirectGetEntryPoint(NDirectMethodDesc *pMD, HINSTANCE hMod); + static LPVOID NDirectGetEntryPoint(NDirectMethodDesc *pMD, NATIVE_LIBRARY_HANDLE hMod); static NATIVE_LIBRARY_HANDLE LoadLibraryFromPath(LPCWSTR libraryPath, BOOL throwOnError); static NATIVE_LIBRARY_HANDLE LoadLibraryByName(LPCWSTR name, Assembly *callingAssembly, BOOL hasDllImportSearchPathFlags, DWORD dllImportSearchPathFlags, BOOL throwOnError); - static HINSTANCE LoadLibraryModule(NDirectMethodDesc * pMD, LoadLibErrorTracker *pErrorTracker); + static NATIVE_LIBRARY_HANDLE LoadLibraryModule(NDirectMethodDesc * pMD, LoadLibErrorTracker *pErrorTracker); static void FreeNativeLibrary(NATIVE_LIBRARY_HANDLE handle); static INT_PTR GetNativeLibraryExport(NATIVE_LIBRARY_HANDLE handle, LPCWSTR symbolName, BOOL throwOnError); diff --git a/src/coreclr/src/vm/method.cpp b/src/coreclr/src/vm/method.cpp index 31c4974..cdbd443 100644 --- a/src/coreclr/src/vm/method.cpp +++ b/src/coreclr/src/vm/method.cpp @@ -5110,7 +5110,7 @@ void NDirectMethodDesc::InterlockedSetNDirectFlags(WORD wFlags) } #ifndef CROSSGEN_COMPILE -FARPROC NDirectMethodDesc::FindEntryPointWithMangling(HINSTANCE hMod, PTR_CUTF8 entryPointName) const +FARPROC NDirectMethodDesc::FindEntryPointWithMangling(NATIVE_LIBRARY_HANDLE hMod, PTR_CUTF8 entryPointName) const { CONTRACTL { @@ -5120,7 +5120,11 @@ FARPROC NDirectMethodDesc::FindEntryPointWithMangling(HINSTANCE hMod, PTR_CUTF8 } CONTRACTL_END; +#ifndef FEATURE_PAL FARPROC pFunc = GetProcAddress(hMod, entryPointName); +#else + FARPROC pFunc = PAL_GetProcAddressDirect(hMod, entryPointName); +#endif #if defined(_TARGET_X86_) @@ -5163,7 +5167,7 @@ FARPROC NDirectMethodDesc::FindEntryPointWithMangling(HINSTANCE hMod, PTR_CUTF8 } //******************************************************************************* -LPVOID NDirectMethodDesc::FindEntryPoint(HINSTANCE hMod) const +LPVOID NDirectMethodDesc::FindEntryPoint(NATIVE_LIBRARY_HANDLE hMod) const { CONTRACTL { diff --git a/src/coreclr/src/vm/method.hpp b/src/coreclr/src/vm/method.hpp index 7722097..66aacb9 100644 --- a/src/coreclr/src/vm/method.hpp +++ b/src/coreclr/src/vm/method.hpp @@ -3030,10 +3030,10 @@ public: // Find the entry point name and function address // based on the module and data from NDirectMethodDesc // - LPVOID FindEntryPoint(HINSTANCE hMod) const; + LPVOID FindEntryPoint(NATIVE_LIBRARY_HANDLE hMod) const; private: - FARPROC FindEntryPointWithMangling(HINSTANCE mod, PTR_CUTF8 entryPointName) const; + FARPROC FindEntryPointWithMangling(NATIVE_LIBRARY_HANDLE mod, PTR_CUTF8 entryPointName) const; public: diff --git a/src/coreclr/src/vm/util.hpp b/src/coreclr/src/vm/util.hpp index d1086a9..ddf9d82 100644 --- a/src/coreclr/src/vm/util.hpp +++ b/src/coreclr/src/vm/util.hpp @@ -810,9 +810,18 @@ inline void UnsafeTlsFreeForHolder(DWORD* addr) typedef Holder, UnsafeTlsFreeForHolder> TlsHolder; // A holder for HMODULE. -FORCEINLINE void VoidFreeLibrary(HMODULE h) { WRAPPER_NO_CONTRACT; CLRFreeLibrary(h); } +FORCEINLINE void VoidFreeNativeLibrary(NATIVE_LIBRARY_HANDLE h) +{ + WRAPPER_NO_CONTRACT; + +#ifdef FEATURE_PAL + PAL_FreeLibraryDirect(h); +#else + FreeLibrary(h); +#endif +} -typedef Wrapper, VoidFreeLibrary, NULL> ModuleHandleHolder; +typedef Wrapper, VoidFreeNativeLibrary, NULL> NativeLibraryHandleHolder; #ifndef FEATURE_PAL -- 2.7.4