Skip to content

Trigger GC in the DRC collector when the OASR list is twice the size it was after the last GC - #13422

Merged
fitzgen merged 4 commits into
bytecodealliance:mainfrom
fitzgen:wasmtime-drc-over-approx-force-gc
May 21, 2026
Merged

fitzgen merged 4 commits into
bytecodealliance:mainfrom
fitzgen:wasmtime-drc-over-approx-force-gc

Conversation

@fitzgen

@fitzgen fitzgen commented May 20, 2026

Copy link
Copy Markdown
Member

This should trigger collection in more situations than just filling up the heap from allocation.

Fixes #13403 in theory, but I haven't been able to measure sweep times reported in that issue without this fix on my laptop. FWIW, that issue does refer to Android, and so is probably using radically different hardware.

@fitzgen
fitzgen requested review from a team as code owners May 20, 2026 19:38
@fitzgen
fitzgen requested review from cfallin and removed request for a team May 20, 2026 19:38
@github-actions github-actions Bot added wasmtime:api Related to the API of the `wasmtime` crate itself wasmtime:ref-types Issues related to reference types and GC in Wasmtime labels May 20, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Subscribe to Label Action

cc @fitzgen

Details This issue or pull request has been labeled: "wasmtime:api", "wasmtime:ref-types"

Thus the following users have been cc'd because of the following labels:

  • fitzgen: wasmtime:ref-types

To subscribe or unsubscribe from this label, edit the .github/subscribe-to-label.json configuration file.

Learn more.

@cfallin cfallin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

fitzgen added 4 commits May 21, 2026 11:28
…it was after the last GC

This should trigger collection in more situations than just filling up the heap
from allocation.

Fixes bytecodealliance#13403 in theory, but I
haven't been able to measure sweep times reported in that issue without this fix
on my laptop. FWIW, that issue does refer to Android, and so is probably using
radically different hardware.
@fitzgen
fitzgen force-pushed the wasmtime-drc-over-approx-force-gc branch from e403269 to 734fab6 Compare May 21, 2026 18:32
@fitzgen
fitzgen enabled auto-merge May 21, 2026 18:34
@fitzgen
fitzgen added this pull request to the merge queue May 21, 2026
Merged via the queue into bytecodealliance:main with commit 9a0b0e0 May 21, 2026
51 checks passed
@fitzgen
fitzgen deleted the wasmtime-drc-over-approx-force-gc branch May 21, 2026 19:09
vouillon added a commit to vouillon/wasmtime that referenced this pull request May 22, 2026
…py loop

`translate_per_element_copy` derives raw `src_elem_addr` /
`dst_elem_addr` pointers once and uses only those raw pointers inside
the per-element forward/backward loop. As a result, the original source
and destination *array* gc-refs (the `CheckedEntity::Array { array,
.. }` values that go through `read_field_at_addr` /
`write_field_at_addr`) are dead in CLIF after the address computation
and are not in the stack maps at safe points inside the loop.

The latent stack-map gap was harmless until the DRC collector started
firing `force_gc` from inside the read barrier when the
over-approximated-stack-roots list grew past 1024 entries (bytecodealliance#13422). At
that point, a GC could run mid-copy with neither the source nor the
destination array marked from any frame's stack map; sweep would
remove them from OASR, and if their only ref-count contribution was
the OASR membership they would be freed, cascading dec-refs through
their elements. The rest of the copy would then read freed memory and
write dangling references into the destination.

Fix this by extracting the array gc-refs from the source and
destination entities (when they are arrays) and threading them through
the loop's forward and backward iteration blocks as block parameters,
calling `declare_value_needs_stack_map` on the per-iteration copies so
they are tracked at safe points inside the loop. After the fix, the
stack map at the in-barrier `force_gc` inside `array.copy`'s loop
lists the source and destination arrays in addition to the
freshly-read element.
vouillon added a commit to vouillon/wasmtime that referenced this pull request May 22, 2026
…py loop

`translate_per_element_copy` derives raw `src_elem_addr` /
`dst_elem_addr` pointers once and uses only those raw pointers inside
the per-element forward/backward loop. As a result, the original source
and destination *array* gc-refs (the `CheckedEntity::Array { array,
.. }` values that go through `read_field_at_addr` /
`write_field_at_addr`) are dead in CLIF after the address computation
and are not in the stack maps at safe points inside the loop.

The latent stack-map gap was harmless until the DRC collector started
firing `force_gc` from inside the read barrier when the
over-approximated-stack-roots list grew past 1024 entries (bytecodealliance#13422). At
that point, a GC could run mid-copy with neither the source nor the
destination array marked from any frame's stack map; sweep would
remove them from OASR, and if their only ref-count contribution was
the OASR membership they would be freed, cascading dec-refs through
their elements. The rest of the copy would then read freed memory and
write dangling references into the destination.

Fix this by extracting the array gc-refs from the source and
destination entities (when they are arrays) and threading them through
the loop's forward and backward iteration blocks as block parameters,
calling `declare_value_needs_stack_map` on the per-iteration copies so
they are tracked at safe points inside the loop. After the fix, the
stack map at the in-barrier `force_gc` inside `array.copy`'s loop
lists the source and destination arrays in addition to the
freshly-read element.
vouillon added a commit to vouillon/wasmtime that referenced this pull request May 23, 2026
…py loop

`translate_per_element_copy` derives raw `src_elem_addr` /
`dst_elem_addr` pointers once and uses only those raw pointers inside
the per-element forward/backward loop. As a result, the original source
and destination *array* gc-refs (the `CheckedEntity::Array { array,
.. }` values that go through `read_field_at_addr` /
`write_field_at_addr`) are dead in CLIF after the address computation
and are not in the stack maps at safe points inside the loop.

The latent stack-map gap was harmless until the DRC collector started
firing `force_gc` from inside the read barrier when the
over-approximated-stack-roots list grew past 1024 entries (bytecodealliance#13422). At
that point, a GC could run mid-copy with neither the source nor the
destination array marked from any frame's stack map; sweep would
remove them from OASR, and if their only ref-count contribution was
the OASR membership they would be freed, cascading dec-refs through
their elements. The rest of the copy would then read freed memory and
write dangling references into the destination.

Fix this by extracting the array gc-refs from the source and
destination entities (when they are arrays) and threading them through
the loop's forward and backward iteration blocks as block parameters,
calling `declare_value_needs_stack_map` on the per-iteration copies so
they are tracked at safe points inside the loop. After the fix, the
stack map at the in-barrier `force_gc` inside `array.copy`'s loop
lists the source and destination arrays in addition to the
freshly-read element.
vouillon added a commit to vouillon/wasmtime that referenced this pull request May 23, 2026
…py loop

`translate_per_element_copy` derives raw `src_elem_addr` /
`dst_elem_addr` pointers once and uses only those raw pointers inside
the per-element forward/backward loop. As a result, the original source
and destination *array* gc-refs (the `CheckedEntity::Array { array,
.. }` values that go through `read_field_at_addr` /
`write_field_at_addr`) are dead in CLIF after the address computation
and are not in the stack maps at safe points inside the loop.

The latent stack-map gap was harmless until the DRC collector started
firing `force_gc` from inside the read barrier when the
over-approximated-stack-roots list grew past 1024 entries (bytecodealliance#13422). At
that point, a GC could run mid-copy with neither the source nor the
destination array marked from any frame's stack map; sweep would
remove them from OASR, and if their only ref-count contribution was
the OASR membership they would be freed, cascading dec-refs through
their elements. The rest of the copy would then read freed memory and
write dangling references into the destination.

Fix this by extracting the array gc-refs from the source and
destination entities (when they are arrays) and threading them through
the loop's forward and backward iteration blocks as block parameters,
calling `declare_value_needs_stack_map` on the per-iteration copies so
they are tracked at safe points inside the loop. After the fix, the
stack map at the in-barrier `force_gc` inside `array.copy`'s loop
lists the source and destination arrays in addition to the
freshly-read element.
fitzgen pushed a commit to fitzgen/wasmtime that referenced this pull request May 26, 2026
…loop (bytecodealliance#13450)

* Add regression test for `array.copy` over GC-ref arrays with mid-loop GC

In `translate_per_element_copy` (used by `array.copy` on arrays whose
elements are GC references) the per-element loop derives raw
`src_elem_addr`/`dst_elem_addr` pointers once and uses only those raw
pointers inside the loop body. The source and destination *array*
gc-refs themselves are dead in CLIF inside the loop and were absent
from the stack maps at safe points there.

Once the DRC collector started firing `force_gc` from inside the read
barrier (when the over-approximated-stack-roots list grew past 1024
entries) a GC could run mid-copy with neither array marked from any
frame's stack map. Sweep would drop the arrays from OASR, freeing them
if they had no other ref-count contributions and cascading dec-refs
through their elements, after which the rest of the copy read from the
freed memory and produced dangling references in the destination.

Surfaces in practice as a `wasm trap: cast failure` deep in OCaml's
sort routines when running jsoo-compiled wasm.

The test uses a small `gc_heap_reservation` and a churn-allocation
pass after the copy to force slot reuse so the corruption (stale
references in the destination array) is observable as wrong values in
the verify pass.

* Keep source/destination array gc-refs alive across the per-element copy loop

`translate_per_element_copy` derives raw `src_elem_addr` /
`dst_elem_addr` pointers once and uses only those raw pointers inside
the per-element forward/backward loop. As a result, the original source
and destination *array* gc-refs (the `CheckedEntity::Array { array,
.. }` values that go through `read_field_at_addr` /
`write_field_at_addr`) are dead in CLIF after the address computation
and are not in the stack maps at safe points inside the loop.

The latent stack-map gap was harmless until the DRC collector started
firing `force_gc` from inside the read barrier when the
over-approximated-stack-roots list grew past 1024 entries (bytecodealliance#13422). At
that point, a GC could run mid-copy with neither the source nor the
destination array marked from any frame's stack map; sweep would
remove them from OASR, and if their only ref-count contribution was
the OASR membership they would be freed, cascading dec-refs through
their elements. The rest of the copy would then read freed memory and
write dangling references into the destination.

Fix this by extracting the array gc-refs from the source and
destination entities (when they are arrays) and threading them through
the loop's forward and backward iteration blocks as block parameters,
calling `declare_value_needs_stack_map` on the per-iteration copies so
they are tracked at safe points inside the loop. After the fix, the
stack map at the in-barrier `force_gc` inside `array.copy`'s loop
lists the source and destination arrays in addition to the
freshly-read element.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

wasmtime:api Related to the API of the `wasmtime` crate itself wasmtime:ref-types Issues related to reference types and GC in Wasmtime

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DRC: per-GC sweep cost grows unbounded over time on steady-state Kotlin/Wasm workload (Android ANR after ~10–48 min)

2 participants