Skip to content

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

wasip3: Refine 0-length behavior in cli/sockets (#11614)

Alex Crichton92f7974c64ec
diff --git a/crates/wasi/src/p3/cli/host.rs b/crates/wasi/src/p3/cli/host.rs
index 42756263e9..0b3d5b177c 100644
--- a/crates/wasi/src/p3/cli/host.rs
+++ b/crates/wasi/src/p3/cli/host.rs
@@ -33,27 +33,34 @@ impl<D> StreamProducer<D> for InputStreamProducer {
dst: Destination<'a, Self::Item, Self::Buffer>,
finish: bool,
) -> Poll<wasmtime::Result<StreamResult>> {
- if let Some(0) = dst.remaining(store.as_context_mut()) {
- Poll::Ready(Ok(StreamResult::Completed))
- } else {
- let mut dst = dst.as_direct(store, DEFAULT_BUFFER_CAPACITY);
- let mut buf = ReadBuf::new(dst.remaining());
- match self.rx.as_mut().poll_read(cx, &mut buf) {
- Poll::Ready(Ok(())) if buf.filled().is_empty() => {
- Poll::Ready(Ok(StreamResult::Dropped))
- }
- Poll::Ready(Ok(())) => {
- let n = buf.filled().len();
- dst.mark_written(n);
- Poll::Ready(Ok(StreamResult::Completed))
- }
- Poll::Ready(Err(..)) => {
- // TODO: Report the error to the guest
- Poll::Ready(Ok(StreamResult::Dropped))
- }
- Poll::Pending if finish => Poll::Ready(Ok(StreamResult::Cancelled)),
- Poll::Pending => Poll::Pending,
+ // If the destination buffer is empty then this is a request on
+ // behalf of the guest to wait for this input stream to be readable.
+ // The `AsyncRead` trait abstraction does not provide the ability to
+ // await this event so we're forced to basically just lie here and
+ // say we're ready read data later.
+ //
+ // See WebAssembly/component-model#561 for some more information.
+ if dst.remaining(store.as_context_mut()) == Some(0) {
+ return Poll::Ready(Ok(StreamResult::Completed));
+ }
+
+ let mut dst = dst.as_direct(store, DEFAULT_BUFFER_CAPACITY);
+ let mut buf = ReadBuf::new(dst.remaining());
+ match self.rx.as_mut().poll_read(cx, &mut buf) {
+ Poll::Ready(Ok(())) if buf.filled().is_empty() => {
+ Poll::Ready(Ok(StreamResult::Dropped))
+ }
+ Poll::Ready(Ok(())) => {
+ let n = buf.filled().len();
+ dst.mark_written(n);
+ Poll::Ready(Ok(StreamResult::Completed))
+ }
+ Poll::Ready(Err(..)) => {
+ // TODO: Report the error to the guest
+ Poll::Ready(Ok(StreamResult::Dropped))
}
+ Poll::Pending if finish => Poll::Ready(Ok(StreamResult::Cancelled)),
+ Poll::Pending => Poll::Pending,
}
}
}
@@ -74,17 +81,26 @@ impl<D> StreamConsumer<D> for OutputStreamConsumer {
) -> Poll<wasmtime::Result<StreamResult>> {
let mut src = src.as_direct(store);
let buf = src.remaining();
+
+ // If the source buffer is empty then this is a request on behalf of
+ // the guest to wait for this output stream to be writable. The
+ // `AsyncWrite` trait abstraction does not provide the ability to await
+ // this event so we're forced to basically just lie here and say we're
+ // ready write data later.
+ //
+ // See WebAssembly/component-model#561 for some more information.
+ if buf.len() == 0 {
+ return Poll::Ready(Ok(StreamResult::Completed));
+ }
match self.tx.as_mut().poll_write(cx, buf) {
- Poll::Ready(Ok(n)) if buf.is_empty() => {
- debug_assert_eq!(n, 0);
- Poll::Ready(Ok(StreamResult::Completed))
- }
Poll::Ready(Ok(n)) => {
src.mark_read(n);
Poll::Ready(Ok(StreamResult::Completed))
}
- Poll::Ready(Err(..)) => {
- // TODO: Report the error to the guest
+ Poll::Ready(Err(e)) => {
+ // FIXME(WebAssembly/wasi-cli#81) should communicate this
+ // error to the guest somehow.
+ tracing::warn!("dropping stdin error: {e}");
Poll::Ready(Ok(StreamResult::Dropped))
}
Poll::Pending if finish => Poll::Ready(Ok(StreamResult::Cancelled)),
diff --git a/crates/wasi/src/p3/sockets/host/types/tcp.rs b/crates/wasi/src/p3/sockets/host/types/tcp.rs
index 6dc5caee8f..f982b75144 100644
--- a/crates/wasi/src/p3/sockets/host/types/tcp.rs
+++ b/crates/wasi/src/p3/sockets/host/types/tcp.rs
@@ -65,6 +65,16 @@ where
mut dst: Destination<'a, Self::Item, Self::Buffer>,
finish: bool,
) -> Poll<wasmtime::Result<StreamResult>> {
+ // If the destination buffer is empty then this is a request on
+ // behalf of the guest to wait for this socket to be ready to accept
+ // without actually accepting something. The `TcpListener` in Tokio does
+ // not have this capability so we're forced to lie here and say instead
+ // "yes we're ready to accept" as a fallback.
+ //
+ // See WebAssembly/component-model#561 for some more information.
+ if dst.remaining(&mut store) == Some(0) {
+ return Poll::Ready(Ok(StreamResult::Completed));
+ }
let res = match self.listener.poll_accept(cx) {
Poll::Ready(res) => res.map(|(stream, _)| stream),
Poll::Pending if finish => return Poll::Ready(Ok(StreamResult::Cancelled)),
@@ -116,37 +126,37 @@ impl<D> StreamProducer<D> for ReceiveStreamProducer {
finish: bool,
) -> Poll<wasmtime::Result<StreamResult>> {
let res = 'result: {
- if let Some(0) = dst.remaining(store.as_context_mut()) {
- match self.stream.poll_read_ready(cx) {
- Poll::Ready(Ok(())) => return Poll::Ready(Ok(StreamResult::Completed)),
+ // 0-length reads are an indication that we should wait for
+ // readiness here, so use `poll_read_ready`.
+ if dst.remaining(store.as_context_mut()) == Some(0) {
+ return match self.stream.poll_read_ready(cx) {
+ Poll::Ready(Ok(())) => Poll::Ready(Ok(StreamResult::Completed)),
Poll::Ready(Err(err)) => break 'result Err(err.into()),
- Poll::Pending if finish => {
- return Poll::Ready(Ok(StreamResult::Cancelled));
+ Poll::Pending if finish => Poll::Ready(Ok(StreamResult::Cancelled)),
+ Poll::Pending => Poll::Pending,
+ };
+ }
+
+ let mut dst = dst.as_direct(store, DEFAULT_BUFFER_CAPACITY);
+ let buf = dst.remaining();
+ loop {
+ match self.stream.try_read(buf) {
+ Ok(0) => break 'result Ok(()),
+ Ok(n) => {
+ dst.mark_written(n);
+ return Poll::Ready(Ok(StreamResult::Completed));
}
- Poll::Pending => return Poll::Pending,
- }
- } else {
- let mut dst = dst.as_direct(store, DEFAULT_BUFFER_CAPACITY);
- let buf = dst.remaining();
- loop {
- match self.stream.try_read(buf) {
- Ok(0) => break 'result Ok(()),
- Ok(n) => {
- dst.mark_written(n);
- return Poll::Ready(Ok(StreamResult::Completed));
- }
- Err(err) if err.kind() == std::io::ErrorKind::WouldBlock => {
- match self.stream.poll_read_ready(cx) {
- Poll::Ready(Ok(())) => continue,
- Poll::Ready(Err(err)) => break 'result Err(err.into()),
- Poll::Pending if finish => {
- return Poll::Ready(Ok(StreamResult::Cancelled));
- }
- Poll::Pending => return Poll::Pending,
+ Err(err) if err.kind() == std::io::ErrorKind::WouldBlock => {
+ match self.stream.poll_read_ready(cx) {
+ Poll::Ready(Ok(())) => continue,
+ Poll::Ready(Err(err)) => break 'result Err(err.into()),
+ Poll::Pending if finish => {
+ return Poll::Ready(Ok(StreamResult::Cancelled));
}
+ Poll::Pending => return Poll::Pending,
}
- Err(err) => break 'result Err(err.into()),
}
+ Err(err) => break 'result Err(err.into()),
}
}
};
@@ -190,13 +200,15 @@ impl<D> StreamConsumer<D> for SendStreamConsumer {
) -> Poll<wasmtime::Result<StreamResult>> {
let mut src = src.as_direct(store);
let res = 'result: {
+ // A 0-length write is a request to wait for readiness so use
+ // `poll_write_ready` to wait for the underlying object to be ready.
if src.remaining().is_empty() {
- match self.stream.poll_write_ready(cx) {
- Poll::Ready(Ok(())) => return Poll::Ready(Ok(StreamResult::Completed)),
+ return match self.stream.poll_write_ready(cx) {
+ Poll::Ready(Ok(())) => Poll::Ready(Ok(StreamResult::Completed)),
Poll::Ready(Err(err)) => break 'result Err(err.into()),
- Poll::Pending if finish => return Poll::Ready(Ok(StreamResult::Cancelled)),
- Poll::Pending => return Poll::Pending,
- }
+ Poll::Pending if finish => Poll::Ready(Ok(StreamResult::Cancelled)),
+ Poll::Pending => Poll::Pending,
+ };
}
loop {
match self.stream.try_write(src.remaining()) {
diff --git a/crates/wasi/tests/all/store.rs b/crates/wasi/tests/all/store.rs
index 72384a7459..5fca50c3f5 100644
--- a/crates/wasi/tests/all/store.rs
+++ b/crates/wasi/tests/all/store.rs
@@ -24,8 +24,9 @@ impl<T> Ctx<T> {
name: &str,
configure: impl FnOnce(&mut WasiCtxBuilder) -> T,
) -> Result<(Store<Ctx<T>>, TempDir)> {
- let stdout = MemoryOutputPipe::new(4096);
- let stderr = MemoryOutputPipe::new(4096);
+ const MAX_OUTPUT_SIZE: usize = 10 << 20;
+ let stdout = MemoryOutputPipe::new(MAX_OUTPUT_SIZE);
+ let stderr = MemoryOutputPipe::new(MAX_OUTPUT_SIZE);
let workspace = prepare_workspace(name)?;
// Create our wasi context.