Deny floating-point parameters and fix docs/indentation

This commit is contained in:
Vurich 2018-04-17 10:06:36 +02:00
parent 6845d0f54e
commit cfc7f1b693
2 changed files with 28 additions and 8 deletions

View File

@ -430,9 +430,7 @@ impl Module {
}) })
} }
/// Create `Module` from `parity_wasm::elements::Module`. /// Fail if the module contains any floating-point operations
///
/// This function will load, validate and prepare a `parity_wasm`'s `Module`.
/// ///
/// # Errors /// # Errors
/// ///

View File

@ -56,10 +56,10 @@ pub fn deny_floating_point(module: &Module) -> Result<(), Error> {
use parity_wasm::elements::Opcode::*; use parity_wasm::elements::Opcode::*;
macro_rules! match_eq { macro_rules! match_eq {
($pattern:pat) => { ($pattern:pat) => {
|val| if let $pattern = *val { true } else { false } |val| if let $pattern = *val { true } else { false }
} };
} }
const DENIED: &[fn(&Opcode) -> bool] = &[ const DENIED: &[fn(&Opcode) -> bool] = &[
match_eq!(F32Load(_, _)), 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)) { 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")));
}
}
}
} }
} }
} }