Skip to content

JIT: (bug) Bounds check incorrectly eliminated — range analysis ignores signed underflow of x + negativeConstant #133820

Closed

Description

@EgorBo

RangeCheck removes an array bounds check for an index computed as x + <negative constant> without accounting for signed underflow of the addition. The result is a silent out-of-bounds read instead of IndexOutOfRangeException.

Minimal Repro

using System;
using System.Runtime.CompilerServices;

public static class Program
{
    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    public static int Test(int x)
    {
        int[] arr = new int[8];
        for (int i = 0; i < arr.Length; i++)
        {
            arr[i] = i + 1;
        }

        if (x < int.MaxValue)
        {
            int y = x + (-2147483640);
            if (y >= 0)
            {
                return arr[y];
            }
        }
        return -1;
    }

    public static void Main()
    {
        Console.WriteLine(Test(int.MinValue));
    }
}

Expected

int.MinValue + (-2147483640) wraps to 8, and arr.Length == 8, so the indexer must throw:

Unhandled exception. System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at Program.Test(Int32 x)
   at Program.Main()

Actual

UB

No CORINFO_HELP_RNGCHKFAIL is emitted. DOTNET_JITMinOpts=1 throws correctly.

Notes

  • x64, main @ 6ed2ba318dcaa936da60f7d6bc9eb30cad54fc0c; also reproduces on .NET 10.0.12 Release, so not a regression (not bisected further).
  • Also happens with a heap array (store arr into a static to force escape), turning it into an out-of-bounds heap read.
  • Root cause: from x <= int.MaxValue - 1 the JIT derives y <= 6, and with the dominating y >= 0 concludes y in [0, 6]. RangeCheck::AddOverflows / GetLimitMax (src/coreclr/jit/rangecheck.cpp) only test the operands' upper limits, so underflow at the lower end — one operand a negative constant, the other's lower limit unconstrained at int.MinValue — is never detected.

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 incorrectly eliminated — range analysis ignores signed underflow of `x + negativeConstant`[/-] [+]JIT: (bug) Bounds check incorrectly eliminated — range analysis ignores signed underflow of `x + negativeConstant`[/+] 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 Oct 21, 2026
  7. added a commit that references this issue on Oct 28, 2026
    3aa2a50
  8. added a commit that references this issue on Oct 29, 2026
    e94a983
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