2019-08-01 20:46:35 -06:00
|
|
|
#![deny(
|
|
|
|
dead_code,
|
2019-08-15 20:18:29 -06:00
|
|
|
nonstandard_style,
|
2019-08-01 20:46:35 -06:00
|
|
|
unused_imports,
|
2019-08-15 20:18:29 -06:00
|
|
|
unused_mut,
|
2019-08-01 20:46:35 -06:00
|
|
|
unused_variables,
|
|
|
|
unused_unsafe,
|
|
|
|
unreachable_patterns
|
|
|
|
)]
|
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-04-11 12:44:03 -07:00
|
|
|
use std::str::FromStr;
|
2018-10-11 21:29:36 +02:00
|
|
|
|
2019-07-31 23:17:42 -07:00
|
|
|
use std::collections::HashMap;
|
2018-10-11 21:29:36 +02:00
|
|
|
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;
|
2019-07-09 17:57:31 -07:00
|
|
|
#[cfg(feature = "backend-llvm")]
|
2019-08-08 19:42:41 -07:00
|
|
|
use wasmer_llvm_backend::{LLVMCompiler, LLVMOptions};
|
2019-04-22 16:45:36 -07:00
|
|
|
use wasmer_runtime::{
|
2019-07-29 18:35:59 -07:00
|
|
|
cache::{Cache as BaseCache, FileSystemCache, WasmHash},
|
|
|
|
Func, Value, VERSION,
|
2019-04-22 16:45:36 -07:00
|
|
|
};
|
2019-04-11 14:34:54 -07:00
|
|
|
use wasmer_runtime_core::{
|
|
|
|
self,
|
2019-07-25 23:33:30 -07:00
|
|
|
backend::{Backend, Compiler, CompilerConfig, Features, MemoryBoundCheckMode},
|
2019-07-08 15:46:28 -07:00
|
|
|
debug,
|
2019-05-14 16:02:27 +08:00
|
|
|
loader::{Instance as LoadedInstance, LocalLoader},
|
2019-04-11 14:34:54 -07:00
|
|
|
};
|
2019-07-09 17:57:31 -07:00
|
|
|
#[cfg(feature = "backend-singlepass")]
|
2019-06-08 16:48:41 -05: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;
|
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
|
|
|
|
}
|
|
|
|
|
2019-07-30 15:59:21 +09:00
|
|
|
pub fn generate_import_object(
|
|
|
|
_args: Vec<Vec<u8>>,
|
|
|
|
_envs: Vec<Vec<u8>>,
|
|
|
|
_preopened_files: Vec<String>,
|
|
|
|
_mapped_dirs: Vec<(String, std::path::PathBuf)>,
|
|
|
|
) -> ImportObject {
|
2019-04-05 10:04:39 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-07-25 23:33:30 -07:00
|
|
|
#[derive(Debug, StructOpt)]
|
|
|
|
struct PrestandardFeatures {
|
|
|
|
/// Enable support for the SIMD proposal.
|
|
|
|
#[structopt(long = "enable-simd")]
|
|
|
|
simd: bool,
|
|
|
|
|
|
|
|
/// Enable support for all pre-standard proposals.
|
|
|
|
#[structopt(long = "enable-all")]
|
|
|
|
all: bool,
|
|
|
|
}
|
|
|
|
|
2019-08-08 19:42:41 -07:00
|
|
|
#[cfg(feature = "backend-llvm")]
|
|
|
|
#[derive(Debug, StructOpt, Clone)]
|
|
|
|
/// LLVM backend flags.
|
|
|
|
pub struct LLVMCLIOptions {
|
|
|
|
/// Emit LLVM IR before optimization pipeline.
|
|
|
|
#[structopt(long = "llvm-pre-opt-ir", parse(from_os_str))]
|
|
|
|
pre_opt_ir: Option<PathBuf>,
|
|
|
|
|
|
|
|
/// Emit LLVM IR after optimization pipeline.
|
|
|
|
#[structopt(long = "llvm-post-opt-ir", parse(from_os_str))]
|
|
|
|
post_opt_ir: Option<PathBuf>,
|
|
|
|
|
|
|
|
/// Emit LLVM generated native code object file.
|
|
|
|
#[structopt(long = "backend-llvm-object-file", parse(from_os_str))]
|
|
|
|
obj_file: Option<PathBuf>,
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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
|
2019-04-10 18:23:25 -07:00
|
|
|
#[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
|
|
|
|
2019-05-03 17:34:57 -07:00
|
|
|
/// Begin execution at the specified symbol
|
2019-05-05 12:13:35 -07:00
|
|
|
#[structopt(long = "em-entrypoint", group = "emscripten")]
|
|
|
|
em_entrypoint: Option<String>,
|
2019-05-03 17:34:57 -07:00
|
|
|
|
2019-04-19 12:48:29 -07:00
|
|
|
/// WASI pre-opened directory
|
|
|
|
#[structopt(long = "dir", multiple = true, group = "wasi")]
|
|
|
|
pre_opened_directories: Vec<String>,
|
2019-04-18 15:43:02 -07:00
|
|
|
|
2019-05-16 17:35:13 -07:00
|
|
|
/// Map a host directory to a different location for the wasm module
|
2019-05-20 16:39:02 -07:00
|
|
|
#[structopt(long = "mapdir", multiple = true)]
|
2019-05-16 17:35:13 -07:00
|
|
|
mapped_dirs: Vec<String>,
|
|
|
|
|
2019-05-30 11:58:52 -07:00
|
|
|
/// Pass custom environment variables
|
|
|
|
#[structopt(long = "env", multiple = true)]
|
|
|
|
env_vars: Vec<String>,
|
|
|
|
|
2019-05-16 17:35:13 -07:00
|
|
|
/// Custom code loader
|
2019-05-03 00:23:41 +08:00
|
|
|
#[structopt(
|
|
|
|
long = "loader",
|
|
|
|
raw(possible_values = "LoaderName::variants()", case_insensitive = "true")
|
|
|
|
)]
|
|
|
|
loader: Option<LoaderName>,
|
|
|
|
|
2019-07-12 23:33:54 +08:00
|
|
|
/// Path to previously saved instance image to resume.
|
2019-07-09 17:57:31 -07:00
|
|
|
#[cfg(feature = "backend-singlepass")]
|
2019-06-27 01:29:10 +08:00
|
|
|
#[structopt(long = "resume")]
|
|
|
|
resume: Option<String>,
|
|
|
|
|
2019-07-12 23:33:54 +08:00
|
|
|
/// Whether or not state tracking should be disabled during compilation.
|
|
|
|
/// State tracking is necessary for tier switching and backtracing.
|
|
|
|
#[structopt(long = "no-track-state")]
|
|
|
|
no_track_state: bool,
|
|
|
|
|
2019-07-08 15:46:28 -07:00
|
|
|
/// The command name is a string that will override the first argument passed
|
|
|
|
/// to the wasm program. This is used in wapm to provide nicer output in
|
|
|
|
/// help commands and error messages of the running wasm program
|
2019-04-12 11:17:02 -07:00
|
|
|
#[structopt(long = "command-name", hidden = true)]
|
|
|
|
command_name: Option<String>,
|
|
|
|
|
2019-07-08 15:46:28 -07:00
|
|
|
/// A prehashed string, used to speed up start times by avoiding hashing the
|
|
|
|
/// wasm module. If the specified hash is not found, Wasmer will hash the module
|
|
|
|
/// as if no `cache-key` argument was passed.
|
|
|
|
#[structopt(long = "cache-key", hidden = true)]
|
|
|
|
cache_key: Option<String>,
|
|
|
|
|
2019-08-08 16:05:17 -07:00
|
|
|
#[cfg(feature = "backend-llvm")]
|
|
|
|
#[structopt(flatten)]
|
2019-08-08 19:42:41 -07:00
|
|
|
backend_llvm_options: LLVMCLIOptions,
|
2019-08-08 16:05:17 -07:00
|
|
|
|
2019-07-25 23:33:30 -07:00
|
|
|
#[structopt(flatten)]
|
|
|
|
features: PrestandardFeatures,
|
|
|
|
|
2019-04-11 12:44:03 -07:00
|
|
|
/// Application arguments
|
|
|
|
#[structopt(name = "--", raw(multiple = "true"))]
|
|
|
|
args: Vec<String>,
|
|
|
|
}
|
|
|
|
|
2019-05-03 00:23:41 +08:00
|
|
|
#[allow(dead_code)]
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
enum LoaderName {
|
|
|
|
Local,
|
2019-07-12 23:02:57 -05:00
|
|
|
#[cfg(feature = "loader-kernel")]
|
2019-05-03 23:07:07 -07:00
|
|
|
Kernel,
|
2019-05-03 00:23:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl LoaderName {
|
|
|
|
pub fn variants() -> &'static [&'static str] {
|
|
|
|
&[
|
|
|
|
"local",
|
2019-07-12 23:02:57 -05:00
|
|
|
#[cfg(feature = "loader-kernel")]
|
2019-05-03 23:07:07 -07:00
|
|
|
"kernel",
|
2019-05-03 00:23:41 +08:00
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for LoaderName {
|
|
|
|
type Err = String;
|
|
|
|
fn from_str(s: &str) -> Result<LoaderName, String> {
|
|
|
|
match s.to_lowercase().as_str() {
|
|
|
|
"local" => Ok(LoaderName::Local),
|
2019-07-12 23:02:57 -05:00
|
|
|
#[cfg(feature = "loader-kernel")]
|
2019-05-03 23:07:07 -07:00
|
|
|
"kernel" => Ok(LoaderName::Kernel),
|
2019-05-03 00:23:41 +08:00
|
|
|
_ => Err(format!("The loader {} doesn't exist", s)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
2019-07-25 23:33:30 -07:00
|
|
|
|
|
|
|
#[structopt(flatten)]
|
|
|
|
features: PrestandardFeatures,
|
2019-04-03 16:52:37 -07:00
|
|
|
}
|
|
|
|
|
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") {
|
2019-07-29 17:33:50 -07:00
|
|
|
Ok(dir) => {
|
|
|
|
let mut path = PathBuf::from(dir);
|
2019-07-29 18:31:57 -07:00
|
|
|
path.push(VERSION);
|
2019-07-29 17:33:50 -07:00
|
|
|
path
|
2019-07-29 17:54:27 -07:00
|
|
|
}
|
2019-02-22 11:42:30 -08:00
|
|
|
Err(_) => {
|
|
|
|
// We use a temporal directory for saving cache files
|
|
|
|
let mut temp_dir = env::temp_dir();
|
|
|
|
temp_dir.push("wasmer");
|
2019-07-29 18:31:57 -07:00
|
|
|
temp_dir.push(VERSION);
|
2019-02-22 11:42:30 -08:00
|
|
|
temp_dir
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-30 11:58:52 -07:00
|
|
|
fn get_mapped_dirs(input: &[String]) -> Result<Vec<(String, PathBuf)>, String> {
|
|
|
|
let mut md = vec![];
|
|
|
|
for entry in input.iter() {
|
|
|
|
if let [alias, real_dir] = entry.split(':').collect::<Vec<&str>>()[..] {
|
|
|
|
let pb = PathBuf::from(&real_dir);
|
|
|
|
if let Ok(pb_metadata) = pb.metadata() {
|
|
|
|
if !pb_metadata.is_dir() {
|
|
|
|
return Err(format!(
|
|
|
|
"\"{}\" exists, but it is not a directory",
|
|
|
|
&real_dir
|
|
|
|
));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Err(format!("Directory \"{}\" does not exist", &real_dir));
|
|
|
|
}
|
|
|
|
md.push((alias.to_string(), pb));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return Err(format!(
|
|
|
|
"Directory mappings must consist of two paths separate by a colon. Found {}",
|
|
|
|
&entry
|
|
|
|
));
|
|
|
|
}
|
|
|
|
Ok(md)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_env_var_args(input: &[String]) -> Result<Vec<(&str, &str)>, String> {
|
|
|
|
let mut ev = vec![];
|
|
|
|
for entry in input.iter() {
|
|
|
|
if let [env_var, value] = entry.split('=').collect::<Vec<&str>>()[..] {
|
|
|
|
ev.push((env_var, value));
|
|
|
|
} else {
|
|
|
|
return Err(format!(
|
|
|
|
"Env vars must be of the form <var_name>=<value>. Found {}",
|
|
|
|
&entry
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(ev)
|
|
|
|
}
|
|
|
|
|
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:53:15 -08:00
|
|
|
let disable_cache = options.disable_cache;
|
2019-02-25 11:46:48 -08:00
|
|
|
|
2019-05-30 11:58:52 -07:00
|
|
|
let mapped_dirs = get_mapped_dirs(&options.mapped_dirs[..])?;
|
|
|
|
let env_vars = get_env_var_args(&options.env_vars[..])?;
|
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 {
|
2019-05-30 11:58:52 -07:00
|
|
|
return Err(
|
2019-03-26 16:41:40 -07:00
|
|
|
"Can't parse symbol map (expected each entry to be of the form: `0:func_name`)"
|
2019-05-30 11:58:52 -07:00
|
|
|
.to_string(),
|
|
|
|
);
|
2019-03-26 16:41:40 -07:00
|
|
|
};
|
|
|
|
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 {
|
2019-05-30 11:58:52 -07:00
|
|
|
return Err(
|
2019-03-26 16:41:40 -07:00
|
|
|
"Can't parse symbol map (expected each entry to be of the form: `0:func_name`)"
|
2019-05-30 11:58:52 -07:00
|
|
|
.to_string(),
|
|
|
|
);
|
2019-03-26 16:41:40 -07:00
|
|
|
}
|
|
|
|
.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-07-25 23:33:30 -07:00
|
|
|
// Don't error on --enable-all for other backends.
|
|
|
|
if options.features.simd && options.backend != Backend::LLVM {
|
|
|
|
return Err("SIMD is only supported in the LLVM backend for now".to_string());
|
|
|
|
}
|
|
|
|
|
2019-01-24 15:09:56 -08:00
|
|
|
if !utils::is_wasm_binary(&wasm_binary) {
|
2019-07-02 14:32:41 -07:00
|
|
|
let mut features = wabt::Features::new();
|
2019-07-25 23:33:30 -07:00
|
|
|
if options.features.simd || options.features.all {
|
2019-07-24 14:10:09 -07:00
|
|
|
features.enable_simd();
|
|
|
|
}
|
2019-07-02 14:32:41 -07:00
|
|
|
wasm_binary = wabt::wat2wasm_with_features(wasm_binary, features)
|
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-08-08 16:05:17 -07:00
|
|
|
#[cfg(feature = "backend-llvm")]
|
|
|
|
{
|
|
|
|
if options.backend == Backend::LLVM {
|
2019-08-08 19:42:41 -07:00
|
|
|
let options = options.backend_llvm_options.clone();
|
2019-08-08 16:05:17 -07:00
|
|
|
unsafe {
|
2019-08-08 19:42:41 -07:00
|
|
|
wasmer_llvm_backend::GLOBAL_OPTIONS = LLVMOptions {
|
|
|
|
pre_opt_ir: options.pre_opt_ir,
|
|
|
|
post_opt_ir: options.post_opt_ir,
|
|
|
|
obj_file: options.obj_file,
|
|
|
|
}
|
2019-08-08 16:05:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-11 14:34:54 -07:00
|
|
|
let compiler: Box<dyn Compiler> = match options.backend {
|
2019-07-09 17:57:31 -07:00
|
|
|
#[cfg(feature = "backend-singlepass")]
|
2019-06-08 16:48:41 -05:00
|
|
|
Backend::Singlepass => Box::new(SinglePassCompiler::new()),
|
2019-07-09 17:57:31 -07:00
|
|
|
#[cfg(not(feature = "backend-singlepass"))]
|
2019-04-11 15:31:02 -07:00
|
|
|
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()),
|
2019-07-09 17:57:31 -07:00
|
|
|
#[cfg(feature = "backend-llvm")]
|
2019-06-08 16:48:41 -05:00
|
|
|
Backend::LLVM => Box::new(LLVMCompiler::new()),
|
2019-07-09 17:57:31 -07:00
|
|
|
#[cfg(not(feature = "backend-llvm"))]
|
2019-04-11 15:31:02 -07:00
|
|
|
Backend::LLVM => return Err("the llvm backend is not enabled".to_string()),
|
2019-04-11 14:34:54 -07:00
|
|
|
};
|
|
|
|
|
2019-07-12 23:33:54 +08:00
|
|
|
let track_state = !options.no_track_state;
|
|
|
|
|
2019-07-12 23:02:57 -05:00
|
|
|
#[cfg(feature = "loader-kernel")]
|
2019-05-06 07:15:30 -07:00
|
|
|
let is_kernel_loader = if let Some(LoaderName::Kernel) = options.loader {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
|
|
|
|
2019-07-12 23:02:57 -05:00
|
|
|
#[cfg(not(feature = "loader-kernel"))]
|
2019-05-14 16:02:27 +08:00
|
|
|
let is_kernel_loader = false;
|
|
|
|
|
2019-05-06 07:15:30 -07:00
|
|
|
let module = if is_kernel_loader {
|
|
|
|
webassembly::compile_with_config_with(
|
|
|
|
&wasm_binary[..],
|
|
|
|
CompilerConfig {
|
|
|
|
symbol_map: em_symbol_map,
|
2019-05-08 10:25:29 -07:00
|
|
|
memory_bound_check_mode: MemoryBoundCheckMode::Disable,
|
2019-05-06 07:15:30 -07:00
|
|
|
enforce_stack_check: true,
|
2019-07-12 23:33:54 +08:00
|
|
|
track_state,
|
2019-07-25 23:33:30 -07:00
|
|
|
features: Features {
|
|
|
|
simd: options.features.simd || options.features.all,
|
|
|
|
},
|
2019-05-06 07:15:30 -07:00
|
|
|
},
|
|
|
|
&*compiler,
|
2019-05-14 16:04:08 +08:00
|
|
|
)
|
|
|
|
.map_err(|e| format!("Can't compile module: {:?}", e))?
|
2019-05-06 07:15:30 -07:00
|
|
|
} else if disable_cache {
|
|
|
|
webassembly::compile_with_config_with(
|
|
|
|
&wasm_binary[..],
|
|
|
|
CompilerConfig {
|
|
|
|
symbol_map: em_symbol_map,
|
2019-07-12 23:33:54 +08:00
|
|
|
track_state,
|
2019-07-25 23:33:30 -07:00
|
|
|
features: Features {
|
|
|
|
simd: options.features.simd || options.features.all,
|
|
|
|
},
|
2019-05-06 07:15:30 -07:00
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
&*compiler,
|
2019-05-14 16:04:08 +08:00
|
|
|
)
|
|
|
|
.map_err(|e| format!("Can't compile module: {:?}", e))?
|
2019-05-06 07:15:30 -07:00
|
|
|
} else {
|
2019-02-22 12:01:03 -08:00
|
|
|
// If we have cache enabled
|
|
|
|
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))?
|
|
|
|
};
|
2019-07-08 15:46:28 -07:00
|
|
|
let load_cache_key = || -> Result<_, String> {
|
|
|
|
if let Some(ref prehashed_cache_key) = options.cache_key {
|
2019-07-08 17:05:54 -07:00
|
|
|
if let Ok(module) =
|
|
|
|
WasmHash::decode(prehashed_cache_key).and_then(|prehashed_key| {
|
|
|
|
cache.load_with_backend(prehashed_key, options.backend)
|
|
|
|
})
|
2019-07-08 15:46:28 -07:00
|
|
|
{
|
|
|
|
debug!("using prehashed key: {}", prehashed_cache_key);
|
|
|
|
return Ok(module);
|
|
|
|
}
|
2019-02-22 12:01:03 -08:00
|
|
|
}
|
2019-07-08 15:46:28 -07:00
|
|
|
// We generate a hash for the given binary, so we can use it as key
|
|
|
|
// for the Filesystem cache
|
2019-07-08 17:05:54 -07:00
|
|
|
let hash = WasmHash::generate(&wasm_binary);
|
2019-07-08 15:46:28 -07:00
|
|
|
|
|
|
|
// 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
|
2019-07-08 17:05:54 -07:00
|
|
|
match cache.load_with_backend(hash, options.backend) {
|
2019-07-08 15:46:28 -07:00
|
|
|
Ok(module) => {
|
|
|
|
// We are able to load the module from cache
|
|
|
|
Ok(module)
|
|
|
|
}
|
|
|
|
Err(_) => {
|
|
|
|
let module = webassembly::compile_with_config_with(
|
|
|
|
&wasm_binary[..],
|
|
|
|
CompilerConfig {
|
|
|
|
symbol_map: em_symbol_map,
|
2019-07-12 23:33:54 +08:00
|
|
|
track_state,
|
2019-07-27 11:13:13 -07:00
|
|
|
features: Features {
|
|
|
|
simd: options.features.simd || options.features.all,
|
|
|
|
},
|
2019-07-08 15:46:28 -07:00
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
&*compiler,
|
|
|
|
)
|
|
|
|
.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();
|
|
|
|
|
|
|
|
Ok(module)
|
|
|
|
}
|
2019-02-22 12:01:03 -08:00
|
|
|
}
|
2019-07-08 15:46:28 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
load_cache_key()?
|
2019-02-22 11:42:30 -08:00
|
|
|
};
|
2018-12-10 20:15:41 -08:00
|
|
|
|
2019-05-03 00:23:41 +08:00
|
|
|
if let Some(loader) = options.loader {
|
2019-05-04 08:28:13 -07:00
|
|
|
let mut import_object = wasmer_runtime_core::import::ImportObject::new();
|
|
|
|
import_object.allow_missing_functions = true; // Import initialization might be left to the loader.
|
2019-05-03 00:23:41 +08:00
|
|
|
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())
|
2019-05-15 10:35:26 -07:00
|
|
|
.map(|x| {
|
|
|
|
Value::I32(x.parse().expect(&format!(
|
|
|
|
"Can't parse the provided argument {:?} as a integer",
|
|
|
|
x
|
|
|
|
)))
|
|
|
|
})
|
2019-05-03 00:23:41 +08:00
|
|
|
.collect();
|
2019-05-15 10:35:26 -07:00
|
|
|
let index = instance
|
|
|
|
.resolve_func("_start")
|
|
|
|
.expect("The loader requires a _start function to be present in the module");
|
2019-08-08 19:42:41 -07:00
|
|
|
let mut ins: Box<dyn LoadedInstance<Error = String>> = match loader {
|
2019-05-15 10:35:26 -07:00
|
|
|
LoaderName::Local => Box::new(
|
|
|
|
instance
|
|
|
|
.load(LocalLoader)
|
|
|
|
.expect("Can't use the local loader"),
|
|
|
|
),
|
2019-07-12 23:02:57 -05:00
|
|
|
#[cfg(feature = "loader-kernel")]
|
2019-05-15 10:35:26 -07:00
|
|
|
LoaderName::Kernel => Box::new(
|
|
|
|
instance
|
|
|
|
.load(::wasmer_kernel_loader::KernelLoader)
|
|
|
|
.expect("Can't use the kernel loader"),
|
|
|
|
),
|
2019-05-03 23:07:07 -07:00
|
|
|
};
|
|
|
|
println!("{:?}", ins.call(index, &args));
|
2019-05-14 16:04:08 +08:00
|
|
|
return Ok(());
|
2019-05-03 00:23:41 +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) {
|
2019-01-28 14:31:03 -08:00
|
|
|
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,
|
2019-07-06 22:05:45 -07:00
|
|
|
&mut emscripten_globals,
|
2019-04-22 16:45:36 -07:00
|
|
|
if let Some(cn) = &options.command_name {
|
|
|
|
cn
|
|
|
|
} else {
|
|
|
|
options.path.to_str().unwrap()
|
|
|
|
},
|
|
|
|
options.args.iter().map(|arg| arg.as_str()).collect(),
|
2019-05-05 12:13:35 -07:00
|
|
|
options.em_entrypoint.clone(),
|
2019-05-24 18:00:07 -07:00
|
|
|
mapped_dirs,
|
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-07-05 00:04:58 -05:00
|
|
|
let import_object = wasmer_wasi::generate_import_object(
|
2019-04-22 16:45:36 -07:00
|
|
|
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(),
|
2019-05-30 11:58:52 -07:00
|
|
|
env_vars
|
|
|
|
.into_iter()
|
2019-04-22 16:45:36 -07:00
|
|
|
.map(|(k, v)| format!("{}={}", k, v).into_bytes())
|
2019-04-12 11:17:02 -07:00
|
|
|
.collect(),
|
2019-04-22 16:45:36 -07:00
|
|
|
options.pre_opened_directories.clone(),
|
2019-05-16 17:35:13 -07:00
|
|
|
mapped_dirs,
|
2019-04-22 16:45:36 -07:00
|
|
|
);
|
2019-03-28 12:19:23 -07:00
|
|
|
|
2019-07-05 15:53:14 -05:00
|
|
|
#[allow(unused_mut)] // mut used in feature
|
|
|
|
let mut instance = module
|
2019-04-22 16:45:36 -07:00
|
|
|
.instantiate(&import_object)
|
|
|
|
.map_err(|e| format!("Can't instantiate module: {:?}", e))?;
|
2018-11-07 11:47:06 +01:00
|
|
|
|
2019-06-27 00:41:07 +08:00
|
|
|
let start: Func<(), ()> = instance.func("_start").map_err(|e| format!("{:?}", e))?;
|
|
|
|
|
2019-07-09 17:57:31 -07:00
|
|
|
#[cfg(feature = "backend-singlepass")]
|
2019-06-27 00:41:07 +08:00
|
|
|
unsafe {
|
|
|
|
if options.backend == Backend::Singlepass {
|
2019-07-04 01:45:54 +08:00
|
|
|
use wasmer_runtime_core::fault::{catch_unsafe_unwind, ensure_sighandler};
|
2019-06-27 00:41:07 +08:00
|
|
|
use wasmer_runtime_core::state::{
|
|
|
|
x64::invoke_call_return_on_stack, InstanceImage,
|
|
|
|
};
|
|
|
|
use wasmer_runtime_core::vm::Ctx;
|
|
|
|
|
|
|
|
ensure_sighandler();
|
|
|
|
|
|
|
|
let start_raw: extern "C" fn(&mut Ctx) =
|
|
|
|
::std::mem::transmute(start.get_vm_func());
|
|
|
|
|
2019-06-27 01:29:10 +08:00
|
|
|
let mut image: Option<InstanceImage> = if let Some(ref path) = options.resume {
|
|
|
|
let mut f = File::open(path).unwrap();
|
|
|
|
let mut out: Vec<u8> = vec![];
|
|
|
|
f.read_to_end(&mut out).unwrap();
|
|
|
|
Some(InstanceImage::from_bytes(&out).expect("failed to decode image"))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2019-06-27 15:49:43 +08:00
|
|
|
let breakpoints = instance.module.runnable_module.get_breakpoints();
|
|
|
|
|
2019-06-27 00:41:07 +08:00
|
|
|
loop {
|
|
|
|
let ret = if let Some(image) = image.take() {
|
|
|
|
let msm = instance
|
|
|
|
.module
|
|
|
|
.runnable_module
|
|
|
|
.get_module_state_map()
|
|
|
|
.unwrap();
|
|
|
|
let code_base =
|
|
|
|
instance.module.runnable_module.get_code().unwrap().as_ptr()
|
|
|
|
as usize;
|
2019-06-27 01:04:59 +08:00
|
|
|
invoke_call_return_on_stack(
|
|
|
|
&msm,
|
|
|
|
code_base,
|
|
|
|
image,
|
|
|
|
instance.context_mut(),
|
2019-06-27 15:49:43 +08:00
|
|
|
breakpoints.clone(),
|
2019-06-27 01:04:59 +08:00
|
|
|
)
|
|
|
|
.map(|_| ())
|
2019-06-27 00:41:07 +08:00
|
|
|
} else {
|
2019-06-27 15:49:43 +08:00
|
|
|
catch_unsafe_unwind(
|
|
|
|
|| start_raw(instance.context_mut()),
|
|
|
|
breakpoints.clone(),
|
|
|
|
)
|
2019-06-27 00:41:07 +08:00
|
|
|
};
|
|
|
|
if let Err(e) = ret {
|
|
|
|
if let Some(new_image) = e.downcast_ref::<InstanceImage>() {
|
|
|
|
let op = interactive_shell(InteractiveShellContext {
|
|
|
|
image: Some(new_image.clone()),
|
|
|
|
});
|
|
|
|
match op {
|
|
|
|
ShellExitOperation::ContinueWith(new_image) => {
|
|
|
|
image = Some(new_image);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Err("Error while executing WebAssembly".into());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2019-06-26 12:46:01 +08:00
|
|
|
}
|
2019-06-26 01:38:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 00:41:07 +08:00
|
|
|
{
|
|
|
|
use wasmer_runtime::error::RuntimeError;
|
|
|
|
let result = start.call();
|
|
|
|
|
|
|
|
if let Err(ref err) = result {
|
|
|
|
match err {
|
|
|
|
RuntimeError::Trap { msg } => panic!("wasm trap occured: {}", msg),
|
2019-07-30 15:59:21 +09:00
|
|
|
#[cfg(feature = "wasi")]
|
2019-06-27 00:41:07 +08:00
|
|
|
RuntimeError::Error { data } => {
|
|
|
|
if let Some(error_code) = data.downcast_ref::<wasmer_wasi::ExitCode>() {
|
|
|
|
std::process::exit(error_code.code as i32)
|
|
|
|
}
|
2019-04-22 16:45:36 -07:00
|
|
|
}
|
2019-07-30 15:59:21 +09:00
|
|
|
#[cfg(not(feature = "wasi"))]
|
|
|
|
RuntimeError::Error { .. } => (),
|
2019-04-22 16:45:36 -07:00
|
|
|
}
|
2019-06-27 00:41:07 +08:00
|
|
|
panic!("error: {:?}", err)
|
2019-04-22 16:45:36 -07:00
|
|
|
}
|
|
|
|
}
|
2019-04-12 11:17:02 -07:00
|
|
|
} 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();
|
2019-06-02 15:51:52 -05:00
|
|
|
instance
|
|
|
|
.dyn_func("main")
|
|
|
|
.map_err(|e| format!("{:?}", e))?
|
|
|
|
.call(&args)
|
|
|
|
.map_err(|e| format!("{:?}", e))?;
|
2019-04-22 16:45:36 -07:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2019-07-09 17:57:31 -07:00
|
|
|
#[cfg(feature = "backend-singlepass")]
|
2019-06-27 00:41:07 +08:00
|
|
|
struct InteractiveShellContext {
|
|
|
|
image: Option<wasmer_runtime_core::state::InstanceImage>,
|
|
|
|
}
|
|
|
|
|
2019-07-09 17:57:31 -07:00
|
|
|
#[cfg(feature = "backend-singlepass")]
|
2019-06-27 00:41:07 +08:00
|
|
|
#[derive(Debug)]
|
|
|
|
enum ShellExitOperation {
|
|
|
|
ContinueWith(wasmer_runtime_core::state::InstanceImage),
|
|
|
|
}
|
|
|
|
|
2019-07-09 17:57:31 -07:00
|
|
|
#[cfg(feature = "backend-singlepass")]
|
2019-06-27 00:41:07 +08:00
|
|
|
fn interactive_shell(mut ctx: InteractiveShellContext) -> ShellExitOperation {
|
|
|
|
use std::io::Write;
|
|
|
|
|
|
|
|
let mut stdout = ::std::io::stdout();
|
|
|
|
let stdin = ::std::io::stdin();
|
|
|
|
|
|
|
|
loop {
|
|
|
|
print!("Wasmer> ");
|
|
|
|
stdout.flush().unwrap();
|
|
|
|
let mut line = String::new();
|
|
|
|
stdin.read_line(&mut line).unwrap();
|
|
|
|
let mut parts = line.split(" ").filter(|x| x.len() > 0).map(|x| x.trim());
|
|
|
|
|
|
|
|
let cmd = parts.next();
|
|
|
|
if cmd.is_none() {
|
|
|
|
println!("Command required");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let cmd = cmd.unwrap();
|
|
|
|
|
|
|
|
match cmd {
|
|
|
|
"snapshot" => {
|
|
|
|
let path = parts.next();
|
|
|
|
if path.is_none() {
|
|
|
|
println!("Usage: snapshot [out_path]");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let path = path.unwrap();
|
|
|
|
|
|
|
|
if let Some(ref image) = ctx.image {
|
|
|
|
let buf = image.to_bytes();
|
|
|
|
let mut f = match File::create(path) {
|
|
|
|
Ok(x) => x,
|
|
|
|
Err(e) => {
|
|
|
|
println!("Cannot open output file at {}: {:?}", path, e);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if let Err(e) = f.write_all(&buf) {
|
|
|
|
println!("Cannot write to output file at {}: {:?}", path, e);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
println!("Done");
|
|
|
|
} else {
|
|
|
|
println!("Program state not available");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"continue" | "c" => {
|
|
|
|
if let Some(image) = ctx.image.take() {
|
|
|
|
return ShellExitOperation::ContinueWith(image);
|
|
|
|
} else {
|
|
|
|
println!("Program state not available, cannot continue execution");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"backtrace" | "bt" => {
|
|
|
|
if let Some(ref image) = ctx.image {
|
|
|
|
println!("{}", image.execution_state.colored_output());
|
|
|
|
} else {
|
|
|
|
println!("State not available");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"exit" | "quit" => {
|
|
|
|
exit(0);
|
|
|
|
}
|
2019-06-27 17:54:06 +08:00
|
|
|
"" => {}
|
2019-06-27 00:41:07 +08:00
|
|
|
_ => {
|
|
|
|
println!("Unknown command: {}", cmd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-06-27 15:49:43 +08:00
|
|
|
eprintln!("execute_wasm: {:?}", 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-07-25 23:33:30 -07:00
|
|
|
wasmer_runtime_core::validate_and_report_errors_with_features(
|
|
|
|
&wasm_binary,
|
|
|
|
Features {
|
|
|
|
simd: validate.features.simd || validate.features.all,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.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-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);
|
|
|
|
}
|
2018-10-14 23:47:35 +02:00
|
|
|
}
|
|
|
|
}
|
2019-07-29 18:25:12 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn filesystem_cache_should_work() -> Result<(), String> {
|
|
|
|
let wasmer_cache_dir = get_cache_dir();
|
|
|
|
|
2019-07-29 18:29:20 -07:00
|
|
|
unsafe { FileSystemCache::new(wasmer_cache_dir).map_err(|e| format!("Cache error: {:?}", e))? };
|
|
|
|
|
2019-07-29 18:25:12 -07:00
|
|
|
Ok(())
|
|
|
|
}
|