1
0
mirror of https://github.com/fluencelabs/wasmer synced 2025-04-03 08:21:04 +00:00

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

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

@ -13,13 +13,13 @@ extern crate wasmparser;
extern crate target_lexicon; extern crate target_lexicon;
extern crate byteorder; extern crate byteorder;
extern crate console; extern crate console;
#[cfg_attr(test, macro_use)]
extern crate field_offset;
extern crate indicatif; extern crate indicatif;
pub extern crate nix; // re-exported for usage in macros pub extern crate nix; // re-exported for usage in macros
extern crate rayon; extern crate rayon;
#[cfg(windows)] #[cfg(windows)]
extern crate winapi; extern crate winapi;
#[cfg_attr(test, macro_use)]
extern crate field_offset;
#[macro_use] #[macro_use]
mod macros; mod macros;
@ -27,12 +27,12 @@ mod macros;
pub mod recovery; pub mod recovery;
pub mod apis; pub mod apis;
pub mod common; pub mod common;
pub mod compilers;
#[cfg(test)] #[cfg(test)]
mod emtests; mod emtests;
pub mod runtime;
pub mod sighandler; pub mod sighandler;
#[cfg(test)] #[cfg(test)]
mod spectests; mod spectests;
pub mod update; pub mod update;
pub mod webassembly; pub mod webassembly;
pub mod runtime;
pub mod compilers;

@ -1,9 +1,4 @@
use crate::runtime::{module::Module, types::FuncIndex, vm};
use crate::runtime::{
vm,
module::Module,
types::FuncIndex,
};
use std::sync::Arc; use std::sync::Arc;
pub trait Compiler { pub trait Compiler {

@ -1,13 +1,12 @@
use crate::runtime::{ use crate::runtime::{
vm, instance::{Import, Imports},
memory::LinearMemory,
module::Module, module::Module,
table::TableBacking, table::TableBacking,
types::{Val, GlobalInit, MapIndex}, types::{GlobalInit, MapIndex, Val},
memory::LinearMemory, vm,
instance::{Imports, Import},
}; };
#[derive(Debug)] #[derive(Debug)]
pub struct LocalBacking { pub struct LocalBacking {
memories: Box<[LinearMemory]>, memories: Box<[LinearMemory]>,
@ -43,10 +42,7 @@ impl LocalBacking {
for (_, mem) in &module.memories { for (_, mem) in &module.memories {
// If we use emscripten, we set a fixed initial and maximum // If we use emscripten, we set a fixed initial and maximum
debug!( debug!("Instance - init memory ({}, {:?})", mem.min, mem.max);
"Instance - init memory ({}, {:?})",
mem.min, mem.max
);
// let memory = if options.abi == InstanceABI::Emscripten { // let memory = if options.abi == InstanceABI::Emscripten {
// // We use MAX_PAGES, so at the end the result is: // // We use MAX_PAGES, so at the end the result is:
// // (initial * LinearMemory::PAGE_SIZE) == LinearMemory::DEFAULT_HEAP_SIZE // // (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]> { fn finalize_memories(module: &Module, memories: &mut [LinearMemory]) -> Box<[vm::LocalMemory]> {
for init in &module.data_initializers { for init in &module.data_initializers {
assert!(init.base.is_none(), "globalvar base not supported yet"); 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 offset = init.offset;
let mem: &mut LinearMemory = &mut memories[init.memory_index.index()]; let mem: &mut LinearMemory = &mut memories[init.memory_index.index()];
let end_of_init = offset + init.data.len(); let end_of_init = offset + init.data.len();
@ -78,7 +76,11 @@ impl LocalBacking {
to_init.copy_from_slice(&init.data); 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]> { fn generate_tables(module: &Module) -> Box<[TableBacking]> {
@ -94,7 +96,11 @@ impl LocalBacking {
// TODO: Actually finish this // TODO: Actually finish this
fn finalize_tables(module: &Module, tables: &mut [TableBacking]) -> Box<[vm::LocalTable]> { 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 // TODO: Actually finish this
@ -104,7 +110,11 @@ impl LocalBacking {
globals.into_boxed_slice() 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()) { for (to, (_, from)) in globals.iter_mut().zip(module.globals.into_iter()) {
to.data = match from.init { to.data = match from.init {
GlobalInit::Val(Val::I32(x)) => x as u64, GlobalInit::Val(Val::I32(x)) => x as u64,
@ -196,8 +206,14 @@ pub struct ImportBacking {
impl ImportBacking { impl ImportBacking {
pub fn new(module: &Module, imports: &Imports) -> Result<Self, String> { pub fn new(module: &Module, imports: &Imports) -> Result<Self, String> {
assert!(module.imported_memories.len() == 0, "imported memories not yet supported"); assert!(
assert!(module.imported_tables.len() == 0, "imported tables not yet supported"); 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()); let mut functions = Vec::with_capacity(module.imported_functions.len());
for (index, (mod_name, item_name)) in &module.imported_functions { for (index, (mod_name, item_name)) in &module.imported_functions {
@ -211,7 +227,10 @@ impl ImportBacking {
// vmctx: ptr::null_mut(), // vmctx: ptr::null_mut(),
}); });
} else { } else {
return Err(format!("unexpected signature for {:?}:{:?}", mod_name, item_name)); return Err(format!(
"unexpected signature for {:?}:{:?}",
mod_name, item_name
));
} }
} else { } else {
return Err(format!("incorrect type for {:?}:{:?}", mod_name, item_name)); return Err(format!("incorrect type for {:?}:{:?}", mod_name, item_name));
@ -234,7 +253,10 @@ impl ImportBacking {
}, },
}); });
} else { } else {
return Err(format!("unexpected global type for {:?}:{:?}", mod_name, item_name)); return Err(format!(
"unexpected global type for {:?}:{:?}",
mod_name, item_name
));
} }
} else { } else {
return Err(format!("incorrect type for {:?}:{:?}", mod_name, item_name)); return Err(format!("incorrect type for {:?}:{:?}", mod_name, item_name));

@ -1,14 +1,14 @@
use crate::runtime::{ use crate::runtime::{
vm, backing::{ImportBacking, LocalBacking},
backing::{LocalBacking, ImportBacking},
module::{Module, ModuleName, ItemName},
types::{Val, Memory, Table, FuncSig},
table::TableBacking,
memory::LinearMemory, memory::LinearMemory,
module::{ItemName, Module, ModuleName},
sig_registry::SigRegistry, sig_registry::SigRegistry,
table::TableBacking,
types::{FuncSig, Memory, Table, Val},
vm,
}; };
use std::sync::Arc;
use hashbrown::HashMap; use hashbrown::HashMap;
use std::sync::Arc;
pub struct Instance { pub struct Instance {
pub vmctx: vm::Ctx, pub vmctx: vm::Ctx,
@ -60,7 +60,10 @@ impl Imports {
} }
pub fn add(&mut self, module: ModuleName, name: ItemName, import: Import) { 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> { pub fn get(&self, module: &[u8], name: &[u8]) -> Option<&Import> {

@ -8,10 +8,7 @@ use std::ops::{Deref, DerefMut};
use std::slice; use std::slice;
use crate::common::mmap::Mmap; use crate::common::mmap::Mmap;
use crate::runtime::{ use crate::runtime::{types::Memory, vm::LocalMemory};
vm::LocalMemory,
types::Memory,
};
/// A linear memory instance. /// A linear memory instance.
#[derive(Debug)] #[derive(Debug)]
@ -54,15 +51,21 @@ impl LinearMemory {
assert!(mem.max.is_none() || mem.max.unwrap() <= Self::MAX_PAGES); assert!(mem.max.is_none() || mem.max.unwrap() <= Self::MAX_PAGES);
debug!("Instantiate LinearMemory(mem: {:?})", mem); debug!("Instantiate LinearMemory(mem: {:?})", mem);
let (mmap_size, initial_pages, offset_guard_size, requires_signal_catch) = if mem.is_static_heap() { let (mmap_size, initial_pages, offset_guard_size, requires_signal_catch) =
(Self::DEFAULT_SIZE, mem.min, Self::DEFAULT_GUARD_SIZE, true) if mem.is_static_heap() {
(Self::DEFAULT_SIZE, mem.min, Self::DEFAULT_GUARD_SIZE, true)
// This is a static heap // This is a static heap
} else { } else {
// this is a dynamic heap // this is a dynamic heap
assert!(!mem.shared, "shared memories must have a maximum size."); 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(); let mut mmap = Mmap::with_size(mmap_size).unwrap();
@ -154,11 +157,13 @@ impl LinearMemory {
new_mmap.as_mut_ptr(), new_mmap.as_mut_ptr(),
new_bytes, new_bytes,
region::Protection::ReadWrite, region::Protection::ReadWrite,
).ok()?; )
.ok()?;
} }
let copy_size = self.mmap.len() - self.offset_guard_size; 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; self.mmap = new_mmap;
} }
@ -201,7 +206,8 @@ impl LinearMemory {
self.mmap.as_ptr().add(prev_bytes) as _, self.mmap.as_ptr().add(prev_bytes) as _,
new_bytes - prev_bytes, new_bytes - prev_bytes,
region::Protection::ReadWrite, region::Protection::ReadWrite,
).ok()?; )
.ok()?;
} }
self.current = new_pages; self.current = new_pages;

@ -1,20 +1,23 @@
pub mod vm;
pub mod types;
mod backing;
mod memory;
mod backend; mod backend;
mod module; mod backing;
mod instance; mod instance;
mod table; mod memory;
mod module;
mod sig_registry; mod sig_registry;
mod table;
pub mod types;
pub mod vm;
pub use self::backend::{Compiler, FuncResolver}; pub use self::backend::{Compiler, FuncResolver};
pub use self::instance::{Instance, Imports, Import}; pub use self::instance::{Import, Imports, Instance};
pub use self::module::{ModuleName, ItemName, Module}; pub use self::module::{ItemName, Module, ModuleName};
/// Compile a webassembly module using the provided compiler and linked with the provided imports. /// 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)?; let module = compiler.compile(wasm)?;
Instance::new(module, imports) Instance::new(module, imports)
} }

@ -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::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. /// This is used to instantiate a new webassembly module.
pub struct Module { pub struct Module {

@ -1,9 +1,8 @@
use hashbrown::HashMap;
use crate::runtime::{ use crate::runtime::{
types::{Map, SigIndex, FuncSig}, types::{FuncSig, Map, SigIndex},
vm, vm,
}; };
use hashbrown::HashMap;
pub struct SigRegistry { pub struct SigRegistry {
sig_set: HashMap<FuncSig, vm::SigId>, sig_set: HashMap<FuncSig, vm::SigId>,
@ -24,9 +23,10 @@ impl SigRegistry {
pub fn register(&mut self, signature: FuncSig) { pub fn register(&mut self, signature: FuncSig) {
let index = self.sig_set.len(); let index = self.sig_set.len();
let vm_sig_id = self.sig_set.entry(signature).or_insert_with(|| { let vm_sig_id = self
vm::SigId(index as u32) .sig_set
}); .entry(signature)
.or_insert_with(|| vm::SigId(index as u32));
self.signatures.push(*vm_sig_id); self.signatures.push(*vm_sig_id);
} }
} }

@ -16,22 +16,20 @@ pub struct TableBacking {
impl TableBacking { impl TableBacking {
pub fn new(table: &Table) -> Self { pub fn new(table: &Table) -> Self {
match table.ty { match table.ty {
ElementType::Anyfunc => { ElementType::Anyfunc => Self {
Self { elements: TableElements::Anyfunc(
elements: TableElements::Anyfunc(vec![vm::Anyfunc::null(); table.min as usize].into_boxed_slice()), vec![vm::Anyfunc::null(); table.min as usize].into_boxed_slice(),
max: table.max, ),
} max: table.max,
} },
} }
} }
pub fn into_vm_table(&mut self) -> vm::LocalTable { pub fn into_vm_table(&mut self) -> vm::LocalTable {
match self.elements { match self.elements {
TableElements::Anyfunc(ref mut funcs) => { TableElements::Anyfunc(ref mut funcs) => vm::LocalTable {
vm::LocalTable { base: funcs.as_mut_ptr() as *mut u8,
base: funcs.as_mut_ptr() as *mut u8, current_elements: funcs.len(),
current_elements: funcs.len(),
}
}, },
} }
} }

@ -1,7 +1,8 @@
use std::marker::PhantomData; use std::marker::PhantomData;
use std::{ use std::{
slice, iter, iter,
ops::{Index, IndexMut}, ops::{Index, IndexMut},
slice,
}; };
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@ -198,7 +199,7 @@ where
impl<'a, T, I> IntoIterator for &'a Map<T, I> impl<'a, T, I> IntoIterator for &'a Map<T, I>
where where
I: MapIndex I: MapIndex,
{ {
type Item = (I, &'a T); type Item = (I, &'a T);
type IntoIter = Iter<'a, T, I>; type IntoIter = Iter<'a, T, I>;
@ -210,7 +211,7 @@ where
impl<'a, T, I> IntoIterator for &'a mut Map<T, I> impl<'a, T, I> IntoIterator for &'a mut Map<T, I>
where where
I: MapIndex I: MapIndex,
{ {
type Item = (I, &'a mut T); type Item = (I, &'a mut T);
type IntoIter = IterMut<'a, T, I>; type IntoIter = IterMut<'a, T, I>;
@ -285,7 +286,4 @@ macro_rules! define_map_index {
}; };
} }
define_map_index![ define_map_index![FuncIndex, MemoryIndex, GlobalIndex, TableIndex, SigIndex,];
FuncIndex, MemoryIndex, GlobalIndex, TableIndex,
SigIndex,
];

@ -1,12 +1,12 @@
use std::{ptr, mem};
use crate::runtime::{ use crate::runtime::{
// types::{ // types::{
// MemoryIndex, TableIndex, GlobalIndex, FuncIndex, // MemoryIndex, TableIndex, GlobalIndex, FuncIndex,
// SigIndex, // SigIndex,
// }, // },
backing::{LocalBacking, ImportBacking}, backing::{ImportBacking, LocalBacking},
sig_registry::SigRegistry, sig_registry::SigRegistry,
}; };
use std::{mem, ptr};
#[derive(Debug)] #[derive(Debug)]
#[repr(C)] #[repr(C)]
@ -192,9 +192,7 @@ impl LocalGlobal {
} }
pub fn null() -> Self { pub fn null() -> Self {
Self { Self { data: 0 }
data: 0,
}
} }
} }
@ -225,9 +223,7 @@ pub struct Anyfunc {
impl Anyfunc { impl Anyfunc {
pub fn null() -> Self { pub fn null() -> Self {
Self { Self {
func_data: ImportedFunc { func_data: ImportedFunc { func: ptr::null() },
func: ptr::null(),
},
sig_id: SigId(u32::max_value()), sig_id: SigId(u32::max_value()),
} }
} }
@ -248,15 +244,8 @@ impl Anyfunc {
#[cfg(test)] #[cfg(test)]
mod vm_offset_tests { mod vm_offset_tests {
use super::{ use super::{
Ctx, Anyfunc, Ctx, ImportedFunc, ImportedGlobal, ImportedMemory, ImportedTable, LocalGlobal,
ImportedFunc, LocalMemory, LocalTable,
LocalTable,
ImportedTable,
LocalMemory,
ImportedMemory,
LocalGlobal,
ImportedGlobal,
Anyfunc,
}; };
#[test] #[test]