Skip to content

Commit

Permalink
Add an --adapt flag to automatically adapt core wasm before running
Browse files Browse the repository at this point in the history
  • Loading branch information
elliottt committed May 30, 2024
1 parent d6ee872 commit 79e86af
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ async fn create_execution_context(
args.wasi_modules(),
guest_profile_path,
args.unknown_import_behavior(),
args.adapt(),
)?
.with_log_stderr(args.log_stderr())
.with_log_stdout(args.log_stdout());
Expand Down
8 changes: 8 additions & 0 deletions cli/src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ pub struct SharedArgs {
/// effect if you set RUST_LOG to a value before starting Viceroy
#[arg(short = 'v', action = clap::ArgAction::Count)]
verbosity: u8,
/// Whether or not to automatically adapt core-wasm modules to
/// components before running them.
#[arg(long = "adapt")]
adapt: bool,
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -213,6 +217,10 @@ impl SharedArgs {
pub fn verbosity(&self) -> u8 {
self.verbosity
}

pub fn adapt(&self) -> bool {
self.adapt
}
}

#[derive(Args, Debug, Clone)]
Expand Down
2 changes: 2 additions & 0 deletions cli/tests/integration/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,14 @@ impl Test {
self.backends.start_servers().await;
}

let adapt_core_wasm = false;
let ctx = ExecuteCtx::new(
&self.module_path,
ProfilingStrategy::None,
HashSet::new(),
None,
self.unknown_import_behavior,
adapt_core_wasm,
)?
.with_backends(self.backends.backend_configs().await)
.with_dictionaries(self.dictionaries.clone())
Expand Down
2 changes: 2 additions & 0 deletions cli/tests/trap-test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ pub type TestResult = Result<(), Error>;
#[tokio::test(flavor = "multi_thread")]
async fn fatal_error_traps() -> TestResult {
let module_path = format!("{RUST_FIXTURE_PATH}/response.wasm");
let adapt_core_wasm = false;
let ctx = ExecuteCtx::new(
module_path,
ProfilingStrategy::None,
HashSet::new(),
None,
viceroy_lib::config::UnknownImportBehavior::LinkError,
adapt_core_wasm,
)?;
let req = Request::get("http://127.0.0.1:7676/").body(Body::from(""))?;
let resp = ctx
Expand Down
2 changes: 2 additions & 0 deletions crates/adapter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use core::slice;
use poll::Pollable;
use wasi::*;

// test

#[macro_use]
mod macros;

Expand Down
21 changes: 20 additions & 1 deletion lib/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::config::UnknownImportBehavior;

use {
crate::{
adapt,
body::Body,
component as compute,
config::{Backends, DeviceDetection, Dictionaries, ExperimentalModule, Geolocation},
Expand Down Expand Up @@ -106,6 +107,7 @@ impl ExecuteCtx {
wasi_modules: HashSet<ExperimentalModule>,
guest_profile_path: Option<PathBuf>,
unknown_import_behavior: UnknownImportBehavior,
adapt_components: bool,
) -> Result<Self, Error> {
let input = fs::read(&module_path)?;

Expand All @@ -126,6 +128,22 @@ impl ExecuteCtx {
})
);

// When the input wasn't a component, but we're automatically adapting,
// apply the component adapter.
let (is_component, input) = if !is_component && adapt_components {
// It's not possible to adapt a component from WAT, we can't continue at this point.
if is_wat {
return Err(Error::Other(anyhow::anyhow!(
"Wasm components may only be adapted from binary wasm components, not wat"
)));
}

let input = adapt::adapt_bytes(&input)?;
(true, input)
} else {
(is_component, input)
};

let config = &configure_wasmtime(is_component, profiling_strategy);
let engine = Engine::new(config)?;
let instance_pre = if is_component {
Expand Down Expand Up @@ -323,7 +341,8 @@ impl ExecuteCtx {
/// # use viceroy_lib::{Error, ExecuteCtx, ProfilingStrategy, ViceroyService};
/// # async fn f() -> Result<(), Error> {
/// # let req = Request::new(Body::from(""));
/// let ctx = ExecuteCtx::new("path/to/a/file.wasm", ProfilingStrategy::None, HashSet::new(), None, Default::default())?;
/// let adapt_core_wasm = false;
/// let ctx = ExecuteCtx::new("path/to/a/file.wasm", ProfilingStrategy::None, HashSet::new(), None, Default::default(), adapt_core_wasm)?;
/// let resp = ctx.handle_request(req, "127.0.0.1".parse().unwrap()).await?;
/// # Ok(())
/// # }
Expand Down
3 changes: 2 additions & 1 deletion lib/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ impl ViceroyService {
/// # use std::collections::HashSet;
/// use viceroy_lib::{Error, ExecuteCtx, ProfilingStrategy, ViceroyService};
/// # fn f() -> Result<(), Error> {
/// let ctx = ExecuteCtx::new("path/to/a/file.wasm", ProfilingStrategy::None, HashSet::new(), None, Default::default())?;
/// let adapt_core_wasm = false;
/// let ctx = ExecuteCtx::new("path/to/a/file.wasm", ProfilingStrategy::None, HashSet::new(), None, Default::default(), adapt_core_wasm)?;
/// let svc = ViceroyService::new(ctx);
/// # Ok(())
/// # }
Expand Down

0 comments on commit 79e86af

Please sign in to comment.