Skip to content

Fix leaks of already-initialized fields when struct/array init fails#12482

Open
zzjas wants to merge 1 commit intobytecodealliance:mainfrom
zzjas:issue-12456-drc-leak
Open

Fix leaks of already-initialized fields when struct/array init fails#12482
zzjas wants to merge 1 commit intobytecodealliance:mainfrom
zzjas:issue-12456-drc-leak

Conversation

@zzjas
Copy link
Contributor

@zzjas zzjas commented Jan 31, 2026

When StructRef::new or ArrayRef::new_fixed fails midway, dealloc_uninit frees the object without dec-refing GC refs already written to earlier fields, leaking them. Fix by zero-filling the object body in alloc_raw so trace_gc_ref skips uninitialized slots, then tracing and dec-refing children in dealloc_uninit before freeing.

Fixes #12456

When `StructRef::new` or `ArrayRef::new_fixed` fails midway, `dealloc_uninit`
frees the object without dec-refing GC refs already written to earlier
fields, leaking them. Fix by zero-filling the object body in `alloc_raw`
so trace_gc_ref skips uninitialized slots, then tracing and dec-refing
children in `dealloc_uninit` before freeing.

Fixes bytecodealliance#12456
@zzjas zzjas requested a review from a team as a code owner January 31, 2026 00:19
@zzjas zzjas requested review from alexcrichton and removed request for a team January 31, 2026 00:19
@zzjas
Copy link
Contributor Author

zzjas commented Jan 31, 2026

cc: @fitzgen

@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 Jan 31, 2026
@github-actions
Copy link

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.

Copy link
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

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

Thanks for this!

Thinking about this a bit more though, it feels like it's a lot of infrastructure here for not a ton of benefit. For example this pessimizes all allocations to be zero-filled instead of truly uninitialized. This isn't even used by compiled guest wasm and it's also only used on the host, a secondary vector for implementing GC things.

Given all that, I'm wondering if we should step back a bit and reconsider the basic strategy here. For example I would naively expect that one way to do this would be to enhance the type-check we have for initializing structs/arrays/etc on the host such that the initialization step never fails. We already do a type-check there and it looks like the externref bits aren't fully checked there. One other way to tackle this problem would be to (1) enhance the type-check to avoid failing during initialization and (2) use .unwrap() to assert that per-field initialization succeeds.

cc @fitzgen on this as well, you likely have thoughts too! I also think there's a case to be made for "let's fix the bug first and then make it fast later", in which case I think this PR is perfectly suitable. The only downside is that it puts things in a bit of a weird state where the "uninit" terminology is now more of a "maybe uninit maybe not" which is a bit confusing. Not enough to block landing this though, but that's what led me to reconsider the overall approach.

@fitzgen
Copy link
Member

fitzgen commented Feb 2, 2026

I like the idea of checking that references are properly rooted at type-checking time, and unwraping during field initialization.

My other suggestion would have been to keep track of how many fields have been initialized and then drop the initialized fields in an Undo, but I like checking ahead of time better.

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 collector leaks GC refs when struct/array initialization fails partway through

3 participants