diff --git a/Cargo.lock b/Cargo.lock index 57bf514a06b..54be6e1714b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1437,6 +1437,7 @@ dependencies = [ "wasmer-kernel-loader 0.1.0", "wasmer-llvm-backend 0.6.0", "wasmer-middleware-common 0.6.0", + "wasmer-middleware-common-tests 0.6.0", "wasmer-runtime 0.6.0", "wasmer-runtime-core 0.6.0", "wasmer-singlepass-backend 0.6.0", @@ -1557,11 +1558,19 @@ dependencies = [ [[package]] name = "wasmer-middleware-common" version = "0.6.0" +dependencies = [ + "wasmer-runtime-core 0.6.0", +] + +[[package]] +name = "wasmer-middleware-common-tests" +version = "0.6.0" dependencies = [ "criterion 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "wabt 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", "wasmer-clif-backend 0.6.0", "wasmer-llvm-backend 0.6.0", + "wasmer-middleware-common 0.6.0", "wasmer-runtime-core 0.6.0", "wasmer-singlepass-backend 0.6.0", ] diff --git a/Cargo.toml b/Cargo.toml index b2992bc6453..73cfac44148 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,6 +35,7 @@ wasmer-wasi = { path = "lib/wasi", optional = true } wasmer-kernel-loader = { path = "lib/kernel-loader", optional = true } wasmer-dev-utils = { path = "lib/dev-utils", optional = true } wasmer-wasi-tests = { path = "lib/wasi-tests", optional = true } +wasmer-middleware-common-tests = { path = "lib/middleware-common-tests", optional = true } wasmer-emscripten-tests = { path = "lib/emscripten-tests", optional = true } [workspace] @@ -56,6 +57,7 @@ members = [ "lib/dev-utils", "lib/wasi-tests", "lib/emscripten-tests", + "lib/middleware-common-tests", "examples/plugin-for-example" ] @@ -75,19 +77,22 @@ fast-tests = [] backend-cranelift = [ "wasmer-runtime-core/backend-cranelift", "wasmer-runtime/cranelift", - "wasmer-middleware-common/clif" + "wasmer-middleware-common-tests/clif", + "wasmer-wasi-tests/clif" ] backend-llvm = [ "wasmer-llvm-backend", "wasmer-runtime-core/backend-llvm", "wasmer-runtime/llvm", - "wasmer-middleware-common/llvm" + "wasmer-middleware-common-tests/llvm", + "wasmer-wasi-tests/llvm" ] backend-singlepass = [ "wasmer-singlepass-backend", "wasmer-runtime-core/backend-singlepass", "wasmer-runtime/singlepass", - "wasmer-middleware-common/singlepass" + "wasmer-middleware-common-tests/singlepass", + "wasmer-wasi-tests/singlepass" ] wasi = ["wasmer-wasi"] # vfs = ["wasmer-runtime-abi"] diff --git a/Makefile b/Makefile index a12d14fe591..eb8b2e97bba 100644 --- a/Makefile +++ b/Makefile @@ -50,13 +50,13 @@ emtests: emtests-unit emtests-singlepass emtests-cranelift emtests-llvm # Middleware tests middleware-singlepass: - cargo test --manifest-path lib/middleware-common/Cargo.toml --release --features singlepass + cargo test --manifest-path lib/middleware-common-tests/Cargo.toml --release --features singlepass middleware-cranelift: - cargo test --manifest-path lib/middleware-common/Cargo.toml --release --features clif + cargo test --manifest-path lib/middleware-common-tests/Cargo.toml --release --features clif middleware-llvm: - cargo test --manifest-path lib/middleware-common/Cargo.toml --release --features llvm + cargo test --manifest-path lib/middleware-common-tests/Cargo.toml --release --features llvm middleware: middleware-singlepass middleware-cranelift middleware-llvm @@ -99,7 +99,7 @@ capi: cargo test -p wasmer-runtime-c-api --release test-rest: capi - cargo test --release --all --exclude wasmer-runtime-c-api --exclude wasmer-emscripten --exclude wasmer-spectests --exclude wasmer-wasi --exclude wasmer-middleware-common --exclude wasmer-singlepass-backend --exclude wasmer-clif-backend --exclude wasmer-llvm-backend --exclude wasmer-wasi-tests --exclude wasmer-emscripten-tests + cargo test --release --all --exclude wasmer-runtime-c-api --exclude wasmer-emscripten --exclude wasmer-spectests --exclude wasmer-wasi --exclude wasmer-middleware-common --exclude wasmer-middleware-common-tests --exclude wasmer-singlepass-backend --exclude wasmer-clif-backend --exclude wasmer-llvm-backend --exclude wasmer-wasi-tests --exclude wasmer-emscripten-tests circleci-clean: @if [ ! -z "${CIRCLE_JOB}" ]; then rm -f /home/circleci/project/target/debug/deps/libcranelift_wasm* && rm -f /Users/distiller/project/target/debug/deps/libcranelift_wasm*; fi; @@ -178,4 +178,4 @@ publish-release: # cargo install cargo-deps # must install graphviz for `dot` dep-graph: - cargo deps --optional-deps --filter wasmer-wasi wasmer-wasi-tests wasmer-kernel-loader wasmer-dev-utils wasmer-llvm-backend wasmer-emscripten wasmer-emscripten-tests wasmer-runtime-core wasmer-runtime wasmer-middleware-common wasmer-singlepass-backend wasmer-clif-backend wasmer --manifest-path Cargo.toml | dot -Tpng > wasmer_depgraph.png + cargo deps --optional-deps --filter wasmer-wasi wasmer-wasi-tests wasmer-kernel-loader wasmer-dev-utils wasmer-llvm-backend wasmer-emscripten wasmer-emscripten-tests wasmer-runtime-core wasmer-runtime wasmer-middleware-common wasmer-middleware-common-tests wasmer-singlepass-backend wasmer-clif-backend wasmer --manifest-path Cargo.toml | dot -Tpng > wasmer_depgraph.png diff --git a/lib/dev-utils/Cargo.toml b/lib/dev-utils/Cargo.toml index 96fb6aeedbf..dbda4549dd6 100644 --- a/lib/dev-utils/Cargo.toml +++ b/lib/dev-utils/Cargo.toml @@ -6,6 +6,7 @@ license = "MIT" authors = ["The Wasmer Engineering Team "] edition = "2018" repository = "https://github.com/wasmerio/wasmer" +publish = false [dependencies] libc = "0.2.60" diff --git a/lib/middleware-common-tests/Cargo.toml b/lib/middleware-common-tests/Cargo.toml new file mode 100644 index 00000000000..147952462ac --- /dev/null +++ b/lib/middleware-common-tests/Cargo.toml @@ -0,0 +1,28 @@ +[package] +name = "wasmer-middleware-common-tests" +version = "0.6.0" +authors = ["The Wasmer Engineering Team "] +edition = "2018" +repository = "https://github.com/wasmerio/wasmer" +license = "MIT" +publish = false + +[dependencies] +wasmer-runtime-core = { path = "../runtime-core", version = "0.6.0" } +wasmer-middleware-common = { path = "../middleware-common", version = "0.6.0" } +wasmer-clif-backend = { path = "../clif-backend", version = "0.6.0" } +wasmer-llvm-backend = { path = "../llvm-backend", version = "0.6.0", optional = true } +wasmer-singlepass-backend = { path = "../singlepass-backend", version = "0.6.0", optional = true } + +[features] +clif = [] +llvm = ["wasmer-llvm-backend"] +singlepass = ["wasmer-singlepass-backend"] + +[dev-dependencies] +wabt = "0.9.1" +criterion = "0.2" + +[[bench]] +name = "metering_benchmark" +harness = false diff --git a/lib/middleware-common/benches/metering_benchmark.rs b/lib/middleware-common-tests/benches/metering_benchmark.rs similarity index 100% rename from lib/middleware-common/benches/metering_benchmark.rs rename to lib/middleware-common-tests/benches/metering_benchmark.rs diff --git a/lib/middleware-common-tests/src/lib.rs b/lib/middleware-common-tests/src/lib.rs new file mode 100644 index 00000000000..31b3e1065f9 --- /dev/null +++ b/lib/middleware-common-tests/src/lib.rs @@ -0,0 +1,152 @@ +#[cfg(all(test, any(feature = "singlepass", feature = "llvm")))] +mod tests { + use wabt::wat2wasm; + + use wasmer_middleware_common::metering::*; + use wasmer_runtime_core::codegen::{MiddlewareChain, StreamingCompiler}; + use wasmer_runtime_core::{backend::Compiler, compile_with, imports, Func}; + + #[cfg(feature = "llvm")] + fn get_compiler(limit: u64) -> impl Compiler { + use wasmer_llvm_backend::ModuleCodeGenerator as LLVMMCG; + let c: StreamingCompiler = StreamingCompiler::new(move || { + let mut chain = MiddlewareChain::new(); + chain.push(Metering::new(limit)); + chain + }); + c + } + + #[cfg(feature = "singlepass")] + fn get_compiler(limit: u64) -> impl Compiler { + use wasmer_singlepass_backend::ModuleCodeGenerator as SinglePassMCG; + let c: StreamingCompiler = StreamingCompiler::new(move || { + let mut chain = MiddlewareChain::new(); + chain.push(Metering::new(limit)); + chain + }); + c + } + + #[cfg(not(any(feature = "llvm", feature = "clif", feature = "singlepass")))] + compile_error!("compiler not specified, activate a compiler via features"); + + #[cfg(feature = "clif")] + fn get_compiler(_limit: u64) -> impl Compiler { + compile_error!("cranelift does not implement metering"); + use wasmer_clif_backend::CraneliftCompiler; + CraneliftCompiler::new() + } + + // Assemblyscript + // export function add_to(x: i32, y: i32): i32 { + // for(var i = 0; i < x; i++){ + // if(i % 1 == 0){ + // y += i; + // } else { + // y *= i + // } + // } + // return y; + // } + static WAT: &'static str = r#" + (module + (type $t0 (func (param i32 i32) (result i32))) + (type $t1 (func)) + (func $add_to (export "add_to") (type $t0) (param $p0 i32) (param $p1 i32) (result i32) + (local $l0 i32) + block $B0 + i32.const 0 + set_local $l0 + loop $L1 + get_local $l0 + get_local $p0 + i32.lt_s + i32.eqz + br_if $B0 + get_local $l0 + i32.const 1 + i32.rem_s + i32.const 0 + i32.eq + if $I2 + get_local $p1 + get_local $l0 + i32.add + set_local $p1 + else + get_local $p1 + get_local $l0 + i32.mul + set_local $p1 + end + get_local $l0 + i32.const 1 + i32.add + set_local $l0 + br $L1 + unreachable + end + unreachable + end + get_local $p1) + (func $f1 (type $t1)) + (table $table (export "table") 1 anyfunc) + (memory $memory (export "memory") 0) + (global $g0 i32 (i32.const 8)) + (elem (i32.const 0) $f1)) + "#; + + #[test] + fn test_points_reduced_after_call() { + let wasm_binary = wat2wasm(WAT).unwrap(); + + let limit = 100u64; + + let module = compile_with(&wasm_binary, &get_compiler(limit)).unwrap(); + + let import_object = imports! {}; + let mut instance = module.instantiate(&import_object).unwrap(); + + set_points_used(&mut instance, 0u64); + + let add_to: Func<(i32, i32), i32> = instance.func("add_to").unwrap(); + let value = add_to.call(3, 4).unwrap(); + + // verify it returns the correct value + assert_eq!(value, 7); + + // verify it used the correct number of points + assert_eq!(get_points_used(&instance), 74); + } + + #[test] + fn test_traps_after_costly_call() { + use wasmer_runtime_core::error::RuntimeError; + let wasm_binary = wat2wasm(WAT).unwrap(); + + let limit = 100u64; + + let module = compile_with(&wasm_binary, &get_compiler(limit)).unwrap(); + + let import_object = imports! {}; + let mut instance = module.instantiate(&import_object).unwrap(); + + set_points_used(&mut instance, 0u64); + + let add_to: Func<(i32, i32), i32> = instance.func("add_to").unwrap(); + let result = add_to.call(10_000_000, 4); + + let err = result.unwrap_err(); + match err { + RuntimeError::Error { data } => { + assert!(data.downcast_ref::().is_some()); + } + _ => unreachable!(), + } + + // verify it used the correct number of points + assert_eq!(get_points_used(&instance), 109); // Used points will be slightly more than `limit` because of the way we do gas checking. + } + +} diff --git a/lib/middleware-common/Cargo.toml b/lib/middleware-common/Cargo.toml index 8944bfa2124..213f241c219 100644 --- a/lib/middleware-common/Cargo.toml +++ b/lib/middleware-common/Cargo.toml @@ -9,19 +9,3 @@ edition = "2018" [dependencies] wasmer-runtime-core = { path = "../runtime-core" } -wasmer-clif-backend = { path = "../clif-backend", version = "0.6.0" } -wasmer-llvm-backend = { path = "../llvm-backend", version = "0.6.0", optional = true } -wasmer-singlepass-backend = { path = "../singlepass-backend", version = "0.6.0", optional = true } - -[dev-dependencies] -wabt = "0.9.1" -criterion = "0.2" - -[features] -clif = [] -llvm = ["wasmer-llvm-backend"] -singlepass = ["wasmer-singlepass-backend"] - -[[bench]] -name = "metering_benchmark" -harness = false diff --git a/lib/middleware-common/src/metering.rs b/lib/middleware-common/src/metering.rs index e9f02d5098f..a7d10aece10 100644 --- a/lib/middleware-common/src/metering.rs +++ b/lib/middleware-common/src/metering.rs @@ -128,156 +128,3 @@ pub fn get_points_used_ctx(ctx: &Ctx) -> u64 { pub fn set_points_used_ctx(ctx: &mut Ctx, value: u64) { ctx.set_internal(&INTERNAL_FIELD, value); } - -#[cfg(all(test, any(feature = "singlepass", feature = "llvm")))] -mod tests { - use super::*; - use wabt::wat2wasm; - - use wasmer_runtime_core::codegen::{MiddlewareChain, StreamingCompiler}; - use wasmer_runtime_core::{backend::Compiler, compile_with, imports, Func}; - - #[cfg(feature = "llvm")] - fn get_compiler(limit: u64) -> impl Compiler { - use wasmer_llvm_backend::ModuleCodeGenerator as LLVMMCG; - let c: StreamingCompiler = StreamingCompiler::new(move || { - let mut chain = MiddlewareChain::new(); - chain.push(Metering::new(limit)); - chain - }); - c - } - - #[cfg(feature = "singlepass")] - fn get_compiler(limit: u64) -> impl Compiler { - use wasmer_singlepass_backend::ModuleCodeGenerator as SinglePassMCG; - let c: StreamingCompiler = StreamingCompiler::new(move || { - let mut chain = MiddlewareChain::new(); - chain.push(Metering::new(limit)); - chain - }); - c - } - - #[cfg(not(any(feature = "llvm", feature = "clif", feature = "singlepass")))] - compile_error!("compiler not specified, activate a compiler via features"); - - #[cfg(feature = "clif")] - fn get_compiler(_limit: u64) -> impl Compiler { - compile_error!("cranelift does not implement metering"); - use wasmer_clif_backend::CraneliftCompiler; - CraneliftCompiler::new() - } - - // Assemblyscript - // export function add_to(x: i32, y: i32): i32 { - // for(var i = 0; i < x; i++){ - // if(i % 1 == 0){ - // y += i; - // } else { - // y *= i - // } - // } - // return y; - // } - static WAT: &'static str = r#" - (module - (type $t0 (func (param i32 i32) (result i32))) - (type $t1 (func)) - (func $add_to (export "add_to") (type $t0) (param $p0 i32) (param $p1 i32) (result i32) - (local $l0 i32) - block $B0 - i32.const 0 - set_local $l0 - loop $L1 - get_local $l0 - get_local $p0 - i32.lt_s - i32.eqz - br_if $B0 - get_local $l0 - i32.const 1 - i32.rem_s - i32.const 0 - i32.eq - if $I2 - get_local $p1 - get_local $l0 - i32.add - set_local $p1 - else - get_local $p1 - get_local $l0 - i32.mul - set_local $p1 - end - get_local $l0 - i32.const 1 - i32.add - set_local $l0 - br $L1 - unreachable - end - unreachable - end - get_local $p1) - (func $f1 (type $t1)) - (table $table (export "table") 1 anyfunc) - (memory $memory (export "memory") 0) - (global $g0 i32 (i32.const 8)) - (elem (i32.const 0) $f1)) - "#; - - #[test] - fn test_points_reduced_after_call() { - let wasm_binary = wat2wasm(WAT).unwrap(); - - let limit = 100u64; - - let module = compile_with(&wasm_binary, &get_compiler(limit)).unwrap(); - - let import_object = imports! {}; - let mut instance = module.instantiate(&import_object).unwrap(); - - set_points_used(&mut instance, 0u64); - - let add_to: Func<(i32, i32), i32> = instance.func("add_to").unwrap(); - let value = add_to.call(3, 4).unwrap(); - - // verify it returns the correct value - assert_eq!(value, 7); - - // verify it used the correct number of points - assert_eq!(get_points_used(&instance), 74); - } - - #[test] - fn test_traps_after_costly_call() { - use wasmer_runtime_core::error::RuntimeError; - let wasm_binary = wat2wasm(WAT).unwrap(); - - let limit = 100u64; - - let module = compile_with(&wasm_binary, &get_compiler(limit)).unwrap(); - - let import_object = imports! {}; - let mut instance = module.instantiate(&import_object).unwrap(); - - set_points_used(&mut instance, 0u64); - - let add_to: Func<(i32, i32), i32> = instance.func("add_to").unwrap(); - let result = add_to.call(10_000_000, 4); - - let err = result.unwrap_err(); - match err { - RuntimeError::Error { data } => { - assert!(data.downcast_ref::().is_some()); - } - _ => unreachable!(), - } - - // verify it used the correct number of points - assert_eq!(get_points_used(&instance), 109); // Used points will be slightly more than `limit` because of the way we do gas checking. - } - -} diff --git a/scripts/update_version_numbers.sh b/scripts/update_version_numbers.sh index 2177848107a..5fb3a001595 100755 --- a/scripts/update_version_numbers.sh +++ b/scripts/update_version_numbers.sh @@ -11,6 +11,7 @@ echo "manually check changes to wasmer.iss" # Order to upload packages in ## runtime-core ## win-exception-handler +## middleware-common ## clif-backend ## llvm-backend ## singlepass-backend