From 087c9fd06c0e02a93da2da64d8c2969a911e10da Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 29 Nov 2022 17:06:38 +0000 Subject: [PATCH 1/5] Remove __unstable__ module check --- crates/cargo-contract/src/validate_wasm.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/crates/cargo-contract/src/validate_wasm.rs b/crates/cargo-contract/src/validate_wasm.rs index c5c31c2b2..698265a4c 100644 --- a/crates/cargo-contract/src/validate_wasm.rs +++ b/crates/cargo-contract/src/validate_wasm.rs @@ -122,18 +122,15 @@ pub fn validate_import_section(module: &Module) -> Result<()> { Ok(()) } -/// Returns `true` if the import is allowed. +/// Returns `Ok` if the import is allowed. fn check_import(module: &str, field: &str) -> Result<(), String> { - if module.starts_with("seal") - || module == "__unstable__" - || field.starts_with("memory") - { + if module.starts_with("seal") || field.starts_with("memory") { Ok(()) } else { Err(format!( "An unexpected import function was found in the contract Wasm: {}.\n\ Import funtions must either be prefixed with 'memory', or part \ - of a module prefixed with 'seal' or '__unstable__", + of a module prefixed with 'seal'", field )) } From 1ee879717d64a5f6a386a02ae71eacb1f5412828 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 29 Nov 2022 17:22:04 +0000 Subject: [PATCH 2/5] Add `skip_wasm_validation` flag --- crates/cargo-contract/src/cmd/build/mod.rs | 15 ++++++++++++--- crates/cargo-contract/src/cmd/build/tests.rs | 3 +++ crates/cargo-contract/src/validate_wasm.rs | 2 +- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/crates/cargo-contract/src/cmd/build/mod.rs b/crates/cargo-contract/src/cmd/build/mod.rs index 99755776d..856ab52a9 100644 --- a/crates/cargo-contract/src/cmd/build/mod.rs +++ b/crates/cargo-contract/src/cmd/build/mod.rs @@ -85,6 +85,7 @@ pub(crate) struct ExecuteArgs { pub keep_debug_symbols: bool, pub lint: bool, pub output_type: OutputType, + pub skip_wasm_validation: bool, } /// Executes build of the smart contract which produces a Wasm binary that is ready for deploying. @@ -158,6 +159,9 @@ pub struct BuildCommand { /// Export the build output in JSON format. #[clap(long, conflicts_with = "verbose")] output_json: bool, + /// Don't perform wasm validation checks e.g. for permitted imports. + #[clap(long)] + skip_wasm_validation: bool, } impl BuildCommand { @@ -219,6 +223,7 @@ impl BuildCommand { keep_debug_symbols: self.keep_debug_symbols, lint: self.lint, output_type, + skip_wasm_validation: self.skip_wasm_validation, }; execute(args) @@ -255,6 +260,7 @@ impl CheckCommand { keep_debug_symbols: false, lint: false, output_type: OutputType::default(), + skip_wasm_validation: false, }; execute(args) @@ -516,7 +522,7 @@ fn load_module>(path: P) -> Result { } /// Performs required post-processing steps on the Wasm artifact. -fn post_process_wasm(crate_metadata: &CrateMetadata) -> Result<()> { +fn post_process_wasm(crate_metadata: &CrateMetadata, skip_wasm_validation: bool) -> Result<()> { // Deserialize Wasm module from a file. let mut module = load_module(&crate_metadata.original_wasm) .context("Loading of original wasm failed")?; @@ -525,7 +531,9 @@ fn post_process_wasm(crate_metadata: &CrateMetadata) -> Result<()> { ensure_maximum_memory_pages(&mut module, MAX_MEMORY_PAGES)?; strip_custom_sections(&mut module); - validate_wasm::validate_import_section(&module)?; + if !skip_wasm_validation { + validate_wasm::validate_import_section(&module)?; + } debug_assert!( !module.clone().into_bytes().unwrap().is_empty(), @@ -596,6 +604,7 @@ pub(crate) fn execute(args: ExecuteArgs) -> Result { keep_debug_symbols, lint, output_type, + skip_wasm_validation, } = args; let crate_metadata = CrateMetadata::collect(&manifest_path)?; @@ -651,7 +660,7 @@ pub(crate) fn execute(args: ExecuteArgs) -> Result { "Post processing wasm file".bright_green().bold() ); build_steps.increment_current(); - post_process_wasm(&crate_metadata)?; + post_process_wasm(&crate_metadata, skip_wasm_validation)?; maybe_println!( verbosity, diff --git a/crates/cargo-contract/src/cmd/build/tests.rs b/crates/cargo-contract/src/cmd/build/tests.rs index 23daedfca..7851f8dd9 100644 --- a/crates/cargo-contract/src/cmd/build/tests.rs +++ b/crates/cargo-contract/src/cmd/build/tests.rs @@ -160,6 +160,7 @@ fn optimization_passes_from_cli_must_take_precedence_over_profile( keep_debug_symbols: false, lint: false, output_json: false, + skip_wasm_validation: false, }; // when @@ -201,6 +202,7 @@ fn optimization_passes_from_profile_must_be_used( keep_debug_symbols: false, lint: false, output_json: false, + skip_wasm_validation: false, }; // when @@ -243,6 +245,7 @@ fn contract_lib_name_different_from_package_name_must_build( keep_debug_symbols: false, lint: false, output_json: false, + skip_wasm_validation: false, }; let res = cmd.exec().expect("build failed"); diff --git a/crates/cargo-contract/src/validate_wasm.rs b/crates/cargo-contract/src/validate_wasm.rs index 698265a4c..f4e3adef4 100644 --- a/crates/cargo-contract/src/validate_wasm.rs +++ b/crates/cargo-contract/src/validate_wasm.rs @@ -112,7 +112,7 @@ pub fn validate_import_section(module: &Module) -> Result<()> { if original_imports_len != filtered_imports.count() { anyhow::bail!(format!( - "Validation of the Wasm failed.\n\n\n{}", + "Validation of the Wasm failed.\n\n\n{}\n\nIgnore with `--skip-wasm-validation`", errs.into_iter() .map(|err| format!("{} {}", "ERROR:".to_string().bold(), err)) .collect::>() From 18159574dfafbac8843c401689ab89b680a0ebaa Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 29 Nov 2022 17:33:59 +0000 Subject: [PATCH 3/5] Print warn when skipping wasm validation. --- crates/cargo-contract/src/cmd/build/mod.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/crates/cargo-contract/src/cmd/build/mod.rs b/crates/cargo-contract/src/cmd/build/mod.rs index 856ab52a9..604aa55ef 100644 --- a/crates/cargo-contract/src/cmd/build/mod.rs +++ b/crates/cargo-contract/src/cmd/build/mod.rs @@ -522,7 +522,11 @@ fn load_module>(path: P) -> Result { } /// Performs required post-processing steps on the Wasm artifact. -fn post_process_wasm(crate_metadata: &CrateMetadata, skip_wasm_validation: bool) -> Result<()> { +fn post_process_wasm( + crate_metadata: &CrateMetadata, + skip_wasm_validation: bool, + verbosity: &Verbosity, +) -> Result<()> { // Deserialize Wasm module from a file. let mut module = load_module(&crate_metadata.original_wasm) .context("Loading of original wasm failed")?; @@ -533,6 +537,12 @@ fn post_process_wasm(crate_metadata: &CrateMetadata, skip_wasm_validation: bool) if !skip_wasm_validation { validate_wasm::validate_import_section(&module)?; + } else { + maybe_println!( + verbosity, + " {}", + "Skipping wasm validation! Contract code may be invalid.".bright_yellow().bold() + ); } debug_assert!( @@ -660,7 +670,7 @@ pub(crate) fn execute(args: ExecuteArgs) -> Result { "Post processing wasm file".bright_green().bold() ); build_steps.increment_current(); - post_process_wasm(&crate_metadata, skip_wasm_validation)?; + post_process_wasm(&crate_metadata, skip_wasm_validation, &verbosity)?; maybe_println!( verbosity, From b05ecd1c49eb0e28fa7db175b864afe1f7c5907f Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 29 Nov 2022 17:35:16 +0000 Subject: [PATCH 4/5] Fmt --- crates/cargo-contract/src/cmd/build/mod.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/cargo-contract/src/cmd/build/mod.rs b/crates/cargo-contract/src/cmd/build/mod.rs index 604aa55ef..ce877f8ef 100644 --- a/crates/cargo-contract/src/cmd/build/mod.rs +++ b/crates/cargo-contract/src/cmd/build/mod.rs @@ -541,7 +541,9 @@ fn post_process_wasm( maybe_println!( verbosity, " {}", - "Skipping wasm validation! Contract code may be invalid.".bright_yellow().bold() + "Skipping wasm validation! Contract code may be invalid." + .bright_yellow() + .bold() ); } From 9e014c16232bb0a4073e1cd626189957381d6442 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 29 Nov 2022 17:51:34 +0000 Subject: [PATCH 5/5] Remove unstable module from test --- crates/cargo-contract/src/validate_wasm.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/cargo-contract/src/validate_wasm.rs b/crates/cargo-contract/src/validate_wasm.rs index f4e3adef4..7730c0fd2 100644 --- a/crates/cargo-contract/src/validate_wasm.rs +++ b/crates/cargo-contract/src/validate_wasm.rs @@ -291,7 +291,6 @@ mod tests { (module (type (;0;) (func (param i32 i32 i32))) (import "seal" "foo" (func (;5;) (type 0))) - (import "__unstable__" "bar" (func (;5;) (type 0))) (import "env" "memory" (func (;5;) (type 0))) (func (;5;) (type 0)) )"#;