From 9a4bd3cfbbd34bc54cac5cca4b553248c52a73e7 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 3 Apr 2019 16:08:41 +0100 Subject: [PATCH 1/3] Reject validation of contract with unknown exports --- srml/contract/src/wasm/prepare.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/srml/contract/src/wasm/prepare.rs b/srml/contract/src/wasm/prepare.rs index 52f1580aa9fec..dd3fd55383fe7 100644 --- a/srml/contract/src/wasm/prepare.rs +++ b/srml/contract/src/wasm/prepare.rs @@ -108,6 +108,8 @@ impl<'a, Gas: 'a + As + Clone> ContractModule<'a, Gas> { /// /// - 'call' /// - 'deploy' + /// + /// Any other exports are not allowed. fn scan_exports(&self) -> Result<(), &'static str> { let mut deploy_found = false; let mut call_found = false; @@ -147,7 +149,7 @@ impl<'a, Gas: 'a + As + Clone> ContractModule<'a, Gas> { match export.field() { "call" => call_found = true, "deploy" => deploy_found = true, - _ => continue, + _ => return Err("unknown export: expecting only deploy and call functions"), } // Then check the export kind. "call" and "deploy" are @@ -582,5 +584,16 @@ mod tests { "#, Err("entry point has wrong signature") ); + + prepare_test!(unknown_exports, + r#" + (module + (func (export "call")) + (func (export "deploy")) + (func (export "whatevs")) + ) + "#, + Err("unknown export: expecting only deploy and call functions") + ); } } From b204f517e3c2f69b4ac020c575c85800742e54f3 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 3 Apr 2019 17:32:51 +0100 Subject: [PATCH 2/3] Validate imports eagerly --- srml/contract/src/wasm/prepare.rs | 57 ++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/srml/contract/src/wasm/prepare.rs b/srml/contract/src/wasm/prepare.rs index dd3fd55383fe7..50edff32971a9 100644 --- a/srml/contract/src/wasm/prepare.rs +++ b/srml/contract/src/wasm/prepare.rs @@ -220,12 +220,19 @@ impl<'a, Gas: 'a + As + Clone> ContractModule<'a, Gas> { } let type_idx = match import.external() { + &External::Table(_) => return Err("Cannot import tables"), + &External::Global(_) => return Err("Cannot import globals"), &External::Function(ref type_idx) => type_idx, &External::Memory(ref memory_type) => { + if import.field() != "memory" { + return Err("Memory import must have the field name 'memory'") + } + if imported_mem_type.is_some() { + return Err("Multiple memory imports defined") + } imported_mem_type = Some(memory_type); continue; } - _ => continue, }; let Type::Function(ref func_ty) = types @@ -449,6 +456,54 @@ mod tests { "#, Err("Maximum number of pages should not exceed the configured maximum.") ); + + prepare_test!(field_name_not_memory, + r#" + (module + (import "env" "forgetit" (memory 1 1)) + + (func (export "call")) + (func (export "deploy")) + ) + "#, + Err("Memory import must have the field name 'memory'") + ); + + prepare_test!(multiple_memory_imports, + r#" + (module + (import "env" "memory" (memory 1 1)) + (import "env" "memory" (memory 1 1)) + + (func (export "call")) + (func (export "deploy")) + ) + "#, + Err("Multiple memory imports defined") + ); + + prepare_test!(table_import, + r#" + (module + (import "env" "table" (table 1 anyfunc)) + + (func (export "call")) + (func (export "deploy")) + ) + "#, + Err("Cannot import tables") + ); + + prepare_test!(global_import, + r#" + (module + (global $g (import "env" "global") (mut i32)) + (func (export "call")) + (func (export "deploy")) + ) + "#, + Err("Cannot import globals") + ); } mod imports { From 6b95968ab85010a526f75c3a779bcf06a3463ba7 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 4 Apr 2019 09:50:26 +0100 Subject: [PATCH 3/3] Increment spec version --- node/runtime/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/node/runtime/src/lib.rs b/node/runtime/src/lib.rs index d74e7a17e73b5..94b910ed00b90 100644 --- a/node/runtime/src/lib.rs +++ b/node/runtime/src/lib.rs @@ -59,7 +59,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_name: create_runtime_str!("node"), impl_name: create_runtime_str!("substrate-node"), authoring_version: 10, - spec_version: 57, + spec_version: 58, impl_version: 58, apis: RUNTIME_API_VERSIONS, };