Delete historical interruptable support in Wasmtime (#3925)

* Delete historical interruptable support in Wasmtime

This commit removes the `Config::interruptable` configuration along with
the `InterruptHandle` type from the `wasmtime` crate. The original
support for adding interruption to WebAssembly was added pretty early on
in the history of Wasmtime when there was no other method to prevent an
infinite loop from the host. Nowadays, however, there are alternative
methods for interruption such as fuel or epoch-based interruption.

One of the major downsides of `Config::interruptable` is that even when
it's not enabled it forces an atomic swap to happen when entering
WebAssembly code. This technically could be a non-atomic swap if the
configuration option isn't enabled but that produces even more branch-y
code on entry into WebAssembly which is already something we try to
optimize. Calling into WebAssembly is on the order of a dozens of
nanoseconds at this time and an atomic swap, even uncontended, can add
up to 5ns on some platforms.

The main goal of this PR is to remove this atomic swap on entry into
WebAssembly. This is done by removing the `Config::interruptable` field
entirely, moving all existing consumers to epochs instead which are
suitable for the same purposes. This means that the stack overflow check
is no longer entangled with the interruption check and perhaps one day
we could continue to optimize that further as well.

Some consequences of this change are:

* Epochs are now the only method of remote-thread interruption.
* There are no more Wasmtime traps that produces the `Interrupted` trap
  code, although we may wish to move future traps to this so I left it
  in place.
* The C API support for interrupt handles was also removed and bindings
  for epoch methods were added.
* Function-entry checks for interruption are a tiny bit less efficient
  since one check is performed for the stack limit and a second is
  performed for the epoch as opposed to the `Config::interruptable`
  style of bundling the stack limit and the interrupt check in one. It's
  expected though that this is likely to not really be measurable.
* The old `VMInterrupts` structure is renamed to `VMRuntimeLimits`.
This commit is contained in:
Alex Crichton
2022-03-14 15:25:11 -05:00
committed by GitHub
parent 62a6a7ab6c
commit c22033bf93
33 changed files with 293 additions and 589 deletions

View File

@@ -175,7 +175,7 @@ fn timeout_in_start() -> Result<()> {
assert_eq!(output.stdout, b"");
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("wasm trap: interrupt"),
stderr.contains("epoch deadline reached during execution"),
"bad stderr: {}",
stderr
);
@@ -196,7 +196,7 @@ fn timeout_in_invoke() -> Result<()> {
assert_eq!(output.stdout, b"");
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("wasm trap: interrupt"),
stderr.contains("epoch deadline reached during execution"),
"bad stderr: {}",
stderr
);

View File

@@ -337,7 +337,7 @@ fn table_drops_externref() -> anyhow::Result<()> {
fn gee_i_sure_hope_refcounting_is_atomic() -> anyhow::Result<()> {
let mut config = Config::new();
config.wasm_reference_types(true);
config.interruptable(true);
config.epoch_interruption(true);
let engine = Engine::new(&config)?;
let mut store = Store::new(&engine, ());
let module = Module::new(
@@ -380,14 +380,13 @@ fn gee_i_sure_hope_refcounting_is_atomic() -> anyhow::Result<()> {
let flag = Arc::new(AtomicBool::new(false));
let externref = ExternRef::new(SetFlagOnDrop(flag.clone()));
let externref2 = externref.clone();
let handle = store.interrupt_handle()?;
let child = std::thread::spawn(move || run.call(&mut store, Some(externref2)));
for _ in 0..10000 {
drop(externref.clone());
}
handle.interrupt();
engine.increment_epoch();
assert!(child.join().unwrap().is_err());
assert!(!flag.load(SeqCst));

View File

@@ -1,9 +1,11 @@
use std::sync::atomic::{AtomicUsize, Ordering::SeqCst};
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering::SeqCst};
use wasmtime::*;
fn interruptable_store() -> Store<()> {
let engine = Engine::new(Config::new().interruptable(true)).unwrap();
Store::new(&engine, ())
let engine = Engine::new(Config::new().epoch_interruption(true)).unwrap();
let mut store = Store::new(&engine, ());
store.set_epoch_deadline(1);
store
}
fn hugely_recursive_module(engine: &Engine) -> anyhow::Result<Module> {
@@ -28,9 +30,13 @@ fn loops_interruptable() -> anyhow::Result<()> {
let module = Module::new(store.engine(), r#"(func (export "loop") (loop br 0))"#)?;
let instance = Instance::new(&mut store, &module, &[])?;
let iloop = instance.get_typed_func::<(), (), _>(&mut store, "loop")?;
store.interrupt_handle()?.interrupt();
store.engine().increment_epoch();
let trap = iloop.call(&mut store, ()).unwrap_err();
assert!(trap.to_string().contains("wasm trap: interrupt"));
assert!(
trap.to_string().contains("epoch deadline reached"),
"bad message: {}",
trap
);
Ok(())
}
@@ -41,22 +47,25 @@ fn functions_interruptable() -> anyhow::Result<()> {
let func = Func::wrap(&mut store, || {});
let instance = Instance::new(&mut store, &module, &[func.into()])?;
let iloop = instance.get_typed_func::<(), (), _>(&mut store, "loop")?;
store.interrupt_handle()?.interrupt();
store.engine().increment_epoch();
let trap = iloop.call(&mut store, ()).unwrap_err();
assert!(
trap.to_string().contains("wasm trap: interrupt"),
trap.to_string().contains("epoch deadline reached"),
"{}",
trap.to_string()
);
Ok(())
}
const NUM_HITS: usize = 100_000;
#[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);
static STOP: AtomicBool = AtomicBool::new(false);
let mut store = interruptable_store();
let module = Module::new(
store.engine(),
@@ -75,24 +84,26 @@ fn loop_interrupt_from_afar() -> anyhow::Result<()> {
});
let instance = Instance::new(&mut 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()?;
// Use the engine to wait for it to enter the loop long enough and then we
// signal an interrupt happens.
let engine = store.engine().clone();
let thread = std::thread::spawn(move || {
while HITS.load(SeqCst) <= 100_000 {
while HITS.load(SeqCst) <= NUM_HITS && !STOP.load(SeqCst) {
// continue ...
}
println!("interrupting");
handle.interrupt();
engine.increment_epoch();
});
// Enter the infinitely looping function and assert that our interrupt
// handle does indeed actually interrupt the function.
let iloop = instance.get_typed_func::<(), (), _>(&mut store, "loop")?;
let trap = iloop.call(&mut store, ()).unwrap_err();
STOP.store(true, SeqCst);
thread.join().unwrap();
assert!(HITS.load(SeqCst) > NUM_HITS);
assert!(
trap.to_string().contains("wasm trap: interrupt"),
trap.to_string().contains("epoch deadline reached"),
"bad message: {}",
trap.to_string()
);
@@ -105,6 +116,8 @@ fn function_interrupt_from_afar() -> anyhow::Result<()> {
// the loop so we can count the number of loop iterations we've executed so
// far.
static HITS: AtomicUsize = AtomicUsize::new(0);
static STOP: AtomicBool = AtomicBool::new(false);
let mut store = interruptable_store();
let module = hugely_recursive_module(store.engine())?;
let func = Func::wrap(&mut store, || {
@@ -114,21 +127,23 @@ fn function_interrupt_from_afar() -> anyhow::Result<()> {
// 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 engine = store.engine().clone();
let thread = std::thread::spawn(move || {
while HITS.load(SeqCst) <= 100_000 {
while HITS.load(SeqCst) <= NUM_HITS && !STOP.load(SeqCst) {
// continue ...
}
handle.interrupt();
engine.increment_epoch();
});
// Enter the infinitely looping function and assert that our interrupt
// handle does indeed actually interrupt the function.
let iloop = instance.get_typed_func::<(), (), _>(&mut store, "loop")?;
let trap = iloop.call(&mut store, ()).unwrap_err();
STOP.store(true, SeqCst);
thread.join().unwrap();
assert!(HITS.load(SeqCst) > NUM_HITS);
assert!(
trap.to_string().contains("wasm trap: interrupt"),
trap.to_string().contains("epoch deadline reached"),
"bad message: {}",
trap.to_string()
);