Skip to content

JIT: (bug) bounds check wrongly removed under a data race (liberal VN reused as conservative VN for LCL_FLD of struct phi) #134489

Closed

Description

@EgorBo

For a LCL_FLD read of a struct local that has a phi SSA def, the liberal VN can end up as the conservative VN. Two separate heap reads then share a conservative VN, and range check elimination removes a bounds check. Under a data race this gives an AV in safe C# code.

Minimal Repro

using System;
using System.Runtime.CompilerServices;
using System.Threading;

public struct S { public int A, B, C, D, E; }
public class Holder { public S F; }

public class Program
{
    [MethodImpl(MethodImplOptions.NoInlining)]
    static string Use(S s) => "";

    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    static int Test(Holder h, int[] arr, bool c)
    {
        S s = h.F;
        if (c) s = h.F;
        if ((uint)s.A < (uint)arr.Length)
        {
            S t = h.F;
            if (c) t = h.F;
            // arr[t.A] has its bounds check removed: t.A is a separate read of h.F.A
            throw new Exception(Use(s) + Use(t) + arr[t.A] + s.B);
        }
        return -1;
    }

    public static void Main()
    {
        var h = new Holder();
        var arr = new int[10];
        new Thread(() => { while (true) { Volatile.Write(ref h.F.A, 0); Volatile.Write(ref h.F.A, 100_000_000); } }) { IsBackground = true }.Start();
        for (int i = 0; i < 1_000_000; i++)
        {
            try { Test(h, arr, true); }
            catch (Exception) { }
        }
        Console.WriteLine("Done");
    }
}

Expected

Done

Actual

UB

Regression?

Yes, a regression from .NET 8. .NET 8.0.31 works (the bounds check is still there). .NET 9.0.20, 10.0.12, 11.0.0-rc.2 and main fail.

Notes

VNForMapSelectWork caches results in MapSelectWorkCache with the key (VNF_MapSelect, map, index) only, so vnk is not part of the key.
For a struct phi, the liberal and conservative sides share the PhiDef map VN. The conservative select therefore hits the cached liberal result, even though the phi args' conservative VNs are different.
Possible fix: add vnk to the cache key, or use separate caches. The Use(...) calls are only there to block physical promotion.

Activity

  1. added this to the 12.0.0 milestone on Sep 23, 2026
  2. added
    area-CodeGen-coreclr
    on Sep 23, 2026
  3. dotnet-policy-service commented on Sep 23, 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 18, 2026
  5. added a commit that references this issue on Nov 20, 2026
    6c4e159
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