From 1917f163659da6ac24bc7d16728774ca069bf937 Mon Sep 17 00:00:00 2001 From: Robin Freyler Date: Mon, 7 Oct 2024 19:47:01 +0200 Subject: [PATCH 1/2] move test related methods into tests module --- crates/wasmi/src/engine/mod.rs | 102 ------------------ crates/wasmi/src/engine/tests/mod.rs | 99 +++++++++++++++++ .../translator/tests/op/call/internal.rs | 2 +- 3 files changed, 100 insertions(+), 103 deletions(-) diff --git a/crates/wasmi/src/engine/mod.rs b/crates/wasmi/src/engine/mod.rs index 3256201f68..d7c7faf68f 100644 --- a/crates/wasmi/src/engine/mod.rs +++ b/crates/wasmi/src/engine/mod.rs @@ -15,11 +15,6 @@ mod utils; #[cfg(test)] mod tests; -#[cfg(test)] -use self::code_map::CompiledFuncRef; -#[cfg(test)] -use crate::{core::UntypedVal, ir::Instruction, ir::RegSpan}; - pub(crate) use self::{ block_type::BlockType, config::FuelCosts, @@ -274,54 +269,6 @@ impl Engine { .init_lazy_func(func_idx, func, bytes, module, func_to_validate) } - /// Resolves the [`EngineFunc`] to the underlying Wasmi bytecode instructions. - /// - /// # Note - /// - /// - This is a variant of [`Engine::resolve_instr`] that returns register - /// machine based bytecode instructions. - /// - This API is mainly intended for unit testing purposes and shall not be used - /// outside of this context. The function bodies are intended to be data private - /// to the Wasmi interpreter. - /// - /// # Errors - /// - /// If the `func` fails Wasm to Wasmi bytecode translation after it was lazily initialized. - /// - /// # Panics - /// - /// - If the [`EngineFunc`] is invalid for the [`Engine`]. - /// - If register machine bytecode translation is disabled. - #[cfg(test)] - pub(crate) fn resolve_instr( - &self, - func: EngineFunc, - index: usize, - ) -> Result, Error> { - self.inner.resolve_instr(func, index) - } - - /// Resolves the function local constant of [`EngineFunc`] at `index` if any. - /// - /// # Note - /// - /// This API is intended for unit testing purposes and shall not be used - /// outside of this context. The function bodies are intended to be data - /// private to the Wasmi interpreter. - /// - /// # Errors - /// - /// If the `func` fails Wasm to Wasmi bytecode translation after it was lazily initialized. - /// - /// # Panics - /// - /// - If the [`EngineFunc`] is invalid for the [`Engine`]. - /// - If register machine bytecode translation is disabled. - #[cfg(test)] - fn get_func_const(&self, func: EngineFunc, index: usize) -> Result, Error> { - self.inner.get_func_const(func, index) - } - /// Executes the given [`Func`] with parameters `params`. /// /// Stores the execution result into `results` upon a successful execution. @@ -739,55 +686,6 @@ impl EngineInner { .init_func_as_uncompiled(func, func_idx, bytes, module, func_to_validate) } - /// Resolves the [`InternalFuncEntity`] for [`EngineFunc`] and applies `f` to it. - /// - /// # Panics - /// - /// If [`EngineFunc`] is invalid for [`Engine`]. - #[cfg(test)] - fn resolve_func<'a, F, R>(&'a self, func: EngineFunc, f: F) -> Result - where - F: FnOnce(CompiledFuncRef<'a>) -> R, - { - // Note: We use `None` so this test-only function will never charge for compilation fuel. - Ok(f(self.code_map.get(None, func)?)) - } - - /// Returns the [`Instruction`] of `func` at `index`. - /// - /// Returns `None` if the function has no instruction at `index`. - /// - /// # Errors - /// - /// If the `func` fails Wasm to Wasmi bytecode translation after it was lazily initialized. - /// - /// # Pancis - /// - /// If `func` cannot be resolved to a function for the [`EngineInner`]. - #[cfg(test)] - fn resolve_instr(&self, func: EngineFunc, index: usize) -> Result, Error> { - self.resolve_func(func, |func| func.instrs().get(index).copied()) - } - - /// Returns the function local constant value of `func` at `index`. - /// - /// Returns `None` if the function has no function local constant at `index`. - /// - /// # Errors - /// - /// If the `func` fails Wasm to Wasmi bytecode translation after it was lazily initialized. - /// - /// # Pancis - /// - /// If `func` cannot be resolved to a function for the [`EngineInner`]. - #[cfg(test)] - fn get_func_const(&self, func: EngineFunc, index: usize) -> Result, Error> { - // Function local constants are stored in reverse order of their indices since - // they are allocated in reverse order to their absolute indices during function - // translation. That is why we need to access them in reverse order. - self.resolve_func(func, |func| func.consts().iter().rev().nth(index).copied()) - } - /// Recycles the given [`Stack`]. fn recycle_stack(&self, stack: Stack) { self.stacks.lock().recycle(stack) diff --git a/crates/wasmi/src/engine/tests/mod.rs b/crates/wasmi/src/engine/tests/mod.rs index 16b0012b33..4a794263a7 100644 --- a/crates/wasmi/src/engine/tests/mod.rs +++ b/crates/wasmi/src/engine/tests/mod.rs @@ -1,2 +1,101 @@ mod host_calls; mod many_inout; + +use super::{EngineInner, code_map::{CompiledFuncRef, EngineFunc}}; +use crate::{Engine, core::UntypedVal, ir::Instruction, Error}; + +impl Engine { + /// Resolves the [`EngineFunc`] to the underlying Wasmi bytecode instructions. + /// + /// # Note + /// + /// - This is a variant of [`Engine::resolve_instr`] that returns register + /// machine based bytecode instructions. + /// - This API is mainly intended for unit testing purposes and shall not be used + /// outside of this context. The function bodies are intended to be data private + /// to the Wasmi interpreter. + /// + /// # Errors + /// + /// If the `func` fails Wasm to Wasmi bytecode translation after it was lazily initialized. + /// + /// # Panics + /// + /// - If the [`EngineFunc`] is invalid for the [`Engine`]. + /// - If register machine bytecode translation is disabled. + pub(crate) fn resolve_instr( + &self, + func: EngineFunc, + index: usize, + ) -> Result, Error> { + self.inner.resolve_instr(func, index) + } + + /// Resolves the function local constant of [`EngineFunc`] at `index` if any. + /// + /// # Note + /// + /// This API is intended for unit testing purposes and shall not be used + /// outside of this context. The function bodies are intended to be data + /// private to the Wasmi interpreter. + /// + /// # Errors + /// + /// If the `func` fails Wasm to Wasmi bytecode translation after it was lazily initialized. + /// + /// # Panics + /// + /// - If the [`EngineFunc`] is invalid for the [`Engine`]. + /// - If register machine bytecode translation is disabled. + pub(crate) fn get_func_const(&self, func: EngineFunc, index: usize) -> Result, Error> { + self.inner.get_func_const(func, index) + } +} + +impl EngineInner { + /// Resolves the [`InternalFuncEntity`] for [`EngineFunc`] and applies `f` to it. + /// + /// # Panics + /// + /// If [`EngineFunc`] is invalid for [`Engine`]. + pub(crate) fn resolve_func<'a, F, R>(&'a self, func: EngineFunc, f: F) -> Result + where + F: FnOnce(CompiledFuncRef<'a>) -> R, + { + // Note: We use `None` so this test-only function will never charge for compilation fuel. + Ok(f(self.code_map.get(None, func)?)) + } + + /// Returns the [`Instruction`] of `func` at `index`. + /// + /// Returns `None` if the function has no instruction at `index`. + /// + /// # Errors + /// + /// If the `func` fails Wasm to Wasmi bytecode translation after it was lazily initialized. + /// + /// # Pancis + /// + /// If `func` cannot be resolved to a function for the [`EngineInner`]. + pub(crate) fn resolve_instr(&self, func: EngineFunc, index: usize) -> Result, Error> { + self.resolve_func(func, |func| func.instrs().get(index).copied()) + } + + /// Returns the function local constant value of `func` at `index`. + /// + /// Returns `None` if the function has no function local constant at `index`. + /// + /// # Errors + /// + /// If the `func` fails Wasm to Wasmi bytecode translation after it was lazily initialized. + /// + /// # Pancis + /// + /// If `func` cannot be resolved to a function for the [`EngineInner`]. + pub(crate) fn get_func_const(&self, func: EngineFunc, index: usize) -> Result, Error> { + // Function local constants are stored in reverse order of their indices since + // they are allocated in reverse order to their absolute indices during function + // translation. That is why we need to access them in reverse order. + self.resolve_func(func, |func| func.consts().iter().rev().nth(index).copied()) + } +} diff --git a/crates/wasmi/src/engine/translator/tests/op/call/internal.rs b/crates/wasmi/src/engine/translator/tests/op/call/internal.rs index b429b70fba..5ef2fd2131 100644 --- a/crates/wasmi/src/engine/translator/tests/op/call/internal.rs +++ b/crates/wasmi/src/engine/translator/tests/op/call/internal.rs @@ -1,5 +1,5 @@ use super::*; -use crate::engine::{EngineFunc, RegSpan}; +use crate::{ir::RegSpan, engine::EngineFunc}; #[test] #[cfg_attr(miri, ignore)] From fd916fbd2e64648ea86a9e4d9acef3af5d34bfef Mon Sep 17 00:00:00 2001 From: Robin Freyler Date: Mon, 7 Oct 2024 19:53:00 +0200 Subject: [PATCH 2/2] apply rustfmt --- crates/wasmi/src/engine/tests/mod.rs | 25 +++++++++++++++---- .../translator/tests/op/call/internal.rs | 2 +- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/crates/wasmi/src/engine/tests/mod.rs b/crates/wasmi/src/engine/tests/mod.rs index 4a794263a7..930691f21c 100644 --- a/crates/wasmi/src/engine/tests/mod.rs +++ b/crates/wasmi/src/engine/tests/mod.rs @@ -1,8 +1,11 @@ mod host_calls; mod many_inout; -use super::{EngineInner, code_map::{CompiledFuncRef, EngineFunc}}; -use crate::{Engine, core::UntypedVal, ir::Instruction, Error}; +use super::{ + code_map::{CompiledFuncRef, EngineFunc}, + EngineInner, +}; +use crate::{core::UntypedVal, ir::Instruction, Engine, Error}; impl Engine { /// Resolves the [`EngineFunc`] to the underlying Wasmi bytecode instructions. @@ -47,7 +50,11 @@ impl Engine { /// /// - If the [`EngineFunc`] is invalid for the [`Engine`]. /// - If register machine bytecode translation is disabled. - pub(crate) fn get_func_const(&self, func: EngineFunc, index: usize) -> Result, Error> { + pub(crate) fn get_func_const( + &self, + func: EngineFunc, + index: usize, + ) -> Result, Error> { self.inner.get_func_const(func, index) } } @@ -77,7 +84,11 @@ impl EngineInner { /// # Pancis /// /// If `func` cannot be resolved to a function for the [`EngineInner`]. - pub(crate) fn resolve_instr(&self, func: EngineFunc, index: usize) -> Result, Error> { + pub(crate) fn resolve_instr( + &self, + func: EngineFunc, + index: usize, + ) -> Result, Error> { self.resolve_func(func, |func| func.instrs().get(index).copied()) } @@ -92,7 +103,11 @@ impl EngineInner { /// # Pancis /// /// If `func` cannot be resolved to a function for the [`EngineInner`]. - pub(crate) fn get_func_const(&self, func: EngineFunc, index: usize) -> Result, Error> { + pub(crate) fn get_func_const( + &self, + func: EngineFunc, + index: usize, + ) -> Result, Error> { // Function local constants are stored in reverse order of their indices since // they are allocated in reverse order to their absolute indices during function // translation. That is why we need to access them in reverse order. diff --git a/crates/wasmi/src/engine/translator/tests/op/call/internal.rs b/crates/wasmi/src/engine/translator/tests/op/call/internal.rs index 5ef2fd2131..513a98feef 100644 --- a/crates/wasmi/src/engine/translator/tests/op/call/internal.rs +++ b/crates/wasmi/src/engine/translator/tests/op/call/internal.rs @@ -1,5 +1,5 @@ use super::*; -use crate::{ir::RegSpan, engine::EngineFunc}; +use crate::{engine::EngineFunc, ir::RegSpan}; #[test] #[cfg_attr(miri, ignore)]