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

add validate subcommand #323

Merged
merged 3 commits into from
Apr 4, 2019
Merged
Changes from 1 commit
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
57 changes: 57 additions & 0 deletions src/bin/wasmer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ enum CLIOptions {
#[structopt(name = "cache")]
Cache(Cache),

/// Validate a Web Assembly binary
#[structopt(name = "validate")]
Validate(Validate),

/// Update wasmer to the latest version
#[structopt(name = "self-update")]
SelfUpdate,
Expand Down Expand Up @@ -63,6 +67,13 @@ enum Cache {
Dir,
}

#[derive(Debug, StructOpt)]
struct Validate {
/// Input file
#[structopt(parse(from_os_str))]
path: PathBuf,
}

/// Read the contents of a file
fn read_file_contents(path: &PathBuf) -> Result<Vec<u8>, io::Error> {
let mut buffer: Vec<u8> = Vec::new();
Expand Down Expand Up @@ -240,6 +251,49 @@ fn run(options: Run) {
}
}

fn validate_wasm(validate: Validate) -> Result<(), String> {
let wasm_path = validate.path;
let wasm_path_as_str = wasm_path.to_str().unwrap();

let wasm_binary: Vec<u8> = read_file_contents(&wasm_path).map_err(|err| {
format!(
"Can't read the file {}: {}",
wasm_path.as_os_str().to_string_lossy(),
err
)
})?;

if !utils::is_wasm_binary(&wasm_binary) {
return Err(format!(
"Cannot recognize \"{}\" as a WASM binary",
wasm_path_as_str,
));
}

wabt::Module::read_binary(wasm_binary, &Default::default())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Runtime has a validate method:

pub use wasmer_runtime_core::{compile_with, validate};

It may be lighterweight solution than wabt.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I think we'd want better error messages than yes or no, I'll look in to the functions that that validate fn calls

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing this out!

.map_err(|err| {
format!(
"Failed to read \"{}\" as a WASM binary: {}",
wasm_path_as_str, err
)
})?
.validate()
.map_err(|err| format!("Failed to validate \"{}\": {}", wasm_path_as_str, err))?;

Ok(())
}

/// Runs logic for the `validate` subcommand
fn validate(validate: Validate) {
match validate_wasm(validate) {
Err(message) => {
eprintln!("Error: {}", message);
exit(-1);
}
_ => (),
}
}

fn main() {
let options = CLIOptions::from_args();
match options {
Expand All @@ -264,6 +318,9 @@ fn main() {
println!("{}", get_cache_dir().to_string_lossy());
}
},
CLIOptions::Validate(validate_options) => {
validate(validate_options);
}
#[cfg(target_os = "windows")]
CLIOptions::Cache(_) => {
println!("Caching is disabled for Windows.");
Expand Down