Improved instance & module data pointers location

This commit is contained in:
Syrus Akbary
2018-11-14 11:05:57 -08:00
parent 278dff0fa6
commit 6bfd7d9081
2 changed files with 16 additions and 10 deletions

View File

@ -78,7 +78,11 @@ fn get_function_addr(
/// An Instance of a WebAssembly module /// An Instance of a WebAssembly module
#[derive(Debug)] #[derive(Debug)]
#[repr(C)]
pub struct Instance { pub struct Instance {
// C-like pointers to data (heaps, globals, tables)
pub data_pointers: DataPointers,
/// WebAssembly table data /// WebAssembly table data
// pub tables: Arc<Vec<RwLock<Vec<usize>>>>, // pub tables: Arc<Vec<RwLock<Vec<usize>>>>,
pub tables: Arc<Vec<Vec<usize>>>, pub tables: Arc<Vec<Vec<usize>>>,
@ -101,9 +105,6 @@ pub struct Instance {
// Region start memory location // Region start memory location
// code_base: *const (), // code_base: *const (),
// C-like pointers to data (heaps, globals, tables)
pub data_pointers: DataPointers,
// Default memory bound // Default memory bound
// TODO: Support for only one LinearMemory for now. // TODO: Support for only one LinearMemory for now.
pub default_memory_bound: i32, pub default_memory_bound: i32,
@ -112,6 +113,7 @@ pub struct Instance {
/// Contains pointers to data (heaps, globals, tables) needed /// Contains pointers to data (heaps, globals, tables) needed
/// by Cranelift. /// by Cranelift.
#[derive(Debug)] #[derive(Debug)]
#[repr(C)]
pub struct DataPointers { pub struct DataPointers {
// Pointer to tables // Pointer to tables
pub tables: UncheckedSlice<BoundedSlice<usize>>, pub tables: UncheckedSlice<BoundedSlice<usize>>,

View File

@ -332,12 +332,12 @@ impl<'environment> FuncEnvironmentTrait for FuncEnvironment<'environment> {
let vmctx = func.create_global_value(ir::GlobalValueData::VMContext); let vmctx = func.create_global_value(ir::GlobalValueData::VMContext);
let ptr_size = self.ptr_size(); let ptr_size = self.ptr_size();
// Given a vmctx, we want to retrieve vmctx.tables // Given a instance, we want to retrieve instance.tables
// Create a table whose base address is stored at `vmctx+88`. // Create a table whose base address is stored at `instance+0`.
// 88 is the offset of the vmctx.tables pointer respect to vmctx pointer // 0 is the offset of the vmctx.tables pointer respect to vmctx pointer
let base = func.create_global_value(ir::GlobalValueData::Load { let base = func.create_global_value(ir::GlobalValueData::Load {
base: vmctx, base: vmctx,
offset: Offset32::new(88), offset: Offset32::new(0),
global_type: self.pointer_type(), global_type: self.pointer_type(),
}); });
@ -369,12 +369,12 @@ impl<'environment> FuncEnvironmentTrait for FuncEnvironment<'environment> {
} }
fn make_heap(&mut self, func: &mut ir::Function, index: MemoryIndex) -> ir::Heap { fn make_heap(&mut self, func: &mut ir::Function, index: MemoryIndex) -> ir::Heap {
// Create a static heap whose base address is stored at `vmctx+96`. // Create a static heap whose base address is stored at `instance+8`.
let vmctx = func.create_global_value(ir::GlobalValueData::VMContext); let vmctx = func.create_global_value(ir::GlobalValueData::VMContext);
let heap_base_addr = func.create_global_value(ir::GlobalValueData::Load { let heap_base_addr = func.create_global_value(ir::GlobalValueData::Load {
base: vmctx, base: vmctx,
offset: Offset32::new(96), offset: Offset32::new(8),
global_type: self.pointer_type(), global_type: self.pointer_type(),
}); });
@ -454,7 +454,7 @@ impl<'environment> FuncEnvironmentTrait for FuncEnvironment<'environment> {
let globals_base_addr = func.create_global_value(ir::GlobalValueData::Load { let globals_base_addr = func.create_global_value(ir::GlobalValueData::Load {
base: vmctx, base: vmctx,
offset: Offset32::new(104), offset: Offset32::new(16),
global_type: self.pointer_type(), global_type: self.pointer_type(),
}); });
@ -506,6 +506,7 @@ impl<'environment> FuncEnvironmentTrait for FuncEnvironment<'environment> {
.special_param(ir::ArgumentPurpose::VMContext) .special_param(ir::ArgumentPurpose::VMContext)
.expect("Missing vmctx parameter"); .expect("Missing vmctx parameter");
// The `callee` value is an index into a table of function pointers. // The `callee` value is an index into a table of function pointers.
// Apparently, that table is stored at absolute address 0 in this dummy environment. // Apparently, that table is stored at absolute address 0 in this dummy environment.
// TODO: Generate bounds checking code. // TODO: Generate bounds checking code.
@ -556,6 +557,9 @@ impl<'environment> FuncEnvironmentTrait for FuncEnvironment<'environment> {
.special_param(ir::ArgumentPurpose::VMContext) .special_param(ir::ArgumentPurpose::VMContext)
.expect("Missing vmctx parameter"); .expect("Missing vmctx parameter");
// println!("POINTER BYTES {}", self.pointer_bytes());
// println!("POINTER SIZE {}", self.ptr_size());
// Build a value list for the call instruction containing the call_args and the vmctx // Build a value list for the call instruction containing the call_args and the vmctx
// parameter. // parameter.
let mut args = ir::ValueList::default(); let mut args = ir::ValueList::default();