Skip to content

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

Handle OOM in `Engine::new` (#12362)

Nick Fitzgeralda05256911e83
diff --git a/crates/fuzzing/tests/oom.rs b/crates/fuzzing/tests/oom.rs
index 784c5a4d17..b5ddb62905 100644
--- a/crates/fuzzing/tests/oom.rs
+++ b/crates/fuzzing/tests/oom.rs
@@ -3,7 +3,7 @@ use std::{
fmt::{self, Write},
sync::atomic::{AtomicU32, Ordering::SeqCst},
};
-use wasmtime::{Config, Error, Result, error::OutOfMemory, format_err};
+use wasmtime::{error::OutOfMemory, *};
use wasmtime_environ::collections::*;
use wasmtime_fuzzing::oom::{OomTest, OomTestAllocator};
@@ -59,6 +59,17 @@ fn config_new() -> Result<()> {
})
}
+#[test]
+#[cfg(arc_try_new)]
+fn engine_new() -> Result<()> {
+ OomTest::new().test(|| {
+ let mut config = Config::new();
+ config.enable_compiler(false);
+ let _ = Engine::new(&config)?;
+ Ok(())
+ })
+}
+
fn ok_if_not_oom(error: Error) -> Result<()> {
if error.is::<OutOfMemory>() {
Err(error)
diff --git a/crates/wasmtime/src/config.rs b/crates/wasmtime/src/config.rs
index ac68506481..0b868f9a90 100644
--- a/crates/wasmtime/src/config.rs
+++ b/crates/wasmtime/src/config.rs
@@ -2540,25 +2540,26 @@ impl Config {
match &self.allocation_strategy {
InstanceAllocationStrategy::OnDemand => {
- let mut _allocator = Box::new(OnDemandInstanceAllocator::new(
+ let mut _allocator = try_new::<Box<_>>(OnDemandInstanceAllocator::new(
self.mem_creator.clone(),
stack_size,
stack_zeroing,
- ));
+ ))?;
#[cfg(feature = "async")]
if let Some(stack_creator) = &self.stack_creator {
_allocator.set_stack_creator(stack_creator.clone());
}
- Ok(_allocator)
+ Ok(_allocator as _)
}
#[cfg(feature = "pooling-allocator")]
InstanceAllocationStrategy::Pooling(config) => {
let mut config = config.config;
config.stack_size = stack_size;
config.async_stack_zeroing = stack_zeroing;
- Ok(Box::new(crate::runtime::vm::PoolingInstanceAllocator::new(
- &config, tunables,
- )?))
+ let allocator = try_new::<Box<_>>(
+ crate::runtime::vm::PoolingInstanceAllocator::new(&config, tunables)?,
+ )?;
+ Ok(allocator as _)
}
}
}
@@ -2581,14 +2582,14 @@ impl Config {
Ok(Some(match self.collector.try_not_auto()? {
#[cfg(feature = "gc-drc")]
Collector::DeferredReferenceCounting => {
- Arc::new(crate::runtime::vm::DrcCollector::default()) as Arc<dyn GcRuntime>
+ try_new::<Arc<_>>(crate::runtime::vm::DrcCollector::default())? as _
}
#[cfg(not(feature = "gc-drc"))]
Collector::DeferredReferenceCounting => unreachable!(),
#[cfg(feature = "gc-null")]
Collector::Null => {
- Arc::new(crate::runtime::vm::NullCollector::default()) as Arc<dyn GcRuntime>
+ try_new::<Arc<_>>(crate::runtime::vm::NullCollector::default())? as _
}
#[cfg(not(feature = "gc-null"))]
Collector::Null => unreachable!(),
diff --git a/crates/wasmtime/src/engine.rs b/crates/wasmtime/src/engine.rs
index 04ce15903d..b2b1ef42f3 100644
--- a/crates/wasmtime/src/engine.rs
+++ b/crates/wasmtime/src/engine.rs
@@ -123,7 +123,7 @@ impl Engine {
let _ = &mut tunables;
Ok(Engine {
- inner: Arc::new(EngineInner {
+ inner: try_new::<Arc<_>>(EngineInner {
#[cfg(any(feature = "cranelift", feature = "winch"))]
compiler,
#[cfg(feature = "runtime")]
@@ -150,7 +150,7 @@ impl Engine {
config,
tunables,
features,
- }),
+ })?,
})
}
@@ -748,7 +748,9 @@ impl Engine {
}
pub(crate) fn allocator(&self) -> &dyn crate::runtime::vm::InstanceAllocator {
- self.inner.allocator.as_ref()
+ let r: &(dyn crate::runtime::vm::InstanceAllocator + Send + Sync) =
+ self.inner.allocator.as_ref();
+ &*r
}
pub(crate) fn gc_runtime(&self) -> Option<&Arc<dyn GcRuntime>> {