Sniff the wasm magic bytes, rather than relying on the filename extension.

This commit is contained in:
Dan Gohman
2017-10-03 09:19:55 -07:00
parent ba14499fe9
commit d857aacec3

View File

@@ -11,7 +11,6 @@ use cretonne::settings::FlagsOrIsa;
use std::fs::File; use std::fs::File;
use std::error::Error; use std::error::Error;
use std::io; use std::io;
use std::io::prelude::*;
use std::path::Path; use std::path::Path;
use std::process::Command; use std::process::Command;
use tempdir::TempDir; use tempdir::TempDir;
@@ -78,18 +77,10 @@ fn handle_module(
terminal.fg(term::color::MAGENTA).unwrap(); terminal.fg(term::color::MAGENTA).unwrap();
vprint!(flag_verbose, "Translating... "); vprint!(flag_verbose, "Translating... ");
terminal.reset().unwrap(); terminal.reset().unwrap();
let data = match path.extension() { let mut data = read_to_end(path.clone()).map_err(|err| {
None => {
return Err(String::from("the file extension is not wasm or wat"));
}
Some(ext) => {
match ext.to_str() {
Some("wasm") => {
read_to_end(path.clone()).map_err(|err| {
String::from(err.description()) String::from(err.description())
})? })?;
} if !data.starts_with(&[b'\0', b'a', b's', b'm']) {
Some("wat") => {
let tmp_dir = TempDir::new("cretonne-wasm").unwrap(); let tmp_dir = TempDir::new("cretonne-wasm").unwrap();
let file_path = tmp_dir.path().join("module.wasm"); let file_path = tmp_dir.path().join("module.wasm");
File::create(file_path.clone()).unwrap(); File::create(file_path.clone()).unwrap();
@@ -103,16 +94,10 @@ fn handle_module(
} else { } else {
return Err(String::from(e.description())); return Err(String::from(e.description()));
})?; })?;
read_to_end(file_path).map_err( data = read_to_end(file_path).map_err(
|err| String::from(err.description()), |err| String::from(err.description()),
)? )?;
} }
None | Some(&_) => {
return Err(String::from("the file extension is not wasm or wat"));
}
}
}
};
let mut dummy_runtime = DummyRuntime::with_flags(fisa.flags.clone()); let mut dummy_runtime = DummyRuntime::with_flags(fisa.flags.clone());
let translation = { let translation = {
let runtime: &mut WasmRuntime = &mut dummy_runtime; let runtime: &mut WasmRuntime = &mut dummy_runtime;