A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Add context-reuse testing support to `OomTest` (#14955)
Nick Fitzgerald65e9dcc3e339
diff --git a/crates/fuzzing/src/oom.rs b/crates/fuzzing/src/oom.rsindex 91bc83aeec..4dc1fceafa 100644--- a/crates/fuzzing/src/oom.rs+++ b/crates/fuzzing/src/oom.rs@@ -14,7 +14,7 @@ use std::{task::{Context, Poll},time,};-use wasmtime_core::error::{OutOfMemory, Result, bail, ensure};+use wasmtime_core::error::{Context as _, OutOfMemory, Result, bail, ensure};/// An allocator for use with `OomTest`.#[non_exhaustive]@@ -221,6 +221,24 @@ unsafe impl GlobalAlloc for OomTestAllocator {/// })/// }/// ```+///+/// Use `test_with_reuse` when you also want to check that some context is still+/// usable after the OOM, e.g. that nothing was left half-registered in state+/// that is shared with an `Engine`:+///+/// ```no_run+/// use wasmtime::Result;+/// use wasmtime_fuzzing::oom::OomTest;+///+/// #[test]+/// fn my_oom_reuse_test() -> Result<()> {+/// OomTest::new().test_with_reuse(+/// || todo!("create the context that is reused after the OOM..."),+/// |context| todo!("insert code here that should handle OOM here..."),+/// |context| todo!("check that `context` is still usable..."),+/// )+/// }+/// ```pub struct OomTest {max_iters: Option<u32>,max_duration: Option<time::Duration>,@@ -319,7 +337,58 @@ impl OomTest {/// The same as `test` but `async`.pub async fn test_async(&self, test_func: impl AsyncFn() -> Result<()>) -> Result<()> {- let total_allocs = Self::count_allocations(&test_func).await?;+ self.test_with_reuse_async(+ async || Ok(()),+ async |_: &mut ()| test_func().await,+ async |_: ()| Ok(()),+ )+ .await+ }++ /// The same as `test`, but additionally exercising context that is reused+ /// after the OOM is triggered.+ ///+ /// Each iteration runs three phases in sequence:+ ///+ /// 1. `make_context` creates the context that is shared between the two+ /// phases that follow. It is called afresh every iteration, so that each+ /// iteration begins from identical state.+ ///+ /// 2. `test_func` is the main OOM test, and is the only phase into which+ /// OOMs are injected. It is otherwise the same as the function given to+ /// `test`.+ ///+ /// 3. `post_oom_func` checks that we recovered from the OOM correctly and+ /// that we can still interact with the context afterwards, e.g. that+ /// nothing was left half-registered in shared state, rather than+ /// properly cleaned up, when the OOM happened.+ ///+ /// OOMs are never injected into `make_context` or `post_oom_func` and+ /// neither's allocations are counted. An `Err(_)` returned from either is a+ /// test failure.+ pub fn test_with_reuse<T>(+ &self,+ make_context: impl Fn() -> Result<T>,+ test_func: impl Fn(&mut T) -> Result<()>,+ post_oom_func: impl Fn(T) -> Result<()>,+ ) -> Result<()> {+ let future = self.test_with_reuse_async(+ async || make_context(),+ async |context: &mut T| test_func(context),+ async |context: T| post_oom_func(context),+ );+ crate::block_on(future)+ }++ /// The same as `test_with_reuse` but `async`.+ pub async fn test_with_reuse_async<T>(+ &self,+ make_context: impl AsyncFn() -> Result<T>,+ test_func: impl AsyncFn(&mut T) -> Result<()>,+ post_oom_func: impl AsyncFn(T) -> Result<()>,+ ) -> Result<()> {+ let total_allocs =+ Self::count_allocations(&make_context, &test_func, &post_oom_func).await?;let start = time::Instant::now();@@ -331,7 +400,8 @@ impl OomTest {}log::trace!("=== Injecting OOM after {i} allocations ===");- self.run_one_oom_injection(&test_func, i).await?;+ self.run_one_oom_injection(&make_context, &test_func, &post_oom_func, i)+ .await?;}Ok(())@@ -353,7 +423,12 @@ impl OomTest {None => bail!("OomTest::fuzz requires max_iters to be set"),};- let total_allocs = Self::count_allocations(&test_func).await?;+ let make_context = async || Ok(());+ let test_func = async |_: &mut ()| test_func().await;+ let post_oom_func = async |_: ()| Ok(());++ let total_allocs =+ Self::count_allocations(&make_context, &test_func, &post_oom_func).await?;if total_allocs == 0 {return Ok(());@@ -364,7 +439,8 @@ impl OomTest {for _ in 0..max_iters {let i = rng.random_range(0..total_allocs);log::trace!("=== Injecting OOM after {i} allocations (fuzz) ===");- self.run_one_oom_injection(&test_func, i).await?;+ self.run_one_oom_injection(&make_context, &test_func, &post_oom_func, i)+ .await?;}Ok(())@@ -372,21 +448,29 @@ impl OomTest {/// Run the test function once, injecting OOM on the `i`th allocation, and/// check that the result correctly reflects whether an OOM was hit.- async fn run_one_oom_injection(+ async fn run_one_oom_injection<T>(&self,- test_func: &impl AsyncFn() -> Result<()>,+ make_context: &impl AsyncFn() -> Result<T>,+ test_func: &impl AsyncFn(&mut T) -> Result<()>,+ post_oom_func: &impl AsyncFn(T) -> Result<()>,i: u32,) -> Result<()> {- let future = std::pin::pin!(test_func());- let (result, oom_state) = OomTestFuture::new(- future,- OomState::OomOnAlloc {- counter: i,- allow_alloc_after: self.allow_alloc_after_oom,- alloc_succeeds_after: self.alloc_succeeds_after_oom,- },- )- .await;+ let mut context = make_context()+ .await+ .context("OOM test failed to create its reused context")?;++ let (result, oom_state) = {+ let future = std::pin::pin!(test_func(&mut context));+ OomTestFuture::new(+ future,+ OomState::OomOnAlloc {+ counter: i,+ allow_alloc_after: self.allow_alloc_after_oom,+ alloc_succeeds_after: self.alloc_succeeds_after_oom,+ },+ )+ .await+ };match (result, oom_state) {(_, OomState::OutsideOomTest | OomState::Counting { .. }) => unreachable!(),@@ -424,7 +508,9 @@ impl OomTest {}}- Ok(())+ post_oom_func(context)+ .await+ .context("OOM test's post-OOM function failed")}/// Run the test function without injecting OOMs, counting allocations.@@ -432,16 +518,31 @@ impl OomTest {/// Repeats until the count stabilizes across two consecutive runs,/// since the first run may change internal state (e.g. grow hash map/// capacities) that subsequent runs reuse.- async fn count_allocations(test_func: &impl AsyncFn() -> Result<()>) -> Result<u32> {+ async fn count_allocations<T>(+ make_context: &impl AsyncFn() -> Result<T>,+ test_func: &impl AsyncFn(&mut T) -> Result<()>,+ post_oom_func: &impl AsyncFn(T) -> Result<()>,+ ) -> Result<u32> {const MAX_DRY_RUN_ITERS: usize = 10;let mut total_allocs = u32::MAX;for i in 0..MAX_DRY_RUN_ITERS {log::trace!("=== Counting allocations (dry run {i}) ===");- let future = std::pin::pin!(test_func());- let (result, oom_state) =- OomTestFuture::new(future, OomState::Counting { count: 0 }).await;++ let mut context = make_context()+ .await+ .context("OOM test failed to create its reused context")?;++ let (result, oom_state) = {+ let future = std::pin::pin!(test_func(&mut context));+ OomTestFuture::new(future, OomState::Counting { count: 0 }).await+ };result?;++ post_oom_func(context)+ .await+ .context("OOM test's post-OOM function failed")?;+let count = match oom_state {OomState::Counting { count } => count,_ => unreachable!(),diff --git a/crates/fuzzing/tests/oom/instance.rs b/crates/fuzzing/tests/oom/instance.rsindex 8016aa230c..db68f5dedc 100644--- a/crates/fuzzing/tests/oom/instance.rs+++ b/crates/fuzzing/tests/oom/instance.rs@@ -296,3 +296,68 @@ fn table_element_segment_init() -> Result<()> {Ok(())})}++#[tokio::test]+async fn instance_new_async_reuse_engine() -> Result<()> {+ // A module with an imported function, an exported function, and a rec+ // group with more than one type in it, so that building the context+ // registers several types in the engine's type registry.+ let module_bytes = {+ let mut config = Config::new();+ config.concurrency_support(false);+ let engine = Engine::new(&config)?;+ Module::new(+ &engine,+ r#"+ (module+ (rec+ (type $a (func (param (ref null $b)) (result i32)))+ (type $b (func (param (ref null $a))))+ )+ (import "" "f" (func))+ (func (export "g") (result i32)+ (call 0)+ (i32.const 42)+ )+ )+ "#,+ )?+ .serialize()?+ };++ /// A fresh engine and module for each iteration of the OOM test.+ struct Context {+ engine: Engine,+ module: Module,+ }++ OomTest::new()+ .allow_alloc_after_oom(true)+ .test_with_reuse_async(+ async || {+ let mut config = Config::new();+ config.enable_compiler(false);+ config.concurrency_support(false);+ let engine = Engine::new(&config)?;+ let module = unsafe { Module::deserialize(&engine, &module_bytes)? };+ Ok(Context { engine, module })+ },+ async |cx: &mut Context| {+ let mut store = Store::try_new(&cx.engine, ())?;+ let func = Func::try_wrap(&mut store, || {})?;+ let _ = Instance::new_async(&mut store, &cx.module, &[func.into()]).await?;+ Ok(())+ },+ async |cx: Context| {+ // Whether or not we hit an OOM above, we must still be able to+ // instantiate the module and call into it.+ let mut store = Store::try_new(&cx.engine, ())?;+ let func = Func::try_wrap(&mut store, || {})?;+ let instance = Instance::new_async(&mut store, &cx.module, &[func.into()]).await?;+ let g = instance.get_typed_func::<(), i32>(&mut store, "g")?;+ assert_eq!(g.call_async(&mut store, ()).await?, 42);+ Ok(())+ },+ )+ .await+}diff --git a/crates/fuzzing/tests/oom/module.rs b/crates/fuzzing/tests/oom/module.rsindex 60a0afb0fd..9db2693b53 100644--- a/crates/fuzzing/tests/oom/module.rs+++ b/crates/fuzzing/tests/oom/module.rs@@ -1,6 +1,6 @@#![cfg(arc_try_new)]-use wasmtime::{Config, Engine, Module, Result, WasmBacktraceDetails};+use wasmtime::{Config, Engine, FuncType, Module, Result, ValType, WasmBacktraceDetails};use wasmtime_fuzzing::oom::OomTest;#[test]@@ -86,3 +86,60 @@ fn module_deserialize_multiple_rec_groups() -> Result<()> {Ok(())})}++/// Check that failing to register a module's types partway through leaves the+/// engine's type registry in a good state, by reusing the engine afterwards.+#[test]+fn module_deserialize_reuse_engine() -> Result<()> {+ let module_bytes = {+ let mut config = Config::new();+ config.concurrency_support(false);+ let engine = Engine::new(&config)?;+ Module::new(+ &engine,+ r#"+ (module+ (rec+ (type $a (func (param (ref null $b)) (result i32)))+ (type $b (func (param (ref null $a))))+ )+ (type $c (func (param i32 i32) (result i64)))+ (func (export "c") (type $c) (i64.const 0))+ (func (export "b") (type $b))+ )+ "#,+ )?+ .serialize()?+ };++ OomTest::new()+ // See the note on `module_deserialize` above.+ .allow_alloc_after_oom(true)+ .test_with_reuse(+ || {+ let mut config = Config::new();+ config.enable_compiler(false);+ config.concurrency_support(false);+ Engine::new(&config)+ },+ |engine: &mut Engine| unsafe {+ let _ = Module::deserialize(engine, &module_bytes)?;+ Ok(())+ },+ |engine: Engine| unsafe {+ // Whether or not we hit an OOM above, the engine must remain+ // usable and its type registry must not have any of the+ // module's types left half-registered within it.+ let module = Module::deserialize(&engine, &module_bytes)?;++ // The module's types must be as expected.+ let c = module.get_export("c").unwrap().unwrap_func().clone();+ assert_eq!(c.params().len(), 2);+ assert_eq!(c.results().len(), 1);+ let c2 = FuncType::try_new(&engine, [ValType::I32, ValType::I32], [ValType::I64])?;+ assert!(FuncType::eq(&c, &c2), "{c:?} should be equal to {c2:?}");++ Ok(())+ },+ )+}diff --git a/crates/wasmtime/src/runtime/type_registry.rs b/crates/wasmtime/src/runtime/type_registry.rsindex 861683fcdd..f5d45a9135 100644--- a/crates/wasmtime/src/runtime/type_registry.rs+++ b/crates/wasmtime/src/runtime/type_registry.rs@@ -1358,7 +1358,7 @@ impl TypeRegistryInner {log::trace!(" type_to_rec_group.resize({types_capacity})");type_to_rec_group.resize(types_capacity)?;- log::trace!(" type_to_rec_group.reserve({types_capacity})");+ log::trace!(" drop_stack.reserve({types_capacity})");debug_assert!(drop_stack.is_empty());drop_stack.reserve(types_capacity)?;@@ -1595,7 +1595,7 @@ impl TypeRegistryInner {// Okay, we are really going to unregister this entry. Enqueue it on the// drop stack.- debug_assert!(self.drop_stack.capacity() >= self.types.capacity());+ debug_assert!(self.drop_stack.capacity() >= self.types.len());self.push_to_drop_stack(entry);self.drain_drop_stack();}