Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Fix `ResourceAny` conversion of synthetic borrowed host resources (#14384)

B. Naut4ac4c8cbf7a8
diff --git a/RELEASES.md b/RELEASES.md
index 634c9faedd..9bd5437022 100644
--- a/RELEASES.md
+++ b/RELEASES.md
@@ -11,6 +11,14 @@ Unreleased.
- Remove non-functional `listenfd` WASI CLI option.
+### Fixed
+
+- Converting a borrowed `Resource<T>` or `ResourceDynamic` created with
+ `new_borrow` into a `ResourceAny` outside of a component call no longer
+ panics. The borrow is now lowered into the guest when the call happens, and
+ `ResourceAny::resource_drop` is no longer required for such borrows.
+ [#7793](https://github.com/bytecodealliance/wasmtime/issues/7793)
+
--------------------------------------------------------------------------------
Release notes for previous releases of Wasmtime can be found on the respective
diff --git a/crates/wasmtime/src/runtime/component/resources/any.rs b/crates/wasmtime/src/runtime/component/resources/any.rs
index 3497b76984..2c22f2c5a3 100644
--- a/crates/wasmtime/src/runtime/component/resources/any.rs
+++ b/crates/wasmtime/src/runtime/component/resources/any.rs
@@ -3,11 +3,11 @@
//! owned by the guest or the host.
//!
//! This is in contrast with `Resource<T>`, for example, and `ResourceAny` has
-//! more "state" behind it. Specifically a `ResourceAny` has a type and a
-//! `HostResourceIndex` which points inside of a `HostResourceData` structure
-//! inside of a store. The `ResourceAny::resource_drop` method, or a conversion
-//! to `Resource<T>`, is required to be called to avoid leaking data within a
-//! store.
+//! more "state" behind it. Most `ResourceAny` values have a type and a
+//! `HostResourceIndex` which points inside of a store. These must be dropped
+//! or converted to a typed resource to release that state. A synthetic borrow
+//! converted from `Resource::new_borrow` instead holds its representation
+//! directly and has no host table entry.
use crate::component::func::{LiftContext, LowerContext, bad_type_info, desc};
use crate::component::matching::InstanceType;
@@ -21,6 +21,12 @@ use core::mem::MaybeUninit;
use core::ptr::NonNull;
use wasmtime_environ::component::{CanonicalAbiInfo, InterfaceType};
+#[derive(Debug, PartialEq, Eq, Copy, Clone)]
+enum ResourceAnyIndex {
+ Table(HostResourceIndex),
+ Borrow(u32),
+}
+
/// Representation of a resource in the component model, either a guest-defined
/// or a host-defined resource.
///
@@ -36,25 +42,36 @@ use wasmtime_environ::component::{CanonicalAbiInfo, InterfaceType};
/// types.
///
/// Like [`Resource`] this type represents either an `own` or a `borrow`
-/// resource internally. Unlike [`Resource`], however, a [`ResourceAny`] must
-/// always be explicitly destroyed with the [`ResourceAny::resource_drop`]
-/// method. This will update internal dynamic state tracking and invoke the
+/// resource internally. A [`ResourceAny`] with a host table entry must be
+/// explicitly destroyed with [`ResourceAny::resource_drop`] (or converted to
+/// a typed resource). This updates dynamic state tracking and invokes the
/// WebAssembly-defined destructor for a resource, if any.
///
-/// Note that it is required to call `resource_drop` for all instances of
-/// [`ResourceAny`]: even borrows. Both borrows and own handles have state
-/// associated with them that must be discarded by the time they're done being
-/// used.
+/// Borrows lifted from a component have host table state and must be dropped.
+/// Synthetic borrows converted from [`Resource::new_borrow`] have no host table
+/// state; calling `resource_drop` on one is harmless but unnecessary.
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub struct ResourceAny {
- idx: HostResourceIndex,
+ idx: ResourceAnyIndex,
ty: ResourceType,
owned: bool,
}
impl ResourceAny {
pub(crate) fn new(idx: HostResourceIndex, ty: ResourceType, owned: bool) -> ResourceAny {
- ResourceAny { idx, ty, owned }
+ ResourceAny {
+ idx: ResourceAnyIndex::Table(idx),
+ ty,
+ owned,
+ }
+ }
+
+ pub(crate) fn new_borrow(rep: u32, ty: ResourceType) -> ResourceAny {
+ ResourceAny {
+ idx: ResourceAnyIndex::Borrow(rep),
+ ty,
+ owned: false,
+ }
}
/// Attempts to convert an imported [`Resource`] into [`ResourceAny`].
@@ -108,26 +125,28 @@ impl ResourceAny {
T: HostResourceType<D>,
D: PartialEq + Send + Sync + Copy + 'static,
{
- let store = store.as_context_mut();
- let mut tables = HostResourceTables::new_host(store.0)?;
let ResourceAny { idx, ty, owned } = self;
let ty = T::typecheck(ty).ok_or_else(|| crate::format_err!("resource type mismatch"))?;
- if owned {
- let rep = tables.host_resource_lift_own(idx)?;
- Ok(HostResource::new_own(rep, ty))
- } else {
- // For borrowed handles, first acquire the `rep` via lifting the
- // borrow. Afterwards though remove any dynamic state associated
- // with this borrow. `Resource<T>` doesn't participate in dynamic
- // state tracking and it's assumed embedders know what they're
- // doing, so the drop call will clear out that a borrow is active
- //
- // Note that the result of `drop` should always be `None` as it's a
- // borrowed handle, so assert so.
- let rep = tables.host_resource_lift_borrow(idx)?;
- let res = tables.host_resource_drop(idx)?;
- assert!(res.is_none());
- Ok(HostResource::new_borrow(rep, ty))
+ match idx {
+ ResourceAnyIndex::Borrow(rep) => {
+ assert!(!owned);
+ Ok(HostResource::new_borrow(rep, ty))
+ }
+ ResourceAnyIndex::Table(idx) => {
+ let store = store.as_context_mut();
+ let mut tables = HostResourceTables::new_host(store.0)?;
+ if owned {
+ let rep = tables.host_resource_lift_own(idx)?;
+ Ok(HostResource::new_own(rep, ty))
+ } else {
+ // Typed borrows have no dynamic state. Remove the table
+ // entry after lifting its representation.
+ let rep = tables.host_resource_lift_borrow(idx)?;
+ let res = tables.host_resource_drop(idx)?;
+ assert!(res.is_none());
+ Ok(HostResource::new_borrow(rep, ty))
+ }
+ }
}
}
@@ -151,8 +170,8 @@ impl ResourceAny {
/// Destroy this resource and release any state associated with it.
///
- /// This is required to be called for all instances of [`ResourceAny`] to
- /// ensure that state associated with this resource is properly cleaned up.
+ /// This is required for resources with host table state. For synthetic
+ /// borrows converted from [`Resource::new_borrow`] it has no effect.
/// For owned resources this may execute the guest-defined destructor if
/// applicable (or the host-defined destructor if one was specified).
///
@@ -234,7 +253,11 @@ impl ResourceAny {
//
// This could fail if the index is invalid or if this is removing an
// `Own` entry which is currently being borrowed.
- let pair = HostResourceTables::new_host(store.0)?.host_resource_drop(self.idx)?;
+ let idx = match self.idx {
+ ResourceAnyIndex::Table(idx) => idx,
+ ResourceAnyIndex::Borrow(_) => return Ok(()),
+ };
+ let pair = HostResourceTables::new_host(store.0)?.host_resource_drop(idx)?;
let (rep, slot) = match (pair, self.owned) {
(Some(pair), true) => pair,
@@ -288,14 +311,22 @@ impl ResourceAny {
if cx.resource_type(t) != self.ty {
bail!("mismatched resource types");
}
- let rep = cx.host_resource_lift_own(self.idx)?;
+ let rep = match self.idx {
+ ResourceAnyIndex::Table(idx) => cx.host_resource_lift_own(idx)?,
+ ResourceAnyIndex::Borrow(_) => {
+ bail!("cannot lower a `borrow` resource into an `own`")
+ }
+ };
cx.guest_resource_lower_own(t, rep)
}
InterfaceType::Borrow(t) => {
if cx.resource_type(t) != self.ty {
bail!("mismatched resource types");
}
- let rep = cx.host_resource_lift_borrow(self.idx)?;
+ let rep = match self.idx {
+ ResourceAnyIndex::Table(idx) => cx.host_resource_lift_borrow(idx)?,
+ ResourceAnyIndex::Borrow(rep) => rep,
+ };
cx.guest_resource_lower_borrow(t, rep)
}
_ => bad_type_info(),
@@ -308,21 +339,13 @@ impl ResourceAny {
let ty = cx.resource_type(t);
let (rep, dtor, flags) = cx.guest_resource_lift_own(t, index)?;
let idx = cx.host_resource_lower_own(rep, dtor, flags)?;
- Ok(ResourceAny {
- idx,
- ty,
- owned: true,
- })
+ Ok(ResourceAny::new(idx, ty, true))
}
InterfaceType::Borrow(t) => {
let ty = cx.resource_type(t);
let rep = cx.guest_resource_lift_borrow(t, index)?;
let idx = cx.host_resource_lower_borrow(rep)?;
- Ok(ResourceAny {
- idx,
- ty,
- owned: false,
- })
+ Ok(ResourceAny::new(idx, ty, false))
}
_ => bad_type_info(),
}
diff --git a/crates/wasmtime/src/runtime/component/resources/host.rs b/crates/wasmtime/src/runtime/component/resources/host.rs
index 70f43f06f7..b6ffcb1647 100644
--- a/crates/wasmtime/src/runtime/component/resources/host.rs
+++ b/crates/wasmtime/src/runtime/component/resources/host.rs
@@ -291,18 +291,19 @@ where
_marker: _,
} = self;
let store = store.as_context_mut();
-
- let mut tables = HostResourceTables::new_host(store.0)?;
- let (idx, owned) = match state.get() {
- ResourceState::Borrow => (tables.host_resource_lower_borrow(rep)?, false),
+ let idx = match state.get() {
+ // A borrow has no host table entry, so it is carried directly in
+ // the `ResourceAny` and lowered into the guest at call time.
+ ResourceState::Borrow => {
+ return Ok(ResourceAny::new_borrow(rep, T::resource_type(ty)));
+ }
ResourceState::NotInTable => {
- let idx = tables.host_resource_lower_own(rep, None, None)?;
- (idx, true)
+ HostResourceTables::new_host(store.0)?.host_resource_lower_own(rep, None, None)?
}
ResourceState::Taken => bail!("host resource already consumed"),
- ResourceState::Index(idx) => (idx, true),
+ ResourceState::Index(idx) => idx,
};
- Ok(ResourceAny::new(idx, T::resource_type(ty), owned))
+ Ok(ResourceAny::new(idx, T::resource_type(ty), true))
}
}
diff --git a/tests/all/component_model/resources.rs b/tests/all/component_model/resources.rs
index 02b54aeb2f..4006bf91d4 100644
--- a/tests/all/component_model/resources.rs
+++ b/tests/all/component_model/resources.rs
@@ -942,6 +942,7 @@ fn can_use_own_for_borrow() -> Result<()> {
(func (export "f") (param i32)
(call $drop (local.get 0))
)
+ (func (export "forget") (param i32))
)
(core instance $i (instantiate $m
(with "" (instance
@@ -951,6 +952,10 @@ fn can_use_own_for_borrow() -> Result<()> {
(func (export "f") (param "x" (borrow $t))
(canon lift (core func $i "f")))
+ (func (export "forget") (param "x" (borrow $t))
+ (canon lift (core func $i "forget")))
+ (func (export "own") (param "x" (own $t))
+ (canon lift (core func $i "f")))
)
"#,
)?;
@@ -978,11 +983,39 @@ fn can_use_own_for_borrow() -> Result<()> {
f.call(&mut store, &[Val::Resource(resource)], &mut [])?;
resource.resource_drop(&mut store)?;
- // TODO: Enable once https://github.com/bytecodealliance/wasmtime/issues/7793 is fixed
- //let resource =
- // Resource::<MyType>::new_borrow(400).try_into_resource_any(&mut store, &i_pre, ty_idx)?;
- //f.call(&mut store, &[Val::Resource(resource)], &mut [])?;
- //resource.resource_drop(&mut store)?;
+ let resource = Resource::<MyType>::new_borrow(400).try_into_resource_any(&mut store)?;
+ assert!(!resource.owned());
+ assert_eq!(resource.ty(), ResourceType::host::<MyType>());
+
+ struct OtherType;
+ assert!(resource.try_into_resource::<OtherType>(&mut store).is_err());
+
+ let typed = resource.try_into_resource::<MyType>(&mut store)?;
+ assert!(!typed.owned());
+ assert_eq!(typed.rep(), 400);
+ let resource = typed.try_into_resource_any(&mut store)?;
+ f.call(&mut store, &[Val::Resource(resource)], &mut [])?;
+ resource.resource_drop(&mut store)?;
+
+ let mut own_store = Store::new(&engine, ());
+ let own_instance = i_pre.instantiate(&mut own_store)?;
+ let own = own_instance.get_func(&mut own_store, "own").unwrap();
+ let err = own
+ .call(&mut own_store, &[Val::Resource(resource)], &mut [])
+ .unwrap_err();
+ assert!(
+ format!("{err:?}").contains("cannot lower a `borrow` resource into an `own`"),
+ "bad error: {err:?}",
+ );
+
+ let forget = i.get_func(&mut store, "forget").unwrap();
+ let err = forget
+ .call(&mut store, &[Val::Resource(resource)], &mut [])
+ .unwrap_err();
+ assert_eq!(
+ err.to_string(),
+ "borrow handles still remain at the end of the call"
+ );
Ok(())
}
@@ -1579,6 +1612,14 @@ fn resource_dynamic() -> Result<()> {
assert_eq!(r3.rep(), 1);
assert_eq!(r3.ty(), 2);
+ let borrowed = ResourceDynamic::new_borrow(4, 2).try_into_resource_any(&mut store)?;
+ assert!(!borrowed.owned());
+ assert_eq!(borrowed.ty(), ResourceType::host_dynamic(2));
+ let borrowed_again = borrowed.try_into_resource_dynamic(&mut store)?;
+ assert!(!borrowed_again.owned());
+ assert_eq!(borrowed_again.rep(), 4);
+ assert_eq!(borrowed_again.ty(), 2);
+
let c = Component::new(
&engine,
r#"
@@ -1591,6 +1632,8 @@ fn resource_dynamic() -> Result<()> {
(func (export "drop-t") (param "x" (own $t))
(canon lift (core func $t_drop)))
+ (func (export "borrow-t") (param "x" (borrow $t))
+ (canon lift (core func $t_drop)))
(func (export "drop-u") (param "x" (own $u))
(canon lift (core func $u_drop)))
)
@@ -1606,8 +1649,13 @@ fn resource_dynamic() -> Result<()> {
let instance = linker.instantiate(&mut store, &c)?;
let drop_t = instance.get_typed_func::<(ResourceDynamic,), ()>(&mut store, "drop-t")?;
+ let borrow_t = instance.get_func(&mut store, "borrow-t").unwrap();
let drop_u = instance.get_typed_func::<(ResourceDynamic,), ()>(&mut store, "drop-u")?;
+ let borrowed = ResourceDynamic::new_borrow(5, 2).try_into_resource_any(&mut store)?;
+ borrow_t.call(&mut store, &[Val::Resource(borrowed)], &mut [])?;
+ borrowed.resource_drop(&mut store)?;
+
drop_t.call(&mut store, (ResourceDynamic::new_own(1, 2),))?;
drop_u.call(&mut store, (ResourceDynamic::new_own(2, 3),))?;