Import section validation

This commit is contained in:
Sergey Pepyakin
2017-12-05 12:57:42 +01:00
parent 056ac258cd
commit 11e4cd6ba1
2 changed files with 52 additions and 2 deletions

View File

@ -99,6 +99,27 @@ pub fn validate_module(module: &Module) -> Result<ValidatedModule, Error> {
}
}
// validate import section
if let Some(import_section) = module.import_section() {
for import in import_section.entries() {
match import.external() {
&External::Function(function_type_index) => {
context.require_function(function_type_index)?;
},
&External::Global(ref global_type) => {
if global_type.is_mutable() {
return Err(Error(format!("trying to import mutable global {}", import.field())));
}
},
&External::Memory(ref memory_type) => {
memory_type.validate()?;
},
&External::Table(ref table_type) => {
table_type.validate()?;
},
}
}
}
// there must be no greater than 1 table in tables index space
if context.tables().len() > 1 {
@ -110,6 +131,10 @@ pub fn validate_module(module: &Module) -> Result<ValidatedModule, Error> {
return Err(Error(format!("too many memory regions in index space: {}", context.memories().len())));
}
// TODO: data section
// TODO: element section
let ModuleContext {
types,
tables,
@ -128,8 +153,6 @@ pub fn validate_module(module: &Module) -> Result<ValidatedModule, Error> {
}
fn prepare_context(module: &Module) -> Result<ModuleContext, Error> {
// TODO: Validate imports
// Copy types from module as is.
let types = module
.type_section()

View File

@ -251,6 +251,33 @@ fn funcs() {
assert!(validate_module(&m).is_ok());
}
#[test]
fn globals() {
// import immutable global is legal.
let m = module()
.with_import(
ImportEntry::new(
"env".into(),
"ext_global".into(),
External::Global(GlobalType::new(ValueType::I32, false))
)
)
.build();
assert!(validate_module(&m).is_ok());
// import mutable global is invalid.
let m = module()
.with_import(
ImportEntry::new(
"env".into(),
"ext_global".into(),
External::Global(GlobalType::new(ValueType::I32, true))
)
)
.build();
assert!(validate_module(&m).is_err());
}
// TODO: pepyakin
// #[test]
// fn if_else_with_return_type_validation() {