Add utility print function

This commit is contained in:
Steve Akinyemi
2018-11-07 11:47:06 +01:00
parent 5699b46566
commit 2fe528aa57
7 changed files with 67 additions and 22 deletions

View File

@ -2,16 +2,16 @@
# This will re-generate the Rust test files based on spectests/*.wast
spectests:
WASM_GENERATE_SPECTESTS=1 cargo +nightly build
WASM_GENERATE_SPECTESTS=1 cargo build
# clean:
# rm -rf target
build:
cargo +nightly build
cargo build
install:
cargo +nightly install --path .
cargo install --path .
test:
cargo +nightly test -- --test-threads=1
cargo test -- --test-threads=1

View File

@ -20,7 +20,7 @@ pub fn generate_libc_env<'a, 'b>() -> ImportObject<&'a str, &'b str> {
#[cfg(test)]
mod tests {
use super::generate_libc_env;
use crate::webassembly::{instantiate, Export, VmCtx};
use crate::webassembly::{instantiate, Export, Instance};
#[test]
fn test_putchar() {

View File

@ -80,6 +80,9 @@ fn execute_wasm(wasm_path: PathBuf) -> Result<(), String> {
let webassembly::ResultObject { module, instance } =
webassembly::instantiate(wasm_binary, import_object)
.map_err(|err| String::from(err.description()))?;
// webassembly::utils::print_instance_offsets(&instance);
let func_index = instance
.start_func
.unwrap_or_else(|| match module.info.exports.get("main") {

View File

@ -106,6 +106,7 @@ pub struct Instance {
pub data_pointers: DataPointers,
// Default memory bound
// TODO: Support for only one LinearMemory for now.
pub default_memory_bound: i32
}
@ -547,19 +548,29 @@ impl Clone for Instance {
}
}
/// TODO:
/// Need to improve how memories are stored and grown.
/// Dynamic memory is inefficient both for growing and for access
/// Cranelift's dynamic heap assumes a _statically-known_ number of LinearMemories,
/// because it expects a corresponding global variable for each LinearMemory
///
/// Reference:
/// - https://cranelift.readthedocs.io/en/latest/ir.html?highlight=vmctx#heap-examples,
///
extern "C" fn grow_memory(size: u32, memory_index: u32, instance: &mut Instance) -> i32 {
// For now only the first mem can be accessed
// BTW, the memory_index coming in is random!
// TODO: Support for only one LinearMemory for now.
let memory_index: u32 = 0;
let old_mem_size = instance
.memory_mut(memory_index as usize)
.grow(size)
.unwrap_or(i32::max_value()); // Should be -1 ?
// Update the default_memory_bound
instance.default_memory_bound =
(instance.memories.get(0).unwrap().current as usize * LinearMemory::WASM_PAGE_SIZE) as i32;
// PROBLEM: The memories changed, so I have to do the whole slice thing all over again.
// The grown memory changed so data_pointers need to be updated as well.
// TODO: Refactor repetitive code
let tables_pointer: Vec<BoundedSlice<usize>> =
instance.tables.iter().map(|table| table[..].into()).collect();
let memories_pointer: Vec<UncheckedSlice<u8>> =
@ -576,15 +587,6 @@ extern "C" fn grow_memory(size: u32, memory_index: u32, instance: &mut Instance
// Update data_pointers
instance.data_pointers = data_pointers;
println!(
"
new mem loc = {:p}
instance.default_memory_bound = {:?}
",
&instance.memories.get(0).unwrap().mmap.get(0),
instance.default_memory_bound
);
return old_mem_size;
}

View File

@ -105,8 +105,8 @@ impl LinearMemory {
// to move.
let mut new_mmap = MmapMut::map_anon(new_bytes).unwrap();
// Copy old mem to new mem. Will a while loop be faster or
// is this going to be optimized?
// Copy old mem to new mem. Will a while loop be faster or is this going to be optimized?
// TODO: Consider static heap for efficiency.
for i in 0..prev_bytes {
unsafe {
let new_mmap_index = new_mmap.get_unchecked_mut(i);
@ -115,8 +115,8 @@ impl LinearMemory {
}
}
// Zero out the remaining mem region // TODO: Check if memmap
// should zeroes out everything by default.
// Zero out the remaining mem region
// TODO: Check if memmap zeroes out everything by default. This is very inefficient!
for i in prev_bytes..new_bytes {
unsafe {
let index = new_mmap.get_unchecked_mut(i);

View File

@ -13,7 +13,7 @@ use wasmparser;
pub use self::errors::{Error, ErrorKind};
pub use self::import_object::ImportObject;
pub use self::instance::{Instance, DataPointers, VmCtx, UserData};
pub use self::instance::{Instance};
pub use self::memory::LinearMemory;
pub use self::module::{Export, Module, ModuleInfo};

View File

@ -1,6 +1,46 @@
//! Utility functions for the webassembly library
use super::instance::Instance;
use std::mem::transmute;
use super::super::common::slice::{UncheckedSlice, BoundedSlice};
/// Detect if a provided binary is a WASM file
pub fn is_wasm_binary(binary: &Vec<u8>) -> bool {
binary.starts_with(&[b'\0', b'a', b's', b'm'])
}
pub fn print_instance_offsets(instance: &Instance) {
let instance_address = instance as *const _ as usize;
let tables_pointer_address_ptr: *const usize =
unsafe { transmute(&instance.data_pointers.tables) };
let tables_pointer_address = tables_pointer_address_ptr as usize;
let memories_pointer_address_ptr: *const usize =
unsafe { transmute(&instance.data_pointers.memories) };
let memories_pointer_address = memories_pointer_address_ptr as usize;
let globals_pointer_address_ptr: *const usize =
unsafe { transmute(&instance.data_pointers.globals) };
let globals_pointer_address = globals_pointer_address_ptr as usize;
let default_memory_bound_address_ptr: *const usize =
unsafe { transmute(&instance.default_memory_bound) };
let default_memory_bound_address = default_memory_bound_address_ptr as usize;
println!(
"
====== INSTANCE OFFSET TABLE ======
instance \t\t\t- {:X} | offset - {:?}
instance.data_pointers.tables \t- {:X} | offset - {:?}
instance.data_pointers.memories - {:X} | offset - {:?}
instance.data_pointers.globals \t- {:X} | offset - {:?}
instance.default_memory_bound \t- {:X} | offset - {:?}
====== INSTANCE OFFSET TABLE ======
",
instance_address, 0,
tables_pointer_address, tables_pointer_address - instance_address,
memories_pointer_address, memories_pointer_address - instance_address,
globals_pointer_address, globals_pointer_address - instance_address,
default_memory_bound_address, default_memory_bound_address - instance_address,
);
}