wasmer/src/bin/wasmer.rs

458 lines
14 KiB
Rust
Raw Normal View History

2018-11-06 15:51:01 +01:00
extern crate structopt;
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};
use std::io;
use std::io::Read;
2018-10-14 23:48:59 +02:00
use std::path::PathBuf;
use std::process::exit;
2019-04-11 12:44:03 -07:00
use std::str::FromStr;
2019-03-26 16:41:40 -07:00
use hashbrown::HashMap;
use structopt::StructOpt;
2018-11-28 13:15:33 +08:00
use wasmer::*;
2019-04-11 14:34:54 -07:00
use wasmer_clif_backend::CraneliftCompiler;
#[cfg(feature = "backend:llvm")]
2019-04-11 14:44:43 -07:00
use wasmer_llvm_backend::LLVMCompiler;
2019-04-22 16:45:36 -07:00
use wasmer_runtime::{
cache::{Cache as BaseCache, FileSystemCache, WasmHash, WASMER_VERSION_HASH},
Func, Value, error::RuntimeError,
};
2019-04-11 14:34:54 -07:00
use wasmer_runtime_core::{
self,
backend::{Compiler, CompilerConfig},
};
#[cfg(feature = "backend:singlepass")]
2019-04-11 14:34:54 -07:00
use wasmer_singlepass_backend::SinglePassCompiler;
2019-03-28 14:22:28 -07:00
#[cfg(feature = "wasi")]
2019-03-28 12:19:23 -07:00
use wasmer_wasi;
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!()
}
}
#[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 {
// Disable the cache
#[structopt(long = "disable-cache")]
disable_cache: bool,
/// Input file
#[structopt(parse(from_os_str))]
path: PathBuf,
2018-12-06 12:32:53 +01:00
2019-04-11 12:44:03 -07:00
// Disable the cache
2019-04-11 14:34:54 -07:00
#[structopt(
long = "backend",
default_value = "cranelift",
raw(possible_values = "Backend::variants()", case_insensitive = "true")
)]
2019-04-11 12:44:03 -07:00
backend: Backend,
2019-03-26 16:41:40 -07:00
2019-04-19 09:26:47 -07:00
/// Emscripten symbol map
#[structopt(long = "em-symbol-map", parse(from_os_str), group = "emscripten")]
2019-03-27 14:01:27 -07:00
em_symbol_map: Option<PathBuf>,
/// WASI pre-opened directory
#[structopt(long = "dir", multiple = true, group = "wasi")]
pre_opened_directories: Vec<String>,
#[structopt(long = "command-name", hidden = true)]
command_name: Option<String>,
2019-04-11 12:44:03 -07:00
/// Application arguments
#[structopt(name = "--", raw(multiple = "true"))]
args: Vec<String>,
}
2019-04-11 14:34:54 -07:00
#[allow(dead_code)]
2019-04-11 12:44:03 -07:00
#[derive(Debug)]
enum Backend {
Cranelift,
Singlepass,
LLVM,
}
2019-04-11 14:34:54 -07:00
impl Backend {
pub fn variants() -> &'static [&'static str] {
&["singlepass", "cranelift", "llvm"]
}
}
2019-04-11 12:44:03 -07:00
impl FromStr for Backend {
type Err = String;
fn from_str(s: &str) -> Result<Backend, String> {
2019-04-11 14:34:54 -07:00
match s.to_lowercase().as_str() {
2019-04-11 12:44:03 -07:00
"singlepass" => Ok(Backend::Singlepass),
"cranelift" => Ok(Backend::Cranelift),
2019-04-11 14:44:43 -07:00
"llvm" => Ok(Backend::LLVM),
// "llvm" => Err(
// "The LLVM backend option is not enabled by default due to binary size constraints"
// .to_string(),
// ),
2019-04-11 13:17:19 -07:00
_ => Err(format!("The backend {} doesn't exist", s)),
2019-04-11 12:44:03 -07: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> {
let mut buffer: Vec<u8> = Vec::new();
let mut file = File::open(path)?;
file.read_to_end(&mut buffer)?;
// We force to close the file
drop(file);
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)
.map_err(|e| format!("Can't convert from wast to wasm: {:?}", e))?;
}
2018-12-06 12:32:53 +01:00
2019-04-11 14:34:54 -07:00
let compiler: Box<dyn Compiler> = match options.backend {
#[cfg(feature = "backend:singlepass")]
2019-04-11 14:34:54 -07:00
Backend::Singlepass => Box::new(SinglePassCompiler::new()),
#[cfg(not(feature = "backend:singlepass"))]
Backend::Singlepass => return Err("The singlepass backend is not enabled".to_string()),
2019-04-11 14:34:54 -07:00
Backend::Cranelift => Box::new(CraneliftCompiler::new()),
#[cfg(feature = "backend:llvm")]
2019-04-11 14:44:43 -07:00
Backend::LLVM => Box::new(LLVMCompiler::new()),
#[cfg(not(feature = "backend:llvm"))]
Backend::LLVM => return Err("the llvm backend is not enabled".to_string()),
2019-04-11 14:34:54 -07:00
};
2019-02-25 11:46:48 -08:00
let module = if !disable_cache {
// 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-04-11 14:34:54 -07:00
let module = webassembly::compile_with_config_with(
2019-03-27 14:01:27 -07:00
&wasm_binary[..],
CompilerConfig {
symbol_map: em_symbol_map,
},
2019-04-11 14:34:54 -07:00
&*compiler,
2019-03-27 14:01:27 -07:00
)
.map_err(|e| format!("Can't compile module: {:?}", e))?;
// We try to save the module into a cache file
cache.store(hash, module.clone()).unwrap_or_default();
module
}
};
module
} else {
2019-04-11 14:34:54 -07:00
webassembly::compile_with_config_with(
2019-03-27 14:01:27 -07:00
&wasm_binary[..],
CompilerConfig {
symbol_map: em_symbol_map,
},
2019-04-11 14:34:54 -07:00
&*compiler,
2019-03-27 14:01:27 -07:00
)
.map_err(|e| format!("Can't compile module: {:?}", e))?
2019-02-22 11:42:30 -08:00
};
2019-03-28 12:19:23 -07:00
// TODO: refactor this
2019-04-22 16:45:36 -07:00
if wasmer_emscripten::is_emscripten_module(&module) {
let mut emscripten_globals = wasmer_emscripten::EmscriptenGlobals::new(&module);
2019-04-22 16:45:36 -07:00
let import_object = wasmer_emscripten::generate_emscripten_env(&mut emscripten_globals);
let mut instance = module
.instantiate(&import_object)
.map_err(|e| format!("Can't instantiate module: {:?}", e))?;
wasmer_emscripten::run_emscripten_instance(
&module,
&mut instance,
if let Some(cn) = &options.command_name {
cn
} else {
options.path.to_str().unwrap()
},
options.args.iter().map(|arg| arg.as_str()).collect(),
2019-01-19 00:28:41 -06:00
)
2019-04-22 16:45:36 -07:00
.map_err(|e| format!("{:?}", e))?;
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) {
2019-04-22 16:45:36 -07:00
let import_object = wasmer_wasi::generate_import_object(
if let Some(cn) = &options.command_name {
[cn.clone()]
} else {
[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-22 16:45:36 -07:00
options.pre_opened_directories.clone(),
);
2019-03-28 12:19:23 -07:00
2019-04-22 16:45:36 -07:00
let instance = module
.instantiate(&import_object)
.map_err(|e| format!("Can't instantiate module: {:?}", e))?;
2018-11-07 11:47:06 +01:00
2019-04-22 16:45:36 -07:00
let start: Func<(), ()> = instance.func("_start").map_err(|e| format!("{:?}", e))?;
let result = start.call();
if let Err(ref err) = result {
match err {
RuntimeError::Trap { msg } => panic!("wasm trap occured: {}", msg),
RuntimeError::Error { data } => {
if let Some(error_code) = data.downcast_ref::<wasmer_wasi::ExitCode>() {
std::process::exit(error_code.code as i32)
}
}
}
panic!("error: {:?}", err)
}
} else {
2019-04-22 16:45:36 -07:00
let import_object = wasmer_runtime_core::import::ImportObject::new();
let instance = module
.instantiate(&import_object)
.map_err(|e| format!("Can't instantiate module: {:?}", e))?;
let args: Vec<Value> = options
.args
.iter()
.map(|arg| arg.as_str())
.map(|x| Value::I32(x.parse().unwrap()))
.collect();
instance
.dyn_func("main")
.map_err(|e| format!("{:?}", e))?
.call(&args)
.map_err(|e| format!("{:?}", e))?;
}
}
2019-01-19 00:28:41 -06:00
Ok(())
}
2018-10-14 23:47:35 +02:00
fn run(options: Run) {
2018-12-06 12:32:53 +01:00
match execute_wasm(&options) {
Ok(()) => {}
Err(message) => {
eprintln!("{:?}", message);
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,
));
}
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();
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
}
}