A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Fix a stacked borrows violation in futures/streams copy (#12872)
Alex Crichton425a601764b9
diff --git a/crates/wasi-http/tests/all/p3/mod.rs b/crates/wasi-http/tests/all/p3/mod.rsindex 70c85e4f0c..d46bd157a0 100644--- a/crates/wasi-http/tests/all/p3/mod.rs+++ b/crates/wasi-http/tests/all/p3/mod.rs@@ -322,7 +322,8 @@ async fn p3_http_middleware() -> Result<()> {async fn p3_http_middleware_host_to_host() {let error = format!("{:?}", test_http_middleware(true).await.unwrap_err());- let expected = "cannot read from and write to intra-component future with non-numeric payload";+ let expected =+ "cannot read from and write to intra-component future/stream with non-numeric payload";assert!(error.contains(expected),diff --git a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rsindex a34f769ca6..a20fa34a98 100644--- a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs+++ b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs@@ -116,14 +116,20 @@ impl TransmitIndex {TransmitIndex::Future(_) => TransmitKind::Future,}}-}-/// Retrieve the payload type of the specified stream or future, or `None` if it-/// has no payload type.-fn payload(ty: TransmitIndex, types: &ComponentTypes) -> Option<InterfaceType> {- match ty {- TransmitIndex::Future(ty) => types[types[ty].ty].payload,- TransmitIndex::Stream(ty) => types[types[ty].ty].payload,+ /// Retrieve the payload type of the specified stream or future, or `None`+ /// if it has no payload type.+ fn payload<'a>(&self, types: &'a ComponentTypes) -> Option<&'a InterfaceType> {+ match self {+ TransmitIndex::Stream(i) => {+ let ty = types[*i].ty;+ types[ty].payload.as_ref()+ }+ TransmitIndex::Future(i) => {+ let ty = types[*i].ty;+ types[ty].payload.as_ref()+ }+ }}}@@ -177,8 +183,8 @@ fn lower<T: func::Lower + Send + 'static, B: WriteBuffer<T>, U: 'static>(.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) = payload(ty, lower.types) {- T::linear_store_list_to_memory(lower, ty, address, &buffer.remaining()[..count])?;+ if let Some(ty) = ty.payload(lower.types) {+ T::linear_store_list_to_memory(lower, *ty, address, &buffer.remaining()[..count])?;}if let Some(old_thread) = old_thread {@@ -805,11 +811,11 @@ impl<'a, T> Source<'a, T> {};let cx = &mut LiftContext::new(store.0.store_opaque_mut(), options, instance);- let ty = payload(ty, cx.types);+ let ty = ty.payload(cx.types);let old_remaining = buffer.remaining_capacity();lift::<T, B>(cx,- ty,+ ty.copied(),buffer,address + (T::SIZE32 * guest_offset),count - guest_offset,@@ -3173,7 +3179,7 @@ impl Instance {/// specified stream or future.fn copy<T: 'static>(self,- mut store: StoreContextMut<T>,+ store: StoreContextMut<T>,flat_abi: Option<FlatAbi>,write_caller_instance: RuntimeComponentInstanceIndex,write_ty: TransmitIndex,@@ -3187,43 +3193,68 @@ impl Instance {count: usize,rep: u32,) -> Result<()> {- let types = self.id().get(store.0).component().types();+ let (component, mut store) = self.component_and_store_mut(store.0);+ let types = component.types();++ // Validate `write_ty` w.r.t. `write_address` to ensure it's properly+ // aligned and in-bounds.+ let write_payload_ty = write_ty.payload(types);+ let write_abi = match write_payload_ty {+ Some(ty) => types.canonical_abi(ty),+ None => &CanonicalAbiInfo::ZERO,+ };+ let write_length_in_bytes = match flat_abi {+ Some(abi) => usize::try_from(abi.size)? * count,+ None => usize::try_from(write_abi.size32)? * count,+ };+ if write_length_in_bytes > 0 {+ if write_address % usize::try_from(write_abi.align32)? != 0 {+ bail!("write pointer not aligned");+ }+ self.options_memory(store, write_options)+ .get(write_address..)+ .and_then(|b| b.get(..write_length_in_bytes))+ .ok_or_else(|| crate::format_err!("write pointer out of bounds"))?;+ }++ let read_payload_ty = read_ty.payload(types);+ let read_abi = match read_payload_ty {+ Some(ty) => types.canonical_abi(ty),+ None => &CanonicalAbiInfo::ZERO,+ };+ let read_length_in_bytes = match flat_abi {+ Some(abi) => usize::try_from(abi.size)? * count,+ None => usize::try_from(read_abi.size32)? * count,+ };+ if read_length_in_bytes > 0 {+ if read_address % usize::try_from(read_abi.align32)? != 0 {+ bail!("read pointer not aligned");+ }+ self.options_memory(store, read_options)+ .get(read_address..)+ .and_then(|b| b.get(..read_length_in_bytes))+ .ok_or_else(|| crate::format_err!("read pointer out of bounds"))?;+ }++ if write_caller_instance == read_caller_instance+ && !allow_intra_component_read_write(write_payload_ty)+ {+ bail!(+ "cannot read from and write to intra-component future/stream with non-numeric payload"+ )+ }+match (write_ty, read_ty) {- (TransmitIndex::Future(write_ty), TransmitIndex::Future(read_ty)) => {+ (TransmitIndex::Future(_), TransmitIndex::Future(_)) => {if count != 1 {bail_bug!("futures can only send 1 item");}- let payload = types[types[write_ty].ty].payload;-- if write_caller_instance == read_caller_instance- && !allow_intra_component_read_write(payload)- {- bail!(- "cannot read from and write to intra-component future with non-numeric payload"- )- }-- let val = payload+ let val = write_payload_ty.map(|ty| {- let lift =- &mut LiftContext::new(store.0.store_opaque_mut(), write_options, self);-- let abi = lift.types.canonical_abi(&ty);- // FIXME: needs to read an i64 for memory64- if write_address % usize::try_from(abi.align32)? != 0 {- bail!("write pointer not aligned");- }-- let bytes = lift- .memory()- .get(write_address..)- .and_then(|b| b.get(..usize::try_from(abi.size32).ok()?))- .ok_or_else(|| {- crate::format_err!("write pointer out of bounds of memory")- })?;-- Val::load(lift, ty, bytes)+ let lift = &mut LiftContext::new(store, write_options, self);+ let bytes = &lift.memory()[write_address..][..write_length_in_bytes];+ Val::load(lift, *ty, bytes)}).transpose()?;@@ -3231,102 +3262,77 @@ impl Instance {// Serializing the value may require calling the guest's realloc function, so we// set the guest's thread context in case realloc requires it, and restore the original// thread context after the copy is complete.- let old_thread = store.0.set_thread(read_caller_thread)?;+ let old_thread = store.set_thread(read_caller_thread)?;let lower = &mut LowerContext::new(store.as_context_mut(), read_options, self);- let types = lower.types;- let ty = match types[types[read_ty].ty].payload {- Some(ty) => ty,- None => bail_bug!("expected payload type to be present"),- };let ptr = func::validate_inbounds_dynamic(- types.canonical_abi(&ty),+ read_abi,lower.as_slice_mut(),&ValRaw::u32(read_address.try_into()?),)?;- val.store(lower, ty, ptr)?;- store.0.set_thread(old_thread)?;+ let ty = match read_payload_ty {+ Some(ty) => ty,+ None => bail_bug!("expected read payload type to be present"),+ };+ val.store(lower, *ty, ptr)?;+ store.set_thread(old_thread)?;}}- (TransmitIndex::Stream(write_ty), TransmitIndex::Stream(read_ty)) => {- if write_caller_instance == read_caller_instance- && !allow_intra_component_read_write(types[types[write_ty].ty].payload)- {- bail!(- "cannot read from and write to intra-component stream with non-numeric payload"- )+ (TransmitIndex::Stream(_), TransmitIndex::Stream(_)) => {+ if write_length_in_bytes == 0 {+ return Ok(());}-- if let Some(flat_abi) = flat_abi {+ let write_payload_ty = match write_payload_ty {+ Some(ty) => ty,+ None => bail_bug!("expected write payload type to be present"),+ };+ let read_payload_ty = match read_payload_ty {+ Some(ty) => ty,+ None => bail_bug!("expected read payload type to be present"),+ };+ if flat_abi.is_some() {// Fast path memcpy for "flat" (i.e. no pointers or handles) payloads:- let length_in_bytes = usize::try_from(flat_abi.size)? * count;- if length_in_bytes > 0 {- if write_address % usize::try_from(flat_abi.align)? != 0 {- bail!("write pointer not aligned");- }- if read_address % usize::try_from(flat_abi.align)? != 0 {- bail!("read pointer not aligned");- }+ let store_opaque = store.store_opaque_mut();- let store_opaque = store.0.store_opaque_mut();+ assert_eq!(read_length_in_bytes, write_length_in_bytes);- {- let src = self- .options_memory(store_opaque, write_options)- .get(write_address..)- .and_then(|b| b.get(..length_in_bytes))- .ok_or_else(|| {- crate::format_err!("write pointer out of bounds of memory")- })?- .as_ptr();- let dst = self- .options_memory_mut(store_opaque, read_options)- .get_mut(read_address..)- .and_then(|b| b.get_mut(..length_in_bytes))- .ok_or_else(|| {- crate::format_err!("read pointer out of bounds of memory")- })?- .as_mut_ptr();- // SAFETY: Both `src` and `dst` have been validated- // above.- unsafe {- if write_caller_instance == read_caller_instance {- // If the same instance owns both ends of- // the stream, the source and destination- // buffers might overlap.- src.copy_to(dst, length_in_bytes)- } else {- // Since the read and write ends of the- // stream are owned by distinct instances,- // the buffers cannot possibly belong to the- // same memory and thus cannot overlap.- src.copy_to_nonoverlapping(dst, length_in_bytes)- }- }+ if write_caller_instance == read_caller_instance {+ let memory = self.options_memory_mut(store_opaque, read_options);+ memory.copy_within(+ write_address..write_address + write_length_in_bytes,+ read_address,+ );+ } else {+ let src = self.options_memory(store_opaque, write_options)[write_address..]+ [..write_length_in_bytes]+ .as_ptr();+ let dst = self.options_memory_mut(store_opaque, read_options)+ [read_address..][..read_length_in_bytes]+ .as_mut_ptr();++ // SAFETY: Both `src` and `dst` have been validated+ // above to be valid pointers as they're derived from+ // slices that have the desired length with the desired+ // read/write permission. The `unsafe` bit here is that+ // the memories are disjoint (present in different+ // instances) and there's no easy way to borrow both+ // simultaneously from the store. Different instances+ // are guaranteed to be disjoint, however, so the+ // `unsafe` here should be ok.+ unsafe {+ src.copy_to_nonoverlapping(dst, write_length_in_bytes);}}} else {- let store_opaque = store.0.store_opaque_mut();+ let store_opaque = store.store_opaque_mut();let lift = &mut LiftContext::new(store_opaque, write_options, self);- let ty = match lift.types[lift.types[write_ty].ty].payload {- Some(ty) => ty,- None => bail_bug!("expected payload type to be present"),- };- let abi = lift.types.canonical_abi(&ty);- let size = usize::try_from(abi.size32)?;- if write_address % usize::try_from(abi.align32)? != 0 {- bail!("write pointer not aligned");- }- let bytes = lift- .memory()- .get(write_address..)- .and_then(|b| b.get(..size * count))- .ok_or_else(|| {- crate::format_err!("write pointer out of bounds of memory")- })?;+ let bytes = &lift.memory()[write_address..][..write_length_in_bytes];lift.consume_fuel_array(count, size_of::<Val>())?;let values = (0..count)- .map(|index| Val::load(lift, ty, &bytes[(index * size)..][..size]))+ .map(|index| {+ let size = usize::try_from(write_abi.size32)?;+ Val::load(lift, *write_payload_ty, &bytes[(index * size)..][..size])+ }).collect::<Result<Vec<_>>>()?;let id = TableId::<TransmitHandle>::new(rep);@@ -3335,30 +3341,14 @@ impl Instance {// Serializing the value may require calling the guest's realloc function, so we// set the guest's thread context in case realloc requires it, and restore the original// thread context after the copy is complete.- let old_thread = store.0.set_thread(read_caller_thread)?;+ let old_thread = store.set_thread(read_caller_thread)?;let lower = &mut LowerContext::new(store.as_context_mut(), read_options, self);- let ty = match lower.types[lower.types[read_ty].ty].payload {- Some(ty) => ty,- None => bail_bug!("expected payload type to be present"),- };- let abi = lower.types.canonical_abi(&ty);- if read_address % usize::try_from(abi.align32)? != 0 {- bail!("read pointer not aligned");- }- let size = usize::try_from(abi.size32)?;- lower- .as_slice_mut()- .get_mut(read_address..)- .and_then(|b| b.get_mut(..size * count))- .ok_or_else(|| {- crate::format_err!("read pointer out of bounds of memory")- })?;let mut ptr = read_address;for value in values {- value.store(lower, ty, ptr)?;- ptr += size+ value.store(lower, *read_payload_ty, ptr)?;+ ptr += usize::try_from(read_abi.size32)?;}- store.0.set_thread(old_thread)?;+ store.set_thread(old_thread)?;}}_ => bail_bug!("mismatched transmit types in copy"),@@ -3535,8 +3525,8 @@ impl Instance {let instance = self.id().get_mut(store.0);let types = instance.component().types();- let item_size = match payload(ty, types) {- Some(ty) => usize::try_from(types.canonical_abi(&ty).size32)?,+ let item_size = match ty.payload(types) {+ Some(ty) => usize::try_from(types.canonical_abi(ty).size32)?,None => 0,};let concurrent_state = store.0.concurrent_state_mut();@@ -3761,8 +3751,8 @@ impl Instance {let instance = self.id().get_mut(store.0);let types = instance.component().types();- let item_size = match payload(ty, types) {- Some(ty) => usize::try_from(types.canonical_abi(&ty).size32)?,+ let item_size = match ty.payload(types) {+ Some(ty) => usize::try_from(types.canonical_abi(ty).size32)?,None => 0,};let concurrent_state = store.0.concurrent_state_mut();@@ -4741,7 +4731,7 @@ impl Waitable {/// Determine whether an intra-component read/write is allowed for the specified/// `stream` or `future` payload type according to the component model/// specification.-fn allow_intra_component_read_write(ty: Option<InterfaceType>) -> bool {+fn allow_intra_component_read_write(ty: Option<&InterfaceType>) -> bool {matches!(ty,None | Some(diff --git a/tests/all/pulley.rs b/tests/all/pulley.rsindex 3298e9a9e4..a35e4d6fbf 100644--- a/tests/all/pulley.rs+++ b/tests/all/pulley.rs@@ -1,5 +1,4 @@use std::ptr::NonNull;-use std::thread;use wasmtime::Result;use wasmtime::component::{self, Component};use wasmtime::{@@ -415,15 +414,6 @@ fn pulley_provenance_test_components() -> Result<()> {Ok(())}-async fn sleep(duration: std::time::Duration) {- let (tx, rx) = tokio::sync::oneshot::channel();- thread::spawn(move || {- thread::sleep(duration);- tx.send(()).unwrap();- });- rx.await.unwrap()-}-#[tokio::test]#[cfg_attr(miri, ignore)]async fn pulley_provenance_test_async_components() -> Result<()> {@@ -445,12 +435,16 @@ async fn pulley_provenance_test_async_components() -> Result<()> {{let mut store = Store::new(&engine, ());let mut linker = component::Linker::new(&engine);- linker.root().func_wrap_concurrent("sleep", |_, ()| {- Box::pin(async {- sleep(std::time::Duration::from_millis(10)).await;- Ok(())- })- })?;+ linker+ .root()+ .func_wrap_concurrent("return-slowly", |_, ()| {+ Box::pin(async {+ for _ in 0..5 {+ tokio::task::yield_now().await;+ }+ Ok(())+ })+ })?;let instance = linker.instantiate_async(&mut store, &component).await?;@@ -481,6 +475,13 @@ async fn pulley_provenance_test_async_components() -> Result<()> {wasmtime::error::Ok(run.call_concurrent(accessor, ()).await?)}).await??;++ let run = instance.get_typed_func::<(), ()>(&mut store, "intra-component-stream")?;+ store+ .run_concurrent(async move |accessor| {+ wasmtime::error::Ok(run.call_concurrent(accessor, ()).await?)+ })+ .await??;}Ok(())diff --git a/tests/all/pulley_provenance_test_async_component.wat b/tests/all/pulley_provenance_test_async_component.watindex b5f04c7ec7..8eae46e80d 100644--- a/tests/all/pulley_provenance_test_async_component.wat+++ b/tests/all/pulley_provenance_test_async_component.wat@@ -1,5 +1,5 @@(component- (import "sleep" (func $sleep async))+ (import "return-slowly" (func $return-slowly async))(component $A(import "run-stackless" (func $run_stackless async))@@ -48,7 +48,7 @@(func (export "run-stackful")(local $ret i32)(local $set i32)- (local $status i32)+ (local $status i32)(local $call i32)(local $event0 i32)(local $event1 i32)@@ -79,7 +79,8 @@(if (i32.ne (i32.const 1 (; SUBTASK ;)) (local.get $event0))(then unreachable))(call $task_return)- (i32.const 0 (; EXIT ;))))+ (i32.const 0 (; EXIT ;)))+ )(core instance $i (instantiate $m(with "" (instance@@ -92,15 +93,111 @@(export "run-stackful" (func $run_stackful))))))(func (export "run-stackless") async (canon lift (core func $i "run-stackless") async (callback (func $i "cb"))))- (func (export "run-stackful") async (canon lift (core func $i "run-stackful") async)))+ (func (export "run-stackful") async (canon lift (core func $i "run-stackful") async))+ )(instance $a (instantiate $A- (with "run-stackless" (func $sleep))- (with "run-stackful" (func $sleep))))+ (with "run-stackless" (func $return-slowly))+ (with "run-stackful" (func $return-slowly))))(instance $b (instantiate $A(with "run-stackless" (func $a "run-stackless"))(with "run-stackful" (func $a "run-stackful"))))(func (export "run-stackless") (alias export $a "run-stackless"))(func (export "run-stackful") (alias export $a "run-stackful"))(func (export "run-stackless-stackless") (alias export $b "run-stackless"))- (func (export "run-stackful-stackful") (alias export $b "run-stackful")))+ (func (export "run-stackful-stackful") (alias export $b "run-stackful"))+++ ;; Test which exercises an intra-component stream read/write to ensure that+ ;; there's no Miri violations while doing this.+ (core module $libc (memory (export "mem") 1))+ (core instance $libc (instantiate $libc))+ (core module $ics+ (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 "" "stream.drop-readable" (func $stream.drop-readable (param i32)))+ (import "" "stream.drop-writable" (func $stream.drop-writable (param i32)))+ (import "" "waitable-set.new" (func $waitable-set.new (result i32)))+ (import "" "waitable.join" (func $waitable.join (param i32 i32)))+ (import "" "waitable-set.wait" (func $waitable-set.wait (param i32 i32) (result i32)))+ (import "" "waitable-set.drop" (func $waitable-set.drop (param i32)))+ (import "" "mem" (memory 1))++ (global $r (mut i32) (i32.const 0))+ (global $w (mut i32) (i32.const 0))++ (func (export "run")+ (local $t64 i64)++ (local.set $t64 (call $stream.new))+ (global.set $r (i32.wrap_i64 (local.get $t64)))+ (global.set $w (i32.wrap_i64 (i64.shr_u (local.get $t64) (i64.const 32))))++ (call $transfer (i32.const 100) (i32.const 200) (i32.const 100))+ (call $transfer (i32.const 200) (i32.const 100) (i32.const 100))+ (call $transfer (i32.const 150) (i32.const 151) (i32.const 100))+ (call $transfer (i32.const 151) (i32.const 150) (i32.const 100))++ (call $stream.drop-readable (global.get $r))+ (call $stream.drop-writable (global.get $w))+ )++ (func $transfer (param $src i32) (param $dst i32) (param $len i32)+ (local $ws i32)++ (call $stream.read+ (global.get $r)+ (local.get $dst)+ (local.get $len))+ i32.const -1 ;; BLOCKED+ i32.ne+ if unreachable end++ (call $stream.write+ (global.get $w)+ (local.get $src)+ (local.get $len))+ (i32.shl (local.get $len) (i32.const 4)) ;; (len << 4) | COMPLETED+ ;; where COMPLETED==0+ i32.ne+ if unreachable end++ ;; Reap the readable status on the `$r` handle now.+ (local.set $ws (call $waitable-set.new))+ (call $waitable.join (global.get $r) (local.get $ws))+ (call $waitable-set.wait (local.get $ws) (i32.const 0))+ i32.const 2 ;; EVENT_STREAM_READ+ i32.ne+ if unreachable end+ (call $waitable.join (global.get $r) (i32.const 0))+ (call $waitable-set.drop (local.get $ws))+ )+ )+ (type $s (stream u8))+ (core func $stream.new (canon stream.new $s))+ (core func $stream.read (canon stream.read $s async (memory $libc "mem")))+ (core func $stream.write (canon stream.write $s async (memory $libc "mem")))+ (core func $stream.drop-readable (canon stream.drop-readable $s))+ (core func $stream.drop-writable (canon stream.drop-writable $s))+ (core func $waitable-set.new (canon waitable-set.new))+ (core func $waitable.join (canon waitable.join))+ (core func $waitable-set.wait (canon waitable-set.wait (memory $libc "mem")))+ (core func $waitable-set.drop (canon waitable-set.drop))+ (core instance $ics (instantiate $ics+ (with "" (instance+ (export "stream.new" (func $stream.new))+ (export "stream.read" (func $stream.read))+ (export "stream.write" (func $stream.write))+ (export "stream.drop-readable" (func $stream.drop-readable))+ (export "stream.drop-writable" (func $stream.drop-writable))+ (export "mem" (memory $libc "mem"))+ (export "waitable-set.new" (func $waitable-set.new))+ (export "waitable.join" (func $waitable.join))+ (export "waitable-set.wait" (func $waitable-set.wait))+ (export "waitable-set.drop" (func $waitable-set.drop))+ ))+ ))+ (func (export "intra-component-stream") async+ (canon lift (core func $ics "run")))+)diff --git a/tests/misc_testsuite/component-model/async/intra-futures.wast b/tests/misc_testsuite/component-model/async/intra-futures.wastindex b8958712be..fe3b646dd7 100644--- a/tests/misc_testsuite/component-model/async/intra-futures.wast+++ b/tests/misc_testsuite/component-model/async/intra-futures.wast@@ -52,4 +52,4 @@(func (export "run") (canon lift (core func $i "run"))))-(assert_trap (invoke "run") "cannot read from and write to intra-component future with non-numeric payload")+(assert_trap (invoke "run") "cannot read from and write to intra-component future/stream with non-numeric payload")diff --git a/tests/misc_testsuite/component-model/async/intra-streams.wast b/tests/misc_testsuite/component-model/async/intra-streams.wastindex b39f2e7f05..b73af5df13 100644--- a/tests/misc_testsuite/component-model/async/intra-streams.wast+++ b/tests/misc_testsuite/component-model/async/intra-streams.wast@@ -52,4 +52,4 @@(func (export "run") (canon lift (core func $i "run"))))-(assert_trap (invoke "run") "cannot read from and write to intra-component stream with non-numeric payload")+(assert_trap (invoke "run") "cannot read from and write to intra-component future/stream with non-numeric payload")