Skip to content

JIT: (bug) Vector128.WithElement range-checks a re-read of the index local (spurious AOORE / out-of-bounds stack write) #134893

Closed

Description

@EgorBo

The range check in WithElement re-reads the index local after the value argument modified it, while the store uses the original index. This produces a spurious ArgumentOutOfRangeException or an unchecked out-of-bounds stack write.

Minimal Repro

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

class Program
{
    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    static Vector128<int> Test(Vector128<int> v, int idx)
    {
        return v.WithElement(idx, Interlocked.Exchange(ref idx, 100));
    }

    static void Main()
    {
        Console.WriteLine(Test(Vector128<int>.Zero, 1));
    }
}

Expected

<0, 1, 0, 0>

Actual

Unhandled exception. System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.

The inverse case (idx = 6, Interlocked.Exchange(ref idx, 0)) passes the range check and writes past the 16-byte SIMD stack temp, causing an AccessViolation.

Regression?

Yes, regression in main (after 11.0-rc2). .NET 8.0, 9.0, 10.0 and 11.0-rc2 are correct.

Notes

Introduced by #133776. gtNewSimdWithElementNode uses fgMakeMultiUse(&op2), which clones the address-exposed index local instead of spilling it to a temp. As a result, the range check sees the value after op3 (the xchg) modified it.

The same pattern exists in gtNewSimdShuffleVariableNode (AVX2 non-VBMI path for variable Vector256<byte>.Shuffle, regression since .NET 10): op1 is cloned via fgMakeMultiUse and the Permute2x128 copy re-reads it after op2 ran. E.g. with S { [FieldOffset(0)] Vector256<byte> V; [FieldOffset(16)] long L; }, Vector256.Shuffle(s.V, Vector256.Create((byte)Interlocked.Exchange(ref s.L, -1))) returns <255 (x16), 16 (x16)> instead of all 16 under DOTNET_EnableAVX512=0.

Activity

  1. added this to the 12.0.0 milestone on Sep 29, 2026
  2. added
    area-CodeGen-coreclr
    on Sep 29, 2026
  3. dotnet-policy-service commented on Sep 29, 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. EgorBo commented on Sep 30, 2026

    @EgorBo
    MemberAuthor

    Introduced by #133776.

    cc @tannergooding

  5. self-assigned this
    on Nov 18, 2026
  6. added a commit that references this issue on Nov 19, 2026
    f4ee640
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