diff --git a/lib/runtime-core/src/backing.rs b/lib/runtime-core/src/backing.rs index d413ef00e..f9b320b45 100644 --- a/lib/runtime-core/src/backing.rs +++ b/lib/runtime-core/src/backing.rs @@ -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 = unsafe { Box::from_raw(imported_func.vmctx) }; - imported_func.vmctx = ptr::null_mut(); + let _: Box = 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 { diff --git a/lib/runtime-core/src/instance.rs b/lib/runtime-core/src/instance.rs index 2716e272a..5cf92a312 100644 --- a/lib/runtime-core/src/instance.rs +++ b/lib/runtime-core/src/instance.rs @@ -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 diff --git a/lib/runtime-core/src/typed_func.rs b/lib/runtime-core/src/typed_func.rs index 8e0819985..049dfc161 100644 --- a/lib/runtime-core/src/typed_func.rs +++ b/lib/runtime-core/src/typed_func.rs @@ -498,7 +498,7 @@ macro_rules! impl_traits { Trap: TrapEarly, 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, 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( diff --git a/lib/runtime-core/src/vm.rs b/lib/runtime-core/src/vm.rs index adc33f2c5..fa4d3020b 100644 --- a/lib/runtime-core/src/vm.rs +++ b/lib/runtime-core/src/vm.rs @@ -512,7 +512,7 @@ pub struct FuncEnv { #[derive(Debug)] #[repr(C)] pub struct FuncCtx { - pub vmctx: *mut Ctx, + pub vmctx: NonNull, 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, } // 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?))