Skip to content

Fix Delegate.Method reflection cache pollution for private generic-base methods - #135286

Merged
jkotas merged 14 commits into
mainfrom
copilot/fix-delegate-method-reflection-cache
Oct 9, 2026
Merged

jkotas merged 14 commits into
mainfrom
copilot/fix-delegate-method-reflection-cache

Conversation

Copilot AI commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Reading Delegate.Method on an open delegate to a private generic-base method can assert in Checked CoreCLR or expose that method through the derived type’s reflection cache in Release.

  • Resolution: Resolve the method against its exact generic base instantiation, not the delegate’s derived first-argument type.
  • Regression coverage: Verify the method’s reflected type and that the derived type’s method list remains unchanged.

@azure-pipelines

azure-pipelines Bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @steveisok, @dotnet/area-system-reflection
See info in area-owners.md if you want to be subscribed.

Copilot AI and others added 2 commits October 6, 2026 16:56
Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix reflection cache pollution for derived types with private methods Fix Delegate.Method reflection cache pollution for private generic-base methods Oct 6, 2026
Copilot AI requested a review from jkotas October 6, 2026 17:08
Comment thread src/coreclr/System.Private.CoreLib/src/System/Delegate.CoreCLR.cs Outdated
Comment thread src/coreclr/System.Private.CoreLib/src/System/Delegate.CoreCLR.cs Outdated
Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
Comment thread src/coreclr/System.Private.CoreLib/src/System/Delegate.CoreCLR.cs Outdated
Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
@jkotas

jkotas commented Oct 7, 2026

Copy link
Copy Markdown
Member

@copilot Also, test is failing on NAOT

[FAIL] System.Tests.CreateDelegateTests.CreateDelegate10_Nullable_Method
Assert.Equal() Failure: Values differ
Expected: typeof(int?)
Actual:   typeof(System.Tests.CreateDelegateTests+NullableIntToString)
   at System.Tests.CreateDelegateTests.CreateDelegate10_Nullable_Method() in /_/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/DelegateTests.cs:line 1382
   at System.Reflection.DynamicInvokeInfo.Invoke(Object, IntPtr, Object[], BinderBundle, Boolean) in /_/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/DynamicInvokeInfo.cs:line 228

Copilot AI and others added 2 commits October 7, 2026 05:09
…expectation

Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>

Copilot AI commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

@copilot Also, test is failing on NAOT

...

Removed the newly added ReflectedType assertion in 114ca9a; it reflected CoreCLR behavior but is not valid for NativeAOT. The original nullable by-ref invocation test remains. CoreLib rebuilt, and the StackTrace and System.Runtime suites passed locally; I did not run NativeAOT locally.

Note

This reply was generated by GitHub Copilot.

@jkotas

jkotas commented Oct 7, 2026

Copy link
Copy Markdown
Member

Removed the newly added ReflectedType assertion in 114ca9a; it reflected CoreCLR behavior but is not valid for NativeAOT. The original nullable by-ref invocation test remains

@copilot Keep the assertion. Instead, ensure that both regular CoreCLR and NativeAOT have the same behavior.

Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>

Copilot AI commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

Removed the newly added ReflectedType assertion in 114ca9a; it reflected CoreCLR behavior but is not valid for NativeAOT. Th...

Restored the assertion and fixed NativeAOT in 7108211a. Nullable-method delegates use an object-array adapter there; it now retains the original MethodInfo, so Delegate.Method.ReflectedType is Nullable<int> rather than the delegate type. The test failed before and passed after the fix on NativeAOT. Full System.Runtime suites passed on both NativeAOT (78,873 tests, 0 failures) and Checked CoreCLR (79,997 tests, 0 failures); NativeAOT StackTrace tests also passed (135 tests, 0 failures).

@jkotas

jkotas commented Oct 7, 2026

Copy link
Copy Markdown
Member

@copilot Revert the last comit. Too compilated to handle this corner case.

Comment thread src/coreclr/System.Private.CoreLib/src/System/Delegate.CoreCLR.cs Outdated
Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>

Copilot AI commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

@copilot Revert the last comit. Too compilated to handle this corner case.

Reverted 7108211a in full in 2ce36d11, including the NativeAOT adapter changes and the nullable ReflectedType assertion it restored. The original CoreCLR fix remains. Checked baseline and NativeAOT rebuild succeeded; CreateDelegateTests passed on both runtimes (42 tests each).

Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
@jkotas
jkotas merged commit f26cb64 into main Oct 9, 2026
134 of 136 checks passed
@jkotas
jkotas deleted the copilot/fix-delegate-method-reflection-cache branch October 9, 2026 00:50
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

VM: (bug) Delegate.Method on an open delegate to an inherited private generic-base method pollutes the derived type's reflection cache

3 participants