mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-29 16:41:33 +00:00
Improved context data
This commit is contained in:
@ -82,7 +82,7 @@ fn execute_wasm(wasm_path: PathBuf) -> Result<(), String> {
|
||||
});
|
||||
let main: fn(&webassembly::VmCtx) = get_instance_function!(instance, func_index);
|
||||
let context = instance.generate_context();
|
||||
main(context);
|
||||
main(&context);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -68,27 +68,10 @@ 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)]
|
||||
#[repr(C)]
|
||||
pub struct VmCtxData<'phantom> {
|
||||
pub struct VmCtx<'phantom> {
|
||||
pub user_data: UserData,
|
||||
globals: UncheckedSlice<u8>,
|
||||
memories: UncheckedSlice<UncheckedSlice<u8>>,
|
||||
@ -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) => {
|
||||
@ -518,33 +505,33 @@ impl Instance {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn generate_context(&mut self) -> &VmCtx {
|
||||
pub fn generate_context(&mut self) -> VmCtx {
|
||||
let mut memories: Vec<UncheckedSlice<u8>> = self.memories.iter().map(|mem| mem[..].into()).collect();
|
||||
|
||||
let tables: Vec<BoundedSlice<usize>> = self.tables.iter().map(|table| table[..].into()).collect();
|
||||
|
||||
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.
|
||||
@ -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
|
||||
@ -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 {
|
||||
// return 0;
|
||||
println!("current_memory::init {:?}", memory_index);
|
||||
let instance = &vmctx.data().user_data.instance;
|
||||
|
||||
let instance = &vmctx.user_data.instance;
|
||||
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 0;
|
||||
// unimplemented!();
|
||||
|
||||
// println!("current_memory::init {:?}", memory_index);
|
||||
// let instance = &vmctx.data().user_data.instance;
|
||||
|
||||
// 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
|
||||
|
Reference in New Issue
Block a user