Repository navigation
Description
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 commit95017c711e; 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/GCEndevents), 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.
bgc_uoh_alloc_clr(17717), the path for UOH allocations made during a background GC, first callsmake_unused_array(alloc_start, size).make_unused_array(36254) splits a block over 4 GB into several free objects, becausem_NumComponentsis 32-bit. The second piece starts atx + (uint32_t)(size - free_object_base_size) + free_object_base_size, and later pieces follow every0xFFFFFFE0bytes.- Each piece goes through
SetFree(4943). UnderDOUBLY_LINKED_FL(gcpriv.h:195),SetFreecallscheck_and_clear_in_free_list(4757). For any piece of at leastmin_free_listbytes, that writesfree_list_prev(o) = PREV_EMPTY((uint8_t*)1) at piece + 0x18. - Back in
bgc_uoh_alloc_clr:clear_prev_bit(alloc_start, size)(4766) resets the prev slot of the first piece only.memclrthen clears only up toheap_segment_used. Memory beyond that is assumed to be zero.clear_unused_array(36315) runsUnsetFree(4981) on every piece.UnsetFreezeroes 16 bytes, the method table and the length, but not the prev slot at +0x18.
- So on fresh memory, the
PREV_EMPTYwritten 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.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNo status
Activity
dotnet-policy-service commentedon Oct 14, 2026
Tagging subscribers to this area: @anicka-net, @dotnet/gc
See info in area-owners.md if you want to be subscribed.
Fix background GC zeroing of arrays larger than 4 GiB (#135797)