Skip to content

JIT: (bug) Bounds check wrongly removed — decreasing recurrence treated as monotonically increasing #133818

Description

@EgorBo

RangeCheck::IsMonotonicallyIncreasing concludes that an index derived from a decreasing integer recurrence is monotonically increasing, so the array bounds check is removed. The negative index is then zero-extended into the element address, giving an out-of-bounds read / AccessViolationException instead of IndexOutOfRangeException.

Minimal Repro

using System;
using System.Runtime.CompilerServices;

class Program
{
    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    static int Test(int[] arr, int n)
    {
        int sum = 0, x = -5, y = 0;
        for (int c = 0; c < n; c++)
        {
            if (x >= 50 || y >= 50) break;
            int idx = x + 100;
            if (idx >= arr.Length) break;
            sum += arr[idx];
            int t = x + y;
            y = x;
            x = t;
        }
        return sum;
    }

    static void Main()
    {
        int[] arr = new int[100];
        try
        {
            Console.WriteLine("Test: " + Test(arr, 8));
        }
        catch (IndexOutOfRangeException)
        {
            Console.WriteLine("Test: IndexOutOfRangeException");
        }
    }
}

x walks -5, -5, -10, -15, -25, -40, -65, -105, so on the 8th iteration idx == -5 passes the user's upper-bound guard.

Expected

Test: IndexOutOfRangeException

Actual

UB

No CORINFO_HELP_RNGCHKFAIL is emitted; the negative index is zero-extended (mov r9d, r9d) into the load address. DOTNET_JITMinOpts=1 is correct.

Notes

  • x64, main @ 6ed2ba318dcaa936da60f7d6bc9eb30cad54fc0c. Also reproduces on .NET 10.0.12 Release, so it is not a regression in main; memory-safety hole reachable from safe C#.
  • Root cause: the search path in RangeCheck::IsMonotonicallyIncreasing (src/coreclr/jit/rangecheck.cpp) is keyed only on expr, not on (expr, rejectNegativeConst). x = PHI(-5, x + y) re-enters with rejectNegativeConst == true for the ADD(x, y) operand, hits alreadyPresent and returns true without ever re-checking the -5 seed.
  • JitDump shows the resulting bogus lower bound: Computed Range [000031] => <90, $145 + -1>, then [RangeCheck::OptimizeRangeCheck] Between bounds.

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. changed the title [-]JIT (bug): Bounds check wrongly removed — decreasing recurrence treated as monotonically increasing[/-] [+]JIT: (bug) Bounds check wrongly removed — decreasing recurrence treated as monotonically increasing[/+] on Sep 16, 2026
  5. EgorBo commented on Sep 18, 2026

    @EgorBo
    MemberAuthor

    Affected SDK versions:

    • 8.0.425
    • 9.0.318
    • 10.0.401
    • 11.0.100-rc.2.26467.112
  6. self-assigned this
    on Nov 23, 2026
  7. added a commit that references this issue on Nov 24, 2026
    ade3027
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