A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Charge handle table slots, not live entries (#15032)
Alex Crichton9a430860ccaa
diff --git a/crates/cli-flags/src/lib.rs b/crates/cli-flags/src/lib.rsindex 20fb8b801b..590ead9196 100644--- a/crates/cli-flags/src/lib.rs+++ b/crates/cli-flags/src/lib.rs@@ -635,7 +635,7 @@ wasmtime_option_group! {pub max_resources: Option<usize>,/// Fuel to use for all hostcalls to limit guest<->host data transfer.pub hostcall_fuel: Option<usize>,- /// Maximum number of simultaneously-live component-model handles (e.g.+ /// Maximum number of component-model handle-table slots (e.g. for/// resources, streams, futures) per store; allocating past it traps./// (default: 100000)pub max_component_handles: Option<usize>,diff --git a/crates/wasmtime/src/runtime/component/concurrent.rs b/crates/wasmtime/src/runtime/component/concurrent.rsindex d7efe81cba..8b9d582b7f 100644--- a/crates/wasmtime/src/runtime/component/concurrent.rs+++ b/crates/wasmtime/src/runtime/component/concurrent.rs@@ -2170,7 +2170,7 @@ impl StoreOpaque {}/// Same as `instance_state`, but also returns the store's `HandleCounter`- /// for inserting into or removing from the instance's handle tables.+ /// for inserting into the instance's handle tables.fn instance_state_and_handle_counter(&mut self,instance: RuntimeInstance,@@ -2572,10 +2572,9 @@ impl StoreOpaque {let thread_data = state.get_mut(guest_thread.thread)?;let sync_call_set = thread_data.sync_call_set;if let Some(guest_id) = thread_data.instance_rep {- let (state, counter) = self.instance_state_and_handle_counter(runtime_instance);- state+ self.instance_state(runtime_instance).thread_handle_table()- .guest_thread_remove(counter, guest_id)?;+ .guest_thread_remove(guest_id)?;}let state = self.concurrent_state_mut()?;@@ -4006,9 +4005,10 @@ impl Instance {caller_instance: RuntimeComponentInstanceIndex,set: u32,) -> Result<()> {- let (state, counter) =- store.instance_state_and_handle_counter(self.runtime_instance(caller_instance));- let rep = state.handle_table().waitable_set_remove(counter, set)?;+ let rep = store+ .instance_state(self.runtime_instance(caller_instance))+ .handle_table()+ .waitable_set_remove(set)?;log::trace!("drop waitable set {rep} (handle {set})");@@ -4069,9 +4069,10 @@ impl Instance {) -> Result<()> {self.waitable_join(store, caller_instance, task_id, 0)?;- let (state, counter) =- store.instance_state_and_handle_counter(self.runtime_instance(caller_instance));- let (rep, is_host) = state.handle_table().subtask_remove(counter, task_id)?;+ let (rep, is_host) = store+ .instance_state(self.runtime_instance(caller_instance))+ .handle_table()+ .subtask_remove(task_id)?;let concurrent_state = store.concurrent_state_mut()?;let waitable = if is_host {diff --git a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rsindex c0de956b67..baa7967768 100644--- a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs+++ b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs@@ -1474,8 +1474,8 @@ pub(super) fn lift_index_to_future(match ty {InterfaceType::Future(src) => {let id = {- let (state, instance, counter) = cx.concurrent_state_and_instance_mut();- lift_index_to_transmit(instance, counter, state, TransmitIndex::Future(src), index)?+ let (state, instance) = cx.concurrent_state_and_instance_mut();+ lift_index_to_transmit(instance, state, TransmitIndex::Future(src), index)?};cx.host_resource_lower_own(id.rep(), None, None)}@@ -1865,8 +1865,8 @@ pub(super) fn lift_index_to_stream(match ty {InterfaceType::Stream(src) => {let id = {- let (state, instance, counter) = cx.concurrent_state_and_instance_mut();- lift_index_to_transmit(instance, counter, state, TransmitIndex::Stream(src), index)?+ let (state, instance) = cx.concurrent_state_and_instance_mut();+ lift_index_to_transmit(instance, state, TransmitIndex::Stream(src), index)?};cx.host_resource_lower_own(id.rep(), None, None)}@@ -3331,13 +3331,10 @@ impl Instance {ty: TransmitIndex,writer: u32,) -> Result<()> {- let (instance, counter) = self.id().get_mut_and_handle_counter(store);- let table = instance.table_for_transmit(ty);+ let table = self.id().get_mut(store).table_for_transmit(ty);let (transmit_rep, is_done) = match ty {- TransmitIndex::Future(ty) => table.future_remove_writable(counter, ty, writer)?,- TransmitIndex::Stream(ty) => {- (table.stream_remove_writable(counter, ty, writer)?, false)- }+ TransmitIndex::Future(ty) => table.future_remove_writable(ty, writer)?,+ TransmitIndex::Stream(ty) => (table.stream_remove_writable(ty, writer)?, false),};let id = TableId::<TransmitHandle>::new(transmit_rep);@@ -4366,11 +4363,10 @@ impl Instance {ty: TransmitIndex,reader: u32,) -> Result<()> {- let (instance, counter) = self.id().get_mut_and_handle_counter(store);- let table = instance.table_for_transmit(ty);+ let table = self.id().get_mut(store).table_for_transmit(ty);let (rep, _is_done) = match ty {- TransmitIndex::Stream(ty) => table.stream_remove_readable(counter, ty, reader)?,- TransmitIndex::Future(ty) => table.future_remove_readable(counter, ty, reader)?,+ TransmitIndex::Stream(ty) => table.stream_remove_readable(ty, reader)?,+ TransmitIndex::Future(ty) => table.future_remove_readable(ty, reader)?,};let kind = match ty {TransmitIndex::Stream(_) => TransmitKind::Stream,@@ -4578,11 +4574,11 @@ impl Instance {ty: TypeComponentLocalErrorContextTableIndex,error_context: u32,) -> Result<()> {- let (instance, counter) = self.id().get_mut_and_handle_counter(store);+ let instance = self.id().get_mut(store);let local_handle_table = instance.table_for_error_context(ty);- let rep = local_handle_table.error_context_drop(counter, error_context)?;+ let rep = local_handle_table.error_context_drop(error_context)?;let global_ref_count_idx = TypeComponentGlobalErrorContextTableIndex::from_u32(rep);@@ -4631,8 +4627,8 @@ impl Instance {ty: TransmitIndex,src_idx: u32,) -> Result<TableId<TransmitHandle>> {- let (state, _, counter, _, instance) = store.lift_context_parts(self);- lift_index_to_transmit(instance, counter, state.concurrent_state_mut(), ty, src_idx)+ let (state, _, _, _, instance) = store.lift_context_parts(self);+ lift_index_to_transmit(instance, state.concurrent_state_mut(), ty, src_idx)}fn lower_transmit_to_index(@@ -4739,15 +4735,14 @@ impl Instance {/// expected type of future/stream.fn lift_index_to_transmit(instance: Pin<&mut ComponentInstance>,- counter: &mut HandleCounter,concurrent_state: &mut ConcurrentState,ty: TransmitIndex,src_idx: u32,) -> Result<TableId<TransmitHandle>> {let handle_table = instance.table_for_transmit(ty);let (rep, is_done) = match ty {- TransmitIndex::Future(idx) => handle_table.future_remove_readable(counter, idx, src_idx)?,- TransmitIndex::Stream(idx) => handle_table.stream_remove_readable(counter, idx, src_idx)?,+ TransmitIndex::Future(idx) => handle_table.future_remove_readable(idx, src_idx)?,+ TransmitIndex::Stream(idx) => handle_table.stream_remove_readable(idx, src_idx)?,};let desc = match ty {TransmitIndex::Future(_) => "future",diff --git a/crates/wasmtime/src/runtime/component/func/options.rs b/crates/wasmtime/src/runtime/component/func/options.rsindex c88481611c..57163105cc 100644--- a/crates/wasmtime/src/runtime/component/func/options.rs+++ b/crates/wasmtime/src/runtime/component/func/options.rs@@ -445,15 +445,10 @@ impl<'a> LiftContext<'a> {#[cfg(feature = "component-model-async")]pub(crate) fn concurrent_state_and_instance_mut(&mut self,- ) -> (- &mut ConcurrentState,- Pin<&mut ComponentInstance>,- &mut HandleCounter,- ) {+ ) -> (&mut ConcurrentState, Pin<&mut ComponentInstance>) {(self.task_state.concurrent_state_mut(),self.instance.as_mut(),- self.handle_counter,)}diff --git a/crates/wasmtime/src/runtime/component/store.rs b/crates/wasmtime/src/runtime/component/store.rsindex 46a48eadba..1da49249ed 100644--- a/crates/wasmtime/src/runtime/component/store.rs+++ b/crates/wasmtime/src/runtime/component/store.rs@@ -26,7 +26,7 @@ use crate::{/// host doesn't mean that it's automatically susceptible to DoS for example.const DEFAULT_HOSTCALL_FUEL: usize = 128 << 20;-/// Default per-store cap on simultaneously-live component-model handles.+/// Default per-store cap on component-model handle-table slots.////// A guest creates these directly (e.g. `resource.new`), so leaving them/// unbounded is a DoS vector.@@ -568,7 +568,7 @@ impl<T> Store<T> {self.as_context_mut().set_hostcall_fuel(fuel)}- /// Returns the maximum number of simultaneously-live component-model handles+ /// Returns the maximum number of component-model handle-table slots/// permitted in this store.////// This is either the default amount if it hasn't been configured or@@ -579,7 +579,7 @@ impl<T> Store<T> {self.as_context().0.max_component_handles()}- /// Sets the maximum number of simultaneously-live component-model handles+ /// Sets the maximum number of component-model handle-table slots/// permitted in this store.////// Operations such as `resource.new` let a guest allocate handles in@@ -587,10 +587,13 @@ impl<T> Store<T> {/// as it likes. This store-wide limit caps that as a DoS mitigation;/// allocating past it traps.///- /// The limit counts *live* handles, so dropping a handle (e.g. via- /// `resource.drop`) returns budget and steady-state churn is unaffected.- /// Lowering it below the current live count does not trap existing handles,- /// only blocks new allocations until the count falls back under it.+ /// The limit counts the slots allocated in each component instance's+ /// handle table, which is what costs host memory. Dropping a handle (e.g.+ /// via `resource.drop`) frees its slot for the next handle allocated in+ /// the same instance, so steady-state churn is unaffected, but tables+ /// don't shrink: a slot freed in one instance isn't returned for other+ /// instances to use. Lowering the limit below the current count does not+ /// trap existing handles, only blocks tables from growing.////// The default value for this is 100,000 handles.pub fn set_max_component_handles(&mut self, limit: usize) {diff --git a/crates/wasmtime/src/runtime/vm/component/handle_table.rs b/crates/wasmtime/src/runtime/vm/component/handle_table.rsindex d2e4571a41..e856c8f128 100644--- a/crates/wasmtime/src/runtime/vm/component/handle_table.rs+++ b/crates/wasmtime/src/runtime/vm/component/handle_table.rs@@ -9,22 +9,24 @@ use wasmtime_environ::component::{TypeFutureTableIndex, TypeStreamTableIndex};/// currently and keeps the upper bits free for use in the component and ABI.const MAX_HANDLE: u32 = 1 << 28;-/// Live-handle count for all `HandleTable`s in a store.+/// Count of allocated slots across all `HandleTable`s in a store.///-/// The count tracks live handles (incremented in `insert`, decremented in-/// `remove`), since freed slots are reused.+/// This counts slots available to get a proxy for memory allocated on the host+/// for this table. New slots added by `insert` increment the `used` count, and+/// then that never gets decremented for the lifetime of the store as tables are+/// never shrunk.pub struct HandleCounter {- live: usize,+ used: usize,limit: usize,}impl HandleCounter {- /// Creates a counter with no live handles and the given `limit`.+ /// Creates a counter with no allocated slots and the given `limit`.pub fn new(limit: usize) -> HandleCounter {- HandleCounter { live: 0, limit }+ HandleCounter { used: 0, limit }}- /// Returns the configured limit on simultaneously-live handles.+ /// Returns the configured limit on allocated slots.pub fn limit(&self) -> usize {self.limit}@@ -181,37 +183,35 @@ impl HandleTable {bail!("cannot allocate another handle: index overflow");}- // Also enforce the store-wide cap; `MAX_HANDLE` alone scales with the- // guest-controlled instance count. See `HandleCounter`.- if counter.live >= counter.limit {- bail!("cannot allocate another handle: store handle limit exceeded");- }-+ // 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(Slot::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,_ => unreachable!(),};- // Count the handle only once its slot is committed; pairs with the- // decrement in `remove`.- counter.live += 1;-Ok(next + 1)}- fn remove(&mut self, counter: &mut HandleCounter, idx: u32) -> Result<()> {+ /// Frees the slot at `idx` for reuse by a later `insert` into this table.+ ///+ /// 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 };- // Decrement only after this `?`: `get_mut` rejects a freed slot, so a- // failed removal must not touch the counter.let slot = self.get_mut(idx)?;*slot = to_fill;self.next = idx - 1;- counter.live -= 1;Ok(())}@@ -323,11 +323,7 @@ impl HandleTable {/// This can fail if `idx` doesn't point to a resource, points to a/// resource (own or borrow) which is currently lent out, or points to a/// resource of the wrong type.- pub fn remove_resource(- &mut self,- counter: &mut HandleCounter,- idx: TypedResourceIndex,- ) -> Result<RemovedResource> {+ pub fn remove_resource(&mut self, idx: TypedResourceIndex) -> Result<RemovedResource> {let ret = match self.get_mut(idx.raw_index())? {Slot::ResourceOwn {resource,@@ -354,7 +350,7 @@ impl HandleTable {}_ => bail!("index {} is not a resource", idx.raw_index()),};- self.remove(counter, idx.raw_index())?;+ self.remove(idx.raw_index())?;Ok(ret)}@@ -428,7 +424,6 @@ impl HandleTable {/// "done" or the writable end was witnessed as being done.pub fn stream_remove_readable(&mut self,- counter: &mut HandleCounter,expected_ty: TypeStreamTableIndex,idx: u32,) -> Result<(u32, bool)> {@@ -450,14 +445,13 @@ impl HandleTable {}_ => bail!("handle is not a stream"),};- self.remove(counter, idx)?;+ self.remove(idx)?;Ok(ret)}/// Removes the writable stream handle from `idx`, returning its `rep`.pub fn stream_remove_writable(&mut self,- counter: &mut HandleCounter,expected_ty: TypeStreamTableIndex,idx: u32,) -> Result<u32> {@@ -479,7 +473,7 @@ impl HandleTable {}_ => bail!("handle is not a stream"),};- self.remove(counter, idx)?;+ self.remove(idx)?;Ok(ret)}@@ -553,7 +547,6 @@ impl HandleTable {/// "done" or the writable end was witnessed as being done.pub fn future_remove_readable(&mut self,- counter: &mut HandleCounter,expected_ty: TypeFutureTableIndex,idx: u32,) -> Result<(u32, bool)> {@@ -575,7 +568,7 @@ impl HandleTable {}_ => bail!("handle is not a future"),};- self.remove(counter, idx)?;+ self.remove(idx)?;Ok(ret)}@@ -584,7 +577,6 @@ impl HandleTable {/// value or was notified that the readable end was dropped).pub fn future_remove_writable(&mut self,- counter: &mut HandleCounter,expected_ty: TypeFutureTableIndex,idx: u32,) -> Result<(u32, bool)> {@@ -606,7 +598,7 @@ impl HandleTable {}_ => bail!("handle is not a future"),};- self.remove(counter, idx)?;+ self.remove(idx)?;Ok(ret)}@@ -627,12 +619,12 @@ impl HandleTable {/// Drops the error-context pointed to by `idx`.////// Returns the internal `rep`.- pub fn error_context_drop(&mut self, counter: &mut HandleCounter, idx: u32) -> Result<u32> {+ pub fn error_context_drop(&mut self, idx: u32) -> Result<u32> {let rep = match self.get_mut(idx)? {Slot::ErrorContext { rep } => *rep,_ => bail!("handle is not an error-context"),};- self.remove(counter, idx)?;+ self.remove(idx)?;Ok(rep)}@@ -657,13 +649,13 @@ impl HandleTable {}/// Removes the subtask set at `idx`, returning its `rep`.- pub fn subtask_remove(&mut self, counter: &mut HandleCounter, idx: u32) -> Result<(u32, bool)> {+ 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),_ => bail!("handle is not a subtask"),};- self.remove(counter, idx)?;+ self.remove(idx)?;Ok(ret)}@@ -681,12 +673,12 @@ impl HandleTable {}/// Removes the waitable set at `idx`, returning its `rep`.- pub fn waitable_set_remove(&mut self, counter: &mut HandleCounter, idx: u32) -> Result<u32> {+ pub fn waitable_set_remove(&mut self, idx: u32) -> Result<u32> {let ret = match self.get_mut(idx)? {Slot::WaitableSet { rep } => *rep,_ => bail!("handle is not a waitable-set"),};- self.remove(counter, idx)?;+ self.remove(idx)?;Ok(ret)}@@ -726,12 +718,12 @@ impl ThreadHandleTable {/// Removes the guest thread pointed to by `idx`.////// Returns the internal `rep`.- pub fn guest_thread_remove(&mut self, counter: &mut HandleCounter, idx: u32) -> Result<u32> {+ pub fn guest_thread_remove(&mut self, idx: u32) -> Result<u32> {let rep = match self.0.get_mut(idx)? {Slot::GuestThread { rep } => *rep,_ => bail!("handle is not a guest thread"),};- self.0.remove(counter, idx)?;+ self.0.remove(idx)?;Ok(rep)}}diff --git a/crates/wasmtime/src/runtime/vm/component/resources.rs b/crates/wasmtime/src/runtime/vm/component/resources.rsindex afb9922fa3..0c40fe43d0 100644--- a/crates/wasmtime/src/runtime/vm/component/resources.rs+++ b/crates/wasmtime/src/runtime/vm/component/resources.rs@@ -265,8 +265,7 @@ impl ResourceTables<'_> {/// to run. If `None` is returned then that means a `borrow` handle was/// removed and no destructor is necessary.pub fn resource_drop(&mut self, index: TypedResourceIndex) -> Result<Option<u32>> {- let (table, counter) = self.table_for_index(&index);- match table.remove_resource(counter, index)? {+ match self.table_for_index(&index).0.remove_resource(index)? {RemovedResource::Own { rep } => Ok(Some(rep)),RemovedResource::Borrow { scope } => {self.task_state.call_context(scope)?.borrow_count -= 1;@@ -298,8 +297,7 @@ impl ResourceTables<'_> {////// This is an implementation of the canonical ABI `lift_own` function.pub fn resource_lift_own(&mut self, index: TypedResourceIndex) -> Result<u32> {- let (table, counter) = self.table_for_index(&index);- match table.remove_resource(counter, index)? {+ match self.table_for_index(&index).0.remove_resource(index)? {RemovedResource::Own { rep } => Ok(rep),RemovedResource::Borrow { .. } => bail!("cannot lift own resource from a borrow"),}diff --git a/tests/all/component_model/handle_table_limit.rs b/tests/all/component_model/handle_table_limit.rsindex 850db29096..bc31bd0a9a 100644--- a/tests/all/component_model/handle_table_limit.rs+++ b/tests/all/component_model/handle_table_limit.rs@@ -10,7 +10,8 @@ use wasmtime::{Result, Store};/// Two instances of one sub-component, each with its own handle table, so the/// per-store nature of the cap can be exercised. `sprayN(n)` allocates `n` live-/// handles; `churnN(n)` allocates and drops one `n` times, staying at one live.+/// handles; `churnN(n)` allocates and drops one `n` times, staying at one live;+/// `drop-allN(n)` drops handles `1..=n`, the ones a first `sprayN(n)` returns.const COMPONENT: &str = r#"(component(component $C@@ -37,7 +38,15 @@ const COMPONENT: &str = r#"(call $drop (call $new (local.get $i)))(local.set $i (i32.add (local.get $i) (i32.const 1)))(br $l))))- (local.get $i)))+ (local.get $i))+ (func (export "drop-all") (param $n i32)+ (local $i i32)+ (loop $l+ (if (i32.lt_u (local.get $i) (local.get $n))+ (then+ (local.set $i (i32.add (local.get $i) (i32.const 1)))+ (call $drop (local.get $i))+ (br $l))))))(core instance $i (instantiate $m(with "" (instance(export "new" (func $new))@@ -45,11 +54,14 @@ const COMPONENT: &str = r#"(func (export "spray") (param "n" u32) (result u32)(canon lift (core func $i "spray")))(func (export "churn") (param "n" u32) (result u32)- (canon lift (core func $i "churn"))))+ (canon lift (core func $i "churn")))+ (func (export "drop-all") (param "n" u32)+ (canon lift (core func $i "drop-all"))))(instance $i0 (instantiate $C))(export "spray0" (func $i0 "spray"))(export "churn0" (func $i0 "churn"))+ (export "drop-all0" (func $i0 "drop-all"))(instance $i1 (instantiate $C))(export "spray1" (func $i1 "spray"))(export "churn1" (func $i1 "churn"))@@ -116,9 +128,8 @@ fn knob_raises_the_limit() -> Result<()> {Ok(())}-/// The cap counts live handles, not cumulative allocations: churning well past-/// the limit succeeds because each handle is dropped. Catches a missing-/// decrement in `remove`.+/// The cap counts table slots, not cumulative allocations: churning well past+/// the limit succeeds because each dropped handle's slot is reused.#[test]fn freeing_handles_returns_budget() -> Result<()> {let engine = engine();@@ -144,3 +155,30 @@ fn freeing_handles_returns_budget() -> Result<()> {Ok(())}++#[test]+fn freed_slots_charged() -> Result<()> {+ let engine = engine();+ let component = Component::new(&engine, COMPONENT)?;+ let linker = Linker::new(&engine);++ let mut store = Store::new(&engine, ());+ store.set_max_component_handles(20);++ let instance = linker.instantiate(&mut store, &component)?;+ let spray0 = instance.get_typed_func::<(u32,), (u32,)>(&mut store, "spray0")?;+ let drop_all0 = instance.get_typed_func::<(u32,), ()>(&mut store, "drop-all0")?;+ let spray1 = instance.get_typed_func::<(u32,), (u32,)>(&mut store, "spray1")?;++ // Grow the first instance's table to 15 slots, then free them all.+ spray0.call(&mut store, (15,))?;+ drop_all0.call(&mut store, (15,))?;++ // The second instance can't grow its table past the remaining 5.+ spray1+ .call(&mut store, (10,))+ .unwrap_err()+ .assert_contains("store handle limit exceeded");++ Ok(())+}