A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Add `PanicOnOom` extension trait (#12461)
Nick Fitzgerald4c2e3d298d52
diff --git a/crates/core/src/alloc.rs b/crates/core/src/alloc.rsindex 99751857f9..13fdea2177 100644--- a/crates/core/src/alloc.rs+++ b/crates/core/src/alloc.rs@@ -34,3 +34,28 @@ unsafe fn try_alloc(layout: Layout) -> Result<NonNull<u8>, OutOfMemory> {Err(OutOfMemory::new(layout.size()))}}++/// An extension trait for ignoring `OutOfMemory` errors.+///+/// Use this to unwrap a `Result<T, OutOfMemory>` into its inner `T` or+/// otherwise panic, leveraging the type system to be sure that you aren't ever+/// accidentally unwrapping non-`OutOfMemory` errors.+pub trait PanicOnOom {+ /// The non-`OutOfMemory` result of calling `panic_on_oom`.+ type Result;++ /// Panic on `OutOfMemory` errors, returning the non-`OutOfMemory` result.+ fn panic_on_oom(self) -> Self::Result;+}++impl<T> PanicOnOom for Result<T, OutOfMemory> {+ type Result = T;++ #[track_caller]+ fn panic_on_oom(self) -> Self::Result {+ match self {+ Ok(x) => x,+ Err(oom) => panic!("unhandled out-of-memory error: {oom}"),+ }+ }+}diff --git a/crates/wasmtime/src/runtime/type_registry.rs b/crates/wasmtime/src/runtime/type_registry.rsindex 080dc70e82..d0cd39d552 100644--- a/crates/wasmtime/src/runtime/type_registry.rs+++ b/crates/wasmtime/src/runtime/type_registry.rs@@ -21,6 +21,7 @@ use core::{Ordering::{AcqRel, Acquire, Release},},};+use wasmtime_core::alloc::PanicOnOom;use wasmtime_core::slab::{Id as SlabId, Slab};use wasmtime_environ::{EngineOrModuleTypeIndex, GcLayout, ModuleInternedTypeIndex, ModuleTypes, PrimaryMap,@@ -743,7 +744,7 @@ impl TypeRegistryInner {// `?`-propagate an error when we have applied partial side effects.//// TODO(#12069): handle allocation failure here.- self.types.reserve(non_canon_types.len()).unwrap();+ self.types.reserve(non_canon_types.len()).panic_on_oom();// Inter-group edges: increment the referenced group's ref// count, because these other rec groups shouldn't be droppeddiff --git a/crates/wasmtime/src/runtime/vm/gc/func_ref.rs b/crates/wasmtime/src/runtime/vm/gc/func_ref.rsindex e3eb2214a8..4635a5d015 100644--- a/crates/wasmtime/src/runtime/vm/gc/func_ref.rs+++ b/crates/wasmtime/src/runtime/vm/gc/func_ref.rs@@ -13,7 +13,10 @@ use crate::{type_registry::TypeRegistry,vm::{SendSyncPtr, VMFuncRef},};-use wasmtime_core::slab::{Id, Slab};+use wasmtime_core::{+ alloc::PanicOnOom,+ slab::{Id, Slab},+};use wasmtime_environ::VMSharedTypeIndex;/// An identifier into the `FuncRefTable`.@@ -52,7 +55,7 @@ impl FuncRefTable {pub unsafe fn intern(&mut self, func_ref: Option<SendSyncPtr<VMFuncRef>>) -> FuncRefTableId {*self.interned.entry(func_ref).or_insert_with(|| {// TODO(#12069): handle allocation failure here- FuncRefTableId(self.slab.alloc(func_ref).unwrap())+ FuncRefTableId(self.slab.alloc(func_ref).panic_on_oom())})}diff --git a/crates/wasmtime/src/runtime/vm/gc/host_data.rs b/crates/wasmtime/src/runtime/vm/gc/host_data.rsindex d330b84f4d..570cf6b15a 100644--- a/crates/wasmtime/src/runtime/vm/gc/host_data.rs+++ b/crates/wasmtime/src/runtime/vm/gc/host_data.rs@@ -15,7 +15,10 @@use crate::prelude::*;use core::any::Any;-use wasmtime_core::slab::{Id, Slab};+use wasmtime_core::{+ alloc::PanicOnOom,+ slab::{Id, Slab},+};/// Side table for each `externref`'s host data value.#[derive(Default)]@@ -40,7 +43,7 @@ impl ExternRefHostDataTable {/// Allocate a new `externref` host data value.pub fn alloc(&mut self, value: Box<dyn Any + Send + Sync>) -> ExternRefHostDataId {// TODO(#12069): handle allocation failure here- let id = self.slab.alloc(value).unwrap();+ let id = self.slab.alloc(value).panic_on_oom();let id = ExternRefHostDataId(id);log::trace!("allocated new externref host data: {id:?}");iddiff --git a/crates/wasmtime/src/runtime/vm/traphandlers.rs b/crates/wasmtime/src/runtime/vm/traphandlers.rsindex aad7149e00..c3f44de86a 100644--- a/crates/wasmtime/src/runtime/vm/traphandlers.rs+++ b/crates/wasmtime/src/runtime/vm/traphandlers.rs@@ -833,12 +833,14 @@ impl CallThreadState {let result = match &unwind {#[cfg(feature = "gc")]UnwindState::UnwindToWasm(_) => {+ use wasmtime_core::alloc::PanicOnOom;+assert!(store.as_store_opaque().has_pending_exception());let exn = store.as_store_opaque().pending_exception_owned_rooted()// TODO(#12069): handle allocation failure here- .unwrap()+ .panic_on_oom().expect("exception should be set when we are throwing");store.block_on_debug_handler(crate::DebugEvent::CaughtExceptionThrown(exn))}@@ -847,11 +849,13 @@ impl CallThreadState {reason: UnwindReason::Trap(TrapReason::Exception),..} => {+ use wasmtime_core::alloc::PanicOnOom;+let exn = store.as_store_opaque().pending_exception_owned_rooted()// TODO(#12069): handle allocation failure here- .unwrap()+ .panic_on_oom().expect("exception should be set when we are throwing");store.block_on_debug_handler(crate::DebugEvent::UncaughtExceptionThrown(exn.clone(),