From baf22b55bef019bba8e7c5794c08c6f0c0e29239 Mon Sep 17 00:00:00 2001 From: Lachlan Sneff Date: Wed, 26 Dec 2018 16:55:11 -0500 Subject: [PATCH] Fix formatting --- src/common/slice.rs | 2 +- src/compilers/cranelift/mod.rs | 1 + src/compilers/mod.rs | 1 - src/lib.rs | 8 ++--- src/runtime/backend.rs | 9 ++---- src/runtime/backing.rs | 58 +++++++++++++++++++++++----------- src/runtime/instance.rs | 21 ++++++------ src/runtime/memory.rs | 36 ++++++++++++--------- src/runtime/mod.rs | 25 ++++++++------- src/runtime/module.rs | 13 ++++---- src/runtime/sig_registry.rs | 14 ++++---- src/runtime/table.rs | 22 ++++++------- src/runtime/types.rs | 12 +++---- src/runtime/vm.rs | 27 +++++----------- 14 files changed, 131 insertions(+), 118 deletions(-) diff --git a/src/common/slice.rs b/src/common/slice.rs index 471b3b059..2fef84959 100644 --- a/src/common/slice.rs +++ b/src/common/slice.rs @@ -103,4 +103,4 @@ impl<'a, T> From<&'a [T]> for BoundedSlice { len: slice.len(), } } -} \ No newline at end of file +} diff --git a/src/compilers/cranelift/mod.rs b/src/compilers/cranelift/mod.rs index e69de29bb..8b1378917 100644 --- a/src/compilers/cranelift/mod.rs +++ b/src/compilers/cranelift/mod.rs @@ -0,0 +1 @@ + diff --git a/src/compilers/mod.rs b/src/compilers/mod.rs index 30521850f..f4f1519d5 100644 --- a/src/compilers/mod.rs +++ b/src/compilers/mod.rs @@ -1,4 +1,3 @@ - pub mod cranelift; pub static BACKENDS: &'static [&'static str] = &["cranelift"]; diff --git a/src/lib.rs b/src/lib.rs index 82a6451c7..45d902bd1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,13 +13,13 @@ extern crate wasmparser; extern crate target_lexicon; extern crate byteorder; extern crate console; +#[cfg_attr(test, macro_use)] +extern crate field_offset; extern crate indicatif; pub extern crate nix; // re-exported for usage in macros extern crate rayon; #[cfg(windows)] extern crate winapi; -#[cfg_attr(test, macro_use)] -extern crate field_offset; #[macro_use] mod macros; @@ -27,12 +27,12 @@ mod macros; pub mod recovery; pub mod apis; pub mod common; +pub mod compilers; #[cfg(test)] mod emtests; +pub mod runtime; pub mod sighandler; #[cfg(test)] mod spectests; pub mod update; pub mod webassembly; -pub mod runtime; -pub mod compilers; \ No newline at end of file diff --git a/src/runtime/backend.rs b/src/runtime/backend.rs index 5d227166c..18b5387c4 100644 --- a/src/runtime/backend.rs +++ b/src/runtime/backend.rs @@ -1,9 +1,4 @@ - -use crate::runtime::{ - vm, - module::Module, - types::FuncIndex, -}; +use crate::runtime::{module::Module, types::FuncIndex, vm}; use std::sync::Arc; pub trait Compiler { @@ -12,4 +7,4 @@ pub trait Compiler { pub trait FuncResolver { fn resolve(&self, index: FuncIndex) -> Option<*const vm::Func>; -} \ No newline at end of file +} diff --git a/src/runtime/backing.rs b/src/runtime/backing.rs index a5348c256..966ad7bf7 100644 --- a/src/runtime/backing.rs +++ b/src/runtime/backing.rs @@ -1,13 +1,12 @@ use crate::runtime::{ - vm, + instance::{Import, Imports}, + memory::LinearMemory, module::Module, table::TableBacking, - types::{Val, GlobalInit, MapIndex}, - memory::LinearMemory, - instance::{Imports, Import}, + types::{GlobalInit, MapIndex, Val}, + vm, }; - #[derive(Debug)] pub struct LocalBacking { memories: Box<[LinearMemory]>, @@ -43,10 +42,7 @@ impl LocalBacking { for (_, mem) in &module.memories { // If we use emscripten, we set a fixed initial and maximum - debug!( - "Instance - init memory ({}, {:?})", - mem.min, mem.max - ); + debug!("Instance - init memory ({}, {:?})", mem.min, mem.max); // let memory = if options.abi == InstanceABI::Emscripten { // // We use MAX_PAGES, so at the end the result is: // // (initial * LinearMemory::PAGE_SIZE) == LinearMemory::DEFAULT_HEAP_SIZE @@ -65,7 +61,9 @@ impl LocalBacking { fn finalize_memories(module: &Module, memories: &mut [LinearMemory]) -> Box<[vm::LocalMemory]> { for init in &module.data_initializers { assert!(init.base.is_none(), "globalvar base not supported yet"); - assert!(init.offset + init.data.len() <= memories[init.memory_index.index()].current_size()); + assert!( + init.offset + init.data.len() <= memories[init.memory_index.index()].current_size() + ); let offset = init.offset; let mem: &mut LinearMemory = &mut memories[init.memory_index.index()]; let end_of_init = offset + init.data.len(); @@ -78,7 +76,11 @@ impl LocalBacking { to_init.copy_from_slice(&init.data); } - memories.iter_mut().map(|mem| mem.into_vm_memory()).collect::>().into_boxed_slice() + memories + .iter_mut() + .map(|mem| mem.into_vm_memory()) + .collect::>() + .into_boxed_slice() } fn generate_tables(module: &Module) -> Box<[TableBacking]> { @@ -94,7 +96,11 @@ impl LocalBacking { // TODO: Actually finish this fn finalize_tables(module: &Module, tables: &mut [TableBacking]) -> Box<[vm::LocalTable]> { - tables.iter_mut().map(|table| table.into_vm_table()).collect::>().into_boxed_slice() + tables + .iter_mut() + .map(|table| table.into_vm_table()) + .collect::>() + .into_boxed_slice() } // TODO: Actually finish this @@ -104,7 +110,11 @@ impl LocalBacking { globals.into_boxed_slice() } - fn finalize_globals(module: &Module, imports: &ImportBacking, mut globals: Box<[vm::LocalGlobal]>) -> Box<[vm::LocalGlobal]> { + fn finalize_globals( + module: &Module, + imports: &ImportBacking, + mut globals: Box<[vm::LocalGlobal]>, + ) -> Box<[vm::LocalGlobal]> { for (to, (_, from)) in globals.iter_mut().zip(module.globals.into_iter()) { to.data = match from.init { GlobalInit::Val(Val::I32(x)) => x as u64, @@ -196,8 +206,14 @@ pub struct ImportBacking { impl ImportBacking { pub fn new(module: &Module, imports: &Imports) -> Result { - assert!(module.imported_memories.len() == 0, "imported memories not yet supported"); - assert!(module.imported_tables.len() == 0, "imported tables not yet supported"); + assert!( + module.imported_memories.len() == 0, + "imported memories not yet supported" + ); + assert!( + module.imported_tables.len() == 0, + "imported tables not yet supported" + ); let mut functions = Vec::with_capacity(module.imported_functions.len()); for (index, (mod_name, item_name)) in &module.imported_functions { @@ -211,7 +227,10 @@ impl ImportBacking { // vmctx: ptr::null_mut(), }); } else { - return Err(format!("unexpected signature for {:?}:{:?}", mod_name, item_name)); + return Err(format!( + "unexpected signature for {:?}:{:?}", + mod_name, item_name + )); } } else { return Err(format!("incorrect type for {:?}:{:?}", mod_name, item_name)); @@ -234,7 +253,10 @@ impl ImportBacking { }, }); } else { - return Err(format!("unexpected global type for {:?}:{:?}", mod_name, item_name)); + return Err(format!( + "unexpected global type for {:?}:{:?}", + mod_name, item_name + )); } } else { return Err(format!("incorrect type for {:?}:{:?}", mod_name, item_name)); @@ -248,4 +270,4 @@ impl ImportBacking { globals: globals.into_boxed_slice(), }) } -} \ No newline at end of file +} diff --git a/src/runtime/instance.rs b/src/runtime/instance.rs index 4bb3b98f4..c2eafc873 100644 --- a/src/runtime/instance.rs +++ b/src/runtime/instance.rs @@ -1,14 +1,14 @@ use crate::runtime::{ - vm, - backing::{LocalBacking, ImportBacking}, - module::{Module, ModuleName, ItemName}, - types::{Val, Memory, Table, FuncSig}, - table::TableBacking, + backing::{ImportBacking, LocalBacking}, memory::LinearMemory, + module::{ItemName, Module, ModuleName}, sig_registry::SigRegistry, + table::TableBacking, + types::{FuncSig, Memory, Table, Val}, + vm, }; -use std::sync::Arc; use hashbrown::HashMap; +use std::sync::Arc; pub struct Instance { pub vmctx: vm::Ctx, @@ -29,7 +29,7 @@ impl Instance { let sig_registry = SigRegistry::new(); let vmctx = vm::Ctx::new(&mut backing, &mut import_backing, &sig_registry); - + Ok(Box::new(Instance { vmctx, backing, @@ -60,10 +60,13 @@ impl Imports { } pub fn add(&mut self, module: ModuleName, name: ItemName, import: Import) { - self.map.entry(module).or_insert(HashMap::new()).insert(name, import); + self.map + .entry(module) + .or_insert(HashMap::new()) + .insert(name, import); } pub fn get(&self, module: &[u8], name: &[u8]) -> Option<&Import> { self.map.get(module).and_then(|m| m.get(name)) } -} \ No newline at end of file +} diff --git a/src/runtime/memory.rs b/src/runtime/memory.rs index a09caa9f3..63ec4c577 100644 --- a/src/runtime/memory.rs +++ b/src/runtime/memory.rs @@ -8,10 +8,7 @@ use std::ops::{Deref, DerefMut}; use std::slice; use crate::common::mmap::Mmap; -use crate::runtime::{ - vm::LocalMemory, - types::Memory, -}; +use crate::runtime::{types::Memory, vm::LocalMemory}; /// A linear memory instance. #[derive(Debug)] @@ -54,15 +51,21 @@ impl LinearMemory { assert!(mem.max.is_none() || mem.max.unwrap() <= Self::MAX_PAGES); debug!("Instantiate LinearMemory(mem: {:?})", mem); - let (mmap_size, initial_pages, offset_guard_size, requires_signal_catch) = if mem.is_static_heap() { - (Self::DEFAULT_SIZE, mem.min, Self::DEFAULT_GUARD_SIZE, true) + let (mmap_size, initial_pages, offset_guard_size, requires_signal_catch) = + if mem.is_static_heap() { + (Self::DEFAULT_SIZE, mem.min, Self::DEFAULT_GUARD_SIZE, true) // This is a static heap - } else { - // this is a dynamic heap - assert!(!mem.shared, "shared memories must have a maximum size."); + } else { + // this is a dynamic heap + assert!(!mem.shared, "shared memories must have a maximum size."); - (mem.min as usize * Self::PAGE_SIZE as usize, mem.min, 0, false) - }; + ( + mem.min as usize * Self::PAGE_SIZE as usize, + mem.min, + 0, + false, + ) + }; let mut mmap = Mmap::with_size(mmap_size).unwrap(); @@ -154,11 +157,13 @@ impl LinearMemory { new_mmap.as_mut_ptr(), new_bytes, region::Protection::ReadWrite, - ).ok()?; + ) + .ok()?; } let copy_size = self.mmap.len() - self.offset_guard_size; - new_mmap.as_mut_slice()[..copy_size].copy_from_slice(&self.mmap.as_slice()[..copy_size]); + new_mmap.as_mut_slice()[..copy_size] + .copy_from_slice(&self.mmap.as_slice()[..copy_size]); self.mmap = new_mmap; } @@ -201,7 +206,8 @@ impl LinearMemory { self.mmap.as_ptr().add(prev_bytes) as _, new_bytes - prev_bytes, region::Protection::ReadWrite, - ).ok()?; + ) + .ok()?; } self.current = new_pages; @@ -238,4 +244,4 @@ impl DerefMut for LinearMemory { ) } } -} \ No newline at end of file +} diff --git a/src/runtime/mod.rs b/src/runtime/mod.rs index 718932c62..6396cbf14 100644 --- a/src/runtime/mod.rs +++ b/src/runtime/mod.rs @@ -1,20 +1,23 @@ - -pub mod vm; -pub mod types; -mod backing; -mod memory; mod backend; -mod module; +mod backing; mod instance; -mod table; +mod memory; +mod module; mod sig_registry; +mod table; +pub mod types; +pub mod vm; pub use self::backend::{Compiler, FuncResolver}; -pub use self::instance::{Instance, Imports, Import}; -pub use self::module::{ModuleName, ItemName, Module}; +pub use self::instance::{Import, Imports, Instance}; +pub use self::module::{ItemName, Module, ModuleName}; /// Compile a webassembly module using the provided compiler and linked with the provided imports. -pub fn compile(compiler: &dyn Compiler, wasm: &[u8], imports: &Imports) -> Result, String> { +pub fn compile( + compiler: &dyn Compiler, + wasm: &[u8], + imports: &Imports, +) -> Result, String> { let module = compiler.compile(wasm)?; Instance::new(module, imports) -} \ No newline at end of file +} diff --git a/src/runtime/module.rs b/src/runtime/module.rs index 84828e6b6..4afb15bd7 100644 --- a/src/runtime/module.rs +++ b/src/runtime/module.rs @@ -1,9 +1,8 @@ -use crate::runtime::types::{ - Map, - FuncIndex, MemoryIndex, TableIndex, GlobalIndex, SigIndex, - Memory, Global, GlobalDesc, FuncSig, Table, -}; use crate::runtime::backend::FuncResolver; +use crate::runtime::types::{ + FuncIndex, FuncSig, Global, GlobalDesc, GlobalIndex, Map, Memory, MemoryIndex, SigIndex, Table, + TableIndex, +}; /// This is used to instantiate a new webassembly module. pub struct Module { @@ -11,7 +10,7 @@ pub struct Module { pub memories: Map, pub globals: Map, pub tables: Map, - + pub imported_functions: Map, pub imported_memories: Map<(ImportName, Memory), MemoryIndex>, pub imported_tables: Map<(ImportName, Table), TableIndex>, @@ -49,4 +48,4 @@ pub struct DataInitializer { pub offset: usize, /// The initialization data. pub data: Vec, -} \ No newline at end of file +} diff --git a/src/runtime/sig_registry.rs b/src/runtime/sig_registry.rs index f862f555c..cc3236c5e 100644 --- a/src/runtime/sig_registry.rs +++ b/src/runtime/sig_registry.rs @@ -1,9 +1,8 @@ - -use hashbrown::HashMap; use crate::runtime::{ - types::{Map, SigIndex, FuncSig}, + types::{FuncSig, Map, SigIndex}, vm, }; +use hashbrown::HashMap; pub struct SigRegistry { sig_set: HashMap, @@ -24,9 +23,10 @@ impl SigRegistry { pub fn register(&mut self, signature: FuncSig) { let index = self.sig_set.len(); - let vm_sig_id = self.sig_set.entry(signature).or_insert_with(|| { - vm::SigId(index as u32) - }); + let vm_sig_id = self + .sig_set + .entry(signature) + .or_insert_with(|| vm::SigId(index as u32)); self.signatures.push(*vm_sig_id); } -} \ No newline at end of file +} diff --git a/src/runtime/table.rs b/src/runtime/table.rs index 7bbe872d1..1fd2d5a99 100644 --- a/src/runtime/table.rs +++ b/src/runtime/table.rs @@ -16,23 +16,21 @@ pub struct TableBacking { impl TableBacking { pub fn new(table: &Table) -> Self { match table.ty { - ElementType::Anyfunc => { - Self { - elements: TableElements::Anyfunc(vec![vm::Anyfunc::null(); table.min as usize].into_boxed_slice()), - max: table.max, - } - } + ElementType::Anyfunc => Self { + elements: TableElements::Anyfunc( + vec![vm::Anyfunc::null(); table.min as usize].into_boxed_slice(), + ), + max: table.max, + }, } } pub fn into_vm_table(&mut self) -> vm::LocalTable { match self.elements { - TableElements::Anyfunc(ref mut funcs) => { - vm::LocalTable { - base: funcs.as_mut_ptr() as *mut u8, - current_elements: funcs.len(), - } + TableElements::Anyfunc(ref mut funcs) => vm::LocalTable { + base: funcs.as_mut_ptr() as *mut u8, + current_elements: funcs.len(), }, } } -} \ No newline at end of file +} diff --git a/src/runtime/types.rs b/src/runtime/types.rs index 0c31485ae..f863d411d 100644 --- a/src/runtime/types.rs +++ b/src/runtime/types.rs @@ -1,7 +1,8 @@ use std::marker::PhantomData; use std::{ - slice, iter, + iter, ops::{Index, IndexMut}, + slice, }; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] @@ -198,7 +199,7 @@ where impl<'a, T, I> IntoIterator for &'a Map where - I: MapIndex + I: MapIndex, { type Item = (I, &'a T); type IntoIter = Iter<'a, T, I>; @@ -210,7 +211,7 @@ where impl<'a, T, I> IntoIterator for &'a mut Map where - I: MapIndex + I: MapIndex, { type Item = (I, &'a mut T); type IntoIter = IterMut<'a, T, I>; @@ -285,7 +286,4 @@ macro_rules! define_map_index { }; } -define_map_index![ - FuncIndex, MemoryIndex, GlobalIndex, TableIndex, - SigIndex, -]; \ No newline at end of file +define_map_index![FuncIndex, MemoryIndex, GlobalIndex, TableIndex, SigIndex,]; diff --git a/src/runtime/vm.rs b/src/runtime/vm.rs index 832ba1edb..28f791304 100644 --- a/src/runtime/vm.rs +++ b/src/runtime/vm.rs @@ -1,12 +1,12 @@ -use std::{ptr, mem}; use crate::runtime::{ // types::{ // MemoryIndex, TableIndex, GlobalIndex, FuncIndex, // SigIndex, // }, - backing::{LocalBacking, ImportBacking}, + backing::{ImportBacking, LocalBacking}, sig_registry::SigRegistry, }; +use std::{mem, ptr}; #[derive(Debug)] #[repr(C)] @@ -28,7 +28,7 @@ pub struct Ctx { /// A pointer to an array of imported globals, indexed by `GlobalIndex`. pub imported_globals: *mut ImportedGlobal, - + /// A pointer to an array of imported functions, indexed by `FuncIndex`. pub imported_funcs: *mut ImportedFunc, @@ -192,9 +192,7 @@ impl LocalGlobal { } pub fn null() -> Self { - Self { - data: 0, - } + Self { data: 0 } } } @@ -225,9 +223,7 @@ pub struct Anyfunc { impl Anyfunc { pub fn null() -> Self { Self { - func_data: ImportedFunc { - func: ptr::null(), - }, + func_data: ImportedFunc { func: ptr::null() }, sig_id: SigId(u32::max_value()), } } @@ -248,15 +244,8 @@ impl Anyfunc { #[cfg(test)] mod vm_offset_tests { use super::{ - Ctx, - ImportedFunc, - LocalTable, - ImportedTable, - LocalMemory, - ImportedMemory, - LocalGlobal, - ImportedGlobal, - Anyfunc, + Anyfunc, Ctx, ImportedFunc, ImportedGlobal, ImportedMemory, ImportedTable, LocalGlobal, + LocalMemory, LocalTable, }; #[test] @@ -395,4 +384,4 @@ mod vm_offset_tests { offset_of!(Anyfunc => sig_id).get_byte_offset(), ); } -} \ No newline at end of file +}