-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support HasThis and ExplicitThis calling conventions in AssemblyBuilder and DynamicMethod #113666
Open
steveharter
wants to merge
6
commits into
dotnet:main
Choose a base branch
from
steveharter:FpToCalli
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
822af4a
Support HasThis calling convention in PersistedAssemblyBuilder
steveharter 972d0ac
Add support for ExplicitThis
steveharter 8f231b0
Initial attempt to add ExplicitThis support and tests
steveharter 1331ef5
Fix issues with ExplicitThis
steveharter cb2f457
Use 'object' for type in tests; update ctor test to not support Expli…
steveharter 8d36d3c
Add [ActiveIssue] for Mono; add some test comments
steveharter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -303,6 +303,74 @@ public void AssemblyWithDifferentTypes() | |||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
[Fact] | ||||||||||||
public unsafe void AssemblyWithInstanceBasedFunctionPointer() | ||||||||||||
{ | ||||||||||||
byte[] assemblyData = GenerateMethodInPersistedAssembly(); | ||||||||||||
using MemoryStream stream = new MemoryStream(assemblyData); | ||||||||||||
TestAssemblyLoadContext testAssemblyLoadContext = new(); | ||||||||||||
|
||||||||||||
// Verify the assembly is properly generated and the runtime supports the IL. | ||||||||||||
try | ||||||||||||
{ | ||||||||||||
Assembly assembly = testAssemblyLoadContext.LoadFromStream(stream); | ||||||||||||
Type generatedType = assembly.GetType("MyGeneratedType")!; | ||||||||||||
Assert.NotNull(generatedType); | ||||||||||||
MethodInfo generatedMethod = generatedType.GetMethod("GetGuid")!; | ||||||||||||
Assert.NotNull(generatedMethod); | ||||||||||||
Func<object, IntPtr, Guid> generatedMethodToCall = generatedMethod.CreateDelegate<Func<object, IntPtr, Guid>>(); | ||||||||||||
|
||||||||||||
// Call the property getter through the generated method. | ||||||||||||
IntPtr fn = typeof(MyClassWithGuidProperty).GetProperty(nameof(MyClassWithGuidProperty.MyGuid))!.GetGetMethod().MethodHandle.GetFunctionPointer(); | ||||||||||||
Guid guid = Guid.NewGuid(); | ||||||||||||
MyClassWithGuidProperty obj = new (guid); | ||||||||||||
Assert.Equal(guid, generatedMethodToCall(obj, fn)); | ||||||||||||
} | ||||||||||||
finally | ||||||||||||
{ | ||||||||||||
testAssemblyLoadContext.Unload(); | ||||||||||||
} | ||||||||||||
|
||||||||||||
static unsafe byte[] GenerateMethodInPersistedAssembly() | ||||||||||||
{ | ||||||||||||
AssemblyName assemblyName = new("MyAssembly"); | ||||||||||||
PersistedAssemblyBuilder ab = new(assemblyName, typeof(object).Assembly); | ||||||||||||
ModuleBuilder moduleBuilder = ab.DefineDynamicModule("MyModule"); | ||||||||||||
|
||||||||||||
// Define a type with a method that calls the instance-based function pointer through calli. | ||||||||||||
TypeBuilder typeBuilder = moduleBuilder.DefineType("MyGeneratedType", TypeAttributes.Public | TypeAttributes.Class); | ||||||||||||
MethodBuilder methodBuilder = typeBuilder.DefineMethod( | ||||||||||||
"GetGuid", | ||||||||||||
MethodAttributes.Public | MethodAttributes.Static, | ||||||||||||
typeof(Guid), | ||||||||||||
new Type[] { typeof(object), typeof(IntPtr) }); | ||||||||||||
|
||||||||||||
ILGenerator il = methodBuilder.GetILGenerator(); | ||||||||||||
il.Emit(OpCodes.Ldarg_0); // this | ||||||||||||
il.Emit(OpCodes.Ldarg_1); // fn | ||||||||||||
il.EmitCalli(OpCodes.Calli, CallingConventions.HasThis, typeof(Guid), null, null); | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This should be good-enough as a test for ExplicitThis |
||||||||||||
il.Emit(OpCodes.Ret); | ||||||||||||
|
||||||||||||
typeBuilder.CreateType(); | ||||||||||||
|
||||||||||||
MetadataBuilder metadataBuilder = ab.GenerateMetadata(out BlobBuilder ilStream, out BlobBuilder _); | ||||||||||||
ManagedPEBuilder peBuilder = new ManagedPEBuilder(PEHeaderBuilder.CreateLibraryHeader(), new MetadataRootBuilder(metadataBuilder), ilStream); | ||||||||||||
BlobBuilder blob = new BlobBuilder(); | ||||||||||||
peBuilder.Serialize(blob); | ||||||||||||
return blob.ToArray(); | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
private class MyClassWithGuidProperty | ||||||||||||
{ | ||||||||||||
public MyClassWithGuidProperty(Guid guid) | ||||||||||||
{ | ||||||||||||
MyGuid = guid; | ||||||||||||
} | ||||||||||||
|
||||||||||||
public Guid MyGuid { get; init; } | ||||||||||||
} | ||||||||||||
|
||||||||||||
void CheckCattr(IList<CustomAttributeData> attributes) | ||||||||||||
{ | ||||||||||||
CustomAttributeData cattr = attributes.First(a => a.AttributeType.Name == nameof(AttributeUsageAttribute)); | ||||||||||||
|
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any benefit to adding an
ExplicitThis
test? I'm still not entirely clear on what it is used for.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ExplicitThis
means that thethis
type is explicitly encoded in the signature.For example,
MyClassWithGuidProperty.MyGuid
property from the existing test would be called like this:il.EmitCalli(OpCodes.Calli, CallingConventions.HasThis | CallingConventions.ExplicitThis, typeof(Guid), [typeof(MyClassWithGuidProperty)], null);
with ExplicitThis.It makes sense on function pointers and calli signatures only. From ECMA spec: "The EXPLICITTHIS (0x40) bit can be set only in signatures for function pointers"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could add a test, but other than to verify the signature actually has the extra "this" parameter there isn't much to test since the IL stays the same: the caller still needs to push the "this" pointer before the
calli
, and the target instance method still needs to do aldarg.0
as the first statement.I did at least find one test that would verify the runtime doesn't choke on it:
runtime/src/tests/JIT/Directed/tailcall/more_tailcalls.cs
Line 434 in d34ef7e
If C# adds support for instance-based function pointers, I think it would want to use ExplicitThis since it would get the "this" parameter in the signature instead of some passing it some other way. We may also want to use it for intrinstic\UnsafeAccessor for #112994.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, the test would verify that the extra "this" is not dropped on the floor somewhere along the way in the System.Reflection.Emit or System.Reflection.Metadata.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep. I would like to see a test in this particular test suite even if only for completeness.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a test for both the AssemblyBuilder and for the DynamicMethod case, and it is failing on the ExplicitThis tests.
I did verify that the signature is being created correctly with
calli explicit instance
, but the "this" pointer in the test's Guid-based property getter is not valid.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How is it failing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just pushed a change to the jitter - it needs to know that the first arg is "this" for ExplicitThis.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, looks like a pretty old bug. Without this we insert the retbuffer in the wrong place for callis with explicitthis that use return buffers.