Skip to content

Fix NativeAOT reflection invoke copyback - #130065

Merged
MichalStrehovsky merged 3 commits into
dotnet:mainfrom
MichalStrehovsky:nativeaot-reflection-copyback
Jul 1, 2026
Merged

MichalStrehovsky merged 3 commits into
dotnet:mainfrom
MichalStrehovsky:nativeaot-reflection-copyback

Conversation

@MichalStrehovsky

Copy link
Copy Markdown
Member

Fixes #67531.

Better to review with whitespace diffs off. We were not copying back results of binder conversions.

This also aligns reflection invoke argument copy-back with CoreCLR for byref and Type.Missing.

I was scratching my head over why we do the copyback in a finally (CoreCLR demonstratably doesn't and neither does .NET Framework), I guess CI will tell me if I missed something. So this aligns the copyback to also not be under finally.

Align NativeAOT reflection invoke argument copy-back with CoreCLR for byref, Type.Missing, and custom binder conversions.

Co-authored-by: Copilot <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR updates NativeAOT reflection invocation to correctly copy back argument values when a custom binder performs conversions, and re-enables previously skipped reflection tests for NativeAOT.

Changes:

  • Track binder-driven conversions and copy converted argument values back into the caller-provided argument array.
  • Change NativeAOT DynamicInvokeInfo copy-back logic to be per-argument (via shouldCopyBack) instead of a single _needsCopyBack flag.
  • Remove ActiveIssue skips so the affected System.Reflection tests run on NativeAOT again.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/libraries/System.Runtime/tests/System.Reflection.Tests/MethodInfoTests.cs Re-enables a binder conversion Invoke test on NativeAOT.
src/libraries/System.Runtime/tests/System.Reflection.Tests/ConstructorInfoTests.cs Re-enables binder conversion Invoke tests on NativeAOT.
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/MethodInfos/CustomMethodInvoker.cs Adds binder conversion tracking and copies back converted args.
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/DynamicInvokeInfo.cs Adds per-argument copy-back tracking (byref / Missing / binder conversions) and adjusts copy-back timing.
src/coreclr/nativeaot/System.Private.CoreLib/src/System/InvokeUtils.cs Adds out bool copyBack plumbing for binder-based conversions and factors assignability checks.
src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/Augments/RuntimeAugments.cs Exposes the new CheckArgument(..., out bool copyBack) overload for invoker use.

@MichalStrehovsky

Copy link
Copy Markdown
Member Author

Huh, so the TestLinqExpressions test I added in dotnet/corert#8072 is failing...

But it also fails on CoreCLR.

It does not fail on .NET Framework.

Delete the test and file a bug? Or do we care at all?

@jkotas

jkotas commented Jul 1, 2026 •

Copy link
Copy Markdown
Member

.NET Framework does not the copy back under finally in reflection invoke https://github.com/microsoft/referencesource/blob/ec9fa9ae770d522a5b5f0607898044b7478574a3/mscorlib/system/reflection/methodinfo.cs#L761-L765

Is this actually a difference between System.Linq.Expression interpreter vs. compiler? I do not think we care about that. System.Linq.Expression is effectively archived.

@MichalStrehovsky

Copy link
Copy Markdown
Member Author

Is this actually a difference between System.Linq.Expression interpreter vs. compiler?

Ah, yes it is. When I was testing it I had PublishAot in the project which switches expressions to interpreter mode on CoreCLR too. So deleting the test is the right course of action.

@MichalStrehovsky

Copy link
Copy Markdown
Member Author

/azp run runtime-nativeaot-outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@MichalStrehovsky
MichalStrehovsky merged commit 7c4e032 into dotnet:main Jul 1, 2026
159 of 164 checks passed
@MichalStrehovsky
MichalStrehovsky deleted the nativeaot-reflection-copyback branch July 1, 2026 20:55
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview7 milestone Jul 2, 2026
eiriktsarpalis pushed a commit that referenced this pull request Jul 15, 2026
Fixes #67531.

Better to review with whitespace diffs off. We were not copying back
results of binder conversions.

This also aligns reflection invoke argument copy-back with CoreCLR for
byref and Type.Missing.

I was scratching my head over why we do the copyback in a `finally`
(CoreCLR demonstratably doesn't and neither does .NET Framework), I
guess CI will tell me if I missed something. So this aligns the copyback
to also not be under `finally`.

---------

Co-authored-by: Copilot <[email protected]>
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 1, 2026
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.

Binder should update the input array

3 participants