This commit is intended to be the first of many in implementing the module linking proposal. At this time this builds on #2059 so it shouldn't land yet. The goal of this commit is to compile bare-bones modules which use module linking, e.g. those with nested modules. My hope with module linking is that almost everything in wasmtime only needs mild refactorings to handle it. The goal is that all per-module structures are still per-module and at the top level there's just a `Vec` containing a bunch of modules. That's implemented currently where `wasmtime::Module` contains `Arc<[CompiledModule]>` and an index of which one it's pointing to. This should enable serialization/deserialization of any module in a nested modules scenario, no matter how you got it. Tons of features of the module linking proposal are missing from this commit. For example instantiation flat out doesn't work, nor does import/export of modules or instances. That'll be coming as future commits, but the purpose here is to start laying groundwork in Wasmtime for handling lots of modules in lots of places.
137 lines
4.3 KiB
Rust
137 lines
4.3 KiB
Rust
use std::sync::atomic::{AtomicUsize, Ordering::SeqCst};
|
|
use wasmtime::*;
|
|
|
|
fn interruptable_store() -> Store {
|
|
let engine = Engine::new(Config::new().interruptable(true));
|
|
Store::new(&engine)
|
|
}
|
|
|
|
fn hugely_recursive_module(store: &Store) -> anyhow::Result<Module> {
|
|
let mut wat = String::new();
|
|
wat.push_str(
|
|
r#"
|
|
(import "" "" (func))
|
|
(func (export "loop") call 2 call 2)
|
|
"#,
|
|
);
|
|
for i in 0..100 {
|
|
wat.push_str(&format!("(func call {0} call {0})\n", i + 3));
|
|
}
|
|
wat.push_str("(func call 0)\n");
|
|
|
|
Module::new(store.engine(), &wat)
|
|
}
|
|
|
|
#[test]
|
|
fn loops_interruptable() -> anyhow::Result<()> {
|
|
let store = interruptable_store();
|
|
let module = Module::new(store.engine(), r#"(func (export "loop") (loop br 0))"#)?;
|
|
let instance = Instance::new(&store, &module, &[])?;
|
|
let iloop = instance.get_func("loop").unwrap().get0::<()>()?;
|
|
store.interrupt_handle()?.interrupt();
|
|
let trap = iloop().unwrap_err();
|
|
assert!(trap.to_string().contains("wasm trap: interrupt"));
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn functions_interruptable() -> anyhow::Result<()> {
|
|
let store = interruptable_store();
|
|
let module = hugely_recursive_module(&store)?;
|
|
let func = Func::wrap(&store, || {});
|
|
let instance = Instance::new(&store, &module, &[func.into()])?;
|
|
let iloop = instance.get_func("loop").unwrap().get0::<()>()?;
|
|
store.interrupt_handle()?.interrupt();
|
|
let trap = iloop().unwrap_err();
|
|
assert!(
|
|
trap.to_string().contains("wasm trap: interrupt"),
|
|
"{}",
|
|
trap.to_string()
|
|
);
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn loop_interrupt_from_afar() -> anyhow::Result<()> {
|
|
// Create an instance which calls an imported function on each iteration of
|
|
// the loop so we can count the number of loop iterations we've executed so
|
|
// far.
|
|
static HITS: AtomicUsize = AtomicUsize::new(0);
|
|
let store = interruptable_store();
|
|
let module = Module::new(
|
|
store.engine(),
|
|
r#"
|
|
(import "" "" (func))
|
|
|
|
(func (export "loop")
|
|
(loop
|
|
call 0
|
|
br 0)
|
|
)
|
|
"#,
|
|
)?;
|
|
let func = Func::wrap(&store, || {
|
|
HITS.fetch_add(1, SeqCst);
|
|
});
|
|
let instance = Instance::new(&store, &module, &[func.into()])?;
|
|
|
|
// Use the instance's interrupt handle to wait for it to enter the loop long
|
|
// enough and then we signal an interrupt happens.
|
|
let handle = store.interrupt_handle()?;
|
|
let thread = std::thread::spawn(move || {
|
|
while HITS.load(SeqCst) <= 100_000 {
|
|
// continue ...
|
|
}
|
|
println!("interrupting");
|
|
handle.interrupt();
|
|
});
|
|
|
|
// Enter the infinitely looping function and assert that our interrupt
|
|
// handle does indeed actually interrupt the function.
|
|
let iloop = instance.get_func("loop").unwrap().get0::<()>()?;
|
|
let trap = iloop().unwrap_err();
|
|
thread.join().unwrap();
|
|
assert!(
|
|
trap.to_string().contains("wasm trap: interrupt"),
|
|
"bad message: {}",
|
|
trap.to_string()
|
|
);
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn function_interrupt_from_afar() -> anyhow::Result<()> {
|
|
// Create an instance which calls an imported function on each iteration of
|
|
// the loop so we can count the number of loop iterations we've executed so
|
|
// far.
|
|
static HITS: AtomicUsize = AtomicUsize::new(0);
|
|
let store = interruptable_store();
|
|
let module = hugely_recursive_module(&store)?;
|
|
let func = Func::wrap(&store, || {
|
|
HITS.fetch_add(1, SeqCst);
|
|
});
|
|
let instance = Instance::new(&store, &module, &[func.into()])?;
|
|
|
|
// Use the instance's interrupt handle to wait for it to enter the loop long
|
|
// enough and then we signal an interrupt happens.
|
|
let handle = store.interrupt_handle()?;
|
|
let thread = std::thread::spawn(move || {
|
|
while HITS.load(SeqCst) <= 100_000 {
|
|
// continue ...
|
|
}
|
|
handle.interrupt();
|
|
});
|
|
|
|
// Enter the infinitely looping function and assert that our interrupt
|
|
// handle does indeed actually interrupt the function.
|
|
let iloop = instance.get_func("loop").unwrap().get0::<()>()?;
|
|
let trap = iloop().unwrap_err();
|
|
thread.join().unwrap();
|
|
assert!(
|
|
trap.to_string().contains("wasm trap: interrupt"),
|
|
"bad message: {}",
|
|
trap.to_string()
|
|
);
|
|
Ok(())
|
|
}
|