Make function imports unsafe

This commit is contained in:
Lachlan Sneff
2019-01-08 22:09:09 -05:00
parent bba168e61e
commit 82771673b5
4 changed files with 22 additions and 6 deletions

View File

@ -2,7 +2,7 @@ use wasmer_clif_backend::CraneliftCompiler;
use wasmer_runtime::{ use wasmer_runtime::{
self as runtime, self as runtime,
types::{FuncSig, Type, Value}, types::{FuncSig, Type, Value},
vm, Import, Imports, vm, Import, Imports, FuncRef,
}; };
static EXAMPLE_WASM: &'static [u8] = include_bytes!("simple.wasm"); static EXAMPLE_WASM: &'static [u8] = include_bytes!("simple.wasm");
@ -15,7 +15,7 @@ fn main() -> Result<(), String> {
"env".to_string(), "env".to_string(),
"print_num".to_string(), "print_num".to_string(),
Import::Func( Import::Func(
print_num as _, unsafe { FuncRef::new(print_num as _) },
FuncSig { FuncSig {
params: vec![Type::I32], params: vec![Type::I32],
returns: vec![Type::I32], returns: vec![Type::I32],

View File

@ -263,10 +263,10 @@ impl ImportBacking {
let expected_sig = module.sig_registry.lookup_func_sig(sig_index); let expected_sig = module.sig_registry.lookup_func_sig(sig_index);
let import = imports.get(mod_name, item_name); let import = imports.get(mod_name, item_name);
match import { match import {
Some(&Import::Func(func, ref signature)) => { Some(&Import::Func(ref func, ref signature)) => {
if expected_sig == signature { if expected_sig == signature {
functions.push(vm::ImportedFunc { functions.push(vm::ImportedFunc {
func, func: func.inner(),
// vmctx: ptr::null_mut(), // vmctx: ptr::null_mut(),
}); });
} else { } else {

View File

@ -134,9 +134,25 @@ impl Instance {
} }
} }
#[derive(Debug)]
pub struct FuncRef(*const vm::Func);
impl FuncRef {
/// This needs to be unsafe because there is
/// no way to check whether the passed function
/// is valid and has the right signature.
pub unsafe fn new(f: *const vm::Func) -> Self {
FuncRef(f)
}
pub(crate) fn inner(&self) -> *const vm::Func {
self.0
}
}
#[derive(Debug)] #[derive(Debug)]
pub enum Import { pub enum Import {
Func(*const vm::Func, FuncSig), Func(FuncRef, FuncSig),
Table(Arc<TableBacking>, Table), Table(Arc<TableBacking>, Table),
Memory(Arc<LinearMemory>, Memory), Memory(Arc<LinearMemory>, Memory),
Global(Value), Global(Value),

View File

@ -15,7 +15,7 @@ pub mod vm;
pub mod vmcalls; pub mod vmcalls;
pub use self::backend::{Compiler, FuncResolver}; pub use self::backend::{Compiler, FuncResolver};
pub use self::instance::{Import, ImportResolver, Imports, Instance}; pub use self::instance::{Import, ImportResolver, Imports, FuncRef, Instance};
pub use self::memory::LinearMemory; pub use self::memory::LinearMemory;
pub use self::module::{Module, ModuleInner}; pub use self::module::{Module, ModuleInner};
pub use self::sig_registry::SigRegistry; pub use self::sig_registry::SigRegistry;