Fix formatting

This commit is contained in:
Lachlan Sneff 2018-12-26 16:55:11 -05:00
parent 53fab9c278
commit baf22b55be
14 changed files with 131 additions and 118 deletions

View File

@ -103,4 +103,4 @@ impl<'a, T> From<&'a [T]> for BoundedSlice<T> {
len: slice.len(),
}
}
}
}

View File

@ -0,0 +1 @@

View File

@ -1,4 +1,3 @@
pub mod cranelift;
pub static BACKENDS: &'static [&'static str] = &["cranelift"];

View File

@ -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;

View File

@ -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>;
}
}

View File

@ -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::<Vec<_>>().into_boxed_slice()
memories
.iter_mut()
.map(|mem| mem.into_vm_memory())
.collect::<Vec<_>>()
.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::<Vec<_>>().into_boxed_slice()
tables
.iter_mut()
.map(|table| table.into_vm_table())
.collect::<Vec<_>>()
.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<Self, String> {
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(),
})
}
}
}

View File

@ -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))
}
}
}

View File

@ -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 {
)
}
}
}
}

View File

@ -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<Box<Instance>, String> {
pub fn compile(
compiler: &dyn Compiler,
wasm: &[u8],
imports: &Imports,
) -> Result<Box<Instance>, String> {
let module = compiler.compile(wasm)?;
Instance::new(module, imports)
}
}

View File

@ -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<Memory, MemoryIndex>,
pub globals: Map<Global, GlobalIndex>,
pub tables: Map<Table, TableIndex>,
pub imported_functions: Map<ImportName, FuncIndex>,
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<u8>,
}
}

View File

@ -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<FuncSig, vm::SigId>,
@ -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);
}
}
}

View File

@ -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(),
},
}
}
}
}

View File

@ -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<T, I>
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<T, I>
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,
];
define_map_index![FuncIndex, MemoryIndex, GlobalIndex, TableIndex, SigIndex,];

View File

@ -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(),
);
}
}
}