A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Preserve resource aliases when stubbing unknown imports (#14396)
Tomasz Andrzejak10becb09f953
diff --git a/crates/wasmtime/src/runtime/component/linker.rs b/crates/wasmtime/src/runtime/component/linker.rsindex cb41df2032..77ef8612fe 100644--- a/crates/wasmtime/src/runtime/component/linker.rs+++ b/crates/wasmtime/src/runtime/component/linker.rs@@ -355,16 +355,29 @@ impl<T: 'static> Linker<T> {/// memory allocation fails. See the `OutOfMemory` type's documentation for/// details on Wasmtime's out-of-memory handling.pub fn define_unknown_imports_as_traps(&mut self, component: &Component) -> Result<()> {- use wasmtime_environ::component::ComponentTypes;- use wasmtime_environ::component::TypeDef;// Recursively stub out all imports of the component with a function that traps.+ use wasmtime_environ::component::{ComponentTypes, ResourceIndex, TypeDef};+fn stub_item<T>(linker: &mut LinkerInstance<T>,+ resources: &mut TryEntitySet<ResourceIndex>,item_name: &str,item_def: &TypeDef,parent_instance: Option<&str>,types: &ComponentTypes,) -> Result<()> {+ // The first definition of a resource needs to be present, but all other future+ // references to the same resource are aliases of the original resource definition.+ // Once a resource is visited here all future hits on the same resource shouldn't+ // do anything else effectively. If item_name is already defined then we'll bail+ // out in the below matches!, and otherwise a stub will be inserted, so no matter+ // what the first resource is defined and all others will refer to that.+ if let TypeDef::Resource(ty) = item_def+ && !resources.insert(types[*ty].unwrap_concrete_ty())?+ {+ return Ok(());+ }+// Skip if the item isn't an instance and has already been defined in the linker.if !matches!(item_def, TypeDef::ComponentInstance(_)) && linker.get(item_name).is_some(){@@ -421,6 +434,7 @@ impl<T: 'static> Linker<T> {for (export_name, export) in instance.exports.iter() {stub_item(&mut linker_instance,+ resources,export_name,&export.ty,Some(item_name),@@ -440,9 +454,12 @@ impl<T: 'static> Linker<T> {Ok(())}+ let mut resources = TryEntitySet::new();+for (_, (import_name, import_type)) in &component.env_component().import_types {stub_item(&mut self.root(),+ &mut resources,import_name,&import_type.ty,None,diff --git a/tests/all/component_model/linker.rs b/tests/all/component_model/linker.rsindex 826baa8b9d..66c82358ec 100644--- a/tests/all/component_model/linker.rs+++ b/tests/all/component_model/linker.rs@@ -167,6 +167,47 @@ fn linker_defines_unknown_imports_as_traps() -> Result<()> {Ok(())}+/// Resource aliases must follow the real resource regardless of registration order.+#[test]+fn linker_defines_unknown_imports_as_traps_with_resource_aliases() -> Result<()> {+ let engine = Engine::default();+ let component = Component::new(+ &engine,+ r#"(component+ (import "types" (instance $types+ (export "r" (type (sub resource)))+ ))+ (alias export $types "r" (type $r))+ (import "uses" (instance+ (alias outer 1 $r (type $r))+ (export "r" (type (eq $r)))+ ))+ )"#,+ )?;++ for stub_first in [false, true] {+ let mut linker = Linker::<()>::new(&engine);+ linker.allow_shadowing(stub_first);++ if stub_first {+ linker.define_unknown_imports_as_traps(&component)?;+ }++ linker+ .instance("types")?+ .resource("r", ResourceType::host::<u32>(), |_, _| Ok(()))?;++ if !stub_first {+ linker.define_unknown_imports_as_traps(&component)?;+ }++ let mut store = Store::new(&engine, ());+ linker.instantiate(&mut store, &component)?;+ }++ Ok(())+}+#[tokio::test]async fn linker_defines_unknown_async_imports_as_traps() -> Result<()> {// `define_unknown_imports_as_traps` used to always stub with `func_new`,