Update spectests

This commit is contained in:
Steve Akinyemi
2018-11-07 11:18:55 +01:00
parent 069fc3229a
commit df0feb9b7f
58 changed files with 1148 additions and 1259 deletions

View File

@ -59,8 +59,8 @@ fn get_function_addr(
}
// TODO: To be removed.
// #[derive(Debug)]
#[repr(C)]
#[derive(Debug)]
#[repr(C, packed)]
pub struct VmCtx<'phantom> {
pub user_data: UserData,
globals: UncheckedSlice<u8>,
@ -70,8 +70,8 @@ pub struct VmCtx<'phantom> {
}
// TODO: To be removed.
// #[derive(Debug)]
#[repr(C)]
#[derive(Debug)]
#[repr(C, packed)]
pub struct UserData {
// pub process: Dispatch<Process>,
pub instance: Instance,
@ -101,6 +101,27 @@ pub struct Instance {
pub start_func: Option<FuncIndex>,
// Region start memory location
// code_base: *const (),
// C-like pointers to data (heaps, globals, tables)
pub data_pointers: DataPointers,
// Default memory bound
pub default_memory_bound: i32
}
/// Contains pointers to data (heaps, globals, tables) needed
/// by Cranelift.
#[derive(Debug)]
pub struct DataPointers {
// Pointer to tables
pub tables: UncheckedSlice<BoundedSlice<usize>>,
// Pointer to memories
pub memories: UncheckedSlice<UncheckedSlice<u8>>,
// Pointer to globals
pub globals: UncheckedSlice<u8>,
}
impl Instance {
@ -140,6 +161,7 @@ impl Instance {
import_functions.push(function);
relocations.push(vec![]);
}
debug!("Instance - Compiling functions");
// Compile the functions (from cranelift IR to machine code)
for function_body in module.info.function_bodies.values() {
@ -152,7 +174,7 @@ impl Instance {
// .map_err(|e| ErrorKind::CompileError(e.to_string()))?;
// let code_size_offset = func_context
// .compile(&*isa)
// .map_err(|e| ErrorKind::CompileError(e.to_string()))?
// .map_err(|e| ErrorKind::CompileError(e.to_string()))?;
// as usize;
let mut code_buf: Vec<u8> = Vec::new();
@ -163,7 +185,10 @@ impl Instance {
// In case traps need to be triggered, they will go to trap_sink
func_context
.compile_and_emit(&*isa, &mut code_buf, &mut reloc_sink, &mut trap_sink)
.map_err(|e| ErrorKind::CompileError(e.to_string()))?;
.map_err(|e| {
println!("CompileError: {}", e.to_string());
ErrorKind::CompileError(e.to_string())
})?;
// We set this code_buf to be readable & executable
protect_codebuf(&code_buf).unwrap();
@ -385,13 +410,30 @@ impl Instance {
_ => None,
});
// TODO: Refactor repetitive code
let tables_pointer: Vec<BoundedSlice<usize>> =
tables.iter().map(|table| table[..].into()).collect();
let memories_pointer: Vec<UncheckedSlice<u8>> =
memories.iter().map(|mem| mem[..].into()).collect();
let globals_pointer: UncheckedSlice<u8> = globals[..].into();
let data_pointers = DataPointers {
memories: memories_pointer[..].into(),
globals: globals_pointer,
tables: tables_pointer[..].into(),
};
let default_memory_bound = LinearMemory::WASM_PAGE_SIZE as i32;
Ok(Instance {
tables: Arc::new(tables.into_iter().collect()), // tables.into_iter().map(|table| RwLock::new(table)).collect()),
memories: Arc::new(memories.into_iter().collect()),
globals: globals,
functions: functions,
import_functions: import_functions,
start_func: start_func,
globals,
functions,
import_functions,
start_func,
data_pointers,
default_memory_bound,
// code_base: code_base,
})
}
@ -475,6 +517,22 @@ impl Instance {
impl Clone for Instance {
fn clone(&self) -> Instance {
// TODO: Refactor repetitive code
let tables_pointer: Vec<BoundedSlice<usize>> =
self.tables.iter().map(|table| table[..].into()).collect();
let memories_pointer: Vec<UncheckedSlice<u8>> =
self.memories.iter().map(|mem| mem[..].into()).collect();
let globals_pointer: UncheckedSlice<u8> = self.globals[..].into();
let data_pointers = DataPointers {
memories: memories_pointer[..].into(),
globals: globals_pointer,
tables: tables_pointer[..].into(),
};
let default_memory_bound =
self.memories.get(0).unwrap().current as i32;
Instance {
tables: Arc::clone(&self.tables),
memories: Arc::clone(&self.memories),
@ -482,19 +540,52 @@ impl Clone for Instance {
functions: self.functions.clone(),
start_func: self.start_func.clone(),
import_functions: self.import_functions.clone(),
data_pointers,
default_memory_bound,
// code_base: self.code_base,
}
}
}
extern "C" fn grow_memory(size: u32, memory_index: u32, instance: &mut Instance) -> i32 {
extern "C" fn grow_memory(size: u32, memory_index: u32, instance: &mut Instance) -> i32 {
// For now only the first mem can be accessed
// BTW, the memory_index coming in is random!
let memory_index: u32 = 0;
instance
let old_mem_size = instance
.memory_mut(memory_index as usize)
.grow(size)
.unwrap_or(i32::max_value()) // Should be -1 ?
.unwrap_or(i32::max_value()); // Should be -1 ?
instance.default_memory_bound =
(instance.memories.get(0).unwrap().current as usize * LinearMemory::WASM_PAGE_SIZE) as i32;
// PROBLEM: The memories changed, so I have to do the whole slice thing all over again.
let tables_pointer: Vec<BoundedSlice<usize>> =
instance.tables.iter().map(|table| table[..].into()).collect();
let memories_pointer: Vec<UncheckedSlice<u8>> =
instance.memories.iter().map(|mem| mem[..].into()).collect();
let globals_pointer: UncheckedSlice<u8> =
instance.globals[..].into();
let data_pointers = DataPointers {
memories: memories_pointer[..].into(),
globals: globals_pointer,
tables: tables_pointer[..].into(),
};
// Update data_pointers
instance.data_pointers = data_pointers;
println!(
"
new mem loc = {:p}
instance.default_memory_bound = {:?}
",
&instance.memories.get(0).unwrap().mmap.get(0),
instance.default_memory_bound
);
return old_mem_size;
}
extern "C" fn current_memory(memory_index: u32, instance: &mut Instance) -> u32 {