Skip to content

[mono][wasm] References in a large static readonly struct are not kept valid across a GC (browser-wasm) #136602

Closed

Description

@hclewk

Description

On browser-wasm (Mono), the object references held inside a struct kept in a static field are not
kept valid across garbage collections once the struct has 127 or more pointer-sized fields. After a
collection the fields still hold the old addresses, so they no longer refer to the objects they were set
to. Fields of the same struct that are read from a heap object, and structs of up to 126 reference fields,
are not affected. Desktop CoreCLR is not affected.

In a real library this showed up as a static readonly struct of about 1.6 KB (a few hundred fields,
many of them references) that is copied from on every use. The first use works. After the first GC the
copies contain dangling references and the interpreter aborts with one of:

[MONO] * Assertion: should not be reached at /__w/1/s/src/runtime/src/mono/mono/metadata/class-accessors.c:93
RuntimeError: memory access out of bounds

The failure moves around between runs (it depends on what the dangling reference is later used for),
which makes it hard to recognize from the symptom alone.

Environment

  • .NET SDK 10.0.403 (latest .NET 10 servicing; also seen on 10.0.400), Microsoft.NETCore.App.Runtime.Mono.browser-wasm 10.0.14 (RuntimeInformation reports .NET 10.0.14, browser-wasm), no workloads installed for the 10.0.403 runs
  • App: Microsoft.NET.Sdk.WebAssembly, OutputType Exe, driven from main.js with dotnet.create() / runMain()
  • Browser: Google Chrome 149 (headless), Node 22 only as a static server and driver
  • Reproduces in all of these:
    • Debug publish, interpreter, WasmBuildNative=false (no native relink)
    • Release publish, interpreter, WasmBuildNative=false
    • Release publish with WasmBuildNative=true (native relink, wasm-tools workload installed)
    • Debug publish with WasmBuildNative=true, observed on an earlier version of the repro below (same threshold)
  • Does not reproduce: the same Program.cs and Structs.g.cs as a console app on desktop CoreCLR (.NET 10.0.14, linux-x64, Debug and Release, workstation and server GC): all cases pass.
  • AOT-compiled WebAssembly was not tested.

Minimal reproduction

The standalone repro (BCL only, no other packages) is in this gist: https://gist.github.com/hclewk/aabd1c33a7f4bd71242a2cadc500fa0b. It has, for each N in {16, 100, 126, 127, 128, 400}, a struct with N
object? fields, all set to one object, held in a static readonly field. It allocates garbage, calls
GC.Collect() three times, then counts the fields that no longer refer to that object. The same struct held
in a field of a heap object that a static field refers to is the control.

Reduced to its core:

struct Fields127 { public object? F0, F1, /* ... */ F126; }       // 127 reference fields

static class Static127
{
    public static readonly object Target = new object();            // may live anywhere
    public static readonly Fields127 Value = Make();                // all 127 fields = Target
    ...
}

// later, after GC.Collect() a few times:
ReferenceEquals(Static127.Value.F0, Static127.Target)               // false, for every field

Steps (full commands, including the flat-gist layout and the CoreCLR comparison, are in the gist README.md):

dotnet publish -c Debug -o out        # or -c Release
node run.mjs out/wwwroot              # headless Chrome; prints PASS/FAIL per case

Expected

Every field still refers to the object it was set to, as on CoreCLR and as for 126 fields or fewer.

Actual

PASS Static fields= 16 staleBeforeGC=0 staleAfterGC=0
PASS Static fields=100 staleBeforeGC=0 staleAfterGC=0
PASS Static fields=126 staleBeforeGC=0 staleAfterGC=0
FAIL Static fields=127 staleBeforeGC=0 staleAfterGC=127
FAIL Static fields=128 staleBeforeGC=0 staleAfterGC=128
FAIL Static fields=400 staleBeforeGC=0 staleAfterGC=400
PASS Holder fields= 16 ... through fields=400   (all pass: the struct inside a heap object)

Every reference field is stale, not just some, and nothing is stale before the first collection.

Threshold: 126 passes, 127 fails

The boundary is exact and independent of where the target object lives (a static of another class, or a
static of the same class as the struct, both tried): 126 reference fields pass, 127 fail, up to 400 tried.
On wasm32 a pointer-sized word is 4 bytes, so 127 fields is 508 bytes of fields and, with the two-word
object header that a boxed or instance_size-based layout counts, 129 words, one more than the 128-bit
default_bitmap used below.

Hypothesis (not verified; I have not debugged or patched the runtime)

The numbers match the way compute_class_bitmap handles a nested value type when the bitmap it was given
is too small. In src/mono/mono/metadata/object.c (release/10.0):

  • compute_class_bitmap (line 850) starts by computing the size of the type it is describing
    (max_size, line 864, from m_class_get_instance_size) and, if that is larger than the bitmap size it
    was passed (if (max_size > size), line 865), it allocates a new bitmap and writes into that one.
  • For a field of value type that has references it recurses (line 958):
    compute_class_bitmap (fclass, bitmap, size, pos - MONO_OBJECT_HEADER_BITS, max_set, FALSE);
    with the return value discarded. If the nested call decided it needed a bigger bitmap, the bits it
    sets go into its private allocation, which is dropped (and leaked); the caller's bitmap gets none of
    them, while *max_set has still been raised.
  • The statics bitmap starts from gsize default_bitmap [4] (128 bits on wasm32) at the call at line 2236
    (mono_class_vtable_checked), with static_fields = TRUE.
  • A struct of 126 reference fields has instance_size / wordsize = 128 (if the header is two words), which
    does not exceed 128. A struct of 127 has 129, which does. That is the observed 126-versus-127 boundary.

If that is what happens, the static area's GC descriptor reports that nothing in the struct is a
reference, so the collector never updates those slots. That fits "all fields stale, other statics fine",
and it would not depend on the interpreter, which matches the relinked build failing the same way. The
likely fix would be to pass the nested value type a bitmap of adequate size (or to size the outer bitmap
from the largest nested type) instead of discarding the nested call's result. I did not test this against
a patched runtime, so it may be a different cause; I did not look at why a 400-field struct, whose outer
static area exceeds 128 words and so gets a larger outer bitmap, fails as well, other than that the nested
type is always one header larger than the area it sits in.

Related, already fixed: #116879 "[mono][gc] Fix gc descriptor computation for InlineArray structs"
(same function, different defect: the offset advanced per repeated element), backported in #116951. It is
in the 10.0.14 runtime used here, so it does not explain this.

Workaround

Keep the struct in a field of a heap object and have the static field refer to that object:

public static ref readonly Big Value => ref s_holder.Value;
static readonly Holder s_holder = new();
sealed class Holder { public readonly Big Value = Make(); }

The static area is then one reference word, and the object's own descriptor is computed from its instance
layout, which works for every size tried (the Holder rows in the repro). Keeping the struct under 127 reference-sized words also avoids it.

Notes

  • Not an interpreter-only problem: the native-relinked Release build fails the same way.
  • A library that copies a large static struct (for example a table of initial values) hits this after the
    first collection; nothing is reported at the point where the struct is built or first copied.

Activity

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

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @BrzVlad
    See info in area-owners.md if you want to be subscribed.

  3. self-assigned this
    on Oct 29, 2026
  4. removed
    untriaged
    on Oct 29, 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

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions