Skip to content

JIT: (bug) Loop cloning trusts a stale zero-trip guard after the IV is redefined #134007

Closed as not planned

Description

@EgorBo

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

using System;
using System.Runtime.CompilerServices;

public static class Program
{
    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    public static int TestZtt(int[] arr, int n, int start, int off)
    {
        int i = start + 1;
        int sum = 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);
        }
        return sum;
    }

    public static void Main()
    {
        int[] arr = new int[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.

Activity

  1. added this to the 12.0.0 milestone on Sep 15, 2026
  2. added
    area-CodeGen-coreclr
    on Sep 15, 2026
  3. dotnet-policy-service commented on Sep 15, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
    See info in area-owners.md if you want to be subscribed.

  4. AndyAyersMS commented on Sep 15, 2026

    @AndyAyersMS
    Member

    This seems similar to #133762, though for the IV instead of the limit?

  5. EgorBo commented on Sep 15, 2026

    @EgorBo
    MemberAuthor

    This seems similar to #133762, though for the IV instead of the limit?

    Ah, indeed. I've validated locally that #133808 fixes this issue as well

  6. 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
  7. locked and limited conversation to collaborators on Oct 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CodeGen-coreclr

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions