Skip to content

Transitively dec-ref objects in the DRC collector - #10401

Merged
fitzgen merged 5 commits into
bytecodealliance:mainfrom
fitzgen:drc-transitive-dec-ref
Mar 18, 2025
Merged

fitzgen merged 5 commits into
bytecodealliance:mainfrom
fitzgen:drc-transitive-dec-ref

Conversation

@fitzgen

@fitzgen fitzgen commented Mar 14, 2025

Copy link
Copy Markdown
Member

Previously, the DRC collector only performed shallow reference counting where if an object's ref count was decremented and reached zero, then we would deallocate that object, but we would not decrement the reference counts of any other object that was referenced by it. This commit changes that, so that now we will decrement the reference counts of other objects referenced by an object that is being deallocated.

This requires some method of tracing an object's outgoing edges. To avoid needing to keep around and dispatch upon type-specific information about which struct fields are GC references, for example, we instead pack all GC references at the start of the object, just after its header. We additionally keep a count of how many GC references an object has in its header. This allows us to create a slice of any object's GC references, and we can uniformly trace any GC object's outgoing edges. We don't need to reflect on the GC object's actual type.

A final detail: the DRC collector was previously storing the object size in the common GC header's reserved bits. It now stores the number of GC refs there instead, and the object size is stored in its own field in the DRC-specific header, next to the ref count. This is nice because, since we are making the DRC header larger anyways, it raises the DRC collector's implementation limit on object size from u27::MAX to u32::MAX.

Fixes #9701

@fitzgen
fitzgen requested a review from a team as a code owner March 14, 2025 17:53
@fitzgen
fitzgen requested review from pchickey and removed request for a team March 14, 2025 17:53
@fitzgen
fitzgen force-pushed the drc-transitive-dec-ref branch from 514ec58 to 55da619 Compare March 14, 2025 18:12
Previously, the DRC collector only performed shallow reference counting where if
an object's ref count was decremented and reached zero, then we would deallocate
that object, but we would not decrement the reference counts of any other object
that was referenced by it. This commit changes that, so that now we will
decrement the reference counts of other objects referenced by an object that is
being deallocated.

This requires some method of tracing an object's outgoing edges. To avoid
needing to keep around and dispatch upon type-specific information about which
struct fields are GC references, for example, we instead pack all GC references
at the start of the object, just after its header. We additionally keep a count
of how many GC references an object has in its header. This allows us to create
a slice of any object's GC references, and we can uniformly trace any GC
object's outgoing edges. We don't need to reflect on the GC object's actual
type.

A final detail: the DRC collector was previously storing the object size in the
common GC header's reserved bits. It now stores the number of GC refs there
instead, and the object size is stored in its own field in the DRC-specific
header, next to the ref count. This is nice because, since we are making the DRC
header larger anyways, it raises the DRC collector's implementation limit on
object size from `u27::MAX` to `u32::MAX`.

Fixes bytecodealliance#9701
@fitzgen
fitzgen force-pushed the drc-transitive-dec-ref branch from 55da619 to 4539476 Compare March 14, 2025 18:12
@fitzgen
fitzgen enabled auto-merge March 14, 2025 18:12
@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 Mar 14, 2025
@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.

fitzgen added 2 commits March 14, 2025 13:58
Sometimes we need to deallocate uninitialized objects when initializing their
fields fails, and this assertion trips.
@fitzgen
fitzgen added this pull request to the merge queue Mar 17, 2025
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Mar 17, 2025
We can't cast to a slice, we need to do conversion from little-endian bytes.
@fitzgen
fitzgen enabled auto-merge March 18, 2025 00:25
@fitzgen
fitzgen added this pull request to the merge queue Mar 18, 2025
github-merge-queue Bot pushed a commit that referenced this pull request Mar 18, 2025
* Transitively dec-ref objects in the DRC collector

Previously, the DRC collector only performed shallow reference counting where if
an object's ref count was decremented and reached zero, then we would deallocate
that object, but we would not decrement the reference counts of any other object
that was referenced by it. This commit changes that, so that now we will
decrement the reference counts of other objects referenced by an object that is
being deallocated.

This requires some method of tracing an object's outgoing edges. To avoid
needing to keep around and dispatch upon type-specific information about which
struct fields are GC references, for example, we instead pack all GC references
at the start of the object, just after its header. We additionally keep a count
of how many GC references an object has in its header. This allows us to create
a slice of any object's GC references, and we can uniformly trace any GC
object's outgoing edges. We don't need to reflect on the GC object's actual
type.

A final detail: the DRC collector was previously storing the object size in the
common GC header's reserved bits. It now stores the number of GC refs there
instead, and the object size is stored in its own field in the DRC-specific
header, next to the ref count. This is nice because, since we are making the DRC
header larger anyways, it raises the DRC collector's implementation limit on
object size from `u27::MAX` to `u32::MAX`.

Fixes #9701

* update disas tests

* Remove newly-added debug-assert that isn't true in certain edge cases

Sometimes we need to deallocate uninitialized objects when initializing their
fields fails, and this assertion trips.

* fix some tests by returning an error of the correct kind of trap

rather than a generic anyhow error

* fix tracing GC refs on big-endian architectures

We can't cast to a slice, we need to do conversion from little-endian bytes.
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Mar 18, 2025
@fitzgen
fitzgen added this pull request to the merge queue Mar 18, 2025
Merged via the queue into bytecodealliance:main with commit 0a29578 Mar 18, 2025
@fitzgen
fitzgen deleted the drc-transitive-dec-ref branch March 18, 2025 01:11
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.

Wasm GC doesn't seem to collect garbage which results in GC heap out of memory errors

2 participants