Clean up misc. bits of runtime-core

This commit is contained in:
Mark McCaskey
2020-01-24 14:55:02 -08:00
parent ddaaa30e2d
commit 0a02f3bb97
9 changed files with 44 additions and 79 deletions

38
Cargo.lock generated
View File

@ -581,7 +581,7 @@ dependencies = [
"libc", "libc",
"llvm-sys", "llvm-sys",
"once_cell", "once_cell",
"parking_lot 0.10.0", "parking_lot",
"regex", "regex",
] ]
@ -850,17 +850,6 @@ dependencies = [
"md5", "md5",
] ]
[[package]]
name = "parking_lot"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f842b1982eb6c2fe34036a4fbfb06dd185a3f5c8edfaacdf7d1ea10b07de6252"
dependencies = [
"lock_api",
"parking_lot_core 0.6.2",
"rustc_version",
]
[[package]] [[package]]
name = "parking_lot" name = "parking_lot"
version = "0.10.0" version = "0.10.0"
@ -868,22 +857,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "92e98c49ab0b7ce5b222f2cc9193fc4efe11c6d0bd4f648e374684a6857b1cfc" checksum = "92e98c49ab0b7ce5b222f2cc9193fc4efe11c6d0bd4f648e374684a6857b1cfc"
dependencies = [ dependencies = [
"lock_api", "lock_api",
"parking_lot_core 0.7.0", "parking_lot_core",
]
[[package]]
name = "parking_lot_core"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b876b1b9e7ac6e1a74a6da34d25c42e17e8862aa409cbbbdcfc8d86c6f3bc62b"
dependencies = [
"cfg-if",
"cloudabi",
"libc",
"redox_syscall",
"rustc_version",
"smallvec 0.6.13",
"winapi",
] ]
[[package]] [[package]]
@ -1799,7 +1773,7 @@ dependencies = [
"regex", "regex",
"rustc_version", "rustc_version",
"semver", "semver",
"smallvec 1.1.0", "smallvec 0.6.13",
"wabt", "wabt",
"wasmer-runtime-core", "wasmer-runtime-core",
"wasmparser", "wasmparser",
@ -1880,13 +1854,13 @@ dependencies = [
"libc", "libc",
"nix", "nix",
"page_size", "page_size",
"parking_lot 0.9.0", "parking_lot",
"rustc_version", "rustc_version",
"serde", "serde",
"serde-bench", "serde-bench",
"serde_bytes", "serde_bytes",
"serde_derive", "serde_derive",
"smallvec 1.1.0", "smallvec 0.6.13",
"wasmparser", "wasmparser",
"winapi", "winapi",
] ]
@ -1915,7 +1889,7 @@ dependencies = [
"nix", "nix",
"serde", "serde",
"serde_derive", "serde_derive",
"smallvec 1.1.0", "smallvec 0.6.13",
"wasmer-runtime-core", "wasmer-runtime-core",
] ]

View File

@ -745,7 +745,7 @@ fn import_memories(
} }
} }
if link_errors.len() > 0 { if !link_errors.is_empty() {
Err(link_errors) Err(link_errors)
} else { } else {
Ok((memories.into_boxed_map(), vm_memories.into_boxed_map())) Ok((memories.into_boxed_map(), vm_memories.into_boxed_map()))
@ -886,7 +886,7 @@ fn import_globals(
} }
} }
if link_errors.len() > 0 { if !link_errors.is_empty() {
Err(link_errors) Err(link_errors)
} else { } else {
Ok((globals.into_boxed_map(), vm_globals.into_boxed_map())) Ok((globals.into_boxed_map(), vm_globals.into_boxed_map()))

View File

@ -257,10 +257,8 @@ pub fn allocate_and_run<R, F: FnOnce() -> R>(size: usize, f: F) -> R {
// NOTE: Keep this consistent with `image-loading-*.s`. // NOTE: Keep this consistent with `image-loading-*.s`.
stack[end_offset - 4 - 10] = &mut ctx as *mut Context<F, R> as usize as u64; // rdi stack[end_offset - 4 - 10] = &mut ctx as *mut Context<F, R> as usize as u64; // rdi
const NUM_SAVED_REGISTERS: usize = 31; const NUM_SAVED_REGISTERS: usize = 31;
let stack_begin = stack let stack_begin = stack.as_mut_ptr().add(end_offset - 4 - NUM_SAVED_REGISTERS);
.as_mut_ptr() let stack_end = stack.as_mut_ptr().add(end_offset);
.offset((end_offset - 4 - NUM_SAVED_REGISTERS) as isize);
let stack_end = stack.as_mut_ptr().offset(end_offset as isize);
raw::run_on_alternative_stack(stack_end, stack_begin); raw::run_on_alternative_stack(stack_end, stack_begin);
ctx.ret.take().unwrap() ctx.ret.take().unwrap()
@ -392,7 +390,7 @@ extern "C" fn signal_trap_handler(
unwind_result = Box::new(image); unwind_result = Box::new(image);
} else { } else {
// Otherwise, this is a real exception and we just throw it to the caller. // Otherwise, this is a real exception and we just throw it to the caller.
if es_image.frames.len() > 0 { if !es_image.frames.is_empty() {
eprintln!( eprintln!(
"\n{}", "\n{}",
"Wasmer encountered an error while running your WebAssembly program." "Wasmer encountered an error while running your WebAssembly program."

View File

@ -21,7 +21,7 @@ use smallvec::{smallvec, SmallVec};
use std::{ use std::{
mem, mem,
pin::Pin, pin::Pin,
ptr::NonNull, ptr::{self, NonNull},
sync::{Arc, Mutex}, sync::{Arc, Mutex},
}; };
@ -710,7 +710,7 @@ pub(crate) fn call_func_with_index_inner(
match signature.returns() { match signature.returns() {
&[] => { &[] => {
run_wasm(0 as *mut u64)?; run_wasm(ptr::null_mut())?;
Ok(()) Ok(())
} }
&[Type::V128] => { &[Type::V128] => {
@ -721,10 +721,8 @@ pub(crate) fn call_func_with_index_inner(
let mut bytes = [0u8; 16]; let mut bytes = [0u8; 16];
let lo = result[0].to_le_bytes(); let lo = result[0].to_le_bytes();
let hi = result[1].to_le_bytes(); let hi = result[1].to_le_bytes();
for i in 0..8 { bytes[..8].clone_from_slice(&lo);
bytes[i] = lo[i]; bytes[8..16].clone_from_slice(&hi);
bytes[i + 8] = hi[i];
}
rets.push(Value::V128(u128::from_le_bytes(bytes))); rets.push(Value::V128(u128::from_le_bytes(bytes)));
Ok(()) Ok(())
} }

View File

@ -81,7 +81,7 @@ impl<T: Copy + ValueType> WasmPtr<T, Item> {
impl<T: Copy + ValueType> WasmPtr<T, Array> { impl<T: Copy + ValueType> WasmPtr<T, Array> {
/// Dereference this `WasmPtr`. /// Dereference this `WasmPtr`.
#[inline] #[inline]
pub fn deref<'a>(self, memory: &'a Memory, index: u32, length: u32) -> Option<&'a [Cell<T>]> { pub fn deref(self, memory: &Memory, index: u32, length: u32) -> Option<&[Cell<T>]> {
// gets the size of the item in the array with padding added such that // gets the size of the item in the array with padding added such that
// for any index, we will always result an aligned memory access // for any index, we will always result an aligned memory access
let item_size = mem::size_of::<T>() + (mem::size_of::<T>() % mem::align_of::<T>()); let item_size = mem::size_of::<T>() + (mem::size_of::<T>() % mem::align_of::<T>());
@ -104,12 +104,12 @@ impl<T: Copy + ValueType> WasmPtr<T, Array> {
/// Mutable dereference this `WasmPtr`. /// Mutable dereference this `WasmPtr`.
#[inline] #[inline]
pub unsafe fn deref_mut<'a>( pub unsafe fn deref_mut(
self, self,
memory: &'a Memory, memory: &Memory,
index: u32, index: u32,
length: u32, length: u32,
) -> Option<&'a mut [Cell<T>]> { ) -> Option<&mut [Cell<T>]> {
// gets the size of the item in the array with padding added such that // gets the size of the item in the array with padding added such that
// for any index, we will always result an aligned memory access // for any index, we will always result an aligned memory access
let item_size = mem::size_of::<T>() + (mem::size_of::<T>() % mem::align_of::<T>()); let item_size = mem::size_of::<T>() + (mem::size_of::<T>() % mem::align_of::<T>());
@ -129,7 +129,7 @@ impl<T: Copy + ValueType> WasmPtr<T, Array> {
} }
/// Get a UTF-8 string representation of this `WasmPtr` with the given length. /// Get a UTF-8 string representation of this `WasmPtr` with the given length.
pub fn get_utf8_string<'a>(self, memory: &'a Memory, str_len: u32) -> Option<&'a str> { pub fn get_utf8_string(self, memory: &Memory, str_len: u32) -> Option<&str> {
if self.offset as usize + str_len as usize > memory.size().bytes().0 { if self.offset as usize + str_len as usize > memory.size().bytes().0 {
return None; return None;
} }
@ -141,7 +141,7 @@ impl<T: Copy + ValueType> WasmPtr<T, Array> {
/// Get a UTF-8 string representation of this `WasmPtr`, where the string is nul-terminated. /// Get a UTF-8 string representation of this `WasmPtr`, where the string is nul-terminated.
/// Note that this does not account for UTF-8 strings that _contain_ nul themselves, /// Note that this does not account for UTF-8 strings that _contain_ nul themselves,
/// [`get_utf8_string`] has to be used for those. /// [`get_utf8_string`] has to be used for those.
pub fn get_utf8_string_with_nul<'a>(self, memory: &'a Memory) -> Option<&'a str> { pub fn get_utf8_string_with_nul(self, memory: &Memory) -> Option<&str> {
memory.view::<u8>()[(self.offset as usize)..] memory.view::<u8>()[(self.offset as usize)..]
.iter() .iter()
.map(|cell| cell.get()) .map(|cell| cell.get())

View File

@ -145,7 +145,7 @@ pub fn read_module<
ImportSectionEntryType::Memory(memory_ty) => { ImportSectionEntryType::Memory(memory_ty) => {
let mem_desc = MemoryDescriptor::new( let mem_desc = MemoryDescriptor::new(
Pages(memory_ty.limits.initial), Pages(memory_ty.limits.initial),
memory_ty.limits.maximum.map(|max| Pages(max)), memory_ty.limits.maximum.map(Pages),
memory_ty.shared, memory_ty.shared,
) )
.map_err(|x| LoadError::Codegen(format!("{:?}", x)))?; .map_err(|x| LoadError::Codegen(format!("{:?}", x)))?;
@ -183,7 +183,7 @@ pub fn read_module<
ParserState::MemorySectionEntry(memory_ty) => { ParserState::MemorySectionEntry(memory_ty) => {
let mem_desc = MemoryDescriptor::new( let mem_desc = MemoryDescriptor::new(
Pages(memory_ty.limits.initial), Pages(memory_ty.limits.initial),
memory_ty.limits.maximum.map(|max| Pages(max)), memory_ty.limits.maximum.map(Pages),
memory_ty.shared, memory_ty.shared,
) )
.map_err(|x| LoadError::Codegen(format!("{:?}", x)))?; .map_err(|x| LoadError::Codegen(format!("{:?}", x)))?;
@ -271,11 +271,11 @@ pub fn read_module<
Event::Internal(InternalEvent::FunctionBegin(id as u32)), Event::Internal(InternalEvent::FunctionBegin(id as u32)),
&info.read().unwrap(), &info.read().unwrap(),
) )
.map_err(|x| LoadError::Codegen(x))?; .map_err(LoadError::Codegen)?;
} }
middlewares middlewares
.run(Some(fcg), Event::Wasm(op), &info.read().unwrap()) .run(Some(fcg), Event::Wasm(op), &info.read().unwrap())
.map_err(|x| LoadError::Codegen(x))?; .map_err(LoadError::Codegen)?;
} }
ParserState::EndFunctionBody => break, ParserState::EndFunctionBody => break,
_ => unreachable!(), _ => unreachable!(),
@ -287,7 +287,7 @@ pub fn read_module<
Event::Internal(InternalEvent::FunctionEnd), Event::Internal(InternalEvent::FunctionEnd),
&info.read().unwrap(), &info.read().unwrap(),
) )
.map_err(|x| LoadError::Codegen(x))?; .map_err(LoadError::Codegen)?;
fcg.finalize() fcg.finalize()
.map_err(|x| LoadError::Codegen(format!("{:?}", x)))?; .map_err(|x| LoadError::Codegen(format!("{:?}", x)))?;
func_count = func_count.wrapping_add(1); func_count = func_count.wrapping_add(1);

View File

@ -1,3 +1,4 @@
//! A process-global registry for function signatures.
use crate::{ use crate::{
structures::Map, structures::Map,
types::{FuncSig, SigIndex}, types::{FuncSig, SigIndex},

View File

@ -417,7 +417,7 @@ impl ExecutionStateImage {
} }
fn format_optional_u64_sequence(x: &[Option<u64>]) -> String { fn format_optional_u64_sequence(x: &[Option<u64>]) -> String {
if x.len() == 0 { if x.is_empty() {
"(empty)".into() "(empty)".into()
} else { } else {
join_strings( join_strings(
@ -436,7 +436,7 @@ impl ExecutionStateImage {
let mut ret = String::new(); let mut ret = String::new();
if self.frames.len() == 0 { if self.frames.is_empty() {
ret += &"Unknown fault address, cannot read stack."; ret += &"Unknown fault address, cannot read stack.";
ret += "\n"; ret += "\n";
} else { } else {
@ -632,7 +632,7 @@ pub mod x64 {
let mut ptr = &vmctx as *const *const Ctx as *const u8; let mut ptr = &vmctx as *const *const Ctx as *const u8;
for x in seq { for x in seq {
debug_assert!(ptr.align_offset(std::mem::align_of::<*const u8>()) == 0); debug_assert!(ptr.align_offset(std::mem::align_of::<*const u8>()) == 0);
ptr = (*(ptr as *const *const u8)).offset(*x as isize); ptr = (*(ptr as *const *const u8)).add(*x);
} }
ptr as usize as u64 ptr as usize as u64
} }
@ -679,7 +679,7 @@ pub mod x64 {
} else { } else {
fsm.wasm_offset_to_target_offset fsm.wasm_offset_to_target_offset
.get(&f.wasm_inst_offset) .get(&f.wasm_inst_offset)
.map(|x| *x) .copied()
} }
.expect("instruction is not a critical point"); .expect("instruction is not a critical point");
@ -994,8 +994,8 @@ pub mod x64 {
catch_unsafe_unwind( catch_unsafe_unwind(
|| { || {
run_on_alternative_stack( run_on_alternative_stack(
stack.as_mut_ptr().offset(stack.len() as isize), stack.as_mut_ptr().add(stack.len()),
stack.as_mut_ptr().offset(stack_offset as isize), stack.as_mut_ptr().add(stack_offset),
) )
}, },
breakpoints, breakpoints,
@ -1157,24 +1157,18 @@ pub mod x64 {
} }
} }
let mut found_shadow = false; let found_shadow = state
for v in state.stack_values.iter() { .stack_values
match *v { .iter()
MachineValue::ExplicitShadow => { .any(|v| *v == MachineValue::ExplicitShadow);
found_shadow = true;
break;
}
_ => {}
}
}
if !found_shadow { if !found_shadow {
stack = stack.offset((fsm.shadow_size / 8) as isize); stack = stack.add(fsm.shadow_size / 8);
} }
for v in state.stack_values.iter().rev() { for v in state.stack_values.iter().rev() {
match *v { match *v {
MachineValue::ExplicitShadow => { MachineValue::ExplicitShadow => {
stack = stack.offset((fsm.shadow_size / 8) as isize); stack = stack.add(fsm.shadow_size / 8);
} }
MachineValue::Undefined => { MachineValue::Undefined => {
stack = stack.offset(1); stack = stack.offset(1);

View File

@ -231,8 +231,8 @@ pub static INTRINSICS_IMPORTED_DYNAMIC_MEMORY: Intrinsics = Intrinsics {
}; };
fn get_intrinsics_for_module(m: &ModuleInfo) -> *const Intrinsics { fn get_intrinsics_for_module(m: &ModuleInfo) -> *const Intrinsics {
if m.memories.len() == 0 && m.imported_memories.len() == 0 { if m.memories.is_empty() && m.imported_memories.is_empty() {
::std::ptr::null() ptr::null()
} else { } else {
match MemoryIndex::new(0).local_or_import(m) { match MemoryIndex::new(0).local_or_import(m) {
LocalOrImport::Local(local_mem_index) => { LocalOrImport::Local(local_mem_index) => {
@ -274,7 +274,7 @@ impl Ctx {
module: &ModuleInner, module: &ModuleInner,
) -> Self { ) -> Self {
let (mem_base, mem_bound): (*mut u8, usize) = let (mem_base, mem_bound): (*mut u8, usize) =
if module.info.memories.len() == 0 && module.info.imported_memories.len() == 0 { if module.info.memories.is_empty() && module.info.imported_memories.is_empty() {
(::std::ptr::null_mut(), 0) (::std::ptr::null_mut(), 0)
} else { } else {
let mem = match MemoryIndex::new(0).local_or_import(&module.info) { let mem = match MemoryIndex::new(0).local_or_import(&module.info) {
@ -327,7 +327,7 @@ impl Ctx {
data_finalizer: fn(*mut c_void), data_finalizer: fn(*mut c_void),
) -> Self { ) -> Self {
let (mem_base, mem_bound): (*mut u8, usize) = let (mem_base, mem_bound): (*mut u8, usize) =
if module.info.memories.len() == 0 && module.info.imported_memories.len() == 0 { if module.info.memories.is_empty() && module.info.imported_memories.is_empty() {
(::std::ptr::null_mut(), 0) (::std::ptr::null_mut(), 0)
} else { } else {
let mem = match MemoryIndex::new(0).local_or_import(&module.info) { let mem = match MemoryIndex::new(0).local_or_import(&module.info) {
@ -351,7 +351,7 @@ impl Ctx {
intrinsics: get_intrinsics_for_module(&module.info), intrinsics: get_intrinsics_for_module(&module.info),
stack_lower_bound: ::std::ptr::null_mut(), stack_lower_bound: ptr::null_mut(),
memory_base: mem_base, memory_base: mem_base,
memory_bound: mem_bound, memory_bound: mem_bound,