You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
JIT: (bug) Loop cloning merges the unsigned zero-trip guard 0 <u n with the signed n >= 0 guard and drops the latter, removing bounds checks for n >= 0x80000000 #137676
For a do { a[i] ... } while (i < n) loop with uint i, n, loop cloning combines the unsigned zero-trip guard 0 <u n with the signed n >= 0 guard and keeps only the former. For n >= 0x80000000 the remaining signed guard n <= a.Length passes, the bounds-check-free fast loop is entered and runs off the end of the array.
IndexOutOfRangeException: a[4] is out of range on the 5th iteration. This is also what a Checked JIT prints with DOTNET_JitCloneLoops=0.
Actual
Checked and Release JIT (win-x64, main a5d7014cefc): System.AccessViolationException. The fast loop has no bounds checks. JitDump of the Checked JIT:
Conditions: (0 LTU V01) && (V01 GE 0) && (V01 LE V00.Length)
...
After optimizing cloning conditions
(0 LTU V01) && (V01 LE V00.Length)
For n = 0x80000000 (negative as an int) the signed check n <= a.Length passes.
Regression?
Yes. Same repro, default settings:
Runtime
Result
.NET 8 (8.0.34)
OK (IndexOutOfRangeException)
.NET 9 (9.0.23)
OK
.NET 10 (10.0.15)
OK
.NET 11 (11.0.0) / main (a5d7014cefc)
AccessViolationException
Likely introduced by #128532 (it added the NeedsZeroTripGuard cloning condition; not bisected).
Notes
LC_Condition::Combines (loopcloning.cpp) treats a condition and one with GenTree::ReverseRelop(oper) and swapped operands as redundant (0 < n vs n >= 0), and LoopCloneContext::OptimizeConditions keeps the first one and removes the other. Combines never looks at compareUnsigned.
Keeping 0 <u n and dropping n >= 0 is wrong: 0 <u n only means n != 0; it doesn't imply n >= 0 as a signed int, so the non-negativity guard that protects the signed n <= a.Length check is lost.
Possible fix: don't combine conditions whose compareUnsigned differs (and, more generally, only drop a condition that the kept one implies).
Activity
dotnet-policy-service commentedon Nov 16, 2026
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.
JIT: fix loop cloning condition merging (dotnet#137776)