mirror of
https://github.com/fluencelabs/wasmer
synced 2025-04-25 10:22:19 +00:00
Cleanup internal logic.
This commit is contained in:
parent
1bd30bed4b
commit
db117d2f26
@ -1,10 +1,5 @@
|
|||||||
use blake2::{Blake2b, Digest};
|
use blake2::{Blake2b, Digest};
|
||||||
|
|
||||||
#[link(wasm_import_module = "wasmer_suspend")]
|
|
||||||
extern "C" {
|
|
||||||
fn check_interrupt();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut data: Vec<u8> = b"test".to_vec();
|
let mut data: Vec<u8> = b"test".to_vec();
|
||||||
|
|
||||||
@ -17,8 +12,5 @@ fn main() {
|
|||||||
if i % 1000000 == 0 {
|
if i % 1000000 == 0 {
|
||||||
println!("Round {}: {:?}", i, data);
|
println!("Round {}: {:?}", i, data);
|
||||||
}
|
}
|
||||||
unsafe {
|
|
||||||
check_interrupt();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -198,7 +198,6 @@ extern "C" fn signal_trap_handler(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(SIGSEGV) | Ok(SIGBUS) => {
|
Ok(SIGSEGV) | Ok(SIGBUS) => {
|
||||||
println!("SIGSEGV/SIGBUS on addr {:?}", fault.faulting_addr);
|
|
||||||
if fault.faulting_addr as usize == get_wasm_interrupt_signal_mem() as usize {
|
if fault.faulting_addr as usize == get_wasm_interrupt_signal_mem() as usize {
|
||||||
is_suspend_signal = true;
|
is_suspend_signal = true;
|
||||||
clear_wasm_interrupt();
|
clear_wasm_interrupt();
|
||||||
|
@ -53,13 +53,20 @@ pub struct FunctionStateMap {
|
|||||||
pub locals: Vec<WasmAbstractValue>,
|
pub locals: Vec<WasmAbstractValue>,
|
||||||
pub shadow_size: usize, // for single-pass backend, 32 bytes on x86-64
|
pub shadow_size: usize, // for single-pass backend, 32 bytes on x86-64
|
||||||
pub diffs: Vec<MachineStateDiff>,
|
pub diffs: Vec<MachineStateDiff>,
|
||||||
pub wasm_function_header_target_offset: Option<usize>,
|
pub wasm_function_header_target_offset: Option<SuspendOffset>,
|
||||||
pub wasm_offset_to_target_offset: Vec<usize>,
|
pub wasm_offset_to_target_offset: BTreeMap<usize, SuspendOffset>,
|
||||||
pub loop_offsets: BTreeMap<usize, OffsetInfo>, /* suspend_offset -> info */
|
pub loop_offsets: BTreeMap<usize, OffsetInfo>, /* suspend_offset -> info */
|
||||||
pub call_offsets: BTreeMap<usize, OffsetInfo>, /* suspend_offset -> info */
|
pub call_offsets: BTreeMap<usize, OffsetInfo>, /* suspend_offset -> info */
|
||||||
pub trappable_offsets: BTreeMap<usize, OffsetInfo>, /* suspend_offset -> info */
|
pub trappable_offsets: BTreeMap<usize, OffsetInfo>, /* suspend_offset -> info */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug)]
|
||||||
|
pub enum SuspendOffset {
|
||||||
|
Loop(usize),
|
||||||
|
Call(usize),
|
||||||
|
Trappable(usize),
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct OffsetInfo {
|
pub struct OffsetInfo {
|
||||||
pub diff_id: usize,
|
pub diff_id: usize,
|
||||||
@ -166,7 +173,7 @@ impl FunctionStateMap {
|
|||||||
locals,
|
locals,
|
||||||
diffs: vec![],
|
diffs: vec![],
|
||||||
wasm_function_header_target_offset: None,
|
wasm_function_header_target_offset: None,
|
||||||
wasm_offset_to_target_offset: Vec::new(),
|
wasm_offset_to_target_offset: BTreeMap::new(),
|
||||||
loop_offsets: BTreeMap::new(),
|
loop_offsets: BTreeMap::new(),
|
||||||
call_offsets: BTreeMap::new(),
|
call_offsets: BTreeMap::new(),
|
||||||
trappable_offsets: BTreeMap::new(),
|
trappable_offsets: BTreeMap::new(),
|
||||||
@ -361,7 +368,6 @@ pub mod x64 {
|
|||||||
use crate::types::LocalGlobalIndex;
|
use crate::types::LocalGlobalIndex;
|
||||||
use crate::vm::Ctx;
|
use crate::vm::Ctx;
|
||||||
use std::any::Any;
|
use std::any::Any;
|
||||||
use std::ops::Bound::Excluded;
|
|
||||||
|
|
||||||
pub fn new_machine_state() -> MachineState {
|
pub fn new_machine_state() -> MachineState {
|
||||||
MachineState {
|
MachineState {
|
||||||
@ -396,31 +402,22 @@ pub mod x64 {
|
|||||||
// Bottom to top
|
// Bottom to top
|
||||||
for f in image.execution_state.frames.iter().rev() {
|
for f in image.execution_state.frames.iter().rev() {
|
||||||
let fsm = local_functions_vec[f.local_function_id];
|
let fsm = local_functions_vec[f.local_function_id];
|
||||||
let begin_offset = if f.wasm_inst_offset == ::std::usize::MAX {
|
let suspend_offset = if f.wasm_inst_offset == ::std::usize::MAX {
|
||||||
fsm.wasm_function_header_target_offset.unwrap()
|
fsm.wasm_function_header_target_offset
|
||||||
} else {
|
} else {
|
||||||
fsm.wasm_offset_to_target_offset[f.wasm_inst_offset]
|
fsm.wasm_offset_to_target_offset
|
||||||
};
|
.get(&f.wasm_inst_offset)
|
||||||
|
.map(|x| *x)
|
||||||
|
}
|
||||||
|
.expect("instruction is not a critical point");
|
||||||
|
|
||||||
let (target_inst_offset, diff_id) = fsm
|
let (activate_offset, diff_id) = match suspend_offset {
|
||||||
.loop_offsets
|
SuspendOffset::Loop(x) => fsm.loop_offsets.get(&x),
|
||||||
.get(&begin_offset)
|
SuspendOffset::Call(x) => fsm.call_offsets.get(&x),
|
||||||
.map(|v| (v.activate_offset, v.diff_id))
|
SuspendOffset::Trappable(x) => fsm.trappable_offsets.get(&x),
|
||||||
.or_else(|| {
|
}
|
||||||
fsm.trappable_offsets
|
.map(|x| (x.activate_offset, x.diff_id))
|
||||||
.get(&begin_offset)
|
.expect("offset cannot be found in table");
|
||||||
.map(|v| (v.activate_offset, v.diff_id))
|
|
||||||
})
|
|
||||||
.or_else(|| {
|
|
||||||
// Left bound must be Excluded because it's possible that the previous instruction's (after-)call offset == call_begin_offset.
|
|
||||||
// This might not be the correct offset if begin_offset itself does not correspond to a call(_indirect) instruction,
|
|
||||||
// but anyway safety isn't broken because diff_id always corresponds to target_inst_offset.
|
|
||||||
fsm.call_offsets
|
|
||||||
.range((Excluded(&begin_offset), Unbounded))
|
|
||||||
.next()
|
|
||||||
.map(|(_, v)| (v.activate_offset, v.diff_id))
|
|
||||||
})
|
|
||||||
.expect("instruction offset not found in any offset type");
|
|
||||||
|
|
||||||
let diff = &fsm.diffs[diff_id];
|
let diff = &fsm.diffs[diff_id];
|
||||||
let state = diff.build_state(fsm);
|
let state = diff.build_state(fsm);
|
||||||
@ -511,7 +508,7 @@ pub mod x64 {
|
|||||||
// no need to check 16-byte alignment here because it's possible that we're not at a call entry.
|
// no need to check 16-byte alignment here because it's possible that we're not at a call entry.
|
||||||
|
|
||||||
stack_offset -= 1;
|
stack_offset -= 1;
|
||||||
stack[stack_offset] = (code_base + target_inst_offset) as u64; // return address
|
stack[stack_offset] = (code_base + activate_offset) as u64; // return address
|
||||||
}
|
}
|
||||||
|
|
||||||
stack_offset -= 1;
|
stack_offset -= 1;
|
||||||
|
@ -24,7 +24,7 @@ use wasmer_runtime_core::{
|
|||||||
module::{ModuleInfo, ModuleInner},
|
module::{ModuleInfo, ModuleInner},
|
||||||
state::{
|
state::{
|
||||||
x64::new_machine_state, x64::X64Register, FunctionStateMap, MachineState, MachineValue,
|
x64::new_machine_state, x64::X64Register, FunctionStateMap, MachineState, MachineValue,
|
||||||
ModuleStateMap, OffsetInfo, WasmAbstractValue,
|
ModuleStateMap, OffsetInfo, SuspendOffset, WasmAbstractValue,
|
||||||
},
|
},
|
||||||
structures::{Map, TypedIndex},
|
structures::{Map, TypedIndex},
|
||||||
typed_func::Wasm,
|
typed_func::Wasm,
|
||||||
@ -564,6 +564,8 @@ impl X64FunctionCode {
|
|||||||
diff_id: state_diff_id,
|
diff_id: state_diff_id,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
fsm.wasm_offset_to_target_offset
|
||||||
|
.insert(m.state.wasm_inst_offset, SuspendOffset::Trappable(offset));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Moves `loc` to a valid location for `div`/`idiv`.
|
/// Moves `loc` to a valid location for `div`/`idiv`.
|
||||||
@ -1331,6 +1333,8 @@ impl X64FunctionCode {
|
|||||||
diff_id: state_diff_id,
|
diff_id: state_diff_id,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
fsm.wasm_offset_to_target_offset
|
||||||
|
.insert(m.state.wasm_inst_offset, SuspendOffset::Call(offset));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Restore stack.
|
// Restore stack.
|
||||||
@ -1685,7 +1689,7 @@ impl FunctionCodeGenerator<CodegenError> for X64FunctionCode {
|
|||||||
diff_id: state_diff_id,
|
diff_id: state_diff_id,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
self.fsm.wasm_function_header_target_offset = Some(a.get_offset().0);
|
self.fsm.wasm_function_header_target_offset = Some(SuspendOffset::Loop(a.get_offset().0));
|
||||||
a.emit_mov(
|
a.emit_mov(
|
||||||
Size::S64,
|
Size::S64,
|
||||||
Location::Memory(GPR::RAX, 0),
|
Location::Memory(GPR::RAX, 0),
|
||||||
@ -1707,14 +1711,15 @@ impl FunctionCodeGenerator<CodegenError> for X64FunctionCode {
|
|||||||
let a = self.assembler.as_mut().unwrap();
|
let a = self.assembler.as_mut().unwrap();
|
||||||
|
|
||||||
match ev {
|
match ev {
|
||||||
Event::Wasm(_) => {
|
Event::Internal(InternalEvent::FunctionBegin(_))
|
||||||
self.machine.state.wasm_inst_offset =
|
| Event::Internal(InternalEvent::FunctionEnd) => {
|
||||||
self.machine.state.wasm_inst_offset.wrapping_add(1);
|
return Ok(());
|
||||||
self.fsm.wasm_offset_to_target_offset.push(a.get_offset().0);
|
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.machine.state.wasm_inst_offset = self.machine.state.wasm_inst_offset.wrapping_add(1);
|
||||||
|
|
||||||
//println!("{:?} {}", op, self.value_stack.len());
|
//println!("{:?} {}", op, self.value_stack.len());
|
||||||
let was_unreachable;
|
let was_unreachable;
|
||||||
|
|
||||||
@ -3945,6 +3950,10 @@ impl FunctionCodeGenerator<CodegenError> for X64FunctionCode {
|
|||||||
diff_id: state_diff_id,
|
diff_id: state_diff_id,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
self.fsm.wasm_offset_to_target_offset.insert(
|
||||||
|
self.machine.state.wasm_inst_offset,
|
||||||
|
SuspendOffset::Loop(a.get_offset().0),
|
||||||
|
);
|
||||||
a.emit_mov(
|
a.emit_mov(
|
||||||
Size::S64,
|
Size::S64,
|
||||||
Location::Memory(GPR::RAX, 0),
|
Location::Memory(GPR::RAX, 0),
|
||||||
|
@ -696,6 +696,7 @@ fn interactive_shell(mut ctx: InteractiveShellContext) -> ShellExitOperation {
|
|||||||
"exit" | "quit" => {
|
"exit" | "quit" => {
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
"" => {}
|
||||||
_ => {
|
_ => {
|
||||||
println!("Unknown command: {}", cmd);
|
println!("Unknown command: {}", cmd);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user