A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Fix over-eager validation of payload-less futures/streams (#14952)
Alex Crichton6ac580fa040e
diff --git a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rsindex eb1b8f0472..ca7173d2bc 100644--- a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs+++ b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs@@ -267,17 +267,18 @@ fn lower<T: func::Lower + Send + 'static, B: WriteBuffer<T>, U: 'static>()};- if address % usize::try_from(T::ALIGN32)? != 0 {- bail!("read pointer not aligned");- }- lower- .as_slice_mut()- .get_mut(address..)- .and_then(|b| b.get_mut(..T::SIZE32 * count))- .ok_or_else(|| crate::format_err!("read pointer out of bounds of memory"))?;+ // Note that validation only happens with operations that have a payload.+ if let Some(ty) = ty.payload(lower.types).copied() {+ if address % usize::try_from(T::ALIGN32)? != 0 {+ bail!("read pointer not aligned");+ }+ lower+ .as_slice_mut()+ .get_mut(address..)+ .and_then(|b| b.get_mut(..T::SIZE32 * count))+ .ok_or_else(|| crate::format_err!("read pointer out of bounds of memory"))?;- if let Some(ty) = ty.payload(lower.types) {- T::linear_store_list_to_memory(lower, *ty, address, &buffer.remaining()[..count])?;+ T::linear_store_list_to_memory(lower, ty, address, &buffer.remaining()[..count])?;}if let Some(old_thread) = old_thread {diff --git a/crates/wast/src/spectest.rs b/crates/wast/src/spectest.rsindex 4f5dd541e9..9c26a9677b 100644--- a/crates/wast/src/spectest.rs+++ b/crates/wast/src/spectest.rs@@ -91,7 +91,7 @@ pub fn link_spectest<T>(pub fn link_component_spectest<T>(linker: &mut component::Linker<T>) -> Result<()> {use std::sync::Arc;use std::sync::atomic::{AtomicU32, Ordering::SeqCst};- use wasmtime::component::{Resource, ResourceType};+ use wasmtime::component::{FutureReader, Resource, ResourceType};let engine = linker.engine().clone();linker@@ -221,5 +221,8 @@ pub fn link_component_spectest<T>(linker: &mut component::Linker<T>) -> Result<(},)?;i.func_wrap("return-hi", |_cx, (): ()| Ok(("hi".to_string(),)))?;+ i.func_wrap("unit-future", |mut cx, (): ()| {+ Ok((FutureReader::new(&mut cx, async { Ok::<_, Error>(()) })?,))+ })?;Ok(())}diff --git a/tests/misc_testsuite/component-model/async/future-read-unit-host.wast b/tests/misc_testsuite/component-model/async/future-read-unit-host.wastnew file mode 100644index 0000000000..a119d92d70--- /dev/null+++ b/tests/misc_testsuite/component-model/async/future-read-unit-host.wast@@ -0,0 +1,39 @@+;;! component_model_async = true++;; Reading a payload-less future written by the host never uses the pointer+;; passed to `future.read`, so it's valid without a `memory` and with any+;; pointer.+(component+ (import "host" (instance $host+ (export "unit-future" (func (result (future))))+ ))+ (type $future (future))++ (core func $unit-future (canon lower (func $host "unit-future")))+ (core func $future.read (canon future.read $future async))+ (core func $task.return (canon task.return (result u32)))++ (core module $m+ (import "" "unit-future" (func $unit-future (result i32)))+ (import "" "future.read" (func $future.read (param i32 i32) (result i32)))+ (import "" "task.return" (func $task.return (param i32)))+ (func (export "run") (result i32)+ (call $task.return+ (call $future.read (call $unit-future) (i32.const 0x1234)))+ (i32.const 0 (; EXIT ;)))+ (func (export "cb") (param i32 i32 i32) (result i32) unreachable)+ )+ (core instance $i (instantiate $m+ (with "" (instance+ (export "unit-future" (func $unit-future))+ (export "future.read" (func $future.read))+ (export "task.return" (func $task.return))+ ))+ ))++ (func (export "run") async (result u32)+ (canon lift (core func $i "run") async (callback (core func $i "cb"))))+)++;; COMPLETED+(assert_return (invoke "run") (u32.const 0))