From 0b11d665aaefc3473c1758e339e0d333a8949703 Mon Sep 17 00:00:00 2001 From: Sergey Pepyakin Date: Mon, 15 Apr 2019 19:44:02 +0200 Subject: [PATCH] Make wasmi compilation tests work --- src/prepare/mod.rs | 3 +++ src/prepare/tests.rs | 13 ++++++++----- validation/src/func.rs | 3 ++- validation/src/lib.rs | 23 +++++++++++++---------- validation/src/tests.rs | 4 ++-- 5 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/prepare/mod.rs b/src/prepare/mod.rs index cf7531e..e2c2889 100644 --- a/src/prepare/mod.rs +++ b/src/prepare/mod.rs @@ -9,6 +9,9 @@ use parity_wasm::elements::Module; mod compile; +#[cfg(test)] +mod tests; + #[derive(Clone)] pub struct CompiledModule { pub code_map: Vec, diff --git a/src/prepare/tests.rs b/src/prepare/tests.rs index aba3b0b..9f9faeb 100644 --- a/src/prepare/tests.rs +++ b/src/prepare/tests.rs @@ -1,14 +1,17 @@ +use super::{compile_module, CompiledModule}; +use parity_wasm::{deserialize_buffer, elements::Module}; +use isa; +use wabt; - -fn validate(wat: &str) -> ValidatedModule { +fn validate(wat: &str) -> CompiledModule { let wasm = wabt::wat2wasm(wat).unwrap(); let module = deserialize_buffer::(&wasm).unwrap(); - let validated_module = validate_module(module).unwrap(); - validated_module + let compiled_module = compile_module(module).unwrap(); + compiled_module } -fn compile(module: &ValidatedModule) -> (Vec, Vec) { +fn compile(module: &CompiledModule) -> (Vec, Vec) { let code = &module.code_map[0]; let mut instructions = Vec::new(); let mut pcs = Vec::new(); diff --git a/validation/src/func.rs b/validation/src/func.rs index 05efd2e..d51a4e8 100644 --- a/validation/src/func.rs +++ b/validation/src/func.rs @@ -134,7 +134,8 @@ pub fn drive( } // The last `end` opcode should pop last instruction. - // TODO: This looks like it should be returned as an error? + // parity-wasm ensures that there is always `End` opcode at + // the end of the function body. assert!(context.frame_stack.is_empty()); Ok(validator.finish()) diff --git a/validation/src/lib.rs b/validation/src/lib.rs index 24bc2e6..f9b03dc 100644 --- a/validation/src/lib.rs +++ b/validation/src/lib.rs @@ -100,16 +100,19 @@ pub trait FunctionValidator { fn finish(self) -> Self::Output; } -impl Validation for () { +pub struct SimpleValidation; +pub struct SimpleFunctionValidator; + +impl Validation for SimpleValidation { type Output = (); - type FunctionValidator = (); - fn new(module: &Module) -> () { - () + type FunctionValidator = SimpleFunctionValidator; + fn new(_module: &Module) -> SimpleValidation { + SimpleValidation } fn on_function_validated( &mut self, - index: u32, - output: <::FunctionValidator as FunctionValidator>::Output, + _index: u32, + _output: <::FunctionValidator as FunctionValidator>::Output, ) -> () { () } @@ -118,11 +121,11 @@ impl Validation for () { } } -impl FunctionValidator for () { +impl FunctionValidator for SimpleFunctionValidator { type Output = (); - fn new(ctx: &func::FunctionValidationContext) -> () { - () + fn new(_ctx: &func::FunctionValidationContext) -> SimpleFunctionValidator { + SimpleFunctionValidator } fn next_instruction( @@ -130,7 +133,7 @@ impl FunctionValidator for () { ctx: &mut func::FunctionValidationContext, instruction: &Instruction, ) -> Result<(), Error> { - Ok(()) + ctx.step(instruction) } fn finish(self) -> () { diff --git a/validation/src/tests.rs b/validation/src/tests.rs index badfeb5..adda4c0 100644 --- a/validation/src/tests.rs +++ b/validation/src/tests.rs @@ -1,4 +1,4 @@ -use crate::Error; +use crate::{Error, SimpleValidation}; use parity_wasm::builder::module; use parity_wasm::elements::{ BlockType, External, GlobalEntry, GlobalType, ImportEntry, InitExpr, Instruction, Instructions, @@ -6,7 +6,7 @@ use parity_wasm::elements::{ }; fn validate_module(module: &Module) -> Result<(), Error> { - super::validate_module::<()>(module) + super::validate_module::(module) } #[test]