Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unstable module check, add --skip-wasm-validation #846

Merged
merged 5 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions crates/cargo-contract/src/cmd/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -255,6 +260,7 @@ impl CheckCommand {
keep_debug_symbols: false,
lint: false,
output_type: OutputType::default(),
skip_wasm_validation: false,
};

execute(args)
Expand Down Expand Up @@ -516,7 +522,11 @@ fn load_module<P: AsRef<Path>>(path: P) -> Result<Module> {
}

/// 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,
verbosity: &Verbosity,
) -> Result<()> {
// Deserialize Wasm module from a file.
let mut module = load_module(&crate_metadata.original_wasm)
.context("Loading of original wasm failed")?;
Expand All @@ -525,7 +535,17 @@ 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)?;
} else {
maybe_println!(
verbosity,
" {}",
"Skipping wasm validation! Contract code may be invalid."
.bright_yellow()
.bold()
);
}

debug_assert!(
!module.clone().into_bytes().unwrap().is_empty(),
Expand Down Expand Up @@ -596,6 +616,7 @@ pub(crate) fn execute(args: ExecuteArgs) -> Result<BuildResult> {
keep_debug_symbols,
lint,
output_type,
skip_wasm_validation,
} = args;

let crate_metadata = CrateMetadata::collect(&manifest_path)?;
Expand Down Expand Up @@ -651,7 +672,7 @@ pub(crate) fn execute(args: ExecuteArgs) -> Result<BuildResult> {
"Post processing wasm file".bright_green().bold()
);
build_steps.increment_current();
post_process_wasm(&crate_metadata)?;
post_process_wasm(&crate_metadata, skip_wasm_validation, &verbosity)?;

maybe_println!(
verbosity,
Expand Down
3 changes: 3 additions & 0 deletions crates/cargo-contract/src/cmd/build/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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");

Expand Down
12 changes: 4 additions & 8 deletions crates/cargo-contract/src/validate_wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<String>>()
Expand All @@ -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
))
}
Expand Down Expand Up @@ -294,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))
)"#;
Expand Down