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().vmctx_vmfunction_import(index)) }
422 }
423
424 /// Return the index `VMTableImport`.
425 fn imported_table(&self, index: TableIndex) -> &VMTableImport {
426 unsafe { self.vmctx_plus_offset(self.offsets().vmctx_vmtable_import(index)) }
427 }
428
429 /// Return the indexed `VMMemoryImport`.
430 fn imported_memory(&self, index: MemoryIndex) -> &VMMemoryImport {
431 unsafe { self.vmctx_plus_offset(self.offsets().vmctx_vmmemory_import(index)) }
432 }
433
434 /// Return the indexed `VMGlobalImport`.
435 fn imported_global(&self, index: GlobalIndex) -> &VMGlobalImport {
436 unsafe { self.vmctx_plus_offset(self.offsets().vmctx_vmglobal_import(index)) }
437 }
438
439 /// Return the indexed `VMTagImport`.
440 fn imported_tag(&self, index: TagIndex) -> &VMTagImport {
441 unsafe { self.vmctx_plus_offset(self.offsets().vmctx_vmtag_import(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().vmctx_vmtag_definition(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().vmctx_vmtable_definition(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().vmctx_vmmemory_pointer(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().vmctx_vmglobal_definition(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 #[cfg(feature = "component-model")]
748 VMGlobalKind::TaskMayBlock => {
749 // SAFETY: validity of this `&Instance` means validity of its
750 // imports meaning we can read the id of the vmctx within.
751 let id = unsafe {
752 let vmctx = super::component::VMComponentContext::from_opaque(
753 import.vmctx.unwrap().as_non_null(),
754 );
755 super::component::ComponentInstance::vmctx_instance_id(vmctx)
756 };
757 crate::Global::from_task_may_block(
758 crate::component::store::StoreComponentInstanceId::new(store, id),
759 )
760 }
761 }
762 }
763
764 /// Get an exported tag by index.
765 ///
766 /// # Panics
767 ///
768 /// Panics if the index is out-of-range.
769 pub fn get_exported_tag(&self, store: StoreId, index: TagIndex) -> crate::Tag {
770 let (id, def_index) = if let Some(def_index) = self.env_module().defined_tag_index(index) {
771 (self.id, def_index)
772 } else {
773 let import = self.imported_tag(index);
774 // SAFETY: validity of this `Instance` guarantees validity of the
775 // `vmctx` pointer being read here to find the transitive
776 // `InstanceId` that the import is associated with.
777 let id = unsafe { self.sibling_vmctx(import.vmctx.as_non_null()).id };
778 (id, import.index)
779 };
780 crate::Tag::from_raw(StoreInstanceId::new(store, id), def_index)
781 }
782
783 /// Grow memory by the specified amount of pages.
784 ///
785 /// Returns `None` if memory can't be grown by the specified amount
786 /// of pages. Returns `Some` with the old size in bytes if growth was
787 /// successful.
788 pub(crate) async fn memory_grow(
789 mut self: Pin<&mut Self>,
790 limiter: Option<&mut StoreResourceLimiter<'_>>,
791 idx: DefinedMemoryIndex,
792 delta: u64,
793 ) -> Result<Option<usize>, Error> {
794 let memory = &mut self.as_mut().memories_mut()[idx].1;
795
796 // SAFETY: this is the safe wrapper around `Memory::grow` because it
797 // automatically updates the `VMMemoryDefinition` in this instance after
798 // a growth operation below.
799 let result = unsafe { memory.grow(delta, limiter).await };
800
801 // Update the state used by a non-shared Wasm memory in case the base
802 // pointer and/or the length changed.
803 if memory.as_shared_memory().is_none() {
804 let vmmemory = memory.vmmemory();
805 self.set_memory(idx, vmmemory);
806 }
807
808 result
809 }
810
811 /// Performs a grow operation on the `table_index` specified using `grow`.
812 ///
813 /// This will handle updating the VMTableDefinition internally as necessary.
814 ///
815 /// # Safety
816 ///
817 /// This function requires that the caller, on success, fills in the table
818 /// elements with an appropriately typed value.
819 pub(crate) async unsafe fn defined_table_grow(
820 mut self: Pin<&mut Self>,
821 table_index: DefinedTableIndex,
822 limiter: Option<&mut StoreResourceLimiter<'_>>,
823 amt: u64,
824 ) -> Result<Option<usize>> {
825 let table = self.as_mut().get_defined_table(table_index);
826 // SAFETY: updating the `VMContext` table pointers and such is done
827 // below, and the responsibility of filling in the new table elements
828 // is forwarded to the caller.
829 let result = unsafe { table.grow(limiter, amt).await? };
830 let element = table.vmtable();
831 self.set_table(table_index, element);
832 Ok(result)
833 }
834
835 fn alloc_layout(offsets: &VMOffsets<HostPtr>) -> Layout {
836 let size = mem::size_of::<Self>()
837 .checked_add(usize::try_from(offsets.size_of_vmctx()).unwrap())
838 .unwrap();
839 let align = mem::align_of::<Self>();
840 Layout::from_size_align(size, align).unwrap()
841 }
842
843 fn type_ids_array(&self) -> NonNull<VmPtr<VMSharedTypeIndex>> {
844 unsafe { self.vmctx_plus_offset_raw(self.offsets().ptr.vmctx_type_ids_array()) }
845 }
846
847 /// Get a `&VMFuncRef` for the given `FuncIndex`.
848 ///
849 /// Returns `None` if the index is the reserved index value.
850 ///
851 /// The returned reference is a stable reference that won't be moved and can
852 /// be passed into JIT code.
853 pub(crate) fn get_func_ref(
854 self: Pin<&mut Self>,
855 registry: &ModuleRegistry,
856 index: FuncIndex,
857 ) -> Option<NonNull<VMFuncRef>> {
858 if index == FuncIndex::reserved_value() {
859 return None;
860 }
861
862 match self.env_module().defined_func_index(index) {
863 Some(index) => self.initialize_defined_funcref(registry, index),
864 None => {
865 debug_assert!(self.env_module().is_imported_function(index));
866 Some(self.imported_function(index).as_func_ref().into())
867 }
868 }
869 }
870
871 /// Initializes a defined function's `VMFuncRef` in-place and then returns a
872 /// pointer to that location.
873 fn initialize_defined_funcref(
874 self: Pin<&mut Self>,
875 registry: &ModuleRegistry,
876 def_index: DefinedFuncIndex,
877 ) -> Option<NonNull<VMFuncRef>> {
878 let module = self.env_module();
879 let index = module.func_index(def_index);
880 let func = &module.functions[index];
881 let type_index = func.signature.unwrap_engine_type_index();
882 let vmctx_offset = self.offsets().vmctx_func_ref(func.func_ref);
883 let array_to_wasm_key = FuncKey::ArrayToWasmTrampoline(module.module_index, def_index);
884 let wasm_key = FuncKey::DefinedWasmFunction(module.module_index, def_index);
885 // SAFETY: the type/offset/keys here are all valid for the defined
886 // function at `def_index`.
887 unsafe {
888 self.initialize_and_return_funcref(
889 registry,
890 type_index,
891 vmctx_offset,
892 array_to_wasm_key,
893 wasm_key,
894 )
895 }
896 }
897
898 fn get_start_func_ref(
899 self: Pin<&mut Self>,
900 registry: &ModuleRegistry,
901 ) -> Option<NonNull<VMFuncRef>> {
902 let module = self.env_module();
903 let type_index = match module.startup {
904 ModuleStartup::None => return None,
905 ModuleStartup::Always(t) | ModuleStartup::IfMemoriesNeedInit(t) => {
906 t.unwrap_engine_type_index()
907 }
908 };
909 let vmctx_offset = self.offsets().vmctx_startup_func_ref();
910 let array_to_wasm_key = FuncKey::ModuleStartup(Abi::Array, module.module_index);
911 let wasm_key = FuncKey::ModuleStartup(Abi::Wasm, module.module_index);
912 // SAFETY: the type/offset/keys here are all valid for the module
913 // startup function.
914 unsafe {
915 self.initialize_and_return_funcref(
916 registry,
917 type_index,
918 vmctx_offset,
919 array_to_wasm_key,
920 wasm_key,
921 )
922 }
923 }
924
925 /// Common implementation of initializing a `VMFuncRef` stored within this
926 /// instance's `VMContext`.
927 ///
928 /// # Safety
929 ///
930 /// This function requires that `type_index` accurately describes this
931 /// function and `vmctx_offset` is indeed the correct offset for the
932 /// functions here. Effectively all the arguments here must be "logically
933 /// correct" for the `VMFuncRef` being initialized.
934 unsafe fn initialize_and_return_funcref(
935 self: Pin<&mut Self>,
936 registry: &ModuleRegistry,
937 type_index: VMSharedTypeIndex,
938 vmctx_offset: u32,
939 array_to_wasm_key: FuncKey,
940 wasm_key: FuncKey,
941 ) -> Option<NonNull<VMFuncRef>> {
942 // For now, we eagerly initialize an funcref struct in-place whenever
943 // asked for a reference to it. This is mostly fine, because in practice
944 // each funcref is unlikely to be requested more than a few times:
945 // once-ish for funcref tables used for call_indirect (the usual
946 // compilation strategy places each function in the table at most once),
947 // and once or a few times when fetching exports via API. Note that for
948 // any case driven by table accesses, the lazy table init behaves like a
949 // higher-level cache layer that protects this initialization from
950 // happening multiple times, via that particular table at least.
951 //
952 // When `ref.func` becomes more commonly used or if we otherwise see a
953 // use-case where this becomes a hotpath, we can reconsider by using
954 // some state to track "uninitialized" explicitly, for example by
955 // zeroing the funcrefs (perhaps together with other
956 // zeroed-at-instantiate-time state) or using a separate is-initialized
957 // bitmap.
958 //
959 // We arrived at this design because zeroing memory is expensive, so
960 // it's better for instantiation performance if we don't have to track
961 // "is-initialized" state at all!
962
963 let module_with_code = ModuleWithCode::in_store(
964 registry,
965 self.runtime_module()
966 .expect("funcref impossible in fake module"),
967 )
968 .expect("module not in store");
969
970 let array_call =
971 VmPtr::from(NonNull::from(module_with_code.function(array_to_wasm_key)).cast());
972
973 let wasm_call = Some(VmPtr::from(
974 NonNull::new(
975 module_with_code
976 .function(wasm_key)
977 .as_ptr()
978 .cast::<VMWasmCallFunction>()
979 .cast_mut(),
980 )
981 .unwrap(),
982 ));
983
984 let vmctx = VMOpaqueContext::from_vmcontext(self.vmctx()).into();
985
986 // SAFETY: the offset calculated here should be correct with
987 // `self.offsets`
988 let func_ref_ptr = unsafe { self.vmctx_plus_offset_raw::<VMFuncRef>(vmctx_offset) };
989
990 // SAFETY: the `func_ref_ptr` should be valid as it's within our
991 // `VMContext` area.
992 unsafe {
993 func_ref_ptr.write(VMFuncRef {
994 array_call,
995 wasm_call,
996 vmctx,
997 type_index,
998 });
999 }
1000
1001 Some(func_ref_ptr)
1002 }
1003
1004 /// Get the passive elements segment at the given index.
1005 pub(crate) fn passive_element_segment(
1006 self: Pin<&mut Self>,
1007 passive: PassiveElemIndex,
1008 ) -> &mut [ValRaw] {
1009 self.passive_elements_mut()[passive.index()].elements_mut()
1010 }
1011
1012 pub(crate) fn passive_elements_mut(self: Pin<&mut Self>) -> &mut TryVec<PassiveElementSegment> {
1013 // SAFETY: Not moving data out of `self`.
1014 &mut unsafe { self.get_unchecked_mut() }.passive_elements
1015 }
1016
1017 /// Drop an element.
1018 pub(crate) fn passive_elem_drop(
1019 self: Pin<&mut Self>,
1020 gc_store: Option<&mut GcStore>,
1021 passive_index: PassiveElemIndex,
1022 ) -> Result<(), OutOfMemory> {
1023 self.passive_elements_mut()[passive_index.index()].clear(gc_store);
1024 Ok(())
1025 }
1026
1027 /// Get a locally-defined memory.
1028 pub fn get_defined_memory_mut(self: Pin<&mut Self>, index: DefinedMemoryIndex) -> &mut Memory {
1029 &mut self.memories_mut()[index].1
1030 }
1031
1032 /// Get a locally-defined memory.
1033 pub fn get_defined_memory(&self, index: DefinedMemoryIndex) -> &Memory {
1034 &self.memories[index].1
1035 }
1036
1037 pub fn get_defined_memory_vmimport(&self, index: DefinedMemoryIndex) -> VMMemoryImport {
1038 crate::runtime::vm::VMMemoryImport {
1039 from: self.memory_ptr(index).into(),
1040 vmctx: self.vmctx().into(),
1041 index,
1042 }
1043 }
1044
1045 /// Given an internal storage range of a Wasm data segment (or subset of a
1046 /// Wasm data segment), get the data's raw bytes.
1047 pub(crate) fn wasm_data(&self, range: Range<u32>) -> &[u8] {
1048 let start = usize::try_from(range.start).unwrap();
1049 let end = usize::try_from(range.end).unwrap();
1050 &self.runtime_info.wasm_data()[start..end]
1051 }
1052
1053 /// Returns the data for the runtime segment identified by `index`
1054 ///
1055 /// Does not take into account the dynamic size of the data pointed to by
1056 /// `index`, always returns the raw data from the module itself.
1057 ///
1058 /// # Panics
1059 ///
1060 /// Panics if `index` is out-of-bounds.
1061 fn runtime_data(&self, index: RuntimeDataIndex) -> &[u8] {
1062 let range = self.env_module().runtime_data[index].clone();
1063 self.wasm_data(range)
1064 }
1065
1066 /// Get a table by index regardless of whether it is locally-defined
1067 /// or an imported, foreign table. Ensure that the given range of
1068 /// elements in the table is lazily initialized. We define this
1069 /// operation all-in-one for safety, to ensure the lazy-init
1070 /// happens.
1071 ///
1072 /// Takes an `Iterator` for the index-range to lazy-initialize,
1073 /// for flexibility. This can be a range, single item, or empty
1074 /// sequence, for example. The iterator should return indices in
1075 /// increasing order, so that the break-at-out-of-bounds behavior
1076 /// works correctly.
1077 pub(crate) fn get_table_with_lazy_init(
1078 self: Pin<&mut Self>,
1079 registry: &ModuleRegistry,
1080 table_index: TableIndex,
1081 range: impl Iterator<Item = u64>,
1082 ) -> &mut Table {
1083 let (idx, instance) = self.defined_table_index_and_instance(table_index);
1084 instance.get_defined_table_with_lazy_init(registry, idx, range)
1085 }
1086
1087 /// Gets the raw runtime table data structure owned by this instance
1088 /// given the provided `idx`.
1089 ///
1090 /// The `range` specified is eagerly initialized for funcref tables.
1091 pub fn get_defined_table_with_lazy_init(
1092 mut self: Pin<&mut Self>,
1093 registry: &ModuleRegistry,
1094 idx: DefinedTableIndex,
1095 range: impl IntoIterator<Item = u64>,
1096 ) -> &mut Table {
1097 let elt_ty = self.tables[idx].1.element_type();
1098
1099 if elt_ty == TableElementType::Func {
1100 for i in range {
1101 match self.tables[idx].1.get_func_maybe_init(i) {
1102 // Uninitialized table element.
1103 Ok(None) => {}
1104 // Initialized table element, move on to the next.
1105 Ok(Some(_)) => continue,
1106 // Out-of-bounds; caller will handle by likely
1107 // throwing a trap. No work to do to lazy-init
1108 // beyond the end.
1109 Err(_) => break,
1110 };
1111
1112 // The table element `i` is uninitialized and is now being
1113 // initialized. This must imply that a `precompiled` list of
1114 // function indices is available for this table. The precompiled
1115 // list is extracted and then it is consulted with `i` to
1116 // determine the function that is going to be initialized. Note
1117 // that `i` may be outside the limits of the static
1118 // initialization so it's a fallible `get` instead of an index.
1119 let module = self.env_module();
1120 let precomputed = &module.table_initialization[idx];
1121 // Panicking here helps catch bugs rather than silently truncating by accident.
1122 let func_index = precomputed.get(usize::try_from(i).unwrap()).cloned();
1123 let func_ref = func_index
1124 .and_then(|func_index| self.as_mut().get_func_ref(registry, func_index));
1125 self.as_mut().tables_mut()[idx]
1126 .1
1127 .set_func(i, func_ref)
1128 .expect("Table type should match and index should be in-bounds");
1129 }
1130 }
1131
1132 self.get_defined_table(idx)
1133 }
1134
1135 /// Get a locally-defined table.
1136 pub(crate) fn get_defined_table(self: Pin<&mut Self>, index: DefinedTableIndex) -> &mut Table {
1137 &mut self.tables_mut()[index].1
1138 }
1139
1140 pub(crate) fn defined_table_index_and_instance<'a>(
1141 self: Pin<&'a mut Self>,
1142 index: TableIndex,
1143 ) -> (DefinedTableIndex, Pin<&'a mut Instance>) {
1144 if let Some(defined_table_index) = self.env_module().defined_table_index(index) {
1145 (defined_table_index, self)
1146 } else {
1147 let import = self.imported_table(index);
1148 let index = import.index;
1149 let vmctx = import.vmctx.as_non_null();
1150 // SAFETY: the validity of `self` means that the reachable instances
1151 // should also all be owned by the same store and fully initialized,
1152 // so it's safe to laterally move from a mutable borrow of this
1153 // instance to a mutable borrow of a sibling instance.
1154 let foreign_instance = unsafe { self.sibling_vmctx_mut(vmctx) };
1155 (index, foreign_instance)
1156 }
1157 }
1158
1159 /// Same as `self.runtime_info.env_module()` but additionally returns the
1160 /// `Pin<&mut Self>` with the same original lifetime.
1161 pub fn module_and_self(self: Pin<&mut Self>) -> (&wasmtime_environ::Module, Pin<&mut Self>) {
1162 // SAFETY: this function is projecting both `&Module` and the same
1163 // pointer both connected to the same lifetime. This is safe because
1164 // it's a contract of `Pin<&mut Self>` that the `runtime_info` field is
1165 // never written, meaning it's effectively unsafe to have `&mut Module`
1166 // projected from `Pin<&mut Self>`. Consequently it's safe to have a
1167 // read-only view of the field while still retaining mutable access to
1168 // all other fields.
1169 let module = self.runtime_info.env_module();
1170 let module = &raw const *module;
1171 let module = unsafe { &*module };
1172 (module, self)
1173 }
1174
1175 /// Initialize the VMContext data associated with this Instance.
1176 ///
1177 /// The `VMContext` memory is assumed to be uninitialized; any field
1178 /// that we need in a certain state will be explicitly written by this
1179 /// function.
1180 unsafe fn initialize_vmctx(self: Pin<&mut Self>, store: &StoreOpaque, imports: Imports) {
1181 let (module, mut instance) = self.module_and_self();
1182
1183 // SAFETY: the type of the magic field is indeed `u32` and this function
1184 // is initializing its value.
1185 unsafe {
1186 let offsets = instance.runtime_info.offsets();
1187 instance
1188 .vmctx_plus_offset_raw::<u32>(offsets.ptr.vmctx_magic())
1189 .write(VMCONTEXT_MAGIC);
1190 }
1191
1192 // SAFETY: it's up to the caller to provide a valid store pointer here.
1193 unsafe {
1194 instance.as_mut().set_store(store);
1195 }
1196
1197 // Initialize shared types
1198 //
1199 // SAFETY: validity of the vmctx means it should be safe to write to it
1200 // here.
1201 unsafe {
1202 let types = NonNull::from(instance.runtime_info.type_ids());
1203 instance.type_ids_array().write(types.cast().into());
1204 }
1205
1206 // Initialize the built-in functions
1207 //
1208 // SAFETY: the type of the builtin functions field is indeed a pointer
1209 // and the pointer being filled in here, plus the vmctx is valid to
1210 // write to during initialization.
1211 unsafe {
1212 static BUILTINS: VMBuiltinFunctionsArray = VMBuiltinFunctionsArray::INIT;
1213 let ptr = BUILTINS.expose_provenance();
1214 let offsets = instance.runtime_info.offsets();
1215 instance
1216 .vmctx_plus_offset_raw(offsets.ptr.vmctx_builtin_functions())
1217 .write(VmPtr::from(ptr));
1218 }
1219
1220 // Initialize the imports
1221 //
1222 // SAFETY: the vmctx is safe to initialize during this function and
1223 // validity of each item itself is a contract the caller must uphold.
1224 debug_assert_eq!(imports.functions.len(), module.num_imported_funcs);
1225 unsafe {
1226 let offsets = instance.runtime_info.offsets();
1227 ptr::copy_nonoverlapping(
1228 imports.functions.as_ptr(),
1229 instance
1230 .vmctx_plus_offset_raw(offsets.vmctx_imported_functions_begin())
1231 .as_ptr(),
1232 imports.functions.len(),
1233 );
1234 debug_assert_eq!(imports.tables.len(), module.num_imported_tables);
1235 ptr::copy_nonoverlapping(
1236 imports.tables.as_ptr(),
1237 instance
1238 .vmctx_plus_offset_raw(offsets.vmctx_imported_tables_begin())
1239 .as_ptr(),
1240 imports.tables.len(),
1241 );
1242 debug_assert_eq!(imports.memories.len(), module.num_imported_memories);
1243 ptr::copy_nonoverlapping(
1244 imports.memories.as_ptr(),
1245 instance
1246 .vmctx_plus_offset_raw(offsets.vmctx_imported_memories_begin())
1247 .as_ptr(),
1248 imports.memories.len(),
1249 );
1250 debug_assert_eq!(imports.globals.len(), module.num_imported_globals);
1251 ptr::copy_nonoverlapping(
1252 imports.globals.as_ptr(),
1253 instance
1254 .vmctx_plus_offset_raw(offsets.vmctx_imported_globals_begin())
1255 .as_ptr(),
1256 imports.globals.len(),
1257 );
1258 debug_assert_eq!(imports.tags.len(), module.num_imported_tags);
1259 ptr::copy_nonoverlapping(
1260 imports.tags.as_ptr(),
1261 instance
1262 .vmctx_plus_offset_raw(offsets.vmctx_imported_tags_begin())
1263 .as_ptr(),
1264 imports.tags.len(),
1265 );
1266 }
1267
1268 // N.B.: there is no need to initialize the funcrefs array because we
1269 // eagerly construct each element in it whenever asked for a reference
1270 // to that element. In other words, there is no state needed to track
1271 // the lazy-init, so we don't need to initialize any state now.
1272
1273 // Initialize the defined tables
1274 //
1275 // SAFETY: it's safe to initialize these tables during initialization
1276 // here and the various types of pointers and such here should all be
1277 // valid.
1278 unsafe {
1279 let offsets = instance.runtime_info.offsets();
1280 let mut ptr = instance.vmctx_plus_offset_raw(offsets.vmctx_tables_begin());
1281 let tables = instance.as_mut().tables_mut();
1282 for i in 0..module.num_defined_tables() {
1283 ptr.write(tables[DefinedTableIndex::new(i)].1.vmtable());
1284 ptr = ptr.add(1);
1285 }
1286 }
1287
1288 // Initialize the defined memories. This fills in both the
1289 // `defined_memories` table and the `owned_memories` table at the same
1290 // time. Entries in `defined_memories` hold a pointer to a definition
1291 // (all memories) whereas the `owned_memories` hold the actual
1292 // definitions of memories owned (not shared) in the module.
1293 //
1294 // SAFETY: it's safe to initialize these memories during initialization
1295 // here and the various types of pointers and such here should all be
1296 // valid.
1297 unsafe {
1298 let offsets = instance.runtime_info.offsets();
1299 let mut ptr = instance.vmctx_plus_offset_raw(offsets.vmctx_memories_begin());
1300 let mut owned_ptr =
1301 instance.vmctx_plus_offset_raw(offsets.vmctx_owned_memories_begin());
1302 let memories = instance.as_mut().memories_mut();
1303 for i in 0..module.num_defined_memories() {
1304 let defined_memory_index = DefinedMemoryIndex::new(i);
1305 let memory_index = module.memory_index(defined_memory_index);
1306 if module.memories[memory_index].shared {
1307 let def_ptr = memories[defined_memory_index]
1308 .1
1309 .as_shared_memory()
1310 .unwrap()
1311 .vmmemory_ptr();
1312 ptr.write(VmPtr::from(def_ptr));
1313 } else {
1314 owned_ptr.write(memories[defined_memory_index].1.vmmemory());
1315 ptr.write(VmPtr::from(owned_ptr));
1316 owned_ptr = owned_ptr.add(1);
1317 }
1318 ptr = ptr.add(1);
1319 }
1320 }
1321
1322 // Zero-initialize the globals so that nothing is uninitialized memory
1323 // after this function returns. The globals are actually initialized
1324 // with their const expression initializers after the instance is fully
1325 // allocated.
1326 //
1327 // SAFETY: it's safe to initialize globals during initialization
1328 // here. Note that while the value being written is not valid for all
1329 // types of globals it's initializing the memory to zero instead of
1330 // being in an undefined state. So it's still unsafe to access globals
1331 // after this, but if it's read then it'd hopefully crash faster than
1332 // leaving this undefined.
1333 unsafe {
1334 for i in 0..module.num_defined_globals() {
1335 let index = DefinedGlobalIndex::new(i);
1336 instance.global_ptr(index).write(VMGlobalDefinition::new());
1337 }
1338 for (index, val) in module.global_initializers.iter() {
1339 let mut def = VMGlobalDefinition::new();
1340 match val {
1341 GlobalConstValue::I32(i) => *def.as_i32_mut() = *i,
1342 GlobalConstValue::I64(i) => *def.as_i64_mut() = *i,
1343 GlobalConstValue::F32(i) => *def.as_f32_bits_mut() = *i,
1344 GlobalConstValue::F64(i) => *def.as_f64_bits_mut() = *i,
1345 GlobalConstValue::V128(i) => def.set_u128(*i),
1346 }
1347 instance.global_ptr(*index).write(def);
1348 }
1349 }
1350
1351 // Initialize the defined tags
1352 //
1353 // SAFETY: it's safe to initialize these tags during initialization
1354 // here and the various types of pointers and such here should all be
1355 // valid.
1356 unsafe {
1357 let offsets = instance.runtime_info.offsets();
1358 let mut ptr = instance.vmctx_plus_offset_raw(offsets.vmctx_tags_begin());
1359 for i in 0..module.num_defined_tags() {
1360 let defined_index = DefinedTagIndex::new(i);
1361 let tag_index = module.tag_index(defined_index);
1362 let tag = module.tags[tag_index];
1363 ptr.write(VMTagDefinition::new(
1364 tag.signature.unwrap_engine_type_index(),
1365 ));
1366 ptr = ptr.add(1);
1367 }
1368 }
1369
1370 // Initialize the lengths of runtime data segments.
1371 //
1372 // SAFETY: it's safe to initialize these lengths during initialization
1373 // here and the various types of pointers and such here should all be
1374 // valid.
1375 unsafe {
1376 let offsets = instance.runtime_info.offsets();
1377 let mut lengths =
1378 instance.vmctx_plus_offset_raw(offsets.vmctx_runtime_data_lengths_begin());
1379 let mut bases =
1380 instance.vmctx_plus_offset_raw(offsets.vmctx_runtime_data_bases_begin());
1381 for i in module.runtime_data.keys() {
1382 let data = instance.runtime_data(i);
1383 lengths.write(u32::try_from(data.len()).unwrap());
1384 lengths = lengths.add(1);
1385 bases.write(VmPtr::from(NonNull::from(data).cast::<u8>()));
1386 bases = bases.add(1);
1387 }
1388 }
1389
1390 // This is the half of the strategy of implementing memory-init-cow data
1391 // segments. Notably the compiled startup function, if present, will
1392 // skip data segments that have a null pointer. Here each linear memory
1393 // is tested to see if it needs initialization. If it does, then the
1394 // data segment is left in-place (and the startup function will
1395 // initialize linear memory). Otherwise the data segment is null'd out.
1396 // If the startup function runs (e.g. something else in the module needs
1397 // it), then the corresponding data segment's initialization will be
1398 // skipped.
1399 if let MemoryInitialization::Static { map } = &module.memory_initialization {
1400 for (memory, init) in map {
1401 let Some(memory) = module.defined_memory_index(memory) else {
1402 continue;
1403 };
1404 if instance.memories[memory].1.needs_init() {
1405 continue;
1406 }
1407 if let Some((_offset, data)) = init {
1408 let offsets = instance.runtime_info.offsets();
1409 unsafe {
1410 instance
1411 .vmctx_plus_offset_raw(offsets.vmctx_runtime_data_length(*data))
1412 .write(0u32);
1413 instance
1414 .vmctx_plus_offset_raw(offsets.vmctx_runtime_data_base(*data))
1415 .write(0usize);
1416 }
1417 }
1418 }
1419 }
1420 }
1421
1422 /// Attempts to convert from the host `addr` specified to a WebAssembly
1423 /// based address recorded in `WasmFault`.
1424 ///
1425 /// This method will check all linear memories that this instance contains
1426 /// to see if any of them contain `addr`. If one does then `Some` is
1427 /// returned with metadata about the wasm fault. Otherwise `None` is
1428 /// returned and `addr` doesn't belong to this instance.
1429 pub fn wasm_fault(&self, addr: usize) -> Option<WasmFault> {
1430 let mut fault = None;
1431 for (_, (_, memory)) in self.memories.iter() {
1432 let accessible = memory.wasm_accessible();
1433 if accessible.start <= addr && addr < accessible.end {
1434 // All linear memories should be disjoint so assert that no
1435 // prior fault has been found.
1436 assert!(fault.is_none());
1437 fault = Some(WasmFault {
1438 memory_size: memory.byte_size(),
1439 wasm_address: u64::try_from(addr - accessible.start).unwrap(),
1440 });
1441 }
1442 }
1443 fault
1444 }
1445
1446 /// Returns the id, within this instance's store, that it's assigned.
1447 pub fn id(&self) -> InstanceId {
1448 self.id
1449 }
1450
1451 /// Get all memories within this instance.
1452 ///
1453 /// Returns both import and defined memories.
1454 ///
1455 /// Returns both exported and non-exported memories.
1456 ///
1457 /// Gives access to the full memories space.
1458 pub fn all_memories(
1459 &self,
1460 store: StoreId,
1461 ) -> impl ExactSizeIterator<Item = (MemoryIndex, ExportMemory)> + '_ {
1462 self.env_module()
1463 .memories
1464 .iter()
1465 .map(move |(i, _)| (i, self.get_exported_memory(store, i)))
1466 }
1467
1468 /// Return the memories defined in this instance (not imported).
1469 pub fn defined_memories<'a>(
1470 &'a self,
1471 store: StoreId,
1472 ) -> impl ExactSizeIterator<Item = ExportMemory> + 'a {
1473 let num_imported = self.env_module().num_imported_memories;
1474 self.all_memories(store)
1475 .skip(num_imported)
1476 .map(|(_i, memory)| memory)
1477 }
1478
1479 /// Lookup an item with the given index.
1480 ///
1481 /// # Panics
1482 ///
1483 /// Panics if `export` is not valid for this instance.
1484 ///
1485 /// # Safety
1486 ///
1487 /// This function requires that `store` is the correct store which owns this
1488 /// instance.
1489 pub unsafe fn get_export_by_index_mut(
1490 self: Pin<&mut Self>,
1491 registry: &ModuleRegistry,
1492 store: StoreId,
1493 export: EntityIndex,
1494 ) -> Export {
1495 match export {
1496 // SAFETY: the contract of `store` owning the this instance is a
1497 // safety requirement of this function itself.
1498 EntityIndex::Function(i) => {
1499 Export::Function(unsafe { self.get_exported_func(registry, store, i) })
1500 }
1501 EntityIndex::Global(i) => Export::Global(self.get_exported_global(store, i)),
1502 EntityIndex::Table(i) => Export::Table(self.get_exported_table(store, i)),
1503 EntityIndex::Memory(i) => match self.get_exported_memory(store, i) {
1504 ExportMemory::Unshared(m) => Export::Memory(m),
1505 ExportMemory::Shared(m, i) => Export::SharedMemory(m, i),
1506 },
1507 EntityIndex::Tag(i) => Export::Tag(self.get_exported_tag(store, i)),
1508 }
1509 }
1510
1511 fn store_mut(self: Pin<&mut Self>) -> &mut Option<VMStoreRawPtr> {
1512 // SAFETY: this is a pin-projection to get a mutable reference to an
1513 // internal field and is safe so long as the `&mut Self` temporarily
1514 // created is not overwritten, which it isn't here.
1515 unsafe { &mut self.get_unchecked_mut().store }
1516 }
1517
1518 fn memories_mut(
1519 self: Pin<&mut Self>,
1520 ) -> &mut TryPrimaryMap<DefinedMemoryIndex, (MemoryAllocationIndex, Memory)> {
1521 // SAFETY: see `store_mut` above.
1522 unsafe { &mut self.get_unchecked_mut().memories }
1523 }
1524
1525 pub(crate) fn tables_mut(
1526 self: Pin<&mut Self>,
1527 ) -> &mut TryPrimaryMap<DefinedTableIndex, (TableAllocationIndex, Table)> {
1528 // SAFETY: see `store_mut` above.
1529 unsafe { &mut self.get_unchecked_mut().tables }
1530 }
1531
1532 #[cfg(feature = "wmemcheck")]
1533 pub(super) fn wmemcheck_state_mut(self: Pin<&mut Self>) -> &mut Option<Wmemcheck> {
1534 // SAFETY: see `store_mut` above.
1535 unsafe { &mut self.get_unchecked_mut().wmemcheck_state }
1536 }
1537
1538 pub(crate) fn needs_startup(&self) -> bool {
1539 match self.env_module().startup {
1540 ModuleStartup::None => false,
1541 ModuleStartup::Always(_) => true,
1542 ModuleStartup::IfMemoriesNeedInit(_) => self
1543 .memories
1544 .iter()
1545 .any(|(_, (_, memory))| memory.needs_init()),
1546 }
1547 }
1548}
1549
1550// SAFETY: `layout` should describe this accurately and `OwnedVMContext` is the
1551// last field of `ComponentInstance`.
1552unsafe impl InstanceLayout for Instance {
1553 const INIT_ZEROED: bool = false;
1554 type VMContext = VMContext;
1555
1556 fn layout(&self) -> Layout {
1557 Self::alloc_layout(self.runtime_info.offsets())
1558 }
1559
1560 fn owned_vmctx(&self) -> &OwnedVMContext<VMContext> {
1561 &self.vmctx
1562 }
1563
1564 fn owned_vmctx_mut(&mut self) -> &mut OwnedVMContext<VMContext> {
1565 &mut self.vmctx
1566 }
1567}
1568
1569pub type InstanceHandle = OwnedInstance<Instance>;
1570
1571/// A handle holding an `Instance` of a WebAssembly module.
1572///
1573/// This structure is an owning handle of the `instance` contained internally.
1574/// When this value goes out of scope it will deallocate the `Instance` and all
1575/// memory associated with it.
1576///
1577/// Note that this lives within a `StoreOpaque` on a list of instances that a
1578/// store is keeping alive.
1579#[derive(Debug)]
1580#[repr(transparent)] // guarantee this is a zero-cost wrapper
1581pub struct OwnedInstance<T: InstanceLayout> {
1582 /// The raw pointer to the instance that was allocated.
1583 ///
1584 /// Note that this is not equivalent to `Box<Instance>` because the
1585 /// allocation here has a `VMContext` trailing after it. Thus the custom
1586 /// destructor to invoke the `dealloc` function with the appropriate
1587 /// layout.
1588 instance: SendSyncPtr<T>,
1589 _marker: marker::PhantomData<Box<(T, OwnedVMContext<T::VMContext>)>>,
1590}
1591
1592/// Structure that must be placed at the end of a type implementing
1593/// `InstanceLayout`.
1594#[repr(align(16))] // match the alignment of VMContext
1595pub struct OwnedVMContext<T> {
1596 /// A pointer to the `vmctx` field at the end of the `structure`.
1597 ///
1598 /// If you're looking at this a reasonable question would be "why do we need
1599 /// a pointer to ourselves?" because after all the pointer's value is
1600 /// trivially derivable from any `&Instance` pointer. The rationale for this
1601 /// field's existence is subtle, but it's required for correctness. The
1602 /// short version is "this makes miri happy".
1603 ///
1604 /// The long version of why this field exists is that the rules that MIRI
1605 /// uses to ensure pointers are used correctly have various conditions on
1606 /// them depend on how pointers are used. More specifically if `*mut T` is
1607 /// derived from `&mut T`, then that invalidates all prior pointers derived
1608 /// from the `&mut T`. This means that while we liberally want to re-acquire
1609 /// a `*mut VMContext` throughout the implementation of `Instance` the
1610 /// trivial way, a function `fn vmctx(Pin<&mut Instance>) -> *mut VMContext`
1611 /// would effectively invalidate all prior `*mut VMContext` pointers
1612 /// acquired. The purpose of this field is to serve as a sort of
1613 /// source-of-truth for where `*mut VMContext` pointers come from.
1614 ///
1615 /// This field is initialized when the `Instance` is created with the
1616 /// original allocation's pointer. That means that the provenance of this
1617 /// pointer contains the entire allocation (both instance and `VMContext`).
1618 /// This provenance bit is then "carried through" where `fn vmctx` will base
1619 /// all returned pointers on this pointer itself. This provides the means of
1620 /// never invalidating this pointer throughout MIRI and additionally being
1621 /// able to still temporarily have `Pin<&mut Instance>` methods and such.
1622 ///
1623 /// It's important to note, though, that this is not here purely for MIRI.
1624 /// The careful construction of the `fn vmctx` method has ramifications on
1625 /// the LLVM IR generated, for example. A historical CVE on Wasmtime,
1626 /// GHSA-ch89-5g45-qwc7, was caused due to relying on undefined behavior. By
1627 /// deriving VMContext pointers from this pointer it specifically hints to
1628 /// LLVM that trickery is afoot and it properly informs `noalias` and such
1629 /// annotations and analysis. More-or-less this pointer is actually loaded
1630 /// in LLVM IR which helps defeat otherwise present aliasing optimizations,
1631 /// which we want, since writes to this should basically never be optimized
1632 /// out.
1633 ///
1634 /// As a final note it's worth pointing out that the machine code generated
1635 /// for accessing `fn vmctx` is still as one would expect. This member isn't
1636 /// actually ever loaded at runtime (or at least shouldn't be). Perhaps in
1637 /// the future if the memory consumption of this field is a problem we could
1638 /// shrink it slightly, but for now one extra pointer per wasm instance
1639 /// seems not too bad.
1640 vmctx_self_reference: SendSyncPtr<T>,
1641
1642 /// This field ensures that going from `Pin<&mut T>` to `&mut T` is not a
1643 /// safe operation.
1644 _marker: core::marker::PhantomPinned,
1645}
1646
1647impl<T> OwnedVMContext<T> {
1648 /// Creates a new blank vmctx to place at the end of an instance.
1649 pub fn new() -> OwnedVMContext<T> {
1650 OwnedVMContext {
1651 vmctx_self_reference: SendSyncPtr::new(NonNull::dangling()),
1652 _marker: core::marker::PhantomPinned,
1653 }
1654 }
1655}
1656
1657/// Helper trait to plumb both core instances and component instances into
1658/// `OwnedInstance` below.
1659///
1660/// # Safety
1661///
1662/// This trait requires `layout` to correctly describe `Self` and appropriately
1663/// allocate space for `Self::VMContext` afterwards. Additionally the field
1664/// returned by `owned_vmctx()` must be the last field in the structure.
1665pub unsafe trait InstanceLayout {
1666 /// Whether or not to allocate this instance with `alloc_zeroed` or `alloc`.
1667 const INIT_ZEROED: bool;
1668
1669 /// The trailing `VMContext` type at the end of this instance.
1670 type VMContext;
1671
1672 /// The memory layout to use to allocate and deallocate this instance.
1673 fn layout(&self) -> Layout;
1674
1675 fn owned_vmctx(&self) -> &OwnedVMContext<Self::VMContext>;
1676 fn owned_vmctx_mut(&mut self) -> &mut OwnedVMContext<Self::VMContext>;
1677
1678 /// Returns the `vmctx_self_reference` set above.
1679 #[inline]
1680 fn vmctx(&self) -> NonNull<Self::VMContext> {
1681 // The definition of this method is subtle but intentional. The goal
1682 // here is that effectively this should return `&mut self.vmctx`, but
1683 // it's not quite so simple. Some more documentation is available on the
1684 // `vmctx_self_reference` field, but the general idea is that we're
1685 // creating a pointer to return with proper provenance. Provenance is
1686 // still in the works in Rust at the time of this writing but the load
1687 // of the `self.vmctx_self_reference` field is important here as it
1688 // affects how LLVM thinks about aliasing with respect to the returned
1689 // pointer.
1690 //
1691 // The intention of this method is to codegen to machine code as `&mut
1692 // self.vmctx`, however. While it doesn't show up like this in LLVM IR
1693 // (there's an actual load of the field) it does look like that by the
1694 // time the backend runs. (that's magic to me, the backend removing
1695 // loads...)
1696 let owned_vmctx = self.owned_vmctx();
1697 let owned_vmctx_raw = NonNull::from(owned_vmctx);
1698 // SAFETY: it's part of the contract of `InstanceLayout` and the usage
1699 // with `OwnedInstance` that this indeed points to the vmctx.
1700 let addr = unsafe { owned_vmctx_raw.add(1) };
1701 owned_vmctx
1702 .vmctx_self_reference
1703 .as_non_null()
1704 .with_addr(addr.addr())
1705 }
1706
1707 /// Helper function to access various locations offset from our `*mut
1708 /// VMContext` object.
1709 ///
1710 /// Note that this method takes `&self` as an argument but returns
1711 /// `NonNull<T>` which is frequently used to mutate said memory. This is an
1712 /// intentional design decision where the safety of the modification of
1713 /// memory is placed as a burden onto the caller. The implementation of this
1714 /// method explicitly does not require `&mut self` to acquire mutable
1715 /// provenance to update the `VMContext` region. Instead all pointers into
1716 /// the `VMContext` area have provenance/permissions to write.
1717 ///
1718 /// Also note though that care must be taken to ensure that reads/writes of
1719 /// memory must only happen where appropriate, for example a non-atomic
1720 /// write (as most are) should never happen concurrently with another read
1721 /// or write. It's generally on the burden of the caller to adhere to this.
1722 ///
1723 /// Also of note is that most of the time the usage of this method falls
1724 /// into one of:
1725 ///
1726 /// * Something in the VMContext is being read or written. In that case use
1727 /// `vmctx_plus_offset` or `vmctx_plus_offset_mut` if possible due to
1728 /// that having a safer lifetime.
1729 ///
1730 /// * A pointer is being created to pass to other VM* data structures. In
1731 /// that situation the lifetime of all VM data structures are typically
1732 /// tied to the `Store<T>` which is what provides the guarantees around
1733 /// concurrency/etc.
1734 ///
1735 /// There's quite a lot of unsafety riding on this method, especially
1736 /// related to the ascription `T` of the byte `offset`. It's hoped that in
1737 /// the future we're able to settle on an in theory safer design.
1738 ///
1739 /// # Safety
1740 ///
1741 /// This method is unsafe because the `offset` must be within bounds of the
1742 /// `VMContext` object trailing this instance. Additionally `T` must be a
1743 /// valid ascription of the value that resides at that location.
1744 unsafe fn vmctx_plus_offset_raw<T: VmSafe>(&self, offset: impl Into<u32>) -> NonNull<T> {
1745 // SAFETY: the safety requirements of `byte_add` are forwarded to this
1746 // method's caller.
1747 unsafe {
1748 self.vmctx()
1749 .byte_add(usize::try_from(offset.into()).unwrap())
1750 .cast()
1751 }
1752 }
1753
1754 /// Helper above `vmctx_plus_offset_raw` which transfers the lifetime of
1755 /// `&self` to the returned reference `&T`.
1756 ///
1757 /// # Safety
1758 ///
1759 /// See the safety documentation of `vmctx_plus_offset_raw`.
1760 unsafe fn vmctx_plus_offset<T: VmSafe>(&self, offset: impl Into<u32>) -> &T {
1761 // SAFETY: this method has the same safety requirements as
1762 // `vmctx_plus_offset_raw`.
1763 unsafe { self.vmctx_plus_offset_raw(offset).as_ref() }
1764 }
1765
1766 /// Helper above `vmctx_plus_offset_raw` which transfers the lifetime of
1767 /// `&mut self` to the returned reference `&mut T`.
1768 ///
1769 /// # Safety
1770 ///
1771 /// See the safety documentation of `vmctx_plus_offset_raw`.
1772 unsafe fn vmctx_plus_offset_mut<T: VmSafe>(
1773 self: Pin<&mut Self>,
1774 offset: impl Into<u32>,
1775 ) -> &mut T {
1776 // SAFETY: this method has the same safety requirements as
1777 // `vmctx_plus_offset_raw`.
1778 unsafe { self.vmctx_plus_offset_raw(offset).as_mut() }
1779 }
1780}
1781
1782impl<T: InstanceLayout> OwnedInstance<T> {
1783 /// Allocates a new `OwnedInstance` and places `instance` inside of it.
1784 ///
1785 /// This will `instance`
1786 pub(super) fn new(mut instance: T) -> Result<OwnedInstance<T>, OutOfMemory> {
1787 let layout = instance.layout();
1788 debug_assert!(layout.size() >= size_of_val(&instance));
1789 debug_assert!(layout.align() >= align_of_val(&instance));
1790
1791 // SAFETY: it's up to us to assert that `layout` has a non-zero size,
1792 // which is asserted here.
1793 let ptr = unsafe {
1794 assert!(layout.size() > 0);
1795 if T::INIT_ZEROED {
1796 alloc::alloc::alloc_zeroed(layout)
1797 } else {
1798 alloc::alloc::alloc(layout)
1799 }
1800 };
1801 let Some(instance_ptr) = NonNull::new(ptr.cast::<T>()) else {
1802 return Err(OutOfMemory::new(layout.size()));
1803 };
1804
1805 // SAFETY: it's part of the unsafe contract of `InstanceLayout` that the
1806 // `add` here is appropriate for the layout allocated.
1807 let vmctx_self_reference = unsafe { instance_ptr.add(1).cast() };
1808 instance.owned_vmctx_mut().vmctx_self_reference = vmctx_self_reference.into();
1809
1810 // SAFETY: we allocated above and it's an unsafe contract of
1811 // `InstanceLayout` that the layout is suitable for writing the
1812 // instance.
1813 unsafe {
1814 instance_ptr.write(instance);
1815 }
1816
1817 let ret = OwnedInstance {
1818 instance: SendSyncPtr::new(instance_ptr),
1819 _marker: marker::PhantomData,
1820 };
1821
1822 // Double-check various vmctx calculations are correct.
1823 debug_assert_eq!(
1824 vmctx_self_reference.addr(),
1825 // SAFETY: `InstanceLayout` should guarantee it's safe to add 1 to
1826 // the last field to get a pointer to 1-byte-past-the-end of an
1827 // object, which should be valid.
1828 unsafe { NonNull::from(ret.get().owned_vmctx()).add(1).addr() }
1829 );
1830 debug_assert_eq!(vmctx_self_reference.addr(), ret.get().vmctx().addr());
1831
1832 Ok(ret)
1833 }
1834
1835 /// Gets the raw underlying `&Instance` from this handle.
1836 pub fn get(&self) -> &T {
1837 // SAFETY: this is an owned instance handle that retains exclusive
1838 // ownership of the `Instance` inside. With `&self` given we know
1839 // this pointer is valid valid and the returned lifetime is connected
1840 // to `self` so that should also be valid.
1841 unsafe { self.instance.as_non_null().as_ref() }
1842 }
1843
1844 /// Same as [`Self::get`] except for mutability.
1845 pub fn get_mut(&mut self) -> Pin<&mut T> {
1846 // SAFETY: The lifetime concerns here are the same as `get` above.
1847 // Otherwise `new_unchecked` is used here to uphold the contract that
1848 // instances are always pinned in memory.
1849 unsafe { Pin::new_unchecked(self.instance.as_non_null().as_mut()) }
1850 }
1851}
1852
1853impl<T: InstanceLayout> Drop for OwnedInstance<T> {
1854 fn drop(&mut self) {
1855 unsafe {
1856 let layout = self.get().layout();
1857 ptr::drop_in_place(self.instance.as_ptr());
1858 alloc::alloc::dealloc(self.instance.as_ptr().cast(), layout);
1859 }
1860 }
1861}
1862
1863#[derive(Debug)]
1864pub(crate) struct PassiveElementSegment {
1865 needs_gc_rooting: bool,
1866 elements: TryVec<ValRaw>,
1867}
1868
1869impl PassiveElementSegment {
1870 /// Create a new passive element segment with the given capacity.
1871 pub(crate) fn new(ty: WasmRefType, capacity: usize) -> Result<Self, OutOfMemory> {
1872 let mut elements = TryVec::with_capacity(capacity)?;
1873 elements.resize_with(capacity, || ValRaw::null())?;
1874 Ok(Self {
1875 needs_gc_rooting: ty.is_vmgcref_type_and_not_i31(),
1876 elements,
1877 })
1878 }
1879
1880 /// Clear this segment's elements.
1881 pub(crate) fn clear(&mut self, mut gc_store: Option<&mut GcStore>) {
1882 let elements = mem::take(&mut self.elements);
1883 if !self.needs_gc_rooting {
1884 return;
1885 }
1886 for val in elements {
1887 // Like above, `anyref` accessors are used here even if this
1888 // element segment has a different type because all of the vmgcref
1889 // types are treated the same way.
1890 let gc_ref = val.get_anyref();
1891 debug_assert_eq!(gc_ref, val.get_exnref());
1892 debug_assert_eq!(gc_ref, val.get_externref());
1893 if let Some(gc_ref) = VMGcRef::from_raw_u32(gc_ref) {
1894 if let Some(gc_store) = gc_store.as_deref_mut() {
1895 let _ = gc_store.drop_gc_ref(gc_ref);
1896 }
1897 }
1898 }
1899 }
1900
1901 /// The elements of this segment.
1902 pub(crate) fn elements_mut(&mut self) -> &mut [ValRaw] {
1903 &mut self.elements
1904 }
1905}