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 diff --git a/libchisel/src/binaryenopt.rs b/libchisel/src/binaryenopt.rs index 7236e7a..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}; +use super::{ + ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModulePreset, ModuleTranslator, WasmModule, +}; // FIXME: change level names pub enum BinaryenOptimiser { @@ -61,11 +63,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 +111,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 +149,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(); 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(); diff --git a/libchisel/src/deployer.rs b/libchisel/src/deployer.rs index d2392a2..dd7a26c 100644 --- a/libchisel/src/deployer.rs +++ b/libchisel/src/deployer.rs @@ -1,9 +1,11 @@ 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 +96,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 +131,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 +185,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 +304,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 +327,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) diff --git a/libchisel/src/lib.rs b/libchisel/src/lib.rs index 1d25e35..9c05ba1 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}; @@ -50,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 { @@ -141,22 +141,22 @@ mod tests { struct SampleModule {} impl ModuleCreator for SampleModule { - fn create(&self) -> Result { - Ok(Module::default()) + fn create(&self) -> Result { + Ok(WasmModule::default()) } } impl ModuleTranslator for SampleModule { - fn translate(&self, _module: &Module) -> Result, ModuleError> { - Ok(Some(Module::default())) + fn translate(&self, _module: &WasmModule) -> Result, ModuleError> { + Ok(Some(WasmModule::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) } } @@ -189,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()); } @@ -247,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()); } }