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

enable handle_run to accept io redirect paths #342

Merged
merged 1 commit into from
Mar 14, 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
21 changes: 12 additions & 9 deletions Cargo.lock

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

8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ flate2 = "1"
tar = "0.4"

[dev-dependencies]
tempdir = { workspace = true}
tempdir = { workspace = true }
tempfile = { workspace = true }
rand = { worspace = true }

[features]
default = ["keyvalue", "distributed-locking", "messaging", "runtime-configs", "sql", "http-server", "http-client"]
Expand All @@ -58,7 +60,6 @@ edition = "2021"
license = "MIT"
repository = "https://github.com/deislabs/spiderlightning"


[workspace.dependencies]
slight-core = { path = "./crates/core" }
slight-runtime = { path = "./crates/runtime" }
Expand All @@ -83,15 +84,16 @@ tokio = { version = "1", features = ["full"] }
tracing = { version = "0.1", features = ["log"] }
toml = "0.7"
tempdir = "0.3"
tempfile = "3.4"
log = { version = "0.4", default-features = false }
env_logger = "0.10"
as-any = "0.3"
serde = { version = "1", features = ["derive"] }
hyper = "0.14"
url = "2.2"
rand = "0.8"
semver = "1"
clap = { version = "4", features = ["derive"] }
rand = "0.8"

[workspace]
members = [
Expand Down
4 changes: 2 additions & 2 deletions crates/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ repository = { workspace = true }

[lib]
doctest = false
test = false

[dependencies]
wasmtime = { workspace = true }
Expand All @@ -25,4 +24,5 @@ async-trait = { workspace = true }

[dev-dependencies]
slight-keyvalue = { workspace = true }
tokio = { workspace = true }
tempfile = { workspace = true }
tokio = { workspace = true }
152 changes: 119 additions & 33 deletions crates/runtime/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
mod ctx;
pub mod resource;

use std::path::Path;
use std::{
fs::{File, OpenOptions},
path::{Path, PathBuf},
};

use anyhow::Result;
use async_trait::async_trait;
use ctx::SlightCtxBuilder;
use resource::{get_host_state, HttpData};
use slight_common::{CapabilityBuilder, WasmtimeBuildable, WasmtimeLinkable};
use wasi_cap_std_sync::{ambient_authority, Dir, WasiCtxBuilder};
use wasi_common::pipe::{ReadPipe, WritePipe};
use wasi_common::WasiCtx;
use wasmtime::{Config, Engine, Instance, Linker, Module, Store};

Expand Down Expand Up @@ -45,6 +49,14 @@ impl slight_common::Ctx for RuntimeContext {
}
}

/// Input and output redirects to be used for the running module
#[derive(Clone, Default)]
pub struct IORedirects {
pub stdout_path: Option<PathBuf>,
pub stderr_path: Option<PathBuf>,
pub stdin_path: Option<PathBuf>,
}

/// A wasmtime-based runtime builder.
///
/// It knows how to build a `Store` and `Instance` for a wasm module, given
Expand All @@ -55,6 +67,7 @@ pub struct Builder {
engine: Engine,
module: Module,
state_builder: SlightCtxBuilder,
io_redirects: IORedirects,
}

impl Builder {
Expand All @@ -70,9 +83,16 @@ impl Builder {
engine,
module,
state_builder: SlightCtxBuilder::default(),
io_redirects: IORedirects::default(),
})
}

/// Set the I/O redirects for the module
pub fn set_io(mut self, io_redirects: IORedirects) -> Self {
Mossaka marked this conversation as resolved.
Show resolved Hide resolved
self.io_redirects = io_redirects;
self
}

/// Link wasi to the wasmtime::Linker
pub fn link_wasi(&mut self) -> Result<&mut Self> {
wasmtime_wasi::add_to_linker(&mut self.linker, |cx: &mut Ctx| cx.wasi.as_mut().unwrap())?;
Expand All @@ -97,14 +117,32 @@ impl Builder {
}
}

fn maybe_open_stdio(pipe_path: &Path) -> Option<File> {
devigned marked this conversation as resolved.
Show resolved Hide resolved
if pipe_path.as_os_str().is_empty() {
None
} else {
Some(
OpenOptions::new()
.read(true)
.write(true)
.open(pipe_path)
.unwrap_or_else(|_| {
panic!(
"could not open pipe: {path}",
path = pipe_path.to_str().unwrap()
)
}),
)
}
}

#[async_trait]
impl WasmtimeBuildable for Builder {
type Ctx = Ctx;

/// Instantiate the guest module.
async fn build(self) -> (Store<Self::Ctx>, Instance) {
let wasi = default_wasi().unwrap();

let wasi = build_wasi_context(self.io_redirects).unwrap();
let ctx = RuntimeContext {
wasi: Some(wasi),
slight: self.state_builder.build(),
Expand All @@ -121,6 +159,41 @@ impl WasmtimeBuildable for Builder {
}
}

fn build_wasi_context(io_redirects: IORedirects) -> Result<WasiCtx> {
let mut ctx: WasiCtxBuilder = WasiCtxBuilder::new();
ctx = add_io_redirects_to_wasi_context(ctx, io_redirects)?;
Ok(ctx
.inherit_args()?
.preopened_dir(Dir::open_ambient_dir(".", ambient_authority())?, ".")?
.build())
}

/// add_io_redirects_to_wasi_context inherits existing stdio and overrides stdio as available.
fn add_io_redirects_to_wasi_context(
mut ctx: WasiCtxBuilder,
io_redirects: IORedirects,
) -> Result<WasiCtxBuilder> {
ctx = ctx.inherit_stdio();
if let Some(stdout_path) = io_redirects.stdout_path {
if let Some(stdout_file) = maybe_open_stdio(&stdout_path) {
ctx = ctx.stdout(Box::new(WritePipe::new(stdout_file)));
}
}

if let Some(stderr_path) = io_redirects.stderr_path {
if let Some(stderr_file) = maybe_open_stdio(&stderr_path) {
ctx = ctx.stderr(Box::new(WritePipe::new(stderr_file)));
}
}

if let Some(stdin_path) = io_redirects.stdin_path {
if let Some(stdin_file) = maybe_open_stdio(&stdin_path) {
ctx = ctx.stdin(Box::new(ReadPipe::new(stdin_file)));
}
}
Ok(ctx)
}

// TODO (Joe): expose the wasmtime config as a capability?
pub fn default_config() -> Result<Config> {
let mut config = Config::new();
Expand All @@ -130,38 +203,51 @@ pub fn default_config() -> Result<Config> {
Ok(config)
}

// TODO (Joe): expose the wasmtime wasi context as a capability?
pub fn default_wasi() -> Result<WasiCtx> {
let ctx: WasiCtxBuilder = WasiCtxBuilder::new()
.preopened_dir(Dir::open_ambient_dir(".", ambient_authority())?, ".")?
.inherit_stdio()
.inherit_args()?;
Ok(ctx.build())
}

#[cfg(test)]
mod unittest {
use std::collections::HashMap;

use slight_common::WasmtimeBuildable;
use slight_keyvalue::Keyvalue;

use crate::Builder;

#[tokio::test]
async fn test_builder_build() -> anyhow::Result<()> {
let module = "./test/keyvalue-test.wasm";
assert!(std::path::Path::new(module).exists());
let mut builder = Builder::from_module(module)?;
let keyvalue =
slight_keyvalue::Keyvalue::new("keyvalue.filesystem".to_string(), HashMap::default());

builder
.link_wasi()?
.link_capability::<Keyvalue>()?
.add_to_builder("keyvalue".to_string(), keyvalue);

let (_, _) = builder.build().await;
// use std::collections::HashMap;
use std::fs::File;
use std::path::PathBuf;

// use slight_common::WasmtimeBuildable;
// use slight_keyvalue::Keyvalue;
use tempfile::tempdir;

// use crate::Builder;

// TODO(DJ): re-enable this test --currently broken
// #[tokio::test]
// async fn test_builder_build() -> anyhow::Result<()> {
// let module = "./test/keyvalue-test.wasm";
// assert!(std::path::Path::new(module).exists());
// let mut builder = Builder::from_module(module)?;
// let keyvalue =
// slight_keyvalue::Keyvalue::new("keyvalue.filesystem".to_string(), HashMap::default());
//
// builder
// .link_wasi()?
// .link_capability::<Keyvalue>()?
// .add_to_builder("keyvalue".to_string(), keyvalue);
//
// let (_, _) = builder.build().await;
// Ok(())
// }

#[test]
fn test_maybe_open_stdio_with_existing_file() -> anyhow::Result<()> {
let tmp_dir = tempdir()?;
let existing_file_path = tmp_dir.path().join("testpath");
let empty_file_path = PathBuf::new();
let _ = File::create(&existing_file_path)?;
assert!(crate::maybe_open_stdio(&existing_file_path).is_some());
assert!(crate::maybe_open_stdio(&empty_file_path).is_none());
Ok(())
}

#[test]
#[should_panic]
fn test_maybe_open_stdio_with_missing_file() {
let missing_file_path = PathBuf::from("missing");
let _ = crate::maybe_open_stdio(&missing_file_path);
}
}
Loading