wasmer_compiler_cli/commands/
validate.rs

1use crate::store::StoreOptions;
2use anyhow::{bail, Context, Result};
3use clap::Parser;
4use std::path::PathBuf;
5use std::str::FromStr;
6use wasmer_types::is_wasm;
7use wasmer_types::target::{CpuFeature, Target, Triple};
8
9#[derive(Debug, Parser)]
10/// The options for the `wasmer validate` subcommand
11pub struct Validate {
12    /// File to validate as WebAssembly
13    #[clap(name = "FILE")]
14    path: PathBuf,
15
16    #[clap(flatten)]
17    store: StoreOptions,
18}
19
20impl Validate {
21    /// Runs logic for the `validate` subcommand
22    pub fn execute(&self) -> Result<()> {
23        self.inner_execute()
24            .context(format!("failed to validate `{}`", self.path.display()))
25    }
26    fn inner_execute(&self) -> Result<()> {
27        let target = Target::new(
28            Triple::from_str("x86_64-linux-gnu").unwrap(),
29            CpuFeature::SSE2 | CpuFeature::AVX,
30        );
31        let (engine_builder, _compiler_type) = self.store.get_engine_for_target(target)?;
32        let engine = engine_builder.engine();
33        let module_contents = std::fs::read(&self.path)?;
34        if !is_wasm(&module_contents) {
35            bail!("`wasmer validate` only validates WebAssembly files");
36        }
37        engine.validate(&module_contents)?;
38        eprintln!("Validation passed for `{}`.", self.path.display());
39        Ok(())
40    }
41}