[CoreCLR] Dynamically build custom delegate types to support [Export]#10503
Merged
jonathanpeppers merged 2 commits intodotnet:mainfrom Sep 19, 2025
Merged
Conversation
…into function pointers on CoreCLR
This was referenced Sep 19, 2025
Contributor
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
grendello
reviewed
Sep 19, 2025
| string delegateTypeName = $"callback_delegate_{EncodeMethodSignature (parameter_type_infos, return_type_info)}"; | ||
| lock (DynamicCallbackFactory.Module) { | ||
| delegate_type = DynamicCallbackFactory.Module.GetType (delegateTypeName) | ||
| ?? CreateDelegateType (delegateTypeName); |
Contributor
There was a problem hiding this comment.
Can we/should we cache the returned type?
Member
Author
There was a problem hiding this comment.
The type is stored in DynamicCallbackFactory.Module so when we later run into a delegate with the same method signature, the existing delegate type will be reused.
grendello
approved these changes
Sep 19, 2025
jonathanpeppers
approved these changes
Sep 19, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #10472
Supersedes #10493 and dotnet/java-interop#1364
The problem is that the Marshal.GetFunctionPointerForDelegate method does not work with delegates with generic types, such as
Action<...>orFunc<...>(context: dotnet/runtime#32963), which was the case of all the delegates created inMono.Android.ExportthroughDynamicMethod.This PR replaces the use of these generic delegate types with dynamically built non-generic types. It is based on an existing implementation in java-interop (see https://github.com/dotnet/java-interop/blob/02bceb03f7c07858590d930ef507745a88200a48/src/Java.Interop.Export/Java.Interop/MarshalMemberBuilder.cs#L274-L311). The CoreCLR is able to marshal the delegates built with these types to function pointers just fine.
In a previous attempt at fixing this issue (#10493), I chose a solution which modified the shape of
JniNativeMethodRegistrationwhich would be an ABI breaking change (dotnet/java-interop#1364). This implementation is much simpler and doesn't cause the same problem. Thanks to @jonpryor for steering me in this direction./cc @jonathanpeppers @grendello