A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
fix a couple of partial read/write bugs (#11981)
Joel Diceefd56f682b6d
diff --git a/crates/misc/component-async-tests/tests/scenario/streams.rs b/crates/misc/component-async-tests/tests/scenario/streams.rsindex eb251e76a4..267403a563 100644--- a/crates/misc/component-async-tests/tests/scenario/streams.rs+++ b/crates/misc/component-async-tests/tests/scenario/streams.rs@@ -6,20 +6,23 @@ use {util::{OneshotConsumer, OneshotProducer, PipeConsumer, PipeProducer},},futures::{- Sink, SinkExt, Stream, StreamExt,+ FutureExt, Sink, SinkExt, Stream, StreamExt,channel::{mpsc, oneshot},future,},std::{+ mem,+ ops::DerefMut,pin::Pin,sync::{Arc, Mutex},- task::{Context, Poll},+ task::{self, Context, Poll},+ time::Duration,},wasmtime::{Engine, Store, StoreContextMut,component::{- Destination, FutureReader, Linker, ResourceTable, Source, StreamConsumer,- StreamProducer, StreamReader, StreamResult,+ Destination, FutureReader, Lift, Linker, ResourceTable, Source, StreamConsumer,+ StreamProducer, StreamReader, StreamResult, VecBuffer,},},wasmtime_wasi::WasiCtxBuilder,@@ -296,3 +299,172 @@ pub async fn async_closed_stream() -> Result<()> {}).await?}++struct VecProducer<T> {+ source: Vec<T>,+ sleep: Pin<Box<dyn Future<Output = ()> + Send>>,+}++impl<T> VecProducer<T> {+ fn new(source: Vec<T>, delay: bool) -> Self {+ Self {+ source,+ sleep: if delay {+ tokio::time::sleep(Duration::from_millis(10)).boxed()+ } else {+ async {}.boxed()+ },+ }+ }+}++impl<D, T: Lift + Unpin + 'static> StreamProducer<D> for VecProducer<T> {+ type Item = T;+ type Buffer = VecBuffer<T>;++ fn poll_produce(+ mut self: Pin<&mut Self>,+ cx: &mut Context<'_>,+ _: StoreContextMut<D>,+ mut destination: Destination<Self::Item, Self::Buffer>,+ _: bool,+ ) -> Poll<Result<StreamResult>> {+ let sleep = &mut self.as_mut().get_mut().sleep;+ task::ready!(sleep.as_mut().poll(cx));+ *sleep = async {}.boxed();++ destination.set_buffer(mem::take(&mut self.get_mut().source).into());+ Poll::Ready(Ok(StreamResult::Dropped))+ }+}++struct OneAtATime<T> {+ destination: Arc<Mutex<Vec<T>>>,+ sleep: Pin<Box<dyn Future<Output = ()> + Send>>,+}++impl<T> OneAtATime<T> {+ fn new(destination: Arc<Mutex<Vec<T>>>, delay: bool) -> Self {+ Self {+ destination,+ sleep: if delay {+ tokio::time::sleep(Duration::from_millis(10)).boxed()+ } else {+ async {}.boxed()+ },+ }+ }+}++impl<D, T: Lift + 'static> StreamConsumer<D> for OneAtATime<T> {+ type Item = T;++ fn poll_consume(+ mut self: Pin<&mut Self>,+ cx: &mut Context<'_>,+ store: StoreContextMut<D>,+ mut source: Source<Self::Item>,+ _: bool,+ ) -> Poll<Result<StreamResult>> {+ let sleep = &mut self.as_mut().get_mut().sleep;+ task::ready!(sleep.as_mut().poll(cx));+ *sleep = async {}.boxed();++ let value = &mut None;+ source.read(store, value)?;+ self.destination.lock().unwrap().push(value.take().unwrap());+ Poll::Ready(Ok(StreamResult::Completed))+ }+}++mod short_reads {+ wasmtime::component::bindgen!({+ path: "wit",+ world: "short-reads-guest",+ exports: { default: async | task_exit },+ });+}++#[tokio::test]+pub async fn async_short_reads() -> Result<()> {+ test_async_short_reads(false).await+}++#[tokio::test]+async fn async_short_reads_with_delay() -> Result<()> {+ test_async_short_reads(true).await+}++async fn test_async_short_reads(delay: bool) -> Result<()> {+ use short_reads::exports::local::local::short_reads::Thing;++ let engine = Engine::new(&config())?;++ let component = make_component(+ &engine,+ &[test_programs_artifacts::ASYNC_SHORT_READS_COMPONENT],+ )+ .await?;++ let mut linker = Linker::new(&engine);++ wasmtime_wasi::p2::add_to_linker_async(&mut linker)?;++ let mut store = Store::new(+ &engine,+ Ctx {+ wasi: WasiCtxBuilder::new().inherit_stdio().build(),+ table: ResourceTable::default(),+ continue_: false,+ wakers: Arc::new(Mutex::new(None)),+ },+ );++ let guest =+ short_reads::ShortReadsGuest::instantiate_async(&mut store, &component, &linker).await?;+ let thing = guest.local_local_short_reads().thing();++ let strings = ["a", "b", "c", "d", "e"];+ let mut things = Vec::with_capacity(strings.len());+ for string in strings {+ things.push(thing.call_constructor(&mut store, string).await?);+ }++ store+ .run_concurrent(async |store| {+ let count = things.len();+ let stream =+ store.with(|store| StreamReader::new(store, VecProducer::new(things, delay)));++ let (stream, task) = guest+ .local_local_short_reads()+ .call_short_reads(store, stream)+ .await?;++ let received_things = Arc::new(Mutex::new(Vec::<Thing>::with_capacity(count)));+ // Read just one item at a time from the guest, forcing it to+ // re-take ownership of any unwritten items.+ store.with(|store| stream.pipe(store, OneAtATime::new(received_things.clone(), delay)));++ task.block(store).await;++ assert_eq!(count, received_things.lock().unwrap().len());++ let mut received_strings = Vec::with_capacity(strings.len());+ let received_things = mem::take(received_things.lock().unwrap().deref_mut());+ for it in received_things {+ received_strings.push(thing.call_get(store, it).await?.0);+ }++ assert_eq!(+ &strings[..],+ &received_strings+ .iter()+ .map(|s| s.as_str())+ .collect::<Vec<_>>()+ );++ anyhow::Ok(())+ })+ .await?+}diff --git a/crates/misc/component-async-tests/tests/test_all.rs b/crates/misc/component-async-tests/tests/test_all.rsindex 1c4c76e35d..507f05148d 100644--- a/crates/misc/component-async-tests/tests/test_all.rs+++ b/crates/misc/component-async-tests/tests/test_all.rs@@ -30,7 +30,7 @@ use scenario::round_trip_many::{async_round_trip_many_stackful, async_round_trip_many_stackless,async_round_trip_many_synchronous, async_round_trip_many_wait,};-use scenario::streams::{async_closed_stream, async_closed_streams};+use scenario::streams::{async_closed_stream, async_closed_streams, async_short_reads};use scenario::transmit::{async_cancel_callee, async_cancel_caller, async_cancel_transmit, async_intertask_communication,async_poll_stackless, async_poll_synchronous, async_readiness, async_synchronous_transmit,diff --git a/crates/misc/component-async-tests/wit/test.wit b/crates/misc/component-async-tests/wit/test.witindex 88d52af746..0a0bad8e68 100644--- a/crates/misc/component-async-tests/wit/test.wit+++ b/crates/misc/component-async-tests/wit/test.wit@@ -180,6 +180,15 @@ interface closed-stream {get: func() -> stream<u8>;}+interface short-reads {+ resource thing {+ constructor(s: string);+ get: async func() -> string;+ }++ short-reads: async func(s: stream<thing>) -> stream<thing>;+}+world yield-caller {import continue;import ready;@@ -348,3 +357,7 @@ world sleep-post-return-caller {world closed-stream-guest {export closed-stream;}++world short-reads-guest {+ export short-reads;+}diff --git a/crates/test-programs/src/bin/async_short_reads.rs b/crates/test-programs/src/bin/async_short_reads.rsnew file mode 100644index 0000000000..c647e01f8a--- /dev/null+++ b/crates/test-programs/src/bin/async_short_reads.rs@@ -0,0 +1,69 @@+use {+ bindings::{+ exports::local::local::short_reads::{self, Guest, GuestThing},+ wit_stream,+ },+ wit_bindgen::{StreamReader, StreamResult, rt::async_support},+};++mod bindings {+ wit_bindgen::generate!({+ path: "../misc/component-async-tests/wit",+ world: "short-reads-guest",+ });++ use super::Component;+ export!(Component);+}++struct Thing {+ value: String,+}++impl GuestThing for Thing {+ fn new(value: String) -> Self {+ Self { value }+ }++ async fn get(&self) -> String {+ self.value.clone()+ }+}++struct Component;++impl Guest for Component {+ type Thing = Thing;++ async fn short_reads(+ mut stream: StreamReader<short_reads::Thing>,+ ) -> StreamReader<short_reads::Thing> {+ let (mut tx, rx) = wit_stream::new();++ async_support::spawn(async move {+ // Read the things one at a time, forcing the host to re-take+ // ownership of any unwritten items between writes.+ let mut things = Vec::new();+ loop {+ let (status, buffer) = stream.read(Vec::with_capacity(1)).await;+ match status {+ StreamResult::Complete(_) => {+ things.extend(buffer);+ }+ StreamResult::Dropped => break,+ StreamResult::Cancelled => unreachable!(),+ }+ }+ // Write the things all at once. The host will read them only one+ // at a time, forcing us to re-take ownership of any unwritten+ // items between writes.+ things = tx.write_all(things).await;+ assert!(things.is_empty());+ });++ rx+ }+}++// Unused function; required since this file is built as a `bin`:+fn main() {}diff --git a/crates/wasmtime/src/runtime/component/concurrent.rs b/crates/wasmtime/src/runtime/component/concurrent.rsindex cfcf8cf64c..49e9b08ba1 100644--- a/crates/wasmtime/src/runtime/component/concurrent.rs+++ b/crates/wasmtime/src/runtime/component/concurrent.rs@@ -54,9 +54,7 @@ use crate::component::func::{self, Func, Options};use crate::component::{HasData, HasSelf, Instance, Resource, ResourceTable, ResourceTableError};use crate::fiber::{self, StoreFiber, StoreFiberYield};use crate::store::{Store, StoreId, StoreInner, StoreOpaque, StoreToken};-use crate::vm::component::{- CallContext, ComponentInstance, InstanceFlags, ResourceTables, TransmitLocalState,-};+use crate::vm::component::{CallContext, ComponentInstance, InstanceFlags, ResourceTables};use crate::vm::{AlwaysMut, SendSyncPtr, VMFuncRef, VMMemoryDefinition, VMStore};use crate::{AsContext, AsContextMut, FuncType, StoreContext, StoreContextMut, ValRaw, ValType};use anyhow::{Context as _, Result, anyhow, bail};@@ -1568,7 +1566,7 @@ impl Instance {"deliver event {event:?} to {guest_task:?} for {waitable:?} (handle {handle}); set {set:?}");- waitable.on_delivery(self.id().get_mut(store), event);+ waitable.on_delivery(store, self, event);Some((event, Some((waitable, handle))))} else {@@ -4356,55 +4354,6 @@ impl Waitable {Ok(())}- /// Handle the imminent delivery of the specified event, e.g. by updating- /// the state of the stream or future.- fn on_delivery(&self, instance: Pin<&mut ComponentInstance>, event: Event) {- match event {- Event::FutureRead {- pending: Some((ty, handle)),- ..- }- | Event::FutureWrite {- pending: Some((ty, handle)),- ..- } => {- let runtime_instance = instance.component().types()[ty].instance;- let (rep, state) = instance.guest_tables().0[runtime_instance]- .future_rep(ty, handle)- .unwrap();- assert_eq!(rep, self.rep());- assert_eq!(*state, TransmitLocalState::Busy);- *state = match event {- Event::FutureRead { .. } => TransmitLocalState::Read { done: false },- Event::FutureWrite { .. } => TransmitLocalState::Write { done: false },- _ => unreachable!(),- };- }- Event::StreamRead {- pending: Some((ty, handle)),- code,- }- | Event::StreamWrite {- pending: Some((ty, handle)),- code,- } => {- let runtime_instance = instance.component().types()[ty].instance;- let (rep, state) = instance.guest_tables().0[runtime_instance]- .stream_rep(ty, handle)- .unwrap();- assert_eq!(rep, self.rep());- assert_eq!(*state, TransmitLocalState::Busy);- let done = matches!(code, ReturnCode::Dropped(_));- *state = match event {- Event::StreamRead { .. } => TransmitLocalState::Read { done },- Event::StreamWrite { .. } => TransmitLocalState::Write { done },- _ => unreachable!(),- };- }- _ => {}- }- }-/// Remove this waitable from the instance's rep table.fn delete_from(&self, state: &mut ConcurrentState) -> Result<()> {match self {diff --git a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rsindex a64ad8012c..7b30989dd1 100644--- a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs+++ b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs@@ -2212,6 +2212,7 @@ impl<T> StoreContextMut<'_, T> {let (_, read) = state.new_transmit().unwrap();let producer = Arc::new(Mutex::new(Some((Box::pin(producer), P::Buffer::default()))));let id = state.get_mut(read).unwrap().state;+ let mut dropped = false;let produce = Box::new({let producer = producer.clone();move |instance| {@@ -2221,78 +2222,78 @@ impl<T> StoreContextMut<'_, T> {let (result, cancelled) = if buffer.remaining().is_empty() {future::poll_fn(|cx| {- tls::get(|store| {- let transmit = store.concurrent_state_mut().get_mut(id).unwrap();-- let &WriteState::HostReady { cancel, .. } = &transmit.write else {- unreachable!();- };+ tls::get(|store| {+ let transmit = store.concurrent_state_mut().get_mut(id).unwrap();- let mut host_buffer =- if let ReadState::HostToHost { buffer, .. } = &mut transmit.read {- Some(Cursor::new(mem::take(buffer)))- } else {- None+ let &WriteState::HostReady { cancel, .. } = &transmit.write else {+ unreachable!();};- let poll = mine.as_mut().poll_produce(- cx,- token.as_context_mut(store),- Destination {- id,- buffer: &mut buffer,- host_buffer: host_buffer.as_mut(),- _phantom: PhantomData,- },- cancel,- );-- let transmit = store.concurrent_state_mut().get_mut(id).unwrap();+ let mut host_buffer =+ if let ReadState::HostToHost { buffer, .. } = &mut transmit.read {+ Some(Cursor::new(mem::take(buffer)))+ } else {+ None+ };++ let poll = mine.as_mut().poll_produce(+ cx,+ token.as_context_mut(store),+ Destination {+ id,+ buffer: &mut buffer,+ host_buffer: host_buffer.as_mut(),+ _phantom: PhantomData,+ },+ cancel,+ );- let host_offset = if let (- Some(host_buffer),- ReadState::HostToHost { buffer, limit, .. },- ) = (host_buffer, &mut transmit.read)- {- *limit = usize::try_from(host_buffer.position()).unwrap();- *buffer = host_buffer.into_inner();- *limit- } else {- 0- };+ let transmit = store.concurrent_state_mut().get_mut(id).unwrap();- {- let WriteState::HostReady {- guest_offset,- cancel,- cancel_waker,- ..- } = &mut transmit.write- else {- unreachable!();+ let host_offset = if let (+ Some(host_buffer),+ ReadState::HostToHost { buffer, limit, .. },+ ) = (host_buffer, &mut transmit.read)+ {+ *limit = usize::try_from(host_buffer.position()).unwrap();+ *buffer = host_buffer.into_inner();+ *limit+ } else {+ 0};- if poll.is_pending() {- if !buffer.remaining().is_empty()- || *guest_offset > 0- || host_offset > 0- {- return Poll::Ready(Err(anyhow!(- "StreamProducer::poll_produce returned Poll::Pending \- after producing at least one item"- )));+ {+ let WriteState::HostReady {+ guest_offset,+ cancel,+ cancel_waker,+ ..+ } = &mut transmit.write+ else {+ unreachable!();+ };++ if poll.is_pending() {+ if !buffer.remaining().is_empty()+ || *guest_offset > 0+ || host_offset > 0+ {+ return Poll::Ready(Err(anyhow!(+ "StreamProducer::poll_produce returned Poll::Pending \+ after producing at least one item"+ )));+ }+ *cancel_waker = Some(cx.waker().clone());+ } else {+ *cancel_waker = None;+ *cancel = false;}- *cancel_waker = Some(cx.waker().clone());- } else {- *cancel_waker = None;- *cancel = false;}- }- poll.map(|v| v.map(|result| (result, cancel)))+ poll.map(|v| v.map(|result| (result, cancel)))+ })})- })- .await?+ .await?} else {(StreamResult::Completed, false)};@@ -2332,7 +2333,9 @@ impl<T> StoreContextMut<'_, T> {);}}- StreamResult::Dropped => {}+ StreamResult::Dropped => {+ dropped = true;+ }}let write_buffer = !buffer.remaining().is_empty() || host_offset > 0;@@ -2340,10 +2343,19 @@ impl<T> StoreContextMut<'_, T> {*producer.lock().unwrap() = Some((mine, buffer));if write_buffer {- write(instance, token, id, producer, kind).await?;+ write(instance, token, id, producer.clone(), kind).await?;}- Ok(result)+ Ok(if dropped {+ if producer.lock().unwrap().as_ref().unwrap().1.remaining().is_empty()+ {+ StreamResult::Dropped+ } else {+ StreamResult::Completed+ }+ } else {+ result+ })}.boxed()}@@ -3053,7 +3065,11 @@ impl Instance {let set_guest_ready = |me: &mut ConcurrentState| {let transmit = me.get_mut(transmit_id)?;- assert!(matches!(&transmit.write, WriteState::Open));+ assert!(+ matches!(&transmit.write, WriteState::Open),+ "expected `WriteState::Open`; got `{:?}`",+ transmit.write+ );transmit.write = WriteState::GuestReady {instance: self,ty,@@ -3267,7 +3283,11 @@ impl Instance {let set_guest_ready = |me: &mut ConcurrentState| {let transmit = me.get_mut(transmit_id)?;- assert!(matches!(&transmit.read, ReadState::Open));+ assert!(+ matches!(&transmit.read, ReadState::Open),+ "expected `ReadState::Open`; got `{:?}`",+ transmit.read+ );transmit.read = ReadState::GuestReady {ty,flat_abi,@@ -3430,7 +3450,7 @@ impl Instance {if let Some(event @ (Event::StreamWrite { code, .. } | Event::FutureWrite { code, .. })) =event{- waitable.on_delivery(self.id().get_mut(store), event);+ waitable.on_delivery(store, self, event);Ok(code)} else {unreachable!()@@ -3515,7 +3535,7 @@ impl Instance {if let Some(event @ (Event::StreamRead { code, .. } | Event::FutureRead { code, .. })) =event{- waitable.on_delivery(self.id().get_mut(store), event);+ waitable.on_delivery(store, self, event);Ok(code)} else {unreachable!()@@ -4218,6 +4238,72 @@ pub(crate) struct ResourcePair {pub(crate) read: u32,}+impl Waitable {+ /// Handle the imminent delivery of the specified event, e.g. by updating+ /// the state of the stream or future.+ pub(super) fn on_delivery(&self, store: &mut StoreOpaque, instance: Instance, event: Event) {+ match event {+ Event::FutureRead {+ pending: Some((ty, handle)),+ ..+ }+ | Event::FutureWrite {+ pending: Some((ty, handle)),+ ..+ } => {+ let instance = instance.id().get_mut(store);+ let runtime_instance = instance.component().types()[ty].instance;+ let (rep, state) = instance.guest_tables().0[runtime_instance]+ .future_rep(ty, handle)+ .unwrap();+ assert_eq!(rep, self.rep());+ assert_eq!(*state, TransmitLocalState::Busy);+ *state = match event {+ Event::FutureRead { .. } => TransmitLocalState::Read { done: false },+ Event::FutureWrite { .. } => TransmitLocalState::Write { done: false },+ _ => unreachable!(),+ };+ }+ Event::StreamRead {+ pending: Some((ty, handle)),+ code,+ }+ | Event::StreamWrite {+ pending: Some((ty, handle)),+ code,+ } => {+ let instance = instance.id().get_mut(store);+ let runtime_instance = instance.component().types()[ty].instance;+ let (rep, state) = instance.guest_tables().0[runtime_instance]+ .stream_rep(ty, handle)+ .unwrap();+ assert_eq!(rep, self.rep());+ assert_eq!(*state, TransmitLocalState::Busy);+ let done = matches!(code, ReturnCode::Dropped(_));+ *state = match event {+ Event::StreamRead { .. } => TransmitLocalState::Read { done },+ Event::StreamWrite { .. } => TransmitLocalState::Write { done },+ _ => unreachable!(),+ };++ let transmit_handle = TableId::<TransmitHandle>::new(rep);+ let state = store.concurrent_state_mut();+ let transmit_id = state.get_mut(transmit_handle).unwrap().state;+ let transmit = state.get_mut(transmit_id).unwrap();++ match event {+ Event::StreamRead { .. } => {+ transmit.read = ReadState::Open;+ }+ Event::StreamWrite { .. } => transmit.write = WriteState::Open,+ _ => unreachable!(),+ };+ }+ _ => {}+ }+ }+}+#[cfg(test)]mod tests {use super::*;