Skip to content

JIT: (bug) return buffer written through a widened ref to a promoted Vector4 field causes assert / AccessViolationException #135393

Closed

Description

@EgorBo

When a call's return buffer is a promoted 16-byte Vector4 field widened via Unsafe.As to a 32-byte struct, the JIT treats the call as defining only that field while the callee writes 32 bytes. Release runtimes crash with AccessViolationException; Checked asserts.

Minimal Repro

using System;
using System.Numerics;
using System.Runtime.CompilerServices;

public struct Pair
{
    public Vector4 A;
    public Vector4 B;
}

public class Program
{
    public static void Main()
    {
        Pair p = Test(1);
        Console.WriteLine(p.A + " " + p.B);
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    static Pair Make(float x) => new Pair { A = new Vector4(x), B = new Vector4(x + 1) };

    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    public static Pair Test(float x)
    {
        Pair p = default;
        Unsafe.As<Vector4, Pair>(ref p.A) = Make(x);
        return p;
    }
}

Expected

<1, 1, 1, 1> <2, 2, 2, 2>

Actual

Checked:

Assertion failed 'newLclValue.BothDefined()' in 'Program:Test(float):Pair' during 'Do value numbering' (IL size 33; hash 0xaea854bc; FullOpts)
    File: src\coreclr\jit\valuenum.cpp:6745

Release runtimes (and Checked with DOTNET_ContinueOnAssert=1):

Fatal error.
System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at Program.Test(Single)

Regression?

Yes, regression in .NET 9 (win-x64):

  • .NET 8.0.31: correct
  • .NET 9.0.20, 10.0.12, 11.0.0-rc.2: AccessViolationException
  • main (Checked): the assert above

Notes

  • The Unsafe.As is in bounds: p.A is at offset 0 of p, so the 32-byte write stays inside p. No repro without Unsafe is known.
  • The retbuf optimization in LocalAddressVisitor::EscapeAddress (lclmorph.cpp) only checks IsValidLclAddr for the start of the address, not that the 32-byte write fits in the promoted 16-byte field p.A (MorphStructFieldAddress is called with ValueSize(0)), so the call is treated as a definition of p.A only.
  • Correct with DOTNET_JitMinOpts=1.

Activity

  1. added this to the 12.0.0 milestone on Oct 8, 2026
  2. added
    area-CodeGen-coreclr
    on Oct 8, 2026
  3. dotnet-policy-service commented on Oct 8, 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 12, 2026
    4ab4fb6
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