Repository navigation
JIT: Guard loop cloning against limit offset underflow - #137782
Merged
AndyAyersMS merged 1 commit intoNov 18, 2026
Merged
Conversation
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]>
|
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. |
Member
Author
|
@EgorBo ptal |
Contributor
There was a problem hiding this comment.
🟢 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
EgorBo
approved these changes
Nov 18, 2026
Member
Author
|
/ba-g osx coreclr build stopped responding mid build |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
IndexOutOfRangeExceptionrather 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.git diff --checkpassed.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.