Skip to content

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

Move handle-table slots into `for_each_vm_type!` (#15153)

Nick Fitzgeraldc4ce4993c5ca
diff --git a/crates/cranelift/src/alias_region.rs b/crates/cranelift/src/alias_region.rs
index 3eefe3ab56..bfe9447536 100644
--- a/crates/cranelift/src/alias_region.rs
+++ b/crates/cranelift/src/alias_region.rs
@@ -90,6 +90,22 @@ enum VmType {
reason = "generated uniformly for all VM types via `for_each_vm_type!`"
)]
VMResourceIndex,
+ VMHandleTableSlot,
+ #[allow(
+ dead_code,
+ reason = "generated uniformly for all VM types via `for_each_vm_type!`"
+ )]
+ VMTypedResource,
+ #[allow(
+ dead_code,
+ reason = "generated uniformly for all VM types via `for_each_vm_type!`"
+ )]
+ VMScope,
+ #[allow(
+ dead_code,
+ reason = "generated uniformly for all VM types via `for_each_vm_type!`"
+ )]
+ VMTransmitLocalState,
}
/// A key that uniquely identifies an alias region across an entire compilation.
@@ -1010,6 +1026,7 @@ macro_rules! define_vm_type_alias_region_helpers {
(@munch_enum $Name:ident $snake:ident $tag:ident { $($vs:tt)* }
$(#[doc = $vdoc:literal])*
#[snake_name = $vsnake:ident]
+ $(#[$vattr:meta])*
$Variant:ident { $($vbody:tt)* } = $disc:literal ,
$($rest:tt)*
) => {
diff --git a/crates/environ/src/vmoffsets.rs b/crates/environ/src/vmoffsets.rs
index 053bb17352..a0e022ca2e 100644
--- a/crates/environ/src/vmoffsets.rs
+++ b/crates/environ/src/vmoffsets.rs
@@ -96,6 +96,11 @@ macro_rules! define_vm_type_offsets {
u32::from(($p).vm_common_stack_information().size())
};
(@size ($p:expr) VMGcHeader) => { u32::from(($p).vm_gc_header().size()) };
+ (@size ($p:expr) VMTypedResource) => { u32::from(($p).vm_typed_resource().size()) };
+ (@size ($p:expr) VMScope) => { u32::from(($p).vm_scope().size()) };
+ (@size ($p:expr) VMTransmitLocalState) => {
+ u32::from(($p).vm_transmit_local_state().size())
+ };
// `VMStackChain` is a `repr(usize, C)` enum, and is not itself defined by
// `for_each_vm_type!`.
(@size ($p:expr) VMStackChain) => { u32::from(($p).size_of_vmstack_chain()) };
@@ -146,6 +151,11 @@ macro_rules! define_vm_type_offsets {
u32::from(($p).vm_common_stack_information().align())
};
(@align ($p:expr) VMGcHeader) => { u32::from(($p).vm_gc_header().align()) };
+ (@align ($p:expr) VMTypedResource) => { u32::from(($p).vm_typed_resource().align()) };
+ (@align ($p:expr) VMScope) => { u32::from(($p).vm_scope().align()) };
+ (@align ($p:expr) VMTransmitLocalState) => {
+ u32::from(($p).vm_transmit_local_state().align())
+ };
(@align ($p:expr) VMStackChain) => { u32::from($p) };
(@align ($p:expr) VMStackState) => { 4u32 };
(@align ($p:expr) VMContinuationStack) => { u32::from($p) };
@@ -281,6 +291,7 @@ macro_rules! define_vm_type_offsets {
(@enum_variants $Name:ident $snake:ident $tag:ident { $($vs:tt)* }
$(#[doc = $vdoc:literal])*
#[snake_name = $vsnake:ident]
+ $(#[$vattr:meta])*
$Variant:ident { $($vbody:tt)* } = $disc:literal ,
$($rest:tt)*
) => {
diff --git a/crates/environ/src/vmtypes.rs b/crates/environ/src/vmtypes.rs
index 038f769b37..5c07e88651 100644
--- a/crates/environ/src/vmtypes.rs
+++ b/crates/environ/src/vmtypes.rs
@@ -83,8 +83,10 @@
///
/// Beyond a struct's rules, each variant needs an explicit discriminant (the
/// tag values compiled code compares against), braces even when empty, and its
-/// own `#[snake_name]` for its generated accessors. Variant fields take a
-/// struct field's marker attributes but no visibility.
+/// own `#[snake_name]` for its generated accessors. Later attributes reach only
+/// the runtime definition; `#[cfg]` is not among them, since a type's layout
+/// must not vary with Cargo features. Variant fields take a struct field's
+/// marker attributes but no visibility.
///
/// An enum gets two alias regions rather than a struct's one per field: one for
/// the tag, one shared by all variants' fields, because they overlap.
@@ -583,6 +585,181 @@ macro_rules! for_each_vm_type {
pub(crate) index: u32,
}
+ /// A resource's type and "rep", as stored in a `VMHandleTableSlot`.
+ #[cfg(feature = "component-model")]
+ #[derive(Debug, Copy, Clone, PartialEq, Eq)]
+ #[repr(C)]
+ #[snake_name = vm_typed_resource]
+ pub struct VMTypedResource {
+ /// The raw `TypeResourceTableIndex`, or `u32::MAX` for a host resource.
+ pub(crate) ty: u32,
+
+ /// An integer whose meaning is up to whoever defined the
+ /// resource.
+ pub(crate) rep: u32,
+ }
+
+ /// A call's resource-borrow scope, as stored in a
+ /// `VMHandleTableSlot::ResourceBorrow`.
+ ///
+ /// Compiled code never reads this; it needs a defined layout only because
+ /// it shares a slot with `lend_count`, which compiled code does read.
+ #[cfg(feature = "component-model")]
+ #[derive(Debug, Copy, Clone, PartialEq, Eq)]
+ #[repr(C)]
+ #[snake_name = vm_scope]
+ pub struct VMScope {
+ /// The kind of scope. See the `id`/`host_id` constructors.
+ pub(crate) kind: u32,
+
+ /// The scope's identifier, whose meaning depends on `kind`.
+ pub(crate) id: u32,
+ }
+
+ /// One instance's view of a stream or future handle, as stored in a
+ /// `VMHandleTableSlot::{Stream,Future}`.
+ ///
+ /// As with `VMScope`, compiled code never reads this; it needs a defined
+ /// layout only because it sits inside a slot.
+ #[cfg(feature = "component-model")]
+ #[derive(Debug, Copy, Clone, PartialEq, Eq)]
+ #[repr(C)]
+ #[snake_name = vm_transmit_local_state]
+ pub struct VMTransmitLocalState {
+ /// The kind of transmit state. See the `VMTransmitLocalState`
+ /// constructors for details.
+ pub(crate) kind: u32,
+
+ /// Meaningful only for the `Write` and `Read` kinds.
+ pub(crate) done: u32,
+ }
+
+ /// One slot of a component instance's `HandleTable`.
+ ///
+ /// An instance keeps every kind of handle in one heterogeneous table, so a
+ /// slot is a tagged union over all of them, with free slots threaded into a
+ /// free list through `Free::next`.
+ ///
+ /// The layout is guaranteed because compiled adapter code reads and writes
+ /// slots directly; see `transfer_borrow_to_owner`.
+ #[cfg(feature = "component-model")]
+ #[derive(Debug)]
+ #[repr(C, u32)]
+ #[snake_name = vm_handle_table_slot]
+ pub enum VMHandleTableSlot {
+ /// An unallocated slot, holding the index of the next free slot.
+ #[snake_name = free]
+ Free {
+ /// The next free slot's index, forming a free list.
+ next: u32,
+ } = 0,
+
+ /// An owned resource handle.
+ #[snake_name = resource_own]
+ ResourceOwn {
+ /// The resource's type and representation.
+ #[aggregate]
+ resource: VMTypedResource,
+
+ /// How many times this handle has been lent out as a `borrow`.
+ ///
+ /// A handle with a nonzero lend count cannot be removed.
+ ///
+ /// NB: must be at the same offset as `Self::ResourceBorrow::lend_count`.
+ lend_count: u32,
+ } = 1,
+
+ /// A borrowed resource handle, connected to the call scope it was
+ /// created within.
+ ///
+ /// Dropping the borrow decrements that scope's borrow count.
+ #[snake_name = resource_borrow]
+ ResourceBorrow {
+ /// The resource's type and representation.
+ #[aggregate]
+ resource: VMTypedResource,
+
+ /// How many times this borrow has been lent out to other calls.
+ ///
+ /// NB: must be at the same offset as `Self::ResourceOwn::lend_count`.
+ lend_count: u32,
+
+ /// The scope this borrow was created within.
+ #[aggregate]
+ scope: VMScope,
+ } = 2,
+
+ /// A host task handle.
+ #[snake_name = host_task]
+ HostTask {
+ /// The task's representation.
+ rep: u32,
+ } = 3,
+
+ /// A guest task handle.
+ #[snake_name = guest_task]
+ GuestTask {
+ /// The task's representation.
+ rep: u32,
+ } = 4,
+
+ /// A guest thread handle.
+ #[snake_name = guest_thread]
+ #[cfg_attr(
+ not(feature = "component-model-async"),
+ allow(
+ dead_code,
+ reason = "slot layout should not vary with cargo features"
+ )
+ )]
+ GuestThread {
+ /// The thread's representation.
+ rep: u32,
+ } = 5,
+
+ /// A stream handle.
+ #[snake_name = stream]
+ Stream {
+ /// The stream's `TypeStreamTableIndex`.
+ ty: u32,
+
+ /// The stream's representation.
+ rep: u32,
+
+ /// This instance's view of the stream's state.
+ #[aggregate]
+ state: VMTransmitLocalState,
+ } = 6,
+
+ /// A future handle.
+ #[snake_name = future]
+ Future {
+ /// The future's `TypeFutureTableIndex`.
+ ty: u32,
+
+ /// The future's representation.
+ rep: u32,
+
+ /// This instance's view of the future's state.
+ #[aggregate]
+ state: VMTransmitLocalState,
+ } = 7,
+
+ /// A waitable-set handle.
+ #[snake_name = waitable_set]
+ WaitableSet {
+ /// The waitable set's representation.
+ rep: u32,
+ } = 8,
+
+ /// An error-context handle.
+ #[snake_name = error_context]
+ ErrorContext {
+ /// The error context's representation.
+ rep: u32,
+ } = 9,
+ }
+
/// A deferred component-model thread.
///
/// This is an on-stack record pushed by a fused sync-to-sync adapter's fast
diff --git a/crates/wasmtime/src/runtime/component/concurrent.rs b/crates/wasmtime/src/runtime/component/concurrent.rs
index bb20bb8139..1f00109550 100644
--- a/crates/wasmtime/src/runtime/component/concurrent.rs
+++ b/crates/wasmtime/src/runtime/component/concurrent.rs
@@ -64,8 +64,10 @@ use crate::prelude::*;
use crate::store::{Store, StoreId, StoreInner, StoreOpaque, StoreToken};
#[cfg(feature = "gc")]
use crate::vm::GcRootsList;
+use crate::vm::VMScope;
+use crate::vm::component::ScopeKind;
use crate::vm::component::{
- CallContext, ComponentInstance, CurrentScope, HandleCounter, InstanceState, Scope,
+ CallContext, ComponentInstance, CurrentScope, HandleCounter, InstanceState,
};
use crate::vm::{
AlwaysMut, SendSyncPtr, UncaughtException, VMFuncRef, VMLazyThread, VMMemoryDefinition, VMStore,
@@ -1968,7 +1970,7 @@ impl StoreOpaque {
.unforced_current_thread
.guest_task()
{
- Some(task) => Scope::Id(task.rep()),
+ Some(task) => VMScope::id(task.rep()),
None => bail_bug!("replayed deferred frame is not a guest thread"),
};
let instance = Instance::from_wasmtime(self, id);
@@ -2765,12 +2767,12 @@ impl StoreOpaque {
if !self.concurrency_support() {
return Ok(self
.current_scope_id_not_concurrent()?
- .map(|id| CurrentScope::Id(Scope::Id(id))));
+ .map(|id| CurrentScope::Id(VMScope::id(id))));
}
Ok(match self.current_thread()? {
- CurrentThread::Guest(id) => Some(CurrentScope::Id(Scope::Id(id.task.rep()))),
- CurrentThread::Host(id) => Some(CurrentScope::Id(Scope::HostId(id.rep()))),
+ CurrentThread::Guest(id) => Some(CurrentScope::Id(VMScope::id(id.task.rep()))),
+ CurrentThread::Host(id) => Some(CurrentScope::Id(VMScope::host_id(id.rep()))),
CurrentThread::DeferredHost(_) => Some(CurrentScope::DeferredHost),
CurrentThread::None => return Ok(None),
})
@@ -6605,13 +6607,13 @@ impl ConcurrentState {
/// Used by `ResourceTables` to acquire the current `CallContext` for the
/// specified task.
- pub fn call_context(&mut self, task: Scope) -> Result<&mut CallContext> {
- match task {
- Scope::HostId(task) => {
+ pub fn call_context(&mut self, task: VMScope) -> Result<&mut CallContext> {
+ match task.kind() {
+ ScopeKind::HostId(task) => {
let task: TableId<HostTask> = TableId::new(task);
Ok(&mut self.get_mut(task)?.call_context)
}
- Scope::Id(task) => {
+ ScopeKind::Id(task) => {
let task: TableId<GuestTask> = TableId::new(task);
Ok(&mut self.get_mut(task)?.call_context)
}
@@ -6679,9 +6681,9 @@ impl ConcurrentState {
}
}
- pub(crate) fn materialize_current_scope(&mut self) -> Result<Scope> {
+ pub(crate) fn materialize_current_scope(&mut self) -> Result<VMScope> {
match self.materialize_host_task()? {
- CurrentThread::Host(id) => Ok(Scope::HostId(id.rep())),
+ CurrentThread::Host(id) => Ok(VMScope::host_id(id.rep())),
_ => bail_bug!("current scope is not a deferred host scope"),
}
}
diff --git a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs
index 1d7875ecf1..53de7adcc6 100644
--- a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs
+++ b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs
@@ -13,7 +13,9 @@ use crate::component::{
use crate::prelude::*;
use crate::store::{StoreOpaque, StoreToken};
use crate::try_mutex::{TryMutex, TryMutexGuard};
-use crate::vm::component::{ComponentInstance, HandleCounter, HandleTable, TransmitLocalState};
+use crate::vm::VMTransmitLocalState;
+use crate::vm::component::TransmitLocalStateKind;
+use crate::vm::component::{ComponentInstance, HandleCounter, HandleTable};
use crate::vm::{AlwaysMut, VMStore};
use crate::{AsContext, AsContextMut, StoreContextMut, ValRaw};
use crate::{
@@ -230,7 +232,7 @@ fn get_mut_by_index_from(
handle_table: &mut HandleTable,
ty: TransmitIndex,
index: u32,
-) -> Result<(u32, &mut TransmitLocalState)> {
+) -> Result<(u32, &mut VMTransmitLocalState)> {
match ty {
TransmitIndex::Stream(ty) => handle_table.stream_rep(ty, index),
TransmitIndex::Future(ty) => handle_table.future_rep(ty, index),
@@ -3608,11 +3610,11 @@ impl Instance {
self.check_bounds(store.0, options, ty, address, count.as_usize())?;
let async_ = self.options(store.0, options).async_;
let (rep, state) = self.id().get_mut(store.0).get_mut_by_index(ty, handle)?;
- let TransmitLocalState::Write { done } = *state else {
+ let TransmitLocalStateKind::Write { done } = state.kind() else {
bail!(Trap::ConcurrentFutureStreamOp);
};
- *state = TransmitLocalState::Busy;
+ *state = VMTransmitLocalState::busy();
let transmit_handle = TableId::<TransmitHandle>::new(rep);
let concurrent_state = store.0.concurrent_state_mut()?;
let transmit_id = concurrent_state.get_mut(transmit_handle)?.state;
@@ -3837,9 +3839,7 @@ impl Instance {
if result != ReturnCode::Blocked {
*self.id().get_mut(store.0).get_mut_by_index(ty, handle)?.1 =
- TransmitLocalState::Write {
- done: matches!(result, ReturnCode::Dropped(_)),
- };
+ VMTransmitLocalState::write(matches!(result, ReturnCode::Dropped(_)));
}
log::trace!(
@@ -3867,11 +3867,11 @@ impl Instance {
self.check_bounds(store.0, options, ty, address, count.as_usize())?;
let async_ = self.options(store.0, options).async_;
let (rep, state) = self.id().get_mut(store.0).get_mut_by_index(ty, handle)?;
- let TransmitLocalState::Read { done } = *state else {
+ let TransmitLocalStateKind::Read { done } = state.kind() else {
bail!(Trap::ConcurrentFutureStreamOp);
};
- *state = TransmitLocalState::Busy;
+ *state = VMTransmitLocalState::busy();
let transmit_handle = TableId::<TransmitHandle>::new(rep);
let concurrent_state = store.0.concurrent_state_mut()?;
let transmit_id = concurrent_state.get_mut(transmit_handle)?.state;
@@ -4083,12 +4083,10 @@ impl Instance {
if result != ReturnCode::Blocked {
*self.id().get_mut(store.0).get_mut_by_index(ty, handle)?.1 =
- TransmitLocalState::Read {
- done: matches!(
- (result, ty),
- (ReturnCode::Dropped(_), TransmitIndex::Stream(_))
- ),
- };
+ VMTransmitLocalState::read(matches!(
+ (result, ty),
+ (ReturnCode::Dropped(_), TransmitIndex::Stream(_))
+ ));
}
log::trace!(
@@ -4302,27 +4300,27 @@ impl Instance {
get_mut_by_index_from(self.id().get_mut(store).table_for_transmit(ty), ty, writer)?;
let id = TableId::<TransmitHandle>::new(rep);
log::trace!("guest cancel write {id:?} (handle {writer})");
- match state {
- TransmitLocalState::Write { .. } => {
+ match state.kind() {
+ TransmitLocalStateKind::Write { .. } => {
bail!("stream or future write cancelled when no write is pending")
}
- TransmitLocalState::Read { .. } => {
+ TransmitLocalStateKind::Read { .. } => {
bail!("passed read end to `{{stream|future}}.cancel-write`")
}
- TransmitLocalState::Cancelling => {
+ TransmitLocalStateKind::Cancelling => {
bail!("stream or future write cancelled when a cancellation is already pending")
}
- TransmitLocalState::Busy => {}
+ TransmitLocalStateKind::Busy => {}
}
let transmit_id = store.concurrent_state_mut()?.get_mut(id)?.state;
let code = self.cancel_write(store, caller, transmit_id, async_)?;
let state =
get_mut_by_index_from(self.id().get_mut(store).table_for_transmit(ty), ty, writer)?.1;
- if let TransmitLocalState::Busy = state {
+ if let TransmitLocalStateKind::Busy = state.kind() {
*state = if matches!(code, ReturnCode::Blocked) {
- TransmitLocalState::Cancelling
+ VMTransmitLocalState::cancelling()
} else {
- TransmitLocalState::Write { done: false }
+ VMTransmitLocalState::write(false)
};
}
Ok(code)
@@ -4341,27 +4339,27 @@ impl Instance {
get_mut_by_index_from(self.id().get_mut(store).table_for_transmit(ty), ty, reader)?;
let id = TableId::<TransmitHandle>::new(rep);
log::trace!("guest cancel read {id:?} (handle {reader})");
- match state {
- TransmitLocalState::Read { .. } => {
+ match state.kind() {
+ TransmitLocalStateKind::Read { .. } => {
bail!("stream or future read cancelled when no read is pending")
}
- TransmitLocalState::Write { .. } => {
+ TransmitLocalStateKind::Write { .. } => {
bail!("passed write end to `{{stream|future}}.cancel-read`")
}
- TransmitLocalState::Cancelling => {
+ TransmitLocalStateKind::Cancelling => {
bail!("stream or future read cancelled when a cancellation is already pending")
}
- TransmitLocalState::Busy => {}
+ TransmitLocalStateKind::Busy => {}
}
let transmit_id = store.concurrent_state_mut()?.get_mut(id)?.state;
let code = self.cancel_read(store, caller, transmit_id, async_)?;
let state =
get_mut_by_index_from(self.id().get_mut(store).table_for_transmit(ty), ty, reader)?.1;
- if let TransmitLocalState::Busy = state {
+ if let TransmitLocalStateKind::Busy = state.kind() {
*state = if matches!(code, ReturnCode::Blocked) {
- TransmitLocalState::Cancelling
+ VMTransmitLocalState::cancelling()
} else {
- TransmitLocalState::Read { done: false }
+ VMTransmitLocalState::read(false)
};
}
Ok(code)
@@ -4823,7 +4821,7 @@ impl ComponentInstance {
self: Pin<&mut Self>,
ty: TransmitIndex,
index: u32,
- ) -> Result<(u32, &mut TransmitLocalState)> {
+ ) -> Result<(u32, &mut VMTransmitLocalState)> {
get_mut_by_index_from(self.table_for_transmit(ty), ty, index)
}
}
@@ -5055,18 +5053,16 @@ impl Waitable {
bail_bug!("unexpected rep mismatch");
}
if !matches!(
- *state,
- TransmitLocalState::Busy | TransmitLocalState::Cancelling
+ state.kind(),
+ TransmitLocalStateKind::Busy | TransmitLocalStateKind::Cancelling
) {
bail_bug!("expected state to be busy or cancelling");
}
let done = matches!(code, ReturnCode::Dropped(_));
*state = match event {
- Event::FutureRead { .. } | Event::StreamRead { .. } => {
- TransmitLocalState::Read { done }
- }
+ Event::FutureRead { .. } | Event::StreamRead { .. } => VMTransmitLocalState::read(done),
Event::FutureWrite { .. } | Event::StreamWrite { .. } => {
- TransmitLocalState::Write { done }
+ VMTransmitLocalState::write(done)
}
_ => bail_bug!("unexpected event for stream"),
};
diff --git a/crates/wasmtime/src/runtime/component/concurrent_disabled.rs b/crates/wasmtime/src/runtime/component/concurrent_disabled.rs
index eb10fb0d54..86f132309c 100644
--- a/crates/wasmtime/src/runtime/component/concurrent_disabled.rs
+++ b/crates/wasmtime/src/runtime/component/concurrent_disabled.rs
@@ -2,7 +2,8 @@ use crate::component::func::{LiftContext, LowerContext};
use crate::component::matching::InstanceType;
use crate::component::{ComponentType, Lift, Lower, RuntimeInstance, Val};
use crate::store::StoreOpaque;
-use crate::vm::component::{CurrentScope, Scope};
+use crate::vm::VMScope;
+use crate::vm::component::CurrentScope;
use crate::{Result, bail, error::format_err};
use core::convert::Infallible;
use core::mem::MaybeUninit;
@@ -184,6 +185,6 @@ impl StoreOpaque {
pub(crate) fn current_scope(&mut self) -> Result<Option<CurrentScope>> {
Ok(self
.current_scope_id_not_concurrent()?
- .map(|id| CurrentScope::Id(Scope::Id(id))))
+ .map(|id| CurrentScope::Id(VMScope::id(id))))
}
}
diff --git a/crates/wasmtime/src/runtime/component/instance.rs b/crates/wasmtime/src/runtime/component/instance.rs
index eb86939d2c..9d7bdebfb3 100644
--- a/crates/wasmtime/src/runtime/component/instance.rs
+++ b/crates/wasmtime/src/runtime/component/instance.rs
@@ -9,7 +9,8 @@ use crate::component::{ExportLookup, RuntimeInstance};
use crate::instance::OwnedImports;
use crate::linker::DefinitionType;
use crate::prelude::*;
-use crate::runtime::vm::component::{ComponentInstance, TypedResource, TypedResourceIndex};
+use crate::runtime::vm::VMTypedResource;
+use crate::runtime::vm::component::{ComponentInstance, TypedResourceIndex};
use crate::runtime::vm::{self, UncaughtException, VMFuncRef, VMStore};
use crate::store::{AsStoreOpaque, Asyncness, StoreOpaque};
use crate::{AsContext, AsContextMut, Engine, Module, StoreContextMut};
@@ -382,7 +383,7 @@ impl Instance {
) -> Result<u32> {
store
.component_resource_tables(Some(self))?
- .resource_new(TypedResource::Component { ty, rep })
+ .resource_new(VMTypedResource::component(ty, rep))
}
/// Implementation of the `resource.rep` intrinsic for `i32`
@@ -419,7 +420,7 @@ impl Instance {
) -> Result<u32> {
let mut tables = store.component_resource_tables(Some(self))?;
let rep = tables.resource_lift_own(TypedResourceIndex::Component { ty: src, index })?;
- tables.resource_lower_own(TypedResource::Component { ty: dst, rep })
+ tables.resource_lower_own(VMTypedResource::component(dst, rep))
}
pub(crate) fn resource_transfer_borrow(
@@ -438,7 +439,7 @@ impl Instance {
// forced anyway.
let mut tables = store.component_resource_tables(Some(self))?;
let rep = tables.resource_lift_borrow(TypedResourceIndex::Component { ty: src, index })?;
- tables.resource_lower_borrow(TypedResource::Component { ty: dst, rep })
+ tables.resource_lower_borrow(VMTypedResource::component(dst, rep))
}
/// Variant of `resource_transfer_borrow` for when the destination instance
diff --git a/crates/wasmtime/src/runtime/component/resources/host_tables.rs b/crates/wasmtime/src/runtime/component/resources/host_tables.rs
index f120397b4c..c562734223 100644
--- a/crates/wasmtime/src/runtime/component/resources/host_tables.rs
+++ b/crates/wasmtime/src/runtime/component/resources/host_tables.rs
@@ -22,7 +22,8 @@
use crate::prelude::*;
use crate::runtime::component::RuntimeInstance;
-use crate::runtime::vm::component::{ResourceTables, TypedResource, TypedResourceIndex};
+use crate::runtime::vm::VMTypedResource;
+use crate::runtime::vm::component::{ResourceTables, TypedResourceIndex};
use crate::runtime::vm::{SendSyncPtr, VMFuncRef};
use crate::store::StoreOpaque;
use core::ptr::NonNull;
@@ -151,7 +152,7 @@ impl<'a> HostResourceTables<'a> {
dtor: Option<NonNull<VMFuncRef>>,
instance: Option<RuntimeInstance>,
) -> Result<HostResourceIndex> {
- let idx = self.tables.resource_lower_own(TypedResource::Host(rep))?;
+ let idx = self.tables.resource_lower_own(VMTypedResource::host(rep))?;
self.new_host_index(idx, dtor, instance)
}
@@ -159,7 +160,7 @@ impl<'a> HostResourceTables<'a> {
pub fn host_resource_lower_borrow(&mut self, rep: u32) -> Result<HostResourceIndex> {
let idx = self
.tables
- .resource_lower_borrow(TypedResource::Host(rep))?;
+ .resource_lower_borrow(VMTypedResource::host(rep))?;
self.new_host_index(idx, None, None)
}
@@ -276,7 +277,7 @@ impl<'a> HostResourceTables<'a> {
ty: TypeResourceTableIndex,
) -> Result<u32> {
self.tables
- .resource_lower_own(TypedResource::Component { ty, rep })
+ .resource_lower_own(VMTypedResource::component(ty, rep))
}
/// Lowers a `borrow` resource into the guest, converting the `rep`
@@ -294,7 +295,7 @@ impl<'a> HostResourceTables<'a> {
ty: TypeResourceTableIndex,
) -> Result<u32> {
self.tables
- .resource_lower_borrow(TypedResource::Component { ty, rep })
+ .resource_lower_borrow(VMTypedResource::component(ty, rep))
}
/// Lifts an `own` resource from the `idx` specified from the table `ty`.
diff --git a/crates/wasmtime/src/runtime/component/store.rs b/crates/wasmtime/src/runtime/component/store.rs
index 81b5a279c6..34ea6b9e4a 100644
--- a/crates/wasmtime/src/runtime/component/store.rs
+++ b/crates/wasmtime/src/runtime/component/store.rs
@@ -3,9 +3,11 @@ use crate::runtime::component::{HostResourceData, Instance};
use crate::runtime::vm;
use crate::runtime::vm::VMResourceIndex;
use crate::runtime::vm::component::{
- CallContext, ComponentInstance, HandleCounter, HandleTable, OwnedComponentInstance, Scope,
+ CallContext, ComponentInstance, HandleCounter, HandleTable, OwnedComponentInstance,
};
use crate::store::{StoreData, StoreId, StoreOpaque};
+use crate::vm::VMScope;
+use crate::vm::component::ScopeKind;
use crate::{AsContext, AsContextMut, Engine, Store, StoreContextMut, bail_bug};
use core::pin::Pin;
use wasmtime_environ::LAZY_LENDERS_CAPACITY;
@@ -580,7 +582,7 @@ impl StoreOpaque {
pub(crate) fn promote_lazy_lends(
&mut self,
instance: Instance,
- scope: vm::component::Scope,
+ scope: vm::VMScope,
start: usize,
end: usize,
) -> Result<()> {
@@ -779,18 +781,18 @@ pub struct ComponentTasksNotConcurrent {
}
impl ComponentTaskState {
- pub fn call_context(&mut self, id: Scope) -> Result<&mut CallContext> {
+ pub fn call_context(&mut self, id: VMScope) -> Result<&mut CallContext> {
match self {
- ComponentTaskState::NotConcurrent(state) => match id {
- Scope::Id(id) => Ok(&mut state.scopes[id as usize]),
- Scope::HostId(_) => bail_bug!("non-concurrent scope cannot be a host ID"),
+ ComponentTaskState::NotConcurrent(state) => match id.kind() {
+ ScopeKind::Id(id) => Ok(&mut state.scopes[id as usize]),
+ ScopeKind::HostId(_) => bail_bug!("non-concurrent scope cannot be a host ID"),
},
#[cfg(feature = "component-model-async")]
ComponentTaskState::Concurrent(state) => state.call_context(id),
}
}
- pub(crate) fn materialize_current_scope(&mut self) -> Result<Scope> {
+ pub(crate) fn materialize_current_scope(&mut self) -> Result<VMScope> {
match self {
ComponentTaskState::NotConcurrent(_) => {
bail_bug!("a non-concurrent scope cannot be deferred")
diff --git a/crates/wasmtime/src/runtime/vm.rs b/crates/wasmtime/src/runtime/vm.rs
index 9ca03dcfc4..fef015a565 100644
--- a/crates/wasmtime/src/runtime/vm.rs
+++ b/crates/wasmtime/src/runtime/vm.rs
@@ -124,28 +124,7 @@ pub use crate::runtime::vm::table::{Table, TableElementType};
#[cfg(feature = "gc")]
pub use crate::runtime::vm::throw::*;
pub use crate::runtime::vm::traphandlers::*;
-#[cfg(feature = "component-model")]
-pub use crate::runtime::vm::vmcontext::VMArrayCallFunction;
-#[cfg(feature = "gc-copying")]
-pub(crate) use crate::runtime::vm::vmcontext::VMCopyingHeader;
-#[cfg(feature = "gc-copying")]
-pub use crate::runtime::vm::vmcontext::VMCopyingHeapData;
-#[cfg(feature = "gc-drc")]
-pub(crate) use crate::runtime::vm::vmcontext::VMDrcHeader;
-#[cfg(feature = "gc-drc")]
-pub use crate::runtime::vm::vmcontext::VMDrcHeapData;
-#[cfg(feature = "component-model-async")]
-pub use crate::runtime::vm::vmcontext::VMLazyThread;
-#[cfg(feature = "gc-null")]
-pub use crate::runtime::vm::vmcontext::VMNullHeapData;
-#[cfg(feature = "component-model")]
-pub use crate::runtime::vm::vmcontext::VMResourceIndex;
-pub use crate::runtime::vm::vmcontext::{
- VMArrayCallHostFuncContext, VMCommonStackInformation, VMContRef, VMContext, VMFuncRef,
- VMFunctionImport, VMGlobalDefinition, VMGlobalImport, VMGlobalKind, VMHostArray,
- VMMemoryDefinition, VMMemoryImport, VMOpaqueContext, VMPayloads, VMStackLimits, VMStoreContext,
- VMTableImport, VMTagImport, VMWasmCallFunction, ValRaw,
-};
+pub use crate::runtime::vm::vmcontext::*;
#[cfg(has_custom_sync)]
pub(crate) use sys::capi;
diff --git a/crates/wasmtime/src/runtime/vm/component.rs b/crates/wasmtime/src/runtime/vm/component.rs
index e21b11421b..b02f06c68a 100644
--- a/crates/wasmtime/src/runtime/vm/component.rs
+++ b/crates/wasmtime/src/runtime/vm/component.rs
@@ -6,7 +6,6 @@
//! Eventually it's intended that module-to-module calls, which would be
//! cranelift-compiled adapters, will use this `VMComponentContext` as well.
-use crate::Result;
use crate::component::{Component, Instance, InstancePre, ResourceType, RuntimeImport};
use crate::module::ModuleRegistry;
#[cfg(feature = "component-model-async")]
@@ -17,11 +16,12 @@ use crate::runtime::vm::vmcontext::VMFunctionBody;
use crate::runtime::vm::vmcontext::VMLazyThread;
use crate::runtime::vm::{
HostResult, SendSyncPtr, UncaughtException, VMArrayCallFunction, VMFuncRef, VMGlobalDefinition,
- VMMemoryDefinition, VMOpaqueContext, VMResourceIndex, VMStore, VMStoreContext, VMStoreRawPtr,
- VMTableImport, VMWasmCallFunction, ValRaw, VmPtr, VmSafe, catch_unwind_and_record_trap,
+ VMMemoryDefinition, VMOpaqueContext, VMResourceIndex, VMScope, VMStore, VMStoreContext,
+ VMStoreRawPtr, VMTableImport, VMTransmitLocalState, VMTypedResource, VMWasmCallFunction,
+ ValRaw, VmPtr, VmSafe, catch_unwind_and_record_trap,
};
use crate::store::InstanceId;
-use crate::{Func, vm};
+use crate::{Func, Result, bail, vm};
use alloc::alloc::Layout;
use alloc::sync::Arc;
use core::mem;
@@ -47,10 +47,9 @@ mod resources;
pub use self::handle_table::{HandleCounter, HandleTable, RemovedResource};
#[cfg(feature = "component-model-async")]
-pub use self::handle_table::{ThreadHandleTable, TransmitLocalState, Waitable};
-pub use self::resources::{
- CallContext, CurrentScope, ResourceTables, Scope, TypedResource, TypedResourceIndex,
-};
+pub use self::handle_table::{ThreadHandleTable, Waitable};
+pub(crate) use self::resources::ResourceTypeMismatch;
+pub use self::resources::{CallContext, CurrentScope, ResourceTables, TypedResourceIndex};
impl VMResourceIndex {
/// The `ty` of a host resource, which is otherwise never a valid
@@ -92,6 +91,207 @@ impl VMResourceIndex {
}
}
+// SAFETY: `VMTypedResource` is generated with `#[repr(C)]` and holds only
+// `u32`s.
+unsafe impl VmSafe for VMTypedResource {}
+
+impl VMTypedResource {
+ /// The `ty` of a host resource, which is otherwise never a valid
+ /// `TypeResourceTableIndex`.
+ const HOST: u32 = u32::MAX;
+
+ /// A resource defined by the host.
+ pub(crate) fn host(rep: u32) -> VMTypedResource {
+ VMTypedResource {
+ ty: Self::HOST,
+ rep,
+ }
+ }
+
+ /// A resource of type `ty` defined within a component.
+ pub(crate) fn component(ty: TypeResourceTableIndex, rep: u32) -> VMTypedResource {
+ let ty = ty.as_u32();
+ debug_assert_ne!(ty, Self::HOST);
+ VMTypedResource { ty, rep }
+ }
+
+ /// This resource's `TypeResourceTableIndex`, or `None` if it is a host
+ /// resource.
+ pub(crate) fn component_ty(&self) -> Option<TypeResourceTableIndex> {
+ match self.ty {
+ Self::HOST => None,
+ ty => Some(TypeResourceTableIndex::from_u32(ty)),
+ }
+ }
+
+ /// This resource's "rep", if it is being accessed with a matching type.
+ ///
+ /// A host resource may only be accessed as a host resource, and a
+ /// component-defined resource only as the same component-defined type that
+ /// it was created with; anything else is the guest using a handle it does
+ /// not have permission to.
+ pub(crate) fn rep(&self, access_ty: &TypedResourceIndex) -> Result<u32> {
+ match (self.component_ty(), access_ty) {
+ (None, TypedResourceIndex::Host(_)) => Ok(self.rep),
+ (None, expected) => bail!(ResourceTypeMismatch {
+ expected: *expected,
+ found: "host resource",
+ }),
+ (Some(ty), TypedResourceIndex::Component { ty: expected, .. }) => {
+ if ty == *expected {
+ Ok(self.rep)
+ } else {
+ bail!(ResourceTypeMismatch {
+ expected: *access_ty,
+ found: "a different guest-defined resource",
+ })
+ }
+ }
+ (Some(_), expected) => bail!(ResourceTypeMismatch {
+ expected: *expected,
+ found: "guest-defined resource",
+ }),
+ }
+ }
+}
+
+// SAFETY: `VMScope` is generated with `#[repr(C)]` and holds only `u32`s.
+unsafe impl VmSafe for VMScope {}
+
+impl VMScope {
+ /// The `kind` of a non-concurrent or concurrent guest-task scope.
+ const KIND_ID: u32 = 0;
+
+ /// The `kind` of a concurrent host-task scope.
+ const KIND_HOST_ID: u32 = 1;
+
+ /// A non-concurrent scope, or a concurrent guest-task scope.
+ pub(crate) fn id(id: u32) -> VMScope {
+ VMScope {
+ kind: Self::KIND_ID,
+ id,
+ }
+ }
+
+ /// A concurrent host-task scope.
+ #[cfg_attr(not(feature = "component-model-async"), allow(dead_code))]
+ pub(crate) fn host_id(id: u32) -> VMScope {
+ VMScope {
+ kind: Self::KIND_HOST_ID,
+ id,
+ }
+ }
+
+ /// Which kind of scope this is, and its identifier.
+ pub(crate) fn kind(&self) -> ScopeKind {
+ match self.kind {
+ Self::KIND_ID => ScopeKind::Id(self.id),
+ Self::KIND_HOST_ID => ScopeKind::HostId(self.id),
+ kind => unreachable!("invalid `VMScope` kind {kind}"),
+ }
+ }
+}
+
+/// The result of matching on a [`VMScope`].
+#[derive(Debug, Copy, Clone, Eq, PartialEq)]
+pub enum ScopeKind {
+ /// A non-concurrent scope or a concurrent guest-task scope.
+ Id(u32),
+ /// A concurrent host-task scope.
+ HostId(u32),
+}
+
+// SAFETY: `VMTransmitLocalState` is generated with `#[repr(C)]` and holds only
+// `u32`s.
+unsafe impl VmSafe for VMTransmitLocalState {}
+
+impl VMTransmitLocalState {
+ /// The `kind` of the write end of a stream or future.
+ const KIND_WRITE: u32 = 0;
+
+ /// The `kind` of the read end of a stream or future.
+ const KIND_READ: u32 = 1;
+
+ /// The `kind` of an end with a read or write in progress.
+ const KIND_BUSY: u32 = 2;
+
+ /// The `kind` of an end whose in-progress read or write is being cancelled.
+ const KIND_CANCELLING: u32 = 3;
+
+ /// The write end of a stream or future.
+ pub fn write(done: bool) -> VMTransmitLocalState {
+ VMTransmitLocalState {
+ kind: Self::KIND_WRITE,
+ done: done.into(),
+ }
+ }
+
+ /// The read end of a stream or future.
+ pub fn read(done: bool) -> VMTransmitLocalState {
+ VMTransmitLocalState {
+ kind: Self::KIND_READ,
+ done: done.into(),
+ }
+ }
+
+ /// A read or write is in progress.
+ pub fn busy() -> VMTransmitLocalState {
+ VMTransmitLocalState {
+ kind: Self::KIND_BUSY,
+ done: 0,
+ }
+ }
+
+ /// A read or write is in progress and an asynchronous cancellation of it
+ /// has been requested but has not yet completed.
+ pub fn cancelling() -> VMTransmitLocalState {
+ VMTransmitLocalState {
+ kind: Self::KIND_CANCELLING,
+ done: 0,
+ }
+ }
+
+ /// Which kind of state this is, along with its `done` flag where that is
+ /// meaningful.
+ pub fn kind(&self) -> TransmitLocalStateKind {
+ match self.kind {
+ Self::KIND_WRITE => TransmitLocalStateKind::Write {
+ done: self.done != 0,
+ },
+ Self::KIND_READ => TransmitLocalStateKind::Read {
+ done: self.done != 0,
+ },
+ Self::KIND_BUSY => TransmitLocalStateKind::Busy,
+ Self::KIND_CANCELLING => TransmitLocalStateKind::Cancelling,
+ kind => unreachable!("invalid `VMTransmitLocalState` kind {kind}"),
+ }
+ }
+}
+
+/// The result of matching on a [`VMTransmitLocalState`].
+#[derive(Debug, Copy, Clone, Eq, PartialEq)]
+pub enum TransmitLocalStateKind {
+ /// The write end of the stream or future.
+ Write {
+ /// Whether the component instance has been notified that the stream or
+ /// future is "done" (i.e. the other end has dropped, or, in the case of
+ /// a future, a value has been transmitted).
+ done: bool,
+ },
+ /// The read end of the stream or future.
+ Read {
+ /// Whether the component instance has been notified that the stream or
+ /// future is "done" (i.e. the other end has dropped, or, in the case of
+ /// a future, a value has been transmitted).
+ done: bool,
+ },
+ /// A read or write is in progress.
+ Busy,
+ /// A read or write is in progress and an asynchronous cancellation of it
+ /// has been requested but has not yet completed.
+ Cancelling,
+}
+
impl VMStoreContext {
pub(crate) fn current_thread_mut(&mut self) -> &mut VMLazyThread {
self.current_thread.get_mut()
diff --git a/crates/wasmtime/src/runtime/vm/component/handle_table.rs b/crates/wasmtime/src/runtime/vm/component/handle_table.rs
index 491aad97f9..2e1bab7bd2 100644
--- a/crates/wasmtime/src/runtime/vm/component/handle_table.rs
+++ b/crates/wasmtime/src/runtime/vm/component/handle_table.rs
@@ -1,9 +1,39 @@
-use super::{Scope, TypedResource, TypedResourceIndex};
+use super::{TransmitLocalStateKind, TypedResourceIndex};
use crate::prelude::TryVec;
+use crate::vm::{VMHandleTableSlot, VMScope, VMTransmitLocalState, VMTypedResource, VmSafe};
use crate::{Result, bail};
use core::mem;
use wasmtime_environ::component::{TypeFutureTableIndex, TypeStreamTableIndex};
+// SAFETY: `VMHandleTableSlot` is generated with `#[repr(C, u32)]` and every
+// variant holds only `u32`s and `VmSafe` structs of them.
+unsafe impl VmSafe for VMHandleTableSlot {}
+
+#[cfg(test)]
+mod test_vm_handle_table_slot {
+ use wasmtime_environ::PtrSize;
+
+ /// A lent resource's `lend_count` must sit at the same offset whether the
+ /// handle is an `own` or a `borrow`, because compiled code adjusts it
+ /// without first branching on the slot's tag.
+ ///
+ /// A field inserted ahead of `lend_count` in only one of the two variants
+ /// would silently break that, which is what this guards against.
+ ///
+ /// Every field offset here is itself checked against the real Rust layout
+ /// by the generated `test_vm_type_layouts::vm_handle_table_slot`.
+ #[test]
+ fn lend_count_offset_is_variant_agnostic() {
+ for ptr in [4u8, 8] {
+ let slot = ptr.vm_handle_table_slot();
+ assert_eq!(
+ slot.resource_own().lend_count(),
+ slot.resource_borrow().lend_count(),
+ );
+ }
+ }
+}
+
/// The maximum handle value is specified in
/// <https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md>
/// currently and keeps the upper bits free for use in the component and ABI.
@@ -54,37 +84,12 @@ impl HandleCounter {
}
}
-/// Represents the state of a stream or future handle from the perspective of a
-/// given component instance.
-#[derive(Debug, Eq, PartialEq)]
-pub enum TransmitLocalState {
- /// The write end of the stream or future.
- Write {
- /// Whether the component instance has been notified that the stream or
- /// future is "done" (i.e. the other end has dropped, or, in the case of
- /// a future, a value has been transmitted).
- done: bool,
- },
- /// The read end of the stream or future.
- Read {
- /// Whether the component instance has been notified that the stream or
- /// future is "done" (i.e. the other end has dropped, or, in the case of
- /// a future, a value has been transmitted).
- done: bool,
- },
- /// A read or write is in progress.
- Busy,
- /// A read or write is in progress and an asynchronous cancellation of it
- /// has been requested but has not yet completed.
- Cancelling,
-}
-
/// Return value from [`HandleTable::remove_resource`].
pub enum RemovedResource {
/// An `own` resource was removed with the specified `rep`
Own { rep: u32 },
/// A `borrow` resource was removed originally created within `scope`.
- Borrow { scope: Scope },
+ Borrow { scope: VMScope },
}
/// Different kinds of waitables returned by [`HandleTable::waitable_rep`].
@@ -94,79 +99,9 @@ pub enum Waitable {
Stream,
}
-#[derive(Debug)]
-enum Slot {
- Free {
- next: u32,
- },
-
- /// Represents an owned resource handle with the listed representation.
- ///
- /// The `lend_count` tracks how many times this has been lent out as a
- /// `borrow` and if nonzero this can't be removed.
- ResourceOwn {
- resource: TypedResource,
- lend_count: u32,
- },
-
- /// Represents a borrowed resource handle connected to the `scope`
- /// provided.
- ///
- /// The `rep` is listed and dropping this borrow will decrement the borrow
- /// count of the `scope`.
- ///
- /// Like `ResourceOwn` the `lend_count` tracks how many times this borrow
- /// has been lent out to other calls, and if nonzero this can't be removed.
- ResourceBorrow {
- resource: TypedResource,
- scope: Scope,
- lend_count: u32,
- },
-
- /// Represents a host task handle.
- HostTask {
- rep: u32,
- },
-
- /// Represents a guest task handle.
- GuestTask {
- rep: u32,
- },
-
- /// Represents a guest thread handle.
- #[cfg(feature = "component-model-async")]
- GuestThread {
- rep: u32,
- },
-
- /// Represents a stream handle.
- Stream {
- ty: TypeStreamTableIndex,
- rep: u32,
- state: TransmitLocalState,
- },
-
- /// Represents a future handle.
- Future {
- ty: TypeFutureTableIndex,
- rep: u32,
- state: TransmitLocalState,
- },
-
- /// Represents a waitable-set handle.
- WaitableSet {
- rep: u32,
- },
-
- /// Represents an error-context handle.
- ErrorContext {
- rep: u32,
- },
-}
-
pub struct HandleTable {
next: u32,
- slots: TryVec<Slot>,
+ slots: TryVec<VMHandleTableSlot>,
}
impl Default for HandleTable {
@@ -183,10 +118,10 @@ impl HandleTable {
pub fn is_empty(&self) -> bool {
self.slots
.iter()
- .all(|slot| matches!(slot, Slot::Free { .. }))
+ .all(|slot| matches!(slot, VMHandleTableSlot::Free { .. }))
}
- fn insert(&mut self, counter: &mut HandleCounter, slot: Slot) -> Result<u32> {
+ fn insert(&mut self, counter: &mut HandleCounter, slot: VMHandleTableSlot) -> Result<u32> {
let next = self.next;
// The component model reserves index 0 as never allocatable so add one
@@ -208,13 +143,13 @@ impl HandleTable {
if counter.used >= counter.limit {
bail!("cannot allocate another handle: store handle limit exceeded");
}
- self.slots.push(Slot::Free {
+ self.slots.push(VMHandleTableSlot::Free {
next: next.checked_add(1).unwrap(),
})?;
counter.used += 1;
}
self.next = match mem::replace(&mut self.slots[next as usize], slot) {
- Slot::Free { next } => next,
+ VMHandleTableSlot::Free { next } => next,
_ => unreachable!(),
};
@@ -225,7 +160,7 @@ impl HandleTable {
///
/// The slot stays charged to the store's `HandleCounter`; see its docs.
fn remove(&mut self, idx: u32) -> Result<()> {
- let to_fill = Slot::Free { next: self.next };
+ let to_fill = VMHandleTableSlot::Free { next: self.next };
let slot = self.get_mut(idx)?;
*slot = to_fill;
self.next = idx - 1;
@@ -239,12 +174,12 @@ impl HandleTable {
usize::try_from(idx).ok()
}
- fn get_mut(&mut self, idx: u32) -> Result<&mut Slot> {
+ fn get_mut(&mut self, idx: u32) -> Result<&mut VMHandleTableSlot> {
let slot = self
.handle_index_to_table_index(idx)
.and_then(|i| self.slots.get_mut(i));
match slot {
- None | Some(Slot::Free { .. }) => bail!("unknown handle index {idx}"),
+ None | Some(VMHandleTableSlot::Free { .. }) => bail!("unknown handle index {idx}"),
Some(slot) => Ok(slot),
}
}
@@ -254,11 +189,11 @@ impl HandleTable {
pub fn resource_own_insert(
&mut self,
counter: &mut HandleCounter,
- resource: TypedResource,
+ resource: VMTypedResource,
) -> Result<u32> {
self.insert(
counter,
- Slot::ResourceOwn {
+ VMHandleTableSlot::ResourceOwn {
resource,
lend_count: 0,
},
@@ -271,12 +206,12 @@ impl HandleTable {
pub fn resource_borrow_insert(
&mut self,
counter: &mut HandleCounter,
- resource: TypedResource,
- scope: Scope,
+ resource: VMTypedResource,
+ scope: VMScope,
) -> Result<u32> {
self.insert(
counter,
- Slot::ResourceBorrow {
+ VMHandleTableSlot::ResourceBorrow {
resource,
scope,
lend_count: 0,
@@ -290,9 +225,8 @@ impl HandleTable {
/// resource of the appropriate type.
pub fn resource_rep(&mut self, idx: TypedResourceIndex) -> Result<u32> {
match self.get_mut(idx.raw_index())? {
- Slot::ResourceOwn { resource, .. } | Slot::ResourceBorrow { resource, .. } => {
- resource.rep(&idx)
- }
+ VMHandleTableSlot::ResourceOwn { resource, .. }
+ | VMHandleTableSlot::ResourceBorrow { resource, .. } => resource.rep(&idx),
_ => bail!("index is not a resource"),
}
}
@@ -331,11 +265,11 @@ impl HandleTable {
counter: Option<&mut HandleCounter>,
) -> Result<u32> {
match self.get_mut(idx.raw_index())? {
- Slot::ResourceOwn {
+ VMHandleTableSlot::ResourceOwn {
resource,
lend_count,
}
- | Slot::ResourceBorrow {
+ | VMHandleTableSlot::ResourceBorrow {
resource,
lend_count,
..
@@ -364,7 +298,8 @@ impl HandleTable {
idx: TypedResourceIndex,
) -> Result<()> {
match self.get_mut(idx.raw_index())? {
- Slot::ResourceOwn { lend_count, .. } | Slot::ResourceBorrow { lend_count, .. } => {
+ VMHandleTableSlot::ResourceOwn { lend_count, .. }
+ | VMHandleTableSlot::ResourceBorrow { lend_count, .. } => {
*lend_count -= 1;
counter.used -= 1;
Ok(())
@@ -380,7 +315,7 @@ impl HandleTable {
/// resource of the wrong type.
pub fn remove_resource(&mut self, idx: TypedResourceIndex) -> Result<RemovedResource> {
let ret = match self.get_mut(idx.raw_index())? {
- Slot::ResourceOwn {
+ VMHandleTableSlot::ResourceOwn {
resource,
lend_count,
} => {
@@ -391,7 +326,7 @@ impl HandleTable {
rep: resource.rep(&idx)?,
}
}
- Slot::ResourceBorrow {
+ VMHandleTableSlot::ResourceBorrow {
resource,
scope,
lend_count,
@@ -421,10 +356,10 @@ impl HandleTable {
) -> Result<u32> {
self.insert(
counter,
- Slot::Stream {
+ VMHandleTableSlot::Stream {
rep,
- ty,
- state: TransmitLocalState::Read { done: false },
+ ty: ty.as_u32(),
+ state: VMTransmitLocalState::read(false),
},
)
}
@@ -441,10 +376,10 @@ impl HandleTable {
) -> Result<u32> {
self.insert(
counter,
- Slot::Stream {
+ VMHandleTableSlot::Stream {
rep,
- ty,
- state: TransmitLocalState::Write { done: false },
+ ty: ty.as_u32(),
+ state: VMTransmitLocalState::write(false),
},
)
}
@@ -458,10 +393,10 @@ impl HandleTable {
&mut self,
expected_ty: TypeStreamTableIndex,
idx: u32,
- ) -> Result<(u32, &mut TransmitLocalState)> {
+ ) -> Result<(u32, &mut VMTransmitLocalState)> {
match self.get_mut(idx)? {
- Slot::Stream { rep, ty, state } => {
- if *ty != expected_ty {
+ VMHandleTableSlot::Stream { rep, ty, state } => {
+ if *ty != expected_ty.as_u32() {
bail!("handle is a stream of a different type");
}
Ok((*rep, state))
@@ -483,16 +418,16 @@ impl HandleTable {
idx: u32,
) -> Result<(u32, bool)> {
let ret = match self.get_mut(idx)? {
- Slot::Stream { rep, ty, state } => {
- if *ty != expected_ty {
+ VMHandleTableSlot::Stream { rep, ty, state } => {
+ if *ty != expected_ty.as_u32() {
bail!("handle is a stream of a different type");
}
- let is_done = match state {
- TransmitLocalState::Read { done } => *done,
- TransmitLocalState::Write { .. } => {
+ let is_done = match state.kind() {
+ TransmitLocalStateKind::Read { done } => done,
+ TransmitLocalStateKind::Write { .. } => {
bail!("handle is not a readable end of a stream")
}
- TransmitLocalState::Busy | TransmitLocalState::Cancelling => {
+ TransmitLocalStateKind::Busy | TransmitLocalStateKind::Cancelling => {
bail!("cannot remove busy stream")
}
};
@@ -511,16 +446,16 @@ impl HandleTable {
idx: u32,
) -> Result<u32> {
let ret = match self.get_mut(idx)? {
- Slot::Stream { rep, ty, state } => {
- if *ty != expected_ty {
+ VMHandleTableSlot::Stream { rep, ty, state } => {
+ if *ty != expected_ty.as_u32() {
bail!("handle is a stream of a different type");
}
- match state {
- TransmitLocalState::Write { .. } => {}
- TransmitLocalState::Read { .. } => {
+ match state.kind() {
+ TransmitLocalStateKind::Write { .. } => {}
+ TransmitLocalStateKind::Read { .. } => {
bail!("passed read end to `stream.drop-writable`")
}
- TransmitLocalState::Busy | TransmitLocalState::Cancelling => {
+ TransmitLocalStateKind::Busy | TransmitLocalStateKind::Cancelling => {
bail!("cannot drop busy stream")
}
}
@@ -544,10 +479,10 @@ impl HandleTable {
) -> Result<u32> {
self.insert(
counter,
- Slot::Future {
+ VMHandleTableSlot::Future {
rep,
- ty,
- state: TransmitLocalState::Read { done: false },
+ ty: ty.as_u32(),
+ state: VMTransmitLocalState::read(false),
},
)
}
@@ -564,10 +499,10 @@ impl HandleTable {
) -> Result<u32> {
self.insert(
counter,
- Slot::Future {
+ VMHandleTableSlot::Future {
rep,
- ty,
- state: TransmitLocalState::Write { done: false },
+ ty: ty.as_u32(),
+ state: VMTransmitLocalState::write(false),
},
)
}
@@ -581,10 +516,10 @@ impl HandleTable {
&mut self,
expected_ty: TypeFutureTableIndex,
idx: u32,
- ) -> Result<(u32, &mut TransmitLocalState)> {
+ ) -> Result<(u32, &mut VMTransmitLocalState)> {
match self.get_mut(idx)? {
- Slot::Future { rep, ty, state } => {
- if *ty != expected_ty {
+ VMHandleTableSlot::Future { rep, ty, state } => {
+ if *ty != expected_ty.as_u32() {
bail!("handle is a future of a different type");
}
Ok((*rep, state))
@@ -606,16 +541,16 @@ impl HandleTable {
idx: u32,
) -> Result<(u32, bool)> {
let ret = match self.get_mut(idx)? {
- Slot::Future { rep, ty, state } => {
- if *ty != expected_ty {
+ VMHandleTableSlot::Future { rep, ty, state } => {
+ if *ty != expected_ty.as_u32() {
bail!("handle is a future of a different type");
}
- let is_done = match state {
- TransmitLocalState::Read { done } => *done,
- TransmitLocalState::Write { .. } => {
+ let is_done = match state.kind() {
+ TransmitLocalStateKind::Read { done } => done,
+ TransmitLocalStateKind::Write { .. } => {
bail!("handle is not a readable end of a future")
}
- TransmitLocalState::Busy | TransmitLocalState::Cancelling => {
+ TransmitLocalStateKind::Busy | TransmitLocalStateKind::Cancelling => {
bail!("cannot remove busy future")
}
};
@@ -636,16 +571,16 @@ impl HandleTable {
idx: u32,
) -> Result<(u32, bool)> {
let ret = match self.get_mut(idx)? {
- Slot::Future { rep, ty, state } => {
- if *ty != expected_ty {
+ VMHandleTableSlot::Future { rep, ty, state } => {
+ if *ty != expected_ty.as_u32() {
bail!("handle is a future of a different type");
}
- let is_done = match state {
- TransmitLocalState::Write { done } => *done,
- TransmitLocalState::Read { .. } => {
+ let is_done = match state.kind() {
+ TransmitLocalStateKind::Write { done } => done,
+ TransmitLocalStateKind::Read { .. } => {
bail!("passed read end to `future.drop-writable`")
}
- TransmitLocalState::Busy | TransmitLocalState::Cancelling => {
+ TransmitLocalStateKind::Busy | TransmitLocalStateKind::Cancelling => {
bail!("cannot drop busy future")
}
};
@@ -660,13 +595,13 @@ impl HandleTable {
/// Inserts the error-context `rep` into this table, returning the index it
/// now resides at.
pub fn error_context_insert(&mut self, counter: &mut HandleCounter, rep: u32) -> Result<u32> {
- self.insert(counter, Slot::ErrorContext { rep })
+ self.insert(counter, VMHandleTableSlot::ErrorContext { rep })
}
/// Returns the `rep` of an error-context pointed to by `idx`.
pub fn error_context_rep(&mut self, idx: u32) -> Result<u32> {
match self.get_mut(idx)? {
- Slot::ErrorContext { rep } => Ok(*rep),
+ VMHandleTableSlot::ErrorContext { rep } => Ok(*rep),
_ => bail!("handle is not an error-context"),
}
}
@@ -676,7 +611,7 @@ impl HandleTable {
/// Returns the internal `rep`.
pub fn error_context_drop(&mut self, idx: u32) -> Result<u32> {
let rep = match self.get_mut(idx)? {
- Slot::ErrorContext { rep } => *rep,
+ VMHandleTableSlot::ErrorContext { rep } => *rep,
_ => bail!("handle is not an error-context"),
};
self.remove(idx)?;
@@ -685,20 +620,20 @@ impl HandleTable {
/// Inserts `rep` as a guest subtask into this table.
pub fn subtask_insert_guest(&mut self, counter: &mut HandleCounter, rep: u32) -> Result<u32> {
- self.insert(counter, Slot::GuestTask { rep })
+ self.insert(counter, VMHandleTableSlot::GuestTask { rep })
}
/// Inserts `rep` as a host subtask into this table.
pub fn subtask_insert_host(&mut self, counter: &mut HandleCounter, rep: u32) -> Result<u32> {
- self.insert(counter, Slot::HostTask { rep })
+ self.insert(counter, VMHandleTableSlot::HostTask { rep })
}
/// Returns the `rep` of the subtask at `idx` as well as if it's a host
/// task or not.
pub fn subtask_rep(&mut self, idx: u32) -> Result<(u32, bool)> {
match self.get_mut(idx)? {
- Slot::GuestTask { rep } => Ok((*rep, false)),
- Slot::HostTask { rep } => Ok((*rep, true)),
+ VMHandleTableSlot::GuestTask { rep } => Ok((*rep, false)),
+ VMHandleTableSlot::HostTask { rep } => Ok((*rep, true)),
_ => bail!("handle is not a subtask"),
}
}
@@ -706,8 +641,8 @@ impl HandleTable {
/// Removes the subtask set at `idx`, returning its `rep`.
pub fn subtask_remove(&mut self, idx: u32) -> Result<(u32, bool)> {
let ret = match self.get_mut(idx)? {
- Slot::GuestTask { rep } => (*rep, false),
- Slot::HostTask { rep } => (*rep, true),
+ VMHandleTableSlot::GuestTask { rep } => (*rep, false),
+ VMHandleTableSlot::HostTask { rep } => (*rep, true),
_ => bail!("handle is not a subtask"),
};
self.remove(idx)?;
@@ -716,13 +651,13 @@ impl HandleTable {
/// Inserts `rep` as a waitable set into this table.
pub fn waitable_set_insert(&mut self, counter: &mut HandleCounter, rep: u32) -> Result<u32> {
- self.insert(counter, Slot::WaitableSet { rep })
+ self.insert(counter, VMHandleTableSlot::WaitableSet { rep })
}
/// Returns the `rep` of an waitable-set pointed to by `idx`.
pub fn waitable_set_rep(&mut self, idx: u32) -> Result<u32> {
match self.get_mut(idx)? {
- Slot::WaitableSet { rep, .. } => Ok(*rep),
+ VMHandleTableSlot::WaitableSet { rep, .. } => Ok(*rep),
_ => bail!("handle is not an waitable-set"),
}
}
@@ -730,7 +665,7 @@ impl HandleTable {
/// Removes the waitable set at `idx`, returning its `rep`.
pub fn waitable_set_remove(&mut self, idx: u32) -> Result<u32> {
let ret = match self.get_mut(idx)? {
- Slot::WaitableSet { rep } => *rep,
+ VMHandleTableSlot::WaitableSet { rep } => *rep,
_ => bail!("handle is not a waitable-set"),
};
self.remove(idx)?;
@@ -741,10 +676,12 @@ impl HandleTable {
/// kind of waitable it is.
pub fn waitable_rep(&mut self, idx: u32) -> Result<(u32, Waitable)> {
match self.get_mut(idx)? {
- Slot::GuestTask { rep } => Ok((*rep, Waitable::Subtask { is_host: false })),
- Slot::HostTask { rep } => Ok((*rep, Waitable::Subtask { is_host: true })),
- Slot::Future { rep, .. } => Ok((*rep, Waitable::Future)),
- Slot::Stream { rep, .. } => Ok((*rep, Waitable::Stream)),
+ VMHandleTableSlot::GuestTask { rep } => {
+ Ok((*rep, Waitable::Subtask { is_host: false }))
+ }
+ VMHandleTableSlot::HostTask { rep } => Ok((*rep, Waitable::Subtask { is_host: true })),
+ VMHandleTableSlot::Future { rep, .. } => Ok((*rep, Waitable::Future)),
+ VMHandleTableSlot::Stream { rep, .. } => Ok((*rep, Waitable::Stream)),
_ => bail!("handle is not a waitable"),
}
}
@@ -759,13 +696,14 @@ impl ThreadHandleTable {
/// Inserts the guest thread `rep` into this table, returning the index it
/// now resides at.
pub fn guest_thread_insert(&mut self, counter: &mut HandleCounter, rep: u32) -> Result<u32> {
- self.0.insert(counter, Slot::GuestThread { rep })
+ self.0
+ .insert(counter, VMHandleTableSlot::GuestThread { rep })
}
/// Returns the `rep` of a guest thread pointed to by `idx`.
pub fn guest_thread_rep(&mut self, idx: u32) -> Result<u32> {
match self.0.get_mut(idx)? {
- Slot::GuestThread { rep } => Ok(*rep),
+ VMHandleTableSlot::GuestThread { rep } => Ok(*rep),
_ => bail!("handle is not a guest thread"),
}
}
@@ -775,7 +713,7 @@ impl ThreadHandleTable {
/// Returns the internal `rep`.
pub fn guest_thread_remove(&mut self, idx: u32) -> Result<u32> {
let rep = match self.0.get_mut(idx)? {
- Slot::GuestThread { rep } => *rep,
+ VMHandleTableSlot::GuestThread { rep } => *rep,
_ => bail!("handle is not a guest thread"),
};
self.0.remove(idx)?;
diff --git a/crates/wasmtime/src/runtime/vm/component/resources.rs b/crates/wasmtime/src/runtime/vm/component/resources.rs
index 8dede50daa..00ccbbd43f 100644
--- a/crates/wasmtime/src/runtime/vm/component/resources.rs
+++ b/crates/wasmtime/src/runtime/vm/component/resources.rs
@@ -24,7 +24,7 @@ use super::{HandleCounter, HandleTable, InstanceState, RemovedResource};
use crate::bail_bug;
use crate::component::store::ComponentTaskState;
use crate::prelude::*;
-use crate::runtime::vm::{VMResourceIndex, VMStoreContext};
+use crate::runtime::vm::{VMResourceIndex, VMScope, VMStoreContext, VMTypedResource};
use core::error::Error;
use core::fmt;
use core::mem;
@@ -86,77 +86,12 @@ pub struct ResourceTables<'a> {
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum CurrentScope {
/// A scope which already has an ID in the component task state.
- Id(Scope),
+ Id(VMScope),
/// A host scope whose task has not yet been materialized.
#[cfg_attr(not(feature = "component-model-async"), allow(dead_code))]
DeferredHost,
}
-/// Identifier for a component call's resource-borrow scope.
-#[derive(Debug, Copy, Clone, Eq, PartialEq)]
-pub enum Scope {
- /// A non-concurrent scope or a concurrent guest-task scope.
- Id(u32),
- /// A concurrent host-task scope.
- HostId(u32),
-}
-
-/// Typed representation of a "rep" for a resource.
-///
-/// All resources in the component model are stored in a single heterogeneous
-/// table so this type is used to disambiguate what everything is. This is the
-/// representation of a resource stored at-rest in memory.
-#[derive(Debug)]
-pub enum TypedResource {
- /// A resource defined by the host.
- ///
- /// The meaning of the 32-bit integer here is up to the embedder, it
- /// otherwise is not used within the runtime here.
- Host(u32),
-
- /// A resource defined within a component.
- Component {
- /// This is an integer supplied by the component itself when this
- /// resource was created. Typically this is a pointer into linear
- /// memory for a component.
- rep: u32,
-
- /// The type of this component resource.
- ///
- /// This is used, within the context of a component, to keep track of
- /// what the type of `rep` is. This is then used when getting/removing
- /// from the table to ensure that the guest does indeed have the right
- /// permission to access this slot.
- ty: TypeResourceTableIndex,
- },
-}
-
-impl TypedResource {
- pub(super) fn rep(&self, access_ty: &TypedResourceIndex) -> Result<u32> {
- match (self, access_ty) {
- (Self::Host(rep), TypedResourceIndex::Host(_)) => Ok(*rep),
- (Self::Host(_), expected) => bail!(ResourceTypeMismatch {
- expected: *expected,
- found: "host resource",
- }),
- (Self::Component { rep, ty }, TypedResourceIndex::Component { ty: expected, .. }) => {
- if ty == expected {
- Ok(*rep)
- } else {
- bail!(ResourceTypeMismatch {
- expected: *access_ty,
- found: "a different guest-defined resource",
- })
- }
- }
- (Self::Component { .. }, expected) => bail!(ResourceTypeMismatch {
- expected: *expected,
- found: "guest-defined resource",
- }),
- }
- }
-}
-
/// An index used to access a resource.
///
/// This reflects how index operations are always accompanied not only with a
@@ -186,7 +121,7 @@ impl TypedResourceIndex {
}
}
- fn desc(&self) -> &'static str {
+ pub(crate) fn desc(&self) -> &'static str {
match self {
Self::Host(_) => "host resource",
Self::Component { .. } => "guest-defined resource",
@@ -211,13 +146,13 @@ impl CallContext {
impl ResourceTables<'_> {
fn table_for_resource(
&mut self,
- resource: &TypedResource,
+ resource: &VMTypedResource,
) -> (&mut HandleTable, &mut HandleCounter) {
- let table = match resource {
- TypedResource::Host(_) => &mut *self.host_table,
- TypedResource::Component { ty, .. } => {
+ let table = match resource.component_ty() {
+ None => &mut *self.host_table,
+ Some(ty) => {
let (states, types) = self.guest.as_mut().unwrap();
- states[types[*ty].unwrap_concrete_instance()].handle_table()
+ states[types[ty].unwrap_concrete_instance()].handle_table()
}
};
(table, &mut *self.handle_counter)
@@ -240,7 +175,7 @@ impl ResourceTables<'_> {
/// Implementation of the `resource.new` canonical intrinsic.
///
/// Note that this is the same as `resource_lower_own`.
- pub fn resource_new(&mut self, resource: TypedResource) -> Result<u32> {
+ pub fn resource_new(&mut self, resource: VMTypedResource) -> Result<u32> {
let (table, counter) = self.table_for_resource(&resource);
table.resource_own_insert(counter, resource)
}
@@ -284,7 +219,7 @@ impl ResourceTables<'_> {
/// the same as `resource_new` implementation-wise.
///
/// This is an implementation of the canonical ABI `lower_own` function.
- pub fn resource_lower_own(&mut self, resource: TypedResource) -> Result<u32> {
+ pub fn resource_lower_own(&mut self, resource: VMTypedResource) -> Result<u32> {
let (table, counter) = self.table_for_resource(&resource);
table.resource_own_insert(counter, resource)
}
@@ -304,7 +239,7 @@ impl ResourceTables<'_> {
}
}
- fn materialize_current_scope(&mut self) -> Result<Scope> {
+ fn materialize_current_scope(&mut self) -> Result<VMScope> {
let id = match self.current_scope {
Some(CurrentScope::Id(id)) => return Ok(id),
Some(CurrentScope::DeferredHost) => self.task_state.materialize_current_scope()?,
@@ -421,7 +356,7 @@ impl ResourceTables<'_> {
)]
pub fn promote_lazy_lends(
&mut self,
- scope: Scope,
+ scope: VMScope,
lazy_lenders: &[VMResourceIndex],
) -> Result<()> {
// Fail up front on anything that could otherwise fail partway through
@@ -455,7 +390,7 @@ impl ResourceTables<'_> {
/// function. The other half of this implementation is located on
/// `VMComponentContext` which handles the special case of avoiding borrow
/// tracking entirely.
- pub fn resource_lower_borrow(&mut self, resource: TypedResource) -> Result<u32> {
+ pub fn resource_lower_borrow(&mut self, resource: VMTypedResource) -> Result<u32> {
let scope = self.materialize_current_scope()?;
let cx = self.task_state.call_context(scope)?;
cx.borrow_count = cx.borrow_count.checked_add(1).unwrap();
@@ -486,9 +421,9 @@ impl ResourceTables<'_> {
}
#[derive(Debug)]
-struct ResourceTypeMismatch {
- expected: TypedResourceIndex,
- found: &'static str,
+pub(crate) struct ResourceTypeMismatch {
+ pub(crate) expected: TypedResourceIndex,
+ pub(crate) found: &'static str,
}
impl fmt::Display for ResourceTypeMismatch {
diff --git a/crates/wasmtime/src/runtime/vm/vmcontext.rs b/crates/wasmtime/src/runtime/vm/vmcontext.rs
index 198686ff88..d119a0bbf8 100644
--- a/crates/wasmtime/src/runtime/vm/vmcontext.rs
+++ b/crates/wasmtime/src/runtime/vm/vmcontext.rs
@@ -369,6 +369,7 @@ macro_rules! define_vm_types {
$(
$(#[doc = $vdoc:literal])*
#[snake_name = $vsnake:ident]
+ $(#[$vattr:meta])*
$Variant:ident {
$(
$(#[doc = $fdoc:literal])*
@@ -395,6 +396,7 @@ macro_rules! define_vm_types {
$svis enum $Name {
$(
$(#[doc = $vdoc])*
+ $(#[$vattr])*
$Variant {
$(
$(#[doc = $fdoc])*