mirror of
https://github.com/fluencelabs/wasmer
synced 2025-03-31 06:51:04 +00:00
200 lines
6.4 KiB
Rust
200 lines
6.4 KiB
Rust
// use test_case::test_case;
|
|
// use wabt::wat2wasm;
|
|
// use wasmer::compiler::{compile_with, Compiler};
|
|
// use wasmer::imports;
|
|
// use wasmer::wasm::Func;
|
|
// use wasmer_middleware_common::metering::*;
|
|
// use wasmer_runtime_core::codegen::ModuleCodeGenerator;
|
|
// use wasmer_runtime_core::codegen::{MiddlewareChain, StreamingCompiler};
|
|
// use wasmer_runtime_core::fault::{pop_code_version, push_code_version};
|
|
// use wasmer_runtime_core::state::CodeVersion;
|
|
|
|
// fn get_compiler(name: &str, limit: u64) -> impl Compiler {
|
|
// match name {
|
|
// #[cfg(feature = "backend-singlepass")]
|
|
// "singlepass" => {
|
|
// use wasmer_singlepass_backend::ModuleCodeGenerator as SinglepassMCG;
|
|
// StreamingCompiler<SinglepassMCG, _, _, _, _> = StreamingCompiler::new(move || {
|
|
// let mut chain = MiddlewareChain::new();
|
|
// chain.push(Metering::new(limit));
|
|
// chain
|
|
// })
|
|
// },
|
|
// #[cfg(feature = "backend-llvm")]
|
|
// "llvm" => {
|
|
// use wasmer_llvm_backend::ModuleCodeGenerator as LLVMMCG;
|
|
// StreamingCompiler<LLVMMCG, _, _, _, _> = StreamingCompiler::new(move || {
|
|
// let mut chain = MiddlewareChain::new();
|
|
// chain.push(Metering::new(limit));
|
|
// chain
|
|
// })
|
|
// }
|
|
// _ => panic!("Unimplemented backend");
|
|
// }
|
|
// }
|
|
|
|
// // 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))
|
|
// "#;
|
|
|
|
// #[cfg(feature = "backend-singlepass")]
|
|
// mod singlepass {
|
|
// #[test]
|
|
// fn middleware_test_points_reduced_after_call() {
|
|
// super::middleware_test_points_reduced_after_call("singlepass")
|
|
// }
|
|
// #[test]
|
|
// fn middleware_test_traps_after_costly_call() {
|
|
// super::middleware_test_traps_after_costly_call("singlepass")
|
|
// }
|
|
// }
|
|
|
|
// #[cfg(feature = "backend-llvm")]
|
|
// mod llvm {
|
|
// #[test]
|
|
// fn middleware_test_points_reduced_after_call() {
|
|
// super::middleware_test_points_reduced_after_call("llvm")
|
|
// }
|
|
// #[test]
|
|
// fn middleware_test_traps_after_costly_call() {
|
|
// super::middleware_test_traps_after_costly_call("llvm")
|
|
// }
|
|
// }
|
|
|
|
// fn middleware_test_points_reduced_after_call(backend: &'static str) {
|
|
// let wasm_binary = wat2wasm(WAT).unwrap();
|
|
|
|
// let limit = 100u64;
|
|
|
|
// let compiler = get_compiler(backend, limit);
|
|
// let module = compile_with(&wasm_binary, &compiler).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.exports.get("add_to").unwrap();
|
|
|
|
// let cv_pushed = if let Some(msm) = instance.module.runnable_module.get_module_state_map() {
|
|
// push_code_version(CodeVersion {
|
|
// baseline: true,
|
|
// msm: msm,
|
|
// base: instance.module.runnable_module.get_code().unwrap().as_ptr() as usize,
|
|
// backend: &backend,
|
|
// runnable_module: instance.module.runnable_module.clone(),
|
|
// });
|
|
// true
|
|
// } else {
|
|
// false
|
|
// };
|
|
|
|
// let value = add_to.call(3, 4).unwrap();
|
|
// if cv_pushed {
|
|
// pop_code_version().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);
|
|
// }
|
|
|
|
// fn middleware_test_traps_after_costly_call(backend: &'static str) {
|
|
// let wasm_binary = wat2wasm(WAT).unwrap();
|
|
|
|
// let limit = 100u64;
|
|
|
|
// let compiler = get_compiler(backend, limit);
|
|
// let module = compile_with(&wasm_binary, &compiler).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.exports.get("add_to").unwrap();
|
|
|
|
// let cv_pushed = if let Some(msm) = instance.module.runnable_module.get_module_state_map() {
|
|
// push_code_version(CodeVersion {
|
|
// baseline: true,
|
|
// msm: msm,
|
|
// base: instance.module.runnable_module.get_code().unwrap().as_ptr() as usize,
|
|
// backend: &backend,
|
|
// runnable_module: instance.module.runnable_module.clone(),
|
|
// });
|
|
// true
|
|
// } else {
|
|
// false
|
|
// };
|
|
// let result = add_to.call(10_000_000, 4);
|
|
// if cv_pushed {
|
|
// pop_code_version().unwrap();
|
|
// }
|
|
|
|
// let err = result.unwrap_err();
|
|
// assert!(err
|
|
// .0
|
|
// .downcast_ref::<ExecutionLimitExceededError>()
|
|
// .is_some());
|
|
|
|
// // 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.
|
|
// }
|