A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Enable limiting wasip3 resource limits (#12761)
Alex Crichton2264f72aa745
diff --git a/crates/test-programs/src/bin/p2_cli_many_resources.rs b/crates/test-programs/src/bin/p2_cli_many_resources.rsindex fd3d995e06..c4d207427c 100644--- a/crates/test-programs/src/bin/p2_cli_many_resources.rs+++ b/crates/test-programs/src/bin/p2_cli_many_resources.rs@@ -1,7 +1,8 @@use test_programs::wasi::clocks::monotonic_clock::subscribe_duration;fn main() {- loop {+ for _ in 0..1000 {std::mem::forget(subscribe_duration(1_000_000));}+ panic!("should have trapped before now");}diff --git a/crates/test-programs/src/bin/p3_cli_many_tasks.rs b/crates/test-programs/src/bin/p3_cli_many_tasks.rsnew file mode 100644index 0000000000..c2784146cb--- /dev/null+++ b/crates/test-programs/src/bin/p3_cli_many_tasks.rs@@ -0,0 +1,32 @@+use test_programs::p3::wasi as wasip3;++#[link(wasm_import_module = "wasi:clocks/[email protected]")]+unsafe extern "C" {+ #[link_name = "[async-lower]wait-for"]+ fn wait_for(dur: u64) -> u32;+}++struct Component;++test_programs::p3::export!(Component);++impl test_programs::p3::exports::wasi::cli::run::Guest for Component {+ async fn run() -> Result<(), ()> {+ // Execute it once to ensure we get type information pulled in.+ wasip3::clocks::monotonic_clock::wait_for(0).await;++ // Execute the raw function without Rust bindings to stress invoking it+ // many times.+ for _ in 0..1000 {+ unsafe {+ wait_for(1 << 60);+ }+ }++ panic!("should have trapped before now");+ }+}++fn main() {+ unreachable!();+}diff --git a/crates/wasmtime/src/runtime/component/concurrent.rs b/crates/wasmtime/src/runtime/component/concurrent.rsindex 822e3fceb4..34c9d67c4f 100644--- a/crates/wasmtime/src/runtime/component/concurrent.rs+++ b/crates/wasmtime/src/runtime/component/concurrent.rs@@ -5179,6 +5179,10 @@ impl ConcurrentState {None => bail_bug!("futures field of concurrent state is currently taken"),}}++ pub(crate) fn table(&mut self) -> &mut ResourceTable {+ self.table.get_mut()+ }}/// Provide a type hint to compiler about the shape of a parameter lowerdiff --git a/crates/wasmtime/src/runtime/component/store.rs b/crates/wasmtime/src/runtime/component/store.rsindex def9d7f3e6..d5364221f2 100644--- a/crates/wasmtime/src/runtime/component/store.rs+++ b/crates/wasmtime/src/runtime/component/store.rs@@ -1,4 +1,6 @@use crate::prelude::*;+#[cfg(feature = "component-model-async")]+use crate::runtime::component::ResourceTable;use crate::runtime::component::concurrent::ConcurrentState;use crate::runtime::component::{HostResourceData, Instance};use crate::runtime::vm;@@ -415,6 +417,15 @@ impl StoreOpaque {pub(crate) fn set_hostcall_fuel(&mut self, fuel: usize) {self.component_data_mut().hostcall_fuel = fuel;}++ #[cfg(feature = "component-model-async")]+ fn concurrent_resource_table(&mut self) -> Option<&mut ResourceTable> {+ if self.concurrency_support() {+ Some(self.concurrent_state_mut().table())+ } else {+ None+ }+ }}impl<T> Store<T> {@@ -455,6 +466,17 @@ impl<T> Store<T> {pub fn set_hostcall_fuel(&mut self, fuel: usize) {self.as_context_mut().set_hostcall_fuel(fuel)}++ /// Returns the underlying [`ResourceTable`] that the implementation of+ /// concurrency in the component model is using.+ ///+ /// Returns `None` if [`Config::concurrency_support`] is disabled.+ ///+ /// [`Config::concurrency_support`]: crate::Config::concurrency_support+ #[cfg(feature = "component-model-async")]+ pub fn concurrent_resource_table(&mut self) -> Option<&mut ResourceTable> {+ self.as_context_mut().0.concurrent_resource_table()+ }}impl<T> StoreContextMut<'_, T> {@@ -467,6 +489,12 @@ impl<T> StoreContextMut<'_, T> {pub fn set_hostcall_fuel(&mut self, fuel: usize) {self.0.set_hostcall_fuel(fuel)}++ /// See [`Store::concurrent_resource_table`].+ #[cfg(feature = "component-model-async")]+ pub fn concurrent_resource_table(&mut self) -> Option<&mut ResourceTable> {+ self.0.concurrent_resource_table()+ }}#[derive(Default)]diff --git a/src/commands/run.rs b/src/commands/run.rsindex ffb4f458a0..497da6497b 100644--- a/src/commands/run.rs+++ b/src/commands/run.rs@@ -1144,6 +1144,10 @@ impl RunCommand {let mut ctx = builder.build_p1();if let Some(max) = self.run.common.wasi.max_resources {ctx.ctx().table.set_max_capacity(max);+ #[cfg(feature = "component-model-async")]+ if let Some(table) = store.concurrent_resource_table() {+ table.set_max_capacity(max);+ }}if let Some(fuel) = self.run.common.wasi.hostcall_fuel {store.set_hostcall_fuel(fuel);diff --git a/tests/all/cli_tests.rs b/tests/all/cli_tests.rsindex a3dad513e5..b303fbc534 100644--- a/tests/all/cli_tests.rs+++ b/tests/all/cli_tests.rs@@ -2683,7 +2683,29 @@ start a print 1234#[test]fn p2_cli_many_resources() -> Result<()> {- let err = run_wasmtime(&["run", P2_CLI_MANY_RESOURCES_COMPONENT]).unwrap_err();+ let err = run_wasmtime(&[+ "run",+ "-Smax-resources=100",+ P2_CLI_MANY_RESOURCES_COMPONENT,+ ])+ .unwrap_err();+ assert!(+ err.to_string().contains("resource table has no free keys"),+ "bad error message: {err}"+ );+ Ok(())+ }++ #[test]+ fn p3_cli_many_tasks() -> Result<()> {+ let err = run_wasmtime(&[+ "run",+ "-Smax-resources=100",+ "-Sp3",+ "-Wcomponent-model-async",+ dbg!(P3_CLI_MANY_TASKS_COMPONENT),+ ])+ .unwrap_err();assert!(err.to_string().contains("resource table has no free keys"),"bad error message: {err}"