2018-11-06 15:51:01 +01:00
|
|
|
extern crate structopt;
|
2018-10-11 21:29:36 +02:00
|
|
|
|
2019-02-22 11:42:30 -08:00
|
|
|
use std::env;
|
2019-03-26 16:41:40 -07:00
|
|
|
use std::fs::{read_to_string, File};
|
2018-10-11 21:29:36 +02:00
|
|
|
use std::io;
|
|
|
|
use std::io::Read;
|
2018-10-14 23:48:59 +02:00
|
|
|
use std::path::PathBuf;
|
2018-10-11 21:29:36 +02:00
|
|
|
use std::process::exit;
|
|
|
|
|
2019-03-26 16:41:40 -07:00
|
|
|
use hashbrown::HashMap;
|
2018-10-11 21:29:36 +02:00
|
|
|
use structopt::StructOpt;
|
|
|
|
|
2019-01-19 00:28:41 -06:00
|
|
|
use wasmer::webassembly::InstanceABI;
|
2018-11-28 13:15:33 +08:00
|
|
|
use wasmer::*;
|
2019-03-19 10:58:58 -07:00
|
|
|
use wasmer_runtime::cache::{Cache as BaseCache, FileSystemCache, WasmHash, WASMER_VERSION_HASH};
|
2019-04-03 17:21:57 -07:00
|
|
|
use wasmer_runtime_core::{self, backend::CompilerConfig};
|
2019-03-28 14:22:28 -07:00
|
|
|
#[cfg(feature = "wasi")]
|
2019-03-28 12:19:23 -07:00
|
|
|
use wasmer_wasi;
|
2018-10-11 21:29:36 +02:00
|
|
|
|
2019-04-05 10:04:39 -07:00
|
|
|
// stub module to make conditional compilation happy
|
|
|
|
#[cfg(not(feature = "wasi"))]
|
|
|
|
mod wasmer_wasi {
|
|
|
|
use wasmer_runtime_core::{import::ImportObject, module::Module};
|
|
|
|
|
|
|
|
pub fn is_wasi_module(_module: &Module) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn generate_import_object(_args: Vec<Vec<u8>>, _envs: Vec<Vec<u8>>) -> ImportObject {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-11 21:29:36 +02:00
|
|
|
#[derive(Debug, StructOpt)]
|
2019-01-19 00:28:41 -06:00
|
|
|
#[structopt(name = "wasmer", about = "Wasm execution runtime.")]
|
2018-10-14 23:47:35 +02:00
|
|
|
/// The options for the wasmer Command Line Interface
|
|
|
|
enum CLIOptions {
|
|
|
|
/// Run a WebAssembly file. Formats accepted: wasm, wast
|
|
|
|
#[structopt(name = "run")]
|
2018-10-14 23:48:59 +02:00
|
|
|
Run(Run),
|
2018-11-25 21:31:32 -08:00
|
|
|
|
2019-02-22 11:42:30 -08:00
|
|
|
/// Wasmer cache
|
|
|
|
#[structopt(name = "cache")]
|
|
|
|
Cache(Cache),
|
|
|
|
|
2019-04-03 16:52:37 -07:00
|
|
|
/// Validate a Web Assembly binary
|
|
|
|
#[structopt(name = "validate")]
|
|
|
|
Validate(Validate),
|
|
|
|
|
2018-11-25 21:31:32 -08:00
|
|
|
/// Update wasmer to the latest version
|
|
|
|
#[structopt(name = "self-update")]
|
|
|
|
SelfUpdate,
|
2018-10-14 23:47:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, StructOpt)]
|
|
|
|
struct Run {
|
2019-02-22 12:01:03 -08:00
|
|
|
// Disable the cache
|
|
|
|
#[structopt(long = "disable-cache")]
|
|
|
|
disable_cache: bool,
|
|
|
|
|
2018-10-11 21:29:36 +02:00
|
|
|
/// Input file
|
|
|
|
#[structopt(parse(from_os_str))]
|
|
|
|
path: PathBuf,
|
2018-12-06 12:32:53 +01:00
|
|
|
|
|
|
|
/// Application arguments
|
2018-12-15 00:46:11 -06:00
|
|
|
#[structopt(name = "--", raw(multiple = "true"))]
|
2018-12-06 12:32:53 +01:00
|
|
|
args: Vec<String>,
|
2019-03-26 16:41:40 -07:00
|
|
|
|
2019-04-10 18:23:25 -07:00
|
|
|
/// Emscripten symbol ma>p
|
|
|
|
#[structopt(long = "em-symbol-map", parse(from_os_str), group = "emscripten")]
|
2019-03-27 14:01:27 -07:00
|
|
|
em_symbol_map: Option<PathBuf>,
|
2019-04-10 18:23:25 -07:00
|
|
|
|
|
|
|
/// WASI pre-opened file
|
|
|
|
#[structopt(long = "pre-open", group = "wasi")]
|
|
|
|
pre_opened_files: Vec<String>,
|
2018-10-11 21:29:36 +02:00
|
|
|
}
|
|
|
|
|
2019-02-22 11:42:30 -08:00
|
|
|
#[derive(Debug, StructOpt)]
|
|
|
|
enum Cache {
|
2019-03-19 12:13:23 -07:00
|
|
|
/// Clear the cache
|
2019-02-22 11:42:30 -08:00
|
|
|
#[structopt(name = "clean")]
|
|
|
|
Clean,
|
|
|
|
|
2019-03-19 12:13:23 -07:00
|
|
|
/// Display the location of the cache
|
2019-02-22 11:42:30 -08:00
|
|
|
#[structopt(name = "dir")]
|
|
|
|
Dir,
|
|
|
|
}
|
|
|
|
|
2019-04-03 16:52:37 -07:00
|
|
|
#[derive(Debug, StructOpt)]
|
|
|
|
struct Validate {
|
|
|
|
/// Input file
|
|
|
|
#[structopt(parse(from_os_str))]
|
|
|
|
path: PathBuf,
|
|
|
|
}
|
|
|
|
|
2018-10-14 23:48:59 +02:00
|
|
|
/// Read the contents of a file
|
2018-11-15 00:50:54 -08:00
|
|
|
fn read_file_contents(path: &PathBuf) -> Result<Vec<u8>, io::Error> {
|
2018-10-11 21:29:36 +02:00
|
|
|
let mut buffer: Vec<u8> = Vec::new();
|
|
|
|
let mut file = File::open(path)?;
|
|
|
|
file.read_to_end(&mut buffer)?;
|
2018-12-28 13:59:55 +01:00
|
|
|
// We force to close the file
|
|
|
|
drop(file);
|
2018-10-11 21:29:36 +02:00
|
|
|
Ok(buffer)
|
|
|
|
}
|
|
|
|
|
2019-02-22 11:42:30 -08:00
|
|
|
fn get_cache_dir() -> PathBuf {
|
|
|
|
match env::var("WASMER_CACHE_DIR") {
|
|
|
|
Ok(dir) => PathBuf::from(dir),
|
|
|
|
Err(_) => {
|
|
|
|
// We use a temporal directory for saving cache files
|
|
|
|
let mut temp_dir = env::temp_dir();
|
|
|
|
temp_dir.push("wasmer");
|
2019-03-19 11:31:45 -07:00
|
|
|
temp_dir.push(WASMER_VERSION_HASH);
|
2019-02-22 11:42:30 -08:00
|
|
|
temp_dir
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-19 00:28:41 -06:00
|
|
|
/// Execute a wasm/wat file
|
2018-12-06 12:32:53 +01:00
|
|
|
fn execute_wasm(options: &Run) -> Result<(), String> {
|
2019-02-25 11:46:48 -08:00
|
|
|
// force disable caching on windows
|
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
let disable_cache = true;
|
|
|
|
#[cfg(not(target_os = "windows"))]
|
2019-02-25 11:53:15 -08:00
|
|
|
let disable_cache = options.disable_cache;
|
2019-02-25 11:46:48 -08:00
|
|
|
|
2018-12-06 12:32:53 +01:00
|
|
|
let wasm_path = &options.path;
|
|
|
|
|
|
|
|
let mut wasm_binary: Vec<u8> = read_file_contents(wasm_path).map_err(|err| {
|
2018-11-15 13:31:37 -08:00
|
|
|
format!(
|
|
|
|
"Can't read the file {}: {}",
|
|
|
|
wasm_path.as_os_str().to_string_lossy(),
|
|
|
|
err
|
|
|
|
)
|
|
|
|
})?;
|
2018-12-06 12:32:53 +01:00
|
|
|
|
2019-03-27 14:01:27 -07:00
|
|
|
let em_symbol_map = if let Some(em_symbol_map_path) = options.em_symbol_map.clone() {
|
|
|
|
let em_symbol_map_content: String = read_to_string(&em_symbol_map_path)
|
2019-03-26 16:41:40 -07:00
|
|
|
.map_err(|err| {
|
|
|
|
format!(
|
|
|
|
"Can't read symbol map file {}: {}",
|
2019-03-27 14:01:27 -07:00
|
|
|
em_symbol_map_path.as_os_str().to_string_lossy(),
|
2019-03-26 16:41:40 -07:00
|
|
|
err,
|
|
|
|
)
|
|
|
|
})?
|
|
|
|
.to_owned();
|
2019-03-27 14:01:27 -07:00
|
|
|
let mut em_symbol_map = HashMap::new();
|
|
|
|
for line in em_symbol_map_content.lines() {
|
2019-03-26 16:41:40 -07:00
|
|
|
let mut split = line.split(':');
|
|
|
|
let num_str = if let Some(ns) = split.next() {
|
|
|
|
ns
|
|
|
|
} else {
|
|
|
|
return Err(format!(
|
|
|
|
"Can't parse symbol map (expected each entry to be of the form: `0:func_name`)"
|
|
|
|
));
|
|
|
|
};
|
|
|
|
let num: u32 = num_str.parse::<u32>().map_err(|err| {
|
|
|
|
format!(
|
|
|
|
"Failed to parse {} as a number in symbol map: {}",
|
|
|
|
num_str, err
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
let name_str: String = if let Some(name_str) = split.next() {
|
|
|
|
name_str
|
|
|
|
} else {
|
|
|
|
return Err(format!(
|
|
|
|
"Can't parse symbol map (expected each entry to be of the form: `0:func_name`)"
|
|
|
|
));
|
|
|
|
}
|
|
|
|
.to_owned();
|
|
|
|
|
2019-03-27 14:01:27 -07:00
|
|
|
em_symbol_map.insert(num, name_str);
|
2019-03-26 16:41:40 -07:00
|
|
|
}
|
2019-03-27 14:01:27 -07:00
|
|
|
Some(em_symbol_map)
|
2019-03-26 16:41:40 -07:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2019-01-24 15:09:56 -08:00
|
|
|
if !utils::is_wasm_binary(&wasm_binary) {
|
2018-11-15 13:31:37 -08:00
|
|
|
wasm_binary = wabt::wat2wasm(wasm_binary)
|
2019-01-18 10:54:16 -08:00
|
|
|
.map_err(|e| format!("Can't convert from wast to wasm: {:?}", e))?;
|
2018-10-11 21:29:36 +02:00
|
|
|
}
|
2018-12-06 12:32:53 +01:00
|
|
|
|
2019-02-25 11:46:48 -08:00
|
|
|
let module = if !disable_cache {
|
2019-02-22 12:01:03 -08:00
|
|
|
// If we have cache enabled
|
|
|
|
|
|
|
|
// We generate a hash for the given binary, so we can use it as key
|
|
|
|
// for the Filesystem cache
|
|
|
|
let hash = WasmHash::generate(&wasm_binary);
|
|
|
|
|
|
|
|
let wasmer_cache_dir = get_cache_dir();
|
|
|
|
|
|
|
|
// We create a new cache instance.
|
|
|
|
// It could be possible to use any other kinds of caching, as long as they
|
|
|
|
// implement the Cache trait (with save and load functions)
|
|
|
|
let mut cache = unsafe {
|
|
|
|
FileSystemCache::new(wasmer_cache_dir).map_err(|e| format!("Cache error: {:?}", e))?
|
|
|
|
};
|
|
|
|
|
|
|
|
// cache.load will return the Module if it's able to deserialize it properly, and an error if:
|
|
|
|
// * The file is not found
|
|
|
|
// * The file exists, but it's corrupted or can't be converted to a module
|
|
|
|
let module = match cache.load(hash) {
|
|
|
|
Ok(module) => {
|
|
|
|
// We are able to load the module from cache
|
|
|
|
module
|
|
|
|
}
|
|
|
|
Err(_) => {
|
2019-03-27 14:01:27 -07:00
|
|
|
let module = webassembly::compile_with_config(
|
|
|
|
&wasm_binary[..],
|
|
|
|
CompilerConfig {
|
|
|
|
symbol_map: em_symbol_map,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.map_err(|e| format!("Can't compile module: {:?}", e))?;
|
2019-03-16 11:52:11 -07:00
|
|
|
// We try to save the module into a cache file
|
|
|
|
cache.store(hash, module.clone()).unwrap_or_default();
|
|
|
|
|
2019-02-22 12:01:03 -08:00
|
|
|
module
|
|
|
|
}
|
|
|
|
};
|
|
|
|
module
|
|
|
|
} else {
|
2019-03-27 14:01:27 -07:00
|
|
|
webassembly::compile_with_config(
|
|
|
|
&wasm_binary[..],
|
|
|
|
CompilerConfig {
|
|
|
|
symbol_map: em_symbol_map,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.map_err(|e| format!("Can't compile module: {:?}", e))?
|
2019-02-22 11:42:30 -08:00
|
|
|
};
|
2018-12-10 20:15:41 -08:00
|
|
|
|
2019-03-28 12:19:23 -07:00
|
|
|
// TODO: refactor this
|
2019-03-29 10:58:56 -07:00
|
|
|
let (abi, import_object, _em_globals) = if wasmer_emscripten::is_emscripten_module(&module) {
|
2019-01-28 14:31:03 -08:00
|
|
|
let mut emscripten_globals = wasmer_emscripten::EmscriptenGlobals::new(&module);
|
2019-01-19 00:28:41 -06:00
|
|
|
(
|
|
|
|
InstanceABI::Emscripten,
|
2019-01-23 22:29:51 +01:00
|
|
|
wasmer_emscripten::generate_emscripten_env(&mut emscripten_globals),
|
2019-01-26 14:17:17 -06:00
|
|
|
Some(emscripten_globals), // TODO Em Globals is here to extend, lifetime, find better solution
|
2019-01-19 00:28:41 -06:00
|
|
|
)
|
2018-12-15 00:46:11 -06:00
|
|
|
} else {
|
2019-04-05 10:04:39 -07:00
|
|
|
if cfg!(feature = "wasi") && wasmer_wasi::is_wasi_module(&module) {
|
|
|
|
(
|
|
|
|
InstanceABI::WASI,
|
|
|
|
wasmer_wasi::generate_import_object(
|
|
|
|
[options.path.to_str().unwrap().to_owned()]
|
|
|
|
.iter()
|
|
|
|
.chain(options.args.iter())
|
|
|
|
.cloned()
|
|
|
|
.map(|arg| arg.into_bytes())
|
|
|
|
.collect(),
|
|
|
|
env::vars()
|
|
|
|
.map(|(k, v)| format!("{}={}", k, v).into_bytes())
|
|
|
|
.collect(),
|
2019-04-10 18:23:25 -07:00
|
|
|
options.pre_opened_files.clone(),
|
2019-04-05 10:04:39 -07:00
|
|
|
),
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
(
|
|
|
|
InstanceABI::None,
|
|
|
|
wasmer_runtime_core::import::ImportObject::new(),
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
}
|
2019-03-28 12:19:23 -07:00
|
|
|
};
|
|
|
|
|
2019-01-18 00:18:13 -06:00
|
|
|
let mut instance = module
|
2019-03-27 14:01:27 -07:00
|
|
|
.instantiate(&import_object)
|
2019-01-19 00:28:41 -06:00
|
|
|
.map_err(|e| format!("Can't instantiate module: {:?}", e))?;
|
2018-11-07 11:47:06 +01:00
|
|
|
|
2019-01-19 00:28:41 -06:00
|
|
|
webassembly::run_instance(
|
2019-01-18 10:54:16 -08:00
|
|
|
&module,
|
2018-12-15 00:46:11 -06:00
|
|
|
&mut instance,
|
2019-03-29 10:58:56 -07:00
|
|
|
abi,
|
2018-12-15 00:46:11 -06:00
|
|
|
options.path.to_str().unwrap(),
|
|
|
|
options.args.iter().map(|arg| arg.as_str()).collect(),
|
2019-01-18 11:15:13 -08:00
|
|
|
)
|
2019-01-19 00:28:41 -06:00
|
|
|
.map_err(|e| format!("{:?}", e))?;
|
2019-01-26 13:42:38 -06:00
|
|
|
|
2019-01-19 00:28:41 -06:00
|
|
|
Ok(())
|
2018-10-11 21:29:36 +02:00
|
|
|
}
|
|
|
|
|
2018-10-14 23:47:35 +02:00
|
|
|
fn run(options: Run) {
|
2018-12-06 12:32:53 +01:00
|
|
|
match execute_wasm(&options) {
|
2018-10-11 21:29:36 +02:00
|
|
|
Ok(()) => {}
|
|
|
|
Err(message) => {
|
2019-01-20 14:16:13 -08:00
|
|
|
eprintln!("{:?}", message);
|
2018-10-11 21:29:36 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-14 23:47:35 +02:00
|
|
|
|
2019-04-03 16:52:37 -07:00
|
|
|
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,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2019-04-03 17:21:57 -07:00
|
|
|
wasmer_runtime_core::validate_and_report_errors(&wasm_binary)
|
|
|
|
.map_err(|err| format!("Validation failed: {}", err))?;
|
2019-04-03 16:52:37 -07:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Runs logic for the `validate` subcommand
|
|
|
|
fn validate(validate: Validate) {
|
|
|
|
match validate_wasm(validate) {
|
|
|
|
Err(message) => {
|
|
|
|
eprintln!("Error: {}", message);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-14 23:47:35 +02:00
|
|
|
fn main() {
|
|
|
|
let options = CLIOptions::from_args();
|
|
|
|
match options {
|
|
|
|
CLIOptions::Run(options) => run(options),
|
2019-02-14 15:30:42 -08:00
|
|
|
#[cfg(not(target_os = "windows"))]
|
2018-11-25 21:31:32 -08:00
|
|
|
CLIOptions::SelfUpdate => update::self_update(),
|
2019-02-14 15:30:42 -08:00
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
CLIOptions::SelfUpdate => {
|
|
|
|
println!("Self update is not supported on Windows. Use install instructions on the Wasmer homepage: https://wasmer.io");
|
|
|
|
}
|
2019-02-25 11:46:48 -08:00
|
|
|
#[cfg(not(target_os = "windows"))]
|
2019-02-22 11:42:30 -08:00
|
|
|
CLIOptions::Cache(cache) => match cache {
|
|
|
|
Cache::Clean => {
|
2019-02-25 11:46:48 -08:00
|
|
|
use std::fs;
|
2019-02-22 11:42:30 -08:00
|
|
|
let cache_dir = get_cache_dir();
|
2019-03-19 11:08:14 -07:00
|
|
|
if cache_dir.exists() {
|
|
|
|
fs::remove_dir_all(cache_dir.clone()).expect("Can't remove cache dir");
|
|
|
|
}
|
|
|
|
fs::create_dir_all(cache_dir.clone()).expect("Can't create cache dir");
|
2019-02-22 11:42:30 -08:00
|
|
|
}
|
|
|
|
Cache::Dir => {
|
|
|
|
println!("{}", get_cache_dir().to_string_lossy());
|
|
|
|
}
|
|
|
|
},
|
2019-04-03 16:52:37 -07:00
|
|
|
CLIOptions::Validate(validate_options) => {
|
|
|
|
validate(validate_options);
|
|
|
|
}
|
2019-02-25 11:46:48 -08:00
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
CLIOptions::Cache(_) => {
|
|
|
|
println!("Caching is disabled for Windows.");
|
|
|
|
}
|
2018-10-14 23:47:35 +02:00
|
|
|
}
|
|
|
|
}
|