Skip to content

Commit

Permalink
new way to access memory from the vmctx
Browse files Browse the repository at this point in the history
  • Loading branch information
lachlansneff committed Jan 21, 2019
1 parent e8ccea4 commit 6e9b002
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/runtime/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl Instance {

// Initialize the vm::Ctx in-place after the backing
// has been boxed.
*inner.vmctx = unsafe { vm::Ctx::new(&mut inner.backing, &mut inner.import_backing) };
*inner.vmctx = unsafe { vm::Ctx::new(&mut inner.backing, &mut inner.import_backing, &module) };

let mut instance = Instance {
module,
Expand Down
40 changes: 34 additions & 6 deletions lib/runtime/src/vm.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
pub use crate::{
backing::{ImportBacking, LocalBacking},
types::LocalMemoryIndex,
module::ModuleInner,
types::{
MemoryIndex, LocalMemoryIndex, LocalOrImport,
},
};
use std::{ffi::c_void, mem, ptr};
use std::{ffi::c_void, mem, ptr, slice};

#[derive(Debug)]
#[repr(C)]
Expand All @@ -28,10 +31,10 @@ pub struct Ctx {
/// A pointer to an array of imported functions, indexed by `FuncIndex`.
pub(crate) imported_funcs: *mut ImportedFunc,

/// The local backing of the parent instance.
pub local_backing: *mut LocalBacking,
/// The import backing of the parent instance.
pub import_backing: *mut ImportBacking,
pub(crate) local_backing: *mut LocalBacking,
pub(crate) import_backing: *mut ImportBacking,
module: *const ModuleInner,


pub data: *mut c_void,
pub data_finalizer: Option<extern "C" fn(data: *mut c_void)>,
Expand All @@ -41,6 +44,7 @@ impl Ctx {
pub unsafe fn new(
local_backing: &mut LocalBacking,
import_backing: &mut ImportBacking,
module: &ModuleInner,
) -> Self {
Self {
memories: local_backing.vm_memories.as_mut_ptr(),
Expand All @@ -54,6 +58,7 @@ impl Ctx {

local_backing,
import_backing,
module,

data: ptr::null_mut(),
data_finalizer: None,
Expand All @@ -63,6 +68,7 @@ impl Ctx {
pub unsafe fn new_with_data(
local_backing: &mut LocalBacking,
import_backing: &mut ImportBacking,
module: &ModuleInner,
data: *mut c_void,
data_finalizer: extern "C" fn(*mut c_void),
) -> Self {
Expand All @@ -78,12 +84,34 @@ impl Ctx {

local_backing,
import_backing,
module,

data,
data_finalizer: Some(data_finalizer),
}
}

pub fn memory<'a>(&'a mut self, mem_index: MemoryIndex) -> &'a mut [u8] {
let module = unsafe { &*self.module };
match mem_index.local_or_import(module) {
LocalOrImport::Local(local_mem_index) => {
let local_backing = unsafe { &mut *self.local_backing };
&mut local_backing.memories[local_mem_index][..]
},
LocalOrImport::Import(import_mem_index) => {
let import_backing = unsafe { &mut *self.import_backing };
let vm_memory_import = import_backing.memories[import_mem_index].clone();
unsafe {
let memory = &*vm_memory_import.memory;

slice::from_raw_parts_mut(memory.base, memory.size)
}
},
}
}
}

impl Ctx {
#[allow(clippy::erasing_op)] // TODO
pub fn offset_memories() -> u8 {
0 * (mem::size_of::<usize>() as u8)
Expand Down

0 comments on commit 6e9b002

Please sign in to comment.