From 11e4cd6ba116efe54451de81a616dabd06c641a7 Mon Sep 17 00:00:00 2001 From: Sergey Pepyakin Date: Tue, 5 Dec 2017 12:57:42 +0100 Subject: [PATCH] Import section validation --- src/validation/mod.rs | 27 +++++++++++++++++++++++++-- src/validation/tests.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/validation/mod.rs b/src/validation/mod.rs index c490417..af8ab91 100644 --- a/src/validation/mod.rs +++ b/src/validation/mod.rs @@ -99,6 +99,27 @@ pub fn validate_module(module: &Module) -> Result { } } + // 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 { 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 { } fn prepare_context(module: &Module) -> Result { - // TODO: Validate imports - // Copy types from module as is. let types = module .type_section() diff --git a/src/validation/tests.rs b/src/validation/tests.rs index 460d276..43f4f58 100644 --- a/src/validation/tests.rs +++ b/src/validation/tests.rs @@ -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() {