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

PluggableRuntime Cleanup #3692

Merged
merged 3 commits into from
Mar 21, 2023
Merged
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
7 changes: 4 additions & 3 deletions lib/cli/src/commands/run/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ use virtual_fs::{DeviceFile, PassthruFileSystem, RootFileSystemBuilder};
use wasmer::{AsStoreMut, Instance, Module, RuntimeError, Value};
use wasmer_wasix::os::tty_sys::SysTty;
use wasmer_wasix::os::TtyBridge;
use wasmer_wasix::runtime::task_manager::tokio::TokioTaskManager;
use wasmer_wasix::types::__WASI_STDIN_FILENO;
use wasmer_wasix::{
default_fs_backing, get_wasi_versions, PluggableRuntimeImplementation, WasiEnv, WasiError,
WasiFunctionEnv, WasiVersion,
default_fs_backing, get_wasi_versions, PluggableRuntime, WasiEnv, WasiError, WasiFunctionEnv,
WasiVersion,
};

use clap::Parser;
Expand Down Expand Up @@ -129,7 +130,7 @@ impl Wasi {
.map(|(a, b)| (a.to_string(), b.to_string()))
.collect::<HashMap<_, _>>();

let mut rt = PluggableRuntimeImplementation::default();
let mut rt = PluggableRuntime::new(Arc::new(TokioTaskManager::shared()));

if self.networking {
rt.set_networking_implementation(virtual_net::host::LocalNetworking::default());
Expand Down
6 changes: 3 additions & 3 deletions lib/wasi/src/bin_factory/module_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,13 +283,13 @@ impl ModuleCache {
#[cfg(test)]
#[cfg(feature = "sys")]
mod tests {
use std::time::Duration;
use std::{sync::Arc, time::Duration};

use tracing_subscriber::{
filter, prelude::__tracing_subscriber_SubscriberExt, util::SubscriberInitExt, Layer,
};

use crate::PluggableRuntimeImplementation;
use crate::{runtime::task_manager::tokio::TokioTaskManager, PluggableRuntime};

use super::*;

Expand All @@ -306,7 +306,7 @@ mod tests {
let mut cache = ModuleCache::new(None, None, true);
cache.cache_time = std::time::Duration::from_millis(500);

let rt = PluggableRuntimeImplementation::default();
let rt = PluggableRuntime::new(Arc::new(TokioTaskManager::shared()));
let tasks = rt.task_manager();

let mut store = Vec::new();
Expand Down
2 changes: 1 addition & 1 deletion lib/wasi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub use crate::{
},
runtime::{
task_manager::{VirtualTaskManager, VirtualTaskManagerExt},
PluggableRuntimeImplementation, SpawnedMemory, WasiRuntime,
PluggableRuntime, SpawnedMemory, WasiRuntime,
},
wapm::parse_static_webc,
};
Expand Down
4 changes: 2 additions & 2 deletions lib/wasi/src/runners/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use std::sync::Arc;

use crate::{runners::WapmContainer, PluggableRuntimeImplementation, VirtualTaskManager};
use crate::{runners::WapmContainer, PluggableRuntime, VirtualTaskManager};
use crate::{WasiEnv, WasiEnvBuilder};
use anyhow::{Context, Error};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -91,7 +91,7 @@ impl crate::runners::Runner for WasiRunner {
let mut builder = prepare_webc_env(container, &atom_name, &self.args)?;

if let Some(tasks) = &self.tasks {
let rt = PluggableRuntimeImplementation::new(Arc::clone(tasks));
let rt = PluggableRuntime::new(Arc::clone(tasks));
builder.set_runtime(Arc::new(rt));
}

Expand Down
4 changes: 2 additions & 2 deletions lib/wasi/src/runners/wcgi/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::{
capabilities::Capabilities,
http::HttpClientCapabilityV1,
runners::wcgi::{Callbacks, MappedDirectory},
Pipe, PluggableRuntimeImplementation, VirtualTaskManager, WasiEnv,
Pipe, PluggableRuntime, VirtualTaskManager, WasiEnv,
};

/// The shared object that manages the instantiaion of WASI executables and
Expand All @@ -49,7 +49,7 @@ impl Handler {
self.dialect
.prepare_environment_variables(parts, &mut request_specific_env);

let rt = PluggableRuntimeImplementation::new(Arc::clone(&self.task_manager));
let rt = PluggableRuntime::new(Arc::clone(&self.task_manager));
let builder = builder
.envs(self.env.iter())
.envs(request_specific_env)
Expand Down
49 changes: 14 additions & 35 deletions lib/wasi/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ where
fn task_manager(&self) -> &Arc<dyn VirtualTaskManager>;

/// Get a [`wasmer::Engine`] for module compilation.
#[cfg(feature = "sys")]
fn engine(&self) -> Option<wasmer::Engine> {
None
}
Expand Down Expand Up @@ -87,33 +86,16 @@ impl TtyBridge for DefaultTty {

#[derive(Clone, Derivative)]
#[derivative(Debug)]
pub struct PluggableRuntimeImplementation {
pub struct PluggableRuntime {
pub rt: Arc<dyn VirtualTaskManager>,
pub networking: DynVirtualNetworking,
pub http_client: Option<DynHttpClient>,
#[cfg(feature = "sys")]
pub engine: Option<wasmer::Engine>,
#[derivative(Debug = "ignore")]
pub tty: Option<Arc<dyn TtyBridge + Send + Sync>>,
}

impl PluggableRuntimeImplementation {
pub fn set_networking_implementation<I>(&mut self, net: I)
where
I: VirtualNetworking + Sync,
{
self.networking = Arc::new(net)
}

#[cfg(feature = "sys")]
pub fn set_engine(&mut self, engine: Option<wasmer::Engine>) {
self.engine = engine;
}

pub fn set_tty(&mut self, tty: Arc<dyn TtyBridge + Send + Sync>) {
self.tty = Some(tty);
}

impl PluggableRuntime {
pub fn new(rt: Arc<dyn VirtualTaskManager>) -> Self {
// TODO: the cfg flags below should instead be handled by separate implementations.
cfg_if::cfg_if! {
Expand All @@ -137,30 +119,28 @@ impl PluggableRuntimeImplementation {
rt,
networking,
http_client,
#[cfg(feature = "sys")]
engine: None,
tty: None,
}
}
}

impl Default for PluggableRuntimeImplementation {
#[cfg(feature = "sys-thread")]
fn default() -> Self {
let rt = task_manager::tokio::TokioTaskManager::shared();
let mut s = Self::new(Arc::new(rt));
let engine = wasmer::Store::default().engine().clone();
s.engine = Some(engine);
s
pub fn set_networking_implementation<I>(&mut self, net: I)
where
I: VirtualNetworking + Sync,
{
self.networking = Arc::new(net)
}

#[cfg(not(feature = "sys-thread"))]
fn default() -> Self {
unimplemented!("Default WasiRuntime is not implemented on this target")
pub fn set_engine(&mut self, engine: Option<wasmer::Engine>) {
self.engine = engine;
}

pub fn set_tty(&mut self, tty: Arc<dyn TtyBridge + Send + Sync>) {
self.tty = Some(tty);
}
}

impl WasiRuntime for PluggableRuntimeImplementation {
impl WasiRuntime for PluggableRuntime {
fn networking(&self) -> &DynVirtualNetworking {
&self.networking
}
Expand All @@ -169,7 +149,6 @@ impl WasiRuntime for PluggableRuntimeImplementation {
self.http_client.as_ref()
}

#[cfg(feature = "sys")]
fn engine(&self) -> Option<wasmer::Engine> {
self.engine.clone()
}
Expand Down
16 changes: 12 additions & 4 deletions lib/wasi/src/state/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
parse_static_webc,
state::WasiState,
syscalls::types::{__WASI_STDERR_FILENO, __WASI_STDIN_FILENO, __WASI_STDOUT_FILENO},
PluggableRuntimeImplementation, WasiEnv, WasiFunctionEnv, WasiRuntime, WasiRuntimeError,
PluggableRuntime, WasiEnv, WasiFunctionEnv, WasiRuntime, WasiRuntimeError,
};

use super::env::WasiEnvInit;
Expand Down Expand Up @@ -722,9 +722,17 @@ impl WasiEnvBuilder {
}
}

let runtime = self
.runtime
.unwrap_or_else(|| Arc::new(PluggableRuntimeImplementation::default()));
let runtime = self.runtime.unwrap_or_else(|| {
#[cfg(feature = "sys-thread")]
{
Arc::new(PluggableRuntime::new(Arc::new(crate::runtime::task_manager::tokio::TokioTaskManager::shared())))
}

#[cfg(not(feature = "sys-thread"))]
{
panic!("this build does not support a default runtime - specify one with WasiEnvBuilder::runtime()");
}
});

let uses = self.uses;
let map_commands = self.map_commands;
Expand Down
16 changes: 2 additions & 14 deletions lib/wasi/tests/stdio.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::sync::Arc;

use virtual_fs::{AsyncReadExt, AsyncWriteExt};
use wasmer::{Module, Store};
use wasmer_wasix::{Pipe, PluggableRuntimeImplementation, WasiEnv};
use wasmer_wasix::{Pipe, WasiEnv};

mod sys {
#[tokio::test]
Expand Down Expand Up @@ -76,10 +74,7 @@ async fn test_stdout() {
// Create the `WasiEnv`.
let (stdout_tx, mut stdout_rx) = Pipe::channel();

let rt = PluggableRuntimeImplementation::default();

let builder = WasiEnv::builder("command-name")
.runtime(Arc::new(rt))
.args(&["Gordon"])
.stdout(Box::new(stdout_tx));

Expand Down Expand Up @@ -112,13 +107,10 @@ async fn test_env() {
builder.build()
});

let rt = PluggableRuntimeImplementation::default();

// Create the `WasiEnv`.
let (pipe_tx, mut pipe_rx) = Pipe::channel();

let builder = WasiEnv::builder("command-name")
.runtime(Arc::new(rt))
.args(&["Gordon"])
.env("DOG", "X")
.env("TEST", "VALUE")
Expand Down Expand Up @@ -157,11 +149,7 @@ async fn test_stdin() {
let buf = "Hello, stdin!\n".as_bytes().to_owned();
pipe_tx.write_all(&buf[..]).await.unwrap();

let rt = PluggableRuntimeImplementation::default();

let builder = WasiEnv::builder("command-name")
.runtime(Arc::new(rt))
.stdin(Box::new(pipe_rx));
let builder = WasiEnv::builder("command-name").stdin(Box::new(pipe_rx));

#[cfg(feature = "js")]
{
Expand Down
7 changes: 4 additions & 3 deletions tests/lib/wast/src/wasi_wast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ use virtual_fs::{
AsyncWriteExt, FileSystem, Pipe, ReadBuf, RootFileSystemBuilder,
};
use wasmer::{FunctionEnv, Imports, Module, Store};
use wasmer_wasix::runtime::task_manager::tokio::TokioTaskManager;
use wasmer_wasix::types::wasi::{Filesize, Timestamp};
use wasmer_wasix::{
generate_import_object_from_env, get_wasi_version, FsError, PluggableRuntimeImplementation,
VirtualFile, WasiEnv, WasiEnvBuilder, WasiRuntime, WasiVersion,
generate_import_object_from_env, get_wasi_version, FsError, PluggableRuntime, VirtualFile,
WasiEnv, WasiEnvBuilder, WasiRuntime, WasiVersion,
};
use wast::parser::{self, Parse, ParseBuffer, Parser};

Expand Down Expand Up @@ -100,7 +101,7 @@ impl<'a> WasiTest<'a> {
out
};

let mut rt = PluggableRuntimeImplementation::default();
let mut rt = PluggableRuntime::new(Arc::new(TokioTaskManager::shared()));
rt.set_engine(Some(store.engine().clone()));

let tasks = rt.task_manager().runtime().clone();
Expand Down