Skip to content

Report byref-like struct arguments to the GC in helper-based tail calls - #136019

Merged
jkotas merged 4 commits into
dotnet:mainfrom
EgorBo:fix-tailcall-byreflike-gcdesc
Oct 18, 2026
Merged

jkotas merged 4 commits into
dotnet:mainfrom
EgorBo:fix-tailcall-byreflike-gcdesc

Conversation

@EgorBo

@EgorBo EgorBo commented Oct 18, 2026 •

Copy link
Copy Markdown
Member

TailCallHelp::GenerateGCDescriptor skipped value types that have no GC series, but byref-like structs such as Span<T> have none and still contain an interior pointer. It was not reported to the GC while stored in the tail call arg buffer, so a GC between IL_STUB_StoreTailCallArgs and IL_STUB_CallTailCallTarget left the callee with a stale pointer into the old location of the array.

Treat byref-like structs like structs with GC pointers when building the descriptor. getClassGClayoutStatic already reports their byref fields as TYPE_GC_BYREF.

Adds a minimal IL regression test: a helper-based tail call with a Span<int> argument. It needs a GC between the tail call stubs to fail, so it is mainly caught by the GC stress legs.

Fixes #136010

TailCallHelp::GenerateGCDescriptor skipped value types without GC series, but byref-like structs such as Span<T> have no series and still contain interior pointers. These were not reported while stored in the tail call arg buffer, so a GC between the store-args and call-target stubs left the callee with a stale pointer.

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

Copilot-Session: 8a8cb9fd-0c26-426d-9657-9398252f7bce
@azure-pipelines

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: @agocke
See info in area-owners.md if you want to be subscribed.

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

Copilot-Session: 8a8cb9fd-0c26-426d-9657-9398252f7bce
@jkotas

jkotas commented Oct 18, 2026

Copy link
Copy Markdown
Member

Can we add a regression test in IL?

@jkotas
jkotas self-requested a review October 18, 2026 06:00
Co-authored-by: Copilot <[email protected]>

Copilot-Session: 8a8cb9fd-0c26-426d-9657-9398252f7bce
@EgorBo

EgorBo commented Oct 18, 2026

Copy link
Copy Markdown
Member Author

Can we add a regression test in IL?

added tail. call int32 Runtime_136010::Fill(valuetype [System.Runtime]System.Span1)` test

@EgorBo
EgorBo marked this pull request as ready for review October 18, 2026 14:10
Co-authored-by: Copilot <[email protected]>

Copilot-Session: 8a8cb9fd-0c26-426d-9657-9398252f7bce

@jkotas jkotas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I assume that the test fail

@EgorBo

EgorBo commented Oct 18, 2026

Copy link
Copy Markdown
Member Author

I assume that the test fail

Not reliably just like with any GC hole tests, my previous version of that test included a loop with GC.Collect, but such tests are slow (and gcstress makes them even slower), so I pushed a simple version. Before the fix it would fail probably a few times a week I guess.

@jkotas
jkotas merged commit 9b1b0e9 into dotnet:main Oct 18, 2026
107 of 109 checks passed
@jkotas

jkotas commented Oct 18, 2026

Copy link
Copy Markdown
Member

Backport candidate?

@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 19, 2026
JulieLeeMSFT pushed a commit that referenced this pull request Oct 19, 2026
…-based tail calls (#136082)

Fixes Issue #136010 <!-- Issue Number -->

main PR #136019 <!-- Link to PR if any that fixed this in the main
branch. -->

# Description

<!-- Give a brief summary of the issue and how the pull request is
fixing it. -->

Backport of #136019 to release/11.0. Include byref-like structs when
generating the GC descriptor for the helper-based tail-call argument
buffer. The IL regression test is placed under the release branch's
existing merged runner.

# Customer Impact

<!-- What is the impact to customers of not taking this fix? -->

- [x] Customer reported
- [ ] Found internally

Safe managed code passing a `Span<T>` through a helper-based tail call
can suffer memory corruption, wrong results, hangs, or fatal crashes if
a GC moves the underlying array while the arguments are buffered.
Reported with F# on Windows x64.

# Regression

<!-- Is this fixing a problem that was introduced in the most recent
release, ie., fixing a regression? -->

- [ ] Yes
- [x] No

The issue also reproduces on .NET 8, 9, and 10.

# Testing

<!-- What kind of testing has been done with the fix. -->

Test is added.

# Risk

<!-- Please assess the risk of taking this fix. Provide details backing
up your assessment. -->

Low. Uses existing GC-layout handling for byref-like structs.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 293a4931-e666-4c2c-bf85-571092949275
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 19, 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.

Helper-based tail call with a Span<double> argument corrupts memory (Windows x64, Debug/MinOpts)

2 participants