Skip to content

Commit

Permalink
Try #1739:
Browse files Browse the repository at this point in the history
  • Loading branch information
bors[bot] authored Oct 21, 2020
2 parents 36a713a + 149d0b9 commit 19eaacb
Show file tree
Hide file tree
Showing 32 changed files with 513 additions and 178 deletions.
3 changes: 0 additions & 3 deletions examples/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let import_object = wasi_env.import_object(&module)?;
let instance = Instance::new(&module, &import_object)?;

// WASI requires to explicitly set the memory for the `WasiEnv`
wasi_env.set_memory(instance.exports.get_memory("memory")?.clone());

println!("Call WASI `_start` function...");
// And we just call the `_start` function!
let start = instance.exports.get_function("_start")?;
Expand Down
50 changes: 39 additions & 11 deletions lib/api/src/externals/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use std::cmp::max;
use std::fmt;
use wasmer_vm::{
raise_user_trap, resume_panic, wasmer_call_trampoline, Export, ExportFunction,
VMCallerCheckedAnyfunc, VMContext, VMDynamicFunctionContext, VMFunctionBody, VMFunctionKind,
VMTrampoline,
FunctionExtraData, VMCallerCheckedAnyfunc, VMContext, VMDynamicFunctionContext, VMFunctionBody,
VMFunctionKind, VMTrampoline,
};

/// A function defined in the Wasm module
Expand Down Expand Up @@ -85,7 +85,9 @@ 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 = Box::into_raw(Box::new(dynamic_ctx)) as *mut VMContext;
let vmctx = FunctionExtraData {
host_env: Box::into_raw(Box::new(dynamic_ctx)) as *mut _,
};

Self {
store: store.clone(),
Expand All @@ -94,6 +96,7 @@ impl Function {
address,
kind: VMFunctionKind::Dynamic,
vmctx,
function_ptr: None,
signature: ty.clone(),
call_trampoline: None,
},
Expand All @@ -105,12 +108,16 @@ impl Function {
/// # Example
///
/// ```
/// # use wasmer::{Function, FunctionType, Type, Store, Value};
/// # use wasmer::{Function, FunctionType, Type, Store, Value, WasmerEnv, Instance};
/// # let store = Store::default();
///
/// struct Env {
/// multiplier: i32,
/// };
/// impl WasmerEnv for Env {
/// fn finish(&mut self, _instance: &Instance) {}
/// fn free(&mut self) {}
/// }
/// let env = Env { multiplier: 2 };
///
/// let signature = FunctionType::new(vec![Type::I32, Type::I32], vec![Type::I32]);
Expand All @@ -124,7 +131,7 @@ impl Function {
pub fn new_with_env<F, Env>(store: &Store, ty: &FunctionType, env: Env, func: F) -> Self
where
F: Fn(&mut Env, &[Val]) -> Result<Vec<Val>, RuntimeError> + 'static,
Env: Sized + 'static,
Env: Sized + crate::WasmerEnv + 'static,
{
let dynamic_ctx = VMDynamicFunctionContext::from_context(VMDynamicFunctionWithEnv {
env: RefCell::new(env),
Expand All @@ -135,7 +142,12 @@ 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 = Box::into_raw(Box::new(dynamic_ctx)) as *mut VMContext;
let vmctx = FunctionExtraData {
host_env: Box::into_raw(Box::new(dynamic_ctx)) as *mut _,
};
// 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);

Self {
store: store.clone(),
Expand All @@ -144,6 +156,7 @@ impl Function {
address,
kind: VMFunctionKind::Dynamic,
vmctx,
function_ptr,
signature: ty.clone(),
call_trampoline: None,
},
Expand Down Expand Up @@ -176,7 +189,9 @@ impl Function {
{
let function = inner::Function::<Args, Rets>::new(func);
let address = function.address() as *const VMFunctionBody;
let vmctx = std::ptr::null_mut() as *mut _ as *mut VMContext;
let vmctx = FunctionExtraData {
host_env: std::ptr::null_mut() as *mut _,
};
let signature = function.ty();

Self {
Expand All @@ -186,6 +201,9 @@ impl Function {
address,
vmctx,
signature,
// TODO: figure out what's going on in this function: it takes an `Env`
// param but also marks itself as not having an env
function_ptr: None,
kind: VMFunctionKind::Static,
call_trampoline: None,
},
Expand All @@ -200,12 +218,16 @@ impl Function {
/// # Example
///
/// ```
/// # use wasmer::{Store, Function};
/// # use wasmer::{Store, Function, WasmerEnv, Instance};
/// # let store = Store::default();
///
/// struct Env {
/// multiplier: i32,
/// };
/// impl WasmerEnv for Env {
/// fn finish(&mut self, _instance: &Instance) {}
/// fn free(&mut self) {}
/// }
/// let env = Env { multiplier: 2 };
///
/// fn sum_and_multiply(env: &mut Env, a: i32, b: i32) -> i32 {
Expand All @@ -219,7 +241,7 @@ impl Function {
F: HostFunction<Args, Rets, WithEnv, Env>,
Args: WasmTypeList,
Rets: WasmTypeList,
Env: Sized + 'static,
Env: Sized + crate::WasmerEnv + 'static,
{
let function = inner::Function::<Args, Rets>::new(func);
let address = function.address();
Expand All @@ -230,7 +252,12 @@ 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 = Box::into_raw(box_env) as *mut _ as *mut VMContext;
let vmctx = FunctionExtraData {
host_env: Box::into_raw(box_env) as *mut _,
};
// 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);
let signature = function.ty();

Self {
Expand All @@ -240,6 +267,7 @@ impl Function {
address,
kind: VMFunctionKind::Static,
vmctx,
function_ptr,
signature,
call_trampoline: None,
},
Expand Down Expand Up @@ -365,7 +393,7 @@ impl Function {
Self {
store: store.clone(),
definition: FunctionDefinition::Host(HostFunctionDefinition {
has_env: !wasmer_export.vmctx.is_null(),
has_env: !unsafe { wasmer_export.vmctx.host_env.is_null() },
}),
exported: wasmer_export,
}
Expand Down
12 changes: 10 additions & 2 deletions lib/api/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,19 @@ impl Instance {
})
.collect::<Exports>();

Ok(Self {
let instance = Self {
handle,
module: module.clone(),
exports,
})
};

unsafe {
instance
.handle
.initialize_host_envs(&instance as *const _ as *const _);
}

Ok(instance)
}

/// Gets the [`Module`] associated with this instance.
Expand Down
23 changes: 23 additions & 0 deletions lib/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,26 @@ pub use wasmer_engine_native::{Native, NativeArtifact, NativeEngine};

/// Version number of this crate.
pub const VERSION: &str = env!("CARGO_PKG_VERSION");

// --------------------------------------------------------------
// TODO: put this in a proper location, just prototyping for now:
// TODO: rename everything, all names are throw-away names

/// Prototype trait for finishing envs.
pub trait WasmerEnv {
/// The function that Wasmer will call on your type to let it finish
/// instantiating.
fn finish(&mut self, instance: &Instance);

/// Frees memory written to `self` so it can be dropped without any memory leaks.
fn free(&mut self);
}

impl<T: WasmerEnv> WasmerEnv for &'static mut T {
fn finish(&mut self, instance: &Instance) {
(*self).finish(instance)
}
fn free(&mut self) {
(*self).free()
}
}
25 changes: 17 additions & 8 deletions lib/api/src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ use crate::{FromToNativeWasmType, Function, FunctionType, RuntimeError, Store, W
use std::panic::{catch_unwind, AssertUnwindSafe};
use wasmer_types::NativeWasmType;
use wasmer_vm::{
ExportFunction, VMContext, VMDynamicFunctionContext, VMFunctionBody, VMFunctionKind,
ExportFunction, FunctionExtraData, VMContext, VMDynamicFunctionContext, VMFunctionBody,
VMFunctionKind,
};

/// A WebAssembly function that can be called natively
Expand All @@ -26,7 +27,7 @@ pub struct NativeFunc<'a, Args = (), Rets = ()> {
definition: FunctionDefinition,
store: Store,
address: *const VMFunctionBody,
vmctx: *mut VMContext,
vmctx: FunctionExtraData,
arg_kind: VMFunctionKind,
// exported: ExportFunction,
_phantom: PhantomData<(&'a (), Args, Rets)>,
Expand All @@ -42,7 +43,7 @@ where
pub(crate) fn new(
store: Store,
address: *const VMFunctionBody,
vmctx: *mut VMContext,
vmctx: FunctionExtraData,
arg_kind: VMFunctionKind,
definition: FunctionDefinition,
) -> Self {
Expand All @@ -68,6 +69,8 @@ where
address: other.address,
vmctx: other.vmctx,
signature,
// TODO:
function_ptr: None,
kind: other.arg_kind,
call_trampoline: None,
}
Expand All @@ -88,6 +91,8 @@ where
address: other.address,
vmctx: other.vmctx,
signature,
// TODO:
function_ptr: None,
kind: other.arg_kind,
call_trampoline: None,
},
Expand Down Expand Up @@ -165,7 +170,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( *mut VMContext, $( $x, )*) -> Rets::CStruct>(self.address);
let f = std::mem::transmute::<_, unsafe extern "C" fn( FunctionExtraData, $( $x, )*) -> Rets::CStruct>(self.address);
// We always pass the vmctx
f( self.vmctx, $( $x, )* )
})).map_err(|e| RuntimeError::new(format!("{:?}", e)))?;
Expand All @@ -175,12 +180,16 @@ macro_rules! impl_native_traits {
let params_list = [ $( $x.to_native().to_value() ),* ];
let results = if !has_env {
type VMContextWithoutEnv = VMDynamicFunctionContext<VMDynamicFunctionWithoutEnv>;
let ctx = self.vmctx as *mut VMContextWithoutEnv;
unsafe { (*ctx).ctx.call(&params_list)? }
unsafe {
let ctx = self.vmctx.host_env as *mut VMContextWithoutEnv;
(*ctx).ctx.call(&params_list)?
}
} else {
type VMContextWithEnv = VMDynamicFunctionContext<VMDynamicFunctionWithEnv<std::ffi::c_void>>;
let ctx = self.vmctx as *mut VMContextWithEnv;
unsafe { (*ctx).ctx.call(&params_list)? }
unsafe {
let ctx = self.vmctx.host_env as *mut VMContextWithEnv;
(*ctx).ctx.call(&params_list)?
}
};
let mut rets_list_array = Rets::empty_array();
let mut_rets = rets_list_array.as_mut() as *mut [i128] as *mut i128;
Expand Down
7 changes: 6 additions & 1 deletion lib/api/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ impl ValFuncRef for Val {
Self::ExternRef(ExternRef::Null) => wasmer_vm::VMCallerCheckedAnyfunc {
func_ptr: ptr::null(),
type_index: wasmer_vm::VMSharedSignatureIndex::default(),
vmctx: ptr::null_mut(),
vmctx: wasmer_vm::FunctionExtraData {
host_env: ptr::null_mut(),
},
},
Self::FuncRef(f) => f.checked_anyfunc(),
_ => return Err(RuntimeError::new("val is not funcref")),
Expand All @@ -74,6 +76,9 @@ impl ValFuncRef for Val {
let export = wasmer_vm::ExportFunction {
address: item.func_ptr,
signature,
// TODO:
// figure out if we ever need a value here: need testing with complicated import patterns
function_ptr: None,
// All functions in tables are already Static (as dynamic functions
// are converted to use the trampolines with static signatures).
kind: wasmer_vm::VMFunctionKind::Static,
Expand Down
9 changes: 9 additions & 0 deletions lib/api/tests/externals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,11 @@ fn function_new_env() -> Result<()> {
let store = Store::default();
#[derive(Clone)]
struct MyEnv {};
impl WasmerEnv for MyEnv {
fn finish(&mut self, _instance: &Instance) {}
fn free(&mut self) {}
}

let my_env = MyEnv {};
let function = Function::new_native_with_env(&store, my_env.clone(), |_env: &mut MyEnv| {});
assert_eq!(function.ty().clone(), FunctionType::new(vec![], vec![]));
Expand Down Expand Up @@ -273,6 +278,10 @@ fn function_new_dynamic_env() -> Result<()> {
#[derive(Clone)]
struct MyEnv {};
let my_env = MyEnv {};
impl WasmerEnv for MyEnv {
fn finish(&mut self, _instance: &Instance) {}
fn free(&mut self) {}
}

let function_type = FunctionType::new(vec![], vec![]);
let function = Function::new_with_env(
Expand Down
5 changes: 5 additions & 0 deletions lib/c-api/src/deprecated/import/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,11 @@ pub(crate) struct LegacyEnv {
pub(crate) instance_ptr: Option<NonNull<CAPIInstance>>,
}

impl wasmer::WasmerEnv for LegacyEnv {
fn finish(&mut self, _instance: &wasmer::Instance) {}
fn free(&mut self) {}
}

impl LegacyEnv {
pub(crate) fn ctx_ptr(&self) -> *mut CAPIInstance {
self.instance_ptr
Expand Down
7 changes: 1 addition & 6 deletions lib/c-api/src/deprecated/import/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use libc::c_uchar;
use std::path::PathBuf;
use std::ptr;
use std::str;
use wasmer::{Memory, MemoryType, NamedResolver};
use wasmer::NamedResolver;
use wasmer_wasi as wasi;

#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -213,11 +213,6 @@ pub unsafe extern "C" fn wasmer_wasi_generate_default_import_object() -> *mut wa
let mut wasi_state_builder = wasi::WasiState::new("wasmer-wasi-default-program-name");
let wasi_state = wasi_state_builder.build().unwrap();
let mut wasi_env = wasi::WasiEnv::new(wasi_state);
// this API will now leak a `Memory`
let memory_type = MemoryType::new(0, None, false);
let memory = Memory::new(store, memory_type).expect("create memory");
wasi_env.set_memory(memory);
// TODO(mark): review lifetime of `Memory` here
let import_object_inner: Box<dyn NamedResolver> = Box::new(
wasi::generate_import_object_from_env(store, wasi_env, wasi::WasiVersion::Latest),
);
Expand Down
Loading

0 comments on commit 19eaacb

Please sign in to comment.