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;
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?
const TOTAL_STACK: u32 = 5_242_880;
@ -361,10 +363,12 @@ pub struct EmscriptenGlobals {
// The emscripten table
pub table: TableBacking,
pub vm_table: LocalTable,
pub table_min: u32,
pub table_max: Option<u32>,
}
impl EmscriptenGlobals {
pub fn new() -> Self {
pub fn new(table_min: u32, table_max: Option<u32>) -> Self {
// Memory initialization
let memory_type = Memory {
min: 256,
@ -376,8 +380,8 @@ impl EmscriptenGlobals {
let table_type = Table {
ty: ElementType::Anyfunc,
min: 10,
max: Some(10),
min: table_min,
max: table_max,
};
let mut table = TableBacking::new(&table_type);
let vm_table = table.into_vm_table();
@ -406,6 +410,8 @@ impl EmscriptenGlobals {
vm_memory,
table,
vm_table,
table_min,
table_max,
}
}
}
@ -450,8 +456,8 @@ pub fn generate_emscripten_env(globals: &mut EmscriptenGlobals) -> ImportObject
ctx: null_ctx,
table: Table {
ty: ElementType::Anyfunc,
min: 8192,
max: Some(8192),
min: globals.table_min,
max: globals.table_max,
},
},
);

View File

@ -5,7 +5,9 @@ use std::ffi::CStr;
use std::mem::size_of;
use std::os::raw::c_char;
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
pub fn is_emscripten_module(module: &Module) -> bool {
@ -17,6 +19,11 @@ pub fn is_emscripten_module(module: &Module) -> bool {
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 {
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[..])
// .map_err(|err| format!("Can't create the WebAssembly module: {}", err)).unwrap(); // NOTE: Need to figure what the unwrap is for ??
let mut emscripten_globals = EmscriptenGlobals::new();
let (table_min, table_max) = wasmer_emscripten::get_emscripten_table_size(&module);
let mut emscripten_globals = EmscriptenGlobals::new(table_min, table_max);
let import_object = generate_emscripten_env(&mut emscripten_globals);
let mut instance = module.instantiate(import_object)