Skip to content

JIT: (bug) Struct copy through the GC bulk-copy helper faults on the destination before the source expression is evaluated #133587

Closed

Description

@EgorBo

Repro

using System;
using System.Runtime.CompilerServices;

struct Big { public object A, B, C, D, E; }

public class Program
{
    static Big s_src;
    static string s_log = "";

    [MethodImpl(MethodImplOptions.NoInlining)]
    static ref Big GetSrc() { s_log += "GetSrc;"; return ref s_src; }

    [MethodImpl(MethodImplOptions.NoInlining)]
    static int Index() { s_log += "Index;"; return 0; }

    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    static void Test(ref Big dst) { Unsafe.Add(ref dst, Index()) = GetSrc(); }

    public static int Main()
    {
        try { Test(ref Unsafe.NullRef<Big>()); }
        catch (Exception e) { Console.WriteLine(e.GetType().Name); }
        Console.WriteLine("log=[" + s_log + "]");
        return 100;
    }
}

Expected

NullReferenceException
log=[Index;GetSrc;]

Actual

NullReferenceException
log=[Index;]

Both sides of the assignment must be evaluated before the copy can fault, but the destination null
check runs as soon as the destination address is computed, so the call on the right-hand side is
skipped entirely. Reducing Big to three object fields (which no longer uses the bulk-copy helper)
prints the expected log=[Index;GetSrc;].

Platform

Windows x64, .NET 11 (dotnet/runtime main @ a0b86b1, Checked build), DOTNET_TieredCompilation=0.
No special CPU features. Also reproduces identically on the Release build.

Activity

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

    @MihuBot

    I'm a bot. Here is a possible related and/or duplicate issue (I may be wrong):

  5. added a commit that references this issue on Sep 14, 2026
    6085805
  6. EgorBo commented on Sep 18, 2026

    @EgorBo
    MemberAuthor

    Affected SDK versions:

    • 9.0.318
    • 10.0.401
    • 11.0.100-rc.2.26467.112
  7. added a commit that references this issue on Sep 18, 2026
    3dc9346
  8. locked and limited conversation to collaborators on Oct 19, 2026
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