Skip to content

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

Don't panic on invalid adapter modules (#14846)

Alex Crichton40025dd9f876
diff --git a/crates/environ/src/component/translate.rs b/crates/environ/src/component/translate.rs
index 80c45387f4..3d8b194ca9 100644
--- a/crates/environ/src/component/translate.rs
+++ b/crates/environ/src/component/translate.rs
@@ -542,7 +542,7 @@ impl<'a, 'data> Translator<'a, 'data> {
// `{enter,exit}-sync-call` calls.
component.transparent_adapters = transparent_adapters(&component, self.types.types());
- self.partition_adapter_modules(&mut component);
+ self.partition_adapter_modules(&mut component)?;
analyze_same_vmctx_imports(&component, &mut self.static_modules);
diff --git a/crates/environ/src/component/translate/adapt.rs b/crates/environ/src/component/translate/adapt.rs
index f775e5483c..1f14152e40 100644
--- a/crates/environ/src/component/translate/adapt.rs
+++ b/crates/environ/src/component/translate/adapt.rs
@@ -185,7 +185,10 @@ impl<'data> Translator<'_, 'data> {
///
/// This will mutate the provided `component` in-place and fill out the dfg
/// metadata for adapter modules.
- pub(super) fn partition_adapter_modules(&mut self, component: &mut dfg::ComponentDfg) {
+ pub(super) fn partition_adapter_modules(
+ &mut self,
+ component: &mut dfg::ComponentDfg,
+ ) -> Result<()> {
// Visit each adapter, in order of its original definition, during the
// partitioning. This allows for the guarantee that dependencies are
// visited in a topological fashion ideally.
@@ -227,10 +230,7 @@ impl<'data> Translator<'_, 'data> {
}
// With the wasm binary this is then pushed through general
- // translation, validation, etc. Note that multi-memory is
- // specifically enabled here since the adapter module is highly
- // likely to use that if anything is actually indirected through
- // memory.
+ // translation, validation, etc.
self.validator.reset();
let static_module_index = self.static_modules.next_key();
let translation = ModuleEnvironment::new(
@@ -240,7 +240,7 @@ impl<'data> Translator<'_, 'data> {
static_module_index,
)
.translate(Parser::new(0), wasm)
- .expect("invalid adapter module generated");
+ .context("failed to validate a fused adapter module")?;
// Record, for each adapter in this adapter module, the module that
// the adapter was placed within as well as the function index of
@@ -269,6 +269,8 @@ impl<'data> Translator<'_, 'data> {
let id = component.adapter_modules.push((static_module_index, args));
assert_eq!(id, module_id);
}
+
+ Ok(())
}
}
diff --git a/tests/misc_testsuite/component-model/adapter-features-disabled.wast b/tests/misc_testsuite/component-model/adapter-features-disabled.wast
new file mode 100644
index 0000000000..31be83c772
--- /dev/null
+++ b/tests/misc_testsuite/component-model/adapter-features-disabled.wast
@@ -0,0 +1,29 @@
+;;! multi_memory = false
+
+;; A string passed between two components needs an adapter that uses both
+;; components' memories, which fails to validate without multi-memory.
+(assert_invalid
+ (component
+ (component $A
+ (core module $m
+ (memory (export "memory") 1)
+ (func (export "realloc") (param i32 i32 i32 i32) (result i32) i32.const 16)
+ (func (export "f") (param i32 i32) (result i32) local.get 1))
+ (core instance $i (instantiate $m))
+ (func (export "f") (param "s" string) (result u32)
+ (canon lift (core func $i "f") (memory (core memory $i "memory"))
+ (realloc (core func $i "realloc")))))
+ (component $B
+ (import "f" (func $f (param "s" string) (result u32)))
+ (core module $libc (memory (export "memory") 1))
+ (core instance $libc (instantiate $libc))
+ (core func $f (canon lower (func $f) (memory (core memory $libc "memory"))))
+ (core module $m
+ (import "" "f" (func $f (param i32 i32) (result i32)))
+ (func (export "run") (result i32) (call $f (i32.const 0) (i32.const 3))))
+ (core instance $i (instantiate $m (with "" (instance (export "f" (func $f))))))
+ (func (export "run") (result u32) (canon lift (core func $i "run"))))
+ (instance $a (instantiate $A))
+ (instance $b (instantiate $B (with "f" (func $a "f"))))
+ (export "run" (func $b "run")))
+ "multiple memories")