use crate::{
DefinedGlobalIndex, DefinedMemoryIndex, DefinedTableIndex, DefinedTagIndex, FuncIndex,
FuncRefIndex, GlobalIndex, MemoryIndex, Module, OwnedMemoryIndex, RuntimeDataIndex, TableIndex,
TagIndex,
};
use cranelift_entity::packed_option::ReservedValue;
pub const NUM_COMPONENT_CONTEXT_SLOTS: usize = 2;
#[cfg(target_pointer_width = "32")]
fn cast_to_u32(sz: usize) -> u32 {
u32::try_from(sz).unwrap()
}
#[cfg(target_pointer_width = "64")]
fn cast_to_u32(sz: usize) -> u32 {
u32::try_from(sz).expect("overflow in cast from usize to u32")
}
#[inline]
fn align(offset: u32, width: u32) -> u32 {
(offset + (width - 1)) / width * width
}
macro_rules! define_vm_type_offsets {
(@size ($p:expr) UnsafeCell < $inner:tt >) => { define_vm_type_offsets!(@size ($p) $inner) };
(@size ($p:expr) VmPtr < $g:ty >) => { u32::from($p) };
(@size ($p:expr) Option < VmPtr < $g:ty >>) => { u32::from($p) };
(@size ($p:expr) AtomicUsize) => { u32::from($p) };
(@size ($p:expr) usize) => { u32::from($p) };
(@size ($p:expr) i64) => { 8u32 };
(@size ($p:expr) u64) => { 8u32 };
(@size ($p:expr) u32) => { 4u32 };
(@size ($p:expr) [u8; 16]) => { 16u32 };
(@size ($p:expr) [u32; $n:expr]) => { 4u32 * u32::try_from($n).unwrap() };
(@size ($p:expr) VMSharedTypeIndex) => { u32::from(($p).size_of_vmshared_type_index()) };
(@size ($p:expr) DefinedTableIndex) => { 4u32 };
(@size ($p:expr) DefinedMemoryIndex) => { 4u32 };
(@size ($p:expr) DefinedTagIndex) => { 4u32 };
(@size ($p:expr) VMGlobalKind) => { 8u32 };
(@size ($p:expr) Range < *mut u8 >) => { 2u32 * u32::from($p) };
(@size ($p:expr) VMMemoryDefinition) => { u32::from(($p).vm_memory_definition().size()) };
(@size ($p:expr) VMLazyThread) => { u32::from(($p).vm_lazy_thread().size()) };
(@size ($p:expr) VMStackChain) => { u32::from(($p).size_of_vmstack_chain()) };
(@align ($p:expr) UnsafeCell < $inner:tt >) => { define_vm_type_offsets!(@align ($p) $inner) };
(@align ($p:expr) VmPtr < $g:ty >) => { u32::from($p) };
(@align ($p:expr) Option < VmPtr < $g:ty >>) => { u32::from($p) };
(@align ($p:expr) AtomicUsize) => { u32::from($p) };
(@align ($p:expr) usize) => { u32::from($p) };
(@align ($p:expr) i64) => { 8u32 };
(@align ($p:expr) u64) => { 8u32 };
(@align ($p:expr) u32) => { 4u32 };
(@align ($p:expr) [u8; 16]) => { 16u32 };
(@align ($p:expr) [u32; $n:expr]) => { 4u32 };
(@align ($p:expr) VMSharedTypeIndex) => { u32::from(($p).align_of_vmshared_type_index()) };
(@align ($p:expr) DefinedTableIndex) => { 4u32 };
(@align ($p:expr) DefinedMemoryIndex) => { 4u32 };
(@align ($p:expr) DefinedTagIndex) => { 4u32 };
(@align ($p:expr) VMGlobalKind) => { 4u32 };
(@align ($p:expr) Range < *mut u8 >) => { u32::from($p) };
(@align ($p:expr) VMMemoryDefinition) => { u32::from(($p).vm_memory_definition().align()) };
(@align ($p:expr) VMLazyThread) => { u32::from(($p).vm_lazy_thread().align()) };
(@align ($p:expr) VMStackChain) => { u32::from($p) };
(@repr_align C) => { 1u32 };
(@repr_align transparent) => { 1u32 };
(@repr_align C, align($n:literal)) => {{ let align: u32 = $n; align }};
(@fields $Name:ident ($p:ident, $o:ident) prefix( $($prefix:tt)* )) => {};
(@fields $Name:ident ($p:ident, $o:ident) prefix( $($prefix:tt)* )
[ $fname:ident : $($fty:tt)* ]
$($rest:tt)*
) => {
#[doc = concat!(
"The offset of the `", stringify!($fname),
"` field of `", stringify!($Name), "`."
)]
#[inline]
pub fn $fname(&self) -> u8 {
let $p = self.0.size();
let $o: u32 = 0;
$($prefix)*
let $o = align($o, define_vm_type_offsets!(@align ($p) $($fty)*));
let _ = $p;
u8::try_from($o).unwrap()
}
define_vm_type_offsets!(@fields $Name ($p, $o)
prefix(
$($prefix)*
let $o = align($o, define_vm_type_offsets!(@align ($p) $($fty)*));
let $o = $o + define_vm_type_offsets!(@size ($p) $($fty)*);
)
$($rest)*
);
};
(@impl $Name:ident [$($repr:tt)*] { $( [ $fname:ident : $($fty:tt)* ] )* }) => {
impl<P: PtrSize> $Name<P> {
define_vm_type_offsets!(@fields $Name (p, o) prefix()
$( [ $fname : $($fty)* ] )*
);
#[doc = concat!("The alignment of the `", stringify!($Name), "` type.")]
#[inline]
pub fn align(&self) -> u8 {
let p = self.0.size();
let a: u32 = define_vm_type_offsets!(@repr_align $($repr)*);
$(
let a = core::cmp::max(
a,
define_vm_type_offsets!(@align (p) $($fty)*),
);
)*
let _ = p;
u8::try_from(a).unwrap()
}
#[doc = concat!("The size of the `", stringify!($Name), "` type.")]
#[inline]
pub fn size(&self) -> u8 {
let p = self.0.size();
let o: u32 = 0;
$(
let o = align(o, define_vm_type_offsets!(@align (p) $($fty)*));
let o = o + define_vm_type_offsets!(@size (p) $($fty)*);
)*
let o = align(o, u32::from(self.align()));
let _ = p;
u8::try_from(o).unwrap()
}
}
};
(@impl $Name:ident $repr:tt { $($groups:tt)* }
$(#[$($attr:tt)*])* $fvis:vis $fname:ident : $($rest:tt)*
) => {
define_vm_type_offsets!(@impl_ty $Name $repr { $($groups)* } $fname [] $($rest)*);
};
(@impl_ty $Name:ident $repr:tt { $($groups:tt)* } $fname:ident [ $($fty:tt)* ] , $($rest:tt)*) => {
define_vm_type_offsets!(@impl $Name $repr { $($groups)* [ $fname : $($fty)* ] } $($rest)*);
};
(@impl_ty $Name:ident $repr:tt { $($groups:tt)* } $fname:ident [ $($fty:tt)* ] $tok:tt $($rest:tt)*) => {
define_vm_type_offsets!(@impl_ty $Name $repr { $($groups)* } $fname [ $($fty)* $tok ] $($rest)*);
};
( $(
$(#[doc = $sdoc:literal])*
$(#[derive($($d:ident),*)])?
#[repr($($repr:tt)*)]
#[snake_name = $snake:ident]
$svis:vis struct $Name:ident {
$($body:tt)*
}
)* ) => {
pub mod offsets {
use super::{align, PtrSize, NUM_COMPONENT_CONTEXT_SLOTS};
$(
#[doc = concat!("Offsets of fields within the `", stringify!($Name), "` type.")]
pub struct $Name<P: PtrSize>(pub P);
define_vm_type_offsets!(@impl $Name [$($repr)*] {} $($body)*);
)*
}
};
}
for_each_vm_type!(define_vm_type_offsets);
const COMPONENT_CONTEXT_SLOT_SIZE: u8 = 4;
impl<P: PtrSize> offsets::VMStoreContext<P> {
pub fn gc_heap_base(&self) -> u8 {
let offset = self.gc_heap() + self.0.vm_memory_definition().base();
debug_assert!(offset < self.last_wasm_exit_trampoline_fp());
offset
}
pub fn gc_heap_current_length(&self) -> u8 {
let offset = self.gc_heap() + self.0.vm_memory_definition().current_length();
debug_assert!(offset < self.last_wasm_exit_trampoline_fp());
offset
}
pub fn component_context_slot(&self, i: u8) -> u8 {
assert!(usize::from(i) < NUM_COMPONENT_CONTEXT_SLOTS);
self.component_context() + i * COMPONENT_CONTEXT_SLOT_SIZE
}
}
impl<P: PtrSize> offsets::VMDeferredThread<P> {
pub fn saved_context_slot(&self, i: u8) -> u8 {
assert!(usize::from(i) < NUM_COMPONENT_CONTEXT_SLOTS);
self.saved_context() + i * COMPONENT_CONTEXT_SLOT_SIZE
}
}
macro_rules! define_ptr_size_vm_type_accessors {
( $(
$(#[doc = $sdoc:literal])*
$(#[derive($($d:ident),*)])?
#[repr($($repr:tt)*)]
#[snake_name = $snake:ident]
$svis:vis struct $Name:ident {
$($body:tt)*
}
)* ) => {
$(
#[doc = concat!("Get the [`offsets::", stringify!($Name), "`] offsets for this pointer size.")]
#[inline]
fn $snake(&self) -> offsets::$Name<&Self> {
offsets::$Name(self)
}
)*
};
}
#[derive(Debug, Clone, Copy)]
pub struct VMOffsets<P> {
pub ptr: P,
pub num_imported_functions: u32,
pub num_imported_tables: u32,
pub num_imported_memories: u32,
pub num_imported_globals: u32,
pub num_imported_tags: u32,
pub num_defined_tables: u32,
pub num_defined_memories: u32,
pub num_owned_memories: u32,
pub num_defined_globals: u32,
pub num_defined_tags: u32,
pub num_escaped_funcs: u32,
pub num_runtime_data: u32,
pub has_startup_func: bool,
imported_functions: u32,
imported_tables: u32,
imported_memories: u32,
imported_globals: u32,
imported_tags: u32,
defined_tables: u32,
defined_memories: u32,
owned_memories: u32,
defined_globals: u32,
defined_tags: u32,
defined_func_refs: u32,
startup_func_ref: u32,
runtime_data_bases: u32,
runtime_data_lengths: u32,
size: u32,
}
pub trait PtrSize {
fn size(&self) -> u8;
for_each_vm_type!(define_ptr_size_vm_type_accessors);
fn vmcontext_store_context(&self) -> u8 {
u8::try_from(align(
u32::try_from(core::mem::size_of::<u32>()).unwrap(),
u32::from(self.size()),
))
.unwrap()
}
fn vmcontext_builtin_functions(&self) -> u8 {
self.vmcontext_store_context() + self.size()
}
#[inline]
fn size_of_vmshared_type_index(&self) -> u8 {
4
}
#[inline]
fn align_of_vmshared_type_index(&self) -> u8 {
4
}
#[inline]
fn maximum_value_size(&self) -> u8 {
self.vm_global_definition().size()
}
#[inline]
fn size_of_vmmemory_pointer(&self) -> u8 {
self.size()
}
fn vmarray_call_host_func_context_func_ref(&self) -> u8 {
u8::try_from(align(
u32::try_from(core::mem::size_of::<u32>()).unwrap(),
u32::from(self.size()),
))
.unwrap()
}
fn size_of_vmstack_chain(&self) -> u8 {
2 * self.size()
}
fn vmstack_limits_stack_limit(&self) -> u8 {
0
}
fn vmstack_limits_last_wasm_entry_fp(&self) -> u8 {
self.size()
}
fn vmstack_limits_last_wasm_entry_sp(&self) -> u8 {
self.vmstack_limits_last_wasm_entry_fp() + self.size()
}
fn vmstack_limits_last_wasm_entry_trap_handler(&self) -> u8 {
self.vmstack_limits_last_wasm_entry_sp() + self.size()
}
fn vmhostarray_length(&self) -> u8 {
0
}
fn vmhostarray_capacity(&self) -> u8 {
4
}
fn vmhostarray_data(&self) -> u8 {
8
}
fn size_of_vmhostarray(&self) -> u8 {
8 + self.size()
}
fn vmcommon_stack_information_limits(&self) -> u8 {
0 * self.size()
}
fn vmcommon_stack_information_state(&self) -> u8 {
4 * self.size()
}
fn vmcommon_stack_information_handlers(&self) -> u8 {
u8::try_from(align(
self.vmcommon_stack_information_state() as u32 + 4,
u32::from(self.size()),
))
.unwrap()
}
fn vmcommon_stack_information_first_switch_handler_index(&self) -> u8 {
self.vmcommon_stack_information_handlers() + self.size_of_vmhostarray()
}
fn size_of_vmcommon_stack_information(&self) -> u8 {
u8::try_from(align(
self.vmcommon_stack_information_first_switch_handler_index() as u32 + 4,
u32::from(self.size()),
))
.unwrap()
}
fn vmcontobj_contref(&self) -> u8 {
0
}
fn vmcontobj_revision(&self) -> u8 {
self.size()
}
fn size_of_vmcontobj(&self) -> u8 {
u8::try_from(align(
u32::from(self.vmcontobj_revision())
+ u32::try_from(core::mem::size_of::<usize>()).unwrap(),
u32::from(self.size()),
))
.unwrap()
}
fn vmcontref_common_stack_information(&self) -> u8 {
0 * self.size()
}
fn vmcontref_parent_chain(&self) -> u8 {
u8::try_from(align(
(self.vmcontref_common_stack_information() + self.size_of_vmcommon_stack_information())
as u32,
u32::from(self.size()),
))
.unwrap()
}
fn vmcontref_last_ancestor(&self) -> u8 {
self.vmcontref_parent_chain() + 2 * self.size()
}
fn vmcontref_revision(&self) -> u8 {
self.vmcontref_last_ancestor() + self.size()
}
fn vmcontref_stack(&self) -> u8 {
self.vmcontref_revision() + self.size()
}
fn vmcontref_args(&self) -> u8 {
self.vmcontref_stack() + 3 * self.size()
}
fn vmcontref_values(&self) -> u8 {
self.vmcontref_args() + self.size_of_vmhostarray()
}
#[inline]
fn vmctx_magic(&self) -> u8 {
0
}
#[inline]
fn vmctx_store_context(&self) -> u8 {
self.vmctx_magic() + self.size()
}
#[inline]
fn vmctx_builtin_functions(&self) -> u8 {
self.vmctx_store_context() + self.size()
}
#[inline]
fn vmctx_epoch_ptr(&self) -> u8 {
self.vmctx_builtin_functions() + self.size()
}
#[inline]
fn vmctx_gc_heap_data(&self) -> u8 {
self.vmctx_epoch_ptr() + self.size()
}
#[inline]
fn vmdrc_heap_data_over_approximated_stack_roots(&self) -> u8 {
0
}
#[inline]
fn vmdrc_heap_data_current_over_approximated_stack_roots_len(&self) -> u8 {
4
}
#[inline]
fn vmdrc_heap_data_over_approximated_stack_roots_len_after_last_gc(&self) -> u8 {
8
}
#[inline]
fn size_of_vmdrc_heap_data(&self) -> u8 {
12
}
#[inline]
fn align_of_vmdrc_heap_data(&self) -> u8 {
4
}
#[inline]
fn vmcopying_heap_data_bump_ptr(&self) -> u8 {
0
}
#[inline]
fn vmcopying_heap_data_active_space_end(&self) -> u8 {
4
}
#[inline]
fn size_of_vmcopying_heap_data(&self) -> u8 {
8
}
#[inline]
fn align_of_vmcopying_heap_data(&self) -> u8 {
4
}
#[inline]
fn vmctx_type_ids_array(&self) -> u8 {
self.vmctx_gc_heap_data() + self.size()
}
#[inline]
fn vmctx_dynamic_data_start(&self) -> u8 {
self.vmctx_type_ids_array() + self.size()
}
}
pub trait GetPtrSize {
type Ptr: PtrSize;
fn get_ptr_size(&self) -> &Self::Ptr;
}
impl<P> GetPtrSize for P
where
P: PtrSize,
{
type Ptr = Self;
#[inline]
fn get_ptr_size(&self) -> &Self::Ptr {
self
}
}
#[derive(Clone, Copy)]
pub struct HostPtr;
impl PtrSize for HostPtr {
#[inline]
fn size(&self) -> u8 {
core::mem::size_of::<usize>() as u8
}
}
impl PtrSize for u8 {
#[inline]
fn size(&self) -> u8 {
*self
}
}
impl<P> PtrSize for &'_ P
where
P: PtrSize + ?Sized,
{
#[inline]
fn size(&self) -> u8 {
(**self).size()
}
}
#[derive(Debug, Clone, Copy)]
pub struct VMOffsetsFields<P> {
pub ptr: P,
pub num_imported_functions: u32,
pub num_imported_tables: u32,
pub num_imported_memories: u32,
pub num_imported_globals: u32,
pub num_imported_tags: u32,
pub num_defined_tables: u32,
pub num_defined_memories: u32,
pub num_owned_memories: u32,
pub num_defined_globals: u32,
pub num_defined_tags: u32,
pub num_escaped_funcs: u32,
pub num_runtime_data: u32,
pub has_startup_func: bool,
}
impl<P: PtrSize> VMOffsets<P> {
pub fn new(ptr: P, module: &Module) -> Self {
let num_owned_memories = module
.memories
.iter()
.skip(module.num_imported_memories)
.filter(|p| !p.1.shared)
.count()
.try_into()
.unwrap();
VMOffsets::from(VMOffsetsFields {
ptr,
num_imported_functions: cast_to_u32(module.num_imported_funcs),
num_imported_tables: cast_to_u32(module.num_imported_tables),
num_imported_memories: cast_to_u32(module.num_imported_memories),
num_imported_globals: cast_to_u32(module.num_imported_globals),
num_imported_tags: cast_to_u32(module.num_imported_tags),
num_defined_tables: cast_to_u32(module.num_defined_tables()),
num_defined_memories: cast_to_u32(module.num_defined_memories()),
num_owned_memories,
num_defined_globals: cast_to_u32(module.globals.len() - module.num_imported_globals),
num_defined_tags: cast_to_u32(module.tags.len() - module.num_imported_tags),
num_escaped_funcs: cast_to_u32(module.num_escaped_funcs),
num_runtime_data: cast_to_u32(module.runtime_data.len()),
has_startup_func: !module.startup.is_none(),
})
}
#[inline]
pub fn pointer_size(&self) -> u8 {
self.ptr.size()
}
pub fn region_sizes(&self) -> impl Iterator<Item = (&str, u32)> {
macro_rules! calculate_sizes {
($($name:ident: $desc:tt,)*) => {{
let VMOffsets {
ptr: _,
num_imported_functions: _,
num_imported_tables: _,
num_imported_memories: _,
num_imported_globals: _,
num_imported_tags: _,
num_defined_tables: _,
num_defined_globals: _,
num_defined_memories: _,
num_defined_tags: _,
num_owned_memories: _,
num_escaped_funcs: _,
num_runtime_data: _,
has_startup_func: _,
size,
$($name,)*
} = *self;
let mut last = size;
$(
assert!($name <= last);
let tmp = $name;
let $name = last - $name;
last = tmp;
)*
assert_ne!(last, 0);
IntoIterator::into_iter([
$(($desc, $name),)*
("static vmctx data", last),
])
}};
}
calculate_sizes! {
runtime_data_lengths: "runtime data lengths",
runtime_data_bases: "runtime data base pointers",
startup_func_ref: "startup funcref",
defined_func_refs: "module functions",
defined_tags: "defined tags",
defined_globals: "defined globals",
defined_tables: "defined tables",
imported_tags: "imported tags",
imported_globals: "imported globals",
imported_tables: "imported tables",
imported_functions: "imported functions",
owned_memories: "owned memories",
defined_memories: "defined memories",
imported_memories: "imported memories",
}
}
}
impl<P: PtrSize> GetPtrSize for VMOffsets<P> {
type Ptr = P;
#[inline]
fn get_ptr_size(&self) -> &Self::Ptr {
&self.ptr
}
}
impl<P: PtrSize> From<VMOffsetsFields<P>> for VMOffsets<P> {
fn from(fields: VMOffsetsFields<P>) -> VMOffsets<P> {
let mut ret = Self {
ptr: fields.ptr,
num_imported_functions: fields.num_imported_functions,
num_imported_tables: fields.num_imported_tables,
num_imported_memories: fields.num_imported_memories,
num_imported_globals: fields.num_imported_globals,
num_imported_tags: fields.num_imported_tags,
num_defined_tables: fields.num_defined_tables,
num_defined_memories: fields.num_defined_memories,
num_owned_memories: fields.num_owned_memories,
num_defined_globals: fields.num_defined_globals,
num_defined_tags: fields.num_defined_tags,
num_escaped_funcs: fields.num_escaped_funcs,
num_runtime_data: fields.num_runtime_data,
has_startup_func: fields.has_startup_func,
imported_functions: 0,
imported_tables: 0,
imported_memories: 0,
imported_globals: 0,
imported_tags: 0,
defined_tables: 0,
defined_memories: 0,
owned_memories: 0,
defined_globals: 0,
defined_tags: 0,
defined_func_refs: 0,
startup_func_ref: 0,
runtime_data_bases: 0,
runtime_data_lengths: 0,
size: 0,
};
#[inline]
fn cadd(count: u32, size: u32) -> u32 {
count.checked_add(size).unwrap()
}
#[inline]
fn cmul(count: u32, size: u8) -> u32 {
count.checked_mul(u32::from(size)).unwrap()
}
let mut next_field_offset = u32::from(ret.ptr.vmctx_dynamic_data_start());
macro_rules! fields {
(size($field:ident) = $size:expr, $($rest:tt)*) => {
ret.$field = next_field_offset;
next_field_offset = cadd(next_field_offset, u32::from($size));
fields!($($rest)*);
};
(align($align:expr), $($rest:tt)*) => {
next_field_offset = align(next_field_offset, $align);
fields!($($rest)*);
};
() => {};
}
fields! {
size(imported_memories)
= cmul(ret.num_imported_memories, ret.ptr.vm_memory_import().size()),
size(defined_memories)
= cmul(ret.num_defined_memories, ret.ptr.size_of_vmmemory_pointer()),
size(owned_memories)
= cmul(ret.num_owned_memories, ret.ptr.vm_memory_definition().size()),
size(imported_functions)
= cmul(ret.num_imported_functions, ret.ptr.vm_function_import().size()),
size(imported_tables)
= cmul(ret.num_imported_tables, ret.ptr.vm_table_import().size()),
size(imported_globals)
= cmul(ret.num_imported_globals, ret.ptr.vm_global_import().size()),
size(imported_tags)
= cmul(ret.num_imported_tags, ret.ptr.vm_tag_import().size()),
size(defined_tables)
= cmul(ret.num_defined_tables, ret.ptr.vm_table_definition().size()),
align(16),
size(defined_globals)
= cmul(ret.num_defined_globals, ret.ptr.vm_global_definition().size()),
size(defined_tags)
= cmul(ret.num_defined_tags, ret.ptr.vm_tag_definition().size()),
size(defined_func_refs) = cmul(
ret.num_escaped_funcs,
ret.ptr.vm_func_ref().size(),
),
size(startup_func_ref) = if ret.has_startup_func {
ret.ptr.vm_func_ref().size()
} else {
0
},
size(runtime_data_bases) = cmul(ret.num_runtime_data, ret.ptr.size()),
size(runtime_data_lengths) = cmul(ret.num_runtime_data, 4),
}
ret.size = next_field_offset;
return ret;
}
}
impl<P: PtrSize> VMOffsets<P> {
pub fn size_of_vmfunction_body_ptr(&self) -> u8 {
1 * self.pointer_size()
}
}
impl<P: PtrSize> VMOffsets<P> {
#[inline]
pub fn size_of_vmtable_definition_current_elements(&self) -> u8 {
self.pointer_size()
}
}
impl<P: PtrSize> VMOffsets<P> {
#[inline]
pub fn size_of_vmshared_type_index(&self) -> u8 {
self.ptr.size_of_vmshared_type_index()
}
}
impl<P: PtrSize> VMOffsets<P> {
#[inline]
pub fn vmctx_imported_functions_begin(&self) -> u32 {
self.imported_functions
}
#[inline]
pub fn vmctx_imported_tables_begin(&self) -> u32 {
self.imported_tables
}
#[inline]
pub fn vmctx_imported_memories_begin(&self) -> u32 {
self.imported_memories
}
#[inline]
pub fn vmctx_imported_globals_begin(&self) -> u32 {
self.imported_globals
}
#[inline]
pub fn vmctx_imported_tags_begin(&self) -> u32 {
self.imported_tags
}
#[inline]
pub fn vmctx_tables_begin(&self) -> u32 {
self.defined_tables
}
#[inline]
pub fn vmctx_memories_begin(&self) -> u32 {
self.defined_memories
}
#[inline]
pub fn vmctx_owned_memories_begin(&self) -> u32 {
self.owned_memories
}
#[inline]
pub fn vmctx_globals_begin(&self) -> u32 {
self.defined_globals
}
#[inline]
pub fn vmctx_tags_begin(&self) -> u32 {
self.defined_tags
}
#[inline]
pub fn vmctx_func_refs_begin(&self) -> u32 {
self.defined_func_refs
}
#[inline]
pub fn vmctx_runtime_data_bases_begin(&self) -> u32 {
self.runtime_data_bases
}
#[inline]
pub fn vmctx_runtime_data_lengths_begin(&self) -> u32 {
self.runtime_data_lengths
}
#[inline]
pub fn size_of_vmctx(&self) -> u32 {
self.size
}
#[inline]
pub fn vmctx_vmfunction_import(&self, index: FuncIndex) -> u32 {
assert!(index.as_u32() < self.num_imported_functions);
self.vmctx_imported_functions_begin()
+ index.as_u32() * u32::from(self.ptr.vm_function_import().size())
}
#[inline]
pub fn vmctx_vmtable_import(&self, index: TableIndex) -> u32 {
assert!(index.as_u32() < self.num_imported_tables);
self.vmctx_imported_tables_begin()
+ index.as_u32() * u32::from(self.ptr.vm_table_import().size())
}
#[inline]
pub fn vmctx_vmmemory_import(&self, index: MemoryIndex) -> u32 {
assert!(index.as_u32() < self.num_imported_memories);
self.vmctx_imported_memories_begin()
+ index.as_u32() * u32::from(self.ptr.vm_memory_import().size())
}
#[inline]
pub fn vmctx_vmglobal_import(&self, index: GlobalIndex) -> u32 {
assert!(index.as_u32() < self.num_imported_globals);
self.vmctx_imported_globals_begin()
+ index.as_u32() * u32::from(self.ptr.vm_global_import().size())
}
#[inline]
pub fn vmctx_vmtag_import(&self, index: TagIndex) -> u32 {
assert!(index.as_u32() < self.num_imported_tags);
self.vmctx_imported_tags_begin()
+ index.as_u32() * u32::from(self.ptr.vm_tag_import().size())
}
#[inline]
pub fn vmctx_vmtable_definition(&self, index: DefinedTableIndex) -> u32 {
assert!(index.as_u32() < self.num_defined_tables);
self.vmctx_tables_begin()
+ index.as_u32() * u32::from(self.ptr.vm_table_definition().size())
}
#[inline]
pub fn vmctx_vmmemory_pointer(&self, index: DefinedMemoryIndex) -> u32 {
assert!(index.as_u32() < self.num_defined_memories);
self.vmctx_memories_begin()
+ index.as_u32() * u32::from(self.ptr.size_of_vmmemory_pointer())
}
#[inline]
pub fn vmctx_vmmemory_definition(&self, index: OwnedMemoryIndex) -> u32 {
assert!(index.as_u32() < self.num_owned_memories);
self.vmctx_owned_memories_begin()
+ index.as_u32() * u32::from(self.ptr.vm_memory_definition().size())
}
#[inline]
pub fn vmctx_vmglobal_definition(&self, index: DefinedGlobalIndex) -> u32 {
assert!(index.as_u32() < self.num_defined_globals);
self.vmctx_globals_begin()
+ index.as_u32() * u32::from(self.ptr.vm_global_definition().size())
}
#[inline]
pub fn vmctx_vmtag_definition(&self, index: DefinedTagIndex) -> u32 {
assert!(index.as_u32() < self.num_defined_tags);
self.vmctx_tags_begin() + index.as_u32() * u32::from(self.ptr.vm_tag_definition().size())
}
#[inline]
pub fn vmctx_func_ref(&self, index: FuncRefIndex) -> u32 {
assert!(!index.is_reserved_value());
assert!(index.as_u32() < self.num_escaped_funcs);
self.vmctx_func_refs_begin() + index.as_u32() * u32::from(self.ptr.vm_func_ref().size())
}
#[inline]
pub fn vmctx_startup_func_ref(&self) -> u32 {
assert!(self.has_startup_func);
self.startup_func_ref
}
#[inline]
pub fn vmctx_runtime_data_base(&self, index: RuntimeDataIndex) -> u32 {
assert!(!index.is_reserved_value());
assert!(index.as_u32() < self.num_runtime_data);
self.vmctx_runtime_data_bases_begin() + index.as_u32() * u32::from(self.ptr.size())
}
#[inline]
pub fn vmctx_runtime_data_length(&self, index: RuntimeDataIndex) -> u32 {
assert!(!index.is_reserved_value());
assert!(index.as_u32() < self.num_runtime_data);
self.vmctx_runtime_data_lengths_begin() + index.as_u32() * 4
}
#[inline]
pub fn vmctx_vmfunction_import_wasm_call(&self, index: FuncIndex) -> u32 {
self.vmctx_vmfunction_import(index) + u32::from(self.ptr.vm_function_import().wasm_call())
}
#[inline]
pub fn vmctx_vmfunction_import_array_call(&self, index: FuncIndex) -> u32 {
self.vmctx_vmfunction_import(index) + u32::from(self.ptr.vm_function_import().array_call())
}
#[inline]
pub fn vmctx_vmfunction_import_vmctx(&self, index: FuncIndex) -> u32 {
self.vmctx_vmfunction_import(index) + u32::from(self.ptr.vm_function_import().vmctx())
}
#[inline]
pub fn vmctx_vmtable_from(&self, index: TableIndex) -> u32 {
self.vmctx_vmtable_import(index) + u32::from(self.ptr.vm_table_import().from())
}
#[inline]
pub fn vmctx_vmtable_definition_base(&self, index: DefinedTableIndex) -> u32 {
self.vmctx_vmtable_definition(index) + u32::from(self.ptr.vm_table_definition().base())
}
#[inline]
pub fn vmctx_vmtable_definition_current_elements(&self, index: DefinedTableIndex) -> u32 {
self.vmctx_vmtable_definition(index)
+ u32::from(self.ptr.vm_table_definition().current_elements())
}
#[inline]
pub fn vmctx_vmmemory_import_from(&self, index: MemoryIndex) -> u32 {
self.vmctx_vmmemory_import(index) + u32::from(self.ptr.vm_memory_import().from())
}
#[inline]
pub fn vmctx_vmmemory_definition_base(&self, index: OwnedMemoryIndex) -> u32 {
self.vmctx_vmmemory_definition(index) + u32::from(self.ptr.vm_memory_definition().base())
}
#[inline]
pub fn vmctx_vmmemory_definition_current_length(&self, index: OwnedMemoryIndex) -> u32 {
self.vmctx_vmmemory_definition(index)
+ u32::from(self.ptr.vm_memory_definition().current_length())
}
#[inline]
pub fn vmctx_vmglobal_import_from(&self, index: GlobalIndex) -> u32 {
self.vmctx_vmglobal_import(index) + u32::from(self.ptr.vm_global_import().from())
}
#[inline]
pub fn vmctx_vmtag_import_from(&self, index: TagIndex) -> u32 {
self.vmctx_vmtag_import(index) + u32::from(self.ptr.vm_tag_import().from())
}
#[inline]
pub fn vmctx_vmtag_import_vmctx(&self, index: TagIndex) -> u32 {
self.vmctx_vmtag_import(index) + u32::from(self.ptr.vm_tag_import().vmctx())
}
#[inline]
pub fn vmctx_vmtag_import_index(&self, index: TagIndex) -> u32 {
self.vmctx_vmtag_import(index) + u32::from(self.ptr.vm_tag_import().index())
}
}
impl<P: PtrSize> VMOffsets<P> {
#[inline]
pub fn vm_gc_header_kind(&self) -> u32 {
0
}
#[inline]
pub fn vm_gc_header_reserved_bits(&self) -> u32 {
self.vm_gc_header_kind()
}
#[inline]
pub fn vm_gc_header_ty(&self) -> u32 {
self.vm_gc_header_kind() + 4
}
}
impl<P: PtrSize> VMOffsets<P> {
#[inline]
pub fn vm_drc_header_ref_count(&self) -> u32 {
8
}
#[inline]
pub fn vm_drc_header_next_over_approximated_stack_root(&self) -> u32 {
self.vm_drc_header_ref_count() + 8
}
}
pub const VMCONTEXT_MAGIC: u32 = u32::from_le_bytes(*b"core");
pub const VM_ARRAY_CALL_HOST_FUNC_MAGIC: u32 = u32::from_le_bytes(*b"ACHF");
#[cfg(test)]
mod tests {
use crate::vmoffsets::align;
#[test]
fn alignment() {
fn is_aligned(x: u32) -> bool {
x % 16 == 0
}
assert!(is_aligned(align(0, 16)));
assert!(is_aligned(align(32, 16)));
assert!(is_aligned(align(33, 16)));
assert!(is_aligned(align(31, 16)));
}
}
pub const VM_LAZY_THREAD_FORCED: u64 = 1;