Skip to content

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

fix new `idle-drop.wast` tests (#14342)

Joel Dicecc546eee265b
diff --git a/crates/c-api/include/wasmtime/trap.h b/crates/c-api/include/wasmtime/trap.h
index 523c3c56c4..f2903fc307 100644
--- a/crates/c-api/include/wasmtime/trap.h
+++ b/crates/c-api/include/wasmtime/trap.h
@@ -144,6 +144,18 @@ enum wasmtime_trap_code_enum {
WASMTIME_TRAP_CODE_WAITABLE_SYNC_AND_ASYNC = 48,
/// An exception propagated out of a component without being caught.
WASMTIME_TRAP_CODE_UNCAUGHT_EXCEPTION = 49,
+ /// Guest attempted to read from a stream after being notified that the
+ /// writable end was dropped.
+ WASMTIME_TRAP_READ_FROM_DROPPED_STREAM = 50,
+ /// Guest attempted to write to a stream after being notified that the
+ /// readable end was dropped.
+ WASMTIME_TRAP_WRITE_TO_DROPPED_STREAM = 51,
+ /// Guest attempted to write to a future after being notified that the
+ /// readable end was dropped.
+ WASMTIME_TRAP_WRITE_TO_DROPPED_FUTURE = 52,
+ /// Guest attempted to lift a stream after being notified that the writable
+ /// end was dropped.
+ WASMTIME_TRAP_LIFT_DROPPED_STREAM = 53,
};
/**
diff --git a/crates/c-api/src/trap.rs b/crates/c-api/src/trap.rs
index b1750c630c..2582efd5f9 100644
--- a/crates/c-api/src/trap.rs
+++ b/crates/c-api/src/trap.rs
@@ -56,6 +56,10 @@ const _: () = {
assert!(Trap::StreamOpTooBig as u8 == 47);
assert!(Trap::WaitableSyncAndAsync as u8 == 48);
assert!(Trap::UncaughtException as u8 == 49);
+ assert!(Trap::ReadFromDroppedStream as u8 == 50);
+ assert!(Trap::WriteToDroppedStream as u8 == 51);
+ assert!(Trap::WriteToDroppedFuture as u8 == 52);
+ assert!(Trap::LiftDroppedStream as u8 == 53);
};
#[repr(C)]
diff --git a/crates/environ/src/trap_encoding.rs b/crates/environ/src/trap_encoding.rs
index 692238a4a4..c3573938e8 100644
--- a/crates/environ/src/trap_encoding.rs
+++ b/crates/environ/src/trap_encoding.rs
@@ -279,6 +279,22 @@ generate_trap_type! {
/// An exception propagated out of a component without being caught.
UncaughtException = "uncaught exception propagated out of component",
+ /// Guest attempted to read from a stream after being notified that the
+ /// writable end was dropped.
+ ReadFromDroppedStream = "cannot read from stream after being notified that the writable end dropped",
+
+ /// Guest attempted to write to a stream after being notified that the
+ /// readable end was dropped.
+ WriteToDroppedStream = "cannot write to stream after being notified that the readable end dropped",
+
+ /// Guest attempted to write to a future after being notified that the
+ /// readable end was dropped.
+ WriteToDroppedFuture = "cannot write to stream after previous write succeded or readable end dropped",
+
+ /// Guest attempted to lift a stream after being notified that the
+ /// writable end was dropped.
+ LiftDroppedStream = "cannot lift stream after being notified that the writable end dropped",
+
// if adding a variant here be sure to update `trap.rs` and `trap.h` as
// mentioned above
}
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 9c5c4a8071..74d278652d 100644
--- a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs
+++ b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs
@@ -3712,6 +3712,24 @@ impl Instance {
transmit.done = true;
}
+ match Waitable::Transmit(transmit_handle).take_event(concurrent_state)? {
+ Some(
+ Event::StreamWrite {
+ code: ReturnCode::Dropped(ItemCount::ZERO),
+ ..
+ }
+ | Event::FutureWrite {
+ code: ReturnCode::Dropped(ItemCount::ZERO),
+ ..
+ },
+ ) => {}
+ None => bail!(match ty {
+ TransmitIndex::Future(_) => Trap::WriteToDroppedFuture,
+ TransmitIndex::Stream(_) => Trap::WriteToDroppedStream,
+ }),
+ event => bail_bug!("expected pending dropped event for writer; got {event:?}"),
+ }
+
ReturnCode::Dropped(ItemCount::ZERO)
}
};
@@ -3938,7 +3956,24 @@ impl Instance {
ReturnCode::Blocked
}
- WriteState::Dropped => ReturnCode::Dropped(ItemCount::ZERO),
+ WriteState::Dropped => {
+ if let TransmitIndex::Future(_) = ty {
+ bail_bug!(
+ "should not be possible to read from a future whose write end was dropped"
+ );
+ }
+
+ match Waitable::Transmit(transmit_handle).take_event(concurrent_state)? {
+ Some(Event::StreamRead {
+ code: ReturnCode::Dropped(ItemCount::ZERO),
+ ..
+ }) => {}
+ None => bail!(Trap::ReadFromDroppedStream),
+ event => bail_bug!("expected pending dropped event for reader; got {event:?}"),
+ }
+
+ ReturnCode::Dropped(ItemCount::ZERO)
+ }
};
if result == ReturnCode::Blocked && !self.options(store.0, options).async_ {
@@ -4622,7 +4657,10 @@ fn lift_index_to_transmit(
let state = future.state;
if concurrent_state.get_mut(state)?.done {
- bail!("cannot lift {desc} after previous read succeeded");
+ match ty {
+ TransmitIndex::Future(_) => bail!("cannot lift {desc} after previous read succeeded"),
+ TransmitIndex::Stream(_) => bail!(Trap::LiftDroppedStream),
+ };
}
Ok(id)
@@ -4838,6 +4876,31 @@ impl Waitable {
instance: Instance,
event: Event,
) -> Result<()> {
+ if let Event::FutureRead {
+ code: ReturnCode::Dropped(_),
+ ..
+ }
+ | Event::FutureWrite {
+ code: ReturnCode::Dropped(_),
+ ..
+ }
+ | Event::StreamRead {
+ code: ReturnCode::Dropped(_),
+ ..
+ }
+ | Event::StreamWrite {
+ code: ReturnCode::Dropped(_),
+ ..
+ } = event
+ {
+ let Waitable::Transmit(transmit_handle) = self else {
+ bail_bug!("unexpected `{event:?}` for `{self:?}`");
+ };
+ let state = store.concurrent_state_mut()?;
+ let transmit_id = state.get_mut(*transmit_handle)?.state;
+ state.get_mut(transmit_id)?.done = true;
+ }
+
let instance = instance.id().get_mut(store);
let (rep, state, code) = match event {
Event::FutureRead {