Skip to content

JIT: (bug) Loop cloning removes the bounds check when limit + negativeConst wraps to a small positive value: the stride-overflow guard tests only the unwrapped limit #137678

Closed

Description

@EgorBo

For for (int i = start; i < limit + C; i += stride) with a large stride, loop cloning emits the IV-overflow guard on the unwrapped limit but the range conditions on the wrapped limit + C. When limit + C wraps to a small positive value all conditions pass, the fast clone (no bounds check) runs, and i wraps negative on the 2nd iteration.

Minimal Repro

using System;
using System.Runtime.CompilerServices;

public class Program
{
    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    static int Test(int[] a, int start, int limit)
    {
        int sum = 0;
        for (int i = start; i < limit + (int.MinValue + 100); i += int.MaxValue)
        {
            sum += a[i];
        }
        return sum;
    }

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

Expected

IndexOutOfRangeException: the first iteration reads a[32], then i wraps to int.MinValue + 31, i < 103 (the wrapped limit) still holds, so a[int.MinValue + 31] 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. The fast clone has no bounds check and reads at a ~8 GB offset from the array.

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 (LimitOffset / the stride > 1 overflow guard; not bisected). The same area was already touched by #133834 (fix for #133823), which doesn't cover this case.

Notes

  • JitDump: Added stride>1 overflow guard: V02 <= 2147483549, then Conditions: (V02 LE 2147483549) && (V04 GE 0) && (V02-2147483548 GE 0) && (V02-2147483548 LE V00.Length).
  • In the HasInvariantLocalLimit branch of optDeriveLoopCloningConditions (loopcloning.cpp) the stride-overflow guard limit <= 0x7FFFFF9D is computed from LimitBase (the unwrapped limit), ignoring that LimitBase + LimitOffset can wrap.
  • The range conditions are evaluated on the wrapped sum (103 for the repro input), so they hold.
  • Possible fix: compute the guard on the wrapped limit + offset, or don't clone when the limit is local + const with a large stride.

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
    0d69d0d
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