Skip to main content

wasmtime/runtime/vm/
instance.rs

1//! An `Instance` contains all the runtime state used by execution of a
2//! wasm module (except its callstack and register state). An
3//! `InstanceHandle` is a reference-counting handle for an `Instance`.
4
5use crate::code::ModuleWithCode;
6use crate::module::ModuleRegistry;
7use crate::prelude::*;
8use crate::runtime::vm::export::{Export, ExportMemory};
9use crate::runtime::vm::memory::{Memory, RuntimeMemoryCreator};
10use crate::runtime::vm::table::{Table, TableElementType};
11use crate::runtime::vm::vmcontext::{
12    VMBuiltinFunctionsArray, VMContext, VMFuncRef, VMFunctionImport, VMGlobalDefinition,
13    VMGlobalImport, VMMemoryDefinition, VMMemoryImport, VMOpaqueContext, VMStoreContext,
14    VMTableDefinition, VMTableImport, VMTagDefinition, VMTagImport,
15};
16use crate::runtime::vm::{
17    GcStore, HostResult, Imports, ModuleRuntimeInfo, SendSyncPtr, VMGcRef, VMGlobalKind, VMStore,
18    VMStoreRawPtr, VmPtr, VmSafe, WasmFault, catch_unwind_and_record_trap,
19};
20use crate::store::{InstanceId, StoreId, StoreInstanceId, StoreOpaque, StoreResourceLimiter};
21use crate::vm::{VMWasmCallFunction, ValRaw};
22use alloc::sync::Arc;
23use core::alloc::Layout;
24use core::marker;
25use core::ops::Range;
26use core::pin::Pin;
27use core::ptr::NonNull;
28#[cfg(target_has_atomic = "64")]
29use core::sync::atomic::AtomicU64;
30use core::{mem, ptr};
31#[cfg(feature = "gc")]
32use wasmtime_environ::ModuleInternedTypeIndex;
33use wasmtime_environ::error::OutOfMemory;
34use wasmtime_environ::{
35    Abi, DefinedFuncIndex, DefinedGlobalIndex, DefinedMemoryIndex, DefinedTableIndex,
36    DefinedTagIndex, EntityIndex, EntityRef, FuncIndex, FuncKey, GlobalConstValue, GlobalIndex,
37    HostPtr, MemoryIndex, MemoryInitialization, ModuleStartup, PassiveElemIndex, PtrSize,
38    RuntimeDataIndex, TableIndex, TagIndex, VMCONTEXT_MAGIC, VMOffsets, VMSharedTypeIndex,
39    WasmRefType, packed_option::ReservedValue,
40};
41#[cfg(feature = "wmemcheck")]
42use wasmtime_wmemcheck::Wmemcheck;
43
44mod allocator;
45pub use allocator::*;
46
47/// A type that roughly corresponds to a WebAssembly instance, but is also used
48/// for host-defined objects.
49///
50/// Instances here can correspond to actual instantiated modules, but it's also
51/// used ubiquitously for host-defined objects. For example creating a
52/// host-defined memory will have a `module` that looks like it exports a single
53/// memory (and similar for other constructs).
54///
55/// This `Instance` type is used as a ubiquitous representation for WebAssembly
56/// values, whether or not they were created on the host or through a module.
57///
58/// # Ownership
59///
60/// This structure is never allocated directly but is instead managed through
61/// an `InstanceHandle`. This structure ends with a `VMContext` which has a
62/// dynamic size corresponding to the `module` configured within. Memory
63/// management of this structure is always done through `InstanceHandle` as the
64/// sole owner of an instance.
65///
66/// # `Instance` and `Pin`
67///
68/// Given an instance it is accompanied with trailing memory for the
69/// appropriate `VMContext`. The `Instance` also holds `runtime_info` and other
70/// information pointing to relevant offsets for the `VMContext`. Thus it is
71/// not sound to mutate `runtime_info` after an instance is created. More
72/// generally it's also not safe to "swap" instances, for example given two
73/// `&mut Instance` values it's not sound to swap them as then the `VMContext`
74/// values are inaccurately described.
75///
76/// To encapsulate this guarantee this type is only ever mutated through Rust's
77/// `Pin` type. All mutable methods here take `self: Pin<&mut Self>` which
78/// statically disallows safe access to `&mut Instance`. There are assorted
79/// "projection methods" to go from `Pin<&mut Instance>` to `&mut T` for
80/// individual fields, for example `memories_mut`. More methods can be added as
81/// necessary or methods may also be added to project multiple fields at a time
82/// if necessary to. The precise ergonomics around getting mutable access to
83/// some fields (but notably not `runtime_info`) is probably going to evolve
84/// over time.
85///
86/// Note that is is not sound to basically ever pass around `&mut Instance`.
87/// That should always instead be `Pin<&mut Instance>`. All usage of
88/// `Pin::new_unchecked` should be here in this module in just a few `unsafe`
89/// locations and it's recommended to use existing helpers if you can.
90#[repr(C)] // ensure that the vmctx field is last.
91pub struct Instance {
92    /// The index, within a `Store` that this instance lives at
93    id: InstanceId,
94
95    /// The runtime info (corresponding to the "compiled module"
96    /// abstraction in higher layers) that is retained and needed for
97    /// lazy initialization. This provides access to the underlying
98    /// Wasm module entities, the compiled JIT code, metadata about
99    /// functions, lazy initialization state, etc.
100    //
101    // SAFETY: this field cannot be overwritten after an instance is created. It
102    // must contain this exact same value for the entire lifetime of this
103    // instance. This enables borrowing the info's `Module` and this instance at
104    // the same time (instance mutably, module not). Additionally it enables
105    // borrowing a store mutably at the same time as a contained instance.
106    runtime_info: ModuleRuntimeInfo,
107
108    /// WebAssembly linear memory data.
109    ///
110    /// This is where all runtime information about defined linear memories in
111    /// this module lives.
112    ///
113    /// The `MemoryAllocationIndex` was given from our `InstanceAllocator` and
114    /// must be given back to the instance allocator when deallocating each
115    /// memory.
116    memories: TryPrimaryMap<DefinedMemoryIndex, (MemoryAllocationIndex, Memory)>,
117
118    /// WebAssembly table data.
119    ///
120    /// Like memories, this is only for defined tables in the module and
121    /// contains all of their runtime state.
122    ///
123    /// The `TableAllocationIndex` was given from our `InstanceAllocator` and
124    /// must be given back to the instance allocator when deallocating each
125    /// table.
126    tables: TryPrimaryMap<DefinedTableIndex, (TableAllocationIndex, Table)>,
127
128    /// Evaluated passive element segments.
129    ///
130    /// If an entry is none, then it has been dropped.
131    //
132    // TODO(#12621): This should be a `TrySecondaryMap<PassiveElemIndex, _>`
133    // but that type is currently footgun-y / isn't actually OOM-safe yet.
134    passive_elements: TryVec<PassiveElementSegment>,
135
136    // TODO: add support for multiple memories; `wmemcheck_state` corresponds to
137    // memory 0.
138    #[cfg(feature = "wmemcheck")]
139    pub(crate) wmemcheck_state: Option<Wmemcheck>,
140
141    /// Self-pointer back to `Store<T>` and its functions. Not present for
142    /// the brief time that `Store<T>` is itself being created. Also not
143    /// present for some niche uses that are disconnected from stores (e.g.
144    /// cross-thread stuff used in `InstancePre`)
145    store: Option<VMStoreRawPtr>,
146
147    /// Additional context used by compiled wasm code. This field is last, and
148    /// represents a dynamically-sized array that extends beyond the nominal
149    /// end of the struct (similar to a flexible array member).
150    vmctx: OwnedVMContext<VMContext>,
151}
152
153impl Instance {
154    /// Creates a new owned instance handle from `req`.
155    ///
156    /// The runtime memory/table data structures must have been previously
157    /// allocated and are present within `memories` and `tables`. These values
158    /// are `mem::take`n upon allocation success of an instance, and if the
159    /// instance allocation fails then these are otherwise left in place. This
160    /// enable the pooling allocator to run custom deallocation code for them,
161    /// for example.
162    ///
163    /// # Safety
164    ///
165    /// The `req.imports` field must be appropriately sized/typed for the module
166    /// being allocated according to `req.runtime_info`. Additionally `memories`
167    /// and `tables` must have been allocated for `req.store`.
168    unsafe fn new(
169        req: InstanceAllocationRequest,
170        memories: &mut TryPrimaryMap<DefinedMemoryIndex, (MemoryAllocationIndex, Memory)>,
171        tables: &mut TryPrimaryMap<DefinedTableIndex, (TableAllocationIndex, Table)>,
172    ) -> Result<InstanceHandle, OutOfMemory> {
173        let module = req.runtime_info.env_module();
174        let memory_tys = &module.memories;
175        let mut passive_elements = TryVec::with_capacity(module.passive_elements.len())?;
176
177        #[cfg(feature = "wmemcheck")]
178        let wmemcheck_state = if req.store.engine().config().wmemcheck {
179            let size = memory_tys
180                .iter()
181                .next()
182                .map(|memory| memory.1.limits.min)
183                .unwrap_or(0)
184                * 64
185                * 1024;
186            Some(Wmemcheck::new(size.try_into().unwrap()))
187        } else {
188            None
189        };
190        #[cfg(not(feature = "wmemcheck"))]
191        let _ = memory_tys;
192
193        for (_, (ty, len)) in req.runtime_info.env_module().passive_elements.iter() {
194            let len = usize::try_from(*len).unwrap();
195            passive_elements.push(PassiveElementSegment::new(*ty, len)?)?;
196        }
197
198        // Allocate the instance and its `VMContext` with empty memory and table
199        // maps. This is the final fallible allocation in this function; only
200        // after it succeeds do we transfer ownership of the pool-allocated
201        // `memories`/`tables` into the instance (below), so that a failure here
202        // leaves them in the caller's deallocation guard to be freed.
203        let mut ret = OwnedInstance::new(Instance {
204            id: req.id,
205            runtime_info: req.runtime_info.clone(),
206            memories: TryPrimaryMap::default(),
207            tables: TryPrimaryMap::default(),
208            passive_elements,
209            #[cfg(feature = "wmemcheck")]
210            wmemcheck_state,
211            store: None,
212            vmctx: OwnedVMContext::new(),
213        })?;
214
215        // Can't fail any more, so transfer ownership of `memories` and `tables`
216        // to this instance.
217        *ret.get_mut().memories_mut() = mem::take(memories);
218        *ret.get_mut().tables_mut() = mem::take(tables);
219
220        // SAFETY: this vmctx was allocated with the same layout above, so it
221        // should be safe to initialize with the same values here.
222        unsafe {
223            ret.get_mut().initialize_vmctx(req.store, req.imports);
224        }
225
226        Ok(ret)
227    }
228
229    /// Trace element segment GC roots inside this `Instance`.
230    ///
231    /// # Safety
232    ///
233    /// This instance must live for the duration of the associated GC cycle.
234    #[cfg(feature = "gc")]
235    pub(crate) unsafe fn trace_element_segment_roots(
236        self: Pin<&mut Self>,
237        gc_roots: &mut crate::vm::GcRootsList,
238    ) {
239        for segment in self.passive_elements_mut().iter_mut() {
240            if segment.needs_gc_rooting {
241                for e in segment.elements_mut() {
242                    if e.get_vmgcref().is_none() {
243                        continue;
244                    }
245
246                    let root: SendSyncPtr<ValRaw> = e.into();
247
248                    // Safety: We know this is a type that needs GC rooting and
249                    // the lifetime is implied by our safety contract.
250                    unsafe {
251                        gc_roots.add_val_raw_root(root, "passive element segment");
252                    }
253                }
254            }
255        }
256    }
257
258    /// Converts a raw `VMContext` pointer into a raw `Instance` pointer.
259    ///
260    /// # Safety
261    ///
262    /// Calling this function safely requires that `vmctx` is a valid allocation
263    /// of a `VMContext` which is derived from `Instance::new`. To safely
264    /// convert the returned raw pointer into a safe instance pointer callers
265    /// will also want to uphold guarantees such as:
266    ///
267    /// * The instance should not be in use elsewhere. For example you can't
268    ///   call this function twice, turn both raw pointers into safe pointers,
269    ///   and then use both safe pointers.
270    /// * There should be no other active mutable borrow to any other instance
271    ///   within the same store. Note that this is not restricted to just this
272    ///   instance pointer, but to all instances in a store. Instances can
273    ///   safely traverse to other instances "laterally" meaning that a mutable
274    ///   borrow on one is a mutable borrow on all.
275    /// * There should be no active mutable borrow on the store accessible at
276    ///   the same time the instance is turned. Instances are owned by a store
277    ///   and a store can be used to acquire a safe instance borrow at any time.
278    /// * The lifetime of the usage of the instance should not be unnecessarily
279    ///   long, for example it cannot be `'static`.
280    ///
281    /// Other entrypoints exist for converting from a raw `VMContext` to a safe
282    /// pointer such as:
283    ///
284    /// * `Instance::enter_host_from_wasm`
285    /// * `Instance::sibling_vmctx{,_mut}`
286    ///
287    /// These place further restrictions on the API signature to satisfy some of
288    /// the above points.
289    #[inline]
290    pub(crate) unsafe fn from_vmctx(vmctx: NonNull<VMContext>) -> NonNull<Instance> {
291        // SAFETY: The validity of `byte_sub` relies on `vmctx` being a valid
292        // allocation.
293        unsafe {
294            vmctx
295                .byte_sub(mem::size_of::<Instance>())
296                .cast::<Instance>()
297        }
298    }
299
300    /// Encapsulated entrypoint to the host from WebAssembly, converting a raw
301    /// `VMContext` pointer into a `VMStore` plus an `InstanceId`.
302    ///
303    /// This is an entrypoint for core wasm entering back into the host. This is
304    /// used for both host functions and libcalls for example. This will execute
305    /// the closure `f` with safer Internal types than a raw `VMContext`
306    /// pointer.
307    ///
308    /// The closure `f` will have its errors caught, handled, and translated to
309    /// an ABI-safe return value to give back to wasm. This includes both normal
310    /// errors such as traps as well as panics.
311    ///
312    /// # Safety
313    ///
314    /// Callers must ensure that `vmctx` is a valid allocation and is safe to
315    /// dereference at this time. That's generally only true when it's a
316    /// wasm-provided value and this is the first function called after entering
317    /// the host. Otherwise this could unsafely alias the store with a mutable
318    /// pointer, for example.
319    #[inline]
320    pub(crate) unsafe fn enter_host_from_wasm<R>(
321        vmctx: NonNull<VMContext>,
322        f: impl FnOnce(&mut dyn VMStore, InstanceId) -> R,
323    ) -> R::Abi
324    where
325        R: HostResult,
326    {
327        // SAFETY: It's a contract of this function that `vmctx` is a valid
328        // pointer with neither the store nor other instances actively in use
329        // when this is called, so it should be safe to acquire a mutable
330        // pointer to the store and read the instance pointer.
331        let (store, instance) = unsafe {
332            let instance = Instance::from_vmctx(vmctx);
333            let instance = instance.as_ref();
334            let store = &mut *instance.store.unwrap().0.as_ptr();
335            (store, instance.id)
336        };
337
338        // Thread the `store` and `instance` through panic/trap infrastructure
339        // back into `f`.
340        catch_unwind_and_record_trap(store, |store| f(store, instance))
341    }
342
343    /// Converts the provided `*mut VMContext` to an `Instance` pointer and
344    /// returns it with the same lifetime as `self`.
345    ///
346    /// This function can be used when traversing a `VMContext` to reach into
347    /// the context needed for imports, optionally.
348    ///
349    /// # Safety
350    ///
351    /// This function requires that the `vmctx` pointer is indeed valid and
352    /// from the store that `self` belongs to.
353    #[inline]
354    unsafe fn sibling_vmctx<'a>(&'a self, vmctx: NonNull<VMContext>) -> &'a Instance {
355        // SAFETY: it's a contract of this function itself that `vmctx` is a
356        // valid pointer. Additionally with `self` being a
357        let ptr = unsafe { Instance::from_vmctx(vmctx) };
358        // SAFETY: it's a contract of this function itself that `vmctx` is a
359        // valid pointer to dereference. Additionally the lifetime of the return
360        // value is constrained to be the same as `self` to avoid granting a
361        // too-long lifetime.
362        unsafe { ptr.as_ref() }
363    }
364
365    /// Same as [`Self::sibling_vmctx`], but the mutable version.
366    ///
367    /// # Safety
368    ///
369    /// This function requires that the `vmctx` pointer is indeed valid and
370    /// from the store that `self` belongs to.
371    ///
372    /// (Note that it is *NOT* required that `vmctx` be distinct from this
373    /// instance's `vmctx`, or that usage of the resulting instance is limited
374    /// to its defined items! The returned borrow has the same lifetime as
375    /// `self`, which means that this instance cannot be used while the
376    /// resulting instance is in use, and we therefore do not need to worry
377    /// about mutable aliasing between this instance and the resulting
378    /// instance.)
379    #[inline]
380    unsafe fn sibling_vmctx_mut<'a>(
381        self: Pin<&'a mut Self>,
382        vmctx: NonNull<VMContext>,
383    ) -> Pin<&'a mut Instance> {
384        // SAFETY: it's a contract of this function itself that `vmctx` is a
385        // valid pointer such that this pointer arithmetic is valid.
386        let mut ptr = unsafe { Instance::from_vmctx(vmctx) };
387
388        // SAFETY: it's a contract of this function itself that `vmctx` is a
389        // valid pointer to dereference. Additionally the lifetime of the return
390        // value is constrained to be the same as `self` to avoid granting a
391        // too-long lifetime. Finally mutable references to an instance are
392        // always through `Pin`, so it's safe to create a pin-pointer here.
393        unsafe { Pin::new_unchecked(ptr.as_mut()) }
394    }
395
396    pub(crate) fn env_module(&self) -> &Arc<wasmtime_environ::Module> {
397        self.runtime_info.env_module()
398    }
399
400    pub(crate) fn runtime_module(&self) -> Option<&crate::Module> {
401        match &self.runtime_info {
402            ModuleRuntimeInfo::Module(m) => Some(m),
403            ModuleRuntimeInfo::Bare(_) => None,
404        }
405    }
406
407    /// Translate a module-level interned type index into an engine-level
408    /// interned type index.
409    #[cfg(feature = "gc")]
410    pub fn engine_type_index(&self, module_index: ModuleInternedTypeIndex) -> VMSharedTypeIndex {
411        self.runtime_info.engine_type_index(module_index)
412    }
413
414    #[inline]
415    fn offsets(&self) -> &VMOffsets<HostPtr> {
416        self.runtime_info.offsets()
417    }
418
419    /// Return the indexed `VMFunctionImport`.
420    fn imported_function(&self, index: FuncIndex) -> &VMFunctionImport {
421        unsafe { self.vmctx_plus_offset(self.offsets().imported_functions().at(index)) }
422    }
423
424    /// Return the index `VMTableImport`.
425    fn imported_table(&self, index: TableIndex) -> &VMTableImport {
426        unsafe { self.vmctx_plus_offset(self.offsets().imported_tables().at(index)) }
427    }
428
429    /// Return the indexed `VMMemoryImport`.
430    fn imported_memory(&self, index: MemoryIndex) -> &VMMemoryImport {
431        unsafe { self.vmctx_plus_offset(self.offsets().imported_memories().at(index)) }
432    }
433
434    /// Return the indexed `VMGlobalImport`.
435    fn imported_global(&self, index: GlobalIndex) -> &VMGlobalImport {
436        unsafe { self.vmctx_plus_offset(self.offsets().imported_globals().at(index)) }
437    }
438
439    /// Return the indexed `VMTagImport`.
440    fn imported_tag(&self, index: TagIndex) -> &VMTagImport {
441        unsafe { self.vmctx_plus_offset(self.offsets().imported_tags().at(index)) }
442    }
443
444    /// Return the indexed `VMTagDefinition`.
445    pub fn tag_ptr(&self, index: DefinedTagIndex) -> NonNull<VMTagDefinition> {
446        unsafe { self.vmctx_plus_offset_raw(self.offsets().tags().at(index)) }
447    }
448
449    /// Return the indexed `VMTableDefinition`.
450    pub fn table(&self, index: DefinedTableIndex) -> VMTableDefinition {
451        unsafe { self.table_ptr(index).read() }
452    }
453
454    /// Updates the value for a defined table to `VMTableDefinition`.
455    fn set_table(self: Pin<&mut Self>, index: DefinedTableIndex, table: VMTableDefinition) {
456        unsafe {
457            self.table_ptr(index).write(table);
458        }
459    }
460
461    /// Return a pointer to the `index`'th table within this instance, stored
462    /// in vmctx memory.
463    pub fn table_ptr(&self, index: DefinedTableIndex) -> NonNull<VMTableDefinition> {
464        unsafe { self.vmctx_plus_offset_raw(self.offsets().tables().at(index)) }
465    }
466
467    /// Get a locally defined or imported memory.
468    #[cfg(all(has_host_compiler_backend, feature = "debug-builtins"))]
469    pub(crate) fn get_memory(&self, index: MemoryIndex) -> VMMemoryDefinition {
470        if let Some(defined_index) = self.env_module().defined_memory_index(index) {
471            self.memory(defined_index)
472        } else {
473            let import = self.imported_memory(index);
474            unsafe { VMMemoryDefinition::load(import.from.as_ptr()) }
475        }
476    }
477
478    /// Return the indexed `VMMemoryDefinition`, loaded from vmctx memory
479    /// already.
480    #[inline]
481    pub fn memory(&self, index: DefinedMemoryIndex) -> VMMemoryDefinition {
482        unsafe { VMMemoryDefinition::load(self.memory_ptr(index).as_ptr()) }
483    }
484
485    /// Set the indexed memory to `VMMemoryDefinition`.
486    fn set_memory(&self, index: DefinedMemoryIndex, mem: VMMemoryDefinition) {
487        unsafe {
488            self.memory_ptr(index).write(mem);
489        }
490    }
491
492    /// Return the address of the specified memory at `index` within this vmctx.
493    ///
494    /// Note that the returned pointer resides in wasm-code-readable-memory in
495    /// the vmctx.
496    #[inline]
497    pub fn memory_ptr(&self, index: DefinedMemoryIndex) -> NonNull<VMMemoryDefinition> {
498        unsafe {
499            self.vmctx_plus_offset::<VmPtr<_>>(self.offsets().memories().at(index))
500                .as_non_null()
501        }
502    }
503
504    /// Return the indexed `VMGlobalDefinition`.
505    pub fn global_ptr(&self, index: DefinedGlobalIndex) -> NonNull<VMGlobalDefinition> {
506        unsafe { self.vmctx_plus_offset_raw(self.offsets().globals().at(index)) }
507    }
508
509    /// Get all globals within this instance.
510    ///
511    /// Returns both import and defined globals.
512    ///
513    /// Returns both exported and non-exported globals.
514    ///
515    /// Gives access to the full globals space.
516    pub fn all_globals(
517        &self,
518        store: StoreId,
519    ) -> impl ExactSizeIterator<Item = (GlobalIndex, crate::Global)> + '_ {
520        let module = self.env_module();
521        module
522            .globals
523            .keys()
524            .map(move |idx| (idx, self.get_exported_global(store, idx)))
525    }
526
527    /// Get the globals defined in this instance (not imported).
528    pub fn defined_globals(
529        &self,
530        store: StoreId,
531    ) -> impl ExactSizeIterator<Item = (DefinedGlobalIndex, crate::Global)> + '_ {
532        let module = self.env_module();
533        self.all_globals(store)
534            .skip(module.num_imported_globals)
535            .map(move |(i, global)| (module.defined_global_index(i).unwrap(), global))
536    }
537
538    /// Return a pointer to the interrupts structure
539    #[inline]
540    pub fn vm_store_context(&self) -> NonNull<Option<VmPtr<VMStoreContext>>> {
541        unsafe { self.vmctx_plus_offset_raw(self.offsets().ptr.vmctx().store_context()) }
542    }
543
544    /// Return a pointer to the global epoch counter used by this instance.
545    #[cfg(target_has_atomic = "64")]
546    pub fn epoch_ptr(self: Pin<&mut Self>) -> &mut Option<VmPtr<AtomicU64>> {
547        let offset = self.offsets().ptr.vmctx().epoch_ptr();
548        unsafe { self.vmctx_plus_offset_mut(offset) }
549    }
550
551    /// Return a pointer to the collector-specific heap data.
552    pub fn gc_heap_data(self: Pin<&mut Self>) -> &mut Option<VmPtr<u8>> {
553        let offset = self.offsets().ptr.vmctx().gc_heap_data();
554        unsafe { self.vmctx_plus_offset_mut(offset) }
555    }
556
557    pub(crate) unsafe fn set_store(mut self: Pin<&mut Self>, store: &StoreOpaque) {
558        // FIXME: should be more targeted ideally with the `unsafe` than just
559        // throwing this entire function in a large `unsafe` block.
560        unsafe {
561            *self.as_mut().store_mut() = Some(VMStoreRawPtr(store.traitobj()));
562            self.vm_store_context()
563                .write(Some(store.vm_store_context_ptr().into()));
564            #[cfg(target_has_atomic = "64")]
565            {
566                *self.as_mut().epoch_ptr() =
567                    Some(NonNull::from(store.engine().epoch_counter()).into());
568            }
569
570            if self.env_module().needs_gc_heap {
571                self.as_mut().set_gc_heap(Some(store.unwrap_gc_store()));
572            } else {
573                self.as_mut().set_gc_heap(None);
574            }
575        }
576    }
577
578    unsafe fn set_gc_heap(self: Pin<&mut Self>, gc_store: Option<&GcStore>) {
579        if let Some(gc_store) = gc_store {
580            *self.gc_heap_data() = Some(unsafe { gc_store.gc_heap.vmctx_gc_heap_data().into() });
581        } else {
582            *self.gc_heap_data() = None;
583        }
584    }
585
586    /// Return a reference to the vmctx used by compiled wasm code.
587    #[inline]
588    pub fn vmctx(&self) -> NonNull<VMContext> {
589        InstanceLayout::vmctx(self)
590    }
591
592    /// Lookup a function by index.
593    ///
594    /// # Panics
595    ///
596    /// Panics if `index` is out of bounds for this instance.
597    ///
598    /// # Safety
599    ///
600    /// The `store` parameter must be the store that owns this instance and the
601    /// functions that this instance can reference.
602    pub unsafe fn get_exported_func(
603        self: Pin<&mut Self>,
604        registry: &ModuleRegistry,
605        store: StoreId,
606        index: FuncIndex,
607    ) -> crate::Func {
608        let func_ref = self.get_func_ref(registry, index).unwrap();
609
610        // SAFETY: the validity of `func_ref` is guaranteed by the validity of
611        // `self`, and the contract that `store` must own `func_ref` is a
612        // contract of this function itself.
613        unsafe { crate::Func::from_vm_func_ref(store, func_ref) }
614    }
615
616    /// Returns a `Func` corresponding to the startup function for this
617    /// instance, if generated at compile time.
618    ///
619    /// # Safety
620    ///
621    /// The `store` parameter must be the store that owns this instance and the
622    /// functions that this instance can reference.
623    pub unsafe fn get_startup_func(
624        self: Pin<&mut Self>,
625        registry: &ModuleRegistry,
626        store: StoreId,
627    ) -> Option<crate::Func> {
628        let func_ref = self.get_start_func_ref(registry)?;
629
630        // SAFETY: the validity of `func_ref` is guaranteed by the validity of
631        // `self`, and the contract that `store` must own `func_ref` is a
632        // contract of this function itself.
633        Some(unsafe { crate::Func::from_vm_func_ref(store, func_ref) })
634    }
635
636    /// Lookup a table by index.
637    ///
638    /// # Panics
639    ///
640    /// Panics if `index` is out of bounds for this instance.
641    pub fn get_exported_table(&self, store: StoreId, index: TableIndex) -> crate::Table {
642        let (id, def_index) = if let Some(def_index) = self.env_module().defined_table_index(index)
643        {
644            (self.id, def_index)
645        } else {
646            let import = self.imported_table(index);
647            // SAFETY: validity of this `Instance` guarantees validity of the
648            // `vmctx` pointer being read here to find the transitive
649            // `InstanceId` that the import is associated with.
650            let id = unsafe { self.sibling_vmctx(import.vmctx.as_non_null()).id };
651            (id, import.index)
652        };
653        crate::Table::from_raw(StoreInstanceId::new(store, id), def_index)
654    }
655
656    /// Lookup a memory by index.
657    ///
658    /// # Panics
659    ///
660    /// Panics if `index` is out-of-bounds for this instance.
661    #[cfg_attr(
662        not(feature = "threads"),
663        expect(unused_variables, reason = "definitions cfg'd to dummy",)
664    )]
665    pub fn get_exported_memory(&self, store: StoreId, index: MemoryIndex) -> ExportMemory {
666        let module = self.env_module();
667        if module.memories[index].shared {
668            let (memory, import) =
669                if let Some(def_index) = self.env_module().defined_memory_index(index) {
670                    (
671                        self.get_defined_memory(def_index),
672                        self.get_defined_memory_vmimport(def_index),
673                    )
674                } else {
675                    let import = self.imported_memory(index);
676                    // SAFETY: validity of this `Instance` guarantees validity of
677                    // the `vmctx` pointer being read here to find the transitive
678                    // `InstanceId` that the import is associated with.
679                    let instance = unsafe { self.sibling_vmctx(import.vmctx.as_non_null()) };
680                    (instance.get_defined_memory(import.index), *import)
681                };
682
683            let vm = memory.as_shared_memory().unwrap().clone();
684            ExportMemory::Shared(vm, import)
685        } else {
686            let (id, def_index) =
687                if let Some(def_index) = self.env_module().defined_memory_index(index) {
688                    (self.id, def_index)
689                } else {
690                    let import = self.imported_memory(index);
691                    // SAFETY: validity of this `Instance` guarantees validity of the
692                    // `vmctx` pointer being read here to find the transitive
693                    // `InstanceId` that the import is associated with.
694                    let id = unsafe { self.sibling_vmctx(import.vmctx.as_non_null()).id };
695                    (id, import.index)
696                };
697
698            // SAFETY: `from_raw` requires that the memory is not shared, which
699            // was tested above in this if/else.
700            let store_id = StoreInstanceId::new(store, id);
701            ExportMemory::Unshared(unsafe { crate::Memory::from_raw(store_id, def_index) })
702        }
703    }
704
705    /// Lookup a global by index.
706    ///
707    /// # Panics
708    ///
709    /// Panics if `index` is out-of-bounds for this instance.
710    pub(crate) fn get_exported_global(&self, store: StoreId, index: GlobalIndex) -> crate::Global {
711        // If this global is defined within this instance, then that's easy to
712        // calculate the `Global`.
713        if let Some(def_index) = self.env_module().defined_global_index(index) {
714            let instance = StoreInstanceId::new(store, self.id);
715            return crate::Global::from_core(instance, def_index);
716        }
717
718        // For imported globals it's required to match on the `kind` to
719        // determine which `Global` constructor is going to be invoked.
720        let import = self.imported_global(index);
721        match import.kind {
722            VMGlobalKind::Host(index) => crate::Global::from_host(store, index),
723            VMGlobalKind::Instance(index) => {
724                // SAFETY: validity of this `&Instance` means validity of its
725                // imports meaning we can read the id of the vmctx within.
726                let id = unsafe {
727                    let vmctx = VMContext::from_opaque(import.vmctx.unwrap().as_non_null());
728                    self.sibling_vmctx(vmctx).id
729                };
730                crate::Global::from_core(StoreInstanceId::new(store, id), index)
731            }
732            #[cfg(feature = "component-model")]
733            VMGlobalKind::ComponentFlags(index) => {
734                // SAFETY: validity of this `&Instance` means validity of its
735                // imports meaning we can read the id of the vmctx within.
736                let id = unsafe {
737                    let vmctx = super::component::VMComponentContext::from_opaque(
738                        import.vmctx.unwrap().as_non_null(),
739                    );
740                    super::component::ComponentInstance::vmctx_instance_id(vmctx)
741                };
742                crate::Global::from_component_flags(
743                    crate::component::store::StoreComponentInstanceId::new(store, id),
744                    index,
745                )
746            }
747        }
748    }
749
750    /// Get an exported tag by index.
751    ///
752    /// # Panics
753    ///
754    /// Panics if the index is out-of-range.
755    pub fn get_exported_tag(&self, store: StoreId, index: TagIndex) -> crate::Tag {
756        let (id, def_index) = if let Some(def_index) = self.env_module().defined_tag_index(index) {
757            (self.id, def_index)
758        } else {
759            let import = self.imported_tag(index);
760            // SAFETY: validity of this `Instance` guarantees validity of the
761            // `vmctx` pointer being read here to find the transitive
762            // `InstanceId` that the import is associated with.
763            let id = unsafe { self.sibling_vmctx(import.vmctx.as_non_null()).id };
764            (id, import.index)
765        };
766        crate::Tag::from_raw(StoreInstanceId::new(store, id), def_index)
767    }
768
769    /// Grow memory by the specified amount of pages.
770    ///
771    /// Returns `None` if memory can't be grown by the specified amount
772    /// of pages. Returns `Some` with the old size in bytes if growth was
773    /// successful.
774    pub(crate) async fn memory_grow(
775        mut self: Pin<&mut Self>,
776        limiter: Option<&mut StoreResourceLimiter<'_>>,
777        idx: DefinedMemoryIndex,
778        delta: u64,
779    ) -> Result<Option<usize>, Error> {
780        let memory = &mut self.as_mut().memories_mut()[idx].1;
781
782        // SAFETY: this is the safe wrapper around `Memory::grow` because it
783        // automatically updates the `VMMemoryDefinition` in this instance after
784        // a growth operation below.
785        let result = unsafe { memory.grow(delta, limiter).await };
786
787        // Update the state used by a non-shared Wasm memory in case the base
788        // pointer and/or the length changed.
789        if memory.as_shared_memory().is_none() {
790            let vmmemory = memory.vmmemory();
791            self.set_memory(idx, vmmemory);
792        }
793
794        result
795    }
796
797    /// Performs a grow operation on the `table_index` specified using `grow`.
798    ///
799    /// This will handle updating the VMTableDefinition internally as necessary.
800    ///
801    /// # Safety
802    ///
803    /// This function requires that the caller, on success, fills in the table
804    /// elements with an appropriately typed value.
805    pub(crate) async unsafe fn defined_table_grow(
806        mut self: Pin<&mut Self>,
807        table_index: DefinedTableIndex,
808        limiter: Option<&mut StoreResourceLimiter<'_>>,
809        amt: u64,
810    ) -> Result<Option<usize>> {
811        let table = self.as_mut().get_defined_table(table_index);
812        // SAFETY: updating the `VMContext` table pointers and such is done
813        // below, and the responsibility of filling in the new table elements
814        // is forwarded to the caller.
815        let result = unsafe { table.grow(limiter, amt).await? };
816        let element = table.vmtable();
817        self.set_table(table_index, element);
818        Ok(result)
819    }
820
821    fn alloc_layout(offsets: &VMOffsets<HostPtr>) -> Layout {
822        let size = mem::size_of::<Self>()
823            .checked_add(usize::try_from(offsets.size_of_vmctx()).unwrap())
824            .unwrap();
825        let align = mem::align_of::<Self>();
826        Layout::from_size_align(size, align).unwrap()
827    }
828
829    fn type_ids_array(&self) -> NonNull<VmPtr<VMSharedTypeIndex>> {
830        unsafe { self.vmctx_plus_offset_raw(self.offsets().ptr.vmctx().type_ids()) }
831    }
832
833    /// Get a `&VMFuncRef` for the given `FuncIndex`.
834    ///
835    /// Returns `None` if the index is the reserved index value.
836    ///
837    /// The returned reference is a stable reference that won't be moved and can
838    /// be passed into JIT code.
839    pub(crate) fn get_func_ref(
840        self: Pin<&mut Self>,
841        registry: &ModuleRegistry,
842        index: FuncIndex,
843    ) -> Option<NonNull<VMFuncRef>> {
844        if index == FuncIndex::reserved_value() {
845            return None;
846        }
847
848        match self.env_module().defined_func_index(index) {
849            Some(index) => self.initialize_defined_funcref(registry, index),
850            None => {
851                debug_assert!(self.env_module().is_imported_function(index));
852                Some(self.imported_function(index).as_func_ref().into())
853            }
854        }
855    }
856
857    /// Initializes a defined function's `VMFuncRef` in-place and then returns a
858    /// pointer to that location.
859    fn initialize_defined_funcref(
860        self: Pin<&mut Self>,
861        registry: &ModuleRegistry,
862        def_index: DefinedFuncIndex,
863    ) -> Option<NonNull<VMFuncRef>> {
864        let module = self.env_module();
865        let index = module.func_index(def_index);
866        let func = &module.functions[index];
867        let type_index = func.signature.unwrap_engine_type_index();
868        let vmctx_offset = self.offsets().func_refs().at(func.func_ref);
869        let array_to_wasm_key = FuncKey::ArrayToWasmTrampoline(module.module_index, def_index);
870        let wasm_key = FuncKey::DefinedWasmFunction(module.module_index, def_index);
871        // SAFETY: the type/offset/keys here are all valid for the defined
872        // function at `def_index`.
873        unsafe {
874            self.initialize_and_return_funcref(
875                registry,
876                type_index,
877                vmctx_offset,
878                array_to_wasm_key,
879                wasm_key,
880            )
881        }
882    }
883
884    fn get_start_func_ref(
885        self: Pin<&mut Self>,
886        registry: &ModuleRegistry,
887    ) -> Option<NonNull<VMFuncRef>> {
888        let module = self.env_module();
889        let type_index = match module.startup {
890            ModuleStartup::None => return None,
891            ModuleStartup::Always(t) | ModuleStartup::IfMemoriesNeedInit(t) => {
892                t.unwrap_engine_type_index()
893            }
894        };
895        let vmctx_offset = self.offsets().startup_func_ref();
896        let array_to_wasm_key = FuncKey::ModuleStartup(Abi::Array, module.module_index);
897        let wasm_key = FuncKey::ModuleStartup(Abi::Wasm, module.module_index);
898        // SAFETY: the type/offset/keys here are all valid for the module
899        // startup function.
900        unsafe {
901            self.initialize_and_return_funcref(
902                registry,
903                type_index,
904                vmctx_offset,
905                array_to_wasm_key,
906                wasm_key,
907            )
908        }
909    }
910
911    /// Common implementation of initializing a `VMFuncRef` stored within this
912    /// instance's `VMContext`.
913    ///
914    /// # Safety
915    ///
916    /// This function requires that `type_index` accurately describes this
917    /// function and `vmctx_offset` is indeed the correct offset for the
918    /// functions here. Effectively all the arguments here must be "logically
919    /// correct" for the `VMFuncRef` being initialized.
920    unsafe fn initialize_and_return_funcref(
921        self: Pin<&mut Self>,
922        registry: &ModuleRegistry,
923        type_index: VMSharedTypeIndex,
924        vmctx_offset: u32,
925        array_to_wasm_key: FuncKey,
926        wasm_key: FuncKey,
927    ) -> Option<NonNull<VMFuncRef>> {
928        // For now, we eagerly initialize an funcref struct in-place whenever
929        // asked for a reference to it. This is mostly fine, because in practice
930        // each funcref is unlikely to be requested more than a few times:
931        // once-ish for funcref tables used for call_indirect (the usual
932        // compilation strategy places each function in the table at most once),
933        // and once or a few times when fetching exports via API.  Note that for
934        // any case driven by table accesses, the lazy table init behaves like a
935        // higher-level cache layer that protects this initialization from
936        // happening multiple times, via that particular table at least.
937        //
938        // When `ref.func` becomes more commonly used or if we otherwise see a
939        // use-case where this becomes a hotpath, we can reconsider by using
940        // some state to track "uninitialized" explicitly, for example by
941        // zeroing the funcrefs (perhaps together with other
942        // zeroed-at-instantiate-time state) or using a separate is-initialized
943        // bitmap.
944        //
945        // We arrived at this design because zeroing memory is expensive, so
946        // it's better for instantiation performance if we don't have to track
947        // "is-initialized" state at all!
948
949        let module_with_code = ModuleWithCode::in_store(
950            registry,
951            self.runtime_module()
952                .expect("funcref impossible in fake module"),
953        )
954        .expect("module not in store");
955
956        let array_call =
957            VmPtr::from(NonNull::from(module_with_code.function(array_to_wasm_key)).cast());
958
959        let wasm_call = Some(VmPtr::from(
960            NonNull::new(
961                module_with_code
962                    .function(wasm_key)
963                    .as_ptr()
964                    .cast::<VMWasmCallFunction>()
965                    .cast_mut(),
966            )
967            .unwrap(),
968        ));
969
970        let vmctx = VMOpaqueContext::from_vmcontext(self.vmctx()).into();
971
972        // SAFETY: the offset calculated here should be correct with
973        // `self.offsets`
974        let func_ref_ptr = unsafe { self.vmctx_plus_offset_raw::<VMFuncRef>(vmctx_offset) };
975
976        // SAFETY: the `func_ref_ptr` should be valid as it's within our
977        // `VMContext` area.
978        unsafe {
979            func_ref_ptr.write(VMFuncRef {
980                array_call,
981                wasm_call,
982                vmctx,
983                type_index,
984            });
985        }
986
987        Some(func_ref_ptr)
988    }
989
990    /// Get the passive elements segment at the given index.
991    pub(crate) fn passive_element_segment(
992        self: Pin<&mut Self>,
993        passive: PassiveElemIndex,
994    ) -> &mut [ValRaw] {
995        self.passive_elements_mut()[passive.index()].elements_mut()
996    }
997
998    pub(crate) fn passive_elements_mut(self: Pin<&mut Self>) -> &mut TryVec<PassiveElementSegment> {
999        // SAFETY: Not moving data out of `self`.
1000        &mut unsafe { self.get_unchecked_mut() }.passive_elements
1001    }
1002
1003    /// Drop an element.
1004    pub(crate) fn passive_elem_drop(
1005        self: Pin<&mut Self>,
1006        gc_store: Option<&mut GcStore>,
1007        passive_index: PassiveElemIndex,
1008    ) -> Result<(), OutOfMemory> {
1009        self.passive_elements_mut()[passive_index.index()].clear(gc_store);
1010        Ok(())
1011    }
1012
1013    /// Get a locally-defined memory.
1014    pub fn get_defined_memory_mut(self: Pin<&mut Self>, index: DefinedMemoryIndex) -> &mut Memory {
1015        &mut self.memories_mut()[index].1
1016    }
1017
1018    /// Get a locally-defined memory.
1019    pub fn get_defined_memory(&self, index: DefinedMemoryIndex) -> &Memory {
1020        &self.memories[index].1
1021    }
1022
1023    pub fn get_defined_memory_vmimport(&self, index: DefinedMemoryIndex) -> VMMemoryImport {
1024        crate::runtime::vm::VMMemoryImport {
1025            from: self.memory_ptr(index).into(),
1026            vmctx: self.vmctx().into(),
1027            index,
1028        }
1029    }
1030
1031    /// Given an internal storage range of a Wasm data segment (or subset of a
1032    /// Wasm data segment), get the data's raw bytes.
1033    pub(crate) fn wasm_data(&self, range: Range<u32>) -> &[u8] {
1034        let start = usize::try_from(range.start).unwrap();
1035        let end = usize::try_from(range.end).unwrap();
1036        &self.runtime_info.wasm_data()[start..end]
1037    }
1038
1039    /// Returns the data for the runtime segment identified by `index`
1040    ///
1041    /// Does not take into account the dynamic size of the data pointed to by
1042    /// `index`, always returns the raw data from the module itself.
1043    ///
1044    /// # Panics
1045    ///
1046    /// Panics if `index` is out-of-bounds.
1047    fn runtime_data(&self, index: RuntimeDataIndex) -> &[u8] {
1048        let range = self.env_module().runtime_data[index].clone();
1049        self.wasm_data(range)
1050    }
1051
1052    /// Get a table by index regardless of whether it is locally-defined
1053    /// or an imported, foreign table. Ensure that the given range of
1054    /// elements in the table is lazily initialized.  We define this
1055    /// operation all-in-one for safety, to ensure the lazy-init
1056    /// happens.
1057    ///
1058    /// Takes an `Iterator` for the index-range to lazy-initialize,
1059    /// for flexibility. This can be a range, single item, or empty
1060    /// sequence, for example. The iterator should return indices in
1061    /// increasing order, so that the break-at-out-of-bounds behavior
1062    /// works correctly.
1063    pub(crate) fn get_table_with_lazy_init(
1064        self: Pin<&mut Self>,
1065        registry: &ModuleRegistry,
1066        table_index: TableIndex,
1067        range: impl Iterator<Item = u64>,
1068    ) -> &mut Table {
1069        let (idx, instance) = self.defined_table_index_and_instance(table_index);
1070        instance.get_defined_table_with_lazy_init(registry, idx, range)
1071    }
1072
1073    /// Gets the raw runtime table data structure owned by this instance
1074    /// given the provided `idx`.
1075    ///
1076    /// The `range` specified is eagerly initialized for funcref tables.
1077    pub fn get_defined_table_with_lazy_init(
1078        mut self: Pin<&mut Self>,
1079        registry: &ModuleRegistry,
1080        idx: DefinedTableIndex,
1081        range: impl IntoIterator<Item = u64>,
1082    ) -> &mut Table {
1083        let elt_ty = self.tables[idx].1.element_type();
1084
1085        if elt_ty == TableElementType::Func {
1086            for i in range {
1087                match self.tables[idx].1.get_func_maybe_init(i) {
1088                    // Uninitialized table element.
1089                    Ok(None) => {}
1090                    // Initialized table element, move on to the next.
1091                    Ok(Some(_)) => continue,
1092                    // Out-of-bounds; caller will handle by likely
1093                    // throwing a trap. No work to do to lazy-init
1094                    // beyond the end.
1095                    Err(_) => break,
1096                };
1097
1098                // The table element `i` is uninitialized and is now being
1099                // initialized. This must imply that a `precompiled` list of
1100                // function indices is available for this table. The precompiled
1101                // list is extracted and then it is consulted with `i` to
1102                // determine the function that is going to be initialized. Note
1103                // that `i` may be outside the limits of the static
1104                // initialization so it's a fallible `get` instead of an index.
1105                let module = self.env_module();
1106                let precomputed = &module.table_initialization[idx];
1107                // Panicking here helps catch bugs rather than silently truncating by accident.
1108                let func_index = precomputed.get(usize::try_from(i).unwrap()).cloned();
1109                let func_ref = func_index
1110                    .and_then(|func_index| self.as_mut().get_func_ref(registry, func_index));
1111                self.as_mut().tables_mut()[idx]
1112                    .1
1113                    .set_func(i, func_ref)
1114                    .expect("Table type should match and index should be in-bounds");
1115            }
1116        }
1117
1118        self.get_defined_table(idx)
1119    }
1120
1121    /// Get a locally-defined table.
1122    pub(crate) fn get_defined_table(self: Pin<&mut Self>, index: DefinedTableIndex) -> &mut Table {
1123        &mut self.tables_mut()[index].1
1124    }
1125
1126    pub(crate) fn defined_table_index_and_instance<'a>(
1127        self: Pin<&'a mut Self>,
1128        index: TableIndex,
1129    ) -> (DefinedTableIndex, Pin<&'a mut Instance>) {
1130        if let Some(defined_table_index) = self.env_module().defined_table_index(index) {
1131            (defined_table_index, self)
1132        } else {
1133            let import = self.imported_table(index);
1134            let index = import.index;
1135            let vmctx = import.vmctx.as_non_null();
1136            // SAFETY: the validity of `self` means that the reachable instances
1137            // should also all be owned by the same store and fully initialized,
1138            // so it's safe to laterally move from a mutable borrow of this
1139            // instance to a mutable borrow of a sibling instance.
1140            let foreign_instance = unsafe { self.sibling_vmctx_mut(vmctx) };
1141            (index, foreign_instance)
1142        }
1143    }
1144
1145    /// Same as `self.runtime_info.env_module()` but additionally returns the
1146    /// `Pin<&mut Self>` with the same original lifetime.
1147    pub fn module_and_self(self: Pin<&mut Self>) -> (&wasmtime_environ::Module, Pin<&mut Self>) {
1148        // SAFETY: this function is projecting both `&Module` and the same
1149        // pointer both connected to the same lifetime. This is safe because
1150        // it's a contract of `Pin<&mut Self>` that the `runtime_info` field is
1151        // never written, meaning it's effectively unsafe to have `&mut Module`
1152        // projected from `Pin<&mut Self>`. Consequently it's safe to have a
1153        // read-only view of the field while still retaining mutable access to
1154        // all other fields.
1155        let module = self.runtime_info.env_module();
1156        let module = &raw const *module;
1157        let module = unsafe { &*module };
1158        (module, self)
1159    }
1160
1161    /// Initialize the VMContext data associated with this Instance.
1162    ///
1163    /// The `VMContext` memory is assumed to be uninitialized; any field
1164    /// that we need in a certain state will be explicitly written by this
1165    /// function.
1166    unsafe fn initialize_vmctx(self: Pin<&mut Self>, store: &StoreOpaque, imports: Imports) {
1167        let (module, mut instance) = self.module_and_self();
1168
1169        // SAFETY: the type of the magic field is indeed `u32` and this function
1170        // is initializing its value.
1171        unsafe {
1172            let offsets = instance.runtime_info.offsets();
1173            instance
1174                .vmctx_plus_offset_raw::<u32>(offsets.ptr.vmctx().magic())
1175                .write(VMCONTEXT_MAGIC);
1176        }
1177
1178        // SAFETY: it's up to the caller to provide a valid store pointer here.
1179        unsafe {
1180            instance.as_mut().set_store(store);
1181        }
1182
1183        // Initialize shared types
1184        //
1185        // SAFETY: validity of the vmctx means it should be safe to write to it
1186        // here.
1187        unsafe {
1188            let types = NonNull::from(instance.runtime_info.type_ids());
1189            instance.type_ids_array().write(types.cast().into());
1190        }
1191
1192        // Initialize the built-in functions
1193        //
1194        // SAFETY: the type of the builtin functions field is indeed a pointer
1195        // and the pointer being filled in here, plus the vmctx is valid to
1196        // write to during initialization.
1197        unsafe {
1198            static BUILTINS: VMBuiltinFunctionsArray = VMBuiltinFunctionsArray::INIT;
1199            let ptr = BUILTINS.expose_provenance();
1200            let offsets = instance.runtime_info.offsets();
1201            instance
1202                .vmctx_plus_offset_raw(offsets.ptr.vmctx().builtin_functions())
1203                .write(VmPtr::from(ptr));
1204        }
1205
1206        // Initialize the imports
1207        //
1208        // SAFETY: the vmctx is safe to initialize during this function and
1209        // validity of each item itself is a contract the caller must uphold.
1210        debug_assert_eq!(imports.functions.len(), module.num_imported_funcs);
1211        unsafe {
1212            let offsets = instance.runtime_info.offsets();
1213            ptr::copy_nonoverlapping(
1214                imports.functions.as_ptr(),
1215                instance
1216                    .vmctx_plus_offset_raw(offsets.imported_functions().begin())
1217                    .as_ptr(),
1218                imports.functions.len(),
1219            );
1220            debug_assert_eq!(imports.tables.len(), module.num_imported_tables);
1221            ptr::copy_nonoverlapping(
1222                imports.tables.as_ptr(),
1223                instance
1224                    .vmctx_plus_offset_raw(offsets.imported_tables().begin())
1225                    .as_ptr(),
1226                imports.tables.len(),
1227            );
1228            debug_assert_eq!(imports.memories.len(), module.num_imported_memories);
1229            ptr::copy_nonoverlapping(
1230                imports.memories.as_ptr(),
1231                instance
1232                    .vmctx_plus_offset_raw(offsets.imported_memories().begin())
1233                    .as_ptr(),
1234                imports.memories.len(),
1235            );
1236            debug_assert_eq!(imports.globals.len(), module.num_imported_globals);
1237            ptr::copy_nonoverlapping(
1238                imports.globals.as_ptr(),
1239                instance
1240                    .vmctx_plus_offset_raw(offsets.imported_globals().begin())
1241                    .as_ptr(),
1242                imports.globals.len(),
1243            );
1244            debug_assert_eq!(imports.tags.len(), module.num_imported_tags);
1245            ptr::copy_nonoverlapping(
1246                imports.tags.as_ptr(),
1247                instance
1248                    .vmctx_plus_offset_raw(offsets.imported_tags().begin())
1249                    .as_ptr(),
1250                imports.tags.len(),
1251            );
1252        }
1253
1254        // N.B.: there is no need to initialize the funcrefs array because we
1255        // eagerly construct each element in it whenever asked for a reference
1256        // to that element. In other words, there is no state needed to track
1257        // the lazy-init, so we don't need to initialize any state now.
1258
1259        // Initialize the defined tables
1260        //
1261        // SAFETY: it's safe to initialize these tables during initialization
1262        // here and the various types of pointers and such here should all be
1263        // valid.
1264        unsafe {
1265            let offsets = instance.runtime_info.offsets();
1266            let mut ptr = instance.vmctx_plus_offset_raw(offsets.tables().begin());
1267            let tables = instance.as_mut().tables_mut();
1268            for i in 0..module.num_defined_tables() {
1269                ptr.write(tables[DefinedTableIndex::new(i)].1.vmtable());
1270                ptr = ptr.add(1);
1271            }
1272        }
1273
1274        // Initialize the defined memories. This fills in both the
1275        // `defined_memories` table and the `owned_memories` table at the same
1276        // time. Entries in `defined_memories` hold a pointer to a definition
1277        // (all memories) whereas the `owned_memories` hold the actual
1278        // definitions of memories owned (not shared) in the module.
1279        //
1280        // SAFETY: it's safe to initialize these memories during initialization
1281        // here and the various types of pointers and such here should all be
1282        // valid.
1283        unsafe {
1284            let offsets = instance.runtime_info.offsets();
1285            let mut ptr = instance.vmctx_plus_offset_raw(offsets.memories().begin());
1286            let mut owned_ptr = instance.vmctx_plus_offset_raw(offsets.owned_memories().begin());
1287            let memories = instance.as_mut().memories_mut();
1288            for i in 0..module.num_defined_memories() {
1289                let defined_memory_index = DefinedMemoryIndex::new(i);
1290                let memory_index = module.memory_index(defined_memory_index);
1291                if module.memories[memory_index].shared {
1292                    let def_ptr = memories[defined_memory_index]
1293                        .1
1294                        .as_shared_memory()
1295                        .unwrap()
1296                        .vmmemory_ptr();
1297                    ptr.write(VmPtr::from(def_ptr));
1298                } else {
1299                    owned_ptr.write(memories[defined_memory_index].1.vmmemory());
1300                    ptr.write(VmPtr::from(owned_ptr));
1301                    owned_ptr = owned_ptr.add(1);
1302                }
1303                ptr = ptr.add(1);
1304            }
1305        }
1306
1307        // Zero-initialize the globals so that nothing is uninitialized memory
1308        // after this function returns. The globals are actually initialized
1309        // with their const expression initializers after the instance is fully
1310        // allocated.
1311        //
1312        // SAFETY: it's safe to initialize globals during initialization
1313        // here. Note that while the value being written is not valid for all
1314        // types of globals it's initializing the memory to zero instead of
1315        // being in an undefined state. So it's still unsafe to access globals
1316        // after this, but if it's read then it'd hopefully crash faster than
1317        // leaving this undefined.
1318        unsafe {
1319            for i in 0..module.num_defined_globals() {
1320                let index = DefinedGlobalIndex::new(i);
1321                instance.global_ptr(index).write(VMGlobalDefinition::new());
1322            }
1323            for (index, val) in module.global_initializers.iter() {
1324                let mut def = VMGlobalDefinition::new();
1325                match val {
1326                    GlobalConstValue::I32(i) => *def.as_i32_mut() = *i,
1327                    GlobalConstValue::I64(i) => *def.as_i64_mut() = *i,
1328                    GlobalConstValue::F32(i) => *def.as_f32_bits_mut() = *i,
1329                    GlobalConstValue::F64(i) => *def.as_f64_bits_mut() = *i,
1330                    GlobalConstValue::V128(i) => def.set_u128(*i),
1331                }
1332                instance.global_ptr(*index).write(def);
1333            }
1334        }
1335
1336        // Initialize the defined tags
1337        //
1338        // SAFETY: it's safe to initialize these tags during initialization
1339        // here and the various types of pointers and such here should all be
1340        // valid.
1341        unsafe {
1342            let offsets = instance.runtime_info.offsets();
1343            let mut ptr = instance.vmctx_plus_offset_raw(offsets.tags().begin());
1344            for i in 0..module.num_defined_tags() {
1345                let defined_index = DefinedTagIndex::new(i);
1346                let tag_index = module.tag_index(defined_index);
1347                let tag = module.tags[tag_index];
1348                ptr.write(VMTagDefinition::new(
1349                    tag.signature.unwrap_engine_type_index(),
1350                ));
1351                ptr = ptr.add(1);
1352            }
1353        }
1354
1355        // Initialize the lengths of runtime data segments.
1356        //
1357        // SAFETY: it's safe to initialize these lengths during initialization
1358        // here and the various types of pointers and such here should all be
1359        // valid.
1360        unsafe {
1361            let offsets = instance.runtime_info.offsets();
1362            let mut lengths =
1363                instance.vmctx_plus_offset_raw(offsets.runtime_data_lengths().begin());
1364            let mut bases = instance.vmctx_plus_offset_raw(offsets.runtime_data_bases().begin());
1365            for i in module.runtime_data.keys() {
1366                let data = instance.runtime_data(i);
1367                lengths.write(u32::try_from(data.len()).unwrap());
1368                lengths = lengths.add(1);
1369                bases.write(VmPtr::from(NonNull::from(data).cast::<u8>()));
1370                bases = bases.add(1);
1371            }
1372        }
1373
1374        // This is the half of the strategy of implementing memory-init-cow data
1375        // segments. Notably the compiled startup function, if present, will
1376        // skip data segments that have a null pointer. Here each linear memory
1377        // is tested to see if it needs initialization. If it does, then the
1378        // data segment is left in-place (and the startup function will
1379        // initialize linear memory). Otherwise the data segment is null'd out.
1380        // If the startup function runs (e.g. something else in the module needs
1381        // it), then the corresponding data segment's initialization will be
1382        // skipped.
1383        if let MemoryInitialization::Static { map } = &module.memory_initialization {
1384            for (memory, init) in map {
1385                let Some(memory) = module.defined_memory_index(memory) else {
1386                    continue;
1387                };
1388                if instance.memories[memory].1.needs_init() {
1389                    continue;
1390                }
1391                if let Some((_offset, data)) = init {
1392                    let offsets = instance.runtime_info.offsets();
1393                    unsafe {
1394                        instance
1395                            .vmctx_plus_offset_raw(offsets.runtime_data_lengths().at(*data))
1396                            .write(0u32);
1397                        instance
1398                            .vmctx_plus_offset_raw(offsets.runtime_data_bases().at(*data))
1399                            .write(0usize);
1400                    }
1401                }
1402            }
1403        }
1404    }
1405
1406    /// Attempts to convert from the host `addr` specified to a WebAssembly
1407    /// based address recorded in `WasmFault`.
1408    ///
1409    /// This method will check all linear memories that this instance contains
1410    /// to see if any of them contain `addr`. If one does then `Some` is
1411    /// returned with metadata about the wasm fault. Otherwise `None` is
1412    /// returned and `addr` doesn't belong to this instance.
1413    pub fn wasm_fault(&self, addr: usize) -> Option<WasmFault> {
1414        let mut fault = None;
1415        for (_, (_, memory)) in self.memories.iter() {
1416            let accessible = memory.wasm_accessible();
1417            if accessible.start <= addr && addr < accessible.end {
1418                // All linear memories should be disjoint so assert that no
1419                // prior fault has been found.
1420                assert!(fault.is_none());
1421                fault = Some(WasmFault {
1422                    memory_size: memory.byte_size(),
1423                    wasm_address: u64::try_from(addr - accessible.start).unwrap(),
1424                });
1425            }
1426        }
1427        fault
1428    }
1429
1430    /// Returns the id, within this instance's store, that it's assigned.
1431    pub fn id(&self) -> InstanceId {
1432        self.id
1433    }
1434
1435    /// Get all memories within this instance.
1436    ///
1437    /// Returns both import and defined memories.
1438    ///
1439    /// Returns both exported and non-exported memories.
1440    ///
1441    /// Gives access to the full memories space.
1442    pub fn all_memories(
1443        &self,
1444        store: StoreId,
1445    ) -> impl ExactSizeIterator<Item = (MemoryIndex, ExportMemory)> + '_ {
1446        self.env_module()
1447            .memories
1448            .iter()
1449            .map(move |(i, _)| (i, self.get_exported_memory(store, i)))
1450    }
1451
1452    /// Return the memories defined in this instance (not imported).
1453    pub fn defined_memories<'a>(
1454        &'a self,
1455        store: StoreId,
1456    ) -> impl ExactSizeIterator<Item = ExportMemory> + 'a {
1457        let num_imported = self.env_module().num_imported_memories;
1458        self.all_memories(store)
1459            .skip(num_imported)
1460            .map(|(_i, memory)| memory)
1461    }
1462
1463    /// Lookup an item with the given index.
1464    ///
1465    /// # Panics
1466    ///
1467    /// Panics if `export` is not valid for this instance.
1468    ///
1469    /// # Safety
1470    ///
1471    /// This function requires that `store` is the correct store which owns this
1472    /// instance.
1473    pub unsafe fn get_export_by_index_mut(
1474        self: Pin<&mut Self>,
1475        registry: &ModuleRegistry,
1476        store: StoreId,
1477        export: EntityIndex,
1478    ) -> Export {
1479        match export {
1480            // SAFETY: the contract of `store` owning the this instance is a
1481            // safety requirement of this function itself.
1482            EntityIndex::Function(i) => {
1483                Export::Function(unsafe { self.get_exported_func(registry, store, i) })
1484            }
1485            EntityIndex::Global(i) => Export::Global(self.get_exported_global(store, i)),
1486            EntityIndex::Table(i) => Export::Table(self.get_exported_table(store, i)),
1487            EntityIndex::Memory(i) => match self.get_exported_memory(store, i) {
1488                ExportMemory::Unshared(m) => Export::Memory(m),
1489                ExportMemory::Shared(m, i) => Export::SharedMemory(m, i),
1490            },
1491            EntityIndex::Tag(i) => Export::Tag(self.get_exported_tag(store, i)),
1492        }
1493    }
1494
1495    fn store_mut(self: Pin<&mut Self>) -> &mut Option<VMStoreRawPtr> {
1496        // SAFETY: this is a pin-projection to get a mutable reference to an
1497        // internal field and is safe so long as the `&mut Self` temporarily
1498        // created is not overwritten, which it isn't here.
1499        unsafe { &mut self.get_unchecked_mut().store }
1500    }
1501
1502    fn memories_mut(
1503        self: Pin<&mut Self>,
1504    ) -> &mut TryPrimaryMap<DefinedMemoryIndex, (MemoryAllocationIndex, Memory)> {
1505        // SAFETY: see `store_mut` above.
1506        unsafe { &mut self.get_unchecked_mut().memories }
1507    }
1508
1509    pub(crate) fn tables_mut(
1510        self: Pin<&mut Self>,
1511    ) -> &mut TryPrimaryMap<DefinedTableIndex, (TableAllocationIndex, Table)> {
1512        // SAFETY: see `store_mut` above.
1513        unsafe { &mut self.get_unchecked_mut().tables }
1514    }
1515
1516    #[cfg(feature = "wmemcheck")]
1517    pub(super) fn wmemcheck_state_mut(self: Pin<&mut Self>) -> &mut Option<Wmemcheck> {
1518        // SAFETY: see `store_mut` above.
1519        unsafe { &mut self.get_unchecked_mut().wmemcheck_state }
1520    }
1521
1522    pub(crate) fn needs_startup(&self) -> bool {
1523        match self.env_module().startup {
1524            ModuleStartup::None => false,
1525            ModuleStartup::Always(_) => true,
1526            ModuleStartup::IfMemoriesNeedInit(_) => self
1527                .memories
1528                .iter()
1529                .any(|(_, (_, memory))| memory.needs_init()),
1530        }
1531    }
1532}
1533
1534// SAFETY: `layout` should describe this accurately and `OwnedVMContext` is the
1535// last field of `ComponentInstance`.
1536unsafe impl InstanceLayout for Instance {
1537    const INIT_ZEROED: bool = false;
1538    type VMContext = VMContext;
1539
1540    fn layout(&self) -> Layout {
1541        Self::alloc_layout(self.runtime_info.offsets())
1542    }
1543
1544    fn owned_vmctx(&self) -> &OwnedVMContext<VMContext> {
1545        &self.vmctx
1546    }
1547
1548    fn owned_vmctx_mut(&mut self) -> &mut OwnedVMContext<VMContext> {
1549        &mut self.vmctx
1550    }
1551}
1552
1553pub type InstanceHandle = OwnedInstance<Instance>;
1554
1555/// A handle holding an `Instance` of a WebAssembly module.
1556///
1557/// This structure is an owning handle of the `instance` contained internally.
1558/// When this value goes out of scope it will deallocate the `Instance` and all
1559/// memory associated with it.
1560///
1561/// Note that this lives within a `StoreOpaque` on a list of instances that a
1562/// store is keeping alive.
1563#[derive(Debug)]
1564#[repr(transparent)] // guarantee this is a zero-cost wrapper
1565pub struct OwnedInstance<T: InstanceLayout> {
1566    /// The raw pointer to the instance that was allocated.
1567    ///
1568    /// Note that this is not equivalent to `Box<Instance>` because the
1569    /// allocation here has a `VMContext` trailing after it. Thus the custom
1570    /// destructor to invoke the `dealloc` function with the appropriate
1571    /// layout.
1572    instance: SendSyncPtr<T>,
1573    _marker: marker::PhantomData<Box<(T, OwnedVMContext<T::VMContext>)>>,
1574}
1575
1576/// Structure that must be placed at the end of a type implementing
1577/// `InstanceLayout`.
1578#[repr(align(16))] // match the alignment of VMContext
1579pub struct OwnedVMContext<T> {
1580    /// A pointer to the `vmctx` field at the end of the `structure`.
1581    ///
1582    /// If you're looking at this a reasonable question would be "why do we need
1583    /// a pointer to ourselves?" because after all the pointer's value is
1584    /// trivially derivable from any `&Instance` pointer. The rationale for this
1585    /// field's existence is subtle, but it's required for correctness. The
1586    /// short version is "this makes miri happy".
1587    ///
1588    /// The long version of why this field exists is that the rules that MIRI
1589    /// uses to ensure pointers are used correctly have various conditions on
1590    /// them depend on how pointers are used. More specifically if `*mut T` is
1591    /// derived from `&mut T`, then that invalidates all prior pointers derived
1592    /// from the `&mut T`. This means that while we liberally want to re-acquire
1593    /// a `*mut VMContext` throughout the implementation of `Instance` the
1594    /// trivial way, a function `fn vmctx(Pin<&mut Instance>) -> *mut VMContext`
1595    /// would effectively invalidate all prior `*mut VMContext` pointers
1596    /// acquired. The purpose of this field is to serve as a sort of
1597    /// source-of-truth for where `*mut VMContext` pointers come from.
1598    ///
1599    /// This field is initialized when the `Instance` is created with the
1600    /// original allocation's pointer. That means that the provenance of this
1601    /// pointer contains the entire allocation (both instance and `VMContext`).
1602    /// This provenance bit is then "carried through" where `fn vmctx` will base
1603    /// all returned pointers on this pointer itself. This provides the means of
1604    /// never invalidating this pointer throughout MIRI and additionally being
1605    /// able to still temporarily have `Pin<&mut Instance>` methods and such.
1606    ///
1607    /// It's important to note, though, that this is not here purely for MIRI.
1608    /// The careful construction of the `fn vmctx` method has ramifications on
1609    /// the LLVM IR generated, for example. A historical CVE on Wasmtime,
1610    /// GHSA-ch89-5g45-qwc7, was caused due to relying on undefined behavior. By
1611    /// deriving VMContext pointers from this pointer it specifically hints to
1612    /// LLVM that trickery is afoot and it properly informs `noalias` and such
1613    /// annotations and analysis. More-or-less this pointer is actually loaded
1614    /// in LLVM IR which helps defeat otherwise present aliasing optimizations,
1615    /// which we want, since writes to this should basically never be optimized
1616    /// out.
1617    ///
1618    /// As a final note it's worth pointing out that the machine code generated
1619    /// for accessing `fn vmctx` is still as one would expect. This member isn't
1620    /// actually ever loaded at runtime (or at least shouldn't be). Perhaps in
1621    /// the future if the memory consumption of this field is a problem we could
1622    /// shrink it slightly, but for now one extra pointer per wasm instance
1623    /// seems not too bad.
1624    vmctx_self_reference: SendSyncPtr<T>,
1625
1626    /// This field ensures that going from `Pin<&mut T>` to `&mut T` is not a
1627    /// safe operation.
1628    _marker: core::marker::PhantomPinned,
1629}
1630
1631impl<T> OwnedVMContext<T> {
1632    /// Creates a new blank vmctx to place at the end of an instance.
1633    pub fn new() -> OwnedVMContext<T> {
1634        OwnedVMContext {
1635            vmctx_self_reference: SendSyncPtr::new(NonNull::dangling()),
1636            _marker: core::marker::PhantomPinned,
1637        }
1638    }
1639}
1640
1641/// Helper trait to plumb both core instances and component instances into
1642/// `OwnedInstance` below.
1643///
1644/// # Safety
1645///
1646/// This trait requires `layout` to correctly describe `Self` and appropriately
1647/// allocate space for `Self::VMContext` afterwards. Additionally the field
1648/// returned by `owned_vmctx()` must be the last field in the structure.
1649pub unsafe trait InstanceLayout {
1650    /// Whether or not to allocate this instance with `alloc_zeroed` or `alloc`.
1651    const INIT_ZEROED: bool;
1652
1653    /// The trailing `VMContext` type at the end of this instance.
1654    type VMContext;
1655
1656    /// The memory layout to use to allocate and deallocate this instance.
1657    fn layout(&self) -> Layout;
1658
1659    fn owned_vmctx(&self) -> &OwnedVMContext<Self::VMContext>;
1660    fn owned_vmctx_mut(&mut self) -> &mut OwnedVMContext<Self::VMContext>;
1661
1662    /// Returns the `vmctx_self_reference` set above.
1663    #[inline]
1664    fn vmctx(&self) -> NonNull<Self::VMContext> {
1665        // The definition of this method is subtle but intentional. The goal
1666        // here is that effectively this should return `&mut self.vmctx`, but
1667        // it's not quite so simple. Some more documentation is available on the
1668        // `vmctx_self_reference` field, but the general idea is that we're
1669        // creating a pointer to return with proper provenance. Provenance is
1670        // still in the works in Rust at the time of this writing but the load
1671        // of the `self.vmctx_self_reference` field is important here as it
1672        // affects how LLVM thinks about aliasing with respect to the returned
1673        // pointer.
1674        //
1675        // The intention of this method is to codegen to machine code as `&mut
1676        // self.vmctx`, however. While it doesn't show up like this in LLVM IR
1677        // (there's an actual load of the field) it does look like that by the
1678        // time the backend runs. (that's magic to me, the backend removing
1679        // loads...)
1680        let owned_vmctx = self.owned_vmctx();
1681        let owned_vmctx_raw = NonNull::from(owned_vmctx);
1682        // SAFETY: it's part of the contract of `InstanceLayout` and the usage
1683        // with `OwnedInstance` that this indeed points to the vmctx.
1684        let addr = unsafe { owned_vmctx_raw.add(1) };
1685        owned_vmctx
1686            .vmctx_self_reference
1687            .as_non_null()
1688            .with_addr(addr.addr())
1689    }
1690
1691    /// Helper function to access various locations offset from our `*mut
1692    /// VMContext` object.
1693    ///
1694    /// Note that this method takes `&self` as an argument but returns
1695    /// `NonNull<T>` which is frequently used to mutate said memory. This is an
1696    /// intentional design decision where the safety of the modification of
1697    /// memory is placed as a burden onto the caller. The implementation of this
1698    /// method explicitly does not require `&mut self` to acquire mutable
1699    /// provenance to update the `VMContext` region. Instead all pointers into
1700    /// the `VMContext` area have provenance/permissions to write.
1701    ///
1702    /// Also note though that care must be taken to ensure that reads/writes of
1703    /// memory must only happen where appropriate, for example a non-atomic
1704    /// write (as most are) should never happen concurrently with another read
1705    /// or write. It's generally on the burden of the caller to adhere to this.
1706    ///
1707    /// Also of note is that most of the time the usage of this method falls
1708    /// into one of:
1709    ///
1710    /// * Something in the VMContext is being read or written. In that case use
1711    ///   `vmctx_plus_offset` or `vmctx_plus_offset_mut` if possible due to
1712    ///   that having a safer lifetime.
1713    ///
1714    /// * A pointer is being created to pass to other VM* data structures. In
1715    ///   that situation the lifetime of all VM data structures are typically
1716    ///   tied to the `Store<T>` which is what provides the guarantees around
1717    ///   concurrency/etc.
1718    ///
1719    /// There's quite a lot of unsafety riding on this method, especially
1720    /// related to the ascription `T` of the byte `offset`. It's hoped that in
1721    /// the future we're able to settle on an in theory safer design.
1722    ///
1723    /// # Safety
1724    ///
1725    /// This method is unsafe because the `offset` must be within bounds of the
1726    /// `VMContext` object trailing this instance. Additionally `T` must be a
1727    /// valid ascription of the value that resides at that location.
1728    unsafe fn vmctx_plus_offset_raw<T: VmSafe>(&self, offset: impl Into<u32>) -> NonNull<T> {
1729        // SAFETY: the safety requirements of `byte_add` are forwarded to this
1730        // method's caller.
1731        unsafe {
1732            self.vmctx()
1733                .byte_add(usize::try_from(offset.into()).unwrap())
1734                .cast()
1735        }
1736    }
1737
1738    /// Helper above `vmctx_plus_offset_raw` which transfers the lifetime of
1739    /// `&self` to the returned reference `&T`.
1740    ///
1741    /// # Safety
1742    ///
1743    /// See the safety documentation of `vmctx_plus_offset_raw`.
1744    unsafe fn vmctx_plus_offset<T: VmSafe>(&self, offset: impl Into<u32>) -> &T {
1745        // SAFETY: this method has the same safety requirements as
1746        // `vmctx_plus_offset_raw`.
1747        unsafe { self.vmctx_plus_offset_raw(offset).as_ref() }
1748    }
1749
1750    /// Helper above `vmctx_plus_offset_raw` which transfers the lifetime of
1751    /// `&mut self` to the returned reference `&mut T`.
1752    ///
1753    /// # Safety
1754    ///
1755    /// See the safety documentation of `vmctx_plus_offset_raw`.
1756    unsafe fn vmctx_plus_offset_mut<T: VmSafe>(
1757        self: Pin<&mut Self>,
1758        offset: impl Into<u32>,
1759    ) -> &mut T {
1760        // SAFETY: this method has the same safety requirements as
1761        // `vmctx_plus_offset_raw`.
1762        unsafe { self.vmctx_plus_offset_raw(offset).as_mut() }
1763    }
1764}
1765
1766impl<T: InstanceLayout> OwnedInstance<T> {
1767    /// Allocates a new `OwnedInstance` and places `instance` inside of it.
1768    ///
1769    /// This will `instance`
1770    pub(super) fn new(mut instance: T) -> Result<OwnedInstance<T>, OutOfMemory> {
1771        let layout = instance.layout();
1772        debug_assert!(layout.size() >= size_of_val(&instance));
1773        debug_assert!(layout.align() >= align_of_val(&instance));
1774
1775        // SAFETY: it's up to us to assert that `layout` has a non-zero size,
1776        // which is asserted here.
1777        let ptr = unsafe {
1778            assert!(layout.size() > 0);
1779            if T::INIT_ZEROED {
1780                alloc::alloc::alloc_zeroed(layout)
1781            } else {
1782                alloc::alloc::alloc(layout)
1783            }
1784        };
1785        let Some(instance_ptr) = NonNull::new(ptr.cast::<T>()) else {
1786            return Err(OutOfMemory::new(layout.size()));
1787        };
1788
1789        // SAFETY: it's part of the unsafe contract of `InstanceLayout` that the
1790        // `add` here is appropriate for the layout allocated.
1791        let vmctx_self_reference = unsafe { instance_ptr.add(1).cast() };
1792        instance.owned_vmctx_mut().vmctx_self_reference = vmctx_self_reference.into();
1793
1794        // SAFETY: we allocated above and it's an unsafe contract of
1795        // `InstanceLayout` that the layout is suitable for writing the
1796        // instance.
1797        unsafe {
1798            instance_ptr.write(instance);
1799        }
1800
1801        let ret = OwnedInstance {
1802            instance: SendSyncPtr::new(instance_ptr),
1803            _marker: marker::PhantomData,
1804        };
1805
1806        // Double-check various vmctx calculations are correct.
1807        debug_assert_eq!(
1808            vmctx_self_reference.addr(),
1809            // SAFETY: `InstanceLayout` should guarantee it's safe to add 1 to
1810            // the last field to get a pointer to 1-byte-past-the-end of an
1811            // object, which should be valid.
1812            unsafe { NonNull::from(ret.get().owned_vmctx()).add(1).addr() }
1813        );
1814        debug_assert_eq!(vmctx_self_reference.addr(), ret.get().vmctx().addr());
1815
1816        Ok(ret)
1817    }
1818
1819    /// Gets the raw underlying `&Instance` from this handle.
1820    pub fn get(&self) -> &T {
1821        // SAFETY: this is an owned instance handle that retains exclusive
1822        // ownership of the `Instance` inside. With `&self` given we know
1823        // this pointer is valid valid and the returned lifetime is connected
1824        // to `self` so that should also be valid.
1825        unsafe { self.instance.as_non_null().as_ref() }
1826    }
1827
1828    /// Same as [`Self::get`] except for mutability.
1829    pub fn get_mut(&mut self) -> Pin<&mut T> {
1830        // SAFETY: The lifetime concerns here are the same as `get` above.
1831        // Otherwise `new_unchecked` is used here to uphold the contract that
1832        // instances are always pinned in memory.
1833        unsafe { Pin::new_unchecked(self.instance.as_non_null().as_mut()) }
1834    }
1835}
1836
1837impl<T: InstanceLayout> Drop for OwnedInstance<T> {
1838    fn drop(&mut self) {
1839        unsafe {
1840            let layout = self.get().layout();
1841            ptr::drop_in_place(self.instance.as_ptr());
1842            alloc::alloc::dealloc(self.instance.as_ptr().cast(), layout);
1843        }
1844    }
1845}
1846
1847#[derive(Debug)]
1848pub(crate) struct PassiveElementSegment {
1849    needs_gc_rooting: bool,
1850    elements: TryVec<ValRaw>,
1851}
1852
1853impl PassiveElementSegment {
1854    /// Create a new passive element segment with the given capacity.
1855    pub(crate) fn new(ty: WasmRefType, capacity: usize) -> Result<Self, OutOfMemory> {
1856        let mut elements = TryVec::with_capacity(capacity)?;
1857        elements.resize_with(capacity, || ValRaw::null())?;
1858        Ok(Self {
1859            needs_gc_rooting: ty.is_vmgcref_type_and_not_i31(),
1860            elements,
1861        })
1862    }
1863
1864    /// Clear this segment's elements.
1865    pub(crate) fn clear(&mut self, mut gc_store: Option<&mut GcStore>) {
1866        let elements = mem::take(&mut self.elements);
1867        if !self.needs_gc_rooting {
1868            return;
1869        }
1870        for val in elements {
1871            // Like above, `anyref` accessors are used here even if this
1872            // element segment has a different type because all of the vmgcref
1873            // types are treated the same way.
1874            let gc_ref = val.get_anyref();
1875            debug_assert_eq!(gc_ref, val.get_exnref());
1876            debug_assert_eq!(gc_ref, val.get_externref());
1877            if let Some(gc_ref) = VMGcRef::from_raw_u32(gc_ref) {
1878                if let Some(gc_store) = gc_store.as_deref_mut() {
1879                    let _ = gc_store.drop_gc_ref(gc_ref);
1880                }
1881            }
1882        }
1883    }
1884
1885    /// The elements of this segment.
1886    pub(crate) fn elements_mut(&mut self) -> &mut [ValRaw] {
1887        &mut self.elements
1888    }
1889}