Skip to content

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

Fix panics compiling module imports with `rec` groups (#15125)

Alex Crichton3cfc2d68da8e
diff --git a/crates/environ/src/component/types_builder.rs b/crates/environ/src/component/types_builder.rs
index 4833d38aee..16594f0e90 100644
--- a/crates/environ/src/component/types_builder.rs
+++ b/crates/environ/src/component/types_builder.rs
@@ -397,6 +397,13 @@ impl ComponentTypesBuilder {
) -> Result<TypeModuleIndex> {
assert_eq!(types.id(), self.module_types.validator_id());
let ty = &types[id];
+
+ // Intern all types found in this module as they could show up anywhere
+ // within imports/exports/etc.
+ for id in ty.types.iter() {
+ self.module_types_builder_mut().intern_type(types, *id)?;
+ }
+
let mut result = TypeModule::default();
for ((module, field), ty) in ty.imports.iter() {
result.imports.insert(
@@ -426,17 +433,9 @@ impl ComponentTypesBuilder {
.intern_type(types, *id)?
.into()
}),
- Table(ty) => {
- self.intern_ref_type(types, ty.element_type)?;
- EntityType::Table(self.convert_table_type(ty)?)
- }
+ Table(ty) => EntityType::Table(self.convert_table_type(ty)?),
Memory(ty) => EntityType::Memory((*ty).into()),
- Global(ty) => {
- if let wasmparser::ValType::Ref(r) = ty.content_type {
- self.intern_ref_type(types, r)?;
- }
- EntityType::Global(self.convert_global_type(ty)?)
- }
+ Global(ty) => EntityType::Global(self.convert_global_type(ty)?),
Tag(id) => {
let func = self.module_types_builder_mut().intern_type(types, *id)?;
let exc = self
@@ -451,15 +450,6 @@ impl ComponentTypesBuilder {
})
}
- /// Interns the concrete type, if any, referenced by `ty` so that it can
- /// subsequently be converted with this builder's `TypeConvert` impl.
- fn intern_ref_type(&mut self, types: TypesRef<'_>, ty: wasmparser::RefType) -> Result<()> {
- if let Some(id) = ty.type_index().and_then(|i| i.as_core_type_id()) {
- self.module_types_builder_mut().intern_type(types, id)?;
- }
- Ok(())
- }
-
/// Convert a wasmparser `ComponentDefinedTypeId` into Wasmtime's type representation.
pub fn defined_type(
&mut self,
diff --git a/tests/misc_testsuite/component-model/core-type-out-of-group-deps.wast b/tests/misc_testsuite/component-model/core-type-out-of-group-deps.wast
new file mode 100644
index 0000000000..8493e089a8
--- /dev/null
+++ b/tests/misc_testsuite/component-model/core-type-out-of-group-deps.wast
@@ -0,0 +1,28 @@
+;;! reference_types = true
+;;! function_references = true
+;;! gc = true
+
+;; A function type with a supertype.
+(component definition
+ (import "m" (core module
+ (type $super (sub (func)))
+ (type $sub (sub $super (func)))
+ (import "" "f" (func (type $sub))))))
+
+;; A function type referring to a struct type in another rec group.
+(component definition
+ (import "m" (core module
+ (type $s (struct))
+ (type $f (func (param (ref $s))))
+ (import "" "f" (func (type $f))))))
+
+;; A rec group whose members refer to types in two other rec groups, one of
+;; which itself depends on another rec group.
+(component definition
+ (import "m" (core module
+ (type $a (struct))
+ (type $b (sub (struct (field (ref $a)))))
+ (rec
+ (type $f (func (param (ref $b))))
+ (type (sub $b (struct (field (ref $a))))))
+ (import "" "f" (func (type $f))))))