Skip to content

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

Introduce `wasmtime_environ::collections::{OomArc, OomBox}` (#12361)

Nick Fitzgeralddaee7fda2838
diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index 677c71ca9b..9a4c55b76f 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -635,6 +635,11 @@ jobs:
env:
RUSTFLAGS: "--cfg pulley_assume_llvm_makes_tail_calls"
+ # Run OOM tests with `Arc::try_new` usage enabled.
+ - run: cargo test -p wasmtime-fuzzing --test oom
+ env:
+ RUSTFLAGS: "--cfg arc_try_new"
+
# Ensure that fuzzers still build.
#
# Install the OCaml packages necessary for fuzz targets that use the
diff --git a/Cargo.lock b/Cargo.lock
index c500a30a27..238789180c 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -4794,6 +4794,7 @@ dependencies = [
"wasmprinter",
"wasmtime",
"wasmtime-cli-flags",
+ "wasmtime-environ",
"wasmtime-internal-error",
"wasmtime-test-util",
"wasmtime-wasi",
diff --git a/crates/environ/build.rs b/crates/environ/build.rs
new file mode 100644
index 0000000000..554f052d72
--- /dev/null
+++ b/crates/environ/build.rs
@@ -0,0 +1,4 @@
+fn main() {
+ println!("cargo:rerun-if-changed=build.rs");
+ println!("cargo:rustc-check-cfg=cfg(arc_try_new)");
+}
diff --git a/crates/environ/src/collections.rs b/crates/environ/src/collections.rs
new file mode 100644
index 0000000000..af750772b5
--- /dev/null
+++ b/crates/environ/src/collections.rs
@@ -0,0 +1,120 @@
+//! Fallible, OOM-handling collections.
+
+use crate::error::OutOfMemory;
+use alloc::{boxed::Box, sync::Arc};
+use core::{alloc::Layout, mem::MaybeUninit, ptr::NonNull};
+
+/// Helper function to invoke `<T as TryNew>::try_new`.
+///
+/// # Example
+///
+/// ```
+/// # use wasmtime_environ::prelude::*;
+/// # fn _foo() -> Result<()> {
+/// use wasmtime_environ::collections::try_new;
+///
+/// let boxed = try_new::<Box<u32>>(36)?;
+/// assert_eq!(*boxed, 36);
+/// # Ok(())
+/// # }
+/// ```
+#[inline]
+pub fn try_new<T>(value: T::Value) -> Result<T, OutOfMemory>
+where
+ T: TryNew,
+{
+ TryNew::try_new(value)
+}
+
+/// Extension trait providing fallible allocation for types like `Arc<T>` and
+/// `Box<T>.
+pub trait TryNew {
+ /// The inner `T` type that is getting wrapped into an `Arc<T>` or `Box<T>`.
+ type Value;
+
+ /// Allocate a new `Self`, returning `Err(OutOfMemory)` on allocation
+ /// failure.
+ fn try_new(value: Self::Value) -> Result<Self, OutOfMemory>
+ where
+ Self: Sized;
+}
+
+/// Try to allocate a block of memory that fits the given layout, or return an
+/// `OutOfMemory` error.
+///
+/// # Safety
+///
+/// Same as `alloc::alloc::alloc`: layout must have non-zero size.
+#[inline]
+unsafe fn try_alloc(layout: Layout) -> Result<NonNull<u8>, OutOfMemory> {
+ // Safety: same as our safety conditions.
+ debug_assert!(layout.size() > 0);
+ let ptr = unsafe { alloc::alloc::alloc(layout) };
+
+ if let Some(ptr) = NonNull::new(ptr) {
+ Ok(ptr)
+ } else {
+ Err(OutOfMemory::new(layout.size()))
+ }
+}
+
+/// Allocate an `Box<MaybeUninit<T>>` with uninitialized contents, returning
+/// `Err(OutOfMemory)` on allocation failure.
+///
+/// You can initialize the resulting box's value via [`Box::write`].
+#[inline]
+fn new_uninit_box<T>() -> Result<Box<MaybeUninit<T>>, OutOfMemory> {
+ let layout = alloc::alloc::Layout::new::<MaybeUninit<T>>();
+
+ if layout.size() == 0 {
+ // NB: no actual allocation takes place when boxing zero-sized
+ // types.
+ return Ok(Box::new(MaybeUninit::uninit()));
+ }
+
+ // Safety: layout size is non-zero.
+ let ptr = unsafe { try_alloc(layout)? };
+
+ let ptr = ptr.cast::<MaybeUninit<T>>();
+
+ // Safety: The pointer's memory block was allocated by the global allocator.
+ Ok(unsafe { Box::from_raw(ptr.as_ptr()) })
+}
+
+impl<T> TryNew for Box<T> {
+ type Value = T;
+
+ #[inline]
+ fn try_new(value: T) -> Result<Self, OutOfMemory>
+ where
+ Self: Sized,
+ {
+ let boxed = new_uninit_box::<T>()?;
+ Ok(Box::write(boxed, value))
+ }
+}
+
+/// XXX: Stable Rust doesn't actually give us any method to build fallible
+/// allocation for `Arc<T>`, so this is only actually fallible when using
+/// nightly Rust and setting `RUSTFLAGS="--cfg arc_try_new"`.
+impl<T> TryNew for Arc<T> {
+ type Value = T;
+
+ #[inline]
+ fn try_new(value: T) -> Result<Self, OutOfMemory>
+ where
+ Self: Sized,
+ {
+ #[cfg(arc_try_new)]
+ return Arc::try_new(value).map_err(|_| {
+ // We don't have access to the exact size of the inner `Arc`
+ // allocation, but (at least at one point) it was made up of a
+ // strong ref count, a weak ref count, and the inner value.
+ let bytes = core::mem::size_of::<(usize, usize, T)>();
+ OutOfMemory::new(bytes)
+ });
+
+ #[cfg(not(arc_try_new))]
+ return Ok(Arc::new(value));
+ }
+}
diff --git a/crates/environ/src/lib.rs b/crates/environ/src/lib.rs
index ea552f80ff..0c3e155352 100644
--- a/crates/environ/src/lib.rs
+++ b/crates/environ/src/lib.rs
@@ -10,12 +10,15 @@
#![deny(missing_docs)]
#![warn(clippy::cast_sign_loss)]
#![no_std]
+#![cfg_attr(arc_try_new, allow(unstable_features))]
+#![cfg_attr(arc_try_new, feature(allocator_api))]
#[cfg(feature = "std")]
#[macro_use]
extern crate std;
extern crate alloc;
+pub mod collections;
pub mod prelude;
mod address_map;
diff --git a/crates/environ/src/prelude.rs b/crates/environ/src/prelude.rs
index a71fc8d7f3..3f9884ca86 100644
--- a/crates/environ/src/prelude.rs
+++ b/crates/environ/src/prelude.rs
@@ -19,6 +19,7 @@
//!
//! and then `use crate::*` works as usual.
+pub use crate::collections::{TryNew, try_new};
pub use crate::error::{Context, Error, Result, bail, ensure, format_err};
pub use alloc::borrow::ToOwned;
pub use alloc::boxed::Box;
diff --git a/crates/fuzzing/Cargo.toml b/crates/fuzzing/Cargo.toml
index d688e027c4..94984817f1 100644
--- a/crates/fuzzing/Cargo.toml
+++ b/crates/fuzzing/Cargo.toml
@@ -70,6 +70,7 @@ v8 = "139.0.0"
[dev-dependencies]
wat = { workspace = true }
rand = { version = "0.8.0", features = ["small_rng"] }
+wasmtime-environ = { workspace = true }
# Only enable the `build-libinterpret` feature when fuzzing is enabled, enabling
# commands like `cargo test --workspace` or similar to not need an ocaml
diff --git a/crates/fuzzing/build.rs b/crates/fuzzing/build.rs
index 19fb491cbe..ecad2fa00a 100644
--- a/crates/fuzzing/build.rs
+++ b/crates/fuzzing/build.rs
@@ -8,6 +8,7 @@ use wasmtime_test_util::wast::WastTest;
fn main() {
println!("cargo:rerun-if-changed=build.rs");
+ println!("cargo:rustc-check-cfg=cfg(arc_try_new)");
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
diff --git a/crates/fuzzing/tests/oom.rs b/crates/fuzzing/tests/oom.rs
index b1dbaea4d9..784c5a4d17 100644
--- a/crates/fuzzing/tests/oom.rs
+++ b/crates/fuzzing/tests/oom.rs
@@ -3,8 +3,8 @@ use std::{
fmt::{self, Write},
sync::atomic::{AtomicU32, Ordering::SeqCst},
};
-use wasmtime::Config;
-use wasmtime_error::{Error, OutOfMemory, Result, format_err};
+use wasmtime::{Config, Error, Result, error::OutOfMemory, format_err};
+use wasmtime_environ::collections::*;
use wasmtime_fuzzing::oom::{OomTest, OomTestAllocator};
#[global_allocator]
@@ -31,6 +31,25 @@ fn smoke_test_missed_oom() -> Result<()> {
Ok(())
}
+#[test]
+#[cfg(arc_try_new)]
+fn try_new_arc() -> Result<()> {
+ use std::sync::Arc;
+
+ OomTest::new().test(|| {
+ let _arc = try_new::<Arc<u32>>(42)?;
+ Ok(())
+ })
+}
+
+#[test]
+fn try_new_box() -> Result<()> {
+ OomTest::new().test(|| {
+ let _box = try_new::<Box<u32>>(36)?;
+ Ok(())
+ })
+}
+
#[test]
fn config_new() -> Result<()> {
OomTest::new().test(|| {