From cfc7f1b6931934259a2c705513c8f260331afdba Mon Sep 17 00:00:00 2001 From: Vurich Date: Tue, 17 Apr 2018 10:06:36 +0200 Subject: [PATCH] Deny floating-point parameters and fix docs/indentation --- src/lib.rs | 4 +--- src/validation/mod.rs | 32 +++++++++++++++++++++++++++----- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 91782ef..20a1024 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -430,9 +430,7 @@ impl Module { }) } - /// Create `Module` from `parity_wasm::elements::Module`. - /// - /// This function will load, validate and prepare a `parity_wasm`'s `Module`. + /// Fail if the module contains any floating-point operations /// /// # Errors /// diff --git a/src/validation/mod.rs b/src/validation/mod.rs index df83d72..268f420 100644 --- a/src/validation/mod.rs +++ b/src/validation/mod.rs @@ -56,10 +56,10 @@ pub fn deny_floating_point(module: &Module) -> Result<(), Error> { use parity_wasm::elements::Opcode::*; macro_rules! match_eq { - ($pattern:pat) => { - |val| if let $pattern = *val { true } else { false } - } - } + ($pattern:pat) => { + |val| if let $pattern = *val { true } else { false } + }; + } const DENIED: &[fn(&Opcode) -> bool] = &[ match_eq!(F32Load(_, _)), @@ -133,7 +133,29 @@ pub fn deny_floating_point(module: &Module) -> Result<(), Error> { ]; if DENIED.iter().any(|is_denied| is_denied(op)) { - return Err(Error(format!("Unimplemented opcode: {:?}", op))); + return Err(Error(format!("Floating point operation denied: {:?}", op))); + } + } + } + + if let (Some(sec), Some(types)) = (module.function_section(), module.type_section()) { + use parity_wasm::elements::{Type, ValueType}; + + let types = types.types(); + + for sig in sec.entries() { + if let Some(typ) = types.get(sig.type_ref() as usize) { + match *typ { + Type::Function(ref func) => { + if func.params() + .iter() + .chain(func.return_type().as_ref()) + .any(|&typ| typ == ValueType::F32 || typ == ValueType::F64) + { + return Err(Error(format!("Use of floating point types denied"))); + } + } + } } } }