From 175e72bac40f2a89de457195150978b54b9cfbc8 Mon Sep 17 00:00:00 2001 From: Pat Hickey Date: Thu, 21 Oct 2021 16:43:13 -0700 Subject: [PATCH] test that the catch unwind works --- tests/all/limits.rs | 109 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 106 insertions(+), 3 deletions(-) diff --git a/tests/all/limits.rs b/tests/all/limits.rs index a41a2ba881..05585657e9 100644 --- a/tests/all/limits.rs +++ b/tests/all/limits.rs @@ -89,7 +89,6 @@ async fn test_limits_async() -> Result<()> { ) -> bool { desired <= self.memory_size } - fn memory_grow_failed(&mut self, _error: &anyhow::Error) {} async fn table_growing( &mut self, _current: u32, @@ -500,7 +499,6 @@ impl ResourceLimiterAsync for MemoryContext { self.wasm_memory_used = desired; true } - fn memory_grow_failed(&mut self, _e: &anyhow::Error) {} async fn table_growing(&mut self, _current: u32, _desired: u32, _maximum: Option) -> bool { true } @@ -637,7 +635,7 @@ fn test_custom_table_limiter() -> Result<()> { let instance = linker.instantiate(&mut store, &module)?; let table = instance.get_table(&mut store, "t").unwrap(); - // Grow the memory by 10 elements + // Grow the table by 10 elements table.grow(&mut store, 3, Val::FuncRef(None))?; table.grow(&mut store, 5, Val::FuncRef(None))?; table.grow(&mut store, 2, Val::FuncRef(None))?; @@ -891,3 +889,108 @@ async fn custom_limiter_async_detect_grow_failure() -> Result<()> { Ok(()) } + +struct Panic; + +impl ResourceLimiter for Panic { + fn memory_growing( + &mut self, + _current: usize, + _desired: usize, + _maximum: Option, + ) -> bool { + panic!("resource limiter memory growing"); + } + fn table_growing(&mut self, _current: u32, _desired: u32, _maximum: Option) -> bool { + panic!("resource limiter table growing"); + } +} +#[async_trait::async_trait] +impl ResourceLimiterAsync for Panic { + async fn memory_growing( + &mut self, + _current: usize, + _desired: usize, + _maximum: Option, + ) -> bool { + panic!("async resource limiter memory growing"); + } + async fn table_growing(&mut self, _current: u32, _desired: u32, _maximum: Option) -> bool { + panic!("async resource limiter table growing"); + } +} + +#[test] +#[should_panic(expected = "resource limiter memory growing")] +fn panic_in_memory_limiter() { + let engine = Engine::default(); + let linker = Linker::new(&engine); + + let module = Module::new(&engine, r#"(module (memory (export "m") 0))"#).unwrap(); + + let mut store = Store::new(&engine, Panic); + store.limiter(|s| s as &mut dyn ResourceLimiter); + let instance = linker.instantiate(&mut store, &module).unwrap(); + let memory = instance.get_memory(&mut store, "m").unwrap(); + + // Grow the memory, which should panic + memory.grow(&mut store, 3).unwrap(); +} + +#[test] +#[should_panic(expected = "resource limiter table growing")] +fn panic_in_table_limiter() { + let engine = Engine::default(); + let linker = Linker::new(&engine); + + let module = Module::new(&engine, r#"(module (table (export "t") 0 anyfunc))"#).unwrap(); + + let mut store = Store::new(&engine, Panic); + store.limiter(|s| s as &mut dyn ResourceLimiter); + let instance = linker.instantiate(&mut store, &module).unwrap(); + let table = instance.get_table(&mut store, "t").unwrap(); + + // Grow the table, which should panic + table.grow(&mut store, 3, Val::FuncRef(None)).unwrap(); +} + +#[tokio::test] +#[should_panic(expected = "async resource limiter memory growing")] +async fn panic_in_async_memory_limiter() { + let mut config = Config::new(); + config.async_support(true); + let engine = Engine::new(&config).unwrap(); + let linker = Linker::new(&engine); + + let module = Module::new(&engine, r#"(module (memory (export "m") 0))"#).unwrap(); + + let mut store = Store::new(&engine, Panic); + store.limiter_async(|s| s as &mut dyn ResourceLimiterAsync); + let instance = linker.instantiate_async(&mut store, &module).await.unwrap(); + let memory = instance.get_memory(&mut store, "m").unwrap(); + + // Grow the memory, which should panic + memory.grow_async(&mut store, 3).await.unwrap(); +} + +#[tokio::test] +#[should_panic(expected = "async resource limiter table growing")] +async fn panic_in_async_table_limiter() { + let mut config = Config::new(); + config.async_support(true); + let engine = Engine::new(&config).unwrap(); + let linker = Linker::new(&engine); + + let module = Module::new(&engine, r#"(module (table (export "t") 0 anyfunc))"#).unwrap(); + + let mut store = Store::new(&engine, Panic); + store.limiter_async(|s| s as &mut dyn ResourceLimiterAsync); + let instance = linker.instantiate_async(&mut store, &module).await.unwrap(); + let table = instance.get_table(&mut store, "t").unwrap(); + + // Grow the table, which should panic + table + .grow_async(&mut store, 3, Val::FuncRef(None)) + .await + .unwrap(); +}