Implement limiting WebAssembly execution with fuel (#2611)

* Consume fuel during function execution

This commit adds codegen infrastructure necessary to instrument wasm
code to consume fuel as it executes. Currently nothing is really done
with the fuel, but that'll come in later commits.

The focus of this commit is to implement the codegen infrastructure
necessary to consume fuel and account for fuel consumed correctly.

* Periodically check remaining fuel in wasm JIT code

This commit enables wasm code to periodically check to see if fuel has
run out. When fuel runs out an intrinsic is called which can do what it
needs to do in the result of fuel running out. For now a trap is thrown
to have at least some semantics in synchronous stores, but another
planned use for this feature is for asynchronous stores to periodically
yield back to the host based on fuel running out.

Checks for remaining fuel happen in the same locations as interrupt
checks, which is to say the start of the function as well as loop
headers.

* Improve codegen by caching `*const VMInterrupts`

The location of the shared interrupt value and fuel value is through a
double-indirection on the vmctx (load through the vmctx and then load
through that pointer). The second pointer in this chain, however, never
changes, so we can alter codegen to account for this and remove some
extraneous load instructions and hopefully reduce some register
pressure even maybe.

* Add tests fuel can abort infinite loops

* More fuzzing with fuel

Use fuel to time out modules in addition to time, using fuzz input to
figure out which.

* Update docs on trapping instructions

* Fix doc links

* Fix a fuzz test

* Change setting fuel to adding fuel

* Fix a doc link

* Squelch some rustdoc warnings
This commit is contained in:
Alex Crichton
2021-01-29 08:57:17 -06:00
committed by GitHub
parent 78f312799e
commit 0e41861662
26 changed files with 936 additions and 67 deletions

View File

@@ -137,6 +137,28 @@ impl Config {
self
}
/// Configures whether execution of WebAssembly will "consume fuel" to
/// either halt or yield execution as desired.
///
/// This option is similar in purpose to [`Config::interruptable`] where
/// you can prevent infinitely-executing WebAssembly code. The difference
/// is that this option allows deterministic execution of WebAssembly code
/// by instrumenting generated code consume fuel as it executes. When fuel
/// runs out the behavior is defined by configuration within a [`Store`],
/// and by default a trap is raised.
///
/// Note that a [`Store`] starts with no fuel, so if you enable this option
/// you'll have to be sure to pour some fuel into [`Store`] before
/// executing some code.
///
/// By default this option is `false`.
///
/// [`Store`]: crate::Store
pub fn consume_fuel(&mut self, enable: bool) -> &mut Self {
self.tunables.consume_fuel = enable;
self
}
/// Configures the maximum amount of native stack space available to
/// executing WebAssembly code.
///

View File

@@ -6,6 +6,7 @@ use anyhow::{bail, Result};
use std::any::Any;
use std::cell::{Cell, RefCell};
use std::collections::HashSet;
use std::convert::TryFrom;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::rc::{Rc, Weak};
@@ -72,6 +73,10 @@ pub(crate) struct StoreInner {
instance_count: Cell<usize>,
memory_count: Cell<usize>,
table_count: Cell<usize>,
/// An adjustment to add to the fuel consumed value in `interrupts` above
/// to get the true amount of fuel consumed.
fuel_adj: Cell<i64>,
}
struct HostInfoKey(VMExternRef);
@@ -117,6 +122,7 @@ impl Store {
instance_count: Default::default(),
memory_count: Default::default(),
table_count: Default::default(),
fuel_adj: Cell::new(0),
}),
}
}
@@ -427,6 +433,64 @@ impl Store {
);
}
}
/// Returns the amount of fuel consumed by this store's execution so far.
///
/// If fuel consumption is not enabled via
/// [`Config::consume_fuel`](crate::Config::consume_fuel) then this
/// function will return `None`. Also note that fuel, if enabled, must be
/// originally configured via [`Store::add_fuel`].
pub fn fuel_consumed(&self) -> Option<u64> {
if !self.engine().config().tunables.consume_fuel {
return None;
}
let consumed = unsafe { *self.inner.interrupts.fuel_consumed.get() };
Some(u64::try_from(self.inner.fuel_adj.get() + consumed).unwrap())
}
/// Adds fuel to this [`Store`] for wasm to consume while executing.
///
/// For this method to work fuel consumption must be enabled via
/// [`Config::consume_fuel`](crate::Config::consume_fuel). By default a
/// [`Store`] starts with 0 fuel for wasm to execute with (meaning it will
/// immediately trap). This function must be called for the store to have
/// some fuel to allow WebAssembly to execute.
///
/// Note that at this time when fuel is entirely consumed it will cause
/// wasm to trap. More usages of fuel are planned for the future.
///
/// # Panics
///
/// This function will panic if the store's [`Config`](crate::Config) did
/// not have fuel consumption enabled.
pub fn add_fuel(&self, fuel: u64) {
assert!(self.engine().config().tunables.consume_fuel);
// Fuel is stored as an i64, so we need to cast it. If the provided fuel
// value overflows that just assume that i64::max will suffice. Wasm
// execution isn't fast enough to burn through i64::max fuel in any
// reasonable amount of time anyway.
let fuel = i64::try_from(fuel).unwrap_or(i64::max_value());
let adj = self.inner.fuel_adj.get();
let consumed_ptr = unsafe { &mut *self.inner.interrupts.fuel_consumed.get() };
match (consumed_ptr.checked_sub(fuel), adj.checked_add(fuel)) {
// If we succesfully did arithmetic without overflowing then we can
// just update our fields.
(Some(consumed), Some(adj)) => {
self.inner.fuel_adj.set(adj);
*consumed_ptr = consumed;
}
// Otherwise something overflowed. Make sure that we preserve the
// amount of fuel that's already consumed, but otherwise assume that
// we were given infinite fuel.
_ => {
self.inner.fuel_adj.set(i64::max_value());
*consumed_ptr = (*consumed_ptr + adj) - i64::max_value();
}
}
}
}
unsafe impl TrapInfo for Store {
@@ -448,6 +512,23 @@ unsafe impl TrapInfo for Store {
fn max_wasm_stack(&self) -> usize {
self.engine().config().max_wasm_stack
}
fn out_of_gas(&self) {
#[derive(Debug)]
struct OutOfGas;
impl fmt::Display for OutOfGas {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("all fuel consumed by WebAssembly")
}
}
impl std::error::Error for OutOfGas {}
unsafe {
wasmtime_runtime::raise_lib_trap(wasmtime_runtime::Trap::User(Box::new(OutOfGas)))
}
}
}
impl Default for Store {
@@ -481,6 +562,13 @@ pub struct InterruptHandle {
interrupts: Arc<VMInterrupts>,
}
// The `VMInterrupts` type is a pod-type with no destructor, and we only access
// `interrupts` from other threads, so add in these trait impls which are
// otherwise not available due to the `fuel_consumed` variable in
// `VMInterrupts`.
unsafe impl Send for InterruptHandle {}
unsafe impl Sync for InterruptHandle {}
impl InterruptHandle {
/// Flags that execution within this handle's original [`Store`] should be
/// interrupted.