Implement tables and call_indirect (#12)
* Implement tables and call_indirect * Restore comment about sig checking. * Widen callee index on 64bit platforms.
This commit is contained in:
committed by
Dan Gohman
parent
e7c8d23a42
commit
7b222190f5
@@ -5,7 +5,7 @@ use memory::LinearMemory;
|
||||
use region::protect;
|
||||
use region::Protection;
|
||||
use std::mem::transmute;
|
||||
use std::ptr::write_unaligned;
|
||||
use std::ptr::{self, write_unaligned};
|
||||
use wasmtime_environ::{
|
||||
compile_module, Compilation, Module, ModuleTranslation, Relocation, RelocationTarget,
|
||||
};
|
||||
@@ -66,7 +66,7 @@ fn relocate(compilation: &mut Compilation, relocations: &[Vec<Relocation>]) {
|
||||
|
||||
extern "C" fn grow_memory(size: u32, vmctx: *mut *mut u8) -> u32 {
|
||||
unsafe {
|
||||
let instance = (*vmctx.offset(2)) as *mut Instance;
|
||||
let instance = (*vmctx.offset(4)) as *mut Instance;
|
||||
(*instance)
|
||||
.memory_mut(0)
|
||||
.grow(size)
|
||||
@@ -76,7 +76,7 @@ extern "C" fn grow_memory(size: u32, vmctx: *mut *mut u8) -> u32 {
|
||||
|
||||
extern "C" fn current_memory(vmctx: *mut *mut u8) -> u32 {
|
||||
unsafe {
|
||||
let instance = (*vmctx.offset(2)) as *mut Instance;
|
||||
let instance = (*vmctx.offset(4)) as *mut Instance;
|
||||
(*instance).memory_mut(0).current_size()
|
||||
}
|
||||
}
|
||||
@@ -84,10 +84,24 @@ extern "C" fn current_memory(vmctx: *mut *mut u8) -> u32 {
|
||||
/// Create the VmCtx data structure for the JIT'd code to use. This must
|
||||
/// match the VmCtx layout in the environment.
|
||||
fn make_vmctx(instance: &mut Instance, mem_base_addrs: &mut [*mut u8]) -> Vec<*mut u8> {
|
||||
debug_assert!(
|
||||
instance.tables.len() <= 1,
|
||||
"non-default tables is not supported"
|
||||
);
|
||||
|
||||
let (default_table_ptr, default_table_len) = instance
|
||||
.tables
|
||||
.get_mut(0)
|
||||
.map(|table| (table.as_mut_ptr() as *mut u8, table.len()))
|
||||
.unwrap_or((ptr::null_mut(), 0));
|
||||
|
||||
let mut vmctx = Vec::new();
|
||||
vmctx.push(instance.globals.as_mut_ptr());
|
||||
vmctx.push(mem_base_addrs.as_mut_ptr() as *mut u8);
|
||||
vmctx.push(default_table_ptr);
|
||||
vmctx.push(default_table_len as *mut u8);
|
||||
vmctx.push(instance as *mut Instance as *mut u8);
|
||||
|
||||
vmctx
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
use cranelift_codegen::ir;
|
||||
use cranelift_wasm::GlobalIndex;
|
||||
use memory::LinearMemory;
|
||||
use wasmtime_environ::{DataInitializer, Module, TableElements};
|
||||
use wasmtime_environ::{Compilation, DataInitializer, Module, TableElements};
|
||||
|
||||
/// An Instance of a WebAssemby module.
|
||||
#[derive(Debug)]
|
||||
@@ -21,20 +21,29 @@ pub struct Instance {
|
||||
|
||||
impl Instance {
|
||||
/// Create a new `Instance`.
|
||||
pub fn new(module: &Module, data_initializers: &[DataInitializer]) -> Self {
|
||||
pub fn new(
|
||||
module: &Module,
|
||||
compilation: &Compilation,
|
||||
data_initializers: &[DataInitializer],
|
||||
) -> Self {
|
||||
let mut result = Self {
|
||||
tables: Vec::new(),
|
||||
memories: Vec::new(),
|
||||
globals: Vec::new(),
|
||||
};
|
||||
result.instantiate_tables(module, &module.table_elements);
|
||||
result.instantiate_tables(module, compilation, &module.table_elements);
|
||||
result.instantiate_memories(module, data_initializers);
|
||||
result.instantiate_globals(module);
|
||||
result
|
||||
}
|
||||
|
||||
/// Allocate memory in `self` for just the tables of the current module.
|
||||
fn instantiate_tables(&mut self, module: &Module, table_initializers: &[TableElements]) {
|
||||
fn instantiate_tables(
|
||||
&mut self,
|
||||
module: &Module,
|
||||
compilation: &Compilation,
|
||||
table_initializers: &[TableElements],
|
||||
) {
|
||||
debug_assert!(self.tables.is_empty());
|
||||
self.tables.reserve_exact(module.tables.len());
|
||||
for table in &module.tables {
|
||||
@@ -47,7 +56,10 @@ impl Instance {
|
||||
debug_assert!(init.base.is_none(), "globalvar base not supported yet");
|
||||
let to_init =
|
||||
&mut self.tables[init.table_index][init.offset..init.offset + init.elements.len()];
|
||||
to_init.copy_from_slice(&init.elements);
|
||||
for (i, func_idx) in init.elements.iter().enumerate() {
|
||||
let code_buf = &compilation.functions[*func_idx];
|
||||
to_init[i] = code_buf.as_ptr() as usize;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user