A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Make GC struct and exception layout functions handle OOM (#13413)
Nick Fitzgeralda9f0faa49800
diff --git a/crates/cranelift/src/func_environ/gc/enabled.rs b/crates/cranelift/src/func_environ/gc/enabled.rsindex 0ec88b3c96..59474d5854 100644--- a/crates/cranelift/src/func_environ/gc/enabled.rs+++ b/crates/cranelift/src/func_environ/gc/enabled.rs@@ -1765,6 +1765,7 @@ impl FuncEnvironment<'_> {.unwrap().layouts().gc_layout(ty)+ .expect("gc_layout should not OOM at compile time").expect("should only call `FuncEnvironment::gc_layout` for GC types");self.ty_to_gc_layout.insert(type_index, layout);}diff --git a/crates/environ/src/gc.rs b/crates/environ/src/gc.rsindex 7694ff7305..5a5c9d64bf 100644--- a/crates/environ/src/gc.rs+++ b/crates/environ/src/gc.rs@@ -139,9 +139,7 @@ fn common_struct_or_exn_layout(fields: &[crate::WasmFieldType],header_size: u32,header_align: u32,-) -> (u32, u32, TryVec<GcStructLayoutField>) {- use crate::PanicOnOom as _;-+) -> Result<(u32, u32, TryVec<GcStructLayoutField>), OutOfMemory> {// Process each field, aligning it to its natural alignment.//// We don't try and do any fancy field reordering to minimize padding (yet?)@@ -162,15 +160,14 @@ fn common_struct_or_exn_layout(let is_gc_ref = f.element_type.is_vmgcref_type_and_not_i31();GcStructLayoutField { offset, is_gc_ref }})- .try_collect::<TryVec<_>, _>()- .panic_on_oom();+ .try_collect::<TryVec<_>, _>()?;// Ensure that the final size is a multiple of the alignment, for// simplicity.let align_size_to = align;align_up(&mut size, &mut align, align_size_to);- (size, align, fields)+ Ok((size, align, fields))}/// Common code to define a GC struct's layout, given the size and alignment of@@ -180,25 +177,29 @@ fn common_struct_layout(ty: &WasmStructType,header_size: u32,header_align: u32,-) -> GcStructLayout {+) -> Result<GcStructLayout, OutOfMemory> {assert!(header_size >= crate::VM_GC_HEADER_SIZE);assert!(header_align >= crate::VM_GC_HEADER_ALIGN);- let (size, align, fields) = common_struct_or_exn_layout(&ty.fields, header_size, header_align);+ let (size, align, fields) = common_struct_or_exn_layout(&ty.fields, header_size, header_align)?;- GcStructLayout {+ Ok(GcStructLayout {size,align,fields,is_exception: false,- }+ })}/// Common code to define a GC exception object's layout, given the/// size and alignment of the collector's GC header and its expected/// offset of the array length field.#[cfg(any(feature = "gc-null", feature = "gc-drc", feature = "gc-copying"))]-fn common_exn_layout(ty: &WasmExnType, header_size: u32, header_align: u32) -> GcStructLayout {+fn common_exn_layout(+ ty: &WasmExnType,+ header_size: u32,+ header_align: u32,+) -> Result<GcStructLayout, OutOfMemory> {assert!(header_size >= crate::VM_GC_HEADER_SIZE);assert!(header_align >= crate::VM_GC_HEADER_ALIGN);@@ -207,14 +208,14 @@ fn common_exn_layout(ty: &WasmExnType, header_size: u32, header_align: u32) -> Gassert!(header_align >= 8);let header_size = header_size + 2 * u32::try_from(core::mem::size_of::<u32>()).unwrap();- let (size, align, fields) = common_struct_or_exn_layout(&ty.fields, header_size, header_align);+ let (size, align, fields) = common_struct_or_exn_layout(&ty.fields, header_size, header_align)?;- GcStructLayout {+ Ok(GcStructLayout {size,align,fields,is_exception: true,- }+ })}/// A trait for getting the layout of a Wasm GC struct or array inside a@@ -244,16 +245,18 @@ pub trait GcTypeLayouts {////// Returns `None` if the type is a function type, as functions are not/// managed by the GC.- fn gc_layout(&self, ty: &WasmCompositeType) -> Option<GcLayout> {+ fn gc_layout(&self, ty: &WasmCompositeType) -> Result<Option<GcLayout>, OutOfMemory> {assert!(!ty.shared);match &ty.inner {- WasmCompositeInnerType::Array(ty) => Some(self.array_layout(ty).into()),- WasmCompositeInnerType::Struct(ty) => Some(Arc::new(self.struct_layout(ty)).into()),- WasmCompositeInnerType::Func(_) => None,+ WasmCompositeInnerType::Array(ty) => Ok(Some(self.array_layout(ty).into())),+ WasmCompositeInnerType::Struct(ty) => {+ Ok(Some(Arc::new(self.struct_layout(ty)?).into()))+ }+ WasmCompositeInnerType::Func(_) => Ok(None),WasmCompositeInnerType::Cont(_) => {unimplemented!("Stack switching feature not compatible with GC, yet")}- WasmCompositeInnerType::Exn(ty) => Some(Arc::new(self.exn_layout(ty)).into()),+ WasmCompositeInnerType::Exn(ty) => Ok(Some(Arc::new(self.exn_layout(ty)?).into())),}}@@ -261,10 +264,10 @@ pub trait GcTypeLayouts {fn array_layout(&self, ty: &WasmArrayType) -> GcArrayLayout;/// Get this collector's layout for the given struct type.- fn struct_layout(&self, ty: &WasmStructType) -> GcStructLayout;+ fn struct_layout(&self, ty: &WasmStructType) -> Result<GcStructLayout, OutOfMemory>;/// Get this collector's layout for the given exception type.- fn exn_layout(&self, ty: &WasmExnType) -> GcStructLayout;+ fn exn_layout(&self, ty: &WasmExnType) -> Result<GcStructLayout, OutOfMemory>;}/// The layout of a GC-managed object.diff --git a/crates/environ/src/gc/copying.rs b/crates/environ/src/gc/copying.rsindex ca4bd67552..2f98a70c2e 100644--- a/crates/environ/src/gc/copying.rs+++ b/crates/environ/src/gc/copying.rs@@ -63,8 +63,8 @@ impl GcTypeLayouts for CopyingTypeLayouts {layout}- fn struct_layout(&self, ty: &WasmStructType) -> GcStructLayout {- let mut layout = common_struct_layout(ty, HEADER_SIZE, ALIGN);+ fn struct_layout(&self, ty: &WasmStructType) -> Result<GcStructLayout, OutOfMemory> {+ let mut layout = common_struct_layout(ty, HEADER_SIZE, ALIGN)?;// Ensure there is always space for the forwarding reference, even if// the struct has no fields.if layout.size < MIN_OBJECT_SIZE {@@ -74,15 +74,15 @@ impl GcTypeLayouts for CopyingTypeLayouts {debug_assert!(layout.align <= ALIGN);layout.align = ALIGN;debug_assert!(layout.size >= MIN_OBJECT_SIZE);- layout+ Ok(layout)}- fn exn_layout(&self, ty: &WasmExnType) -> GcStructLayout {- let mut layout = common_exn_layout(ty, HEADER_SIZE, ALIGN);+ fn exn_layout(&self, ty: &WasmExnType) -> Result<GcStructLayout, OutOfMemory> {+ let mut layout = common_exn_layout(ty, HEADER_SIZE, ALIGN)?;layout.size = layout.size.next_multiple_of(ALIGN);debug_assert!(layout.align <= ALIGN);layout.align = ALIGN;debug_assert!(layout.size >= MIN_OBJECT_SIZE);- layout+ Ok(layout)}}diff --git a/crates/environ/src/gc/drc.rs b/crates/environ/src/gc/drc.rsindex ac1e6d5739..9f55750630 100644--- a/crates/environ/src/gc/drc.rs+++ b/crates/environ/src/gc/drc.rs@@ -47,11 +47,11 @@ impl GcTypeLayouts for DrcTypeLayouts {common_array_layout(ty, HEADER_SIZE, HEADER_ALIGN, ARRAY_LENGTH_OFFSET)}- fn struct_layout(&self, ty: &WasmStructType) -> GcStructLayout {+ fn struct_layout(&self, ty: &WasmStructType) -> Result<GcStructLayout, OutOfMemory> {common_struct_layout(ty, HEADER_SIZE, HEADER_ALIGN)}- fn exn_layout(&self, ty: &WasmExnType) -> GcStructLayout {+ fn exn_layout(&self, ty: &WasmExnType) -> Result<GcStructLayout, OutOfMemory> {common_exn_layout(ty, HEADER_SIZE, HEADER_ALIGN)}}diff --git a/crates/environ/src/gc/null.rs b/crates/environ/src/gc/null.rsindex c16b33668c..acdd9f56c4 100644--- a/crates/environ/src/gc/null.rs+++ b/crates/environ/src/gc/null.rs@@ -38,11 +38,11 @@ impl GcTypeLayouts for NullTypeLayouts {common_array_layout(ty, HEADER_SIZE, HEADER_ALIGN, ARRAY_LENGTH_OFFSET)}- fn struct_layout(&self, ty: &WasmStructType) -> GcStructLayout {+ fn struct_layout(&self, ty: &WasmStructType) -> Result<GcStructLayout, OutOfMemory> {common_struct_layout(ty, HEADER_SIZE, HEADER_ALIGN)}- fn exn_layout(&self, ty: &WasmExnType) -> GcStructLayout {+ fn exn_layout(&self, ty: &WasmExnType) -> Result<GcStructLayout, OutOfMemory> {common_exn_layout(ty, HEADER_SIZE, HEADER_ALIGN)}}diff --git a/crates/wasmtime/src/runtime/type_registry.rs b/crates/wasmtime/src/runtime/type_registry.rsindex d8bc147387..da4e76cdb2 100644--- a/crates/wasmtime/src/runtime/type_registry.rs+++ b/crates/wasmtime/src/runtime/type_registry.rs@@ -1152,10 +1152,10 @@ impl TypeRegistryInner {gc_runtime.layouts().array_layout(a).into()}wasmtime_environ::WasmCompositeInnerType::Struct(s) => {- try_new::<Arc<_>>(gc_runtime.layouts().struct_layout(s))?.into()+ try_new::<Arc<_>>(gc_runtime.layouts().struct_layout(s)?)?.into()}wasmtime_environ::WasmCompositeInnerType::Exn(e) => {- try_new::<Arc<_>>(gc_runtime.layouts().exn_layout(e))?.into()+ try_new::<Arc<_>>(gc_runtime.layouts().exn_layout(e)?)?.into()}wasmtime_environ::WasmCompositeInnerType::Cont(_) => continue, // FIXME: #10248 stack switching support.};