Implement minimal call_indirect signature checking.

This commit is contained in:
Dan Gohman
2018-12-05 07:46:25 -05:00
parent 27c2f82628
commit 57635eb62b
7 changed files with 93 additions and 22 deletions

View File

@@ -140,8 +140,8 @@ fn instantiate_memories(
/// Allocate memory for just the tables of the current module.
fn instantiate_tables(module: &Module, compilation: &Compilation) -> PrimaryMap<TableIndex, Table> {
let mut tables = PrimaryMap::with_capacity(module.tables.len());
for table in module.tables.values() {
let mut tables = PrimaryMap::with_capacity(module.table_plans.len());
for table in module.table_plans.values() {
tables.push(Table::new(table));
}
@@ -150,12 +150,14 @@ fn instantiate_tables(module: &Module, compilation: &Compilation) -> PrimaryMap<
let slice = &mut tables[init.table_index].as_mut();
let subslice = &mut slice[init.offset..init.offset + init.elements.len()];
for (i, func_idx) in init.elements.iter().enumerate() {
// FIXME: Implement cross-module signature checking.
let type_id = module.functions[*func_idx];
let code_buf = &compilation.functions[module.defined_func_index(*func_idx).expect(
"table element initializer with imported function not supported yet",
)];
subslice[i] = AnyFunc {
func_ptr: code_buf.as_ptr(),
type_id: 0, // TODO: Implement signature checking.
type_id: type_id.index(),
};
}
}

View File

@@ -0,0 +1,2 @@
//! Implement a registry of function signatures, for fast indirect call
//! signature checking.

View File

@@ -2,11 +2,13 @@
//!
//! `Table` is to WebAssembly tables what `LinearMemory` is to WebAssembly linear memories.
use cranelift_wasm::{self, TableElementType};
use cranelift_wasm::TableElementType;
use std::ptr;
use vmcontext::VMTable;
use wasmtime_environ::{TablePlan, TableStyle};
#[derive(Debug, Clone)]
#[repr(C)]
pub struct AnyFunc {
pub func_ptr: *const u8,
pub type_id: usize,
@@ -29,21 +31,25 @@ pub struct Table {
}
impl Table {
/// Create a new table instance with specified minimum and maximum number of pages.
pub fn new(table: &cranelift_wasm::Table) -> Self {
match table.ty {
/// Create a new table instance with specified minimum and maximum number of elements.
pub fn new(plan: &TablePlan) -> Self {
match plan.table.ty {
TableElementType::Func => (),
TableElementType::Val(ty) => {
unimplemented!("tables of types other than anyfunc ({})", ty)
}
};
let mut vec = Vec::new();
vec.resize(table.minimum as usize, AnyFunc::default());
match plan.style {
TableStyle::CallerChecksSignature => {
let mut vec = Vec::new();
vec.resize(plan.table.minimum as usize, AnyFunc::default());
Self {
vec,
maximum: table.maximum,
Self {
vec,
maximum: plan.table.maximum,
}
}
}
}