Suspend & restore.

This commit is contained in:
losfair 2019-06-25 20:01:56 +08:00
parent 8303853227
commit 3f2aeec9bc
14 changed files with 402 additions and 67 deletions

13
Cargo.lock generated
View File

@ -70,6 +70,16 @@ dependencies = [
"libc 0.2.57 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "bincode"
version = "1.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"autocfg 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
"byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "bindgen"
version = "0.46.0"
@ -1546,7 +1556,9 @@ dependencies = [
name = "wasmer-runtime-core"
version = "0.5.0"
dependencies = [
"bincode 1.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
"blake2b_simd 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
"cc 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)",
"digest 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"errno 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
"field-offset 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1707,6 +1719,7 @@ dependencies = [
"checksum autocfg 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "0e49efa51329a5fd37e7c79db4621af617cd4e3e5bc224939808d076077077bf"
"checksum backtrace 0.3.26 (registry+https://github.com/rust-lang/crates.io-index)" = "1a13fc43f04daf08ab4f71e3d27e1fc27fc437d3e95ac0063a796d92fb40f39b"
"checksum backtrace-sys 0.1.28 (registry+https://github.com/rust-lang/crates.io-index)" = "797c830ac25ccc92a7f8a7b9862bde440715531514594a6154e3d4a54dd769b6"
"checksum bincode 1.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "9f04a5e50dc80b3d5d35320889053637d15011aed5e66b66b37ae798c65da6f7"
"checksum bindgen 0.46.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8f7f7f0701772b17de73e4f5cbcb1dd6926f4706cba4c1ab62c5367f8bdc94e1"
"checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12"
"checksum blake2b_simd 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ce2571a6cd634670daa2977cc894c1cc2ba57c563c498e5a82c35446f34d056e"

View File

@ -18,6 +18,7 @@ errno = "0.2.4"
libc = "0.2.49"
hex = "0.3.2"
smallvec = "0.6.9"
bincode = "1.1"
# Dependencies for caching.
[dependencies.serde]
@ -47,6 +48,7 @@ field-offset = "0.1.1"
[build-dependencies]
blake2b_simd = "0.4.1"
rustc_version = "0.2.3"
cc = "1.0"
[features]
debug = []

View File

@ -1,5 +1,6 @@
use blake2b_simd::blake2bp;
use std::{env, fs, io::Write, path::PathBuf};
use cc::Build;
const WASMER_VERSION: &'static str = env!("CARGO_PKG_VERSION");
@ -28,4 +29,8 @@ fn main() {
if rustc_version::version_meta().unwrap().channel == rustc_version::Channel::Nightly {
println!("cargo:rustc-cfg=nightly");
}
cc::Build::new()
.file("image-loading.s")
.compile("image-loading");
}

View File

@ -0,0 +1,34 @@
.globl _run_on_wasm_stack
_run_on_wasm_stack:
# (stack_end, stack_begin)
# We need to ensure 16-byte alignment here.
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rbp
movq %rsp, -16(%rdi)
leaq _run_on_wasm_stack.returning(%rip), %rax
movq %rax, -24(%rdi)
movq %rsi, %rsp
popq %rbp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
int $0x3
retq
_run_on_wasm_stack.returning:
movq (%rsp), %rsp
popq %rbp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq

View File

@ -46,7 +46,7 @@ impl IsExport for Export {
/// ```
pub struct ImportObject {
map: Rc<RefCell<HashMap<String, Box<dyn LikeNamespace>>>>,
state_creator: Option<Rc<Fn() -> (*mut c_void, fn(*mut c_void))>>,
pub(crate) state_creator: Option<Rc<Fn() -> (*mut c_void, fn(*mut c_void))>>,
pub allow_missing_functions: bool,
}

View File

@ -44,6 +44,7 @@ pub mod vmcalls;
#[cfg(all(unix, target_arch = "x86_64"))]
pub use trampoline_x64 as trampoline;
pub mod state;
pub mod suspend;
use self::error::CompileResult;
#[doc(inline)]

View File

@ -17,6 +17,8 @@ pub struct MachineState {
pub wasm_stack: Vec<WasmAbstractValue>,
pub wasm_stack_private_depth: usize,
pub wasm_inst_offset: usize,
}
#[derive(Clone, Debug, Default)]
@ -29,11 +31,14 @@ pub struct MachineStateDiff {
pub wasm_stack_push: Vec<WasmAbstractValue>,
pub wasm_stack_pop: usize,
pub wasm_stack_private_depth: usize, // absolute value; not a diff.
pub wasm_inst_offset: usize, // absolute value; not a diff.
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub enum MachineValue {
Undefined,
Vmctx,
PreserveRegister(RegisterIndex),
CopyStackBPRelative(i32), // relative to Base Pointer, in byte offset
ExplicitShadow, // indicates that all values above this are above the shadow region
@ -48,6 +53,7 @@ pub struct FunctionStateMap {
pub locals: Vec<WasmAbstractValue>,
pub shadow_size: usize, // for single-pass backend, 32 bytes on x86-64
pub diffs: Vec<MachineStateDiff>,
pub wasm_offset_to_target_offset: Vec<usize>,
pub loop_offsets: BTreeMap<usize, usize>, /* offset -> diff_id */
pub call_offsets: BTreeMap<usize, usize>, /* offset -> diff_id */
pub trappable_offsets: BTreeMap<usize, usize>, /* offset -> diff_id */
@ -59,13 +65,19 @@ pub struct ModuleStateMap {
pub total_size: usize,
}
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct WasmFunctionStateDump {
pub local_function_id: usize,
pub wasm_inst_offset: usize,
pub stack: Vec<Option<u64>>,
pub locals: Vec<Option<u64>>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct InstanceImage {
pub frames: Vec<WasmFunctionStateDump>,
}
impl ModuleStateMap {
fn lookup_call_ip(&self, ip: usize, base: usize) -> Option<(&FunctionStateMap, MachineState)> {
if ip < base || ip - base >= self.total_size {
@ -121,6 +133,7 @@ impl FunctionStateMap {
shadow_size,
locals,
diffs: vec![],
wasm_offset_to_target_offset: Vec::new(),
loop_offsets: BTreeMap::new(),
call_offsets: BTreeMap::new(),
trappable_offsets: BTreeMap::new(),
@ -164,6 +177,8 @@ impl MachineState {
wasm_stack_push: self.wasm_stack[first_diff_wasm_stack_depth..].to_vec(),
wasm_stack_pop: old.wasm_stack.len() - first_diff_wasm_stack_depth,
wasm_stack_private_depth: self.wasm_stack_private_depth,
wasm_inst_offset: self.wasm_inst_offset,
}
}
}
@ -198,13 +213,28 @@ impl MachineStateDiff {
}
}
state.wasm_stack_private_depth = self.wasm_stack_private_depth;
state.wasm_inst_offset = self.wasm_inst_offset;
state
}
}
impl InstanceImage {
pub fn from_bytes(input: &[u8]) -> Option<InstanceImage> {
use bincode::deserialize;
match deserialize(input) {
Ok(x) => Some(x),
Err(_) => None,
}
}
}
#[cfg(all(unix, target_arch = "x86_64"))]
pub mod x64 {
extern "C" {
fn run_on_wasm_stack(stack_end: *mut u64, stack_begin: *mut u64) -> u64;
}
use super::*;
use crate::vm::Ctx;
pub fn new_machine_state() -> MachineState {
MachineState {
@ -212,9 +242,156 @@ pub mod x64 {
register_values: vec![MachineValue::Undefined; 16 + 8],
wasm_stack: vec![],
wasm_stack_private_depth: 0,
wasm_inst_offset: ::std::usize::MAX,
}
}
pub unsafe fn invoke_call_return_on_stack_raw_image(
msm: &ModuleStateMap,
code_base: usize,
image: &[u8],
vmctx: &mut Ctx,
) -> u64 {
use bincode::deserialize;
let image: InstanceImage = deserialize(image).unwrap();
invoke_call_return_on_stack(msm, code_base, &image, vmctx)
}
#[warn(unused_variables)]
pub unsafe fn invoke_call_return_on_stack(
msm: &ModuleStateMap,
code_base: usize,
image: &InstanceImage,
vmctx: &mut Ctx,
) -> u64 {
let mut stack: Vec<u64> = vec![0; 1048576 * 8 / 8]; // 8MB stack
let mut stack_offset: usize = stack.len();
stack_offset -= 3; // placeholder for call return
let mut last_stack_offset: u64 = 0; // rbp
let mut known_registers: [Option<u64>; 24] = [None; 24];
let local_functions_vec: Vec<&FunctionStateMap> = msm.local_functions.iter().map(|(k, v)| v).collect();
// Bottom to top
for f in image.frames.iter().rev() {
let fsm = local_functions_vec[f.local_function_id];
let call_begin_offset = fsm.wasm_offset_to_target_offset[f.wasm_inst_offset];
let (after_call_inst, diff_id) = fsm.call_offsets.range((Included(&call_begin_offset), Unbounded)).next().map(|(k, v)| (*k, *v)).expect("instruction offset not found in call offsets");
let diff = &fsm.diffs[diff_id];
let state = diff.build_state(fsm);
stack_offset -= 1;
stack[stack_offset] = stack.as_ptr().offset(last_stack_offset as isize) as usize as u64; // push rbp
last_stack_offset = stack_offset as _;
let mut got_explicit_shadow = false;
for v in state.stack_values.iter() {
match *v {
MachineValue::Undefined => { stack_offset -= 1 },
MachineValue::Vmctx => {
stack_offset -= 1;
stack[stack_offset] = vmctx as *mut Ctx as usize as u64;
}
MachineValue::PreserveRegister(index) => {
stack_offset -= 1;
stack[stack_offset] = known_registers[index.0].unwrap_or(0);
}
MachineValue::CopyStackBPRelative(byte_offset) => {
assert!(byte_offset % 8 == 0);
let target_offset = (byte_offset / 8) as isize;
let v = stack[(last_stack_offset as isize + target_offset) as usize];
stack_offset -= 1;
stack[stack_offset] = v;
}
MachineValue::ExplicitShadow => {
assert!(fsm.shadow_size % 8 == 0);
stack_offset -= fsm.shadow_size / 8;
got_explicit_shadow = true;
}
MachineValue::WasmStack(x) => {
stack_offset -= 1;
match state.wasm_stack[x] {
WasmAbstractValue::Const(x) => {
stack[stack_offset] = x;
}
WasmAbstractValue::Runtime => {
stack[stack_offset] = f.stack[x].unwrap();
}
}
}
MachineValue::WasmLocal(x) => {
stack_offset -= 1;
match fsm.locals[x] {
WasmAbstractValue::Const(x) => {
stack[stack_offset] = x;
}
WasmAbstractValue::Runtime => {
stack[stack_offset] = f.locals[x].unwrap();
}
}
}
}
}
assert!(got_explicit_shadow);
for (i, v) in state.register_values.iter().enumerate() {
match *v {
MachineValue::Undefined => {},
MachineValue::Vmctx => {
known_registers[i] = Some(vmctx as *mut Ctx as usize as u64);
}
MachineValue::WasmStack(x) => {
match state.wasm_stack[x] {
WasmAbstractValue::Const(x) => {
known_registers[i] = Some(x);
}
WasmAbstractValue::Runtime => {
known_registers[i] = Some(f.stack[x].unwrap());
}
}
}
MachineValue::WasmLocal(x) => {
match fsm.locals[x] {
WasmAbstractValue::Const(x) => {
known_registers[i] = Some(x);
}
WasmAbstractValue::Runtime => {
known_registers[i] = Some(f.locals[x].unwrap());
}
}
}
_ => unreachable!()
}
}
assert!((stack.len() - stack_offset) % 2 == 0); // 16-byte alignment
stack_offset -= 1;
stack[stack_offset] = (code_base + after_call_inst) as u64; // return address
}
stack_offset -= 1;
stack[stack_offset] = known_registers[X64Register::GPR(GPR::R15).to_index().0].unwrap_or(0);
stack_offset -= 1;
stack[stack_offset] = known_registers[X64Register::GPR(GPR::R14).to_index().0].unwrap_or(0);
stack_offset -= 1;
stack[stack_offset] = known_registers[X64Register::GPR(GPR::R13).to_index().0].unwrap_or(0);
stack_offset -= 1;
stack[stack_offset] = known_registers[X64Register::GPR(GPR::R12).to_index().0].unwrap_or(0);
stack_offset -= 1;
stack[stack_offset] = known_registers[X64Register::GPR(GPR::RBX).to_index().0].unwrap_or(0);
stack_offset -= 1;
stack[stack_offset] = stack.as_ptr().offset(last_stack_offset as isize) as usize as u64; // rbp
run_on_wasm_stack(stack.as_mut_ptr().offset(stack.len() as isize), stack.as_mut_ptr().offset(stack_offset as isize))
}
#[warn(unused_variables)]
pub unsafe fn read_stack(
msm: &ModuleStateMap,
@ -222,7 +399,7 @@ pub mod x64 {
mut stack: *const u64,
initially_known_registers: [Option<u64>; 24],
mut initial_address: Option<u64>,
) -> Vec<WasmFunctionStateDump> {
) -> InstanceImage {
let mut known_registers: [Option<u64>; 24] = initially_known_registers;
let mut results: Vec<WasmFunctionStateDump> = vec![];
@ -237,7 +414,9 @@ pub mod x64 {
.or_else(|| msm.lookup_trappable_ip(ret_addr as usize, code_base))
{
Some(x) => x,
_ => return results,
_ => return InstanceImage {
frames: results,
}
};
let mut wasm_stack: Vec<Option<u64>> = state
@ -261,6 +440,7 @@ pub mod x64 {
for (i, v) in state.register_values.iter().enumerate() {
match *v {
MachineValue::Undefined => {}
MachineValue::Vmctx => {}
MachineValue::WasmStack(idx) => {
if let Some(v) = known_registers[i] {
wasm_stack[idx] = Some(v);
@ -297,6 +477,9 @@ pub mod x64 {
MachineValue::Undefined => {
stack = stack.offset(1);
}
MachineValue::Vmctx => {
stack = stack.offset(1);
}
MachineValue::PreserveRegister(idx) => {
known_registers[idx.0] = Some(*stack);
stack = stack.offset(1);
@ -325,6 +508,7 @@ pub mod x64 {
let wfs = WasmFunctionStateDump {
local_function_id: fsm.local_function_id,
wasm_inst_offset: state.wasm_inst_offset,
stack: wasm_stack,
locals: wasm_locals,
};

View File

@ -0,0 +1,95 @@
use crate::import::{ImportObject, Namespace};
use crate::trampoline::{TrampolineBufferBuilder, TrampolineBuffer, CallContext};
use crate::vm::Ctx;
use std::ffi::c_void;
use std::rc::Rc;
use bincode::serialize;
use std::fs::File;
use std::io::Write;
pub struct SuspendConfig {
pub image_path: String,
}
struct ImportContext {
next: Option<(*mut c_void, fn(*mut c_void))>,
trampolines: Rc<TrampolineBuffer>,
config: Rc<SuspendConfig>
}
impl ImportContext {
fn new(trampolines: Rc<TrampolineBuffer>, config: Rc<SuspendConfig>) -> ImportContext {
ImportContext {
trampolines,
next: None,
config,
}
}
}
fn destroy_import_context(x: *mut c_void) {
unsafe {
let ctx = Box::from_raw(x as *mut ImportContext);
if let Some(x) = ctx.next {
(x.1)(x.0);
}
}
}
pub fn patch_import_object(x: &mut ImportObject, config: SuspendConfig) {
let config = Rc::new(config);
let mut builder = TrampolineBufferBuilder::new();
let config_ptr: &SuspendConfig = &*config;
let idx = builder.add_context_rsp_state_preserving_trampoline(suspend, config_ptr as *const SuspendConfig as *const CallContext);
let trampolines = builder.build();
let suspend_indirect: fn(&mut Ctx) =
unsafe { ::std::mem::transmute(trampolines.get_trampoline(idx)) };
let trampolines = Rc::new(trampolines);
// FIXME: Memory leak!
::std::mem::forget(ImportContext::new(trampolines.clone(), config.clone()));
let mut ns = Namespace::new();
ns.insert("suspend", func!(suspend_indirect));
x.register("wasmer_suspend", ns);
}
unsafe extern "C" fn suspend(ctx: &mut Ctx, config_ptr_raw: *const CallContext, mut stack: *const u64) {
use crate::state::x64::{X64Register, GPR, read_stack};
{
let config = &*(config_ptr_raw as *const SuspendConfig);
let msm = (*ctx.module)
.runnable_module
.get_module_state_map()
.unwrap();
let code_base = (*ctx.module).runnable_module.get_code().unwrap().as_ptr() as usize;
let mut known_registers: [Option<u64>; 24] = [None; 24];
let r15 = *stack;
let r14 = *stack.offset(1);
let r13 = *stack.offset(2);
let r12 = *stack.offset(3);
let rbx = *stack.offset(4);
stack = stack.offset(5);
known_registers[X64Register::GPR(GPR::R15).to_index().0] = Some(r15);
known_registers[X64Register::GPR(GPR::R14).to_index().0] = Some(r14);
known_registers[X64Register::GPR(GPR::R13).to_index().0] = Some(r13);
known_registers[X64Register::GPR(GPR::R12).to_index().0] = Some(r12);
known_registers[X64Register::GPR(GPR::RBX).to_index().0] = Some(rbx);
let image = read_stack(&msm, code_base, stack, known_registers, None);
let image_bin = serialize(&image).unwrap();
let mut f = File::create(&config.image_path).unwrap();
f.write_all(&image_bin).unwrap();
println!("{:?}", image);
}
panic!("suspended");
}

View File

@ -1612,6 +1612,8 @@ impl FunctionCodeGenerator<CodegenError> for X64FunctionCode {
.machine
.init_locals(a, self.num_locals, self.num_params);
self.machine.state.register_values[X64Register::GPR(Machine::get_vmctx_reg()).to_index().0] = MachineValue::Vmctx;
self.fsm = FunctionStateMap::new(
new_machine_state(),
self.local_function_id,
@ -1638,6 +1640,9 @@ impl FunctionCodeGenerator<CodegenError> for X64FunctionCode {
state: self.machine.state.clone(),
state_diff_id,
});
assert_eq!(self.machine.state.wasm_inst_offset, ::std::usize::MAX);
Ok(())
}
@ -1648,6 +1653,17 @@ impl FunctionCodeGenerator<CodegenError> for X64FunctionCode {
}
fn feed_event(&mut self, ev: Event, module_info: &ModuleInfo) -> Result<(), CodegenError> {
let a = self.assembler.as_mut().unwrap();
match ev {
Event::Wasm(_) => {
self.machine.state.wasm_inst_offset =
self.machine.state.wasm_inst_offset.wrapping_add(1);
self.fsm.wasm_offset_to_target_offset.push(a.get_offset().0);
}
_ => {}
}
//println!("{:?} {}", op, self.value_stack.len());
let was_unreachable;
@ -1682,8 +1698,6 @@ impl FunctionCodeGenerator<CodegenError> for X64FunctionCode {
was_unreachable = false;
}
let a = self.assembler.as_mut().unwrap();
let op = match ev {
Event::Wasm(x) => x,
Event::WasmOwned(ref x) => x,

View File

@ -22,7 +22,7 @@ extern crate smallvec;
mod codegen_x64;
mod emitter_x64;
mod machine;
mod protect_unix;
pub mod protect_unix;
pub use codegen_x64::X64FunctionCode as FunctionCodeGenerator;
pub use codegen_x64::X64ModuleCodeGenerator as ModuleCodeGenerator;

View File

@ -94,7 +94,7 @@ extern "C" fn signal_trap_handler(
.get_module_state_map()
.unwrap();
let code_base = (*ctx.module).runnable_module.get_code().unwrap().as_ptr() as usize;
let frames = self::read_stack(
let image = self::read_stack(
&msm,
code_base,
rsp as usize as *const u64,
@ -109,16 +109,21 @@ extern "C" fn signal_trap_handler(
.bold()
.red()
);
if frames.len() == 0 {
if image.frames.len() == 0 {
eprintln!("{}", "Unknown fault address, cannot read stack.".yellow());
} else {
use colored::*;
eprintln!("{}\n", "Backtrace:".bold());
for (i, f) in frames.iter().enumerate() {
for (i, f) in image.frames.iter().enumerate() {
eprintln!(
"{}",
format!("* Frame {} @ Local function {}", i, f.local_function_id).bold()
);
eprintln!(
" {} {}",
"Offset:".bold().yellow(),
format!("{}", f.wasm_inst_offset).bold().cyan(),
);
eprintln!(
" {} {}",
"Locals:".bold().yellow(),

View File

@ -20,14 +20,10 @@ use std::path::PathBuf;
pub use self::utils::is_wasi_module;
use std::rc::Rc;
use wasmer_runtime_core::state::x64::read_stack;
use wasmer_runtime_core::vm::Ctx;
use wasmer_runtime_core::{
func,
import::ImportObject,
imports,
trampoline::{CallContext, TrampolineBufferBuilder},
};
/// This is returned in the Box<dyn Any> RuntimeError::Error variant.
@ -42,43 +38,6 @@ pub fn generate_import_object(
preopened_files: Vec<String>,
mapped_dirs: Vec<(String, PathBuf)>,
) -> ImportObject {
unsafe extern "C" fn read_stack(ctx: &mut Ctx, _: *const CallContext, mut stack: *const u64) {
use wasmer_runtime_core::state::x64::{X64Register, GPR};
let msm = (*ctx.module)
.runnable_module
.get_module_state_map()
.unwrap();
let code_base = (*ctx.module).runnable_module.get_code().unwrap().as_ptr() as usize;
let mut known_registers: [Option<u64>; 24] = [None; 24];
let r15 = *stack;
let r14 = *stack.offset(1);
let r13 = *stack.offset(2);
let r12 = *stack.offset(3);
let rbx = *stack.offset(4);
stack = stack.offset(5);
known_registers[X64Register::GPR(GPR::R15).to_index().0] = Some(r15);
known_registers[X64Register::GPR(GPR::R14).to_index().0] = Some(r14);
known_registers[X64Register::GPR(GPR::R13).to_index().0] = Some(r13);
known_registers[X64Register::GPR(GPR::R12).to_index().0] = Some(r12);
known_registers[X64Register::GPR(GPR::RBX).to_index().0] = Some(rbx);
let stack_dump = self::read_stack(&msm, code_base, stack, known_registers, None);
println!("{:?}", stack_dump);
}
let mut builder = TrampolineBufferBuilder::new();
let idx = builder.add_context_rsp_state_preserving_trampoline(read_stack, ::std::ptr::null());
let trampolines = builder.build();
let read_stack_indirect: fn(&mut Ctx) =
unsafe { ::std::mem::transmute(trampolines.get_trampoline(idx)) };
let trampolines = Rc::new(trampolines);
let state_gen = move || {
fn state_destructor(data: *mut c_void) {
unsafe {
@ -90,7 +49,6 @@ pub fn generate_import_object(
fs: WasiFs::new(&preopened_files, &mapped_dirs).unwrap(),
args: &args[..],
envs: &envs[..],
trampolines: trampolines.clone(),
});
(
@ -102,7 +60,6 @@ pub fn generate_import_object(
// This generates the wasi state.
state_gen,
"wasi_unstable" => {
"stack_read" => func!(read_stack_indirect),
"args_get" => func!(args_get),
"args_sizes_get" => func!(args_sizes_get),
"clock_res_get" => func!(clock_res_get),

View File

@ -10,11 +10,9 @@ use std::{
fs,
io::{self, Read, Seek, Write},
path::PathBuf,
rc::Rc,
time::SystemTime,
};
use wasmer_runtime_core::debug;
use wasmer_runtime_core::trampoline::TrampolineBuffer;
pub const MAX_SYMLINKS: usize = 100;
@ -462,7 +460,6 @@ pub struct WasiState<'a> {
pub fs: WasiFs,
pub args: &'a [Vec<u8>],
pub envs: &'a [Vec<u8>],
pub trampolines: Rc<TrampolineBuffer>,
}
pub fn host_file_type_to_wasi_file_type(file_type: fs::FileType) -> __wasi_filetype_t {

View File

@ -112,6 +112,12 @@ struct Run {
)]
loader: Option<LoaderName>,
#[structopt(long = "suspend-to")]
suspend_to: Option<String>,
#[structopt(long = "restore-from")]
restore_from: Option<String>,
#[structopt(long = "command-name", hidden = true)]
command_name: Option<String>,
@ -483,7 +489,7 @@ fn execute_wasm(options: &Run) -> Result<(), String> {
.map_err(|e| format!("{:?}", e))?;
} else {
if cfg!(feature = "wasi") && wasmer_wasi::is_wasi_module(&module) {
let import_object = wasmer_wasi::generate_import_object(
let mut import_object = wasmer_wasi::generate_import_object(
if let Some(cn) = &options.command_name {
[cn.clone()]
} else {
@ -502,24 +508,46 @@ fn execute_wasm(options: &Run) -> Result<(), String> {
mapped_dirs,
);
let instance = module
if let Some(ref name) = options.suspend_to {
use wasmer_runtime_core::suspend::{SuspendConfig, patch_import_object};
patch_import_object(&mut import_object, SuspendConfig {
image_path: name.clone(),
});
}
let mut instance = module
.instantiate(&import_object)
.map_err(|e| format!("Can't instantiate module: {:?}", e))?;
let start: Func<(), ()> = instance.func("_start").map_err(|e| format!("{:?}", e))?;
if let Some(ref name) = options.restore_from {
use wasmer_singlepass_backend::protect_unix::call_protected;
use wasmer_runtime_core::state::x64::invoke_call_return_on_stack_raw_image;
let result = start.call();
let mut file = File::open(name).expect("cannot open image file");
let mut image: Vec<u8> = vec![];
file.read_to_end(&mut image).unwrap();
if let Err(ref err) = result {
match err {
RuntimeError::Trap { msg } => panic!("wasm trap occured: {}", msg),
RuntimeError::Error { data } => {
if let Some(error_code) = data.downcast_ref::<wasmer_wasi::ExitCode>() {
std::process::exit(error_code.code as i32)
let msm = instance.module.runnable_module.get_module_state_map().unwrap();
let code_base = instance.module.runnable_module.get_code().unwrap().as_ptr() as usize;
call_protected(|| {
unsafe { invoke_call_return_on_stack_raw_image(&msm, code_base, &image, instance.context_mut()); }
}).map_err(|_| "ERROR").unwrap();
} else {
let start: Func<(), ()> = instance.func("_start").map_err(|e| format!("{:?}", e))?;
let result = start.call();
if let Err(ref err) = result {
match err {
RuntimeError::Trap { msg } => panic!("wasm trap occured: {}", msg),
RuntimeError::Error { data } => {
if let Some(error_code) = data.downcast_ref::<wasmer_wasi::ExitCode>() {
std::process::exit(error_code.code as i32)
}
}
}
panic!("error: {:?}", err)
}
panic!("error: {:?}", err)
}
} else {
let import_object = wasmer_runtime_core::import::ImportObject::new();