Skip to content

Commit

Permalink
Clean up misc. bits of runtime-core
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark McCaskey committed Jan 24, 2020
1 parent ddaaa30 commit 0a02f3b
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 79 deletions.
38 changes: 6 additions & 32 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions lib/runtime-core/src/backing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ fn import_memories(
}
}

if link_errors.len() > 0 {
if !link_errors.is_empty() {
Err(link_errors)
} else {
Ok((memories.into_boxed_map(), vm_memories.into_boxed_map()))
Expand Down Expand Up @@ -886,7 +886,7 @@ fn import_globals(
}
}

if link_errors.len() > 0 {
if !link_errors.is_empty() {
Err(link_errors)
} else {
Ok((globals.into_boxed_map(), vm_globals.into_boxed_map()))
Expand Down
8 changes: 3 additions & 5 deletions lib/runtime-core/src/fault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
stack[end_offset - 4 - 10] = &mut ctx as *mut Context<F, R> as usize as u64; // rdi
const NUM_SAVED_REGISTERS: usize = 31;
let stack_begin = stack
.as_mut_ptr()
.offset((end_offset - 4 - NUM_SAVED_REGISTERS) as isize);
let stack_end = stack.as_mut_ptr().offset(end_offset as isize);
let stack_begin = stack.as_mut_ptr().add(end_offset - 4 - NUM_SAVED_REGISTERS);
let stack_end = stack.as_mut_ptr().add(end_offset);

raw::run_on_alternative_stack(stack_end, stack_begin);
ctx.ret.take().unwrap()
Expand Down Expand Up @@ -392,7 +390,7 @@ extern "C" fn signal_trap_handler(
unwind_result = Box::new(image);
} else {
// 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!(
"\n{}",
"Wasmer encountered an error while running your WebAssembly program."
Expand Down
10 changes: 4 additions & 6 deletions lib/runtime-core/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use smallvec::{smallvec, SmallVec};
use std::{
mem,
pin::Pin,
ptr::NonNull,
ptr::{self, NonNull},
sync::{Arc, Mutex},
};

Expand Down Expand Up @@ -710,7 +710,7 @@ pub(crate) fn call_func_with_index_inner(

match signature.returns() {
&[] => {
run_wasm(0 as *mut u64)?;
run_wasm(ptr::null_mut())?;
Ok(())
}
&[Type::V128] => {
Expand All @@ -721,10 +721,8 @@ pub(crate) fn call_func_with_index_inner(
let mut bytes = [0u8; 16];
let lo = result[0].to_le_bytes();
let hi = result[1].to_le_bytes();
for i in 0..8 {
bytes[i] = lo[i];
bytes[i + 8] = hi[i];
}
bytes[..8].clone_from_slice(&lo);
bytes[8..16].clone_from_slice(&hi);
rets.push(Value::V128(u128::from_le_bytes(bytes)));
Ok(())
}
Expand Down
12 changes: 6 additions & 6 deletions lib/runtime-core/src/memory/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl<T: Copy + ValueType> WasmPtr<T, Item> {
impl<T: Copy + ValueType> WasmPtr<T, Array> {
/// Dereference this `WasmPtr`.
#[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
// 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>());
Expand All @@ -104,12 +104,12 @@ impl<T: Copy + ValueType> WasmPtr<T, Array> {

/// Mutable dereference this `WasmPtr`.
#[inline]
pub unsafe fn deref_mut<'a>(
pub unsafe fn deref_mut(
self,
memory: &'a Memory,
memory: &Memory,
index: 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
// 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>());
Expand All @@ -129,7 +129,7 @@ impl<T: Copy + ValueType> WasmPtr<T, Array> {
}

/// 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 {
return None;
}
Expand All @@ -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.
/// Note that this does not account for UTF-8 strings that _contain_ nul themselves,
/// [`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)..]
.iter()
.map(|cell| cell.get())
Expand Down
10 changes: 5 additions & 5 deletions lib/runtime-core/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ pub fn read_module<
ImportSectionEntryType::Memory(memory_ty) => {
let mem_desc = MemoryDescriptor::new(
Pages(memory_ty.limits.initial),
memory_ty.limits.maximum.map(|max| Pages(max)),
memory_ty.limits.maximum.map(Pages),
memory_ty.shared,
)
.map_err(|x| LoadError::Codegen(format!("{:?}", x)))?;
Expand Down Expand Up @@ -183,7 +183,7 @@ pub fn read_module<
ParserState::MemorySectionEntry(memory_ty) => {
let mem_desc = MemoryDescriptor::new(
Pages(memory_ty.limits.initial),
memory_ty.limits.maximum.map(|max| Pages(max)),
memory_ty.limits.maximum.map(Pages),
memory_ty.shared,
)
.map_err(|x| LoadError::Codegen(format!("{:?}", x)))?;
Expand Down Expand Up @@ -271,11 +271,11 @@ pub fn read_module<
Event::Internal(InternalEvent::FunctionBegin(id as u32)),
&info.read().unwrap(),
)
.map_err(|x| LoadError::Codegen(x))?;
.map_err(LoadError::Codegen)?;
}
middlewares
.run(Some(fcg), Event::Wasm(op), &info.read().unwrap())
.map_err(|x| LoadError::Codegen(x))?;
.map_err(LoadError::Codegen)?;
}
ParserState::EndFunctionBody => break,
_ => unreachable!(),
Expand All @@ -287,7 +287,7 @@ pub fn read_module<
Event::Internal(InternalEvent::FunctionEnd),
&info.read().unwrap(),
)
.map_err(|x| LoadError::Codegen(x))?;
.map_err(LoadError::Codegen)?;
fcg.finalize()
.map_err(|x| LoadError::Codegen(format!("{:?}", x)))?;
func_count = func_count.wrapping_add(1);
Expand Down
1 change: 1 addition & 0 deletions lib/runtime-core/src/sig_registry.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! A process-global registry for function signatures.
use crate::{
structures::Map,
types::{FuncSig, SigIndex},
Expand Down
30 changes: 12 additions & 18 deletions lib/runtime-core/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ impl ExecutionStateImage {
}

fn format_optional_u64_sequence(x: &[Option<u64>]) -> String {
if x.len() == 0 {
if x.is_empty() {
"(empty)".into()
} else {
join_strings(
Expand All @@ -436,7 +436,7 @@ impl ExecutionStateImage {

let mut ret = String::new();

if self.frames.len() == 0 {
if self.frames.is_empty() {
ret += &"Unknown fault address, cannot read stack.";
ret += "\n";
} else {
Expand Down Expand Up @@ -632,7 +632,7 @@ pub mod x64 {
let mut ptr = &vmctx as *const *const Ctx as *const u8;
for x in seq {
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
}
Expand Down Expand Up @@ -679,7 +679,7 @@ pub mod x64 {
} else {
fsm.wasm_offset_to_target_offset
.get(&f.wasm_inst_offset)
.map(|x| *x)
.copied()
}
.expect("instruction is not a critical point");

Expand Down Expand Up @@ -994,8 +994,8 @@ pub mod x64 {
catch_unsafe_unwind(
|| {
run_on_alternative_stack(
stack.as_mut_ptr().offset(stack.len() as isize),
stack.as_mut_ptr().offset(stack_offset as isize),
stack.as_mut_ptr().add(stack.len()),
stack.as_mut_ptr().add(stack_offset),
)
},
breakpoints,
Expand Down Expand Up @@ -1157,24 +1157,18 @@ pub mod x64 {
}
}

let mut found_shadow = false;
for v in state.stack_values.iter() {
match *v {
MachineValue::ExplicitShadow => {
found_shadow = true;
break;
}
_ => {}
}
}
let found_shadow = state
.stack_values
.iter()
.any(|v| *v == MachineValue::ExplicitShadow);
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() {
match *v {
MachineValue::ExplicitShadow => {
stack = stack.offset((fsm.shadow_size / 8) as isize);
stack = stack.add(fsm.shadow_size / 8);
}
MachineValue::Undefined => {
stack = stack.offset(1);
Expand Down
10 changes: 5 additions & 5 deletions lib/runtime-core/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ pub static INTRINSICS_IMPORTED_DYNAMIC_MEMORY: Intrinsics = Intrinsics {
};

fn get_intrinsics_for_module(m: &ModuleInfo) -> *const Intrinsics {
if m.memories.len() == 0 && m.imported_memories.len() == 0 {
::std::ptr::null()
if m.memories.is_empty() && m.imported_memories.is_empty() {
ptr::null()
} else {
match MemoryIndex::new(0).local_or_import(m) {
LocalOrImport::Local(local_mem_index) => {
Expand Down Expand Up @@ -274,7 +274,7 @@ impl Ctx {
module: &ModuleInner,
) -> Self {
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)
} else {
let mem = match MemoryIndex::new(0).local_or_import(&module.info) {
Expand Down Expand Up @@ -327,7 +327,7 @@ impl Ctx {
data_finalizer: fn(*mut c_void),
) -> Self {
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)
} else {
let mem = match MemoryIndex::new(0).local_or_import(&module.info) {
Expand All @@ -351,7 +351,7 @@ impl Ctx {

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_bound: mem_bound,
Expand Down

0 comments on commit 0a02f3b

Please sign in to comment.