Skip to content

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

async: add stream/future producers and blanket impl for `Future` (#11684)

Roman Volosatovs625a3e3fd995
diff --git a/crates/wasi-http/src/p3/body.rs b/crates/wasi-http/src/p3/body.rs
index dc1f10d311..d9b40e4212 100644
--- a/crates/wasi-http/src/p3/body.rs
+++ b/crates/wasi-http/src/p3/body.rs
@@ -2,6 +2,7 @@ use crate::p3::bindings::http::types::{ErrorCode, Fields, Trailers};
use crate::p3::{WasiHttp, WasiHttpCtxView};
use anyhow::Context as _;
use bytes::Bytes;
+use core::iter;
use core::num::NonZeroUsize;
use core::pin::Pin;
use core::task::{Context, Poll, ready};
@@ -17,7 +18,6 @@ use wasmtime::component::{
StreamProducer, StreamReader, StreamResult,
};
use wasmtime::{AsContextMut, StoreContextMut};
-use wasmtime_wasi::p3::{FutureOneshotProducer, StreamEmptyProducer};
/// The concrete type behind a `wasi:http/types/body` resource.
pub(crate) enum Body {
@@ -75,7 +75,7 @@ impl Body {
// https://github.com/WebAssembly/wasi-http/issues/176
_ = result_tx.send(Box::new(async { Ok(()) }));
Ok((
- StreamReader::new(instance, &mut store, StreamEmptyProducer::default()),
+ StreamReader::new(instance, &mut store, iter::empty()),
trailers_rx,
))
}
@@ -95,11 +95,7 @@ impl Body {
getter,
},
),
- FutureReader::new(
- instance,
- &mut store,
- FutureOneshotProducer::from(trailers_rx),
- ),
+ FutureReader::new(instance, &mut store, trailers_rx),
))
}
Body::Consumed => Err(()),
diff --git a/crates/wasi/src/p3/filesystem/host.rs b/crates/wasi/src/p3/filesystem/host.rs
index 6bba67ab56..df94cdefb7 100644
--- a/crates/wasi/src/p3/filesystem/host.rs
+++ b/crates/wasi/src/p3/filesystem/host.rs
@@ -5,16 +5,13 @@ use crate::p3::bindings::filesystem::types::{
Filesize, MetadataHashValue, NewTimestamp, OpenFlags, PathFlags,
};
use crate::p3::filesystem::{FilesystemError, FilesystemResult, preopens};
-use crate::p3::{
- DEFAULT_BUFFER_CAPACITY, FallibleIteratorProducer, FutureOneshotProducer, FutureReadyProducer,
- StreamEmptyProducer,
-};
+use crate::p3::{DEFAULT_BUFFER_CAPACITY, FallibleIteratorProducer};
use crate::{DirPerms, FilePerms};
use anyhow::Context as _;
use bytes::BytesMut;
-use core::mem;
use core::pin::Pin;
use core::task::{Context, Poll, ready};
+use core::{iter, mem};
use std::io::{self, Cursor};
use std::sync::Arc;
use system_interface::fs::FileIoExt as _;
@@ -498,12 +495,10 @@ impl types::HostDescriptorWithStore for WasiFilesystem {
let file = get_file(store.get().table, &fd)?;
if !file.perms.contains(FilePerms::READ) {
return Ok((
- StreamReader::new(instance, &mut store, StreamEmptyProducer::default()),
- FutureReader::new(
- instance,
- &mut store,
- FutureReadyProducer(Some(Err(ErrorCode::NotPermitted))),
- ),
+ StreamReader::new(instance, &mut store, iter::empty()),
+ FutureReader::new(instance, &mut store, async {
+ anyhow::Ok(Err(ErrorCode::NotPermitted))
+ }),
));
}
@@ -520,7 +515,7 @@ impl types::HostDescriptorWithStore for WasiFilesystem {
task: None,
},
),
- FutureReader::new(instance, &mut store, FutureOneshotProducer(result_rx)),
+ FutureReader::new(instance, &mut store, result_rx),
))
})
}
@@ -644,12 +639,10 @@ impl types::HostDescriptorWithStore for WasiFilesystem {
let dir = get_dir(store.get().table, &fd)?;
if !dir.perms.contains(DirPerms::READ) {
return Ok((
- StreamReader::new(instance, &mut store, StreamEmptyProducer::default()),
- FutureReader::new(
- instance,
- &mut store,
- FutureReadyProducer(Some(Err(ErrorCode::NotPermitted))),
- ),
+ StreamReader::new(instance, &mut store, iter::empty()),
+ FutureReader::new(instance, &mut store, async {
+ anyhow::Ok(Err(ErrorCode::NotPermitted))
+ }),
));
}
let allow_blocking_current_thread = dir.allow_blocking_current_thread;
@@ -667,16 +660,13 @@ impl types::HostDescriptorWithStore for WasiFilesystem {
),
Err(e) => {
result_tx.send(Err(e.into())).unwrap();
- StreamReader::new(instance, &mut store, StreamEmptyProducer::default())
+ StreamReader::new(instance, &mut store, iter::empty())
}
}
} else {
StreamReader::new(instance, &mut store, ReadDirStream::new(dir, result_tx))
};
- Ok((
- stream,
- FutureReader::new(instance, &mut store, FutureOneshotProducer(result_rx)),
- ))
+ Ok((stream, FutureReader::new(instance, &mut store, result_rx)))
})
}
diff --git a/crates/wasi/src/p3/mod.rs b/crates/wasi/src/p3/mod.rs
index 5ca938895d..5e5fac2d65 100644
--- a/crates/wasi/src/p3/mod.rs
+++ b/crates/wasi/src/p3/mod.rs
@@ -17,94 +17,15 @@ pub mod sockets;
use crate::WasiView;
use crate::p3::bindings::LinkOptions;
-use anyhow::Context as _;
-use core::marker::PhantomData;
use core::pin::Pin;
use core::task::{Context, Poll};
use tokio::sync::oneshot;
use wasmtime::StoreContextMut;
-use wasmtime::component::{
- Destination, FutureProducer, Linker, StreamProducer, StreamResult, VecBuffer,
-};
+use wasmtime::component::{Destination, Linker, StreamProducer, StreamResult, VecBuffer};
// Default buffer capacity to use for reads of byte-sized values.
const DEFAULT_BUFFER_CAPACITY: usize = 8192;
-pub struct StreamEmptyProducer<T>(PhantomData<fn(T) -> T>);
-
-impl<T> Default for StreamEmptyProducer<T> {
- fn default() -> Self {
- Self(PhantomData)
- }
-}
-
-impl<T: Send + Sync + 'static, D> StreamProducer<D> for StreamEmptyProducer<T> {
- type Item = T;
- type Buffer = Option<Self::Item>;
-
- fn poll_produce<'a>(
- self: Pin<&mut Self>,
- _: &mut Context<'_>,
- _: StoreContextMut<'a, D>,
- _: Destination<'a, Self::Item, Self::Buffer>,
- _: bool,
- ) -> Poll<wasmtime::Result<StreamResult>> {
- Poll::Ready(Ok(StreamResult::Dropped))
- }
-}
-
-struct FutureReadyProducer<T>(Option<T>);
-
-impl<T, D> FutureProducer<D> for FutureReadyProducer<T>
-where
- T: Send + Unpin + 'static,
-{
- type Item = T;
-
- fn poll_produce(
- self: Pin<&mut Self>,
- _: &mut Context<'_>,
- _: StoreContextMut<D>,
- _: bool,
- ) -> Poll<wasmtime::Result<Option<T>>> {
- let v = self
- .get_mut()
- .0
- .take()
- .context("polled after returning `Ready`")?;
- Poll::Ready(Ok(Some(v)))
- }
-}
-
-pub struct FutureOneshotProducer<T>(oneshot::Receiver<T>);
-
-impl<T> From<oneshot::Receiver<T>> for FutureOneshotProducer<T> {
- fn from(rx: oneshot::Receiver<T>) -> Self {
- Self(rx)
- }
-}
-
-impl<T, D> FutureProducer<D> for FutureOneshotProducer<T>
-where
- T: Send + 'static,
-{
- type Item = T;
-
- fn poll_produce(
- self: Pin<&mut Self>,
- cx: &mut Context<'_>,
- _: StoreContextMut<D>,
- finish: bool,
- ) -> Poll<wasmtime::Result<Option<T>>> {
- match Pin::new(&mut self.get_mut().0).poll(cx) {
- Poll::Ready(Ok(v)) => Poll::Ready(Ok(Some(v))),
- Poll::Ready(Err(err)) => Poll::Ready(Err(err).context("oneshot sender dropped")),
- Poll::Pending if finish => Poll::Ready(Ok(None)),
- Poll::Pending => Poll::Pending,
- }
- }
-}
-
/// Helper structure to convert an iterator of `Result<T, E>` into a `stream<T>`
/// plus a `future<result<_, T>>` in WIT.
///
diff --git a/crates/wasi/src/p3/sockets/host/types/tcp.rs b/crates/wasi/src/p3/sockets/host/types/tcp.rs
index abd6fa3ae4..441ce14db7 100644
--- a/crates/wasi/src/p3/sockets/host/types/tcp.rs
+++ b/crates/wasi/src/p3/sockets/host/types/tcp.rs
@@ -1,15 +1,14 @@
use super::is_addr_allowed;
+use crate::p3::DEFAULT_BUFFER_CAPACITY;
use crate::p3::bindings::sockets::types::{
Duration, ErrorCode, HostTcpSocket, HostTcpSocketWithStore, IpAddressFamily, IpSocketAddress,
TcpSocket,
};
use crate::p3::sockets::{SocketError, SocketResult, WasiSockets};
-use crate::p3::{
- DEFAULT_BUFFER_CAPACITY, FutureOneshotProducer, FutureReadyProducer, StreamEmptyProducer,
-};
use crate::sockets::{NonInheritedOptions, SocketAddrUse, SocketAddressFamily, WasiSocketsCtxView};
use anyhow::Context as _;
use bytes::BytesMut;
+use core::iter;
use core::pin::Pin;
use core::task::{Context, Poll};
use io_lifetimes::AsSocketlike as _;
@@ -351,16 +350,14 @@ impl HostTcpSocketWithStore for WasiSockets {
result: Some(result_tx),
},
),
- FutureReader::new(instance, &mut store, FutureOneshotProducer(result_rx)),
+ FutureReader::new(instance, &mut store, result_rx),
))
}
None => Ok((
- StreamReader::new(instance, &mut store, StreamEmptyProducer::default()),
- FutureReader::new(
- instance,
- &mut store,
- FutureReadyProducer(Some(Err(ErrorCode::InvalidState))),
- ),
+ StreamReader::new(instance, &mut store, iter::empty()),
+ FutureReader::new(instance, &mut store, async {
+ anyhow::Ok(Err(ErrorCode::InvalidState))
+ }),
)),
}
})
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 ee0ee17549..a693d59fea 100644
--- a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs
+++ b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs
@@ -9,21 +9,21 @@ use crate::store::{StoreOpaque, StoreToken};
use crate::vm::component::{ComponentInstance, HandleTable, TransmitLocalState};
use crate::vm::{AlwaysMut, VMStore};
use crate::{AsContextMut, StoreContextMut, ValRaw};
-use anyhow::{Context as _, Result, anyhow, bail};
+use anyhow::{Context as _, Error, Result, anyhow, bail};
use buffers::{Extender, SliceBuffer, UntypedWriteBuffer};
-use futures::FutureExt;
+use core::fmt;
+use core::future;
+use core::iter;
+use core::marker::PhantomData;
+use core::mem::{self, MaybeUninit};
+use core::pin::Pin;
+use core::task::{Context, Poll, Waker, ready};
use futures::channel::oneshot;
+use futures::{FutureExt as _, stream};
use std::boxed::Box;
-use std::fmt;
-use std::future;
use std::io::Cursor;
-use std::iter;
-use std::marker::PhantomData;
-use std::mem::{self, MaybeUninit};
-use std::pin::Pin;
use std::string::{String, ToString};
use std::sync::{Arc, Mutex};
-use std::task::{self, Context, Poll, Waker};
use std::vec::Vec;
use wasmtime_environ::component::{
CanonicalAbiInfo, ComponentTypes, InterfaceType, OptionsIndex, RuntimeComponentInstanceIndex,
@@ -509,6 +509,136 @@ pub trait StreamProducer<D>: Send + 'static {
) -> Poll<Result<StreamResult>>;
}
+impl<T, D> StreamProducer<D> for iter::Empty<T>
+where
+ T: Send + Sync + 'static,
+{
+ type Item = T;
+ type Buffer = Option<Self::Item>;
+
+ fn poll_produce<'a>(
+ self: Pin<&mut Self>,
+ _: &mut Context<'_>,
+ _: StoreContextMut<'a, D>,
+ _: Destination<'a, Self::Item, Self::Buffer>,
+ _: bool,
+ ) -> Poll<Result<StreamResult>> {
+ Poll::Ready(Ok(StreamResult::Dropped))
+ }
+}
+
+impl<T, D> StreamProducer<D> for stream::Empty<T>
+where
+ T: Send + Sync + 'static,
+{
+ type Item = T;
+ type Buffer = Option<Self::Item>;
+
+ fn poll_produce<'a>(
+ self: Pin<&mut Self>,
+ _: &mut Context<'_>,
+ _: StoreContextMut<'a, D>,
+ _: Destination<'a, Self::Item, Self::Buffer>,
+ _: bool,
+ ) -> Poll<Result<StreamResult>> {
+ Poll::Ready(Ok(StreamResult::Dropped))
+ }
+}
+
+impl<T, D> StreamProducer<D> for Vec<T>
+where
+ T: Unpin + Send + Sync + 'static,
+{
+ type Item = T;
+ type Buffer = VecBuffer<T>;
+
+ fn poll_produce<'a>(
+ self: Pin<&mut Self>,
+ _: &mut Context<'_>,
+ _: StoreContextMut<'a, D>,
+ mut dst: Destination<'a, Self::Item, Self::Buffer>,
+ _: bool,
+ ) -> Poll<Result<StreamResult>> {
+ dst.set_buffer(mem::take(self.get_mut()).into());
+ Poll::Ready(Ok(StreamResult::Dropped))
+ }
+}
+
+impl<T, D> StreamProducer<D> for Box<[T]>
+where
+ T: Unpin + Send + Sync + 'static,
+{
+ type Item = T;
+ type Buffer = VecBuffer<T>;
+
+ fn poll_produce<'a>(
+ self: Pin<&mut Self>,
+ _: &mut Context<'_>,
+ _: StoreContextMut<'a, D>,
+ mut dst: Destination<'a, Self::Item, Self::Buffer>,
+ _: bool,
+ ) -> Poll<Result<StreamResult>> {
+ dst.set_buffer(mem::take(self.get_mut()).into_vec().into());
+ Poll::Ready(Ok(StreamResult::Dropped))
+ }
+}
+
+#[cfg(feature = "component-model-async-bytes")]
+impl<D> StreamProducer<D> for bytes::Bytes {
+ type Item = u8;
+ type Buffer = Cursor<Self>;
+
+ fn poll_produce<'a>(
+ mut self: Pin<&mut Self>,
+ _: &mut Context<'_>,
+ mut store: StoreContextMut<'a, D>,
+ mut dst: Destination<'a, Self::Item, Self::Buffer>,
+ _: bool,
+ ) -> Poll<Result<StreamResult>> {
+ let cap = dst.remaining(&mut store);
+ let Some(cap) = cap.and_then(core::num::NonZeroUsize::new) else {
+ // on 0-length or host reads, buffer the bytes
+ dst.set_buffer(Cursor::new(mem::take(self.get_mut())));
+ return Poll::Ready(Ok(StreamResult::Dropped));
+ };
+ let cap = cap.into();
+ // data does not fit in destination, fill it and buffer the rest
+ dst.set_buffer(Cursor::new(self.split_off(cap)));
+ let mut dst = dst.as_direct(store, cap);
+ dst.remaining().copy_from_slice(&self);
+ dst.mark_written(cap);
+ Poll::Ready(Ok(StreamResult::Dropped))
+ }
+}
+
+#[cfg(feature = "component-model-async-bytes")]
+impl<D> StreamProducer<D> for bytes::BytesMut {
+ type Item = u8;
+ type Buffer = Cursor<Self>;
+
+ fn poll_produce<'a>(
+ mut self: Pin<&mut Self>,
+ _: &mut Context<'_>,
+ mut store: StoreContextMut<'a, D>,
+ mut dst: Destination<'a, Self::Item, Self::Buffer>,
+ _: bool,
+ ) -> Poll<Result<StreamResult>> {
+ let cap = dst.remaining(&mut store);
+ let Some(cap) = cap.and_then(core::num::NonZeroUsize::new) else {
+ // on 0-length or host reads, buffer the bytes
+ dst.set_buffer(Cursor::new(mem::take(self.get_mut())));
+ return Poll::Ready(Ok(StreamResult::Dropped));
+ };
+ let cap = cap.into();
+ // data does not fit in destination, fill it and buffer the rest
+ dst.set_buffer(Cursor::new(self.split_off(cap)));
+ let mut dst = dst.as_direct(store, cap);
+ dst.remaining().copy_from_slice(&self);
+ dst.mark_written(cap);
+ Poll::Ready(Ok(StreamResult::Dropped))
+ }
+}
+
/// Represents the buffer for a host- or guest-initiated stream write.
pub struct Source<'a, T> {
instance: Instance,
@@ -818,6 +948,28 @@ pub trait FutureProducer<D>: Send + 'static {
) -> Poll<Result<Option<Self::Item>>>;
}
+impl<T, E, D, Fut> FutureProducer<D> for Fut
+where
+ E: Into<Error>,
+ Fut: Future<Output = Result<T, E>> + ?Sized + Send + 'static,
+{
+ type Item = T;
+
+ fn poll_produce<'a>(
+ self: Pin<&mut Self>,
+ cx: &mut Context<'_>,
+ _: StoreContextMut<'a, D>,
+ finish: bool,
+ ) -> Poll<Result<Option<T>>> {
+ match self.poll(cx) {
+ Poll::Ready(Ok(v)) => Poll::Ready(Ok(Some(v))),
+ Poll::Ready(Err(err)) => Poll::Ready(Err(err.into())),
+ Poll::Pending if finish => Poll::Ready(Ok(None)),
+ Poll::Pending => Poll::Pending,
+ }
+ }
+}
+
/// Represents a host-owned read end of a future.
pub trait FutureConsumer<D>: Send + 'static {
/// The payload type of this future.
@@ -885,7 +1037,7 @@ impl<T> FutureReader<T> {
let producer = unsafe { self.map_unchecked_mut(|v| &mut v.0) };
Poll::Ready(Ok(
- if let Some(value) = task::ready!(producer.poll_produce(cx, store, finish))? {
+ if let Some(value) = ready!(producer.poll_produce(cx, store, finish))? {
destination.set_buffer(Some(value));
// Here we return `StreamResult::Completed` even though
@@ -946,7 +1098,7 @@ impl<T> FutureReader<T> {
// out of `self`.
let consumer = unsafe { self.map_unchecked_mut(|v| &mut v.0) };
- task::ready!(consumer.poll_consume(
+ ready!(consumer.poll_consume(
cx,
store.as_context_mut(),
source.reborrow(),
@@ -4019,3 +4171,90 @@ pub(crate) struct ResourcePair {
pub(crate) write: u32,
pub(crate) read: u32,
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::{Engine, Store};
+ use core::future::pending;
+ use core::pin::pin;
+ use std::sync::LazyLock;
+
+ static ENGINE: LazyLock<Engine> = LazyLock::new(Engine::default);
+
+ fn poll_future_producer<T>(rx: Pin<&mut T>, finish: bool) -> Poll<Result<Option<T::Item>>>
+ where
+ T: FutureProducer<()>,
+ {
+ rx.poll_produce(
+ &mut Context::from_waker(Waker::noop()),
+ Store::new(&ENGINE, ()).as_context_mut(),
+ finish,
+ )
+ }
+
+ #[test]
+ fn future_producer() {
+ let mut fut = pin!(async { anyhow::Ok(()) });
+ assert!(matches!(
+ poll_future_producer(fut.as_mut(), false),
+ Poll::Ready(Ok(Some(()))),
+ ));
+
+ let mut fut = pin!(async { anyhow::Ok(()) });
+ assert!(matches!(
+ poll_future_producer(fut.as_mut(), true),
+ Poll::Ready(Ok(Some(()))),
+ ));
+
+ let mut fut = pin!(pending::<Result<()>>());
+ assert!(matches!(
+ poll_future_producer(fut.as_mut(), false),
+ Poll::Pending,
+ ));
+ assert!(matches!(
+ poll_future_producer(fut.as_mut(), true),
+ Poll::Ready(Ok(None)),
+ ));
+
+ let (tx, rx) = oneshot::channel();
+ let mut rx = pin!(rx);
+ assert!(matches!(
+ poll_future_producer(rx.as_mut(), false),
+ Poll::Pending,
+ ));
+ assert!(matches!(
+ poll_future_producer(rx.as_mut(), true),
+ Poll::Ready(Ok(None)),
+ ));
+ tx.send(()).unwrap();
+ assert!(matches!(
+ poll_future_producer(rx.as_mut(), true),
+ Poll::Ready(Ok(Some(()))),
+ ));
+
+ let (tx, rx) = oneshot::channel();
+ let mut rx = pin!(rx);
+ tx.send(()).unwrap();
+ assert!(matches!(
+ poll_future_producer(rx.as_mut(), false),
+ Poll::Ready(Ok(Some(()))),
+ ));
+
+ let (tx, rx) = oneshot::channel::<()>();
+ let mut rx = pin!(rx);
+ drop(tx);
+ assert!(matches!(
+ poll_future_producer(rx.as_mut(), false),
+ Poll::Ready(Err(..)),
+ ));
+
+ let (tx, rx) = oneshot::channel::<()>();
+ let mut rx = pin!(rx);
+ drop(tx);
+ assert!(matches!(
+ poll_future_producer(rx.as_mut(), true),
+ Poll::Ready(Err(..)),
+ ));
+ }
+}