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 drops bounds checks when the limit local is redefined between the zero-trip guard and the loop preheader #133762
usingSystem;usingSystem.Runtime.CompilerServices;publicclassProgram{[MethodImpl(MethodImplOptions.NoInlining|MethodImplOptions.AggressiveOptimization)]staticintTest(int[]a,inti,intn){intsum=0;if(i<n){n=a.Length;do{sum+=a[i];i++;}while(i<n);}returnsum;}publicstaticintMain(){int[]a=newint[10];try{intr=Test(a,20,30);Console.WriteLine($"Expected: IndexOutOfRangeException / Actual: returned {r}");return1;}catch(IndexOutOfRangeException){Console.WriteLine("Expected: IndexOutOfRangeException / Actual: IndexOutOfRangeException");return100;}}}
Run: corerun repro.dll (Checked or Release JIT, .NET 11 main @ b44cd90)
Expected
a[20] on a 10-element array throws IndexOutOfRangeException; exit code 100.
Actual
No exception
Notes
No JIT knobs needed. DOTNET_JitCloneLoops=0 restores the exception. FlowGraphNaturalLoop::HasZeroTripTest (flowgraph.cpp:6629) accepts the i < n guard without checking that n is not redefined between the guard and the preheader, so NeedsZeroTripGuard stays false and no entry guard is cloned (loopcloning.cpp:1346).
Thought this was a cloning bug, but it looks like the regression was introduced by #119537, which allowed zero-trip detection to walk unique predecessors without invalidating an earlier guard when the iterator or limit is redefined along that path
Yeah, MatchLimit takes care to validate that the limit is invariant inside the loop, but similar care needs to be taken when we try to validate the zero trip test since there we are walking outside the loop.
@jakobbotsch would you consider backporting the fix from #133808 to release/11.0?
It currently only exists in main (12.0). The repro still fails on the shipped 11.0 RC2 SDK (11.0.100-rc.2.26467.112), while 8.0.425, 9.0.318 and 10.0.401 all behave correctly, so this is a regression introduced during the 11.0 cycle rather than a long-standing bug — 11.0 would ship with it.
Note: this comment was produced by an AI agent; the repro was re-run against all four released SDKs to confirm the results above.
Activity
dotnet-policy-service commentedon Sep 11, 2026
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.
AndyAyersMS commentedon Sep 11, 2026
Thought this was a cloning bug, but it looks like the regression was introduced by #119537, which allowed zero-trip detection to walk unique predecessors without invalidating an earlier guard when the iterator or limit is redefined along that path
jakobbotsch commentedon Sep 12, 2026
Yeah,
MatchLimittakes care to validate that the limit is invariant inside the loop, but similar care needs to be taken when we try to validate the zero trip test since there we are walking outside the loop.EgorBo commentedon Sep 18, 2026
Affected SDK versions:
EgorBo commentedon Sep 18, 2026
@jakobbotsch would you consider backporting the fix from #133808 to
release/11.0?It currently only exists in
main(12.0). The repro still fails on the shipped 11.0 RC2 SDK (11.0.100-rc.2.26467.112), while 8.0.425, 9.0.318 and 10.0.401 all behave correctly, so this is a regression introduced during the 11.0 cycle rather than a long-standing bug — 11.0 would ship with it.Note: this comment was produced by an AI agent; the repro was re-run against all four released SDKs to confirm the results above.