A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Track attempted allocation sizes in `OutOfMemory` errors (#12351)
Nick Fitzgerald2e23e223c035
diff --git a/crates/error/src/boxed.rs b/crates/error/src/boxed.rsindex a3c215e8c9..92124fcda5 100644--- a/crates/error/src/boxed.rs+++ b/crates/error/src/boxed.rs@@ -19,7 +19,9 @@ pub(crate) unsafe fn try_alloc(layout: Layout) -> Result<NonNull<u8>, OutOfMemorif let Some(ptr) = NonNull::new(ptr) {Ok(ptr)} else {- out_of_line_slow_path!(Err(OutOfMemory::new()))+ out_of_line_slow_path! {+ Err(OutOfMemory::new(layout.size()))+ }}}diff --git a/crates/error/src/error.rs b/crates/error/src/error.rsindex db3eb0c00f..7346fbbfd1 100644--- a/crates/error/src/error.rs+++ b/crates/error/src/error.rs@@ -592,9 +592,17 @@ impl Error {E: core::error::Error + Send + Sync + 'static,{if TypeId::of::<E>() == TypeId::of::<OutOfMemory>() {- return Error {- inner: OutOfMemory::new().into(),- };+ // Although we know that `E == OutOfMemory` in this block, the+ // compiler doesn't understand that, and we have to do this litle+ // dance.+ union ToOom<T> {+ oom: OutOfMemory,+ error: mem::ManuallyDrop<T>,+ }+ let error = mem::ManuallyDrop::new(error);+ // Safety: `E == OutOfMemory`.+ let oom = unsafe { (ToOom { error }).oom };+ return Error { inner: oom.into() };}Self::from_error_ext(ForeignError(error))@@ -856,7 +864,7 @@ impl Error {/// # use wasmtime_internal_error as wasmtime;/// use wasmtime::{Error, OutOfMemory};///- /// let oom = Error::from(OutOfMemory::new());+ /// let oom = Error::from(OutOfMemory::new(1234));/// assert!(oom.is::<OutOfMemory>());/// assert!(!oom.is::<std::num::TryFromIntError>());///@@ -1147,7 +1155,32 @@ impl Error {/// ```#[inline]pub fn into_boxed_dyn_error(self) -> Box<dyn core::error::Error + Send + Sync + 'static> {- self.inner.into_boxed_dyn_core_error()+ /// A specialized OOM error that is zero-sized, so that it can always be+ /// boxed without allocation, and we can keep this method infallible (to+ /// match `anyhow`'s API).+ #[derive(Debug)]+ struct IntoBoxedDynCoreErrorFailure;++ impl core::fmt::Display for IntoBoxedDynCoreErrorFailure {+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {+ write!(+ f,+ "failed to box error into `Box<dyn core::error::Error>` \+ (allocation of {} bytes failed)",+ mem::size_of::<Error>()+ )+ }+ }++ impl core::error::Error for IntoBoxedDynCoreErrorFailure {}++ match self.inner.into_boxed_dyn_core_error() {+ Ok(boxed) => boxed,+ Err(_oom) => {+ // NB: `Box::new` will never actually allocate for zero-sized types.+ Box::new(IntoBoxedDynCoreErrorFailure) as _+ }+ }}}@@ -1582,7 +1615,7 @@ impl Drop for OomOrDynError {impl From<BoxedDynError> for OomOrDynError {fn from(boxed: BoxedDynError) -> Self {let inner = boxed.into_owned_ptr().into_non_null().cast::<u8>();- debug_assert_ne!(inner, Self::OOM.inner);+ debug_assert!(!Self::is_oom_ptr(inner));OomOrDynError { inner }}}@@ -1590,36 +1623,55 @@ impl From<BoxedDynError> for OomOrDynError {impl OomOrDynError {const _SIZE: () = assert!(mem::size_of::<OomOrDynError>() == mem::size_of::<usize>());- // Our pointer tagging relies on this property.+ /// Our pointer tagging relies on this property, which implies that+ /// `Self::OOM_BIT` is never set for any `*mut DynError` pointer.const _DYN_ERROR_HAS_GREATER_ALIGN_THAN_OOM: () = assert!(mem::align_of::<DynError>() > 1);- const OOM_REPR: NonNull<u8> = unsafe {- // Safety: `0x1` is not null.- NonNull::<u8>::new_unchecked(0x1 as *mut u8)- };+ /// If this bit is set in the inner pointer's address, then it is a bitpacked+ /// `OutOfMemory` rather than a pointer to a boxed dyn error.+ const OOM_BIT: usize = 0x1;++ pub(crate) const fn new_oom_ptr(size: usize) -> NonNull<u8> {+ let size = if size > (isize::MAX as usize) {+ isize::MAX as usize+ } else {+ size+ };+ let repr = (size << 1) | Self::OOM_BIT;+ let inner = core::ptr::without_provenance_mut(repr);+ NonNull::new(inner).unwrap()+ }++ pub(crate) fn new_oom(bitpacked: NonNull<u8>) -> Self {+ assert!(Self::is_oom_ptr(bitpacked));+ OomOrDynError { inner: bitpacked }+ }- pub(crate) const OOM: Self = OomOrDynError {- inner: Self::OOM_REPR,- };+ fn is_oom_ptr(ptr: NonNull<u8>) -> bool {+ (ptr.addr().get() & Self::OOM_BIT) == Self::OOM_BIT+ }fn is_oom(&self) -> bool {- self.inner == Self::OOM_REPR+ Self::is_oom_ptr(self.inner)}fn is_boxed_dyn_error(&self) -> bool {!self.is_oom()}+ pub(crate) fn oom_size(inner: NonNull<u8>) -> usize {+ debug_assert!(Self::is_oom_ptr(inner));+ inner.addr().get() >> 1+ }+/// # Safety////// `self.is_oom()` must be true.unsafe fn unchecked_oom(&self) -> &OutOfMemory {debug_assert!(self.is_oom());- let dangling = NonNull::<OutOfMemory>::dangling();- debug_assert_eq!(mem::size_of::<OutOfMemory>(), 0);- // Safety: it is always valid to turn `T`'s dangling pointer into an- // `&T` reference for unit types.- unsafe { dangling.as_ref() }+ // Safety: `self.is_oom()` and `OutOfMemory` has the same representation+ // as `Self`.+ unsafe { mem::transmute(self) }}/// # Safety@@ -1627,11 +1679,9 @@ impl OomOrDynError {/// `self.is_oom()` must be true.unsafe fn unchecked_oom_mut(&mut self) -> &mut OutOfMemory {debug_assert!(self.is_oom());- let mut dangling = NonNull::<OutOfMemory>::dangling();- debug_assert_eq!(mem::size_of::<OutOfMemory>(), 0);- // Safety: it is always valid to turn `T`'s dangling pointer into an- // `&mut T` reference for unit types.- unsafe { dangling.as_mut() }+ // Safety: `self.is_oom()` and `OutOfMemory` has the same representation+ // as `Self`.+ unsafe { mem::transmute(self) }}/// # Safety@@ -1684,15 +1734,12 @@ impl OomOrDynError {pub(crate) fn into_boxed_dyn_core_error(self,- ) -> Box<dyn core::error::Error + Send + Sync + 'static> {- let box_dyn_error_of_oom = || {- // NB: `Box::new` will never actually allocate for zero-sized types- // like `OutOfMemory`.- Box::new(OutOfMemory::new()) as _- };-+ ) -> Result<Box<dyn core::error::Error + Send + Sync + 'static>, OutOfMemory> {if self.is_oom() {- box_dyn_error_of_oom()+ let boxed = try_new_uninit_box::<OutOfMemory>()?;+ // Safety: `self.is_oom()` is true.+ let boxed = Box::write(boxed, unsafe { *self.unchecked_oom() });+ Ok(boxed as _)} else {debug_assert!(self.is_boxed_dyn_error());// Safety: this is a boxed dyn error.@@ -1701,10 +1748,7 @@ impl OomOrDynError {let vtable = unsafe { ptr.as_ref().vtable };// Safety: the pointer is valid and the vtable is associated with// this pointer's concrete error type.- match unsafe { (vtable.into_boxed_dyn_core_error)(ptr) } {- Ok(e) => e,- Err(_oom) => box_dyn_error_of_oom(),- }+ unsafe { (vtable.into_boxed_dyn_core_error)(ptr) }}}@@ -1813,7 +1857,7 @@ mod tests {#[test]fn from_oom() {- let mut error = Error::from(OutOfMemory::new());+ let mut error = Error::from(OutOfMemory::new(5));assert!(error.is::<OutOfMemory>());assert!(error.downcast_ref::<OutOfMemory>().is_some());assert!(error.downcast_mut::<OutOfMemory>().is_some());diff --git a/crates/error/src/macros.rs b/crates/error/src/macros.rsindex 848b6080d0..3d4c789783 100644--- a/crates/error/src/macros.rs+++ b/crates/error/src/macros.rs@@ -334,7 +334,7 @@ pub mod formatting {Ok(())}Err(_) => {- self.oom = Some(OutOfMemory::new());+ self.oom = Some(OutOfMemory::new(self.message.len() + s.len()));Err(fmt::Error)}}diff --git a/crates/error/src/oom.rs b/crates/error/src/oom.rsindex 27364b8427..1e865f68e6 100644--- a/crates/error/src/oom.rs+++ b/crates/error/src/oom.rs@@ -1,5 +1,5 @@-use super::OomOrDynError;-use core::fmt;+use crate::error::OomOrDynError;+use core::{fmt, mem, ptr::NonNull};/// Out-of-memory error.///@@ -11,20 +11,40 @@ use core::fmt;/// Allocation failure inside any `Error` method that must allocate/// (e.g. [`Error::context`][crate::Error::context]) will propagate an/// `OutOfMemory` error.-#[derive(Clone, Copy, Default)]+#[derive(Clone, Copy)]+// NB: `OutOfMemory`'s representation must be the same as `OomOrDynError`+// (and therefore also `Error`).+#[repr(transparent)]pub struct OutOfMemory {- _private: (),+ inner: NonNull<u8>,}+// Safety: The `inner` pointer is not a real pointer, it is just bitpacked size+// data.+unsafe impl Send for OutOfMemory {}++// Safety: The `inner` pointer is not a real pointer, it is just bitpacked size+// data.+unsafe impl Sync for OutOfMemory {}+impl fmt::Debug for OutOfMemory {fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {- f.write_str("OutOfMemory")+ f.debug_struct("OutOfMemory")+ .field(+ "requested_allocation_size",+ &self.requested_allocation_size(),+ )+ .finish()}}impl fmt::Display for OutOfMemory {fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {- f.write_str("out of memory")+ write!(+ f,+ "out of memory (failed to allocate {} bytes)",+ self.requested_allocation_size()+ )}}@@ -36,17 +56,80 @@ impl core::error::Error for OutOfMemory {}impl OutOfMemory {+ // NB: `OutOfMemory`'s representation must be the same as `OomOrDynError`+ // (and therefore also `Error`).+ const _SAME_SIZE_AS_OOM_OR_DYN_ERROR: () =+ assert!(mem::size_of::<OutOfMemory>() == mem::size_of::<OomOrDynError>());+ const _SAME_ALIGN_AS_OOM_OR_DYN_ERROR: () =+ assert!(mem::align_of::<OutOfMemory>() == mem::align_of::<OomOrDynError>());+ const _SAME_SIZE_AS_ERROR: () =+ assert!(mem::size_of::<OutOfMemory>() == mem::size_of::<crate::Error>());+ const _SAME_ALIGN_AS_ERROR: () =+ assert!(mem::align_of::<OutOfMemory>() == mem::align_of::<crate::Error>());+/// Construct a new `OutOfMemory` error.///+ /// The `requested_allocation_size` argument should be the size (in bytes)+ /// of the associated allocation that was attempted and failed.+ ////// This operation does not allocate.+ ///+ /// # Example+ ///+ /// ```rust+ /// # use wasmtime_internal_error::OutOfMemory;+ /// # extern crate alloc;+ /// use alloc::alloc::{Layout, alloc};+ /// use core::ptr::NonNull;+ ///+ /// /// Attempt to allocate a block of memory from the global allocator,+ /// /// returning an `OutOfMemory` error on failure.+ /// fn try_global_alloc(layout: Layout) -> Result<NonNull<u8>, OutOfMemory> {+ /// if layout.size() == 0 {+ /// return Ok(NonNull::dangling());+ /// }+ ///+ /// // Safety: the layout's size is non-zero.+ /// let ptr = unsafe { alloc(layout) };+ ///+ /// if let Some(ptr) = NonNull::new(ptr) {+ /// Ok(ptr)+ /// } else {+ /// // The allocation failed, so return an `OutOfMemory` error,+ /// // passing the attempted allocation's size into the `OutOfMemory`+ /// // constructor.+ /// Err(OutOfMemory::new(layout.size()))+ /// }+ /// }+ /// ```+ #[inline]+ pub const fn new(requested_allocation_size: usize) -> Self {+ Self {+ inner: OomOrDynError::new_oom_ptr(requested_allocation_size),+ }+ }++ /// Get the size (in bytes) of the associated allocation that was attempted+ /// and which failed.+ ///+ /// Very large allocation sizes (near `isize::MAX` and larger) may be capped+ /// to a maximum value.+ ///+ /// # Example+ ///+ /// ```rust+ /// # use wasmtime_internal_error::OutOfMemory;+ /// let oom = OutOfMemory::new(8192);+ /// assert_eq!(oom.requested_allocation_size(), 8192);+ /// ```#[inline]- pub const fn new() -> Self {- Self { _private: () }+ pub fn requested_allocation_size(&self) -> usize {+ OomOrDynError::oom_size(self.inner)}}impl From<OutOfMemory> for OomOrDynError {- fn from(OutOfMemory { _private: () }: OutOfMemory) -> Self {- OomOrDynError::OOM+ fn from(oom: OutOfMemory) -> Self {+ OomOrDynError::new_oom(oom.inner)}}diff --git a/crates/error/tests/tests.rs b/crates/error/tests/tests.rsindex 587a146ec1..b8f73928b0 100644--- a/crates/error/tests/tests.rs+++ b/crates/error/tests/tests.rs@@ -194,7 +194,7 @@ fn is() {assert!(!e.is::<OutOfMemory>());// `is<T>` is true for `Error::from(OutOfMemory)`.- let e = Error::from(OutOfMemory::new());+ let e = Error::from(OutOfMemory::new(5));assert!(e.is::<OutOfMemory>());assert!(!e.is::<TestError>());assert!(!e.is::<&str>());@@ -212,7 +212,7 @@ fn is_for_root_cause_with_initial_error_source() {#[cfg(feature = "backtrace")]fn backtrace() {// Backtrace on OOM.- let e = Error::from(OutOfMemory::new());+ let e = Error::from(OutOfMemory::new(5));assert_eq!(e.backtrace().status(), BacktraceStatus::Disabled);let backtraces_enabled =@@ -425,7 +425,7 @@ fn downcast() {assert_eq!(error.downcast::<TestError>().unwrap().0, 42);// Error::from(oom)- let error = Error::from(OutOfMemory::new());+ let error = Error::from(OutOfMemory::new(5));let error = error.downcast::<&str>().unwrap_err();let error = error.downcast::<TestError>().unwrap_err();assert!(error.downcast::<OutOfMemory>().is_ok());@@ -433,21 +433,21 @@ fn downcast() {// First in context chain.let error = Error::new(TestError(42)).context("yikes")- .context(OutOfMemory::new());+ .context(OutOfMemory::new(5));let error = error.downcast::<String>().unwrap_err();assert!(error.downcast::<OutOfMemory>().is_ok());// Middle in context chain.let error = Error::new(TestError(42)).context("yikes")- .context(OutOfMemory::new());+ .context(OutOfMemory::new(5));let error = error.downcast::<String>().unwrap_err();assert_eq!(error.downcast::<&str>().unwrap(), "yikes");// Last in context chain.let error = Error::new(TestError(42)).context("yikes")- .context(OutOfMemory::new());+ .context(OutOfMemory::new(5));let error = error.downcast::<String>().unwrap_err();assert_eq!(error.downcast::<TestError>().unwrap().0, 42);@@ -508,7 +508,7 @@ fn downcast_ref() {assert!(e.downcast_ref::<OutOfMemory>().is_none());// `Error::from(OutOfMemory)`- let e = Error::from(OutOfMemory::new());+ let e = Error::from(OutOfMemory::new(5));assert!(e.downcast_ref::<OutOfMemory>().is_some());assert!(e.downcast_ref::<TestError>().is_none());assert!(e.downcast_ref::<&str>().is_none());@@ -545,7 +545,7 @@ fn downcast_mut() {assert_eq!(e.downcast_ref::<TestError>().unwrap().0, 37);// `Error::from(OutOfMemory)`- let mut e = Error::from(OutOfMemory::new());+ let mut e = Error::from(OutOfMemory::new(5));assert!(e.downcast_mut::<OutOfMemory>().is_some());assert!(e.downcast_mut::<TestError>().is_none());assert!(e.downcast_mut::<&str>().is_none());@@ -553,7 +553,7 @@ fn downcast_mut() {#[test]fn context_on_oom() {- let error = Error::new(OutOfMemory::new());+ let error = Error::new(OutOfMemory::new(5));let error = error.context("yikes");assert!(error.is::<OutOfMemory>());assert!(@@ -745,7 +745,7 @@ Error {#[test]fn fmt_debug_alternate_with_oom() {- let error = Error::new(OutOfMemory::new());+ let error = Error::new(OutOfMemory::new(5));let actual = format!("{error:#?}");let actual = actual.trim();@@ -754,7 +754,9 @@ fn fmt_debug_alternate_with_oom() {let expected = r#"Error {inner: Oom(- OutOfMemory,+ OutOfMemory {+ requested_allocation_size: 5,+ },),}"#@@ -878,3 +880,26 @@ fn chain_with_leaf_sources() {assert!(chain.next().is_none());}++#[test]+fn oom_requested_allocation_size() {+ // Check that a bunch of interesting allocation sizes roundtrip.+ for shift in 0..30 {+ let bytes = (1 << shift) - 1;+ let oom = OutOfMemory::new(bytes);+ assert_eq!(oom.requested_allocation_size(), bytes);++ let bytes = 1 << shift;+ let oom = OutOfMemory::new(bytes);+ assert_eq!(oom.requested_allocation_size(), bytes);++ let bytes = (1 << shift) + 1;+ let oom = OutOfMemory::new(bytes);+ assert_eq!(oom.requested_allocation_size(), bytes);+ }++ // We will round down if the allocation size is too large, but make no+ // specific guarantees about that behavior.+ let oom = OutOfMemory::new(usize::MAX);+ assert!(oom.requested_allocation_size() <= usize::try_from(isize::MAX).unwrap());+}