From 0a02f3bb97c314a65718650176c46bf540d5ffae Mon Sep 17 00:00:00 2001 From: Mark McCaskey Date: Fri, 24 Jan 2020 14:55:02 -0800 Subject: [PATCH] Clean up misc. bits of runtime-core --- Cargo.lock | 38 +++++----------------------- lib/runtime-core/src/backing.rs | 4 +-- lib/runtime-core/src/fault.rs | 8 +++--- lib/runtime-core/src/instance.rs | 10 +++----- lib/runtime-core/src/memory/ptr.rs | 12 ++++----- lib/runtime-core/src/parse.rs | 10 ++++---- lib/runtime-core/src/sig_registry.rs | 1 + lib/runtime-core/src/state.rs | 30 +++++++++------------- lib/runtime-core/src/vm.rs | 10 ++++---- 9 files changed, 44 insertions(+), 79 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 02c8247d8..7b4d90258 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -581,7 +581,7 @@ dependencies = [ "libc", "llvm-sys", "once_cell", - "parking_lot 0.10.0", + "parking_lot", "regex", ] @@ -850,17 +850,6 @@ dependencies = [ "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]] name = "parking_lot" version = "0.10.0" @@ -868,22 +857,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "92e98c49ab0b7ce5b222f2cc9193fc4efe11c6d0bd4f648e374684a6857b1cfc" dependencies = [ "lock_api", - "parking_lot_core 0.7.0", -] - -[[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", + "parking_lot_core", ] [[package]] @@ -1799,7 +1773,7 @@ dependencies = [ "regex", "rustc_version", "semver", - "smallvec 1.1.0", + "smallvec 0.6.13", "wabt", "wasmer-runtime-core", "wasmparser", @@ -1880,13 +1854,13 @@ dependencies = [ "libc", "nix", "page_size", - "parking_lot 0.9.0", + "parking_lot", "rustc_version", "serde", "serde-bench", "serde_bytes", "serde_derive", - "smallvec 1.1.0", + "smallvec 0.6.13", "wasmparser", "winapi", ] @@ -1915,7 +1889,7 @@ dependencies = [ "nix", "serde", "serde_derive", - "smallvec 1.1.0", + "smallvec 0.6.13", "wasmer-runtime-core", ] diff --git a/lib/runtime-core/src/backing.rs b/lib/runtime-core/src/backing.rs index cf7876752..3cd592bda 100644 --- a/lib/runtime-core/src/backing.rs +++ b/lib/runtime-core/src/backing.rs @@ -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())) @@ -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())) diff --git a/lib/runtime-core/src/fault.rs b/lib/runtime-core/src/fault.rs index d30578624..3f9db15d5 100644 --- a/lib/runtime-core/src/fault.rs +++ b/lib/runtime-core/src/fault.rs @@ -257,10 +257,8 @@ pub fn allocate_and_run R>(size: usize, f: F) -> R { // NOTE: Keep this consistent with `image-loading-*.s`. stack[end_offset - 4 - 10] = &mut ctx as *mut Context 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() @@ -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." diff --git a/lib/runtime-core/src/instance.rs b/lib/runtime-core/src/instance.rs index 0f21248df..d012ae8d6 100644 --- a/lib/runtime-core/src/instance.rs +++ b/lib/runtime-core/src/instance.rs @@ -21,7 +21,7 @@ use smallvec::{smallvec, SmallVec}; use std::{ mem, pin::Pin, - ptr::NonNull, + ptr::{self, NonNull}, sync::{Arc, Mutex}, }; @@ -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] => { @@ -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(()) } diff --git a/lib/runtime-core/src/memory/ptr.rs b/lib/runtime-core/src/memory/ptr.rs index cb12ff749..ac381d106 100644 --- a/lib/runtime-core/src/memory/ptr.rs +++ b/lib/runtime-core/src/memory/ptr.rs @@ -81,7 +81,7 @@ impl WasmPtr { impl WasmPtr { /// Dereference this `WasmPtr`. #[inline] - pub fn deref<'a>(self, memory: &'a Memory, index: u32, length: u32) -> Option<&'a [Cell]> { + pub fn deref(self, memory: &Memory, index: u32, length: u32) -> Option<&[Cell]> { // 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::() + (mem::size_of::() % mem::align_of::()); @@ -104,12 +104,12 @@ impl WasmPtr { /// 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]> { + ) -> Option<&mut [Cell]> { // 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::() + (mem::size_of::() % mem::align_of::()); @@ -129,7 +129,7 @@ impl WasmPtr { } /// 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; } @@ -141,7 +141,7 @@ impl WasmPtr { /// 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::()[(self.offset as usize)..] .iter() .map(|cell| cell.get()) diff --git a/lib/runtime-core/src/parse.rs b/lib/runtime-core/src/parse.rs index 3d4e27b41..ad91afb77 100644 --- a/lib/runtime-core/src/parse.rs +++ b/lib/runtime-core/src/parse.rs @@ -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)))?; @@ -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)))?; @@ -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!(), @@ -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); diff --git a/lib/runtime-core/src/sig_registry.rs b/lib/runtime-core/src/sig_registry.rs index 97a69c02d..63499b5d3 100644 --- a/lib/runtime-core/src/sig_registry.rs +++ b/lib/runtime-core/src/sig_registry.rs @@ -1,3 +1,4 @@ +//! A process-global registry for function signatures. use crate::{ structures::Map, types::{FuncSig, SigIndex}, diff --git a/lib/runtime-core/src/state.rs b/lib/runtime-core/src/state.rs index 2bd72282c..1dfcae813 100644 --- a/lib/runtime-core/src/state.rs +++ b/lib/runtime-core/src/state.rs @@ -417,7 +417,7 @@ impl ExecutionStateImage { } fn format_optional_u64_sequence(x: &[Option]) -> String { - if x.len() == 0 { + if x.is_empty() { "(empty)".into() } else { join_strings( @@ -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 { @@ -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 } @@ -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"); @@ -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, @@ -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); diff --git a/lib/runtime-core/src/vm.rs b/lib/runtime-core/src/vm.rs index d7b32c94a..2a39bdec1 100644 --- a/lib/runtime-core/src/vm.rs +++ b/lib/runtime-core/src/vm.rs @@ -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) => { @@ -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) { @@ -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) { @@ -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,