Cargo fmt

This commit is contained in:
losfair
2019-06-25 03:56:20 +08:00
parent 988b2c5748
commit 8303853227
4 changed files with 122 additions and 56 deletions

View File

@ -17,7 +17,6 @@ pub struct MachineState {
pub wasm_stack: Vec<WasmAbstractValue>, pub wasm_stack: Vec<WasmAbstractValue>,
pub wasm_stack_private_depth: usize, pub wasm_stack_private_depth: usize,
} }
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug, Default)]
@ -68,11 +67,7 @@ pub struct WasmFunctionStateDump {
} }
impl ModuleStateMap { impl ModuleStateMap {
fn lookup_call_ip( fn lookup_call_ip(&self, ip: usize, base: usize) -> Option<(&FunctionStateMap, MachineState)> {
&self,
ip: usize,
base: usize,
) -> Option<(&FunctionStateMap, MachineState)> {
if ip < base || ip - base >= self.total_size { if ip < base || ip - base >= self.total_size {
None None
} else { } else {
@ -114,7 +109,12 @@ impl ModuleStateMap {
} }
impl FunctionStateMap { impl FunctionStateMap {
pub fn new(initial: MachineState, local_function_id: usize, shadow_size: usize, locals: Vec<WasmAbstractValue>) -> FunctionStateMap { pub fn new(
initial: MachineState,
local_function_id: usize,
shadow_size: usize,
locals: Vec<WasmAbstractValue>,
) -> FunctionStateMap {
FunctionStateMap { FunctionStateMap {
initial, initial,
local_function_id, local_function_id,
@ -216,7 +216,13 @@ pub mod x64 {
} }
#[warn(unused_variables)] #[warn(unused_variables)]
pub unsafe fn read_stack(msm: &ModuleStateMap, code_base: usize, mut stack: *const u64, initially_known_registers: [Option<u64>; 24], mut initial_address: Option<u64>) -> Vec<WasmFunctionStateDump> { pub unsafe fn read_stack(
msm: &ModuleStateMap,
code_base: usize,
mut stack: *const u64,
initially_known_registers: [Option<u64>; 24],
mut initial_address: Option<u64>,
) -> Vec<WasmFunctionStateDump> {
let mut known_registers: [Option<u64>; 24] = initially_known_registers; let mut known_registers: [Option<u64>; 24] = initially_known_registers;
let mut results: Vec<WasmFunctionStateDump> = vec![]; let mut results: Vec<WasmFunctionStateDump> = vec![];
@ -226,24 +232,30 @@ pub mod x64 {
stack = stack.offset(1); stack = stack.offset(1);
x x
}); });
let (fsm, state) = match let (fsm, state) = match msm
msm.lookup_call_ip(ret_addr as usize, code_base) .lookup_call_ip(ret_addr as usize, code_base)
.or_else(|| msm.lookup_trappable_ip(ret_addr as usize, code_base)) .or_else(|| msm.lookup_trappable_ip(ret_addr as usize, code_base))
{ {
Some(x) => x, Some(x) => x,
_ => return results, _ => return results,
}; };
let mut wasm_stack: Vec<Option<u64>> = state.wasm_stack.iter() let mut wasm_stack: Vec<Option<u64>> = state
.wasm_stack
.iter()
.map(|x| match *x { .map(|x| match *x {
WasmAbstractValue::Const(x) => Some(x), WasmAbstractValue::Const(x) => Some(x),
WasmAbstractValue::Runtime => None, WasmAbstractValue::Runtime => None,
}).collect(); })
let mut wasm_locals: Vec<Option<u64>> = fsm.locals.iter() .collect();
let mut wasm_locals: Vec<Option<u64>> = fsm
.locals
.iter()
.map(|x| match *x { .map(|x| match *x {
WasmAbstractValue::Const(x) => Some(x), WasmAbstractValue::Const(x) => Some(x),
WasmAbstractValue::Runtime => None, WasmAbstractValue::Runtime => None,
}).collect(); })
.collect();
// This must be before the next loop because that modifies `known_registers`. // This must be before the next loop because that modifies `known_registers`.
for (i, v) in state.register_values.iter().enumerate() { for (i, v) in state.register_values.iter().enumerate() {
@ -304,7 +316,12 @@ pub mod x64 {
} }
stack = stack.offset(1); // RBP stack = stack.offset(1); // RBP
wasm_stack.truncate(wasm_stack.len().checked_sub(state.wasm_stack_private_depth).unwrap()); wasm_stack.truncate(
wasm_stack
.len()
.checked_sub(state.wasm_stack_private_depth)
.unwrap(),
);
let wfs = WasmFunctionStateDump { let wfs = WasmFunctionStateDump {
local_function_id: fsm.local_function_id, local_function_id: fsm.local_function_id,

View File

@ -540,7 +540,12 @@ impl ModuleCodeGenerator<X64FunctionCode, X64ExecutionContext, CodegenError>
} }
impl X64FunctionCode { impl X64FunctionCode {
fn mark_trappable(a: &mut Assembler, m: &Machine, fsm: &mut FunctionStateMap, control_stack: &mut [ControlFrame]) { fn mark_trappable(
a: &mut Assembler,
m: &Machine,
fsm: &mut FunctionStateMap,
control_stack: &mut [ControlFrame],
) {
let state_diff_id = Self::get_state_diff(m, fsm, control_stack); let state_diff_id = Self::get_state_diff(m, fsm, control_stack);
let offset = a.get_offset().0; let offset = a.get_offset().0;
fsm.trappable_offsets.insert(offset, state_diff_id); fsm.trappable_offsets.insert(offset, state_diff_id);
@ -1607,7 +1612,14 @@ impl FunctionCodeGenerator<CodegenError> for X64FunctionCode {
.machine .machine
.init_locals(a, self.num_locals, self.num_params); .init_locals(a, self.num_locals, self.num_params);
self.fsm = FunctionStateMap::new(new_machine_state(), self.local_function_id, 32, (0..self.locals.len()).map(|_| WasmAbstractValue::Runtime).collect()); self.fsm = FunctionStateMap::new(
new_machine_state(),
self.local_function_id,
32,
(0..self.locals.len())
.map(|_| WasmAbstractValue::Runtime)
.collect(),
);
let diff = self.machine.state.diff(&new_machine_state()); let diff = self.machine.state.diff(&new_machine_state());
let state_diff_id = self.fsm.diffs.len(); let state_diff_id = self.fsm.diffs.len();
@ -1869,7 +1881,10 @@ impl FunctionCodeGenerator<CodegenError> for X64FunctionCode {
let local_index = local_index as usize; let local_index = local_index as usize;
self.value_stack self.value_stack
.push((self.locals[local_index], LocalOrTemp::Local)); .push((self.locals[local_index], LocalOrTemp::Local));
self.machine.state.wasm_stack.push(WasmAbstractValue::Runtime); self.machine
.state
.wasm_stack
.push(WasmAbstractValue::Runtime);
} }
Operator::SetLocal { local_index } => { Operator::SetLocal { local_index } => {
let local_index = local_index as usize; let local_index = local_index as usize;
@ -1899,11 +1914,13 @@ impl FunctionCodeGenerator<CodegenError> for X64FunctionCode {
); );
} }
Operator::I32Const { value } => { Operator::I32Const { value } => {
self self.value_stack
.value_stack
.push((Location::Imm32(value as u32), LocalOrTemp::Temp)); .push((Location::Imm32(value as u32), LocalOrTemp::Temp));
self.machine.state.wasm_stack.push(WasmAbstractValue::Const(value as u32 as u64)); self.machine
}, .state
.wasm_stack
.push(WasmAbstractValue::Const(value as u32 as u64));
}
Operator::I32Add => Self::emit_binop_i32( Operator::I32Add => Self::emit_binop_i32(
a, a,
&mut self.machine, &mut self.machine,
@ -2184,7 +2201,10 @@ impl FunctionCodeGenerator<CodegenError> for X64FunctionCode {
let value = value as u64; let value = value as u64;
self.value_stack self.value_stack
.push((Location::Imm64(value), LocalOrTemp::Temp)); .push((Location::Imm64(value), LocalOrTemp::Temp));
self.machine.state.wasm_stack.push(WasmAbstractValue::Const(value)); self.machine
.state
.wasm_stack
.push(WasmAbstractValue::Const(value));
} }
Operator::I64Add => Self::emit_binop_i64( Operator::I64Add => Self::emit_binop_i64(
a, a,
@ -2519,11 +2539,13 @@ impl FunctionCodeGenerator<CodegenError> for X64FunctionCode {
} }
Operator::F32Const { value } => { Operator::F32Const { value } => {
self self.value_stack
.value_stack
.push((Location::Imm32(value.bits()), LocalOrTemp::Temp)); .push((Location::Imm32(value.bits()), LocalOrTemp::Temp));
self.machine.state.wasm_stack.push(WasmAbstractValue::Const(value.bits() as u64)); self.machine
}, .state
.wasm_stack
.push(WasmAbstractValue::Const(value.bits() as u64));
}
Operator::F32Add => Self::emit_fp_binop_avx( Operator::F32Add => Self::emit_fp_binop_avx(
a, a,
&mut self.machine, &mut self.machine,
@ -2696,11 +2718,13 @@ impl FunctionCodeGenerator<CodegenError> for X64FunctionCode {
} }
Operator::F64Const { value } => { Operator::F64Const { value } => {
self self.value_stack
.value_stack
.push((Location::Imm64(value.bits()), LocalOrTemp::Temp)); .push((Location::Imm64(value.bits()), LocalOrTemp::Temp));
self.machine.state.wasm_stack.push(WasmAbstractValue::Const(value.bits())); self.machine
}, .state
.wasm_stack
.push(WasmAbstractValue::Const(value.bits()));
}
Operator::F64Add => Self::emit_fp_binop_avx( Operator::F64Add => Self::emit_fp_binop_avx(
a, a,
&mut self.machine, &mut self.machine,
@ -4591,7 +4615,8 @@ impl FunctionCodeGenerator<CodegenError> for X64FunctionCode {
); );
} }
Operator::Unreachable => { Operator::Unreachable => {
let state_diff_id = Self::get_state_diff(&self.machine, &mut self.fsm, &mut self.control_stack); let state_diff_id =
Self::get_state_diff(&self.machine, &mut self.fsm, &mut self.control_stack);
let offset = a.get_offset().0; let offset = a.get_offset().0;
self.fsm.trappable_offsets.insert(offset, state_diff_id); self.fsm.trappable_offsets.insert(offset, state_diff_id);

View File

@ -277,10 +277,7 @@ impl Machine {
} }
} }
pub fn release_locations_only_osr_state( pub fn release_locations_only_osr_state(&mut self, n: usize) {
&mut self,
n: usize,
) {
for _ in 0..n { for _ in 0..n {
self.state.wasm_stack.pop().unwrap(); self.state.wasm_stack.pop().unwrap();
} }

View File

@ -21,8 +21,8 @@ use std::ptr;
use std::sync::Arc; use std::sync::Arc;
use std::sync::Once; use std::sync::Once;
use wasmer_runtime_core::codegen::BkptInfo; use wasmer_runtime_core::codegen::BkptInfo;
use wasmer_runtime_core::state::x64::{read_stack, X64Register, GPR};
use wasmer_runtime_core::typed_func::WasmTrapInfo; use wasmer_runtime_core::typed_func::WasmTrapInfo;
use wasmer_runtime_core::state::x64::{X64Register, GPR, read_stack};
use wasmer_runtime_core::vm; use wasmer_runtime_core::vm;
fn join_strings(x: impl Iterator<Item = String>, sep: &str) -> String { fn join_strings(x: impl Iterator<Item = String>, sep: &str) -> String {
@ -47,12 +47,19 @@ fn format_optional_u64_sequence(x: &[Option<u64>]) -> String {
if x.len() == 0 { if x.len() == 0 {
"(empty)".into() "(empty)".into()
} else { } else {
join_strings(x.iter() join_strings(
.enumerate() x.iter().enumerate().map(|(i, x)| {
.map(|(i, x)| { format!(
format!("[{}] = {}", i, x.map(|x| format!("{}", x)).unwrap_or_else(|| "?".to_string()).bold().cyan()) "[{}] = {}",
}) i,
, ", ") x.map(|x| format!("{}", x))
.unwrap_or_else(|| "?".to_string())
.bold()
.cyan()
)
}),
", ",
)
} }
} }
@ -78,7 +85,8 @@ extern "C" fn signal_trap_handler(
} }
// TODO: make this safer // TODO: make this safer
let ctx = &*(fault.known_registers[X64Register::GPR(GPR::R15).to_index().0].unwrap() as *mut vm::Ctx); let ctx = &*(fault.known_registers[X64Register::GPR(GPR::R15).to_index().0].unwrap()
as *mut vm::Ctx);
let rsp = fault.known_registers[X64Register::GPR(GPR::RSP).to_index().0].unwrap(); let rsp = fault.known_registers[X64Register::GPR(GPR::RSP).to_index().0].unwrap();
let msm = (*ctx.module) let msm = (*ctx.module)
@ -86,19 +94,41 @@ extern "C" fn signal_trap_handler(
.get_module_state_map() .get_module_state_map()
.unwrap(); .unwrap();
let code_base = (*ctx.module).runnable_module.get_code().unwrap().as_ptr() as usize; let code_base = (*ctx.module).runnable_module.get_code().unwrap().as_ptr() as usize;
let frames = self::read_stack(&msm, code_base, rsp as usize as *const u64, fault.known_registers, Some(fault.ip as usize as u64)); let frames = self::read_stack(
&msm,
code_base,
rsp as usize as *const u64,
fault.known_registers,
Some(fault.ip as usize as u64),
);
use colored::*; use colored::*;
eprintln!("\n{}\n", "Wasmer encountered an error while running your WebAssembly program.".bold().red()); eprintln!(
"\n{}\n",
"Wasmer encountered an error while running your WebAssembly program."
.bold()
.red()
);
if frames.len() == 0 { if frames.len() == 0 {
eprintln!("{}", "Unknown fault address, cannot read stack.".yellow()); eprintln!("{}", "Unknown fault address, cannot read stack.".yellow());
} else { } else {
use colored::*; use colored::*;
eprintln!("{}\n", "Backtrace:".bold()); eprintln!("{}\n", "Backtrace:".bold());
for (i, f) in frames.iter().enumerate() { for (i, f) in frames.iter().enumerate() {
eprintln!("{}", format!("* Frame {} @ Local function {}", i, f.local_function_id).bold()); eprintln!(
eprintln!(" {} {}", "Locals:".bold().yellow(), format_optional_u64_sequence(&f.locals)); "{}",
eprintln!(" {} {}", "Stack:".bold().yellow(), format_optional_u64_sequence(&f.stack)); format!("* Frame {} @ Local function {}", i, f.local_function_id).bold()
);
eprintln!(
" {} {}",
"Locals:".bold().yellow(),
format_optional_u64_sequence(&f.locals)
);
eprintln!(
" {} {}",
"Stack:".bold().yellow(),
format_optional_u64_sequence(&f.stack)
);
eprintln!(""); eprintln!("");
} }
} }
@ -246,10 +276,7 @@ unsafe fn get_faulting_addr_and_ip(
} }
#[cfg(all(target_os = "macos", target_arch = "x86_64"))] #[cfg(all(target_os = "macos", target_arch = "x86_64"))]
unsafe fn get_fault_info( unsafe fn get_fault_info(siginfo: *const c_void, ucontext: *const c_void) -> FaultInfo {
siginfo: *const c_void,
ucontext: *const c_void,
) -> FaultInfo {
#[allow(dead_code)] #[allow(dead_code)]
#[repr(C)] #[repr(C)]
struct ucontext_t { struct ucontext_t {