Skip to content
This repository was archived by the owner on Nov 13, 2026. It is now read-only.

Lazy PInvoke resolution - #1240

Merged
MichalStrehovsky merged 5 commits into
dotnet:masterfrom
MichalStrehovsky:lazyPinvoke
May 6, 2016
Merged

MichalStrehovsky merged 5 commits into
dotnet:masterfrom
MichalStrehovsky:lazyPinvoke

Conversation

@MichalStrehovsky

@MichalStrehovsky MichalStrehovsky commented May 5, 2016 •

Copy link
Copy Markdown
Member

Work to resolve PInvoke entrypoints lazily. This enables us to support programs that have platform-specific light up behaviors (call platform APIs depending on the platform they are native-compiled for). It also removes the need to supply import libraries when linking the OBJ files (which is super convenient because managed developers are not used to it).

ILLocalVariable vNativeFunctionPointer = _emitter.NewLocal(_targetMethod.Context.GetWellKnownType(WellKnownType.IntPtr));

MetadataType lazyHelperType = _targetMethod.Context.GetHelperType("LazyPInvokeHelpers");
if (_importMetadata.Module == "[MRT]" || _importMetadata.Module == "*" || _targetMethod.OwningType == lazyHelperType)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The [MRT] and * conditions are the result of me bashing on it until it worked. I'll sit down and find the right pattern tomorrow.

@jkotas jkotas May 5, 2016 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

  • * condition is fine.
  • [MRT] would be ideally replaced with * as well. But we can live it for now. Replacing it with * would require modifying the .NET Native for UWP (adding MRT export library to the link step).
  • _targetMethod.OwningType == lazyHelperType should be changed to:
if (targeting Windows)
    eager = _importMetadata.Module.StartsWith("api-ms-win-");
else
    eager = _importMetadata.Module.StartsWith("System.Private.");

Eventually, we can have command line options or attributes to allow overriding this default policy.

@jkotas

jkotas commented May 5, 2016

Copy link
Copy Markdown
Member

cc @yizhang82

// Eager call
MethodDesc nativeMethod =
new PInvokeTargetNativeMethod(_targetMethod.OwningType, nativeSig, _importMetadata);
fnptrLoadStream.Emit(ILOpcode.ldftn, _emitter.NewToken(nativeMethod));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can this just use call as before? No need to stress JIT optimizations... .

@yizhang82

Copy link
Copy Markdown
Contributor

Incidentally I've happened to asked @tijoytom to look at supporting LoadLibrary in MCG today :)

}

if (!Internal.IL.Stubs.PInvokeMarshallingILEmitter.RequiresMarshalling(method))
// Only treat the synthetic target methods as real PInvoke.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The pre-generated interop should still go directly here, without going through PInvokeMarshallingILEmitter. We should encapsulate it in helper method - something like:

static bool IsRawPInvoke(MethodDesc method)
{
   return method.IsPInvoke && ((method is Internal.IL.Stubs.PInvokeTargetNativeMethod) || Internal.IL.McgInteropSupport.IsPregeneratedInterop(method));
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

(I will have several uses for this helper in the GC transitions change that I am working on.)

if (importModule == "[MRT]" || importModule == "*")
useLazyResolution = false;
else if (_targetMethod.Context.Target.IsWindows)
useLazyResolution = !importModule.StartsWith("api-ms-win-core-libraryloader-l1-2-0");

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Note to self: fix this before merging. There's not much to test if this is just api-ms-win-


extern "C" void* CoreLibNative_LoadLibrary(const char* filename)
{
return dlopen(filename, RTLD_LAZY);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Might need to prepend lib + append .so depending on the platform conventions here, but I don't have a Linux/OSX machine to test with so...

@MichalStrehovsky MichalStrehovsky changed the title [WIP] Lazy PInvoke resolution Lazy PInvoke resolution May 5, 2016
@MichalStrehovsky

Copy link
Copy Markdown
Member Author

@jkotas Removing the WIP tag. I'll file an up-for-grabs issue about making this work on Unix.

@jkotas

jkotas commented May 5, 2016

Copy link
Copy Markdown
Member

LGTM

@MichalStrehovsky
MichalStrehovsky merged commit cbba590 into dotnet:master May 6, 2016
@MichalStrehovsky
MichalStrehovsky deleted the lazyPinvoke branch May 6, 2016 00:49
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