#![allow(missing_docs)] use crate::func_data_registry::VMFuncRef;
use crate::probestack::PROBESTACK;
use crate::table::{RawTableElement, TableElement};
use crate::trap::{raise_lib_trap, Trap, TrapCode};
use crate::vmcontext::VMContext;
use crate::VMExternRef;
use std::fmt;
use wasmer_types::{
DataIndex, ElemIndex, FunctionIndex, LocalMemoryIndex, LocalTableIndex, MemoryIndex,
TableIndex, Type,
};
#[no_mangle]
pub extern "C" fn wasmer_vm_f32_ceil(x: f32) -> f32 {
x.ceil()
}
#[no_mangle]
pub extern "C" fn wasmer_vm_f32_floor(x: f32) -> f32 {
x.floor()
}
#[no_mangle]
pub extern "C" fn wasmer_vm_f32_trunc(x: f32) -> f32 {
x.trunc()
}
#[allow(clippy::float_arithmetic, clippy::float_cmp)]
#[no_mangle]
pub extern "C" fn wasmer_vm_f32_nearest(x: f32) -> f32 {
if x == 0.0 {
x
} else {
let u = x.ceil();
let d = x.floor();
let um = (x - u).abs();
let dm = (x - d).abs();
if um < dm
|| (um == dm && {
let h = u / 2.;
h.floor() == h
})
{
u
} else {
d
}
}
}
#[no_mangle]
pub extern "C" fn wasmer_vm_f64_ceil(x: f64) -> f64 {
x.ceil()
}
#[no_mangle]
pub extern "C" fn wasmer_vm_f64_floor(x: f64) -> f64 {
x.floor()
}
#[no_mangle]
pub extern "C" fn wasmer_vm_f64_trunc(x: f64) -> f64 {
x.trunc()
}
#[allow(clippy::float_arithmetic, clippy::float_cmp)]
#[no_mangle]
pub extern "C" fn wasmer_vm_f64_nearest(x: f64) -> f64 {
if x == 0.0 {
x
} else {
let u = x.ceil();
let d = x.floor();
let um = (x - u).abs();
let dm = (x - d).abs();
if um < dm
|| (um == dm && {
let h = u / 2.;
h.floor() == h
})
{
u
} else {
d
}
}
}
#[no_mangle]
pub unsafe extern "C" fn wasmer_vm_memory32_grow(
vmctx: *mut VMContext,
delta: u32,
memory_index: u32,
) -> u32 {
let instance = (&*vmctx).instance();
let memory_index = LocalMemoryIndex::from_u32(memory_index);
instance
.memory_grow(memory_index, delta)
.map(|pages| pages.0)
.unwrap_or(u32::max_value())
}
#[no_mangle]
pub unsafe extern "C" fn wasmer_vm_imported_memory32_grow(
vmctx: *mut VMContext,
delta: u32,
memory_index: u32,
) -> u32 {
let instance = (&*vmctx).instance();
let memory_index = MemoryIndex::from_u32(memory_index);
instance
.imported_memory_grow(memory_index, delta)
.map(|pages| pages.0)
.unwrap_or(u32::max_value())
}
#[no_mangle]
pub unsafe extern "C" fn wasmer_vm_memory32_size(vmctx: *mut VMContext, memory_index: u32) -> u32 {
let instance = (&*vmctx).instance();
let memory_index = LocalMemoryIndex::from_u32(memory_index);
instance.memory_size(memory_index).0
}
#[no_mangle]
pub unsafe extern "C" fn wasmer_vm_imported_memory32_size(
vmctx: *mut VMContext,
memory_index: u32,
) -> u32 {
let instance = (&*vmctx).instance();
let memory_index = MemoryIndex::from_u32(memory_index);
instance.imported_memory_size(memory_index).0
}
#[no_mangle]
pub unsafe extern "C" fn wasmer_vm_table_copy(
vmctx: *mut VMContext,
dst_table_index: u32,
src_table_index: u32,
dst: u32,
src: u32,
len: u32,
) {
let result = {
let dst_table_index = TableIndex::from_u32(dst_table_index);
let src_table_index = TableIndex::from_u32(src_table_index);
let instance = (&*vmctx).instance();
let dst_table = instance.get_table(dst_table_index);
let src_table = instance.get_table(src_table_index);
dst_table.copy(src_table, dst, src, len)
};
if let Err(trap) = result {
raise_lib_trap(trap);
}
}
#[no_mangle]
pub unsafe extern "C" fn wasmer_vm_table_init(
vmctx: *mut VMContext,
table_index: u32,
elem_index: u32,
dst: u32,
src: u32,
len: u32,
) {
let result = {
let table_index = TableIndex::from_u32(table_index);
let elem_index = ElemIndex::from_u32(elem_index);
let instance = (&*vmctx).instance();
instance.table_init(table_index, elem_index, dst, src, len)
};
if let Err(trap) = result {
raise_lib_trap(trap);
}
}
#[no_mangle]
pub unsafe extern "C" fn wasmer_vm_table_fill(
vmctx: *mut VMContext,
table_index: u32,
start_idx: u32,
item: RawTableElement,
len: u32,
) {
let result = {
let table_index = TableIndex::from_u32(table_index);
let instance = (&*vmctx).instance();
let elem = match instance.get_table(table_index).ty().ty {
Type::ExternRef => TableElement::ExternRef(item.extern_ref.into()),
Type::FuncRef => TableElement::FuncRef(item.func_ref),
_ => panic!("Unrecognized table type: does not contain references"),
};
instance.table_fill(table_index, start_idx, elem, len)
};
if let Err(trap) = result {
raise_lib_trap(trap);
}
}
#[no_mangle]
pub unsafe extern "C" fn wasmer_vm_table_size(vmctx: *mut VMContext, table_index: u32) -> u32 {
let instance = (&*vmctx).instance();
let table_index = LocalTableIndex::from_u32(table_index);
instance.table_size(table_index)
}
#[no_mangle]
pub unsafe extern "C" fn wasmer_vm_imported_table_size(
vmctx: *mut VMContext,
table_index: u32,
) -> u32 {
let instance = (&*vmctx).instance();
let table_index = TableIndex::from_u32(table_index);
instance.imported_table_size(table_index)
}
#[no_mangle]
pub unsafe extern "C" fn wasmer_vm_table_get(
vmctx: *mut VMContext,
table_index: u32,
elem_index: u32,
) -> RawTableElement {
let instance = (&*vmctx).instance();
let table_index = LocalTableIndex::from_u32(table_index);
match instance.table_get(table_index, elem_index) {
Some(table_ref) => table_ref.into(),
None => raise_lib_trap(Trap::lib(TrapCode::TableAccessOutOfBounds)),
}
}
#[no_mangle]
pub unsafe extern "C" fn wasmer_vm_imported_table_get(
vmctx: *mut VMContext,
table_index: u32,
elem_index: u32,
) -> RawTableElement {
let instance = (&*vmctx).instance();
let table_index = TableIndex::from_u32(table_index);
match instance.imported_table_get(table_index, elem_index) {
Some(table_ref) => table_ref.into(),
None => raise_lib_trap(Trap::lib(TrapCode::TableAccessOutOfBounds)),
}
}
#[no_mangle]
pub unsafe extern "C" fn wasmer_vm_table_set(
vmctx: *mut VMContext,
table_index: u32,
elem_index: u32,
value: RawTableElement,
) {
let instance = (&*vmctx).instance();
let table_index = TableIndex::from_u32(table_index);
if let Ok(local_table) = instance
.artifact
.import_counts()
.local_table_index(table_index)
{
let elem = match instance.get_local_table(local_table).ty().ty {
Type::ExternRef => TableElement::ExternRef(value.extern_ref.into()),
Type::FuncRef => TableElement::FuncRef(value.func_ref),
_ => panic!("Unrecognized table type: does not contain references"),
};
let result = instance.table_set(local_table, elem_index, elem);
if let Err(trap) = result {
raise_lib_trap(trap);
}
} else {
panic!("wasmer_vm_imported_table_set should have been called");
}
}
#[no_mangle]
pub unsafe extern "C" fn wasmer_vm_imported_table_set(
vmctx: *mut VMContext,
table_index: u32,
elem_index: u32,
value: RawTableElement,
) {
let instance = (&*vmctx).instance();
let table_index = TableIndex::from_u32(table_index);
let elem = match instance.get_foreign_table(table_index).ty().ty {
Type::ExternRef => TableElement::ExternRef(value.extern_ref.into()),
Type::FuncRef => TableElement::FuncRef(value.func_ref),
_ => panic!("Unrecognized table type: does not contain references"),
};
let result = instance.imported_table_set(table_index, elem_index, elem);
if let Err(trap) = result {
raise_lib_trap(trap);
}
}
#[no_mangle]
pub unsafe extern "C" fn wasmer_vm_table_grow(
vmctx: *mut VMContext,
init_value: RawTableElement,
delta: u32,
table_index: u32,
) -> u32 {
let instance = (&*vmctx).instance();
let table_index = LocalTableIndex::from_u32(table_index);
let init_value = match instance.get_local_table(table_index).ty().ty {
Type::ExternRef => TableElement::ExternRef(init_value.extern_ref.into()),
Type::FuncRef => TableElement::FuncRef(init_value.func_ref),
_ => panic!("Unrecognized table type: does not contain references"),
};
instance
.table_grow(table_index, delta, init_value)
.unwrap_or(u32::max_value())
}
#[no_mangle]
pub unsafe extern "C" fn wasmer_vm_imported_table_grow(
vmctx: *mut VMContext,
init_value: RawTableElement,
delta: u32,
table_index: u32,
) -> u32 {
let instance = (&*vmctx).instance();
let table_index = TableIndex::from_u32(table_index);
let init_value = match instance.get_table(table_index).ty().ty {
Type::ExternRef => TableElement::ExternRef(init_value.extern_ref.into()),
Type::FuncRef => TableElement::FuncRef(init_value.func_ref),
_ => panic!("Unrecognized table type: does not contain references"),
};
instance
.imported_table_grow(table_index, delta, init_value)
.unwrap_or(u32::max_value())
}
#[no_mangle]
pub unsafe extern "C" fn wasmer_vm_func_ref(
vmctx: *mut VMContext,
function_index: u32,
) -> VMFuncRef {
let instance = (&*vmctx).instance();
let function_index = FunctionIndex::from_u32(function_index);
instance.func_ref(function_index).unwrap()
}
#[no_mangle]
pub unsafe extern "C" fn wasmer_vm_externref_inc(externref: VMExternRef) {
externref.ref_clone();
}
#[no_mangle]
pub unsafe extern "C" fn wasmer_vm_externref_dec(mut externref: VMExternRef) {
externref.ref_drop()
}
#[no_mangle]
pub unsafe extern "C" fn wasmer_vm_elem_drop(vmctx: *mut VMContext, elem_index: u32) {
let elem_index = ElemIndex::from_u32(elem_index);
let instance = (&*vmctx).instance();
instance.elem_drop(elem_index);
}
#[no_mangle]
pub unsafe extern "C" fn wasmer_vm_memory32_copy(
vmctx: *mut VMContext,
memory_index: u32,
dst: u32,
src: u32,
len: u32,
) {
let result = {
let memory_index = LocalMemoryIndex::from_u32(memory_index);
let instance = (&*vmctx).instance();
instance.local_memory_copy(memory_index, dst, src, len)
};
if let Err(trap) = result {
raise_lib_trap(trap);
}
}
#[no_mangle]
pub unsafe extern "C" fn wasmer_vm_imported_memory32_copy(
vmctx: *mut VMContext,
memory_index: u32,
dst: u32,
src: u32,
len: u32,
) {
let result = {
let memory_index = MemoryIndex::from_u32(memory_index);
let instance = (&*vmctx).instance();
instance.imported_memory_copy(memory_index, dst, src, len)
};
if let Err(trap) = result {
raise_lib_trap(trap);
}
}
#[no_mangle]
pub unsafe extern "C" fn wasmer_vm_memory32_fill(
vmctx: *mut VMContext,
memory_index: u32,
dst: u32,
val: u32,
len: u32,
) {
let result = {
let memory_index = LocalMemoryIndex::from_u32(memory_index);
let instance = (&*vmctx).instance();
instance.local_memory_fill(memory_index, dst, val, len)
};
if let Err(trap) = result {
raise_lib_trap(trap);
}
}
#[no_mangle]
pub unsafe extern "C" fn wasmer_vm_imported_memory32_fill(
vmctx: *mut VMContext,
memory_index: u32,
dst: u32,
val: u32,
len: u32,
) {
let result = {
let memory_index = MemoryIndex::from_u32(memory_index);
let instance = (&*vmctx).instance();
instance.imported_memory_fill(memory_index, dst, val, len)
};
if let Err(trap) = result {
raise_lib_trap(trap);
}
}
#[no_mangle]
pub unsafe extern "C" fn wasmer_vm_memory32_init(
vmctx: *mut VMContext,
memory_index: u32,
data_index: u32,
dst: u32,
src: u32,
len: u32,
) {
let result = {
let memory_index = MemoryIndex::from_u32(memory_index);
let data_index = DataIndex::from_u32(data_index);
let instance = (&*vmctx).instance();
instance.memory_init(memory_index, data_index, dst, src, len)
};
if let Err(trap) = result {
raise_lib_trap(trap);
}
}
#[no_mangle]
pub unsafe extern "C" fn wasmer_vm_data_drop(vmctx: *mut VMContext, data_index: u32) {
let data_index = DataIndex::from_u32(data_index);
let instance = (&*vmctx).instance();
instance.data_drop(data_index)
}
#[no_mangle]
pub unsafe extern "C" fn wasmer_vm_raise_trap(trap_code: TrapCode) -> ! {
let trap = Trap::lib(trap_code);
raise_lib_trap(trap)
}
#[no_mangle]
pub static wasmer_vm_probestack: unsafe extern "C" fn() = PROBESTACK;
#[derive(
rkyv::Serialize, rkyv::Deserialize, rkyv::Archive, Copy, Clone, Debug, PartialEq, Eq, Hash,
)]
pub enum LibCall {
CeilF32,
CeilF64,
FloorF32,
FloorF64,
NearestF32,
NearestF64,
TruncF32,
TruncF64,
Memory32Size,
ImportedMemory32Size,
TableCopy,
TableInit,
TableFill,
TableSize,
ImportedTableSize,
TableGet,
ImportedTableGet,
TableSet,
ImportedTableSet,
TableGrow,
ImportedTableGrow,
FuncRef,
ElemDrop,
Memory32Copy,
ImportedMemory32Copy,
Memory32Fill,
ImportedMemory32Fill,
Memory32Init,
DataDrop,
RaiseTrap,
Probestack,
}
impl LibCall {
pub fn function_pointer(self) -> usize {
match self {
Self::CeilF32 => wasmer_vm_f32_ceil as usize,
Self::CeilF64 => wasmer_vm_f64_ceil as usize,
Self::FloorF32 => wasmer_vm_f32_floor as usize,
Self::FloorF64 => wasmer_vm_f64_floor as usize,
Self::NearestF32 => wasmer_vm_f32_nearest as usize,
Self::NearestF64 => wasmer_vm_f64_nearest as usize,
Self::TruncF32 => wasmer_vm_f32_trunc as usize,
Self::TruncF64 => wasmer_vm_f64_trunc as usize,
Self::Memory32Size => wasmer_vm_memory32_size as usize,
Self::ImportedMemory32Size => wasmer_vm_imported_memory32_size as usize,
Self::TableCopy => wasmer_vm_table_copy as usize,
Self::TableInit => wasmer_vm_table_init as usize,
Self::TableFill => wasmer_vm_table_fill as usize,
Self::TableSize => wasmer_vm_table_size as usize,
Self::ImportedTableSize => wasmer_vm_imported_table_size as usize,
Self::TableGet => wasmer_vm_table_get as usize,
Self::ImportedTableGet => wasmer_vm_imported_table_get as usize,
Self::TableSet => wasmer_vm_table_set as usize,
Self::ImportedTableSet => wasmer_vm_imported_table_set as usize,
Self::TableGrow => wasmer_vm_table_grow as usize,
Self::ImportedTableGrow => wasmer_vm_imported_table_grow as usize,
Self::FuncRef => wasmer_vm_func_ref as usize,
Self::ElemDrop => wasmer_vm_elem_drop as usize,
Self::Memory32Copy => wasmer_vm_memory32_copy as usize,
Self::ImportedMemory32Copy => wasmer_vm_imported_memory32_copy as usize,
Self::Memory32Fill => wasmer_vm_memory32_fill as usize,
Self::ImportedMemory32Fill => wasmer_vm_memory32_fill as usize,
Self::Memory32Init => wasmer_vm_memory32_init as usize,
Self::DataDrop => wasmer_vm_data_drop as usize,
Self::Probestack => wasmer_vm_probestack as usize,
Self::RaiseTrap => wasmer_vm_raise_trap as usize,
}
}
pub fn to_function_name(&self) -> &str {
match self {
Self::CeilF32 => "wasmer_vm_f32_ceil",
Self::CeilF64 => "wasmer_vm_f64_ceil",
Self::FloorF32 => "wasmer_vm_f32_floor",
Self::FloorF64 => "wasmer_vm_f64_floor",
Self::NearestF32 => "wasmer_vm_f32_nearest",
Self::NearestF64 => "wasmer_vm_f64_nearest",
Self::TruncF32 => "wasmer_vm_f32_trunc",
Self::TruncF64 => "wasmer_vm_f64_trunc",
Self::Memory32Size => "wasmer_vm_memory32_size",
Self::ImportedMemory32Size => "wasmer_vm_imported_memory32_size",
Self::TableCopy => "wasmer_vm_table_copy",
Self::TableInit => "wasmer_vm_table_init",
Self::TableFill => "wasmer_vm_table_fill",
Self::TableSize => "wasmer_vm_table_size",
Self::ImportedTableSize => "wasmer_vm_imported_table_size",
Self::TableGet => "wasmer_vm_table_get",
Self::ImportedTableGet => "wasmer_vm_imported_table_get",
Self::TableSet => "wasmer_vm_table_set",
Self::ImportedTableSet => "wasmer_vm_imported_table_set",
Self::TableGrow => "wasmer_vm_table_grow",
Self::ImportedTableGrow => "wasmer_vm_imported_table_grow",
Self::FuncRef => "wasmer_vm_func_ref",
Self::ElemDrop => "wasmer_vm_elem_drop",
Self::Memory32Copy => "wasmer_vm_memory32_copy",
Self::ImportedMemory32Copy => "wasmer_vm_imported_memory32_copy",
Self::Memory32Fill => "wasmer_vm_memory32_fill",
Self::ImportedMemory32Fill => "wasmer_vm_imported_memory32_fill",
Self::Memory32Init => "wasmer_vm_memory32_init",
Self::DataDrop => "wasmer_vm_data_drop",
Self::RaiseTrap => "wasmer_vm_raise_trap",
#[cfg(target_vendor = "apple")]
Self::Probestack => "_wasmer_vm_probestack",
#[cfg(not(target_vendor = "apple"))]
Self::Probestack => "wasmer_vm_probestack",
}
}
}
impl fmt::Display for LibCall {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(self, f)
}
}