feat(runtime-core) vm::ImportedFunc and vm::FuncCtx have NonNull pointers.

This commit is contained in:
Ivan Enderlin 2019-11-04 11:45:09 +01:00
parent 177c507a4e
commit e002c377ef
4 changed files with 40 additions and 24 deletions

View File

@ -15,7 +15,11 @@ use crate::{
},
vm,
};
use std::{fmt::Debug, ptr, slice};
use std::{
fmt::Debug,
ptr::{self, NonNull},
slice,
};
pub const INTERNALS_SIZE: usize = 256;
@ -382,9 +386,9 @@ impl LocalBacking {
vmctx,
),
LocalOrImport::Import(imported_func_index) => {
let vm::ImportedFunc { func, vmctx } =
let vm::ImportedFunc { func, func_ctx } =
imports.vm_functions[imported_func_index];
(func, vmctx as _)
(func, unsafe { func_ctx.as_ref() }.vmctx.as_ptr())
}
};
@ -415,9 +419,9 @@ impl LocalBacking {
vmctx,
),
LocalOrImport::Import(imported_func_index) => {
let vm::ImportedFunc { func, vmctx } =
let vm::ImportedFunc { func, func_ctx } =
imports.vm_functions[imported_func_index];
(func, vmctx as _)
(func, unsafe { func_ctx.as_ref() }.vmctx.as_ptr())
}
};
@ -544,8 +548,7 @@ impl ImportBacking {
impl Drop for ImportBacking {
fn drop(&mut self) {
for (_imported_func_index, imported_func) in (*self.vm_functions).iter_mut() {
let _: Box<vm::FuncCtx> = unsafe { Box::from_raw(imported_func.vmctx) };
imported_func.vmctx = ptr::null_mut();
let _: Box<vm::FuncCtx> = unsafe { Box::from_raw(imported_func.func_ctx.as_ptr()) };
}
}
}
@ -583,16 +586,17 @@ fn import_functions(
if *expected_sig == *signature {
functions.push(vm::ImportedFunc {
func: func.inner(),
vmctx: {
func_ctx: {
let _ = match ctx {
Context::External(ctx) => ctx,
Context::Internal => vmctx,
};
Box::into_raw(Box::new(vm::FuncCtx {
vmctx,
NonNull::new(Box::into_raw(Box::new(vm::FuncCtx {
vmctx: NonNull::new(vmctx).expect("`vmctx` must not be null."),
func_env: ptr::null_mut(),
}))
})))
.unwrap()
},
});
} else {
@ -622,8 +626,8 @@ fn import_functions(
None => {
if imports.allow_missing_functions {
functions.push(vm::ImportedFunc {
func: ::std::ptr::null(),
vmctx: ::std::ptr::null_mut(),
func: ptr::null(),
func_ctx: unsafe { NonNull::new_unchecked(ptr::null_mut()) }, // TODO: Non-sense…
});
} else {
link_errors.push(LinkError::ImportNotFound {

View File

@ -110,9 +110,13 @@ impl Instance {
let ctx_ptr = match start_index.local_or_import(&instance.module.info) {
LocalOrImport::Local(_) => instance.inner.vmctx,
LocalOrImport::Import(imported_func_index) => {
instance.inner.import_backing.vm_functions[imported_func_index].vmctx as _
LocalOrImport::Import(imported_func_index) => unsafe {
instance.inner.import_backing.vm_functions[imported_func_index]
.func_ctx
.as_ref()
}
.vmctx
.as_ptr(),
};
let sig_index = *instance
@ -195,9 +199,13 @@ impl Instance {
let ctx = match func_index.local_or_import(&self.module.info) {
LocalOrImport::Local(_) => self.inner.vmctx,
LocalOrImport::Import(imported_func_index) => {
self.inner.import_backing.vm_functions[imported_func_index].vmctx as _
LocalOrImport::Import(imported_func_index) => unsafe {
self.inner.import_backing.vm_functions[imported_func_index]
.func_ctx
.as_ref()
}
.vmctx
.as_ptr(),
};
let func_wasm_inner = self
@ -449,7 +457,7 @@ impl InstanceInner {
let imported_func = &self.import_backing.vm_functions[imported_func_index];
(
imported_func.func as *const _,
Context::External(imported_func.vmctx as _),
Context::External(unsafe { imported_func.func_ctx.as_ref() }.vmctx.as_ptr()),
)
}
};
@ -574,9 +582,13 @@ fn call_func_with_index(
let ctx_ptr = match func_index.local_or_import(info) {
LocalOrImport::Local(_) => local_ctx,
LocalOrImport::Import(imported_func_index) => {
import_backing.vm_functions[imported_func_index].vmctx as _
LocalOrImport::Import(imported_func_index) => unsafe {
import_backing.vm_functions[imported_func_index]
.func_ctx
.as_ref()
}
.vmctx
.as_ptr(),
};
let wasm = runnable

View File

@ -498,7 +498,7 @@ macro_rules! impl_traits {
Trap: TrapEarly<Rets>,
FN: Fn(&mut vm::Ctx, $( $x, )*) -> Trap,
{
let vmctx = unsafe { &mut *func_ctx.vmctx };
let vmctx = unsafe { func_ctx.vmctx.as_mut() };
let f: FN = unsafe { mem::transmute_copy(&()) };
let err = match panic::catch_unwind(
@ -557,7 +557,7 @@ macro_rules! impl_traits {
Trap: TrapEarly<Rets>,
FN: Fn($( $x, )*) -> Trap,
{
let vmctx = unsafe { &mut *func_ctx.vmctx };
let vmctx = unsafe { func_ctx.vmctx.as_mut() };
let f: FN = unsafe { mem::transmute_copy(&()) };
let err = match panic::catch_unwind(

View File

@ -512,7 +512,7 @@ pub struct FuncEnv {
#[derive(Debug)]
#[repr(C)]
pub struct FuncCtx {
pub vmctx: *mut Ctx,
pub vmctx: NonNull<Ctx>,
pub func_env: *mut FuncEnv,
}
@ -521,7 +521,7 @@ pub struct FuncCtx {
#[repr(C)]
pub struct ImportedFunc {
pub func: *const Func,
pub vmctx: *mut FuncCtx,
pub func_ctx: NonNull<FuncCtx>,
}
// manually implemented because ImportedFunc contains raw pointers directly; `Func` is marked Send (But `Ctx` actually isn't! (TODO: review this, shouldn't `Ctx` be Send?))