Repository navigation
JIT: Account for underflow in RangeCheck AddOverflows and MultiplyOverflows - #136496
Conversation
|
Azure Pipelines: Successfully started running 5 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. |
|
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch |
There was a problem hiding this comment.
🟡 Changes recommended
Positive symbolic length offsets can wrap, leaving the new minimum-bound analysis unsound and allowing incorrect bounds-check elimination.
1 open issue
What changed in this PR
This PR addresses #133820 by extending CoreCLR JIT range analysis to consider signed underflow before eliminating bounds checks.
Changes:
- Adds lower-bound analysis for addition and checks all endpoint combinations for multiplication, including converted left shifts.
- Updates overflow helpers to accept complete operand ranges.
- Adds a regression test expecting
IndexOutOfRangeExceptionwhen addition underflows.
Validation was limited to source inspection; no builds or tests were run.
| File | Description |
|---|---|
| src/tests/JIT/Regression_ro_2/Runtime_133820.cs | Adds the signed-addition underflow regression test. |
| src/coreclr/jit/rangecheck.h | Declares minimum-bound lookup and range-based overflow helpers. |
| src/coreclr/jit/rangecheck.cpp | Implements lower-bound checks and updates overflow-analysis callers. |
Review effort: Balanced
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
|
After investigating some of the regressions it looks like the issue is that we were relying on the fact that int n = first.Length;
...
for (int i = 1; i <= n; i++)
{
... arr[i - 1]; // shouldn't need bounds check, but before we were only checking that upperLimit(i) - 1 didn't overflow and not worrying about lowerLimit(i)
}We now regress to keeping an inserted |
|
@adamperlin oh, you picked up a complex area. I think you're trying to fix different issues here. I suggest we only fix one at a time, so here let's just fix the Add (e.g. something like https://github.com/dotnet/runtime/compare/main...EgorBo:runtime-1:egorbo/rangecheck-underflow?expand=1) If you believe mul has an issue - let's handle it separately, but I am not sure I am seeing it. for |
AddOverflows only checked the sum of the upper limits, so e.g. "x + (-2147483640)" with x in [?, int.MaxValue - 1] was assumed to be in [?, 6], although an underflow wraps it to large values. Check the lower limits too, treating unknown ones as INT_MIN. DoesBinOpOverflow also passed the range of the whole index expression down to its operands, so DoesVarDefOverflow could skip checking the def of a local whose assertions fit that range, even though the binop used the def-tightened range of the local (e.g. "arr[l * 2]" with "l >= 0 && l <= 6" and an underflowing def of "l"). Pass the range the binop actually used for each operand instead. Fixes dotnet#133820 Co-authored-by: Copilot <[email protected]> Copilot-Session: 1b8140f-94c1-4743-aa73-b0d2a9e4c56c
@EgorBo Yes this got more complex than I had realized! I cherry-picked your fix for Add into this PR and the regression test seems to be passing. I don't think mul has a confirmed issue. The fix to mul was defensive to prevent something like this from coming up again, but I'm ok to do without it. |
) <!-- --> `RangeCheck::IsMonotonicallyIncreasing` could treat a recurrence like `x = x + y; y = x` with a negative seed as increasing, and remove a bounds check on an index derived from `x`, leading to an out-of-bounds access from safe code. For `lcl + lcl` both operands are walked with `rejectNegativeConst` set, but the search path was keyed only on the tree. When that walk came back around to the `x` phi, first visited without `rejectNegativeConst`, it was treated as closing the cycle, and the phi's negative initial value was never checked. Track walks with `rejectNegativeConst` set in a separate search path, so such phis are revisited and their inputs checked. The search depth limit still applies to the combined walk. The original repro in the issue is currently masked on main (since #136496 the `x + y` def is considered to possibly underflow), so the new `Regression_ro_2/Runtime_133818` test bounds the recurrence below to reach the monotonicity check. Validation (Windows x64 Checked): - Full `clr+libs` build and Core_Root on this branch, with the base (main) and fixed JITs swapped in. `Runtime_133818` crashes the merged runner with an AccessViolationException on the base JIT and passes with the fix. All of `Regression_ro_2` passes with the fix (508 passed, 24 skipped), with and without tiered compilation. - SuperPMI asmdiffs, all 12 windows-x64 collections (~2.89M contexts): no diffs. This was run before a comment-only change and a small accessor refactor. Resolves #133818 > [!NOTE] > This PR description was generated with the help of GitHub Copilot. Co-authored-by: Copilot <[email protected]> Copilot-Session: 827ade8d-8f51-41d6-a731-75387ac49646

Fixes #133820.
RangeCheck::AddOverflowsdid not account for underflow, so it would report a range addition like:[x, int.MaxValue - 1] + [-2147483640, -2147483640]as non-overflowing with upper bound(int.MaxValue - 1 + -2147483640)=6This case in particular lead to an incorrectly removed bounds check since
x + -2147483640can underflow and wrap around to be above the determined maximum range.I also decided to change RangeCheck::MultiplyOverflows to account for underflow as well to be as safe as possible for callers.