Skip to content

JIT: (bug) multi-reg struct arg load re-reads the heap address per register (torn struct values) #134474

Description

@EgorBo

When a struct arg passed in multiple registers is loaded, the whole address tree is cloned for each register. So o.I is read twice, which violates the memory model ("reads cannot be re-done") and produces torn values.

Minimal Repro

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

public struct Pair { public long A, B; }
public class Inner { public Pair P; }
public class Outer { public Inner I; }

public class Program
{
    static volatile bool s_done;

    public static void Main()
    {
        Inner x = new() { P = new Pair { A = 1, B = 1 } };
        Inner y = new() { P = new Pair { A = 2, B = 2 } };
        Outer o = new() { I = x };
        new Thread(() => { while (!s_done) { o.I = y; o.I = x; } }).Start();
        for (int i = 0; i < 100_000_000; i++)
        {
            Test(o);
        }
        s_done = true;
        Console.WriteLine("Done");
    }

    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
    static void Test(Outer o) => Use(o.I.P);

    [MethodImpl(MethodImplOptions.NoInlining)]
    static void Use(Pair p)
    {
        if (p.A != p.B)
        {
            Console.WriteLine($"Torn: {p.A}, {p.B}");
            Environment.Exit(1);
        }
    }
}

Expected

Done

Actual

On linux-x64:

Torn: 1, 2
       mov      rax, gword ptr [rbp-0x08]
       mov      rax, gword ptr [rax+0x08]      ; o.I  (1st read)
       mov      rdi, qword ptr [rax+0x08]      ; .P.A
       mov      rax, gword ptr [rbp-0x08]
       mov      rax, gword ptr [rax+0x08]      ; o.I  (2nd read)
       mov      rsi, qword ptr [rax+0x10]      ; .P.B
       call     [Program:Use(Pair)]

Regression?

Not a regression. Reproduces on .NET 8, 9, 10, 11 preview 3 and main (linux-x64). win-x64 isn't affected because it passes the struct by reference.

Notes

The bug shows up in Tier0/MinOpts code. NoOptimization is used only to make that deterministic. In optimized code, CSE happens to merge the two reads. It affects SysV x64 and arm64 (and probably other multi-reg-arg targets). fgTryMorphStructArg/createSlotAccess clones indirAddr and only checks GTF_PERSISTENT_SIDE_EFFECTS.

Activity

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