Files
parity-wasm/src/validation/mod.rs

165 lines
3.3 KiB
Rust
Raw Normal View History

2017-12-01 09:07:56 +03:00
#![allow(unused, missing_docs)]
2017-12-01 15:35:01 +03:00
mod module;
mod func;
use std::fmt;
2017-12-01 09:05:33 +03:00
use elements::{Module, ResizableLimits, MemoryType, TableType};
2017-12-01 15:35:01 +03:00
use common::stack;
pub struct Error(String);
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
2017-12-01 08:58:25 +03:00
2017-12-01 15:35:01 +03:00
impl From<stack::Error> for Error {
fn from(e: stack::Error) -> Error {
Error(format!("Stack: {}", e))
}
}
2017-12-01 08:58:25 +03:00
pub fn validate_module(module: &Module) -> Result<(), Error> {
2017-12-01 09:07:56 +03:00
// TODO: Functions
if let Some(table_section) = module.table_section() {
table_section
2017-12-01 08:58:25 +03:00
.entries()
.iter()
2017-12-01 09:07:56 +03:00
.map(TableType::validate)
2017-12-01 08:58:25 +03:00
.collect::<Result<_, _>>()?
}
2017-12-01 09:07:56 +03:00
if let Some(mem_section) = module.memory_section() {
mem_section
2017-12-01 09:05:33 +03:00
.entries()
.iter()
2017-12-01 09:07:56 +03:00
.map(MemoryType::validate)
2017-12-01 09:05:33 +03:00
.collect::<Result<_, _>>()?
}
2017-12-01 08:58:25 +03:00
Ok(())
}
impl ResizableLimits {
fn validate(&self) -> Result<(), Error> {
if let Some(maximum) = self.maximum() {
2017-12-01 09:00:42 +03:00
if self.initial() > maximum {
2017-12-01 08:58:25 +03:00
return Err(Error(format!(
"maximum limit {} is lesser than minimum {}",
maximum,
self.initial()
)));
}
}
Ok(())
}
}
impl MemoryType {
fn validate(&self) -> Result<(), Error> {
self.limits().validate()
}
}
2017-12-01 09:05:33 +03:00
impl TableType {
fn validate(&self) -> Result<(), Error> {
self.limits().validate()
}
}
2017-12-01 08:58:25 +03:00
#[cfg(test)]
mod tests {
use super::validate_module;
use builder::module;
use elements::{BlockType, ExportEntry, External, FunctionType, GlobalEntry, GlobalType,
ImportEntry, InitExpr, Internal, MemoryType, Opcode, Opcodes, TableType,
ValueType};
#[test]
fn empty_is_valid() {
let module = module().build();
assert!(validate_module(&module).is_ok());
}
#[test]
fn mem_limits() {
// min > max
let m = module()
.memory()
.with_min(10)
.with_max(Some(9))
.build()
.build();
assert!(validate_module(&m).is_err());
2017-12-01 09:00:42 +03:00
// min = max
let m = module()
.memory()
.with_min(10)
.with_max(Some(10))
.build()
.build();
assert!(validate_module(&m).is_ok());
2017-12-01 09:05:33 +03:00
// mem is always valid without max.
2017-12-01 08:58:25 +03:00
let m = module()
.memory()
.with_min(10)
.build()
.build();
assert!(validate_module(&m).is_ok());
}
2017-12-01 09:05:33 +03:00
#[test]
fn table_limits() {
// min > max
let m = module()
.table()
.with_min(10)
.with_max(Some(9))
.build()
.build();
assert!(validate_module(&m).is_err());
// min = max
let m = module()
.table()
.with_min(10)
.with_max(Some(10))
.build()
.build();
assert!(validate_module(&m).is_ok());
// table is always valid without max.
let m = module()
.table()
.with_min(10)
.build()
.build();
assert!(validate_module(&m).is_ok());
}
2017-12-01 15:35:01 +03:00
// #[test]
// fn if_else_with_return_type_validation() {
// let module_instance = ModuleInstance::new(Weak::default(), "test".into(), module().build()).unwrap();
// let mut context = FunctionValidationContext::new(&module_instance, None, &[], 1024, 1024, FunctionSignature::Module(&FunctionType::default()));
// Validator::validate_function(&mut context, BlockType::NoResult, &[
// Opcode::I32Const(1),
// Opcode::If(BlockType::NoResult),
// Opcode::I32Const(1),
// Opcode::If(BlockType::Value(ValueType::I32)),
// Opcode::I32Const(1),
// Opcode::Else,
// Opcode::I32Const(2),
// Opcode::End,
// Opcode::Drop,
// Opcode::End,
// Opcode::End,
// ]).unwrap();
// }
2017-12-01 08:58:25 +03:00
}