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 removes the Span bounds check although the IV wraps: no IV-overflow guard for a small stride with an arr.Length + largeOffset limit #137679
Loop cloning emits no IV-overflow guard for a loop whose limit is arr.Length + <large positive constant> when the stride is small and the loop indexes a Span<T>. The fast clone has no bounds check, i += 4 wraps to int.MinValue, i < limit still holds, and the loop reads far outside the span.
Minimal Repro
Needs ~2 GB for the array (the span length is int.MaxValue - 1).
IndexOutOfRangeException: span[2147483644] is valid, then i wraps to int.MinValue, i < 2147483646 holds and span[i] must throw. This is also what a Checked JIT prints with DOTNET_JitCloneLoops=0.
Actual
Checked and Release JIT (win-x64, main a5d7014cefc): System.AccessViolationException in Program.Test. The fast loop has no bounds check and no overflow protection. JitDump of the Checked JIT shows the emitted conditions (all pass for this input, no overflow guard among them):
Conditions: (V05 GE 0) && (V01.Length+2147483645 GE 0) && (V01.Length+2147483645 LE V17[V05])
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 related to #129309 / #129349 (span + stride > 1 and LimitOffset cloning; not bisected). Same area as #133823 (fixed by #133834), but a different path: that fix doesn't cover this case.
Notes
In optDeriveLoopCloningConditions (loopcloning.cpp), the HasArrayLengthLimit overflow guard is only emitted when largeStride (stride >= 58), even if needsOverflowGuard is true because of hasSpans.
The comment's reasoning (arr.Length <= Array.MaxLength, so limit + s - 1 fits for s <= 57) ignores LimitOffset: here the limit can be up to int.MaxValue - 1, so limit + stride - 1 overflows.
limit <= span.Length doesn't help since span.Length can be int.MaxValue - 1 (a short[] reinterpreted as bytes).
Possible fix: also emit the guard when hasSpans || LimitOffset != 0, or compare maxLimitBase64 against Array.MaxLength regardless of stride.
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: guard IV overflow for span loops with array-length limits (dotne…