Skip to content

JIT: (bug) Loop cloning drops bounds checks when an arr.Length + K loop limit overflows to a negative value #133823

Closed

Description

@EgorBo

optDeriveLoopCloningConditions only emits the arr.Length + offset >= 0 guard when the peeled limit offset is negative, but the 32-bit add also wraps negative for large positive offsets, so the check-free fast clone runs out of bounds.

Minimal Repro

using System;
using System.Runtime.CompilerServices;

public class Program
{
    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    public static int Test(int[] a, int[] b)
    {
        int sum = 0;
        for (int i = a.Length - 1; i > b.Length + int.MaxValue; i--)
        {
            sum += a[i];
        }
        return sum;
    }

    public static void Main()
    {
        int[] a = new int[4];
        int[] b = new int[4];
        try
        {
            Console.WriteLine("result " + Test(a, b));
        }
        catch (IndexOutOfRangeException)
        {
            Console.WriteLine("IndexOutOfRangeException");
        }
    }
}

b.Length + int.MaxValue wraps to -2147483645, so the decreasing loop must walk i below 0 and throw.

Expected

IndexOutOfRangeException

DOTNET_JitCloneLoops=0 produces this correct result.

Actual

Fatal error.

Exit code 0xC0000005. Silent out-of-bounds read from safe C#.

Notes

  • MatchLimit / NaturalLoopIterInfo::LimitOffset (src/coreclr/jit/flowgraph.cpp) peel the constant out of b.Length + K; loopcloning.cpp guards arr.Length + offset >= 0 only under if (iterInfo->LimitOffset < 0).
  • The derived conditions are only about the initial IV ((V03 GE 0) && (V03 LT V00.Length)), both true here, so the check-free clone is entered and i walks down through negative values.
  • Fix direction: emit the arr.Length + offset >= 0 condition for positive offsets too, or reject offsets where the add can overflow.
  • .NET 11 regression (10.0.12 is correct, 11.0.0-rc.1 Release is bad); introduced by "JIT: extend loop cloning for span+stride>1 and ±const limits" (JIT: extend loop cloning for span+stride>1 and ±const limits #129309).

Activity

  1. added this to the 12.0.0 milestone on Sep 13, 2026
  2. added
    area-CodeGen-coreclr
    on Sep 13, 2026
  3. dotnet-policy-service commented on Sep 13, 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 Sep 13, 2026
  5. changed the title [-]JIT (bug): Loop cloning drops bounds checks when an arr.Length + K loop limit overflows to a negative value[/-] [+]JIT: (bug) Loop cloning drops bounds checks when an arr.Length + K loop limit overflows to a negative value[/+] on Sep 16, 2026
  6. added a commit that references this issue on Sep 16, 2026
    e58c6f8
  7. added a commit that references this issue on Sep 18, 2026
    b1c50f7
  8. EgorBo commented on Sep 18, 2026

    @EgorBo
    MemberAuthor

    Affected SDK versions:

    • 11.0.100-rc.2.26467.112
  9. EgorBo commented on Sep 18, 2026

    @EgorBo
    MemberAuthor

    @AndyAyersMS would you consider backporting the fix from #133834 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.

  10. locked and limited conversation to collaborators on Oct 19, 2026
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