mirror of
https://github.com/fluencelabs/parity-wasm
synced 2025-06-15 07:51:46 +00:00
Import section validation
This commit is contained in:
@ -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()
|
||||
|
@ -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() {
|
||||
|
Reference in New Issue
Block a user