Skip to content

Resolve diagnostic info for delegates pointing to stubs of folded methods - #137982

Draft
MichalStrehovsky wants to merge 1 commit into
dotnet:mainfrom
MichalStrehovsky:folded-stub-diagnostics
Draft

MichalStrehovsky wants to merge 1 commit into
dotnet:mainfrom
MichalStrehovsky:folded-stub-diagnostics

Conversation

@MichalStrehovsky

Copy link
Copy Markdown
Member

Fixes #136811

When a method body is folded with the body of a different method, delegates pointing to stubs that forward to it (address-taken jump stubs, unboxing stubs, instantiating unboxing thunks) couldn't be mapped back to the right method.

  • Emit stack trace records keyed by such stubs. On WASM, stubs don't have a virtual IP so they're recorded by function pointer only (new IsStub command).
  • Look up function pointers in the stack trace tables without going through the code manager, which only knows about the managed code range.
  • Look up the stub before its target in DiagnosticMethodInfo.Create(Delegate) and in reflection's Delegate.Method lookup.
  • Add a StackTraceMetadata test variant that folds all identical method bodies.

…hods

When a method body is folded with the body of a different method, delegates
pointing to stubs that forward to it (address-taken jump stubs, unboxing stubs,
instantiating unboxing thunks) couldn't be mapped back to the right method.

* Emit stack trace records keyed by such stubs. On WASM, stubs don't have a
  virtual IP so they're recorded by function pointer only (new IsStub command).
* Look up function pointers in the stack trace tables without going through the
  code manager, which only knows about the managed code range.
* Look up the stub before its target in DiagnosticMethodInfo.Create(Delegate)
  and in reflection's Delegate.Method lookup.
* Add a StackTraceMetadata test variant that folds all identical method bodies.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 81bced03-ceeb-4535-bba7-0bd66971e80d
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 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: @agocke, @dotnet/ilc-contrib
See info in area-owners.md if you want to be subscribed.

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.

🔵 Needs a closer look

The coupled NativeAOT metadata, WASM function identities, and runtime decoding paths require platform-specific execution to validate folding and exact generic identity, but those tests were not run.

No open issues 🎉

What changed in this PR

This PR preserves method identity in NativeAOT delegate diagnostics and reflection when identical method bodies are folded together, addressing #136811.

Changes:

  • Emits diagnostic records keyed by forwarding stubs, with function-pointer-only records on WebAssembly.
  • Resolves stub identities before falling back to their target bodies, without relying on code-manager ownership for diagnostic lookup.
  • Expands delegate regression coverage and adds a test variant that enables folding across different methods.
File Description
src/​tests/​nativeaot/​SmokeTests/​StackTraceMetadata/​StackTraceMetadata_FoldAll.csproj Adds a smoke-test variant with all-body folding enabled.
src/​tests/​nativeaot/​SmokeTests/​StackTraceMetadata/​BodyFoldingTest.cs Adds generic and unboxing delegate identity checks, including diagnostic-only targets.
src/​coreclr/​tools/​Common/​Internal/​Runtime/​StackTraceData.cs Defines the IsStub metadata command flag.
src/​coreclr/​tools/​aot/​ILCompiler.Compiler/​Compiler/​MetadataManager.cs Generates stub-keyed records for folded targets.
src/​coreclr/​tools/​aot/​ILCompiler.Compiler/​Compiler/​DependencyAnalysis/​StackTraceMethodMappingNode.cs Serializes stub records and counts address-keyed entries separately.
src/​coreclr/​tools/​aot/​ILCompiler.Compiler/​Compiler/​DependencyAnalysis/​StackTraceLineNumbersNode.cs Excludes stub records from line-number mappings.
src/​coreclr/​nativeaot/​System.Private.StackTraceMetadata/​src/​Internal/​StackTraceMetadata/​StackTraceMetadata.cs Decodes stub records and searches module metadata directly by function pointer.
src/​coreclr/​nativeaot/​System.Private.Reflection.Execution/​src/​Internal/​Reflection/​Execution/​ExecutionEnvironmentImplementation.MappingTables.cs Prefers original stub pointers during reflection reverse lookup.
src/​coreclr/​nativeaot/​System.Private.CoreLib/​src/​System/​Delegate.NativeAot.cs Looks up diagnostic information for the stub before its target.

Review effort: Balanced

This branch has not been deployed

No deployments
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.

Missing stack-trace mappings for folded address-taken methods

2 participants