Skip to content

Commit

Permalink
Proof of Concept
Browse files Browse the repository at this point in the history
  • Loading branch information
pepyakin committed Aug 17, 2018
1 parent 7b22219 commit 625a2b6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion filetests/call_indirect.wat
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
)
(func $minus_1 (param i64) (result i64)
get_local 0
i64.const 1
i64.const 2
i64.sub
)

Expand Down
1 change: 1 addition & 0 deletions lib/execute/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ cranelift-wasm = "0.18.1"
region = "0.3.0"
wasmtime-environ = { path = "../environ" }
memmap = "0.6.2"
nix = "0.11"
27 changes: 25 additions & 2 deletions lib/execute/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::ptr::{self, write_unaligned};
use wasmtime_environ::{
compile_module, Compilation, Module, ModuleTranslation, Relocation, RelocationTarget,
};
use nix::sys::signal::{sigaction, SigAction, SIGILL, SaFlags, SigSet, SigHandler};

/// Executes a module that has been translated with the `wasmtime-environ` environment
/// implementation.
Expand Down Expand Up @@ -149,8 +150,30 @@ pub fn execute(
// the generated code.Thanks to this, we can transmute the code region into a first-class
// Rust function and call it.
unsafe {
let start_func = transmute::<_, fn(*const *mut u8)>(code_buf.as_ptr());
start_func(vmctx.as_ptr());
let sa = SigAction::new(SigHandler::Handler(catch_sigill), SaFlags::empty(), SigSet::empty());
sigaction(SIGILL, &sa).unwrap();

let result = setjmp((&mut setjmp_buffer[..]).as_mut_ptr() as *mut ::nix::libc::c_void);
if result == 0 {
let start_func = transmute::<_, fn(*const *mut u8)>(code_buf.as_ptr());
start_func(vmctx.as_ptr());
} else {
panic!("error: {}", result);
}
}
Ok(())
}

// why 27?
static mut setjmp_buffer: [::nix::libc::c_int; 27] = [0; 27];

extern "C" {
fn setjmp(env: *mut ::nix::libc::c_void) -> ::nix::libc::c_int;
fn longjmp(env: *mut ::nix::libc::c_void, val: ::nix::libc::c_int);
}

extern "C" fn catch_sigill(_: ::nix::libc::c_int) {
unsafe {
longjmp((&mut setjmp_buffer).as_mut_ptr() as *mut ::nix::libc::c_void, 3);
}
}
1 change: 1 addition & 0 deletions lib/execute/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ extern crate cranelift_wasm;
extern crate memmap;
extern crate region;
extern crate wasmtime_environ;
extern crate nix;

mod execute;
mod instance;
Expand Down

0 comments on commit 625a2b6

Please sign in to comment.