Skip to content

Commit

Permalink
Replace direct wat usage with Wasmi's new Module::new Wat support (
Browse files Browse the repository at this point in the history
…#1327)

* add wat dependency to wasmi crate

* add wat::Error to Wasmi's Error type

* add optional wat parsing to Module::new method

* remove wat dependency from wasmi_cli

* remove wat dependency from wasmi_wasi tests

* remove unnecessary wat2wasm usage in benches

* remove wat2wasm call from load_instance_from_wat

* remove wat2wasm call in EnforcedLimits tests

* remove wat2wasm usage in many_inout test

* remove wat2wasm usage in e2e tests

* apply clippy suggestion

* make Module::new take bytes as AsRef<[u8]>

* remove wat2wasm from instance tests

* re-active i{i32,64}_eqz translation tests

* apply rustfmt and clippy suggestions

* make use of AsRef<[u8]> parameter in Module::new

* replace more direct wat crate usage

* remove wat usage in TranslationTest::from_wat

* remove TranslationTest::from_wat

This is now just the same as TranslationTest::new, so use that instead.

* update Cargo.lock
  • Loading branch information
Robbepop authored Dec 1, 2024
1 parent c889d43 commit df21a1a
Show file tree
Hide file tree
Showing 66 changed files with 599 additions and 751 deletions.
57 changes: 28 additions & 29 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ anyhow = "1"
clap = { version = "4", features = ["derive"] }
wasmi = { workspace = true }
wasmi_wasi = { workspace = true }
wat = { version = "1", default-features = false }

[dev-dependencies]
assert_cmd = "2.0.7"
Expand Down
8 changes: 4 additions & 4 deletions crates/cli/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::utils;
use anyhow::{anyhow, Error};
use std::path::Path;
use std::{fs, path::Path};
use wasmi::{CompilationMode, Config, ExternType, Func, FuncType, Instance, Module, Store};
use wasmi_wasi::WasiCtx;

Expand Down Expand Up @@ -35,8 +34,9 @@ impl Context {
}
config.compilation_mode(compilation_mode);
let engine = wasmi::Engine::new(&config);
let wasm_bytes = utils::read_wasm_or_wat(wasm_file)?;
let module = wasmi::Module::new(&engine, &wasm_bytes[..]).map_err(|error| {
let wasm =
fs::read(wasm_file).map_err(|_| anyhow!("failed to read Wasm file {wasm_file:?}"))?;
let module = wasmi::Module::new(&engine, wasm).map_err(|error| {
anyhow!("failed to parse and validate Wasm module {wasm_file:?}: {error}")
})?;
let mut store = wasmi::Store::new(&engine, wasi_ctx);
Expand Down
24 changes: 0 additions & 24 deletions crates/cli/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,11 @@
use crate::display::DisplayValueType;
use anyhow::{anyhow, bail, Error};
use std::{ffi::OsStr, fs, path::Path};
use wasmi::{
core::{ValType, F32, F64},
FuncType,
Val,
};

/// Converts the given `.wat` into `.wasm`.
fn wat2wasm(wat: &str) -> Result<Vec<u8>, wat::Error> {
wat::parse_str(wat)
}

/// Returns the contents of the given `.wasm` or `.wat` file.
///
/// # Errors
///
/// If the Wasm file `wasm_file` does not exist.
/// If the Wasm file `wasm_file` is not a valid `.wasm` or `.wat` format.
pub fn read_wasm_or_wat(wasm_file: &Path) -> Result<Vec<u8>, Error> {
let mut wasm_bytes =
fs::read(wasm_file).map_err(|_| anyhow!("failed to read Wasm file {wasm_file:?}"))?;
if wasm_file.extension().and_then(OsStr::to_str) == Some("wat") {
let wat = String::from_utf8(wasm_bytes)
.map_err(|error| anyhow!("failed to read UTF-8 file {wasm_file:?}: {error}"))?;
wasm_bytes = wat2wasm(&wat)
.map_err(|error| anyhow!("failed to parse .wat file {wasm_file:?}: {error}"))?;
}
Ok(wasm_bytes)
}

/// Returns a [`Val`] buffer capable of holding the return values.
///
/// The returned buffer can be used as function results for [`Func::call`](`wasmi::Func::call`).
Expand Down
2 changes: 1 addition & 1 deletion crates/wasi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ wiggle = { version = "27.0.0", default-features = false }
wasmi = { workspace = true, features = ["std"]}

[dev-dependencies]
wat = { version = "1", default-features = false }
wasmi = { workspace = true, features = ["std", "wat"] }
10 changes: 2 additions & 8 deletions crates/wasi/tests/wasi_wat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ use wasi_common::sync::WasiCtxBuilder;
use wasmi::{Config, Engine, Extern, Instance, Linker, Module, Store};
use wasmi_wasi::{add_to_linker, WasiCtx};

pub fn load_instance_from_wat(wat_bytes: &[u8]) -> (Store<WasiCtx>, wasmi::Instance) {
let wasm = wat2wasm(wat_bytes);
pub fn load_instance_from_wat(wasm: &[u8]) -> (Store<WasiCtx>, wasmi::Instance) {
let config = Config::default();
let engine = Engine::new(&config);
let module = Module::new(&engine, &wasm[..]).unwrap();
let module = Module::new(&engine, wasm).unwrap();
let mut linker = <Linker<WasiCtx>>::new(&engine);
// add wasi to linker
let wasi = WasiCtxBuilder::new()
Expand All @@ -25,11 +24,6 @@ pub fn load_instance_from_wat(wat_bytes: &[u8]) -> (Store<WasiCtx>, wasmi::Insta
(store, instance)
}

/// Converts the `.wat` encoded `bytes` into `.wasm` encoded bytes.
pub fn wat2wasm(bytes: &[u8]) -> Vec<u8> {
wat::parse_bytes(bytes).unwrap().into_owned()
}

fn load() -> (Store<WasiCtx>, Instance) {
let bytes = include_bytes!("wat/hello_world.wat");
load_instance_from_wat(bytes)
Expand Down
7 changes: 3 additions & 4 deletions crates/wasmi/benches/bench/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub fn bench_config() -> Config {
pub fn load_module_from_file(file_name: &str) -> wasmi::Module {
let wasm = load_wasm_from_file(file_name);
let engine = wasmi::Engine::new(&bench_config());
wasmi::Module::new(&engine, &wasm[..]).unwrap_or_else(|error| {
wasmi::Module::new(&engine, wasm).unwrap_or_else(|error| {
panic!(
"could not parse Wasm module from file {}: {}",
file_name, error
Expand Down Expand Up @@ -84,10 +84,9 @@ pub fn wat2wasm(bytes: &[u8]) -> Vec<u8> {
/// # Panics
///
/// If the benchmark Wasm file could not be opened, read or parsed.
pub fn load_instance_from_wat(wat_bytes: &[u8]) -> (wasmi::Store<()>, wasmi::Instance) {
let wasm = wat2wasm(wat_bytes);
pub fn load_instance_from_wat(wasm: &[u8]) -> (wasmi::Store<()>, wasmi::Instance) {
let engine = wasmi::Engine::new(&bench_config());
let module = wasmi::Module::new(&engine, &wasm[..]).unwrap();
let module = wasmi::Module::new(&engine, wasm).unwrap();
let linker = <wasmi::Linker<()>>::new(&engine);
let mut store = wasmi::Store::new(&engine, ());
let instance = linker
Expand Down
10 changes: 5 additions & 5 deletions crates/wasmi/benches/benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ fn bench_translate_case_best(c: &mut Criterion) {
});
b.iter_with_large_drop(|| {
let engine = Engine::default();
let _ = Module::new(&engine, &wasm[..]).unwrap();
let _ = Module::new(&engine, wasm).unwrap();
})
});
}
Expand Down Expand Up @@ -405,7 +405,7 @@ fn bench_translate_case_worst_stackbomb_small(c: &mut Criterion) {
});
b.iter_with_large_drop(|| {
let engine = Engine::default();
let _ = Module::new(&engine, &wasm[..]).unwrap();
let _ = Module::new(&engine, wasm).unwrap();
})
});
}
Expand Down Expand Up @@ -435,7 +435,7 @@ fn bench_translate_case_worst_stackbomb_big(c: &mut Criterion) {
});
b.iter_with_large_drop(|| {
let engine = Engine::default();
let _ = Module::new(&engine, &wasm[..]).unwrap();
let _ = Module::new(&engine, wasm).unwrap();
})
});
}
Expand Down Expand Up @@ -1169,9 +1169,9 @@ fn bench_execute_host_calls(c: &mut Criterion) {
}

let mut g = c.benchmark_group("execute/call/host");
let wasm = wat2wasm(include_bytes!("wat/host_calls.wat"));
let wasm = include_bytes!("wat/host_calls.wat");
let engine = Engine::default();
let module = Module::new(&engine, &wasm[..]).unwrap();
let module = Module::new(&engine, wasm).unwrap();
let mut store = Store::new(&engine, ());
let host0 = Func::wrap(&mut store, || ());
let host1 = Func::wrap(&mut store, |a: i64| a);
Expand Down
9 changes: 1 addition & 8 deletions crates/wasmi/src/engine/limits/tests.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
use self::engine::AvgBytesPerFunctionLimit;
use super::*;
use crate::{error::ErrorKind, Config, Engine, Error, Module};
use std::vec::Vec;

/// Converts the given `.wat` into `.wasm`.
fn wat2wasm(wat: &str) -> Vec<u8> {
wat::parse_str(wat).unwrap()
}

/// Parses and returns the Wasm module `wasm` with the given [`EnforcedLimits`] `limits`.
fn parse_with(wasm: &str, limits: EnforcedLimits) -> Result<Module, Error> {
let wasm = wat2wasm(wasm);
let mut config = Config::default();
config.enforced_limits(limits);
let engine = Engine::new(&config);
Module::new(&engine, &wasm[..])
Module::new(&engine, wasm)
}

#[test]
Expand Down
11 changes: 2 additions & 9 deletions crates/wasmi/src/engine/tests/many_inout.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
use crate::{Engine, Func, Linker, Module, Store, Val};
use std::vec::Vec;

/// Converts the given `.wat` into `.wasm`.
fn wat2wasm(wat: &str) -> Vec<u8> {
wat::parse_str(wat).unwrap()
}

/// Common routine to setup the tests.
fn setup_test(wat: &str) -> (Store<()>, Func) {
let wasm = wat2wasm(wat);
fn setup_test(wasm: &str) -> (Store<()>, Func) {
let engine = Engine::default();
let mut store = <Store<()>>::new(&engine, ());
let linker = <Linker<()>>::new(&engine);
let module = Module::new(&engine, &wasm[..]).unwrap();
let module = Module::new(&engine, wasm).unwrap();
let instance = linker
.instantiate(&mut store, &module)
.unwrap()
Expand Down
Loading

0 comments on commit df21a1a

Please sign in to comment.