1 //! Wasmtime's "store" type
2 //!
3 //! This module, and its submodules, contain the `Store` type and various types
4 //! used to interact with it. At first glance this is a pretty confusing module
5 //! where you need to know the difference between:
6 //!
7 //! * `Store<T>`
8 //! * `StoreContext<T>`
9 //! * `StoreContextMut<T>`
10 //! * `AsContext`
11 //! * `AsContextMut`
12 //! * `StoreInner<T>`
13 //! * `StoreOpaque`
14 //! * `StoreData`
15 //!
16 //! There's... quite a lot going on here, and it's easy to be confused. This
17 //! comment is ideally going to serve the purpose of clarifying what all these
18 //! types are for and why they're motivated.
19 //!
20 //! First it's important to know what's "internal" and what's "external". Almost
21 //! everything above is defined as `pub`, but only some of the items are
22 //! reexported to the outside world to be usable from this crate. Otherwise all
23 //! items are `pub` within this `store` module, and the `store` module is
24 //! private to the `wasmtime` crate. Notably `Store<T>`, `StoreContext<T>`,
25 //! `StoreContextMut<T>`, `AsContext`, and `AsContextMut` are all public
26 //! interfaces to the `wasmtime` crate. You can think of these as:
27 //!
28 //! * `Store<T>` - an owned reference to a store, the "root of everything"
29 //! * `StoreContext<T>` - basically `&StoreInner<T>`
30 //! * `StoreContextMut<T>` - more-or-less `&mut StoreInner<T>` with caveats.
31 //!   Explained later.
32 //! * `AsContext` - similar to `AsRef`, but produces `StoreContext<T>`
33 //! * `AsContextMut` - similar to `AsMut`, but produces `StoreContextMut<T>`
34 //!
35 //! Next comes the internal structure of the `Store<T>` itself. This looks like:
36 //!
37 //! * `Store<T>` - this type is just a pointer large. It's primarily just
38 //!   intended to be consumed by the outside world. Note that the "just a
39 //!   pointer large" is a load-bearing implementation detail in Wasmtime. This
40 //!   enables it to store a pointer to its own trait object which doesn't need
41 //!   to change over time.
42 //!
43 //! * `StoreInner<T>` - the first layer of the contents of a `Store<T>`, what's
44 //!   stored inside the `Box`. This is the general Rust pattern when one struct
45 //!   is a layer over another. The surprising part, though, is that this is
46 //!   further subdivided. This structure only contains things which actually
47 //!   need `T` itself. The downside of this structure is that it's always
48 //!   generic and means that code is monomorphized into consumer crates. We
49 //!   strive to have things be as monomorphic as possible in `wasmtime` so this
50 //!   type is not heavily used.
51 //!
52 //! * `StoreOpaque` - this is the primary contents of the `StoreInner<T>` type.
53 //!   Stored inline in the outer type the "opaque" here means that it's a
54 //!   "store" but it doesn't have access to the `T`. This is the primary
55 //!   "internal" reference that Wasmtime uses since `T` is rarely needed by the
56 //!   internals of Wasmtime.
57 //!
58 //! * `StoreData` - this is a final helper struct stored within `StoreOpaque`.
59 //!   All references of Wasm items into a `Store` are actually indices into a
60 //!   table in this structure, and the `StoreData` being separate makes it a bit
61 //!   easier to manage/define/work with. There's no real fundamental reason this
62 //!   is split out, although sometimes it's useful to have separate borrows into
63 //!   these tables than the `StoreOpaque`.
64 //!
65 //! A major caveat with these representations is that the internal `&mut
66 //! StoreInner<T>` is never handed out publicly to consumers of this crate, only
67 //! through a wrapper of `StoreContextMut<'_, T>`. The reason for this is that
68 //! we want to provide mutable, but not destructive, access to the contents of a
69 //! `Store`. For example if a `StoreInner<T>` were replaced with some other
70 //! `StoreInner<T>` then that would drop live instances, possibly those
71 //! currently executing beneath the current stack frame. This would not be a
72 //! safe operation.
73 //!
74 //! This means, though, that the `wasmtime` crate, which liberally uses `&mut
75 //! StoreOpaque` internally, has to be careful to never actually destroy the
76 //! contents of `StoreOpaque`. This is an invariant that we, as the authors of
77 //! `wasmtime`, must uphold for the public interface to be safe.
78 
79 #[cfg(all(feature = "gc", feature = "debug"))]
80 use crate::OwnedRooted;
81 use crate::RootSet;
82 #[cfg(feature = "gc")]
83 use crate::ThrownException;
84 use crate::error::OutOfMemory;
85 #[cfg(feature = "async")]
86 use crate::fiber;
87 use crate::module::RegisteredModuleId;
88 use crate::prelude::*;
89 #[cfg(feature = "gc")]
90 use crate::runtime::vm::GcRootsList;
91 #[cfg(feature = "stack-switching")]
92 use crate::runtime::vm::VMContRef;
93 use crate::runtime::vm::mpk::ProtectionKey;
94 use crate::runtime::vm::{
95     self, ExportMemory, GcStore, Imports, InstanceAllocationRequest, InstanceAllocator,
96     InstanceHandle, Interpreter, InterpreterRef, ModuleRuntimeInfo, OnDemandInstanceAllocator,
97     SendSyncPtr, SignalHandler, StoreBox, Unwind, VMContext, VMFuncRef, VMGcRef, VMStore,
98     VMStoreContext,
99 };
100 use crate::trampoline::VMHostGlobalContext;
101 #[cfg(feature = "debug")]
102 use crate::{BreakpointState, DebugHandler, FrameDataCache};
103 use crate::{Engine, Module, Val, ValRaw, module::ModuleRegistry};
104 #[cfg(feature = "gc")]
105 use crate::{ExnRef, Rooted};
106 use crate::{Global, Instance, Table};
107 use core::convert::Infallible;
108 use core::fmt;
109 use core::marker;
110 use core::mem::{self, ManuallyDrop, MaybeUninit};
111 use core::num::NonZeroU64;
112 use core::ops::{Deref, DerefMut};
113 use core::pin::Pin;
114 use core::ptr::NonNull;
115 use wasmtime_environ::{DefinedGlobalIndex, DefinedTableIndex, EntityRef, PrimaryMap, TripleExt};
116 
117 mod context;
118 pub use self::context::*;
119 mod data;
120 pub use self::data::*;
121 mod func_refs;
122 use func_refs::FuncRefs;
123 #[cfg(feature = "component-model-async")]
124 mod token;
125 #[cfg(feature = "component-model-async")]
126 pub(crate) use token::StoreToken;
127 #[cfg(feature = "async")]
128 mod async_;
129 #[cfg(all(feature = "async", feature = "call-hook"))]
130 pub use self::async_::CallHookHandler;
131 
132 #[cfg(feature = "gc")]
133 use super::vm::VMExnRef;
134 #[cfg(feature = "gc")]
135 mod gc;
136 
137 /// A [`Store`] is a collection of WebAssembly instances and host-defined state.
138 ///
139 /// All WebAssembly instances and items will be attached to and refer to a
140 /// [`Store`]. For example instances, functions, globals, and tables are all
141 /// attached to a [`Store`]. Instances are created by instantiating a
142 /// [`Module`](crate::Module) within a [`Store`].
143 ///
144 /// A [`Store`] is intended to be a short-lived object in a program. No form
145 /// of GC is implemented at this time so once an instance is created within a
146 /// [`Store`] it will not be deallocated until the [`Store`] itself is dropped.
147 /// This makes [`Store`] unsuitable for creating an unbounded number of
148 /// instances in it because [`Store`] will never release this memory. It's
149 /// recommended to have a [`Store`] correspond roughly to the lifetime of a
150 /// "main instance" that an embedding is interested in executing.
151 ///
152 /// ## Type parameter `T`
153 ///
154 /// Each [`Store`] has a type parameter `T` associated with it. This `T`
155 /// represents state defined by the host. This state will be accessible through
156 /// the [`Caller`](crate::Caller) type that host-defined functions get access
157 /// to. This `T` is suitable for storing `Store`-specific information which
158 /// imported functions may want access to.
159 ///
160 /// The data `T` can be accessed through methods like [`Store::data`] and
161 /// [`Store::data_mut`].
162 ///
163 /// ## Stores, contexts, oh my
164 ///
165 /// Most methods in Wasmtime take something of the form
166 /// [`AsContext`](crate::AsContext) or [`AsContextMut`](crate::AsContextMut) as
167 /// the first argument. These two traits allow ergonomically passing in the
168 /// context you currently have to any method. The primary two sources of
169 /// contexts are:
170 ///
171 /// * `Store<T>`
172 /// * `Caller<'_, T>`
173 ///
174 /// corresponding to what you create and what you have access to in a host
175 /// function. You can also explicitly acquire a [`StoreContext`] or
176 /// [`StoreContextMut`] and pass that around as well.
177 ///
178 /// Note that all methods on [`Store`] are mirrored onto [`StoreContext`],
179 /// [`StoreContextMut`], and [`Caller`](crate::Caller). This way no matter what
180 /// form of context you have you can call various methods, create objects, etc.
181 ///
182 /// ## Stores and `Default`
183 ///
184 /// You can create a store with default configuration settings using
185 /// `Store::default()`. This will create a brand new [`Engine`] with default
186 /// configuration (see [`Config`](crate::Config) for more information).
187 ///
188 /// ## Cross-store usage of items
189 ///
190 /// In `wasmtime` wasm items such as [`Global`] and [`Memory`] "belong" to a
191 /// [`Store`]. The store they belong to is the one they were created with
192 /// (passed in as a parameter) or instantiated with. This store is the only
193 /// store that can be used to interact with wasm items after they're created.
194 ///
195 /// The `wasmtime` crate will panic if the [`Store`] argument passed in to these
196 /// operations is incorrect. In other words it's considered a programmer error
197 /// rather than a recoverable error for the wrong [`Store`] to be used when
198 /// calling APIs.
199 ///
200 /// [`Memory`]: crate::Memory
201 pub struct Store<T: 'static> {
202     // for comments about `ManuallyDrop`, see `Store::into_data`
203     inner: ManuallyDrop<Box<StoreInner<T>>>,
204 }
205 
206 #[derive(Copy, Clone, Debug)]
207 /// Passed to the argument of [`Store::call_hook`] to indicate a state transition in
208 /// the WebAssembly VM.
209 pub enum CallHook {
210     /// Indicates the VM is calling a WebAssembly function, from the host.
211     CallingWasm,
212     /// Indicates the VM is returning from a WebAssembly function, to the host.
213     ReturningFromWasm,
214     /// Indicates the VM is calling a host function, from WebAssembly.
215     CallingHost,
216     /// Indicates the VM is returning from a host function, to WebAssembly.
217     ReturningFromHost,
218 }
219 
220 impl CallHook {
221     /// Indicates the VM is entering host code (exiting WebAssembly code)
222     pub fn entering_host(&self) -> bool {
223         match self {
224             CallHook::ReturningFromWasm | CallHook::CallingHost => true,
225             _ => false,
226         }
227     }
228     /// Indicates the VM is exiting host code (entering WebAssembly code)
229     pub fn exiting_host(&self) -> bool {
230         match self {
231             CallHook::ReturningFromHost | CallHook::CallingWasm => true,
232             _ => false,
233         }
234     }
235 }
236 
237 /// Internal contents of a `Store<T>` that live on the heap.
238 ///
239 /// The members of this struct are those that need to be generic over `T`, the
240 /// store's internal type storage. Otherwise all things that don't rely on `T`
241 /// should go into `StoreOpaque`.
242 pub struct StoreInner<T: 'static> {
243     /// Generic metadata about the store that doesn't need access to `T`.
244     inner: StoreOpaque,
245 
246     limiter: Option<ResourceLimiterInner<T>>,
247     call_hook: Option<CallHookInner<T>>,
248     #[cfg(target_has_atomic = "64")]
249     epoch_deadline_behavior:
250         Option<Box<dyn FnMut(StoreContextMut<T>) -> Result<UpdateDeadline> + Send + Sync>>,
251 
252     /// The user's `T` data.
253     ///
254     /// Don't actually access it via this field, however! Use the
255     /// `Store{,Inner,Context,ContextMut}::data[_mut]` methods instead, to
256     /// preserve stacked borrows and provenance in the face of potential
257     /// direct-access of `T` from Wasm code (via unsafe intrinsics).
258     ///
259     /// The only exception to the above is when taking ownership of the value,
260     /// e.g. in `Store::into_data`, after which nothing can access this field
261     /// via raw pointers anymore so there is no more provenance to preserve.
262     ///
263     /// For comments about `ManuallyDrop`, see `Store::into_data`.
264     data_no_provenance: ManuallyDrop<T>,
265 
266     /// The user's debug handler, if any. See [`crate::DebugHandler`]
267     /// for more documentation.
268     ///
269     /// We need this to be an `Arc` because the handler itself takes
270     /// `&self` and also the whole Store mutably (via
271     /// `StoreContextMut`); so we need to hold a separate reference to
272     /// it while invoking it.
273     #[cfg(feature = "debug")]
274     debug_handler: Option<Box<dyn StoreDebugHandler<T>>>,
275 }
276 
277 /// Adapter around `DebugHandler` that gets monomorphized into an
278 /// object-safe dyn trait to place in `store.debug_handler`.
279 #[cfg(feature = "debug")]
280 trait StoreDebugHandler<T: 'static>: Send + Sync {
281     fn handle<'a>(
282         self: Box<Self>,
283         store: StoreContextMut<'a, T>,
284         event: crate::DebugEvent<'a>,
285     ) -> Box<dyn Future<Output = ()> + Send + 'a>;
286 }
287 
288 #[cfg(feature = "debug")]
289 impl<D> StoreDebugHandler<D::Data> for D
290 where
291     D: DebugHandler,
292     D::Data: Send,
293 {
294     fn handle<'a>(
295         self: Box<Self>,
296         store: StoreContextMut<'a, D::Data>,
297         event: crate::DebugEvent<'a>,
298     ) -> Box<dyn Future<Output = ()> + Send + 'a> {
299         // Clone the underlying `DebugHandler` (the trait requires
300         // Clone as a supertrait), not the Box. The clone happens here
301         // rather than at the callsite because `Clone::clone` is not
302         // object-safe so needs to be in a monomorphized context.
303         let handler: D = (*self).clone();
304         // Since we temporarily took `self` off the store at the
305         // callsite, put it back now that we've cloned it.
306         store.0.debug_handler = Some(self);
307         Box::new(async move { handler.handle(store, event).await })
308     }
309 }
310 
311 enum ResourceLimiterInner<T> {
312     Sync(Box<dyn (FnMut(&mut T) -> &mut dyn crate::ResourceLimiter) + Send + Sync>),
313     #[cfg(feature = "async")]
314     Async(Box<dyn (FnMut(&mut T) -> &mut dyn crate::ResourceLimiterAsync) + Send + Sync>),
315 }
316 
317 /// Representation of a configured resource limiter for a store.
318 ///
319 /// This is acquired with `resource_limiter_and_store_opaque` for example and is
320 /// threaded through to growth operations on tables/memories. Note that this is
321 /// passed around as `Option<&mut StoreResourceLimiter<'_>>` to make it
322 /// efficient to pass around (nullable pointer) and it's also notably passed
323 /// around as an `Option` to represent how this is optionally specified within a
324 /// store.
325 pub enum StoreResourceLimiter<'a> {
326     Sync(&'a mut dyn crate::ResourceLimiter),
327     #[cfg(feature = "async")]
328     Async(&'a mut dyn crate::ResourceLimiterAsync),
329 }
330 
331 impl StoreResourceLimiter<'_> {
332     pub(crate) async fn memory_growing(
333         &mut self,
334         current: usize,
335         desired: usize,
336         maximum: Option<usize>,
337     ) -> Result<bool, Error> {
338         match self {
339             Self::Sync(s) => s.memory_growing(current, desired, maximum),
340             #[cfg(feature = "async")]
341             Self::Async(s) => s.memory_growing(current, desired, maximum).await,
342         }
343     }
344 
345     pub(crate) fn memory_grow_failed(&mut self, error: crate::Error) -> Result<()> {
346         match self {
347             Self::Sync(s) => s.memory_grow_failed(error),
348             #[cfg(feature = "async")]
349             Self::Async(s) => s.memory_grow_failed(error),
350         }
351     }
352 
353     pub(crate) async fn table_growing(
354         &mut self,
355         current: usize,
356         desired: usize,
357         maximum: Option<usize>,
358     ) -> Result<bool, Error> {
359         match self {
360             Self::Sync(s) => s.table_growing(current, desired, maximum),
361             #[cfg(feature = "async")]
362             Self::Async(s) => s.table_growing(current, desired, maximum).await,
363         }
364     }
365 
366     pub(crate) fn table_grow_failed(&mut self, error: crate::Error) -> Result<()> {
367         match self {
368             Self::Sync(s) => s.table_grow_failed(error),
369             #[cfg(feature = "async")]
370             Self::Async(s) => s.table_grow_failed(error),
371         }
372     }
373 }
374 
375 enum CallHookInner<T: 'static> {
376     #[cfg(feature = "call-hook")]
377     Sync(Box<dyn FnMut(StoreContextMut<'_, T>, CallHook) -> Result<()> + Send + Sync>),
378     #[cfg(all(feature = "async", feature = "call-hook"))]
379     Async(Box<dyn CallHookHandler<T> + Send + Sync>),
380     #[expect(
381         dead_code,
382         reason = "forcing, regardless of cfg, the type param to be used"
383     )]
384     ForceTypeParameterToBeUsed {
385         uninhabited: Infallible,
386         _marker: marker::PhantomData<T>,
387     },
388 }
389 
390 /// What to do after returning from a callback when the engine epoch reaches
391 /// the deadline for a Store during execution of a function using that store.
392 #[non_exhaustive]
393 pub enum UpdateDeadline {
394     /// Halt execution of WebAssembly, don't update the epoch deadline, and
395     /// raise a trap.
396     Interrupt,
397     /// Extend the deadline by the specified number of ticks.
398     Continue(u64),
399     /// Extend the deadline by the specified number of ticks after yielding to
400     /// the async executor loop.
401     ///
402     /// This can only be used when WebAssembly is invoked with `*_async`
403     /// methods. If WebAssembly was invoked with a synchronous method then
404     /// returning this variant will raise a trap.
405     #[cfg(feature = "async")]
406     Yield(u64),
407     /// Extend the deadline by the specified number of ticks after yielding to
408     /// the async executor loop.
409     ///
410     /// This can only be used when WebAssembly is invoked with `*_async`
411     /// methods. If WebAssembly was invoked with a synchronous method then
412     /// returning this variant will raise a trap.
413     ///
414     /// The yield will be performed by the future provided; when using `tokio`
415     /// it is recommended to provide [`tokio::task::yield_now`](https://docs.rs/tokio/latest/tokio/task/fn.yield_now.html)
416     /// here.
417     #[cfg(feature = "async")]
418     YieldCustom(
419         u64,
420         ::core::pin::Pin<Box<dyn ::core::future::Future<Output = ()> + Send>>,
421     ),
422 }
423 
424 // Forward methods on `StoreOpaque` to also being on `StoreInner<T>`
425 impl<T> Deref for StoreInner<T> {
426     type Target = StoreOpaque;
427     fn deref(&self) -> &Self::Target {
428         &self.inner
429     }
430 }
431 
432 impl<T> DerefMut for StoreInner<T> {
433     fn deref_mut(&mut self) -> &mut Self::Target {
434         &mut self.inner
435     }
436 }
437 
438 /// Monomorphic storage for a `Store<T>`.
439 ///
440 /// This structure contains the bulk of the metadata about a `Store`. This is
441 /// used internally in Wasmtime when dependence on the `T` of `Store<T>` isn't
442 /// necessary, allowing code to be monomorphic and compiled into the `wasmtime`
443 /// crate itself.
444 pub struct StoreOpaque {
445     // This `StoreOpaque` structure has references to itself. These aren't
446     // immediately evident, however, so we need to tell the compiler that it
447     // contains self-references. This notably suppresses `noalias` annotations
448     // when this shows up in compiled code because types of this structure do
449     // indeed alias itself. An example of this is `default_callee` holds a
450     // `*mut dyn Store` to the address of this `StoreOpaque` itself, indeed
451     // aliasing!
452     //
453     // It's somewhat unclear to me at this time if this is 100% sufficient to
454     // get all the right codegen in all the right places. For example does
455     // `Store` need to internally contain a `Pin<Box<StoreInner<T>>>`? Do the
456     // contexts need to contain `Pin<&mut StoreInner<T>>`? I'm not familiar
457     // enough with `Pin` to understand if it's appropriate here (we do, for
458     // example want to allow movement in and out of `data: T`, just not movement
459     // of most of the other members). It's also not clear if using `Pin` in a
460     // few places buys us much other than a bunch of `unsafe` that we already
461     // sort of hand-wave away.
462     //
463     // In any case this seems like a good mid-ground for now where we're at
464     // least telling the compiler something about all the aliasing happening
465     // within a `Store`.
466     _marker: marker::PhantomPinned,
467 
468     engine: Engine,
469     vm_store_context: VMStoreContext,
470 
471     // Contains all continuations ever allocated throughout the lifetime of this
472     // store.
473     #[cfg(feature = "stack-switching")]
474     continuations: Vec<Box<VMContRef>>,
475 
476     instances: wasmtime_environ::collections::PrimaryMap<InstanceId, StoreInstance>,
477 
478     signal_handler: Option<SignalHandler>,
479     modules: ModuleRegistry,
480     func_refs: FuncRefs,
481     host_globals: PrimaryMap<DefinedGlobalIndex, StoreBox<VMHostGlobalContext>>,
482     // GC-related fields.
483     gc_store: Option<GcStore>,
484     gc_roots: RootSet,
485     #[cfg(feature = "gc")]
486     gc_roots_list: GcRootsList,
487     // Types for which the embedder has created an allocator for.
488     #[cfg(feature = "gc")]
489     gc_host_alloc_types: crate::hash_set::HashSet<crate::type_registry::RegisteredType>,
490     /// Pending exception, if any. This is also a GC root, because it
491     /// needs to be rooted somewhere between the time that a pending
492     /// exception is set and the time that the handling code takes the
493     /// exception object. We use this rooting strategy rather than a
494     /// root in an `Err` branch of a `Result` on the host side because
495     /// it is less error-prone with respect to rooting behavior. See
496     /// `throw()`, `take_pending_exception()`,
497     /// `peek_pending_exception()`, `has_pending_exception()`, and
498     /// `catch()`.
499     #[cfg(feature = "gc")]
500     pending_exception: Option<VMExnRef>,
501 
502     // Numbers of resources instantiated in this store, and their limits
503     instance_count: usize,
504     instance_limit: usize,
505     memory_count: usize,
506     memory_limit: usize,
507     table_count: usize,
508     table_limit: usize,
509     #[cfg(feature = "async")]
510     async_state: fiber::AsyncState,
511 
512     // If fuel_yield_interval is enabled, then we store the remaining fuel (that isn't in
513     // runtime_limits) here. The total amount of fuel is the runtime limits and reserve added
514     // together. Then when we run out of gas, we inject the yield amount from the reserve
515     // until the reserve is empty.
516     fuel_reserve: u64,
517     pub(crate) fuel_yield_interval: Option<NonZeroU64>,
518     /// Indexed data within this `Store`, used to store information about
519     /// globals, functions, memories, etc.
520     store_data: StoreData,
521     traitobj: StorePtr,
522     default_caller_vmctx: SendSyncPtr<VMContext>,
523 
524     /// Used to optimized wasm->host calls when the host function is defined with
525     /// `Func::new` to avoid allocating a new vector each time a function is
526     /// called.
527     hostcall_val_storage: Vec<Val>,
528     /// Same as `hostcall_val_storage`, but for the direction of the host
529     /// calling wasm.
530     wasm_val_raw_storage: Vec<ValRaw>,
531 
532     /// Keep track of what protection key is being used during allocation so
533     /// that the right memory pages can be enabled when entering WebAssembly
534     /// guest code.
535     pkey: Option<ProtectionKey>,
536 
537     /// State related to the executor of wasm code.
538     ///
539     /// For example if Pulley is enabled and configured then this will store a
540     /// Pulley interpreter.
541     executor: Executor,
542 
543     /// The debug breakpoint state for this store.
544     ///
545     /// When guest debugging is enabled, a given store may have a set
546     /// of breakpoints defined, denoted by module and Wasm PC within
547     /// that module. Or alternately, it may be in "single-step" mode,
548     /// where every possible breakpoint is logically enabled.
549     ///
550     /// When execution of any instance in this store hits any defined
551     /// breakpoint, a `Breakpoint` debug event is emitted and the
552     /// handler defined above, if any, has a chance to perform some
553     /// logic before returning to allow execution to resume.
554     #[cfg(feature = "debug")]
555     breakpoints: BreakpointState,
556 
557     /// The debug PC-to-FrameData cache for this store.
558     ///
559     /// When guest debugging is enabled, we parse compiler metadata
560     /// and pass out `FrameHandle`s that represent Wasm guest
561     /// frames. These handles represent a specific frame within a
562     /// frozen stack and are invalidated upon further execution. In
563     /// order to keep these handles lightweight, and to avoid
564     /// redundant work when passing out *new* handles after further
565     /// execution, we cache the mapping from store-specific PCs to
566     /// parsed frame data. (This cache needs to be store-specific
567     /// rather than e.g. engine-specific because each store has its
568     /// own privately mapped copy of guest code when debugging is
569     /// enabled, so the key-space is unique for each store.)
570     #[cfg(feature = "debug")]
571     frame_data_cache: FrameDataCache,
572 }
573 
574 /// Self-pointer to `StoreInner<T>` from within a `StoreOpaque` which is chiefly
575 /// used to copy into instances during instantiation.
576 ///
577 /// FIXME: ideally this type would get deleted and Wasmtime's reliance on it
578 /// would go away.
579 struct StorePtr(Option<NonNull<dyn VMStore>>);
580 
581 // We can't make `VMStore: Send + Sync` because that requires making all of
582 // Wastime's internals generic over the `Store`'s `T`. So instead, we take care
583 // in the whole VM layer to only use the `VMStore` in ways that are `Send`- and
584 // `Sync`-safe and we have to have these unsafe impls.
585 unsafe impl Send for StorePtr {}
586 unsafe impl Sync for StorePtr {}
587 
588 /// Executor state within `StoreOpaque`.
589 ///
590 /// Effectively stores Pulley interpreter state and handles conditional support
591 /// for Cranelift at compile time.
592 pub(crate) enum Executor {
593     Interpreter(Interpreter),
594     #[cfg(has_host_compiler_backend)]
595     Native,
596 }
597 
598 impl Executor {
599     pub(crate) fn new(engine: &Engine) -> Result<Self, OutOfMemory> {
600         #[cfg(has_host_compiler_backend)]
601         if cfg!(feature = "pulley") && engine.target().is_pulley() {
602             Ok(Executor::Interpreter(Interpreter::new(engine)?))
603         } else {
604             Ok(Executor::Native)
605         }
606         #[cfg(not(has_host_compiler_backend))]
607         {
608             debug_assert!(engine.target().is_pulley());
609             Ok(Executor::Interpreter(Interpreter::new(engine)?))
610         }
611     }
612 }
613 
614 /// A borrowed reference to `Executor` above.
615 pub(crate) enum ExecutorRef<'a> {
616     Interpreter(InterpreterRef<'a>),
617     #[cfg(has_host_compiler_backend)]
618     Native,
619 }
620 
621 /// An RAII type to automatically mark a region of code as unsafe for GC.
622 #[doc(hidden)]
623 pub struct AutoAssertNoGc<'a> {
624     store: &'a mut StoreOpaque,
625     entered: bool,
626 }
627 
628 impl<'a> AutoAssertNoGc<'a> {
629     #[inline]
630     pub fn new(store: &'a mut StoreOpaque) -> Self {
631         let entered = if !cfg!(feature = "gc") {
632             false
633         } else if let Some(gc_store) = store.gc_store.as_mut() {
634             gc_store.gc_heap.enter_no_gc_scope();
635             true
636         } else {
637             false
638         };
639 
640         AutoAssertNoGc { store, entered }
641     }
642 
643     /// Creates an `AutoAssertNoGc` value which is forcibly "not entered" and
644     /// disables checks for no GC happening for the duration of this value.
645     ///
646     /// This is used when it is statically otherwise known that a GC doesn't
647     /// happen for the various types involved.
648     ///
649     /// # Unsafety
650     ///
651     /// This method is `unsafe` as it does not provide the same safety
652     /// guarantees as `AutoAssertNoGc::new`. It must be guaranteed by the
653     /// caller that a GC doesn't happen.
654     #[inline]
655     pub unsafe fn disabled(store: &'a mut StoreOpaque) -> Self {
656         if cfg!(debug_assertions) {
657             AutoAssertNoGc::new(store)
658         } else {
659             AutoAssertNoGc {
660                 store,
661                 entered: false,
662             }
663         }
664     }
665 }
666 
667 impl core::ops::Deref for AutoAssertNoGc<'_> {
668     type Target = StoreOpaque;
669 
670     #[inline]
671     fn deref(&self) -> &Self::Target {
672         &*self.store
673     }
674 }
675 
676 impl core::ops::DerefMut for AutoAssertNoGc<'_> {
677     #[inline]
678     fn deref_mut(&mut self) -> &mut Self::Target {
679         &mut *self.store
680     }
681 }
682 
683 impl Drop for AutoAssertNoGc<'_> {
684     #[inline]
685     fn drop(&mut self) {
686         if self.entered {
687             self.store.unwrap_gc_store_mut().gc_heap.exit_no_gc_scope();
688         }
689     }
690 }
691 
692 /// Used to associate instances with the store.
693 ///
694 /// This is needed to track if the instance was allocated explicitly with the on-demand
695 /// instance allocator.
696 struct StoreInstance {
697     handle: InstanceHandle,
698     kind: StoreInstanceKind,
699 }
700 
701 enum StoreInstanceKind {
702     /// An actual, non-dummy instance.
703     Real {
704         /// The id of this instance's module inside our owning store's
705         /// `ModuleRegistry`.
706         module_id: RegisteredModuleId,
707     },
708 
709     /// This is a dummy instance that is just an implementation detail for
710     /// something else. For example, host-created memories internally create a
711     /// dummy instance.
712     ///
713     /// Regardless of the configured instance allocator for the engine, dummy
714     /// instances always use the on-demand allocator to deallocate the instance.
715     Dummy,
716 }
717 
718 impl<T> Store<T> {
719     /// Creates a new [`Store`] to be associated with the given [`Engine`] and
720     /// `data` provided.
721     ///
722     /// The created [`Store`] will place no additional limits on the size of
723     /// linear memories or tables at runtime. Linear memories and tables will
724     /// be allowed to grow to any upper limit specified in their definitions.
725     /// The store will limit the number of instances, linear memories, and
726     /// tables created to 10,000. This can be overridden with the
727     /// [`Store::limiter`] configuration method.
728     pub fn new(engine: &Engine, data: T) -> Self {
729         Self::try_new(engine, data).expect(
730             "allocation failure during `Store::new` (use `Store::try_new` to handle such errors)",
731         )
732     }
733 
734     /// Like `Store::new` but returns an error on allocation failure.
735     pub fn try_new(engine: &Engine, data: T) -> Result<Self> {
736         let store_data = StoreData::new(engine);
737         log::trace!("creating new store {:?}", store_data.id());
738 
739         let pkey = engine.allocator().next_available_pkey();
740 
741         let inner = StoreOpaque {
742             _marker: marker::PhantomPinned,
743             engine: engine.clone(),
744             vm_store_context: Default::default(),
745             #[cfg(feature = "stack-switching")]
746             continuations: Vec::new(),
747             instances: wasmtime_environ::collections::PrimaryMap::new(),
748             signal_handler: None,
749             gc_store: None,
750             gc_roots: RootSet::default(),
751             #[cfg(feature = "gc")]
752             gc_roots_list: GcRootsList::default(),
753             #[cfg(feature = "gc")]
754             gc_host_alloc_types: Default::default(),
755             #[cfg(feature = "gc")]
756             pending_exception: None,
757             modules: ModuleRegistry::default(),
758             func_refs: FuncRefs::default(),
759             host_globals: PrimaryMap::new(),
760             instance_count: 0,
761             instance_limit: crate::DEFAULT_INSTANCE_LIMIT,
762             memory_count: 0,
763             memory_limit: crate::DEFAULT_MEMORY_LIMIT,
764             table_count: 0,
765             table_limit: crate::DEFAULT_TABLE_LIMIT,
766             #[cfg(feature = "async")]
767             async_state: Default::default(),
768             fuel_reserve: 0,
769             fuel_yield_interval: None,
770             store_data,
771             traitobj: StorePtr(None),
772             default_caller_vmctx: SendSyncPtr::new(NonNull::dangling()),
773             hostcall_val_storage: Vec::new(),
774             wasm_val_raw_storage: Vec::new(),
775             pkey,
776             executor: Executor::new(engine)?,
777             #[cfg(feature = "debug")]
778             breakpoints: Default::default(),
779             #[cfg(feature = "debug")]
780             frame_data_cache: FrameDataCache::new(),
781         };
782         let mut inner = try_new::<Box<_>>(StoreInner {
783             inner,
784             limiter: None,
785             call_hook: None,
786             #[cfg(target_has_atomic = "64")]
787             epoch_deadline_behavior: None,
788             data_no_provenance: ManuallyDrop::new(data),
789             #[cfg(feature = "debug")]
790             debug_handler: None,
791         })?;
792 
793         let store_data =
794             <NonNull<ManuallyDrop<T>>>::from(&mut inner.data_no_provenance).cast::<()>();
795         inner.inner.vm_store_context.store_data = store_data.into();
796 
797         inner.traitobj = StorePtr(Some(NonNull::from(&mut *inner)));
798 
799         // Wasmtime uses the callee argument to host functions to learn about
800         // the original pointer to the `Store` itself, allowing it to
801         // reconstruct a `StoreContextMut<T>`. When we initially call a `Func`,
802         // however, there's no "callee" to provide. To fix this we allocate a
803         // single "default callee" for the entire `Store`. This is then used as
804         // part of `Func::call` to guarantee that the `callee: *mut VMContext`
805         // is never null.
806         let allocator = OnDemandInstanceAllocator::default();
807         let info = engine.empty_module_runtime_info();
808         allocator
809             .validate_module(info.env_module(), info.offsets())
810             .unwrap();
811 
812         unsafe {
813             // Note that this dummy instance doesn't allocate tables or memories
814             // (also no limiter is passed in) so it won't have an async await
815             // point meaning that it should be ok to assert the future is
816             // always ready.
817             let result = vm::assert_ready(inner.allocate_instance(
818                 None,
819                 AllocateInstanceKind::Dummy {
820                     allocator: &allocator,
821                 },
822                 info,
823                 Default::default(),
824             ));
825             let id = match result {
826                 Ok(id) => id,
827                 Err(e) => {
828                     if e.is::<OutOfMemory>() {
829                         return Err(e);
830                     }
831                     panic!("instance allocator failed to allocate default callee")
832                 }
833             };
834             let default_caller_vmctx = inner.instance(id).vmctx();
835             inner.default_caller_vmctx = default_caller_vmctx.into();
836         }
837 
838         Ok(Self {
839             inner: ManuallyDrop::new(inner),
840         })
841     }
842 
843     /// Access the underlying `T` data owned by this `Store`.
844     #[inline]
845     pub fn data(&self) -> &T {
846         self.inner.data()
847     }
848 
849     /// Access the underlying `T` data owned by this `Store`.
850     #[inline]
851     pub fn data_mut(&mut self) -> &mut T {
852         self.inner.data_mut()
853     }
854 
855     fn run_manual_drop_routines(&mut self) {
856         StoreData::run_manual_drop_routines(StoreContextMut(&mut self.inner));
857 
858         // Ensure all fiber stacks, even cached ones, are all flushed out to the
859         // instance allocator.
860         self.inner.flush_fiber_stack();
861     }
862 
863     /// Consumes this [`Store`], destroying it, and returns the underlying data.
864     pub fn into_data(mut self) -> T {
865         self.run_manual_drop_routines();
866 
867         // This is an unsafe operation because we want to avoid having a runtime
868         // check or boolean for whether the data is actually contained within a
869         // `Store`. The data itself is stored as `ManuallyDrop` since we're
870         // manually managing the memory here, and there's also a `ManuallyDrop`
871         // around the `Box<StoreInner<T>>`. The way this works though is a bit
872         // tricky, so here's how things get dropped appropriately:
873         //
874         // * When a `Store<T>` is normally dropped, the custom destructor for
875         //   `Store<T>` will drop `T`, then the `self.inner` field. The
876         //   rustc-glue destructor runs for `Box<StoreInner<T>>` which drops
877         //   `StoreInner<T>`. This cleans up all internal fields and doesn't
878         //   touch `T` because it's wrapped in `ManuallyDrop`.
879         //
880         // * When calling this method we skip the top-level destructor for
881         //   `Store<T>` with `mem::forget`. This skips both the destructor for
882         //   `T` and the destructor for `StoreInner<T>`. We do, however, run the
883         //   destructor for `Box<StoreInner<T>>` which, like above, will skip
884         //   the destructor for `T` since it's `ManuallyDrop`.
885         //
886         // In both cases all the other fields of `StoreInner<T>` should all get
887         // dropped, and the manual management of destructors is basically
888         // between this method and `Drop for Store<T>`. Note that this also
889         // means that `Drop for StoreInner<T>` cannot access `self.data`, so
890         // there is a comment indicating this as well.
891         unsafe {
892             let mut inner = ManuallyDrop::take(&mut self.inner);
893             core::mem::forget(self);
894             ManuallyDrop::take(&mut inner.data_no_provenance)
895         }
896     }
897 
898     /// Configures the [`ResourceLimiter`] used to limit resource creation
899     /// within this [`Store`].
900     ///
901     /// Whenever resources such as linear memory, tables, or instances are
902     /// allocated the `limiter` specified here is invoked with the store's data
903     /// `T` and the returned [`ResourceLimiter`] is used to limit the operation
904     /// being allocated. The returned [`ResourceLimiter`] is intended to live
905     /// within the `T` itself, for example by storing a
906     /// [`StoreLimits`](crate::StoreLimits).
907     ///
908     /// Note that this limiter is only used to limit the creation/growth of
909     /// resources in the future, this does not retroactively attempt to apply
910     /// limits to the [`Store`].
911     ///
912     /// # Examples
913     ///
914     /// ```
915     /// use wasmtime::*;
916     ///
917     /// struct MyApplicationState {
918     ///     my_state: u32,
919     ///     limits: StoreLimits,
920     /// }
921     ///
922     /// let engine = Engine::default();
923     /// let my_state = MyApplicationState {
924     ///     my_state: 42,
925     ///     limits: StoreLimitsBuilder::new()
926     ///         .memory_size(1 << 20 /* 1 MB */)
927     ///         .instances(2)
928     ///         .build(),
929     /// };
930     /// let mut store = Store::new(&engine, my_state);
931     /// store.limiter(|state| &mut state.limits);
932     ///
933     /// // Creation of smaller memories is allowed
934     /// Memory::new(&mut store, MemoryType::new(1, None)).unwrap();
935     ///
936     /// // Creation of a larger memory, however, will exceed the 1MB limit we've
937     /// // configured
938     /// assert!(Memory::new(&mut store, MemoryType::new(1000, None)).is_err());
939     ///
940     /// // The number of instances in this store is limited to 2, so the third
941     /// // instance here should fail.
942     /// let module = Module::new(&engine, "(module)").unwrap();
943     /// assert!(Instance::new(&mut store, &module, &[]).is_ok());
944     /// assert!(Instance::new(&mut store, &module, &[]).is_ok());
945     /// assert!(Instance::new(&mut store, &module, &[]).is_err());
946     /// ```
947     ///
948     /// [`ResourceLimiter`]: crate::ResourceLimiter
949     pub fn limiter(
950         &mut self,
951         mut limiter: impl (FnMut(&mut T) -> &mut dyn crate::ResourceLimiter) + Send + Sync + 'static,
952     ) {
953         // Apply the limits on instances, tables, and memory given by the limiter:
954         let inner = &mut self.inner;
955         let (instance_limit, table_limit, memory_limit) = {
956             let l = limiter(inner.data_mut());
957             (l.instances(), l.tables(), l.memories())
958         };
959         let innermost = &mut inner.inner;
960         innermost.instance_limit = instance_limit;
961         innermost.table_limit = table_limit;
962         innermost.memory_limit = memory_limit;
963 
964         // Save the limiter accessor function:
965         inner.limiter = Some(ResourceLimiterInner::Sync(Box::new(limiter)));
966     }
967 
968     /// Configure a function that runs on calls and returns between WebAssembly
969     /// and host code.
970     ///
971     /// The function is passed a [`CallHook`] argument, which indicates which
972     /// state transition the VM is making.
973     ///
974     /// This function may return a [`Trap`]. If a trap is returned when an
975     /// import was called, it is immediately raised as-if the host import had
976     /// returned the trap. If a trap is returned after wasm returns to the host
977     /// then the wasm function's result is ignored and this trap is returned
978     /// instead.
979     ///
980     /// After this function returns a trap, it may be called for subsequent returns
981     /// to host or wasm code as the trap propagates to the root call.
982     ///
983     /// [`Trap`]: crate::Trap
984     #[cfg(feature = "call-hook")]
985     pub fn call_hook(
986         &mut self,
987         hook: impl FnMut(StoreContextMut<'_, T>, CallHook) -> Result<()> + Send + Sync + 'static,
988     ) {
989         self.inner.call_hook = Some(CallHookInner::Sync(Box::new(hook)));
990     }
991 
992     /// Returns the [`Engine`] that this store is associated with.
993     pub fn engine(&self) -> &Engine {
994         self.inner.engine()
995     }
996 
997     /// Perform garbage collection.
998     ///
999     /// Note that it is not required to actively call this function. GC will
1000     /// automatically happen according to various internal heuristics. This is
1001     /// provided if fine-grained control over the GC is desired.
1002     ///
1003     /// If you are calling this method after an attempted allocation failed, you
1004     /// may pass in the [`GcHeapOutOfMemory`][crate::GcHeapOutOfMemory] error.
1005     /// When you do so, this method will attempt to create enough space in the
1006     /// GC heap for that allocation, so that it will succeed on the next
1007     /// attempt.
1008     ///
1009     /// # Errors
1010     ///
1011     /// This method will fail if an [async limiter is
1012     /// configured](Store::limiter_async) in which case [`Store::gc_async`] must
1013     /// be used instead.
1014     #[cfg(feature = "gc")]
1015     pub fn gc(&mut self, why: Option<&crate::GcHeapOutOfMemory<()>>) -> Result<()> {
1016         StoreContextMut(&mut self.inner).gc(why)
1017     }
1018 
1019     /// Returns the amount fuel in this [`Store`]. When fuel is enabled, it must
1020     /// be configured via [`Store::set_fuel`].
1021     ///
1022     /// # Errors
1023     ///
1024     /// This function will return an error if fuel consumption is not enabled
1025     /// via [`Config::consume_fuel`](crate::Config::consume_fuel).
1026     pub fn get_fuel(&self) -> Result<u64> {
1027         self.inner.get_fuel()
1028     }
1029 
1030     /// Set the fuel to this [`Store`] for wasm to consume while executing.
1031     ///
1032     /// For this method to work fuel consumption must be enabled via
1033     /// [`Config::consume_fuel`](crate::Config::consume_fuel). By default a
1034     /// [`Store`] starts with 0 fuel for wasm to execute with (meaning it will
1035     /// immediately trap). This function must be called for the store to have
1036     /// some fuel to allow WebAssembly to execute.
1037     ///
1038     /// Most WebAssembly instructions consume 1 unit of fuel. Some
1039     /// instructions, such as `nop`, `drop`, `block`, and `loop`, consume 0
1040     /// units, as any execution cost associated with them involves other
1041     /// instructions which do consume fuel.
1042     ///
1043     /// Note that when fuel is entirely consumed it will cause wasm to trap.
1044     ///
1045     /// # Errors
1046     ///
1047     /// This function will return an error if fuel consumption is not enabled via
1048     /// [`Config::consume_fuel`](crate::Config::consume_fuel).
1049     pub fn set_fuel(&mut self, fuel: u64) -> Result<()> {
1050         self.inner.set_fuel(fuel)
1051     }
1052 
1053     /// Configures a [`Store`] to yield execution of async WebAssembly code
1054     /// periodically.
1055     ///
1056     /// When a [`Store`] is configured to consume fuel with
1057     /// [`Config::consume_fuel`](crate::Config::consume_fuel) this method will
1058     /// configure WebAssembly to be suspended and control will be yielded back
1059     /// to the caller every `interval` units of fuel consumed. When using this
1060     /// method it requires further invocations of WebAssembly to use `*_async`
1061     /// entrypoints.
1062     ///
1063     /// The purpose of this behavior is to ensure that futures which represent
1064     /// execution of WebAssembly do not execute too long inside their
1065     /// `Future::poll` method. This allows for some form of cooperative
1066     /// multitasking where WebAssembly will voluntarily yield control
1067     /// periodically (based on fuel consumption) back to the running thread.
1068     ///
1069     /// Note that futures returned by this crate will automatically flag
1070     /// themselves to get re-polled if a yield happens. This means that
1071     /// WebAssembly will continue to execute, just after giving the host an
1072     /// opportunity to do something else.
1073     ///
1074     /// The `interval` parameter indicates how much fuel should be
1075     /// consumed between yields of an async future. When fuel runs out wasm will trap.
1076     ///
1077     /// # Error
1078     ///
1079     /// This method will error if fuel is not enabled or `interval` is
1080     /// `Some(0)`.
1081     #[cfg(feature = "async")]
1082     pub fn fuel_async_yield_interval(&mut self, interval: Option<u64>) -> Result<()> {
1083         self.inner.fuel_async_yield_interval(interval)
1084     }
1085 
1086     /// Sets the epoch deadline to a certain number of ticks in the future.
1087     ///
1088     /// When the Wasm guest code is compiled with epoch-interruption
1089     /// instrumentation
1090     /// ([`Config::epoch_interruption()`](crate::Config::epoch_interruption)),
1091     /// and when the `Engine`'s epoch is incremented
1092     /// ([`Engine::increment_epoch()`](crate::Engine::increment_epoch))
1093     /// past a deadline, execution can be configured to either trap or
1094     /// yield and then continue.
1095     ///
1096     /// This deadline is always set relative to the current epoch:
1097     /// `ticks_beyond_current` ticks in the future. The deadline can
1098     /// be set explicitly via this method, or refilled automatically
1099     /// on a yield if configured via
1100     /// [`epoch_deadline_async_yield_and_update()`](Store::epoch_deadline_async_yield_and_update). After
1101     /// this method is invoked, the deadline is reached when
1102     /// [`Engine::increment_epoch()`] has been invoked at least
1103     /// `ticks_beyond_current` times.
1104     ///
1105     /// By default a store will trap immediately with an epoch deadline of 0
1106     /// (which has always "elapsed"). This method is required to be configured
1107     /// for stores with epochs enabled to some future epoch deadline.
1108     ///
1109     /// See documentation on
1110     /// [`Config::epoch_interruption()`](crate::Config::epoch_interruption)
1111     /// for an introduction to epoch-based interruption.
1112     #[cfg(target_has_atomic = "64")]
1113     pub fn set_epoch_deadline(&mut self, ticks_beyond_current: u64) {
1114         self.inner.set_epoch_deadline(ticks_beyond_current);
1115     }
1116 
1117     /// Configures epoch-deadline expiration to trap.
1118     ///
1119     /// When epoch-interruption-instrumented code is executed on this
1120     /// store and the epoch deadline is reached before completion,
1121     /// with the store configured in this way, execution will
1122     /// terminate with a trap as soon as an epoch check in the
1123     /// instrumented code is reached.
1124     ///
1125     /// This behavior is the default if the store is not otherwise
1126     /// configured via
1127     /// [`epoch_deadline_trap()`](Store::epoch_deadline_trap),
1128     /// [`epoch_deadline_callback()`](Store::epoch_deadline_callback) or
1129     /// [`epoch_deadline_async_yield_and_update()`](Store::epoch_deadline_async_yield_and_update).
1130     ///
1131     /// This setting is intended to allow for coarse-grained
1132     /// interruption, but not a deterministic deadline of a fixed,
1133     /// finite interval. For deterministic interruption, see the
1134     /// "fuel" mechanism instead.
1135     ///
1136     /// Note that when this is used it's required to call
1137     /// [`Store::set_epoch_deadline`] or otherwise wasm will always immediately
1138     /// trap.
1139     ///
1140     /// See documentation on
1141     /// [`Config::epoch_interruption()`](crate::Config::epoch_interruption)
1142     /// for an introduction to epoch-based interruption.
1143     #[cfg(target_has_atomic = "64")]
1144     pub fn epoch_deadline_trap(&mut self) {
1145         self.inner.epoch_deadline_trap();
1146     }
1147 
1148     /// Configures epoch-deadline expiration to invoke a custom callback
1149     /// function.
1150     ///
1151     /// When epoch-interruption-instrumented code is executed on this
1152     /// store and the epoch deadline is reached before completion, the
1153     /// provided callback function is invoked.
1154     ///
1155     /// This callback should either return an [`UpdateDeadline`], or
1156     /// return an error, which will terminate execution with a trap.
1157     ///
1158     /// The [`UpdateDeadline`] is a positive number of ticks to
1159     /// add to the epoch deadline, as well as indicating what
1160     /// to do after the callback returns. If the [`Store`] is
1161     /// configured with async support, then the callback may return
1162     /// [`UpdateDeadline::Yield`] or [`UpdateDeadline::YieldCustom`]
1163     /// to yield to the async executor before updating the epoch deadline.
1164     /// Alternatively, the callback may return [`UpdateDeadline::Continue`] to
1165     /// update the epoch deadline immediately.
1166     ///
1167     /// This setting is intended to allow for coarse-grained
1168     /// interruption, but not a deterministic deadline of a fixed,
1169     /// finite interval. For deterministic interruption, see the
1170     /// "fuel" mechanism instead.
1171     ///
1172     /// See documentation on
1173     /// [`Config::epoch_interruption()`](crate::Config::epoch_interruption)
1174     /// for an introduction to epoch-based interruption.
1175     #[cfg(target_has_atomic = "64")]
1176     pub fn epoch_deadline_callback(
1177         &mut self,
1178         callback: impl FnMut(StoreContextMut<T>) -> Result<UpdateDeadline> + Send + Sync + 'static,
1179     ) {
1180         self.inner.epoch_deadline_callback(Box::new(callback));
1181     }
1182 
1183     /// Set an exception as the currently pending exception, and
1184     /// return an error that propagates the throw.
1185     ///
1186     /// This method takes an exception object and stores it in the
1187     /// `Store` as the currently pending exception. This is a special
1188     /// rooted slot that holds the exception as long as it is
1189     /// propagating. This method then returns a `ThrownException`
1190     /// error, which is a special type that indicates a pending
1191     /// exception exists. When this type propagates as an error
1192     /// returned from a Wasm-to-host call, the pending exception is
1193     /// thrown within the Wasm context, and either caught or
1194     /// propagated further to the host-to-Wasm call boundary. If an
1195     /// exception is thrown out of Wasm (or across Wasm from a
1196     /// hostcall) back to the host-to-Wasm call boundary, *that*
1197     /// invocation returns a `ThrownException`, and the pending
1198     /// exception slot is again set. In other words, the
1199     /// `ThrownException` error type should propagate upward exactly
1200     /// and only when a pending exception is set.
1201     ///
1202     /// To take the pending exception, use [`Self::take_pending_exception`].
1203     ///
1204     /// This method is parameterized over `R` for convenience, but
1205     /// will always return an `Err`.
1206     ///
1207     /// # Panics
1208     ///
1209     /// - Will panic if `exception` has been unrooted.
1210     /// - Will panic if `exception` is a null reference.
1211     /// - Will panic if a pending exception has already been set.
1212     #[cfg(feature = "gc")]
1213     pub fn throw<R>(&mut self, exception: Rooted<ExnRef>) -> Result<R, ThrownException> {
1214         self.inner.throw_impl(exception);
1215         Err(ThrownException)
1216     }
1217 
1218     /// Take the currently pending exception, if any, and return it,
1219     /// removing it from the "pending exception" slot.
1220     ///
1221     /// If there is no pending exception, returns `None`.
1222     ///
1223     /// Note: the returned exception is a LIFO root (see
1224     /// [`crate::Rooted`]), rooted in the current handle scope. Take
1225     /// care to ensure that it is re-rooted or otherwise does not
1226     /// escape this scope! It is usually best to allow an exception
1227     /// object to be rooted in the store's "pending exception" slot
1228     /// until the final consumer has taken it, rather than root it and
1229     /// pass it up the callstack in some other way.
1230     ///
1231     /// This method is useful to implement ad-hoc exception plumbing
1232     /// in various ways, but for the most idiomatic handling, see
1233     /// [`StoreContextMut::throw`].
1234     #[cfg(feature = "gc")]
1235     pub fn take_pending_exception(&mut self) -> Option<Rooted<ExnRef>> {
1236         self.inner.take_pending_exception_rooted()
1237     }
1238 
1239     /// Tests whether there is a pending exception.
1240     ///
1241     /// Ordinarily, a pending exception will be set on a store if and
1242     /// only if a host-side callstack is propagating a
1243     /// [`crate::ThrownException`] error. The final consumer that
1244     /// catches the exception takes it; it may re-place it to re-throw
1245     /// (using [`Self::throw`]) if it chooses not to actually handle the
1246     /// exception.
1247     ///
1248     /// This method is useful to tell whether a store is in this
1249     /// state, but should not be used as part of the ordinary
1250     /// exception-handling flow. For the most idiomatic handling, see
1251     /// [`StoreContextMut::throw`].
1252     #[cfg(feature = "gc")]
1253     pub fn has_pending_exception(&self) -> bool {
1254         self.inner.pending_exception.is_some()
1255     }
1256 
1257     /// Return all breakpoints.
1258     #[cfg(feature = "debug")]
1259     pub fn breakpoints(&self) -> Option<impl Iterator<Item = crate::Breakpoint> + '_> {
1260         self.as_context().breakpoints()
1261     }
1262 
1263     /// Indicate whether single-step mode is enabled.
1264     #[cfg(feature = "debug")]
1265     pub fn is_single_step(&self) -> bool {
1266         self.as_context().is_single_step()
1267     }
1268 
1269     /// Set the debug callback on this store.
1270     ///
1271     /// See [`crate::DebugHandler`] for more documentation.
1272     ///
1273     /// # Panics
1274     ///
1275     /// - Will panic if guest-debug support was not enabled via
1276     ///   [`crate::Config::guest_debug`].
1277     #[cfg(feature = "debug")]
1278     pub fn set_debug_handler(&mut self, handler: impl DebugHandler<Data = T>)
1279     where
1280         // We require `Send` here because the debug handler becomes
1281         // referenced from a future: when `DebugHandler::handle` is
1282         // invoked, its `self` references the `handler` with the
1283         // user's state. Note that we are careful to keep this bound
1284         // constrained to debug-handler-related code only and not
1285         // propagate it outward to the store in general. The presence
1286         // of the trait implementation serves as a witness that `T:
1287         // Send`. This is required in particular because we will have
1288         // a `&mut dyn VMStore` on the stack when we pause a fiber
1289         // with `block_on` to run a debugger hook; that `VMStore` must
1290         // be a `Store<T> where T: Send`.
1291         T: Send,
1292     {
1293         // Debug hooks rely on async support, so async entrypoints are required.
1294         self.inner.set_async_required(Asyncness::Yes);
1295 
1296         assert!(
1297             self.engine().tunables().debug_guest,
1298             "debug hooks require guest debugging to be enabled"
1299         );
1300         self.inner.debug_handler = Some(Box::new(handler));
1301     }
1302 
1303     /// Clear the debug handler on this store. If any existed, it will
1304     /// be dropped.
1305     #[cfg(feature = "debug")]
1306     pub fn clear_debug_handler(&mut self) {
1307         self.inner.debug_handler = None;
1308     }
1309 }
1310 
1311 impl<'a, T> StoreContext<'a, T> {
1312     /// Returns the underlying [`Engine`] this store is connected to.
1313     pub fn engine(&self) -> &Engine {
1314         self.0.engine()
1315     }
1316 
1317     /// Access the underlying data owned by this `Store`.
1318     ///
1319     /// Same as [`Store::data`].
1320     pub fn data(&self) -> &'a T {
1321         self.0.data()
1322     }
1323 
1324     /// Returns the remaining fuel in this store.
1325     ///
1326     /// For more information see [`Store::get_fuel`].
1327     pub fn get_fuel(&self) -> Result<u64> {
1328         self.0.get_fuel()
1329     }
1330 }
1331 
1332 impl<'a, T> StoreContextMut<'a, T> {
1333     /// Access the underlying data owned by this `Store`.
1334     ///
1335     /// Same as [`Store::data`].
1336     pub fn data(&self) -> &T {
1337         self.0.data()
1338     }
1339 
1340     /// Access the underlying data owned by this `Store`.
1341     ///
1342     /// Same as [`Store::data_mut`].
1343     pub fn data_mut(&mut self) -> &mut T {
1344         self.0.data_mut()
1345     }
1346 
1347     /// Returns the underlying [`Engine`] this store is connected to.
1348     pub fn engine(&self) -> &Engine {
1349         self.0.engine()
1350     }
1351 
1352     /// Perform garbage collection of `ExternRef`s.
1353     ///
1354     /// Same as [`Store::gc`].
1355     #[cfg(feature = "gc")]
1356     pub fn gc(&mut self, why: Option<&crate::GcHeapOutOfMemory<()>>) -> Result<()> {
1357         let (mut limiter, store) = self.0.validate_sync_resource_limiter_and_store_opaque()?;
1358         vm::assert_ready(store.gc(
1359             limiter.as_mut(),
1360             None,
1361             why.map(|e| e.bytes_needed()),
1362             Asyncness::No,
1363         ));
1364         Ok(())
1365     }
1366 
1367     /// Returns remaining fuel in this store.
1368     ///
1369     /// For more information see [`Store::get_fuel`]
1370     pub fn get_fuel(&self) -> Result<u64> {
1371         self.0.get_fuel()
1372     }
1373 
1374     /// Set the amount of fuel in this store.
1375     ///
1376     /// For more information see [`Store::set_fuel`]
1377     pub fn set_fuel(&mut self, fuel: u64) -> Result<()> {
1378         self.0.set_fuel(fuel)
1379     }
1380 
1381     /// Configures this `Store` to periodically yield while executing futures.
1382     ///
1383     /// For more information see [`Store::fuel_async_yield_interval`]
1384     #[cfg(feature = "async")]
1385     pub fn fuel_async_yield_interval(&mut self, interval: Option<u64>) -> Result<()> {
1386         self.0.fuel_async_yield_interval(interval)
1387     }
1388 
1389     /// Sets the epoch deadline to a certain number of ticks in the future.
1390     ///
1391     /// For more information see [`Store::set_epoch_deadline`].
1392     #[cfg(target_has_atomic = "64")]
1393     pub fn set_epoch_deadline(&mut self, ticks_beyond_current: u64) {
1394         self.0.set_epoch_deadline(ticks_beyond_current);
1395     }
1396 
1397     /// Configures epoch-deadline expiration to trap.
1398     ///
1399     /// For more information see [`Store::epoch_deadline_trap`].
1400     #[cfg(target_has_atomic = "64")]
1401     pub fn epoch_deadline_trap(&mut self) {
1402         self.0.epoch_deadline_trap();
1403     }
1404 
1405     /// Set an exception as the currently pending exception, and
1406     /// return an error that propagates the throw.
1407     ///
1408     /// See [`Store::throw`] for more details.
1409     #[cfg(feature = "gc")]
1410     pub fn throw<R>(&mut self, exception: Rooted<ExnRef>) -> Result<R, ThrownException> {
1411         self.0.inner.throw_impl(exception);
1412         Err(ThrownException)
1413     }
1414 
1415     /// Take the currently pending exception, if any, and return it,
1416     /// removing it from the "pending exception" slot.
1417     ///
1418     /// See [`Store::take_pending_exception`] for more details.
1419     #[cfg(feature = "gc")]
1420     pub fn take_pending_exception(&mut self) -> Option<Rooted<ExnRef>> {
1421         self.0.inner.take_pending_exception_rooted()
1422     }
1423 
1424     /// Tests whether there is a pending exception.
1425     ///
1426     /// See [`Store::has_pending_exception`] for more details.
1427     #[cfg(feature = "gc")]
1428     pub fn has_pending_exception(&self) -> bool {
1429         self.0.inner.pending_exception.is_some()
1430     }
1431 }
1432 
1433 impl<T> StoreInner<T> {
1434     #[inline]
1435     fn data(&self) -> &T {
1436         // We are actually just accessing `&self.data_no_provenance` but we must
1437         // do so with the `VMStoreContext::store_data` pointer's provenance. If
1438         // we did otherwise, i.e. directly accessed the field, we would
1439         // invalidate that pointer, which would in turn invalidate any direct
1440         // `T` accesses that Wasm code makes via unsafe intrinsics.
1441         let data: *const ManuallyDrop<T> = &raw const self.data_no_provenance;
1442         let provenance = self.inner.vm_store_context.store_data.as_ptr().cast::<T>();
1443         let ptr = provenance.with_addr(data.addr());
1444 
1445         // SAFETY: The pointer is non-null, points to our `T` data, and is valid
1446         // to access because of our `&self` borrow.
1447         debug_assert_ne!(ptr, core::ptr::null_mut());
1448         debug_assert_eq!(ptr.addr(), (&raw const self.data_no_provenance).addr());
1449         unsafe { &*ptr }
1450     }
1451 
1452     #[inline]
1453     fn data_limiter_and_opaque(
1454         &mut self,
1455     ) -> (
1456         &mut T,
1457         Option<&mut ResourceLimiterInner<T>>,
1458         &mut StoreOpaque,
1459     ) {
1460         // See the comments about provenance in `StoreInner::data` above.
1461         let data: *mut ManuallyDrop<T> = &raw mut self.data_no_provenance;
1462         let provenance = self.inner.vm_store_context.store_data.as_ptr().cast::<T>();
1463         let ptr = provenance.with_addr(data.addr());
1464 
1465         // SAFETY: The pointer is non-null, points to our `T` data, and is valid
1466         // to access because of our `&mut self` borrow.
1467         debug_assert_ne!(ptr, core::ptr::null_mut());
1468         debug_assert_eq!(ptr.addr(), (&raw const self.data_no_provenance).addr());
1469         let data = unsafe { &mut *ptr };
1470 
1471         let limiter = self.limiter.as_mut();
1472 
1473         (data, limiter, &mut self.inner)
1474     }
1475 
1476     #[inline]
1477     fn data_mut(&mut self) -> &mut T {
1478         self.data_limiter_and_opaque().0
1479     }
1480 
1481     #[inline]
1482     pub fn call_hook(&mut self, s: CallHook) -> Result<()> {
1483         if self.inner.pkey.is_none() && self.call_hook.is_none() {
1484             Ok(())
1485         } else {
1486             self.call_hook_slow_path(s)
1487         }
1488     }
1489 
1490     fn call_hook_slow_path(&mut self, s: CallHook) -> Result<()> {
1491         if let Some(pkey) = &self.inner.pkey {
1492             let allocator = self.engine().allocator();
1493             match s {
1494                 CallHook::CallingWasm | CallHook::ReturningFromHost => {
1495                     allocator.restrict_to_pkey(*pkey)
1496                 }
1497                 CallHook::ReturningFromWasm | CallHook::CallingHost => allocator.allow_all_pkeys(),
1498             }
1499         }
1500 
1501         // Temporarily take the configured behavior to avoid mutably borrowing
1502         // multiple times.
1503         if let Some(mut call_hook) = self.call_hook.take() {
1504             let result = self.invoke_call_hook(&mut call_hook, s);
1505             self.call_hook = Some(call_hook);
1506             return result;
1507         }
1508 
1509         Ok(())
1510     }
1511 
1512     fn invoke_call_hook(&mut self, call_hook: &mut CallHookInner<T>, s: CallHook) -> Result<()> {
1513         match call_hook {
1514             #[cfg(feature = "call-hook")]
1515             CallHookInner::Sync(hook) => hook((&mut *self).as_context_mut(), s),
1516 
1517             #[cfg(all(feature = "async", feature = "call-hook"))]
1518             CallHookInner::Async(handler) => {
1519                 if !self.can_block() {
1520                     bail!("couldn't grab async_cx for call hook")
1521                 }
1522                 return (&mut *self)
1523                     .as_context_mut()
1524                     .with_blocking(|store, cx| cx.block_on(handler.handle_call_event(store, s)))?;
1525             }
1526 
1527             CallHookInner::ForceTypeParameterToBeUsed { uninhabited, .. } => {
1528                 let _ = s;
1529                 match *uninhabited {}
1530             }
1531         }
1532     }
1533 
1534     #[cfg(not(feature = "async"))]
1535     fn flush_fiber_stack(&mut self) {
1536         // noop shim so code can assume this always exists.
1537     }
1538 
1539     /// Splits this `StoreInner<T>` into a `limiter`/`StoerOpaque` borrow while
1540     /// validating that an async limiter is not configured.
1541     ///
1542     /// This is used for sync entrypoints which need to fail if an async limiter
1543     /// is configured as otherwise the async entrypoint must be used instead.
1544     pub(crate) fn validate_sync_resource_limiter_and_store_opaque(
1545         &mut self,
1546     ) -> Result<(Option<StoreResourceLimiter<'_>>, &mut StoreOpaque)> {
1547         let (limiter, store) = self.resource_limiter_and_store_opaque();
1548         if !matches!(limiter, None | Some(StoreResourceLimiter::Sync(_))) {
1549             bail!(
1550                 "when using an async resource limiter `*_async` functions must \
1551              be used instead"
1552             );
1553         }
1554         Ok((limiter, store))
1555     }
1556 }
1557 
1558 fn get_fuel(injected_fuel: i64, fuel_reserve: u64) -> u64 {
1559     fuel_reserve.saturating_add_signed(-injected_fuel)
1560 }
1561 
1562 // Add remaining fuel from the reserve into the active fuel if there is any left.
1563 fn refuel(
1564     injected_fuel: &mut i64,
1565     fuel_reserve: &mut u64,
1566     yield_interval: Option<NonZeroU64>,
1567 ) -> bool {
1568     let fuel = get_fuel(*injected_fuel, *fuel_reserve);
1569     if fuel > 0 {
1570         set_fuel(injected_fuel, fuel_reserve, yield_interval, fuel);
1571         true
1572     } else {
1573         false
1574     }
1575 }
1576 
1577 fn set_fuel(
1578     injected_fuel: &mut i64,
1579     fuel_reserve: &mut u64,
1580     yield_interval: Option<NonZeroU64>,
1581     new_fuel_amount: u64,
1582 ) {
1583     let interval = yield_interval.unwrap_or(NonZeroU64::MAX).get();
1584     // If we're yielding periodically we only store the "active" amount of fuel into consumed_ptr
1585     // for the VM to use.
1586     let injected = core::cmp::min(interval, new_fuel_amount);
1587     // Fuel in the VM is stored as an i64, so we have to cap the amount of fuel we inject into the
1588     // VM at once to be i64 range.
1589     let injected = core::cmp::min(injected, i64::MAX as u64);
1590     // Add whatever is left over after injection to the reserve for later use.
1591     *fuel_reserve = new_fuel_amount - injected;
1592     // Within the VM we increment to count fuel, so inject a negative amount. The VM will halt when
1593     // this counter is positive.
1594     *injected_fuel = -(injected as i64);
1595 }
1596 
1597 #[doc(hidden)]
1598 impl StoreOpaque {
1599     pub fn id(&self) -> StoreId {
1600         self.store_data.id()
1601     }
1602 
1603     pub fn bump_resource_counts(&mut self, module: &Module) -> Result<()> {
1604         fn bump(slot: &mut usize, max: usize, amt: usize, desc: &str) -> Result<()> {
1605             let new = slot.saturating_add(amt);
1606             if new > max {
1607                 bail!("resource limit exceeded: {desc} count too high at {new}");
1608             }
1609             *slot = new;
1610             Ok(())
1611         }
1612 
1613         let module = module.env_module();
1614         let memories = module.num_defined_memories();
1615         let tables = module.num_defined_tables();
1616 
1617         bump(&mut self.instance_count, self.instance_limit, 1, "instance")?;
1618         bump(
1619             &mut self.memory_count,
1620             self.memory_limit,
1621             memories,
1622             "memory",
1623         )?;
1624         bump(&mut self.table_count, self.table_limit, tables, "table")?;
1625 
1626         Ok(())
1627     }
1628 
1629     #[inline]
1630     pub fn engine(&self) -> &Engine {
1631         &self.engine
1632     }
1633 
1634     #[inline]
1635     pub fn store_data(&self) -> &StoreData {
1636         &self.store_data
1637     }
1638 
1639     #[inline]
1640     pub fn store_data_mut(&mut self) -> &mut StoreData {
1641         &mut self.store_data
1642     }
1643 
1644     pub fn store_data_mut_and_registry(&mut self) -> (&mut StoreData, &ModuleRegistry) {
1645         (&mut self.store_data, &self.modules)
1646     }
1647 
1648     #[cfg(feature = "debug")]
1649     pub(crate) fn breakpoints_and_registry_mut(
1650         &mut self,
1651     ) -> (&mut BreakpointState, &mut ModuleRegistry) {
1652         (&mut self.breakpoints, &mut self.modules)
1653     }
1654 
1655     #[cfg(feature = "debug")]
1656     pub(crate) fn breakpoints_and_registry(&self) -> (&BreakpointState, &ModuleRegistry) {
1657         (&self.breakpoints, &self.modules)
1658     }
1659 
1660     #[cfg(feature = "debug")]
1661     pub(crate) fn frame_data_cache_mut_and_registry(
1662         &mut self,
1663     ) -> (&mut FrameDataCache, &ModuleRegistry) {
1664         (&mut self.frame_data_cache, &self.modules)
1665     }
1666 
1667     #[inline]
1668     pub(crate) fn modules(&self) -> &ModuleRegistry {
1669         &self.modules
1670     }
1671 
1672     #[inline]
1673     pub(crate) fn modules_and_engine_mut(&mut self) -> (&mut ModuleRegistry, &Engine) {
1674         (&mut self.modules, &self.engine)
1675     }
1676 
1677     pub(crate) fn func_refs_and_modules(&mut self) -> (&mut FuncRefs, &ModuleRegistry) {
1678         (&mut self.func_refs, &self.modules)
1679     }
1680 
1681     pub(crate) fn host_globals(
1682         &self,
1683     ) -> &PrimaryMap<DefinedGlobalIndex, StoreBox<VMHostGlobalContext>> {
1684         &self.host_globals
1685     }
1686 
1687     pub(crate) fn host_globals_mut(
1688         &mut self,
1689     ) -> &mut PrimaryMap<DefinedGlobalIndex, StoreBox<VMHostGlobalContext>> {
1690         &mut self.host_globals
1691     }
1692 
1693     pub fn module_for_instance(&self, instance: StoreInstanceId) -> Option<&'_ Module> {
1694         instance.store_id().assert_belongs_to(self.id());
1695         match self.instances[instance.instance()].kind {
1696             StoreInstanceKind::Dummy => None,
1697             StoreInstanceKind::Real { module_id } => {
1698                 let module = self
1699                     .modules()
1700                     .module_by_id(module_id)
1701                     .expect("should always have a registered module for real instances");
1702                 Some(module)
1703             }
1704         }
1705     }
1706 
1707     /// Accessor from `InstanceId` to `&vm::Instance`.
1708     ///
1709     /// Note that if you have a `StoreInstanceId` you should use
1710     /// `StoreInstanceId::get` instead. This assumes that `id` has been
1711     /// validated to already belong to this store.
1712     #[inline]
1713     pub fn instance(&self, id: InstanceId) -> &vm::Instance {
1714         self.instances[id].handle.get()
1715     }
1716 
1717     /// Accessor from `InstanceId` to `Pin<&mut vm::Instance>`.
1718     ///
1719     /// Note that if you have a `StoreInstanceId` you should use
1720     /// `StoreInstanceId::get_mut` instead. This assumes that `id` has been
1721     /// validated to already belong to this store.
1722     #[inline]
1723     pub fn instance_mut(&mut self, id: InstanceId) -> Pin<&mut vm::Instance> {
1724         self.instances[id].handle.get_mut()
1725     }
1726 
1727     /// Accessor from `InstanceId` to both `Pin<&mut vm::Instance>`
1728     /// and `&ModuleRegistry`.
1729     #[inline]
1730     pub fn instance_and_module_registry_mut(
1731         &mut self,
1732         id: InstanceId,
1733     ) -> (Pin<&mut vm::Instance>, &ModuleRegistry) {
1734         (self.instances[id].handle.get_mut(), &self.modules)
1735     }
1736 
1737     /// Access multiple instances specified via `ids`.
1738     ///
1739     /// # Panics
1740     ///
1741     /// This method will panic if any indices in `ids` overlap.
1742     ///
1743     /// # Safety
1744     ///
1745     /// This method is not safe if the returned instances are used to traverse
1746     /// "laterally" between other instances. For example accessing imported
1747     /// items in an instance may traverse laterally to a sibling instance thus
1748     /// aliasing a returned value here. The caller must ensure that only defined
1749     /// items within the instances themselves are accessed.
1750     #[inline]
1751     pub unsafe fn optional_gc_store_and_instances_mut<const N: usize>(
1752         &mut self,
1753         ids: [InstanceId; N],
1754     ) -> (Option<&mut GcStore>, [Pin<&mut vm::Instance>; N]) {
1755         let instances = self
1756             .instances
1757             .get_disjoint_mut(ids)
1758             .unwrap()
1759             .map(|h| h.handle.get_mut());
1760         (self.gc_store.as_mut(), instances)
1761     }
1762 
1763     /// Pair of `Self::optional_gc_store_mut` and `Self::instance_mut`
1764     pub fn optional_gc_store_and_instance_mut(
1765         &mut self,
1766         id: InstanceId,
1767     ) -> (Option<&mut GcStore>, Pin<&mut vm::Instance>) {
1768         (self.gc_store.as_mut(), self.instances[id].handle.get_mut())
1769     }
1770 
1771     /// Tuple of `Self::optional_gc_store_mut`, `Self::modules`, and
1772     /// `Self::instance_mut`.
1773     pub fn optional_gc_store_and_registry_and_instance_mut(
1774         &mut self,
1775         id: InstanceId,
1776     ) -> (
1777         Option<&mut GcStore>,
1778         &ModuleRegistry,
1779         Pin<&mut vm::Instance>,
1780     ) {
1781         (
1782             self.gc_store.as_mut(),
1783             &self.modules,
1784             self.instances[id].handle.get_mut(),
1785         )
1786     }
1787 
1788     /// Get all instances (ignoring dummy instances) within this store.
1789     pub fn all_instances<'a>(&'a mut self) -> impl ExactSizeIterator<Item = Instance> + 'a {
1790         let instances = self
1791             .instances
1792             .iter()
1793             .filter_map(|(id, inst)| {
1794                 if let StoreInstanceKind::Dummy = inst.kind {
1795                     None
1796                 } else {
1797                     Some(id)
1798                 }
1799             })
1800             .collect::<Vec<_>>();
1801         instances
1802             .into_iter()
1803             .map(|i| Instance::from_wasmtime(i, self))
1804     }
1805 
1806     /// Get all memories (host- or Wasm-defined) within this store.
1807     pub fn all_memories<'a>(&'a self) -> impl Iterator<Item = ExportMemory> + 'a {
1808         // NB: Host-created memories have dummy instances. Therefore, we can get
1809         // all memories in the store by iterating over all instances (including
1810         // dummy instances) and getting each of their defined memories.
1811         let id = self.id();
1812         self.instances
1813             .iter()
1814             .flat_map(move |(_, instance)| instance.handle.get().defined_memories(id))
1815     }
1816 
1817     /// Iterate over all tables (host- or Wasm-defined) within this store.
1818     pub fn for_each_table(&mut self, mut f: impl FnMut(&mut Self, Table)) {
1819         // NB: Host-created tables have dummy instances. Therefore, we can get
1820         // all tables in the store by iterating over all instances (including
1821         // dummy instances) and getting each of their defined memories.
1822         for id in self.instances.keys() {
1823             let instance = StoreInstanceId::new(self.id(), id);
1824             for table in 0..self.instance(id).env_module().num_defined_tables() {
1825                 let table = DefinedTableIndex::new(table);
1826                 f(self, Table::from_raw(instance, table));
1827             }
1828         }
1829     }
1830 
1831     /// Iterate over all globals (host- or Wasm-defined) within this store.
1832     pub fn for_each_global(&mut self, mut f: impl FnMut(&mut Self, Global)) {
1833         // First enumerate all the host-created globals.
1834         for global in self.host_globals.keys() {
1835             let global = Global::new_host(self, global);
1836             f(self, global);
1837         }
1838 
1839         // Then enumerate all instances' defined globals.
1840         for id in self.instances.keys() {
1841             for index in 0..self.instance(id).env_module().num_defined_globals() {
1842                 let index = DefinedGlobalIndex::new(index);
1843                 let global = Global::new_instance(self, id, index);
1844                 f(self, global);
1845             }
1846         }
1847     }
1848 
1849     #[cfg(all(feature = "std", any(unix, windows)))]
1850     pub fn set_signal_handler(&mut self, handler: Option<SignalHandler>) {
1851         self.signal_handler = handler;
1852     }
1853 
1854     #[inline]
1855     pub fn vm_store_context(&self) -> &VMStoreContext {
1856         &self.vm_store_context
1857     }
1858 
1859     #[inline]
1860     pub fn vm_store_context_mut(&mut self) -> &mut VMStoreContext {
1861         &mut self.vm_store_context
1862     }
1863 
1864     /// Performs a lazy allocation of the `GcStore` within this store, returning
1865     /// the previous allocation if it's already present.
1866     ///
1867     /// This method will, if necessary, allocate a new `GcStore` -- linear
1868     /// memory and all. This is a blocking operation due to
1869     /// `ResourceLimiterAsync` which means that this should only be executed
1870     /// in a fiber context at this time.
1871     #[inline]
1872     pub(crate) async fn ensure_gc_store(
1873         &mut self,
1874         limiter: Option<&mut StoreResourceLimiter<'_>>,
1875     ) -> Result<&mut GcStore> {
1876         if self.gc_store.is_some() {
1877             return Ok(self.gc_store.as_mut().unwrap());
1878         }
1879         self.allocate_gc_store(limiter).await
1880     }
1881 
1882     #[inline(never)]
1883     async fn allocate_gc_store(
1884         &mut self,
1885         limiter: Option<&mut StoreResourceLimiter<'_>>,
1886     ) -> Result<&mut GcStore> {
1887         log::trace!("allocating GC heap for store {:?}", self.id());
1888 
1889         assert!(self.gc_store.is_none());
1890         assert_eq!(
1891             self.vm_store_context.gc_heap.base.as_non_null(),
1892             NonNull::dangling(),
1893         );
1894         assert_eq!(self.vm_store_context.gc_heap.current_length(), 0);
1895 
1896         let gc_store = allocate_gc_store(self, limiter).await?;
1897         self.vm_store_context.gc_heap = gc_store.vmmemory_definition();
1898         return Ok(self.gc_store.insert(gc_store));
1899 
1900         #[cfg(feature = "gc")]
1901         async fn allocate_gc_store(
1902             store: &mut StoreOpaque,
1903             limiter: Option<&mut StoreResourceLimiter<'_>>,
1904         ) -> Result<GcStore> {
1905             use wasmtime_environ::packed_option::ReservedValue;
1906 
1907             let engine = store.engine();
1908             let mem_ty = engine.tunables().gc_heap_memory_type();
1909             ensure!(
1910                 engine.features().gc_types(),
1911                 "cannot allocate a GC store when GC is disabled at configuration time"
1912             );
1913 
1914             // First, allocate the memory that will be our GC heap's storage.
1915             let mut request = InstanceAllocationRequest {
1916                 id: InstanceId::reserved_value(),
1917                 runtime_info: engine.empty_module_runtime_info(),
1918                 imports: vm::Imports::default(),
1919                 store,
1920                 limiter,
1921             };
1922 
1923             let (mem_alloc_index, mem) = engine
1924                 .allocator()
1925                 .allocate_memory(&mut request, &mem_ty, None)
1926                 .await?;
1927 
1928             // Then, allocate the actual GC heap, passing in that memory
1929             // storage.
1930             let gc_runtime = engine
1931                 .gc_runtime()
1932                 .context("no GC runtime: GC disabled at compile time or configuration time")?;
1933             let (index, heap) =
1934                 engine
1935                     .allocator()
1936                     .allocate_gc_heap(engine, &**gc_runtime, mem_alloc_index, mem)?;
1937 
1938             Ok(GcStore::new(index, heap))
1939         }
1940 
1941         #[cfg(not(feature = "gc"))]
1942         async fn allocate_gc_store(
1943             _: &mut StoreOpaque,
1944             _: Option<&mut StoreResourceLimiter<'_>>,
1945         ) -> Result<GcStore> {
1946             bail!("cannot allocate a GC store: the `gc` feature was disabled at compile time")
1947         }
1948     }
1949 
1950     /// Helper method to require that a `GcStore` was previously allocated for
1951     /// this store, failing if it has not yet been allocated.
1952     ///
1953     /// Note that this should only be used in a context where allocation of a
1954     /// `GcStore` is sure to have already happened prior, otherwise this may
1955     /// return a confusing error to embedders which is a bug in Wasmtime.
1956     ///
1957     /// Some situations where it's safe to call this method:
1958     ///
1959     /// * There's already a non-null and non-i31 `VMGcRef` in scope. By existing
1960     ///   this shows proof that the `GcStore` was previously allocated.
1961     /// * During instantiation and instance's `needs_gc_heap` flag will be
1962     ///   handled and instantiation will automatically create a GC store.
1963     #[inline]
1964     #[cfg(feature = "gc")]
1965     pub(crate) fn require_gc_store(&self) -> Result<&GcStore> {
1966         match &self.gc_store {
1967             Some(gc_store) => Ok(gc_store),
1968             None => bail!("GC heap not initialized yet"),
1969         }
1970     }
1971 
1972     /// Same as [`Self::require_gc_store`], but mutable.
1973     #[inline]
1974     #[cfg(feature = "gc")]
1975     pub(crate) fn require_gc_store_mut(&mut self) -> Result<&mut GcStore> {
1976         match &mut self.gc_store {
1977             Some(gc_store) => Ok(gc_store),
1978             None => bail!("GC heap not initialized yet"),
1979         }
1980     }
1981 
1982     /// Attempts to access the GC store that has been previously allocated.
1983     ///
1984     /// This method will return `Some` if the GC store was previously allocated.
1985     /// A `None` return value means either that the GC heap hasn't yet been
1986     /// allocated or that it does not need to be allocated for this store. Note
1987     /// that to require a GC store in a particular situation it's recommended to
1988     /// use [`Self::require_gc_store_mut`] instead.
1989     #[inline]
1990     pub(crate) fn optional_gc_store_mut(&mut self) -> Option<&mut GcStore> {
1991         if cfg!(not(feature = "gc")) || !self.engine.features().gc_types() {
1992             debug_assert!(self.gc_store.is_none());
1993             None
1994         } else {
1995             self.gc_store.as_mut()
1996         }
1997     }
1998 
1999     /// Helper to assert that a GC store was previously allocated and is
2000     /// present.
2001     ///
2002     /// # Panics
2003     ///
2004     /// This method will panic if the GC store has not yet been allocated. This
2005     /// should only be used in a context where there's an existing GC reference,
2006     /// for example, or if `ensure_gc_store` has already been called.
2007     #[inline]
2008     #[track_caller]
2009     pub(crate) fn unwrap_gc_store(&self) -> &GcStore {
2010         self.gc_store
2011             .as_ref()
2012             .expect("attempted to access the store's GC heap before it has been allocated")
2013     }
2014 
2015     /// Same as [`Self::unwrap_gc_store`], but mutable.
2016     #[inline]
2017     #[track_caller]
2018     pub(crate) fn unwrap_gc_store_mut(&mut self) -> &mut GcStore {
2019         self.gc_store
2020             .as_mut()
2021             .expect("attempted to access the store's GC heap before it has been allocated")
2022     }
2023 
2024     #[inline]
2025     pub(crate) fn gc_roots(&self) -> &RootSet {
2026         &self.gc_roots
2027     }
2028 
2029     #[inline]
2030     #[cfg(feature = "gc")]
2031     pub(crate) fn gc_roots_mut(&mut self) -> &mut RootSet {
2032         &mut self.gc_roots
2033     }
2034 
2035     #[inline]
2036     pub(crate) fn exit_gc_lifo_scope(&mut self, scope: usize) {
2037         self.gc_roots.exit_lifo_scope(self.gc_store.as_mut(), scope);
2038     }
2039 
2040     #[cfg(feature = "gc")]
2041     async fn do_gc(&mut self, asyncness: Asyncness) {
2042         // If the GC heap hasn't been initialized, there is nothing to collect.
2043         if self.gc_store.is_none() {
2044             return;
2045         }
2046 
2047         log::trace!("============ Begin GC ===========");
2048 
2049         // Take the GC roots out of `self` so we can borrow it mutably but still
2050         // call mutable methods on `self`.
2051         let mut roots = core::mem::take(&mut self.gc_roots_list);
2052 
2053         self.trace_roots(&mut roots, asyncness).await;
2054         self.unwrap_gc_store_mut()
2055             .gc(asyncness, unsafe { roots.iter() })
2056             .await;
2057 
2058         // Restore the GC roots for the next GC.
2059         roots.clear();
2060         self.gc_roots_list = roots;
2061 
2062         log::trace!("============ End GC ===========");
2063     }
2064 
2065     #[cfg(feature = "gc")]
2066     async fn trace_roots(&mut self, gc_roots_list: &mut GcRootsList, asyncness: Asyncness) {
2067         log::trace!("Begin trace GC roots");
2068 
2069         // We shouldn't have any leftover, stale GC roots.
2070         assert!(gc_roots_list.is_empty());
2071 
2072         self.trace_wasm_stack_roots(gc_roots_list);
2073         if asyncness != Asyncness::No {
2074             vm::Yield::new().await;
2075         }
2076         #[cfg(feature = "stack-switching")]
2077         {
2078             self.trace_wasm_continuation_roots(gc_roots_list);
2079             if asyncness != Asyncness::No {
2080                 vm::Yield::new().await;
2081             }
2082         }
2083         self.trace_vmctx_roots(gc_roots_list);
2084         if asyncness != Asyncness::No {
2085             vm::Yield::new().await;
2086         }
2087         self.trace_user_roots(gc_roots_list);
2088         self.trace_pending_exception_roots(gc_roots_list);
2089 
2090         log::trace!("End trace GC roots")
2091     }
2092 
2093     #[cfg(feature = "gc")]
2094     fn trace_wasm_stack_frame(
2095         &self,
2096         gc_roots_list: &mut GcRootsList,
2097         frame: crate::runtime::vm::Frame,
2098     ) {
2099         let pc = frame.pc();
2100         debug_assert!(pc != 0, "we should always get a valid PC for Wasm frames");
2101 
2102         let fp = frame.fp() as *mut usize;
2103         debug_assert!(
2104             !fp.is_null(),
2105             "we should always get a valid frame pointer for Wasm frames"
2106         );
2107 
2108         let (module_with_code, _offset) = self
2109             .modules()
2110             .module_and_code_by_pc(pc)
2111             .expect("should have module info for Wasm frame");
2112 
2113         if let Some(stack_map) = module_with_code.lookup_stack_map(pc) {
2114             log::trace!(
2115                 "We have a stack map that maps {} bytes in this Wasm frame",
2116                 stack_map.frame_size()
2117             );
2118 
2119             let sp = unsafe { stack_map.sp(fp) };
2120             for stack_slot in unsafe { stack_map.live_gc_refs(sp) } {
2121                 unsafe {
2122                     self.trace_wasm_stack_slot(gc_roots_list, stack_slot);
2123                 }
2124             }
2125         }
2126 
2127         #[cfg(feature = "debug")]
2128         if let Some(frame_table) = module_with_code.module().frame_table() {
2129             let relpc = module_with_code
2130                 .text_offset(pc)
2131                 .expect("PC should be within module");
2132             for stack_slot in super::debug::gc_refs_in_frame(frame_table, relpc, fp) {
2133                 unsafe {
2134                     self.trace_wasm_stack_slot(gc_roots_list, stack_slot);
2135                 }
2136             }
2137         }
2138     }
2139 
2140     #[cfg(feature = "gc")]
2141     unsafe fn trace_wasm_stack_slot(&self, gc_roots_list: &mut GcRootsList, stack_slot: *mut u32) {
2142         use crate::runtime::vm::SendSyncPtr;
2143         use core::ptr::NonNull;
2144 
2145         let raw: u32 = unsafe { core::ptr::read(stack_slot) };
2146         log::trace!("Stack slot @ {stack_slot:p} = {raw:#x}");
2147 
2148         let gc_ref = vm::VMGcRef::from_raw_u32(raw);
2149         if gc_ref.is_some() {
2150             unsafe {
2151                 gc_roots_list
2152                     .add_wasm_stack_root(SendSyncPtr::new(NonNull::new(stack_slot).unwrap()));
2153             }
2154         }
2155     }
2156 
2157     #[cfg(feature = "gc")]
2158     fn trace_wasm_stack_roots(&mut self, gc_roots_list: &mut GcRootsList) {
2159         use crate::runtime::vm::Backtrace;
2160         log::trace!("Begin trace GC roots :: Wasm stack");
2161 
2162         Backtrace::trace(self, |frame| {
2163             self.trace_wasm_stack_frame(gc_roots_list, frame);
2164             core::ops::ControlFlow::Continue(())
2165         });
2166 
2167         log::trace!("End trace GC roots :: Wasm stack");
2168     }
2169 
2170     #[cfg(all(feature = "gc", feature = "stack-switching"))]
2171     fn trace_wasm_continuation_roots(&mut self, gc_roots_list: &mut GcRootsList) {
2172         use crate::{runtime::vm::Backtrace, vm::VMStackState};
2173         log::trace!("Begin trace GC roots :: continuations");
2174 
2175         for continuation in &self.continuations {
2176             let state = continuation.common_stack_information.state;
2177 
2178             // FIXME(frank-emrich) In general, it is not enough to just trace
2179             // through the stacks of continuations; we also need to look through
2180             // their `cont.bind` arguments. However, we don't currently have
2181             // enough RTTI information to check if any of the values in the
2182             // buffers used by `cont.bind` are GC values. As a workaround, note
2183             // that we currently disallow cont.bind-ing GC values altogether.
2184             // This way, it is okay not to check them here.
2185             match state {
2186                 VMStackState::Suspended => {
2187                     Backtrace::trace_suspended_continuation(self, continuation.deref(), |frame| {
2188                         self.trace_wasm_stack_frame(gc_roots_list, frame);
2189                         core::ops::ControlFlow::Continue(())
2190                     });
2191                 }
2192                 VMStackState::Running => {
2193                     // Handled by `trace_wasm_stack_roots`.
2194                 }
2195                 VMStackState::Parent => {
2196                     // We don't know whether our child is suspended or running, but in
2197                     // either case things should be handled correctly when traversing
2198                     // further along in the chain, nothing required at this point.
2199                 }
2200                 VMStackState::Fresh | VMStackState::Returned => {
2201                     // Fresh/Returned continuations have no gc values on their stack.
2202                 }
2203             }
2204         }
2205 
2206         log::trace!("End trace GC roots :: continuations");
2207     }
2208 
2209     #[cfg(feature = "gc")]
2210     fn trace_vmctx_roots(&mut self, gc_roots_list: &mut GcRootsList) {
2211         log::trace!("Begin trace GC roots :: vmctx");
2212         self.for_each_global(|store, global| global.trace_root(store, gc_roots_list));
2213         self.for_each_table(|store, table| table.trace_roots(store, gc_roots_list));
2214         log::trace!("End trace GC roots :: vmctx");
2215     }
2216 
2217     #[cfg(feature = "gc")]
2218     fn trace_user_roots(&mut self, gc_roots_list: &mut GcRootsList) {
2219         log::trace!("Begin trace GC roots :: user");
2220         self.gc_roots.trace_roots(gc_roots_list);
2221         log::trace!("End trace GC roots :: user");
2222     }
2223 
2224     #[cfg(feature = "gc")]
2225     fn trace_pending_exception_roots(&mut self, gc_roots_list: &mut GcRootsList) {
2226         log::trace!("Begin trace GC roots :: pending exception");
2227         if let Some(pending_exception) = self.pending_exception.as_mut() {
2228             unsafe {
2229                 let root = pending_exception.as_gc_ref_mut();
2230                 gc_roots_list.add_root(root.into(), "Pending exception");
2231             }
2232         }
2233         log::trace!("End trace GC roots :: pending exception");
2234     }
2235 
2236     /// Insert a host-allocated GC type into this store.
2237     ///
2238     /// This makes it suitable for the embedder to allocate instances of this
2239     /// type in this store, and we don't have to worry about the type being
2240     /// reclaimed (since it is possible that none of the Wasm modules in this
2241     /// store are holding it alive).
2242     #[cfg(feature = "gc")]
2243     pub(crate) fn insert_gc_host_alloc_type(&mut self, ty: crate::type_registry::RegisteredType) {
2244         self.gc_host_alloc_types.insert(ty);
2245     }
2246 
2247     /// Helper function execute a `init_gc_ref` when placing `gc_ref` in `dest`.
2248     ///
2249     /// This avoids allocating `GcStore` where possible.
2250     pub(crate) fn init_gc_ref(
2251         &mut self,
2252         dest: &mut MaybeUninit<Option<VMGcRef>>,
2253         gc_ref: Option<&VMGcRef>,
2254     ) {
2255         if GcStore::needs_init_barrier(gc_ref) {
2256             self.unwrap_gc_store_mut().init_gc_ref(dest, gc_ref)
2257         } else {
2258             dest.write(gc_ref.map(|r| r.copy_i31()));
2259         }
2260     }
2261 
2262     /// Helper function execute a write barrier when placing `gc_ref` in `dest`.
2263     ///
2264     /// This avoids allocating `GcStore` where possible.
2265     pub(crate) fn write_gc_ref(&mut self, dest: &mut Option<VMGcRef>, gc_ref: Option<&VMGcRef>) {
2266         GcStore::write_gc_ref_optional_store(self.optional_gc_store_mut(), dest, gc_ref)
2267     }
2268 
2269     /// Helper function to clone `gc_ref` notably avoiding allocating a
2270     /// `GcStore` where possible.
2271     pub(crate) fn clone_gc_ref(&mut self, gc_ref: &VMGcRef) -> VMGcRef {
2272         if gc_ref.is_i31() {
2273             gc_ref.copy_i31()
2274         } else {
2275             self.unwrap_gc_store_mut().clone_gc_ref(gc_ref)
2276         }
2277     }
2278 
2279     pub fn get_fuel(&self) -> Result<u64> {
2280         crate::ensure!(
2281             self.engine().tunables().consume_fuel,
2282             "fuel is not configured in this store"
2283         );
2284         let injected_fuel = unsafe { *self.vm_store_context.fuel_consumed.get() };
2285         Ok(get_fuel(injected_fuel, self.fuel_reserve))
2286     }
2287 
2288     pub(crate) fn refuel(&mut self) -> bool {
2289         let injected_fuel = unsafe { &mut *self.vm_store_context.fuel_consumed.get() };
2290         refuel(
2291             injected_fuel,
2292             &mut self.fuel_reserve,
2293             self.fuel_yield_interval,
2294         )
2295     }
2296 
2297     pub fn set_fuel(&mut self, fuel: u64) -> Result<()> {
2298         crate::ensure!(
2299             self.engine().tunables().consume_fuel,
2300             "fuel is not configured in this store"
2301         );
2302         let injected_fuel = unsafe { &mut *self.vm_store_context.fuel_consumed.get() };
2303         set_fuel(
2304             injected_fuel,
2305             &mut self.fuel_reserve,
2306             self.fuel_yield_interval,
2307             fuel,
2308         );
2309         Ok(())
2310     }
2311 
2312     #[cfg(feature = "async")]
2313     pub fn fuel_async_yield_interval(&mut self, interval: Option<u64>) -> Result<()> {
2314         crate::ensure!(
2315             self.engine().tunables().consume_fuel,
2316             "fuel is not configured in this store"
2317         );
2318         crate::ensure!(
2319             interval != Some(0),
2320             "fuel_async_yield_interval must not be 0"
2321         );
2322 
2323         // All future entrypoints must be async to handle the case that fuel
2324         // runs out and an async yield is needed.
2325         self.set_async_required(Asyncness::Yes);
2326 
2327         self.fuel_yield_interval = interval.and_then(|i| NonZeroU64::new(i));
2328         // Reset the fuel active + reserve states by resetting the amount.
2329         self.set_fuel(self.get_fuel()?)
2330     }
2331 
2332     #[inline]
2333     pub fn signal_handler(&self) -> Option<*const SignalHandler> {
2334         let handler = self.signal_handler.as_ref()?;
2335         Some(handler)
2336     }
2337 
2338     #[inline]
2339     pub fn vm_store_context_ptr(&self) -> NonNull<VMStoreContext> {
2340         NonNull::from(&self.vm_store_context)
2341     }
2342 
2343     #[inline]
2344     pub fn default_caller(&self) -> NonNull<VMContext> {
2345         self.default_caller_vmctx.as_non_null()
2346     }
2347 
2348     #[inline]
2349     pub fn traitobj(&self) -> NonNull<dyn VMStore> {
2350         self.traitobj.0.unwrap()
2351     }
2352 
2353     /// Takes the cached `Vec<Val>` stored internally across hostcalls to get
2354     /// used as part of calling the host in a `Func::new` method invocation.
2355     #[inline]
2356     pub fn take_hostcall_val_storage(&mut self) -> Vec<Val> {
2357         mem::take(&mut self.hostcall_val_storage)
2358     }
2359 
2360     /// Restores the vector previously taken by `take_hostcall_val_storage`
2361     /// above back into the store, allowing it to be used in the future for the
2362     /// next wasm->host call.
2363     #[inline]
2364     pub fn save_hostcall_val_storage(&mut self, storage: Vec<Val>) {
2365         if storage.capacity() > self.hostcall_val_storage.capacity() {
2366             self.hostcall_val_storage = storage;
2367         }
2368     }
2369 
2370     /// Same as `take_hostcall_val_storage`, but for the direction of the host
2371     /// calling wasm.
2372     #[inline]
2373     pub fn take_wasm_val_raw_storage(&mut self) -> Vec<ValRaw> {
2374         mem::take(&mut self.wasm_val_raw_storage)
2375     }
2376 
2377     /// Same as `save_hostcall_val_storage`, but for the direction of the host
2378     /// calling wasm.
2379     #[inline]
2380     pub fn save_wasm_val_raw_storage(&mut self, storage: Vec<ValRaw>) {
2381         if storage.capacity() > self.wasm_val_raw_storage.capacity() {
2382             self.wasm_val_raw_storage = storage;
2383         }
2384     }
2385 
2386     /// Translates a WebAssembly fault at the native `pc` and native `addr` to a
2387     /// WebAssembly-relative fault.
2388     ///
2389     /// This function may abort the process if `addr` is not found to actually
2390     /// reside in any linear memory. In such a situation it means that the
2391     /// segfault was erroneously caught by Wasmtime and is possibly indicative
2392     /// of a code generator bug.
2393     ///
2394     /// This function returns `None` for dynamically-bounds-checked-memories
2395     /// with spectre mitigations enabled since the hardware fault address is
2396     /// always zero in these situations which means that the trapping context
2397     /// doesn't have enough information to report the fault address.
2398     pub(crate) fn wasm_fault(&self, pc: usize, addr: usize) -> Option<vm::WasmFault> {
2399         // There are a few instances where a "close to zero" pointer is loaded
2400         // and we expect that to happen:
2401         //
2402         // * Explicitly bounds-checked memories with spectre-guards enabled will
2403         //   cause out-of-bounds accesses to get routed to address 0, so allow
2404         //   wasm instructions to fault on the null address.
2405         // * `call_indirect` when invoking a null function pointer may load data
2406         //   from the a `VMFuncRef` whose address is null, meaning any field of
2407         //   `VMFuncRef` could be the address of the fault.
2408         //
2409         // In these situations where the address is so small it won't be in any
2410         // instance, so skip the checks below.
2411         if addr <= mem::size_of::<VMFuncRef>() {
2412             const _: () = {
2413                 // static-assert that `VMFuncRef` isn't too big to ensure that
2414                 // it lives solely within the first page as we currently only
2415                 // have the guarantee that the first page of memory is unmapped,
2416                 // no more.
2417                 assert!(mem::size_of::<VMFuncRef>() <= 512);
2418             };
2419             return None;
2420         }
2421 
2422         // Search all known instances in this store for this address. Note that
2423         // this is probably not the speediest way to do this. Traps, however,
2424         // are generally not expected to be super fast and additionally stores
2425         // probably don't have all that many instances or memories.
2426         //
2427         // If this loop becomes hot in the future, however, it should be
2428         // possible to precompute maps about linear memories in a store and have
2429         // a quicker lookup.
2430         let mut fault = None;
2431         for (_, instance) in self.instances.iter() {
2432             if let Some(f) = instance.handle.get().wasm_fault(addr) {
2433                 assert!(fault.is_none());
2434                 fault = Some(f);
2435             }
2436         }
2437         if fault.is_some() {
2438             return fault;
2439         }
2440 
2441         cfg_if::cfg_if! {
2442             if #[cfg(feature = "std")] {
2443                 // With the standard library a rich error can be printed here
2444                 // to stderr and the native abort path is used.
2445                 eprintln!(
2446                     "\
2447 Wasmtime caught a segfault for a wasm program because the faulting instruction
2448 is allowed to segfault due to how linear memories are implemented. The address
2449 that was accessed, however, is not known to any linear memory in use within this
2450 Store. This may be indicative of a critical bug in Wasmtime's code generation
2451 because all addresses which are known to be reachable from wasm won't reach this
2452 message.
2453 
2454     pc:      0x{pc:x}
2455     address: 0x{addr:x}
2456 
2457 This is a possible security issue because WebAssembly has accessed something it
2458 shouldn't have been able to. Other accesses may have succeeded and this one just
2459 happened to be caught. The process will now be aborted to prevent this damage
2460 from going any further and to alert what's going on. If this is a security
2461 issue please reach out to the Wasmtime team via its security policy
2462 at https://bytecodealliance.org/security.
2463 "
2464                 );
2465                 std::process::abort();
2466             } else if #[cfg(panic = "abort")] {
2467                 // Without the standard library but with `panic=abort` then
2468                 // it's safe to panic as that's known to halt execution. For
2469                 // now avoid the above error message as well since without
2470                 // `std` it's probably best to be a bit more size-conscious.
2471                 let _ = pc;
2472                 panic!("invalid fault");
2473             } else {
2474                 // Without `std` and with `panic = "unwind"` there's no
2475                 // dedicated API to abort the process portably, so manufacture
2476                 // this with a double-panic.
2477                 let _ = pc;
2478 
2479                 struct PanicAgainOnDrop;
2480 
2481                 impl Drop for PanicAgainOnDrop {
2482                     fn drop(&mut self) {
2483                         panic!("panicking again to trigger a process abort");
2484                     }
2485 
2486                 }
2487 
2488                 let _bomb = PanicAgainOnDrop;
2489 
2490                 panic!("invalid fault");
2491             }
2492         }
2493     }
2494 
2495     /// Retrieve the store's protection key.
2496     #[inline]
2497     #[cfg(feature = "pooling-allocator")]
2498     pub(crate) fn get_pkey(&self) -> Option<ProtectionKey> {
2499         self.pkey
2500     }
2501 
2502     #[cfg(feature = "async")]
2503     pub(crate) fn fiber_async_state_mut(&mut self) -> &mut fiber::AsyncState {
2504         &mut self.async_state
2505     }
2506 
2507     #[cfg(feature = "async")]
2508     pub(crate) fn has_pkey(&self) -> bool {
2509         self.pkey.is_some()
2510     }
2511 
2512     pub(crate) fn executor(&mut self) -> ExecutorRef<'_> {
2513         match &mut self.executor {
2514             Executor::Interpreter(i) => ExecutorRef::Interpreter(i.as_interpreter_ref()),
2515             #[cfg(has_host_compiler_backend)]
2516             Executor::Native => ExecutorRef::Native,
2517         }
2518     }
2519 
2520     #[cfg(feature = "async")]
2521     pub(crate) fn swap_executor(&mut self, executor: &mut Executor) {
2522         mem::swap(&mut self.executor, executor);
2523     }
2524 
2525     pub(crate) fn unwinder(&self) -> &'static dyn Unwind {
2526         match &self.executor {
2527             Executor::Interpreter(i) => i.unwinder(),
2528             #[cfg(has_host_compiler_backend)]
2529             Executor::Native => &vm::UnwindHost,
2530         }
2531     }
2532 
2533     /// Allocates a new continuation. Note that we currently don't support
2534     /// deallocating them. Instead, all continuations remain allocated
2535     /// throughout the store's lifetime.
2536     #[cfg(feature = "stack-switching")]
2537     pub fn allocate_continuation(&mut self) -> Result<*mut VMContRef> {
2538         // FIXME(frank-emrich) Do we need to pin this?
2539         let mut continuation = Box::new(VMContRef::empty());
2540         let stack_size = self.engine.config().async_stack_size;
2541         let stack = crate::vm::VMContinuationStack::new(stack_size)?;
2542         continuation.stack = stack;
2543         let ptr = continuation.deref_mut() as *mut VMContRef;
2544         self.continuations.push(continuation);
2545         Ok(ptr)
2546     }
2547 
2548     /// Constructs and executes an `InstanceAllocationRequest` and pushes the
2549     /// returned instance into the store.
2550     ///
2551     /// This is a helper method for invoking
2552     /// `InstanceAllocator::allocate_module` with the appropriate parameters
2553     /// from this store's own configuration. The `kind` provided is used to
2554     /// distinguish between "real" modules and dummy ones that are synthesized
2555     /// for embedder-created memories, globals, tables, etc. The `kind` will
2556     /// also use a different instance allocator by default, the one passed in,
2557     /// rather than the engine's default allocator.
2558     ///
2559     /// This method will push the instance within `StoreOpaque` onto the
2560     /// `instances` array and return the `InstanceId` which can be use to look
2561     /// it up within the store.
2562     ///
2563     /// # Safety
2564     ///
2565     /// The `imports` provided must be correctly sized/typed for the module
2566     /// being allocated.
2567     pub(crate) async unsafe fn allocate_instance(
2568         &mut self,
2569         limiter: Option<&mut StoreResourceLimiter<'_>>,
2570         kind: AllocateInstanceKind<'_>,
2571         runtime_info: &ModuleRuntimeInfo,
2572         imports: Imports<'_>,
2573     ) -> Result<InstanceId> {
2574         let id = self.instances.next_key();
2575 
2576         let allocator = match kind {
2577             AllocateInstanceKind::Module(_) => self.engine().allocator(),
2578             AllocateInstanceKind::Dummy { allocator } => allocator,
2579         };
2580         // SAFETY: this function's own contract is the same as
2581         // `allocate_module`, namely the imports provided are valid.
2582         let handle = unsafe {
2583             allocator
2584                 .allocate_module(InstanceAllocationRequest {
2585                     id,
2586                     runtime_info,
2587                     imports,
2588                     store: self,
2589                     limiter,
2590                 })
2591                 .await?
2592         };
2593 
2594         let actual = match kind {
2595             AllocateInstanceKind::Module(module_id) => {
2596                 log::trace!(
2597                     "Adding instance to store: store={:?}, module={module_id:?}, instance={id:?}",
2598                     self.id()
2599                 );
2600                 self.instances.push(StoreInstance {
2601                     handle,
2602                     kind: StoreInstanceKind::Real { module_id },
2603                 })?
2604             }
2605             AllocateInstanceKind::Dummy { .. } => {
2606                 log::trace!(
2607                     "Adding dummy instance to store: store={:?}, instance={id:?}",
2608                     self.id()
2609                 );
2610                 self.instances.push(StoreInstance {
2611                     handle,
2612                     kind: StoreInstanceKind::Dummy,
2613                 })?
2614             }
2615         };
2616 
2617         // double-check we didn't accidentally allocate two instances and our
2618         // prediction of what the id would be is indeed the id it should be.
2619         assert_eq!(id, actual);
2620 
2621         Ok(id)
2622     }
2623 
2624     /// Set a pending exception. The `exnref` is taken and held on
2625     /// this store to be fetched later by an unwind. This method does
2626     /// *not* set up an unwind request on the TLS call state; that
2627     /// must be done separately.
2628     #[cfg(feature = "gc")]
2629     pub(crate) fn set_pending_exception(&mut self, exnref: VMExnRef) {
2630         self.pending_exception = Some(exnref);
2631     }
2632 
2633     /// Take a pending exception, if any.
2634     #[cfg(feature = "gc")]
2635     pub(crate) fn take_pending_exception(&mut self) -> Option<VMExnRef> {
2636         self.pending_exception.take()
2637     }
2638 
2639     /// Tests whether there is a pending exception.
2640     #[cfg(feature = "gc")]
2641     pub fn has_pending_exception(&self) -> bool {
2642         self.pending_exception.is_some()
2643     }
2644 
2645     #[cfg(feature = "gc")]
2646     fn take_pending_exception_rooted(&mut self) -> Option<Rooted<ExnRef>> {
2647         let vmexnref = self.take_pending_exception()?;
2648         let mut nogc = AutoAssertNoGc::new(self);
2649         Some(Rooted::new(&mut nogc, vmexnref.into()))
2650     }
2651 
2652     /// Get an owned rooted reference to the pending exception,
2653     /// without taking it off the store.
2654     #[cfg(all(feature = "gc", feature = "debug"))]
2655     pub(crate) fn pending_exception_owned_rooted(
2656         &mut self,
2657     ) -> Result<Option<OwnedRooted<ExnRef>>, crate::error::OutOfMemory> {
2658         let mut nogc = AutoAssertNoGc::new(self);
2659         nogc.pending_exception
2660             .take()
2661             .map(|vmexnref| {
2662                 let cloned = nogc.clone_gc_ref(vmexnref.as_gc_ref());
2663                 nogc.pending_exception = Some(cloned.into_exnref_unchecked());
2664                 OwnedRooted::new(&mut nogc, vmexnref.into())
2665             })
2666             .transpose()
2667     }
2668 
2669     #[cfg(feature = "gc")]
2670     fn throw_impl(&mut self, exception: Rooted<ExnRef>) {
2671         let mut nogc = AutoAssertNoGc::new(self);
2672         let exnref = exception._to_raw(&mut nogc).unwrap();
2673         let exnref = VMGcRef::from_raw_u32(exnref)
2674             .expect("exception cannot be null")
2675             .into_exnref_unchecked();
2676         nogc.set_pending_exception(exnref);
2677     }
2678 
2679     #[cfg(target_has_atomic = "64")]
2680     pub(crate) fn set_epoch_deadline(&mut self, delta: u64) {
2681         // Set a new deadline based on the "epoch deadline delta".
2682         //
2683         // Also, note that when this update is performed while Wasm is
2684         // on the stack, the Wasm will reload the new value once we
2685         // return into it.
2686         let current_epoch = self.engine().current_epoch();
2687         let epoch_deadline = self.vm_store_context.epoch_deadline.get_mut();
2688         *epoch_deadline = current_epoch + delta;
2689     }
2690 
2691     pub(crate) fn get_epoch_deadline(&mut self) -> u64 {
2692         *self.vm_store_context.epoch_deadline.get_mut()
2693     }
2694 
2695     #[inline]
2696     pub(crate) fn validate_sync_call(&self) -> Result<()> {
2697         #[cfg(feature = "async")]
2698         if self.async_state.async_required {
2699             bail!("store configuration requires that `*_async` functions are used instead");
2700         }
2701         Ok(())
2702     }
2703 
2704     /// Returns whether this store is presently on a fiber and is allowed to
2705     /// block via `block_on` with fibers.
2706     pub(crate) fn can_block(&mut self) -> bool {
2707         #[cfg(feature = "async")]
2708         if true {
2709             return self.fiber_async_state_mut().can_block();
2710         }
2711 
2712         false
2713     }
2714 
2715     #[cfg(not(feature = "async"))]
2716     pub(crate) fn set_async_required(&mut self, asyncness: Asyncness) {
2717         match asyncness {
2718             Asyncness::No => {}
2719         }
2720     }
2721 }
2722 
2723 /// Helper parameter to [`StoreOpaque::allocate_instance`].
2724 pub(crate) enum AllocateInstanceKind<'a> {
2725     /// An embedder-provided module is being allocated meaning that the default
2726     /// engine's allocator will be used.
2727     Module(RegisteredModuleId),
2728 
2729     /// Add a dummy instance that to the store.
2730     ///
2731     /// These are instances that are just implementation details of something
2732     /// else (e.g. host-created memories that are not actually defined in any
2733     /// Wasm module) and therefore shouldn't show up in things like core dumps.
2734     ///
2735     /// A custom, typically OnDemand-flavored, allocator is provided to execute
2736     /// the allocation.
2737     Dummy {
2738         allocator: &'a dyn InstanceAllocator,
2739     },
2740 }
2741 
2742 unsafe impl<T> VMStore for StoreInner<T> {
2743     #[cfg(feature = "component-model-async")]
2744     fn component_async_store(
2745         &mut self,
2746     ) -> &mut dyn crate::runtime::component::VMComponentAsyncStore {
2747         self
2748     }
2749 
2750     #[cfg(feature = "component-model")]
2751     fn component_task_state_mut(&mut self) -> &mut crate::component::store::ComponentTaskState {
2752         StoreOpaque::component_task_state_mut(self)
2753     }
2754 
2755     fn store_opaque(&self) -> &StoreOpaque {
2756         &self.inner
2757     }
2758 
2759     fn store_opaque_mut(&mut self) -> &mut StoreOpaque {
2760         &mut self.inner
2761     }
2762 
2763     fn resource_limiter_and_store_opaque(
2764         &mut self,
2765     ) -> (Option<StoreResourceLimiter<'_>>, &mut StoreOpaque) {
2766         let (data, limiter, opaque) = self.data_limiter_and_opaque();
2767 
2768         let limiter = limiter.map(|l| match l {
2769             ResourceLimiterInner::Sync(s) => StoreResourceLimiter::Sync(s(data)),
2770             #[cfg(feature = "async")]
2771             ResourceLimiterInner::Async(s) => StoreResourceLimiter::Async(s(data)),
2772         });
2773 
2774         (limiter, opaque)
2775     }
2776 
2777     #[cfg(target_has_atomic = "64")]
2778     fn new_epoch_updated_deadline(&mut self) -> Result<UpdateDeadline> {
2779         // Temporarily take the configured behavior to avoid mutably borrowing
2780         // multiple times.
2781         let mut behavior = self.epoch_deadline_behavior.take();
2782         let update = match &mut behavior {
2783             Some(callback) => callback((&mut *self).as_context_mut()),
2784             None => Ok(UpdateDeadline::Interrupt),
2785         };
2786 
2787         // Put back the original behavior which was replaced by `take`.
2788         self.epoch_deadline_behavior = behavior;
2789         update
2790     }
2791 
2792     #[cfg(feature = "debug")]
2793     fn block_on_debug_handler(&mut self, event: crate::DebugEvent<'_>) -> crate::Result<()> {
2794         if let Some(handler) = self.debug_handler.take() {
2795             if !self.can_block() {
2796                 bail!("could not invoke debug handler without async context");
2797             }
2798             log::trace!("about to raise debug event {event:?}");
2799             StoreContextMut(self).with_blocking(|store, cx| {
2800                 cx.block_on(Pin::from(handler.handle(store, event)).as_mut())
2801             })
2802         } else {
2803             Ok(())
2804         }
2805     }
2806 }
2807 
2808 impl<T> StoreInner<T> {
2809     #[cfg(target_has_atomic = "64")]
2810     fn epoch_deadline_trap(&mut self) {
2811         self.epoch_deadline_behavior = None;
2812     }
2813 
2814     #[cfg(target_has_atomic = "64")]
2815     fn epoch_deadline_callback(
2816         &mut self,
2817         callback: Box<dyn FnMut(StoreContextMut<T>) -> Result<UpdateDeadline> + Send + Sync>,
2818     ) {
2819         self.epoch_deadline_behavior = Some(callback);
2820     }
2821 }
2822 
2823 impl<T: Default> Default for Store<T> {
2824     fn default() -> Store<T> {
2825         Store::new(&Engine::default(), T::default())
2826     }
2827 }
2828 
2829 impl<T: fmt::Debug> fmt::Debug for Store<T> {
2830     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2831         let inner = &**self.inner as *const StoreInner<T>;
2832         f.debug_struct("Store")
2833             .field("inner", &inner)
2834             .field("data", self.inner.data())
2835             .finish()
2836     }
2837 }
2838 
2839 impl<T> Drop for Store<T> {
2840     fn drop(&mut self) {
2841         self.run_manual_drop_routines();
2842 
2843         // For documentation on this `unsafe`, see `into_data`.
2844         unsafe {
2845             ManuallyDrop::drop(&mut self.inner.data_no_provenance);
2846             ManuallyDrop::drop(&mut self.inner);
2847         }
2848     }
2849 }
2850 
2851 impl Drop for StoreOpaque {
2852     fn drop(&mut self) {
2853         // NB it's important that this destructor does not access `self.data`.
2854         // That is deallocated by `Drop for Store<T>` above.
2855 
2856         unsafe {
2857             let allocator = self.engine.allocator();
2858             let ondemand = OnDemandInstanceAllocator::default();
2859             let store_id = self.id();
2860 
2861             #[cfg(feature = "gc")]
2862             if let Some(gc_store) = self.gc_store.take() {
2863                 let gc_alloc_index = gc_store.allocation_index;
2864                 log::trace!("store {store_id:?} is deallocating GC heap {gc_alloc_index:?}");
2865                 debug_assert!(self.engine.features().gc_types());
2866                 let (mem_alloc_index, mem) =
2867                     allocator.deallocate_gc_heap(gc_alloc_index, gc_store.gc_heap);
2868                 allocator.deallocate_memory(None, mem_alloc_index, mem);
2869             }
2870 
2871             for (id, instance) in self.instances.iter_mut() {
2872                 log::trace!("store {store_id:?} is deallocating {id:?}");
2873                 let allocator = match instance.kind {
2874                     StoreInstanceKind::Dummy => &ondemand,
2875                     _ => allocator,
2876                 };
2877                 allocator.deallocate_module(&mut instance.handle);
2878             }
2879 
2880             self.store_data.decrement_allocator_resources(allocator);
2881         }
2882     }
2883 }
2884 
2885 #[cfg_attr(
2886     not(any(feature = "gc", feature = "async")),
2887     // NB: Rust 1.89, current stable, does not fire this lint. Rust 1.90,
2888     // however, does, so use #[allow] until our MSRV is 1.90.
2889     allow(dead_code, reason = "don't want to put #[cfg] on all impls below too")
2890 )]
2891 pub(crate) trait AsStoreOpaque {
2892     fn as_store_opaque(&mut self) -> &mut StoreOpaque;
2893 }
2894 
2895 impl AsStoreOpaque for StoreOpaque {
2896     fn as_store_opaque(&mut self) -> &mut StoreOpaque {
2897         self
2898     }
2899 }
2900 
2901 impl AsStoreOpaque for dyn VMStore {
2902     fn as_store_opaque(&mut self) -> &mut StoreOpaque {
2903         self
2904     }
2905 }
2906 
2907 impl<T: 'static> AsStoreOpaque for Store<T> {
2908     fn as_store_opaque(&mut self) -> &mut StoreOpaque {
2909         &mut self.inner.inner
2910     }
2911 }
2912 
2913 impl<T: 'static> AsStoreOpaque for StoreInner<T> {
2914     fn as_store_opaque(&mut self) -> &mut StoreOpaque {
2915         self
2916     }
2917 }
2918 
2919 impl<T: AsStoreOpaque + ?Sized> AsStoreOpaque for &mut T {
2920     fn as_store_opaque(&mut self) -> &mut StoreOpaque {
2921         T::as_store_opaque(self)
2922     }
2923 }
2924 
2925 /// Helper enum to indicate, in some function contexts, whether `async` should
2926 /// be taken advantage of or not.
2927 ///
2928 /// This is used throughout Wasmtime where internal functions are all `async`
2929 /// but external functions might be either sync or `async`. If the external
2930 /// function is sync, then internally Wasmtime shouldn't yield as it won't do
2931 /// anything. If the external function is `async`, however, yields are fine.
2932 ///
2933 /// An example of this is GC. Right now GC will cooperatively yield after phases
2934 /// of GC have passed, but this cooperative yielding is only enabled with
2935 /// `Asyncness::Yes`.
2936 ///
2937 /// This enum is additionally conditionally defined such that `Yes` is only
2938 /// present in `async`-enabled builds. That ensures that this compiles down to a
2939 /// zero-sized type in `async`-disabled builds in case that interests embedders.
2940 #[derive(PartialEq, Eq, Copy, Clone)]
2941 pub enum Asyncness {
2942     /// Don't do async things, don't yield, etc. It's ok to execute an `async`
2943     /// function, but it should be validated ahead of time that when doing so a
2944     /// yield isn't possible (e.g. `validate_sync_*` methods on Store.
2945     No,
2946 
2947     /// Async things is OK. This should only be used when the API entrypoint is
2948     /// itself `async`.
2949     #[cfg(feature = "async")]
2950     Yes,
2951 }
2952 
2953 impl core::ops::BitOr for Asyncness {
2954     type Output = Self;
2955 
2956     fn bitor(self, rhs: Self) -> Self::Output {
2957         match (self, rhs) {
2958             (Asyncness::No, Asyncness::No) => Asyncness::No,
2959             #[cfg(feature = "async")]
2960             (Asyncness::Yes, _) | (_, Asyncness::Yes) => Asyncness::Yes,
2961         }
2962     }
2963 }
2964 
2965 #[cfg(test)]
2966 mod tests {
2967     use super::*;
2968 
2969     struct FuelTank {
2970         pub consumed_fuel: i64,
2971         pub reserve_fuel: u64,
2972         pub yield_interval: Option<NonZeroU64>,
2973     }
2974 
2975     impl FuelTank {
2976         fn new() -> Self {
2977             FuelTank {
2978                 consumed_fuel: 0,
2979                 reserve_fuel: 0,
2980                 yield_interval: None,
2981             }
2982         }
2983         fn get_fuel(&self) -> u64 {
2984             get_fuel(self.consumed_fuel, self.reserve_fuel)
2985         }
2986         fn refuel(&mut self) -> bool {
2987             refuel(
2988                 &mut self.consumed_fuel,
2989                 &mut self.reserve_fuel,
2990                 self.yield_interval,
2991             )
2992         }
2993         fn set_fuel(&mut self, fuel: u64) {
2994             set_fuel(
2995                 &mut self.consumed_fuel,
2996                 &mut self.reserve_fuel,
2997                 self.yield_interval,
2998                 fuel,
2999             );
3000         }
3001     }
3002 
3003     #[test]
3004     fn smoke() {
3005         let mut tank = FuelTank::new();
3006         tank.set_fuel(10);
3007         assert_eq!(tank.consumed_fuel, -10);
3008         assert_eq!(tank.reserve_fuel, 0);
3009 
3010         tank.yield_interval = NonZeroU64::new(10);
3011         tank.set_fuel(25);
3012         assert_eq!(tank.consumed_fuel, -10);
3013         assert_eq!(tank.reserve_fuel, 15);
3014     }
3015 
3016     #[test]
3017     fn does_not_lose_precision() {
3018         let mut tank = FuelTank::new();
3019         tank.set_fuel(u64::MAX);
3020         assert_eq!(tank.get_fuel(), u64::MAX);
3021 
3022         tank.set_fuel(i64::MAX as u64);
3023         assert_eq!(tank.get_fuel(), i64::MAX as u64);
3024 
3025         tank.set_fuel(i64::MAX as u64 + 1);
3026         assert_eq!(tank.get_fuel(), i64::MAX as u64 + 1);
3027     }
3028 
3029     #[test]
3030     fn yielding_does_not_lose_precision() {
3031         let mut tank = FuelTank::new();
3032 
3033         tank.yield_interval = NonZeroU64::new(10);
3034         tank.set_fuel(u64::MAX);
3035         assert_eq!(tank.get_fuel(), u64::MAX);
3036         assert_eq!(tank.consumed_fuel, -10);
3037         assert_eq!(tank.reserve_fuel, u64::MAX - 10);
3038 
3039         tank.yield_interval = NonZeroU64::new(u64::MAX);
3040         tank.set_fuel(u64::MAX);
3041         assert_eq!(tank.get_fuel(), u64::MAX);
3042         assert_eq!(tank.consumed_fuel, -i64::MAX);
3043         assert_eq!(tank.reserve_fuel, u64::MAX - (i64::MAX as u64));
3044 
3045         tank.yield_interval = NonZeroU64::new((i64::MAX as u64) + 1);
3046         tank.set_fuel(u64::MAX);
3047         assert_eq!(tank.get_fuel(), u64::MAX);
3048         assert_eq!(tank.consumed_fuel, -i64::MAX);
3049         assert_eq!(tank.reserve_fuel, u64::MAX - (i64::MAX as u64));
3050     }
3051 
3052     #[test]
3053     fn refueling() {
3054         // It's possible to fuel to have consumed over the limit as some instructions can consume
3055         // multiple units of fuel at once. Refueling should be strict in it's consumption and not
3056         // add more fuel than there is.
3057         let mut tank = FuelTank::new();
3058 
3059         tank.yield_interval = NonZeroU64::new(10);
3060         tank.reserve_fuel = 42;
3061         tank.consumed_fuel = 4;
3062         assert!(tank.refuel());
3063         assert_eq!(tank.reserve_fuel, 28);
3064         assert_eq!(tank.consumed_fuel, -10);
3065 
3066         tank.yield_interval = NonZeroU64::new(1);
3067         tank.reserve_fuel = 8;
3068         tank.consumed_fuel = 4;
3069         assert_eq!(tank.get_fuel(), 4);
3070         assert!(tank.refuel());
3071         assert_eq!(tank.reserve_fuel, 3);
3072         assert_eq!(tank.consumed_fuel, -1);
3073         assert_eq!(tank.get_fuel(), 4);
3074 
3075         tank.yield_interval = NonZeroU64::new(10);
3076         tank.reserve_fuel = 3;
3077         tank.consumed_fuel = 4;
3078         assert_eq!(tank.get_fuel(), 0);
3079         assert!(!tank.refuel());
3080         assert_eq!(tank.reserve_fuel, 3);
3081         assert_eq!(tank.consumed_fuel, 4);
3082         assert_eq!(tank.get_fuel(), 0);
3083     }
3084 
3085     #[test]
3086     fn store_data_provenance() {
3087         // Test that we juggle pointer provenance and all that correctly, and
3088         // miri is happy with everything, while allowing both Rust code and
3089         // "Wasm" to access and modify the store's `T` data. Note that this is
3090         // not actually Wasm mutating the store data here because compiling Wasm
3091         // under miri is way too slow.
3092 
3093         unsafe fn run_wasm(store: &mut Store<u32>) {
3094             let ptr = store
3095                 .inner
3096                 .inner
3097                 .vm_store_context
3098                 .store_data
3099                 .as_ptr()
3100                 .cast::<u32>();
3101             unsafe { *ptr += 1 }
3102         }
3103 
3104         let engine = Engine::default();
3105         let mut store = Store::new(&engine, 0_u32);
3106 
3107         assert_eq!(*store.data(), 0);
3108         *store.data_mut() += 1;
3109         assert_eq!(*store.data(), 1);
3110         unsafe { run_wasm(&mut store) }
3111         assert_eq!(*store.data(), 2);
3112         *store.data_mut() += 1;
3113         assert_eq!(*store.data(), 3);
3114     }
3115 }
3116