Skip to content

Commit ec861ec

Browse files
author
swaroop-sridhar
committed
Refactor LoadLibrary Methods
This change refactors the code in DllImport in preparation for implementing the new NativeLibrary API here: dotnet/corefx#32015 The two main changes are: 1) A change in the semantics of the internal LoadLibrary helper functions. When a native library is loaded, there are two categories of callers expecting different return values: External callers like AssemblyNative::InternalLoadUnmanagedDllFromPath() and the upcoming System.Runtime.Interop.Marshall.LoadLibrary() need the raw system handle Internal callers like LoadLibraryModule() need the PAL registered handle This change modifies the internal LoadLibraryModule* methods to work in terms of native system handles, so that external callers can obrain them directly. Methods requiring PAL-handles can register them explicitly. There is no change in external signature of DllImport class, or the native Dll cache in AppDomain class. 2) Differentiate HMODULE and NATIVE_LIBRARY_HANDLE This change defines NATIVE_LIBRARY_HANDLE type to represent raw system handles to native libraries that are not registered with the PAL (On Unix systems). The types on PAL and DlImport methods are adjusted to make this semantic distinction explicit.
1 parent d793aa3 commit ec861ec

File tree

6 files changed

+201
-177
lines changed

6 files changed

+201
-177
lines changed

src/inc/palclr_win.h

+6
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,10 @@
142142
#define WIN_PAL_ENDTRY_NAKED_DBG
143143
#endif // defined(ENABLE_CONTRACTS_IMPL) && !defined(JIT64_BUILD)
144144

145+
#if !defined (FEATURE_PAL)
146+
// Native system libray handle.
147+
// In Windows, NATIVE_LIBRARY_HANDLE is the same as HMODULE.
148+
typedef HMODULE NATIVE_LIBRARY_HANDLE;
149+
#endif // !FEATURE_PAL
150+
145151
#endif // __PALCLR_WIN_H__

src/pal/inc/pal.h

+5
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ extern "C" {
6868
#include <pal_error.h>
6969
#include <pal_mstypes.h>
7070

71+
// Native system libray handle.
72+
// On Unix systems, NATIVE_LIBRARY_HANDLE type represents a library handle not registered with the PAL.
73+
// To get a HMODULE on Unix, call PAL_RegisterLibraryDirect() on a NATIVE_LIBRARY_HANDLE.
74+
typedef void * NATIVE_LIBRARY_HANDLE;
75+
7176
/******************* Processor-specific glue *****************************/
7277

7378
#ifndef _MSC_VER

src/pal/src/loader/module.cpp

+14-14
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ static bool LOADConvertLibraryPathWideStringToMultibyteString(
103103
INT *multibyteLibraryPathLengthRef);
104104
static BOOL LOADValidateModule(MODSTRUCT *module);
105105
static LPWSTR LOADGetModuleFileName(MODSTRUCT *module);
106-
static MODSTRUCT *LOADAddModule(void *dl_handle, LPCSTR libraryNameOrPath);
106+
static MODSTRUCT *LOADAddModule(NATIVE_LIBRARY_HANDLE dl_handle, LPCSTR libraryNameOrPath);
107107
static void *LOADLoadLibraryDirect(LPCSTR libraryNameOrPath);
108108
static BOOL LOADFreeLibrary(MODSTRUCT *module, BOOL fCallDllMain);
109-
static HMODULE LOADRegisterLibraryDirect(void *dl_handle, LPCSTR libraryNameOrPath, BOOL fDynamic);
109+
static HMODULE LOADRegisterLibraryDirect(NATIVE_LIBRARY_HANDLE dl_handle, LPCSTR libraryNameOrPath, BOOL fDynamic);
110110
static HMODULE LOADLoadLibrary(LPCSTR shortAsciiName, BOOL fDynamic);
111111
static BOOL LOADCallDllMainSafe(MODSTRUCT *module, DWORD dwReason, LPVOID lpReserved);
112112

@@ -564,15 +564,15 @@ GetModuleFileNameW(
564564
565565
Returns the system handle to the loaded library, or nullptr upon failure (error is set via SetLastError()).
566566
*/
567-
void *
567+
NATIVE_LIBRARY_HANDLE
568568
PALAPI
569569
PAL_LoadLibraryDirect(
570570
IN LPCWSTR lpLibFileName)
571571
{
572572
PathCharString pathstr;
573573
CHAR * lpstr = nullptr;
574574
INT name_length;
575-
void *dl_handle = nullptr;
575+
NATIVE_LIBRARY_HANDLE dl_handle = nullptr;
576576

577577
PERF_ENTRY(LoadLibraryDirect);
578578
ENTRY("LoadLibraryDirect (lpLibFileName=%p (%S)) \n",
@@ -617,7 +617,7 @@ PAL_LoadLibraryDirect(
617617
HMODULE
618618
PALAPI
619619
PAL_RegisterLibraryDirect(
620-
IN void *dl_handle,
620+
IN NATIVE_LIBRARY_HANDLE dl_handle,
621621
IN LPCWSTR lpLibFileName)
622622
{
623623
PathCharString pathstr;
@@ -684,7 +684,7 @@ PAL_RegisterModule(
684684

685685
LockModuleList();
686686

687-
void *dl_handle = LOADLoadLibraryDirect(lpLibFileName);
687+
NATIVE_LIBRARY_HANDLE dl_handle = LOADLoadLibraryDirect(lpLibFileName);
688688
if (dl_handle)
689689
{
690690
// This only creates/adds the module handle and doesn't call DllMain
@@ -1400,7 +1400,7 @@ static void *LOADLoadLibraryDirect(LPCSTR libraryNameOrPath)
14001400
_ASSERTE(libraryNameOrPath != nullptr);
14011401
_ASSERTE(libraryNameOrPath[0] != '\0');
14021402

1403-
void *dl_handle = dlopen(libraryNameOrPath, RTLD_LAZY);
1403+
NATIVE_LIBRARY_HANDLE dl_handle = dlopen(libraryNameOrPath, RTLD_LAZY);
14041404
if (dl_handle == nullptr)
14051405
{
14061406
SetLastError(ERROR_MOD_NOT_FOUND);
@@ -1420,7 +1420,7 @@ Function :
14201420
Allocate and initialize a new MODSTRUCT structure
14211421
14221422
Parameters :
1423-
void *dl_handle : handle returned by dl_open, goes in MODSTRUCT::dl_handle
1423+
NATIVE_LIBRARY_HANDLE dl_handle : handle returned by dl_open, goes in MODSTRUCT::dl_handle
14241424
14251425
char *name : name of new module. after conversion to widechar,
14261426
goes in MODSTRUCT::lib_name
@@ -1432,7 +1432,7 @@ Notes :
14321432
'name' is used to initialize MODSTRUCT::lib_name. The other member is set to NULL
14331433
In case of failure (in malloc or MBToWC), this function sets LastError.
14341434
--*/
1435-
static MODSTRUCT *LOADAllocModule(void *dl_handle, LPCSTR name)
1435+
static MODSTRUCT *LOADAllocModule(NATIVE_LIBRARY_HANDLE dl_handle, LPCSTR name)
14361436
{
14371437
MODSTRUCT *module;
14381438
LPWSTR wide_name;
@@ -1485,13 +1485,13 @@ static MODSTRUCT *LOADAllocModule(void *dl_handle, LPCSTR name)
14851485
Registers a system handle to a loaded library with the module list.
14861486
14871487
Parameters:
1488-
void *dl_handle: System handle to the loaded library.
1488+
NATIVE_LIBRARY_HANDLE dl_handle: System handle to the loaded library.
14891489
LPCSTR libraryNameOrPath: The library that was loaded.
14901490
14911491
Return value:
14921492
PAL handle to the loaded library, or nullptr upon failure (error is set via SetLastError()).
14931493
*/
1494-
static MODSTRUCT *LOADAddModule(void *dl_handle, LPCSTR libraryNameOrPath)
1494+
static MODSTRUCT *LOADAddModule(NATIVE_LIBRARY_HANDLE dl_handle, LPCSTR libraryNameOrPath)
14951495
{
14961496
_ASSERTE(dl_handle != nullptr);
14971497
_ASSERTE(libraryNameOrPath != nullptr);
@@ -1555,14 +1555,14 @@ static MODSTRUCT *LOADAddModule(void *dl_handle, LPCSTR libraryNameOrPath)
15551555
Registers a system handle to a loaded library with the module list.
15561556
15571557
Parameters:
1558-
void *dl_handle: System handle to the loaded library.
1558+
NATIVE_LIBRARY_HANDLE dl_handle: System handle to the loaded library.
15591559
LPCSTR libraryNameOrPath: The library that was loaded.
15601560
BOOL fDynamic: TRUE if dynamic load through LoadLibrary, FALSE if static load through RegisterLibrary.
15611561
15621562
Return value:
15631563
PAL handle to the loaded library, or nullptr upon failure (error is set via SetLastError()).
15641564
*/
1565-
static HMODULE LOADRegisterLibraryDirect(void *dl_handle, LPCSTR libraryNameOrPath, BOOL fDynamic)
1565+
static HMODULE LOADRegisterLibraryDirect(NATIVE_LIBRARY_HANDLE dl_handle, LPCSTR libraryNameOrPath, BOOL fDynamic)
15661566
{
15671567
MODSTRUCT *module = LOADAddModule(dl_handle, libraryNameOrPath);
15681568
if (module == nullptr)
@@ -1631,7 +1631,7 @@ Return value :
16311631
static HMODULE LOADLoadLibrary(LPCSTR shortAsciiName, BOOL fDynamic)
16321632
{
16331633
HMODULE module = nullptr;
1634-
void *dl_handle = nullptr;
1634+
NATIVE_LIBRARY_HANDLE dl_handle = nullptr;
16351635

16361636
// Check whether we have been requested to load 'libc'. If that's the case, then:
16371637
// * For Linux, use the full name of the library that is defined in <gnu/lib-names.h> by the

src/vm/assemblynative.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ INT_PTR QCALLTYPE AssemblyNative::InternalLoadUnmanagedDllFromPath(LPCWSTR unman
310310
{
311311
QCALL_CONTRACT;
312312

313-
HMODULE moduleHandle = nullptr;
313+
NATIVE_LIBRARY_HANDLE moduleHandle = nullptr;
314314

315315
BEGIN_QCALL;
316316

0 commit comments

Comments
 (0)