-
Notifications
You must be signed in to change notification settings - Fork 824
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
733: Split middleware-common tests into separate subcrate r=MarkMcCaskey a=MarkMcCaskey New depgraph: ![wasmer_depgraph](https://user-images.githubusercontent.com/5770194/63965617-8719c000-cad4-11e9-8d46-ad8799caad6a.png) Co-authored-by: Mark McCaskey <[email protected]>
- Loading branch information
Showing
10 changed files
with
204 additions
and
177 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ license = "MIT" | |
authors = ["The Wasmer Engineering Team <[email protected]>"] | ||
edition = "2018" | ||
repository = "https://github.com/wasmerio/wasmer" | ||
publish = false | ||
|
||
[dependencies] | ||
libc = "0.2.60" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]>"] | ||
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 |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.