Skip to content

GC: a ≥ 4 GB array allocated during a background GC can contain a stray 0x01 byte (PREV_EMPTY left by make_unused_array) #135791

Closed

Description

@kforge-labs-io

Description

A managed array of 4 GB or more, allocated while a background GC is in progress, can come back from new with one nonzero byte inside it. The byte is 0x01, at a fixed offset determined by the object's size. Code that relies on the array being zero-initialised (for example Nullable<T>.HasValue in a T?[]) then sees one element as set.

What varies

Condition Result
Array of 2.5, 3.5 or 3.99 GiB, background GC overlapping, fresh memory clean (6 of 6 processes)
Array of 4.0001, 4.5, 5 or 6 GiB, the same exactly one 0x01 at data offset (byteLength mod 2^32) + 0x20 (8 of 8)
Array of 8 GiB, the same three 0x01s, at data offsets 0x20, 0x100000000 and 0x1FFFFFFE0
DOTNET_gcConcurrent=0 clean
No GC overlapping the allocation clean
Reused memory, where the allocation takes 250–400 ms because the GC clears it clean
DOTNET_GCName=clrgc.dll (segments), overlap forced but not event-confirmed dirty in 1 of 4, the same offset
.NET 8.0.31, 9.0.20, 10.0.12 dirty in each, the same offset

Multi-dimensional arrays are affected in the same way. In a T?[,,] of 28-byte elements, the byte lands on the hasValue field of one element whenever the element count is even.

Impact

This is silent data corruption in a newly allocated object. We found it in a dual-contouring mesher, where a stray hasValue produced a bogus surface element at a position fixed by the array dimensions. It happened in up to 4 of 6 large meshes, depending on whether a background GC coincided with the allocation.

Reproduction Steps

The program is a console app; no packages are needed. It builds a large live object graph so that a background mark lasts long enough to overlap the allocation, starts a background gen-2 GC, allocates a 6 GiB long[], and scans the array for nonzero bytes.

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

var live = new object[20_000_000];
for (int k = 0; k < live.Length; k++) live[k] = new int[2];
Thread.Sleep(2000);

GC.Collect(2, GCCollectionMode.Forced, blocking: false, compacting: false);
var a = new long[(6L << 30) / 8];

ref byte b0 = ref Unsafe.As<long, byte>(ref MemoryMarshal.GetArrayDataReference(a));
long bytes = a.LongLength * 8;
for (long i = 0; i < bytes; i++)
    if (Unsafe.Add(ref b0, (nint)i) != 0)
        Console.WriteLine($"nonzero byte 0x{Unsafe.Add(ref b0, (nint)i):X2} at data offset 0x{i:X}");
GC.KeepAlive(live);

Expected behavior

no output.

Actual behavior

nonzero byte 0x01 at data offset 0x80000020. This snippet printed it in 3 of 3 processes (10.0.12 twice, 8.0.31 once), and printed nothing with DOTNET_gcConcurrent=0. The array has to land on fresh memory, which is why it's the first large allocation in the process.

A fuller version of this program timestamped each allocation against the runtime's own GCStart/GCEnd events. Every dirty allocation overlapped a GC with Type = 1 (background). That version produced the table below.

Regression?

no

Known Workarounds

No response

Configuration

.NET version. Reproduced on three runtimes, all with the same byte at the same offset:

  • 10.0.12 (Microsoft.NETCore.App, host commit 95017c711e; SDK 10.0.401)
  • 9.0.20
  • 8.0.31

OS. Windows 11 Pro 26H2, build 10.0.26300.9711.

Architecture. x64 (win-x64). The machine is an Intel Core i7-14700F with 64 GB RAM.

GC configuration. Workstation GC with the default settings, so concurrent (background) GC is enabled. No runtimeconfig GC options are set.

Is it specific to that configuration? What we measured:

  • It reproduces with the default regions GC and with the segments GC (DOTNET_GCName=clrgc.dll, on 10.0.12).
  • It does not reproduce with DOTNET_gcConcurrent=0.
  • It does not reproduce when no background GC overlaps the allocation (confirmed from the runtime's GCStart/GCEnd events), or when the allocation lands on reused memory that the GC clears.
  • It needs an array of 4 GB or more. 3.99 GiB was clean in 6 of 6 processes, and 4.0001 GiB was dirty in 8 of 8.

What we have not tested: Linux, macOS, ARM64, Server GC, or any other machine.

From reading gc.cpp, we'd expect it on any 64-bit platform with background GC. The path is bgc_uoh_alloc_clr → make_unused_array, and PREV_EMPTY is written under DOUBLY_LINKED_FL, which gcpriv.h defines for BACKGROUND_GC && HOST_64BIT. Nothing in that code looks Windows-specific, and Server GC appears to go through the same function. That expectation comes from the source only; we haven't run it on those configurations.

Other information

Line numbers are from src/coreclr/gc/gc.cpp at v10.0.0; the code at v8.0.0 is the same.

  1. bgc_uoh_alloc_clr (17717), the path for UOH allocations made during a background GC, first calls make_unused_array(alloc_start, size).
  2. make_unused_array (36254) splits a block over 4 GB into several free objects, because m_NumComponents is 32-bit. The second piece starts at x + (uint32_t)(size - free_object_base_size) + free_object_base_size, and later pieces follow every 0xFFFFFFE0 bytes.
  3. Each piece goes through SetFree (4943). Under DOUBLY_LINKED_FL (gcpriv.h:195), SetFree calls check_and_clear_in_free_list (4757). For any piece of at least min_free_list bytes, that writes free_list_prev(o) = PREV_EMPTY ((uint8_t*)1) at piece + 0x18.
  4. Back in bgc_uoh_alloc_clr:
    • clear_prev_bit(alloc_start, size) (4766) resets the prev slot of the first piece only.
    • memclr then clears only up to heap_segment_used. Memory beyond that is assumed to be zero.
    • clear_unused_array (36315) runs UnsetFree (4981) on every piece. UnsetFree zeroes 16 bytes, the method table and the length, but not the prev slot at +0x18.
  5. So on fresh memory, the PREV_EMPTY written into every piece after the first survives into the object handed back to the caller.

This reproduces every offset in the table above. At 6 GiB, the second piece is at x + 0x80000018, and its prev slot is data + 0x80000020. The last piece is 0x20 bytes, smaller than min_free_list, so it isn't written. At 8 GiB, the pieces sit at x + 24 and x + 0xFFFFFFF8, plus a last piece of 0x40 bytes.

Possible fixes: have clear_unused_array clear free_list_prev for each piece that make_unused_array created (under DOUBLY_LINKED_FL), or call clear_prev_bit per piece rather than only on the first.

Activity

  1. added
    untriaged
    on Oct 14, 2026
  2. dotnet-policy-service commented on Oct 14, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @anicka-net, @dotnet/gc
    See info in area-owners.md if you want to be subscribed.

  3. removed
    untriaged
    on Oct 14, 2026
  4. added this to the 10.0.x milestone on Oct 14, 2026
  5. added a commit that references this issue on Oct 19, 2026
    f25130a
  6. locked and limited conversation to collaborators on Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    • Status
      No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions