Fixed warnings

This commit is contained in:
Syrus Akbary
2018-11-07 14:44:17 +01:00
parent 993f88e9f9
commit 58c6822359
2 changed files with 67 additions and 61 deletions

View File

@ -12,7 +12,6 @@ use cranelift_entity::EntityRef;
use cranelift_wasm::{FuncIndex, GlobalInit}; use cranelift_wasm::{FuncIndex, GlobalInit};
use region; use region;
use std::iter::Iterator; use std::iter::Iterator;
use std::marker::PhantomData;
use std::ptr::write_unaligned; use std::ptr::write_unaligned;
use std::slice; use std::slice;
use std::sync::Arc; use std::sync::Arc;
@ -59,23 +58,23 @@ fn get_function_addr(
} }
// TODO: To be removed. // TODO: To be removed.
#[derive(Debug)] // #[derive(Debug)]
#[repr(C, packed)] // #[repr(C, packed)]
pub struct VmCtx<'phantom> { // pub struct VmCtx<'phantom> {
pub user_data: UserData, // pub user_data: UserData,
globals: UncheckedSlice<u8>, // globals: UncheckedSlice<u8>,
memories: UncheckedSlice<UncheckedSlice<u8>>, // memories: UncheckedSlice<UncheckedSlice<u8>>,
tables: UncheckedSlice<BoundedSlice<usize>>, // tables: UncheckedSlice<BoundedSlice<usize>>,
phantom: PhantomData<&'phantom ()>, // phantom: PhantomData<&'phantom ()>,
} // }
// TODO: To be removed. // // TODO: To be removed.
#[derive(Debug)] // #[derive(Debug)]
#[repr(C, packed)] // #[repr(C, packed)]
pub struct UserData { // pub struct UserData {
// pub process: Dispatch<Process>, // // pub process: Dispatch<Process>,
pub instance: Instance, // pub instance: Instance,
} // }
/// An Instance of a WebAssembly module /// An Instance of a WebAssembly module
#[derive(Debug)] #[derive(Debug)]
@ -107,10 +106,9 @@ pub struct Instance {
// Default memory bound // Default memory bound
// TODO: Support for only one LinearMemory for now. // TODO: Support for only one LinearMemory for now.
pub default_memory_bound: i32 pub default_memory_bound: i32,
} }
/// Contains pointers to data (heaps, globals, tables) needed /// Contains pointers to data (heaps, globals, tables) needed
/// by Cranelift. /// by Cranelift.
#[derive(Debug)] #[derive(Debug)]
@ -464,36 +462,35 @@ impl Instance {
} }
// TODO: To be removed. // TODO: To be removed.
pub fn generate_context(&self) -> VmCtx { // pub fn generate_context(&self) -> VmCtx {
let memories: Vec<UncheckedSlice<u8>> = // let memories: Vec<UncheckedSlice<u8>> =
self.memories.iter().map(|mem| mem[..].into()).collect(); // self.memories.iter().map(|mem| mem[..].into()).collect();
let tables: Vec<BoundedSlice<usize>> = // let tables: Vec<BoundedSlice<usize>> =
self.tables.iter().map(|table| table[..].into()).collect(); // self.tables.iter().map(|table| table[..].into()).collect();
let globals: UncheckedSlice<u8> = self.globals[..].into(); // let globals: UncheckedSlice<u8> = self.globals[..].into();
// println!("GENERATING CONTEXT {:?}", self.globals); // // println!("GENERATING CONTEXT {:?}", self.globals);
// assert!(memories.len() >= 1, "modules must have at least one memory"); // // assert!(memories.len() >= 1, "modules must have at least one memory");
// the first memory has a space of `mem::size_of::<VmCtxData>()` rounded // // the first memory has a space of `mem::size_of::<VmCtxData>()` rounded
// up to the 4KiB before it. We write the VmCtxData into that. // // up to the 4KiB before it. We write the VmCtxData into that.
let instance = self.clone(); // let instance = self.clone();
let data = VmCtx { // VmCtx {
globals: globals, // globals: globals,
memories: memories[..].into(), // memories: memories[..].into(),
tables: tables[..].into(), // tables: tables[..].into(),
user_data: UserData { // user_data: UserData {
// process, // // process,
instance: instance, // instance: instance,
}, // },
phantom: PhantomData, // phantom: PhantomData,
}; // }
data // // let main_heap_ptr = memories[0].as_mut_ptr() as *mut VmCtxData;
// let main_heap_ptr = memories[0].as_mut_ptr() as *mut VmCtxData; // // unsafe {
// unsafe { // // main_heap_ptr.sub(1).write(data);
// main_heap_ptr.sub(1).write(data); // // &*(main_heap_ptr as *const VmCtx)
// &*(main_heap_ptr as *const VmCtx) // // }
// } // }
}
/// Returns a slice of the contents of allocated linear memory. /// Returns a slice of the contents of allocated linear memory.
pub fn inspect_memory(&self, memory_index: usize, address: usize, len: usize) -> &[u8] { pub fn inspect_memory(&self, memory_index: usize, address: usize, len: usize) -> &[u8] {
@ -531,8 +528,7 @@ impl Clone for Instance {
tables: tables_pointer[..].into(), tables: tables_pointer[..].into(),
}; };
let default_memory_bound = let default_memory_bound = self.memories.get(0).unwrap().current as i32;
self.memories.get(0).unwrap().current as i32;
Instance { Instance {
tables: Arc::clone(&self.tables), tables: Arc::clone(&self.tables),
@ -557,9 +553,13 @@ impl Clone for Instance {
/// Reference: /// Reference:
/// - https://cranelift.readthedocs.io/en/latest/ir.html?highlight=vmctx#heap-examples, /// - 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 { extern "C" fn grow_memory(size: u32, memory_index: u32, instance: &mut Instance) -> i32 {
// TODO: Support for only one LinearMemory for now. // TODO: Support for only one LinearMemory for now.
let memory_index: u32 = 0; debug_assert_eq!(
memory_index, 0,
"non-default memory_index (0) not supported yet"
);
let old_mem_size = instance let old_mem_size = instance
.memory_mut(memory_index as usize) .memory_mut(memory_index as usize)
.grow(size) .grow(size)
@ -571,12 +571,14 @@ extern "C" fn grow_memory(size: u32, memory_index: u32, instance: &mut Instance
// The grown memory changed so data_pointers need to be updated as well. // The grown memory changed so data_pointers need to be updated as well.
// TODO: Refactor repetitive code // TODO: Refactor repetitive code
let tables_pointer: Vec<BoundedSlice<usize>> = let tables_pointer: Vec<BoundedSlice<usize>> = instance
instance.tables.iter().map(|table| table[..].into()).collect(); .tables
.iter()
.map(|table| table[..].into())
.collect();
let memories_pointer: Vec<UncheckedSlice<u8>> = let memories_pointer: Vec<UncheckedSlice<u8>> =
instance.memories.iter().map(|mem| mem[..].into()).collect(); instance.memories.iter().map(|mem| mem[..].into()).collect();
let globals_pointer: UncheckedSlice<u8> = let globals_pointer: UncheckedSlice<u8> = instance.globals[..].into();
instance.globals[..].into();
let data_pointers = DataPointers { let data_pointers = DataPointers {
memories: memories_pointer[..].into(), memories: memories_pointer[..].into(),

View File

@ -1,7 +1,6 @@
//! Utility functions for the webassembly library //! Utility functions for the webassembly library
use super::instance::Instance; use super::instance::Instance;
use std::mem::transmute; use std::mem::transmute;
use super::super::common::slice::{UncheckedSlice, BoundedSlice};
/// Detect if a provided binary is a WASM file /// Detect if a provided binary is a WASM file
pub fn is_wasm_binary(binary: &Vec<u8>) -> bool { pub fn is_wasm_binary(binary: &Vec<u8>) -> bool {
@ -37,10 +36,15 @@ instance.data_pointers.globals \t- {:X} | offset - {:?}
instance.default_memory_bound \t- {:X} | offset - {:?} instance.default_memory_bound \t- {:X} | offset - {:?}
====== INSTANCE OFFSET TABLE ====== ====== INSTANCE OFFSET TABLE ======
", ",
instance_address, 0, instance_address,
tables_pointer_address, tables_pointer_address - instance_address, 0,
memories_pointer_address, memories_pointer_address - instance_address, tables_pointer_address,
globals_pointer_address, globals_pointer_address - instance_address, tables_pointer_address - instance_address,
default_memory_bound_address, default_memory_bound_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,
); );
} }