diff --git a/lib/middleware-common/src/call_trace.rs b/lib/middleware-common/src/call_trace.rs index 6430cc038..5cb77534c 100644 --- a/lib/middleware-common/src/call_trace.rs +++ b/lib/middleware-common/src/call_trace.rs @@ -1,8 +1,11 @@ +use std::sync::{ + atomic::{AtomicU32, Ordering}, + Arc, +}; use wasmer_runtime_core::{ codegen::{Event, EventSink, FunctionMiddleware, InternalEvent}, module::ModuleInfo, }; -use std::sync::{Arc, atomic::{Ordering, AtomicU32}}; pub struct CallTrace { counter: Arc, diff --git a/lib/runtime-core/src/backend.rs b/lib/runtime-core/src/backend.rs index 9491c2377..ae439969e 100644 --- a/lib/runtime-core/src/backend.rs +++ b/lib/runtime-core/src/backend.rs @@ -94,11 +94,15 @@ pub fn get_inline_breakpoint_size(arch: Architecture, backend: Backend) -> Optio match (arch, backend) { (Architecture::X64, Backend::Singlepass) => Some(7), (Architecture::Aarch64, Backend::Singlepass) => Some(12), - _ => None + _ => None, } } -pub fn read_inline_breakpoint(arch: Architecture, backend: Backend, code: &[u8]) -> Option { +pub fn read_inline_breakpoint( + arch: Architecture, + backend: Backend, + code: &[u8], +) -> Option { match arch { Architecture::X64 => match backend { Backend::Singlepass => { @@ -118,7 +122,7 @@ pub fn read_inline_breakpoint(arch: Architecture, backend: Backend, code: &[u8]) None } } - _ => None + _ => None, }, Architecture::Aarch64 => match backend { Backend::Singlepass => { @@ -131,14 +135,14 @@ pub fn read_inline_breakpoint(arch: Architecture, backend: Backend, code: &[u8]) 0 => InlineBreakpointType::Trace, 1 => InlineBreakpointType::Middleware, _ => InlineBreakpointType::Unknown, - } + }, }) } else { None } - }, + } _ => None, - } + }, } } diff --git a/lib/runtime-core/src/fault.rs b/lib/runtime-core/src/fault.rs index 1a50b3faa..51d00d52e 100644 --- a/lib/runtime-core/src/fault.rs +++ b/lib/runtime-core/src/fault.rs @@ -244,7 +244,9 @@ extern "C" fn signal_trap_handler( siginfo: *mut siginfo_t, ucontext: *mut c_void, ) { - use crate::backend::{Architecture, InlineBreakpointType, get_inline_breakpoint_size, read_inline_breakpoint}; + use crate::backend::{ + get_inline_breakpoint_size, read_inline_breakpoint, Architecture, InlineBreakpointType, + }; #[cfg(target_arch = "x86_64")] static ARCH: Architecture = Architecture::X64; @@ -260,30 +262,35 @@ extern "C" fn signal_trap_handler( let magic_size = if let Some(x) = get_inline_breakpoint_size(ARCH, v.backend) { x } else { - continue + continue; }; let ip = fault.ip.get(); let end = v.base + v.msm.total_size; if ip >= v.base && ip < end && ip + magic_size <= end { - if let Some(ib) = read_inline_breakpoint(ARCH, v.backend, std::slice::from_raw_parts(ip as *const u8, magic_size)) { + if let Some(ib) = read_inline_breakpoint( + ARCH, + v.backend, + std::slice::from_raw_parts(ip as *const u8, magic_size), + ) { fault.ip.set(ip + magic_size); - + match ib.ty { - InlineBreakpointType::Trace => {}, + InlineBreakpointType::Trace => {} InlineBreakpointType::Middleware => { - let out: Option>> = with_breakpoint_map(|bkpt_map| { - bkpt_map.and_then(|x| x.get(&ip)).map(|x| { - x(BreakpointInfo { - fault: Some(&fault), + let out: Option>> = + with_breakpoint_map(|bkpt_map| { + bkpt_map.and_then(|x| x.get(&ip)).map(|x| { + x(BreakpointInfo { + fault: Some(&fault), + }) }) - }) - }); + }); if let Some(Ok(())) = out { } else { println!("Failed calling middleware: {:?}", out); } } - _ => println!("Unknown breakpoint type: {:?}", ib.ty) + _ => println!("Unknown breakpoint type: {:?}", ib.ty), } return true; } diff --git a/lib/runtime-core/src/state.rs b/lib/runtime-core/src/state.rs index 82f34eaed..e31e55bb4 100644 --- a/lib/runtime-core/src/state.rs +++ b/lib/runtime-core/src/state.rs @@ -1,6 +1,6 @@ +use crate::backend::Backend; use std::collections::BTreeMap; use std::ops::Bound::{Included, Unbounded}; -use crate::backend::Backend; #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] pub struct RegisterIndex(pub usize); diff --git a/lib/singlepass-backend/src/emitter_x64.rs b/lib/singlepass-backend/src/emitter_x64.rs index d16c87233..1d0127cf7 100644 --- a/lib/singlepass-backend/src/emitter_x64.rs +++ b/lib/singlepass-backend/src/emitter_x64.rs @@ -1,6 +1,6 @@ use dynasmrt::{x64::Assembler, AssemblyOffset, DynamicLabel, DynasmApi, DynasmLabelApi}; +use wasmer_runtime_core::backend::InlineBreakpointType; pub use wasmer_runtime_core::state::x64_decl::{GPR, XMM}; -use wasmer_runtime_core::backend::{InlineBreakpointType}; #[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)] pub enum Location { diff --git a/lib/singlepass-backend/src/translator_aarch64.rs b/lib/singlepass-backend/src/translator_aarch64.rs index 3ae27ddd8..55649db49 100644 --- a/lib/singlepass-backend/src/translator_aarch64.rs +++ b/lib/singlepass-backend/src/translator_aarch64.rs @@ -3,7 +3,7 @@ use crate::codegen_x64::*; use crate::emitter_x64::*; use dynasmrt::{aarch64::Assembler, AssemblyOffset, DynamicLabel, DynasmApi, DynasmLabelApi}; -use wasmer_runtime_core::backend::{InlineBreakpointType}; +use wasmer_runtime_core::backend::InlineBreakpointType; #[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)] pub struct AX(pub u32); @@ -558,7 +558,7 @@ impl Emitter for Assembler { } dynasm!(self ; str D(map_xmm(src).v()), [x_tmp3] ); } - _ => panic!("NOT IMPL: {:?} {:?} {:?}", sz, src, dst) + _ => panic!("NOT IMPL: {:?} {:?} {:?}", sz, src, dst), } } @@ -844,7 +844,7 @@ impl Emitter for Assembler { ; ldr w_tmp1, [x_tmp3] ; cmp w_tmp1, W(map_gpr(left).x()) ) - }, + } (Size::S64, Location::GPR(left), Location::Memory(base, disp)) => { if disp >= 0 { dynasm!(self ; add x_tmp3, X(map_gpr(base).x()), disp as u32); @@ -856,7 +856,7 @@ impl Emitter for Assembler { ; ldr x_tmp1, [x_tmp3] ; cmp x_tmp1, X(map_gpr(left).x()) ) - }, + } (Size::S32, Location::Memory(base, disp), Location::GPR(right)) => { if disp >= 0 { dynasm!(self ; add x_tmp3, X(map_gpr(base).x()), disp as u32); @@ -868,7 +868,7 @@ impl Emitter for Assembler { ; ldr w_tmp1, [x_tmp3] ; cmp W(map_gpr(right).x()), w_tmp1 ) - }, + } (Size::S64, Location::Memory(base, disp), Location::GPR(right)) => { if disp >= 0 { dynasm!(self ; add x_tmp3, X(map_gpr(base).x()), disp as u32); @@ -880,7 +880,7 @@ impl Emitter for Assembler { ; ldr x_tmp1, [x_tmp3] ; cmp X(map_gpr(right).x()), x_tmp1 ) - }, + } _ => unreachable!(), } } @@ -927,7 +927,7 @@ impl Emitter for Assembler { ; ldr w_tmp1, [x_tmp3] ) } - _ => unreachable!() + _ => unreachable!(), } dynasm!( self @@ -953,7 +953,7 @@ impl Emitter for Assembler { ; ldr x_tmp1, [x_tmp3] ) } - _ => unreachable!() + _ => unreachable!(), } dynasm!( self @@ -962,7 +962,7 @@ impl Emitter for Assembler { ; msub X(map_gpr(GPR::RDX).x()), X(map_gpr(GPR::RAX).x()), x_tmp1, x_tmp2 ) } - _ => unreachable!() + _ => unreachable!(), } } fn emit_idiv(&mut self, sz: Size, divisor: Location) { @@ -984,7 +984,7 @@ impl Emitter for Assembler { ; ldr w_tmp1, [x_tmp3] ) } - _ => unreachable!() + _ => unreachable!(), } dynasm!( self @@ -1010,7 +1010,7 @@ impl Emitter for Assembler { ; ldr x_tmp1, [x_tmp3] ) } - _ => unreachable!() + _ => unreachable!(), } dynasm!( self @@ -1019,7 +1019,7 @@ impl Emitter for Assembler { ; msub X(map_gpr(GPR::RDX).x()), X(map_gpr(GPR::RAX).x()), x_tmp1, x_tmp2 ) } - _ => unreachable!() + _ => unreachable!(), } } fn emit_shl(&mut self, sz: Size, src: Location, dst: Location) { @@ -1034,43 +1034,42 @@ impl Emitter for Assembler { fn emit_rol(&mut self, sz: Size, src: Location, dst: Location) { // TODO: We are changing content of `src` (possibly RCX) here. Will this break any assumptions? match sz { - Size::S32 => { - match src { - Location::Imm8(x) => { - assert!(x < 32); - binop_shift!(ror, self, sz, Location::Imm8(32 - x), dst, { unreachable!("rol") }); - } - Location::GPR(GPR::RCX) => { - dynasm!( - self - ; mov w_tmp1, 32 - ; sub W(map_gpr(GPR::RCX).x()), w_tmp1, W(map_gpr(GPR::RCX).x()) - ); - binop_shift!(ror, self, sz, src, dst, { unreachable!("rol") }); - } - _ => unreachable!() + Size::S32 => match src { + Location::Imm8(x) => { + assert!(x < 32); + binop_shift!(ror, self, sz, Location::Imm8(32 - x), dst, { + unreachable!("rol") + }); } - } - Size::S64 => { - match src { - Location::Imm8(x) => { - assert!(x < 64); - binop_shift!(ror, self, sz, Location::Imm8(64 - x), dst, { unreachable!("rol") }); - } - Location::GPR(GPR::RCX) => { - dynasm!( - self - ; mov x_tmp1, 64 - ; sub X(map_gpr(GPR::RCX).x()), x_tmp1, X(map_gpr(GPR::RCX).x()) - ); - binop_shift!(ror, self, sz, src, dst, { unreachable!("rol") }); - } - _ => unreachable!() + Location::GPR(GPR::RCX) => { + dynasm!( + self + ; mov w_tmp1, 32 + ; sub W(map_gpr(GPR::RCX).x()), w_tmp1, W(map_gpr(GPR::RCX).x()) + ); + binop_shift!(ror, self, sz, src, dst, { unreachable!("rol") }); } - } - _ => unreachable!() + _ => unreachable!(), + }, + Size::S64 => match src { + Location::Imm8(x) => { + assert!(x < 64); + binop_shift!(ror, self, sz, Location::Imm8(64 - x), dst, { + unreachable!("rol") + }); + } + Location::GPR(GPR::RCX) => { + dynasm!( + self + ; mov x_tmp1, 64 + ; sub X(map_gpr(GPR::RCX).x()), x_tmp1, X(map_gpr(GPR::RCX).x()) + ); + binop_shift!(ror, self, sz, src, dst, { unreachable!("rol") }); + } + _ => unreachable!(), + }, + _ => unreachable!(), } - } fn emit_ror(&mut self, sz: Size, src: Location, dst: Location) { binop_shift!(ror, self, sz, src, dst, { unreachable!("ror") }); @@ -1485,76 +1484,78 @@ impl Emitter for Assembler { } } -fn emit_clz_variant(assembler: &mut Assembler, sz: Size, src: &Location, dst: &Location, reversed: bool) { +fn emit_clz_variant( + assembler: &mut Assembler, + sz: Size, + src: &Location, + dst: &Location, + reversed: bool, +) { match sz { - Size::S32 => { - match *src { - Location::GPR(src) => { - dynasm!( - assembler - ; mov w_tmp1, W(map_gpr(src).x()) - ) + Size::S32 => { + match *src { + Location::GPR(src) => dynasm!( + assembler + ; mov w_tmp1, W(map_gpr(src).x()) + ), + Location::Memory(base, disp) => { + if disp >= 0 { + dynasm!(assembler ; add x_tmp3, X(map_gpr(base).x()), disp as u32); + } else { + dynasm!(assembler ; sub x_tmp3, X(map_gpr(base).x()), (-disp) as u32); } - Location::Memory(base, disp) => { - if disp >= 0 { - dynasm!(assembler ; add x_tmp3, X(map_gpr(base).x()), disp as u32); - } else { - dynasm!(assembler ; sub x_tmp3, X(map_gpr(base).x()), (-disp) as u32); - } - dynasm!( - assembler - ; ldr w_tmp1, [x_tmp3] - ) - } - _ => unreachable!() - } - match *dst { - Location::GPR(dst) => { - if reversed { - dynasm!(assembler ; rbit w_tmp1, w_tmp1); - } - dynasm!( - assembler - ; clz W(map_gpr(dst).x()), w_tmp1 - ); - } - _ => unreachable!() + dynasm!( + assembler + ; ldr w_tmp1, [x_tmp3] + ) } + _ => unreachable!(), } - Size::S64 => { - match *src { - Location::GPR(src) => { - dynasm!( - assembler - ; mov x_tmp1, X(map_gpr(src).x()) - ) + match *dst { + Location::GPR(dst) => { + if reversed { + dynasm!(assembler ; rbit w_tmp1, w_tmp1); } - Location::Memory(base, disp) => { - if disp >= 0 { - dynasm!(assembler ; add x_tmp3, X(map_gpr(base).x()), disp as u32); - } else { - dynasm!(assembler ; sub x_tmp3, X(map_gpr(base).x()), (-disp) as u32); - } - dynasm!( - assembler - ; ldr x_tmp1, [x_tmp3] - ) - } - _ => unreachable!() - } - match *dst { - Location::GPR(dst) => { - if reversed { - dynasm!(assembler ; rbit x_tmp1, x_tmp1) - } - dynasm!( - assembler - ; clz X(map_gpr(dst).x()), x_tmp1 - ); - } - _ => unreachable!() + dynasm!( + assembler + ; clz W(map_gpr(dst).x()), w_tmp1 + ); } + _ => unreachable!(), } - _ => unreachable!() } -} \ No newline at end of file + Size::S64 => { + match *src { + Location::GPR(src) => dynasm!( + assembler + ; mov x_tmp1, X(map_gpr(src).x()) + ), + Location::Memory(base, disp) => { + if disp >= 0 { + dynasm!(assembler ; add x_tmp3, X(map_gpr(base).x()), disp as u32); + } else { + dynasm!(assembler ; sub x_tmp3, X(map_gpr(base).x()), (-disp) as u32); + } + dynasm!( + assembler + ; ldr x_tmp1, [x_tmp3] + ) + } + _ => unreachable!(), + } + match *dst { + Location::GPR(dst) => { + if reversed { + dynasm!(assembler ; rbit x_tmp1, x_tmp1) + } + dynasm!( + assembler + ; clz X(map_gpr(dst).x()), x_tmp1 + ); + } + _ => unreachable!(), + } + } + _ => unreachable!(), + } +} diff --git a/src/bin/wasmer.rs b/src/bin/wasmer.rs index 16d048d97..e54fddd93 100644 --- a/src/bin/wasmer.rs +++ b/src/bin/wasmer.rs @@ -621,10 +621,17 @@ fn execute_wasm(options: &Run) -> Result<(), String> { options .optimized_backends .iter() - .map(|&backend| -> (Backend, Box Box + Send>) { - let options = options.clone(); - (backend, Box::new(move || get_compiler_by_backend(backend, &options).unwrap())) - }) + .map( + |&backend| -> (Backend, Box Box + Send>) { + let options = options.clone(); + ( + backend, + Box::new(move || { + get_compiler_by_backend(backend, &options).unwrap() + }), + ) + }, + ) .collect(), interactive_shell, )? @@ -635,13 +642,17 @@ fn execute_wasm(options: &Run) -> Result<(), String> { { use wasmer_runtime::error::RuntimeError; use wasmer_runtime_core::{ - fault::{push_code_version, pop_code_version}, - state::CodeVersion + fault::{pop_code_version, push_code_version}, + state::CodeVersion, }; push_code_version(CodeVersion { baseline: true, - msm: instance.module.runnable_module.get_module_state_map().unwrap(), + msm: instance + .module + .runnable_module + .get_module_state_map() + .unwrap(), base: instance.module.runnable_module.get_code().unwrap().as_ptr() as usize, backend: options.backend, }); @@ -815,7 +826,7 @@ fn validate(validate: Validate) { } fn get_compiler_by_backend(backend: Backend, opts: &Run) -> Option> { - use wasmer_runtime_core::codegen::{MiddlewareChain}; + use wasmer_runtime_core::codegen::MiddlewareChain; let opts = opts.clone(); let middlewares_gen = move || { let mut middlewares = MiddlewareChain::new(); @@ -825,14 +836,15 @@ fn get_compiler_by_backend(backend: Backend, opts: &Run) -> Option { + use wasmer_runtime_core::codegen::StreamingCompiler; use wasmer_singlepass_backend::ModuleCodeGenerator as SinglePassMCG; - use wasmer_runtime_core::codegen::{StreamingCompiler}; - let c: StreamingCompiler = StreamingCompiler::new(middlewares_gen); + let c: StreamingCompiler = + StreamingCompiler::new(middlewares_gen); Box::new(c) } #[cfg(not(feature = "backend-singlepass"))]