* Refactor wasmtime_runtime::Export Instead of an enumeration with variants that have data fields have an enumeration where each variant has a struct, and each struct has the data fields. This allows us to store the structs in the `wasmtime` API and avoid lots of `panic!` calls and various extraneous matches. * Pre-generate trampoline functions The `wasmtime` crate supports calling arbitrary function signatures in wasm code, and to do this it generates "trampoline functions" which have a known ABI that then internally convert to a particular signature's ABI and call it. These trampoline functions are currently generated on-the-fly and are cached in the global `Store` structure. This, however, is suboptimal for a few reasons: * Due to how code memory is managed each trampoline resides in its own 64kb allocation of memory. This means if you have N trampolines you're using N * 64kb of memory, which is quite a lot of overhead! * Trampolines are never free'd, even if the referencing module goes away. This is similar to #925. * Trampolines are a source of shared state which prevents `Store` from being easily thread safe. This commit refactors how trampolines are managed inside of the `wasmtime` crate and jit/runtime internals. All trampolines are now allocated in the same pass of `CodeMemory` that the main module is allocated into. A trampoline is generated per-signature in a module as well, instead of per-function. This cache of trampolines is stored directly inside of an `Instance`. Trampolines are stored based on `VMSharedSignatureIndex` so they can be looked up from the internals of the `ExportFunction` value. The `Func` API has been updated with various bits and pieces to ensure the right trampolines are registered in the right places. Overall this should ensure that all trampolines necessary are generated up-front rather than lazily. This allows us to remove the trampoline cache from the `Compiler` type, and move one step closer to making `Compiler` threadsafe for usage across multiple threads. Note that as one small caveat the `Func::wrap*` family of functions don't need to generate a trampoline at runtime, they actually generate the trampoline at compile time which gets passed in. Also in addition to shuffling a lot of code around this fixes one minor bug found in `code_memory.rs`, where `self.position` was loaded before allocation, but the allocation may push a new chunk which would cause `self.position` to be zero instead. * Pass the `SignatureRegistry` as an argument to where it's needed. This avoids the need for storing it in an `Arc`. * Ignore tramoplines for functions with lots of arguments Co-authored-by: Dan Gohman <sunfish@mozilla.com>
330 lines
9.5 KiB
Rust
330 lines
9.5 KiB
Rust
use anyhow::Result;
|
|
use std::rc::Rc;
|
|
use std::sync::atomic::{AtomicUsize, Ordering::SeqCst};
|
|
use wasmtime::{Callable, Func, FuncType, Instance, Module, Store, Trap, Val, ValType};
|
|
|
|
#[test]
|
|
fn func_constructors() {
|
|
let store = Store::default();
|
|
Func::wrap0(&store, || {});
|
|
Func::wrap1(&store, |_: i32| {});
|
|
Func::wrap2(&store, |_: i32, _: i64| {});
|
|
Func::wrap2(&store, |_: f32, _: f64| {});
|
|
Func::wrap0(&store, || -> i32 { 0 });
|
|
Func::wrap0(&store, || -> i64 { 0 });
|
|
Func::wrap0(&store, || -> f32 { 0.0 });
|
|
Func::wrap0(&store, || -> f64 { 0.0 });
|
|
|
|
Func::wrap0(&store, || -> Result<(), Trap> { loop {} });
|
|
Func::wrap0(&store, || -> Result<i32, Trap> { loop {} });
|
|
Func::wrap0(&store, || -> Result<i64, Trap> { loop {} });
|
|
Func::wrap0(&store, || -> Result<f32, Trap> { loop {} });
|
|
Func::wrap0(&store, || -> Result<f64, Trap> { loop {} });
|
|
}
|
|
|
|
#[test]
|
|
fn dtor_runs() {
|
|
static HITS: AtomicUsize = AtomicUsize::new(0);
|
|
|
|
struct A;
|
|
|
|
impl Drop for A {
|
|
fn drop(&mut self) {
|
|
HITS.fetch_add(1, SeqCst);
|
|
}
|
|
}
|
|
|
|
let store = Store::default();
|
|
let a = A;
|
|
assert_eq!(HITS.load(SeqCst), 0);
|
|
Func::wrap0(&store, move || {
|
|
drop(&a);
|
|
});
|
|
assert_eq!(HITS.load(SeqCst), 1);
|
|
}
|
|
|
|
#[test]
|
|
fn dtor_delayed() -> Result<()> {
|
|
static HITS: AtomicUsize = AtomicUsize::new(0);
|
|
|
|
struct A;
|
|
|
|
impl Drop for A {
|
|
fn drop(&mut self) {
|
|
HITS.fetch_add(1, SeqCst);
|
|
}
|
|
}
|
|
|
|
let store = Store::default();
|
|
let a = A;
|
|
let func = Func::wrap0(&store, move || drop(&a));
|
|
|
|
assert_eq!(HITS.load(SeqCst), 0);
|
|
let wasm = wat::parse_str(r#"(import "" "" (func))"#)?;
|
|
let module = Module::new(&store, &wasm)?;
|
|
let instance = Instance::new(&module, &[func.into()])?;
|
|
assert_eq!(HITS.load(SeqCst), 0);
|
|
drop(instance);
|
|
assert_eq!(HITS.load(SeqCst), 1);
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn signatures_match() {
|
|
let store = Store::default();
|
|
|
|
let f = Func::wrap0(&store, || {});
|
|
assert_eq!(f.ty().params(), &[]);
|
|
assert_eq!(f.ty().results(), &[]);
|
|
|
|
let f = Func::wrap0(&store, || -> i32 { loop {} });
|
|
assert_eq!(f.ty().params(), &[]);
|
|
assert_eq!(f.ty().results(), &[ValType::I32]);
|
|
|
|
let f = Func::wrap0(&store, || -> i64 { loop {} });
|
|
assert_eq!(f.ty().params(), &[]);
|
|
assert_eq!(f.ty().results(), &[ValType::I64]);
|
|
|
|
let f = Func::wrap0(&store, || -> f32 { loop {} });
|
|
assert_eq!(f.ty().params(), &[]);
|
|
assert_eq!(f.ty().results(), &[ValType::F32]);
|
|
|
|
let f = Func::wrap0(&store, || -> f64 { loop {} });
|
|
assert_eq!(f.ty().params(), &[]);
|
|
assert_eq!(f.ty().results(), &[ValType::F64]);
|
|
|
|
let f = Func::wrap5(&store, |_: f32, _: f64, _: i32, _: i64, _: i32| -> f64 {
|
|
loop {}
|
|
});
|
|
assert_eq!(
|
|
f.ty().params(),
|
|
&[
|
|
ValType::F32,
|
|
ValType::F64,
|
|
ValType::I32,
|
|
ValType::I64,
|
|
ValType::I32
|
|
]
|
|
);
|
|
assert_eq!(f.ty().results(), &[ValType::F64]);
|
|
}
|
|
|
|
#[test]
|
|
fn import_works() -> Result<()> {
|
|
static HITS: AtomicUsize = AtomicUsize::new(0);
|
|
|
|
let wasm = wat::parse_str(
|
|
r#"
|
|
(import "" "" (func))
|
|
(import "" "" (func (param i32) (result i32)))
|
|
(import "" "" (func (param i32) (param i64)))
|
|
(import "" "" (func (param i32 i64 i32 f32 f64)))
|
|
|
|
(func $foo
|
|
call 0
|
|
i32.const 0
|
|
call 1
|
|
i32.const 1
|
|
i32.add
|
|
i64.const 3
|
|
call 2
|
|
|
|
i32.const 100
|
|
i64.const 200
|
|
i32.const 300
|
|
f32.const 400
|
|
f64.const 500
|
|
call 3
|
|
)
|
|
(start $foo)
|
|
"#,
|
|
)?;
|
|
let store = Store::default();
|
|
let module = Module::new(&store, &wasm)?;
|
|
Instance::new(
|
|
&module,
|
|
&[
|
|
Func::wrap0(&store, || {
|
|
assert_eq!(HITS.fetch_add(1, SeqCst), 0);
|
|
})
|
|
.into(),
|
|
Func::wrap1(&store, |x: i32| -> i32 {
|
|
assert_eq!(x, 0);
|
|
assert_eq!(HITS.fetch_add(1, SeqCst), 1);
|
|
1
|
|
})
|
|
.into(),
|
|
Func::wrap2(&store, |x: i32, y: i64| {
|
|
assert_eq!(x, 2);
|
|
assert_eq!(y, 3);
|
|
assert_eq!(HITS.fetch_add(1, SeqCst), 2);
|
|
})
|
|
.into(),
|
|
Func::wrap5(&store, |a: i32, b: i64, c: i32, d: f32, e: f64| {
|
|
assert_eq!(a, 100);
|
|
assert_eq!(b, 200);
|
|
assert_eq!(c, 300);
|
|
assert_eq!(d, 400.0);
|
|
assert_eq!(e, 500.0);
|
|
assert_eq!(HITS.fetch_add(1, SeqCst), 3);
|
|
})
|
|
.into(),
|
|
],
|
|
)?;
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn trap_smoke() {
|
|
let store = Store::default();
|
|
let f = Func::wrap0(&store, || -> Result<(), Trap> { Err(Trap::new("test")) });
|
|
let err = f.call(&[]).unwrap_err();
|
|
assert_eq!(err.message(), "test");
|
|
}
|
|
|
|
#[test]
|
|
fn trap_import() -> Result<()> {
|
|
let wasm = wat::parse_str(
|
|
r#"
|
|
(import "" "" (func))
|
|
(start 0)
|
|
"#,
|
|
)?;
|
|
let store = Store::default();
|
|
let module = Module::new(&store, &wasm)?;
|
|
let trap = Instance::new(
|
|
&module,
|
|
&[Func::wrap0(&store, || -> Result<(), Trap> { Err(Trap::new("foo")) }).into()],
|
|
)
|
|
.err()
|
|
.unwrap()
|
|
.downcast::<Trap>()?;
|
|
assert_eq!(trap.message(), "foo");
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn get_from_wrapper() {
|
|
let store = Store::default();
|
|
let f = Func::wrap0(&store, || {});
|
|
assert!(f.get0::<()>().is_ok());
|
|
assert!(f.get0::<i32>().is_err());
|
|
assert!(f.get1::<(), ()>().is_ok());
|
|
assert!(f.get1::<i32, ()>().is_err());
|
|
assert!(f.get1::<i32, i32>().is_err());
|
|
assert!(f.get2::<(), (), ()>().is_ok());
|
|
assert!(f.get2::<i32, i32, ()>().is_err());
|
|
assert!(f.get2::<i32, i32, i32>().is_err());
|
|
|
|
let f = Func::wrap0(&store, || -> i32 { loop {} });
|
|
assert!(f.get0::<i32>().is_ok());
|
|
let f = Func::wrap0(&store, || -> f32 { loop {} });
|
|
assert!(f.get0::<f32>().is_ok());
|
|
let f = Func::wrap0(&store, || -> f64 { loop {} });
|
|
assert!(f.get0::<f64>().is_ok());
|
|
|
|
let f = Func::wrap1(&store, |_: i32| {});
|
|
assert!(f.get1::<i32, ()>().is_ok());
|
|
assert!(f.get1::<i64, ()>().is_err());
|
|
assert!(f.get1::<f32, ()>().is_err());
|
|
assert!(f.get1::<f64, ()>().is_err());
|
|
let f = Func::wrap1(&store, |_: i64| {});
|
|
assert!(f.get1::<i64, ()>().is_ok());
|
|
let f = Func::wrap1(&store, |_: f32| {});
|
|
assert!(f.get1::<f32, ()>().is_ok());
|
|
let f = Func::wrap1(&store, |_: f64| {});
|
|
assert!(f.get1::<f64, ()>().is_ok());
|
|
}
|
|
|
|
#[test]
|
|
fn get_from_signature() {
|
|
struct Foo;
|
|
impl Callable for Foo {
|
|
fn call(&self, _params: &[Val], _results: &mut [Val]) -> Result<(), Trap> {
|
|
panic!()
|
|
}
|
|
}
|
|
let store = Store::default();
|
|
let ty = FuncType::new(Box::new([]), Box::new([]));
|
|
let f = Func::new(&store, ty, Rc::new(Foo));
|
|
assert!(f.get0::<()>().is_ok());
|
|
assert!(f.get0::<i32>().is_err());
|
|
assert!(f.get1::<i32, ()>().is_err());
|
|
|
|
let ty = FuncType::new(Box::new([ValType::I32]), Box::new([ValType::F64]));
|
|
let f = Func::new(&store, ty, Rc::new(Foo));
|
|
assert!(f.get0::<()>().is_err());
|
|
assert!(f.get0::<i32>().is_err());
|
|
assert!(f.get1::<i32, ()>().is_err());
|
|
assert!(f.get1::<i32, f64>().is_ok());
|
|
}
|
|
|
|
#[test]
|
|
fn get_from_module() -> anyhow::Result<()> {
|
|
let store = Store::default();
|
|
let module = Module::new(
|
|
&store,
|
|
r#"
|
|
(module
|
|
(func (export "f0"))
|
|
(func (export "f1") (param i32))
|
|
(func (export "f2") (result i32)
|
|
i32.const 0)
|
|
)
|
|
|
|
"#,
|
|
)?;
|
|
let instance = Instance::new(&module, &[])?;
|
|
let f0 = instance.get_export("f0").unwrap().func().unwrap();
|
|
assert!(f0.get0::<()>().is_ok());
|
|
assert!(f0.get0::<i32>().is_err());
|
|
let f1 = instance.get_export("f1").unwrap().func().unwrap();
|
|
assert!(f1.get0::<()>().is_err());
|
|
assert!(f1.get1::<i32, ()>().is_ok());
|
|
assert!(f1.get1::<i32, f32>().is_err());
|
|
let f2 = instance.get_export("f2").unwrap().func().unwrap();
|
|
assert!(f2.get0::<()>().is_err());
|
|
assert!(f2.get0::<i32>().is_ok());
|
|
assert!(f2.get1::<i32, ()>().is_err());
|
|
assert!(f2.get1::<i32, f32>().is_err());
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn call_wrapped_func() -> Result<()> {
|
|
let store = Store::default();
|
|
let f = Func::wrap4(&store, |a: i32, b: i64, c: f32, d: f64| {
|
|
assert_eq!(a, 1);
|
|
assert_eq!(b, 2);
|
|
assert_eq!(c, 3.0);
|
|
assert_eq!(d, 4.0);
|
|
});
|
|
f.call(&[Val::I32(1), Val::I64(2), 3.0f32.into(), 4.0f64.into()])?;
|
|
f.get4::<i32, i64, f32, f64, ()>()?(1, 2, 3.0, 4.0)?;
|
|
|
|
let f = Func::wrap0(&store, || 1i32);
|
|
let results = f.call(&[])?;
|
|
assert_eq!(results.len(), 1);
|
|
assert_eq!(results[0].unwrap_i32(), 1);
|
|
assert_eq!(f.get0::<i32>()?()?, 1);
|
|
|
|
let f = Func::wrap0(&store, || 2i64);
|
|
let results = f.call(&[])?;
|
|
assert_eq!(results.len(), 1);
|
|
assert_eq!(results[0].unwrap_i64(), 2);
|
|
assert_eq!(f.get0::<i64>()?()?, 2);
|
|
|
|
let f = Func::wrap0(&store, || 3.0f32);
|
|
let results = f.call(&[])?;
|
|
assert_eq!(results.len(), 1);
|
|
assert_eq!(results[0].unwrap_f32(), 3.0);
|
|
assert_eq!(f.get0::<f32>()?()?, 3.0);
|
|
|
|
let f = Func::wrap0(&store, || 4.0f64);
|
|
let results = f.call(&[])?;
|
|
assert_eq!(results.len(), 1);
|
|
assert_eq!(results[0].unwrap_f64(), 4.0);
|
|
assert_eq!(f.get0::<f64>()?()?, 4.0);
|
|
Ok(())
|
|
}
|