mirror of
https://github.com/fluencelabs/parity-wasm
synced 2025-06-17 08:51:47 +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
|
// there must be no greater than 1 table in tables index space
|
||||||
if context.tables().len() > 1 {
|
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())));
|
return Err(Error(format!("too many memory regions in index space: {}", context.memories().len())));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// TODO: data section
|
||||||
|
// TODO: element section
|
||||||
|
|
||||||
let ModuleContext {
|
let ModuleContext {
|
||||||
types,
|
types,
|
||||||
tables,
|
tables,
|
||||||
@ -128,8 +153,6 @@ pub fn validate_module(module: &Module) -> Result<ValidatedModule, Error> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn prepare_context(module: &Module) -> Result<ModuleContext, Error> {
|
fn prepare_context(module: &Module) -> Result<ModuleContext, Error> {
|
||||||
// TODO: Validate imports
|
|
||||||
|
|
||||||
// Copy types from module as is.
|
// Copy types from module as is.
|
||||||
let types = module
|
let types = module
|
||||||
.type_section()
|
.type_section()
|
||||||
|
@ -251,6 +251,33 @@ fn funcs() {
|
|||||||
assert!(validate_module(&m).is_ok());
|
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
|
// TODO: pepyakin
|
||||||
// #[test]
|
// #[test]
|
||||||
// fn if_else_with_return_type_validation() {
|
// fn if_else_with_return_type_validation() {
|
||||||
|
Reference in New Issue
Block a user