Introduce wasmtime::StructRef and allocating Wasm GC structs#8933
Merged
fitzgen merged 14 commits intobytecodealliance:mainfrom Jul 11, 2024
Merged
Introduce wasmtime::StructRef and allocating Wasm GC structs#8933fitzgen merged 14 commits intobytecodealliance:mainfrom
wasmtime::StructRef and allocating Wasm GC structs#8933fitzgen merged 14 commits intobytecodealliance:mainfrom
Conversation
This commit introduces the `wasmtime::StructRef` type and support for allocating Wasm GC structs from the host. This commit does *not* add support for the `struct.new` family of Wasm instructions; guests still cannot allocate Wasm GC objects yet, but initial support should be pretty straightforward after this commit lands. The `StructRef` type has everything you expect from other value types in the `wasmtime` crate: * A method to get its type or check whether it matches a given type * An implementation of `WasmTy` so that it can be used with `Func::wrap`-style APIs * The ability to upcast it into an `AnyRef` and to do checked downcasts in the opposite direction There are, additionally, methods for getting, setting, and enumerating a `StructRef`'s fields. To allocate a `StructRef`, we need proof that the struct type we are allocating is being kept alive for the duration that the allocation may live. This is required for many reasons, but a basic example is getting a struct instance's type from the embedder API: this does a type-index-to-`StructType` lookup and conversion and if the type wasn't kept alive, then the type-index lookup will result in what is logically a use-after-free bug. This won't be a problem for Wasm guests (when we get around to implementing allocation for them) since their module defines the type, the store holds onto its instances' modules, and the allocation cannot outlive the store. For the host, we need another method of keeping the object's type alive, since it might be that the host defined the type and there is no module that also defined it, let alone such a module that is being kept alive in the store. The solution to the struct-type-lifetime problem that this commit implements for hosts is for the store to hold a hash set of `RegisteredType`s specifically for objects which were allocated via the embedder API. But we also don't want to do a hash lookup on every allocation, so we also implement a `StructRefPre` type. A `StructRefPre` is proof that the embedder has inserted a `StructType`'s inner `RegisteredType` into a store. Structurally, it is a pair of the struct type and a store id. All `StructRef` allocation methods require a `StructRefPre` argument, which does a fast store id check, rather than a whole hash table insertion. I opted to require `StructRefPre` in all allocation cases -- even though this has the downside of always forcing callers to create one before they allocate, even if they are only allocating a single object -- because of two reasons. First, this avoids needing to define duplicate methods, with and without a `StructRefPre` argument. Second, this avoids a performance footgun in the API where users don't realize that they *can* avoid extra work by creating a single `StructRefPre` and then using it multiple times. Anecdotally, I've heard multiple people complain about instantiation being slower than advertised but it turns out they weren't using `InstancePre`, and I'd like to avoid that situation for allocation if we can.
fitzgen
commented
Jul 10, 2024
Comment on lines
+320
to
+321
| // TODO: `dec_ref_and_maybe_dealloc` each `VMGcRef` inside this | ||
| // object. |
Member
Author
There was a problem hiding this comment.
Note that this is intentionally left for follow up PRs. I want to focus on getting the spec tests passing and all that first, especially since the DRC collector is just the stand in until we eventually grow our semi-space collector.
Subscribe to Label Actioncc @fitzgen DetailsThis 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:
To subscribe or unsubscribe from this label, edit the |
alexcrichton
approved these changes
Jul 11, 2024
Member
alexcrichton
left a comment
There was a problem hiding this comment.
I'm liking how this is all shaping up, very nice! I've got cosmetic comments here and there but nothing major.
And define `fields()` in terms of `field()` rather than the other way around.
…ializing vs writing fields
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This commit introduces the
wasmtime::StructReftype and support for allocating Wasm GC structs from the host. This commit does not add support for thestruct.newfamily of Wasm instructions; guests still cannot allocate Wasm GC objects yet, but initial support should be pretty straightforward after this commit lands.The
StructReftype has everything you expect from other value types in thewasmtimecrate:A method to get its type or check whether it matches a given type
An implementation of
WasmTyso that it can be used withFunc::wrap-style APIsThe ability to upcast it into an
AnyRefand to do checked downcasts in the opposite directionThere are, additionally, methods for getting, setting, and enumerating a
StructRef's fields.To allocate a
StructRef, we need proof that the struct type we are allocating is being kept alive for the duration that the allocation may live. This is required for many reasons, but a basic example is getting a struct instance's type from the embedder API: this does a type-index-to-StructTypelookup and conversion and if the type wasn't kept alive, then the type-index lookup will result in what is logically a use-after-free bug. This won't be a problem for Wasm guests (when we get around to implementing allocation for them) since their module defines the type, the store holds onto its instances' modules, and the allocation cannot outlive the store. For the host, we need another method of keeping the object's type alive, since it might be that the host defined the type and there is no module that also defined it, let alone such a module that is being kept alive in the store.The solution to the struct-type-lifetime problem that this commit implements for hosts is for the store to hold a hash set of
RegisteredTypes specifically for objects which were allocated via the embedder API. But we also don't want to do a hash lookup on every allocation, so we also implement aStructRefPretype. AStructRefPreis proof that the embedder has inserted aStructType's innerRegisteredTypeinto a store. Structurally, it is a pair of the struct type and a store id. AllStructRefallocation methods require aStructRefPreargument, which does a fast store id check, rather than a whole hash table insertion.I opted to require
StructRefPrein all allocation cases -- even though this has the downside of always forcing callers to create one before they allocate, even if they are only allocating a single object -- because of two reasons. First, this avoids needing to define duplicate methods, with and without aStructRefPreargument. Second, this avoids a performance footgun in the API where users don't realize that they can avoid extra work by creating a singleStructRefPreand then using it multiple times. Anecdotally, I've heard multiple people complain about instantiation being slower than advertised but it turns out they weren't usingInstancePre, and I'd like to avoid that situation for allocation if we can.