A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
fix panic in Direct{Source|Destination}::remaining (#14740)
Alex Crichtonaa03698bd18f
diff --git a/crates/misc/component-async-tests/tests/scenario/streams.rs b/crates/misc/component-async-tests/tests/scenario/streams.rsindex f54c95ea15..7e6169a5fd 100644--- a/crates/misc/component-async-tests/tests/scenario/streams.rs+++ b/crates/misc/component-async-tests/tests/scenario/streams.rs@@ -53,7 +53,7 @@ impl<D, S: Stream<Item = u8> + Send + 'static> StreamProducer<D> for DirectPipeP}Poll::Ready(Some(item)) => {let mut destination = destination.as_direct(store, 1);- destination.remaining()[0] = item;+ destination.remaining()?[0] = item;destination.mark_written(1);Poll::Ready(Ok(StreamResult::Completed))}@@ -97,7 +97,7 @@ impl<D, S: Sink<u8, Error: std::error::Error + Send + Sync> + Send + 'static> StPoll::Ready(result) => {result?;let mut source = source.as_direct(store);- let item = source.remaining()[0];+ let item = source.remaining()?[0];source.mark_read(1);sink.start_send(item)?;Poll::Ready(Ok(StreamResult::Completed))diff --git a/crates/misc/component-async-tests/tests/scenario/transmit.rs b/crates/misc/component-async-tests/tests/scenario/transmit.rsindex d48b9d2d66..062260238d 100644--- a/crates/misc/component-async-tests/tests/scenario/transmit.rs+++ b/crates/misc/component-async-tests/tests/scenario/transmit.rs@@ -36,13 +36,13 @@ impl<D> StreamProducer<D> for BufferStreamProducer {_: bool,) -> Poll<Result<StreamResult>> {let me = self.get_mut();- let capacity = destination.remaining(store.as_context_mut());+ let capacity = destination.remaining(store.as_context_mut())?;if capacity == Some(0) {Poll::Ready(Ok(StreamResult::Completed))} else {assert_eq!(capacity, Some(me.buffer.len()));let mut destination = destination.as_direct(store, me.buffer.len());- destination.remaining().copy_from_slice(&me.buffer);+ destination.remaining()?.copy_from_slice(&me.buffer);destination.mark_written(me.buffer.len());Poll::Ready(Ok(StreamResult::Dropped))@@ -65,13 +65,13 @@ impl<D> StreamConsumer<D> for BufferStreamConsumer {_: bool,) -> Poll<Result<StreamResult>> {let me = self.get_mut();- let available = source.remaining(store.as_context_mut());+ let available = source.remaining(store.as_context_mut())?;if available == 0 {Poll::Ready(Ok(StreamResult::Completed))} else {assert_eq!(available, me.expected.len());let mut source = source.as_direct(store);- assert_eq!(&me.expected, source.remaining());+ assert_eq!(&me.expected, source.remaining()?);source.mark_read(me.expected.len());Poll::Ready(Ok(StreamResult::Dropped))diff --git a/crates/wasi-http/src/p3/body.rs b/crates/wasi-http/src/p3/body.rsindex 6521a3446f..ad14d1df52 100644--- a/crates/wasi-http/src/p3/body.rs+++ b/crates/wasi-http/src/p3/body.rs@@ -240,7 +240,7 @@ impl<D> StreamConsumer<D> for LimitedGuestBodyConsumer {) -> Poll<wasmtime::Result<StreamResult>> {debug_assert!(!self.closed);let mut src = src.as_direct(store);- let buf = src.remaining();+ let buf = src.remaining()?;let n = buf.len().min(self.max_chunk_size);let buf = &buf[..n];@@ -303,7 +303,7 @@ impl<D> StreamConsumer<D> for UnlimitedGuestBodyConsumer {match self.contents_tx.poll_reserve(cx) {Poll::Ready(Ok(())) => {let mut src = src.as_direct(store);- let buf = src.remaining();+ let buf = src.remaining()?;let n = buf.len().min(self.max_chunk_size);let buf = Bytes::copy_from_slice(&buf[..n]);match self.contents_tx.send_item(Ok(buf)) {@@ -529,7 +529,7 @@ wherefinish: bool,) -> Poll<wasmtime::Result<StreamResult>> {let res = 'result: {- let cap = match dst.remaining(&mut store).map(NonZeroUsize::new) {+ let cap = match dst.remaining(&mut store)?.map(NonZeroUsize::new) {Some(Some(cap)) => Some(cap),Some(None) => {// On 0-length the best we can do is check that underlying stream has not@@ -568,12 +568,12 @@ where// data frame does not fit in destination, fill it and buffer the restdst.set_buffer(frame.split_off(cap));let mut dst = dst.as_direct(store, cap);- dst.remaining().copy_from_slice(&frame);+ dst.remaining()?.copy_from_slice(&frame);dst.mark_written(cap);} else {// copy the whole frame into the destinationlet mut dst = dst.as_direct(store, n);- dst.remaining()[..n].copy_from_slice(&frame);+ dst.remaining()?[..n].copy_from_slice(&frame);dst.mark_written(n);}} else {diff --git a/crates/wasi-tls/src/p3/util/tokio_streams.rs b/crates/wasi-tls/src/p3/util/tokio_streams.rsindex bc115e1797..53899f0331 100644--- a/crates/wasi-tls/src/p3/util/tokio_streams.rs+++ b/crates/wasi-tls/src/p3/util/tokio_streams.rs@@ -50,7 +50,7 @@ where};let mut dst = dst.as_direct(store, pipe::CAPACITY);- let remaining = dst.remaining();+ let remaining = dst.remaining()?;// A zero-remaining destination means the guest is waiting for// readiness rather than requesting data. AsyncRead has no@@ -125,7 +125,7 @@ where};let mut src = src.as_direct(store);- let remaining = src.remaining();+ let remaining = src.remaining()?;if remaining.is_empty() {// Zero-length consume = readiness check. AsyncWrite has nodiff --git a/crates/wasi/src/p3/cli/host.rs b/crates/wasi/src/p3/cli/host.rsindex 47f3f81ba4..f2aa50e520 100644--- a/crates/wasi/src/p3/cli/host.rs+++ b/crates/wasi/src/p3/cli/host.rs@@ -53,12 +53,12 @@ impl<D> StreamProducer<D> for InputStreamProducer {// 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) {+ 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());+ 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))@@ -135,7 +135,7 @@ impl<D> StreamConsumer<D> for OutputStreamConsumer {}let mut src = src.as_direct(store);- let buf = src.remaining();+ 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. Thediff --git a/crates/wasi/src/p3/filesystem/host.rs b/crates/wasi/src/p3/filesystem/host.rsindex f558fbc2c8..3f70bcbfc9 100644--- a/crates/wasi/src/p3/filesystem/host.rs+++ b/crates/wasi/src/p3/filesystem/host.rs@@ -142,7 +142,7 @@ impl<D> StreamProducer<D> for ReadStreamProducer {// Once a blocking file, always a blocking file, so assert as such.assert!(self.task.is_none());let mut dst = dst.as_direct(store, DEFAULT_BUFFER_CAPACITY);- let buf = dst.remaining();+ let buf = dst.remaining()?;if buf.is_empty() {return Poll::Ready(Ok(StreamResult::Completed));}@@ -298,7 +298,7 @@ impl<D> StreamProducer<D> for ReadDirStream {// API to wait for an item to be available without taking it out of the// channel. In lieu of that just say that we're complete and ready for a// read.- if dst.remaining(&mut store) == Some(0) {+ if dst.remaining(&mut store)? == Some(0) {return Poll::Ready(Ok(StreamResult::Completed));}@@ -415,7 +415,7 @@ impl<D> StreamConsumer<D> for WriteStreamConsumer {if let Some(file) = self.file.as_blocking_file() {// Once a blocking file, always a blocking file, so assert as such.assert!(self.task.is_none());- return match self.location.write(file, src.remaining()) {+ return match self.location.write(file, src.remaining()?) {Ok(n) => {src.mark_read(n);Poll::Ready(Ok(self.complete_write(n)))@@ -427,9 +427,9 @@ impl<D> StreamConsumer<D> for WriteStreamConsumer {};}let me = &mut *self;+ let remaining = src.remaining()?;let task = me.task.get_or_insert_with(|| {debug_assert!(me.buffer.is_empty());- let remaining = src.remaining();let n = remaining.len().min(DEFAULT_BUFFER_CAPACITY);me.buffer.extend_from_slice(&remaining[..n]);let buf = mem::take(&mut me.buffer);diff --git a/crates/wasi/src/p3/mod.rs b/crates/wasi/src/p3/mod.rsindex 303bbd9f35..9d3691c7a7 100644--- a/crates/wasi/src/p3/mod.rs+++ b/crates/wasi/src/p3/mod.rs@@ -63,7 +63,7 @@ where) -> Poll<wasmtime::Result<StreamResult>> {// Take up to `count` items as requested by the guest, or pick some// reasonable-ish number for the host.- let count = dst.remaining(&mut store).unwrap_or(32);+ let count = dst.remaining(&mut store)?.unwrap_or(32);// Handle 0-length reads which test for readiness as saying "we're// always ready" since, in theory, this is.diff --git a/crates/wasi/src/p3/sockets/host/types/tcp.rs b/crates/wasi/src/p3/sockets/host/types/tcp.rsindex 9e54bda22e..c85373b441 100644--- a/crates/wasi/src/p3/sockets/host/types/tcp.rs+++ b/crates/wasi/src/p3/sockets/host/types/tcp.rs@@ -60,7 +60,7 @@ wherefinish: bool,) -> Poll<wasmtime::Result<StreamResult>> {// If the destination buffer is empty then this is a readiness check.- if dst.remaining(&mut store) == Some(0) {+ if dst.remaining(&mut store)? == Some(0) {return match self.listener.poll_ready(cx) {Poll::Ready(()) => Poll::Ready(Ok(StreamResult::Completed)),Poll::Pending if finish => Poll::Ready(Ok(StreamResult::Cancelled)),@@ -115,7 +115,7 @@ impl<D> StreamProducer<D> for ReceiveStreamProducer {};// 0-length read is a readiness check.- if dst.remaining(store.as_context_mut()) == Some(0) {+ if dst.remaining(store.as_context_mut())? == Some(0) {return match stream.poll_ready(cx) {Poll::Ready(()) => Poll::Ready(Ok(StreamResult::Completed)),Poll::Pending if finish => Poll::Ready(Ok(StreamResult::Cancelled)),@@ -124,7 +124,7 @@ impl<D> StreamProducer<D> for ReceiveStreamProducer {}let mut dst = dst.as_direct(store, DEFAULT_BUFFER_CAPACITY);- let buf = dst.remaining();+ let buf = dst.remaining()?;match stream.poll_read(cx, buf) {Poll::Ready(Ok(0)) => {self.close(Ok(()));@@ -177,7 +177,7 @@ impl<D> StreamConsumer<D> for SendStreamConsumer {let mut src = src.as_direct(store);// A 0-length write is a readiness check.- if src.remaining().is_empty() {+ if src.remaining()?.is_empty() {return match stream.poll_ready(cx) {Poll::Ready(()) => Poll::Ready(Ok(StreamResult::Completed)),Poll::Pending if finish => Poll::Ready(Ok(StreamResult::Cancelled)),@@ -185,7 +185,7 @@ impl<D> StreamConsumer<D> for SendStreamConsumer {};}- match stream.poll_write(cx, src.remaining()) {+ match stream.poll_write(cx, src.remaining()?) {Poll::Ready(Ok(n)) => {debug_assert!(n > 0);src.mark_read(n);diff --git a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rsindex 507335efd3..02665b8a09 100644--- a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs+++ b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs@@ -410,11 +410,8 @@ impl<'a, T, B> Destination<'a, T, B> {/// Readiness" in the component-model repo][docs].////// [docs]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/Concurrency.md#stream-readiness- pub fn remaining(&self, mut store: impl AsContextMut) -> Option<usize> {- // Note that this unwrap should only trigger for bugs in Wasmtime, and- // this is modeled here to centralize the `.unwrap()` for this method in- // one location.- self.remaining_(store.as_context_mut().0).unwrap()+ pub fn remaining(&self, mut store: impl AsContextMut) -> Result<Option<usize>> {+ self.remaining_(store.as_context_mut().0)}fn remaining_(&self, store: &mut StoreOpaque) -> Result<Option<usize>> {@@ -472,7 +469,7 @@ pub struct DirectDestination<'a, D: 'static> {#[cfg(feature = "std")]impl<D: 'static> std::io::Write for DirectDestination<'_, D> {fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {- let rem = self.remaining();+ let rem = self.remaining().map_err(std::io::Error::other)?;let n = rem.len().min(buf.len());rem[..n].copy_from_slice(&buf[..n]);self.mark_written(n);@@ -486,14 +483,7 @@ impl<D: 'static> std::io::Write for DirectDestination<'_, D> {impl<D: 'static> DirectDestination<'_, D> {/// Provide direct access to the writer's buffer.- pub fn remaining(&mut self) -> &mut [u8] {- // Note that this unwrap should only trigger for bugs in Wasmtime, and- // this is modeled here to centralize the `.unwrap()` for this method in- // one location.- self.remaining_().unwrap()- }-- fn remaining_(&mut self) -> Result<&mut [u8]> {+ pub fn remaining(&mut self) -> Result<&mut [u8]> {if let Some(buffer) = self.host_buffer.as_mut() {return Ok(buffer.dst);}@@ -521,7 +511,13 @@ impl<D: 'static> DirectDestination<'_, D> {let memory = instance.options_memory_mut(self.store.0, options)- .get_mut((address + guest_offset.as_usize())..)+ .get_mut(+ if count == 0 {+ 0+ } else {+ address + guest_offset.as_usize()+ }..,+ ).and_then(|b| b.get_mut(..(count.as_usize() - guest_offset.as_usize())));match memory {Some(memory) => Ok(memory),@@ -937,14 +933,11 @@ impl<'a, T> Source<'a, T> {/// Return the number of items remaining to be read from the current write/// operation.- pub fn remaining(&self, mut store: impl AsContextMut) -> usize+ pub fn remaining(&self, mut store: impl AsContextMut) -> Result<usize>whereT: 'static,{- // Note that this unwrap should only trigger for bugs in Wasmtime, and- // this is modeled here to centralize the `.unwrap()` for this method in- // one location.- self.remaining_(store.as_context_mut().0).unwrap()+ self.remaining_(store.as_context_mut().0)}fn remaining_(&self, store: &mut StoreOpaque) -> Result<usize>@@ -989,7 +982,7 @@ pub struct DirectSource<'a, D: 'static> {#[cfg(feature = "std")]impl<D: 'static> std::io::Read for DirectSource<'_, D> {fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {- let rem = self.remaining();+ let rem = self.remaining().map_err(std::io::Error::other)?;let n = rem.len().min(buf.len());buf[..n].copy_from_slice(&rem[..n]);self.mark_read(n);@@ -999,14 +992,7 @@ impl<D: 'static> std::io::Read for DirectSource<'_, D> {impl<D: 'static> DirectSource<'_, D> {/// Provide direct access to the writer's buffer.- pub fn remaining(&mut self) -> &[u8] {- // Note that this unwrap should only trigger for bugs in Wasmtime, and- // this is modeled here to centralize the `.unwrap()` for this method in- // one location.- self.remaining_().unwrap()- }-- fn remaining_(&mut self) -> Result<&[u8]> {+ pub fn remaining(&mut self) -> Result<&[u8]> {if let Some(buffer) = self.host_buffer.as_deref_mut() {return Ok(buffer.remaining());}@@ -1034,7 +1020,13 @@ impl<D: 'static> DirectSource<'_, D> {let memory = instance.options_memory(self.store.0, options)- .get((address + guest_offset.as_usize())..)+ .get(+ if count == 0 {+ 0+ } else {+ address + guest_offset.as_usize()+ }..,+ ).and_then(|b| b.get(..(count.as_usize() - guest_offset.as_usize())));match memory {Some(memory) => Ok(memory),@@ -1385,7 +1377,7 @@ impl<T> FutureReader<T> {finish))?;- Poll::Ready(Ok(if source.remaining(store) == 0 {+ Poll::Ready(Ok(if source.remaining(store)? == 0 {// Here we return `StreamResult::Completed` even though// we've consumed the last item we'll ever consume. That's// because the ABI expects `ReturnCode::Completed(1)` ratherdiff --git a/tests/all/component_model/async.rs b/tests/all/component_model/async.rsindex 5929405601..d500a934b3 100644--- a/tests/all/component_model/async.rs+++ b/tests/all/component_model/async.rs@@ -1,7 +1,7 @@use crate::async_functions::{PollOnce, execute_across_threads};use futures::stream::{FuturesUnordered, TryStreamExt as _};use std::collections::HashMap;-use std::future::Future;+use std::future::{self, Future};use std::iter;use std::mem;use std::pin::Pin;@@ -2101,6 +2101,139 @@ async fn resource_drop_call_hook_error() -> Result<()> {Ok(())}+#[tokio::test]+#[cfg_attr(miri, ignore)]+async fn zero_length_reads_and_writes() -> Result<()> {+ let engine = Engine::default();+ let component = Component::new(+ &engine,+ r#"+ (component+ (core module $libc (memory (export "mem") 1))+ (core instance $libc (instantiate $libc))++ (core module $a+ (import "" "stream.new" (func $stream.new (result i64)))+ (import "" "stream.read" (func $stream.read (param i32 i32 i32) (result i32)))+ (import "" "stream.write" (func $stream.write (param i32 i32 i32) (result i32)))+ (import "" "waitable-set.new" (func $waitable-set.new (result i32)))+ (import "" "waitable.join" (func $waitable.join (param i32 i32)))+ (import "" "task.return" (func $task.return (param i32)))++ (func (export "a") (param $stream i32) (result i32)+ (local $set i32)+ (local $tmp i64)+ (local $stream-writer i32)+ (local $stream-reader i32)+ (local $ret i32)++ (local.set $ret (call $stream.read (local.get $stream) (i32.const 0xFFFFFF) (i32.const 0)))+ (if (i32.ne (local.get $ret) (i32.const 1 (; DROPPED ;))) (then unreachable))++ (local.set $tmp (call $stream.new))+ (local.set $stream-writer (i32.wrap_i64 (i64.shr_u (local.get $tmp) (i64.const 32))))+ (local.set $stream-reader (i32.wrap_i64 (local.get $tmp)))++ (call $task.return (local.get $stream-reader))++ (local.set $ret (call $stream.write (local.get $stream-writer) (i32.const 0xFFFFFF) (i32.const 0)))+ (if (i32.ne (local.get $ret) (i32.const -1 (; BLOCKED ;))) (then unreachable))++ (local.set $set (call $waitable-set.new))+ (call $waitable.join (local.get $stream-writer) (local.get $set))+ (i32.or (i32.const 2 (; WAIT ;)) (i32.shl (local.get $set) (i32.const 4)))+ )++ (func (export "a-callback") (param i32 i32 i32) (result i32)+ (i32.const 0 (; EXIT ;))+ )+ )++ (type $stream (stream u8))+ (canon stream.new $stream (core func $stream.new))+ (canon stream.read $stream async (memory (core memory $libc "mem")) (core func $stream.read))+ (canon stream.write $stream async (memory (core memory $libc "mem")) (core func $stream.write))+ (canon waitable-set.new (core func $waitable-set.new))+ (canon waitable.join (core func $waitable.join))+ (canon task.return (result $stream) (core func $task.return))++ (core instance $a (instantiate $a+ (with "" (instance+ (export "stream.new" (func $stream.new))+ (export "stream.read" (func $stream.read))+ (export "stream.write" (func $stream.write))+ (export "waitable-set.new" (func $waitable-set.new))+ (export "waitable.join" (func $waitable.join))+ (export "task.return" (func $task.return))+ ))+ ))++ (func (export "a") async (param "stream" $stream) (result $stream)+ (canon lift (core func $a "a") async (callback (core func $a "a-callback")))+ )+ )+ "#,+ )?;+ let mut store = Store::new(&engine, ());+ let instance = Linker::new(&engine)+ .instantiate_async(&mut store, &component)+ .await?;+ let func =+ instance.get_typed_func::<(StreamReader<u8>,), (StreamReader<u8>,)>(&mut store, "a")?;++ struct Producer;++ impl StreamProducer<()> for Producer {+ type Item = u8;+ type Buffer = Option<u8>;++ fn poll_produce<'a>(+ self: Pin<&mut Self>,+ _cx: &mut Context<'_>,+ store: StoreContextMut<'a, ()>,+ destination: Destination<'a, u8, Option<u8>>,+ _finish: bool,+ ) -> Poll<Result<StreamResult>> {+ // This used to panic on zero-length reads with out-of-bounds+ // addresses:+ _ = destination.as_direct(store, 1).remaining();+ Poll::Ready(Ok(StreamResult::Dropped))+ }+ }++ struct Consumer;++ impl StreamConsumer<()> for Consumer {+ type Item = u8;++ fn poll_consume(+ self: Pin<&mut Self>,+ _cx: &mut Context<'_>,+ store: StoreContextMut<()>,+ source: Source<'_, u8>,+ _finish: bool,+ ) -> Poll<Result<StreamResult>> {+ // This used to panic on zero-length reads with out-of-bounds+ // addresses:+ _ = source.as_direct(store).remaining();+ Poll::Ready(Ok(StreamResult::Dropped))+ }+ }++ let input = StreamReader::<u8>::new(&mut store, Producer)?;+ store+ .run_concurrent(async |accessor| {+ let (output,) = func.call_concurrent(accessor, (input,)).await?;++ accessor.with(|access| output.pipe(access, Consumer))?;++ future::poll_fn(|cx| accessor.poll_no_interesting_tasks(cx)).await;++ Ok(())+ })+ .await?+}+#[test]#[cfg_attr(miri, ignore)]fn sync_call_async_typed() -> Result<()> {diff --git a/tests/all/component_model/stream_contract.rs b/tests/all/component_model/stream_contract.rsindex 0698dddce6..6bce079c88 100644--- a/tests/all/component_model/stream_contract.rs+++ b/tests/all/component_model/stream_contract.rs@@ -126,7 +126,7 @@ impl StreamProducer<()> for TestProducer {dst.set_buffer(vec![1, 2, 3].into());} else {let mut dst = dst.as_direct(store, 3);- dst.remaining().copy_from_slice(&[1, 2, 3]);+ dst.remaining()?.copy_from_slice(&[1, 2, 3]);dst.mark_written(3);}StreamResult::Completed@@ -179,7 +179,7 @@ impl StreamConsumer<()> for TestConsumer {_finish: bool,) -> Poll<Result<StreamResult>> {if self.take {- let mut buf = Vec::with_capacity(src.remaining(&mut store));+ let mut buf = Vec::with_capacity(src.remaining(&mut store)?);src.read(&mut store, &mut buf)?;self.taken.extend(buf);}