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 trusts a stale zero-trip guard after the IV is redefined #134007
HasZeroTripTest can treat a guard before the loop preheader as proving the loop entry condition even when the iteration variable is redefined afterward. Loop cloning then omits the zero-trip guard and removes a required bounds check.
Minimal Repro
usingSystem;usingSystem.Runtime.CompilerServices;publicstaticclassProgram{[MethodImpl(MethodImplOptions.NoInlining|MethodImplOptions.AggressiveOptimization)]publicstaticintTestZtt(int[]arr,intn,intstart,intoff){inti=start+1;intsum=i*2;if(i<n)// <- guard that HasZeroTripTest latches on to{i=n+off;// <- kills the guard above (IV redefined)do{sum+=arr[i];i++;}while(i<n);}returnsum;}publicstaticvoidMain(){int[]arr=newint[10];try{Console.WriteLine("result = "+TestZtt(arr,10,-1,100000000));}catch(IndexOutOfRangeException){Console.WriteLine("IndexOutOfRangeException (expected)");}}}
Expected
IndexOutOfRangeException (expected)
i is set to 10 + 100000000 before the first arr[i], so the access must throw.
Actual
UB
Notes
Root cause is FlowGraphNaturalLoop::HasZeroTripTest/IsZeroTripTest (flowgraph.cpp): the predecessor walk does not check whether the IV or limit is killed between the candidate guard and the preheader. FindConstInit has an analogous gtTreeHasLocalStore kill check, but HasZeroTripTest does not.
This is a main-only regression; released .NET 10.0.12 prints the expected IndexOutOfRangeException. Correctness/memory-safety issue in fully verifiable C#. A fix should reject stale guards when the IV or limit is redefined on the path to the preheader.
changed the title [-]JIT (bug): Loop cloning trusts a stale zero-trip guard after the IV is redefined[/-][+]JIT: (bug) Loop cloning trusts a stale zero-trip guard after the IV is redefined[/+]on Sep 16, 2026
Activity
dotnet-policy-service commentedon Sep 15, 2026
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.
AndyAyersMS commentedon Sep 15, 2026
This seems similar to #133762, though for the IV instead of the limit?
EgorBo commentedon Sep 15, 2026
Ah, indeed. I've validated locally that #133808 fixes this issue as well
[-]JIT (bug): Loop cloning trusts a stale zero-trip guard after the IV is redefined[/-][+]JIT: (bug) Loop cloning trusts a stale zero-trip guard after the IV is redefined[/+]