Signature checking for call_indirect, integrate with wasmtime

This commit is contained in:
Jef
2019-02-08 11:46:25 +01:00
parent 2fad984a0d
commit 7e5c3c567f
9 changed files with 472 additions and 235 deletions

View File

@@ -85,32 +85,34 @@ mod op32 {
mod $name {
use super::{translate_wat, ExecutableModule};
use std::sync::Once;
lazy_static! {
static ref AS_PARAM: ExecutableModule = translate_wat(
concat!("(module (func (param i32) (result i32)
(i32.",stringify!($name)," (get_local 0))))"),
);
static ref AS_PARAM: ExecutableModule = translate_wat(concat!(
"(module (func (param i32) (result i32)
(i32.",
stringify!($name),
" (get_local 0))))"
),);
}
quickcheck! {
fn as_param(a: u32) -> bool {
AS_PARAM.execute_func::<(u32,), u32>(0, (a,)) == Ok($func(a))
}
fn lit(a: u32) -> bool {
let translated = translate_wat(&format!(concat!("
let translated = translate_wat(&format!(concat!("
(module (func (result i32)
(i32.",stringify!($name)," (i32.const {val}))))
"), val = a));
static ONCE: Once = Once::new();
ONCE.call_once(|| translated.disassemble());
translated.execute_func::<(), u32>(0, ()) == Ok($func(a))
}
}
}
}
};
}
unop_test!(clz, u32::leading_zeros);
@@ -206,32 +208,36 @@ mod op64 {
mod $name {
use super::{translate_wat, ExecutableModule};
use std::sync::Once;
lazy_static! {
static ref AS_PARAM: ExecutableModule = translate_wat(
concat!("(module (func (param i64) (result ",stringify!($out_ty),")
(i64.",stringify!($name)," (get_local 0))))"),
);
static ref AS_PARAM: ExecutableModule = translate_wat(concat!(
"(module (func (param i64) (result ",
stringify!($out_ty),
")
(i64.",
stringify!($name),
" (get_local 0))))"
),);
}
quickcheck! {
fn as_param(a: u64) -> bool {
AS_PARAM.execute_func::<(u64,), $out_ty>(0, (a,)) == Ok($func(a))
}
fn lit(a: u64) -> bool {
let translated = translate_wat(&format!(concat!("
let translated = translate_wat(&format!(concat!("
(module (func (result ",stringify!($out_ty),")
(i64.",stringify!($name)," (i64.const {val}))))
"), val = a));
static ONCE: Once = Once::new();
ONCE.call_once(|| translated.disassemble());
translated.execute_func::<(), $out_ty>(0, ()) == Ok($func(a))
}
}
}
}
};
}
unop_test!(clz, |a: u64| a.leading_zeros() as _);
@@ -1081,3 +1087,120 @@ fn bench_fibonacci_baseline(b: &mut test::Bencher) {
b.iter(|| test::black_box(fib(test::black_box(20))));
}
#[test]
fn test_recursive_factorial() {
let code = r#"
(module
(func (export "fac-rec") (param i64) (result i64)
(if (result i64) (i64.eq (get_local 0) (i64.const 0))
(then (i64.const 1))
(else
(i64.mul (get_local 0) (call 0 (i64.sub (get_local 0) (i64.const 1))))
)
)
)
)
"#;
assert_eq!(translate_wat(code).execute_func::<_, u64>(0, (25u64,)).unwrap(), 7034535277573963776u64);
}
#[test]
fn test_recursive_factorial_named() {
let code = r#"
(module
(func $fac-rec-named (export "fac-rec-named") (param $n i64) (result i64)
(if (result i64) (i64.eq (get_local $n) (i64.const 0))
(then (i64.const 1))
(else
(i64.mul
(get_local $n)
(call $fac-rec-named (i64.sub (get_local $n) (i64.const 1)))
)
)
)
)
)
"#;
assert_eq!(translate_wat(code).execute_func::<_, u64>(0, (25u64,)).unwrap(), 7034535277573963776u64);
}
#[test]
fn test_iterative_factorial() {
let code = r#"
(module
(func (export "fac-iter") (param i64) (result i64)
(local i64 i64)
(set_local 1 (get_local 0))
(set_local 2 (i64.const 1))
(block
(loop
(if
(i64.eq (get_local 1) (i64.const 0))
(then (br 2))
(else
(set_local 2 (i64.mul (get_local 1) (get_local 2)))
(set_local 1 (i64.sub (get_local 1) (i64.const 1)))
)
)
(br 0)
)
)
(get_local 2)
)
)
"#;
assert_eq!(translate_wat(code).execute_func::<_, u64>(0, (25u64,)).unwrap(), 7034535277573963776u64);
}
#[test]
fn test_iterative_factorial_named() {
let code = r#"
(module
(func (export "fac-iter-named") (param $n i64) (result i64)
(local $i i64)
(local $res i64)
(set_local $i (get_local $n))
(set_local $res (i64.const 1))
(block $done
(loop $loop
(if
(i64.eq (get_local $i) (i64.const 0))
(then (br $done))
(else
(set_local $res (i64.mul (get_local $i) (get_local $res)))
(set_local $i (i64.sub (get_local $i) (i64.const 1)))
)
)
(br $loop)
)
)
(get_local $res)
)
)
"#;
assert_eq!(translate_wat(code).execute_func::<_, u64>(0, (25u64,)).unwrap(), 7034535277573963776u64);
}
#[test]
fn test_optimized_factorial() {
let code = r#"
(module
(func (export "fac-opt") (param i64) (result i64)
(local i64)
(set_local 1 (i64.const 1))
(block
(br_if 0 (i64.lt_s (get_local 0) (i64.const 2)))
(loop
(set_local 1 (i64.mul (get_local 1) (get_local 0)))
(set_local 0 (i64.add (get_local 0) (i64.const -1)))
(br_if 0 (i64.gt_s (get_local 0) (i64.const 1)))
)
)
(get_local 1)
)
)
"#;
assert_eq!(translate_wat(code).execute_func::<_, u64>(0, (25u64,)).unwrap(), 7034535277573963776u64);
}