From e669beea1329f84a471ae60011dda576601eb456 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Thu, 19 Sep 2019 21:13:58 +0100 Subject: [PATCH 1/8] Rename re-exported Module to WasmModule for clarity --- libchisel/src/lib.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/libchisel/src/lib.rs b/libchisel/src/lib.rs index 1d25e35..c19a188 100644 --- a/libchisel/src/lib.rs +++ b/libchisel/src/lib.rs @@ -22,6 +22,9 @@ pub mod verifyimports; mod depgraph; +/// Exported for users of this library. +pub type WasmModule = Module; + #[derive(Eq, PartialEq, Debug)] pub enum ModuleKind { Creator, From 074cd47b77658b6e7b2f0cef8b3ae1eb3dfaee7d Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Thu, 19 Sep 2019 22:17:49 +0100 Subject: [PATCH 2/8] rename --- libchisel/src/lib.rs | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/libchisel/src/lib.rs b/libchisel/src/lib.rs index c19a188..01df9f8 100644 --- a/libchisel/src/lib.rs +++ b/libchisel/src/lib.rs @@ -1,4 +1,4 @@ -pub use parity_wasm::elements::Module; +pub use parity_wasm::elements::Module as WasmModule; use std::collections::HashMap; use std::{error, fmt}; @@ -22,9 +22,6 @@ pub mod verifyimports; mod depgraph; -/// Exported for users of this library. -pub type WasmModule = Module; - #[derive(Eq, PartialEq, Debug)] pub enum ModuleKind { Creator, @@ -53,20 +50,20 @@ pub trait ChiselModule<'a> { pub trait ModuleCreator { /// Returns new module. - fn create(&self) -> Result; + fn create(&self) -> Result; } pub trait ModuleTranslator { /// Translates module. Returns new module or none if nothing was modified. Can fail with ModuleError::NotSupported. - fn translate(&self, module: &Module) -> Result, ModuleError>; + fn translate(&self, module: &WasmModule) -> Result, ModuleError>; /// Translates module in-place. Returns true if the module was modified. Can fail with ModuleError::NotSupported. - fn translate_inplace(&self, module: &mut Module) -> Result; + fn translate_inplace(&self, module: &mut WasmModule) -> Result; } pub trait ModuleValidator { /// Validates module. Returns true if it is valid or false if invalid. - fn validate(&self, module: &Module) -> Result; + fn validate(&self, module: &WasmModule) -> Result; } pub trait ModulePreset { @@ -144,22 +141,22 @@ mod tests { struct SampleModule {} impl ModuleCreator for SampleModule { - fn create(&self) -> Result { + fn create(&self) -> Result { Ok(Module::default()) } } impl ModuleTranslator for SampleModule { - fn translate(&self, _module: &Module) -> Result, ModuleError> { + fn translate(&self, _module: &WasmModule) -> Result, ModuleError> { Ok(Some(Module::default())) } - fn translate_inplace(&self, _module: &mut Module) -> Result { + fn translate_inplace(&self, _module: &mut WasmModule) -> Result { Ok(true) } } impl ModuleValidator for SampleModule { - fn validate(&self, _module: &Module) -> Result { + fn validate(&self, _module: &WasmModule) -> Result { Ok(true) } } @@ -192,21 +189,21 @@ mod tests { #[test] fn translator_succeeds() { let translator = SampleModule {}; - let result = translator.translate(&Module::default()); + let result = translator.translate(&WasmModule::default()); assert!(result.is_ok()); } #[test] fn translator_inplace_succeeds() { let translator = SampleModule {}; - let result = translator.translate_inplace(&mut Module::default()); + let result = translator.translate_inplace(&mut WasmModule::default()); assert!(result.is_ok()); } #[test] fn validator_succeeds() { let validator = SampleModule {}; - let result = validator.validate(&Module::default()); + let result = validator.validate(&WasmModule::default()); assert!(result.is_ok()); } @@ -250,7 +247,7 @@ mod tests { let as_trait: &dyn ModuleValidator = opaque.as_abstract(); - let result = as_trait.validate(&Module::default()); + let result = as_trait.validate(&WasmModule::default()); assert!(result.is_ok()); } } From d3d6b1f2684f703ce78b45cd1f2885a46e9e369f Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Tue, 24 Sep 2019 22:59:22 +0100 Subject: [PATCH 3/8] f --- libchisel/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libchisel/src/lib.rs b/libchisel/src/lib.rs index 01df9f8..9c05ba1 100644 --- a/libchisel/src/lib.rs +++ b/libchisel/src/lib.rs @@ -142,13 +142,13 @@ mod tests { impl ModuleCreator for SampleModule { fn create(&self) -> Result { - Ok(Module::default()) + Ok(WasmModule::default()) } } impl ModuleTranslator for SampleModule { fn translate(&self, _module: &WasmModule) -> Result, ModuleError> { - Ok(Some(Module::default())) + Ok(Some(WasmModule::default())) } fn translate_inplace(&self, _module: &mut WasmModule) -> Result { Ok(true) From f8c0d89bf9451888bf99257cddc90ffdf191f4e5 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Tue, 24 Sep 2019 23:01:19 +0100 Subject: [PATCH 4/8] k --- libchisel/src/binaryenopt.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/libchisel/src/binaryenopt.rs b/libchisel/src/binaryenopt.rs index 7236e7a..178a23b 100644 --- a/libchisel/src/binaryenopt.rs +++ b/libchisel/src/binaryenopt.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use parity_wasm::elements::Module; -use super::{ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModulePreset, ModuleTranslator}; +use super::{ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModulePreset, ModuleTranslator, WasmModule}; // FIXME: change level names pub enum BinaryenOptimiser { @@ -61,11 +61,11 @@ impl ModulePreset for BinaryenOptimiser { } impl ModuleTranslator for BinaryenOptimiser { - fn translate_inplace(&self, _module: &mut Module) -> Result { + fn translate_inplace(&self, _module: &mut WasmModule) -> Result { Err(ModuleError::NotSupported) } - fn translate(&self, module: &Module) -> Result, ModuleError> { + fn translate(&self, module: &WasmModule) -> Result, ModuleError> { let has_names_section = module.has_names_section(); // FIXME: could just move this into `BinaryenOptimiser` @@ -109,7 +109,7 @@ impl ModuleTranslator for BinaryenOptimiser { let serialized = module.clone().to_bytes()?; let output = binaryen_optimiser(&serialized, &config)?; - let output = Module::from_bytes(&output)?; + let output = WasmModule::from_bytes(&output)?; Ok(Some(output)) } } @@ -147,7 +147,7 @@ mod tests { 0x0a, 0x05, 0x01, 0x03, 0x00, 0x01, 0x0b, ]; - let module = Module::from_bytes(&input).unwrap(); + let module = WasmModule::from_bytes(&input).unwrap(); let translator = BinaryenOptimiser::with_preset("O0").unwrap(); let result = translator.translate(&module).unwrap().unwrap(); let serialized = result.to_bytes().unwrap(); From 7cfd88893ee278b54397c2ec9a983aaf39a01708 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Sun, 29 Dec 2019 00:41:53 +0100 Subject: [PATCH 5/8] f --- libchisel/src/checkfloat.rs | 10 +++++----- libchisel/src/checkstartfunc.rs | 14 ++++++-------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/libchisel/src/checkfloat.rs b/libchisel/src/checkfloat.rs index 26c57be..db78383 100644 --- a/libchisel/src/checkfloat.rs +++ b/libchisel/src/checkfloat.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use parity_wasm::elements::{Instruction, Module}; -use super::{ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModuleValidator}; +use super::{ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModuleValidator, WasmModule}; /// Struct on which ModuleValidator is implemented. pub struct CheckFloat {} @@ -41,7 +41,7 @@ impl CheckFloat { impl ModuleValidator for CheckFloat { // NOTE: this will not check for SIMD instructions. - fn validate(&self, module: &Module) -> Result { + fn validate(&self, module: &WasmModule) -> Result { let code_section = module.code_section(); if code_section.is_none() { return Err(ModuleError::NotFound); @@ -148,7 +148,7 @@ mod tests { 0x7f, 0x01, 0x7f, 0x03, 0x02, 0x01, 0x00, 0x07, 0x07, 0x01, 0x03, 0x61, 0x64, 0x64, 0x00, 0x00, 0x0a, 0x09, 0x01, 0x07, 0x00, 0x20, 0x00, 0x20, 0x01, 0x6a, 0x0b, ]; - let module = Module::from_bytes(&wasm).unwrap(); + let module = WasmModule::from_bytes(&wasm).unwrap(); let checker = CheckFloat::new(); let result = checker.validate(&module).unwrap(); assert_eq!(true, result); @@ -168,7 +168,7 @@ mod tests { 0x7d, 0x01, 0x7d, 0x03, 0x02, 0x01, 0x00, 0x07, 0x07, 0x01, 0x03, 0x61, 0x64, 0x64, 0x00, 0x00, 0x0a, 0x09, 0x01, 0x07, 0x00, 0x20, 0x00, 0x20, 0x01, 0x92, 0x0b, ]; - let module = Module::from_bytes(&wasm).unwrap(); + let module = WasmModule::from_bytes(&wasm).unwrap(); let checker = CheckFloat::new(); let result = checker.validate(&module).unwrap(); assert_eq!(false, result); @@ -188,7 +188,7 @@ mod tests { 0x7c, 0x01, 0x7c, 0x03, 0x02, 0x01, 0x00, 0x07, 0x07, 0x01, 0x03, 0x61, 0x64, 0x64, 0x00, 0x00, 0x0a, 0x09, 0x01, 0x07, 0x00, 0x20, 0x00, 0x20, 0x01, 0xa0, 0x0b, ]; - let module = Module::from_bytes(&wasm).unwrap(); + let module = WasmModule::from_bytes(&wasm).unwrap(); let checker = CheckFloat::new(); let result = checker.validate(&module).unwrap(); assert_eq!(false, result); diff --git a/libchisel/src/checkstartfunc.rs b/libchisel/src/checkstartfunc.rs index 844a5ff..65bf8a0 100644 --- a/libchisel/src/checkstartfunc.rs +++ b/libchisel/src/checkstartfunc.rs @@ -1,8 +1,6 @@ use std::collections::HashMap; -use parity_wasm::elements::Module; - -use super::{ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModuleValidator}; +use super::{ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModuleValidator, WasmModule}; /// Struct on which ModuleValidator is implemented. pub struct CheckStartFunc { @@ -51,7 +49,7 @@ impl ModuleConfig for CheckStartFunc { } impl ModuleValidator for CheckStartFunc { - fn validate(&self, module: &Module) -> Result { + fn validate(&self, module: &WasmModule) -> Result { Ok(module.start_section().is_some() == self.start_required) } } @@ -68,7 +66,7 @@ mod tests { 0x08, 0x01, 0x00, 0x0a, 0x04, 0x01, 0x02, 0x00, 0x0b, ]; - let module = Module::from_bytes(&wasm).unwrap(); + let module = WasmModule::from_bytes(&wasm).unwrap(); let checker = CheckStartFunc::new(true); let result = checker.validate(&module).unwrap(); @@ -83,7 +81,7 @@ mod tests { 0x08, 0x01, 0x00, 0x0a, 0x04, 0x01, 0x02, 0x00, 0x0b, ]; - let module = Module::from_bytes(&wasm).unwrap(); + let module = WasmModule::from_bytes(&wasm).unwrap(); let checker = CheckStartFunc::new(false); let result = checker.validate(&module).unwrap(); @@ -98,7 +96,7 @@ mod tests { 0x0a, 0x04, 0x01, 0x02, 0x00, 0x0b, ]; - let module = Module::from_bytes(&wasm).unwrap(); + let module = WasmModule::from_bytes(&wasm).unwrap(); let checker = CheckStartFunc::new(false); let result = checker.validate(&module).unwrap(); @@ -113,7 +111,7 @@ mod tests { 0x0a, 0x04, 0x01, 0x02, 0x00, 0x0b, ]; - let module = Module::from_bytes(&wasm).unwrap(); + let module = WasmModule::from_bytes(&wasm).unwrap(); let checker = CheckStartFunc::new(true); let result = checker.validate(&module).unwrap(); From 38e8dbf3abe325520c67bb66e1a8f6b6692879d3 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Sun, 29 Dec 2019 00:44:09 +0100 Subject: [PATCH 6/8] f --- libchisel/src/deployer.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/libchisel/src/deployer.rs b/libchisel/src/deployer.rs index d2392a2..01a818f 100644 --- a/libchisel/src/deployer.rs +++ b/libchisel/src/deployer.rs @@ -1,9 +1,9 @@ use std::collections::HashMap; use parity_wasm::builder; -use parity_wasm::elements::{CustomSection, Module}; +use parity_wasm::elements::CustomSection; -use super::{ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModulePreset, ModuleTranslator}; +use super::{ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModulePreset, ModuleTranslator, WasmModule}; /// Enum on which ModuleTranslator is implemented. pub enum Deployer { @@ -94,13 +94,13 @@ fn deployer_code() -> Vec { } /// Returns a module which contains the deployable bytecode as a custom section. -fn create_custom_deployer(payload: &[u8]) -> Result { +fn create_custom_deployer(payload: &[u8]) -> Result { // The standard deployer code, which expects a 32 bit little endian as the trailing content // immediately following the payload, placed in a custom section. let code = deployer_code(); // This is the pre-written deployer code. - let mut module = Module::from_bytes(&code)?; + let mut module = WasmModule::from_bytes(&code)?; // Re-write memory to pre-allocate enough for code size let memory_initial = (payload.len() as u32 / 65536) + 1; @@ -129,7 +129,7 @@ fn create_custom_deployer(payload: &[u8]) -> Result { /// Returns a module which contains the deployable bytecode as a data segment. #[rustfmt::skip] -fn create_memory_deployer(payload: &[u8]) -> Module { +fn create_memory_deployer(payload: &[u8]) -> WasmModule { // Instructions calling finish(0, payload_len) let instructions = vec![ parity_wasm::elements::Instruction::I32Const(0), @@ -183,11 +183,11 @@ fn create_memory_deployer(payload: &[u8]) -> Module { } impl ModuleTranslator for Deployer { - fn translate_inplace(&self, _module: &mut Module) -> Result { + fn translate_inplace(&self, _module: &mut WasmModule) -> Result { Err(ModuleError::NotSupported) } - fn translate(&self, module: &Module) -> Result, ModuleError> { + fn translate(&self, module: &WasmModule) -> Result, ModuleError> { let payload = module.clone().to_bytes()?; let output = match self { Deployer::Memory => create_memory_deployer(&payload), @@ -302,7 +302,7 @@ mod tests { #[test] fn customsection_interface_test() { - let payload = Module::default(); + let payload = WasmModule::default(); let module = Deployer::with_preset("customsection") .unwrap() .translate(&payload) @@ -325,7 +325,7 @@ mod tests { #[test] fn memory_interface_test() { - let payload = Module::default(); + let payload = WasmModule::default(); let module = Deployer::with_preset("memory") .unwrap() .translate(&payload) From b3e356c7a5f8229d92dc8ea7fea124262eb96f29 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Sun, 29 Dec 2019 00:46:01 +0100 Subject: [PATCH 7/8] f --- chisel/src/driver.rs | 6 +++--- chisel/src/result.rs | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/chisel/src/driver.rs b/chisel/src/driver.rs index 1452adc..0011eb2 100644 --- a/chisel/src/driver.rs +++ b/chisel/src/driver.rs @@ -17,7 +17,7 @@ use libchisel::{ checkfloat::CheckFloat, checkstartfunc::CheckStartFunc, deployer::Deployer, dropsection::DropSection, remapimports::RemapImports, remapstart::RemapStart, repack::Repack, snip::Snip, trimexports::TrimExports, trimstartfunc::TrimStartFunc, - verifyexports::VerifyExports, verifyimports::VerifyImports, Module, ModulePreset, + verifyexports::VerifyExports, verifyimports::VerifyImports, WasmModule, ModulePreset, ModuleTranslator, ModuleValidator, }; @@ -161,7 +161,7 @@ impl ChiselDriver { }; // Deserialize the Wasm binary and parse its names section. - let mut wasm = match Module::from_bytes(wasm_raw) { + let mut wasm = match WasmModule::from_bytes(wasm_raw) { Ok(wasm) => { chisel_debug!(1, "Successfully deserialized Wasm module"); // TODO: Make this error recoverable @@ -216,7 +216,7 @@ impl ChiselDriver { &mut self, name: String, module: ModuleConfig, - wasm: &mut Module, + wasm: &mut WasmModule, ) -> Result { let result = match name.as_str() { "checkfloat" => { diff --git a/chisel/src/result.rs b/chisel/src/result.rs index 6ffd251..9a310c9 100644 --- a/chisel/src/result.rs +++ b/chisel/src/result.rs @@ -15,7 +15,7 @@ use std::path::PathBuf; use ansi_term::Colour::{Green, Red, Yellow}; -use libchisel::{Module, ModuleError}; +use libchisel::{WasmModule, ModuleError}; #[derive(Clone)] /// Main result structure returned by ChiselDriver, containing a manifest of modules executed and @@ -28,7 +28,7 @@ pub struct RulesetResult { ruleset_name: String, results: Vec, output_path: PathBuf, - output_module: Option, + output_module: Option, } #[derive(Clone)] @@ -76,7 +76,7 @@ impl RulesetResult { self.output_path = path; } - pub fn set_output_module(&mut self, module: Module) { + pub fn set_output_module(&mut self, module: WasmModule) { self.output_module = Some(module); } @@ -204,7 +204,7 @@ mod tests { fn writer_success_to_stdout() { let mut ruleset_result = { let mut result = RulesetResult::new("Test".to_string()); - let module = Module::default(); + let module = WasmModule::default(); result.set_output_module(module); result.set_output_path(PathBuf::from("/dev/stdout")); result @@ -225,7 +225,7 @@ mod tests { fn writer_deny_raw_binary_to_stdout() { let mut ruleset_result = { let mut result = RulesetResult::new("Test".to_string()); - let module = Module::default(); + let module = WasmModule::default(); result.set_output_module(module); result.set_output_path(PathBuf::from("/dev/stdout")); result @@ -239,7 +239,7 @@ mod tests { fn writer_invalid_mode() { let mut ruleset_result = { let mut result = RulesetResult::new("Test".to_string()); - let module = Module::default(); + let module = WasmModule::default(); result.set_output_module(module); result.set_output_path(PathBuf::from("/dev/stdout")); result From 93f6e5321410e31eec2959bc20b012978303593f Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Sat, 18 Jan 2020 23:53:42 +0000 Subject: [PATCH 8/8] f --- libchisel/src/binaryenopt.rs | 4 +++- libchisel/src/deployer.rs | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/libchisel/src/binaryenopt.rs b/libchisel/src/binaryenopt.rs index 178a23b..d4e78f7 100644 --- a/libchisel/src/binaryenopt.rs +++ b/libchisel/src/binaryenopt.rs @@ -2,7 +2,9 @@ use std::collections::HashMap; use parity_wasm::elements::Module; -use super::{ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModulePreset, ModuleTranslator, WasmModule}; +use super::{ + ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModulePreset, ModuleTranslator, WasmModule, +}; // FIXME: change level names pub enum BinaryenOptimiser { diff --git a/libchisel/src/deployer.rs b/libchisel/src/deployer.rs index 01a818f..dd7a26c 100644 --- a/libchisel/src/deployer.rs +++ b/libchisel/src/deployer.rs @@ -3,7 +3,9 @@ use std::collections::HashMap; use parity_wasm::builder; use parity_wasm::elements::CustomSection; -use super::{ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModulePreset, ModuleTranslator, WasmModule}; +use super::{ + ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModulePreset, ModuleTranslator, WasmModule, +}; /// Enum on which ModuleTranslator is implemented. pub enum Deployer {