A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Bump table/memory counts for embedder-created items (#14948)
Alex Crichton8b6439143421
diff --git a/crates/wasmtime/src/runtime/externals/table.rs b/crates/wasmtime/src/runtime/externals/table.rsindex 0e7bec42d0..fbe642d889 100644--- a/crates/wasmtime/src/runtime/externals/table.rs+++ b/crates/wasmtime/src/runtime/externals/table.rs@@ -134,6 +134,7 @@ impl Table {) -> Result<Table> {init.ensure_matches_ty(store, ty.element()).context("type mismatch: value does not match table element type")?;+ store.bump_table_count()?;let table = generate_table_export(store, limiter, &ty).await?;// Tables are always allocated as all zeroes, so skip the fill below ifdiff --git a/crates/wasmtime/src/runtime/memory.rs b/crates/wasmtime/src/runtime/memory.rsindex e4f01edc37..caa357e7f2 100644--- a/crates/wasmtime/src/runtime/memory.rs+++ b/crates/wasmtime/src/runtime/memory.rs@@ -305,6 +305,7 @@ impl Memory {if ty.is_shared() {bail!("shared memories must be created through `SharedMemory`")}+ store.bump_memory_count()?;Ok(generate_memory_export(store, limiter, &ty, None).await?.unshared()diff --git a/crates/wasmtime/src/runtime/store.rs b/crates/wasmtime/src/runtime/store.rsindex ace96cef00..1041750ada 100644--- a/crates/wasmtime/src/runtime/store.rs+++ b/crates/wasmtime/src/runtime/store.rs@@ -1526,6 +1526,15 @@ fn set_fuel(*injected_fuel = -(injected as i64);}+fn bump(slot: &mut usize, max: usize, amt: usize, desc: &str) -> Result<()> {+ let new = slot.saturating_add(amt);+ if new > max {+ bail!("resource limit exceeded: {desc} count too high at {new}");+ }+ *slot = new;+ Ok(())+}+#[doc(hidden)]impl StoreOpaque {pub fn id(&self) -> StoreId {@@ -1533,15 +1542,6 @@ impl StoreOpaque {}pub fn bump_resource_counts(&mut self, module: &Module) -> Result<()> {- fn bump(slot: &mut usize, max: usize, amt: usize, desc: &str) -> Result<()> {- let new = slot.saturating_add(amt);- if new > max {- bail!("resource limit exceeded: {desc} count too high at {new}");- }- *slot = new;- Ok(())- }-let module = module.env_module();let memories = module.num_defined_memories();let tables = module.num_defined_tables();@@ -1558,6 +1558,14 @@ impl StoreOpaque {Ok(())}+ pub fn bump_memory_count(&mut self) -> Result<()> {+ bump(&mut self.memory_count, self.memory_limit, 1, "memory")+ }++ pub fn bump_table_count(&mut self) -> Result<()> {+ bump(&mut self.table_count, self.table_limit, 1, "table")+ }+#[inline]pub fn engine(&self) -> &Engine {&self.enginediff --git a/tests/all/limits.rs b/tests/all/limits.rsindex bff7d1b748..9363695e27 100644--- a/tests/all/limits.rs+++ b/tests/all/limits.rs@@ -235,6 +235,43 @@ fn test_limits_memory_only() -> Result<()> {Ok(())}+#[test]+fn test_host_memory_and_table_counts() -> Result<()> {+ let engine = Engine::default();+ let module = Module::new(&engine, r#"(module (memory 0) (table 0 funcref))"#)?;++ let mut store = Store::new(+ &engine,+ StoreLimitsBuilder::new().memories(2).tables(2).build(),+ );+ store.limiter(|s| s as &mut dyn ResourceLimiter);++ // Host-created memories and tables count against the limits...+ Memory::new(&mut store, MemoryType::new(0, None)?)?;+ Table::new(+ &mut store,+ TableType::new(RefType::FUNCREF, 0, None)?,+ Ref::Func(None),+ )?;++ // ... together with those defined by modules ...+ Instance::new(&mut store, &module, &[])?;++ // ... so no more of either can be created.+ assert!(Memory::new(&mut store, MemoryType::new(0, None)?).is_err());+ assert!(+ Table::new(+ &mut store,+ TableType::new(RefType::FUNCREF, 0, None)?,+ Ref::Func(None),+ )+ .is_err()+ );+ assert!(Instance::new(&mut store, &module, &[]).is_err());++ Ok(())+}+#[test]fn test_initial_memory_limits_exceeded() -> Result<()> {let engine = Engine::default();