Skip to content

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

Skip `{enter,exit}-sync-call` for "thread-transparent" adapters (#14270)

Nick Fitzgeraldcd4ec0e4e3bd
diff --git a/crates/cranelift/src/compiler/component.rs b/crates/cranelift/src/compiler/component.rs
index fa48ca3fba..c580f197bd 100644
--- a/crates/cranelift/src/compiler/component.rs
+++ b/crates/cranelift/src/compiler/component.rs
@@ -1189,7 +1189,7 @@ impl<'a> TrampolineCompiler<'a> {
}
if self.compiler.tunables.concurrency_support {
- Some(self.enter_sync_call_inline(instance, def.instance))
+ Some(self.enter_sync_call_inline(def.instance))
} else {
None
}
@@ -1286,14 +1286,9 @@ impl<'a> TrampolineCompiler<'a> {
/// otherwise do eagerly.
fn enter_sync_call_inline(
&mut self,
- caller_instance: RuntimeComponentInstanceIndex,
callee_instance: RuntimeComponentInstanceIndex,
) -> ir::StackSlot {
let vmctx = self.caller_vmctx();
- let caller_instance = self
- .builder
- .ins()
- .iconst(ir::types::I32, i64::from(caller_instance.as_u32()));
let callee_async = self.builder.ins().iconst(ir::types::I32, 0);
let callee_instance = self
.builder
@@ -1304,7 +1299,6 @@ impl<'a> TrampolineCompiler<'a> {
&mut self.alias_regions,
vmctx,
crate::component_sync_call::EnterArgs {
- caller_instance,
callee_async,
callee_instance,
},
diff --git a/crates/cranelift/src/component_sync_call.rs b/crates/cranelift/src/component_sync_call.rs
index 876bb4e170..0313255414 100644
--- a/crates/cranelift/src/component_sync_call.rs
+++ b/crates/cranelift/src/component_sync_call.rs
@@ -29,8 +29,6 @@ use wasmtime_environ::{GetPtrSize, NUM_COMPONENT_CONTEXT_SLOTS, PtrSize};
/// `VMDeferredThread`, to be replayed by the host if it ever has to promote the
/// deferred thread into a real one.
pub struct EnterArgs {
- /// The component instance performing the call.
- pub caller_instance: ir::Value,
/// Whether the callee is async-lifted, as an `i32` boolean.
pub callee_async: ir::Value,
/// The component instance being called into.
@@ -79,11 +77,6 @@ where
.store(&mut builder.cursor(), slot_addr, parent);
// Record the deferred `enter_sync_call` arguments.
- alias_regions.vm_deferred_thread().caller_instance().store(
- &mut builder.cursor(),
- slot_addr,
- args.caller_instance,
- );
alias_regions.vm_deferred_thread().callee_async().store(
&mut builder.cursor(),
slot_addr,
diff --git a/crates/cranelift/src/func_environ.rs b/crates/cranelift/src/func_environ.rs
index 3445f11c9c..a2a03e7df7 100644
--- a/crates/cranelift/src/func_environ.rs
+++ b/crates/cranelift/src/func_environ.rs
@@ -2078,8 +2078,8 @@ impl<'a, 'func, 'module_env> Call<'a, 'func, 'module_env> {
/// defers the heavyweight task bookkeeping the `enter_sync_call` libcall
/// would otherwise do eagerly.
///
- /// `real_call_args` is `[callee_vmctx, caller_vmctx, caller_instance,
- /// callee_async, callee_instance]`.
+ /// `real_call_args` is `[callee_vmctx, caller_vmctx, callee_async,
+ /// callee_instance]`.
fn lower_fact_enter_sync_call(&mut self, real_call_args: &[ir::Value]) -> CallRets {
let vmctx = self.env.vmctx_val(&mut self.builder.cursor());
let slot = crate::component_sync_call::enter(
@@ -2087,9 +2087,8 @@ impl<'a, 'func, 'module_env> Call<'a, 'func, 'module_env> {
&mut self.env.alias_regions,
vmctx,
crate::component_sync_call::EnterArgs {
- caller_instance: real_call_args[2],
- callee_async: real_call_args[3],
- callee_instance: real_call_args[4],
+ callee_async: real_call_args[2],
+ callee_instance: real_call_args[3],
},
);
diff --git a/crates/environ/src/component.rs b/crates/environ/src/component.rs
index 9e3233209d..9329cb5440 100644
--- a/crates/environ/src/component.rs
+++ b/crates/environ/src/component.rs
@@ -69,12 +69,16 @@ mod compiler;
#[cfg(feature = "compile")]
pub mod dfg;
#[cfg(feature = "compile")]
+mod thread_transparency;
+#[cfg(feature = "compile")]
mod translate;
#[cfg(feature = "compile")]
mod types_builder;
#[cfg(feature = "compile")]
pub use self::compiler::*;
#[cfg(feature = "compile")]
+pub use self::thread_transparency::transparent_adapters;
+#[cfg(feature = "compile")]
pub use self::translate::*;
#[cfg(feature = "compile")]
pub use self::types_builder::*;
@@ -97,7 +101,7 @@ macro_rules! foreach_builtin_component_function {
resource_transfer_own(vmctx: vmctx, src_idx: u32, src_table: u32, dst_table: u32) -> u64;
resource_transfer_borrow(vmctx: vmctx, src_idx: u32, src_table: u32, dst_table: u32) -> u64;
- enter_sync_call(vmctx: vmctx, caller_instance: u32, callee_async: u32, callee_instance: u32) -> bool;
+ enter_sync_call(vmctx: vmctx, callee_async: u32, callee_instance: u32) -> bool;
exit_sync_call(vmctx: vmctx) -> bool;
#[cfg(feature = "component-model-async")]
diff --git a/crates/environ/src/component/dfg.rs b/crates/environ/src/component/dfg.rs
index 69e4560388..a67bf17d35 100644
--- a/crates/environ/src/component/dfg.rs
+++ b/crates/environ/src/component/dfg.rs
@@ -31,6 +31,7 @@ use crate::component::*;
use crate::error::Result;
use crate::prelude::*;
use crate::{EntityIndex, EntityRef, ModuleInternedTypeIndex, PrimaryMap, Trap, WasmValType};
+use cranelift_entity::EntitySet;
use cranelift_entity::packed_option::PackedOption;
use indexmap::IndexMap;
use info::LinearMemoryOptions;
@@ -143,13 +144,17 @@ pub struct ComponentDfg {
///
/// Currently all side effects are either instantiating core wasm modules or
/// declaring a resource. These side effects affect the dataflow processing
- /// of this component by idnicating what order operations should be
+ /// of this component by indicating what order operations should be
/// performed during instantiation.
pub side_effects: Vec<SideEffect>,
/// Interned map of id-to-`CanonicalOptions`, or all sets-of-options used by
/// this component.
pub options: Intern<OptionsId, CanonicalOptions>,
+
+ /// The set of fused adapters which may skip their
+ /// `{enter,exit}-sync-call` window.
+ pub transparent_adapters: EntitySet<AdapterId>,
}
/// Possible side effects that are possible with instantiating this component.
diff --git a/crates/environ/src/component/thread_transparency.rs b/crates/environ/src/component/thread_transparency.rs
new file mode 100644
index 0000000000..1d3931f5c0
--- /dev/null
+++ b/crates/environ/src/component/thread_transparency.rs
@@ -0,0 +1,555 @@
+//! Static analysis of "thread transparency" for fused adapters.
+//!
+//! Every fused sync adapter normally brackets its callee with
+//! `enter-sync-call`/`exit-sync-call`. That pair saves the caller's
+//! component-model thread state (`current_thread`, the context slots, the "may
+//! block" flag, etc...) and installs fresh state for the callee, restoring the
+//! caller's state on the way back out.
+//!
+//! However, that bracketing is only necessary if the callee can observe that
+//! thread state. If it cannot, the callee may simply run on top of whatever
+//! thread state the caller left in place, and the adapter can skip the
+//! save/restore entirely. We call such an adapter "thread transparent".
+//!
+//! Core Wasm state cannot be shared between components, so the only way core
+//! Wasm can touch component-model thread state is by calling a `canon lower`ed
+//! function or a canonical built-in. All of those are declared by a particular
+//! component instance and are only reachable from core Wasm belonging to that
+//! same component instance.
+//!
+//! Therefore this analysis works per component instance: an adapter whose
+//! callee instance declares nothing capable of touching thread state is
+//! transparent.
+//!
+//! Note that (lack of) transparency is not transitive across component
+//! instances because a call into an opaque instance will itself save/restore
+//! the thread state, even if none of its transparent callers needed to.
+
+use crate::component::dfg::{
+ AdapterId, CanonicalOptionsDataModel, ComponentDfg, CoreDef, Export, Instance, SideEffect,
+ Trampoline,
+};
+use crate::component::{
+ ComponentTypesBuilder, DataModel, RuntimeComponentInstanceIndex, UnsafeIntrinsic,
+};
+use cranelift_entity::EntitySet;
+
+/// Something a component instance makes callable by its core Wasm instances.
+#[derive(Clone, Copy, PartialEq, Eq, Debug)]
+enum CoreCallable {
+ /// A `canon lower` of a function imported from the host.
+ ///
+ /// The host can do anything at all, including reading and writing the
+ /// current thread's context slots.
+ HostImport,
+
+ /// A `canon lower` of a guest function that was `canon lift`ed elsewhere.
+ Adapter {
+ /// Whether the function on the lifting side uses the `async` ABI.
+ async_lift: bool,
+ },
+
+ /// A `canon context.get` or `canon context.set`.
+ ContextAccess,
+
+ /// Any other canonical built-in that reads or writes thread state:
+ /// `resource.*`, `backpressure.*`, `task.*`, `waitable*.*`, `subtask.*`,
+ /// `stream.*`, `future.*`, `error-context.*`, or `thread.*`.
+ ThreadStateBuiltin,
+
+ /// Something that cannot touch thread state at all: a plain core function,
+ /// a string transcoder, a non-context Wasmtime intrinsic, and so on.
+ Inert,
+}
+
+impl CoreCallable {
+ /// Can calling this reach code that reads or writes component-model thread
+ /// state?
+ fn may_touch_thread_state(self) -> bool {
+ match self {
+ // The host is unconstrained.
+ CoreCallable::HostImport => true,
+
+ // A sync-lifted callee is reached through its own fused adapter,
+ // which saves and restores whatever it needs; nothing about that
+ // call is visible in this instance's thread state.
+ //
+ // An async-lifted callee is different: it may block, and if it
+ // does the scheduler has to find the sync-typed call in progress
+ // on this instance, which means this instance needs real thread
+ // state of its own.
+ CoreCallable::Adapter { async_lift } => async_lift,
+
+ CoreCallable::ContextAccess | CoreCallable::ThreadStateBuiltin => true,
+
+ CoreCallable::Inert => false,
+ }
+ }
+}
+
+/// Everything about one fused adapter that bears on whether it can skip its
+/// `{enter,exit}-sync-call` window.
+#[derive(Clone, Copy, PartialEq, Eq, Debug)]
+struct AdapterFacts {
+ /// The component instance whose core Wasm this adapter calls into, i.e.
+ /// the instance that did the `canon lift`.
+ callee: RuntimeComponentInstanceIndex,
+
+ /// Whether either side of this adapter is `async` in either its canonical
+ /// options or function signature.
+ any_async: bool,
+
+ /// Whether either function signature mentions a handle.
+ any_handle: bool,
+}
+
+/// A builder for a `ThreadTransparency`.
+///
+/// Accumulates the observations that ultimately produce the
+/// `ThreadTransparency` analysis.
+#[derive(Default)]
+struct ThreadTransparencyBuilder {
+ /// The component instances observed to be able to touch thread state.
+ opaque: EntitySet<RuntimeComponentInstanceIndex>,
+}
+
+impl ThreadTransparencyBuilder {
+ /// Record that `instance` makes `callable` available to its core Wasm.
+ fn observe(&mut self, instance: RuntimeComponentInstanceIndex, callable: CoreCallable) {
+ if callable.may_touch_thread_state() {
+ self.opaque.insert(instance);
+ }
+ }
+
+ /// Finish observing and produce the queryable analysis results.
+ fn finish(self) -> ThreadTransparency {
+ ThreadTransparency {
+ opaque: self.opaque,
+ }
+ }
+}
+
+/// A completed thread-transparency analysis, ready to be queried.
+struct ThreadTransparency {
+ /// The component instances that can touch thread state.
+ opaque: EntitySet<RuntimeComponentInstanceIndex>,
+}
+
+impl ThreadTransparency {
+ /// May the adapter described by `facts` omit its `{enter,exit}-sync-call`
+ /// window?
+ fn adapter_is_transparent(&self, facts: AdapterFacts) -> bool {
+ // Async adapters genuinely need thread state of their own.
+ if facts.any_async {
+ return false;
+ }
+
+ // Transferring handles across an instance boundary unconditionally
+ // requires thread state for now.
+ if facts.any_handle {
+ return false;
+ }
+
+ !self.opaque.contains(facts.callee)
+ }
+}
+
+/// Run the thread-transparency analysis over `dfg`, returning the set of
+/// adapters that may skip their `{enter,exit}-sync-call` window.
+pub fn transparent_adapters(
+ dfg: &ComponentDfg,
+ types: &ComponentTypesBuilder,
+) -> EntitySet<AdapterId> {
+ let mut builder = ThreadTransparencyBuilder::default();
+
+ // Observe canonical built-ins.
+ for (_, (_, trampoline)) in dfg.trampolines.iter() {
+ let (instance, callable) = trampoline_callable(dfg, trampoline);
+ builder.observe(instance, callable);
+ }
+
+ // Observe fused adapters.
+ for (_, adapter) in dfg.adapters.iter() {
+ let async_lift = types[adapter.lift_ty].async_;
+ builder.observe(
+ adapter.lower_options.instance,
+ CoreCallable::Adapter { async_lift },
+ );
+ }
+
+ // Observe `CoreDef`s.
+ for_each_core_def(dfg, |instance, def| {
+ builder.observe(instance, core_def_callable(def));
+ });
+
+ // Finish the analysis and query its results to determine the set of
+ // adapters that are transparent.
+ let analysis = builder.finish();
+ let mut transparent = EntitySet::new();
+ for (id, adapter) in dfg.adapters.iter() {
+ if analysis.adapter_is_transparent(facts(types, adapter)) {
+ transparent.insert(id);
+ }
+ }
+ transparent
+}
+
+fn facts(types: &ComponentTypesBuilder, adapter: &super::Adapter) -> AdapterFacts {
+ AdapterFacts {
+ callee: adapter.lift_options.instance,
+ any_async: adapter.lift_options.async_
+ || adapter.lower_options.async_
+ || types[adapter.lift_ty].async_
+ || types[adapter.lower_ty].async_,
+ any_handle: types.func_contains_any_handle(adapter.lift_ty)
+ || types.func_contains_any_handle(adapter.lower_ty),
+ }
+}
+
+/// Classify a `CoreDef` that some component instance references.
+fn core_def_callable(def: &CoreDef) -> CoreCallable {
+ // NB: deliberately exhaustive so that new variants must be classified here.
+ match def {
+ CoreDef::UnsafeIntrinsic(_, intrinsic) => match intrinsic {
+ UnsafeIntrinsic::ContextGetI32_0
+ | UnsafeIntrinsic::ContextSetI32_0
+ | UnsafeIntrinsic::ContextGetI32_1
+ | UnsafeIntrinsic::ContextSetI32_1 => CoreCallable::ContextAccess,
+
+ // Unsafe intrinsics can't access thread state.
+ UnsafeIntrinsic::StoreDataAddress
+ | UnsafeIntrinsic::U8NativeLoad
+ | UnsafeIntrinsic::U8NativeStore
+ | UnsafeIntrinsic::U16NativeLoad
+ | UnsafeIntrinsic::U16NativeStore
+ | UnsafeIntrinsic::U32NativeLoad
+ | UnsafeIntrinsic::U32NativeStore
+ | UnsafeIntrinsic::U64NativeLoad
+ | UnsafeIntrinsic::U64NativeStore
+ | UnsafeIntrinsic::U8CheckedNativeLoad
+ | UnsafeIntrinsic::U8CheckedNativeStore
+ | UnsafeIntrinsic::U16CheckedNativeLoad
+ | UnsafeIntrinsic::U16CheckedNativeStore
+ | UnsafeIntrinsic::U32CheckedNativeLoad
+ | UnsafeIntrinsic::U32CheckedNativeStore
+ | UnsafeIntrinsic::U64CheckedNativeLoad
+ | UnsafeIntrinsic::U64CheckedNativeStore => CoreCallable::Inert,
+ },
+
+ // Trampolines and adapters are observed directly in
+ // `transparent_adapters`, so we don't need to worry about them here.
+ CoreDef::Trampoline(_) | CoreDef::Adapter(_) => CoreCallable::Inert,
+
+ // Plain core Wasm; can't introduce any state-access capability that
+ // isn't already there.
+ CoreDef::Export(_) | CoreDef::InstanceFlags(_) => CoreCallable::Inert,
+ }
+}
+
+/// Classify a trampoline, and identify the component instance that declared it.
+fn trampoline_callable(
+ dfg: &ComponentDfg,
+ trampoline: &Trampoline,
+) -> (RuntimeComponentInstanceIndex, CoreCallable) {
+ use Trampoline::*;
+
+ // NB: deliberately exhaustive so that new variants must be classified here.
+ match trampoline {
+ LowerImport { options, .. } => (dfg.options[*options].instance, CoreCallable::HostImport),
+
+ ResourceNew { instance, .. }
+ | ResourceRep { instance, .. }
+ | ResourceDrop { instance, .. }
+ | BackpressureInc { instance }
+ | BackpressureDec { instance }
+ | TaskReturn { instance, .. }
+ | TaskCancel { instance }
+ | WaitableSetNew { instance }
+ | WaitableSetWait { instance, .. }
+ | WaitableSetPoll { instance, .. }
+ | WaitableSetDrop { instance }
+ | WaitableJoin { instance }
+ | SubtaskDrop { instance }
+ | SubtaskCancel { instance, .. }
+ | StreamNew { instance, .. }
+ | StreamRead { instance, .. }
+ | StreamWrite { instance, .. }
+ | StreamCancelRead { instance, .. }
+ | StreamCancelWrite { instance, .. }
+ | StreamDropReadable { instance, .. }
+ | StreamDropWritable { instance, .. }
+ | FutureNew { instance, .. }
+ | FutureRead { instance, .. }
+ | FutureWrite { instance, .. }
+ | FutureCancelRead { instance, .. }
+ | FutureCancelWrite { instance, .. }
+ | FutureDropReadable { instance, .. }
+ | FutureDropWritable { instance, .. }
+ | ErrorContextNew { instance, .. }
+ | ErrorContextDebugMessage { instance, .. }
+ | ErrorContextDrop { instance, .. }
+ | ThreadIndex { instance }
+ | ThreadNewIndirect { instance, .. }
+ | ThreadResumeLater { instance }
+ | ThreadSuspend { instance, .. }
+ | ThreadYield { instance, .. }
+ | ThreadSuspendThenResume { instance, .. }
+ | ThreadYieldThenResume { instance, .. }
+ | ThreadSuspendThenPromote { instance, .. }
+ | ThreadYieldThenPromote { instance, .. } => (*instance, CoreCallable::ThreadStateBuiltin),
+
+ // These trampolines are only ever created in `translate::adapt`, which
+ // happens strictly after this analysis has run. Therefore none of them
+ // can exist yet.
+ Transcoder { .. }
+ | ResourceTransferOwn
+ | ResourceTransferBorrow
+ | PrepareCall { .. }
+ | SyncStartCall { .. }
+ | AsyncStartCall { .. }
+ | FutureTransfer
+ | StreamTransfer
+ | ErrorContextTransfer
+ | Trap(_)
+ | EnterSyncCall
+ | ExitSyncCall => {
+ unreachable!("these trampolines do not exist yet")
+ }
+ }
+}
+
+/// Visit every `CoreDef` in `dfg` together with the component instance that
+/// references it.
+fn for_each_core_def(
+ dfg: &ComponentDfg,
+ mut f: impl FnMut(RuntimeComponentInstanceIndex, &CoreDef),
+) {
+ for effect in dfg.side_effects.iter() {
+ let SideEffect::Instance(id, instance) = effect else {
+ continue;
+ };
+ match &dfg.instances[*id] {
+ Instance::Static(_, args) => {
+ for def in args.iter() {
+ f(*instance, def);
+ }
+ }
+ Instance::Import(_, args) => {
+ for (_, defs) in args {
+ for (_, def) in defs {
+ f(*instance, def);
+ }
+ }
+ }
+ }
+ }
+
+ for (_, adapter) in dfg.adapters.iter() {
+ f(adapter.lift_options.instance, &adapter.func);
+ for options in [&adapter.lift_options, &adapter.lower_options] {
+ for def in options
+ .callback
+ .iter()
+ .chain(options.post_return.iter())
+ .chain(match &options.data_model {
+ DataModel::LinearMemory { realloc, .. } => realloc.iter(),
+ DataModel::Gc {} => None.iter(),
+ })
+ {
+ f(options.instance, def);
+ }
+ }
+ }
+
+ for (_, options) in dfg.options.iter() {
+ if let Some(callback) = options.callback {
+ f(options.instance, &dfg.callbacks[callback]);
+ }
+ if let Some(post_return) = options.post_return {
+ f(options.instance, &dfg.post_returns[post_return]);
+ }
+ if let CanonicalOptionsDataModel::LinearMemory {
+ realloc: Some(realloc),
+ ..
+ } = &options.data_model
+ {
+ f(options.instance, &dfg.reallocs[*realloc]);
+ }
+ }
+
+ for (_, resource) in dfg.resources.iter() {
+ if let Some(dtor) = &resource.dtor {
+ f(resource.instance, dtor);
+ }
+ }
+
+ for (_, (export, _)) in dfg.exports.iter() {
+ for_each_export_core_def(dfg, export, &mut f);
+ }
+}
+
+fn for_each_export_core_def(
+ dfg: &ComponentDfg,
+ export: &Export,
+ f: &mut impl FnMut(RuntimeComponentInstanceIndex, &CoreDef),
+) {
+ match export {
+ Export::LiftedFunction { func, options, .. } => {
+ f(dfg.options[*options].instance, func);
+ }
+ Export::Instance { exports, .. } => {
+ for (_, (export, _)) in exports.iter() {
+ for_each_export_core_def(dfg, export, f);
+ }
+ }
+ Export::ModuleStatic { .. } | Export::ModuleImport { .. } | Export::Type(_) => {}
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn instance(i: u32) -> RuntimeComponentInstanceIndex {
+ RuntimeComponentInstanceIndex::from_u32(i)
+ }
+
+ /// Run the build phase over `observations`, given as `(instance, callable)`
+ /// pairs, and finish it into queryable results.
+ fn analyze(observations: &[(u32, CoreCallable)]) -> ThreadTransparency {
+ let mut builder = ThreadTransparencyBuilder::default();
+ for (i, callable) in observations {
+ builder.observe(instance(*i), *callable);
+ }
+ builder.finish()
+ }
+
+ /// An adapter calling into instance `callee` with a plain, handle-free,
+ /// sync signature.
+ fn facts(callee: u32) -> AdapterFacts {
+ AdapterFacts {
+ callee: instance(callee),
+ any_async: false,
+ any_handle: false,
+ }
+ }
+
+ #[test]
+ fn clean_instance_is_transparent() {
+ let t = analyze(&[]);
+ assert!(t.adapter_is_transparent(facts(0)));
+ }
+
+ #[test]
+ fn context_access_makes_instance_opaque() {
+ let t = analyze(&[(0, CoreCallable::ContextAccess)]);
+ assert!(!t.adapter_is_transparent(facts(0)));
+ }
+
+ #[test]
+ fn thread_state_builtin_makes_instance_opaque() {
+ let t = analyze(&[(0, CoreCallable::ThreadStateBuiltin)]);
+ assert!(!t.adapter_is_transparent(facts(0)));
+ }
+
+ #[test]
+ fn host_import_lowering_makes_instance_opaque() {
+ let t = analyze(&[(0, CoreCallable::HostImport)]);
+ assert!(!t.adapter_is_transparent(facts(0)));
+ }
+
+ #[test]
+ fn inert_callable_leaves_instance_transparent() {
+ let t = analyze(&[(0, CoreCallable::Inert), (0, CoreCallable::Inert)]);
+ assert!(t.adapter_is_transparent(facts(0)));
+ }
+
+ /// Lowering a sync-lifted guest function is just another adapter, and that
+ /// adapter does its own save/restore, so it does not taint the instance
+ /// doing the lowering.
+ #[test]
+ fn sync_adapter_lowering_leaves_instance_transparent() {
+ let t = analyze(&[(0, CoreCallable::Adapter { async_lift: false })]);
+ assert!(t.adapter_is_transparent(facts(0)));
+ }
+
+ /// Lowering an async-lifted guest function does taint the lowering
+ /// instance: the callee may block, and then the scheduler needs to find
+ /// this instance's sync call in progress.
+ #[test]
+ fn async_lift_makes_the_lowering_instance_opaque() {
+ let t = analyze(&[(0, CoreCallable::Adapter { async_lift: true })]);
+ assert!(!t.adapter_is_transparent(facts(0)));
+ }
+
+ /// Only the callee side is judged: an opaque caller calling into a clean
+ /// callee still gets a transparent adapter.
+ #[test]
+ fn only_the_callee_side_is_judged() {
+ let t = analyze(&[(0, CoreCallable::ContextAccess)]);
+ assert!(t.adapter_is_transparent(facts(1)));
+ }
+
+ /// Opacity does not propagate outward: in a chain `outer -> mid -> inner`
+ /// where only `inner` is opaque, the `outer -> mid` adapter is still
+ /// transparent because the `mid -> inner` call has its own adapter.
+ #[test]
+ fn opacity_does_not_propagate_outward() {
+ let t = analyze(&[(2, CoreCallable::ContextAccess)]);
+ assert!(t.adapter_is_transparent(facts(1)), "outer -> mid");
+ assert!(!t.adapter_is_transparent(facts(2)), "mid -> inner");
+ }
+
+ /// Nor inward: an opaque `outer` calling a clean `mid` calling a clean
+ /// `inner` leaves both adapters transparent.
+ #[test]
+ fn opacity_does_not_propagate_inward() {
+ let t = analyze(&[(0, CoreCallable::ContextAccess)]);
+ assert!(t.adapter_is_transparent(facts(1)), "outer -> mid");
+ assert!(t.adapter_is_transparent(facts(2)), "mid -> inner");
+ }
+
+ /// An unrelated sibling instance being opaque taints nobody else.
+ #[test]
+ fn siblings_are_judged_independently() {
+ let t = analyze(&[(0, CoreCallable::ContextAccess)]);
+ assert!(!t.adapter_is_transparent(facts(0)));
+ assert!(t.adapter_is_transparent(facts(1)));
+ }
+
+ #[test]
+ fn async_signature_is_never_transparent() {
+ let t = analyze(&[]);
+ assert!(!t.adapter_is_transparent(AdapterFacts {
+ any_async: true,
+ ..facts(0)
+ }));
+ }
+
+ #[test]
+ fn handle_in_signature_is_never_transparent() {
+ let t = analyze(&[]);
+ assert!(!t.adapter_is_transparent(AdapterFacts {
+ any_handle: true,
+ ..facts(0)
+ }));
+ }
+
+ /// The build phase is monotone, so observations may arrive in any order and
+ /// a clean observation can never undo an opaque one.
+ #[test]
+ fn observations_are_monotone_and_order_independent() {
+ let dirty_first = analyze(&[
+ (0, CoreCallable::ContextAccess),
+ (0, CoreCallable::Inert),
+ (0, CoreCallable::Adapter { async_lift: false }),
+ ]);
+ let dirty_last = analyze(&[
+ (0, CoreCallable::Adapter { async_lift: false }),
+ (0, CoreCallable::Inert),
+ (0, CoreCallable::ContextAccess),
+ ]);
+ assert!(!dirty_first.adapter_is_transparent(facts(0)));
+ assert!(!dirty_last.adapter_is_transparent(facts(0)));
+ }
+}
diff --git a/crates/environ/src/component/translate.rs b/crates/environ/src/component/translate.rs
index 3cbf3d0264..280c97574c 100644
--- a/crates/environ/src/component/translate.rs
+++ b/crates/environ/src/component/translate.rs
@@ -544,6 +544,11 @@ impl<'a, 'data> Translator<'a, 'data> {
&self.static_components,
)?;
+ // Now that inlining has finished and the dataflow graph is complete,
+ // determine which fused adapters can skip their
+ // `{enter,exit}-sync-call` calls.
+ component.transparent_adapters = transparent_adapters(&component, self.types.types());
+
self.partition_adapter_modules(&mut component);
let translation =
diff --git a/crates/environ/src/component/translate/adapt.rs b/crates/environ/src/component/translate/adapt.rs
index 9555adcb7a..d68bd0c41a 100644
--- a/crates/environ/src/component/translate/adapt.rs
+++ b/crates/environ/src/component/translate/adapt.rs
@@ -211,7 +211,7 @@ impl<'data> Translator<'_, 'data> {
let mut names = Vec::with_capacity(adapter_module.adapters.len());
for adapter in adapter_module.adapters.iter() {
let name = format!("adapter{}", adapter.as_u32());
- module.adapt(&name, &component.adapters[*adapter]);
+ module.adapt(&name, component, *adapter);
names.push(name);
}
let wasm = module.encode();
diff --git a/crates/environ/src/component/types_builder.rs b/crates/environ/src/component/types_builder.rs
index cb36353aeb..84ce84f874 100644
--- a/crates/environ/src/component/types_builder.rs
+++ b/crates/environ/src/component/types_builder.rs
@@ -825,7 +825,25 @@ impl ComponentTypesBuilder {
/// Returns whether the type specified contains any borrowed resources
/// within it.
pub fn ty_contains_borrow_resource(&self, ty: &InterfaceType) -> bool {
- self.type_information(ty).has_borrow
+ self.type_information(ty).handles.has_borrow()
+ }
+
+ /// Returns whether the type specified contains any handle within it, where
+ /// "handle" means `own`, `borrow`, `future`, `stream`, or `error-context`.
+ fn ty_contains_any_handle(&self, ty: &InterfaceType) -> bool {
+ self.type_information(ty).handles.has_any()
+ }
+
+ /// Returns whether the signature of `ty` mentions any handle, in either its
+ /// parameters or its results.
+ pub fn func_contains_any_handle(&self, ty: TypeFuncIndex) -> bool {
+ let ty = &self[ty];
+ let params = &self[ty.params].types;
+ let results = &self[ty.results].types;
+ params
+ .iter()
+ .chain(results.iter())
+ .any(|ty| self.ty_contains_any_handle(ty))
}
fn type_information(&self, ty: &InterfaceType) -> &TypeInformation {
@@ -837,18 +855,25 @@ impl ComponentTypesBuilder {
| InterfaceType::S16
| InterfaceType::U32
| InterfaceType::S32
- | InterfaceType::Char
- | InterfaceType::Own(_)
+ | InterfaceType::Char => {
+ static INFO: TypeInformation = TypeInformation::primitive(FlatType::I32);
+ &INFO
+ }
+ InterfaceType::Own(_)
| InterfaceType::Future(_)
| InterfaceType::Stream(_)
| InterfaceType::ErrorContext(_) => {
- static INFO: TypeInformation = TypeInformation::primitive(FlatType::I32);
+ static INFO: TypeInformation = {
+ let mut info = TypeInformation::primitive(FlatType::I32);
+ info.handles = Handles::NoBorrow;
+ info
+ };
&INFO
}
InterfaceType::Borrow(_) => {
static INFO: TypeInformation = {
let mut info = TypeInformation::primitive(FlatType::I32);
- info.has_borrow = true;
+ info.handles = Handles::Borrow;
info
};
&INFO
@@ -993,16 +1018,54 @@ struct TypeInformationCache {
fixed_length_lists: PrimaryMap<TypeFixedLengthListIndex, TypeInformation>,
}
+/// What kind of handles a type transitively contains.
+#[derive(Copy, Clone, PartialEq, Eq, Debug)]
+enum Handles {
+ /// This type contains no handles at all.
+ None,
+
+ /// This type contains at least one handle, but none of them are `borrow`s.
+ ///
+ /// That is, it contains an `own`, `future`, `stream`, or `error-context`.
+ NoBorrow,
+
+ /// This type contains at least one `borrow` handle.
+ Borrow,
+}
+
+impl Handles {
+ /// Does this type contain any handle at all?
+ const fn has_any(self) -> bool {
+ !matches!(self, Handles::None)
+ }
+
+ /// Does this type contain a `borrow` handle?
+ const fn has_borrow(self) -> bool {
+ matches!(self, Handles::Borrow)
+ }
+
+ /// Join two facts together.
+ const fn join(a: Self, b: Self) -> Handles {
+ match (a, b) {
+ (Handles::Borrow, _) | (_, Handles::Borrow) => Handles::Borrow,
+ (Handles::NoBorrow, _) | (_, Handles::NoBorrow) => Handles::NoBorrow,
+ (Handles::None, Handles::None) => Handles::None,
+ }
+ }
+}
+
struct TypeInformation {
flat: FlatTypesStorage,
- has_borrow: bool,
+
+ /// Which handles this type transitively contains, if any.
+ handles: Handles,
}
impl TypeInformation {
const fn new() -> TypeInformation {
TypeInformation {
flat: FlatTypesStorage::new(),
- has_borrow: false,
+ handles: Handles::None,
}
}
@@ -1028,7 +1091,7 @@ impl TypeInformation {
/// for all of the component fields of the record.
fn build_record<'a>(&mut self, types: impl Iterator<Item = &'a TypeInformation>) {
for info in types {
- self.has_borrow = self.has_borrow || info.has_borrow;
+ self.handles = Handles::join(self.handles, info.handles);
match info.flat.as_flat_types() {
Some(types) => {
for (t32, t64) in types.memory32.iter().zip(types.memory64) {
@@ -1069,7 +1132,7 @@ impl TypeInformation {
// the flat representation
None => continue,
};
- self.has_borrow = self.has_borrow || info.has_borrow;
+ self.handles = Handles::join(self.handles, info.handles);
// If this variant is already unrepresentable in a flat
// representation then this can be skipped.
@@ -1134,7 +1197,7 @@ impl TypeInformation {
fn fixed_length_lists(&mut self, types: &ComponentTypesBuilder, ty: &TypeFixedLengthList) {
let element_info = types.type_information(&ty.element);
- self.has_borrow = element_info.has_borrow;
+ self.handles = element_info.handles;
match element_info.flat.as_flat_types() {
Some(types) => {
'outer: for _ in 0..ty.size {
@@ -1189,7 +1252,7 @@ impl TypeInformation {
fn lists(&mut self, types: &ComponentTypesBuilder, ty: &TypeList) {
*self = TypeInformation::string();
let info = types.type_information(&ty.element);
- self.has_borrow = info.has_borrow;
+ self.handles = info.handles;
}
fn maps(&mut self, types: &ComponentTypesBuilder, ty: &TypeMap) {
@@ -1198,6 +1261,6 @@ impl TypeInformation {
*self = TypeInformation::string();
let key_info = types.type_information(&ty.key);
let value_info = types.type_information(&ty.value);
- self.has_borrow = key_info.has_borrow || value_info.has_borrow;
+ self.handles = Handles::join(key_info.handles, value_info.handles);
}
}
diff --git a/crates/environ/src/fact.rs b/crates/environ/src/fact.rs
index 650647decc..54f50880c6 100644
--- a/crates/environ/src/fact.rs
+++ b/crates/environ/src/fact.rs
@@ -18,11 +18,11 @@
//! their imports and then generating a core wasm module to implement all of
//! that.
-use crate::component::dfg::CoreDef;
+use crate::component::dfg::{AdapterId, ComponentDfg, CoreDef};
use crate::component::{
- Adapter, AdapterOptions as AdapterOptionsDfg, CanonicalAbiInfo, ComponentTypesBuilder,
- FlatType, InterfaceType, RuntimeComponentInstanceIndex, StringEncoding, Transcode,
- TypeFuncIndex, UnsafeIntrinsic,
+ AdapterOptions as AdapterOptionsDfg, CanonicalAbiInfo, ComponentTypesBuilder, FlatType,
+ InterfaceType, RuntimeComponentInstanceIndex, StringEncoding, Transcode, TypeFuncIndex,
+ UnsafeIntrinsic,
};
use crate::fact::transcode::Transcoder;
use crate::prelude::*;
@@ -125,6 +125,12 @@ struct AdapterData {
/// The core wasm function that this adapter will be calling (the original
/// function that was `canon lift`'d)
callee: FuncIndex,
+ /// Whether nothing this adapter can reach is able to observe or mutate the
+ /// thread state that `enter-sync-call`/`exit-sync-call` maintain, meaning
+ /// that pair can be omitted entirely.
+ ///
+ /// See `crates/environ/src/component/thread_transparency.rs` for details.
+ thread_transparent: bool,
}
/// Configuration options which apply at the "global adapter" level.
@@ -299,8 +305,12 @@ impl<'a> Module<'a> {
/// Registers a new adapter within this adapter module.
///
/// The `name` provided is the export name of the adapter from the final
- /// module, and `adapter` contains all metadata necessary for compilation.
- pub fn adapt(&mut self, name: &str, adapter: &Adapter) {
+ /// module, and `adapter` indexes into `component` for all the metadata
+ /// necessary for compilation.
+ pub fn adapt(&mut self, name: &str, component: &ComponentDfg, adapter: AdapterId) {
+ let thread_transparent = component.transparent_adapters.contains(adapter);
+ let adapter = &component.adapters[adapter];
+
// Import any items required by the various canonical options
// (memories, reallocs, etc)
let mut lift = self.import_options(adapter.lift_ty, &adapter.lift_options);
@@ -335,6 +345,7 @@ impl<'a> Module<'a> {
lift,
lower,
callee,
+ thread_transparent,
},
);
@@ -722,7 +733,7 @@ impl<'a> Module<'a> {
self.import_simple(
"async",
"enter-sync-call",
- &[ValType::I32; 3],
+ &[ValType::I32; 2],
&[],
Import::EnterSyncCall,
|me| &mut me.imported_enter_sync_call,
diff --git a/crates/environ/src/fact/trampoline.rs b/crates/environ/src/fact/trampoline.rs
index f55b4ccf09..84bd0aa062 100644
--- a/crates/environ/src/fact/trampoline.rs
+++ b/crates/environ/src/fact/trampoline.rs
@@ -756,7 +756,19 @@ impl<'a, 'b> Compiler<'a, 'b> {
let saved_lower_may_leave =
self.trap_if_not_may_leave(adapter.lower.flags, Trap::CannotLeaveComponent);
- if self.module.tunables.concurrency_support {
+ // If nothing that this adapter can reach is able to observe or mutate
+ // the thread state that `enter-sync-call`/`exit-sync-call` maintain then
+ // none of its bookkeeping is necessary.
+ //
+ // See `crates/environ/src/component/thread_transparency.rs` for details.
+ debug_assert!(
+ !(adapter.thread_transparent && self.emit_resource_call),
+ "resources are not thread transparent",
+ );
+ let needs_thread_state =
+ self.module.tunables.concurrency_support && !adapter.thread_transparent;
+
+ if needs_thread_state {
// Push a task onto the current task stack.
//
// Note that for sync-to-sync calls, we replace this call with
@@ -765,9 +777,6 @@ impl<'a, 'b> Compiler<'a, 'b> {
// adapter for most sync-to-sync calls, since most sync-to-sync
// calls do not do anything to force the task's creation
// (e.g. adjust backpressure).
- self.instruction(I32Const(
- i32::try_from(adapter.lower.instance.as_u32()).unwrap(),
- ));
self.instruction(I32Const(if self.types[adapter.lift.ty].async_ {
1
} else {
@@ -780,9 +789,6 @@ impl<'a, 'b> Compiler<'a, 'b> {
self.instruction(Call(enter_sync_call.as_u32()));
} else if self.emit_resource_call {
assert!(!self.types[adapter.lift.ty].async_);
- self.instruction(I32Const(
- i32::try_from(adapter.lower.instance.as_u32()).unwrap(),
- ));
self.instruction(I32Const(0));
self.instruction(I32Const(
i32::try_from(adapter.lift.instance.as_u32()).unwrap(),
@@ -875,7 +881,7 @@ impl<'a, 'b> Compiler<'a, 'b> {
// Note that for sync-to-sync calls, we will emit inline code during
// translation to CLIF to avoid actually calling out to a libcall when
// the deferred task's allocation was never forced.
- if self.emit_resource_call || self.module.tunables.concurrency_support {
+ if self.emit_resource_call || needs_thread_state {
let exit_sync_call = self.module.import_exit_sync_call();
self.instruction(Call(exit_sync_call.as_u32()));
}
diff --git a/crates/environ/src/vmtypes.rs b/crates/environ/src/vmtypes.rs
index a9b24f7722..aca8cdcc37 100644
--- a/crates/environ/src/vmtypes.rs
+++ b/crates/environ/src/vmtypes.rs
@@ -549,8 +549,6 @@ macro_rules! for_each_vm_type {
pub struct VMDeferredThread {
/// The previous value of `VMStoreContext::current_thread`.
pub parent: VMLazyThread,
- /// The caller component instance (a deferred `enter_sync_call` argument).
- pub caller_instance: u32,
/// Whether the callee is async-lifted (a deferred `enter_sync_call` arg).
pub callee_async: u32,
/// The callee component instance (a deferred `enter_sync_call` argument).
diff --git a/crates/wasmtime/src/runtime/component/concurrent.rs b/crates/wasmtime/src/runtime/component/concurrent.rs
index b7f8593a1f..d5ae0f5cb2 100644
--- a/crates/wasmtime/src/runtime/component/concurrent.rs
+++ b/crates/wasmtime/src/runtime/component/concurrent.rs
@@ -1646,6 +1646,18 @@ impl<T> StoreContextMut<'_, T> {
/// Tasks are yielded "youngest first" where the first item in the iterator
/// is the current task, and the last item in the iterator is the original
/// call.
+ ///
+ /// # Omitted Tasks
+ ///
+ /// This stack might not contain an entry for every guest-to-guest component
+ /// call that is currently executing: if those guest-to-guest calls are not
+ /// asynchronous and provably cannot access async task state, then Wasmtime
+ /// can avoid materializing the guest task state for that call, and such
+ /// tasks will not have a `GuestTaskId` and will not appear in this stack
+ /// trace.
+ ///
+ /// The `GuestTaskId`s for host-to-guest and guest-to-host calls, however,
+ /// will always appear in this stack trace.
pub fn async_call_stack(&mut self) -> Result<impl Iterator<Item = GuestTaskId>> {
let mut cur = Some(self.0.current_thread()?);
let state = self.0.concurrent_state_mut()?;
@@ -1773,7 +1785,7 @@ impl StoreOpaque {
instance: id,
index: RuntimeComponentInstanceIndex::from_u32(callee_instance),
};
- self.enter_guest_sync_call(None, callee_async, callee)?;
+ self.enter_guest_sync_call(callee_async, callee)?;
}
// Replaying done; restore the current context.
@@ -1852,7 +1864,6 @@ impl StoreOpaque {
/// sync!
pub(crate) fn enter_guest_sync_call(
&mut self,
- guest_caller: Option<RuntimeInstance>,
callee_async_typed: bool,
callee: RuntimeInstance,
) -> Result<()> {
@@ -1863,14 +1874,6 @@ impl StoreOpaque {
let thread = self.current_thread()?;
let state = self.concurrent_state_mut()?;
- let instance = if let Some(task) = thread.guest_task() {
- Some(state.get_mut(task)?.instance)
- } else {
- None
- };
- if guest_caller.is_some() {
- debug_assert_eq!(instance, guest_caller);
- }
let guest_thread = GuestTask::new(
state,
Box::new(move |_, _| bail_bug!("cannot lower params in sync call")),
diff --git a/crates/wasmtime/src/runtime/component/concurrent_disabled.rs b/crates/wasmtime/src/runtime/component/concurrent_disabled.rs
index 9be79a482b..2ad8e26a4c 100644
--- a/crates/wasmtime/src/runtime/component/concurrent_disabled.rs
+++ b/crates/wasmtime/src/runtime/component/concurrent_disabled.rs
@@ -151,7 +151,6 @@ unsafe impl Lower for StreamAny {
impl StoreOpaque {
pub(crate) fn enter_guest_sync_call(
&mut self,
- _guest_caller: Option<RuntimeInstance>,
_callee_async: bool,
_callee: RuntimeInstance,
) -> Result<()> {
diff --git a/crates/wasmtime/src/runtime/component/func.rs b/crates/wasmtime/src/runtime/component/func.rs
index a9cf79707f..8d0eb34cf5 100644
--- a/crates/wasmtime/src/runtime/component/func.rs
+++ b/crates/wasmtime/src/runtime/component/func.rs
@@ -469,7 +469,7 @@ impl Func {
bail!(crate::Trap::CannotEnterComponent);
}
- store.0.enter_guest_sync_call(None, async_, instance)?;
+ store.0.enter_guest_sync_call(async_, instance)?;
#[repr(C)]
union Union<Params: Copy, Return: Copy> {
diff --git a/crates/wasmtime/src/runtime/component/instance.rs b/crates/wasmtime/src/runtime/component/instance.rs
index 8bf24920e9..548024c22b 100644
--- a/crates/wasmtime/src/runtime/component/instance.rs
+++ b/crates/wasmtime/src/runtime/component/instance.rs
@@ -817,7 +817,6 @@ impl<'a> Instantiator<'a> {
let exit = if let Some(component_instance) = *component_instance {
store.0.enter_guest_sync_call(
- None,
false,
RuntimeInstance {
instance,
diff --git a/crates/wasmtime/src/runtime/component/resources/any.rs b/crates/wasmtime/src/runtime/component/resources/any.rs
index 0cfb1220f1..8dda3dc2d4 100644
--- a/crates/wasmtime/src/runtime/component/resources/any.rs
+++ b/crates/wasmtime/src/runtime/component/resources/any.rs
@@ -218,7 +218,7 @@ impl ResourceAny {
// means that this is a host resource being destroyed by the host. In
// that case restrictions around blocking and such are exempt.
if let Some(instance) = slot.instance {
- store.0.enter_guest_sync_call(None, false, instance)?;
+ store.0.enter_guest_sync_call(false, instance)?;
}
// This should be safe because `dtor` has been checked to belong to the
diff --git a/crates/wasmtime/src/runtime/vm/component/libcalls.rs b/crates/wasmtime/src/runtime/vm/component/libcalls.rs
index 18b881a2e2..295b253b75 100644
--- a/crates/wasmtime/src/runtime/vm/component/libcalls.rs
+++ b/crates/wasmtime/src/runtime/vm/component/libcalls.rs
@@ -671,12 +671,10 @@ fn resource_transfer_borrow(
fn enter_sync_call(
store: &mut dyn VMStore,
instance: Instance,
- caller_instance: u32,
callee_async: u32,
callee_instance: u32,
) -> Result<()> {
store.enter_guest_sync_call(
- Some(instance.runtime_instance(RuntimeComponentInstanceIndex::from_u32(caller_instance))),
callee_async != 0,
instance.runtime_instance(RuntimeComponentInstanceIndex::from_u32(callee_instance)),
)
diff --git a/tests/all/component_model/async.rs b/tests/all/component_model/async.rs
index b3ad7b4f89..43c3ab23a0 100644
--- a/tests/all/component_model/async.rs
+++ b/tests/all/component_model/async.rs
@@ -1063,6 +1063,108 @@ async fn async_call_stack() -> Result<()> {
Ok(())
}
+#[tokio::test]
+#[cfg_attr(miri, ignore)]
+async fn async_call_stack_omits_transparent_adapters() -> Result<()> {
+ async fn call_stack(taint: bool) -> Result<(Vec<GuestTaskId>, GuestTaskId)> {
+ let mut config = Config::new();
+ config.wasm_component_model_async(true);
+ let engine = Engine::new(&config)?;
+
+ let (taint_canon, taint_import, taint_arg) = if taint {
+ (
+ r#"(core func $ctx (canon context.get i32 0))"#,
+ r#"(import "" "ctx" (func $ctx (result i32)))"#,
+ r#"(export "ctx" (func $ctx))"#,
+ )
+ } else {
+ ("", "", "")
+ };
+
+ let component = Component::new(
+ &engine,
+ &format!(
+ r#"
+ (component
+ (import "a" (func $a))
+
+ ;; Lowers the host import, so this is never thread-transparent.
+ (component $Deep
+ (import "a" (func $a))
+ (core func $a (canon lower (func $a)))
+ (core module $m
+ (import "" "a" (func $a))
+ (func (export "a") call $a))
+ (core instance $m (instantiate $m
+ (with "" (instance (export "a" (func $a))))))
+ (func (export "a") (canon lift (core func $m "a")))
+ )
+
+ ;; Declares nothing but a `canon lower` of an imported *lifted*,
+ ;; non-`async` function, so an instance of this component is
+ ;; thread-transparent -- unless the taint below is present.
+ (component $Mid
+ (import "a" (func $a))
+ {taint_canon}
+ (core func $a (canon lower (func $a)))
+ (core module $m
+ (import "" "a" (func $a))
+ {taint_import}
+ (func (export "a") call $a))
+ (core instance $m (instantiate $m
+ (with "" (instance (export "a" (func $a)) {taint_arg}))))
+ (func (export "a") (canon lift (core func $m "a")))
+ )
+
+ (instance $deep (instantiate $Deep (with "a" (func $a))))
+ (instance $mid (instantiate $Mid (with "a" (func $deep "a"))))
+ (instance $top (instantiate $Mid (with "a" (func $mid "a"))))
+ (export "a" (func $top "a"))
+ )
+ "#
+ ),
+ )?;
+
+ let mut linker = Linker::new(&engine);
+ linker.root().func_wrap(
+ "a",
+ |mut store: StoreContextMut<Option<Vec<GuestTaskId>>>, (): ()| {
+ let stack = store.async_call_stack()?.collect::<Vec<_>>();
+ *store.data_mut() = Some(stack);
+ Ok(())
+ },
+ )?;
+
+ let mut store = Store::new(&engine, None);
+ let instance = linker.instantiate_async(&mut store, &component).await?;
+ let func = instance.get_typed_func::<(), ()>(&mut store, "a")?;
+
+ let call = func.start_call_concurrent(&mut store, ())?;
+ let root = call.task();
+ store
+ .run_concurrent(async |store| func.finish_call_concurrent(store, call).await)
+ .await??;
+
+ Ok((store.data_mut().take().unwrap(), root))
+ }
+
+ // With the intermediate instances tainted into opacity, all three
+ // guest-to-guest calls materialize a task and all three show up.
+ let (stack, root) = call_stack(true).await?;
+ assert_eq!(stack.len(), 3);
+ assert_eq!(stack.last(), Some(&root));
+
+ // Without the taint, `$top -> $mid` is a thread-transparent call, its task
+ // is never materialized, and so it is omitted from the stack. The root
+ // host-to-guest call is always materialized, so it is still reported, and
+ // `$mid -> $deep` still is too since `$deep` is opaque.
+ let (stack, root) = call_stack(false).await?;
+ assert_eq!(stack.len(), 2);
+ assert_eq!(stack.last(), Some(&root));
+
+ Ok(())
+}
+
#[test]
#[cfg_attr(miri, ignore)]
fn inter_component_stream_is_not_intra_component() -> Result<()> {
diff --git a/tests/all/component_model/sync_call_inline.rs b/tests/all/component_model/sync_call_inline.rs
index fe2130b8be..c807db21db 100644
--- a/tests/all/component_model/sync_call_inline.rs
+++ b/tests/all/component_model/sync_call_inline.rs
@@ -267,6 +267,287 @@ async fn repeated_calls_have_no_state_leak() -> Result<()> {
Ok(())
}
+#[tokio::test]
+async fn transparent_frame_nested_inside_opaque_one() -> Result<()> {
+ let component = r#"
+(component
+ (import "poke" (func $poke))
+
+ (component $Leaf
+ (core module $M
+ (func (export "leaf'") (param i32) (result i32)
+ (i32.add (local.get 0) (i32.const 1))))
+ (core instance $m (instantiate $M))
+ (func (export "leaf") (param "x" u32) (result u32)
+ (canon lift (core func $m "leaf'"))))
+
+ (component $Mid
+ (import "leaf" (func $leaf (param "x" u32) (result u32)))
+ (import "poke" (func $poke))
+ (core func $leaf' (canon lower (func $leaf)))
+ (core func $poke' (canon lower (func $poke)))
+ (core func $cget (canon context.get i32 0))
+ (core func $cset (canon context.set i32 0))
+ (core module $M
+ (import "" "leaf'" (func $leaf' (param i32) (result i32)))
+ (import "" "poke" (func $poke'))
+ (import "" "cget" (func $cget (result i32)))
+ (import "" "cset" (func $cset (param i32)))
+ (func (export "mid'") (param i32) (result i32) (local $r i32)
+ ;; Freshly entered deferred thread: context starts zeroed.
+ (if (i32.ne (call $cget) (i32.const 0)) (then unreachable))
+ (call $cset (i32.const 0x0d00d100))
+ ;; Call through the transparent adapter, which pushes no frame.
+ (local.set $r (call $leaf' (local.get 0)))
+ ;; Only now force the deferred thread, after the omitted frame has
+ ;; come and gone.
+ (call $poke')
+ (if (i32.ne (call $cget) (i32.const 0x0d00d100)) (then unreachable))
+ (i32.add (local.get $r) (i32.const 10))))
+ (core instance $m (instantiate $M (with "" (instance
+ (export "leaf'" (func $leaf'))
+ (export "poke" (func $poke'))
+ (export "cget" (func $cget))
+ (export "cset" (func $cset))))))
+ (func (export "mid") (param "x" u32) (result u32)
+ (canon lift (core func $m "mid'"))))
+
+ (component $Root
+ (import "mid" (func $mid (param "x" u32) (result u32)))
+ (core func $mid' (canon lower (func $mid)))
+ (core func $cget (canon context.get i32 0))
+ (core func $cset (canon context.set i32 0))
+ (core module $M
+ (import "" "mid'" (func $mid' (param i32) (result i32)))
+ (import "" "cget" (func $cget (result i32)))
+ (import "" "cset" (func $cset (param i32)))
+ (func (export "root'") (result i32) (local $r i32)
+ (call $cset (i32.const 0x0badf00d))
+ (local.set $r (call $mid' (i32.const 100)))
+ (if (i32.ne (call $cget) (i32.const 0x0badf00d)) (then unreachable))
+ (i32.add (local.get $r) (i32.const 1000))))
+ (core instance $m (instantiate $M (with "" (instance
+ (export "mid'" (func $mid'))
+ (export "cget" (func $cget))
+ (export "cset" (func $cset))))))
+ (func (export "root") (result u32)
+ (canon lift (core func $m "root'"))))
+
+ (instance $leaf (instantiate $Leaf))
+ (instance $mid (instantiate $Mid
+ (with "leaf" (func $leaf "leaf"))
+ (with "poke" (func $poke))))
+ (instance $root (instantiate $Root (with "mid" (func $mid "mid"))))
+ (export "root" (func $root "root"))
+)
+ "#;
+
+ let engine = engine();
+ let component = Component::new(&engine, component)?;
+ let mut store = Store::new(&engine, 0u32);
+ let mut linker = Linker::new(&engine);
+ linker
+ .root()
+ .func_wrap("poke", |mut cx: StoreContextMut<u32>, (): ()| {
+ *cx.data_mut() += 1;
+ Ok(())
+ })?;
+ let instance = linker.instantiate_async(&mut store, &component).await?;
+ let root = instance.get_typed_func::<(), (u32,)>(&mut store, "root")?;
+
+ // Call more than once so that any state the omitted frame failed to clean
+ // up would be visible on a later call.
+ for i in 1..=3 {
+ let (result,) = root.call_async(&mut store, ()).await?;
+ assert_eq!(result, 1111);
+ assert_eq!(*store.data(), i);
+ }
+ Ok(())
+}
+
+#[tokio::test]
+async fn opaque_frame_nested_inside_transparent_one() -> Result<()> {
+ let component = r#"
+(component
+ (import "poke" (func $poke))
+
+ (component $Leaf
+ (import "poke" (func $poke))
+ (core func $poke' (canon lower (func $poke)))
+ (core func $cget (canon context.get i32 0))
+ (core func $cset (canon context.set i32 0))
+ (core module $M
+ (import "" "poke" (func $poke'))
+ (import "" "cget" (func $cget (result i32)))
+ (import "" "cset" (func $cset (param i32)))
+ (func (export "leaf'") (param i32) (result i32)
+ ;; The maintained frame zeroed the context on the way in.
+ (if (i32.ne (call $cget) (i32.const 0)) (then unreachable))
+ (call $cset (i32.const 0x0feed000))
+ ;; Force the deferred thread from the innermost frame.
+ (call $poke')
+ (if (i32.ne (call $cget) (i32.const 0x0feed000)) (then unreachable))
+ (i32.add (local.get 0) (i32.const 1))))
+ (core instance $m (instantiate $M (with "" (instance
+ (export "poke" (func $poke'))
+ (export "cget" (func $cget))
+ (export "cset" (func $cset))))))
+ (func (export "leaf") (param "x" u32) (result u32)
+ (canon lift (core func $m "leaf'"))))
+
+ ;; Nothing in here can observe or mutate thread state, so the adapter that
+ ;; lifts `mid` is thread-transparent.
+ (component $Mid
+ (import "leaf" (func $leaf (param "x" u32) (result u32)))
+ (core func $leaf' (canon lower (func $leaf)))
+ (core module $M
+ (import "" "leaf'" (func $leaf' (param i32) (result i32)))
+ (func (export "mid'") (param i32) (result i32)
+ (i32.add (call $leaf' (local.get 0)) (i32.const 10))))
+ (core instance $m (instantiate $M (with "" (instance
+ (export "leaf'" (func $leaf'))))))
+ (func (export "mid") (param "x" u32) (result u32)
+ (canon lift (core func $m "mid'"))))
+
+ (component $Root
+ (import "mid" (func $mid (param "x" u32) (result u32)))
+ (core func $mid' (canon lower (func $mid)))
+ (core func $cget (canon context.get i32 0))
+ (core func $cset (canon context.set i32 0))
+ (core module $M
+ (import "" "mid'" (func $mid' (param i32) (result i32)))
+ (import "" "cget" (func $cget (result i32)))
+ (import "" "cset" (func $cset (param i32)))
+ (func (export "root'") (result i32) (local $r i32)
+ (call $cset (i32.const 0x0badf00d))
+ (local.set $r (call $mid' (i32.const 100)))
+ ;; Neither `$Mid` nor `$Leaf` disturbed our slot.
+ (if (i32.ne (call $cget) (i32.const 0x0badf00d)) (then unreachable))
+ (i32.add (local.get $r) (i32.const 1000))))
+ (core instance $m (instantiate $M (with "" (instance
+ (export "mid'" (func $mid'))
+ (export "cget" (func $cget))
+ (export "cset" (func $cset))))))
+ (func (export "root") (result u32)
+ (canon lift (core func $m "root'"))))
+
+ (instance $leaf (instantiate $Leaf (with "poke" (func $poke))))
+ (instance $mid (instantiate $Mid (with "leaf" (func $leaf "leaf"))))
+ (instance $root (instantiate $Root (with "mid" (func $mid "mid"))))
+ (export "root" (func $root "root"))
+)
+ "#;
+
+ let engine = engine();
+ let component = Component::new(&engine, component)?;
+ let mut store = Store::new(&engine, 0u32);
+ let mut linker = Linker::new(&engine);
+ linker
+ .root()
+ .func_wrap("poke", |mut cx: StoreContextMut<u32>, (): ()| {
+ *cx.data_mut() += 1;
+ Ok(())
+ })?;
+ let instance = linker.instantiate_async(&mut store, &component).await?;
+ let root = instance.get_typed_func::<(), (u32,)>(&mut store, "root")?;
+
+ for i in 1..=3 {
+ let (result,) = root.call_async(&mut store, ()).await?;
+ assert_eq!(result, 1111);
+ assert_eq!(*store.data(), i);
+ }
+ Ok(())
+}
+
+#[tokio::test]
+async fn trap_unwinds_through_transparent_frames() -> Result<()> {
+ let component = r#"
+(component
+ (import "poke" (func $poke))
+
+ (component $Leaf
+ (core module $M
+ (func (export "leaf'") (param i32) (result i32) unreachable))
+ (core instance $m (instantiate $M))
+ (func (export "leaf") (param "x" u32) (result u32)
+ (canon lift (core func $m "leaf'"))))
+
+ (component $Mid
+ (import "leaf" (func $leaf (param "x" u32) (result u32)))
+ (import "poke" (func $poke))
+ (core func $leaf' (canon lower (func $leaf)))
+ (core func $poke' (canon lower (func $poke)))
+ (core func $cset (canon context.set i32 0))
+ (core module $M
+ (import "" "leaf'" (func $leaf' (param i32) (result i32)))
+ (import "" "poke" (func $poke'))
+ (import "" "cset" (func $cset (param i32)))
+ (func (export "mid'") (param i32) (result i32)
+ (call $cset (i32.const 0x0d00d100))
+ ;; Force the deferred thread first so that the unwind below has a real
+ ;; task to tear down rather than just a stack slot.
+ (call $poke')
+ (call $leaf' (local.get 0))))
+ (core instance $m (instantiate $M (with "" (instance
+ (export "leaf'" (func $leaf'))
+ (export "poke" (func $poke'))
+ (export "cset" (func $cset))))))
+ (func (export "mid") (param "x" u32) (result u32)
+ (canon lift (core func $m "mid'"))))
+
+ (component $Root
+ (import "mid" (func $mid (param "x" u32) (result u32)))
+ (core func $mid' (canon lower (func $mid)))
+ (core module $M
+ (import "" "mid'" (func $mid' (param i32) (result i32)))
+ (func (export "root'") (result i32) (call $mid' (i32.const 1))))
+ (core instance $m (instantiate $M (with "" (instance
+ (export "mid'" (func $mid'))))))
+ (func (export "root") (result u32)
+ (canon lift (core func $m "root'"))))
+
+ (instance $leaf (instantiate $Leaf))
+ (instance $mid (instantiate $Mid
+ (with "leaf" (func $leaf "leaf"))
+ (with "poke" (func $poke))))
+ (instance $root (instantiate $Root (with "mid" (func $mid "mid"))))
+ (export "root" (func $root "root"))
+)
+ "#;
+
+ let other = r#"
+(component
+ (core module $m (func (export "x")))
+ (core instance (instantiate $m))
+)
+ "#;
+
+ let engine = engine();
+ let component = Component::new(&engine, component)?;
+ let other = Component::new(&engine, other)?;
+ let mut store = Store::new(&engine, 0u32);
+ let mut linker = Linker::new(&engine);
+ linker
+ .root()
+ .func_wrap("poke", |mut cx: StoreContextMut<u32>, (): ()| {
+ *cx.data_mut() += 1;
+ Ok(())
+ })?;
+ let instance = linker.instantiate_async(&mut store, &component).await?;
+ let root = instance.get_typed_func::<(), (u32,)>(&mut store, "root")?;
+
+ let err = root.call_async(&mut store, ()).await.unwrap_err();
+ assert!(
+ err.downcast_ref::<wasmtime::Trap>().is_some(),
+ "expected a trap, got: {err:?}"
+ );
+ assert_eq!(*store.data(), 1);
+
+ // The store is still usable afterwards.
+ let _ = linker.instantiate_async(&mut store, &other).await?;
+ Ok(())
+}
+
#[tokio::test]
async fn trap_then_instantiate_uses_freed_deferred_thread() -> Result<()> {
let trapping = [
@@ -456,3 +737,167 @@ async fn trap_then_instantiate_uses_freed_deferred_thread() -> Result<()> {
}
Ok(())
}
+
+#[tokio::test]
+async fn shared_table_does_not_leak_context() -> Result<()> {
+ // The `$Victim`/`$Evil` pair, parameterized over which core module owns the
+ // shared table and what else `$Victim` imports.
+ fn component(inner: &str) -> String {
+ format!(
+ r#"
+(component
+ (component $Inner {inner})
+
+ (component $Outer
+ (import "f" (func $f (result u32)))
+ (core func $f' (canon lower (func $f)))
+ (core func $cget (canon context.get i32 0))
+ (core func $cset (canon context.set i32 0))
+ (core module $N
+ (import "" "f" (func $f' (result i32)))
+ (import "" "cget" (func $cget (result i32)))
+ (import "" "cset" (func $cset (param i32)))
+ ;; What the callee saw in *our* slot.
+ (func (export "g") (result i32)
+ (call $cset (i32.const 0x1234))
+ (call $f'))
+ ;; Our slot after the call returns.
+ (func (export "h") (result i32)
+ (call $cset (i32.const 0x1234))
+ (drop (call $f'))
+ (call $cget)))
+ (core instance $n (instantiate $N (with "" (instance
+ (export "f" (func $f'))
+ (export "cget" (func $cget))
+ (export "cset" (func $cset))))))
+ (func (export "g") (result u32) (canon lift (core func $n "g")))
+ (func (export "h") (result u32) (canon lift (core func $n "h"))))
+
+ (instance $inner (instantiate $Inner))
+ (instance $outer (instantiate $Outer (with "f" (func $inner "f"))))
+ (export "g" (func $outer "g"))
+ (export "h" (func $outer "h"))
+)
+ "#
+ )
+ }
+
+ // `$Evil`'s body: read the caller's slot, clobber it, return what was read.
+ // The element segment writes into the *imported* table, so this happens
+ // during instantiation with no start function or host call involved.
+ const EVIL: &str = r#"
+ (core module $Evil
+ (import "" "t" (table 1 funcref))
+ (import "" "cget" (func $cget (result i32)))
+ (import "" "cset" (func $cset (param i32)))
+ (func $leak (result i32) (local $t i32)
+ (local.set $t (call $cget))
+ (call $cset (i32.const 0x9999))
+ (local.get $t))
+ (elem (table 0) (i32.const 0) func $leak))
+ "#;
+
+ let cases = [
+ // The table is defined by a third module that imports nothing at all.
+ component(&format!(
+ r#"
+ (core func $cget (canon context.get i32 0))
+ (core func $cset (canon context.set i32 0))
+
+ (core module $Shared (table (export "t") 1 funcref))
+ (core instance $shared (instantiate $Shared))
+
+ {EVIL}
+ (core instance $evil (instantiate $Evil (with "" (instance
+ (export "t" (table $shared "t"))
+ (export "cget" (func $cget))
+ (export "cset" (func $cset))))))
+
+ (core module $Victim
+ (import "" "t" (table 1 funcref))
+ (type $sig (func (result i32)))
+ (func (export "f") (result i32)
+ (call_indirect (type $sig) (i32.const 0))))
+ (core instance $victim (instantiate $Victim (with "" (instance
+ (export "t" (table $shared "t"))))))
+
+ (func (export "f") (result u32) (canon lift (core func $victim "f")))
+ "#
+ )),
+ // Same thing with only two core modules: the table is defined and
+ // exported by `$Victim` itself, which still imports nothing.
+ component(&format!(
+ r#"
+ (core func $cget (canon context.get i32 0))
+ (core func $cset (canon context.set i32 0))
+
+ (core module $Victim
+ (table (export "t") 1 funcref)
+ (type $sig (func (result i32)))
+ (func (export "f") (result i32)
+ (call_indirect (type $sig) (i32.const 0))))
+ (core instance $victim (instantiate $Victim))
+
+ {EVIL}
+ (core instance $evil (instantiate $Evil (with "" (instance
+ (export "t" (table $victim "t"))
+ (export "cget" (func $cget))
+ (export "cset" (func $cset))))))
+
+ (func (export "f") (result u32) (canon lift (core func $victim "f")))
+ "#
+ )),
+ // Control: `$Victim` also directly imports `context.get`, which even a
+ // core-instance-granularity analysis would have seen.
+ component(&format!(
+ r#"
+ (core func $cget (canon context.get i32 0))
+ (core func $cset (canon context.set i32 0))
+
+ (core module $Shared (table (export "t") 1 funcref))
+ (core instance $shared (instantiate $Shared))
+
+ {EVIL}
+ (core instance $evil (instantiate $Evil (with "" (instance
+ (export "t" (table $shared "t"))
+ (export "cget" (func $cget))
+ (export "cset" (func $cset))))))
+
+ (core module $Victim
+ (import "" "t" (table 1 funcref))
+ (import "" "cget" (func (result i32)))
+ (type $sig (func (result i32)))
+ (func (export "f") (result i32)
+ (call_indirect (type $sig) (i32.const 0))))
+ (core instance $victim (instantiate $Victim (with "" (instance
+ (export "t" (table $shared "t"))
+ (export "cget" (func $cget))))))
+
+ (func (export "f") (result u32) (canon lift (core func $victim "f")))
+ "#
+ )),
+ ];
+
+ let engine = engine();
+ for (i, wat) in cases.iter().enumerate() {
+ let component = Component::new(&engine, wat)?;
+ let mut store = Store::new(&engine, ());
+ let linker = Linker::new(&engine);
+ let instance = linker.instantiate_async(&mut store, &component).await?;
+
+ let g = instance.get_typed_func::<(), (u32,)>(&mut store, "g")?;
+ let (leaked,) = g.call_async(&mut store, ()).await?;
+ assert_eq!(
+ leaked, 0,
+ "case {i}: callee read the caller's private context slot: {leaked:#x}",
+ );
+
+ let h = instance.get_typed_func::<(), (u32,)>(&mut store, "h")?;
+ let (after,) = h.call_async(&mut store, ()).await?;
+ assert_eq!(
+ after, 0x1234,
+ "case {i}: callee clobbered the caller's context slot: {after:#x}",
+ );
+ }
+ Ok(())
+}
diff --git a/tests/disas/component-model/sync-adapter-calls-x64.wat b/tests/disas/component-model/sync-adapter-calls-x64.wat
index 49e9ec05de..1511868ce1 100644
--- a/tests/disas/component-model/sync-adapter-calls-x64.wat
+++ b/tests/disas/component-model/sync-adapter-calls-x64.wat
@@ -54,39 +54,13 @@
;; wasm[1]::function[1]:
;; pushq %rbp
;; movq %rsp, %rbp
-;; movq 8(%rdi), %r10
-;; movq 0x18(%r10), %r10
-;; addq $0x20, %r10
-;; cmpq %rsp, %r10
-;; ja 0xd2
-;; 39: subq $0x20, %rsp
-;; movq 0x48(%rdi), %rdx
-;; movq 0xe8(%rdx), %rax
-;; movl (%rax), %ecx
-;; testl %ecx, %ecx
-;; je 0xd4
-;; 52: movq 8(%rdx), %rdx
-;; movq 0x88(%rdx), %rsi
-;; leaq (%rsp), %r8
-;; movq %rsi, (%rsp)
-;; movl $2, 8(%rsp)
-;; movl $0, 0xc(%rsp)
-;; movl $1, 0x10(%rsp)
-;; movl 0x80(%rdx), %edi
-;; movl %edi, 0x14(%rsp)
-;; movl $0, 0x80(%rdx)
-;; movl 0x84(%rdx), %r9d
-;; movl %r9d, 0x18(%rsp)
-;; movl $0, 0x84(%rdx)
-;; movq %r8, 0x88(%rdx)
-;; movq %rsi, 0x88(%rdx)
-;; movl %edi, 0x80(%rdx)
-;; movl %r9d, 0x84(%rdx)
-;; movl %ecx, (%rax)
-;; movl $0x4fc, %eax
-;; addq $0x20, %rsp
+;; movq 0x48(%rdi), %rdi
+;; movq 0xa8(%rdi), %rdi
+;; movl (%rdi), %edi
+;; testl %edi, %edi
+;; je 0x43
+;; 39: movl $0x4fc, %eax
;; movq %rbp, %rsp
;; popq %rbp
;; retq
-;; d2: ud2
-;; d4: ud2
+;; 43: ud2
diff --git a/tests/disas/component-model/sync-adapter-calls.wat b/tests/disas/component-model/sync-adapter-calls.wat
index 6b8c2c0977..3abc2b8492 100644
--- a/tests/disas/component-model/sync-adapter-calls.wat
+++ b/tests/disas/component-model/sync-adapter-calls.wat
@@ -52,22 +52,12 @@
)
;; function u1:0(i64 vmctx, i64) -> i32 tail {
-;; ss0 = explicit_slot 32, align = 8
;; region0 = 123 ""
;; region1 = 160 ""
;; region2 = 25 ""
;; region3 = 44 ""
;; region4 = 78 ""
-;; region5 = 68 ""
-;; region6 = 198 ""
-;; region7 = 100 ""
-;; region8 = 254 ""
-;; region9 = 176 ""
-;; region10 = 112 ""
-;; region11 = 224 ""
-;; region12 = 149 ""
-;; region13 = 62 ""
-;; region14 = 227 ""
+;; region5 = 227 ""
;; gv0 = vmctx
;; gv1 = load.i64 notrap aligned readonly can_move region0 gv0+8
;; gv2 = load.i64 notrap aligned region1 gv1+24
@@ -79,10 +69,9 @@
;; gv8 = load.i64 notrap aligned region1 gv7+24
;; sig0 = (i64 vmctx, i64, i32) -> i32 tail
;; sig1 = (i64 vmctx, i64) tail
-;; sig2 = (i64 vmctx, i64, i32, i32, i32) tail
-;; sig3 = (i64 vmctx, i64, i32) -> i32 tail
+;; sig2 = (i64 vmctx, i64, i32) -> i32 tail
;; fn0 = colocated u2:0 sig0
-;; fn1 = colocated u0:0 sig3
+;; fn1 = colocated u0:0 sig2
;; stack_limit = gv2
;;
;; block0(v0: i64, v1: i64):
@@ -93,50 +82,23 @@
;;
;; block6:
;; @00ee v3 = load.i64 notrap aligned readonly can_move region2 v0+72
-;; v9 = load.i64 notrap aligned readonly can_move region3 v3+232
+;; v9 = load.i64 notrap aligned readonly can_move region3 v3+168
;; v10 = load.i32 notrap aligned region4 v9
;; trapz v10, user26
;; jump block9
;;
;; block9:
-;; v16 = load.i64 notrap aligned readonly can_move region0 v3+8
-;; v17 = load.i64 notrap aligned region5 v16+136
-;; v15 = stack_addr.i64 ss0
-;; store notrap aligned region6 v17, v15
-;; v11 = iconst.i32 2
-;; store notrap aligned region7 v11, v15+8 ; v11 = 2
-;; v8 = iconst.i32 0
-;; store notrap aligned region8 v8, v15+12 ; v8 = 0
-;; v13 = iconst.i32 1
-;; store notrap aligned region9 v13, v15+16 ; v13 = 1
-;; v18 = load.i32 notrap aligned region10 v16+128
-;; store notrap aligned region11 v18, v15+20
-;; store notrap aligned region10 v8, v16+128 ; v8 = 0
-;; v20 = load.i32 notrap aligned region12 v16+132
-;; store notrap aligned region13 v20, v15+24
-;; store notrap aligned region12 v8, v16+132 ; v8 = 0
-;; store notrap aligned region5 v15, v16+136
-;; v22 = load.i64 notrap aligned readonly can_move region3 v3+208
-;; v23 = load.i32 notrap aligned region14 v22
-;; jump block16
+;; v11 = load.i64 notrap aligned readonly can_move region3 v3+144
+;; v12 = load.i32 notrap aligned region5 v11
+;; jump block12
;;
-;; block16:
-;; jump block17
+;; block12:
+;; jump block13
;;
-;; block17:
+;; block13:
;; jump block11
;;
;; block11:
-;; jump block12
-;;
-;; block12:
-;; store.i64 notrap aligned region5 v17, v16+136
-;; store.i32 notrap aligned region10 v18, v16+128
-;; store.i32 notrap aligned region12 v20, v16+132
-;; jump block14
-;;
-;; block14:
-;; store.i32 notrap aligned region4 v10, v9
;; jump block7
;;
;; block7:
@@ -146,12 +108,12 @@
;; jump block3
;;
;; block3:
-;; jump block18
+;; jump block14
;;
-;; block18:
+;; block14:
;; @00f0 jump block1
;;
;; block1:
-;; v45 = iconst.i32 1276
-;; @00f0 return v45 ; v45 = 1276
+;; v23 = iconst.i32 1276
+;; @00f0 return v23 ; v23 = 1276
;; }
diff --git a/tests/disas/component-model/thread-transparency/opaque.wat b/tests/disas/component-model/thread-transparency/opaque.wat
new file mode 100644
index 0000000000..e9e1e8fd73
--- /dev/null
+++ b/tests/disas/component-model/thread-transparency/opaque.wat
@@ -0,0 +1,170 @@
+;;! target = "x86_64"
+;;! test = "optimize"
+;;! filter = "wasm[1]--function"
+;;! flags = "-C inlining=y -Wconcurrency-support=y"
+
+;; The callee here imports `canon context.get`, so the adapter is *not*
+;; thread-transparent and the `enter-sync-call`/`exit-sync-call` calls must
+;; still be emitted.
+
+(component
+ (component $A
+ (core func $cget (canon context.get i32 0))
+
+ (core module $M
+ (import "" "cget" (func $cget (result i32)))
+ (func (export "f'") (param i32) (result i32)
+ (i32.add (local.get 0) (call $cget))
+ )
+ )
+
+ (core instance $m
+ (instantiate $M
+ (with "" (instance (export "cget" (func $cget))))
+ )
+ )
+
+ (func (export "f") (param "x" u32) (result u32)
+ (canon lift (core func $m "f'"))
+ )
+ )
+
+ (component $B
+ (import "f" (func $f (param "x" u32) (result u32)))
+
+ (core func $f' (canon lower (func $f)))
+
+ (core module $N
+ (import "" "f'" (func $f' (param i32) (result i32)))
+ (func (export "g'") (result i32)
+ (call $f' (i32.const 1234))
+ )
+ )
+
+ (core instance $n
+ (instantiate $N
+ (with "" (instance (export "f'" (func $f'))))
+ )
+ )
+
+ (func (export "g") (result u32)
+ (canon lift (core func $n "g'"))
+ )
+ )
+
+ (instance $a (instantiate $A))
+ (instance $b
+ (instantiate $B
+ (with "f" (func $a "f"))
+ )
+ )
+
+ (export "g" (func $b "g"))
+)
+;; function u1:0(i64 vmctx, i64) -> i32 tail {
+;; ss0 = explicit_slot 24, align = 8
+;; region0 = 123 ""
+;; region1 = 160 ""
+;; region2 = 25 ""
+;; region3 = 44 ""
+;; region4 = 78 ""
+;; region5 = 68 ""
+;; region6 = 198 ""
+;; region7 = 100 ""
+;; region8 = 254 ""
+;; region9 = 112 ""
+;; region10 = 176 ""
+;; region11 = 149 ""
+;; region12 = 224 ""
+;; region13 = 227 ""
+;; gv0 = vmctx
+;; gv1 = load.i64 notrap aligned readonly can_move region0 gv0+8
+;; gv2 = load.i64 notrap aligned region1 gv1+24
+;; gv3 = vmctx
+;; gv4 = load.i64 notrap aligned readonly can_move region0 gv3+8
+;; gv5 = load.i64 notrap aligned region1 gv4+24
+;; gv6 = vmctx
+;; gv7 = load.i64 notrap aligned readonly can_move region0 gv6+8
+;; gv8 = load.i64 notrap aligned region1 gv7+24
+;; sig0 = (i64 vmctx, i64, i32) -> i32 tail
+;; sig1 = (i64 vmctx, i64) tail
+;; sig2 = (i64 vmctx, i64, i32, i32) tail
+;; sig3 = (i64 vmctx, i64, i32) -> i32 tail
+;; sig4 = (i64 vmctx, i64) -> i32 tail
+;; fn0 = colocated u2:0 sig0
+;; fn1 = colocated u0:0 sig3
+;; fn2 = colocated u2147483648:17 sig4
+;; stack_limit = gv2
+;;
+;; block0(v0: i64, v1: i64):
+;; @0123 jump block2
+;;
+;; block2:
+;; jump block6
+;;
+;; block6:
+;; @0123 v3 = load.i64 notrap aligned readonly can_move region2 v0+72
+;; v9 = load.i64 notrap aligned readonly can_move region3 v3+232
+;; v10 = load.i32 notrap aligned region4 v9
+;; trapz v10, user26
+;; jump block9
+;;
+;; block9:
+;; v15 = load.i64 notrap aligned readonly can_move region0 v3+8
+;; v16 = load.i64 notrap aligned region5 v15+136
+;; v14 = stack_addr.i64 ss0
+;; store notrap aligned region6 v16, v14
+;; v8 = iconst.i32 0
+;; store notrap aligned region7 v8, v14+8 ; v8 = 0
+;; v12 = iconst.i32 1
+;; store notrap aligned region8 v12, v14+12 ; v12 = 1
+;; v17 = load.i32 notrap aligned region9 v15+128
+;; store notrap aligned region10 v17, v14+16
+;; store notrap aligned region9 v8, v15+128 ; v8 = 0
+;; v19 = load.i32 notrap aligned region11 v15+132
+;; store notrap aligned region12 v19, v14+20
+;; store notrap aligned region11 v8, v15+132 ; v8 = 0
+;; store notrap aligned region5 v14, v15+136
+;; v21 = load.i64 notrap aligned readonly can_move region3 v3+208
+;; v22 = load.i32 notrap aligned region13 v21
+;; jump block16
+;;
+;; block16:
+;; v26 = load.i64 notrap aligned readonly can_move region2 v3+72
+;; v28 = load.i64 notrap aligned readonly can_move region0 v26+8
+;; v29 = load.i32 notrap aligned region9 v28+128
+;; jump block17
+;;
+;; block17:
+;; jump block11
+;;
+;; block11:
+;; jump block12
+;;
+;; block12:
+;; store.i64 notrap aligned region5 v16, v15+136
+;; store.i32 notrap aligned region9 v17, v15+128
+;; store.i32 notrap aligned region11 v19, v15+132
+;; jump block14
+;;
+;; block14:
+;; store.i32 notrap aligned region4 v10, v9
+;; jump block7
+;;
+;; block7:
+;; jump block4
+;;
+;; block4:
+;; jump block3
+;;
+;; block3:
+;; jump block18
+;;
+;; block18:
+;; @0125 jump block1
+;;
+;; block1:
+;; @0120 v2 = iconst.i32 1234
+;; v46 = iadd.i32 v29, v2 ; v2 = 1234
+;; @0125 return v46
+;; }
diff --git a/tests/misc_testsuite/component-model/thread-transparency/blocks.wast b/tests/misc_testsuite/component-model/thread-transparency/blocks.wast
new file mode 100644
index 0000000000..fc2fa94d7c
--- /dev/null
+++ b/tests/misc_testsuite/component-model/thread-transparency/blocks.wast
@@ -0,0 +1,69 @@
+;;! component_model_async = true
+
+;; $Inner's synchronously-lifted `async`-typed export really blocks, on a
+;; `waitable-set.wait` over a fresh, empty waitable set. If the
+;; thread-transparent $Outer -> $Mid adapter left `task_may_block` set, this
+;; synchronous call chain would suspend instead of trapping.
+
+(component definition $Tester
+ (component $Inner
+ (core module $Memory (memory (export "mem") 1))
+ (core instance $memory (instantiate $Memory))
+ (core module $M
+ (import "" "waitable-set.new" (func $ws-new (result i32)))
+ (import "" "waitable-set.wait" (func $ws-wait (param i32 i32) (result i32)))
+ (func (export "g")
+ (drop (call $ws-wait (call $ws-new) (i32.const 0)))
+ unreachable)
+ )
+ (core func $ws-new (canon waitable-set.new))
+ (core func $ws-wait
+ (canon waitable-set.wait (memory (core memory $memory "mem"))))
+ (core instance $m (instantiate $M
+ (with "" (instance
+ (export "waitable-set.new" (func $ws-new))
+ (export "waitable-set.wait" (func $ws-wait))))))
+ (func (export "g") async (canon lift (core func $m "g")))
+ )
+
+ (component $Mid
+ (import "inner" (instance $inner (export "g" (func async))))
+ (core module $M
+ (import "" "g" (func $g))
+ (func (export "f") (call $g))
+ )
+ (canon lower (func $inner "g") (core func $g))
+ (core instance $m (instantiate $M
+ (with "" (instance (export "g" (func $g))))))
+ (func (export "f") (canon lift (core func $m "f")))
+ )
+
+ (component $Outer
+ (import "mid" (instance $mid (export "f" (func))))
+ (core module $M
+ (import "" "task.return" (func $task-return))
+ (import "" "f" (func $f))
+ (func (export "run") (result i32)
+ (call $f)
+ (call $task-return)
+ (i32.const 0 (; EXIT ;)))
+ (func (export "cb") (param i32 i32 i32) (result i32) unreachable)
+ )
+ (canon task.return (core func $task-return))
+ (canon lower (func $mid "f") (core func $f))
+ (core instance $m (instantiate $M
+ (with "" (instance
+ (export "task.return" (func $task-return))
+ (export "f" (func $f))))))
+ (func (export "run") async
+ (canon lift (core func $m "run") async (callback (core func $m "cb"))))
+ )
+
+ (instance $inner (instantiate $Inner))
+ (instance $mid (instantiate $Mid (with "inner" (instance $inner))))
+ (instance $outer (instantiate $Outer (with "mid" (instance $mid))))
+ (func (export "run") (alias export $outer "run"))
+)
+
+(component instance $i $Tester)
+(assert_trap (invoke "run") "cannot block a synchronous task before returning")
diff --git a/tests/misc_testsuite/component-model/thread-transparency/reentrancy.wast b/tests/misc_testsuite/component-model/thread-transparency/reentrancy.wast
new file mode 100644
index 0000000000..9db5a41bbf
--- /dev/null
+++ b/tests/misc_testsuite/component-model/thread-transparency/reentrancy.wast
@@ -0,0 +1,114 @@
+;;! component_model_async = true
+;;! component_model_more_async_builtins = true
+;;! component_model_async_stackful = true
+;;! component_model_threading = true
+
+;; Sync-to-sync adapters between sibling instances have no dynamic `may_enter`
+;; check, so a guest that cares about reentrance has to guard against it itself.
+;; This exercises the case where it actually happens:
+;;
+;; host --async-lift--> $Outer --adapter--> $Mid --async-typed--> $Inner
+;;
+;; $Inner blocks on a fresh, empty waitable set. Blocking in a sync-typed call
+;; no longer traps outright: the scheduler first looks for another eligible
+;; thread to run, and $Outer has started a second one. That second thread calls
+;; $Mid.f while the first call's frame is still live, reentrance now being
+;; permitted, so $Mid's own `$inside` guard is what fires.
+;;
+;; This is not an artifact of dropping the `{enter,exit}-sync-call` window: the
+;; $Outer -> $Mid adapter is opaque here anyway, because $Mid lowers an
+;; `async`-typed lift.
+
+(component definition $Tester
+ (component $Inner
+ (core module $Memory (memory (export "mem") 1))
+ (core instance $memory (instantiate $Memory))
+ (core module $M
+ (import "" "waitable-set.new" (func $ws-new (result i32)))
+ (import "" "waitable-set.wait" (func $ws-wait (param i32 i32) (result i32)))
+ (func (export "g")
+ (drop (call $ws-wait (call $ws-new) (i32.const 0)))
+ unreachable)
+ )
+ (core func $ws-new (canon waitable-set.new))
+ (core func $ws-wait
+ (canon waitable-set.wait (memory (core memory $memory "mem"))))
+ (core instance $m (instantiate $M
+ (with "" (instance
+ (export "waitable-set.new" (func $ws-new))
+ (export "waitable-set.wait" (func $ws-wait))))))
+ (func (export "g") async (canon lift (core func $m "g")))
+ )
+
+ ;; $Mid traps if it is ever re-entered while a previous call is still in
+ ;; progress.
+ (component $Mid
+ (import "inner" (instance $inner (export "g" (func async))))
+ (core module $M
+ (import "" "g" (func $g))
+ (global $inside (mut i32) (i32.const 0))
+ (func (export "f")
+ (if (global.get $inside)
+ (then unreachable (; REENTERED ;)))
+ (global.set $inside (i32.const 1))
+ (call $g)
+ (global.set $inside (i32.const 0)))
+ )
+ (canon lower (func $inner "g") (core func $g))
+ (core instance $m (instantiate $M
+ (with "" (instance (export "g" (func $g))))))
+ (func (export "f") (canon lift (core func $m "f")))
+ )
+
+ (component $Outer
+ (import "mid" (instance $mid (export "f" (func))))
+ (core module $Table
+ (table (export "__indirect_function_table") 1 funcref))
+ (core instance $table (instantiate $Table))
+ (core module $M
+ (import "" "task.return" (func $task-return))
+ (import "" "f" (func $f))
+ (import "" "thread.new-indirect" (func $thread-new (param i32 i32) (result i32)))
+ (import "" "thread.resume-later" (func $resume-later (param i32)))
+ (import "" "__indirect_function_table" (table $tbl 1 funcref))
+
+ ;; Runs on the second thread, once the first one has blocked.
+ (func $second (param i32)
+ (call $f))
+ (elem (table $tbl) (i32.const 0) func $second)
+
+ (func (export "run") (result i32)
+ (call $resume-later (call $thread-new (i32.const 0) (i32.const 0)))
+ (call $f)
+ (call $task-return)
+ (i32.const 0 (; EXIT ;)))
+ (func (export "cb") (param i32 i32 i32) (result i32) unreachable)
+ )
+ (core type $start-func-ty (func (param i32)))
+ (alias core export $table "__indirect_function_table"
+ (core table $indirect-function-table))
+ (core func $thread-new
+ (canon thread.new-indirect $start-func-ty
+ (core table $indirect-function-table)))
+ (core func $resume-later (canon thread.resume-later))
+ (canon task.return (core func $task-return))
+ (canon lower (func $mid "f") (core func $f))
+ (core instance $m (instantiate $M
+ (with "" (instance
+ (export "task.return" (func $task-return))
+ (export "f" (func $f))
+ (export "thread.new-indirect" (func $thread-new))
+ (export "thread.resume-later" (func $resume-later))
+ (export "__indirect_function_table" (table $indirect-function-table))))))
+ (func (export "run") async
+ (canon lift (core func $m "run") async (callback (core func $m "cb"))))
+ )
+
+ (instance $inner (instantiate $Inner))
+ (instance $mid (instantiate $Mid (with "inner" (instance $inner))))
+ (instance $outer (instantiate $Outer (with "mid" (instance $mid))))
+ (func (export "run") (alias export $outer "run"))
+)
+
+(component instance $i $Tester)
+(assert_trap (invoke "run") "wasm `unreachable` instruction executed")