Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Move Runner out of WASIX #4306

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ wasi-test-generator = { version = "=4.2.3", path = "tests/wasi-wast", optional =
wasmer-cache = { version = "=4.2.3", path = "lib/cache", optional = true }
wasmer-types = { version = "=4.2.3", path = "lib/types" }
wasmer-middlewares = { version = "=4.2.3", path = "lib/middlewares", optional = true }
wasmer-runner = { version = "=0.1.0", path = "lib/runner", optional = true }
cfg-if = "1.0"
tokio = { version = "1", features = [
"rt",
Expand Down
2 changes: 1 addition & 1 deletion lib/cli/src/commands/run/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ impl<R: wasmer_wasix::Runtime + Send + Sync> wasmer_wasix::Runtime for Monitorin
self.runtime.http_client()
}

fn tty(&self) -> Option<&(dyn wasmer_wasix::os::TtyBridge + Send + Sync)> {
fn tty(&self) -> Option<&(dyn wasmer_wasix::runtime::TtyBridge + Send + Sync)> {
self.runtime.tty()
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/cli/src/commands/run/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use wasmer_wasix::{
capabilities::Capabilities,
default_fs_backing, get_wasi_versions,
http::HttpClient,
os::{tty_sys::SysTty, TtyBridge},
rewind_ext,
runners::{MappedCommand, MappedDirectory},
runtime::{
Expand All @@ -32,6 +31,7 @@ use wasmer_wasix::{
tokio::{RuntimeOrHandle, TokioTaskManager},
VirtualTaskManagerExt,
},
SysTty, TtyBridge,
},
types::__WASI_STDIN_FILENO,
wasmer_wasix_types::wasi::Errno,
Expand Down
54 changes: 54 additions & 0 deletions lib/runner/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
[package]
name = "wasmer-runner"
version = "0.1.0"
description = "Wasmer Runner allows plugging in different implementations."
categories = ["wasm", "os"]
keywords = ["wasm", "webassembly", "wasi", "sandbox", "ABI"]
readme = "README.md"
authors.workspace = true
edition.workspace = true
homepage.workspace = true
license.workspace = true
repository.workspace = true
rust-version.workspace = true

[dependencies]
cfg-if = "1.0"
thiserror = "1"
tracing = { version = "0.1.37" }
wasmer-wasix-types = { path = "../wasi-types", version = "0.16.0", features = [ "enable-serde" ] }
wasmer-types = { path = "../types", version = "=4.2.3", default-features = false }
wasmer = { path = "../api", version = "=4.2.3", default-features = true }
webc.workspace = true
virtual-mio = { path = "../virtual-io", version = "0.3.0", default-features = false }
virtual-fs = { path = "../virtual-fs", version = "0.9.0", default-features = false, features = ["webc-fs"] }
virtual-net = { path = "../virtual-net", version = "0.6.1", default-features = false }
serde = { version = "1.0", default-features = false, features = ["derive"] }
bincode = { version = "1.3" }
derivative = { version = "^2" }
bytes = "1"
serde_cbor = { version = "0.11.2" }
anyhow = { version = "1.0.66" }
sha2 = { version = "0.10" }
tokio = { version = "1", features = ["sync", "macros", "time", "rt", "rt-multi-thread"], default_features = false }
futures = { version = "0.3" }
async-trait = { version = "^0.1" }
serde_json = { version = "^1" }
weezl = { version = "^0.1" }
hex = { version = "^0.4" }
term_size = { version = "0.3" }
http = "0.2.8"
wai-bindgen-wasmer = { path = "../wai-bindgen-wasmer", version = "0.16.0", features = ["tracing"] }
once_cell = "1.17.0"
semver = "1.0.17"
dashmap = "5.4.0"
tempfile = "3.6.0"
url = "2.3.1"
petgraph = "0.6.3"
wasm-bindgen = { version = "0.2.87", optional = true }

[target.'cfg(unix)'.dependencies]
libc = { version = "^0.2", default-features = false }

[target.'cfg(all(unix, not(target_os="ios")))'.dependencies]
termios = { version = "0.3" }
7 changes: 7 additions & 0 deletions lib/runner/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# `wasmer-runner` [![Build Status](https://github.com/wasmerio/wasmer/workflows/build/badge.svg?style=flat-square)](https://github.com/wasmerio/wasmer/actions?query=workflow%3Abuild) [![Join Wasmer Slack](https://img.shields.io/static/v1?label=Slack&message=join%20chat&color=brighgreen&style=flat-square)](https://slack.wasmer.io) [![MIT License](https://img.shields.io/github/license/wasmerio/wasmer.svg?style=flat-square)](https://github.com/wasmerio/wasmer/blob/master/LICENSE)

This library provides the proper abstractions to manage and run Wasmer packages anywhere.

There are different libraries that use it to implement their own runners:
* `wasmer-emscripten`
* `wasmer-wasix`
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ use virtual_fs::FileSystem;
use webc::{compat::SharedBytes, Container};

use crate::{
runtime::{
module_cache::ModuleHash,
resolver::{PackageId, PackageInfo, PackageSpecifier, ResolveError},
},
module_cache::ModuleHash,
resolver::{PackageId, PackageInfo, PackageSpecifier, ResolveError},
Runtime,
};

Expand Down Expand Up @@ -89,7 +87,7 @@ impl BinaryPackage {
version: root.version.clone(),
};

let resolution = crate::runtime::resolver::resolve(&root_id, &root, &*source).await?;
let resolution = crate::resolver::resolve(&root_id, &root, &*source).await?;
let pkg = rt
.package_loader()
.load_package_tree(container, &resolution)
Expand Down Expand Up @@ -117,7 +115,7 @@ impl BinaryPackage {
let root = runtime.package_loader().load(&root_summary).await?;
let id = root_summary.package_id();

let resolution = crate::runtime::resolver::resolve(&id, &root_summary.pkg, &source)
let resolution = crate::resolver::resolve(&id, &root_summary.pkg, &source)
.await
.context("Dependency resolution failed")?;
let pkg = runtime
Expand Down Expand Up @@ -158,16 +156,15 @@ mod tests {
use virtual_fs::AsyncReadExt;

use crate::{
runtime::{package_loader::BuiltinPackageLoader, task_manager::VirtualTaskManager},
PluggableRuntime,
package_loader::BuiltinPackageLoader, task_manager::VirtualTaskManager, PluggableRuntime,
};

use super::*;

fn task_manager() -> Arc<dyn VirtualTaskManager + Send + Sync> {
cfg_if::cfg_if! {
if #[cfg(feature = "sys-thread")] {
Arc::new(crate::runtime::task_manager::tokio::TokioTaskManager::new(tokio::runtime::Handle::current()))
Arc::new(crate::task_manager::tokio::TokioTaskManager::new(tokio::runtime::Handle::current()))
} else {
unimplemented!("Unable to get the task manager")
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
bindings::wasix_http_client_v1 as sys,
capabilities::Capabilities,
http::{DynHttpClient, HttpClientCapabilityV1},
runtime::task_manager::InlineWaker,
task_manager::InlineWaker,
Runtime, WasiEnv,
};

Expand Down
File renamed without changes.
File renamed without changes.
56 changes: 17 additions & 39 deletions lib/wasix/src/runtime/mod.rs → lib/runner/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
pub mod binary_package;
pub mod bindings;
pub mod capabilities;
pub mod http;
pub mod module_cache;
pub mod package_loader;
pub mod resolver;
pub mod runner;
pub mod snapshot;
pub mod task_manager;
pub mod tty_sys;

pub use self::binary_package::{BinaryPackage, BinaryPackageCommand};
pub use self::runner::Runner;
pub use self::task_manager::{SpawnMemoryType, VirtualTaskManager};
use self::{
module_cache::{CacheError, ModuleHash},
task_manager::InlineWaker,
};
pub use self::tty_sys::{SysTty, TtyBridge, TtyState};

use std::{
fmt,
sync::{Arc, Mutex},
};
use self::module_cache::{CacheError, ModuleHash};
use self::task_manager::InlineWaker;

use std::{fmt, sync::Arc};

use derivative::Derivative;
use futures::future::BoxFuture;
Expand All @@ -21,13 +27,9 @@ use wasmer::Module;

use crate::{
http::{DynHttpClient, HttpClient},
os::TtyBridge,
runtime::{
module_cache::{ModuleCache, ThreadLocalCache},
package_loader::{PackageLoader, UnsupportedPackageLoader},
resolver::{MultiSource, Source, WapmSource},
},
WasiTtyState,
module_cache::{ModuleCache, ThreadLocalCache},
package_loader::{PackageLoader, UnsupportedPackageLoader},
resolver::{MultiSource, Source, WapmSource},
};

/// Runtime components used when running WebAssembly programs.
Expand Down Expand Up @@ -144,30 +146,6 @@ pub async fn load_module(
Ok(module)
}

#[derive(Debug, Default)]
pub struct DefaultTty {
state: Mutex<WasiTtyState>,
}

impl TtyBridge for DefaultTty {
fn reset(&self) {
let mut state = self.state.lock().unwrap();
state.echo = false;
state.line_buffered = false;
state.line_feeds = false
}

fn tty_get(&self) -> WasiTtyState {
let state = self.state.lock().unwrap();
state.clone()
}

fn tty_set(&self, tty_state: WasiTtyState) {
let mut state = self.state.lock().unwrap();
*state = tty_state;
}
}

#[derive(Clone, Derivative)]
#[derivative(Debug)]
pub struct PluggableRuntime {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use wasmer::{Engine, Module};

use crate::runtime::module_cache::{CacheError, ModuleCache, ModuleHash};
use crate::module_cache::{CacheError, ModuleCache, ModuleHash};

/// [`FallbackCache`] is a combinator for the [`ModuleCache`] trait that enables
/// the chaining of two caching strategies together, typically via
Expand Down Expand Up @@ -104,7 +104,7 @@ mod tests {
use std::sync::atomic::{AtomicUsize, Ordering};

use super::*;
use crate::runtime::module_cache::SharedCache;
use crate::module_cache::SharedCache;

const ADD_WAT: &[u8] = br#"(
module
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
use tempfile::NamedTempFile;
use wasmer::{Engine, Module};

use crate::runtime::module_cache::{CacheError, ModuleCache, ModuleHash};
use crate::module_cache::{CacheError, ModuleCache, ModuleHash};

/// A cache that saves modules to a folder on the host filesystem using
/// [`Module::serialize()`].
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use dashmap::DashMap;
use wasmer::{Engine, Module};

use crate::runtime::module_cache::{CacheError, ModuleCache, ModuleHash};
use crate::module_cache::{CacheError, ModuleCache, ModuleHash};

/// A [`ModuleCache`] based on a <code>[DashMap]<[ModuleHash], [Module]></code>.
#[derive(Debug, Default, Clone)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{cell::RefCell, collections::HashMap};

use wasmer::{Engine, Module};

use crate::runtime::module_cache::{CacheError, ModuleCache, ModuleHash};
use crate::module_cache::{CacheError, ModuleCache, ModuleHash};

std::thread_local! {
static CACHED_MODULES: RefCell<HashMap<(ModuleHash, String), Module>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
use sha2::{Digest, Sha256};
use wasmer::{Engine, Module};

use crate::runtime::module_cache::FallbackCache;
use crate::module_cache::FallbackCache;

/// A cache for compiled WebAssembly modules.
///
Expand Down Expand Up @@ -52,7 +52,7 @@ pub trait ModuleCache: Debug {
/// be significantly slower than the previous one.
///
/// ```rust
/// use wasmer_wasix::runtime::module_cache::{
/// use wasmer_runner::module_cache::{
/// ModuleCache, ThreadLocalCache, FileSystemCache, SharedCache,
/// };
///
Expand Down
Loading
Loading