Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Context improvements #1

Merged
merged 5 commits into from
Oct 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fn execute_wasm(wasm_path: PathBuf) -> Result<(), String> {
}

let import_object = integrations::generate_libc_env();
let webassembly::ResultObject { module, instance } =
let webassembly::ResultObject { module, mut instance } =
webassembly::instantiate(wasm_binary, import_object)
.map_err(|err| String::from(err.description()))?;
let func_index = instance
Expand All @@ -80,8 +80,9 @@ fn execute_wasm(wasm_path: PathBuf) -> Result<(), String> {
Some(&webassembly::Export::Function(index)) => index,
_ => panic!("Main function not found"),
});
let main: fn() = get_instance_function!(instance, func_index);
main();
let main: fn(&webassembly::VmCtx) = get_instance_function!(instance, func_index);
let context = instance.generate_context();
main(&context);
Ok(())
}

Expand Down
95 changes: 44 additions & 51 deletions src/webassembly/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,38 +68,21 @@ fn get_function_addr(
// (base as usize + offset) as _
// }

/// Zero-sized, non-instantiable type.
#[derive(Debug)]
pub enum VmCtx {}

impl VmCtx {
pub fn data(&self) -> &VmCtxData {
let heap_ptr = self as *const _ as *const VmCtxData;
unsafe { &*heap_ptr.sub(1) }
}

/// This is safe because the offset is 32 bits and thus
/// cannot extend out of the guarded wasm memory.
pub fn fastpath_offset_ptr<T>(&self, offset: u32) -> *const T {
let heap_ptr = self as *const _ as *const u8;
unsafe { heap_ptr.add(offset as usize) as *const T }
}
}

#[derive(Debug)]
// #[derive(Debug)]
#[repr(C)]
pub struct VmCtxData<'phantom> {
pub struct VmCtx<'phantom> {
pub user_data: UserData,
// globals: UncheckedSlice<u8>,
// memories: UncheckedSlice<UncheckedSlice<u8>>,
// tables: UncheckedSlice<BoundedSlice<usize>>,
globals: Vec<u8>,
memories: Vec<Vec<u8>>,
tables: Vec<Vec<usize>>,
globals: UncheckedSlice<u8>,
memories: UncheckedSlice<UncheckedSlice<u8>>,
tables: UncheckedSlice<BoundedSlice<usize>>,
// globals: Vec<u8>,
// memories: Vec<Vec<u8>>,
// pub tables: UncheckedSlice<BoundedSlice<usize>>,
phantom: PhantomData<&'phantom ()>,
}

#[derive(Debug)]
// #[derive(Debug)]
#[repr(C)]
pub struct UserData {
// pub process: Dispatch<Process>,
Expand Down Expand Up @@ -240,9 +223,13 @@ impl Instance {
},
RelocationType::CurrentMemory => {
current_memory as isize
// panic!("current memory not yet implemented");
// unimplemented!()
},
RelocationType::GrowMemory => {
grow_memory as isize
// panic!("Grow memory not yet implemented");
// unimplemented!()
},
_ => unimplemented!()
// RelocationType::Intrinsic(name) => {
Expand Down Expand Up @@ -518,33 +505,33 @@ impl Instance {
}
}

pub fn generate_context(&mut self) -> &VmCtx {
let mut memories: Vec<Vec<u8>> = self.memories.iter().map(|mem| mem[..].into()).collect();
pub fn generate_context(&mut self) -> VmCtx {
let mut memories: Vec<UncheckedSlice<u8>> = self.memories.iter().map(|mem| mem[..].into()).collect();

let tables: Vec<Vec<usize>> = self.tables.iter().map(|table| table[..].into()).collect();
let tables: Vec<BoundedSlice<usize>> = self.tables.iter().map(|table| table[..].into()).collect();

let globals: Vec<u8> = self.globals[..].into();
let globals: UncheckedSlice<u8> = self.globals[..].into();

assert!(memories.len() >= 1, "modules must have at least one memory");
// assert!(memories.len() >= 1, "modules must have at least one memory");
// the first memory has a space of `mem::size_of::<VmCtxData>()` rounded
// up to the 4KiB before it. We write the VmCtxData into that.
let instance = self.clone();
let data = VmCtxData {
let data = VmCtx {
globals: globals,
memories: memories[1..].into(),
memories: memories[..].into(),
tables: tables[..].into(),
user_data: UserData {
// process,
instance,
instance: instance,
},
phantom: PhantomData,
};

let main_heap_ptr = memories[0].as_mut_ptr() as *mut VmCtxData;
unsafe {
main_heap_ptr.sub(1).write(data);
&*(main_heap_ptr as *const VmCtx)
}
data
// let main_heap_ptr = memories[0].as_mut_ptr() as *mut VmCtxData;
// unsafe {
// main_heap_ptr.sub(1).write(data);
// &*(main_heap_ptr as *const VmCtx)
// }
}

/// Returns a slice of the contents of allocated linear memory.
Expand Down Expand Up @@ -583,8 +570,8 @@ impl Clone for Instance {
}

extern "C" fn grow_memory(size: u32, memory_index: u32, vmctx: &VmCtx) -> i32 {
// return 0;
unimplemented!();
return 0;
// unimplemented!();
// let instance = &vmctx
// .data()
// .user_data
Expand All @@ -608,18 +595,24 @@ extern "C" fn grow_memory(size: u32, memory_index: u32, vmctx: &VmCtx) -> i32 {
}

extern "C" fn current_memory(memory_index: u32, vmctx: &VmCtx) -> u32 {
let instance = &vmctx.user_data.instance;
let memory = &instance.memories[memory_index as usize];
memory.current_size() as u32

// return 0;
println!("current_memory::init {:?}", memory_index);
let instance = &vmctx.data().user_data.instance;
// unimplemented!();

let memory = &instance.memories[memory_index as usize];
println!(
"INSPECTED MEMORY ({:?}) {:?}",
memory.current_size(),
instance.inspect_memory(0, 0, 1)
);
// println!("current_memory::init {:?}", memory_index);
// let instance = &vmctx.data().user_data.instance;

memory.current_size() as u32
// let memory = &instance.memories[memory_index as usize];
// println!(
// "INSPECTED MEMORY ({:?}) {:?}",
// memory.current_size(),
// instance.inspect_memory(0, 0, 1)
// );

// memory.current_size() as u32
// return 1;
// let vm = unsafe {
// (*vmctx) as *mut VmCtx
Expand Down