Skip to content

Commit 91b199f

Browse files
author
Mark McCaskey
committed
Split middleware-common tests into separate subcrate
1 parent 5205ada commit 91b199f

File tree

10 files changed

+204
-177
lines changed

10 files changed

+204
-177
lines changed

Cargo.lock

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+8-3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ wasmer-wasi = { path = "lib/wasi", optional = true }
3535
wasmer-kernel-loader = { path = "lib/kernel-loader", optional = true }
3636
wasmer-dev-utils = { path = "lib/dev-utils", optional = true }
3737
wasmer-wasi-tests = { path = "lib/wasi-tests", optional = true }
38+
wasmer-middleware-common-tests = { path = "lib/middleware-common-tests", optional = true }
3839
wasmer-emscripten-tests = { path = "lib/emscripten-tests", optional = true }
3940

4041
[workspace]
@@ -56,6 +57,7 @@ members = [
5657
"lib/dev-utils",
5758
"lib/wasi-tests",
5859
"lib/emscripten-tests",
60+
"lib/middleware-common-tests",
5961
"examples/plugin-for-example"
6062
]
6163

@@ -75,19 +77,22 @@ fast-tests = []
7577
backend-cranelift = [
7678
"wasmer-runtime-core/backend-cranelift",
7779
"wasmer-runtime/cranelift",
78-
"wasmer-middleware-common/clif"
80+
"wasmer-middleware-common-tests/clif",
81+
"wasmer-wasi-tests/clif"
7982
]
8083
backend-llvm = [
8184
"wasmer-llvm-backend",
8285
"wasmer-runtime-core/backend-llvm",
8386
"wasmer-runtime/llvm",
84-
"wasmer-middleware-common/llvm"
87+
"wasmer-middleware-common-tests/llvm",
88+
"wasmer-wasi-tests/llvm"
8589
]
8690
backend-singlepass = [
8791
"wasmer-singlepass-backend",
8892
"wasmer-runtime-core/backend-singlepass",
8993
"wasmer-runtime/singlepass",
90-
"wasmer-middleware-common/singlepass"
94+
"wasmer-middleware-common-tests/singlepass",
95+
"wasmer-wasi-tests/singlepass"
9196
]
9297
wasi = ["wasmer-wasi"]
9398
# vfs = ["wasmer-runtime-abi"]

Makefile

+5-5
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ emtests: emtests-unit emtests-singlepass emtests-cranelift emtests-llvm
5050

5151
# Middleware tests
5252
middleware-singlepass:
53-
cargo test --manifest-path lib/middleware-common/Cargo.toml --release --features singlepass
53+
cargo test --manifest-path lib/middleware-common-tests/Cargo.toml --release --features singlepass
5454

5555
middleware-cranelift:
56-
cargo test --manifest-path lib/middleware-common/Cargo.toml --release --features clif
56+
cargo test --manifest-path lib/middleware-common-tests/Cargo.toml --release --features clif
5757

5858
middleware-llvm:
59-
cargo test --manifest-path lib/middleware-common/Cargo.toml --release --features llvm
59+
cargo test --manifest-path lib/middleware-common-tests/Cargo.toml --release --features llvm
6060

6161
middleware: middleware-singlepass middleware-cranelift middleware-llvm
6262

@@ -99,7 +99,7 @@ capi:
9999
cargo test -p wasmer-runtime-c-api --release
100100

101101
test-rest: capi
102-
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
102+
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
103103

104104
circleci-clean:
105105
@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:
178178
# cargo install cargo-deps
179179
# must install graphviz for `dot`
180180
dep-graph:
181-
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
181+
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

lib/dev-utils/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ license = "MIT"
66
authors = ["The Wasmer Engineering Team <engineering@wasmer.io>"]
77
edition = "2018"
88
repository = "https://github.com/wasmerio/wasmer"
9+
publish = false
910

1011
[dependencies]
1112
libc = "0.2.60"
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[package]
2+
name = "wasmer-middleware-common-tests"
3+
version = "0.6.0"
4+
authors = ["The Wasmer Engineering Team <engineering@wasmer.io>"]
5+
edition = "2018"
6+
repository = "https://github.com/wasmerio/wasmer"
7+
license = "MIT"
8+
publish = false
9+
10+
[dependencies]
11+
wasmer-runtime-core = { path = "../runtime-core", version = "0.6.0" }
12+
wasmer-middleware-common = { path = "../middleware-common", version = "0.6.0" }
13+
wasmer-clif-backend = { path = "../clif-backend", version = "0.6.0" }
14+
wasmer-llvm-backend = { path = "../llvm-backend", version = "0.6.0", optional = true }
15+
wasmer-singlepass-backend = { path = "../singlepass-backend", version = "0.6.0", optional = true }
16+
17+
[features]
18+
clif = []
19+
llvm = ["wasmer-llvm-backend"]
20+
singlepass = ["wasmer-singlepass-backend"]
21+
22+
[dev-dependencies]
23+
wabt = "0.9.1"
24+
criterion = "0.2"
25+
26+
[[bench]]
27+
name = "metering_benchmark"
28+
harness = false
+152
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
#[cfg(all(test, any(feature = "singlepass", feature = "llvm")))]
2+
mod tests {
3+
use wabt::wat2wasm;
4+
5+
use wasmer_middleware_common::metering::*;
6+
use wasmer_runtime_core::codegen::{MiddlewareChain, StreamingCompiler};
7+
use wasmer_runtime_core::{backend::Compiler, compile_with, imports, Func};
8+
9+
#[cfg(feature = "llvm")]
10+
fn get_compiler(limit: u64) -> impl Compiler {
11+
use wasmer_llvm_backend::ModuleCodeGenerator as LLVMMCG;
12+
let c: StreamingCompiler<LLVMMCG, _, _, _, _> = StreamingCompiler::new(move || {
13+
let mut chain = MiddlewareChain::new();
14+
chain.push(Metering::new(limit));
15+
chain
16+
});
17+
c
18+
}
19+
20+
#[cfg(feature = "singlepass")]
21+
fn get_compiler(limit: u64) -> impl Compiler {
22+
use wasmer_singlepass_backend::ModuleCodeGenerator as SinglePassMCG;
23+
let c: StreamingCompiler<SinglePassMCG, _, _, _, _> = StreamingCompiler::new(move || {
24+
let mut chain = MiddlewareChain::new();
25+
chain.push(Metering::new(limit));
26+
chain
27+
});
28+
c
29+
}
30+
31+
#[cfg(not(any(feature = "llvm", feature = "clif", feature = "singlepass")))]
32+
compile_error!("compiler not specified, activate a compiler via features");
33+
34+
#[cfg(feature = "clif")]
35+
fn get_compiler(_limit: u64) -> impl Compiler {
36+
compile_error!("cranelift does not implement metering");
37+
use wasmer_clif_backend::CraneliftCompiler;
38+
CraneliftCompiler::new()
39+
}
40+
41+
// Assemblyscript
42+
// export function add_to(x: i32, y: i32): i32 {
43+
// for(var i = 0; i < x; i++){
44+
// if(i % 1 == 0){
45+
// y += i;
46+
// } else {
47+
// y *= i
48+
// }
49+
// }
50+
// return y;
51+
// }
52+
static WAT: &'static str = r#"
53+
(module
54+
(type $t0 (func (param i32 i32) (result i32)))
55+
(type $t1 (func))
56+
(func $add_to (export "add_to") (type $t0) (param $p0 i32) (param $p1 i32) (result i32)
57+
(local $l0 i32)
58+
block $B0
59+
i32.const 0
60+
set_local $l0
61+
loop $L1
62+
get_local $l0
63+
get_local $p0
64+
i32.lt_s
65+
i32.eqz
66+
br_if $B0
67+
get_local $l0
68+
i32.const 1
69+
i32.rem_s
70+
i32.const 0
71+
i32.eq
72+
if $I2
73+
get_local $p1
74+
get_local $l0
75+
i32.add
76+
set_local $p1
77+
else
78+
get_local $p1
79+
get_local $l0
80+
i32.mul
81+
set_local $p1
82+
end
83+
get_local $l0
84+
i32.const 1
85+
i32.add
86+
set_local $l0
87+
br $L1
88+
unreachable
89+
end
90+
unreachable
91+
end
92+
get_local $p1)
93+
(func $f1 (type $t1))
94+
(table $table (export "table") 1 anyfunc)
95+
(memory $memory (export "memory") 0)
96+
(global $g0 i32 (i32.const 8))
97+
(elem (i32.const 0) $f1))
98+
"#;
99+
100+
#[test]
101+
fn test_points_reduced_after_call() {
102+
let wasm_binary = wat2wasm(WAT).unwrap();
103+
104+
let limit = 100u64;
105+
106+
let module = compile_with(&wasm_binary, &get_compiler(limit)).unwrap();
107+
108+
let import_object = imports! {};
109+
let mut instance = module.instantiate(&import_object).unwrap();
110+
111+
set_points_used(&mut instance, 0u64);
112+
113+
let add_to: Func<(i32, i32), i32> = instance.func("add_to").unwrap();
114+
let value = add_to.call(3, 4).unwrap();
115+
116+
// verify it returns the correct value
117+
assert_eq!(value, 7);
118+
119+
// verify it used the correct number of points
120+
assert_eq!(get_points_used(&instance), 74);
121+
}
122+
123+
#[test]
124+
fn test_traps_after_costly_call() {
125+
use wasmer_runtime_core::error::RuntimeError;
126+
let wasm_binary = wat2wasm(WAT).unwrap();
127+
128+
let limit = 100u64;
129+
130+
let module = compile_with(&wasm_binary, &get_compiler(limit)).unwrap();
131+
132+
let import_object = imports! {};
133+
let mut instance = module.instantiate(&import_object).unwrap();
134+
135+
set_points_used(&mut instance, 0u64);
136+
137+
let add_to: Func<(i32, i32), i32> = instance.func("add_to").unwrap();
138+
let result = add_to.call(10_000_000, 4);
139+
140+
let err = result.unwrap_err();
141+
match err {
142+
RuntimeError::Error { data } => {
143+
assert!(data.downcast_ref::<ExecutionLimitExceededError>().is_some());
144+
}
145+
_ => unreachable!(),
146+
}
147+
148+
// verify it used the correct number of points
149+
assert_eq!(get_points_used(&instance), 109); // Used points will be slightly more than `limit` because of the way we do gas checking.
150+
}
151+
152+
}

lib/middleware-common/Cargo.toml

-16
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,3 @@ edition = "2018"
99

1010
[dependencies]
1111
wasmer-runtime-core = { path = "../runtime-core" }
12-
wasmer-clif-backend = { path = "../clif-backend", version = "0.6.0" }
13-
wasmer-llvm-backend = { path = "../llvm-backend", version = "0.6.0", optional = true }
14-
wasmer-singlepass-backend = { path = "../singlepass-backend", version = "0.6.0", optional = true }
15-
16-
[dev-dependencies]
17-
wabt = "0.9.1"
18-
criterion = "0.2"
19-
20-
[features]
21-
clif = []
22-
llvm = ["wasmer-llvm-backend"]
23-
singlepass = ["wasmer-singlepass-backend"]
24-
25-
[[bench]]
26-
name = "metering_benchmark"
27-
harness = false

0 commit comments

Comments
 (0)