mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-25 14:41:32 +00:00
Add utility print function
This commit is contained in:
8
Makefile
8
Makefile
@ -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
|
||||
|
@ -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() {
|
||||
|
@ -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") {
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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};
|
||||
|
||||
|
@ -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,
|
||||
);
|
||||
}
|
||||
|
Reference in New Issue
Block a user