Skip to content

Crossgen: (bug) GC hole when an [InlineArray] is overlapped by a ref field at offset 0 (elements 1..N-1 lose their GC slots) #137842

In review

Description

@EgorBo

CorInfoImpl.getClassGClayout reports an incomplete GC layout for an explicit-layout struct in which an object field overlaps element 0 of an [InlineArray] field. Only element 0 is reported as a GC slot, so R2R and NativeAOT code do not report elements 1..N-1 to the GC. JIT-only execution is correct.

Minimal Repro

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

[InlineArray(2)]
public struct Arr { public object e; }

[StructLayout(LayoutKind.Explicit)]
public struct Outer
{
    [FieldOffset(0)] public object o;
    [FieldOffset(0)] public Arr arr;
}

public class Program
{
    [MethodImpl(MethodImplOptions.NoInlining)]
    static Outer Make(int i)
    {
        Outer x = default;
        x.arr[1] = new string('a', 40 + (i & 7));
        return x;
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    static int Test(int i)
    {
        Outer x = Make(i);
        GC.Collect();
        for (int k = 0; k < 100; k++) GC.KeepAlive(new string('b', 40 + (i & 7)));
        return ((string)x.arr[1]).IndexOf('b');
    }

    public static void Main()
    {
        int bad = 0;
        for (int i = 0; i < 200; i++)
            if (Test(i) != -1) bad++;
        Console.WriteLine(bad == 0 ? "OK" : "FAIL " + bad);
    }
}

Expected

OK

Actual

The crossgen2-compiled code fails with:

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at Program.Test(Int32 i)
   at Program.Main()

The GC info for Test has only [rsp+0x28] (o / arr[0]) as a GC slot, but the code loads x.arr[1] from gword ptr [rsp+0x30]. ILC produces the same incomplete GC info.

Regression?

The bug is reported to also reproduce with .NET 10.0.15 PublishReadyToRun; this was not re-verified. It reproduces on main.

Notes

In GatherClassGCLayout, MarkGcField returns 0 when a slot is already marked with the same GC type. Because o marks slot 0 first, walking Arr gives result == 0, and the replication loop for elements 1..N-1 is skipped. Replication should depend on the element's GC layout (e.g. ContainsGCPointers), not on the number of newly marked slots.

Activity

  1. added this to the 12.0.0 milestone on Nov 18, 2026
  2. added
    area-crossgen2-coreclr
    on Nov 18, 2026
  3. added and removed
    area-crossgen2-coreclr
    on Nov 19, 2026
  4. dotnet-policy-service commented on Nov 19, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/crossgen-contrib
    See info in area-owners.md if you want to be subscribed.

  5. linked a pull request that will close this issueFix GC hole in InlineArray struct layout #137932on Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

  • Status
    No status

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions