use crate::export::Export;
use crate::global::Global;
use crate::imports::Imports;
use crate::memory::{Memory, MemoryError};
use crate::table::Table;
use crate::trap::{catch_traps, init_traps, Trap, TrapCode};
use crate::vmcontext::{
VMBuiltinFunctionsArray, VMCallerCheckedAnyfunc, VMContext, VMFunctionBody,
VMFunctionEnvironment, VMFunctionImport, VMFunctionKind, VMGlobalDefinition, VMGlobalImport,
VMMemoryDefinition, VMMemoryImport, VMSharedSignatureIndex, VMTableDefinition, VMTableImport,
VMTrampoline,
};
use crate::{ExportFunction, ExportGlobal, ExportMemory, ExportTable};
use crate::{FunctionBodyPtr, ModuleInfo, VMOffsets};
use memoffset::offset_of;
use more_asserts::assert_lt;
use std::alloc::{self, Layout};
use std::any::Any;
use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::convert::{TryFrom, TryInto};
use std::ptr::NonNull;
use std::sync::Arc;
use std::{mem, ptr, slice};
use wasmer_types::entity::{packed_option::ReservedValue, BoxedSlice, EntityRef, PrimaryMap};
use wasmer_types::{
DataIndex, DataInitializer, ElemIndex, ExportIndex, FunctionIndex, GlobalIndex, GlobalInit,
LocalFunctionIndex, LocalGlobalIndex, LocalMemoryIndex, LocalTableIndex, MemoryIndex, Pages,
SignatureIndex, TableIndex, TableInitializer,
};
cfg_if::cfg_if! {
if #[cfg(unix)] {
pub type SignalHandler = dyn Fn(libc::c_int, *const libc::siginfo_t, *const libc::c_void) -> bool;
impl InstanceHandle {
pub fn set_signal_handler<H>(&self, handler: H)
where
H: 'static + Fn(libc::c_int, *const libc::siginfo_t, *const libc::c_void) -> bool,
{
self.instance().signal_handler.set(Some(Box::new(handler)));
}
}
} else if #[cfg(target_os = "windows")] {
pub type SignalHandler = dyn Fn(winapi::um::winnt::PEXCEPTION_POINTERS) -> bool;
impl InstanceHandle {
pub fn set_signal_handler<H>(&self, handler: H)
where
H: 'static + Fn(winapi::um::winnt::PEXCEPTION_POINTERS) -> bool,
{
self.instance().signal_handler.set(Some(Box::new(handler)));
}
}
}
}
#[repr(C)]
pub(crate) struct Instance {
module: Arc<ModuleInfo>,
offsets: VMOffsets,
memories: BoxedSlice<LocalMemoryIndex, Arc<dyn Memory>>,
tables: BoxedSlice<LocalTableIndex, Arc<dyn Table>>,
globals: BoxedSlice<LocalGlobalIndex, Arc<Global>>,
functions: BoxedSlice<LocalFunctionIndex, FunctionBodyPtr>,
function_call_trampolines: BoxedSlice<SignatureIndex, VMTrampoline>,
passive_elements: RefCell<HashMap<ElemIndex, Box<[VMCallerCheckedAnyfunc]>>>,
passive_data: RefCell<HashMap<DataIndex, Arc<[u8]>>>,
host_state: Box<dyn Any>,
pub(crate) signal_handler: Cell<Option<Box<SignalHandler>>>,
vmctx: VMContext,
}
#[allow(clippy::cast_ptr_alignment)]
impl Instance {
unsafe fn vmctx_plus_offset<T>(&self, offset: u32) -> *mut T {
(self.vmctx_ptr() as *mut u8)
.add(usize::try_from(offset).unwrap())
.cast()
}
fn signature_id(&self, index: SignatureIndex) -> VMSharedSignatureIndex {
let index = usize::try_from(index.as_u32()).unwrap();
unsafe { *self.signature_ids_ptr().add(index) }
}
pub(crate) fn module(&self) -> &Arc<ModuleInfo> {
&self.module
}
pub(crate) fn module_ref(&self) -> &ModuleInfo {
&*self.module
}
fn signature_ids_ptr(&self) -> *mut VMSharedSignatureIndex {
unsafe { self.vmctx_plus_offset(self.offsets.vmctx_signature_ids_begin()) }
}
fn imported_function(&self, index: FunctionIndex) -> &VMFunctionImport {
let index = usize::try_from(index.as_u32()).unwrap();
unsafe { &*self.imported_functions_ptr().add(index) }
}
fn imported_functions_ptr(&self) -> *mut VMFunctionImport {
unsafe { self.vmctx_plus_offset(self.offsets.vmctx_imported_functions_begin()) }
}
fn imported_table(&self, index: TableIndex) -> &VMTableImport {
let index = usize::try_from(index.as_u32()).unwrap();
unsafe { &*self.imported_tables_ptr().add(index) }
}
fn imported_tables_ptr(&self) -> *mut VMTableImport {
unsafe { self.vmctx_plus_offset(self.offsets.vmctx_imported_tables_begin()) }
}
fn imported_memory(&self, index: MemoryIndex) -> &VMMemoryImport {
let index = usize::try_from(index.as_u32()).unwrap();
unsafe { &*self.imported_memories_ptr().add(index) }
}
fn imported_memories_ptr(&self) -> *mut VMMemoryImport {
unsafe { self.vmctx_plus_offset(self.offsets.vmctx_imported_memories_begin()) }
}
fn imported_global(&self, index: GlobalIndex) -> &VMGlobalImport {
let index = usize::try_from(index.as_u32()).unwrap();
unsafe { &*self.imported_globals_ptr().add(index) }
}
fn imported_globals_ptr(&self) -> *mut VMGlobalImport {
unsafe { self.vmctx_plus_offset(self.offsets.vmctx_imported_globals_begin()) }
}
#[allow(dead_code)]
fn table(&self, index: LocalTableIndex) -> VMTableDefinition {
unsafe { *self.table_ptr(index).as_ref() }
}
#[allow(dead_code)]
fn set_table(&self, index: LocalTableIndex, table: &VMTableDefinition) {
unsafe {
*self.table_ptr(index).as_ptr() = *table;
}
}
fn table_ptr(&self, index: LocalTableIndex) -> NonNull<VMTableDefinition> {
let index = usize::try_from(index.as_u32()).unwrap();
NonNull::new(unsafe { self.tables_ptr().add(index) }).unwrap()
}
fn tables_ptr(&self) -> *mut VMTableDefinition {
unsafe { self.vmctx_plus_offset(self.offsets.vmctx_tables_begin()) }
}
pub(crate) fn get_memory(&self, index: MemoryIndex) -> VMMemoryDefinition {
if let Some(local_index) = self.module.local_memory_index(index) {
self.memory(local_index)
} else {
let import = self.imported_memory(index);
unsafe { *import.definition.as_ref() }
}
}
fn memory(&self, index: LocalMemoryIndex) -> VMMemoryDefinition {
unsafe { *self.memory_ptr(index).as_ref() }
}
#[allow(dead_code)]
fn set_memory(&self, index: LocalMemoryIndex, mem: &VMMemoryDefinition) {
unsafe {
*self.memory_ptr(index).as_ptr() = *mem;
}
}
fn memory_ptr(&self, index: LocalMemoryIndex) -> NonNull<VMMemoryDefinition> {
let index = usize::try_from(index.as_u32()).unwrap();
NonNull::new(unsafe { self.memories_ptr().add(index) }).unwrap()
}
fn memories_ptr(&self) -> *mut VMMemoryDefinition {
unsafe { self.vmctx_plus_offset(self.offsets.vmctx_memories_begin()) }
}
fn global(&self, index: LocalGlobalIndex) -> VMGlobalDefinition {
unsafe { self.global_ptr(index).as_ref().clone() }
}
#[allow(dead_code)]
fn set_global(&self, index: LocalGlobalIndex, global: &VMGlobalDefinition) {
unsafe {
*self.global_ptr(index).as_ptr() = global.clone();
}
}
fn global_ptr(&self, index: LocalGlobalIndex) -> NonNull<VMGlobalDefinition> {
let index = usize::try_from(index.as_u32()).unwrap();
NonNull::new(unsafe { *self.globals_ptr().add(index) }).unwrap()
}
fn globals_ptr(&self) -> *mut *mut VMGlobalDefinition {
unsafe { self.vmctx_plus_offset(self.offsets.vmctx_globals_begin()) }
}
fn builtin_functions_ptr(&self) -> *mut VMBuiltinFunctionsArray {
unsafe { self.vmctx_plus_offset(self.offsets.vmctx_builtin_functions_begin()) }
}
pub fn vmctx(&self) -> &VMContext {
&self.vmctx
}
pub fn vmctx_ptr(&self) -> *mut VMContext {
self.vmctx() as *const VMContext as *mut VMContext
}
pub fn lookup(&self, field: &str) -> Option<Export> {
let export = if let Some(export) = self.module.exports.get(field) {
export.clone()
} else {
return None;
};
Some(self.lookup_by_declaration(&export))
}
pub fn lookup_by_declaration(&self, export: &ExportIndex) -> Export {
match export {
ExportIndex::Function(index) => {
let sig_index = &self.module.functions[*index];
let (address, vmctx) = if let Some(def_index) = self.module.local_func_index(*index)
{
(
self.functions[def_index].0 as *const _,
VMFunctionEnvironment {
vmctx: self.vmctx_ptr(),
},
)
} else {
let import = self.imported_function(*index);
(import.body, import.environment)
};
let call_trampoline = Some(self.function_call_trampolines[*sig_index]);
let signature = self.module.signatures[*sig_index].clone();
ExportFunction {
address,
kind: VMFunctionKind::Static,
signature,
vmctx,
call_trampoline,
}
.into()
}
ExportIndex::Table(index) => {
let from = if let Some(def_index) = self.module.local_table_index(*index) {
self.tables[def_index].clone()
} else {
let import = self.imported_table(*index);
import.from.clone()
};
ExportTable { from }.into()
}
ExportIndex::Memory(index) => {
let from = if let Some(def_index) = self.module.local_memory_index(*index) {
self.memories[def_index].clone()
} else {
let import = self.imported_memory(*index);
import.from.clone()
};
ExportMemory { from }.into()
}
ExportIndex::Global(index) => {
let from = {
if let Some(def_index) = self.module.local_global_index(*index) {
self.globals[def_index].clone()
} else {
let import = self.imported_global(*index);
import.from.clone()
}
};
ExportGlobal { from }.into()
}
}
}
pub fn exports(&self) -> indexmap::map::Iter<String, ExportIndex> {
self.module.exports.iter()
}
#[inline]
pub fn host_state(&self) -> &dyn Any {
&*self.host_state
}
fn invoke_start_function(&self) -> Result<(), Trap> {
let start_index = match self.module.start_function {
Some(idx) => idx,
None => return Ok(()),
};
let (callee_address, callee_vmctx) = match self.module.local_func_index(start_index) {
Some(local_index) => {
let body = self
.functions
.get(local_index)
.expect("function index is out of bounds")
.0;
(
body as *const _,
VMFunctionEnvironment {
vmctx: self.vmctx_ptr(),
},
)
}
None => {
assert_lt!(start_index.index(), self.module.num_imported_functions);
let import = self.imported_function(start_index);
(import.body, import.environment)
}
};
unsafe {
catch_traps(callee_vmctx, || {
mem::transmute::<*const VMFunctionBody, unsafe extern "C" fn(VMFunctionEnvironment)>(
callee_address,
)(callee_vmctx)
})
}
}
#[inline]
pub(crate) fn vmctx_offset() -> isize {
offset_of!(Self, vmctx) as isize
}
pub(crate) fn table_index(&self, table: &VMTableDefinition) -> LocalTableIndex {
let offsets = &self.offsets;
let begin = unsafe {
(&self.vmctx as *const VMContext as *const u8)
.add(usize::try_from(offsets.vmctx_tables_begin()).unwrap())
} as *const VMTableDefinition;
let end: *const VMTableDefinition = table;
let index = LocalTableIndex::new(
(end as usize - begin as usize) / mem::size_of::<VMTableDefinition>(),
);
assert_lt!(index.index(), self.tables.len());
index
}
pub(crate) fn memory_index(&self, memory: &VMMemoryDefinition) -> LocalMemoryIndex {
let offsets = &self.offsets;
let begin = unsafe {
(&self.vmctx as *const VMContext as *const u8)
.add(usize::try_from(offsets.vmctx_memories_begin()).unwrap())
} as *const VMMemoryDefinition;
let end: *const VMMemoryDefinition = memory;
let index = LocalMemoryIndex::new(
(end as usize - begin as usize) / mem::size_of::<VMMemoryDefinition>(),
);
assert_lt!(index.index(), self.memories.len());
index
}
pub(crate) fn memory_grow<IntoPages>(
&self,
memory_index: LocalMemoryIndex,
delta: IntoPages,
) -> Result<Pages, MemoryError>
where
IntoPages: Into<Pages>,
{
let mem = self
.memories
.get(memory_index)
.unwrap_or_else(|| panic!("no memory for index {}", memory_index.index()));
let result = mem.grow(delta.into());
result
}
pub(crate) unsafe fn imported_memory_grow<IntoPages>(
&self,
memory_index: MemoryIndex,
delta: IntoPages,
) -> Result<Pages, MemoryError>
where
IntoPages: Into<Pages>,
{
let import = self.imported_memory(memory_index);
let from = import.from.as_ref();
from.grow(delta.into())
}
pub(crate) fn memory_size(&self, memory_index: LocalMemoryIndex) -> Pages {
self.memories
.get(memory_index)
.unwrap_or_else(|| panic!("no memory for index {}", memory_index.index()))
.size()
}
pub(crate) unsafe fn imported_memory_size(&self, memory_index: MemoryIndex) -> Pages {
let import = self.imported_memory(memory_index);
let from = import.from.as_ref();
from.size()
}
pub(crate) fn table_grow(&self, table_index: LocalTableIndex, delta: u32) -> Option<u32> {
let result = self
.tables
.get(table_index)
.unwrap_or_else(|| panic!("no table for index {}", table_index.index()))
.grow(delta);
result
}
fn table_get(
&self,
table_index: LocalTableIndex,
index: u32,
) -> Option<VMCallerCheckedAnyfunc> {
self.tables
.get(table_index)
.unwrap_or_else(|| panic!("no table for index {}", table_index.index()))
.get(index)
}
fn table_set(
&self,
table_index: LocalTableIndex,
index: u32,
val: VMCallerCheckedAnyfunc,
) -> Result<(), Trap> {
self.tables
.get(table_index)
.unwrap_or_else(|| panic!("no table for index {}", table_index.index()))
.set(index, val)
}
fn alloc_layout(offsets: &VMOffsets) -> Layout {
let size = mem::size_of::<Self>()
.checked_add(usize::try_from(offsets.size_of_vmctx()).unwrap())
.unwrap();
let align = mem::align_of::<Self>();
Layout::from_size_align(size, align).unwrap()
}
fn get_caller_checked_anyfunc(&self, index: FunctionIndex) -> VMCallerCheckedAnyfunc {
if index == FunctionIndex::reserved_value() {
return VMCallerCheckedAnyfunc::default();
}
let sig = self.module.functions[index];
let type_index = self.signature_id(sig);
let (func_ptr, vmctx) = if let Some(def_index) = self.module.local_func_index(index) {
(
self.functions[def_index].0 as *const _,
VMFunctionEnvironment {
vmctx: self.vmctx_ptr(),
},
)
} else {
let import = self.imported_function(index);
(import.body, import.environment)
};
VMCallerCheckedAnyfunc {
func_ptr,
type_index,
vmctx,
}
}
pub(crate) fn table_init(
&self,
table_index: TableIndex,
elem_index: ElemIndex,
dst: u32,
src: u32,
len: u32,
) -> Result<(), Trap> {
let table = self.get_table(table_index);
let passive_elements = self.passive_elements.borrow();
let elem = passive_elements
.get(&elem_index)
.map_or_else(|| -> &[VMCallerCheckedAnyfunc] { &[] }, |e| &**e);
if src
.checked_add(len)
.map_or(true, |n| n as usize > elem.len())
|| dst.checked_add(len).map_or(true, |m| m > table.size())
{
return Err(Trap::new_from_runtime(TrapCode::TableAccessOutOfBounds));
}
for (dst, src) in (dst..dst + len).zip(src..src + len) {
table
.set(dst, elem[src as usize].clone())
.expect("should never panic because we already did the bounds check above");
}
Ok(())
}
pub(crate) fn elem_drop(&self, elem_index: ElemIndex) {
let mut passive_elements = self.passive_elements.borrow_mut();
passive_elements.remove(&elem_index);
}
pub(crate) fn local_memory_copy(
&self,
memory_index: LocalMemoryIndex,
dst: u32,
src: u32,
len: u32,
) -> Result<(), Trap> {
let memory = self.memory(memory_index);
unsafe { memory.memory_copy(dst, src, len) }
}
pub(crate) fn imported_memory_copy(
&self,
memory_index: MemoryIndex,
dst: u32,
src: u32,
len: u32,
) -> Result<(), Trap> {
let import = self.imported_memory(memory_index);
let memory = unsafe { import.definition.as_ref() };
unsafe { memory.memory_copy(dst, src, len) }
}
pub(crate) fn local_memory_fill(
&self,
memory_index: LocalMemoryIndex,
dst: u32,
val: u32,
len: u32,
) -> Result<(), Trap> {
let memory = self.memory(memory_index);
unsafe { memory.memory_fill(dst, val, len) }
}
pub(crate) fn imported_memory_fill(
&self,
memory_index: MemoryIndex,
dst: u32,
val: u32,
len: u32,
) -> Result<(), Trap> {
let import = self.imported_memory(memory_index);
let memory = unsafe { import.definition.as_ref() };
unsafe { memory.memory_fill(dst, val, len) }
}
pub(crate) fn memory_init(
&self,
memory_index: MemoryIndex,
data_index: DataIndex,
dst: u32,
src: u32,
len: u32,
) -> Result<(), Trap> {
let memory = self.get_memory(memory_index);
let passive_data = self.passive_data.borrow();
let data = passive_data
.get(&data_index)
.map_or(&[][..], |data| &**data);
if src
.checked_add(len)
.map_or(true, |n| n as usize > data.len())
|| dst
.checked_add(len)
.map_or(true, |m| m > memory.current_length)
{
return Err(Trap::new_from_runtime(TrapCode::HeapAccessOutOfBounds));
}
let src_slice = &data[src as usize..(src + len) as usize];
unsafe {
let dst_start = memory.base.add(dst as usize);
let dst_slice = slice::from_raw_parts_mut(dst_start, len as usize);
dst_slice.copy_from_slice(src_slice);
}
Ok(())
}
pub(crate) fn data_drop(&self, data_index: DataIndex) {
let mut passive_data = self.passive_data.borrow_mut();
passive_data.remove(&data_index);
}
pub(crate) fn get_table(&self, table_index: TableIndex) -> &dyn Table {
if let Some(local_table_index) = self.module.local_table_index(table_index) {
self.get_local_table(local_table_index)
} else {
self.get_foreign_table(table_index)
}
}
pub(crate) fn get_local_table(&self, index: LocalTableIndex) -> &dyn Table {
self.tables[index].as_ref()
}
pub(crate) fn get_foreign_table(&self, index: TableIndex) -> &dyn Table {
let import = self.imported_table(index);
&*import.from
}
}
#[derive(Hash, PartialEq, Eq)]
pub struct InstanceHandle {
instance: *mut Instance,
}
unsafe impl Send for InstanceHandle {}
impl InstanceHandle {
pub fn allocate_instance(module: &ModuleInfo) -> (NonNull<u8>, VMOffsets) {
let offsets = VMOffsets::new(mem::size_of::<*const u8>() as u8, module);
let layout = Instance::alloc_layout(&offsets);
#[allow(clippy::cast_ptr_alignment)]
let instance_ptr = unsafe { alloc::alloc(layout) as *mut Instance };
let ptr = if let Some(ptr) = NonNull::new(instance_ptr) {
ptr.cast()
} else {
alloc::handle_alloc_error(layout);
};
(ptr, offsets)
}
pub unsafe fn memory_definition_locations(
instance_ptr: NonNull<u8>,
offsets: &VMOffsets,
) -> Vec<NonNull<VMMemoryDefinition>> {
let num_memories = offsets.num_local_memories;
let num_memories = usize::try_from(num_memories).unwrap();
let mut out = Vec::with_capacity(num_memories);
let base_ptr = instance_ptr.as_ptr().add(std::mem::size_of::<Instance>());
for i in 0..num_memories {
let mem_offset = offsets.vmctx_vmmemory_definition(LocalMemoryIndex::new(i));
let mem_offset = usize::try_from(mem_offset).unwrap();
let new_ptr = NonNull::new_unchecked(base_ptr.add(mem_offset));
out.push(new_ptr.cast());
}
out
}
pub unsafe fn table_definition_locations(
instance_ptr: NonNull<u8>,
offsets: &VMOffsets,
) -> Vec<NonNull<VMTableDefinition>> {
let num_tables = offsets.num_local_tables;
let num_tables = usize::try_from(num_tables).unwrap();
let mut out = Vec::with_capacity(num_tables);
let base_ptr = instance_ptr.as_ptr().add(std::mem::size_of::<Instance>());
for i in 0..num_tables {
let table_offset = offsets.vmctx_vmtable_definition(LocalTableIndex::new(i));
let table_offset = usize::try_from(table_offset).unwrap();
let new_ptr = NonNull::new_unchecked(base_ptr.add(table_offset));
out.push(new_ptr.cast());
}
out
}
#[allow(clippy::too_many_arguments)]
pub unsafe fn new(
instance_ptr: NonNull<u8>,
offsets: VMOffsets,
module: Arc<ModuleInfo>,
finished_functions: BoxedSlice<LocalFunctionIndex, FunctionBodyPtr>,
finished_function_call_trampolines: BoxedSlice<SignatureIndex, VMTrampoline>,
finished_memories: BoxedSlice<LocalMemoryIndex, Arc<dyn Memory>>,
finished_tables: BoxedSlice<LocalTableIndex, Arc<dyn Table>>,
finished_globals: BoxedSlice<LocalGlobalIndex, Arc<Global>>,
imports: Imports,
vmshared_signatures: BoxedSlice<SignatureIndex, VMSharedSignatureIndex>,
host_state: Box<dyn Any>,
) -> Result<Self, Trap> {
let instance_ptr = instance_ptr.cast::<Instance>().as_ptr();
let vmctx_globals = finished_globals
.values()
.map(|m| m.vmglobal())
.collect::<PrimaryMap<LocalGlobalIndex, _>>()
.into_boxed_slice();
let passive_data = RefCell::new(module.passive_data.clone());
let handle = {
let instance = Instance {
module,
offsets,
memories: finished_memories,
tables: finished_tables,
globals: finished_globals,
functions: finished_functions,
function_call_trampolines: finished_function_call_trampolines,
passive_elements: Default::default(),
passive_data,
host_state,
signal_handler: Cell::new(None),
vmctx: VMContext {},
};
ptr::write(instance_ptr, instance);
Self {
instance: instance_ptr,
}
};
let instance = handle.instance();
ptr::copy(
vmshared_signatures.values().as_slice().as_ptr(),
instance.signature_ids_ptr() as *mut VMSharedSignatureIndex,
vmshared_signatures.len(),
);
ptr::copy(
imports.functions.values().as_slice().as_ptr(),
instance.imported_functions_ptr() as *mut VMFunctionImport,
imports.functions.len(),
);
ptr::copy(
imports.tables.values().as_slice().as_ptr(),
instance.imported_tables_ptr() as *mut VMTableImport,
imports.tables.len(),
);
ptr::copy(
imports.memories.values().as_slice().as_ptr(),
instance.imported_memories_ptr() as *mut VMMemoryImport,
imports.memories.len(),
);
ptr::copy(
imports.globals.values().as_slice().as_ptr(),
instance.imported_globals_ptr() as *mut VMGlobalImport,
imports.globals.len(),
);
ptr::copy(
vmctx_globals.values().as_slice().as_ptr(),
instance.globals_ptr() as *mut NonNull<VMGlobalDefinition>,
vmctx_globals.len(),
);
ptr::write(
instance.builtin_functions_ptr() as *mut VMBuiltinFunctionsArray,
VMBuiltinFunctionsArray::initialized(),
);
init_traps();
initialize_passive_elements(instance);
initialize_globals(instance);
Ok(handle)
}
pub unsafe fn finish_instantiation(
&self,
data_initializers: &[DataInitializer<'_>],
) -> Result<(), Trap> {
check_table_init_bounds(self.instance())?;
check_memory_init_bounds(self.instance(), data_initializers)?;
initialize_tables(self.instance())?;
initialize_memories(self.instance(), data_initializers)?;
self.instance().invoke_start_function()?;
Ok(())
}
pub unsafe fn from_vmctx(vmctx: *mut VMContext) -> Self {
let instance = (&*vmctx).instance();
Self {
instance: instance as *const Instance as *mut Instance,
}
}
pub fn vmctx(&self) -> &VMContext {
self.instance().vmctx()
}
pub fn vmctx_ptr(&self) -> *mut VMContext {
self.instance().vmctx_ptr()
}
pub fn module(&self) -> &Arc<ModuleInfo> {
self.instance().module()
}
pub fn module_ref(&self) -> &ModuleInfo {
self.instance().module_ref()
}
pub fn lookup(&self, field: &str) -> Option<Export> {
self.instance().lookup(field)
}
pub fn lookup_by_declaration(&self, export: &ExportIndex) -> Export {
self.instance().lookup_by_declaration(export)
}
pub fn exports(&self) -> indexmap::map::Iter<String, ExportIndex> {
self.instance().exports()
}
pub fn host_state(&self) -> &dyn Any {
self.instance().host_state()
}
pub fn memory_index(&self, memory: &VMMemoryDefinition) -> LocalMemoryIndex {
self.instance().memory_index(memory)
}
pub fn memory_grow<IntoPages>(
&self,
memory_index: LocalMemoryIndex,
delta: IntoPages,
) -> Result<Pages, MemoryError>
where
IntoPages: Into<Pages>,
{
self.instance().memory_grow(memory_index, delta)
}
pub fn table_index(&self, table: &VMTableDefinition) -> LocalTableIndex {
self.instance().table_index(table)
}
pub fn table_grow(&self, table_index: LocalTableIndex, delta: u32) -> Option<u32> {
self.instance().table_grow(table_index, delta)
}
pub fn table_get(
&self,
table_index: LocalTableIndex,
index: u32,
) -> Option<VMCallerCheckedAnyfunc> {
self.instance().table_get(table_index, index)
}
pub fn table_set(
&self,
table_index: LocalTableIndex,
index: u32,
val: VMCallerCheckedAnyfunc,
) -> Result<(), Trap> {
self.instance().table_set(table_index, index, val)
}
pub fn get_local_table(&self, index: LocalTableIndex) -> &dyn Table {
self.instance().get_local_table(index)
}
pub(crate) fn instance(&self) -> &Instance {
unsafe { &*(self.instance as *const Instance) }
}
pub unsafe fn dealloc(&self) {
let instance = self.instance();
let layout = Instance::alloc_layout(&instance.offsets);
ptr::drop_in_place(self.instance);
alloc::dealloc(self.instance.cast(), layout);
}
}
impl Clone for InstanceHandle {
fn clone(&self) -> Self {
Self {
instance: self.instance,
}
}
}
fn check_table_init_bounds(instance: &Instance) -> Result<(), Trap> {
let module = Arc::clone(&instance.module);
for init in &module.table_initializers {
let start = get_table_init_start(init, instance);
let table = instance.get_table(init.table_index);
let size = usize::try_from(table.size()).unwrap();
if size < start + init.elements.len() {
return Err(Trap::new_from_runtime(TrapCode::TableSetterOutOfBounds));
}
}
Ok(())
}
fn get_memory_init_start(init: &DataInitializer<'_>, instance: &Instance) -> usize {
let mut start = init.location.offset;
if let Some(base) = init.location.base {
let val = unsafe {
if let Some(def_index) = instance.module.local_global_index(base) {
instance.global(def_index).to_u32()
} else {
instance.imported_global(base).definition.as_ref().to_u32()
}
};
start += usize::try_from(val).unwrap();
}
start
}
#[allow(clippy::mut_from_ref)]
unsafe fn get_memory_slice<'instance>(
init: &DataInitializer<'_>,
instance: &'instance Instance,
) -> &'instance mut [u8] {
let memory = if let Some(local_memory_index) = instance
.module
.local_memory_index(init.location.memory_index)
{
instance.memory(local_memory_index)
} else {
let import = instance.imported_memory(init.location.memory_index);
*import.definition.as_ref()
};
slice::from_raw_parts_mut(memory.base, memory.current_length.try_into().unwrap())
}
fn check_memory_init_bounds(
instance: &Instance,
data_initializers: &[DataInitializer<'_>],
) -> Result<(), Trap> {
for init in data_initializers {
let start = get_memory_init_start(init, instance);
unsafe {
let mem_slice = get_memory_slice(init, instance);
if mem_slice.get_mut(start..start + init.data.len()).is_none() {
return Err(Trap::new_from_runtime(TrapCode::HeapSetterOutOfBounds));
}
}
}
Ok(())
}
fn get_table_init_start(init: &TableInitializer, instance: &Instance) -> usize {
let mut start = init.offset;
if let Some(base) = init.base {
let val = unsafe {
if let Some(def_index) = instance.module.local_global_index(base) {
instance.global(def_index).to_u32()
} else {
instance.imported_global(base).definition.as_ref().to_u32()
}
};
start += usize::try_from(val).unwrap();
}
start
}
fn initialize_tables(instance: &Instance) -> Result<(), Trap> {
let module = Arc::clone(&instance.module);
for init in &module.table_initializers {
let start = get_table_init_start(init, instance);
let table = instance.get_table(init.table_index);
if start
.checked_add(init.elements.len())
.map_or(true, |end| end > table.size() as usize)
{
return Err(Trap::new_from_runtime(TrapCode::TableAccessOutOfBounds));
}
for (i, func_idx) in init.elements.iter().enumerate() {
let anyfunc = instance.get_caller_checked_anyfunc(*func_idx);
table
.set(u32::try_from(start + i).unwrap(), anyfunc)
.unwrap();
}
}
Ok(())
}
fn initialize_passive_elements(instance: &Instance) {
let mut passive_elements = instance.passive_elements.borrow_mut();
debug_assert!(
passive_elements.is_empty(),
"should only be called once, at initialization time"
);
passive_elements.extend(
instance
.module
.passive_elements
.iter()
.filter(|(_, segments)| !segments.is_empty())
.map(|(idx, segments)| {
(
*idx,
segments
.iter()
.map(|s| instance.get_caller_checked_anyfunc(*s))
.collect(),
)
}),
);
}
fn initialize_memories(
instance: &Instance,
data_initializers: &[DataInitializer<'_>],
) -> Result<(), Trap> {
for init in data_initializers {
let memory = instance.get_memory(init.location.memory_index);
let start = get_memory_init_start(init, instance);
if start
.checked_add(init.data.len())
.map_or(true, |end| end > memory.current_length.try_into().unwrap())
{
return Err(Trap::new_from_runtime(TrapCode::HeapAccessOutOfBounds));
}
unsafe {
let mem_slice = get_memory_slice(init, instance);
let end = start + init.data.len();
let to_init = &mut mem_slice[start..end];
to_init.copy_from_slice(init.data);
}
}
Ok(())
}
fn initialize_globals(instance: &Instance) {
let module = Arc::clone(&instance.module);
for (index, initializer) in module.global_initializers.iter() {
unsafe {
let to = instance.global_ptr(index).as_ptr();
match initializer {
GlobalInit::I32Const(x) => *(*to).as_i32_mut() = *x,
GlobalInit::I64Const(x) => *(*to).as_i64_mut() = *x,
GlobalInit::F32Const(x) => *(*to).as_f32_mut() = *x,
GlobalInit::F64Const(x) => *(*to).as_f64_mut() = *x,
GlobalInit::V128Const(x) => *(*to).as_bytes_mut() = *x.bytes(),
GlobalInit::GetGlobal(x) => {
let from: VMGlobalDefinition =
if let Some(def_x) = module.local_global_index(*x) {
instance.global(def_x)
} else {
instance.imported_global(*x).definition.as_ref().clone()
};
*to = from;
}
GlobalInit::RefNullConst | GlobalInit::RefFunc(_) => unimplemented!(),
}
}
}
}