Skip to content

JIT: (bug) Loop cloning removes the bounds check when the loop limit is a checked(local + const) that always overflows and the catch re-enters the loop #137677

Closed

Description

@EgorBo

Loop cloning peels local + const off the loop limit and derives its conditions as if the addition were an unchecked, non-throwing ADD. When the checked addition always overflows (the OverflowException is swallowed by a catch that loops back), the conditions are evaluated on the wrapped value and select the fast clone without a bounds check, which then reads far past the end of the array.

Minimal Repro

using System;
using System.Runtime.CompilerServices;

public class Program
{
    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    static int Test(int[] a, int limit)
    {
        int sum = 0;
        int i = 0;
        int retries = 0;
        while (true)
        {
            sum += a[i];
            try
            {
                i++;
                if (i >= checked(limit + (int.MinValue + 5)))
                {
                    break;
                }
            }
            catch (OverflowException)
            {
                if (++retries > 1000)
                {
                    return -1;
                }
            }
        }
        return sum;
    }

    public static void Main()
    {
        try
        {
            Console.WriteLine(Test(new int[8], int.MinValue + 3));
        }
        catch (IndexOutOfRangeException)
        {
            Console.WriteLine("IndexOutOfRangeException");
        }
    }
}

Expected

IndexOutOfRangeException: checked(limit + (int.MinValue + 5)) always overflows and is caught, i keeps increasing, and a[8] must throw. This is also what a Checked JIT prints with DOTNET_JitCloneLoops=0.

Actual

Checked and Release JIT (win-x64, main a5d7014cefc) print -1: the loop reads 1000 elements past the end of the 8-element array without a bounds check before the retry counter runs out.

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) prints -1

Likely related to #129309 (it introduced peeling a constant offset off the loop limit into LimitOffset; not bisected).

Notes

  • JitDump: Conditions: (0 LT V01-2147483643) && (V01-2147483643 GE 0) && (V01-2147483643 LE V00.Length). For the repro input the wrapped (unchecked) limit is 8, so all conditions hold and the fast clone is selected.
  • FlowGraphNaturalLoop::MatchLimit (flowgraph.cpp) peels GT_ADD/GT_SUB with a constant off the limit without checking gtOverflow(), and the cloning condition is rebuilt as a non-throwing ADD.
  • Possible fix: bail out of limit matching (or of cloning) when the limit contains an overflow-checking ADD/SUB.

Activity

  1. added this to the 12.0.0 milestone on Nov 16, 2026
  2. added
    area-CodeGen-coreclr
    on Nov 16, 2026
  3. dotnet-policy-service commented on Nov 16, 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. self-assigned this
    on Nov 17, 2026
  5. added a commit that references this issue on Nov 18, 2026
    d7e9c16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions