Skip to content

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

Check for cross-engine usage in more locations (#15223)

Alex Crichton97e6c1ec8fc2
diff --git a/crates/wasmtime/src/runtime/component/linker.rs b/crates/wasmtime/src/runtime/component/linker.rs
index 38de9cb5d5..96a351a8f5 100644
--- a/crates/wasmtime/src/runtime/component/linker.rs
+++ b/crates/wasmtime/src/runtime/component/linker.rs
@@ -197,7 +197,14 @@ impl<T: 'static> Linker<T> {
/// This function will return an [`OutOfMemory`][crate::OutOfMemory] error when
/// memory allocation fails. See the `OutOfMemory` type's documentation for
/// details on Wasmtime's out-of-memory handling.
+ ///
+ /// Returns an error if `component` was not compiled by the same
+ /// [`Engine`](crate::Engine) as this linker.
pub fn substituted_component_type(&self, component: &Component) -> Result<types::Component> {
+ ensure!(
+ Engine::same(&self.engine, component.engine()),
+ "cross-`Engine` usage is not supported"
+ );
let cx = self.typecheck(&component)?;
Ok(types::Component::from(
component.ty(),
@@ -992,7 +999,14 @@ impl<T: 'static> LinkerInstance<'_, T> {
/// This function will return an [`OutOfMemory`][crate::OutOfMemory] error when
/// memory allocation fails. See the `OutOfMemory` type's documentation for
/// details on Wasmtime's out-of-memory handling.
+ ///
+ /// Returns an error if `module` was not compiled by the same
+ /// [`Engine`](crate::Engine) as this linker.
pub fn module(&mut self, name: &str, module: &Module) -> Result<()> {
+ ensure!(
+ Engine::same(self.engine, module.engine()),
+ "cross-`Engine` usage is not supported"
+ );
self.insert(name, Definition::Module(module.clone()))?;
Ok(())
}
diff --git a/crates/wasmtime/src/runtime/component/types.rs b/crates/wasmtime/src/runtime/component/types.rs
index 6a3dc5fa26..76ebc450b5 100644
--- a/crates/wasmtime/src/runtime/component/types.rs
+++ b/crates/wasmtime/src/runtime/component/types.rs
@@ -1173,6 +1173,12 @@ impl Module {
}
/// Iterates over imports of the module
+ ///
+ /// # Panics
+ ///
+ /// The `engine` provided must be the [`Engine`] that this type's component
+ /// was compiled with. This method may panic, or return types that don't
+ /// describe this component, if it's a different [`Engine`].
pub fn imports<'a>(
&'a self,
engine: &'a Engine,
@@ -1189,6 +1195,12 @@ impl Module {
}
/// Iterates over exports of the module
+ ///
+ /// # Panics
+ ///
+ /// The `engine` provided must be the [`Engine`] that this type's component
+ /// was compiled with. This method may panic, or return types that don't
+ /// describe this component, if it's a different [`Engine`].
pub fn exports<'a>(
&'a self,
engine: &'a Engine,
@@ -1212,6 +1224,12 @@ impl Component {
}
/// Returns import associated with `name`, if such exists in the component
+ ///
+ /// # Panics
+ ///
+ /// The `engine` provided must be the [`Engine`] that this type's component
+ /// was compiled with. This method may panic, or return types that don't
+ /// describe this component, if it's a different [`Engine`].
pub fn get_import<'a>(&'a self, engine: &'a Engine, name: &str) -> Option<ComponentExtern<'a>> {
self.0.types[self.0.index]
.imports
@@ -1220,6 +1238,12 @@ impl Component {
}
/// Iterates over imports of the component
+ ///
+ /// # Panics
+ ///
+ /// The `engine` provided must be the [`Engine`] that this type's component
+ /// was compiled with. This method may panic, or return types that don't
+ /// describe this component, if it's a different [`Engine`].
pub fn imports<'a>(
&'a self,
engine: &'a Engine,
@@ -1233,6 +1257,12 @@ impl Component {
}
/// Returns export associated with `name`, if such exists in the component
+ ///
+ /// # Panics
+ ///
+ /// The `engine` provided must be the [`Engine`] that this type's component
+ /// was compiled with. This method may panic, or return types that don't
+ /// describe this component, if it's a different [`Engine`].
pub fn get_export<'a>(&'a self, engine: &'a Engine, name: &str) -> Option<ComponentExtern<'a>> {
self.0.types[self.0.index]
.exports
@@ -1241,6 +1271,12 @@ impl Component {
}
/// Iterates over exports of the component
+ ///
+ /// # Panics
+ ///
+ /// The `engine` provided must be the [`Engine`] that this type's component
+ /// was compiled with. This method may panic, or return types that don't
+ /// describe this component, if it's a different [`Engine`].
pub fn exports<'a>(
&'a self,
engine: &'a Engine,
@@ -1272,6 +1308,12 @@ impl ComponentInstance {
}
/// Returns export associated with `name`, if such exists in the component instance
+ ///
+ /// # Panics
+ ///
+ /// The `engine` provided must be the [`Engine`] that this type's component
+ /// was compiled with. This method may panic, or return types that don't
+ /// describe this component, if it's a different [`Engine`].
pub fn get_export<'a>(&'a self, engine: &'a Engine, name: &str) -> Option<ComponentExtern<'a>> {
self.0.types[self.0.index]
.exports
@@ -1280,6 +1322,12 @@ impl ComponentInstance {
}
/// Iterates over exports of the component instance
+ ///
+ /// # Panics
+ ///
+ /// The `engine` provided must be the [`Engine`] that this type's component
+ /// was compiled with. This method may panic, or return types that don't
+ /// describe this component, if it's a different [`Engine`].
pub fn exports<'a>(
&'a self,
engine: &'a Engine,
diff --git a/tests/all/cross_engine.rs b/tests/all/cross_engine.rs
index 325f697523..64934fa067 100644
--- a/tests/all/cross_engine.rs
+++ b/tests/all/cross_engine.rs
@@ -274,6 +274,37 @@ fn component_linker_instantiate_pre_rejects_foreign_component() -> Result<()> {
Ok(())
}
+#[test]
+#[cfg_attr(miri, ignore)]
+fn component_linker_module_rejects_foreign_module() -> Result<()> {
+ let (a, b) = engine_pair();
+ let foreign = Module::new(&b, "(module (func (export \"f\") (param i64)))")?;
+
+ assert!(
+ ComponentLinker::<()>::new(&a)
+ .root()
+ .module("m", &foreign)
+ .is_err()
+ );
+
+ Ok(())
+}
+
+#[test]
+#[cfg_attr(miri, ignore)]
+fn component_linker_substituted_component_type_rejects_foreign_component() -> Result<()> {
+ let (a, b) = engine_pair();
+ let foreign = Component::new(&b, COLLIDING_COMPONENT)?;
+
+ assert!(
+ ComponentLinker::<()>::new(&a)
+ .substituted_component_type(&foreign)
+ .is_err()
+ );
+
+ Ok(())
+}
+
#[test]
#[cfg_attr(miri, ignore)]
fn component_instance_pre_instantiate_rejects_foreign_store() -> Result<()> {