From 88abecb66c280967c648497993294406f45210ac Mon Sep 17 00:00:00 2001 From: Mrmaxmeier Date: Tue, 16 May 2023 21:06:54 +0200 Subject: [PATCH 1/2] Avoid duplicate call to `_initialize` We call the reactor init function automatically before running the user's init function. Let's not call it twice if the user explicitly requests `_initialize`. --- src/lib.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 2b70b11..3e188e6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -866,6 +866,12 @@ impl Wizer { f.call(&mut *store, ()).map_err(Into::into) }) .context("calling the Reactor initialization function")?; + + if self.init_func == "_initialize" && has_wasi_initialize { + // Don't run `_initialize` twice if the it was explicitly + // requested as the init function. + return Ok((instance, has_wasi_initialize)); + } } } From ea2ec093594fbaa119ce8cabd2ce2957470e37e9 Mon Sep 17 00:00:00 2001 From: Mrmaxmeier Date: Wed, 17 May 2023 21:09:54 +0200 Subject: [PATCH 2/2] Add test for `--init-func=_initialize` --- tests/tests.rs | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/tests/tests.rs b/tests/tests.rs index 4be4496..8b8f28d 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -6,7 +6,7 @@ use wizer::Wizer; fn run_wat(args: &[wasmtime::Val], expected: i32, wat: &str) -> Result<()> { let _ = env_logger::try_init(); let wasm = wat_to_wasm(wat)?; - run_wasm(args, expected, &wasm) + wiz_and_run_wasm(args, expected, &wasm, get_wizer()) } fn get_wizer() -> Wizer { @@ -17,7 +17,12 @@ fn get_wizer() -> Wizer { wizer } -fn run_wasm(args: &[wasmtime::Val], expected: i32, wasm: &[u8]) -> Result<()> { +fn wiz_and_run_wasm( + args: &[wasmtime::Val], + expected: i32, + wasm: &[u8], + wizer: Wizer, +) -> Result<()> { let _ = env_logger::try_init(); log::debug!( @@ -26,7 +31,7 @@ fn run_wasm(args: &[wasmtime::Val], expected: i32, wasm: &[u8]) -> Result<()> { ===========================================================================", wasmprinter::print_bytes(&wasm).unwrap() ); - let wasm = get_wizer().run(&wasm)?; + let wasm = wizer.run(&wasm)?; log::debug!( "=== Wizened Wasm ==========================================================\n\ {}\n\ @@ -348,10 +353,11 @@ fn reject_data_drop() -> Result<()> { #[test] fn rust_regex() -> Result<()> { - run_wasm( + wiz_and_run_wasm( &[wasmtime::Val::I32(13)], 42, &include_bytes!("./regex_test.wasm")[..], + get_wizer(), ) } @@ -494,6 +500,30 @@ fn wasi_reactor() -> anyhow::Result<()> { ) } +#[test] +fn wasi_reactor_initializer_as_init_func() -> anyhow::Result<()> { + let wat = r#" + (module + (global $g (mut i32) i32.const 0) + (func (export "_initialize") + global.get $g + i32.const 1 + i32.add + global.set $g + ) + (func (export "run") (result i32) + global.get $g + ) + )"#; + + let _ = env_logger::try_init(); + let mut wizer = Wizer::new(); + wizer.init_func("_initialize"); + let wasm = wat_to_wasm(wat)?; + // we expect `_initialize` to be called _exactly_ once + wiz_and_run_wasm(&[], 1, &wasm, wizer) +} + #[test] fn call_undefined_import_function_during_init() -> Result<()> { fails_wizening( @@ -596,7 +626,7 @@ fn accept_bulk_memory_data_count() -> Result<()> { data.active(0, &ConstExpr::i32_const(4), vec![5, 6, 7, 8]); module.section(&data); - run_wasm(&[], 42, &module.finish()).unwrap(); + wiz_and_run_wasm(&[], 42, &module.finish(), get_wizer()).unwrap(); Ok(()) }