Skip to content

Commit

Permalink
wasmi_wasi: Update wasi-common and wiggle dependencies (#1140)
Browse files Browse the repository at this point in the history
* update wasi-common and wiggle dependencies

This makes it possible to remove the old and outdated `wasi-cap-std-sync` dependency altogether.

Unfortunately with this update the `wasi-common` crate still pulls the huge `wasmtime` dependency into Wasmi and I am a bit confused why this happens.

Issue: bytecodealliance/wasmtime#9009

* update wasi-common and wiggle dependencies
  • Loading branch information
Robbepop authored Aug 21, 2024
1 parent 79996af commit e0106b4
Show file tree
Hide file tree
Showing 8 changed files with 307 additions and 389 deletions.
638 changes: 282 additions & 356 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ exclude.workspace = true
anyhow = "1"
clap = { version = "4", features = ["derive"] }
wasmi = { workspace = true }
wasmi_wasi = { workspace = true, features = ["sync"] }
wasmi_wasi = { workspace = true }
wat = "1"

[dev-dependencies]
Expand Down
10 changes: 5 additions & 5 deletions crates/cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,20 +210,20 @@ impl Args {
pub fn wasi_context(&self) -> Result<WasiCtx, Error> {
let mut wasi_builder = WasiCtxBuilder::new();
for KeyValue { key, value } in &self.envs {
wasi_builder = wasi_builder.env(key, value)?;
wasi_builder.env(key, value)?;
}
wasi_builder = wasi_builder.args(&self.argv())?;
wasi_builder.args(&self.argv())?;
// Add pre-opened TCP sockets.
//
// Note that `num_fd` starts at 3 because the inherited `stdin`, `stdout` and `stderr`
// are already mapped to `0, 1, 2` respectively.
wasi_builder = wasi_builder.inherit_stdio();
wasi_builder.inherit_stdio();
for (socket, num_fd) in self.preopen_sockets()?.into_iter().zip(3..) {
wasi_builder = wasi_builder.preopened_socket(num_fd, socket)?;
wasi_builder.preopened_socket(num_fd, socket)?;
}
// Add pre-opened directories.
for (dir_name, dir) in self.preopen_dirs()? {
wasi_builder = wasi_builder.preopened_dir(dir, dir_name)?;
wasi_builder.preopened_dir(dir, dir_name)?;
}
Ok(wasi_builder.build())
}
Expand Down
9 changes: 2 additions & 7 deletions crates/wasi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,9 @@ categories.workspace = true
exclude.workspace = true

[dependencies]
wasi-common = "2.0"
wasi-cap-std-sync = "2.0"
wiggle = { version = "2.0", default-features = false, features = ["wiggle_metadata"] }
wasi-common = { version = "24.0.0", default-features = false, features = ["sync"]}
wiggle = { version = "24.0.0", default-features = false }
wasmi = { workspace = true, features = ["std"]}

[dev-dependencies]
wat = "1.0.50"

[features]
default = ["sync"]
sync = []
8 changes: 3 additions & 5 deletions crates/wasi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
//!
//! Use [`add_to_linker`] to add all supported WASI definitions to the Wasmi linker.
mod guest_memory;

#[cfg(feature = "sync")]
pub mod sync;

pub use self::guest_memory::WasmiGuestMemory;
pub use wasi_common::{Error, WasiCtx, WasiDir, WasiFile};
pub use wiggle::GuestMemory as WasmiGuestMemory;

/// Sync mode is the "default" of this crate, so we also export it at the top level.
#[cfg(feature = "sync")]
pub use sync::*;

pub use wasi_common;
2 changes: 1 addition & 1 deletion crates/wasi/src/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
pub mod snapshots;

pub use wasi_cap_std_sync::*;
pub use wasi_common::sync::*;

#[doc(inline)]
pub use self::snapshots::preview_1::{
Expand Down
25 changes: 12 additions & 13 deletions crates/wasi/src/sync/snapshots/preview_1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ use std::{
pin::Pin,
task::{Context, RawWaker, RawWakerVTable, Waker},
};
use wasi_common::{
snapshots::preview_1::wasi_snapshot_preview1::{UserErrorConversion, WasiSnapshotPreview1},
Error,
};
use wasi_common::{snapshots::preview_1::wasi_snapshot_preview1::WasiSnapshotPreview1, Error};
use wasmi::{state::Constructing, Caller, Extern, Linker, LinkerBuilder};

// Creates a dummy `RawWaker`. We can only create Wakers from `RawWaker`s
Expand Down Expand Up @@ -45,7 +42,7 @@ pub trait AddWasi<T> {
wasi_ctx: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
) -> Result<(), Error>
where
U: WasiSnapshotPreview1 + UserErrorConversion;
U: WasiSnapshotPreview1;
}

/// Adds the entire WASI API to the Wasmi [`Linker`].
Expand All @@ -66,7 +63,7 @@ pub fn add_wasi_snapshot_preview1_to_linker<T, U>(
wasi_ctx: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
) -> Result<(), Error>
where
U: WasiSnapshotPreview1 + UserErrorConversion,
U: WasiSnapshotPreview1,
{
<Linker<T> as AddWasi<T>>::add_wasi(linker, wasi_ctx)
}
Expand All @@ -79,7 +76,7 @@ pub fn add_wasi_snapshot_preview1_to_linker_builder<T, U>(
wasi_ctx: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
) -> Result<(), Error>
where
U: WasiSnapshotPreview1 + UserErrorConversion,
U: WasiSnapshotPreview1,
{
<LinkerBuilder<Constructing, T> as AddWasi<T>>::add_wasi(linker, wasi_ctx)
}
Expand All @@ -100,7 +97,7 @@ macro_rules! add_funcs_to_linker {
wasi_ctx: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
) -> Result<(), Error>
where
U: WasiSnapshotPreview1 + UserErrorConversion,
U: WasiSnapshotPreview1,
{
$(
// $(#[$docs])* // TODO: find place for docs
Expand All @@ -115,16 +112,18 @@ macro_rules! add_funcs_to_linker {
};
let(memory, ctx) = memory.data_and_store_mut(&mut caller);
let ctx = wasi_ctx(ctx);
let memory = WasmiGuestMemory::new(memory);
match wasi_common::snapshots::preview_1::wasi_snapshot_preview1::$fname(ctx, &memory, $($arg,)*).await {
let mut memory = WasmiGuestMemory::Unshared(memory);
match wasi_common::snapshots::preview_1::wasi_snapshot_preview1::$fname(ctx, &mut memory, $($arg,)*).await {
Ok(r) => Ok(<$ret>::from(r)),
Err(wiggle::Trap::String(err)) => Err(wasmi::Error::new(err)),
Err(wiggle::Trap::I32Exit(i)) => Err(wasmi::Error::i32_exit(i)),
Err(e) => match e.downcast::<wasi_common::I32Exit>() {
Ok(wasi_common::I32Exit(status)) => Err(wasmi::Error::i32_exit(status)),
Err(e) => Err(wasmi::Error::new(e.to_string())),
}
}
};
run_in_dummy_executor(result)?
}
)?;
).map_err(wiggle::anyhow::Error::from).map_err(wasi_common::Error::trap)?;
)*
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion crates/wasi/tests/wasi_wat.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use wasi_cap_std_sync::WasiCtxBuilder;
use wasi_common::sync::WasiCtxBuilder;
use wasmi::{Config, Engine, Extern, Instance, Linker, Module, Store};
use wasmi_wasi::{add_to_linker, WasiCtx};

Expand Down

0 comments on commit e0106b4

Please sign in to comment.