Implement support for async functions in Wasmtime (#2434)
* Implement support for `async` functions in Wasmtime This is an implementation of [RFC 2] in Wasmtime which is to support `async`-defined host functions. At a high level support is added by executing WebAssembly code that might invoke an asynchronous host function on a separate native stack. When the host function's future is not ready we switch back to the main native stack to continue execution. There's a whole bunch of details in this commit, and it's a bit much to go over them all here in this commit message. The most important changes here are: * A new `wasmtime-fiber` crate has been written to manage the low-level details of stack-switching. Unixes use `mmap` to allocate a stack and Windows uses the native fibers implementation. We'll surely want to refactor this to move stack allocation elsewhere in the future. Fibers are intended to be relatively general with a lot of type paremters to fling values back and forth across suspension points. The whole crate is a giant wad of `unsafe` unfortunately and involves handwritten assembly with custom dwarf CFI directives to boot. Definitely deserves a close eye in review! * The `Store` type has two new methods -- `block_on` and `on_fiber` which bridge between the async and non-async worlds. Lots of unsafe fiddly bits here as we're trying to communicate context pointers between disparate portions of the code. Extra eyes and care in review is greatly appreciated. * The APIs for binding `async` functions are unfortunately pretty ugly in `Func`. This is mostly due to language limitations and compiler bugs (I believe) in Rust. Instead of `Func::wrap` we have a `Func::wrapN_async` family of methods, and we've also got a whole bunch of `Func::getN_async` methods now too. It may be worth rethinking the API of `Func` to try to make the documentation page actually grok'able. This isn't super heavily tested but the various test should suffice for engaging hopefully nearly all the infrastructure in one form or another. This is just the start though! [RFC 2]: https://github.com/bytecodealliance/rfcs/pull/2 * Add wasmtime-fiber to publish script * Save vector/float registers on ARM too. * Fix a typo * Update lock file * Implement periodically yielding with fuel consumption This commit implements APIs on `Store` to periodically yield execution of futures through the consumption of fuel. When fuel runs out a future's execution is yielded back to the caller, and then upon resumption fuel is re-injected. The goal of this is to allow cooperative multi-tasking with futures. * Fix compile without async * Save/restore the frame pointer in fiber switching Turns out this is another caller-saved register! * Simplify x86_64 fiber asm Take a leaf out of aarch64's playbook and don't have extra memory to load/store these arguments, instead leverage how `wasmtime_fiber_switch` already loads a bunch of data into registers which we can then immediately start using on a fiber's start without any extra memory accesses. * Add x86 support to wasmtime-fiber * Add ARM32 support to fiber crate * Make fiber build file probing more flexible * Use CreateFiberEx on Windows * Remove a stray no-longer-used trait declaration * Don't reach into `Caller` internals * Tweak async fuel to eventually run out. With fuel it's probably best to not provide any way to inject infinite fuel. * Fix some typos * Cleanup asm a bit * Use a shared header file to deduplicate some directives * Guarantee hidden visibility for functions * Enable gc-sections on macOS x86_64 * Add `.type` annotations for ARM * Update lock file * Fix compile error * Review comments
This commit is contained in:
@@ -9,12 +9,16 @@ repository = "https://github.com/bytecodealliance/wasmtime"
|
||||
readme = "README.md"
|
||||
edition = "2018"
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
rustdoc-args = ["--cfg", "nightlydoc"]
|
||||
|
||||
[dependencies]
|
||||
wasmtime-runtime = { path = "../runtime", version = "0.23.0" }
|
||||
wasmtime-environ = { path = "../environ", version = "0.23.0" }
|
||||
wasmtime-jit = { path = "../jit", version = "0.23.0" }
|
||||
wasmtime-cache = { path = "../cache", version = "0.23.0", optional = true }
|
||||
wasmtime-profiling = { path = "../profiling", version = "0.23.0" }
|
||||
wasmtime-fiber = { path = "../fiber", version = "0.23.0", optional = true }
|
||||
target-lexicon = { version = "0.11.0", default-features = false }
|
||||
wasmparser = "0.75"
|
||||
anyhow = "1.0.19"
|
||||
@@ -30,6 +34,7 @@ smallvec = "1.6.1"
|
||||
serde = { version = "1.0.94", features = ["derive"] }
|
||||
bincode = "1.2.1"
|
||||
indexmap = "1.6"
|
||||
paste = "1.0.3"
|
||||
|
||||
[target.'cfg(target_os = "windows")'.dependencies]
|
||||
winapi = "0.3.7"
|
||||
@@ -43,7 +48,7 @@ wasi-cap-std-sync = { path = "../wasi-common/cap-std-sync" }
|
||||
maintenance = { status = "actively-developed" }
|
||||
|
||||
[features]
|
||||
default = ['cache', 'wat', 'jitdump', 'parallel-compilation']
|
||||
default = ['async', 'cache', 'wat', 'jitdump', 'parallel-compilation']
|
||||
|
||||
# Enables experimental support for the lightbeam codegen backend, an alternative
|
||||
# to cranelift. Requires Nightly Rust currently, and this is not enabled by
|
||||
@@ -64,3 +69,7 @@ cache = ["wasmtime-cache"]
|
||||
|
||||
# Enables support for new x64 backend.
|
||||
experimental_x64 = ["wasmtime-jit/experimental_x64"]
|
||||
|
||||
# Enables support for "async stores" as well as defining host functions as
|
||||
# `async fn` and calling functions asynchronously.
|
||||
async = ["wasmtime-fiber"]
|
||||
|
||||
@@ -470,6 +470,7 @@ impl Config {
|
||||
///
|
||||
/// [docs]: https://bytecodealliance.github.io/wasmtime/cli-cache.html
|
||||
#[cfg(feature = "cache")]
|
||||
#[cfg_attr(nightlydoc, doc(cfg(feature = "cache")))]
|
||||
pub fn cache_config_load(&mut self, path: impl AsRef<Path>) -> Result<&mut Self> {
|
||||
self.cache_config = CacheConfig::from_file(Some(path.as_ref()))?;
|
||||
Ok(self)
|
||||
@@ -497,6 +498,7 @@ impl Config {
|
||||
///
|
||||
/// [docs]: https://bytecodealliance.github.io/wasmtime/cli-cache.html
|
||||
#[cfg(feature = "cache")]
|
||||
#[cfg_attr(nightlydoc, doc(cfg(feature = "cache")))]
|
||||
pub fn cache_config_load_default(&mut self) -> Result<&mut Self> {
|
||||
self.cache_config = CacheConfig::from_file(None)?;
|
||||
Ok(self)
|
||||
|
||||
@@ -5,10 +5,13 @@ use anyhow::{bail, ensure, Context as _, Result};
|
||||
use smallvec::{smallvec, SmallVec};
|
||||
use std::cmp::max;
|
||||
use std::fmt;
|
||||
use std::future::Future;
|
||||
use std::mem;
|
||||
use std::panic::{self, AssertUnwindSafe};
|
||||
use std::pin::Pin;
|
||||
use std::ptr::{self, NonNull};
|
||||
use std::rc::Weak;
|
||||
use std::task::{Context, Poll};
|
||||
use wasmtime_environ::wasm::EntityIndex;
|
||||
use wasmtime_runtime::{
|
||||
raise_user_trap, InstanceHandle, VMContext, VMFunctionBody, VMSharedSignatureIndex,
|
||||
@@ -34,6 +37,61 @@ use wasmtime_runtime::{
|
||||
/// cloning process only performs a shallow clone, so two cloned `Func`
|
||||
/// instances are equivalent in their functionality.
|
||||
///
|
||||
/// # `Func` and `async`
|
||||
///
|
||||
/// Functions from the perspective of WebAssembly are always synchronous. You
|
||||
/// might have an `async` function in Rust, however, which you'd like to make
|
||||
/// available from WebAssembly. Wasmtime supports asynchronously calling
|
||||
/// WebAssembly through native stack switching. You can get some more
|
||||
/// information about [asynchronous stores](Store::new_async), but from the
|
||||
/// perspective of `Func` it's important to know that whether or not your
|
||||
/// [`Store`] is asynchronous will dictate whether you call functions through
|
||||
/// [`Func::call`] or [`Func::call_async`] (or the wrappers such as
|
||||
/// [`Func::get0`] vs [`Func::get0_async`]).
|
||||
///
|
||||
/// Note that asynchronous function APIs here are a bit trickier than their
|
||||
/// synchronous bretheren. For example [`Func::new_async`] and
|
||||
/// [`Func::wrapN_async`](Func::wrap1_async) take explicit state parameters to
|
||||
/// allow you to close over the state in the returned future. It's recommended
|
||||
/// that you pass state via these parameters instead of through the closure's
|
||||
/// environment, which may give Rust lifetime errors. Additionally unlike
|
||||
/// synchronous functions which can all get wrapped through [`Func::wrap`]
|
||||
/// asynchronous functions need to explicitly wrap based on the number of
|
||||
/// parameters that they have (e.g. no wasm parameters gives you
|
||||
/// [`Func::wrap0_async`], one wasm parameter you'd use [`Func::wrap1_async`],
|
||||
/// etc). Be sure to consult the documentation for [`Func::wrap`] for how the
|
||||
/// wasm type signature is inferred from the Rust type signature.
|
||||
///
|
||||
/// # To `Func::call` or to `Func::getN`
|
||||
///
|
||||
/// There are four ways to call a `Func`. Half are asynchronus and half are
|
||||
/// synchronous, corresponding to the type of store you're using. Within each
|
||||
/// half you've got two choices:
|
||||
///
|
||||
/// * Dynamically typed - if you don't statically know the signature of the
|
||||
/// function that you're calling you'll be using [`Func::call`] or
|
||||
/// [`Func::call_async`]. These functions take a variable-length slice of
|
||||
/// "boxed" arguments in their [`Val`] representation. Additionally the
|
||||
/// results are returned as an owned slice of [`Val`]. These methods are not
|
||||
/// optimized due to the dynamic type checks that must occur, in addition to
|
||||
/// some dynamic allocations for where to put all the arguments. While this
|
||||
/// allows you to call all possible wasm function signatures, if you're
|
||||
/// looking for a speedier alternative you can also use...
|
||||
///
|
||||
/// * Statically typed - if you statically know the type signature of the wasm
|
||||
/// function you're calling then you can use the [`Func::getN`](Func::get1)
|
||||
/// family of functions where `N` is the number of wasm arguments the function
|
||||
/// takes. Asynchronous users can use [`Func::getN_async`](Func::get1_async).
|
||||
/// These functions will perform type validation up-front and then return a
|
||||
/// specialized closure which can be used to invoke the wasm function. This
|
||||
/// route involves no dynamic allocation and does not type-checks during
|
||||
/// runtime, so it's recommended to use this where possible for maximal speed.
|
||||
///
|
||||
/// Unfortunately a limitation of the code generation backend right now means
|
||||
/// that the statically typed `getN` methods only work with wasm functions that
|
||||
/// return 0 or 1 value. If 2 or more values are returned you'll need to use the
|
||||
/// dynamic `call` API. We hope to fix this in the future, though!
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// One way to get a `Func` is from an [`Instance`] after you've instantiated
|
||||
@@ -148,15 +206,104 @@ pub struct Func {
|
||||
export: wasmtime_runtime::ExportFunction,
|
||||
}
|
||||
|
||||
macro_rules! getters {
|
||||
($(
|
||||
$(#[$doc:meta])*
|
||||
($name:ident $(,$args:ident)*)
|
||||
)*) => ($(
|
||||
$(#[$doc])*
|
||||
macro_rules! for_each_function_signature {
|
||||
($mac:ident) => {
|
||||
$mac!(0);
|
||||
$mac!(1 A1);
|
||||
$mac!(2 A1 A2);
|
||||
$mac!(3 A1 A2 A3);
|
||||
$mac!(4 A1 A2 A3 A4);
|
||||
$mac!(5 A1 A2 A3 A4 A5);
|
||||
$mac!(6 A1 A2 A3 A4 A5 A6);
|
||||
$mac!(7 A1 A2 A3 A4 A5 A6 A7);
|
||||
$mac!(8 A1 A2 A3 A4 A5 A6 A7 A8);
|
||||
$mac!(9 A1 A2 A3 A4 A5 A6 A7 A8 A9);
|
||||
$mac!(10 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10);
|
||||
$mac!(11 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11);
|
||||
$mac!(12 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12);
|
||||
$mac!(13 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13);
|
||||
$mac!(14 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14);
|
||||
$mac!(15 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14 A15);
|
||||
$mac!(16 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14 A15 A16);
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! generate_get_methods {
|
||||
($num:tt $($args:ident)*) => (paste::paste! {
|
||||
/// Extracts a natively-callable object from this `Func`, if the
|
||||
/// signature matches.
|
||||
///
|
||||
/// See the [`Func`] structure for more documentation. Returns an error
|
||||
/// if the type parameters and return parameter provided don't match the
|
||||
/// actual function's type signature.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if this is called on a function in an asynchronous store.
|
||||
#[allow(non_snake_case)]
|
||||
pub fn $name<$($args,)* R>(&self)
|
||||
-> anyhow::Result<impl Fn($($args,)*) -> Result<R, Trap>>
|
||||
pub fn [<get $num>]<$($args,)* R>(&self)
|
||||
-> Result<impl Fn($($args,)*) -> Result<R, Trap>>
|
||||
where
|
||||
$($args: WasmTy,)*
|
||||
R: WasmTy,
|
||||
{
|
||||
assert!(!self.store().is_async(), concat!("must use `get", $num, "_async` on synchronous stores"));
|
||||
self.[<_get $num>]::<$($args,)* R>()
|
||||
}
|
||||
|
||||
/// Extracts a natively-callable object from this `Func`, if the
|
||||
/// signature matches.
|
||||
///
|
||||
/// See the [`Func`] structure for more documentation. Returns an error
|
||||
/// if the type parameters and return parameter provided don't match the
|
||||
/// actual function's type signature.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if this is called on a function in a synchronous store.
|
||||
#[allow(non_snake_case, warnings)]
|
||||
#[cfg(feature = "async")]
|
||||
#[cfg_attr(nightlydoc, doc(cfg(feature = "async")))]
|
||||
pub fn [<get $num _async>]<$($args,)* R>(&self)
|
||||
-> Result<impl Fn($($args,)*) -> WasmCall<R>>
|
||||
where
|
||||
$($args: WasmTy + 'static,)*
|
||||
R: WasmTy + 'static,
|
||||
{
|
||||
assert!(self.store().is_async(), concat!("must use `get", $num, "` on synchronous stores"));
|
||||
|
||||
// TODO: ideally we wouldn't box up the future here but could
|
||||
// instead name the future returned by `on_fiber`. Unfortunately
|
||||
// that would require a bunch of inter-future borrows which are safe
|
||||
// but gnarly to implement by hand.
|
||||
//
|
||||
// Otherwise the implementation here is pretty similar to
|
||||
// `call_async` where we're just calling the `on_fiber` method to
|
||||
// run the blocking work. Most of the goop here is juggling
|
||||
// lifetimes and making sure everything lives long enough and
|
||||
// closures have the right signatures.
|
||||
//
|
||||
// This is... less readable than ideal.
|
||||
let func = self.[<_get $num>]::<$($args,)* R>()?;
|
||||
let store = self.store().clone();
|
||||
Ok(move |$($args),*| {
|
||||
let func = func.clone();
|
||||
let store = store.clone();
|
||||
WasmCall {
|
||||
inner: Pin::from(Box::new(async move {
|
||||
match store.on_fiber(|| func($($args,)*)).await {
|
||||
Ok(result) => result,
|
||||
Err(trap) => Err(trap),
|
||||
}
|
||||
}))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Internal helper to share between `getN` and `getN_async`
|
||||
#[allow(non_snake_case)]
|
||||
fn [<_get $num>]<$($args,)* R>(&self)
|
||||
-> Result<impl Fn($($args,)*) -> Result<R, Trap> + Clone>
|
||||
where
|
||||
$($args: WasmTy,)*
|
||||
R: WasmTy,
|
||||
@@ -228,7 +375,41 @@ macro_rules! getters {
|
||||
}
|
||||
})
|
||||
}
|
||||
)*)
|
||||
})
|
||||
}
|
||||
|
||||
macro_rules! generate_wrap_async_func {
|
||||
($num:tt $($args:ident)*) => (paste::paste!{
|
||||
/// Same as [`Func::wrap`], except the closure asynchronously produces
|
||||
/// its result. For more information see the [`Func`] documentation.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// This function will panic if called with a non-asynchronous store.
|
||||
#[allow(non_snake_case)]
|
||||
#[cfg(feature = "async")]
|
||||
#[cfg_attr(nightlydoc, doc(cfg(feature = "async")))]
|
||||
pub fn [<wrap $num _async>]<T, $($args,)* R>(
|
||||
store: &Store,
|
||||
state: T,
|
||||
func: impl for<'a> Fn(Caller<'a>, &'a T, $($args),*) -> Box<dyn Future<Output = R> + 'a> + 'static,
|
||||
) -> Func
|
||||
where
|
||||
T: 'static,
|
||||
$($args: WasmTy,)*
|
||||
R: WasmRet,
|
||||
{
|
||||
assert!(store.is_async());
|
||||
Func::wrap(store, move |caller: Caller<'_>, $($args: $args),*| {
|
||||
let store = caller.store();
|
||||
let mut future = Pin::from(func(caller, &state, $($args),*));
|
||||
match store.block_on(future.as_mut()) {
|
||||
Ok(ret) => ret.into_result(),
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
impl Func {
|
||||
@@ -324,6 +505,100 @@ impl Func {
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new host-defined WebAssembly function which, when called,
|
||||
/// will run the asynchronous computation defined by `func` to completion
|
||||
/// and then return the result to WebAssembly.
|
||||
///
|
||||
/// This function is the asynchronous analogue of [`Func::new`] and much of
|
||||
/// that documentation applies to this as well. There are a few key
|
||||
/// differences (besides being asynchronous) that are worth pointing out:
|
||||
///
|
||||
/// * The state parameter `T` is passed to the provided function `F` on
|
||||
/// each invocation. This is done so you can use the state in `T` in the
|
||||
/// computation of the output future (the future can close over this
|
||||
/// value). Unfortunately due to limitations of async-in-Rust right now
|
||||
/// you **cannot** close over the captured variables in `F` itself in the
|
||||
/// returned future. This means that you likely won't close over much
|
||||
/// state in `F` and will instead use `T`.
|
||||
///
|
||||
/// * The closure here returns a *boxed* future, not something that simply
|
||||
/// implements a future. This is also unfortunately due to limitations in
|
||||
/// Rust right now.
|
||||
///
|
||||
/// Overall we're not super happy with this API signature and would love to
|
||||
/// change it to make it more ergonomic. Despite this, however, you should
|
||||
/// be able to still hook into asynchronous computations and plug them into
|
||||
/// wasm. Improvements are always welcome with PRs!
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// This function will panic if `store` is not an [asynchronous
|
||||
/// store](Store::new_async).
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// # use wasmtime::*;
|
||||
/// # fn main() -> anyhow::Result<()> {
|
||||
/// // Simulate some application-specific state as well as asynchronous
|
||||
/// // functions to query that state.
|
||||
/// struct MyDatabase {
|
||||
/// // ...
|
||||
/// }
|
||||
///
|
||||
/// impl MyDatabase {
|
||||
/// async fn get_row_count(&self) -> u32 {
|
||||
/// // ...
|
||||
/// # 100
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// let my_database = MyDatabase {
|
||||
/// // ...
|
||||
/// };
|
||||
///
|
||||
/// // Using `new_async` we can hook up into calling our async
|
||||
/// // `get_row_count` function.
|
||||
/// let store = Store::new_async(&Engine::default());
|
||||
/// let get_row_count_type = wasmtime::FuncType::new(
|
||||
/// None,
|
||||
/// Some(wasmtime::ValType::I32),
|
||||
/// );
|
||||
/// let double = Func::new_async(&store, get_row_count_type, my_database, |_, database, params, results| {
|
||||
/// Box::new(async move {
|
||||
/// let count = database.get_row_count().await;
|
||||
/// results[0] = Val::I32(count as i32);
|
||||
/// Ok(())
|
||||
/// })
|
||||
/// });
|
||||
/// // ...
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
#[cfg(feature = "async")]
|
||||
#[cfg_attr(nightlydoc, doc(cfg(feature = "async")))]
|
||||
pub fn new_async<T, F>(store: &Store, ty: FuncType, state: T, func: F) -> Func
|
||||
where
|
||||
T: 'static,
|
||||
F: for<'a> Fn(
|
||||
Caller<'a>,
|
||||
&'a T,
|
||||
&'a [Val],
|
||||
&'a mut [Val],
|
||||
) -> Box<dyn Future<Output = Result<(), Trap>> + 'a>
|
||||
+ 'static,
|
||||
{
|
||||
assert!(store.is_async());
|
||||
Func::new(store, ty, move |caller, params, results| {
|
||||
let store = caller.store();
|
||||
let mut future = Pin::from(func(caller, &state, params, results));
|
||||
match store.block_on(future.as_mut()) {
|
||||
Ok(Ok(())) => Ok(()),
|
||||
Ok(Err(trap)) | Err(trap) => Err(trap),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn from_caller_checked_anyfunc(
|
||||
store: &Store,
|
||||
anyfunc: *mut wasmtime_runtime::VMCallerCheckedAnyfunc,
|
||||
@@ -535,6 +810,8 @@ impl Func {
|
||||
func.into_func(store)
|
||||
}
|
||||
|
||||
for_each_function_signature!(generate_wrap_async_func);
|
||||
|
||||
pub(crate) fn sig_index(&self) -> VMSharedSignatureIndex {
|
||||
unsafe { self.export.anyfunc.as_ref().type_index }
|
||||
}
|
||||
@@ -579,9 +856,52 @@ impl Func {
|
||||
/// trap will occur. If a trap occurs while executing this function, then a
|
||||
/// trap will also be returned.
|
||||
///
|
||||
/// This function should not panic unless the underlying function itself
|
||||
/// # Panics
|
||||
///
|
||||
/// This function will panic if called on a function belonging to an async
|
||||
/// store. Asynchronous stores must always use `call_async`.
|
||||
/// initiates a panic.
|
||||
pub fn call(&self, params: &[Val]) -> Result<Box<[Val]>> {
|
||||
assert!(
|
||||
!self.store().is_async(),
|
||||
"must use `call_async` on asynchronous stores",
|
||||
);
|
||||
self._call(params)
|
||||
}
|
||||
|
||||
/// Invokes this function with the `params` given, returning the results
|
||||
/// asynchronously.
|
||||
///
|
||||
/// This function is the same as [`Func::call`] except that it is
|
||||
/// asynchronous. This is only compatible with [asynchronous
|
||||
/// stores](Store::new_async).
|
||||
///
|
||||
/// It's important to note that the execution of WebAssembly will happen
|
||||
/// synchronously in the `poll` method of the future returned from this
|
||||
/// function. Wasmtime does not manage its own thread pool or similar to
|
||||
/// execute WebAssembly in. Future `poll` methods are generally expected to
|
||||
/// resolve quickly, so it's recommended that you run or poll this future
|
||||
/// in a "blocking context".
|
||||
///
|
||||
/// For more information see the documentation on [asynchronous
|
||||
/// stores](Store::new_async).
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if this is called on a function in a synchronous store. This
|
||||
/// only works with functions defined within an asynchronous store.
|
||||
#[cfg(feature = "async")]
|
||||
#[cfg_attr(nightlydoc, doc(cfg(feature = "async")))]
|
||||
pub async fn call_async(&self, params: &[Val]) -> Result<Box<[Val]>> {
|
||||
assert!(
|
||||
self.store().is_async(),
|
||||
"must use `call` on synchronous stores",
|
||||
);
|
||||
let result = self.store().on_fiber(|| self._call(params)).await??;
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
fn _call(&self, params: &[Val]) -> Result<Box<[Val]>> {
|
||||
// We need to perform a dynamic check that the arguments given to us
|
||||
// match the signature of this function and are appropriate to pass to
|
||||
// this function. This involves checking to make sure we have the right
|
||||
@@ -669,127 +989,7 @@ impl Func {
|
||||
}
|
||||
}
|
||||
|
||||
getters! {
|
||||
/// Extracts a natively-callable object from this `Func`, if the
|
||||
/// signature matches.
|
||||
///
|
||||
/// See the [`Func::get1`] method for more documentation.
|
||||
(get0)
|
||||
|
||||
/// Extracts a natively-callable object from this `Func`, if the
|
||||
/// signature matches.
|
||||
///
|
||||
/// This function serves as an optimized version of the [`Func::call`]
|
||||
/// method if the type signature of a function is statically known to
|
||||
/// the program. This method is faster than `call` on a few metrics:
|
||||
///
|
||||
/// * Runtime type-checking only happens once, when this method is
|
||||
/// called.
|
||||
/// * The result values, if any, aren't boxed into a vector.
|
||||
/// * Arguments and return values don't go through boxing and unboxing.
|
||||
/// * No trampolines are used to transfer control flow to/from JIT code,
|
||||
/// instead this function jumps directly into JIT code.
|
||||
///
|
||||
/// For more information about which Rust types match up to which wasm
|
||||
/// types, see the documentation on [`Func::wrap`].
|
||||
///
|
||||
/// # Return
|
||||
///
|
||||
/// This function will return `None` if the type signature asserted
|
||||
/// statically does not match the runtime type signature. `Some`,
|
||||
/// however, will be returned if the underlying function takes one
|
||||
/// parameter of type `A` and returns the parameter `R`. Currently `R`
|
||||
/// can either be `()` (no return values) or one wasm type. At this time
|
||||
/// a multi-value return isn't supported.
|
||||
///
|
||||
/// The returned closure will always return a `Result<R, Trap>` and an
|
||||
/// `Err` is returned if a trap happens while the wasm is executing.
|
||||
(get1, A1)
|
||||
|
||||
/// Extracts a natively-callable object from this `Func`, if the
|
||||
/// signature matches.
|
||||
///
|
||||
/// See the [`Func::get1`] method for more documentation.
|
||||
(get2, A1, A2)
|
||||
|
||||
/// Extracts a natively-callable object from this `Func`, if the
|
||||
/// signature matches.
|
||||
///
|
||||
/// See the [`Func::get1`] method for more documentation.
|
||||
(get3, A1, A2, A3)
|
||||
|
||||
/// Extracts a natively-callable object from this `Func`, if the
|
||||
/// signature matches.
|
||||
///
|
||||
/// See the [`Func::get1`] method for more documentation.
|
||||
(get4, A1, A2, A3, A4)
|
||||
|
||||
/// Extracts a natively-callable object from this `Func`, if the
|
||||
/// signature matches.
|
||||
///
|
||||
/// See the [`Func::get1`] method for more documentation.
|
||||
(get5, A1, A2, A3, A4, A5)
|
||||
|
||||
/// Extracts a natively-callable object from this `Func`, if the
|
||||
/// signature matches.
|
||||
///
|
||||
/// See the [`Func::get1`] method for more documentation.
|
||||
(get6, A1, A2, A3, A4, A5, A6)
|
||||
|
||||
/// Extracts a natively-callable object from this `Func`, if the
|
||||
/// signature matches.
|
||||
///
|
||||
/// See the [`Func::get1`] method for more documentation.
|
||||
(get7, A1, A2, A3, A4, A5, A6, A7)
|
||||
|
||||
/// Extracts a natively-callable object from this `Func`, if the
|
||||
/// signature matches.
|
||||
///
|
||||
/// See the [`Func::get1`] method for more documentation.
|
||||
(get8, A1, A2, A3, A4, A5, A6, A7, A8)
|
||||
|
||||
/// Extracts a natively-callable object from this `Func`, if the
|
||||
/// signature matches.
|
||||
///
|
||||
/// See the [`Func::get1`] method for more documentation.
|
||||
(get9, A1, A2, A3, A4, A5, A6, A7, A8, A9)
|
||||
|
||||
/// Extracts a natively-callable object from this `Func`, if the
|
||||
/// signature matches.
|
||||
///
|
||||
/// See the [`Func::get1`] method for more documentation.
|
||||
(get10, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10)
|
||||
|
||||
/// Extracts a natively-callable object from this `Func`, if the
|
||||
/// signature matches.
|
||||
///
|
||||
/// See the [`Func::get1`] method for more documentation.
|
||||
(get11, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11)
|
||||
|
||||
/// Extracts a natively-callable object from this `Func`, if the
|
||||
/// signature matches.
|
||||
///
|
||||
/// See the [`Func::get1`] method for more documentation.
|
||||
(get12, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12)
|
||||
|
||||
/// Extracts a natively-callable object from this `Func`, if the
|
||||
/// signature matches.
|
||||
///
|
||||
/// See the [`Func::get1`] method for more documentation.
|
||||
(get13, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13)
|
||||
|
||||
/// Extracts a natively-callable object from this `Func`, if the
|
||||
/// signature matches.
|
||||
///
|
||||
/// See the [`Func::get1`] method for more documentation.
|
||||
(get14, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14)
|
||||
|
||||
/// Extracts a natively-callable object from this `Func`, if the
|
||||
/// signature matches.
|
||||
///
|
||||
/// See the [`Func::get1`] method for more documentation.
|
||||
(get15, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15)
|
||||
}
|
||||
for_each_function_signature!(generate_get_methods);
|
||||
|
||||
/// Get a reference to this function's store.
|
||||
pub fn store(&self) -> &Store {
|
||||
@@ -900,6 +1100,11 @@ pub unsafe trait WasmRet {
|
||||
#[doc(hidden)]
|
||||
type Abi: Copy;
|
||||
|
||||
// The "ok" version of this, meaning that which is returned if there is no
|
||||
// error.
|
||||
#[doc(hidden)]
|
||||
type Ok: WasmTy;
|
||||
|
||||
// Same as `WasmTy::compatible_with_store`.
|
||||
#[doc(hidden)]
|
||||
fn compatible_with_store<'a>(&self, store: WeakStore<'a>) -> bool;
|
||||
@@ -932,6 +1137,10 @@ pub unsafe trait WasmRet {
|
||||
// Same as `WasmTy::store_to_args`.
|
||||
#[doc(hidden)]
|
||||
unsafe fn store_to_args(abi: Self::Abi, ptr: *mut u128);
|
||||
|
||||
// Converts this result into an explicit `Result` to match on.
|
||||
#[doc(hidden)]
|
||||
fn into_result(self) -> Result<Self::Ok, Trap>;
|
||||
}
|
||||
|
||||
unsafe impl WasmTy for () {
|
||||
@@ -1335,6 +1544,7 @@ where
|
||||
T: WasmTy,
|
||||
{
|
||||
type Abi = <T as WasmTy>::Abi;
|
||||
type Ok = T;
|
||||
|
||||
#[inline]
|
||||
fn compatible_with_store<'a>(&self, store: WeakStore<'a>) -> bool {
|
||||
@@ -1369,6 +1579,11 @@ where
|
||||
unsafe fn store_to_args(abi: Self::Abi, ptr: *mut u128) {
|
||||
<Self as WasmTy>::store_to_args(abi, ptr)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn into_result(self) -> Result<T, Trap> {
|
||||
Ok(self)
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl<T> WasmRet for Result<T, Trap>
|
||||
@@ -1376,6 +1591,7 @@ where
|
||||
T: WasmTy,
|
||||
{
|
||||
type Abi = <T as WasmTy>::Abi;
|
||||
type Ok = T;
|
||||
|
||||
#[inline]
|
||||
fn compatible_with_store<'a>(&self, store: WeakStore<'a>) -> bool {
|
||||
@@ -1419,6 +1635,11 @@ where
|
||||
unsafe fn store_to_args(abi: Self::Abi, ptr: *mut u128) {
|
||||
<T as WasmTy>::store_to_args(abi, ptr);
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn into_result(self) -> Result<T, Trap> {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// Internal trait implemented for all arguments that can be passed to
|
||||
@@ -1552,9 +1773,7 @@ unsafe fn raise_cross_store_trap() -> ! {
|
||||
}
|
||||
|
||||
macro_rules! impl_into_func {
|
||||
($(
|
||||
($($args:ident)*)
|
||||
)*) => ($(
|
||||
($num:tt $($args:ident)*) => {
|
||||
// Implement for functions without a leading `&Caller` parameter,
|
||||
// delegating to the implementation below which does have the leading
|
||||
// `Caller` parameter.
|
||||
@@ -1699,27 +1918,23 @@ macro_rules! impl_into_func {
|
||||
}
|
||||
}
|
||||
}
|
||||
)*)
|
||||
}
|
||||
}
|
||||
|
||||
impl_into_func! {
|
||||
()
|
||||
(A1)
|
||||
(A1 A2)
|
||||
(A1 A2 A3)
|
||||
(A1 A2 A3 A4)
|
||||
(A1 A2 A3 A4 A5)
|
||||
(A1 A2 A3 A4 A5 A6)
|
||||
(A1 A2 A3 A4 A5 A6 A7)
|
||||
(A1 A2 A3 A4 A5 A6 A7 A8)
|
||||
(A1 A2 A3 A4 A5 A6 A7 A8 A9)
|
||||
(A1 A2 A3 A4 A5 A6 A7 A8 A9 A10)
|
||||
(A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11)
|
||||
(A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12)
|
||||
(A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13)
|
||||
(A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14)
|
||||
(A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14 A15)
|
||||
(A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14 A15 A16)
|
||||
for_each_function_signature!(impl_into_func);
|
||||
|
||||
/// Returned future from the [`Func::get1_async`] family of methods, used to
|
||||
/// represent an asynchronous call into WebAssembly.
|
||||
pub struct WasmCall<R> {
|
||||
inner: Pin<Box<dyn Future<Output = Result<R, Trap>>>>,
|
||||
}
|
||||
|
||||
impl<R> Future for WasmCall<R> {
|
||||
type Output = Result<R, Trap>;
|
||||
|
||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
self.inner.as_mut().poll(cx)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -90,10 +90,22 @@ impl Instance {
|
||||
/// see why it failed, or bubble it upwards. If you'd like to specifically
|
||||
/// check for trap errors, you can use `error.downcast::<Trap>()`.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// This function will panic if called within an asynchronous store
|
||||
/// (created with [`Store::new_async`]).
|
||||
///
|
||||
/// [inst]: https://webassembly.github.io/spec/core/exec/modules.html#exec-instantiation
|
||||
/// [issue]: https://github.com/bytecodealliance/wasmtime/issues/727
|
||||
/// [`ExternType`]: crate::ExternType
|
||||
pub fn new(store: &Store, module: &Module, imports: &[Extern]) -> Result<Instance, Error> {
|
||||
assert!(
|
||||
!store.is_async(),
|
||||
"cannot instantiate synchronously within an asynchronous store"
|
||||
);
|
||||
|
||||
// NB: this is the same code as `Instance::new_async`. It's intentionally
|
||||
// small but should be kept in sync (modulo the async bits).
|
||||
let mut i = Instantiator::new(store, module, imports)?;
|
||||
loop {
|
||||
if let Some((instance, items)) = i.step()? {
|
||||
@@ -105,6 +117,50 @@ impl Instance {
|
||||
}
|
||||
}
|
||||
|
||||
/// Same as [`Instance::new`], except for usage in [asynchronous stores].
|
||||
///
|
||||
/// For more details about this function see the documentation on
|
||||
/// [`Instance::new`]. The only difference between these two methods is that
|
||||
/// this one will asynchronously invoke the wasm start function in case it
|
||||
/// calls any imported function which is an asynchronous host function (e.g.
|
||||
/// created with [`Func::new_async`](crate::Func::new_async).
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// This function will panic if called within a non-asynchronous store
|
||||
/// (created with [`Store::new`]). This is only compatible with asynchronous
|
||||
/// stores created with [`Store::new_async`].
|
||||
///
|
||||
/// [asynchronous stores]: Store::new_async
|
||||
#[cfg(feature = "async")]
|
||||
#[cfg_attr(nightlydoc, doc(cfg(feature = "async")))]
|
||||
pub async fn new_async(
|
||||
store: &Store,
|
||||
module: &Module,
|
||||
imports: &[Extern],
|
||||
) -> Result<Instance, Error> {
|
||||
assert!(
|
||||
store.is_async(),
|
||||
"cannot instantiate asynchronously within a synchronous store"
|
||||
);
|
||||
|
||||
// NB: this is the same code as `Instance::new`. It's intentionally
|
||||
// small but should be kept in sync (modulo the async bits).
|
||||
let mut i = Instantiator::new(store, module, imports)?;
|
||||
loop {
|
||||
if let Some((instance, items)) = i.step()? {
|
||||
if instance.handle.module().start_func.is_some() {
|
||||
store
|
||||
.on_fiber(|| Instantiator::start_raw(&instance))
|
||||
.await??;
|
||||
}
|
||||
if let Some(items) = items {
|
||||
break Ok(Instance::from_wasmtime(&items, store));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn from_wasmtime(handle: &RuntimeInstance, store: &Store) -> Instance {
|
||||
Instance {
|
||||
items: handle.clone(),
|
||||
|
||||
@@ -140,6 +140,38 @@
|
||||
//! create a "wasi instance" and then add all of its items into a [`Linker`],
|
||||
//! which can then be used to instantiate a [`Module`] that uses WASI.
|
||||
//!
|
||||
//! ## Crate Features
|
||||
//!
|
||||
//! The `wasmtime` crate comes with a number of compile-time features that can
|
||||
//! be used to customize what features it supports. Some of these features are
|
||||
//! just internal details, but some affect the public API of the `wasmtime`
|
||||
//! crate. Be sure to check the API you're using to see if any crate features
|
||||
//! are enabled.
|
||||
//!
|
||||
//! * `cache` - Enabled by default, this feature adds support for wasmtime to
|
||||
//! perform internal caching of modules in a global location. This must still
|
||||
//! be enabled explicitly through [`Config::cache_config_load`] or
|
||||
//! [`Config::cache_config_load_default`].
|
||||
//!
|
||||
//! * `wat` - Enabled by default, this feature adds support for accepting the
|
||||
//! text format of WebAssembly in [`Module::new`]. The text format will be
|
||||
//! automatically recognized and translated to binary when compiling a
|
||||
//! module.
|
||||
//!
|
||||
//! * `parallel-compilation` - Enabled by default, this feature enables support
|
||||
//! for compiling functions of a module in parallel with `rayon`.
|
||||
//!
|
||||
//! * `async` - Enabled by default, this feature enables APIs and runtime
|
||||
//! support for defining asynchronous host functions and calling WebAssembly
|
||||
//! asynchronously.
|
||||
//!
|
||||
//! * `jitdump` - Enabled by default, this feature compiles in support for the
|
||||
//! jitdump runtime profilng format. The profiler can be selected with
|
||||
//! [`Config::profiler`].
|
||||
//!
|
||||
//! * `vtune` - Not enabled by default, this feature compiles in support for
|
||||
//! supporting VTune profiling of JIT code.
|
||||
//!
|
||||
//! ## Examples
|
||||
//!
|
||||
//! In addition to the examples below be sure to check out the [online embedding
|
||||
@@ -234,6 +266,8 @@
|
||||
#![deny(missing_docs, broken_intra_doc_links)]
|
||||
#![doc(test(attr(deny(warnings))))]
|
||||
#![doc(test(attr(allow(dead_code, unused_variables, unused_mut))))]
|
||||
#![cfg_attr(nightlydoc, feature(doc_cfg))]
|
||||
#![cfg_attr(not(feature = "default"), allow(dead_code, unused_imports))]
|
||||
|
||||
mod config;
|
||||
mod engine;
|
||||
|
||||
@@ -1,16 +1,20 @@
|
||||
use crate::frame_info::StoreFrameInfo;
|
||||
use crate::sig_registry::SignatureRegistry;
|
||||
use crate::trampoline::StoreInstanceHandle;
|
||||
use crate::{Engine, Module};
|
||||
use crate::{Engine, Module, Trap};
|
||||
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::future::Future;
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::pin::Pin;
|
||||
use std::ptr;
|
||||
use std::rc::{Rc, Weak};
|
||||
use std::sync::Arc;
|
||||
use std::task::{Context, Poll};
|
||||
use wasmtime_environ::wasm;
|
||||
use wasmtime_jit::{CompiledModule, ModuleCode, TypeTables};
|
||||
use wasmtime_runtime::{
|
||||
@@ -55,6 +59,7 @@ pub struct Store {
|
||||
}
|
||||
|
||||
pub(crate) struct StoreInner {
|
||||
is_async: bool,
|
||||
engine: Engine,
|
||||
interrupts: Arc<VMInterrupts>,
|
||||
signatures: RefCell<SignatureRegistry>,
|
||||
@@ -77,6 +82,21 @@ pub(crate) struct StoreInner {
|
||||
/// An adjustment to add to the fuel consumed value in `interrupts` above
|
||||
/// to get the true amount of fuel consumed.
|
||||
fuel_adj: Cell<i64>,
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
current_suspend: Cell<*const wasmtime_fiber::Suspend<Result<(), Trap>, (), Result<(), Trap>>>,
|
||||
#[cfg(feature = "async")]
|
||||
current_poll_cx: Cell<*mut Context<'static>>,
|
||||
out_of_gas_behavior: Cell<OutOfGas>,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
enum OutOfGas {
|
||||
Trap,
|
||||
InjectFuel {
|
||||
injection_count: u32,
|
||||
fuel_to_inject: u64,
|
||||
},
|
||||
}
|
||||
|
||||
struct HostInfoKey(VMExternRef);
|
||||
@@ -100,7 +120,99 @@ impl Hash for HostInfoKey {
|
||||
|
||||
impl Store {
|
||||
/// Creates a new store to be associated with the given [`Engine`].
|
||||
///
|
||||
/// Note that this `Store` cannot be used with asynchronous host calls nor
|
||||
/// can it be used to call functions asynchronously. For that you'll want to
|
||||
/// use [`Store::new_async`].
|
||||
pub fn new(engine: &Engine) -> Store {
|
||||
Store::_new(engine, false)
|
||||
}
|
||||
|
||||
/// Creates a new async store to be associated with the given [`Engine`].
|
||||
///
|
||||
/// The returned store can optionally define host functions with `async`.
|
||||
/// Instances created and functions called within the returned `Store`
|
||||
/// *must* be called through their asynchronous APIs, however. For example
|
||||
/// using [`Func::call`](crate::Func::call) will panic in the returned
|
||||
/// store.
|
||||
///
|
||||
/// # Asynchronous Wasm
|
||||
///
|
||||
/// WebAssembly does not currently have a way to specify at the bytecode
|
||||
/// level what is and isn't async. Host-defined functions, however, may be
|
||||
/// defined as `async`. WebAssembly imports always appear synchronous, which
|
||||
/// gives rise to a bit of an impedence mismatch here. To solve this
|
||||
/// Wasmtime supports "asynchronous stores" which enables calling these
|
||||
/// asynchronous functions in a way that looks synchronous to the executing
|
||||
/// WebAssembly code.
|
||||
///
|
||||
/// An asynchronous store must always invoke wasm code asynchronously,
|
||||
/// meaning we'll always represent its computation as a
|
||||
/// [`Future`](std::future::Future). The `poll` method of the futures
|
||||
/// returned by Wasmtime will perform the actual work of calling the
|
||||
/// WebAssembly. Wasmtime won't manage its own thread pools or similar,
|
||||
/// that's left up to the embedder.
|
||||
///
|
||||
/// To implement futures in a way that WebAssembly sees asynchronous host
|
||||
/// functions as synchronous, all async Wasmtime futures will execute on a
|
||||
/// separately allocated native stack from the thread otherwise executing
|
||||
/// Wasmtime. This separate native stack can then be switched to and from.
|
||||
/// Using this whenever an `async` host function returns a future that
|
||||
/// resolves to `Pending` we switch away from the temporary stack back to
|
||||
/// the main stack and propagate the `Pending` status.
|
||||
///
|
||||
/// In general it's encouraged that the integration with `async` and
|
||||
/// wasmtime is designed early on in your embedding of Wasmtime to ensure
|
||||
/// that it's planned that WebAssembly executes in the right context of your
|
||||
/// application.
|
||||
///
|
||||
/// # Execution in `poll`
|
||||
///
|
||||
/// The [`Future::poll`](std::future::Future::poll) method is the main
|
||||
/// driving force behind Rust's futures. That method's own documentation
|
||||
/// states "an implementation of `poll` should strive to return quickly, and
|
||||
/// should not block". This, however, can be at odds with executing
|
||||
/// WebAssembly code as part of the `poll` method itself. If your
|
||||
/// WebAssembly is untrusted then this could allow the `poll` method to take
|
||||
/// arbitrarily long in the worst case, likely blocking all other
|
||||
/// asynchronous tasks.
|
||||
///
|
||||
/// To remedy this situation you have a two possible ways to solve this:
|
||||
///
|
||||
/// * First you can spawn futures into a thread pool. Care must be taken for
|
||||
/// this because Wasmtime futures are not `Send` or `Sync`. If you ensure
|
||||
/// that the entire state of a `Store` is wrapped up in a single future,
|
||||
/// though, you can send the whole future at once to a separate thread. By
|
||||
/// doing this in a thread pool you are relaxing the requirement that
|
||||
/// `Future::poll` must be fast because your future is executing on a
|
||||
/// separate thread. This strategy, however, would likely still require
|
||||
/// some form of cancellation via [`Store::interrupt_handle`] to ensure
|
||||
/// wasm doesn't take *too* long to execute.
|
||||
///
|
||||
/// * Alternatively you can enable the
|
||||
/// [`Config::consume_fuel`](crate::Config::consume_fuel) method as well
|
||||
/// as [`Store::out_of_fuel_async_yield`] When doing so this will
|
||||
/// configure Wasmtime futures to yield periodically while they're
|
||||
/// executing WebAssembly code. After consuming the specified amount of
|
||||
/// fuel wasm futures will return `Poll::Pending` from their `poll`
|
||||
/// method, and will get automatically re-polled later. This enables the
|
||||
/// `Future::poll` method to take roughly a fixed amount of time since
|
||||
/// fuel is guaranteed to get consumed while wasm is executing. Note that
|
||||
/// to prevent infinite execution of wasm you'll still need to use
|
||||
/// [`Store::interrupt_handle`].
|
||||
///
|
||||
/// In either case special care needs to be taken when integrating
|
||||
/// asynchronous wasm into your application. You should carefully plan where
|
||||
/// WebAssembly will execute and what compute resources will be allotted to
|
||||
/// it. If Wasmtime doesn't support exactly what you'd like just yet, please
|
||||
/// feel free to open an issue!
|
||||
#[cfg(feature = "async")]
|
||||
#[cfg_attr(nightlydoc, doc(cfg(feature = "async")))]
|
||||
pub fn new_async(engine: &Engine) -> Store {
|
||||
Store::_new(engine, true)
|
||||
}
|
||||
|
||||
fn _new(engine: &Engine, is_async: bool) -> Store {
|
||||
// Ensure that wasmtime_runtime's signal handlers are configured. Note
|
||||
// that at the `Store` level it means we should perform this
|
||||
// once-per-thread. Platforms like Unix, however, only require this
|
||||
@@ -110,6 +222,7 @@ impl Store {
|
||||
|
||||
Store {
|
||||
inner: Rc::new(StoreInner {
|
||||
is_async,
|
||||
engine: engine.clone(),
|
||||
interrupts: Arc::new(Default::default()),
|
||||
signatures: RefCell::new(Default::default()),
|
||||
@@ -123,6 +236,11 @@ impl Store {
|
||||
memory_count: Default::default(),
|
||||
table_count: Default::default(),
|
||||
fuel_adj: Cell::new(0),
|
||||
#[cfg(feature = "async")]
|
||||
current_suspend: Cell::new(ptr::null()),
|
||||
#[cfg(feature = "async")]
|
||||
current_poll_cx: Cell::new(ptr::null_mut()),
|
||||
out_of_gas_behavior: Cell::new(OutOfGas::Trap),
|
||||
}),
|
||||
}
|
||||
}
|
||||
@@ -496,6 +614,301 @@ impl Store {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Configures a [`Store`] to generate a [`Trap`] whenever it runs out of
|
||||
/// fuel.
|
||||
///
|
||||
/// When a [`Store`] is configured to consume fuel with
|
||||
/// [`Config::consume_fuel`](crate::Config::consume_fuel) this method will
|
||||
/// configure what happens when fuel runs out. Specifically a WebAssembly
|
||||
/// trap will be raised and the current execution of WebAssembly will be
|
||||
/// aborted.
|
||||
///
|
||||
/// This is the default behavior for running out of fuel.
|
||||
pub fn out_of_fuel_trap(&self) {
|
||||
self.inner.out_of_gas_behavior.set(OutOfGas::Trap);
|
||||
}
|
||||
|
||||
/// Configures a [`Store`] to yield execution of async WebAssembly code
|
||||
/// periodically.
|
||||
///
|
||||
/// When a [`Store`] is configured to consume fuel with
|
||||
/// [`Config::consume_fuel`](crate::Config::consume_fuel) this method will
|
||||
/// configure what happens when fuel runs out. Specifically executing
|
||||
/// WebAssembly will be suspended and control will be yielded back to the
|
||||
/// caller. This is only suitable with use of [async
|
||||
/// stores](Store::new_async) because only then are futures used and yields
|
||||
/// are possible.
|
||||
///
|
||||
/// The purpose of this behavior is to ensure that futures which represent
|
||||
/// execution of WebAssembly do not execute too long inside their
|
||||
/// `Future::poll` method. This allows for some form of cooperative
|
||||
/// multitasking where WebAssembly will voluntarily yield control
|
||||
/// periodically (based on fuel consumption) back to the running thread.
|
||||
///
|
||||
/// Note that futures returned by this crate will automatically flag
|
||||
/// themselves to get re-polled if a yield happens. This means that
|
||||
/// WebAssembly will continue to execute, just after giving the host an
|
||||
/// opportunity to do something else.
|
||||
///
|
||||
/// The `fuel_to_inject` parameter indicates how much fuel should be
|
||||
/// automatically re-injected after fuel runs out. This is how much fuel
|
||||
/// will be consumed between yields of an async future.
|
||||
///
|
||||
/// The `injection_count` parameter indicates how many times this fuel will
|
||||
/// be injected. Multiplying the two parameters is the total amount of fuel
|
||||
/// this store is allowed before wasm traps.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// This method will panic if it is not called on an [async
|
||||
/// store](Store::new_async).
|
||||
pub fn out_of_fuel_async_yield(&self, injection_count: u32, fuel_to_inject: u64) {
|
||||
assert!(self.is_async());
|
||||
self.inner.out_of_gas_behavior.set(OutOfGas::InjectFuel {
|
||||
injection_count,
|
||||
fuel_to_inject,
|
||||
});
|
||||
}
|
||||
|
||||
pub(crate) fn is_async(&self) -> bool {
|
||||
self.inner.is_async
|
||||
}
|
||||
|
||||
/// Blocks on the asynchronous computation represented by `future` and
|
||||
/// produces the result here, in-line.
|
||||
///
|
||||
/// This function is designed to only work when it's currently executing on
|
||||
/// a native fiber. This fiber provides the ability for us to handle the
|
||||
/// future's `Pending` state as "jump back to whomever called the fiber in
|
||||
/// an asynchronous fashion and propagate `Pending`". This tight coupling
|
||||
/// with `on_fiber` below is what powers the asynchronicity of calling wasm.
|
||||
/// Note that the asynchronous part only applies to host functions, wasm
|
||||
/// itself never really does anything asynchronous at this time.
|
||||
///
|
||||
/// This function takes a `future` and will (appear to) synchronously wait
|
||||
/// on the result. While this function is executing it will fiber switch
|
||||
/// to-and-from the original frame calling `on_fiber` which should be a
|
||||
/// guarantee due to how async stores are configured.
|
||||
///
|
||||
/// The return value here is either the output of the future `T`, or a trap
|
||||
/// which represents that the asynchronous computation was cancelled. It is
|
||||
/// not recommended to catch the trap and try to keep executing wasm, so
|
||||
/// we've tried to liberally document this.
|
||||
#[cfg(feature = "async")]
|
||||
pub(crate) fn block_on<T>(
|
||||
&self,
|
||||
mut future: Pin<&mut dyn Future<Output = T>>,
|
||||
) -> Result<T, Trap> {
|
||||
debug_assert!(self.is_async());
|
||||
|
||||
// Take our current `Suspend` context which was configured as soon as
|
||||
// our fiber started. Note that we must load it at the front here and
|
||||
// save it on our stack frame. While we're polling the future other
|
||||
// fibers may be started for recursive computations, and the current
|
||||
// suspend context is only preserved at the edges of the fiber, not
|
||||
// during the fiber itself.
|
||||
//
|
||||
// For a little bit of extra safety we also replace the current value
|
||||
// with null to try to catch any accidental bugs on our part early.
|
||||
// This is all pretty unsafe so we're trying to be careful...
|
||||
//
|
||||
// Note that there should be a segfaulting test in `async_functions.rs`
|
||||
// if this `Reset` is removed.
|
||||
let suspend = self.inner.current_suspend.replace(ptr::null());
|
||||
let _reset = Reset(&self.inner.current_suspend, suspend);
|
||||
assert!(!suspend.is_null());
|
||||
|
||||
loop {
|
||||
let future_result = unsafe {
|
||||
let current_poll_cx = self.inner.current_poll_cx.replace(ptr::null_mut());
|
||||
let _reset = Reset(&self.inner.current_poll_cx, current_poll_cx);
|
||||
assert!(!current_poll_cx.is_null());
|
||||
future.as_mut().poll(&mut *current_poll_cx)
|
||||
};
|
||||
match future_result {
|
||||
Poll::Ready(t) => break Ok(t),
|
||||
Poll::Pending => {}
|
||||
}
|
||||
unsafe {
|
||||
(*suspend).suspend(())?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Executes a synchronous computation `func` asynchronously on a new fiber.
|
||||
///
|
||||
/// This function will convert the synchronous `func` into an asynchronous
|
||||
/// future. This is done by running `func` in a fiber on a separate native
|
||||
/// stack which can be suspended and resumed from.
|
||||
///
|
||||
/// Most of the nitty-gritty here is how we juggle the various contexts
|
||||
/// necessary to suspend the fiber later on and poll sub-futures. It's hoped
|
||||
/// that the various comments are illuminating as to what's going on here.
|
||||
#[cfg(feature = "async")]
|
||||
pub(crate) async fn on_fiber<R>(&self, func: impl FnOnce() -> R) -> Result<R, Trap> {
|
||||
debug_assert!(self.is_async());
|
||||
|
||||
// TODO: allocation of a fiber should be much more abstract where we
|
||||
// shouldn't be allocating huge stacks on every async wasm function call.
|
||||
let mut slot = None;
|
||||
let fiber = wasmtime_fiber::Fiber::new(10 * 1024 * 1024, |keep_going, suspend| {
|
||||
// First check and see if we were interrupted/dropped, and only
|
||||
// continue if we haven't been.
|
||||
keep_going?;
|
||||
|
||||
// Configure our store's suspension context for the rest of the
|
||||
// execution of this fiber. Note that a raw pointer is stored here
|
||||
// which is only valid for the duration of this closure.
|
||||
// Consequently we at least replace it with the previous value when
|
||||
// we're done. This reset is also required for correctness because
|
||||
// otherwise our value will overwrite another active fiber's value.
|
||||
// There should be a test that segfaults in `async_functions.rs` if
|
||||
// this `Replace` is removed.
|
||||
let prev = self.inner.current_suspend.replace(suspend);
|
||||
let _reset = Reset(&self.inner.current_suspend, prev);
|
||||
|
||||
slot = Some(func());
|
||||
Ok(())
|
||||
})
|
||||
.map_err(|e| Trap::from(anyhow::Error::from(e)))?;
|
||||
|
||||
// Once we have the fiber representing our synchronous computation, we
|
||||
// wrap that in a custom future implementation which does the
|
||||
// translation from the future protocol to our fiber API.
|
||||
FiberFuture { fiber, store: self }.await?;
|
||||
return Ok(slot.unwrap());
|
||||
|
||||
struct FiberFuture<'a> {
|
||||
fiber: wasmtime_fiber::Fiber<'a, Result<(), Trap>, (), Result<(), Trap>>,
|
||||
store: &'a Store,
|
||||
}
|
||||
|
||||
impl Future for FiberFuture<'_> {
|
||||
type Output = Result<(), Trap>;
|
||||
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
||||
// We need to carry over this `cx` into our fiber's runtime
|
||||
// for when it trys to poll sub-futures that are created. Doing
|
||||
// this must be done unsafely, however, since `cx` is only alive
|
||||
// for this one singular function call. Here we do a `transmute`
|
||||
// to extend the lifetime of `Context` so it can be stored in
|
||||
// our `Store`, and then we replace the current polling context
|
||||
// with this one.
|
||||
//
|
||||
// Note that the replace is done for weird situations where
|
||||
// futures might be switching contexts and there's multiple
|
||||
// wasmtime futures in a chain of futures.
|
||||
//
|
||||
// On exit from this function, though, we reset the polling
|
||||
// context back to what it was to signify that `Store` no longer
|
||||
// has access to this pointer.
|
||||
let cx =
|
||||
unsafe { std::mem::transmute::<&mut Context<'_>, *mut Context<'static>>(cx) };
|
||||
let prev = self.store.inner.current_poll_cx.replace(cx);
|
||||
let _reste = Reset(&self.store.inner.current_poll_cx, prev);
|
||||
|
||||
// After that's set up we resume execution of the fiber, which
|
||||
// may also start the fiber for the first time. This either
|
||||
// returns `Ok` saying the fiber finished (yay!) or it returns
|
||||
// `Err` with the payload passed to `suspend`, which in our case
|
||||
// is `()`. If `Err` is returned that means the fiber polled a
|
||||
// future but it said "Pending", so we propagate that here.
|
||||
match self.fiber.resume(Ok(())) {
|
||||
Ok(result) => Poll::Ready(result),
|
||||
Err(()) => Poll::Pending,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Dropping futures is pretty special in that it means the future has
|
||||
// been requested to be cancelled. Here we run the risk of dropping an
|
||||
// in-progress fiber, and if we were to do nothing then the fiber would
|
||||
// leak all its owned stack resources.
|
||||
//
|
||||
// To handle this we implement `Drop` here and, if the fiber isn't done,
|
||||
// resume execution of the fiber saying "hey please stop you're
|
||||
// interrupted". Our `Trap` created here (which has the stack trace
|
||||
// of whomever dropped us) will then get propagated in whatever called
|
||||
// `block_on`, and the idea is that the trap propagates all the way back
|
||||
// up to the original fiber start, finishing execution.
|
||||
//
|
||||
// We don't actually care about the fiber's return value here (no one's
|
||||
// around to look at it), we just assert the fiber finished to
|
||||
// completion.
|
||||
impl Drop for FiberFuture<'_> {
|
||||
fn drop(&mut self) {
|
||||
if self.fiber.done() {
|
||||
return;
|
||||
}
|
||||
let result = self.fiber.resume(Err(Trap::new("future dropped")));
|
||||
// This resumption with an error should always complete the
|
||||
// fiber. While it's technically possible for host code to catch
|
||||
// the trap and re-resume, we'd ideally like to signal that to
|
||||
// callers that they shouldn't be doing that.
|
||||
debug_assert!(result.is_ok());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Immediately raise a trap on an out-of-gas condition.
|
||||
fn out_of_gas_trap(&self) -> ! {
|
||||
#[derive(Debug)]
|
||||
struct OutOfGasError;
|
||||
|
||||
impl fmt::Display for OutOfGasError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.write_str("all fuel consumed by WebAssembly")
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for OutOfGasError {}
|
||||
unsafe {
|
||||
wasmtime_runtime::raise_lib_trap(wasmtime_runtime::Trap::User(Box::new(OutOfGasError)))
|
||||
}
|
||||
}
|
||||
|
||||
/// Yields execution to the caller on out-of-gas
|
||||
///
|
||||
/// This only works on async futures and stores, and assumes that we're
|
||||
/// executing on a fiber. This will yield execution back to the caller once
|
||||
/// and when we come back we'll continue with `fuel_to_inject` more fuel.
|
||||
#[cfg(feature = "async")]
|
||||
fn out_of_gas_yield(&self, fuel_to_inject: u64) {
|
||||
// Small future that yields once and then returns ()
|
||||
#[derive(Default)]
|
||||
struct Yield {
|
||||
yielded: bool,
|
||||
}
|
||||
|
||||
impl Future for Yield {
|
||||
type Output = ();
|
||||
|
||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
|
||||
if self.yielded {
|
||||
Poll::Ready(())
|
||||
} else {
|
||||
// Flag ourselves as yielded to return next time, and also
|
||||
// flag the waker that we're already ready to get
|
||||
// re-enqueued for another poll.
|
||||
self.yielded = true;
|
||||
cx.waker().wake_by_ref();
|
||||
Poll::Pending
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut future = Yield::default();
|
||||
match self.block_on(unsafe { Pin::new_unchecked(&mut future) }) {
|
||||
// If this finished successfully then we were resumed normally via a
|
||||
// `poll`, so inject some more fuel and keep going.
|
||||
Ok(()) => self.add_fuel(fuel_to_inject).unwrap(),
|
||||
// If the future was dropped while we were yielded, then we need to
|
||||
// clean up this fiber. Do so by raising a trap which will abort all
|
||||
// wasm and get caught on the other side to clean things up.
|
||||
Err(trap) => unsafe { wasmtime_runtime::raise_user_trap(trap.into()) },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl TrapInfo for Store {
|
||||
@@ -519,19 +932,24 @@ unsafe impl TrapInfo for Store {
|
||||
}
|
||||
|
||||
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")
|
||||
match self.inner.out_of_gas_behavior.get() {
|
||||
OutOfGas::Trap => self.out_of_gas_trap(),
|
||||
#[cfg(feature = "async")]
|
||||
OutOfGas::InjectFuel {
|
||||
injection_count,
|
||||
fuel_to_inject,
|
||||
} => {
|
||||
if injection_count == 0 {
|
||||
self.out_of_gas_trap();
|
||||
}
|
||||
self.inner.out_of_gas_behavior.set(OutOfGas::InjectFuel {
|
||||
injection_count: injection_count - 1,
|
||||
fuel_to_inject,
|
||||
});
|
||||
self.out_of_gas_yield(fuel_to_inject);
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for OutOfGas {}
|
||||
|
||||
unsafe {
|
||||
wasmtime_runtime::raise_lib_trap(wasmtime_runtime::Trap::User(Box::new(OutOfGas)))
|
||||
#[cfg(not(feature = "async"))]
|
||||
OutOfGas::InjectFuel { .. } => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -604,3 +1022,11 @@ impl Hash for ArcModuleCode {
|
||||
Arc::as_ptr(&self.0).hash(hasher)
|
||||
}
|
||||
}
|
||||
|
||||
struct Reset<'a, T: Copy>(&'a Cell<T>, T);
|
||||
|
||||
impl<T: Copy> Drop for Reset<'_, T> {
|
||||
fn drop(&mut self) {
|
||||
self.0.set(self.1);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user