Skip to content

Commit

Permalink
Upgrade to wasmer 4.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
sug0 committed May 24, 2024
1 parent 3e7fde8 commit 9d9cb52
Show file tree
Hide file tree
Showing 21 changed files with 2,251 additions and 1,236 deletions.
912 changes: 539 additions & 373 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 4 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,10 @@ tracing-log = "0.2.0"
tracing-subscriber = {version = "0.3.7", default-features = false, features = ["env-filter", "fmt"]}
wasmparser = "0.107.0"
wasm-instrument = {version = "0.4.0", features = ["sign_ext"]}
wasmer = {git = "https://github.com/heliaxdev/wasmer", rev = "255054f7f58b7b4a525f2fee6b9b86422d1ca15b"}
wasmer-cache = { git = "https://github.com/heliaxdev/wasmer", rev = "255054f7f58b7b4a525f2fee6b9b86422d1ca15b" }
wasmer-compiler-singlepass = { git = "https://github.com/heliaxdev/wasmer", rev = "255054f7f58b7b4a525f2fee6b9b86422d1ca15b" }
wasmer-engine-dylib = { git = "https://github.com/heliaxdev/wasmer", rev = "255054f7f58b7b4a525f2fee6b9b86422d1ca15b" }
wasmer-engine-universal = { git = "https://github.com/heliaxdev/wasmer", rev = "255054f7f58b7b4a525f2fee6b9b86422d1ca15b" }
wasmer-vm = { git = "https://github.com/heliaxdev/wasmer", rev = "255054f7f58b7b4a525f2fee6b9b86422d1ca15b" }
wasmer = "4.3.1"
wasmer-cache = "4.3.1"
wasmer-compiler-singlepass = "4.3.1"
wasmer-vm = "4.3.1"
winapi = "0.3.9"
yansi = "0.5.1"
zeroize = { version = "1.5.5", features = ["zeroize_derive"] }
Expand Down
1 change: 0 additions & 1 deletion crates/benches/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,4 @@ tempfile.workspace = true
sha2.workspace = true
wasm-instrument.workspace = true
wasmer-compiler-singlepass.workspace = true
wasmer-engine-universal.workspace = true
wasmer.workspace = true
31 changes: 17 additions & 14 deletions crates/benches/wasm_opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,12 +410,11 @@ impl Display for WatBuilder {
// optimizations that would compile out the benchmarks since most of them are
// trivial operations
fn get_wasm_store() -> Store {
wasmer::Store::new(
&wasmer_engine_universal::Universal::new(
wasmer_compiler_singlepass::Singlepass::default(),
)
.engine(),
)
Store::new(<wasmer::Engine as wasmer::NativeEngineExt>::new(
Box::new(wasmer_compiler_singlepass::Singlepass::default()),
wasmer::Target::default(),
wasmer::sys::Features::default(),
))
}

// An empty wasm module to serve as the base reference for all the other
Expand All @@ -428,29 +427,33 @@ fn empty_module(c: &mut Criterion) {
)
"#,
);
let module = Module::new(&get_wasm_store(), module_wat).unwrap();
let instance = Instance::new(&module, &imports! {}).unwrap();
let mut store = get_wasm_store();
let module = Module::new(&store, module_wat).unwrap();
let instance = Instance::new(&mut store, &module, &imports! {}).unwrap();
let function = instance.exports.get_function(ENTRY_POINT).unwrap();

c.bench_function("empty_module", |b| {
b.iter(|| function.call(&[Value::I32(0)]).unwrap());
b.iter(|| function.call(&mut store, &[Value::I32(0)]).unwrap());
});
}

fn ops(c: &mut Criterion) {
let mut group = c.benchmark_group("wasm_opts");

for builder in bench_functions() {
let module =
Module::new(&get_wasm_store(), builder.to_string()).unwrap();
let instance = Instance::new(&module, &imports! {}).unwrap();
let mut store = get_wasm_store();
let module = Module::new(&store, builder.to_string()).unwrap();
let instance =
Instance::new(&mut store, &module, &imports! {}).unwrap();
let function = instance.exports.get_function(ENTRY_POINT).unwrap();

group.bench_function(format!("{}", builder.instruction), |b| {
if let Unreachable = builder.instruction {
b.iter(|| function.call(&[Value::I32(0)]).unwrap_err());
b.iter(|| {
function.call(&mut store, &[Value::I32(0)]).unwrap_err()
});
} else {
b.iter(|| function.call(&[Value::I32(0)]).unwrap());
b.iter(|| function.call(&mut store, &[Value::I32(0)]).unwrap());
}
});
}
Expand Down
6 changes: 0 additions & 6 deletions crates/namada/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,11 @@ default = ["wasm-runtime"]
mainnet = ["namada_core/mainnet"]
std = ["namada_sdk/std"]
wasm-runtime = [
"loupe",
"parity-wasm",
"rayon",
"wasm-instrument",
"wasmer-cache",
"wasmer-compiler-singlepass",
"wasmer-engine-dylib",
"wasmer-engine-universal",
"wasmer-vm",
"wasmer",
]
Expand Down Expand Up @@ -121,7 +118,6 @@ futures.workspace = true
itertools.workspace = true
konst.workspace = true
linkme = {workspace = true, optional = true}
loupe = { version = "0.1.3", optional = true }
masp_primitives.workspace = true
masp_proofs.workspace = true
num256.workspace = true
Expand Down Expand Up @@ -154,8 +150,6 @@ wasm-instrument = { workspace = true, optional = true }
wasmer = { workspace = true, optional = true }
wasmer-cache = { workspace = true, optional = true }
wasmer-compiler-singlepass = { workspace = true, optional = true }
wasmer-engine-dylib = { workspace = true, optional = true }
wasmer-engine-universal = { workspace = true, optional = true }
wasmer-vm = { workspace = true, optional = true }
# Greater versions break in `test_tx_stack_limiter` and `test_vp_stack_limiter`
wat = "=1.0.71"
Expand Down
21 changes: 13 additions & 8 deletions crates/namada/src/vm/host_env.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Virtual machine's host environment exposes functions that may be called from
//! within a virtual machine.

use std::cell::RefCell;
use std::collections::BTreeSet;
use std::fmt::Debug;
Expand Down Expand Up @@ -2385,6 +2386,8 @@ where
/// A helper module for testing
#[cfg(feature = "testing")]
pub mod testing {
use std::rc::Rc;

use super::*;
use crate::vm::memory::testing::NativeMemory;
use crate::vm::wasm::memory::WasmMemory;
Expand Down Expand Up @@ -2451,15 +2454,14 @@ pub mod testing {
S: State,
CA: WasmCacheAccess,
{
let store = crate::vm::wasm::compilation_cache::common::store();
let initial_memory =
crate::vm::wasm::memory::prepare_tx_memory(&store).unwrap();
let mut wasm_memory = WasmMemory::default();
wasm_memory.inner.initialize(initial_memory);
let mut store = crate::vm::wasm::compilation_cache::common::store();

let wasm_memory =
crate::vm::wasm::memory::prepare_tx_memory(&mut store).unwrap();

let (write_log, in_mem, db) = state.split_borrow();
TxVmEnv::new(
wasm_memory,
let mut env = TxVmEnv::new(
WasmMemory::new(Rc::new(RefCell::new(store))),
write_log,
in_mem,
db,
Expand All @@ -2476,7 +2478,10 @@ pub mod testing {
vp_wasm_cache,
#[cfg(feature = "wasm-runtime")]
tx_wasm_cache,
)
);

env.memory.init_from(&wasm_memory);
env
}

/// Setup a validity predicate environment
Expand Down
45 changes: 2 additions & 43 deletions crates/namada/src/vm/wasm/compilation_cache/common.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//! WASM compilation cache.
//!
//! The cache is backed by in-memory LRU cache with configurable size
//! limit and a file system cache of compiled modules (either to dynamic libs
//! compiled via the `dylib` module, or serialized modules compiled via the
//! `universal` module).
//! limit and a file system cache of serialized modules.

use std::collections::hash_map::RandomState;
use std::fs;
Expand Down Expand Up @@ -496,9 +495,6 @@ fn hash_of_code(code: impl AsRef<[u8]>) -> Hash {
fn compile(
code: impl AsRef<[u8]>,
) -> Result<(Module, Store), wasm::run::Error> {
// There's an issue with dylib compiler on mac in linker and on linux
// with the dylib's store loading the dylib from a file, so we're caching a
// module serialized to bytes instead for now.
universal::compile(code).map_err(wasm::run::Error::CompileError)
}

Expand Down Expand Up @@ -581,39 +577,6 @@ mod universal {
}
}

/// A dynamic library engine compilation.
mod dylib {
use super::*;

#[allow(dead_code)]
#[cfg(windows)]
pub const FILE_EXT: &str = "dll";
#[allow(dead_code)]
#[cfg(all(not(unix), target_os = "macos"))]
pub const FILE_EXT: &str = "dylib";
#[allow(dead_code)]
#[cfg(all(unix, not(target_os = "macos")))]
pub const FILE_EXT: &str = "so";

/// Compile wasm to a dynamic library
#[allow(dead_code)]
pub fn compile(
code: impl AsRef<[u8]>,
) -> Result<(Module, Store), wasmer::CompileError> {
let store = store();
let module = Module::new(&store, code.as_ref())?;
Ok((module, store))
}

/// Dylib WASM store
#[allow(dead_code)]
pub fn store() -> Store {
let compiler = wasmer_compiler_singlepass::Singlepass::default();
let engine = wasmer_engine_dylib::Dylib::new(compiler).engine();
Store::new_with_tunables(&engine, memory::vp_limit())
}
}

/// Testing helpers
#[cfg(any(test, feature = "testing"))]
pub mod testing {
Expand Down Expand Up @@ -1063,10 +1026,6 @@ mod test {

/// Get the WASM code bytes, its hash and find the compiled module's size
fn load_wasm(file: impl AsRef<Path>) -> WasmWithMeta {
// When `WeightScale` calls `loupe::size_of_val` in the cache, for some
// reason it returns 8 bytes more than the same call in here.
let _extra_bytes = 8;

let file = file.as_ref();
let code = fs::read(file).unwrap();
let hash = hash_of_code(&code);
Expand Down
Loading

0 comments on commit 9d9cb52

Please sign in to comment.