Skip to content

Commit 946225b

Browse files
committed
Handle OOM in wasmtime_environ::types::WasmFuncType
It should also not `#[derive(Clone)]` anymore, just `TryClone`, which propagates out to a bunch of other types as well. And while we are here: * Rename its "returns" to "results" to match Wasmtime's public API and also the Wasm text format. * Store both its params and results in a single allocation. * Shrink its size on 64-bit architectures by storing its param- and result-GC-type counts as `u32`s rather than `usize`s.
1 parent d42d0b6 commit 946225b

File tree

15 files changed

+161
-120
lines changed

15 files changed

+161
-120
lines changed

crates/cranelift/src/compiler.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ impl wasmtime_environ::Compiler for Compiler {
465465

466466
// Return results from the array as native return values.
467467
let results =
468-
self.load_values_from_array(wasm_func_ty.returns(), &mut builder, args_base, args_len);
468+
self.load_values_from_array(wasm_func_ty.results(), &mut builder, args_base, args_len);
469469
builder.ins().return_(&results);
470470
builder.finalize();
471471

@@ -1068,7 +1068,7 @@ impl Compiler {
10681068

10691069
// Compute the size of the values vector.
10701070
let value_size = mem::size_of::<u128>();
1071-
let values_vec_len = cmp::max(ty.params().len(), ty.returns().len());
1071+
let values_vec_len = cmp::max(ty.params().len(), ty.results().len());
10721072
let values_vec_byte_size = u32::try_from(value_size * values_vec_len).unwrap();
10731073
let values_vec_len = u32::try_from(values_vec_len).unwrap();
10741074

@@ -1419,7 +1419,7 @@ impl Compiler {
14191419
builder.switch_to_block(normal_return);
14201420
self.store_values_to_array(
14211421
&mut builder,
1422-
callee_sig.returns(),
1422+
callee_sig.results(),
14231423
&normal_return_values,
14241424
values_vec_ptr,
14251425
values_vec_len,

crates/cranelift/src/compiler/component.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use cranelift_codegen::isa::{CallConv, TargetIsa};
1010
use cranelift_frontend::FunctionBuilder;
1111
use wasmtime_environ::error::{Result, bail};
1212
use wasmtime_environ::{
13-
Abi, BuiltinFunctionIndex, CompiledFunctionBody, EntityRef, FuncKey, HostCall, PtrSize,
14-
TrapSentinel, Tunables, WasmFuncType, WasmValType, component::*,
13+
Abi, BuiltinFunctionIndex, CompiledFunctionBody, EntityRef, FuncKey, HostCall, PanicOnOom as _,
14+
PtrSize, TrapSentinel, Tunables, WasmFuncType, WasmValType, component::*,
1515
fact::PREPARE_CALL_FIXED_PARAMS,
1616
};
1717

@@ -177,7 +177,7 @@ impl<'a> TrampolineCompiler<'a> {
177177
}
178178
Trampoline::ResourceRep { instance, ty } => {
179179
// Currently this only supports resources represented by `i32`
180-
assert_eq!(self.signature.returns()[0], WasmValType::I32);
180+
assert_eq!(self.signature.results()[0], WasmValType::I32);
181181
self.translate_libcall(
182182
host::resource_rep32,
183183
TrapSentinel::NegativeOne,
@@ -639,10 +639,7 @@ impl<'a> TrampolineCompiler<'a> {
639639
Trampoline::SyncStartCall { callback } => {
640640
let pointer_type = self.isa.pointer_type();
641641
let (values_vec_ptr, len) = self.compiler.allocate_stack_array_and_spill_args(
642-
&WasmFuncType::new(
643-
Box::new([]),
644-
self.signature.returns().iter().copied().collect(),
645-
),
642+
&WasmFuncType::new([], self.signature.results().iter().copied()).panic_on_oom(),
646643
&mut self.builder,
647644
&[],
648645
);
@@ -972,10 +969,8 @@ impl<'a> TrampolineCompiler<'a> {
972969
// A mixture of the above two.
973970
WasmArgs::InRegistersUpTo(n) => {
974971
let (values_vec_ptr, len) = self.compiler.allocate_stack_array_and_spill_args(
975-
&WasmFuncType::new(
976-
self.signature.params().iter().skip(n).copied().collect(),
977-
Box::new([]),
978-
),
972+
&WasmFuncType::new(self.signature.params().iter().skip(n).copied(), [])
973+
.panic_on_oom(),
979974
&mut self.builder,
980975
&wasm_params[n..],
981976
);
@@ -1026,7 +1021,7 @@ impl<'a> TrampolineCompiler<'a> {
10261021
// accounts for the ABI of this function when storing results.
10271022
let result = self.builder.func.dfg.inst_results(call).get(0).copied();
10281023
let result_ty = result.map(|v| self.builder.func.dfg.value_type(v));
1029-
let expected = self.signature.returns();
1024+
let expected = self.signature.results();
10301025
match host_result.into() {
10311026
HostResult::Sentinel(TrapSentinel::NegativeOne) => {
10321027
assert_eq!(expected.len(), 1);
@@ -1054,7 +1049,7 @@ impl<'a> TrampolineCompiler<'a> {
10541049
let len = len.or(val_raw_len).unwrap();
10551050
self.raise_if_host_trapped(result.unwrap());
10561051
let results = self.compiler.load_values_from_array(
1057-
self.signature.returns(),
1052+
self.signature.results(),
10581053
&mut self.builder,
10591054
ptr,
10601055
len,
@@ -1671,9 +1666,10 @@ impl ComponentCompiler for Compiler {
16711666
symbol: &str,
16721667
) -> Result<CompiledFunctionBody> {
16731668
let wasm_func_ty = WasmFuncType::new(
1674-
intrinsic.core_params().into(),
1675-
intrinsic.core_results().into(),
1676-
);
1669+
intrinsic.core_params().iter().copied(),
1670+
intrinsic.core_results().iter().copied(),
1671+
)
1672+
.panic_on_oom();
16771673

16781674
match abi {
16791675
// Fall through to the trampoline compiler.

crates/cranelift/src/func_environ.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2594,7 +2594,7 @@ impl FuncEnvironment<'_> {
25942594

25952595
pub fn sig_ref_result_needs_stack_map(&self, sig_ref: ir::SigRef, index: usize) -> bool {
25962596
let wasm_func_ty = self.sig_ref_to_ty[sig_ref].as_ref().unwrap();
2597-
wasm_func_ty.returns()[index].is_vmgcref_type_and_not_i31()
2597+
wasm_func_ty.results()[index].is_vmgcref_type_and_not_i31()
25982598
}
25992599

26002600
pub fn translate_table_grow(
@@ -3999,7 +3999,7 @@ impl FuncEnvironment<'_> {
39993999
let idx = self.module.types[index].unwrap_module_type_index();
40004000
self.types[self.types[idx].unwrap_cont().unwrap_module_type_index()]
40014001
.unwrap_func()
4002-
.returns()
4002+
.results()
40034003
}
40044004

40054005
pub fn tag_params(&self, tag_index: TagIndex) -> &[WasmValType] {
@@ -4013,7 +4013,7 @@ impl FuncEnvironment<'_> {
40134013
let idx = self.module.tags[tag_index].signature;
40144014
self.types[idx.unwrap_module_type_index()]
40154015
.unwrap_func()
4016-
.returns()
4016+
.results()
40174017
}
40184018

40194019
pub fn use_blendv_for_relaxed_laneselect(&self, ty: Type) -> bool {

crates/cranelift/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ fn wasm_call_signature(
203203
let mut sig = blank_sig(isa, call_conv);
204204
let cvt = |ty: &WasmValType| ir::AbiParam::new(value_type(isa, *ty));
205205
sig.params.extend(wasm_func_ty.params().iter().map(&cvt));
206-
sig.returns.extend(wasm_func_ty.returns().iter().map(&cvt));
206+
sig.returns.extend(wasm_func_ty.results().iter().map(&cvt));
207207
sig
208208
}
209209

crates/environ/src/collections.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ pub use index_map::IndexMap;
1414
pub use primary_map::PrimaryMap;
1515
pub use secondary_map::SecondaryMap;
1616
pub use wasmtime_core::{
17-
alloc::{String, TryClone, TryCollect, TryExtend, TryFromIterator, TryNew, Vec, try_new},
17+
alloc::{
18+
String, TryClone, TryCollect, TryCow, TryExtend, TryFromIterator, TryNew, TryToOwned, Vec,
19+
try_new,
20+
},
1821
vec,
1922
};
2023

crates/environ/src/compile/module_types.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
use crate::{
22
EngineOrModuleTypeIndex, EntityRef, ModuleInternedRecGroupIndex, ModuleInternedTypeIndex,
3-
ModuleTypes, TypeConvert, TypeIndex, WasmArrayType, WasmCompositeInnerType, WasmCompositeType,
4-
WasmExnType, WasmFieldType, WasmFuncType, WasmHeapType, WasmResult, WasmStorageType,
5-
WasmStructType, WasmSubType, wasm_unsupported,
3+
ModuleTypes, PanicOnOom as _, TypeConvert, TypeIndex, WasmArrayType, WasmCompositeInnerType,
4+
WasmCompositeType, WasmExnType, WasmFieldType, WasmFuncType, WasmHeapType, WasmResult,
5+
WasmStorageType, WasmStructType, WasmSubType,
6+
collections::{TryClone as _, TryCow},
7+
wasm_unsupported,
68
};
79
use std::{
8-
borrow::Cow,
910
collections::{HashMap, hash_map::Entry},
1011
ops::Index,
1112
};
1213
use wasmparser::{UnpackedIndex, Validator, ValidatorId};
13-
use wasmtime_core::alloc::PanicOnOom as _;
1414

1515
/// A type marking the start of a recursion group's definition.
1616
///
@@ -166,19 +166,20 @@ impl ModuleTypesBuilder {
166166
// type. We can reuse the definition and its index, but still
167167
// need to intern the type into our `trampoline_types` map so we
168168
// can reuse it in the future.
169-
Cow::Borrowed(f) => {
170-
self.trampoline_types.insert(f.clone(), for_func_ty);
169+
TryCow::Borrowed(f) => {
170+
self.trampoline_types
171+
.insert(f.try_clone().panic_on_oom(), for_func_ty);
171172
for_func_ty
172173
}
173174
// The trampoline type is different from the original function
174175
// type. Define the trampoline type and then intern it in
175176
// `trampoline_types` so we can reuse it in the future.
176-
Cow::Owned(f) => {
177+
TryCow::Owned(f) => {
177178
let idx = self.types.push(WasmSubType {
178179
is_final: true,
179180
supertype: None,
180181
composite_type: WasmCompositeType {
181-
inner: WasmCompositeInnerType::Func(f.clone()),
182+
inner: WasmCompositeInnerType::Func(f.try_clone().panic_on_oom()),
182183
shared: sub_ty.composite_type.shared,
183184
},
184185
});

crates/environ/src/component/types_builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ impl ComponentTypesBuilder {
173173
.find(|(_, ty)| {
174174
ty.as_func().map_or(false, |sig| {
175175
sig.params().len() == 1
176-
&& sig.returns().len() == 0
176+
&& sig.results().len() == 0
177177
&& sig.params()[0] == WasmValType::I32
178178
})
179179
})

crates/environ/src/fact/signature.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ impl ComponentTypesBuilder {
3333
.unwrap_func();
3434
Signature {
3535
params: f.params().iter().map(|ty| self.val_type(ty)).collect(),
36-
results: f.returns().iter().map(|ty| self.val_type(ty)).collect(),
36+
results: f.results().iter().map(|ty| self.val_type(ty)).collect(),
3737
}
3838
}
3939

0 commit comments

Comments
 (0)