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

This commit is contained in:
Brandon Fish
2019-01-25 20:12:36 -06:00
parent 1a1958a0ce
commit ee911092ea
4 changed files with 29 additions and 9 deletions

View File

@ -52,7 +52,8 @@ mod varargs;
pub use self::storage::align_memory;
pub use self::utils::{
allocate_cstr_on_stack, allocate_on_stack, get_emscripten_table_size, is_emscripten_module,
allocate_cstr_on_stack, allocate_on_stack, get_emscripten_memory_size,
get_emscripten_table_size, is_emscripten_module,
};
// TODO: Magic number - how is this calculated?
@ -365,14 +366,21 @@ pub struct EmscriptenGlobals {
pub vm_table: LocalTable,
pub table_min: u32,
pub table_max: Option<u32>,
pub memory_min: u32,
pub memory_max: Option<u32>,
}
impl EmscriptenGlobals {
pub fn new(table_min: u32, table_max: Option<u32>) -> Self {
pub fn new(
table_min: u32,
table_max: Option<u32>,
memory_min: u32,
memory_max: Option<u32>,
) -> Self {
// Memory initialization
let memory_type = Memory {
min: 256,
max: Some(256),
min: memory_min,
max: memory_max,
shared: false,
};
let mut memory = LinearMemory::new(&memory_type);
@ -412,6 +420,8 @@ impl EmscriptenGlobals {
vm_table,
table_min,
table_max,
memory_min,
memory_max,
}
}
}
@ -438,8 +448,8 @@ pub fn generate_emscripten_env(globals: &mut EmscriptenGlobals) -> ImportObject
local: local_memory,
ctx: null_ctx,
memory: Memory {
min: 256,
max: Some(256),
min: globals.memory_min,
max: globals.memory_max,
shared: false,
},
},

View File

@ -6,7 +6,10 @@ use std::mem::size_of;
use std::os::raw::c_char;
use std::slice;
use wasmer_runtime_core::{
module::Module, structures::TypedIndex, types::ImportedTableIndex, vm::Ctx,
module::Module,
structures::TypedIndex,
types::{ImportedMemoryIndex, ImportedTableIndex},
vm::Ctx,
};
/// We check if a provided module is an Emscripten generated one
@ -24,6 +27,11 @@ pub fn get_emscripten_table_size(module: &Module) -> (u32, Option<u32>) {
(table.min, table.max)
}
pub fn get_emscripten_memory_size(module: &Module) -> (u32, Option<u32>) {
let (_, memory) = &module.0.imported_memories[ImportedMemoryIndex::new(0)];
(memory.min, memory.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;