Skip to content

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

Implement `TryClone` for `WasmSubType`; use it in the `TypeRegistry` (#12527)

Nick Fitzgeraldcbbe5551e56c
diff --git a/crates/cranelift/src/func_environ.rs b/crates/cranelift/src/func_environ.rs
index 6be31d3a67..5bb1698096 100644
--- a/crates/cranelift/src/func_environ.rs
+++ b/crates/cranelift/src/func_environ.rs
@@ -3962,22 +3962,16 @@ impl FuncEnvironment<'_> {
pub fn continuation_arguments(&self, index: TypeIndex) -> &[WasmValType] {
let idx = self.module.types[index].unwrap_module_type_index();
- self.types[self.types[idx]
- .unwrap_cont()
- .clone()
- .unwrap_module_type_index()]
- .unwrap_func()
- .params()
+ self.types[self.types[idx].unwrap_cont().unwrap_module_type_index()]
+ .unwrap_func()
+ .params()
}
pub fn continuation_returns(&self, index: TypeIndex) -> &[WasmValType] {
let idx = self.module.types[index].unwrap_module_type_index();
- self.types[self.types[idx]
- .unwrap_cont()
- .clone()
- .unwrap_module_type_index()]
- .unwrap_func()
- .returns()
+ self.types[self.types[idx].unwrap_cont().unwrap_module_type_index()]
+ .unwrap_func()
+ .returns()
}
pub fn tag_params(&self, tag_index: TagIndex) -> &[WasmValType] {
diff --git a/crates/environ/src/types.rs b/crates/environ/src/types.rs
index 24da68d079..461fe1c0a7 100644
--- a/crates/environ/src/types.rs
+++ b/crates/environ/src/types.rs
@@ -846,7 +846,7 @@ impl WasmFuncType {
}
/// WebAssembly continuation type -- equivalent of `wasmparser`'s ContType.
-#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
+#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct WasmContType(EngineOrModuleTypeIndex);
impl fmt::Display for WasmContType {
@@ -926,6 +926,15 @@ pub struct WasmExnType {
pub fields: Box<[WasmFieldType]>,
}
+impl TryClone for WasmExnType {
+ fn try_clone(&self) -> Result<Self, OutOfMemory> {
+ Ok(Self {
+ func_ty: self.func_ty,
+ fields: self.fields.try_clone()?,
+ })
+ }
+}
+
impl fmt::Display for WasmExnType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "(exn ({})", self.func_ty)?;
@@ -1018,7 +1027,7 @@ impl WasmStorageType {
}
/// The type of a struct field or array element.
-#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
+#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct WasmFieldType {
/// The field's element type.
pub element_type: WasmStorageType,
@@ -1027,6 +1036,12 @@ pub struct WasmFieldType {
pub mutable: bool,
}
+impl TryClone for WasmFieldType {
+ fn try_clone(&self) -> Result<Self, OutOfMemory> {
+ Ok(*self)
+ }
+}
+
impl fmt::Display for WasmFieldType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.mutable {
@@ -1054,7 +1069,7 @@ impl TypeTrace for WasmFieldType {
}
/// A concrete array type.
-#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
+#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct WasmArrayType(pub WasmFieldType);
impl fmt::Display for WasmArrayType {
@@ -1086,6 +1101,14 @@ pub struct WasmStructType {
pub fields: Box<[WasmFieldType]>,
}
+impl TryClone for WasmStructType {
+ fn try_clone(&self) -> Result<Self, OutOfMemory> {
+ Ok(Self {
+ fields: self.fields.try_clone()?,
+ })
+ }
+}
+
impl fmt::Display for WasmStructType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "(struct")?;
@@ -1128,6 +1151,15 @@ pub struct WasmCompositeType {
pub shared: bool,
}
+impl TryClone for WasmCompositeType {
+ fn try_clone(&self) -> Result<Self, OutOfMemory> {
+ Ok(Self {
+ inner: self.inner.try_clone()?,
+ shared: self.shared,
+ })
+ }
+}
+
impl fmt::Display for WasmCompositeType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.shared {
@@ -1152,6 +1184,18 @@ pub enum WasmCompositeInnerType {
Exn(WasmExnType),
}
+impl TryClone for WasmCompositeInnerType {
+ fn try_clone(&self) -> Result<Self, OutOfMemory> {
+ Ok(match self {
+ Self::Array(ty) => Self::Array(*ty),
+ Self::Func(ty) => Self::Func(ty.try_clone()?),
+ Self::Struct(ty) => Self::Struct(ty.try_clone()?),
+ Self::Cont(ty) => Self::Cont(*ty),
+ Self::Exn(ty) => Self::Exn(ty.try_clone()?),
+ })
+ }
+}
+
impl fmt::Display for WasmCompositeInnerType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
@@ -1299,6 +1343,16 @@ pub struct WasmSubType {
pub composite_type: WasmCompositeType,
}
+impl TryClone for WasmSubType {
+ fn try_clone(&self) -> Result<Self, OutOfMemory> {
+ Ok(Self {
+ is_final: self.is_final,
+ supertype: self.supertype,
+ composite_type: self.composite_type.try_clone()?,
+ })
+ }
+}
+
impl fmt::Display for WasmSubType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.is_final && self.supertype.is_none() {
diff --git a/crates/wasmtime/src/runtime/type_registry.rs b/crates/wasmtime/src/runtime/type_registry.rs
index 058fd75414..bed00a226e 100644
--- a/crates/wasmtime/src/runtime/type_registry.rs
+++ b/crates/wasmtime/src/runtime/type_registry.rs
@@ -761,15 +761,15 @@ impl TypeRegistryInner {
.zip(iter_entity_range(range.clone()))
.map(|(mut ty, module_index)| {
non_canon_types
- .push((module_index, ty.clone()))
+ .push((module_index, ty.try_clone()?))
.expect("reserved capacity");
ty.canonicalize_for_hash_consing(range.clone(), &mut |idx| {
debug_assert!(idx < range.clone().start);
map[idx]
});
- ty
+ Ok(ty)
})
- .try_collect()?,
+ .try_collect::<Box<[_]>, OutOfMemory>()?,
};
// Any references in the hash-consing key to types outside of this rec