From b22070966b105462b48bcfd4467d2ddca6f28ddf Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Mon, 17 Dec 2018 12:56:19 +0000 Subject: [PATCH 1/2] Introduce ModuleError enum for errors --- libchisel/src/checkstartfunc.rs | 4 ++-- libchisel/src/deployer.rs | 4 ++-- libchisel/src/lib.rs | 21 +++++++++++++-------- libchisel/src/remapimports.rs | 6 +++--- libchisel/src/trimexports.rs | 6 +++--- libchisel/src/verifyexports.rs | 4 ++-- libchisel/src/verifyimports.rs | 4 ++-- 7 files changed, 27 insertions(+), 22 deletions(-) diff --git a/libchisel/src/checkstartfunc.rs b/libchisel/src/checkstartfunc.rs index 004af69..30b1062 100644 --- a/libchisel/src/checkstartfunc.rs +++ b/libchisel/src/checkstartfunc.rs @@ -1,4 +1,4 @@ -use super::ModuleValidator; +use super::{ModuleError, ModuleValidator}; use parity_wasm::elements::Module; /// Struct on which ModuleValidator is implemented. @@ -15,7 +15,7 @@ impl CheckStartFunc { } impl ModuleValidator for CheckStartFunc { - fn validate(&self, module: &Module) -> Result { + fn validate(&self, module: &Module) -> Result { Ok(module.start_section().is_some() == self.start_required) } } diff --git a/libchisel/src/deployer.rs b/libchisel/src/deployer.rs index b9e995e..f3082b5 100644 --- a/libchisel/src/deployer.rs +++ b/libchisel/src/deployer.rs @@ -1,4 +1,4 @@ -use super::ModuleCreator; +use super::{ModuleCreator, ModuleError}; use parity_wasm::builder; use parity_wasm::elements::{CustomSection, Module}; @@ -148,7 +148,7 @@ fn create_memory_deployer(payload: &[u8]) -> Module { } impl<'a> ModuleCreator for Deployer<'a> { - fn create(&self) -> Result { + fn create(&self) -> Result { let output = match self { Deployer::Memory(payload) => create_memory_deployer(&payload), Deployer::CustomSection(payload) => create_custom_deployer(&payload), diff --git a/libchisel/src/lib.rs b/libchisel/src/lib.rs index 0f42969..2d759c1 100644 --- a/libchisel/src/lib.rs +++ b/libchisel/src/lib.rs @@ -11,22 +11,27 @@ pub mod trimexports; pub mod verifyexports; pub mod verifyimports; +#[derive(Eq, PartialEq, Debug)] +pub enum ModuleError { + Custom(String), +} + pub trait ModuleCreator { /// Returns new module. - fn create(&self) -> Result; + fn create(&self) -> Result; } pub trait ModuleTranslator { /// Translates module. Returns new module. - fn translate(&self, module: &Module) -> Result; + fn translate(&self, module: &Module) -> Result; /// Translates module in-place. Returns true if the module was modified. - fn translate_inplace(&self, module: &mut Module) -> Result; + fn translate_inplace(&self, module: &mut Module) -> 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: &Module) -> Result; } #[cfg(test)] @@ -36,22 +41,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 { + fn translate(&self, module: &Module) -> Result { Ok(Module::default()) } - fn translate_inplace(&self, module: &mut Module) -> Result { + fn translate_inplace(&self, module: &mut Module) -> Result { Ok((true)) } } impl ModuleValidator for SampleModule { - fn validate(&self, module: &Module) -> Result { + fn validate(&self, module: &Module) -> Result { Ok(true) } } diff --git a/libchisel/src/remapimports.rs b/libchisel/src/remapimports.rs index 4629a69..a61a54b 100644 --- a/libchisel/src/remapimports.rs +++ b/libchisel/src/remapimports.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; -use super::ModuleTranslator; +use super::{ModuleError, ModuleTranslator}; use parity_wasm::elements::*; #[derive(Hash, Eq, PartialEq, Debug, Clone)] @@ -204,11 +204,11 @@ impl RemapImports { } impl ModuleTranslator for RemapImports { - fn translate_inplace(&self, module: &mut Module) -> Result { + fn translate_inplace(&self, module: &mut Module) -> Result { Ok(rename_imports(module, &self.translations)) } - fn translate(&self, module: &Module) -> Result { + fn translate(&self, module: &Module) -> Result { let mut ret = module.clone(); rename_imports(&mut ret, &self.translations); Ok(ret) diff --git a/libchisel/src/trimexports.rs b/libchisel/src/trimexports.rs index 50a9a1f..94dfb3e 100644 --- a/libchisel/src/trimexports.rs +++ b/libchisel/src/trimexports.rs @@ -1,4 +1,4 @@ -use super::ModuleTranslator; +use super::{ModuleError, ModuleTranslator}; use parity_wasm::elements::*; /// Struct containing a list of valid exports. @@ -102,11 +102,11 @@ impl TrimExports { } impl ModuleTranslator for TrimExports { - fn translate_inplace(&self, module: &mut Module) -> Result { + fn translate_inplace(&self, module: &mut Module) -> Result { Ok(self.trim_exports(module)) } - fn translate(&self, module: &Module) -> Result { + fn translate(&self, module: &Module) -> Result { let mut ret = module.clone(); self.trim_exports(&mut ret); Ok(ret) diff --git a/libchisel/src/verifyexports.rs b/libchisel/src/verifyexports.rs index c17713a..d7275ef 100644 --- a/libchisel/src/verifyexports.rs +++ b/libchisel/src/verifyexports.rs @@ -1,4 +1,4 @@ -use super::ModuleValidator; +use super::{ModuleError, ModuleValidator}; use parity_wasm::elements::{ ExportSection, External, FunctionSection, FunctionType, ImportSection, Internal, Module, Type, }; @@ -38,7 +38,7 @@ impl<'a> VerifyExports<'a> { } impl<'a> ModuleValidator for VerifyExports<'a> { - fn validate(&self, module: &Module) -> Result { + fn validate(&self, module: &Module) -> Result { // FIXME: This validating algorithm runs in O(n^2). Needs to be optimized let required_exports_not_found = self .entries diff --git a/libchisel/src/verifyimports.rs b/libchisel/src/verifyimports.rs index 653cdee..08f8d32 100644 --- a/libchisel/src/verifyimports.rs +++ b/libchisel/src/verifyimports.rs @@ -1,4 +1,4 @@ -use super::ModuleValidator; +use super::{ModuleError, ModuleValidator}; use parity_wasm::elements::{External, FunctionType, ImportSection, Module, Type, ValueType}; /// Enum representing a type of import and any extra data to check. @@ -295,7 +295,7 @@ impl<'a> VerifyImports<'a> { } impl<'a> ModuleValidator for VerifyImports<'a> { - fn validate(&self, module: &Module) -> Result { + fn validate(&self, module: &Module) -> Result { let import_section_len = if let Some(section) = module.import_section() { section.entries().len() } else { From deeaa7f0de918f8e8daf9c290a490ac551d84626 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Mon, 17 Dec 2018 13:17:25 +0000 Subject: [PATCH 2/2] Introduce ModuleError::NotSupported --- libchisel/src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/libchisel/src/lib.rs b/libchisel/src/lib.rs index 2d759c1..5b65eb9 100644 --- a/libchisel/src/lib.rs +++ b/libchisel/src/lib.rs @@ -13,6 +13,7 @@ pub mod verifyimports; #[derive(Eq, PartialEq, Debug)] pub enum ModuleError { + NotSupported, Custom(String), } @@ -22,10 +23,10 @@ pub trait ModuleCreator { } pub trait ModuleTranslator { - /// Translates module. Returns new module. + /// Translates module. Returns new module. Can fail with ModuleError::NotSupported. fn translate(&self, module: &Module) -> Result; - /// Translates module in-place. Returns true if the module was modified. + /// Translates module in-place. Returns true if the module was modified. Can fail with ModuleError::NotSupported. fn translate_inplace(&self, module: &mut Module) -> Result; }