Skip to content

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

Assert engines match in `StructType::with_finality_and_supertype` (#15078)

Alex Crichton37b3c724ff6c
diff --git a/crates/wasmtime/src/runtime/types.rs b/crates/wasmtime/src/runtime/types.rs
index 807460989b..cbf296289b 100644
--- a/crates/wasmtime/src/runtime/types.rs
+++ b/crates/wasmtime/src/runtime/types.rs
@@ -1938,7 +1938,8 @@ impl StructType {
///
/// # Panics
///
- /// Panics if any given field type is not associated with the given engine.
+ /// Panics if any given field type, or the supertype, is not associated
+ /// with the given engine.
pub fn with_finality_and_supertype(
engine: &Engine,
finality: Finality,
@@ -1978,6 +1979,7 @@ impl StructType {
.try_collect()?;
if let Some(supertype) = supertype {
+ assert!(supertype.comes_from_same_engine(engine));
ensure!(
supertype.finality().is_non_final(),
"cannot create a subtype of a final supertype"
@@ -2215,7 +2217,8 @@ impl ArrayType {
///
/// # Panics
///
- /// Panics if the given field type is not associated with the given engine.
+ /// Panics if the given field type, or the supertype, is not associated
+ /// with the given engine.
pub fn with_finality_and_supertype(
engine: &Engine,
finality: Finality,
@@ -2494,8 +2497,8 @@ impl FuncType {
///
/// # Panics
///
- /// Panics if any parameter or value type is not associated with the given
- /// engine.
+ /// Panics if any parameter or value type, or the supertype, is not
+ /// associated with the given engine.
pub fn with_finality_and_supertype(
engine: &Engine,
finality: Finality,
diff --git a/tests/all/cross_engine.rs b/tests/all/cross_engine.rs
index 4b90b345fc..325f697523 100644
--- a/tests/all/cross_engine.rs
+++ b/tests/all/cross_engine.rs
@@ -425,3 +425,32 @@ fn exn_ref_pre_rejects_foreign_type() {
let _ = ExnRefPre::new(&mut store, foreign_ty);
}
+
+#[test]
+#[should_panic = "supertype.comes_from_same_engine(engine)"]
+fn struct_type_rejects_foreign_supertype() {
+ let (a, b) = engine_pair();
+ let i32_field = || FieldType::new(Mutability::Const, StorageType::ValType(ValType::I32));
+ let i64_field = || FieldType::new(Mutability::Var, StorageType::ValType(ValType::I64));
+
+ // Give engine A an unrelated type at the same index as the foreign
+ // supertype, so that interpreting the foreign index in engine A's registry
+ // would find this type.
+ let _local_ty = StructType::with_finality_and_supertype(
+ &a,
+ Finality::NonFinal,
+ None,
+ [i64_field(), i64_field(), i64_field()],
+ )
+ .unwrap();
+ let foreign_supertype =
+ StructType::with_finality_and_supertype(&b, Finality::NonFinal, None, [i32_field()])
+ .unwrap();
+
+ let _ = StructType::with_finality_and_supertype(
+ &a,
+ Finality::Final,
+ Some(&foreign_supertype),
+ [i32_field()],
+ );
+}