Skip to content

JIT: Guard loop cloning against limit offset underflow - #137782

Merged
AndyAyersMS merged 1 commit into
dotnet:mainfrom
AndyAyersMS:fix/137678-loop-cloning-limit-underflow
Nov 18, 2026
Merged

AndyAyersMS merged 1 commit into
dotnet:mainfrom
AndyAyersMS:fix/137678-loop-cloning-limit-underflow

Conversation

@AndyAyersMS

Copy link
Copy Markdown
Member

Problem

For an increasing loop with a large stride and an invariant limit plus a negative constant offset, the loop-cloning overflow guard checked the unadjusted limit while the clone's range checks used the wrapped sum. A negative limit could therefore wrap the sum positive, allowing the bounds-check-free clone to run and access outside the array.

Fix

Require the unadjusted invariant limit to be nonnegative when the offset is negative. Add a regression test that verifies the overflowing loop throws IndexOutOfRangeException rather than accessing out of bounds.

Validation

  • build.cmd clr+libs -lc Release -rc Release — passed, 0 warnings and 0 errors.
  • build.cmd clr.jit -c Checked — passed, 0 warnings and 0 errors.
  • src/tests/run.cmd x64 checked tree JIT/opt/JIT.opt — passed: 584 passed, 0 failed, 11 skipped, 1 active issue. The new regression test passed.
  • JIT formatting and git diff --check passed.

Risk

Low. The additional condition is emitted only for increasing loops with stride greater than one, an invariant local limit, a negative offset, and an overflow guard requirement. If the limit is negative, cloning is skipped and the original bounds-checked loop remains.

Resolves #137678

Note

This PR description was created with GitHub Copilot.

Loop cloning can treat an invariant limit plus a negative offset as a safe bound even when the addition wraps from a negative limit to a positive value. The fast clone then omits bounds checks and can access outside the array after the loop index overflows.

Require the unadjusted limit to be nonnegative for negative offsets, and add a regression test for the overflow case.

Fixes dotnet#137678

Co-authored-by: Copilot <[email protected]>
@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 17, 2026
@AndyAyersMS
AndyAyersMS requested a balanced review from Copilot November 17, 2026 23:05
@azure-pipelines

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

@AndyAyersMS
AndyAyersMS requested a review from EgorBo November 17, 2026 23:05
@AndyAyersMS

Copy link
Copy Markdown
Member Author

@EgorBo ptal
fyi @dotnet/jit-contrib

@AndyAyersMS AndyAyersMS changed the title Guard loop cloning against limit offset underflow JIT: Guard loop cloning against limit offset underflow Nov 17, 2026

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.

🟢 Approval recommended

There are no active review issues with the JIT loop-cloning underflow fix.

No open issues 🎉

What changed in this PR

This PR addresses an integer-underflow case in JIT loop cloning that could allow a bounds-check-free loop to access outside an array.

Changes:

  • Requires a nonnegative invariant limit when deriving overflow guards for increasing loops with strides greater than one and negative limit offsets.
  • Adds a regression test verifying that the overflowing loop throws IndexOutOfRangeException.
File Description
src/​tests/​JIT/​opt/​Cloning/​OffsetLimit.cs Adds a large-stride, negative-offset regression case and exception assertion.
src/​coreclr/​jit/​loopcloning.cpp Adds the nonnegative-limit condition to prevent unsafe loop cloning.

Review effort: Balanced

@AndyAyersMS

Copy link
Copy Markdown
Member Author

/ba-g osx coreclr build stopped responding mid build

@AndyAyersMS
AndyAyersMS merged commit 0d69d0d into dotnet:main Nov 18, 2026
128 of 131 checks passed
@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

3 participants