Skip to content

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

Track live resources and preflight component async table allocations (#14706)

Daniel Macoveia559c7fe6c06
diff --git a/crates/wasmtime/src/runtime/component/concurrent.rs b/crates/wasmtime/src/runtime/component/concurrent.rs
index 3d684703c2..bdb4e4ce5c 100644
--- a/crates/wasmtime/src/runtime/component/concurrent.rs
+++ b/crates/wasmtime/src/runtime/component/concurrent.rs
@@ -1155,13 +1155,7 @@ impl<T> StoreContextMut<'_, T> {
/// Only intended for use in Wasmtime's own testing.
#[doc(hidden)]
pub fn concurrent_state_table_size(&mut self) -> usize {
- self.0
- .concurrent_state_mut()
- .unwrap()
- .table
- .get_mut()
- .iter_mut()
- .count()
+ self.0.concurrent_state_mut().unwrap().table.get_mut().len()
}
/// Spawn a background task to run as part of this instance's event loop.
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 5bb3163fdc..c8ca98aaa5 100644
--- a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs
+++ b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs
@@ -4929,6 +4929,9 @@ impl ConcurrentState {
&mut self,
origin: TransmitOrigin,
) -> Result<(TableId<TransmitHandle>, TableId<TransmitHandle>)> {
+ // All three entries must fit before inserting any of them so a full
+ // table does not leave an inaccessible state or handle behind.
+ self.table().ensure_capacity(3)?;
let state_id = self.push(TransmitState::new(origin))?;
let write = self.push(TransmitHandle::new(state_id))?;
diff --git a/crates/wasmtime/src/runtime/component/resource_table.rs b/crates/wasmtime/src/runtime/component/resource_table.rs
index 4f2a351ff5..af8c84d10a 100644
--- a/crates/wasmtime/src/runtime/component/resource_table.rs
+++ b/crates/wasmtime/src/runtime/component/resource_table.rs
@@ -36,7 +36,10 @@ impl core::error::Error for ResourceTableError {}
pub struct ResourceTable {
entries: Vec<Entry>,
free_head: Option<usize>,
+ // Number of reusable slots on the free list, excluding debug tombstones.
+ free_count: usize,
max_capacity: usize,
+ live: usize,
}
#[derive(Debug)]
@@ -114,15 +117,33 @@ impl ResourceTable {
ResourceTable::with_capacity(0)
}
- /// Returns whether or not this table is empty.
+ /// Returns the number of live resources in this table.
///
- /// Note that this is an `O(n)` operation, where `n` is the number of
- /// entries in the backing `Vec`.
+ /// Deleted resources, including debug tombstones, are not counted. This is
+ /// an `O(1)` operation.
+ pub fn len(&self) -> usize {
+ self.live
+ }
+
+ /// Returns whether or not this table is empty in `O(1)` time.
pub fn is_empty(&self) -> bool {
- self.entries.iter().all(|entry| match entry {
- Entry::Free { .. } => true,
- Entry::Occupied { entry } => entry.entry.downcast_ref::<Tombstone>().is_some(),
- })
+ self.len() == 0
+ }
+
+ /// Checks that a batch of pushes has enough free or appendable slots.
+ ///
+ /// Free slots are reused even if the maximum capacity has been lowered below
+ /// the number of backing slots.
+ #[cfg(any(feature = "component-model-async", test))]
+ pub(crate) fn ensure_capacity(&self, additional: usize) -> Result<(), ResourceTableError> {
+ let Some(to_append) = additional.checked_sub(self.free_count) else {
+ return Ok(());
+ };
+ if to_append > self.max_capacity.saturating_sub(self.entries.len()) {
+ Err(ResourceTableError::Full)
+ } else {
+ Ok(())
+ }
}
/// Returns the maximum capacity of this table, in elements, before adding
@@ -146,7 +167,9 @@ impl ResourceTable {
ResourceTable {
entries: Vec::with_capacity(capacity),
free_head: None,
+ free_count: 0,
max_capacity: DEFAULT_MAX_CAPACITY,
+ live: 0,
}
}
@@ -168,6 +191,7 @@ impl ResourceTable {
Entry::Free { next } => self.free_head = *next,
Entry::Occupied { .. } => unreachable!(),
}
+ self.free_count -= 1;
Some(ix)
} else {
None
@@ -176,7 +200,7 @@ impl ResourceTable {
/// Free an entry in the table, returning its [`TableEntry`]. Add the index to the free list.
fn free_entry(&mut self, ix: usize, debug: bool) -> TableEntry {
- if debug {
+ let removed_entry = if debug {
// Instead of making this entry available for reuse, we leave a
// tombstone in debug mode. This helps detect use-after-delete and
// double-delete bugs.
@@ -205,9 +229,14 @@ impl ResourceTable {
};
self.free_head = Some(ix);
+ self.free_count += 1;
entry
+ };
+ if !removed_entry.entry.is::<Tombstone>() {
+ self.live -= 1;
}
+ removed_entry
}
/// Push a new entry into the table, returning its handle. This will prefer to use free entries
@@ -215,6 +244,7 @@ impl ResourceTable {
fn push_(&mut self, e: TableEntry) -> Result<u32, ResourceTableError> {
if let Some(free) = self.pop_free_list() {
self.entries[free] = Entry::Occupied { entry: e };
+ self.live += 1;
Ok(free.try_into().unwrap())
} else {
if self.entries.len() >= self.max_capacity {
@@ -226,6 +256,7 @@ impl ResourceTable {
.try_into()
.map_err(|_| ResourceTableError::Full)?;
self.entries.push(Entry::Occupied { entry: e });
+ self.live += 1;
Ok(ix)
}
}
@@ -446,17 +477,22 @@ impl fmt::Debug for ResourceTable {
#[test]
pub fn test_free_list() {
let mut table = ResourceTable::new();
+ assert!(table.is_empty());
let x = table.push(()).unwrap();
assert_eq!(x.rep(), 0);
+ assert_eq!(table.len(), 1);
let y = table.push(()).unwrap();
assert_eq!(y.rep(), 1);
+ assert_eq!(table.len(), 2);
// Deleting x should put it on the free list, so the next entry should have the same rep.
table.delete_maybe_debug(x, false).unwrap();
+ assert_eq!(table.len(), 1);
let x = table.push(()).unwrap();
assert_eq!(x.rep(), 0);
+ assert_eq!(table.len(), 2);
// Deleting x and then y should yield indices 1 and then 0 for new entries.
table.delete_maybe_debug(x, false).unwrap();
@@ -471,6 +507,7 @@ pub fn test_free_list() {
// As the free list is empty, this entry will have a new id.
let x = table.push(()).unwrap();
assert_eq!(x.rep(), 2);
+ assert_eq!(table.len(), 3);
}
#[test]
@@ -486,6 +523,7 @@ fn test_max_capacity() {
assert_eq!(table.max_capacity(), 1);
let x = table.push(()).unwrap();
assert!(table.push(()).is_err());
+ assert_eq!(table.len(), 1);
table.set_max_capacity(0);
assert!(table.push(()).is_err());
@@ -500,6 +538,111 @@ fn test_max_capacity() {
for handle in handles {
table.delete(handle).unwrap();
}
+ assert!(table.is_empty());
table.push(()).unwrap();
}
+
+#[test]
+fn test_len_and_delete_errors() {
+ let mut table = ResourceTable::new();
+ let parent = table.push(()).unwrap();
+ let child = table.push_child(1u32, &parent).unwrap();
+ assert_eq!(table.len(), 2);
+ table.set_max_capacity(2);
+ assert!(matches!(
+ table.push_child(2u32, &parent),
+ Err(ResourceTableError::Full)
+ ));
+ assert_eq!(table.len(), 2);
+ assert!(matches!(
+ table.delete(Resource::<()>::new_own(parent.rep())),
+ Err(ResourceTableError::HasChildren)
+ ));
+ assert_eq!(table.len(), 2);
+ assert!(matches!(
+ table.push_child((), &Resource::<()>::new_own(100)),
+ Err(ResourceTableError::NotPresent)
+ ));
+ assert_eq!(table.len(), 2);
+ assert!(matches!(
+ table.delete(Resource::<()>::new_own(child.rep())),
+ Err(ResourceTableError::WrongType)
+ ));
+ assert_eq!(table.len(), 1);
+ assert!(matches!(
+ table.delete(Resource::<()>::new_own(100)),
+ Err(ResourceTableError::NotPresent)
+ ));
+ assert_eq!(table.len(), 1);
+ table.delete(parent).unwrap();
+ assert!(table.is_empty());
+}
+
+#[test]
+fn test_len_and_tombstones() {
+ let mut table = ResourceTable::new();
+ table.set_max_capacity(1);
+ let value = table.push(1u32).unwrap();
+ let rep = value.rep();
+ table.delete_maybe_debug(value, true).unwrap();
+ assert_eq!(table.len(), 0);
+ assert!(table.is_empty());
+ assert_eq!(table.entries.len(), 1);
+ assert!(matches!(
+ table.ensure_capacity(1),
+ Err(ResourceTableError::Full)
+ ));
+ assert!(matches!(
+ table.delete_maybe_debug(Resource::<u32>::new_own(rep), true),
+ Err(ResourceTableError::WrongType)
+ ));
+ assert_eq!(table.len(), 0);
+ assert!(matches!(
+ table.delete_maybe_debug(Resource::<u32>::new_own(rep), false),
+ Err(ResourceTableError::WrongType)
+ ));
+ assert_eq!(table.len(), 0);
+ table.ensure_capacity(1).unwrap();
+ assert_eq!(table.push(()).unwrap().rep(), rep);
+ assert_eq!(table.len(), 1);
+ assert!(matches!(
+ table.ensure_capacity(1),
+ Err(ResourceTableError::Full)
+ ));
+}
+
+#[test]
+fn test_batch_capacity() {
+ let mut table = ResourceTable::new();
+ table.set_max_capacity(3);
+ table.ensure_capacity(0).unwrap();
+ table.ensure_capacity(3).unwrap();
+ assert!(matches!(
+ table.ensure_capacity(4),
+ Err(ResourceTableError::Full)
+ ));
+ assert_eq!(table.entries.len(), 0);
+ let first = table.push(()).unwrap();
+ table.ensure_capacity(2).unwrap();
+ assert!(matches!(
+ table.ensure_capacity(3),
+ Err(ResourceTableError::Full)
+ ));
+ table.delete_maybe_debug(first, false).unwrap();
+ // A batch can combine free slots and new slots.
+ table.ensure_capacity(3).unwrap();
+ let handles = (0..3).map(|_| table.push(()).unwrap()).collect::<Vec<_>>();
+ for handle in handles {
+ table.delete_maybe_debug(handle, false).unwrap();
+ }
+ table.set_max_capacity(0);
+ // Reusing slots remains valid even after lowering the maximum capacity.
+ table.ensure_capacity(3).unwrap();
+ assert!(matches!(
+ table.ensure_capacity(4),
+ Err(ResourceTableError::Full)
+ ));
+ assert_eq!(table.len(), 0);
+ assert_eq!(table.entries.len(), 3);
+}
diff --git a/crates/wasmtime/src/runtime/component/store.rs b/crates/wasmtime/src/runtime/component/store.rs
index ff390ee22b..46a48eadba 100644
--- a/crates/wasmtime/src/runtime/component/store.rs
+++ b/crates/wasmtime/src/runtime/component/store.rs
@@ -602,6 +602,17 @@ impl<T> Store<T> {
///
/// Returns `None` if [`Config::concurrency_support`] is disabled.
///
+ /// Use [`ResourceTable::set_max_capacity`] on this table to bound component
+ /// async allocations, including guest-created streams, futures, and waitable
+ /// sets. Configure the capacity before instantiating components or starting
+ /// tasks: lowering it later does not evict entries or prevent reuse of
+ /// existing slots. This table is separate from any resource table maintained
+ /// by the embedder for host resources.
+ ///
+ /// [`ResourceTable::len`] returns the number of live entries in constant
+ /// time. Internal tasks, threads, and scratch waitable sets also occupy
+ /// entries; some are allocated lazily as execution requires them.
+ ///
/// [`Config::concurrency_support`]: crate::Config::concurrency_support
#[cfg(feature = "component-model-async")]
pub fn concurrent_resource_table(&mut self) -> Option<&mut ResourceTable> {
diff --git a/tests/all/async_table_capacity.rs b/tests/all/async_table_capacity.rs
new file mode 100644
index 0000000000..e52d2babab
--- /dev/null
+++ b/tests/all/async_table_capacity.rs
@@ -0,0 +1,76 @@
+use wasmtime::component::{FutureReader, ResourceTableError, StreamReader};
+use wasmtime::{Config, Engine, Result, Store};
+
+fn engine() -> Result<Engine> {
+ let mut config = Config::new();
+ config.wasm_component_model_async(true);
+ Engine::new(&config)
+}
+
+fn len<T>(store: &mut Store<T>) -> usize {
+ store.concurrent_resource_table().unwrap().len()
+}
+
+fn set_capacity<T>(store: &mut Store<T>, capacity: usize) {
+ store
+ .concurrent_resource_table()
+ .unwrap()
+ .set_max_capacity(capacity);
+}
+
+fn assert_full(error: wasmtime::Error) {
+ assert!(
+ matches!(
+ error.downcast_ref::<ResourceTableError>(),
+ Some(ResourceTableError::Full)
+ ),
+ "{error:?}"
+ );
+}
+
+fn check_capacity<R>(
+ new: impl Fn(&mut Store<()>) -> Result<R>,
+ close: impl Fn(&mut R, &mut Store<()>) -> Result<()>,
+) -> Result<()> {
+ let engine = engine()?;
+ let mut probe = Store::new(&engine, ());
+ let mut resource = new(&mut probe)?;
+ let required = len(&mut probe);
+ assert!(required > 0);
+ close(&mut resource, &mut probe)?;
+ assert_eq!(len(&mut probe), 0);
+
+ for capacity in 0..required {
+ // A fresh store has no previously allocated slots that could be reused
+ // even after lowering the capacity.
+ let mut store = Store::new(&engine, ());
+ set_capacity(&mut store, capacity);
+ for _ in 0..2 {
+ let error = match new(&mut store) {
+ Ok(_) => panic!("allocation succeeded with insufficient capacity"),
+ Err(error) => error,
+ };
+ assert_full(error);
+ assert_eq!(len(&mut store), 0);
+ }
+ set_capacity(&mut store, required);
+ for _ in 0..2 {
+ let mut resource = new(&mut store)?;
+ close(&mut resource, &mut store)?;
+ assert_eq!(len(&mut store), 0);
+ }
+ }
+ Ok(())
+}
+
+#[test]
+fn host_transmits_refuse_without_retaining_partial_allocations() -> Result<()> {
+ check_capacity(
+ |store| StreamReader::new(store, Vec::<u8>::new()),
+ |reader, store| reader.close(store),
+ )?;
+ check_capacity(
+ |store| FutureReader::new(store, async { wasmtime::error::Ok(0_u8) }),
+ |reader, store| reader.close(store),
+ )
+}
diff --git a/tests/all/main.rs b/tests/all/main.rs
index ed8c845a6e..d6bab36bac 100644
--- a/tests/all/main.rs
+++ b/tests/all/main.rs
@@ -4,6 +4,7 @@ use wasmtime::Result;
mod arrays;
mod async_functions;
+mod async_table_capacity;
mod call_hook;
mod cli_tests;
mod compile_time_builtins;