Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

LoadLibrary refactoring #20841

Merged
merged 2 commits into from
Nov 12, 2018
Merged

LoadLibrary refactoring #20841

merged 2 commits into from
Nov 12, 2018

Conversation

swaroop-sridhar
Copy link

@swaroop-sridhar swaroop-sridhar commented Nov 6, 2018

This change refactors the code in DllImport in preparation
for implementing the new NativeLibrary API here:
dotnet/corefx#32015

In particular, it introduces 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.

[PS: NDirect::LoadLibraryFromPath was already written to return the
system handle instead of the PAL handle. This change extends
the behavior to other private members.]

There is no change in external signature of DllImport class, or the
native Dll cache in AppDomain class.

@swaroop-sridhar swaroop-sridhar changed the title WIP Test Load Library refactoring Test Load Library refactoring Nov 7, 2018
@AaronRobinsonMSFT AaronRobinsonMSFT added this to the 3.0 milestone Nov 7, 2018
// Local helper function for the LoadLibraryModule function below
// Local helper function to load a library.
// Load the library directly, and don't register it yet with PAL. Returns the native system libray handle.
// * External callers likeAssemblyNative::InternalLoadUnmanagedDllFromPath() and the upcoming
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"like AssemblyNative::InternalLoadUnmanagedDllFromPath()"

// Load the library directly, and don't register it yet with PAL. Returns the native system libray handle.
// * External callers likeAssemblyNative::InternalLoadUnmanagedDllFromPath() and the upcoming
// System.Runtime.Interop.Marshall.LoadLibrary() need the raw system handle
// * Internal callers like LoadLibraryModule() can obrain it via PAL_RegisterLibraryDirect()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"obtain"

*szComma = '\0';
while (COMCharacter::nativeIsWhiteSpace(*(++szComma)));
*szComma = '\0';
while (COMCharacter::nativeIsWhiteSpace(*(++szComma)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move the ; to the next line or comment that we are trimming here.

@swaroop-sridhar swaroop-sridhar changed the title Test Load Library refactoring LoadLibrary refactoring Nov 7, 2018
@janvorli
Copy link
Member

janvorli commented Nov 7, 2018

@swaroop-sridhar I am sorry for the delay, I was focusing on other stuff. I am planning to review this later today.

Copy link
Member

@janvorli janvorli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing to consider. You have added comments describing when the HMODULE is actually the native handle, which is great. But I wonder if it would be even better to change the return type of these functions to VOID*.

@@ -6069,7 +6069,13 @@ class LoadLibErrorTracker
SString m_message;
}; // class LoadLibErrorTracker

// Local helper function for the LoadLibraryModule function below
// Local helper function to load a library.
// Load the library directly, and don't register it yet with PAL. Returns the native system libray handle.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A nit - maybe update to "don't register it yet with PAL on Unix" as there is no PAL on Windows. There is another place with a similar comment.


if (pMD->HasDefaultDllImportSearchPathsAttribute())
if(pModule->HasDefaultDllImportSearchPathsAttribute())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A nit - missing space after if at multiple places. I know it didn't come from your change, but it would be nice to fix when you are editing the code.

// System.Runtime.Interop.Marshall.LoadLibrary() need the raw system handle
// * Internal callers like LoadLibraryModule() can obrain it via PAL_RegisterLibraryDirect()
//
// This method returns native system libray handle not registered with the PAL
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A nit: libray -> library (multiple places)

// First checks if the method has DefaultDllImportSearchPathsAttribute. If method has the attribute
// then dllImportSearchPathFlag is set to its value.
// Otherwise checks if the assembly has the attribute.
// If assembly has the attribute then flag ise set to its value.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A nit: ise -> is

@jkotas
Copy link
Member

jkotas commented Nov 8, 2018

added comments describing when the HMODULE

Or even better: define NATIVE_LIBRARY_HANDLE to make this self-documenting.

@swaroop-sridhar
Copy link
Author

Or even better: define NATIVE_LIBRARY_HANDLE to make this self-documenting.

I thought about this, but held back because the types defined in pal_mstypes.h were a subset of
https://docs.microsoft.com/en-us/windows/desktop/winprog/windows-data-types

I'll make the change now.

@swaroop-sridhar
Copy link
Author

Thanks for the feedback @janvorli @jkotas @AaronRobinsonMSFT
I've addressed the feedback in 73012b5

// In Windows, NATIVE_LIBRARY_HANDLE is the same as HMODULE.
// On Unix systems, NATIVE_LIBRARY_HANDLE type represents a library handle not registered with the PAL.
// To get a HMODULE on Unix, call PAL_RegisterLibraryDirect() on a NATIVE_LIBRARY_HANDLE.
typedef HANDLE NATIVE_LIBRARY_HANDLE;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A nit - the dlopen returns void*, so it would be cleaner to define it that way.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I cannot see the type definition for Windows. The pal.h is included for Unix only.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a definition in common.h, for the !FEATURE_PAL case Thanks

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inc/palclr.h would be a better place for it. It is where all similar definitions are.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I changed the Windows definition to be in palclr.h via palclr_win.h

@swaroop-sridhar swaroop-sridhar force-pushed the pal branch 3 times, most recently from 7b14435 to 220797a Compare November 8, 2018 22:01
@swaroop-sridhar
Copy link
Author

@dotnet-bot test dotnet-coreclr

@swaroop-sridhar
Copy link
Author

@dotnet-bot test Ubuntu x64 Checked CoreFX Tests

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.
@swaroop-sridhar swaroop-sridhar force-pushed the pal branch 2 times, most recently from ec861ec to 0a0077c Compare November 9, 2018 09:01
@swaroop-sridhar
Copy link
Author

@dotnet-bot test this please

@swaroop-sridhar
Copy link
Author

@janvorli, I needed to make another small change in PAL code for test pass. Please review this commit: 48289a5

@janvorli janvorli merged commit 214c3b6 into dotnet:master Nov 12, 2018
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
* Refactor LoadLibrary Methods

This change refactors the code in DllImport in preparation
for implementing the new NativeLibrary API here:
dotnet/corefxdotnet/coreclr#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.

* 
Fix loading LibC via PAL_LoadLibraryDirect()


Commit migrated from dotnet/coreclr@214c3b6
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants