mirror of
https://github.com/fluencelabs/wasmer
synced 2025-04-25 18:32:15 +00:00
feat(runtime-core) vm::ImportedFunc
and vm::FuncCtx
have NonNull
pointers.
This commit is contained in:
parent
177c507a4e
commit
e002c377ef
@ -15,7 +15,11 @@ use crate::{
|
|||||||
},
|
},
|
||||||
vm,
|
vm,
|
||||||
};
|
};
|
||||||
use std::{fmt::Debug, ptr, slice};
|
use std::{
|
||||||
|
fmt::Debug,
|
||||||
|
ptr::{self, NonNull},
|
||||||
|
slice,
|
||||||
|
};
|
||||||
|
|
||||||
pub const INTERNALS_SIZE: usize = 256;
|
pub const INTERNALS_SIZE: usize = 256;
|
||||||
|
|
||||||
@ -382,9 +386,9 @@ impl LocalBacking {
|
|||||||
vmctx,
|
vmctx,
|
||||||
),
|
),
|
||||||
LocalOrImport::Import(imported_func_index) => {
|
LocalOrImport::Import(imported_func_index) => {
|
||||||
let vm::ImportedFunc { func, vmctx } =
|
let vm::ImportedFunc { func, func_ctx } =
|
||||||
imports.vm_functions[imported_func_index];
|
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,
|
vmctx,
|
||||||
),
|
),
|
||||||
LocalOrImport::Import(imported_func_index) => {
|
LocalOrImport::Import(imported_func_index) => {
|
||||||
let vm::ImportedFunc { func, vmctx } =
|
let vm::ImportedFunc { func, func_ctx } =
|
||||||
imports.vm_functions[imported_func_index];
|
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 {
|
impl Drop for ImportBacking {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
for (_imported_func_index, imported_func) in (*self.vm_functions).iter_mut() {
|
for (_imported_func_index, imported_func) in (*self.vm_functions).iter_mut() {
|
||||||
let _: Box<vm::FuncCtx> = unsafe { Box::from_raw(imported_func.vmctx) };
|
let _: Box<vm::FuncCtx> = unsafe { Box::from_raw(imported_func.func_ctx.as_ptr()) };
|
||||||
imported_func.vmctx = ptr::null_mut();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -583,16 +586,17 @@ fn import_functions(
|
|||||||
if *expected_sig == *signature {
|
if *expected_sig == *signature {
|
||||||
functions.push(vm::ImportedFunc {
|
functions.push(vm::ImportedFunc {
|
||||||
func: func.inner(),
|
func: func.inner(),
|
||||||
vmctx: {
|
func_ctx: {
|
||||||
let _ = match ctx {
|
let _ = match ctx {
|
||||||
Context::External(ctx) => ctx,
|
Context::External(ctx) => ctx,
|
||||||
Context::Internal => vmctx,
|
Context::Internal => vmctx,
|
||||||
};
|
};
|
||||||
|
|
||||||
Box::into_raw(Box::new(vm::FuncCtx {
|
NonNull::new(Box::into_raw(Box::new(vm::FuncCtx {
|
||||||
vmctx,
|
vmctx: NonNull::new(vmctx).expect("`vmctx` must not be null."),
|
||||||
func_env: ptr::null_mut(),
|
func_env: ptr::null_mut(),
|
||||||
}))
|
})))
|
||||||
|
.unwrap()
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
@ -622,8 +626,8 @@ fn import_functions(
|
|||||||
None => {
|
None => {
|
||||||
if imports.allow_missing_functions {
|
if imports.allow_missing_functions {
|
||||||
functions.push(vm::ImportedFunc {
|
functions.push(vm::ImportedFunc {
|
||||||
func: ::std::ptr::null(),
|
func: ptr::null(),
|
||||||
vmctx: ::std::ptr::null_mut(),
|
func_ctx: unsafe { NonNull::new_unchecked(ptr::null_mut()) }, // TODO: Non-sense…
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
link_errors.push(LinkError::ImportNotFound {
|
link_errors.push(LinkError::ImportNotFound {
|
||||||
|
@ -110,9 +110,13 @@ impl Instance {
|
|||||||
|
|
||||||
let ctx_ptr = match start_index.local_or_import(&instance.module.info) {
|
let ctx_ptr = match start_index.local_or_import(&instance.module.info) {
|
||||||
LocalOrImport::Local(_) => instance.inner.vmctx,
|
LocalOrImport::Local(_) => instance.inner.vmctx,
|
||||||
LocalOrImport::Import(imported_func_index) => {
|
LocalOrImport::Import(imported_func_index) => unsafe {
|
||||||
instance.inner.import_backing.vm_functions[imported_func_index].vmctx as _
|
instance.inner.import_backing.vm_functions[imported_func_index]
|
||||||
|
.func_ctx
|
||||||
|
.as_ref()
|
||||||
}
|
}
|
||||||
|
.vmctx
|
||||||
|
.as_ptr(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let sig_index = *instance
|
let sig_index = *instance
|
||||||
@ -195,9 +199,13 @@ impl Instance {
|
|||||||
|
|
||||||
let ctx = match func_index.local_or_import(&self.module.info) {
|
let ctx = match func_index.local_or_import(&self.module.info) {
|
||||||
LocalOrImport::Local(_) => self.inner.vmctx,
|
LocalOrImport::Local(_) => self.inner.vmctx,
|
||||||
LocalOrImport::Import(imported_func_index) => {
|
LocalOrImport::Import(imported_func_index) => unsafe {
|
||||||
self.inner.import_backing.vm_functions[imported_func_index].vmctx as _
|
self.inner.import_backing.vm_functions[imported_func_index]
|
||||||
|
.func_ctx
|
||||||
|
.as_ref()
|
||||||
}
|
}
|
||||||
|
.vmctx
|
||||||
|
.as_ptr(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let func_wasm_inner = self
|
let func_wasm_inner = self
|
||||||
@ -449,7 +457,7 @@ impl InstanceInner {
|
|||||||
let imported_func = &self.import_backing.vm_functions[imported_func_index];
|
let imported_func = &self.import_backing.vm_functions[imported_func_index];
|
||||||
(
|
(
|
||||||
imported_func.func as *const _,
|
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) {
|
let ctx_ptr = match func_index.local_or_import(info) {
|
||||||
LocalOrImport::Local(_) => local_ctx,
|
LocalOrImport::Local(_) => local_ctx,
|
||||||
LocalOrImport::Import(imported_func_index) => {
|
LocalOrImport::Import(imported_func_index) => unsafe {
|
||||||
import_backing.vm_functions[imported_func_index].vmctx as _
|
import_backing.vm_functions[imported_func_index]
|
||||||
|
.func_ctx
|
||||||
|
.as_ref()
|
||||||
}
|
}
|
||||||
|
.vmctx
|
||||||
|
.as_ptr(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let wasm = runnable
|
let wasm = runnable
|
||||||
|
@ -498,7 +498,7 @@ macro_rules! impl_traits {
|
|||||||
Trap: TrapEarly<Rets>,
|
Trap: TrapEarly<Rets>,
|
||||||
FN: Fn(&mut vm::Ctx, $( $x, )*) -> Trap,
|
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 f: FN = unsafe { mem::transmute_copy(&()) };
|
||||||
|
|
||||||
let err = match panic::catch_unwind(
|
let err = match panic::catch_unwind(
|
||||||
@ -557,7 +557,7 @@ macro_rules! impl_traits {
|
|||||||
Trap: TrapEarly<Rets>,
|
Trap: TrapEarly<Rets>,
|
||||||
FN: Fn($( $x, )*) -> Trap,
|
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 f: FN = unsafe { mem::transmute_copy(&()) };
|
||||||
|
|
||||||
let err = match panic::catch_unwind(
|
let err = match panic::catch_unwind(
|
||||||
|
@ -512,7 +512,7 @@ pub struct FuncEnv {
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
pub struct FuncCtx {
|
pub struct FuncCtx {
|
||||||
pub vmctx: *mut Ctx,
|
pub vmctx: NonNull<Ctx>,
|
||||||
pub func_env: *mut FuncEnv,
|
pub func_env: *mut FuncEnv,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -521,7 +521,7 @@ pub struct FuncCtx {
|
|||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
pub struct ImportedFunc {
|
pub struct ImportedFunc {
|
||||||
pub func: *const Func,
|
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?))
|
// 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?))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user