Skip to content

[release/10.0] JIT: fix bug in loop cloning with down-counting loops#126885

Merged
JulieLeeMSFT merged 1 commit intorelease/10.0from
backport/pr-126770-to-release/10.0
Apr 15, 2026
Merged

[release/10.0] JIT: fix bug in loop cloning with down-counting loops#126885
JulieLeeMSFT merged 1 commit intorelease/10.0from
backport/pr-126770-to-release/10.0

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

@github-actions github-actions bot commented Apr 14, 2026

Backport of #126770 to release/10.0

/cc @AndyAyersMS

Customer Impact

  • Customer reported
  • Found internally

JIT optimizations can cause certain down-counting loops to bypass a bounds check. Code that normally would throw index out of bounds exceptions on an array store might instead corrupt the heap.

In particular the loop must have unknown upper bound and have a shape like:

for (int i = N; i > 0; i--)
{
    a[i] = ...;
}

and then be invoked in a context where N is exactly a.Length. The loop exiting predicate must be > and not >=.

In such cases the code will write to a[N] which is beyond the extent of a.

Customer impact is likely low. Correct behavior here is to throw an exception.

Regression

  • Yes
  • No

Introduced in .NET 7 with #67930.

Testing

Verified fix on repro case. SPMI had 132 method contexts with diffs from the fix change. Inspected a few and most either had redundant guards beforehand or else were only reading from the arrays.

Risk

Low, changes the code that decides at runtime if execution can use a "cloned" loop that omits bounds checks; now we are correctly cautious about running the fully checked loop.

We were not creating proper cloning conditions, so the fast path
might execute in cases where it shouldn't.

We need to always verify for down counting that the initial value
is strictly less than the array length(s).
@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 Apr 14, 2026
@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.

@AndyAyersMS
Copy link
Copy Markdown
Member

@EgorBo PTAL

@JulieLeeMSFT JulieLeeMSFT added the Servicing-approved Approved for servicing release label Apr 15, 2026
@JulieLeeMSFT JulieLeeMSFT added this to the 10.0.x milestone Apr 15, 2026
@JulieLeeMSFT
Copy link
Copy Markdown
Member

@AndyAyersMS, please check the build failure.

@AndyAyersMS
Copy link
Copy Markdown
Member

AndyAyersMS commented Apr 15, 2026

@AndyAyersMS, please check the build failure.

It is a timeout.

@AndyAyersMS
Copy link
Copy Markdown
Member

/ba-g build timed out

@rbhanda rbhanda modified the milestones: 10.0.x, 10.0.7 Apr 15, 2026
@JulieLeeMSFT JulieLeeMSFT merged commit a12fb0c into release/10.0 Apr 15, 2026
108 of 112 checks passed
@JulieLeeMSFT JulieLeeMSFT deleted the backport/pr-126770-to-release/10.0 branch April 15, 2026 22:03
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 Servicing-approved Approved for servicing release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants