Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split middleware-common tests into separate subcrate #733

Merged
merged 1 commit into from
Aug 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Cargo.lock

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

11 changes: 8 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -56,6 +57,7 @@ members = [
"lib/dev-utils",
"lib/wasi-tests",
"lib/emscripten-tests",
"lib/middleware-common-tests",
"examples/plugin-for-example"
]

Expand All @@ -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"]
Expand Down
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions lib/dev-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ license = "MIT"
authors = ["The Wasmer Engineering Team <engineering@wasmer.io>"]
edition = "2018"
repository = "https://github.com/wasmerio/wasmer"
publish = false

[dependencies]
libc = "0.2.60"
28 changes: 28 additions & 0 deletions lib/middleware-common-tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[package]
name = "wasmer-middleware-common-tests"
version = "0.6.0"
authors = ["The Wasmer Engineering Team <engineering@wasmer.io>"]
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
152 changes: 152 additions & 0 deletions lib/middleware-common-tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<LLVMMCG, _, _, _, _> = 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<SinglePassMCG, _, _, _, _> = 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::<ExecutionLimitExceededError>().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.
}

}
16 changes: 0 additions & 16 deletions lib/middleware-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading