Skip to content

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

Centralize type-checks of futures/streams (#14827)

Alex Crichton48dacc76241f
diff --git a/crates/wasmtime/src/runtime/component/concurrent/future_stream_any.rs b/crates/wasmtime/src/runtime/component/concurrent/future_stream_any.rs
index 7e63037c80..987b5f6917 100644
--- a/crates/wasmtime/src/runtime/component/concurrent/future_stream_any.rs
+++ b/crates/wasmtime/src/runtime/component/concurrent/future_stream_any.rs
@@ -1,15 +1,15 @@
//! Implementation of [`FutureAny`] and [`StreamAny`].
use crate::component::concurrent::futures_and_streams::{self, TransmitOrigin};
-use crate::component::func::{LiftContext, LowerContext, bad_type_info, desc};
+use crate::component::func::{LiftContext, LowerContext, desc};
use crate::component::matching::InstanceType;
use crate::component::resources::HostResourceIndex;
-use crate::component::types::{self, FutureType, StreamType};
+use crate::component::types::{FutureType, StreamType};
use crate::component::{
ComponentInstanceId, ComponentType, FutureReader, Lift, Lower, StreamReader,
};
use crate::store::StoreOpaque;
-use crate::{AsContextMut, Result, bail, error::Context};
+use crate::{AsContextMut, Result, bail};
use core::any::TypeId;
use core::mem::MaybeUninit;
use wasmtime_environ::component::{
@@ -53,27 +53,6 @@ pub struct FutureAny {
}
impl FutureAny {
- fn lower_to_index<T>(&self, cx: &mut LowerContext<'_, T>, ty: InterfaceType) -> Result<u32> {
- // Note that unlike `FutureReader<T>` we need to perform an extra
- // typecheck to ensure that the dynamic type of this future matches
- // what the guest we're lowering into expects. This couldn't happen
- // before this point (see the `ComponentType::typecheck` implementation
- // for this type), so do it now.
- let future_ty = match ty {
- InterfaceType::Future(payload) => payload,
- _ => bad_type_info(),
- };
- let payload = cx.types[cx.types[future_ty].ty].payload.as_ref();
- self.ty.typecheck_guest(
- &cx.instance_type(),
- payload,
- FutureType::equivalent_payload_guest,
- )?;
-
- // Like `FutureReader<T>`, however, lowering "just" gets a u32.
- futures_and_streams::lower_future_to_index(self.idx, cx, ty)
- }
-
/// Attempts to convert this [`FutureAny`] to a [`FutureReader<T>`]
/// with a statically known type.
///
@@ -106,7 +85,7 @@ impl FutureAny {
{
let store = store.as_context_mut();
let ty = match store.0.transmit_origin(reader.idx())? {
- TransmitOrigin::Host => PayloadType::new_host::<T>(),
+ TransmitOrigin::Host { .. } => PayloadType::new_host::<T>(),
TransmitOrigin::GuestFuture(id, ty) => PayloadType::new_guest_future(store.0, id, ty),
TransmitOrigin::GuestStream(..) => bail!("not a future"),
};
@@ -168,8 +147,11 @@ unsafe impl Lower for FutureAny {
ty: InterfaceType,
dst: &mut MaybeUninit<Self::Lower>,
) -> Result<()> {
- self.lower_to_index(cx, ty)?
- .linear_lower_to_flat(cx, InterfaceType::U32, dst)
+ futures_and_streams::lower_future_to_index(self.idx, cx, ty)?.linear_lower_to_flat(
+ cx,
+ InterfaceType::U32,
+ dst,
+ )
}
fn linear_lower_to_memory<T>(
@@ -178,8 +160,11 @@ unsafe impl Lower for FutureAny {
ty: InterfaceType,
offset: usize,
) -> Result<()> {
- self.lower_to_index(cx, ty)?
- .linear_lower_to_memory(cx, InterfaceType::U32, offset)
+ futures_and_streams::lower_future_to_index(self.idx, cx, ty)?.linear_lower_to_memory(
+ cx,
+ InterfaceType::U32,
+ offset,
+ )
}
}
@@ -240,22 +225,6 @@ pub struct StreamAny {
}
impl StreamAny {
- fn lower_to_index<T>(&self, cx: &mut LowerContext<'_, T>, ty: InterfaceType) -> Result<u32> {
- // See comments in `FutureAny::lower_to_index` for why this is
- // different from `StreamReader`'s implementation.
- let stream_ty = match ty {
- InterfaceType::Stream(payload) => payload,
- _ => bad_type_info(),
- };
- let payload = cx.types[cx.types[stream_ty].ty].payload.as_ref();
- self.ty.typecheck_guest(
- &cx.instance_type(),
- payload,
- StreamType::equivalent_payload_guest,
- )?;
- futures_and_streams::lower_stream_to_index(self.idx, cx, ty)
- }
-
/// Attempts to convert this [`StreamAny`] to a [`StreamReader<T>`]
/// with a statically known type.
///
@@ -288,7 +257,7 @@ impl StreamAny {
{
let store = store.as_context_mut();
let ty = match store.0.transmit_origin(reader.idx())? {
- TransmitOrigin::Host => PayloadType::new_host::<T>(),
+ TransmitOrigin::Host { .. } => PayloadType::new_host::<T>(),
TransmitOrigin::GuestStream(id, ty) => PayloadType::new_guest_stream(store.0, id, ty),
TransmitOrigin::GuestFuture(..) => bail!("not a stream"),
};
@@ -348,8 +317,11 @@ unsafe impl Lower for StreamAny {
ty: InterfaceType,
dst: &mut MaybeUninit<Self::Lower>,
) -> Result<()> {
- self.lower_to_index(cx, ty)?
- .linear_lower_to_flat(cx, InterfaceType::U32, dst)
+ futures_and_streams::lower_stream_to_index(self.idx, cx, ty)?.linear_lower_to_flat(
+ cx,
+ InterfaceType::U32,
+ dst,
+ )
}
fn linear_lower_to_memory<T>(
@@ -358,8 +330,11 @@ unsafe impl Lower for StreamAny {
ty: InterfaceType,
offset: usize,
) -> Result<()> {
- self.lower_to_index(cx, ty)?
- .linear_lower_to_memory(cx, InterfaceType::U32, offset)
+ futures_and_streams::lower_stream_to_index(self.idx, cx, ty)?.linear_lower_to_memory(
+ cx,
+ InterfaceType::U32,
+ offset,
+ )
}
}
@@ -383,26 +358,10 @@ unsafe impl Lift for StreamAny {
}
}
-#[derive(Debug, Clone)]
+#[derive(Debug, Clone, PartialEq)]
enum PayloadType<T> {
Guest(T),
- Host {
- id: TypeId,
- typecheck: fn(Option<&InterfaceType>, &InstanceType<'_>) -> Result<()>,
- },
-}
-
-impl<T: PartialEq> PartialEq for PayloadType<T> {
- fn eq(&self, other: &Self) -> bool {
- match (self, other) {
- (PayloadType::Guest(a), PayloadType::Guest(b)) => a == b,
- (PayloadType::Guest(_), _) => false,
- (PayloadType::Host { id: a_id, .. }, PayloadType::Host { id: b_id, .. }) => {
- a_id == b_id
- }
- (PayloadType::Host { .. }, _) => false,
- }
- }
+ Host(TypeId),
}
impl PayloadType<FutureType> {
@@ -434,30 +393,7 @@ impl<T> PayloadType<T> {
where
P: ComponentType + 'static,
{
- PayloadType::Host {
- typecheck: types::typecheck_payload::<P>,
- id: TypeId::of::<P>(),
- }
- }
-
- fn typecheck_guest(
- &self,
- types: &InstanceType<'_>,
- payload: Option<&InterfaceType>,
- equivalent: fn(&T, &InstanceType<'_>, Option<&InterfaceType>) -> bool,
- ) -> Result<()> {
- match self {
- Self::Guest(ty) => {
- if equivalent(ty, types, payload) {
- Ok(())
- } else {
- bail!("future payload types differ")
- }
- }
- Self::Host { typecheck, .. } => {
- typecheck(payload, types).context("future payload types differ")
- }
- }
+ PayloadType::Host(TypeId::of::<P>())
}
fn typecheck_host<P>(&self, equivalent: fn(&T) -> Result<()>) -> Result<()>
@@ -466,7 +402,7 @@ impl<T> PayloadType<T> {
{
match self {
Self::Guest(ty) => equivalent(ty),
- Self::Host { id, .. } => {
+ Self::Host(id) => {
if *id == TypeId::of::<P>() {
Ok(())
} else {
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 c8ca98aaa5..eb1b8f0472 100644
--- a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs
+++ b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs
@@ -1493,6 +1493,39 @@ pub(super) fn lift_index_to_future(
}
}
+/// Checks that the host-owned read end `idx` may be lowered into a guest as a
+/// future or stream with the payload type `payload`.
+///
+/// Note that this is technically redundant for `StreamReader<T>` where its
+/// static typecheck already verified this, but this is otherwise required in
+/// the face of the `{Stream,Future}Any` abstraction to handle various use cases
+/// of types dynamically being labeled or possibly switched.
+fn typecheck_lowered_transmit<U>(
+ idx: HostResourceIndex,
+ cx: &mut LowerContext<'_, U>,
+ payload: Option<&InterfaceType>,
+) -> Result<()> {
+ let origin = cx.store.0.transmit_origin(idx)?;
+ let dst = cx.instance_type();
+ let equivalent = match origin {
+ TransmitOrigin::Host { typecheck } => {
+ return typecheck(payload, &dst).context("future or stream payload types differ");
+ }
+ TransmitOrigin::GuestFuture(id, ty) => {
+ let src = InstanceType::new(cx.store.0.component_instance(id));
+ types::FutureType::from(src.types[ty].ty, &src).equivalent_payload_guest(&dst, payload)
+ }
+ TransmitOrigin::GuestStream(id, ty) => {
+ let src = InstanceType::new(cx.store.0.component_instance(id));
+ types::StreamType::from(src.types[ty].ty, &src).equivalent_payload_guest(&dst, payload)
+ }
+ };
+ if !equivalent {
+ bail!("future or stream payload types differ");
+ }
+ Ok(())
+}
+
/// Transfer ownership of the read end of a future from the host to a guest.
pub(super) fn lower_future_to_index<U>(
idx: HostResourceIndex,
@@ -1501,6 +1534,8 @@ pub(super) fn lower_future_to_index<U>(
) -> Result<u32> {
match ty {
InterfaceType::Future(dst) => {
+ let types = cx.types;
+ typecheck_lowered_transmit(idx, cx, types[types[dst].ty].payload.as_ref())?;
let id = cx.store.0.host_reader_take(idx)?;
cx.instance_handle()
.lower_transmit_to_index(cx.store.0, TransmitIndex::Future(dst), id)
@@ -1857,6 +1892,8 @@ pub(super) fn lower_stream_to_index<U>(
) -> Result<u32> {
match ty {
InterfaceType::Stream(dst) => {
+ let types = cx.types;
+ typecheck_lowered_transmit(idx, cx, types[types[dst].ty].payload.as_ref())?;
let id = cx.store.0.host_reader_take(idx)?;
cx.instance_handle()
.lower_transmit_to_index(cx.store.0, TransmitIndex::Stream(dst), id)
@@ -2155,14 +2192,18 @@ struct TransmitState {
read: ReadState,
/// Whether further values may be transmitted via this stream or future.
done: bool,
- /// The original creator of this stream, used for type-checking with
- /// `{Future,Stream}Any`.
+ /// The original creator of this stream or future, whose payload type is
+ /// checked against a guest's whenever the host lowers the read end into
+ /// that guest, and which `{Future,Stream}Any` use as their initial type.
pub(super) origin: TransmitOrigin,
}
#[derive(Copy, Clone)]
pub(super) enum TransmitOrigin {
- Host,
+ Host {
+ /// Type-checks the producer's items against the guest-supplied payload.
+ typecheck: fn(Option<&InterfaceType>, &InstanceType<'_>) -> Result<()>,
+ },
GuestFuture(ComponentInstanceId, TypeFutureTableIndex),
GuestStream(ComponentInstanceId, TypeStreamTableIndex),
}
@@ -2627,7 +2668,9 @@ impl<T> StoreContextMut<'_, T> {
{
let token = StoreToken::new(self.as_context_mut());
let state = self.0.concurrent_state_mut()?;
- let (_, read) = state.new_transmit(TransmitOrigin::Host)?;
+ let (_, read) = state.new_transmit(TransmitOrigin::Host {
+ typecheck: types::typecheck_payload::<P::Item>,
+ })?;
let producer = Arc::new(LockedState::new((Box::pin(producer), P::Buffer::default())));
let id = state.get_mut(read)?.state;
let mut dropped = false;
diff --git a/tests/all/component_model/async.rs b/tests/all/component_model/async.rs
index 5df0262216..e0601afeb6 100644
--- a/tests/all/component_model/async.rs
+++ b/tests/all/component_model/async.rs
@@ -2421,6 +2421,144 @@ async fn host_to_host_stream_buffer_type_mismatch() -> wasmtime::Result<()> {
Ok(())
}
+#[tokio::test]
+#[cfg_attr(miri, ignore)]
+async fn lower_stream_payload_type_mismatch() -> wasmtime::Result<()> {
+ // A component that echoes streams and futures of streams and futures of
+ // strings back to the caller, creates a stream of streams of strings, and
+ // accepts the same with `u32` instead of `string`.
+ const WAT: &str = r#"
+(component
+ (type $s (stream string))
+ (type $ss (stream $s))
+ (type $f (future string))
+ (type $ff (future $f))
+ (type $u (stream u32))
+ (type $su (stream $u))
+ (type $fu32 (future u32))
+ (type $fu (future $fu32))
+ (core func $stream.new (canon stream.new $ss))
+ (core module $m
+ (import "" "stream.new" (func $stream.new (result i64)))
+ (func (export "echo") (param i32) (result i32) local.get 0)
+ (func (export "new-stream") (result i32)
+ (i32.wrap_i64 (call $stream.new)))
+ (func (export "take") (param i32)))
+ (core instance $i (instantiate $m
+ (with "" (instance (export "stream.new" (func $stream.new))))))
+ (func (export "echo-stream") (param "s" $ss) (result $ss)
+ (canon lift (core func $i "echo")))
+ (func (export "echo-future") (param "f" $ff) (result $ff)
+ (canon lift (core func $i "echo")))
+ (func (export "new-stream") (result $ss)
+ (canon lift (core func $i "new-stream")))
+ (func (export "take-stream-string") (param "s" $ss)
+ (canon lift (core func $i "take")))
+ (func (export "take-stream-u32") (param "s" $su)
+ (canon lift (core func $i "take")))
+ (func (export "take-future-u32") (param "f" $fu)
+ (canon lift (core func $i "take")))
+)
+"#;
+
+ let engine = Engine::default();
+ let component = Component::new(&engine, WAT)?;
+
+ // An error while lowering parameters leaves the instance unusable, so each
+ // case below uses a new store and instance.
+ let instantiate = async || -> Result<(Store<()>, Instance)> {
+ let mut store = Store::new(&engine, ());
+ let instance = Linker::new(&engine)
+ .instantiate_async(&mut store, &component)
+ .await?;
+ Ok((store, instance))
+ };
+
+ // Created with `StreamReader<String>` items, which can't be lowered as
+ // `stream<u32>` items, even though `StreamAny` statically matches it. The
+ // host still owns the stream afterwards, so it can close it.
+ let (mut store, instance) = instantiate().await?;
+ let echo = instance
+ .get_typed_func::<(StreamReader<StreamReader<String>>,), (StreamReader<StreamAny>,)>(
+ &mut store,
+ "echo-stream",
+ )?;
+ let take = instance
+ .get_typed_func::<(&StreamReader<StreamAny>,), ()>(&mut store, "take-stream-u32")?;
+ let inner = StreamReader::new(&mut store, vec!["hello".to_string()])?;
+ let outer = StreamReader::new(&mut store, vec![inner])?;
+ let (mut outer,) = echo.call_async(&mut store, (outer,)).await?;
+ assert!(take.call_async(&mut store, (&outer,)).await.is_err());
+ outer.close(&mut store)?;
+
+ // The same through `Val`.
+ let (mut store, instance) = instantiate().await?;
+ let echo = instance
+ .get_typed_func::<(StreamReader<StreamReader<String>>,), (StreamReader<StreamAny>,)>(
+ &mut store,
+ "echo-stream",
+ )?;
+ let take = instance.get_func(&mut store, "take-stream-u32").unwrap();
+ let inner = StreamReader::new(&mut store, vec!["hello".to_string()])?;
+ let outer = StreamReader::new(&mut store, vec![inner])?;
+ let (outer,) = echo.call_async(&mut store, (outer,)).await?;
+ let outer = outer.try_into_stream_any(&mut store)?;
+ assert!(
+ take.call_async(&mut store, &[Val::Stream(outer)], &mut [])
+ .await
+ .is_err()
+ );
+
+ // The same for futures.
+ let (mut store, instance) = instantiate().await?;
+ let echo = instance
+ .get_typed_func::<(FutureReader<FutureReader<String>>,), (FutureReader<FutureAny>,)>(
+ &mut store,
+ "echo-future",
+ )?;
+ let take =
+ instance.get_typed_func::<(FutureReader<FutureAny>,), ()>(&mut store, "take-future-u32")?;
+ let inner = FutureReader::new(&mut store, async {
+ wasmtime::error::Ok("hello".to_string())
+ })?;
+ let outer = FutureReader::new(&mut store, async move { wasmtime::error::Ok(inner) })?;
+ let (outer,) = echo.call_async(&mut store, (outer,)).await?;
+ assert!(take.call_async(&mut store, (outer,)).await.is_err());
+
+ // The same for a stream created by a guest.
+ let (mut store, instance) = instantiate().await?;
+ let new =
+ instance.get_typed_func::<(), (StreamReader<StreamAny>,)>(&mut store, "new-stream")?;
+ let take =
+ instance.get_typed_func::<(StreamReader<StreamAny>,), ()>(&mut store, "take-stream-u32")?;
+ let (stream,) = new.call_async(&mut store, ()).await?;
+ assert!(take.call_async(&mut store, (stream,)).await.is_err());
+
+ // A matching payload type is still accepted.
+ let (mut store, instance) = instantiate().await?;
+ let echo = instance
+ .get_typed_func::<(StreamReader<StreamReader<String>>,), (StreamReader<StreamAny>,)>(
+ &mut store,
+ "echo-stream",
+ )?;
+ let take = instance
+ .get_typed_func::<(StreamReader<StreamAny>,), ()>(&mut store, "take-stream-string")?;
+ let inner = StreamReader::new(&mut store, vec!["hello".to_string()])?;
+ let outer = StreamReader::new(&mut store, vec![inner])?;
+ let (outer,) = echo.call_async(&mut store, (outer,)).await?;
+ take.call_async(&mut store, (outer,)).await?;
+
+ let (mut store, instance) = instantiate().await?;
+ let new =
+ instance.get_typed_func::<(), (StreamReader<StreamAny>,)>(&mut store, "new-stream")?;
+ let take = instance
+ .get_typed_func::<(StreamReader<StreamAny>,), ()>(&mut store, "take-stream-string")?;
+ let (stream,) = new.call_async(&mut store, ()).await?;
+ take.call_async(&mut store, (stream,)).await?;
+
+ Ok(())
+}
+
/// Regression test for case which previously panicked when finishing a call
/// which was started before an intervening, failing call.
#[tokio::test]