Skip to content

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

Publish handle-table slot pointers in `VMComponentContext` (#15171)

Nick Fitzgeraldc2851ff669c5
diff --git a/crates/environ/src/component/vmcomponent_offsets.rs b/crates/environ/src/component/vmcomponent_offsets.rs
index dec9f80fdf..fbd415a899 100644
--- a/crates/environ/src/component/vmcomponent_offsets.rs
+++ b/crates/environ/src/component/vmcomponent_offsets.rs
@@ -50,6 +50,7 @@ pub struct VMComponentOffsets<P> {
// generated `compute_field_offsets` and read by the generated accessors of
// the same names.
may_leave: u32,
+ handle_table_slots: u32,
trampoline_func_refs: u32,
intrinsic_func_refs: u32,
lowerings: u32,
@@ -165,6 +166,7 @@ impl<P: PtrSize> VMComponentOffsets<P> {
},
num_resources: component.num_resources,
may_leave: 0,
+ handle_table_slots: 0,
trampoline_func_refs: 0,
intrinsic_func_refs: 0,
lowerings: 0,
diff --git a/crates/environ/src/vmctxtypes.rs b/crates/environ/src/vmctxtypes.rs
index 8a13fc8e27..d0171280fb 100644
--- a/crates/environ/src/vmctxtypes.rs
+++ b/crates/environ/src/vmctxtypes.rs
@@ -304,6 +304,15 @@ macro_rules! for_each_vmctx_type {
align { ptr }
+ // Each instance's `HandleTable::slots`, which a fused
+ // adapter reads to reach a handle's slot without calling
+ // into the host.
+ array {
+ handle_table_slots
+ [num_runtime_component_instances; RuntimeComponentInstanceIndex]:
+ VmPtr<VMHandleTableSlot>
+ }
+
array {
#[aggregate]
trampoline_func_refs[num_trampolines; TrampolineIndex]: VMFuncRef
diff --git a/crates/wasmtime/src/runtime/component/concurrent.rs b/crates/wasmtime/src/runtime/component/concurrent.rs
index 1f00109550..c4020b9a58 100644
--- a/crates/wasmtime/src/runtime/component/concurrent.rs
+++ b/crates/wasmtime/src/runtime/component/concurrent.rs
@@ -67,7 +67,8 @@ use crate::vm::GcRootsList;
use crate::vm::VMScope;
use crate::vm::component::ScopeKind;
use crate::vm::component::{
- CallContext, ComponentInstance, CurrentScope, HandleCounter, InstanceState,
+ CallContext, ComponentInstance, CurrentScope, HandleCounter, HandleTableInsertion,
+ InstanceState,
};
use crate::vm::{
AlwaysMut, SendSyncPtr, UncaughtException, VMFuncRef, VMLazyThread, VMMemoryDefinition, VMStore,
@@ -2244,6 +2245,14 @@ impl StoreOpaque {
(component.instance_state(instance.index), counter)
}
+ /// Begins inserting into the handle table of the specified instance.
+ fn handle_table_insertion(&mut self, instance: RuntimeInstance) -> HandleTableInsertion<'_> {
+ let (component, counter) = self
+ .store_data_mut()
+ .component_instance_and_handle_counter_mut(instance.instance);
+ component.handle_table_insertion(instance.index, counter)
+ }
+
/// Configure the currently running `thread`.
///
/// This will save off any state necessary for the previous thread, if
@@ -3712,10 +3721,10 @@ impl Instance {
// It hasn't returned yet, but the caller is calling via an
// async-lowered import, so we generate a handle for the task
// waitable and return the status.
- let (state, counter) = store.0.instance_state_and_handle_counter(caller_instance);
- let handle = state
- .handle_table()
- .subtask_insert_guest(counter, guest_thread.task.rep())?;
+ let handle = store
+ .0
+ .handle_table_insertion(caller_instance)
+ .subtask_guest(guest_thread.task.rep())?;
store
.0
.concurrent_state_mut()?
@@ -3909,10 +3918,10 @@ impl Instance {
let state = store.0.concurrent_state_mut()?;
state.push_future(future);
let instance = state.get_mut(caller.task)?.instance;
- let (state, counter) = store.0.instance_state_and_handle_counter(instance);
- let handle = state
- .handle_table()
- .subtask_insert_host(counter, task.rep())?;
+ let handle = store
+ .0
+ .handle_table_insertion(instance)
+ .subtask_host(task.rep())?;
store.0.concurrent_state_mut()?.get_mut(task)?.common.handle = Some(handle);
log::trace!("assign {task:?} handle {handle} for {caller:?} instance {instance:?}");
@@ -4052,11 +4061,9 @@ impl Instance {
caller_instance: RuntimeComponentInstanceIndex,
) -> Result<u32> {
let set = store.concurrent_state_mut()?.push(WaitableSet::default())?;
- let (state, counter) =
- store.instance_state_and_handle_counter(self.runtime_instance(caller_instance));
- let handle = state
- .handle_table()
- .waitable_set_insert(counter, set.rep())?;
+ let handle = store
+ .handle_table_insertion(self.runtime_instance(caller_instance))
+ .waitable_set(set.rep())?;
log::trace!("new waitable set {set:?} (handle {handle})");
Ok(handle)
}
@@ -7085,12 +7092,11 @@ mod tests {
state,
group,
})?;
- let (state, counter) = store_opaque.instance_state_and_handle_counter(
- instance.runtime_instance(RuntimeComponentInstanceIndex::from_u32(0)),
- );
- let handle = state
- .handle_table()
- .subtask_insert_host(counter, task.rep())?;
+ let handle = store_opaque
+ .handle_table_insertion(
+ instance.runtime_instance(RuntimeComponentInstanceIndex::from_u32(0)),
+ )
+ .subtask_host(task.rep())?;
let common = &mut store_opaque.concurrent_state_mut()?.get_mut(task)?.common;
common.handle = Some(handle);
common.event = event;
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 53de7adcc6..d325defad7 100644
--- a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs
+++ b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs
@@ -15,7 +15,7 @@ use crate::store::{StoreOpaque, StoreToken};
use crate::try_mutex::{TryMutex, TryMutexGuard};
use crate::vm::VMTransmitLocalState;
use crate::vm::component::TransmitLocalStateKind;
-use crate::vm::component::{ComponentInstance, HandleCounter, HandleTable};
+use crate::vm::component::{ComponentInstance, HandleCounter, HandleTable, HandleTableInsertion};
use crate::vm::{AlwaysMut, VMStore};
use crate::{AsContext, AsContextMut, StoreContextMut, ValRaw};
use crate::{
@@ -2071,8 +2071,8 @@ pub(crate) fn lower_error_context_to_index<U>(
.id()
.get_mut_and_handle_counter(cx.store.0);
instance
- .table_for_error_context(dst)
- .error_context_insert(counter, rep)
+ .error_context_table_insertion(dst, counter)
+ .error_context(rep)
}
_ => func::bad_type_info(),
}
@@ -4422,8 +4422,8 @@ impl Instance {
// ref tracking data structures to enable later lookup
let (instance, counter) = self.id().get_mut_and_handle_counter(store);
let local_idx = instance
- .table_for_error_context(ty)
- .error_context_insert(counter, table_id.rep())?;
+ .error_context_table_insertion(ty, counter)
+ .error_context(table_id.rep())?;
Ok(local_idx)
}
@@ -4554,15 +4554,15 @@ impl Instance {
.new_transmit(TransmitOrigin::guest(self.id().instance(), ty))?;
let (instance, counter) = self.id().get_mut_and_handle_counter(store);
- let table = instance.table_for_transmit(ty);
+ let mut insertion = instance.transmit_table_insertion(ty, counter);
let (read_handle, write_handle) = match ty {
TransmitIndex::Future(ty) => (
- table.future_insert_read(counter, ty, read.rep())?,
- table.future_insert_write(counter, ty, write.rep())?,
+ insertion.future_read(ty, read.rep())?,
+ insertion.future_write(ty, write.rep())?,
),
TransmitIndex::Stream(ty) => (
- table.stream_insert_read(counter, ty, read.rep())?,
- table.stream_insert_write(counter, ty, write.rep())?,
+ insertion.stream_read(ty, read.rep())?,
+ insertion.stream_write(ty, write.rep())?,
),
};
@@ -4716,8 +4716,8 @@ impl Instance {
.table_for_error_context(src)
.error_context_rep(src_idx)?;
let dst_idx = instance
- .table_for_error_context(dst)
- .error_context_insert(counter, rep)?;
+ .error_context_table_insertion(dst, counter)
+ .error_context(rep)?;
// Update the global (cross-subcomponent) count for error contexts
// as the new component has essentially created a new reference that will
@@ -4789,32 +4789,53 @@ fn lower_transmit_to_index(
) -> Result<u32> {
let state = concurrent_state.get_mut(id)?.state;
debug_assert_eq!(concurrent_state.get_mut(state)?.read_handle, id);
- let handle_table = instance.table_for_transmit(ty);
+ let mut insertion = instance.transmit_table_insertion(ty, counter);
let handle = match ty {
- TransmitIndex::Future(idx) => handle_table.future_insert_read(counter, idx, id.rep()),
- TransmitIndex::Stream(idx) => handle_table.stream_insert_read(counter, idx, id.rep()),
+ TransmitIndex::Future(idx) => insertion.future_read(idx, id.rep()),
+ TransmitIndex::Stream(idx) => insertion.stream_read(idx, id.rep()),
}?;
concurrent_state.get_mut(id)?.common.handle = Some(handle);
Ok(handle)
}
impl ComponentInstance {
- fn table_for_transmit(self: Pin<&mut Self>, ty: TransmitIndex) -> &mut HandleTable {
- let (states, types) = self.instance_states();
- let runtime_instance = match ty {
+ fn transmit_instance(&self, ty: TransmitIndex) -> RuntimeComponentInstanceIndex {
+ let types = self.component().types();
+ match ty {
TransmitIndex::Stream(ty) => types[ty].instance,
TransmitIndex::Future(ty) => types[ty].instance,
- };
- states[runtime_instance].handle_table()
+ }
+ }
+
+ fn table_for_transmit(self: Pin<&mut Self>, ty: TransmitIndex) -> &mut HandleTable {
+ let runtime_instance = self.transmit_instance(ty);
+ self.instance_state(runtime_instance).handle_table()
+ }
+
+ fn transmit_table_insertion<'a>(
+ self: Pin<&'a mut Self>,
+ ty: TransmitIndex,
+ counter: &'a mut HandleCounter,
+ ) -> HandleTableInsertion<'a> {
+ let runtime_instance = self.transmit_instance(ty);
+ self.handle_table_insertion(runtime_instance, counter)
}
fn table_for_error_context(
self: Pin<&mut Self>,
ty: TypeComponentLocalErrorContextTableIndex,
) -> &mut HandleTable {
- let (states, types) = self.instance_states();
- let runtime_instance = types[ty].instance;
- states[runtime_instance].handle_table()
+ let runtime_instance = self.component().types()[ty].instance;
+ self.instance_state(runtime_instance).handle_table()
+ }
+
+ fn error_context_table_insertion<'a>(
+ self: Pin<&'a mut Self>,
+ ty: TypeComponentLocalErrorContextTableIndex,
+ counter: &'a mut HandleCounter,
+ ) -> HandleTableInsertion<'a> {
+ let runtime_instance = self.component().types()[ty].instance;
+ self.handle_table_insertion(runtime_instance, counter)
}
fn get_mut_by_index(
diff --git a/crates/wasmtime/src/runtime/component/func/options.rs b/crates/wasmtime/src/runtime/component/func/options.rs
index eda17e7e3a..3b6c8b1f14 100644
--- a/crates/wasmtime/src/runtime/component/func/options.rs
+++ b/crates/wasmtime/src/runtime/component/func/options.rs
@@ -520,7 +520,7 @@ impl<'a> LiftContext<'a> {
host_table: self.host_table,
handle_counter: self.handle_counter,
task_state: self.task_state,
- guest: Some(self.instance.as_mut().instance_states()),
+ guest: Some(self.instance.as_mut().instance_states_and_published()),
current_scope: self.current_scope,
},
self.host_resource_data,
diff --git a/crates/wasmtime/src/runtime/component/store.rs b/crates/wasmtime/src/runtime/component/store.rs
index 34ea6b9e4a..0412c4fe4e 100644
--- a/crates/wasmtime/src/runtime/component/store.rs
+++ b/crates/wasmtime/src/runtime/component/store.rs
@@ -162,7 +162,7 @@ impl ComponentStoreData {
.as_mut()
.unwrap()
.get_mut()
- .instance_states()
+ .instance_states_and_published()
});
(
vm::component::ResourceTables {
diff --git a/crates/wasmtime/src/runtime/vm/component.rs b/crates/wasmtime/src/runtime/vm/component.rs
index b02f06c68a..1f1e9f1bf0 100644
--- a/crates/wasmtime/src/runtime/vm/component.rs
+++ b/crates/wasmtime/src/runtime/vm/component.rs
@@ -16,9 +16,9 @@ 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, VMScope, VMStore, VMStoreContext,
- VMStoreRawPtr, VMTableImport, VMTransmitLocalState, VMTypedResource, VMWasmCallFunction,
- ValRaw, VmPtr, VmSafe, catch_unwind_and_record_trap,
+ VMHandleTableSlot, 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, Result, bail, vm};
@@ -45,7 +45,7 @@ mod handle_table;
mod libcalls;
mod resources;
-pub use self::handle_table::{HandleCounter, HandleTable, RemovedResource};
+pub use self::handle_table::{HandleCounter, HandleTable, HandleTableInsertion, RemovedResource};
#[cfg(feature = "component-model-async")]
pub use self::handle_table::{ThreadHandleTable, Waitable};
pub(crate) use self::resources::ResourceTypeMismatch;
@@ -630,6 +630,16 @@ impl ComponentInstance {
InstanceLayout::vmctx(self)
}
+ /// Seed the vmctx array compiled code reads. Only needed at startup:
+ /// afterwards `HandleTableInsertion` keeps it current.
+ pub(crate) fn publish_handle_tables(self: Pin<&mut Self>) {
+ let (states, _types, published) = self.instance_states_and_published();
+ for (instance, state) in states.iter_mut() {
+ let i = usize::try_from(instance.as_u32()).unwrap();
+ published[i] = state.handle_table().slots_ptr();
+ }
+ }
+
/// Returns a pointer to the "may leave" flag for this instance specified
/// for canonical lowering and lifting operations.
#[inline]
@@ -970,6 +980,8 @@ impl ComponentInstance {
VmPtr::from(self.store.0.as_ref().vm_store_context_ptr());
}
+ self.as_mut().publish_handle_tables();
+
for i in 0..self.offsets.num_runtime_component_instances {
let i = RuntimeComponentInstanceIndex::from_u32(i);
let mut def = VMGlobalDefinition::new();
@@ -1138,6 +1150,52 @@ impl ComponentInstance {
}
}
+ /// Begins inserting into `instance`'s handle table, keeping the vmctx copy
+ /// of its slots pointer current.
+ pub fn handle_table_insertion<'a>(
+ self: Pin<&'a mut Self>,
+ instance: RuntimeComponentInstanceIndex,
+ counter: &'a mut HandleCounter,
+ ) -> HandleTableInsertion<'a> {
+ let (states, _types, published) = self.instance_states_and_published();
+ // SAFETY: `published[instance]` is where compiled code reads
+ // `instance`'s handle table.
+ unsafe {
+ states[instance]
+ .handle_table()
+ .insert(counter, &mut published[instance.as_u32() as usize])
+ }
+ }
+
+ /// As `instance_states`, plus the vmctx entries through which compiled
+ /// code reads each instance's handle table.
+ pub fn instance_states_and_published(
+ mut self: Pin<&mut Self>,
+ ) -> (
+ &mut TryPrimaryMap<RuntimeComponentInstanceIndex, InstanceState>,
+ &ComponentTypes,
+ &mut [VmPtr<VMHandleTableSlot>],
+ ) {
+ let offset = self
+ .offsets
+ .handle_table_slots()
+ .at(RuntimeComponentInstanceIndex::from_u32(0));
+ let len = usize::try_from(self.offsets.num_runtime_component_instances).unwrap();
+ // SAFETY: the array is part of this `VMComponentContext`'s allocation
+ // and holds exactly `len` entries.
+ let published = unsafe {
+ let base = self
+ .as_mut()
+ .vmctx_plus_offset_raw::<VmPtr<VMHandleTableSlot>>(offset);
+ core::slice::from_raw_parts_mut(base.as_ptr(), len)
+ };
+ // safety: as in `instance_states`.
+ unsafe {
+ let me = self.get_unchecked_mut();
+ (&mut me.instance_states, me.component.types(), published)
+ }
+ }
+
pub fn instance_state(
self: Pin<&mut Self>,
instance: RuntimeComponentInstanceIndex,
diff --git a/crates/wasmtime/src/runtime/vm/component/handle_table.rs b/crates/wasmtime/src/runtime/vm/component/handle_table.rs
index 2e1bab7bd2..e1643048bc 100644
--- a/crates/wasmtime/src/runtime/vm/component/handle_table.rs
+++ b/crates/wasmtime/src/runtime/vm/component/handle_table.rs
@@ -1,8 +1,9 @@
use super::{TransmitLocalStateKind, TypedResourceIndex};
use crate::prelude::TryVec;
-use crate::vm::{VMHandleTableSlot, VMScope, VMTransmitLocalState, VMTypedResource, VmSafe};
+use crate::vm::{VMHandleTableSlot, VMScope, VMTransmitLocalState, VMTypedResource, VmPtr, VmSafe};
use crate::{Result, bail};
use core::mem;
+use core::ptr::NonNull;
use wasmtime_environ::component::{TypeFutureTableIndex, TypeStreamTableIndex};
// SAFETY: `VMHandleTableSlot` is generated with `#[repr(C, u32)]` and every
@@ -114,6 +115,13 @@ impl Default for HandleTable {
}
impl HandleTable {
+ /// This table's slots, as compiled code reads them.
+ pub fn slots_ptr(&mut self) -> VmPtr<VMHandleTableSlot> {
+ // An empty `TryVec`'s pointer is dangling but well-aligned; a table
+ // with no slots has none for compiled code to reach.
+ VmPtr::from(NonNull::new(self.slots.as_mut_ptr()).unwrap())
+ }
+
/// Returns whether or not this table is empty.
pub fn is_empty(&self) -> bool {
self.slots
@@ -121,39 +129,41 @@ impl HandleTable {
.all(|slot| matches!(slot, VMHandleTableSlot::Free { .. }))
}
- 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
- // to the table index to start the numbering at 1 instead. Also note
- // that the component model places an upper-limit per-table on the
- // maximum allowed index.
- //
- // First check to make sure the returned handle is in-bounds, then do
- // the actual allocation below.
- if next + 1 >= MAX_HANDLE {
- bail!("cannot allocate another handle: index overflow");
+ /// Begins inserting into this table, which compiled code reads through
+ /// `vm_ptr`, re-publishing the table's slots there whenever they move.
+ ///
+ /// # Safety
+ ///
+ /// `vm_ptr` must be the `VMComponentContext` entry through which compiled
+ /// code reads this table's slots, or else compiled code may later read
+ /// through a stale pointer.
+ pub unsafe fn insert<'a>(
+ &'a mut self,
+ counter: &'a mut HandleCounter,
+ vm_ptr: &'a mut VmPtr<VMHandleTableSlot>,
+ ) -> HandleTableInsertion<'a> {
+ HandleTableInsertion {
+ table: self,
+ counter,
+ vm_ptr: Some(vm_ptr),
}
+ }
- // Growing the table is charged to the store-wide cap; `MAX_HANDLE`
- // alone scales with the guest-controlled instance count. Reusing a
- // free slot was already charged when it was allocated. See
- // `HandleCounter`.
- if next as usize == self.slots.len() {
- if counter.used >= counter.limit {
- bail!("cannot allocate another handle: store handle limit exceeded");
- }
- self.slots.push(VMHandleTableSlot::Free {
- next: next.checked_add(1).unwrap(),
- })?;
- counter.used += 1;
+ /// Begins inserting into this table, which compiled code never reads.
+ ///
+ /// # Safety
+ ///
+ /// This table's slots must never be published to compiled code, or else
+ /// compiled code may later read through a stale pointer.
+ pub unsafe fn insert_unpublished<'a>(
+ &'a mut self,
+ counter: &'a mut HandleCounter,
+ ) -> HandleTableInsertion<'a> {
+ HandleTableInsertion {
+ table: self,
+ counter,
+ vm_ptr: None,
}
- self.next = match mem::replace(&mut self.slots[next as usize], slot) {
- VMHandleTableSlot::Free { next } => next,
- _ => unreachable!(),
- };
-
- Ok(next + 1)
}
/// Frees the slot at `idx` for reuse by a later `insert` into this table.
@@ -184,41 +194,6 @@ impl HandleTable {
}
}
- /// Inserts a new `own` resource into this table whose type/rep are
- /// specified by `resource`.
- pub fn resource_own_insert(
- &mut self,
- counter: &mut HandleCounter,
- resource: VMTypedResource,
- ) -> Result<u32> {
- self.insert(
- counter,
- VMHandleTableSlot::ResourceOwn {
- resource,
- lend_count: 0,
- },
- )
- }
-
- /// Inserts a new `borrow` resource into this table whose type/rep are
- /// specified by `resource`. The `scope` specified is used by
- /// `CallContexts` to manage lending information.
- pub fn resource_borrow_insert(
- &mut self,
- counter: &mut HandleCounter,
- resource: VMTypedResource,
- scope: VMScope,
- ) -> Result<u32> {
- self.insert(
- counter,
- VMHandleTableSlot::ResourceBorrow {
- resource,
- scope,
- lend_count: 0,
- },
- )
- }
-
/// Returns the internal "rep" of the resource specified by `idx`.
///
/// Returns an error if `idx` is out-of-bounds or doesn't point to a
@@ -344,46 +319,6 @@ impl HandleTable {
Ok(ret)
}
- /// Inserts a readable-end stream of type `ty` and with the specified `rep`
- /// into this table.
- ///
- /// Returns the table-local index of the stream.
- pub fn stream_insert_read(
- &mut self,
- counter: &mut HandleCounter,
- ty: TypeStreamTableIndex,
- rep: u32,
- ) -> Result<u32> {
- self.insert(
- counter,
- VMHandleTableSlot::Stream {
- rep,
- ty: ty.as_u32(),
- state: VMTransmitLocalState::read(false),
- },
- )
- }
-
- /// Inserts a writable-end stream of type `ty` and with the specified `rep`
- /// into this table.
- ///
- /// Returns the table-local index of the stream.
- pub fn stream_insert_write(
- &mut self,
- counter: &mut HandleCounter,
- ty: TypeStreamTableIndex,
- rep: u32,
- ) -> Result<u32> {
- self.insert(
- counter,
- VMHandleTableSlot::Stream {
- rep,
- ty: ty.as_u32(),
- state: VMTransmitLocalState::write(false),
- },
- )
- }
-
/// Returns the `rep` and `state` associated with the stream pointed to by
/// `idx`.
///
@@ -467,46 +402,6 @@ impl HandleTable {
Ok(ret)
}
- /// Inserts a readable-end future of type `ty` and with the specified `rep`
- /// into this table.
- ///
- /// Returns the table-local index of the future.
- pub fn future_insert_read(
- &mut self,
- counter: &mut HandleCounter,
- ty: TypeFutureTableIndex,
- rep: u32,
- ) -> Result<u32> {
- self.insert(
- counter,
- VMHandleTableSlot::Future {
- rep,
- ty: ty.as_u32(),
- state: VMTransmitLocalState::read(false),
- },
- )
- }
-
- /// Inserts a writable-end future of type `ty` and with the specified `rep`
- /// into this table.
- ///
- /// Returns the table-local index of the future.
- pub fn future_insert_write(
- &mut self,
- counter: &mut HandleCounter,
- ty: TypeFutureTableIndex,
- rep: u32,
- ) -> Result<u32> {
- self.insert(
- counter,
- VMHandleTableSlot::Future {
- rep,
- ty: ty.as_u32(),
- state: VMTransmitLocalState::write(false),
- },
- )
- }
-
/// Returns the `rep` and `state` associated with the future pointed to by
/// `idx`.
///
@@ -592,12 +487,6 @@ impl HandleTable {
Ok(ret)
}
- /// 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, 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)? {
@@ -618,16 +507,6 @@ impl HandleTable {
Ok(rep)
}
- /// 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, 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, 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)> {
@@ -649,11 +528,6 @@ impl HandleTable {
Ok(ret)
}
- /// 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, 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)? {
@@ -687,6 +561,145 @@ impl HandleTable {
}
}
+/// An in-progress insertion into a `HandleTable`, created by
+/// `HandleTable::insert` or `HandleTable::insert_unpublished`.
+///
+/// Keeps the copy of the table's slots pointer that compiled code reads, if
+/// any, up to date as the table grows.
+#[must_use]
+pub struct HandleTableInsertion<'a> {
+ table: &'a mut HandleTable,
+ counter: &'a mut HandleCounter,
+ /// `None` when compiled code never reads `table`.
+ vm_ptr: Option<&'a mut VmPtr<VMHandleTableSlot>>,
+}
+
+impl HandleTableInsertion<'_> {
+ fn insert(&mut self, slot: VMHandleTableSlot) -> Result<u32> {
+ let table = &mut *self.table;
+ let next = table.next;
+
+ // The component model reserves index 0 as never allocatable so add one
+ // to the table index to start the numbering at 1 instead. Also note
+ // that the component model places an upper-limit per-table on the
+ // maximum allowed index.
+ //
+ // First check to make sure the returned handle is in-bounds, then do
+ // the actual allocation below.
+ if next + 1 >= MAX_HANDLE {
+ bail!("cannot allocate another handle: index overflow");
+ }
+
+ // Growing the table is charged to the store-wide cap; `MAX_HANDLE`
+ // alone scales with the guest-controlled instance count. Reusing a
+ // free slot was already charged when it was allocated. See
+ // `HandleCounter`.
+ if next as usize == table.slots.len() {
+ if self.counter.used >= self.counter.limit {
+ bail!("cannot allocate another handle: store handle limit exceeded");
+ }
+ table.slots.push(VMHandleTableSlot::Free {
+ next: next.checked_add(1).unwrap(),
+ })?;
+ self.counter.used += 1;
+ if let Some(vm_ptr) = &mut self.vm_ptr {
+ **vm_ptr = table.slots_ptr();
+ }
+ }
+ table.next = match mem::replace(&mut table.slots[next as usize], slot) {
+ VMHandleTableSlot::Free { next } => next,
+ _ => unreachable!(),
+ };
+
+ Ok(next + 1)
+ }
+
+ /// Inserts a new `own` resource whose type/rep are specified by
+ /// `resource`.
+ pub fn resource_own(&mut self, resource: VMTypedResource) -> Result<u32> {
+ self.insert(VMHandleTableSlot::ResourceOwn {
+ resource,
+ lend_count: 0,
+ })
+ }
+
+ /// Inserts a new `borrow` resource whose type/rep are specified by
+ /// `resource`. The `scope` specified is used by `CallContexts` to manage
+ /// lending information.
+ pub fn resource_borrow(&mut self, resource: VMTypedResource, scope: VMScope) -> Result<u32> {
+ self.insert(VMHandleTableSlot::ResourceBorrow {
+ resource,
+ scope,
+ lend_count: 0,
+ })
+ }
+
+ /// Inserts a readable-end stream of type `ty` and with the specified `rep`.
+ ///
+ /// Returns the table-local index of the stream.
+ pub fn stream_read(&mut self, ty: TypeStreamTableIndex, rep: u32) -> Result<u32> {
+ self.insert(VMHandleTableSlot::Stream {
+ rep,
+ ty: ty.as_u32(),
+ state: VMTransmitLocalState::read(false),
+ })
+ }
+
+ /// Inserts a writable-end stream of type `ty` and with the specified `rep`.
+ ///
+ /// Returns the table-local index of the stream.
+ pub fn stream_write(&mut self, ty: TypeStreamTableIndex, rep: u32) -> Result<u32> {
+ self.insert(VMHandleTableSlot::Stream {
+ rep,
+ ty: ty.as_u32(),
+ state: VMTransmitLocalState::write(false),
+ })
+ }
+
+ /// Inserts a readable-end future of type `ty` and with the specified `rep`.
+ ///
+ /// Returns the table-local index of the future.
+ pub fn future_read(&mut self, ty: TypeFutureTableIndex, rep: u32) -> Result<u32> {
+ self.insert(VMHandleTableSlot::Future {
+ rep,
+ ty: ty.as_u32(),
+ state: VMTransmitLocalState::read(false),
+ })
+ }
+
+ /// Inserts a writable-end future of type `ty` and with the specified `rep`.
+ ///
+ /// Returns the table-local index of the future.
+ pub fn future_write(&mut self, ty: TypeFutureTableIndex, rep: u32) -> Result<u32> {
+ self.insert(VMHandleTableSlot::Future {
+ rep,
+ ty: ty.as_u32(),
+ state: VMTransmitLocalState::write(false),
+ })
+ }
+
+ /// Inserts the error-context `rep`, returning the index it now resides at.
+ pub fn error_context(&mut self, rep: u32) -> Result<u32> {
+ self.insert(VMHandleTableSlot::ErrorContext { rep })
+ }
+
+ /// Inserts `rep` as a guest subtask.
+ pub fn subtask_guest(&mut self, rep: u32) -> Result<u32> {
+ self.insert(VMHandleTableSlot::GuestTask { rep })
+ }
+
+ /// Inserts `rep` as a host subtask.
+ pub fn subtask_host(&mut self, rep: u32) -> Result<u32> {
+ self.insert(VMHandleTableSlot::HostTask { rep })
+ }
+
+ /// Inserts `rep` as a waitable set.
+ pub fn waitable_set(&mut self, rep: u32) -> Result<u32> {
+ self.insert(VMHandleTableSlot::WaitableSet { rep })
+ }
+}
+
+/// A table of guest threads, which compiled code never reads.
#[derive(Default)]
#[cfg(feature = "component-model-async")]
pub struct ThreadHandleTable(HandleTable);
@@ -696,8 +709,8 @@ 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, VMHandleTableSlot::GuestThread { rep })
+ // SAFETY: a `ThreadHandleTable` is never published to compiled code.
+ unsafe { self.0.insert_unpublished(counter) }.insert(VMHandleTableSlot::GuestThread { rep })
}
/// Returns the `rep` of a guest thread pointed to by `idx`.
diff --git a/crates/wasmtime/src/runtime/vm/component/resources.rs b/crates/wasmtime/src/runtime/vm/component/resources.rs
index 00ccbbd43f..1bc0c86158 100644
--- a/crates/wasmtime/src/runtime/vm/component/resources.rs
+++ b/crates/wasmtime/src/runtime/vm/component/resources.rs
@@ -20,11 +20,13 @@
//! about ABI details can be found in lifting/lowering throughout Wasmtime,
//! namely in the `Resource<T>` and `ResourceAny` types.
-use super::{HandleCounter, HandleTable, InstanceState, RemovedResource};
+use super::{HandleCounter, HandleTable, HandleTableInsertion, InstanceState, RemovedResource};
use crate::bail_bug;
use crate::component::store::ComponentTaskState;
use crate::prelude::*;
-use crate::runtime::vm::{VMResourceIndex, VMScope, VMStoreContext, VMTypedResource};
+use crate::runtime::vm::{
+ VMHandleTableSlot, VMResourceIndex, VMScope, VMStoreContext, VMTypedResource, VmPtr,
+};
use core::error::Error;
use core::fmt;
use core::mem;
@@ -56,9 +58,13 @@ pub struct ResourceTables<'a> {
/// embedder-originating operations though such as
/// `ResourceAny::resource_drop` which won't consult this table as it's
/// only operating over the host table.
+ ///
+ /// The slice holds the `VMComponentContext` entries through which compiled
+ /// code reads each instance's handle table.
pub guest: Option<(
&'a mut TryPrimaryMap<RuntimeComponentInstanceIndex, InstanceState>,
&'a ComponentTypes,
+ &'a mut [VmPtr<VMHandleTableSlot>],
)>,
/// Runtime state for resources currently owned by the host.
@@ -144,18 +150,23 @@ impl CallContext {
}
impl ResourceTables<'_> {
- fn table_for_resource(
- &mut self,
- resource: &VMTypedResource,
- ) -> (&mut HandleTable, &mut HandleCounter) {
- let table = match resource.component_ty() {
- None => &mut *self.host_table,
+ fn insertion_for_resource(&mut self, resource: &VMTypedResource) -> HandleTableInsertion<'_> {
+ let counter = &mut *self.handle_counter;
+ match resource.component_ty() {
+ // SAFETY: the host table is never published to compiled code.
+ None => unsafe { self.host_table.insert_unpublished(counter) },
Some(ty) => {
- let (states, types) = self.guest.as_mut().unwrap();
- states[types[ty].unwrap_concrete_instance()].handle_table()
+ let (states, types, published) = self.guest.as_mut().unwrap();
+ let i = types[ty].unwrap_concrete_instance();
+ // SAFETY: `published[i]` is where compiled code reads instance
+ // `i`'s handle table.
+ unsafe {
+ states[i]
+ .handle_table()
+ .insert(counter, &mut published[i.as_u32() as usize])
+ }
}
- };
- (table, &mut *self.handle_counter)
+ }
}
fn table_for_index(
@@ -165,7 +176,7 @@ impl ResourceTables<'_> {
let table = match index {
TypedResourceIndex::Host(_) => &mut *self.host_table,
TypedResourceIndex::Component { ty, .. } => {
- let (states, types) = self.guest.as_mut().unwrap();
+ let (states, types, _published) = self.guest.as_mut().unwrap();
states[types[*ty].unwrap_concrete_instance()].handle_table()
}
};
@@ -176,8 +187,8 @@ impl ResourceTables<'_> {
///
/// Note that this is the same as `resource_lower_own`.
pub fn resource_new(&mut self, resource: VMTypedResource) -> Result<u32> {
- let (table, counter) = self.table_for_resource(&resource);
- table.resource_own_insert(counter, resource)
+ self.insertion_for_resource(&resource)
+ .resource_own(resource)
}
/// Implementation of the `resource.rep` canonical intrinsic.
@@ -220,8 +231,8 @@ impl ResourceTables<'_> {
///
/// This is an implementation of the canonical ABI `lower_own` function.
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)
+ self.insertion_for_resource(&resource)
+ .resource_own(resource)
}
/// Attempts to remove an "own" handle from the specified table and its
@@ -394,8 +405,8 @@ impl ResourceTables<'_> {
let scope = self.materialize_current_scope()?;
let cx = self.task_state.call_context(scope)?;
cx.borrow_count = cx.borrow_count.checked_add(1).unwrap();
- let (table, counter) = self.table_for_resource(&resource);
- table.resource_borrow_insert(counter, resource, scope)
+ self.insertion_for_resource(&resource)
+ .resource_borrow(resource, scope)
}
/// Validates that the current scope can be exited.
diff --git a/tests/all/pooling_allocator.rs b/tests/all/pooling_allocator.rs
index 9848e052da..7ec5d88a75 100644
--- a/tests/all/pooling_allocator.rs
+++ b/tests/all/pooling_allocator.rs
@@ -884,9 +884,9 @@ fn component_instance_size_limit() -> Result<()> {
match wasmtime::component::Component::new(&engine, "(component)") {
Ok(_) => panic!("should have hit limit"),
Err(e) => {
- e.assert_contains("instance allocation for this component requires 48 bytes");
+ e.assert_contains("instance allocation for this component requires 56 bytes");
e.assert_contains("which exceeds the configured maximum of 1 bytes");
- e.assert_contains("`VMComponentContext` used 48 bytes");
+ e.assert_contains("`VMComponentContext` used 56 bytes");
}
}