Skip to content

JIT: (bug) OptimizeBools merges x < 0 || v.GetElement(x) < 0 and speculates a GetElement whose bounds check was removed #135815

Closed

Description

@EgorBo

Optimize bools folds x < 0 || v.GetElement(x) < 0 into (x | v.GetElement(x)) < 0 after assertion prop removed the bounds check of GetElement(x). The variable-index GetElement is then evaluated unconditionally, also for x < 0, reading out-of-bounds stack memory and crashing with AccessViolationException in valid safe C#.

Minimal Repro

using System;
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;

class Program
{
    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    static int Test(int a, int x)
    {
        Vector128<int> v = Vector128.Create(a, a + 1, a + 2, a + 3);
        int s = v.GetElement(1) + v.GetElement(2) + v.GetElement(3);
        if (x >= 4)
            return s;
        if (x < 0 || v.GetElement(x) < 0)
            return 2;
        return 3;
    }

    static void Main()
    {
        Console.WriteLine(Test(5, 1));
        Console.WriteLine(Test(5, 7));
        Console.WriteLine(Test(5, -1));
    }
}

Expected

3
21
2

Actual

3
21
Fatal error.

The disassembly spills v to the stack and does mov eax, dword ptr [rsp+4*rax] before the x < 0 check (with x = -1 this reads far outside the frame).

Regression?

Not a regression: 8.0.x, 9.0.x, 10.0.12, 11.0.0-rc.1 and current main all throw; 7.0.20 prints 3 21 2.

Notes

  • DOTNET_JITMinOpts=1 prints 3 21 2. Vector256<int> with an x >= 8 guard also fails. The extra uses of v only lower the GetElement cost to the merge threshold (GetCostEx() <= 12).
  • Assertion prop removes the BOUNDS_CHECK using 0 <= x < 4 from the dominating branches, so the HWINTRINSIC loses GTF_EXCEPT and (unlike IND/array elements) can't carry GTF_ORDER_SIDEEFF (OperSupportsOrderingSideEffect).
  • optOptimizeBoolsChkTypeCostCond (optimizebools.cpp) only checks GTF_GLOB_EFFECT and cost for m_c2, so it treats the second condition as safe to speculate. It could reject such nodes or check the VN exception set (still has IndexOutOfRangeExc), as loop hoisting does. Related: JIT: (bug) x >= 0 && x < (a[x] & int.MaxValue) drops the x >= 0 guard #134202 (same pattern for FoldNeverNegativeRangeTest, fixed via GTF_ORDER_SIDEEFF).

Activity

  1. added this to the 12.0.0 milestone on Oct 14, 2026
  2. added
    area-CodeGen-coreclr
    on Oct 14, 2026
  3. dotnet-policy-service commented on Oct 14, 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 11, 2026
  5. added a commit that references this issue on Nov 19, 2026
    c355a53
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