A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Scope the "power" of epoch/call hooks (#14386)
Alex Crichton5db315bdb61e
diff --git a/crates/c-api/include/wasmtime/_store_class.hh b/crates/c-api/include/wasmtime/_store_class.hhindex 199271353b..96029d106a 100644--- a/crates/c-api/include/wasmtime/_store_class.hh+++ b/crates/c-api/include/wasmtime/_store_class.hh@@ -257,11 +257,11 @@ public:private:template <typename F>static wasmtime_error_t *- raw_epoch_callback(wasmtime_context_t *context, void *data,+ raw_epoch_callback(const wasmtime_context_t *context, void *data,uint64_t *epoch_deadline_delta,wasmtime_update_deadline_kind_t *update_kind) {auto &callback = *static_cast<F *>(data);- Context ctx(context);+ Context ctx(const_cast<wasmtime_context_t *>(context));auto result = callback(ctx, *epoch_deadline_delta);if (!result) {diff --git a/crates/c-api/include/wasmtime/store.h b/crates/c-api/include/wasmtime/store.hindex 0fbbc1bd3f..668fe30bc8 100644--- a/crates/c-api/include/wasmtime/store.h+++ b/crates/c-api/include/wasmtime/store.h@@ -253,7 +253,7 @@ typedef uint8_t wasmtime_update_deadline_kind_t;*/WASM_API_EXTERN void wasmtime_store_epoch_deadline_callback(wasmtime_store_t *store,- wasmtime_error_t *(*func)(wasmtime_context_t *context, void *data,+ wasmtime_error_t *(*func)(const wasmtime_context_t *context, void *data,uint64_t *epoch_deadline_delta,wasmtime_update_deadline_kind_t *update_kind),void *data, void (*finalizer)(void *));diff --git a/crates/c-api/src/store.rs b/crates/c-api/src/store.rsindex 44f8c673ee..3b3c5140d0 100644--- a/crates/c-api/src/store.rs+++ b/crates/c-api/src/store.rs@@ -151,7 +151,7 @@ pub const WASMTIME_UPDATE_DEADLINE_YIELD: wasmtime_update_deadline_kind_t = 1;pub extern "C" fn wasmtime_store_epoch_deadline_callback(store: &mut wasmtime_store_t,func: extern "C" fn(- WasmtimeStoreContextMut<'_>,+ WasmtimeStoreContext<'_>,*mut c_void,*mut u64,*mut wasmtime_update_deadline_kind_t,@@ -160,12 +160,12 @@ pub extern "C" fn wasmtime_store_epoch_deadline_callback(finalizer: Option<extern "C" fn(*mut c_void)>,) {let foreign = crate::ForeignData { data, finalizer };- store.store.epoch_deadline_callback(move |mut store_ctx| {+ store.store.epoch_deadline_callback(move |store_ctx| {let _ = &foreign; // Move foreign into this closurelet mut delta: u64 = 0;let mut kind = WASMTIME_UPDATE_DEADLINE_CONTINUE;let result = (func)(- store_ctx.as_context_mut(),+ store_ctx.as_context(),foreign.data,&mut delta as *mut u64,&mut kind as *mut wasmtime_update_deadline_kind_t,diff --git a/crates/wasmtime/src/runtime.rs b/crates/wasmtime/src/runtime.rsindex 883f10a059..0913b117ae 100644--- a/crates/wasmtime/src/runtime.rs+++ b/crates/wasmtime/src/runtime.rs@@ -105,7 +105,8 @@ pub use resources::*;#[cfg(all(feature = "async", feature = "call-hook"))]pub use store::CallHookHandler;pub use store::{- AsContext, AsContextMut, CallHook, Store, StoreContext, StoreContextMut, UpdateDeadline,+ AsContext, AsContextMut, CallHook, Store, StoreContext, StoreContextMut, StoreHookState,+ UpdateDeadline,};pub use trap::*;pub use types::*;diff --git a/crates/wasmtime/src/runtime/store.rs b/crates/wasmtime/src/runtime/store.rsindex a0816548b8..174d522913 100644--- a/crates/wasmtime/src/runtime/store.rs+++ b/crates/wasmtime/src/runtime/store.rs@@ -242,7 +242,7 @@ pub struct StoreInner<T: 'static> {call_hook: Option<CallHookInner<T>>,#[cfg(target_has_atomic = "64")]epoch_deadline_behavior:- Option<Box<dyn FnMut(StoreContextMut<T>) -> Result<UpdateDeadline> + Send + Sync>>,+ Option<Box<dyn FnMut(StoreHookState<T>) -> Result<UpdateDeadline> + Send + Sync>>,/// The user's `T` data.///@@ -369,7 +369,7 @@ impl StoreResourceLimiter<'_> {enum CallHookInner<T: 'static> {#[cfg(feature = "call-hook")]- Sync(Box<dyn FnMut(StoreContextMut<'_, T>, CallHook) -> Result<()> + Send + Sync>),+ Sync(Box<dyn FnMut(StoreHookState<'_, T>, CallHook) -> Result<()> + Send + Sync>),#[cfg(all(feature = "async", feature = "call-hook"))]Async(Box<dyn CallHookHandler<T> + Send + Sync>),#[expect(@@ -965,7 +965,7 @@ impl<T> Store<T> {#[cfg(feature = "call-hook")]pub fn call_hook(&mut self,- hook: impl FnMut(StoreContextMut<'_, T>, CallHook) -> Result<()> + Send + Sync + 'static,+ hook: impl FnMut(StoreHookState<'_, T>, CallHook) -> Result<()> + Send + Sync + 'static,) {self.inner.call_hook = Some(CallHookInner::Sync(Box::new(hook)));}@@ -1146,7 +1146,7 @@ impl<T> Store<T> {#[cfg(target_has_atomic = "64")]pub fn epoch_deadline_callback(&mut self,- callback: impl FnMut(StoreContextMut<T>) -> Result<UpdateDeadline> + Send + Sync + 'static,+ callback: impl FnMut(StoreHookState<T>) -> Result<UpdateDeadline> + Send + Sync + 'static,) {self.inner.epoch_deadline_callback(Box::new(callback));}@@ -1432,16 +1432,18 @@ impl<T> StoreInner<T> {fn invoke_call_hook(&mut self, call_hook: &mut CallHookInner<T>, s: CallHook) -> Result<()> {match call_hook {#[cfg(feature = "call-hook")]- CallHookInner::Sync(hook) => hook((&mut *self).as_context_mut(), s),+ CallHookInner::Sync(hook) => {+ hook(StoreHookState::new((&mut *self).as_context_mut()), s)+ }#[cfg(all(feature = "async", feature = "call-hook"))]CallHookInner::Async(handler) => {if !self.can_block() {bail!("couldn't grab async_cx for call hook")}- return (&mut *self)- .as_context_mut()- .with_blocking(|store, cx| cx.block_on(handler.handle_call_event(store, s)))?;+ return (&mut *self).as_context_mut().with_blocking(|store, cx| {+ cx.block_on(handler.handle_call_event(StoreHookState::new(store), s))+ })?;}CallHookInner::ForceTypeParameterToBeUsed { uninhabited, .. } => {@@ -2354,7 +2356,7 @@ unsafe impl<T> VMStore for StoreInner<T> {// multiple times.let mut behavior = self.epoch_deadline_behavior.take();let update = match &mut behavior {- Some(callback) => callback((&mut *self).as_context_mut()),+ Some(callback) => callback(StoreHookState::new((&mut *self).as_context_mut())),None => Ok(UpdateDeadline::Interrupt),};@@ -2388,7 +2390,7 @@ impl<T> StoreInner<T> {#[cfg(target_has_atomic = "64")]fn epoch_deadline_callback(&mut self,- callback: Box<dyn FnMut(StoreContextMut<T>) -> Result<UpdateDeadline> + Send + Sync>,+ callback: Box<dyn FnMut(StoreHookState<T>) -> Result<UpdateDeadline> + Send + Sync>,) {self.epoch_deadline_behavior = Some(callback);}diff --git a/crates/wasmtime/src/runtime/store/async_.rs b/crates/wasmtime/src/runtime/store/async_.rsindex dd7cccfff4..f458234dfa 100644--- a/crates/wasmtime/src/runtime/store/async_.rs+++ b/crates/wasmtime/src/runtime/store/async_.rs@@ -13,7 +13,7 @@ use crate::{Store, StoreContextMut, UpdateDeadline};pub trait CallHookHandler<T>: Send {/// A callback to run when wasmtime is about to enter a host call, or when about to/// exit the hostcall.- async fn handle_call_event(&self, t: StoreContextMut<'_, T>, ch: CallHook) -> Result<()>;+ async fn handle_call_event(&self, t: crate::StoreHookState<'_, T>, ch: CallHook) -> Result<()>;}impl<T> Store<T> {diff --git a/crates/wasmtime/src/runtime/store/context.rs b/crates/wasmtime/src/runtime/store/context.rsindex 8b2be535d2..4191a9b66f 100644--- a/crates/wasmtime/src/runtime/store/context.rs+++ b/crates/wasmtime/src/runtime/store/context.rs@@ -221,3 +221,47 @@ impl<'a, T: AsContextMut> From<&'a mut T> for StoreContextMut<'a, T::Data> {t.as_context_mut()}}++/// State yielded to and epoch deadline callback or a call hook.+///+/// This structure is similar to [`StoreContextMut`] except that it does not+/// implement [`AsContextMut`] and instead only implements [`AsContext`]. This+/// means that the store can be inspected and read but not mutated. This+/// structure does allow mutable access to the underlying `T` within the+/// store, however.+///+/// This type is used in APIs such as [`Store::epoch_deadline_callback`] and+/// [`Store::call_hook`] where the embedder is given access to the store at+/// points in time where it's not valid to mutate WebAssembly-facing state.+/// For example when an epoch deadline happens WebAssembly is between two+/// instructions and general state in the module cannot be mutated spec-wise.+/// This type expresses this static guarantee by ensuring that store data+/// can be read, embedder-specific data can be mutated, but nothing within the+/// store otherwise can be mutated.+pub struct StoreHookState<'a, T: 'static> {+ inner: StoreContextMut<'a, T>,+}++impl<'a, T> StoreHookState<'a, T> {+ pub(crate) fn new(inner: StoreContextMut<'a, T>) -> Self {+ Self { inner }+ }++ /// Returns a shared reference to the underlying store data.+ pub fn data(&self) -> &T {+ self.inner.data()+ }++ /// Returns a mutable reference to the underlying store data.+ pub fn data_mut(&mut self) -> &mut T {+ self.inner.data_mut()+ }+}++impl<'a, T> AsContext for StoreHookState<'a, T> {+ type Data = T;++ fn as_context(&self) -> StoreContext<'_, T> {+ self.inner.as_context()+ }+}diff --git a/src/commands/run.rs b/src/commands/run.rsindex 6e32df1541..d896dc60b0 100644--- a/src/commands/run.rs+++ b/src/commands/run.rs@@ -593,7 +593,7 @@ impl RunCommand {path: &str,interval: std::time::Duration,) -> Result<Box<dyn FnOnce(&mut Store<Host>) + Send>> {- use wasmtime::{AsContext, GuestProfiler, StoreContext, StoreContextMut, UpdateDeadline};+ use wasmtime::{AsContext, GuestProfiler, StoreContext, StoreHookState, UpdateDeadline};let module_name = self.module_and_args[0].to_str().unwrap_or("<main module>");store.data_mut().guest_profiler = match main_target {@@ -613,7 +613,7 @@ impl RunCommand {};fn sample(- mut store: StoreContextMut<Host>,+ mut store: StoreHookState<Host>,f: impl FnOnce(&mut GuestProfiler, StoreContext<Host>),) {let mut profiler = store.data_mut().guest_profiler.take().unwrap();diff --git a/src/commands/serve.rs b/src/commands/serve.rsindex 07e5a28478..cc9d4961a4 100644--- a/src/commands/serve.rs+++ b/src/commands/serve.rs@@ -1249,7 +1249,7 @@ fn setup_guest_profiler()?));fn sample(- mut store: StoreContextMut<Host>,+ mut store: wasmtime::StoreHookState<Host>,f: impl FnOnce(&mut GuestProfiler, StoreContext<Host>),) {let mut profiler = store.data_mut().guest_profiler.take().unwrap();diff --git a/tests/all/call_hook.rs b/tests/all/call_hook.rsindex 1041952e42..63de3509b7 100644--- a/tests/all/call_hook.rs+++ b/tests/all/call_hook.rs@@ -552,7 +552,7 @@ async fn basic_async_hook() -> Result<(), Error> {impl CallHookHandler<State> for HandlerR {async fn handle_call_event(&self,- ctx: StoreContextMut<'_, State>,+ ctx: StoreHookState<'_, State>,ch: CallHook,) -> Result<()> {sync_call_hook(ctx, ch)@@ -628,7 +628,7 @@ async fn timeout_async_hook() -> Result<(), Error> {impl CallHookHandler<State> for HandlerR {async fn handle_call_event(&self,- mut ctx: StoreContextMut<'_, State>,+ mut ctx: StoreHookState<'_, State>,ch: CallHook,) -> Result<()> {let obj = ctx.data_mut();@@ -707,7 +707,7 @@ async fn drop_suspended_async_hook() -> Result<(), Error> {impl CallHookHandler<u32> for Handler {async fn handle_call_event(&self,- mut ctx: StoreContextMut<'_, u32>,+ mut ctx: StoreHookState<'_, u32>,_ch: CallHook,) -> Result<()> {let state = ctx.data_mut();@@ -894,7 +894,7 @@ impl State {}}-pub fn sync_call_hook(mut ctx: StoreContextMut<'_, State>, transition: CallHook) -> Result<()> {+pub fn sync_call_hook(mut ctx: StoreHookState<'_, State>, transition: CallHook) -> Result<()> {ctx.data_mut().call_hook(transition)}diff --git a/tests/all/component_model/call_hook.rs b/tests/all/component_model/call_hook.rsindex fd51fa8396..61da3b27db 100644--- a/tests/all/component_model/call_hook.rs+++ b/tests/all/component_model/call_hook.rs@@ -6,7 +6,7 @@ use std::future::Future;use std::pin::Pin;use std::task::{self, Poll};use wasmtime::component::*;-use wasmtime::{CallHook, CallHookHandler, Engine, Result, Store, StoreContextMut, bail};+use wasmtime::{CallHook, CallHookHandler, Engine, Result, Store, StoreHookState, bail};// Crate a synchronous Func, call it directly:#[test]@@ -406,7 +406,7 @@ async fn timeout_async_hook() -> Result<()> {impl CallHookHandler<State> for HandlerR {async fn handle_call_event(&self,- mut ctx: StoreContextMut<'_, State>,+ mut ctx: StoreHookState<'_, State>,ch: CallHook,) -> Result<()> {let obj = ctx.data_mut();@@ -497,7 +497,7 @@ async fn drop_suspended_async_hook() -> Result<()> {impl CallHookHandler<u32> for Handler {async fn handle_call_event(&self,- mut ctx: StoreContextMut<'_, u32>,+ mut ctx: StoreHookState<'_, u32>,_ch: CallHook,) -> Result<()> {let state = ctx.data_mut();diff --git a/tests/all/epoch_interruption.rs b/tests/all/epoch_interruption.rsindex 1d2b4664f3..cfae640851 100644--- a/tests/all/epoch_interruption.rs+++ b/tests/all/epoch_interruption.rs@@ -33,7 +33,7 @@ fn make_env<T: 'static>(engine: &Engine) -> Linker<T> {enum InterruptMode {Trap,- Callback(fn(StoreContextMut<usize>) -> Result<UpdateDeadline>),+ Callback(fn(StoreHookState<usize>) -> Result<UpdateDeadline>),Yield(u64),}@@ -496,130 +496,3 @@ async fn drop_future_on_epoch_yield(config: &mut Config) -> Result<()> {assert_eq!(true, alive_flag.load(Ordering::Acquire));Ok(())}--#[test]-fn memory_grow_in_epoch_callback() -> Result<()> {- let mut config = Config::new();- config.epoch_interruption(true);- config.memory_reservation(0);- config.memory_reservation_for_growth(0);- config.memory_guard_size(0);- config.memory_may_move(true);- config.memory_init_cow(false);- let engine = Engine::new(&config)?;- let module = Module::new(- &engine,- r#"- (module- (memory (export "mem") 1)- (func (export "go")- (memory.fill (i32.const 0) (i32.const 0x41) (i32.const 65536))))- "#,- )?;-- let mut store: Store<Option<Memory>> = Store::new(&engine, None);- store.set_epoch_deadline(1);- store.epoch_deadline_callback(move |mut cx| {- if let Some(mem) = *cx.data() {- mem.grow(&mut cx, 5)?;- }- Ok(UpdateDeadline::Continue(0))- });-- let instance = Instance::new(&mut store, &module, &[])?;- let mem = instance.get_memory(&mut store, "mem").unwrap();- *store.data_mut() = Some(mem);- engine.increment_epoch();-- instance- .get_typed_func::<(), ()>(&mut store, "go")?- .call(&mut store, ())?;-- let data = mem.data(&store);- assert_eq!(data[0], 0x41);- assert_eq!(data[65535], 0x41);- Ok(())-}--#[test]-fn table_grow_in_epoch_callback() -> Result<()> {- let mut config = Config::new();- config.epoch_interruption(true);- let engine = Engine::new(&config)?;- let module = Module::new(- &engine,- r#"- (module- (table $t (export "t") 1 funcref)- (func (export "go")- (table.fill $t (i32.const 0) (ref.null func) (i32.const 1))))- "#,- )?;-- let mut store: Store<Option<Table>> = Store::new(&engine, None);- store.set_epoch_deadline(1);- store.epoch_deadline_callback(move |mut cx| {- if let Some(t) = *cx.data() {- t.grow(&mut cx, 5, Ref::Func(None))?;- }- Ok(UpdateDeadline::Continue(0))- });-- let instance = Instance::new(&mut store, &module, &[])?;- let t = instance.get_table(&mut store, "t").unwrap();- *store.data_mut() = Some(t);- engine.increment_epoch();-- instance- .get_typed_func::<(), ()>(&mut store, "go")?- .call(&mut store, ())?;- Ok(())-}--#[test]-fn gc_during_epoch_callback() -> Result<()> {- let mut config = Config::new();- config.epoch_interruption(true);- let engine = Engine::new(&config)?;- let module = Module::new(- &engine,- r#"- (module- (type $box (struct (field i32)))- (type $arr (array (mut (ref null $box))))- (global $sink (mut (ref null $arr)) (ref.null $arr))- (func $mk (param $n i32) (result (ref $arr))- (array.new_default $arr (local.get $n)))- (func (export "run") (param $n i32) (result i32)- (local $i i32)- (block $done- (loop $l- (br_if $done (i32.ge_u (local.get $i) (i32.const 40)))- (array.fill $arr (call $mk (local.get $n)) (i32.const 0)- (struct.new $box (i32.const 7)) (local.get $n))- (global.set $sink (call $mk (i32.const 8)))- (local.set $i (i32.add (local.get $i) (i32.const 1)))- (br $l)- )- )- (i32.mul (local.get $n) (i32.const 7))))- "#,- )?;-- let mut store = Store::new(&engine, ());- store.set_epoch_deadline(1);- store.epoch_deadline_callback(|mut caller| {- caller.gc(None)?;- Ok(UpdateDeadline::Continue(0))- });- engine.increment_epoch();-- let instance = Instance::new(&mut store, &module, &[])?;- let run = instance.get_typed_func::<u32, u32>(&mut store, "run")?;- let n = 200;- for i in 0..5 {- let got = run.call(&mut store, n)?;- assert_eq!(got, 7 * n, "iteration {i} read back {got}");- }- Ok(())-}diff --git a/tests/all/gc.rs b/tests/all/gc.rsindex 65359fe1c8..944630d582 100644--- a/tests/all/gc.rs+++ b/tests/all/gc.rs@@ -4104,110 +4104,6 @@ fn winch_ref_params_and_results_across_gc() -> Result<()> {Ok(())}-#[test]-#[cfg_attr(miri, ignore)]-fn array_fill_i64_gc_during_epoch() -> Result<()> {- gc_during_epoch(- r#"- (module- (type $arr (array (mut i64)))- (type $box (struct (field i32)))- (func (export "run") (param $n i32) (result i32)- (local $a (ref null $arr)) (local $i i32) (local $s i32)- (local.set $a (array.new_default $arr (local.get $n)))- ;; Keep the collector busy so it has something to move.- (drop (struct.new $box (i32.const 1)))- (array.fill $arr (local.get $a) (i32.const 0) (i64.const 7) (local.get $n))- (block $done (loop $l- (br_if $done (i32.ge_u (local.get $i) (local.get $n)))- (local.set $s (i32.add (local.get $s)- (i32.wrap_i64 (array.get $arr (local.get $a) (local.get $i)))))- (local.set $i (i32.add (local.get $i) (i32.const 1)))- (br $l)))- (local.get $s)))- "#,- )-}--#[test]-#[cfg_attr(miri, ignore)]-fn array_new_gc_during_epoch() -> Result<()> {- gc_during_epoch(- r#"- (module- (type $box (struct (field i32)))- (type $arr (array (mut (ref null $box))))- (func (export "run") (param $n i32) (result i32)- (local $a (ref null $arr)) (local $i i32) (local $s i32)- (local.set $a (array.new $arr (struct.new $box (i32.const 7)) (local.get $n)))- (block $done (loop $l- (br_if $done (i32.ge_u (local.get $i) (local.get $n)))- (local.set $s (i32.add (local.get $s)- (struct.get $box 0 (ref.as_non_null- (array.get $arr (local.get $a) (local.get $i))))))- (local.set $i (i32.add (local.get $i) (i32.const 1)))- (br $l)))- (local.get $s)))- "#,- )-}--#[test]-#[cfg_attr(miri, ignore)]-fn array_copy_gc_during_epoch() -> Result<()> {- gc_during_epoch(- r#"- (module- (type $box (struct (field i32)))- (type $arr (array (mut (ref null $box))))- (func (export "run") (param $n i32) (result i32)- (local $a (ref null $arr)) (local $b (ref null $arr))- (local $i i32) (local $s i32)- (local.set $a (array.new_default $arr (local.get $n)))- (local.set $b (array.new_default $arr (local.get $n)))- (array.fill $arr (local.get $b) (i32.const 0)- (struct.new $box (i32.const 7)) (local.get $n))- (array.copy $arr $arr (local.get $a) (i32.const 0)- (local.get $b) (i32.const 0) (local.get $n))- (block $done (loop $l- (br_if $done (i32.ge_u (local.get $i) (local.get $n)))- (local.set $s (i32.add (local.get $s)- (struct.get $box 0 (ref.as_non_null- (array.get $arr (local.get $a) (local.get $i))))))- (local.set $i (i32.add (local.get $i) (i32.const 1)))- (br $l)))- (local.get $s)))- "#,- )-}--fn gc_during_epoch(wat: &str) -> Result<()> {- let mut config = Config::new();- config.epoch_interruption(true);- let engine = Engine::new(&config)?;- let module = Module::new(&engine, wat)?;-- let mut store = Store::new(&engine, ());- store.set_epoch_deadline(1);- store.epoch_deadline_callback(|mut caller| {- caller.gc(None)?;- Ok(UpdateDeadline::Continue(0))- });- engine.increment_epoch();-- let instance = Instance::new(&mut store, &module, &[])?;- let f = instance.get_typed_func::<u32, u32>(&mut store, "run")?;-- let n = 100;- for i in 0..5 {- match f.call(&mut store, n) {- Ok(got) => assert_eq!(got, 7 * n, "iteration {i} read back {got}"),- Err(e) => panic!("iteration {i} failed: {e:?}"),- }- }- Ok(())-}-#[test]#[cfg_attr(miri, ignore)]fn table_fill_null_barriers_ok() -> Result<()> {diff --git a/tests/all/missing_async.rs b/tests/all/missing_async.rsindex 878458f5fc..c6a5efa97c 100644--- a/tests/all/missing_async.rs+++ b/tests/all/missing_async.rs@@ -151,7 +151,7 @@ struct MyAsyncCallHook;#[async_trait::async_trait]impl CallHookHandler<()> for MyAsyncCallHook {- async fn handle_call_event(&self, _: StoreContextMut<'_, ()>, _: CallHook) -> Result<()> {+ async fn handle_call_event(&self, _: StoreHookState<'_, ()>, _: CallHook) -> Result<()> {Ok(())}}