wasmtime/runtime/vm/vmcontext.rs
1//! This file declares `VMContext` and several related structs which contain
2//! fields that compiled wasm code accesses directly.
3
4mod vm_host_func_context;
5
6pub use self::vm_host_func_context::VMArrayCallHostFuncContext;
7use crate::prelude::*;
8use crate::runtime::vm::{InterpreterRef, VMGcRef, VmPtr, VmSafe, f32x4, f64x2, i8x16};
9use crate::store::StoreOpaque;
10use crate::vm::stack_switching::VMStackChain;
11use core::cell::UnsafeCell;
12use core::ffi::c_void;
13use core::fmt;
14use core::marker;
15use core::mem::{self, MaybeUninit};
16use core::ops::Range;
17use core::ptr::{self, NonNull};
18use core::sync::atomic::{AtomicUsize, Ordering};
19use wasmtime_environ::{
20 BuiltinFunctionIndex, DefinedGlobalIndex, DefinedMemoryIndex, DefinedTableIndex,
21 DefinedTagIndex, NUM_COMPONENT_CONTEXT_SLOTS, VMCONTEXT_MAGIC, VMSharedTypeIndex,
22};
23
24/// A function pointer that exposes the array calling convention.
25///
26/// Regardless of the underlying Wasm function type, all functions using the
27/// array calling convention have the same Rust signature.
28///
29/// Arguments:
30///
31/// * Callee `vmctx` for the function itself.
32///
33/// * Caller's `vmctx` (so that host functions can access the linear memory of
34/// their Wasm callers).
35///
36/// * A pointer to a buffer of `ValRaw`s where both arguments are passed into
37/// this function, and where results are returned from this function.
38///
39/// * The capacity of the `ValRaw` buffer. Must always be at least
40/// `max(len(wasm_params), len(wasm_results))`.
41///
42/// Return value:
43///
44/// * `true` if this call succeeded.
45/// * `false` if this call failed and a trap was recorded in TLS.
46pub type VMArrayCallNative = unsafe extern "C" fn(
47 NonNull<VMOpaqueContext>,
48 NonNull<VMContext>,
49 NonNull<ValRaw>,
50 usize,
51) -> bool;
52
53/// An opaque function pointer which might be `VMArrayCallNative` or it might be
54/// pulley bytecode. Requires external knowledge to determine what kind of
55/// function pointer this is.
56#[repr(transparent)]
57pub struct VMArrayCallFunction(VMFunctionBody);
58
59/// A function pointer that exposes the Wasm calling convention.
60///
61/// In practice, different Wasm function types end up mapping to different Rust
62/// function types, so this isn't simply a type alias the way that
63/// `VMArrayCallFunction` is. However, the exact details of the calling
64/// convention are left to the Wasm compiler (e.g. Cranelift or Winch). Runtime
65/// code never does anything with these function pointers except shuffle them
66/// around and pass them back to Wasm.
67#[repr(transparent)]
68pub struct VMWasmCallFunction(VMFunctionBody);
69
70/// An imported function.
71///
72/// Basically the same as `VMFuncRef`, except that `wasm_call` is not optional.
73#[derive(Debug, Clone)]
74#[repr(C)]
75pub struct VMFunctionImport {
76 /// Same as `VMFuncRef::array_call`.
77 pub array_call: VmPtr<VMArrayCallFunction>,
78
79 /// Same as `VMFuncRef::wasm_call`, except always non-null. Must be filled
80 /// in by the time Wasm is importing this function!
81 pub wasm_call: VmPtr<VMWasmCallFunction>,
82
83 /// Function signature's _actual_ type id.
84 ///
85 /// This is the type that the function was defined with, not the type that
86 /// it was imported as. These two can be different in the face of subtyping
87 /// and we need the former for to correctly implement dynamic downcasts.
88 pub type_index: VMSharedTypeIndex,
89
90 /// Same as `VMFuncRef::vmctx`.
91 pub vmctx: VmPtr<VMOpaqueContext>,
92 // If more elements are added here, remember to add offset_of tests below!
93}
94
95// SAFETY: the above structure is repr(C) and only contains `VmSafe` fields.
96unsafe impl VmSafe for VMFunctionImport {}
97
98impl VMFunctionImport {
99 /// Convert `&VMFunctionImport` into `&VMFuncRef`.
100 pub fn as_func_ref(&self) -> &VMFuncRef {
101 // Safety: `VMFunctionImport` and `VMFuncRef` have the same
102 // representation.
103 unsafe { Self::as_non_null_func_ref(NonNull::from(self)).as_ref() }
104 }
105
106 /// Convert `NonNull<VMFunctionImport>` into `NonNull<VMFuncRef>`.
107 pub fn as_non_null_func_ref(p: NonNull<VMFunctionImport>) -> NonNull<VMFuncRef> {
108 p.cast()
109 }
110
111 /// Convert `*mut VMFunctionImport` into `*mut VMFuncRef`.
112 pub fn as_func_ref_ptr(p: *mut VMFunctionImport) -> *mut VMFuncRef {
113 p.cast()
114 }
115}
116
117#[cfg(test)]
118mod test_vmfunction_import {
119 use super::{VMFuncRef, VMFunctionImport};
120 use core::mem::offset_of;
121 use std::mem::size_of;
122 use wasmtime_environ::{HostPtr, Module, StaticModuleIndex, VMOffsets};
123
124 #[test]
125 fn check_vmfunction_import_offsets() {
126 let module = Module::new(StaticModuleIndex::from_u32(0));
127 let offsets = VMOffsets::new(HostPtr, &module);
128 assert_eq!(
129 size_of::<VMFunctionImport>(),
130 usize::from(offsets.size_of_vmfunction_import())
131 );
132 assert_eq!(
133 offset_of!(VMFunctionImport, array_call),
134 usize::from(offsets.vmfunction_import_array_call())
135 );
136 assert_eq!(
137 offset_of!(VMFunctionImport, wasm_call),
138 usize::from(offsets.vmfunction_import_wasm_call())
139 );
140 assert_eq!(
141 offset_of!(VMFunctionImport, type_index),
142 usize::from(offsets.vmfunction_import_type_index())
143 );
144 assert_eq!(
145 offset_of!(VMFunctionImport, vmctx),
146 usize::from(offsets.vmfunction_import_vmctx())
147 );
148 }
149
150 #[test]
151 fn vmfunction_import_and_vmfunc_ref_have_same_layout() {
152 assert_eq!(size_of::<VMFunctionImport>(), size_of::<VMFuncRef>());
153 assert_eq!(
154 offset_of!(VMFunctionImport, array_call),
155 offset_of!(VMFuncRef, array_call),
156 );
157 assert_eq!(
158 offset_of!(VMFunctionImport, wasm_call),
159 offset_of!(VMFuncRef, wasm_call),
160 );
161 assert_eq!(
162 offset_of!(VMFunctionImport, type_index),
163 offset_of!(VMFuncRef, type_index),
164 );
165 assert_eq!(
166 offset_of!(VMFunctionImport, vmctx),
167 offset_of!(VMFuncRef, vmctx),
168 );
169 }
170}
171
172/// A placeholder byte-sized type which is just used to provide some amount of type
173/// safety when dealing with pointers to JIT-compiled function bodies. Note that it's
174/// deliberately not Copy, as we shouldn't be carelessly copying function body bytes
175/// around.
176#[repr(C)]
177pub struct VMFunctionBody(u8);
178
179// SAFETY: this structure is never read and is safe to pass to jit code.
180unsafe impl VmSafe for VMFunctionBody {}
181
182#[cfg(test)]
183mod test_vmfunction_body {
184 use super::VMFunctionBody;
185 use std::mem::size_of;
186
187 #[test]
188 fn check_vmfunction_body_offsets() {
189 assert_eq!(size_of::<VMFunctionBody>(), 1);
190 }
191}
192
193/// The fields compiled code needs to access to utilize a WebAssembly table
194/// imported from another instance.
195#[derive(Debug, Copy, Clone)]
196#[repr(C)]
197pub struct VMTableImport {
198 /// A pointer to the imported table description.
199 pub from: VmPtr<VMTableDefinition>,
200
201 /// A pointer to the `VMContext` that owns the table description.
202 pub vmctx: VmPtr<VMContext>,
203
204 /// The table index, within `vmctx`, this definition resides at.
205 pub index: DefinedTableIndex,
206}
207
208// SAFETY: the above structure is repr(C) and only contains `VmSafe` fields.
209unsafe impl VmSafe for VMTableImport {}
210
211#[cfg(test)]
212mod test_vmtable {
213 use super::VMTableImport;
214 use core::mem::offset_of;
215 use std::mem::size_of;
216 use wasmtime_environ::component::{Component, VMComponentOffsets};
217 use wasmtime_environ::{HostPtr, Module, StaticModuleIndex, VMOffsets};
218
219 #[test]
220 fn check_vmtable_offsets() {
221 let module = Module::new(StaticModuleIndex::from_u32(0));
222 let offsets = VMOffsets::new(HostPtr, &module);
223 assert_eq!(
224 size_of::<VMTableImport>(),
225 usize::from(offsets.size_of_vmtable_import())
226 );
227 assert_eq!(
228 offset_of!(VMTableImport, from),
229 usize::from(offsets.vmtable_import_from())
230 );
231 assert_eq!(
232 offset_of!(VMTableImport, vmctx),
233 usize::from(offsets.vmtable_import_vmctx())
234 );
235 assert_eq!(
236 offset_of!(VMTableImport, index),
237 usize::from(offsets.vmtable_import_index())
238 );
239 }
240
241 #[test]
242 fn ensure_sizes_match() {
243 // Because we use `VMTableImport` for recording tables used by components, we
244 // want to make sure that the size calculations between `VMOffsets` and
245 // `VMComponentOffsets` stay the same.
246 let module = Module::new(StaticModuleIndex::from_u32(0));
247 let vm_offsets = VMOffsets::new(HostPtr, &module);
248 let component = Component::default();
249 let vm_component_offsets = VMComponentOffsets::new(HostPtr, &component);
250 assert_eq!(
251 vm_offsets.size_of_vmtable_import(),
252 vm_component_offsets.size_of_vmtable_import()
253 );
254 }
255}
256
257/// The fields compiled code needs to access to utilize a WebAssembly linear
258/// memory imported from another instance.
259#[derive(Debug, Copy, Clone)]
260#[repr(C)]
261pub struct VMMemoryImport {
262 /// A pointer to the imported memory description.
263 pub from: VmPtr<VMMemoryDefinition>,
264
265 /// A pointer to the `VMContext` that owns the memory description.
266 pub vmctx: VmPtr<VMContext>,
267
268 /// The index of the memory in the containing `vmctx`.
269 pub index: DefinedMemoryIndex,
270}
271
272// SAFETY: the above structure is repr(C) and only contains `VmSafe` fields.
273unsafe impl VmSafe for VMMemoryImport {}
274
275#[cfg(test)]
276mod test_vmmemory_import {
277 use super::VMMemoryImport;
278 use core::mem::offset_of;
279 use std::mem::size_of;
280 use wasmtime_environ::{HostPtr, Module, StaticModuleIndex, VMOffsets};
281
282 #[test]
283 fn check_vmmemory_import_offsets() {
284 let module = Module::new(StaticModuleIndex::from_u32(0));
285 let offsets = VMOffsets::new(HostPtr, &module);
286 assert_eq!(
287 size_of::<VMMemoryImport>(),
288 usize::from(offsets.size_of_vmmemory_import())
289 );
290 assert_eq!(
291 offset_of!(VMMemoryImport, from),
292 usize::from(offsets.vmmemory_import_from())
293 );
294 assert_eq!(
295 offset_of!(VMMemoryImport, vmctx),
296 usize::from(offsets.vmmemory_import_vmctx())
297 );
298 assert_eq!(
299 offset_of!(VMMemoryImport, index),
300 usize::from(offsets.vmmemory_import_index())
301 );
302 }
303}
304
305/// The fields compiled code needs to access to utilize a WebAssembly global
306/// variable imported from another instance.
307///
308/// Note that unlike with functions, tables, and memories, `VMGlobalImport`
309/// doesn't include a `vmctx` pointer. Globals are never resized, and don't
310/// require a `vmctx` pointer to access.
311#[derive(Debug, Copy, Clone)]
312#[repr(C)]
313pub struct VMGlobalImport {
314 /// A pointer to the imported global variable description.
315 pub from: VmPtr<VMGlobalDefinition>,
316
317 /// A pointer to the context that owns the global.
318 ///
319 /// Exactly what's stored here is dictated by `kind` below. This is `None`
320 /// for `VMGlobalKind::Host`, it's a `VMContext` for
321 /// `VMGlobalKind::Instance`, and it's `VMComponentContext` for
322 /// `VMGlobalKind::ComponentFlags`.
323 pub vmctx: Option<VmPtr<VMOpaqueContext>>,
324
325 /// The kind of global, and extra location information in addition to
326 /// `vmctx` above.
327 pub kind: VMGlobalKind,
328}
329
330// SAFETY: the above structure is repr(C) and only contains `VmSafe` fields.
331unsafe impl VmSafe for VMGlobalImport {}
332
333/// The kinds of globals that Wasmtime has.
334#[derive(Debug, Copy, Clone)]
335#[repr(C, u32)]
336pub enum VMGlobalKind {
337 /// Host globals, stored in a `StoreOpaque`.
338 Host(DefinedGlobalIndex),
339 /// Instance globals, stored in `VMContext`s
340 Instance(DefinedGlobalIndex),
341 /// Flags for a component instance, stored in `VMComponentContext`.
342 #[cfg(feature = "component-model")]
343 ComponentFlags(wasmtime_environ::component::RuntimeComponentInstanceIndex),
344 #[cfg(feature = "component-model")]
345 TaskMayBlock,
346}
347
348// SAFETY: the above enum is repr(C) and stores nothing else
349unsafe impl VmSafe for VMGlobalKind {}
350
351#[cfg(test)]
352mod test_vmglobal_import {
353 use super::VMGlobalImport;
354 use core::mem::offset_of;
355 use std::mem::size_of;
356 use wasmtime_environ::{HostPtr, Module, StaticModuleIndex, VMOffsets};
357
358 #[test]
359 fn check_vmglobal_import_offsets() {
360 let module = Module::new(StaticModuleIndex::from_u32(0));
361 let offsets = VMOffsets::new(HostPtr, &module);
362 assert_eq!(
363 size_of::<VMGlobalImport>(),
364 usize::from(offsets.size_of_vmglobal_import())
365 );
366 assert_eq!(
367 offset_of!(VMGlobalImport, from),
368 usize::from(offsets.vmglobal_import_from())
369 );
370 }
371}
372
373/// The fields compiled code needs to access to utilize a WebAssembly
374/// tag imported from another instance.
375#[derive(Debug, Copy, Clone)]
376#[repr(C)]
377pub struct VMTagImport {
378 /// A pointer to the imported tag description.
379 pub from: VmPtr<VMTagDefinition>,
380
381 /// The instance that owns this tag.
382 pub vmctx: VmPtr<VMContext>,
383
384 /// The index of the tag in the containing `vmctx`.
385 pub index: DefinedTagIndex,
386}
387
388// SAFETY: the above structure is repr(C) and only contains `VmSafe` fields.
389unsafe impl VmSafe for VMTagImport {}
390
391#[cfg(test)]
392mod test_vmtag_import {
393 use super::VMTagImport;
394 use core::mem::{offset_of, size_of};
395 use wasmtime_environ::{HostPtr, Module, StaticModuleIndex, VMOffsets};
396
397 #[test]
398 fn check_vmtag_import_offsets() {
399 let module = Module::new(StaticModuleIndex::from_u32(0));
400 let offsets = VMOffsets::new(HostPtr, &module);
401 assert_eq!(
402 size_of::<VMTagImport>(),
403 usize::from(offsets.size_of_vmtag_import())
404 );
405 assert_eq!(
406 offset_of!(VMTagImport, from),
407 usize::from(offsets.vmtag_import_from())
408 );
409 assert_eq!(
410 offset_of!(VMTagImport, vmctx),
411 usize::from(offsets.vmtag_import_vmctx())
412 );
413 assert_eq!(
414 offset_of!(VMTagImport, index),
415 usize::from(offsets.vmtag_import_index())
416 );
417 }
418}
419
420/// The fields compiled code needs to access to utilize a WebAssembly linear
421/// memory defined within the instance, namely the start address and the
422/// size in bytes.
423#[derive(Debug)]
424#[repr(C)]
425pub struct VMMemoryDefinition {
426 /// The start address.
427 pub base: VmPtr<u8>,
428
429 /// The current logical size of this linear memory in bytes.
430 ///
431 /// This is atomic because shared memories must be able to grow their length
432 /// atomically. For relaxed access, see
433 /// [`VMMemoryDefinition::current_length()`].
434 pub current_length: AtomicUsize,
435}
436
437// SAFETY: the above definition has `repr(C)` and each field individually
438// implements `VmSafe`, which satisfies the requirements of this trait.
439unsafe impl VmSafe for VMMemoryDefinition {}
440
441impl VMMemoryDefinition {
442 /// Return the current length (in bytes) of the [`VMMemoryDefinition`] by
443 /// performing a relaxed load; do not use this function for situations in
444 /// which a precise length is needed. Owned memories (i.e., non-shared) will
445 /// always return a precise result (since no concurrent modification is
446 /// possible) but shared memories may see an imprecise value--a
447 /// `current_length` potentially smaller than what some other thread
448 /// observes. Since Wasm memory only grows, this under-estimation may be
449 /// acceptable in certain cases.
450 #[inline]
451 pub fn current_length(&self) -> usize {
452 self.current_length.load(Ordering::Relaxed)
453 }
454
455 /// Return a copy of the [`VMMemoryDefinition`] using the relaxed value of
456 /// `current_length`; see [`VMMemoryDefinition::current_length()`].
457 #[inline]
458 pub unsafe fn load(ptr: *mut Self) -> Self {
459 let other = unsafe { &*ptr };
460 VMMemoryDefinition {
461 base: other.base,
462 current_length: other.current_length().into(),
463 }
464 }
465}
466
467#[cfg(test)]
468mod test_vmmemory_definition {
469 use super::VMMemoryDefinition;
470 use core::mem::offset_of;
471 use std::mem::size_of;
472 use wasmtime_environ::{HostPtr, Module, PtrSize, StaticModuleIndex, VMOffsets};
473
474 #[test]
475 fn check_vmmemory_definition_offsets() {
476 let module = Module::new(StaticModuleIndex::from_u32(0));
477 let offsets = VMOffsets::new(HostPtr, &module);
478 assert_eq!(
479 size_of::<VMMemoryDefinition>(),
480 usize::from(offsets.ptr.size_of_vmmemory_definition())
481 );
482 assert_eq!(
483 offset_of!(VMMemoryDefinition, base),
484 usize::from(offsets.ptr.vmmemory_definition_base())
485 );
486 assert_eq!(
487 offset_of!(VMMemoryDefinition, current_length),
488 usize::from(offsets.ptr.vmmemory_definition_current_length())
489 );
490 /* TODO: Assert that the size of `current_length` matches.
491 assert_eq!(
492 size_of::<VMMemoryDefinition::current_length>(),
493 usize::from(offsets.size_of_vmmemory_definition_current_length())
494 );
495 */
496 }
497}
498
499/// The fields compiled code needs to access to utilize a WebAssembly table
500/// defined within the instance.
501#[derive(Debug, Copy, Clone)]
502#[repr(C)]
503pub struct VMTableDefinition {
504 /// Pointer to the table data.
505 pub base: VmPtr<u8>,
506
507 /// The current number of elements in the table.
508 pub current_elements: usize,
509}
510
511// SAFETY: the above structure is repr(C) and only contains `VmSafe` fields.
512unsafe impl VmSafe for VMTableDefinition {}
513
514#[cfg(test)]
515mod test_vmtable_definition {
516 use super::VMTableDefinition;
517 use core::mem::offset_of;
518 use std::mem::size_of;
519 use wasmtime_environ::{HostPtr, Module, StaticModuleIndex, VMOffsets};
520
521 #[test]
522 fn check_vmtable_definition_offsets() {
523 let module = Module::new(StaticModuleIndex::from_u32(0));
524 let offsets = VMOffsets::new(HostPtr, &module);
525 assert_eq!(
526 size_of::<VMTableDefinition>(),
527 usize::from(offsets.size_of_vmtable_definition())
528 );
529 assert_eq!(
530 offset_of!(VMTableDefinition, base),
531 usize::from(offsets.vmtable_definition_base())
532 );
533 assert_eq!(
534 offset_of!(VMTableDefinition, current_elements),
535 usize::from(offsets.vmtable_definition_current_elements())
536 );
537 }
538}
539
540/// The storage for a WebAssembly global defined within the instance.
541///
542/// TODO: Pack the globals more densely, rather than using the same size
543/// for every type.
544#[derive(Debug)]
545#[repr(C, align(16))]
546pub struct VMGlobalDefinition {
547 storage: [u8; 16],
548 // If more elements are added here, remember to add offset_of tests below!
549}
550
551// SAFETY: the above structure is repr(C) and only contains `VmSafe` fields.
552unsafe impl VmSafe for VMGlobalDefinition {}
553
554#[cfg(test)]
555mod test_vmglobal_definition {
556 use super::VMGlobalDefinition;
557 use std::mem::{align_of, size_of};
558 use wasmtime_environ::{HostPtr, Module, PtrSize, StaticModuleIndex, VMOffsets};
559
560 #[test]
561 fn check_vmglobal_definition_alignment() {
562 assert!(align_of::<VMGlobalDefinition>() >= align_of::<i32>());
563 assert!(align_of::<VMGlobalDefinition>() >= align_of::<i64>());
564 assert!(align_of::<VMGlobalDefinition>() >= align_of::<f32>());
565 assert!(align_of::<VMGlobalDefinition>() >= align_of::<f64>());
566 assert!(align_of::<VMGlobalDefinition>() >= align_of::<[u8; 16]>());
567 assert!(align_of::<VMGlobalDefinition>() >= align_of::<[f32; 4]>());
568 assert!(align_of::<VMGlobalDefinition>() >= align_of::<[f64; 2]>());
569 }
570
571 #[test]
572 fn check_vmglobal_definition_offsets() {
573 let module = Module::new(StaticModuleIndex::from_u32(0));
574 let offsets = VMOffsets::new(HostPtr, &module);
575 assert_eq!(
576 size_of::<VMGlobalDefinition>(),
577 usize::from(offsets.ptr.size_of_vmglobal_definition())
578 );
579 }
580
581 #[test]
582 fn check_vmglobal_begins_aligned() {
583 let module = Module::new(StaticModuleIndex::from_u32(0));
584 let offsets = VMOffsets::new(HostPtr, &module);
585 assert_eq!(offsets.vmctx_globals_begin() % 16, 0);
586 }
587
588 #[test]
589 #[cfg(feature = "gc")]
590 fn check_vmglobal_can_contain_gc_ref() {
591 assert!(size_of::<crate::runtime::vm::VMGcRef>() <= size_of::<VMGlobalDefinition>());
592 }
593}
594
595impl VMGlobalDefinition {
596 /// Construct a `VMGlobalDefinition`.
597 pub fn new() -> Self {
598 Self { storage: [0; 16] }
599 }
600
601 /// Return a reference to the value as an i32.
602 pub unsafe fn as_i32(&self) -> &i32 {
603 unsafe { &*(self.storage.as_ref().as_ptr().cast::<i32>()) }
604 }
605
606 /// Return a mutable reference to the value as an i32.
607 pub unsafe fn as_i32_mut(&mut self) -> &mut i32 {
608 unsafe { &mut *(self.storage.as_mut().as_mut_ptr().cast::<i32>()) }
609 }
610
611 /// Return a reference to the value as a u32.
612 pub unsafe fn as_u32(&self) -> &u32 {
613 unsafe { &*(self.storage.as_ref().as_ptr().cast::<u32>()) }
614 }
615
616 /// Return a mutable reference to the value as an u32.
617 pub unsafe fn as_u32_mut(&mut self) -> &mut u32 {
618 unsafe { &mut *(self.storage.as_mut().as_mut_ptr().cast::<u32>()) }
619 }
620
621 /// Return a reference to the value as an i64.
622 pub unsafe fn as_i64(&self) -> &i64 {
623 unsafe { &*(self.storage.as_ref().as_ptr().cast::<i64>()) }
624 }
625
626 /// Return a mutable reference to the value as an i64.
627 pub unsafe fn as_i64_mut(&mut self) -> &mut i64 {
628 unsafe { &mut *(self.storage.as_mut().as_mut_ptr().cast::<i64>()) }
629 }
630
631 /// Return a reference to the value as an u64.
632 pub unsafe fn as_u64(&self) -> &u64 {
633 unsafe { &*(self.storage.as_ref().as_ptr().cast::<u64>()) }
634 }
635
636 /// Return a mutable reference to the value as an u64.
637 pub unsafe fn as_u64_mut(&mut self) -> &mut u64 {
638 unsafe { &mut *(self.storage.as_mut().as_mut_ptr().cast::<u64>()) }
639 }
640
641 /// Return a reference to the value as an f32.
642 pub unsafe fn as_f32(&self) -> &f32 {
643 unsafe { &*(self.storage.as_ref().as_ptr().cast::<f32>()) }
644 }
645
646 /// Return a mutable reference to the value as an f32.
647 pub unsafe fn as_f32_mut(&mut self) -> &mut f32 {
648 unsafe { &mut *(self.storage.as_mut().as_mut_ptr().cast::<f32>()) }
649 }
650
651 /// Return a reference to the value as f32 bits.
652 pub unsafe fn as_f32_bits(&self) -> &u32 {
653 unsafe { &*(self.storage.as_ref().as_ptr().cast::<u32>()) }
654 }
655
656 /// Return a mutable reference to the value as f32 bits.
657 pub unsafe fn as_f32_bits_mut(&mut self) -> &mut u32 {
658 unsafe { &mut *(self.storage.as_mut().as_mut_ptr().cast::<u32>()) }
659 }
660
661 /// Return a reference to the value as an f64.
662 pub unsafe fn as_f64(&self) -> &f64 {
663 unsafe { &*(self.storage.as_ref().as_ptr().cast::<f64>()) }
664 }
665
666 /// Return a mutable reference to the value as an f64.
667 pub unsafe fn as_f64_mut(&mut self) -> &mut f64 {
668 unsafe { &mut *(self.storage.as_mut().as_mut_ptr().cast::<f64>()) }
669 }
670
671 /// Return a reference to the value as f64 bits.
672 pub unsafe fn as_f64_bits(&self) -> &u64 {
673 unsafe { &*(self.storage.as_ref().as_ptr().cast::<u64>()) }
674 }
675
676 /// Return a mutable reference to the value as f64 bits.
677 pub unsafe fn as_f64_bits_mut(&mut self) -> &mut u64 {
678 unsafe { &mut *(self.storage.as_mut().as_mut_ptr().cast::<u64>()) }
679 }
680
681 /// Gets the underlying 128-bit vector value.
682 //
683 // Note that vectors are stored in little-endian format while other types
684 // are stored in native-endian format.
685 pub unsafe fn get_u128(&self) -> u128 {
686 unsafe { u128::from_le(*(self.storage.as_ref().as_ptr().cast::<u128>())) }
687 }
688
689 /// Sets the 128-bit vector values.
690 //
691 // Note that vectors are stored in little-endian format while other types
692 // are stored in native-endian format.
693 pub unsafe fn set_u128(&mut self, val: u128) {
694 unsafe {
695 *self.storage.as_mut().as_mut_ptr().cast::<u128>() = val.to_le();
696 }
697 }
698
699 /// Return a reference to the value as u128 bits.
700 pub unsafe fn as_u128_bits(&self) -> &[u8; 16] {
701 unsafe { &*(self.storage.as_ref().as_ptr().cast::<[u8; 16]>()) }
702 }
703
704 /// Return a mutable reference to the value as u128 bits.
705 pub unsafe fn as_u128_bits_mut(&mut self) -> &mut [u8; 16] {
706 unsafe { &mut *(self.storage.as_mut().as_mut_ptr().cast::<[u8; 16]>()) }
707 }
708
709 /// Return a reference to the global value as a borrowed GC reference.
710 pub unsafe fn as_gc_ref(&self) -> Option<&VMGcRef> {
711 let raw_ptr = self.storage.as_ref().as_ptr().cast::<Option<VMGcRef>>();
712 let ret = unsafe { (*raw_ptr).as_ref() };
713 assert!(cfg!(feature = "gc") || ret.is_none());
714 ret
715 }
716
717 /// Return a reference to the global value as a borrowed GC reference.
718 pub unsafe fn as_gc_ref_mut(&mut self) -> Option<&mut VMGcRef> {
719 let raw_ptr = self.storage.as_mut().as_mut_ptr().cast::<Option<VMGcRef>>();
720 let ret = unsafe { (*raw_ptr).as_mut() };
721 assert!(cfg!(feature = "gc") || ret.is_none());
722 ret
723 }
724
725 /// Initialize a global to the given GC reference.
726 pub unsafe fn init_gc_ref(
727 &mut self,
728 store: &mut StoreOpaque,
729 gc_ref: Option<&VMGcRef>,
730 ) -> Result<()> {
731 let dest = unsafe {
732 &mut *(self
733 .storage
734 .as_mut()
735 .as_mut_ptr()
736 .cast::<MaybeUninit<Option<VMGcRef>>>())
737 };
738
739 store.init_gc_ref(dest, gc_ref)
740 }
741
742 /// Write a GC reference into this global value.
743 pub unsafe fn write_gc_ref(
744 &mut self,
745 store: &mut StoreOpaque,
746 gc_ref: Option<&VMGcRef>,
747 ) -> Result<()> {
748 let dest = unsafe { &mut *(self.storage.as_mut().as_mut_ptr().cast::<Option<VMGcRef>>()) };
749 store.write_gc_ref(dest, gc_ref)
750 }
751
752 /// Return a reference to the value as a `VMFuncRef`.
753 pub unsafe fn as_func_ref(&self) -> *mut VMFuncRef {
754 unsafe { *(self.storage.as_ref().as_ptr().cast::<*mut VMFuncRef>()) }
755 }
756
757 /// Return a mutable reference to the value as a `VMFuncRef`.
758 pub unsafe fn as_func_ref_mut(&mut self) -> &mut *mut VMFuncRef {
759 unsafe { &mut *(self.storage.as_mut().as_mut_ptr().cast::<*mut VMFuncRef>()) }
760 }
761}
762
763#[cfg(test)]
764mod test_vmshared_type_index {
765 use super::VMSharedTypeIndex;
766 use std::mem::size_of;
767 use wasmtime_environ::{HostPtr, Module, StaticModuleIndex, VMOffsets};
768
769 #[test]
770 fn check_vmshared_type_index() {
771 let module = Module::new(StaticModuleIndex::from_u32(0));
772 let offsets = VMOffsets::new(HostPtr, &module);
773 assert_eq!(
774 size_of::<VMSharedTypeIndex>(),
775 usize::from(offsets.size_of_vmshared_type_index())
776 );
777 }
778}
779
780/// A WebAssembly tag defined within the instance.
781///
782#[derive(Debug)]
783#[repr(C)]
784pub struct VMTagDefinition {
785 /// Function signature's type id.
786 pub type_index: VMSharedTypeIndex,
787}
788
789impl VMTagDefinition {
790 pub fn new(type_index: VMSharedTypeIndex) -> Self {
791 Self { type_index }
792 }
793}
794
795// SAFETY: the above structure is repr(C) and only contains VmSafe
796// fields.
797unsafe impl VmSafe for VMTagDefinition {}
798
799#[cfg(test)]
800mod test_vmtag_definition {
801 use super::VMTagDefinition;
802 use std::mem::size_of;
803 use wasmtime_environ::{HostPtr, Module, PtrSize, StaticModuleIndex, VMOffsets};
804
805 #[test]
806 fn check_vmtag_definition_offsets() {
807 let module = Module::new(StaticModuleIndex::from_u32(0));
808 let offsets = VMOffsets::new(HostPtr, &module);
809 assert_eq!(
810 size_of::<VMTagDefinition>(),
811 usize::from(offsets.ptr.size_of_vmtag_definition())
812 );
813 }
814
815 #[test]
816 fn check_vmtag_begins_aligned() {
817 let module = Module::new(StaticModuleIndex::from_u32(0));
818 let offsets = VMOffsets::new(HostPtr, &module);
819 assert_eq!(offsets.vmctx_tags_begin() % 16, 0);
820 }
821}
822
823/// The VM caller-checked "funcref" record, for caller-side signature checking.
824///
825/// It consists of function pointer(s), a type id to be checked by the
826/// caller, and the vmctx closure associated with this function.
827#[derive(Debug, Clone)]
828#[repr(C)]
829pub struct VMFuncRef {
830 /// Function pointer for this funcref if being called via the "array"
831 /// calling convention that `Func::new` et al use.
832 pub array_call: VmPtr<VMArrayCallFunction>,
833
834 /// Function pointer for this funcref if being called via the calling
835 /// convention we use when compiling Wasm.
836 ///
837 /// Most functions come with a function pointer that we can use when they
838 /// are called from Wasm. The notable exception is when we `Func::wrap` a
839 /// host function, and we don't have a Wasm compiler on hand to compile a
840 /// Wasm-to-native trampoline for the function. In this case, we leave
841 /// `wasm_call` empty until the function is passed as an import to Wasm (or
842 /// otherwise exposed to Wasm via tables/globals). At this point, we look up
843 /// a Wasm-to-native trampoline for the function in the Wasm's compiled
844 /// module and use that fill in `VMFunctionImport::wasm_call`. **However**
845 /// there is no guarantee that the Wasm module has a trampoline for this
846 /// function's signature. The Wasm module only has trampolines for its
847 /// types, and if this function isn't of one of those types, then the Wasm
848 /// module will not have a trampoline for it. This is actually okay, because
849 /// it means that the Wasm cannot actually call this function. But it does
850 /// mean that this field needs to be an `Option` even though it is non-null
851 /// the vast vast vast majority of the time.
852 pub wasm_call: Option<VmPtr<VMWasmCallFunction>>,
853
854 /// Function signature's type id.
855 pub type_index: VMSharedTypeIndex,
856
857 /// The VM state associated with this function.
858 ///
859 /// The actual definition of what this pointer points to depends on the
860 /// function being referenced: for core Wasm functions, this is a `*mut
861 /// VMContext`, for host functions it is a `*mut VMHostFuncContext`, and for
862 /// component functions it is a `*mut VMComponentContext`.
863 pub vmctx: VmPtr<VMOpaqueContext>,
864 // If more elements are added here, remember to add offset_of tests below!
865}
866
867// SAFETY: the above structure is repr(C) and only contains `VmSafe` fields.
868unsafe impl VmSafe for VMFuncRef {}
869
870impl VMFuncRef {
871 /// Invokes the `array_call` field of this `VMFuncRef` with the supplied
872 /// arguments.
873 ///
874 /// This will invoke the function pointer in the `array_call` field with:
875 ///
876 /// * the `callee` vmctx as `self.vmctx`
877 /// * the `caller` as `caller` specified here
878 /// * the args pointer as `args_and_results`
879 /// * the args length as `args_and_results`
880 ///
881 /// The `args_and_results` area must be large enough to both load all
882 /// arguments from and store all results to.
883 ///
884 /// Returns whether a trap was recorded in TLS for raising.
885 ///
886 /// # Unsafety
887 ///
888 /// This method is unsafe because it can be called with any pointers. They
889 /// must all be valid for this wasm function call to proceed. For example
890 /// the `caller` must be valid machine code if `pulley` is `None` or it must
891 /// be valid bytecode if `pulley` is `Some`. Additionally `args_and_results`
892 /// must be large enough to handle all the arguments/results for this call.
893 ///
894 /// Note that the unsafety invariants to maintain here are not currently
895 /// exhaustively documented.
896 #[inline]
897 pub unsafe fn array_call(
898 me: NonNull<VMFuncRef>,
899 pulley: Option<InterpreterRef<'_>>,
900 caller: NonNull<VMContext>,
901 args_and_results: NonNull<[ValRaw]>,
902 ) -> bool {
903 match pulley {
904 Some(vm) => unsafe { Self::array_call_interpreted(me, vm, caller, args_and_results) },
905 None => unsafe { Self::array_call_native(me, caller, args_and_results) },
906 }
907 }
908
909 unsafe fn array_call_interpreted(
910 me: NonNull<VMFuncRef>,
911 vm: InterpreterRef<'_>,
912 caller: NonNull<VMContext>,
913 args_and_results: NonNull<[ValRaw]>,
914 ) -> bool {
915 // If `caller` is actually a `VMArrayCallHostFuncContext` then skip the
916 // interpreter, even though it's available, as `array_call` will be
917 // native code.
918 unsafe {
919 if me.as_ref().vmctx.as_non_null().as_ref().magic
920 == wasmtime_environ::VM_ARRAY_CALL_HOST_FUNC_MAGIC
921 {
922 return Self::array_call_native(me, caller, args_and_results);
923 }
924 vm.call(
925 me.as_ref().array_call.as_non_null().cast(),
926 me.as_ref().vmctx.as_non_null(),
927 caller,
928 args_and_results,
929 )
930 }
931 }
932
933 #[inline]
934 unsafe fn array_call_native(
935 me: NonNull<VMFuncRef>,
936 caller: NonNull<VMContext>,
937 args_and_results: NonNull<[ValRaw]>,
938 ) -> bool {
939 unsafe {
940 union GetNativePointer {
941 native: VMArrayCallNative,
942 ptr: NonNull<VMArrayCallFunction>,
943 }
944 let native = GetNativePointer {
945 ptr: me.as_ref().array_call.as_non_null(),
946 }
947 .native;
948 native(
949 me.as_ref().vmctx.as_non_null(),
950 caller,
951 args_and_results.cast(),
952 args_and_results.len(),
953 )
954 }
955 }
956
957 pub(crate) fn as_vm_function_import(&self) -> Option<&VMFunctionImport> {
958 if self.wasm_call.is_some() {
959 // Safety: `VMFuncRef` and `VMFunctionImport` have the same layout
960 // and `wasm_call` is non-null.
961 Some(unsafe { NonNull::from(self).cast::<VMFunctionImport>().as_ref() })
962 } else {
963 None
964 }
965 }
966}
967
968#[cfg(test)]
969mod test_vm_func_ref {
970 use super::VMFuncRef;
971 use core::mem::offset_of;
972 use std::mem::size_of;
973 use wasmtime_environ::{HostPtr, Module, PtrSize, StaticModuleIndex, VMOffsets};
974
975 #[test]
976 fn check_vm_func_ref_offsets() {
977 let module = Module::new(StaticModuleIndex::from_u32(0));
978 let offsets = VMOffsets::new(HostPtr, &module);
979 assert_eq!(
980 size_of::<VMFuncRef>(),
981 usize::from(offsets.ptr.size_of_vm_func_ref())
982 );
983 assert_eq!(
984 offset_of!(VMFuncRef, array_call),
985 usize::from(offsets.ptr.vm_func_ref_array_call())
986 );
987 assert_eq!(
988 offset_of!(VMFuncRef, wasm_call),
989 usize::from(offsets.ptr.vm_func_ref_wasm_call())
990 );
991 assert_eq!(
992 offset_of!(VMFuncRef, type_index),
993 usize::from(offsets.ptr.vm_func_ref_type_index())
994 );
995 assert_eq!(
996 offset_of!(VMFuncRef, vmctx),
997 usize::from(offsets.ptr.vm_func_ref_vmctx())
998 );
999 }
1000}
1001
1002macro_rules! define_builtin_array {
1003 (
1004 $(
1005 $( #[$attr:meta] )*
1006 $name:ident( $( $pname:ident: $param:ident ),* ) $( -> $result:ident )?;
1007 )*
1008 ) => {
1009 /// An array that stores addresses of builtin functions. We translate code
1010 /// to use indirect calls. This way, we don't have to patch the code.
1011 #[repr(C)]
1012 #[allow(improper_ctypes_definitions, reason = "__m128i known not FFI-safe")]
1013 pub struct VMBuiltinFunctionsArray {
1014 $(
1015 $name: unsafe extern "C" fn(
1016 $(define_builtin_array!(@ty $param)),*
1017 ) $( -> define_builtin_array!(@ty $result))?,
1018 )*
1019 }
1020
1021 impl VMBuiltinFunctionsArray {
1022 pub const INIT: VMBuiltinFunctionsArray = VMBuiltinFunctionsArray {
1023 $(
1024 $name: crate::runtime::vm::libcalls::raw::$name,
1025 )*
1026 };
1027
1028 /// Helper to call `expose_provenance()` on all contained pointers.
1029 ///
1030 /// This is required to be called at least once before entering wasm
1031 /// to inform the compiler that these function pointers may all be
1032 /// loaded/stored and used on the "other end" to reacquire
1033 /// provenance in Pulley. Pulley models hostcalls with a host
1034 /// pointer as the first parameter that's a function pointer under
1035 /// the hood, and this call ensures that the use of the function
1036 /// pointer is considered valid.
1037 pub fn expose_provenance(&self) -> NonNull<Self>{
1038 $(
1039 (self.$name as *mut u8).expose_provenance();
1040 )*
1041 NonNull::from(self)
1042 }
1043 }
1044 };
1045
1046 (@ty u32) => (u32);
1047 (@ty u64) => (u64);
1048 (@ty f32) => (f32);
1049 (@ty f64) => (f64);
1050 (@ty u8) => (u8);
1051 (@ty i8x16) => (i8x16);
1052 (@ty f32x4) => (f32x4);
1053 (@ty f64x2) => (f64x2);
1054 (@ty bool) => (bool);
1055 (@ty pointer) => (*mut u8);
1056 (@ty size) => (usize);
1057 (@ty vmctx) => (NonNull<VMContext>);
1058}
1059
1060// SAFETY: the above structure is repr(C) and only contains `VmSafe` fields.
1061unsafe impl VmSafe for VMBuiltinFunctionsArray {}
1062
1063wasmtime_environ::foreach_builtin_function!(define_builtin_array);
1064
1065const _: () = {
1066 assert!(
1067 mem::size_of::<VMBuiltinFunctionsArray>()
1068 == mem::size_of::<usize>() * (BuiltinFunctionIndex::len() as usize)
1069 )
1070};
1071
1072/// Structure that holds all mutable context that is shared across all instances
1073/// in a store, for example data related to fuel or epochs.
1074///
1075/// `VMStoreContext`s are one-to-one with `wasmtime::Store`s, the same way that
1076/// `VMContext`s are one-to-one with `wasmtime::Instance`s. And the same way
1077/// that multiple `wasmtime::Instance`s may be associated with the same
1078/// `wasmtime::Store`, multiple `VMContext`s hold a pointer to the same
1079/// `VMStoreContext` when they are associated with the same `wasmtime::Store`.
1080#[derive(Debug)]
1081#[repr(C)]
1082pub struct VMStoreContext {
1083 // NB: 64-bit integer fields are located first with pointer-sized fields
1084 // trailing afterwards. That makes the offsets in this structure easier to
1085 // calculate on 32-bit platforms as we don't have to worry about the
1086 // alignment of 64-bit integers.
1087 //
1088 /// Indicator of how much fuel has been consumed and is remaining to
1089 /// WebAssembly.
1090 ///
1091 /// This field is typically negative and increments towards positive. Upon
1092 /// turning positive a wasm trap will be generated. This field is only
1093 /// modified if wasm is configured to consume fuel.
1094 pub fuel_consumed: UnsafeCell<i64>,
1095
1096 /// Deadline epoch for interruption: if epoch-based interruption
1097 /// is enabled and the global (per engine) epoch counter is
1098 /// observed to reach or exceed this value, the guest code will
1099 /// yield if running asynchronously.
1100 pub epoch_deadline: UnsafeCell<u64>,
1101
1102 /// The "store version".
1103 ///
1104 /// This is used to test whether stack-frame handles referring to
1105 /// suspended stack frames remain valid.
1106 ///
1107 /// The invariant that this upward-counting number must satisfy
1108 /// is: the number must be incremented whenever execution starts
1109 /// or resumes in the `Store` or when any stack is
1110 /// dropped/freed. That way, if we take a reference to some
1111 /// suspended stack frame and track the "version" at the time we
1112 /// took that reference, if the version still matches, we can be
1113 /// sure that nothing could have unwound the referenced Wasm
1114 /// frame.
1115 ///
1116 /// This version number is incremented in exactly one place: the
1117 /// Wasm-to-host trampolines, after return from host code. Note
1118 /// that this captures both the normal "return into Wasm" case
1119 /// (where Wasm frames can subsequently return normally and thus
1120 /// invalidate frames), and the "trap/exception unwinds Wasm
1121 /// frames" case, which is done internally via the `raise` libcall
1122 /// invoked after the main hostcall returns an error, and after we
1123 /// increment this version number.
1124 ///
1125 /// Note that this also handles the fiber/future-drop case because
1126 /// because we *always* return into the trampoline to clean up;
1127 /// that trampoline immediately raises an error and uses the
1128 /// longjmp-like unwind within Cranelift frames to skip over all
1129 /// the guest Wasm frames, but not before it increments the
1130 /// store's execution version number.
1131 ///
1132 /// This field is in use only if guest debugging is enabled.
1133 pub execution_version: u64,
1134
1135 /// Current stack limit of the wasm module.
1136 ///
1137 /// For more information see `crates/cranelift/src/lib.rs`.
1138 pub stack_limit: UnsafeCell<usize>,
1139
1140 /// The `VMMemoryDefinition` for this store's GC heap.
1141 pub gc_heap: UnsafeCell<VMMemoryDefinition>,
1142
1143 /// The value of the frame pointer register in the trampoline used
1144 /// to call from Wasm to the host.
1145 ///
1146 /// Maintained by our Wasm-to-host trampoline, and cleared just
1147 /// before calling into Wasm in `catch_traps`.
1148 ///
1149 /// This member is `0` when Wasm is actively running and has not called out
1150 /// to the host.
1151 ///
1152 /// Used to find the start of a contiguous sequence of Wasm frames
1153 /// when walking the stack. Note that we record the FP of the
1154 /// *trampoline*'s frame, not the last Wasm frame, because we need
1155 /// to know the SP (bottom of frame) of the last Wasm frame as
1156 /// well in case we need to resume to an exception handler in that
1157 /// frame. The FP of the last Wasm frame can be recovered by
1158 /// loading the saved FP value at this FP address.
1159 pub last_wasm_exit_trampoline_fp: UnsafeCell<usize>,
1160
1161 /// The last Wasm program counter before we called from Wasm to the host.
1162 ///
1163 /// Maintained by our Wasm-to-host trampoline, and cleared just before
1164 /// calling into Wasm in `catch_traps`.
1165 ///
1166 /// This member is `0` when Wasm is actively running and has not called out
1167 /// to the host.
1168 ///
1169 /// Used when walking a contiguous sequence of Wasm frames.
1170 pub last_wasm_exit_pc: UnsafeCell<usize>,
1171
1172 /// The last host stack pointer before we called into Wasm from the host.
1173 ///
1174 /// Maintained by our host-to-Wasm trampoline. This member is `0` when Wasm
1175 /// is not running, and it's set to nonzero once a host-to-wasm trampoline
1176 /// is executed.
1177 ///
1178 /// When a host function is wrapped into a `wasmtime::Func`, and is then
1179 /// called from the host, then this member is not changed meaning that the
1180 /// previous activation in pointed to by `last_wasm_exit_trampoline_fp` is
1181 /// still the last wasm set of frames on the stack.
1182 ///
1183 /// This field is saved/restored during fiber suspension/resumption
1184 /// resumption as part of `CallThreadState::swap`.
1185 ///
1186 /// This field is used to find the end of a contiguous sequence of Wasm
1187 /// frames when walking the stack. Additionally it's used when a trap is
1188 /// raised as part of the set of parameters used to resume in the entry
1189 /// trampoline's "catch" block.
1190 pub last_wasm_entry_sp: UnsafeCell<usize>,
1191
1192 /// Same as `last_wasm_entry_sp`, but for the `fp` of the trampoline.
1193 pub last_wasm_entry_fp: UnsafeCell<usize>,
1194
1195 /// The last trap handler from a host-to-wasm entry trampoline on the stack.
1196 ///
1197 /// This field is configured when the host calls into wasm by the trampoline
1198 /// itself. It stores the `pc` of an exception handler suitable to handle
1199 /// all traps (or uncaught exceptions).
1200 pub last_wasm_entry_trap_handler: UnsafeCell<usize>,
1201
1202 /// Stack information used by stack switching instructions. See documentation
1203 /// on `VMStackChain` for details.
1204 pub stack_chain: UnsafeCell<VMStackChain>,
1205
1206 /// A pointer to the embedder's `T` inside a `Store<T>`, for use with the
1207 /// `store-data-address` unsafe intrinsic.
1208 pub store_data: VmPtr<()>,
1209
1210 /// The range, in addresses, of the guard page that is currently in use.
1211 ///
1212 /// This field is used when signal handlers are run to determine whether a
1213 /// faulting address lies within the guard page of an async stack for
1214 /// example. If this happens then the signal handler aborts with a stack
1215 /// overflow message similar to what would happen had the stack overflow
1216 /// happened on the main thread. This field is, by default a null..null
1217 /// range indicating that no async guard is in use (aka no fiber). In such a
1218 /// situation while this field is read it'll never classify a fault as an
1219 /// guard page fault.
1220 pub async_guard_range: Range<*mut u8>,
1221
1222 /// The `context.{get,set}` values for the current thread in the component
1223 /// model. This is only used for `component-model-async` and slot[1] is only
1224 /// used for `component-model-threading`. Despite the conditional use nature
1225 /// this is unconditionally present as it avoids the need to make logic in
1226 /// `VMOffsets` conditional.
1227 ///
1228 /// This is saved/restored when threads are swapped in the component model.
1229 pub component_context: [u32; NUM_COMPONENT_CONTEXT_SLOTS],
1230}
1231
1232impl VMStoreContext {
1233 /// From the current saved trampoline FP, get the FP of the last
1234 /// Wasm frame. If the current saved trampoline FP is null, return
1235 /// null.
1236 ///
1237 /// We store only the trampoline FP, because (i) we need the
1238 /// trampoline FP, so we know the size (bottom) of the last Wasm
1239 /// frame; and (ii) the last Wasm frame, just above the trampoline
1240 /// frame, can be recovered via the FP chain.
1241 ///
1242 /// # Safety
1243 ///
1244 /// This function requires that the `last_wasm_exit_trampoline_fp`
1245 /// field either points to an active trampoline frame or is a null
1246 /// pointer.
1247 pub(crate) unsafe fn last_wasm_exit_fp(&self) -> usize {
1248 // SAFETY: the unsafe cell is safe to load (no other threads
1249 // will be writing our store when we have control), and the
1250 // helper function's safety condition is the same as ours.
1251 unsafe {
1252 let trampoline_fp = *self.last_wasm_exit_trampoline_fp.get();
1253 Self::wasm_exit_fp_from_trampoline_fp(trampoline_fp)
1254 }
1255 }
1256
1257 /// From any saved trampoline FP, get the FP of the last Wasm
1258 /// frame. If the given trampoline FP is null, return null.
1259 ///
1260 /// This differs from `last_wasm_exit_fp()` above in that it
1261 /// allows accessing activations further up the stack as well,
1262 /// e.g. via `CallThreadState::old_state`.
1263 ///
1264 /// # Safety
1265 ///
1266 /// This function requires that the provided FP value is valid,
1267 /// and points to an active trampoline frame, or is null.
1268 ///
1269 /// This function depends on the invariant that on all supported
1270 /// architectures, we store the previous FP value under the
1271 /// current FP. This is a property of our ABI that we control and
1272 /// ensure.
1273 pub(crate) unsafe fn wasm_exit_fp_from_trampoline_fp(trampoline_fp: usize) -> usize {
1274 if trampoline_fp != 0 {
1275 // SAFETY: We require that trampoline_fp points to a valid
1276 // frame, which will (by definition) contain an old FP value
1277 // that we can load.
1278 unsafe { *(trampoline_fp as *const usize) }
1279 } else {
1280 0
1281 }
1282 }
1283}
1284
1285// The `VMStoreContext` type is a pod-type with no destructor, and we don't
1286// access any fields from other threads, so add in these trait impls which are
1287// otherwise not available due to the `fuel_consumed` and `epoch_deadline`
1288// variables in `VMStoreContext`.
1289unsafe impl Send for VMStoreContext {}
1290unsafe impl Sync for VMStoreContext {}
1291
1292// SAFETY: the above structure is repr(C) and only contains `VmSafe` fields.
1293unsafe impl VmSafe for VMStoreContext {}
1294
1295impl Default for VMStoreContext {
1296 fn default() -> VMStoreContext {
1297 VMStoreContext {
1298 fuel_consumed: UnsafeCell::new(0),
1299 epoch_deadline: UnsafeCell::new(0),
1300 execution_version: 0,
1301 stack_limit: UnsafeCell::new(usize::max_value()),
1302 gc_heap: UnsafeCell::new(VMMemoryDefinition {
1303 base: NonNull::dangling().into(),
1304 current_length: AtomicUsize::new(0),
1305 }),
1306 last_wasm_exit_trampoline_fp: UnsafeCell::new(0),
1307 last_wasm_exit_pc: UnsafeCell::new(0),
1308 last_wasm_entry_fp: UnsafeCell::new(0),
1309 last_wasm_entry_sp: UnsafeCell::new(0),
1310 last_wasm_entry_trap_handler: UnsafeCell::new(0),
1311 stack_chain: UnsafeCell::new(VMStackChain::Absent),
1312 async_guard_range: ptr::null_mut()..ptr::null_mut(),
1313 store_data: VmPtr::dangling(),
1314 component_context: [0; NUM_COMPONENT_CONTEXT_SLOTS],
1315 }
1316 }
1317}
1318
1319#[cfg(test)]
1320mod test_vmstore_context {
1321 use super::{VMMemoryDefinition, VMStoreContext};
1322 use core::mem::offset_of;
1323 use wasmtime_environ::{HostPtr, Module, PtrSize, StaticModuleIndex, VMOffsets};
1324
1325 #[test]
1326 fn field_offsets() {
1327 let module = Module::new(StaticModuleIndex::from_u32(0));
1328 let offsets = VMOffsets::new(HostPtr, &module);
1329 assert_eq!(
1330 offset_of!(VMStoreContext, stack_limit),
1331 usize::from(offsets.ptr.vmstore_context_stack_limit())
1332 );
1333 assert_eq!(
1334 offset_of!(VMStoreContext, fuel_consumed),
1335 usize::from(offsets.ptr.vmstore_context_fuel_consumed())
1336 );
1337 assert_eq!(
1338 offset_of!(VMStoreContext, epoch_deadline),
1339 usize::from(offsets.ptr.vmstore_context_epoch_deadline())
1340 );
1341 assert_eq!(
1342 offset_of!(VMStoreContext, execution_version),
1343 usize::from(offsets.ptr.vmstore_context_execution_version())
1344 );
1345 assert_eq!(
1346 offset_of!(VMStoreContext, gc_heap),
1347 usize::from(offsets.ptr.vmstore_context_gc_heap())
1348 );
1349 assert_eq!(
1350 offset_of!(VMStoreContext, gc_heap) + offset_of!(VMMemoryDefinition, base),
1351 usize::from(offsets.ptr.vmstore_context_gc_heap_base())
1352 );
1353 assert_eq!(
1354 offset_of!(VMStoreContext, gc_heap) + offset_of!(VMMemoryDefinition, current_length),
1355 usize::from(offsets.ptr.vmstore_context_gc_heap_current_length())
1356 );
1357 assert_eq!(
1358 offset_of!(VMStoreContext, last_wasm_exit_trampoline_fp),
1359 usize::from(offsets.ptr.vmstore_context_last_wasm_exit_trampoline_fp())
1360 );
1361 assert_eq!(
1362 offset_of!(VMStoreContext, last_wasm_exit_pc),
1363 usize::from(offsets.ptr.vmstore_context_last_wasm_exit_pc())
1364 );
1365 assert_eq!(
1366 offset_of!(VMStoreContext, last_wasm_entry_fp),
1367 usize::from(offsets.ptr.vmstore_context_last_wasm_entry_fp())
1368 );
1369 assert_eq!(
1370 offset_of!(VMStoreContext, last_wasm_entry_sp),
1371 usize::from(offsets.ptr.vmstore_context_last_wasm_entry_sp())
1372 );
1373 assert_eq!(
1374 offset_of!(VMStoreContext, last_wasm_entry_trap_handler),
1375 usize::from(offsets.ptr.vmstore_context_last_wasm_entry_trap_handler())
1376 );
1377 assert_eq!(
1378 offset_of!(VMStoreContext, stack_chain),
1379 usize::from(offsets.ptr.vmstore_context_stack_chain())
1380 );
1381 assert_eq!(
1382 offset_of!(VMStoreContext, store_data),
1383 usize::from(offsets.ptr.vmstore_context_store_data())
1384 );
1385 assert_eq!(
1386 offset_of!(VMStoreContext, component_context),
1387 usize::from(offsets.ptr.vmstore_context_component_context_slot(0))
1388 );
1389
1390 // Make sure that the calculation for the size of a slot is also
1391 // accurate.
1392 let slot_width = offsets.ptr.vmstore_context_component_context_slot(1)
1393 - offsets.ptr.vmstore_context_component_context_slot(0);
1394 let default = VMStoreContext::default();
1395 assert_eq!(
1396 size_of_val(&default.component_context[0]),
1397 usize::from(slot_width)
1398 );
1399 }
1400}
1401
1402/// The VM "context", which is pointed to by the `vmctx` arg in Cranelift.
1403/// This has information about globals, memories, tables, and other runtime
1404/// state associated with the current instance.
1405///
1406/// The struct here is empty, as the sizes of these fields are dynamic, and
1407/// we can't describe them in Rust's type system. Sufficient memory is
1408/// allocated at runtime.
1409#[derive(Debug)]
1410#[repr(C, align(16))] // align 16 since globals are aligned to that and contained inside
1411pub struct VMContext {
1412 _magic: u32,
1413}
1414
1415impl VMContext {
1416 /// Helper function to cast between context types using a debug assertion to
1417 /// protect against some mistakes.
1418 #[inline]
1419 pub unsafe fn from_opaque(opaque: NonNull<VMOpaqueContext>) -> NonNull<VMContext> {
1420 // Note that in general the offset of the "magic" field is stored in
1421 // `VMOffsets::vmctx_magic`. Given though that this is a sanity check
1422 // about converting this pointer to another type we ideally don't want
1423 // to read the offset from potentially corrupt memory. Instead it would
1424 // be better to catch errors here as soon as possible.
1425 //
1426 // To accomplish this the `VMContext` structure is laid out with the
1427 // magic field at a statically known offset (here it's 0 for now). This
1428 // static offset is asserted in `VMOffsets::from` and needs to be kept
1429 // in sync with this line for this debug assertion to work.
1430 //
1431 // Also note that this magic is only ever invalid in the presence of
1432 // bugs, meaning we don't actually read the magic and act differently
1433 // at runtime depending what it is, so this is a debug assertion as
1434 // opposed to a regular assertion.
1435 unsafe {
1436 debug_assert_eq!(opaque.as_ref().magic, VMCONTEXT_MAGIC);
1437 }
1438 opaque.cast()
1439 }
1440}
1441
1442/// A "raw" and unsafe representation of a WebAssembly value.
1443///
1444/// This is provided for use with the `Func::new_unchecked` and
1445/// `Func::call_unchecked` APIs. In general it's unlikely you should be using
1446/// this from Rust, rather using APIs like `Func::wrap` and `TypedFunc::call`.
1447///
1448/// This is notably an "unsafe" way to work with `Val` and it's recommended to
1449/// instead use `Val` where possible. An important note about this union is that
1450/// fields are all stored in little-endian format, regardless of the endianness
1451/// of the host system.
1452#[repr(C)]
1453#[derive(Copy, Clone)]
1454pub union ValRaw {
1455 /// A WebAssembly `i32` value.
1456 ///
1457 /// Note that the payload here is a Rust `i32` but the WebAssembly `i32`
1458 /// type does not assign an interpretation of the upper bit as either signed
1459 /// or unsigned. The Rust type `i32` is simply chosen for convenience.
1460 ///
1461 /// This value is always stored in a little-endian format.
1462 i32: i32,
1463
1464 /// A WebAssembly `i64` value.
1465 ///
1466 /// Note that the payload here is a Rust `i64` but the WebAssembly `i64`
1467 /// type does not assign an interpretation of the upper bit as either signed
1468 /// or unsigned. The Rust type `i64` is simply chosen for convenience.
1469 ///
1470 /// This value is always stored in a little-endian format.
1471 i64: i64,
1472
1473 /// A WebAssembly `f32` value.
1474 ///
1475 /// Note that the payload here is a Rust `u32`. This is to allow passing any
1476 /// representation of NaN into WebAssembly without risk of changing NaN
1477 /// payload bits as its gets passed around the system. Otherwise though this
1478 /// `u32` value is the return value of `f32::to_bits` in Rust.
1479 ///
1480 /// This value is always stored in a little-endian format.
1481 f32: u32,
1482
1483 /// A WebAssembly `f64` value.
1484 ///
1485 /// Note that the payload here is a Rust `u64`. This is to allow passing any
1486 /// representation of NaN into WebAssembly without risk of changing NaN
1487 /// payload bits as its gets passed around the system. Otherwise though this
1488 /// `u64` value is the return value of `f64::to_bits` in Rust.
1489 ///
1490 /// This value is always stored in a little-endian format.
1491 f64: u64,
1492
1493 /// A WebAssembly `v128` value.
1494 ///
1495 /// The payload here is a Rust `[u8; 16]` which has the same number of bits
1496 /// but note that `v128` in WebAssembly is often considered a vector type
1497 /// such as `i32x4` or `f64x2`. This means that the actual interpretation
1498 /// of the underlying bits is left up to the instructions which consume
1499 /// this value.
1500 ///
1501 /// This value is always stored in a little-endian format.
1502 v128: [u8; 16],
1503
1504 /// A WebAssembly `funcref` value (or one of its subtypes).
1505 ///
1506 /// The payload here is a pointer which is runtime-defined. This is one of
1507 /// the main points of unsafety about the `ValRaw` type as the validity of
1508 /// the pointer here is not easily verified and must be preserved by
1509 /// carefully calling the correct functions throughout the runtime.
1510 ///
1511 /// This value is always stored in a little-endian format.
1512 funcref: *mut c_void,
1513
1514 /// A WebAssembly `externref` value (or one of its subtypes).
1515 ///
1516 /// The payload here is a compressed pointer value which is
1517 /// runtime-defined. This is one of the main points of unsafety about the
1518 /// `ValRaw` type as the validity of the pointer here is not easily verified
1519 /// and must be preserved by carefully calling the correct functions
1520 /// throughout the runtime.
1521 ///
1522 /// This value is always stored in a little-endian format.
1523 externref: u32,
1524
1525 /// A WebAssembly `anyref` value (or one of its subtypes).
1526 ///
1527 /// The payload here is a compressed pointer value which is
1528 /// runtime-defined. This is one of the main points of unsafety about the
1529 /// `ValRaw` type as the validity of the pointer here is not easily verified
1530 /// and must be preserved by carefully calling the correct functions
1531 /// throughout the runtime.
1532 ///
1533 /// This value is always stored in a little-endian format.
1534 anyref: u32,
1535
1536 /// A WebAssembly `exnref` value (or one of its subtypes).
1537 ///
1538 /// The payload here is a compressed pointer value which is
1539 /// runtime-defined. This is one of the main points of unsafety about the
1540 /// `ValRaw` type as the validity of the pointer here is not easily verified
1541 /// and must be preserved by carefully calling the correct functions
1542 /// throughout the runtime.
1543 ///
1544 /// This value is always stored in a little-endian format.
1545 exnref: u32,
1546}
1547
1548// The `ValRaw` type is matched as `wasmtime_val_raw_t` in the C API so these
1549// are some simple assertions about the shape of the type which are additionally
1550// matched in C.
1551const _: () = {
1552 assert!(mem::size_of::<ValRaw>() == 16);
1553 assert!(mem::align_of::<ValRaw>() == mem::align_of::<u64>());
1554};
1555
1556// This type is just a bag-of-bits so it's up to the caller to figure out how
1557// to safely deal with threading concerns and safely access interior bits.
1558unsafe impl Send for ValRaw {}
1559unsafe impl Sync for ValRaw {}
1560
1561impl fmt::Debug for ValRaw {
1562 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1563 struct Hex<T>(T);
1564 impl<T: fmt::LowerHex> fmt::Debug for Hex<T> {
1565 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1566 let bytes = mem::size_of::<T>();
1567 let hex_digits_per_byte = 2;
1568 let hex_digits = bytes * hex_digits_per_byte;
1569 write!(f, "0x{:0width$x}", self.0, width = hex_digits)
1570 }
1571 }
1572
1573 unsafe {
1574 f.debug_struct("ValRaw")
1575 .field("i32", &Hex(self.i32))
1576 .field("i64", &Hex(self.i64))
1577 .field("f32", &Hex(self.f32))
1578 .field("f64", &Hex(self.f64))
1579 .field("v128", &Hex(u128::from_le_bytes(self.v128)))
1580 .field("funcref", &self.funcref)
1581 .field("externref", &Hex(self.externref))
1582 .field("anyref", &Hex(self.anyref))
1583 .field("exnref", &Hex(self.exnref))
1584 .finish()
1585 }
1586 }
1587}
1588
1589impl ValRaw {
1590 /// Create a null reference that is compatible with any of
1591 /// `{any,extern,func,exn}ref`.
1592 pub fn null() -> ValRaw {
1593 unsafe {
1594 let raw = mem::MaybeUninit::<Self>::zeroed().assume_init();
1595 debug_assert_eq!(raw.get_anyref(), 0);
1596 debug_assert_eq!(raw.get_exnref(), 0);
1597 debug_assert_eq!(raw.get_externref(), 0);
1598 debug_assert_eq!(raw.get_funcref(), ptr::null_mut());
1599 raw
1600 }
1601 }
1602
1603 /// Creates a WebAssembly `i32` value
1604 #[inline]
1605 pub fn i32(i: i32) -> ValRaw {
1606 // Note that this is intentionally not setting the `i32` field, instead
1607 // setting the `i64` field with a zero-extended version of `i`. For more
1608 // information on this see the comments on `Lower for Result` in the
1609 // `wasmtime` crate. Otherwise though all `ValRaw` constructors are
1610 // otherwise constrained to guarantee that the initial 64-bits are
1611 // always initialized.
1612 ValRaw::u64(i.cast_unsigned().into())
1613 }
1614
1615 /// Creates a WebAssembly `i64` value
1616 #[inline]
1617 pub fn i64(i: i64) -> ValRaw {
1618 ValRaw { i64: i.to_le() }
1619 }
1620
1621 /// Creates a WebAssembly `i32` value
1622 #[inline]
1623 pub fn u32(i: u32) -> ValRaw {
1624 // See comments in `ValRaw::i32` for why this is setting the upper
1625 // 32-bits as well.
1626 ValRaw::u64(i.into())
1627 }
1628
1629 /// Creates a WebAssembly `i64` value
1630 #[inline]
1631 pub fn u64(i: u64) -> ValRaw {
1632 ValRaw::i64(i as i64)
1633 }
1634
1635 /// Creates a WebAssembly `f32` value
1636 #[inline]
1637 pub fn f32(i: u32) -> ValRaw {
1638 // See comments in `ValRaw::i32` for why this is setting the upper
1639 // 32-bits as well.
1640 ValRaw::u64(i.into())
1641 }
1642
1643 /// Creates a WebAssembly `f64` value
1644 #[inline]
1645 pub fn f64(i: u64) -> ValRaw {
1646 ValRaw { f64: i.to_le() }
1647 }
1648
1649 /// Creates a WebAssembly `v128` value
1650 #[inline]
1651 pub fn v128(i: u128) -> ValRaw {
1652 ValRaw {
1653 v128: i.to_le_bytes(),
1654 }
1655 }
1656
1657 /// Creates a WebAssembly `funcref` value
1658 #[inline]
1659 pub fn funcref(i: *mut c_void) -> ValRaw {
1660 ValRaw {
1661 funcref: i.map_addr(|i| i.to_le()),
1662 }
1663 }
1664
1665 /// Creates a WebAssembly `externref` value
1666 #[inline]
1667 pub fn externref(e: u32) -> ValRaw {
1668 assert!(cfg!(feature = "gc") || e == 0);
1669 ValRaw {
1670 externref: e.to_le(),
1671 }
1672 }
1673
1674 /// Creates a WebAssembly `anyref` value
1675 #[inline]
1676 pub fn anyref(r: u32) -> ValRaw {
1677 assert!(cfg!(feature = "gc") || r == 0);
1678 ValRaw { anyref: r.to_le() }
1679 }
1680
1681 /// Creates a WebAssembly `exnref` value
1682 #[inline]
1683 pub fn exnref(r: u32) -> ValRaw {
1684 assert!(cfg!(feature = "gc") || r == 0);
1685 ValRaw { exnref: r.to_le() }
1686 }
1687
1688 #[inline]
1689 pub(crate) fn vmgcref(r: Option<VMGcRef>) -> ValRaw {
1690 let raw = r.map_or(0, |r| r.as_raw_u32());
1691
1692 // NB: All `VMGcRef`-based `ValRaw`s are the same.
1693 debug_assert_eq!(raw, ValRaw::anyref(raw).get_exnref());
1694 debug_assert_eq!(raw, ValRaw::exnref(raw).get_externref());
1695 debug_assert_eq!(raw, ValRaw::externref(raw).get_anyref());
1696
1697 ValRaw::anyref(raw)
1698 }
1699
1700 /// Gets the WebAssembly `i32` value
1701 #[inline]
1702 pub fn get_i32(&self) -> i32 {
1703 unsafe { i32::from_le(self.i32) }
1704 }
1705
1706 /// Gets the WebAssembly `i64` value
1707 #[inline]
1708 pub fn get_i64(&self) -> i64 {
1709 unsafe { i64::from_le(self.i64) }
1710 }
1711
1712 /// Gets the WebAssembly `i32` value
1713 #[inline]
1714 pub fn get_u32(&self) -> u32 {
1715 self.get_i32().cast_unsigned()
1716 }
1717
1718 /// Gets the WebAssembly `i64` value
1719 #[inline]
1720 pub fn get_u64(&self) -> u64 {
1721 self.get_i64().cast_unsigned()
1722 }
1723
1724 /// Gets the WebAssembly `f32` value
1725 #[inline]
1726 pub fn get_f32(&self) -> u32 {
1727 unsafe { u32::from_le(self.f32) }
1728 }
1729
1730 /// Gets the WebAssembly `f64` value
1731 #[inline]
1732 pub fn get_f64(&self) -> u64 {
1733 unsafe { u64::from_le(self.f64) }
1734 }
1735
1736 /// Gets the WebAssembly `v128` value
1737 #[inline]
1738 pub fn get_v128(&self) -> u128 {
1739 unsafe { u128::from_le_bytes(self.v128) }
1740 }
1741
1742 /// Gets the WebAssembly `funcref` value
1743 #[inline]
1744 pub fn get_funcref(&self) -> *mut c_void {
1745 let addr = unsafe { usize::from_le(self.funcref.addr()) };
1746 core::ptr::with_exposed_provenance_mut(addr)
1747 }
1748
1749 /// Gets the WebAssembly `externref` value
1750 #[inline]
1751 pub fn get_externref(&self) -> u32 {
1752 let externref = u32::from_le(unsafe { self.externref });
1753 assert!(cfg!(feature = "gc") || externref == 0);
1754 externref
1755 }
1756
1757 /// Gets the WebAssembly `anyref` value
1758 #[inline]
1759 pub fn get_anyref(&self) -> u32 {
1760 let anyref = u32::from_le(unsafe { self.anyref });
1761 assert!(cfg!(feature = "gc") || anyref == 0);
1762 anyref
1763 }
1764
1765 /// Gets the WebAssembly `exnref` value
1766 #[inline]
1767 pub fn get_exnref(&self) -> u32 {
1768 let exnref = u32::from_le(unsafe { self.exnref });
1769 assert!(cfg!(feature = "gc") || exnref == 0);
1770 exnref
1771 }
1772
1773 /// Get the inner `VMGcRef`.
1774 pub(crate) fn get_vmgcref(&self) -> Option<crate::vm::VMGcRef> {
1775 debug_assert_eq!(self.get_anyref(), self.get_exnref());
1776 debug_assert_eq!(self.get_anyref(), self.get_externref());
1777 VMGcRef::from_raw_u32(self.get_anyref())
1778 }
1779}
1780
1781/// An "opaque" version of `VMContext` which must be explicitly casted to a
1782/// target context.
1783///
1784/// This context is used to represent that contexts specified in
1785/// `VMFuncRef` can have any type and don't have an implicit
1786/// structure. Neither wasmtime nor cranelift-generated code can rely on the
1787/// structure of an opaque context in general and only the code which configured
1788/// the context is able to rely on a particular structure. This is because the
1789/// context pointer configured for `VMFuncRef` is guaranteed to be
1790/// the first parameter passed.
1791///
1792/// Note that Wasmtime currently has a layout where all contexts that are casted
1793/// to an opaque context start with a 32-bit "magic" which can be used in debug
1794/// mode to debug-assert that the casts here are correct and have at least a
1795/// little protection against incorrect casts.
1796pub struct VMOpaqueContext {
1797 pub(crate) magic: u32,
1798 _marker: marker::PhantomPinned,
1799}
1800
1801impl VMOpaqueContext {
1802 /// Helper function to clearly indicate that casts are desired.
1803 #[inline]
1804 pub fn from_vmcontext(ptr: NonNull<VMContext>) -> NonNull<VMOpaqueContext> {
1805 ptr.cast()
1806 }
1807
1808 /// Helper function to clearly indicate that casts are desired.
1809 #[inline]
1810 pub fn from_vm_array_call_host_func_context(
1811 ptr: NonNull<VMArrayCallHostFuncContext>,
1812 ) -> NonNull<VMOpaqueContext> {
1813 ptr.cast()
1814 }
1815}