mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-08 05:38:19 +00:00
Remove VMContext port to union, split into #1753
This commit is contained in:
22
lib/api/src/externals/function.rs
vendored
22
lib/api/src/externals/function.rs
vendored
@@ -11,8 +11,8 @@ use std::cmp::max;
|
||||
use std::fmt;
|
||||
use wasmer_vm::{
|
||||
raise_user_trap, resume_panic, wasmer_call_trampoline, Export, ExportFunction,
|
||||
FunctionExtraData, VMCallerCheckedAnyfunc, VMContext, VMDynamicFunctionContext, VMFunctionBody,
|
||||
VMFunctionKind, VMTrampoline,
|
||||
VMCallerCheckedAnyfunc, VMContext, VMDynamicFunctionContext, VMFunctionBody, VMFunctionKind,
|
||||
VMTrampoline,
|
||||
};
|
||||
|
||||
/// A function defined in the Wasm module
|
||||
@@ -85,9 +85,7 @@ impl Function {
|
||||
// The engine linker will replace the address with one pointing to a
|
||||
// generated dynamic trampoline.
|
||||
let address = std::ptr::null() as *const VMFunctionBody;
|
||||
let vmctx = FunctionExtraData {
|
||||
host_env: Box::into_raw(Box::new(dynamic_ctx)) as *mut _,
|
||||
};
|
||||
let vmctx = Box::into_raw(Box::new(dynamic_ctx)) as *mut VMContext;
|
||||
|
||||
Self {
|
||||
store: store.clone(),
|
||||
@@ -142,9 +140,7 @@ impl Function {
|
||||
// The engine linker will replace the address with one pointing to a
|
||||
// generated dynamic trampoline.
|
||||
let address = std::ptr::null() as *const VMFunctionBody;
|
||||
let vmctx = FunctionExtraData {
|
||||
host_env: Box::into_raw(Box::new(dynamic_ctx)) as *mut _,
|
||||
};
|
||||
let vmctx = Box::into_raw(Box::new(dynamic_ctx)) as *mut VMContext;
|
||||
// TODO: look into removing transmute by changing API type signatures
|
||||
let function_ptr = Some(unsafe { std::mem::transmute::<fn(_, _), fn(_, _)>(Env::finish) });
|
||||
//dbg!(function_ptr);
|
||||
@@ -189,9 +185,7 @@ impl Function {
|
||||
{
|
||||
let function = inner::Function::<Args, Rets>::new(func);
|
||||
let address = function.address() as *const VMFunctionBody;
|
||||
let vmctx = FunctionExtraData {
|
||||
host_env: std::ptr::null_mut() as *mut _,
|
||||
};
|
||||
let vmctx = std::ptr::null_mut() as *mut VMContext;
|
||||
let signature = function.ty();
|
||||
|
||||
Self {
|
||||
@@ -252,9 +246,7 @@ impl Function {
|
||||
// In the case of Host-defined functions `VMContext` is whatever environment
|
||||
// the user want to attach to the function.
|
||||
let box_env = Box::new(env);
|
||||
let vmctx = FunctionExtraData {
|
||||
host_env: Box::into_raw(box_env) as *mut _,
|
||||
};
|
||||
let vmctx = Box::into_raw(box_env) as *mut VMContext;
|
||||
// TODO: look into removing transmute by changing API type signatures
|
||||
let function_ptr = Some(unsafe { std::mem::transmute::<fn(_, _), fn(_, _)>(Env::finish) });
|
||||
//dbg!(function_ptr as usize);
|
||||
@@ -393,7 +385,7 @@ impl Function {
|
||||
Self {
|
||||
store: store.clone(),
|
||||
definition: FunctionDefinition::Host(HostFunctionDefinition {
|
||||
has_env: !unsafe { wasmer_export.vmctx.host_env.is_null() },
|
||||
has_env: !wasmer_export.vmctx.is_null(),
|
||||
}),
|
||||
exported: wasmer_export,
|
||||
}
|
||||
|
||||
@@ -17,8 +17,7 @@ use crate::{FromToNativeWasmType, Function, FunctionType, RuntimeError, Store, W
|
||||
use std::panic::{catch_unwind, AssertUnwindSafe};
|
||||
use wasmer_types::NativeWasmType;
|
||||
use wasmer_vm::{
|
||||
ExportFunction, FunctionExtraData, VMContext, VMDynamicFunctionContext, VMFunctionBody,
|
||||
VMFunctionKind,
|
||||
ExportFunction, VMContext, VMDynamicFunctionContext, VMFunctionBody, VMFunctionKind,
|
||||
};
|
||||
|
||||
/// A WebAssembly function that can be called natively
|
||||
@@ -27,7 +26,7 @@ pub struct NativeFunc<'a, Args = (), Rets = ()> {
|
||||
definition: FunctionDefinition,
|
||||
store: Store,
|
||||
address: *const VMFunctionBody,
|
||||
vmctx: FunctionExtraData,
|
||||
vmctx: *mut VMContext,
|
||||
arg_kind: VMFunctionKind,
|
||||
// exported: ExportFunction,
|
||||
_phantom: PhantomData<(&'a (), Args, Rets)>,
|
||||
@@ -43,7 +42,7 @@ where
|
||||
pub(crate) fn new(
|
||||
store: Store,
|
||||
address: *const VMFunctionBody,
|
||||
vmctx: FunctionExtraData,
|
||||
vmctx: *mut VMContext,
|
||||
arg_kind: VMFunctionKind,
|
||||
definition: FunctionDefinition,
|
||||
) -> Self {
|
||||
@@ -170,7 +169,7 @@ macro_rules! impl_native_traits {
|
||||
match self.arg_kind {
|
||||
VMFunctionKind::Static => {
|
||||
let results = catch_unwind(AssertUnwindSafe(|| unsafe {
|
||||
let f = std::mem::transmute::<_, unsafe extern "C" fn( FunctionExtraData, $( $x, )*) -> Rets::CStruct>(self.address);
|
||||
let f = std::mem::transmute::<_, unsafe extern "C" fn( *mut VMContext, $( $x, )*) -> Rets::CStruct>(self.address);
|
||||
// We always pass the vmctx
|
||||
f( self.vmctx, $( $x, )* )
|
||||
})).map_err(|e| RuntimeError::new(format!("{:?}", e)))?;
|
||||
@@ -180,16 +179,12 @@ macro_rules! impl_native_traits {
|
||||
let params_list = [ $( $x.to_native().to_value() ),* ];
|
||||
let results = if !has_env {
|
||||
type VMContextWithoutEnv = VMDynamicFunctionContext<VMDynamicFunctionWithoutEnv>;
|
||||
unsafe {
|
||||
let ctx = self.vmctx.host_env as *mut VMContextWithoutEnv;
|
||||
(*ctx).ctx.call(¶ms_list)?
|
||||
}
|
||||
let ctx = self.vmctx as *mut VMContextWithoutEnv;
|
||||
unsafe { (*ctx).ctx.call(¶ms_list)? }
|
||||
} else {
|
||||
type VMContextWithEnv = VMDynamicFunctionContext<VMDynamicFunctionWithEnv<std::ffi::c_void>>;
|
||||
unsafe {
|
||||
let ctx = self.vmctx.host_env as *mut VMContextWithEnv;
|
||||
(*ctx).ctx.call(¶ms_list)?
|
||||
}
|
||||
let ctx = self.vmctx as *mut VMContextWithEnv;
|
||||
unsafe { (*ctx).ctx.call(¶ms_list)? }
|
||||
};
|
||||
let mut rets_list_array = Rets::empty_array();
|
||||
let mut_rets = rets_list_array.as_mut() as *mut [i128] as *mut i128;
|
||||
|
||||
@@ -56,9 +56,7 @@ impl ValFuncRef for Val {
|
||||
Self::ExternRef(ExternRef::Null) => wasmer_vm::VMCallerCheckedAnyfunc {
|
||||
func_ptr: ptr::null(),
|
||||
type_index: wasmer_vm::VMSharedSignatureIndex::default(),
|
||||
vmctx: wasmer_vm::FunctionExtraData {
|
||||
host_env: ptr::null_mut(),
|
||||
},
|
||||
vmctx: ptr::null_mut(),
|
||||
},
|
||||
Self::FuncRef(f) => f.checked_anyfunc(),
|
||||
_ => return Err(RuntimeError::new("val is not funcref")),
|
||||
|
||||
@@ -112,10 +112,7 @@ pub trait Artifact: Send + Sync + Upcastable {
|
||||
.cloned()
|
||||
.zip(imports.functions.values())
|
||||
{
|
||||
let host_env = unsafe {
|
||||
//dbg!(func.extra_data.host_env);
|
||||
func.extra_data.host_env
|
||||
};
|
||||
let host_env = func.vmctx as _;
|
||||
thunks.push((func_init, host_env));
|
||||
}
|
||||
// ------------
|
||||
|
||||
@@ -170,7 +170,7 @@ pub fn resolve_imports(
|
||||
};
|
||||
function_imports.push(VMFunctionImport {
|
||||
body: address,
|
||||
extra_data: f.vmctx,
|
||||
vmctx: f.vmctx,
|
||||
});
|
||||
|
||||
host_function_env_initializers.push(f.function_ptr);
|
||||
|
||||
@@ -30,7 +30,7 @@ pub struct ExportFunction {
|
||||
/// The address of the native-code function.
|
||||
pub address: *const VMFunctionBody,
|
||||
/// Pointer to the containing `VMContext`.
|
||||
pub vmctx: crate::vmcontext::FunctionExtraData,
|
||||
pub vmctx: *mut VMContext,
|
||||
/// temp code to set vmctx for host functions
|
||||
pub function_ptr: Option<fn(*mut std::ffi::c_void, *const std::ffi::c_void)>,
|
||||
/// The function type, used for compatibility checking.
|
||||
|
||||
@@ -317,15 +317,13 @@ impl Instance {
|
||||
if let Some(def_index) = self.module.local_func_index(*index) {
|
||||
(
|
||||
self.functions[def_index].0 as *const _,
|
||||
crate::vmcontext::FunctionExtraData {
|
||||
vmctx: self.vmctx_ptr(),
|
||||
},
|
||||
self.vmctx_ptr(),
|
||||
None,
|
||||
)
|
||||
} else {
|
||||
let import = self.imported_function(*index);
|
||||
let initializer = self.imported_function_env_initializer(*index);
|
||||
(import.body, import.extra_data, initializer)
|
||||
(import.body, import.vmctx, initializer)
|
||||
};
|
||||
let call_trampoline = Some(self.function_call_trampolines[*sig_index]);
|
||||
let signature = self.module.signatures[*sig_index].clone();
|
||||
@@ -404,27 +402,21 @@ impl Instance {
|
||||
.get(local_index)
|
||||
.expect("function index is out of bounds")
|
||||
.0;
|
||||
(
|
||||
body as *const _,
|
||||
crate::FunctionExtraData {
|
||||
vmctx: self.vmctx_ptr(),
|
||||
},
|
||||
)
|
||||
(body as *const _, self.vmctx_ptr())
|
||||
}
|
||||
None => {
|
||||
assert_lt!(start_index.index(), self.module.num_imported_functions);
|
||||
let import = self.imported_function(start_index);
|
||||
(import.body, import.extra_data)
|
||||
(import.body, import.vmctx)
|
||||
}
|
||||
};
|
||||
|
||||
// Make the call.
|
||||
unsafe {
|
||||
catch_traps(callee_extra_data, || {
|
||||
mem::transmute::<
|
||||
*const VMFunctionBody,
|
||||
unsafe extern "C" fn(crate::FunctionExtraData),
|
||||
>(callee_address)(callee_extra_data)
|
||||
mem::transmute::<*const VMFunctionBody, unsafe extern "C" fn(*mut VMContext)>(
|
||||
callee_address,
|
||||
)(callee_extra_data)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -594,15 +586,10 @@ impl Instance {
|
||||
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 _,
|
||||
crate::FunctionExtraData {
|
||||
vmctx: self.vmctx_ptr(),
|
||||
},
|
||||
)
|
||||
(self.functions[def_index].0 as *const _, self.vmctx_ptr())
|
||||
} else {
|
||||
let import = self.imported_function(index);
|
||||
(import.body, import.extra_data)
|
||||
(import.body, import.vmctx)
|
||||
};
|
||||
VMCallerCheckedAnyfunc {
|
||||
func_ptr,
|
||||
|
||||
@@ -48,10 +48,10 @@ pub use crate::sig_registry::SignatureRegistry;
|
||||
pub use crate::table::{LinearTable, Table, TableStyle};
|
||||
pub use crate::trap::*;
|
||||
pub use crate::vmcontext::{
|
||||
FunctionExtraData, VMBuiltinFunctionIndex, VMCallerCheckedAnyfunc, VMContext,
|
||||
VMDynamicFunctionContext, VMFunctionBody, VMFunctionImport, VMFunctionKind, VMGlobalDefinition,
|
||||
VMGlobalImport, VMMemoryDefinition, VMMemoryImport, VMSharedSignatureIndex, VMTableDefinition,
|
||||
VMTableImport, VMTrampoline,
|
||||
VMBuiltinFunctionIndex, VMCallerCheckedAnyfunc, VMContext, VMDynamicFunctionContext,
|
||||
VMFunctionBody, VMFunctionImport, VMFunctionKind, VMGlobalDefinition, VMGlobalImport,
|
||||
VMMemoryDefinition, VMMemoryImport, VMSharedSignatureIndex, VMTableDefinition, VMTableImport,
|
||||
VMTrampoline,
|
||||
};
|
||||
pub use crate::vmoffsets::{TargetSharedSignatureIndex, VMOffsets};
|
||||
|
||||
|
||||
@@ -429,13 +429,13 @@ impl Trap {
|
||||
/// Wildly unsafe because it calls raw function pointers and reads/writes raw
|
||||
/// function pointers.
|
||||
pub unsafe fn wasmer_call_trampoline(
|
||||
vmctx: crate::FunctionExtraData,
|
||||
vmctx: *mut VMContext,
|
||||
trampoline: VMTrampoline,
|
||||
callee: *const VMFunctionBody,
|
||||
values_vec: *mut u8,
|
||||
) -> Result<(), Trap> {
|
||||
catch_traps(vmctx, || {
|
||||
mem::transmute::<_, extern "C" fn(crate::FunctionExtraData, *const VMFunctionBody, *mut u8)>(
|
||||
mem::transmute::<_, extern "C" fn(*mut VMContext, *const VMFunctionBody, *mut u8)>(
|
||||
trampoline,
|
||||
)(vmctx, callee, values_vec)
|
||||
})
|
||||
@@ -447,7 +447,7 @@ pub unsafe fn wasmer_call_trampoline(
|
||||
/// # Safety
|
||||
///
|
||||
/// Highly unsafe since `closure` won't have any destructors run.
|
||||
pub unsafe fn catch_traps<F>(vmctx: crate::FunctionExtraData, mut closure: F) -> Result<(), Trap>
|
||||
pub unsafe fn catch_traps<F>(vmctx: *mut VMContext, mut closure: F) -> Result<(), Trap>
|
||||
where
|
||||
F: FnMut(),
|
||||
{
|
||||
@@ -481,7 +481,7 @@ where
|
||||
///
|
||||
/// Check [`catch_traps`].
|
||||
pub unsafe fn catch_traps_with_result<F, R>(
|
||||
vmctx: crate::FunctionExtraData,
|
||||
vmctx: *mut VMContext,
|
||||
mut closure: F,
|
||||
) -> Result<R, Trap>
|
||||
where
|
||||
@@ -501,7 +501,7 @@ pub struct CallThreadState {
|
||||
jmp_buf: Cell<*const u8>,
|
||||
reset_guard_page: Cell<bool>,
|
||||
prev: Option<*const CallThreadState>,
|
||||
vmctx: crate::FunctionExtraData,
|
||||
vmctx: *mut VMContext,
|
||||
handling_trap: Cell<bool>,
|
||||
}
|
||||
|
||||
@@ -518,7 +518,7 @@ enum UnwindReason {
|
||||
}
|
||||
|
||||
impl CallThreadState {
|
||||
fn new(vmctx: crate::FunctionExtraData) -> Self {
|
||||
fn new(vmctx: *mut VMContext) -> Self {
|
||||
Self {
|
||||
unwind: Cell::new(UnwindReason::None),
|
||||
vmctx,
|
||||
@@ -561,7 +561,7 @@ impl CallThreadState {
|
||||
|
||||
fn any_instance(&self, func: impl Fn(&InstanceHandle) -> bool) -> bool {
|
||||
unsafe {
|
||||
if func(&InstanceHandle::from_vmctx(self.vmctx.vmctx)) {
|
||||
if func(&InstanceHandle::from_vmctx(self.vmctx)) {
|
||||
return true;
|
||||
}
|
||||
match self.prev {
|
||||
|
||||
@@ -15,29 +15,6 @@ use std::ptr::{self, NonNull};
|
||||
use std::sync::Arc;
|
||||
use std::u32;
|
||||
|
||||
/// We stop lying about what this daat is
|
||||
/// TODO:
|
||||
#[derive(Copy, Clone)]
|
||||
pub union FunctionExtraData {
|
||||
/// Wasm function, it has a real VMContext:
|
||||
pub vmctx: *mut VMContext,
|
||||
/// Host functions can have custom environments
|
||||
pub host_env: *mut std::ffi::c_void,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for FunctionExtraData {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
write!(f, "FunctionExtarData union")
|
||||
}
|
||||
}
|
||||
|
||||
impl std::cmp::PartialEq for FunctionExtraData {
|
||||
fn eq(&self, rhs: &Self) -> bool {
|
||||
// TODO
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// An imported function.
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
#[repr(C)]
|
||||
@@ -46,7 +23,7 @@ pub struct VMFunctionImport {
|
||||
pub body: *const VMFunctionBody,
|
||||
|
||||
/// A pointer to the `VMContext` that owns the function or host data.
|
||||
pub extra_data: FunctionExtraData,
|
||||
pub vmctx: *mut VMContext,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -69,7 +46,7 @@ mod test_vmfunction_import {
|
||||
usize::from(offsets.vmfunction_import_body())
|
||||
);
|
||||
assert_eq!(
|
||||
offset_of!(VMFunctionImport, extra_data),
|
||||
offset_of!(VMFunctionImport, vmctx),
|
||||
usize::from(offsets.vmfunction_import_vmctx())
|
||||
);
|
||||
}
|
||||
@@ -752,7 +729,7 @@ pub struct VMCallerCheckedAnyfunc {
|
||||
/// Function signature id.
|
||||
pub type_index: VMSharedSignatureIndex,
|
||||
/// Function `VMContext`.
|
||||
pub vmctx: FunctionExtraData,
|
||||
pub vmctx: *mut VMContext,
|
||||
// If more elements are added here, remember to add offset_of tests below!
|
||||
}
|
||||
|
||||
@@ -791,9 +768,7 @@ impl Default for VMCallerCheckedAnyfunc {
|
||||
Self {
|
||||
func_ptr: ptr::null_mut(),
|
||||
type_index: Default::default(),
|
||||
vmctx: FunctionExtraData {
|
||||
vmctx: ptr::null_mut(),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user