From b789ea7422db38d2a303be68eee012d746da7328 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Tue, 6 Jun 2017 13:14:19 +0300 Subject: [PATCH 1/3] update wabt and also failing spec --- spec/src/fixtures.rs | 7 +++++++ spec/src/run.rs | 44 ++++++++++++++++++++++++++++++++++++-------- spec/wabt | 2 +- 3 files changed, 44 insertions(+), 9 deletions(-) diff --git a/spec/src/fixtures.rs b/spec/src/fixtures.rs index 8481ed0..a3dc3cd 100644 --- a/spec/src/fixtures.rs +++ b/spec/src/fixtures.rs @@ -1,4 +1,10 @@ macro_rules! run_test { + ($label: expr, $test_name: ident, fail) => ( + #[test] + fn $test_name() { + ::run::failing_spec($label) + } + ); ($label: expr, $test_name: ident) => ( #[test] fn $test_name() { @@ -8,6 +14,7 @@ macro_rules! run_test { } run_test!("address", wasm_address); +run_test!("address-offset-range.fail", wasm_address_offset_range_fail, fail); run_test!("endianness", wasm_endianness); run_test!("f32", wasm_f32); run_test!("f32_bitwise", wasm_f32_bitwise); diff --git a/spec/src/run.rs b/spec/src/run.rs index ad03cc0..b643758 100644 --- a/spec/src/run.rs +++ b/spec/src/run.rs @@ -80,7 +80,12 @@ fn run_action(module: &ModuleInstance, action: &test::Action) } } -pub fn spec(name: &str) { +pub struct FixtureParams { + failing: bool, + json: String, +} + +pub fn run_wast2wasm(name: &str) -> FixtureParams { let outdir = env::var("OUT_DIR").unwrap(); let mut wast2wasm_path = PathBuf::from(outdir.clone()); @@ -98,15 +103,38 @@ pub fn spec(name: &str) { .output() .expect("Failed to execute process"); - if !wast2wasm_output.status.success() { - println!("wasm2wast error code: {}", wast2wasm_output.status); - println!("wasm2wast stdout: {}", String::from_utf8_lossy(&wast2wasm_output.stdout)); - println!("wasm2wast stderr: {}", String::from_utf8_lossy(&wast2wasm_output.stderr)); - panic!("wasm2wast exited with status {}", wast2wasm_output.status); + FixtureParams { + json: json_spec_path.to_str().unwrap().to_owned(), + failing: { + if !wast2wasm_output.status.success() { + println!("wasm2wast error code: {}", wast2wasm_output.status); + println!("wasm2wast stdout: {}", String::from_utf8_lossy(&wast2wasm_output.stdout)); + println!("wasm2wast stderr: {}", String::from_utf8_lossy(&wast2wasm_output.stderr)); + true + } else { + false + } + } + } +} + +pub fn failing_spec(name: &str) { + let fixture = run_wast2wasm(name); + if !fixture.failing { + panic!("wasm2wast expected to fail, but terminated normally"); + } +} + +pub fn spec(name: &str) { + let outdir = env::var("OUT_DIR").unwrap(); + + let fixture = run_wast2wasm(name); + if fixture.failing { + panic!("wasm2wast terminated abnormally, expected to success"); } - let mut f = File::open(&json_spec_path) - .expect(&format!("Failed to load json file {}", &json_spec_path.to_string_lossy())); + let mut f = File::open(&fixture.json) + .expect(&format!("Failed to load json file {}", &fixture.json)); let spec: test::Spec = serde_json::from_reader(&mut f).expect("Failed to deserialize JSON file"); let first_command = &spec.commands[0]; diff --git a/spec/wabt b/spec/wabt index 7425bcc..1e5b546 160000 --- a/spec/wabt +++ b/spec/wabt @@ -1 +1 @@ -Subproject commit 7425bcc31e6516375391d18a62daee4c9f3502de +Subproject commit 1e5b546c65e8fef609431e77b1b2434dbf162e6c From 8407520ab4993c1e554de30084fd860b99bb0c45 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Tue, 6 Jun 2017 14:19:36 +0300 Subject: [PATCH 2/3] malformed test --- spec/src/fixtures.rs | 1 + spec/src/run.rs | 4 +++- spec/src/test.rs | 6 ++++++ src/elements/mod.rs | 4 ++++ src/elements/module.rs | 22 ++++++++++++++++++---- 5 files changed, 32 insertions(+), 5 deletions(-) diff --git a/spec/src/fixtures.rs b/spec/src/fixtures.rs index a3dc3cd..26f4264 100644 --- a/spec/src/fixtures.rs +++ b/spec/src/fixtures.rs @@ -15,6 +15,7 @@ macro_rules! run_test { run_test!("address", wasm_address); run_test!("address-offset-range.fail", wasm_address_offset_range_fail, fail); +run_test!("binary", wasm_binary); run_test!("endianness", wasm_endianness); run_test!("f32", wasm_f32); run_test!("f32_bitwise", wasm_f32_bitwise); diff --git a/spec/src/run.rs b/spec/src/run.rs index b643758..31b41a4 100644 --- a/spec/src/run.rs +++ b/spec/src/run.rs @@ -211,7 +211,9 @@ pub fn spec(name: &str) { } } }, - &test::Command::AssertInvalid { line, ref filename, .. } => { + &test::Command::AssertInvalid { line, ref filename, .. } + | &test::Command::AssertMalformed { line, ref filename, .. } + => { let module_load = try_load(&outdir, filename); match module_load { Ok(_) => { diff --git a/spec/src/test.rs b/spec/src/test.rs index 736f187..cc7b505 100644 --- a/spec/src/test.rs +++ b/spec/src/test.rs @@ -47,6 +47,12 @@ pub enum Command { filename: String, text: String, }, + #[serde(rename = "assert_malformed")] + AssertMalformed { + line: u64, + filename: String, + text: String, + }, #[serde(rename = "action")] Action { line: u64, diff --git a/src/elements/mod.rs b/src/elements/mod.rs index dfb40db..dccc52b 100644 --- a/src/elements/mod.rs +++ b/src/elements/mod.rs @@ -52,6 +52,10 @@ pub trait Serialize { pub enum Error { /// Unexpected end of input UnexpectedEof, + /// Invalid magic + InvalidMagic, + /// Unsupported version + UnsupportedVersion(u32), /// Inconsistence between declared and actual length InconsistentLength { /// Expected length of the definition diff --git a/src/elements/module.rs b/src/elements/module.rs index 636ea02..c144a8e 100644 --- a/src/elements/module.rs +++ b/src/elements/module.rs @@ -1,10 +1,14 @@ use std::io; +use byteorder::{LittleEndian, ByteOrder}; + use super::{Deserialize, Serialize, Error, Uint32}; use super::section::{ Section, CodeSection, TypeSection, ImportSection, ExportSection, FunctionSection, GlobalSection, TableSection, ElementSection, DataSection, MemorySection }; +const WASM_MAGIC_NUMBER: [u8; 4] = [0x00, 0x61, 0x73, 0x6d]; + /// WebAssembly module pub struct Module { magic: u32, @@ -144,8 +148,18 @@ impl Deserialize for Module { fn deserialize(reader: &mut R) -> Result { let mut sections = Vec::new(); - let magic = Uint32::deserialize(reader)?; - let version = Uint32::deserialize(reader)?; + + let mut magic = [0u8; 4]; + reader.read(&mut magic)?; + if magic != WASM_MAGIC_NUMBER { + return Err(Error::InvalidMagic); + } + + let version: u32 = Uint32::deserialize(reader)?.into(); + + if version != 1 { + return Err(Error::UnsupportedVersion(version)); + } loop { match Section::deserialize(reader) { @@ -156,8 +170,8 @@ impl Deserialize for Module { } Ok(Module { - magic: magic.into(), - version: version.into(), + magic: LittleEndian::read_u32(&magic), + version: version, sections: sections, }) } From a9ec98fa0c686d17749e0d20bc5a65cba2800fdf Mon Sep 17 00:00:00 2001 From: NikVolf Date: Tue, 6 Jun 2017 14:34:58 +0300 Subject: [PATCH 3/3] bump version --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 7a7aa29..3d721e1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "parity-wasm" -version = "0.8.3" +version = "0.8.4" authors = ["NikVolf "] license = "MIT/Apache-2.0" readme = "README.md"