Skip to content

Honor [UnscopedRef] on properties in canValueClassInstancePointerEscape - #137849

Merged
EgorBo merged 1 commit into
dotnet:mainfrom
EgorBo:fix-137839-unscopedref-property
Nov 18, 2026
Merged

EgorBo merged 1 commit into
dotnet:mainfrom
EgorBo:fix-137839-unscopedref-property

Conversation

@EgorBo

@EgorBo EgorBo commented Nov 18, 2026

Copy link
Copy Markdown
Member

C# emits [UnscopedRef] for a property on the property itself, not on its get_ accessor. canValueClassInstancePointerEscape (VM and crossgen2/ILC) only looked at the method's own attributes, so for a devirtualized call to an [UnscopedRef] ref-returning property getter on a boxed struct, the JIT replaced the box with a stack copy and the returned ref pointed into a dead frame.

Both implementations now also check the owning property when the method is a property accessor. The ECMA-335 augment (III.1.7.7) is updated to allow the annotation on the property for accessors.

Fixes #137839

C# emits `[UnscopedRef]` for a property on the property itself, not on its `get_` accessor. `canValueClassInstancePointerEscape` (VM and crossgen2/ILC) only looked at the method's own attributes, so for a devirtualized call to an `[UnscopedRef]` ref-returning property getter on a boxed struct, the JIT replaced the box with a stack copy and the returned ref pointed into a dead frame.

Both implementations now also check the owning property when the method is a property accessor. The ECMA-335 augment (III.1.7.7) is updated to allow the annotation on the property for accessors.

Fixes dotnet#137839

Co-authored-by: Copilot <[email protected]>
Copilot-Session: a7d5e4f0-950c-4eb5-85e7-c5ba6a7533c0
@github-actions github-actions Bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Nov 18, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 4 pipeline(s).
11 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: @JulieLeeMSFT, @jakobbotsch
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.

🟡 Changes recommended

The regression’s AggressiveOptimization flag causes crossgen2 to skip the method, leaving the ReadyToRun fix untested without an invoked NoInlining-only variant.

1 open issue
What changed in this PR

This PR fixes escape analysis for boxed value types whose properties carry [UnscopedRef], preventing returned references from pointing into expired stack storage.

Changes:

  • Check property-level attributes in the VM and shared crossgen2/NativeAOT escape analysis.
  • Add a regression test for a ref-returning property on a boxed struct.
  • Clarify property-accessor annotations in the ECMA-335 augment.
File Description
src/​tests/​JIT/​Regression_ro_2/​Runtime_137839.cs Adds the boxed-struct property regression test.
src/​coreclr/​vm/​jitinterface.cpp Checks the owning property's [UnscopedRef] annotation.
src/​coreclr/​tools/​Common/​JitInterface/​CorInfoImpl.cs Adds equivalent property checks for ahead-of-time compilation.
docs/​design/​specs/​Ecma-335-Augments.md Allows property-level annotations for accessors.

Review effort: Balanced

@EgorBo

EgorBo commented Nov 18, 2026

Copy link
Copy Markdown
Member Author

PTAL @jkotas @jakobbotsch since it's related to #130863

@EgorBo
EgorBo requested a review from jkotas November 18, 2026 17:40
@EgorBo

EgorBo commented Nov 18, 2026

Copy link
Copy Markdown
Member Author

/ba-g filed #137875

@EgorBo
EgorBo merged commit 1da9846 into dotnet:main Nov 18, 2026
103 of 105 checks passed
@EgorBo
EgorBo deleted the fix-137839-unscopedref-property branch November 18, 2026 23:43
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JIT: (bug) box turned into a stack copy despite [UnscopedRef] on the property, returning a dangling ref

4 participants