#![deny(rustdoc::broken_intra_doc_links)]
use crate::VMBuiltinFunctionIndex;
use more_asserts::assert_lt;
use std::convert::TryFrom;
use std::mem::align_of;
use wasmer_types::{
FunctionIndex, GlobalIndex, LocalGlobalIndex, LocalMemoryIndex, LocalTableIndex, MemoryIndex,
ModuleInfo, SignatureIndex, TableIndex,
};
#[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")
}
const fn align(offset: u32, width: u32) -> u32 {
(offset + (width - 1)) / width * width
}
#[derive(Clone, Debug)]
pub struct VMOffsets {
pub pointer_size: u8,
pub num_signature_ids: u32,
pub num_imported_functions: u32,
pub num_imported_tables: u32,
pub num_imported_memories: u32,
pub num_imported_globals: u32,
pub num_local_tables: u32,
pub num_local_memories: u32,
pub num_local_globals: u32,
pub has_trap_handlers: bool,
}
impl VMOffsets {
pub fn new(pointer_size: u8) -> Self {
Self {
pointer_size,
num_signature_ids: 0,
num_imported_functions: 0,
num_imported_tables: 0,
num_imported_memories: 0,
num_imported_globals: 0,
num_local_tables: 0,
num_local_memories: 0,
num_local_globals: 0,
has_trap_handlers: false,
}
}
pub fn for_host() -> Self {
Self::new(std::mem::size_of::<*const u8>() as u8)
}
pub fn with_module_info(mut self, module: &ModuleInfo) -> Self {
self.num_imported_functions = module.import_counts.functions;
self.num_imported_tables = module.import_counts.tables;
self.num_imported_memories = module.import_counts.memories;
self.num_imported_globals = module.import_counts.globals;
self.num_signature_ids = cast_to_u32(module.signatures.len());
self.num_local_tables = cast_to_u32(module.tables.len());
self.num_local_memories = cast_to_u32(module.memories.len());
self.num_local_globals = cast_to_u32(module.globals.len());
self.has_trap_handlers = true;
self
}
pub fn with_archived_module_info(mut self, module: &rkyv::Archived<ModuleInfo>) -> Self {
self.num_imported_functions = module.import_counts.functions;
self.num_imported_tables = module.import_counts.tables;
self.num_imported_memories = module.import_counts.memories;
self.num_imported_globals = module.import_counts.globals;
self.num_signature_ids = cast_to_u32(module.signatures.len());
self.num_local_tables = cast_to_u32(module.tables.len());
self.num_local_memories = cast_to_u32(module.memories.len());
self.num_local_globals = cast_to_u32(module.globals.len());
self.has_trap_handlers = true;
self
}
}
impl VMOffsets {
#[allow(clippy::erasing_op)]
pub const fn vmfunction_import_body(&self) -> u8 {
0 * self.pointer_size
}
#[allow(clippy::identity_op)]
pub const fn vmfunction_import_vmctx(&self) -> u8 {
3 * self.pointer_size
}
pub const fn size_of_vmfunction_import(&self) -> u8 {
4 * self.pointer_size
}
}
impl VMOffsets {
#[allow(clippy::erasing_op)]
pub const fn vmdynamicfunction_import_context_address(&self) -> u8 {
0 * self.pointer_size
}
#[allow(clippy::identity_op)]
pub const fn vmdynamicfunction_import_context_ctx(&self) -> u8 {
1 * self.pointer_size
}
pub const fn size_of_vmdynamicfunction_import_context(&self) -> u8 {
2 * self.pointer_size
}
}
impl VMOffsets {
#[allow(clippy::identity_op)]
pub const fn size_of_vmfunction_body_ptr(&self) -> u8 {
1 * self.pointer_size
}
}
impl VMOffsets {
#[allow(clippy::erasing_op)]
pub const fn vmtable_import_definition(&self) -> u8 {
0 * self.pointer_size
}
#[allow(clippy::identity_op)]
pub const fn vmtable_import_from(&self) -> u8 {
1 * self.pointer_size
}
pub const fn size_of_vmtable_import(&self) -> u8 {
3 * self.pointer_size
}
}
impl VMOffsets {
#[allow(clippy::erasing_op)]
pub const fn vmtable_definition_base(&self) -> u8 {
0 * self.pointer_size
}
#[allow(clippy::identity_op)]
pub const fn vmtable_definition_current_elements(&self) -> u8 {
1 * self.pointer_size
}
pub const fn size_of_vmtable_definition_current_elements(&self) -> u8 {
4
}
pub const fn size_of_vmtable_definition(&self) -> u8 {
2 * self.pointer_size
}
}
impl VMOffsets {
#[allow(clippy::erasing_op)]
pub const fn vmmemory_import_definition(&self) -> u8 {
0 * self.pointer_size
}
#[allow(clippy::identity_op)]
pub const fn vmmemory_import_from(&self) -> u8 {
1 * self.pointer_size
}
pub const fn size_of_vmmemory_import(&self) -> u8 {
3 * self.pointer_size
}
}
impl VMOffsets {
#[allow(clippy::erasing_op)]
pub const fn vmmemory_definition_base(&self) -> u8 {
0 * self.pointer_size
}
#[allow(clippy::identity_op)]
pub const fn vmmemory_definition_current_length(&self) -> u8 {
1 * self.pointer_size
}
pub const fn size_of_vmmemory_definition_current_length(&self) -> u8 {
4
}
pub const fn size_of_vmmemory_definition(&self) -> u8 {
2 * self.pointer_size
}
}
impl VMOffsets {
#[allow(clippy::erasing_op)]
pub const fn vmglobal_import_definition(&self) -> u8 {
0 * self.pointer_size
}
#[allow(clippy::identity_op)]
pub const fn vmglobal_import_from(&self) -> u8 {
1 * self.pointer_size
}
#[allow(clippy::identity_op)]
pub const fn size_of_vmglobal_import(&self) -> u8 {
2 * self.pointer_size
}
}
impl VMOffsets {
pub const fn size_of_vmglobal_local(&self) -> u8 {
self.pointer_size
}
}
impl VMOffsets {
pub const fn size_of_vmshared_signature_index(&self) -> u8 {
4
}
}
impl VMOffsets {
#[allow(clippy::erasing_op)]
pub const fn vmcaller_checked_anyfunc_func_ptr(&self) -> u8 {
0 * self.pointer_size
}
#[allow(clippy::identity_op)]
pub const fn vmcaller_checked_anyfunc_type_index(&self) -> u8 {
1 * self.pointer_size
}
pub const fn vmcaller_checked_anyfunc_vmctx(&self) -> u8 {
2 * self.pointer_size
}
pub const fn size_of_vmcaller_checked_anyfunc(&self) -> u8 {
3 * self.pointer_size
}
}
impl VMOffsets {
#[allow(clippy::erasing_op)]
pub const fn vm_funcref_anyfunc_ptr(&self) -> u8 {
0 * self.pointer_size
}
pub const fn size_of_vm_funcref(&self) -> u8 {
self.pointer_size
}
}
fn offset_by(base: u32, num_items: u32, prev_item_size: u32, next_item_align: usize) -> u32 {
align(
base.checked_add(num_items.checked_mul(prev_item_size).unwrap())
.unwrap(),
next_item_align as u32,
)
}
impl VMOffsets {
pub fn vmctx_signature_ids_begin(&self) -> u32 {
0
}
#[allow(clippy::erasing_op)]
pub fn vmctx_imported_functions_begin(&self) -> u32 {
offset_by(
self.vmctx_signature_ids_begin(),
self.num_signature_ids,
u32::from(self.size_of_vmshared_signature_index()),
align_of::<crate::VMFunctionImport>(),
)
}
#[allow(clippy::identity_op)]
pub fn vmctx_imported_tables_begin(&self) -> u32 {
offset_by(
self.vmctx_imported_functions_begin(),
self.num_imported_functions,
u32::from(self.size_of_vmfunction_import()),
align_of::<crate::VMTableImport>(),
)
}
pub fn vmctx_imported_memories_begin(&self) -> u32 {
offset_by(
self.vmctx_imported_tables_begin(),
self.num_imported_tables,
u32::from(self.size_of_vmtable_import()),
align_of::<crate::VMMemoryImport>(),
)
}
pub fn vmctx_imported_globals_begin(&self) -> u32 {
offset_by(
self.vmctx_imported_memories_begin(),
self.num_imported_memories,
u32::from(self.size_of_vmmemory_import()),
align_of::<crate::VMGlobalImport>(),
)
}
pub fn vmctx_tables_begin(&self) -> u32 {
offset_by(
self.vmctx_imported_globals_begin(),
self.num_imported_globals,
u32::from(self.size_of_vmglobal_import()),
align_of::<crate::VMTableImport>(),
)
}
pub fn vmctx_memories_begin(&self) -> u32 {
offset_by(
self.vmctx_tables_begin(),
self.num_local_tables,
u32::from(self.size_of_vmtable_definition()),
align_of::<crate::VMMemoryDefinition>(),
)
}
pub fn vmctx_globals_begin(&self) -> u32 {
offset_by(
self.vmctx_memories_begin(),
self.num_local_memories,
u32::from(self.size_of_vmmemory_definition()),
align_of::<crate::VMGlobalDefinition>(),
)
}
pub fn vmctx_builtin_functions_begin(&self) -> u32 {
offset_by(
self.vmctx_globals_begin(),
self.num_local_globals,
u32::from(self.size_of_vmglobal_local()),
align_of::<crate::vmcontext::VMBuiltinFunctionsArray>(),
)
}
pub fn vmctx_trap_handler_begin(&self) -> u32 {
offset_by(
self.vmctx_builtin_functions_begin(),
VMBuiltinFunctionIndex::builtin_functions_total_number(),
u32::from(self.pointer_size),
align_of::<fn()>(),
)
}
pub fn vmctx_gas_limiter_pointer(&self) -> u32 {
offset_by(
self.vmctx_trap_handler_begin(),
if self.has_trap_handlers { 1 } else { 0 },
u32::from(self.pointer_size),
align_of::<*mut wasmer_types::FastGasCounter>(),
)
}
pub fn vmctx_stack_limit_begin(&self) -> u32 {
offset_by(
self.vmctx_gas_limiter_pointer(),
1,
u32::from(self.pointer_size),
align_of::<u32>(),
)
}
pub fn vmctx_stack_limit_initial_begin(&self) -> u32 {
self.vmctx_stack_limit_begin().checked_add(4).unwrap()
}
pub fn size_of_vmctx(&self) -> u32 {
self.vmctx_stack_limit_initial_begin()
.checked_add(4)
.unwrap()
}
pub fn vmctx_vmshared_signature_id(&self, index: SignatureIndex) -> u32 {
assert_lt!(index.as_u32(), self.num_signature_ids);
self.vmctx_signature_ids_begin()
.checked_add(
index
.as_u32()
.checked_mul(u32::from(self.size_of_vmshared_signature_index()))
.unwrap(),
)
.unwrap()
}
pub fn vmctx_vmfunction_import(&self, index: FunctionIndex) -> u32 {
assert_lt!(index.as_u32(), self.num_imported_functions);
self.vmctx_imported_functions_begin()
.checked_add(
index
.as_u32()
.checked_mul(u32::from(self.size_of_vmfunction_import()))
.unwrap(),
)
.unwrap()
}
pub fn vmctx_vmtable_import(&self, index: TableIndex) -> u32 {
assert_lt!(index.as_u32(), self.num_imported_tables);
self.vmctx_imported_tables_begin()
.checked_add(
index
.as_u32()
.checked_mul(u32::from(self.size_of_vmtable_import()))
.unwrap(),
)
.unwrap()
}
pub fn vmctx_vmmemory_import(&self, index: MemoryIndex) -> u32 {
assert_lt!(index.as_u32(), self.num_imported_memories);
self.vmctx_imported_memories_begin()
.checked_add(
index
.as_u32()
.checked_mul(u32::from(self.size_of_vmmemory_import()))
.unwrap(),
)
.unwrap()
}
pub fn vmctx_vmglobal_import(&self, index: GlobalIndex) -> u32 {
assert_lt!(index.as_u32(), self.num_imported_globals);
self.vmctx_imported_globals_begin()
.checked_add(
index
.as_u32()
.checked_mul(u32::from(self.size_of_vmglobal_import()))
.unwrap(),
)
.unwrap()
}
pub fn vmctx_vmtable_definition(&self, index: LocalTableIndex) -> u32 {
assert_lt!(index.as_u32(), self.num_local_tables);
self.vmctx_tables_begin()
.checked_add(
index
.as_u32()
.checked_mul(u32::from(self.size_of_vmtable_definition()))
.unwrap(),
)
.unwrap()
}
pub fn vmctx_vmmemory_definition(&self, index: LocalMemoryIndex) -> u32 {
assert_lt!(index.as_u32(), self.num_local_memories);
self.vmctx_memories_begin()
.checked_add(
index
.as_u32()
.checked_mul(u32::from(self.size_of_vmmemory_definition()))
.unwrap(),
)
.unwrap()
}
pub fn vmctx_vmglobal_definition(&self, index: LocalGlobalIndex) -> u32 {
assert_lt!(index.as_u32(), self.num_local_globals);
self.vmctx_globals_begin()
.checked_add(
index
.as_u32()
.checked_mul(u32::from(self.size_of_vmglobal_local()))
.unwrap(),
)
.unwrap()
}
pub fn vmctx_vmfunction_import_body(&self, index: FunctionIndex) -> u32 {
self.vmctx_vmfunction_import(index)
.checked_add(u32::from(self.vmfunction_import_body()))
.unwrap()
}
pub fn vmctx_vmfunction_import_vmctx(&self, index: FunctionIndex) -> u32 {
self.vmctx_vmfunction_import(index)
.checked_add(u32::from(self.vmfunction_import_vmctx()))
.unwrap()
}
pub fn vmctx_vmtable_import_definition(&self, index: TableIndex) -> u32 {
self.vmctx_vmtable_import(index)
.checked_add(u32::from(self.vmtable_import_definition()))
.unwrap()
}
pub fn vmctx_vmtable_definition_base(&self, index: LocalTableIndex) -> u32 {
self.vmctx_vmtable_definition(index)
.checked_add(u32::from(self.vmtable_definition_base()))
.unwrap()
}
pub fn vmctx_vmtable_definition_current_elements(&self, index: LocalTableIndex) -> u32 {
self.vmctx_vmtable_definition(index)
.checked_add(u32::from(self.vmtable_definition_current_elements()))
.unwrap()
}
pub fn vmctx_vmmemory_import_definition(&self, index: MemoryIndex) -> u32 {
self.vmctx_vmmemory_import(index)
.checked_add(u32::from(self.vmmemory_import_definition()))
.unwrap()
}
pub fn vmctx_vmmemory_import_from(&self, index: MemoryIndex) -> u32 {
self.vmctx_vmmemory_import(index)
.checked_add(u32::from(self.vmmemory_import_from()))
.unwrap()
}
pub fn vmctx_vmmemory_definition_base(&self, index: LocalMemoryIndex) -> u32 {
self.vmctx_vmmemory_definition(index)
.checked_add(u32::from(self.vmmemory_definition_base()))
.unwrap()
}
pub fn vmctx_vmmemory_definition_current_length(&self, index: LocalMemoryIndex) -> u32 {
self.vmctx_vmmemory_definition(index)
.checked_add(u32::from(self.vmmemory_definition_current_length()))
.unwrap()
}
pub fn vmctx_vmglobal_import_definition(&self, index: GlobalIndex) -> u32 {
self.vmctx_vmglobal_import(index)
.checked_add(u32::from(self.vmglobal_import_definition()))
.unwrap()
}
pub fn vmctx_builtin_function(&self, index: VMBuiltinFunctionIndex) -> u32 {
self.vmctx_builtin_functions_begin()
.checked_add(
index
.index()
.checked_mul(u32::from(self.pointer_size))
.unwrap(),
)
.unwrap()
}
pub fn vmctx_trap_handler(&self) -> u32 {
assert!(self.has_trap_handlers);
self.vmctx_trap_handler_begin()
}
}
#[derive(Debug, Copy, Clone)]
pub struct TargetSharedSignatureIndex(u32);
impl TargetSharedSignatureIndex {
pub const fn new(value: u32) -> Self {
Self(value)
}
pub const fn index(self) -> u32 {
self.0
}
}
#[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)));
}
}