Skip to content

Commit

Permalink
Merge pull request #3650 from wasmerio/wasmer-run-v2
Browse files Browse the repository at this point in the history
Wasmer run-unstable
  • Loading branch information
Michael Bryan authored Mar 21, 2023
2 parents 4a99b50 + 3c7f73b commit 898ae3c
Show file tree
Hide file tree
Showing 31 changed files with 2,567 additions and 521 deletions.
841 changes: 611 additions & 230 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions lib/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@
//! compilation-time and runtime performance, useful for development,
//! * [`wasmer-compiler-llvm`] provides a deeply optimized executable
//! code with the fastest runtime speed, ideal for production.
//!
//!
//! * **Headless mode** — Once a WebAssembly module has been compiled, it
//! is possible to serialize it in a file for example, and later execute
//! it with Wasmer with headless mode turned on. Headless Wasmer has no
//! compiler, which makes it more portable and faster to load. It's
//! ideal for constrainted environments.
//!
//!
//! * **Cross-compilation** — Most compilers support cross-compilation. It
//! means it possible to pre-compile a WebAssembly module targetting a
//! different architecture or platform and serialize it, to then run it
Expand Down
Binary file added lib/c-api/examples/assets/staticserver.webc
Binary file not shown.
4 changes: 2 additions & 2 deletions lib/cache/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub trait Cache {
/// The deserialization error for the implementation
type DeserializeError: Error + Send + Sync;

/// Loads a module using the provided [`Store`] and [`Hash`].
/// Loads a module using the provided [`wasmer::Store`] and [`crate::Hash`].
///
/// # Safety
/// This function is unsafe as the cache store could be tampered with.
Expand All @@ -23,6 +23,6 @@ pub trait Cache {
key: Hash,
) -> Result<Module, Self::DeserializeError>;

/// Store a [`Module`] into the cache with the given [`Hash`].
/// Store a [`Module`] into the cache with the given [`crate::Hash`].
fn store(&mut self, key: Hash, module: &Module) -> Result<(), Self::SerializeError>;
}
5 changes: 3 additions & 2 deletions lib/cache/src/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use wasmer::{AsEngineRef, DeserializeError, Module, SerializeError};
/// Ok(())
/// }
/// ```
#[derive(Debug, Clone)]
pub struct FileSystemCache {
path: PathBuf,
ext: Option<String>,
Expand Down Expand Up @@ -97,7 +98,7 @@ impl Cache for FileSystemCache {
key: Hash,
) -> Result<Module, Self::DeserializeError> {
let filename = if let Some(ref ext) = self.ext {
format!("{}.{}", key.to_string(), ext)
format!("{}.{}", key, ext)
} else {
key.to_string()
};
Expand All @@ -113,7 +114,7 @@ impl Cache for FileSystemCache {

fn store(&mut self, key: Hash, module: &Module) -> Result<(), Self::SerializeError> {
let filename = if let Some(ref ext) = self.ext {
format!("{}.{}", key.to_string(), ext)
format!("{}.{}", key, ext)
} else {
key.to_string()
};
Expand Down
34 changes: 21 additions & 13 deletions lib/cache/src/hash.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
use crate::DeserializeError;
use std::str::FromStr;
use std::string::ToString;
use std::{
fmt::{self, Display, Formatter},
str::FromStr,
};

/// A hash used as a key when loading and storing modules in a
/// [`Cache`].
/// [`crate::Cache`].
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
// Hash is made up of a 32 byte array
pub struct Hash([u8; 32]);

impl Hash {
/// Creates a new instance from 32 raw bytes.
/// Does not perform any hashing. In order to create a hash from data,
/// use `Hash::generate`.
/// use [`Hash::generate()`].
pub fn new(bytes: [u8; 32]) -> Self {
Self(bytes)
}
Expand All @@ -21,17 +24,19 @@ impl Hash {
let hash = blake3::hash(bytes);
Self::new(hash.into())
}

pub(crate) fn to_array(self) -> [u8; 32] {
self.0
}
}

impl ToString for Hash {
/// Create the hexadecimal representation of the
impl Display for Hash {
/// Print the hexadecimal representation of the
/// stored hash.
fn to_string(&self) -> String {
hex::encode(&self.to_array())
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let mut buffer = [0_u8; 64];

hex::encode_to_slice(&self.0, &mut buffer)
.expect("Can never fail with a hard-coded buffer length");
let s = std::str::from_utf8(&buffer).map_err(|_| fmt::Error)?;

f.write_str(s)
}
}

Expand Down Expand Up @@ -62,13 +67,16 @@ mod tests {
use super::*;

#[test]
fn hash_to_array_works() {
fn hash_is_displayed_as_hex() {
let original = [
0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x12, 0x65, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
0x12, 0x65, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x12, 0x65, 0xAA, 0xBB, 0xCC, 0xDD,
0xEE, 0xFF, 0x12, 0x65,
];
let hash = Hash::new(original);
assert_eq!(hash.to_array(), original);
assert_eq!(
hash.to_string(),
"aabbccddeeff1265aabbccddeeff1265aabbccddeeff1265aabbccddeeff1265"
);
}
}
47 changes: 21 additions & 26 deletions lib/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ required-features = ["headless"]

[dependencies]
wasmer = { version = "=3.2.0-alpha.1", path = "../api", default-features = false }
wasmer-compiler = { version = "=3.2.0-alpha.1", path = "../compiler", features = ["compiler", ] }
wasmer-compiler = { version = "=3.2.0-alpha.1", path = "../compiler", features = ["compiler"] }
wasmer-compiler-cranelift = { version = "=3.2.0-alpha.1", path = "../compiler-cranelift", optional = true }
wasmer-compiler-singlepass = { version = "=3.2.0-alpha.1", path = "../compiler-singlepass", optional = true }
wasmer-compiler-llvm = { version = "=3.2.0-alpha.1", path = "../compiler-llvm", optional = true }
wasmer-emscripten = { version = "=3.2.0-alpha.1", path = "../emscripten", optional = true }
wasmer-emscripten = { version = "=3.2.0-alpha.1", path = "../emscripten" }
wasmer-vm = { version = "=3.2.0-alpha.1", path = "../vm" }
wasmer-wasix = { version = "0.1.0", path = "../wasi", optional = true }
wasmer-wasix = { version = "0.1.0", path = "../wasi", features = ["logging", "webc_runner", "webc_runner_rt_wcgi", "webc_runner_rt_wasi", "webc_runner_rt_emscripten", "host-fs"] }
wasmer-wasix-experimental-io-devices = { version = "0.1.0", path = "../wasi-experimental-io-devices", optional = true, features = ["link_external_libs"] }
wasmer-wast = { version = "=3.2.0-alpha.1", path = "../../tests/lib/wast", optional = true }
wasmer-cache = { version = "=3.2.0-alpha.1", path = "../cache", optional = true }
wasmer-cache = { version = "=3.2.0-alpha.1", path = "../cache", features = ["blake3-pure"] }
wasmer-types = { version = "=3.2.0-alpha.1", path = "../types", features = ["enable-serde"] }
wasmer-registry = { version = "=4.0.0", path = "../registry" }
wasmer-object = { version = "=3.2.0-alpha.1", path = "../object", optional = true }
Expand Down Expand Up @@ -68,8 +68,10 @@ regex = "1.6.0"
toml = "0.5.9"
url = "2.3.1"
libc = { version = "^0.2", default-features = false }
nuke-dir = { version = "0.1.0", optional = true }
webc = { version = "5.0.0-rc.5", optional = true }
webc = { version = "5.0.0-rc.5" }
# HACK(Michael-F-Bryan): Remove this once a new version of wapm-targz-to-pirita
# is published that doesn't have a public dependency on webc
webc_v4 = { version = "4", package = "webc" }
isatty = "0.1.9"
dialoguer = "0.10.2"
tldextract = "0.6.0"
Expand All @@ -88,11 +90,13 @@ pathdiff = "0.2.1"
sha2 = "0.10.6"
object = "0.30.0"
wasm-coredump-builder = { version = "0.1.11" }
tracing = { version = "0.1", optional = true }
tracing-subscriber = { version = "0.3", features = [ "env-filter", "fmt" ], optional = true }
tracing = { version = "0.1" }
tracing-subscriber = { version = "0.3", features = [ "env-filter", "fmt" ] }
clap-verbosity-flag = "1"
wapm-targz-to-pirita = "0.1.7"

[build-dependencies]
chrono = { version = "^0.4", default-features = false, features = [ "std", "clock" ] }
chrono = { version = "^0.4", default-features = false, features = ["std", "clock"] }

[target.'cfg(target_os = "linux")'.dependencies]
unix_mode = "0.1.3"
Expand All @@ -110,24 +114,10 @@ default = [
"wasmer-artifact-create",
"static-artifact-create",
"webc_runner",
"tracing",
]
cache = ["wasmer-cache"]
cache-blake3-pure = ["wasmer-cache/blake3-pure"]
wast = ["wasmer-wast"]
wasi = ["wasmer-wasix", "host-net"]
host-net = [ "virtual-net/host-net" ]
emscripten = ["wasmer-emscripten"]
wat = ["wasmer/wat"]
webc_runner = [
"wasi",
"wasmer-wasix/webc_runner",
"wasmer-wasix/webc_runner_rt_wasi",
"wasmer-wasix/webc_runner_rt_wcgi",
"wasmer-wasix/webc_runner_rt_emscripten",
"nuke-dir",
"webc"
]
compiler = [
"wasmer-compiler/translator",
"wasmer-compiler/compiler",
Expand Down Expand Up @@ -155,7 +145,6 @@ static-artifact-load = ["compiler",
"wasmer/static-artifact-load",
"wasmer-compiler/static-artifact-load",
]

experimental-io-devices = [
"wasmer-wasix-experimental-io-devices",
"wasi"
Expand All @@ -172,11 +161,9 @@ llvm = [
"wasmer-compiler-llvm",
"compiler",
]
debug = ["tracing", "wasmer-wasix/logging"]
disable-all-logging = ["wasmer-wasix/disable-all-logging", "log/release_max_level_off"]
headless = []
headless-minimal = ["headless", "disable-all-logging", "wasi"]
tracing = [ "dep:tracing", "tracing-subscriber" ]

# Optional
enable-serde = [
Expand All @@ -186,6 +173,14 @@ enable-serde = [
"wasmer-wasix/enable-serde",
]

# Deprecated. These feature flags no longer protect anything.
cache = []
cache-blake3-pure = []
debug = []
wasi = []
emscripten = []
webc_runner = []

[target.'cfg(target_os = "windows")'.dependencies]
colored = "2.0.0"

Expand Down
13 changes: 10 additions & 3 deletions lib/cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use crate::commands::CreateExe;
#[cfg(feature = "wast")]
use crate::commands::Wast;
use crate::commands::{
Add, Cache, Config, Init, Inspect, List, Login, Publish, Run, SelfUpdate, Validate, Whoami,
Add, Cache, Config, Init, Inspect, List, Login, Publish, Run, RunUnstable, SelfUpdate,
Validate, Whoami,
};
#[cfg(feature = "static-artifact-create")]
use crate::commands::{CreateObj, GenCHeader};
Expand Down Expand Up @@ -160,6 +161,9 @@ enum WasmerCLIOptions {

/// Add a WAPM package's bindings to your application.
Add(Add),

/// (unstable) Run a WebAssembly file or WEBC container.
RunUnstable(RunUnstable),
}

impl WasmerCLIOptions {
Expand Down Expand Up @@ -189,6 +193,7 @@ impl WasmerCLIOptions {
Self::Binfmt(binfmt) => binfmt.execute(),
Self::Whoami(whoami) => whoami.execute(),
Self::Add(install) => install.execute(),
Self::RunUnstable(run2) => run2.execute(),
}
}
}
Expand Down Expand Up @@ -241,8 +246,10 @@ fn wasmer_main_inner() -> Result<(), anyhow::Error> {
} else {
match command.unwrap_or(&"".to_string()).as_ref() {
"add" | "cache" | "compile" | "config" | "create-obj" | "create-exe" | "help"
| "gen-c-header" | "inspect" | "init" | "run" | "self-update" | "validate" | "wast"
| "binfmt" | "list" | "login" | "publish" => WasmerCLIOptions::parse(),
| "gen-c-header" | "inspect" | "init" | "run" | "run-unstable" | "self-update"
| "validate" | "wast" | "binfmt" | "list" | "login" | "publish" => {
WasmerCLIOptions::parse()
}
_ => {
WasmerCLIOptions::try_parse_from(args.iter()).unwrap_or_else(|e| {
match e.kind() {
Expand Down
3 changes: 2 additions & 1 deletion lib/cli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ mod list;
mod login;
mod publish;
mod run;
mod run_unstable;
mod self_update;
mod validate;
#[cfg(feature = "wast")]
Expand All @@ -34,7 +35,7 @@ pub use create_exe::*;
pub use wast::*;
pub use {
add::*, cache::*, config::*, init::*, inspect::*, list::*, login::*, publish::*, run::*,
self_update::*, validate::*, whoami::*,
run_unstable::RunUnstable, self_update::*, validate::*, whoami::*,
};
#[cfg(feature = "static-artifact-create")]
pub use {create_obj::*, gen_c_header::*};
7 changes: 2 additions & 5 deletions lib/cli/src/commands/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,8 @@ fn lookup_bindings_for_package(

match all_bindings.iter().find(|b| b.language == *language) {
Some(b) => {
#[cfg(feature = "debug")]
{
let Bindings { url, generator, .. } = b;
log::debug!("Found {pkg} bindings generated by {generator} at {url}");
}
let Bindings { url, generator, .. } = b;
log::debug!("Found {pkg} bindings generated by {generator} at {url}");

Ok(b.clone())
}
Expand Down
2 changes: 0 additions & 2 deletions lib/cli/src/commands/create_exe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2012,7 +2012,6 @@ mod http_fetch {
.context("Could not lookup wasmer repository on Github.")?;

if response.status_code() != StatusCode::new(200) {
#[cfg(feature = "debug")]
log::warn!(
"Warning: Github API replied with non-200 status code: {}. Response: {}",
response.status_code(),
Expand Down Expand Up @@ -2134,7 +2133,6 @@ mod http_fetch {
let download_path = download_tempdir.path().join(&filename);

let mut file = std::fs::File::create(&download_path)?;
#[cfg(feature = "debug")]
log::debug!(
"Downloading {} to {}",
browser_download_url,
Expand Down
Loading

0 comments on commit 898ae3c

Please sign in to comment.