Use table min and max values from module to generate environment table

This commit is contained in:
Brandon Fish
2019-01-25 19:55:33 -06:00
parent 5209b5a558
commit 1a1958a0ce
4 changed files with 25 additions and 10 deletions

View File

@ -51,7 +51,9 @@ mod utils;
mod varargs; mod varargs;
pub use self::storage::align_memory; pub use self::storage::align_memory;
pub use self::utils::{allocate_cstr_on_stack, allocate_on_stack, is_emscripten_module}; pub use self::utils::{
allocate_cstr_on_stack, allocate_on_stack, get_emscripten_table_size, is_emscripten_module,
};
// TODO: Magic number - how is this calculated? // TODO: Magic number - how is this calculated?
const TOTAL_STACK: u32 = 5_242_880; const TOTAL_STACK: u32 = 5_242_880;
@ -361,10 +363,12 @@ pub struct EmscriptenGlobals {
// The emscripten table // The emscripten table
pub table: TableBacking, pub table: TableBacking,
pub vm_table: LocalTable, pub vm_table: LocalTable,
pub table_min: u32,
pub table_max: Option<u32>,
} }
impl EmscriptenGlobals { impl EmscriptenGlobals {
pub fn new() -> Self { pub fn new(table_min: u32, table_max: Option<u32>) -> Self {
// Memory initialization // Memory initialization
let memory_type = Memory { let memory_type = Memory {
min: 256, min: 256,
@ -376,8 +380,8 @@ impl EmscriptenGlobals {
let table_type = Table { let table_type = Table {
ty: ElementType::Anyfunc, ty: ElementType::Anyfunc,
min: 10, min: table_min,
max: Some(10), max: table_max,
}; };
let mut table = TableBacking::new(&table_type); let mut table = TableBacking::new(&table_type);
let vm_table = table.into_vm_table(); let vm_table = table.into_vm_table();
@ -406,6 +410,8 @@ impl EmscriptenGlobals {
vm_memory, vm_memory,
table, table,
vm_table, vm_table,
table_min,
table_max,
} }
} }
} }
@ -450,8 +456,8 @@ pub fn generate_emscripten_env(globals: &mut EmscriptenGlobals) -> ImportObject
ctx: null_ctx, ctx: null_ctx,
table: Table { table: Table {
ty: ElementType::Anyfunc, ty: ElementType::Anyfunc,
min: 8192, min: globals.table_min,
max: Some(8192), max: globals.table_max,
}, },
}, },
); );

View File

@ -5,7 +5,9 @@ use std::ffi::CStr;
use std::mem::size_of; use std::mem::size_of;
use std::os::raw::c_char; use std::os::raw::c_char;
use std::slice; use std::slice;
use wasmer_runtime_core::{module::Module, vm::Ctx}; use wasmer_runtime_core::{
module::Module, structures::TypedIndex, types::ImportedTableIndex, vm::Ctx,
};
/// We check if a provided module is an Emscripten generated one /// We check if a provided module is an Emscripten generated one
pub fn is_emscripten_module(module: &Module) -> bool { pub fn is_emscripten_module(module: &Module) -> bool {
@ -17,6 +19,11 @@ pub fn is_emscripten_module(module: &Module) -> bool {
false false
} }
pub fn get_emscripten_table_size(module: &Module) -> (u32, Option<u32>) {
let (_, table) = &module.0.imported_tables[ImportedTableIndex::new(0)];
(table.min, table.max)
}
pub unsafe fn write_to_buf(string: *const c_char, buf: u32, max: u32, ctx: &mut Ctx) -> u32 { pub unsafe fn write_to_buf(string: *const c_char, buf: u32, max: u32, ctx: &mut Ctx) -> u32 {
let buf_addr = emscripten_memory_pointer!(ctx.memory(0), buf) as *mut c_char; let buf_addr = emscripten_memory_pointer!(ctx.memory(0), buf) as *mut c_char;

View File

@ -15,8 +15,8 @@ macro_rules! assert_emscripten_output {
// let module = compile(&wasm_bytes[..]) // let module = compile(&wasm_bytes[..])
// .map_err(|err| format!("Can't create the WebAssembly module: {}", err)).unwrap(); // NOTE: Need to figure what the unwrap is for ?? // .map_err(|err| format!("Can't create the WebAssembly module: {}", err)).unwrap(); // NOTE: Need to figure what the unwrap is for ??
let (table_min, table_max) = wasmer_emscripten::get_emscripten_table_size(&module);
let mut emscripten_globals = EmscriptenGlobals::new(); let mut emscripten_globals = EmscriptenGlobals::new(table_min, table_max);
let import_object = generate_emscripten_env(&mut emscripten_globals); let import_object = generate_emscripten_env(&mut emscripten_globals);
let mut instance = module.instantiate(import_object) let mut instance = module.instantiate(import_object)

View File

@ -70,7 +70,9 @@ fn execute_wasm(options: &Run) -> Result<(), String> {
.map_err(|e| format!("Can't compile module: {:?}", e))?; .map_err(|e| format!("Can't compile module: {:?}", e))?;
let (_abi, import_object) = if wasmer_emscripten::is_emscripten_module(&module) { let (_abi, import_object) = if wasmer_emscripten::is_emscripten_module(&module) {
let mut emscripten_globals = wasmer_emscripten::EmscriptenGlobals::new(); let (table_min, table_max) = wasmer_emscripten::get_emscripten_table_size(&module);
let mut emscripten_globals =
wasmer_emscripten::EmscriptenGlobals::new(table_min, table_max);
( (
InstanceABI::Emscripten, InstanceABI::Emscripten,
wasmer_emscripten::generate_emscripten_env(&mut emscripten_globals), wasmer_emscripten::generate_emscripten_env(&mut emscripten_globals),