A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
wasip0: Consume fuel in poll_oneoff before copying subscriptions (#14481)
Alex Crichtone719487ce359
diff --git a/crates/wasi/src/p0.rs b/crates/wasi/src/p0.rsindex f9d3f904d6..0d4592e645 100644--- a/crates/wasi/src/p0.rs+++ b/crates/wasi/src/p0.rs@@ -69,7 +69,7 @@ impl wiggle::GuestErrorType for types::Errno {}}-impl<T: Snapshot1 + Send> wasi_unstable::WasiUnstable for T {+impl wasi_unstable::WasiUnstable for WasiP1Ctx {fn set_hostcall_fuel(&mut self, fuel: usize) {Snapshot1::set_hostcall_fuel(self, fuel)}@@ -518,6 +518,7 @@ impl<T: Snapshot1 + Send> wasi_unstable::WasiUnstable for T {nsubscriptions: types::Size,) -> Result<types::Size, Error> {let subs_array = subs.as_array(nsubscriptions);+ self.consume_fuel_for_array(subs_array)?;let mut old_subs = Vec::new();for slot in subs_array.iter() {let slot = slot?;diff --git a/crates/wasi/src/p1.rs b/crates/wasi/src/p1.rsindex 3559576ee9..038a4cc834 100644--- a/crates/wasi/src/p1.rs+++ b/crates/wasi/src/p1.rs@@ -166,7 +166,7 @@ impl WasiP1Ctx {/// Assumes the host is going to copy all of `array` in which case a/// corresponding amount of fuel is consumed to ensure it's not too large.- fn consume_fuel_for_array<T>(&mut self, array: wiggle::GuestPtr<[T]>) -> Result<()> {+ pub(crate) fn consume_fuel_for_array<T>(&mut self, array: wiggle::GuestPtr<[T]>) -> Result<()> {let byte_size = usize::try_from(array.len())?.checked_mul(size_of::<T>()).ok_or(types::Errno::Overflow)?;diff --git a/tests/all/cli_tests.rs b/tests/all/cli_tests.rsindex ac4953237f..63654779b3 100644--- a/tests/all/cli_tests.rs+++ b/tests/all/cli_tests.rs@@ -4151,3 +4151,16 @@ fn hostcall_fuel() -> Result<()> {}Ok(())}++#[test]+#[cfg_attr(target_pointer_width = "32", ignore)] // this test takes 4GiB virtual memory+fn wasi_snapshot0_poll_oneoff_hostcall_fuel() -> Result<()> {+ let stdout = run_wasmtime(&[+ "run",+ "-Shostcall-fuel=1000,preview0=y",+ "--invoke=run",+ "tests/all/cli_tests/poll-oneoff.wat",+ ])?;+ assert_eq!(stdout, "48\n"); // `errno::nomem`+ Ok(())+}diff --git a/tests/all/cli_tests/poll-oneoff.wat b/tests/all/cli_tests/poll-oneoff.watnew file mode 100644index 0000000000..5a0dac64ac--- /dev/null+++ b/tests/all/cli_tests/poll-oneoff.wat@@ -0,0 +1,16 @@+(module+ (import "wasi_unstable" "poll_oneoff"+ (func $poll_oneoff (param i32 i32 i32 i32) (result i32))+ )++ (memory (export "memory") 65536)++ (func (export "run") (result i32)+ i32.const 0+ i32.const 0+ ;;such a large buffer should exhaust hostcall fuel+ i32.const 200000000+ i32.const 0+ call $poll_oneoff+ )+)