Skip to content

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

Handle OOM in `wasmtime_environ::types::WasmFuncType` (#12670)

Nick Fitzgerald25e3bd12d436
diff --git a/crates/cranelift/src/compiler.rs b/crates/cranelift/src/compiler.rs
index 7ea49bbcba..cb0b22526e 100644
--- a/crates/cranelift/src/compiler.rs
+++ b/crates/cranelift/src/compiler.rs
@@ -465,7 +465,7 @@ impl wasmtime_environ::Compiler for Compiler {
// Return results from the array as native return values.
let results =
- self.load_values_from_array(wasm_func_ty.returns(), &mut builder, args_base, args_len);
+ self.load_values_from_array(wasm_func_ty.results(), &mut builder, args_base, args_len);
builder.ins().return_(&results);
builder.finalize();
@@ -1068,7 +1068,7 @@ impl Compiler {
// Compute the size of the values vector.
let value_size = mem::size_of::<u128>();
- let values_vec_len = cmp::max(ty.params().len(), ty.returns().len());
+ let values_vec_len = cmp::max(ty.params().len(), ty.results().len());
let values_vec_byte_size = u32::try_from(value_size * values_vec_len).unwrap();
let values_vec_len = u32::try_from(values_vec_len).unwrap();
@@ -1419,7 +1419,7 @@ impl Compiler {
builder.switch_to_block(normal_return);
self.store_values_to_array(
&mut builder,
- callee_sig.returns(),
+ callee_sig.results(),
&normal_return_values,
values_vec_ptr,
values_vec_len,
diff --git a/crates/cranelift/src/compiler/component.rs b/crates/cranelift/src/compiler/component.rs
index bba19ea5bf..c165868b00 100644
--- a/crates/cranelift/src/compiler/component.rs
+++ b/crates/cranelift/src/compiler/component.rs
@@ -10,8 +10,8 @@ use cranelift_codegen::isa::{CallConv, TargetIsa};
use cranelift_frontend::FunctionBuilder;
use wasmtime_environ::error::{Result, bail};
use wasmtime_environ::{
- Abi, BuiltinFunctionIndex, CompiledFunctionBody, EntityRef, FuncKey, HostCall, PtrSize,
- TrapSentinel, Tunables, WasmFuncType, WasmValType, component::*,
+ Abi, BuiltinFunctionIndex, CompiledFunctionBody, EntityRef, FuncKey, HostCall, PanicOnOom as _,
+ PtrSize, TrapSentinel, Tunables, WasmFuncType, WasmValType, component::*,
fact::PREPARE_CALL_FIXED_PARAMS,
};
@@ -177,7 +177,7 @@ impl<'a> TrampolineCompiler<'a> {
}
Trampoline::ResourceRep { instance, ty } => {
// Currently this only supports resources represented by `i32`
- assert_eq!(self.signature.returns()[0], WasmValType::I32);
+ assert_eq!(self.signature.results()[0], WasmValType::I32);
self.translate_libcall(
host::resource_rep32,
TrapSentinel::NegativeOne,
@@ -639,10 +639,7 @@ impl<'a> TrampolineCompiler<'a> {
Trampoline::SyncStartCall { callback } => {
let pointer_type = self.isa.pointer_type();
let (values_vec_ptr, len) = self.compiler.allocate_stack_array_and_spill_args(
- &WasmFuncType::new(
- Box::new([]),
- self.signature.returns().iter().copied().collect(),
- ),
+ &WasmFuncType::new([], self.signature.results().iter().copied()).panic_on_oom(),
&mut self.builder,
&[],
);
@@ -972,10 +969,8 @@ impl<'a> TrampolineCompiler<'a> {
// A mixture of the above two.
WasmArgs::InRegistersUpTo(n) => {
let (values_vec_ptr, len) = self.compiler.allocate_stack_array_and_spill_args(
- &WasmFuncType::new(
- self.signature.params().iter().skip(n).copied().collect(),
- Box::new([]),
- ),
+ &WasmFuncType::new(self.signature.params().iter().skip(n).copied(), [])
+ .panic_on_oom(),
&mut self.builder,
&wasm_params[n..],
);
@@ -1026,7 +1021,7 @@ impl<'a> TrampolineCompiler<'a> {
// accounts for the ABI of this function when storing results.
let result = self.builder.func.dfg.inst_results(call).get(0).copied();
let result_ty = result.map(|v| self.builder.func.dfg.value_type(v));
- let expected = self.signature.returns();
+ let expected = self.signature.results();
match host_result.into() {
HostResult::Sentinel(TrapSentinel::NegativeOne) => {
assert_eq!(expected.len(), 1);
@@ -1054,7 +1049,7 @@ impl<'a> TrampolineCompiler<'a> {
let len = len.or(val_raw_len).unwrap();
self.raise_if_host_trapped(result.unwrap());
let results = self.compiler.load_values_from_array(
- self.signature.returns(),
+ self.signature.results(),
&mut self.builder,
ptr,
len,
@@ -1671,9 +1666,10 @@ impl ComponentCompiler for Compiler {
symbol: &str,
) -> Result<CompiledFunctionBody> {
let wasm_func_ty = WasmFuncType::new(
- intrinsic.core_params().into(),
- intrinsic.core_results().into(),
- );
+ intrinsic.core_params().iter().copied(),
+ intrinsic.core_results().iter().copied(),
+ )
+ .panic_on_oom();
match abi {
// Fall through to the trampoline compiler.
diff --git a/crates/cranelift/src/func_environ.rs b/crates/cranelift/src/func_environ.rs
index 96bc9126c3..22849dbb16 100644
--- a/crates/cranelift/src/func_environ.rs
+++ b/crates/cranelift/src/func_environ.rs
@@ -2594,7 +2594,7 @@ impl FuncEnvironment<'_> {
pub fn sig_ref_result_needs_stack_map(&self, sig_ref: ir::SigRef, index: usize) -> bool {
let wasm_func_ty = self.sig_ref_to_ty[sig_ref].as_ref().unwrap();
- wasm_func_ty.returns()[index].is_vmgcref_type_and_not_i31()
+ wasm_func_ty.results()[index].is_vmgcref_type_and_not_i31()
}
pub fn translate_table_grow(
@@ -3999,7 +3999,7 @@ impl FuncEnvironment<'_> {
let idx = self.module.types[index].unwrap_module_type_index();
self.types[self.types[idx].unwrap_cont().unwrap_module_type_index()]
.unwrap_func()
- .returns()
+ .results()
}
pub fn tag_params(&self, tag_index: TagIndex) -> &[WasmValType] {
@@ -4013,7 +4013,7 @@ impl FuncEnvironment<'_> {
let idx = self.module.tags[tag_index].signature;
self.types[idx.unwrap_module_type_index()]
.unwrap_func()
- .returns()
+ .results()
}
pub fn use_blendv_for_relaxed_laneselect(&self, ty: Type) -> bool {
diff --git a/crates/cranelift/src/lib.rs b/crates/cranelift/src/lib.rs
index 75a1687520..8c0a2da908 100644
--- a/crates/cranelift/src/lib.rs
+++ b/crates/cranelift/src/lib.rs
@@ -203,7 +203,7 @@ fn wasm_call_signature(
let mut sig = blank_sig(isa, call_conv);
let cvt = |ty: &WasmValType| ir::AbiParam::new(value_type(isa, *ty));
sig.params.extend(wasm_func_ty.params().iter().map(&cvt));
- sig.returns.extend(wasm_func_ty.returns().iter().map(&cvt));
+ sig.returns.extend(wasm_func_ty.results().iter().map(&cvt));
sig
}
diff --git a/crates/environ/src/collections.rs b/crates/environ/src/collections.rs
index f9a9a39747..69fc7f4517 100644
--- a/crates/environ/src/collections.rs
+++ b/crates/environ/src/collections.rs
@@ -14,7 +14,10 @@ pub use index_map::IndexMap;
pub use primary_map::PrimaryMap;
pub use secondary_map::SecondaryMap;
pub use wasmtime_core::{
- alloc::{String, TryClone, TryCollect, TryExtend, TryFromIterator, TryNew, Vec, try_new},
+ alloc::{
+ String, TryClone, TryCollect, TryCow, TryExtend, TryFromIterator, TryNew, TryToOwned, Vec,
+ try_new,
+ },
vec,
};
diff --git a/crates/environ/src/compile/module_types.rs b/crates/environ/src/compile/module_types.rs
index ed9e789431..2ecfcd0e88 100644
--- a/crates/environ/src/compile/module_types.rs
+++ b/crates/environ/src/compile/module_types.rs
@@ -1,16 +1,16 @@
use crate::{
EngineOrModuleTypeIndex, EntityRef, ModuleInternedRecGroupIndex, ModuleInternedTypeIndex,
- ModuleTypes, TypeConvert, TypeIndex, WasmArrayType, WasmCompositeInnerType, WasmCompositeType,
- WasmExnType, WasmFieldType, WasmFuncType, WasmHeapType, WasmResult, WasmStorageType,
- WasmStructType, WasmSubType, wasm_unsupported,
+ ModuleTypes, PanicOnOom as _, TypeConvert, TypeIndex, WasmArrayType, WasmCompositeInnerType,
+ WasmCompositeType, WasmExnType, WasmFieldType, WasmFuncType, WasmHeapType, WasmResult,
+ WasmStorageType, WasmStructType, WasmSubType,
+ collections::{TryClone as _, TryCow},
+ wasm_unsupported,
};
use std::{
- borrow::Cow,
collections::{HashMap, hash_map::Entry},
ops::Index,
};
use wasmparser::{UnpackedIndex, Validator, ValidatorId};
-use wasmtime_core::alloc::PanicOnOom as _;
/// A type marking the start of a recursion group's definition.
///
@@ -166,19 +166,20 @@ impl ModuleTypesBuilder {
// type. We can reuse the definition and its index, but still
// need to intern the type into our `trampoline_types` map so we
// can reuse it in the future.
- Cow::Borrowed(f) => {
- self.trampoline_types.insert(f.clone(), for_func_ty);
+ TryCow::Borrowed(f) => {
+ self.trampoline_types
+ .insert(f.try_clone().panic_on_oom(), for_func_ty);
for_func_ty
}
// The trampoline type is different from the original function
// type. Define the trampoline type and then intern it in
// `trampoline_types` so we can reuse it in the future.
- Cow::Owned(f) => {
+ TryCow::Owned(f) => {
let idx = self.types.push(WasmSubType {
is_final: true,
supertype: None,
composite_type: WasmCompositeType {
- inner: WasmCompositeInnerType::Func(f.clone()),
+ inner: WasmCompositeInnerType::Func(f.try_clone().panic_on_oom()),
shared: sub_ty.composite_type.shared,
},
});
diff --git a/crates/environ/src/component/types_builder.rs b/crates/environ/src/component/types_builder.rs
index f680d28494..97df81cb3a 100644
--- a/crates/environ/src/component/types_builder.rs
+++ b/crates/environ/src/component/types_builder.rs
@@ -173,7 +173,7 @@ impl ComponentTypesBuilder {
.find(|(_, ty)| {
ty.as_func().map_or(false, |sig| {
sig.params().len() == 1
- && sig.returns().len() == 0
+ && sig.results().len() == 0
&& sig.params()[0] == WasmValType::I32
})
})
diff --git a/crates/environ/src/fact/signature.rs b/crates/environ/src/fact/signature.rs
index c565bd9737..d0c170a0c5 100644
--- a/crates/environ/src/fact/signature.rs
+++ b/crates/environ/src/fact/signature.rs
@@ -33,7 +33,7 @@ impl ComponentTypesBuilder {
.unwrap_func();
Signature {
params: f.params().iter().map(|ty| self.val_type(ty)).collect(),
- results: f.returns().iter().map(|ty| self.val_type(ty)).collect(),
+ results: f.results().iter().map(|ty| self.val_type(ty)).collect(),
}
}
diff --git a/crates/environ/src/types.rs b/crates/environ/src/types.rs
index c52d24c3b7..2c9561038e 100644
--- a/crates/environ/src/types.rs
+++ b/crates/environ/src/types.rs
@@ -1,10 +1,14 @@
-use crate::{Tunables, WasmResult, error::OutOfMemory, wasm_unsupported};
-use alloc::borrow::Cow;
+use crate::{
+ PanicOnOom as _, Tunables, WasmResult,
+ collections::{TryClone, TryCollect as _, TryCow, TryExtend},
+ error::OutOfMemory,
+ prelude::*,
+ wasm_unsupported,
+};
use alloc::boxed::Box;
use core::{fmt, ops::Range};
use serde_derive::{Deserialize, Serialize};
use smallvec::SmallVec;
-use wasmtime_core::alloc::{TryClone, TryCollect as _};
/// A trait for things that can trace all type-to-type edges, aka all type
/// indices within this thing.
@@ -683,21 +687,21 @@ pub enum WasmHeapBottomType {
}
/// WebAssembly function type -- equivalent of `wasmparser`'s FuncType.
-#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
+#[derive(Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct WasmFuncType {
- params: Box<[WasmValType]>,
- non_i31_gc_ref_params_count: usize,
- returns: Box<[WasmValType]>,
- non_i31_gc_ref_returns_count: usize,
+ params_results: Box<[WasmValType]>,
+ params_len: u32,
+ non_i31_gc_ref_params_count: u32,
+ non_i31_gc_ref_results_count: u32,
}
impl TryClone for WasmFuncType {
fn try_clone(&self) -> Result<Self, OutOfMemory> {
- Ok(WasmFuncType {
- params: TryClone::try_clone(&self.params)?,
+ Ok(Self {
+ params_results: TryClone::try_clone(&self.params_results)?,
+ params_len: self.params_len,
non_i31_gc_ref_params_count: self.non_i31_gc_ref_params_count,
- returns: TryClone::try_clone(&self.returns)?,
- non_i31_gc_ref_returns_count: self.non_i31_gc_ref_returns_count,
+ non_i31_gc_ref_results_count: self.non_i31_gc_ref_results_count,
})
}
}
@@ -705,16 +709,16 @@ impl TryClone for WasmFuncType {
impl fmt::Display for WasmFuncType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "(func")?;
- if !self.params.is_empty() {
+ if !self.params().is_empty() {
write!(f, " (param")?;
- for p in self.params.iter() {
+ for p in self.params() {
write!(f, " {p}")?;
}
write!(f, ")")?;
}
- if !self.returns.is_empty() {
+ if !self.results().is_empty() {
write!(f, " (result")?;
- for r in self.returns.iter() {
+ for r in self.results() {
write!(f, " {r}")?;
}
write!(f, ")")?;
@@ -728,11 +732,8 @@ impl TypeTrace for WasmFuncType {
where
F: FnMut(EngineOrModuleTypeIndex) -> Result<(), E>,
{
- for p in self.params.iter() {
- p.trace(func)?;
- }
- for r in self.returns.iter() {
- r.trace(func)?;
+ for ty in self.params_results.iter() {
+ ty.trace(func)?;
}
Ok(())
}
@@ -741,11 +742,8 @@ impl TypeTrace for WasmFuncType {
where
F: FnMut(&mut EngineOrModuleTypeIndex) -> Result<(), E>,
{
- for p in self.params.iter_mut() {
- p.trace_mut(func)?;
- }
- for r in self.returns.iter_mut() {
- r.trace_mut(func)?;
+ for ty in self.params_results.iter_mut() {
+ ty.trace_mut(func)?;
}
Ok(())
}
@@ -754,51 +752,68 @@ impl TypeTrace for WasmFuncType {
impl WasmFuncType {
/// Creates a new function type from the provided `params` and `returns`.
#[inline]
- pub fn new(params: Box<[WasmValType]>, returns: Box<[WasmValType]>) -> Self {
- let non_i31_gc_ref_params_count = params
+ pub fn new(
+ params: impl IntoIterator<Item = WasmValType>,
+ results: impl IntoIterator<Item = WasmValType>,
+ ) -> Result<Self, OutOfMemory> {
+ let mut params_results: crate::collections::Vec<_> = params.into_iter().try_collect()?;
+ let non_i31_gc_ref_params_count = params_results
.iter()
.filter(|p| p.is_vmgcref_type_and_not_i31())
.count();
- let non_i31_gc_ref_returns_count = returns
+
+ let params_len = params_results.len();
+ params_results.try_extend(results)?;
+ let non_i31_gc_ref_results_count = params_results[params_len..]
.iter()
.filter(|r| r.is_vmgcref_type_and_not_i31())
.count();
- WasmFuncType {
- params,
+
+ let params_results = params_results.into_boxed_slice()?;
+ let params_len = u32::try_from(params_len).unwrap();
+ let non_i31_gc_ref_params_count = u32::try_from(non_i31_gc_ref_params_count).unwrap();
+ let non_i31_gc_ref_results_count = u32::try_from(non_i31_gc_ref_results_count).unwrap();
+
+ Ok(Self {
+ params_results,
+ params_len,
non_i31_gc_ref_params_count,
- returns,
- non_i31_gc_ref_returns_count,
- }
+ non_i31_gc_ref_results_count,
+ })
+ }
+
+ fn results_start(&self) -> usize {
+ usize::try_from(self.params_len).unwrap()
}
/// Function params types.
#[inline]
pub fn params(&self) -> &[WasmValType] {
- &self.params
+ &self.params_results[..self.results_start()]
}
/// How many `externref`s are in this function's params?
#[inline]
pub fn non_i31_gc_ref_params_count(&self) -> usize {
- self.non_i31_gc_ref_params_count
+ usize::try_from(self.non_i31_gc_ref_params_count).unwrap()
}
/// Returns params types.
#[inline]
- pub fn returns(&self) -> &[WasmValType] {
- &self.returns
+ pub fn results(&self) -> &[WasmValType] {
+ &self.params_results[self.results_start()..]
}
/// How many `externref`s are in this function's returns?
#[inline]
- pub fn non_i31_gc_ref_returns_count(&self) -> usize {
- self.non_i31_gc_ref_returns_count
+ pub fn non_i31_gc_ref_results_count(&self) -> usize {
+ usize::try_from(self.non_i31_gc_ref_results_count).unwrap()
}
/// Is this function type compatible with trampoline usage in Wasmtime?
pub fn is_trampoline_type(&self) -> bool {
self.params().iter().all(|p| *p == p.trampoline_type())
- && self.returns().iter().all(|r| *r == r.trampoline_type())
+ && self.results().iter().all(|r| *r == r.trampoline_type())
}
/// Get the version of this function type that is suitable for usage as a
@@ -826,21 +841,15 @@ impl WasmFuncType {
/// references themselves (unless the trampolines start doing explicit,
/// fallible downcasts, but if we ever need that, then we might want to
/// redesign this stuff).
- pub fn trampoline_type(&self) -> Result<Cow<'_, Self>, OutOfMemory> {
+ pub fn trampoline_type(&self) -> Result<TryCow<'_, Self>, OutOfMemory> {
if self.is_trampoline_type() {
- return Ok(Cow::Borrowed(self));
+ return Ok(TryCow::Borrowed(self));
}
- Ok(Cow::Owned(Self::new(
- self.params()
- .iter()
- .map(|p| p.trampoline_type())
- .try_collect()?,
- self.returns()
- .iter()
- .map(|r| r.trampoline_type())
- .try_collect()?,
- )))
+ Ok(TryCow::Owned(Self::new(
+ self.params().iter().map(|p| p.trampoline_type()),
+ self.results().iter().map(|r| r.trampoline_type()),
+ )?))
}
}
@@ -1140,7 +1149,7 @@ impl TypeTrace for WasmStructType {
}
}
-#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
+#[derive(Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
#[expect(missing_docs, reason = "self-describing type")]
pub struct WasmCompositeType {
/// The type defined inside the composite type.
@@ -1173,7 +1182,7 @@ impl fmt::Display for WasmCompositeType {
}
/// A function, array, or struct type.
-#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
+#[derive(Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
#[expect(missing_docs, reason = "self-describing variants")]
pub enum WasmCompositeInnerType {
Array(WasmArrayType),
@@ -1329,7 +1338,7 @@ impl TypeTrace for WasmCompositeType {
}
/// A concrete, user-defined (or host-defined) Wasm type.
-#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
+#[derive(Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct WasmSubType {
/// Whether this type is forbidden from being the supertype of any other
/// type.
@@ -1507,7 +1516,7 @@ impl TypeTrace for WasmSubType {
/// (rec (type (func $f (result (ref null $g))))
/// (type (func $g (result (ref null $f)))))
/// ```
-#[derive(Debug, Default, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
+#[derive(Debug, Default, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct WasmRecGroup {
/// The types inside of this recgroup.
pub types: Box<[WasmSubType]>,
@@ -2472,13 +2481,13 @@ pub trait TypeConvert {
.params()
.iter()
.map(|t| self.convert_valtype(*t))
- .collect::<WasmResult<_>>()?;
+ .collect::<WasmResult<Vec<_>>>()?;
let results = ty
.results()
.iter()
.map(|t| self.convert_valtype(*t))
- .collect::<WasmResult<_>>()?;
- Ok(WasmFuncType::new(params, results))
+ .collect::<WasmResult<Vec<_>>>()?;
+ Ok(WasmFuncType::new(params, results).panic_on_oom())
}
/// Converts a wasmparser value type to a wasmtime type
@@ -2533,3 +2542,23 @@ pub trait TypeConvert {
/// heap type.
fn lookup_type_index(&self, index: wasmparser::UnpackedIndex) -> EngineOrModuleTypeIndex;
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn wasm_func_type_new() -> Result<()> {
+ let i32 = WasmValType::I32;
+ let anyref = WasmValType::Ref(WasmRefType {
+ nullable: true,
+ heap_type: WasmHeapType::Any,
+ });
+ let ty = WasmFuncType::new([i32, i32, anyref, anyref], [i32, anyref])?;
+ assert_eq!(ty.params(), &[i32, i32, anyref, anyref]);
+ assert_eq!(ty.non_i31_gc_ref_params_count(), 2);
+ assert_eq!(ty.results(), &[i32, anyref]);
+ assert_eq!(ty.non_i31_gc_ref_results_count(), 1);
+ Ok(())
+ }
+}
diff --git a/crates/wasmtime/src/runtime/component/types.rs b/crates/wasmtime/src/runtime/component/types.rs
index 8e4875aadc..9fbc562af2 100644
--- a/crates/wasmtime/src/runtime/component/types.rs
+++ b/crates/wasmtime/src/runtime/component/types.rs
@@ -14,7 +14,7 @@ use wasmtime_environ::component::{
TypeRecordIndex, TypeResourceTable, TypeResourceTableIndex, TypeResultIndex, TypeStreamIndex,
TypeStreamTableIndex, TypeTupleIndex, TypeVariantIndex,
};
-use wasmtime_environ::{PanicOnOom, PrimaryMap};
+use wasmtime_environ::{PanicOnOom as _, PrimaryMap, collections::TryClone as _};
pub use crate::component::resources::ResourceType;
@@ -1104,7 +1104,7 @@ impl ComponentItem {
engine,
subty.is_final,
subty.supertype,
- subty.unwrap_func().clone(),
+ subty.unwrap_func().try_clone().panic_on_oom(),
)
.panic_on_oom(),
)
diff --git a/crates/wasmtime/src/runtime/type_registry.rs b/crates/wasmtime/src/runtime/type_registry.rs
index abf9dc59e4..f006de80e7 100644
--- a/crates/wasmtime/src/runtime/type_registry.rs
+++ b/crates/wasmtime/src/runtime/type_registry.rs
@@ -8,7 +8,6 @@ use crate::error::OutOfMemory;
use crate::prelude::*;
use crate::sync::RwLock;
use crate::vm::GcRuntime;
-use alloc::borrow::Cow;
use alloc::sync::Arc;
use core::cell::Cell;
use core::iter;
@@ -26,7 +25,7 @@ use wasmtime_core::slab::{Id as SlabId, Slab};
use wasmtime_environ::{
EngineOrModuleTypeIndex, EntityRef, GcLayout, ModuleInternedTypeIndex, ModuleTypes,
PanicOnOom as _, TypeTrace, Undo, VMSharedTypeIndex, WasmRecGroup, WasmSubType,
- collections::{HashSet, PrimaryMap, SecondaryMap, TryClone, Vec},
+ collections::{HashSet, PrimaryMap, SecondaryMap, TryClone, TryCow, Vec},
iter_entity_range,
packed_option::{PackedOption, ReservedValue},
};
@@ -703,7 +702,7 @@ impl TypeRegistryInner {
gc_runtime,
&map,
module_group.clone(),
- iter_entity_range(module_group.clone()).map(|ty| types[ty].clone()),
+ iter_entity_range(module_group.clone()).map(|ty| types[ty].try_clone()),
)?;
// Update the module-to-engine map with this rec group's
@@ -754,7 +753,7 @@ impl TypeRegistryInner {
gc_runtime: Option<&dyn GcRuntime>,
map: &PrimaryMap<ModuleInternedTypeIndex, VMSharedTypeIndex>,
range: Range<ModuleInternedTypeIndex>,
- types: impl ExactSizeIterator<Item = WasmSubType>,
+ types: impl ExactSizeIterator<Item = Result<WasmSubType, OutOfMemory>>,
) -> Result<RecGroupEntry, OutOfMemory> {
log::trace!("registering rec group of length {}", types.len());
debug_assert_eq!(iter_entity_range(range.clone()).len(), types.len());
@@ -771,7 +770,8 @@ impl TypeRegistryInner {
let hash_consing_key = WasmRecGroup {
types: types
.zip(iter_entity_range(range.clone()))
- .map(|(mut ty, module_index)| {
+ .map(|(ty, module_index)| {
+ let mut ty = ty?;
non_canon_types
.push((module_index, ty.try_clone()?))
.expect("reserved capacity");
@@ -1016,11 +1016,11 @@ impl TypeRegistryInner {
};
let trampoline_ty = match func_ty.trampoline_type()? {
- Cow::Owned(ty) => ty,
- Cow::Borrowed(ty) if !sub_ty.is_final || sub_ty.supertype.is_some() => {
+ TryCow::Owned(ty) => ty,
+ TryCow::Borrowed(ty) if !sub_ty.is_final || sub_ty.supertype.is_some() => {
ty.try_clone()?
}
- Cow::Borrowed(_) => {
+ TryCow::Borrowed(_) => {
// The function type is its own trampoline type. Leave its entry
// in `type_to_trampoline` empty to signal this.
debug_assert!(func_ty.is_trampoline_type());
@@ -1354,7 +1354,7 @@ impl TypeRegistryInner {
let range = ModuleInternedTypeIndex::from_bits(u32::MAX - 1)
..ModuleInternedTypeIndex::from_bits(u32::MAX);
- self.register_rec_group(gc_runtime, &map, range, iter::once(ty))
+ self.register_rec_group(gc_runtime, &map, range, iter::once(Ok(ty)))
}
/// Unregister all of a type collection's rec groups.
diff --git a/crates/wasmtime/src/runtime/types.rs b/crates/wasmtime/src/runtime/types.rs
index e37f333d2e..5cea99266f 100644
--- a/crates/wasmtime/src/runtime/types.rs
+++ b/crates/wasmtime/src/runtime/types.rs
@@ -12,7 +12,7 @@ use wasmtime_environ::{
PanicOnOom as _, Table, Tag, TypeTrace, VMSharedTypeIndex, WasmArrayType,
WasmCompositeInnerType, WasmCompositeType, WasmFieldType, WasmFuncType, WasmHeapType,
WasmRefType, WasmStorageType, WasmStructType, WasmSubType, WasmValType,
- collections::Vec as FallibleVec,
+ collections::{TryClone as _, Vec as FallibleVec},
};
pub(crate) mod matching;
@@ -1509,7 +1509,7 @@ impl ExternType {
engine,
subty.is_final,
subty.supertype,
- subty.unwrap_func().clone(),
+ subty.unwrap_func().try_clone().panic_on_oom(),
)
.panic_on_oom()
.into()
@@ -2493,7 +2493,7 @@ impl FuncType {
let results: Box<[_]> = results
.map(|p| to_wasm_type(p, &mut wasmtime_results))
.try_collect()?;
- let wasm_func_ty = WasmFuncType::new(params, results);
+ let wasm_func_ty = WasmFuncType::new(params, results)?;
if let Some(supertype) = supertype {
assert!(supertype.comes_from_same_engine(engine));
@@ -2591,7 +2591,7 @@ impl FuncType {
let engine = self.engine();
self.registered_type
.unwrap_func()
- .returns()
+ .results()
.get(i)
.map(|ty| ValType::from_wasm_type(engine, ty))
}
@@ -2602,7 +2602,7 @@ impl FuncType {
let engine = self.engine();
self.registered_type
.unwrap_func()
- .returns()
+ .results()
.iter()
.map(|ty| ValType::from_wasm_type(engine, ty))
}
diff --git a/winch/codegen/src/abi/mod.rs b/winch/codegen/src/abi/mod.rs
index 19741eb786..b5bd4e6b9e 100644
--- a/winch/codegen/src/abi/mod.rs
+++ b/winch/codegen/src/abi/mod.rs
@@ -81,7 +81,7 @@ pub(crate) fn wasm_sig<A: ABI>(ty: &WasmFuncType) -> Result<ABISig> {
params.extend_from_slice(&vmctx_types::<A>());
params.extend_from_slice(ty.params());
- A::sig_from(&params, ty.returns(), &CallingConvention::Default)
+ A::sig_from(&params, ty.results(), &CallingConvention::Default)
}
/// Returns the callee and caller [VMContext] types.
@@ -112,7 +112,7 @@ pub(crate) trait ABI {
/// function type.
#[cfg(test)]
fn sig(wasm_sig: &WasmFuncType, call_conv: &CallingConvention) -> Result<ABISig> {
- Self::sig_from(wasm_sig.params(), wasm_sig.returns(), call_conv)
+ Self::sig_from(wasm_sig.params(), wasm_sig.results(), call_conv)
}
/// Construct an ABI signature from WasmType params and returns.
diff --git a/winch/codegen/src/codegen/control.rs b/winch/codegen/src/codegen/control.rs
index c143c2557c..cecf901256 100644
--- a/winch/codegen/src/codegen/control.rs
+++ b/winch/codegen/src/codegen/control.rs
@@ -16,10 +16,10 @@ use crate::{
stack::Val,
};
use cranelift_codegen::MachLabel;
-use wasmtime_environ::{WasmFuncType, WasmValType};
+use wasmtime_environ::{PanicOnOom as _, WasmFuncType, WasmValType, collections::TryClone as _};
/// Categorization of the type of the block.
-#[derive(Debug, Clone)]
+#[derive(Debug)]
pub(crate) enum BlockType {
/// Doesn't produce or consume any values.
Void,
@@ -31,6 +31,17 @@ pub(crate) enum BlockType {
ABISig(ABISig),
}
+impl Clone for BlockType {
+ fn clone(&self) -> Self {
+ match self {
+ Self::Void => Self::Void,
+ Self::Single(x) => Self::Single(*x),
+ Self::ABISig(x) => Self::ABISig(x.clone()),
+ Self::Func(f) => Self::Func(f.try_clone().panic_on_oom()),
+ }
+ }
+}
+
/// Holds all the information about the signature of the block.
#[derive(Debug, Clone)]
pub(crate) struct BlockSig {
@@ -84,7 +95,7 @@ impl BlockSig {
<M::ABI as ABI>::abi_results(&[*ty], &CallingConvention::Default)
}
BlockType::Func(f) => {
- <M::ABI as ABI>::abi_results(f.returns(), &CallingConvention::Default)
+ <M::ABI as ABI>::abi_results(f.results(), &CallingConvention::Default)
}
BlockType::ABISig(_) => unreachable!(),
};
@@ -136,7 +147,7 @@ impl BlockSig {
match &self.ty {
BlockType::Void => 0,
BlockType::Single(_) => 1,
- BlockType::Func(f) => f.returns().len(),
+ BlockType::Func(f) => f.results().len(),
BlockType::ABISig(sig) => sig.results().len(),
}
}
diff --git a/winch/codegen/src/codegen/env.rs b/winch/codegen/src/codegen/env.rs
index 2a0c6423a9..683d354698 100644
--- a/winch/codegen/src/codegen/env.rs
+++ b/winch/codegen/src/codegen/env.rs
@@ -13,8 +13,9 @@ use std::mem;
use wasmparser::BlockType;
use wasmtime_environ::{
BuiltinFunctionIndex, DefinedFuncIndex, FuncIndex, FuncKey, GlobalIndex, IndexType, Memory,
- MemoryIndex, ModuleTranslation, ModuleTypesBuilder, PrimaryMap, PtrSize, Table, TableIndex,
- TypeConvert, TypeIndex, VMOffsets, WasmHeapType, WasmValType,
+ MemoryIndex, ModuleTranslation, ModuleTypesBuilder, PanicOnOom as _, PrimaryMap, PtrSize,
+ Table, TableIndex, TypeConvert, TypeIndex, VMOffsets, WasmHeapType, WasmValType,
+ collections::TryClone as _,
};
#[derive(Debug, Clone, Copy)]
@@ -194,7 +195,7 @@ impl<'a, 'translation, 'data, P: PtrSize> FuncEnv<'a, 'translation, 'data, P> {
let sig_index = self.translation.module.types[TypeIndex::from_u32(idx)]
.unwrap_module_type_index();
let sig = self.types[sig_index].unwrap_func();
- BlockSig::new(control::BlockType::func(sig.clone()))
+ BlockSig::new(control::BlockType::func(sig.try_clone().panic_on_oom()))
}
})
}
diff --git a/winch/codegen/src/isa/aarch64/abi.rs b/winch/codegen/src/isa/aarch64/abi.rs
index 1e61d5b546..200e837f28 100644
--- a/winch/codegen/src/isa/aarch64/abi.rs
+++ b/winch/codegen/src/isa/aarch64/abi.rs
@@ -210,10 +210,7 @@ mod tests {
#[test]
fn xreg_abi_sig() -> Result<()> {
- let wasm_sig = WasmFuncType::new(
- [I32, I64, I32, I64, I32, I32, I64, I32, I64].into(),
- [].into(),
- );
+ let wasm_sig = WasmFuncType::new([I32, I64, I32, I64, I32, I32, I64, I32, I64], [])?;
let sig = Aarch64ABI::sig(&wasm_sig, &CallingConvention::Default)?;
let params = sig.params;
@@ -232,10 +229,7 @@ mod tests {
#[test]
fn vreg_abi_sig() -> Result<()> {
- let wasm_sig = WasmFuncType::new(
- [F32, F64, F32, F64, F32, F32, F64, F32, F64].into(),
- [].into(),
- );
+ let wasm_sig = WasmFuncType::new([F32, F64, F32, F64, F32, F32, F64, F32, F64], [])?;
let sig = Aarch64ABI::sig(&wasm_sig, &CallingConvention::Default)?;
let params = sig.params;
@@ -254,10 +248,7 @@ mod tests {
#[test]
fn mixed_abi_sig() -> Result<()> {
- let wasm_sig = WasmFuncType::new(
- [F32, I32, I64, F64, I32, F32, F64, F32, F64].into(),
- [].into(),
- );
+ let wasm_sig = WasmFuncType::new([F32, I32, I64, F64, I32, F32, F64, F32, F64], [])?;
let sig = Aarch64ABI::sig(&wasm_sig, &CallingConvention::Default)?;
let params = sig.params;
@@ -276,10 +267,7 @@ mod tests {
#[test]
fn int_abi_sig_multi_returns() -> Result<()> {
- let wasm_sig = WasmFuncType::new(
- [I32, I64, I32, I64, I32, I32].into(),
- [I32, I32, I32].into(),
- );
+ let wasm_sig = WasmFuncType::new([I32, I64, I32, I64, I32, I32], [I32, I32, I32])?;
let sig = Aarch64ABI::sig(&wasm_sig, &CallingConvention::Default)?;
let params = sig.params;
@@ -300,10 +288,7 @@ mod tests {
#[test]
fn mixed_abi_sig_multi_returns() -> Result<()> {
- let wasm_sig = WasmFuncType::new(
- [F32, I32, I64, F64, I32].into(),
- [I32, F32, I32, F32, I64].into(),
- );
+ let wasm_sig = WasmFuncType::new([F32, I32, I64, F64, I32], [I32, F32, I32, F32, I64])?;
let sig = Aarch64ABI::sig(&wasm_sig, &CallingConvention::Default)?;
let params = sig.params;
diff --git a/winch/codegen/src/isa/x64/abi.rs b/winch/codegen/src/isa/x64/abi.rs
index 5f668dd553..7ec1433ca0 100644
--- a/winch/codegen/src/isa/x64/abi.rs
+++ b/winch/codegen/src/isa/x64/abi.rs
@@ -284,8 +284,7 @@ mod tests {
#[test]
fn int_abi_sig() -> Result<()> {
- let wasm_sig =
- WasmFuncType::new([I32, I64, I32, I64, I32, I32, I64, I32].into(), [].into());
+ let wasm_sig = WasmFuncType::new([I32, I64, I32, I64, I32, I32, I64, I32], [])?;
let sig = X64ABI::sig(&wasm_sig, &CallingConvention::Default)?;
let params = sig.params;
@@ -303,10 +302,8 @@ mod tests {
#[test]
fn int_abi_sig_multi_returns() -> Result<()> {
- let wasm_sig = WasmFuncType::new(
- [I32, I64, I32, I64, I32, I32, I64, I32].into(),
- [I32, I32, I32].into(),
- );
+ let wasm_sig =
+ WasmFuncType::new([I32, I64, I32, I64, I32, I32, I64, I32], [I32, I32, I32])?;
let sig = X64ABI::sig(&wasm_sig, &CallingConvention::Default)?;
let params = sig.params;
@@ -329,10 +326,7 @@ mod tests {
#[test]
fn float_abi_sig() -> Result<()> {
- let wasm_sig = WasmFuncType::new(
- [F32, F64, F32, F64, F32, F32, F64, F32, F64].into(),
- [].into(),
- );
+ let wasm_sig = WasmFuncType::new([F32, F64, F32, F64, F32, F32, F64, F32, F64], [])?;
let sig = X64ABI::sig(&wasm_sig, &CallingConvention::Default)?;
let params = sig.params;
@@ -352,9 +346,9 @@ mod tests {
#[test]
fn vector_abi_sig() -> Result<()> {
let wasm_sig = WasmFuncType::new(
- [V128, V128, V128, V128, V128, V128, V128, V128, V128, V128].into(),
- [].into(),
- );
+ [V128, V128, V128, V128, V128, V128, V128, V128, V128, V128],
+ [],
+ )?;
let sig = X64ABI::sig(&wasm_sig, &CallingConvention::Default)?;
let params = sig.params;
@@ -374,7 +368,7 @@ mod tests {
#[test]
fn vector_abi_sig_multi_returns() -> Result<()> {
- let wasm_sig = WasmFuncType::new([].into(), [V128, V128, V128].into());
+ let wasm_sig = WasmFuncType::new([], [V128, V128, V128])?;
let sig = X64ABI::sig(&wasm_sig, &CallingConvention::Default)?;
let results = sig.results;
@@ -387,10 +381,7 @@ mod tests {
#[test]
fn mixed_abi_sig() -> Result<()> {
- let wasm_sig = WasmFuncType::new(
- [F32, I32, I64, F64, I32, F32, F64, F32, F64].into(),
- [].into(),
- );
+ let wasm_sig = WasmFuncType::new([F32, I32, I64, F64, I32, F32, F64, F32, F64], [])?;
let sig = X64ABI::sig(&wasm_sig, &CallingConvention::Default)?;
let params = sig.params;
@@ -405,10 +396,8 @@ mod tests {
match_reg_arg(params.get(7).unwrap(), F32, regs::xmm4());
match_reg_arg(params.get(8).unwrap(), F64, regs::xmm5());
- let wasm_sig = WasmFuncType::new(
- [F32, F32, F32, F32, F32, F32, F32, F32, F32, V128].into(),
- [V128].into(),
- );
+ let wasm_sig =
+ WasmFuncType::new([F32, F32, F32, F32, F32, F32, F32, F32, F32, V128], [V128])?;
let sig = X64ABI::sig(&wasm_sig, &CallingConvention::Default)?;
let params = sig.params;
@@ -420,10 +409,7 @@ mod tests {
#[test]
fn system_v_call_conv() -> Result<()> {
- let wasm_sig = WasmFuncType::new(
- [F32, I32, I64, F64, I32, F32, F64, F32, F64].into(),
- [].into(),
- );
+ let wasm_sig = WasmFuncType::new([F32, I32, I64, F64, I32, F32, F64, F32, F64], [])?;
let sig = X64ABI::sig(&wasm_sig, &CallingConvention::SystemV)?;
let params = sig.params;
@@ -442,10 +428,7 @@ mod tests {
#[test]
fn fastcall_call_conv() -> Result<()> {
- let wasm_sig = WasmFuncType::new(
- [F32, I32, I64, F64, I32, F32, F64, F32, F64].into(),
- [].into(),
- );
+ let wasm_sig = WasmFuncType::new([F32, I32, I64, F64, I32, F32, F64, F32, F64], [])?;
let sig = X64ABI::sig(&wasm_sig, &CallingConvention::WindowsFastcall)?;
let params = sig.params;
@@ -462,9 +445,9 @@ mod tests {
#[test]
fn fastcall_call_conv_multi_returns() -> Result<()> {
let wasm_sig = WasmFuncType::new(
- [F32, I32, I64, F64, I32, F32, F64, F32, F64].into(),
- [I32, F32, I32, F32, I64].into(),
- );
+ [F32, I32, I64, F64, I32, F32, F64, F32, F64],
+ [I32, F32, I32, F32, I64],
+ )?;
let sig = X64ABI::sig(&wasm_sig, &CallingConvention::WindowsFastcall)?;
let params = sig.params;