Allow using wasmer as a library.

This commit is contained in:
losfair 2018-11-28 13:15:33 +08:00
parent bb91006158
commit 3815eaf13a
3 changed files with 59 additions and 54 deletions

View File

@ -1,19 +1,5 @@
#[macro_use]
extern crate error_chain;
extern crate cranelift_codegen;
extern crate cranelift_entity;
extern crate cranelift_native;
extern crate cranelift_wasm;
extern crate libc;
extern crate memmap;
extern crate region;
extern crate structopt; extern crate structopt;
extern crate wabt; extern crate wasmer;
extern crate wasmparser;
#[macro_use]
extern crate target_lexicon;
extern crate nix;
extern crate rayon;
use std::fs::File; use std::fs::File;
use std::io; use std::io;
@ -23,16 +9,7 @@ use std::process::exit;
use structopt::StructOpt; use structopt::StructOpt;
#[macro_use] use wasmer::*;
mod macros;
#[macro_use]
mod recovery;
pub mod apis;
pub mod common;
pub mod sighandler;
#[cfg(test)]
mod spectests;
pub mod webassembly;
#[derive(Debug, StructOpt)] #[derive(Debug, StructOpt)]
#[structopt(name = "wasmer", about = "WASM execution runtime.")] #[structopt(name = "wasmer", about = "WASM execution runtime.")]

27
src/lib.rs Normal file
View File

@ -0,0 +1,27 @@
#[macro_use]
extern crate error_chain;
extern crate cranelift_codegen;
extern crate cranelift_entity;
extern crate cranelift_native;
extern crate cranelift_wasm;
extern crate libc;
extern crate memmap;
extern crate region;
extern crate structopt;
extern crate wabt;
extern crate wasmparser;
#[macro_use]
extern crate target_lexicon;
pub extern crate nix; // re-exported for usage in macros
extern crate rayon;
#[macro_use]
mod macros;
#[macro_use]
pub mod recovery;
pub mod apis;
pub mod common;
pub mod sighandler;
#[cfg(test)]
mod spectests;
pub mod webassembly;

View File

@ -4,10 +4,10 @@
//! are very special, the async signal unsafety of Rust's TLS implementation generally does not affect the correctness here //! are very special, the async signal unsafety of Rust's TLS implementation generally does not affect the correctness here
//! unless you have memory unsafety elsewhere in your code. //! unless you have memory unsafety elsewhere in your code.
use std::cell::UnsafeCell;
use nix::sys::signal::{Signal, SIGFPE, SIGILL, SIGSEGV, SIGBUS};
use super::webassembly::ErrorKind;
use super::sighandler::install_sighandler; use super::sighandler::install_sighandler;
use super::webassembly::ErrorKind;
use nix::sys::signal::{Signal, SIGBUS, SIGFPE, SIGILL, SIGSEGV};
use std::cell::UnsafeCell;
extern "C" { extern "C" {
pub fn setjmp(env: *mut ::nix::libc::c_void) -> ::nix::libc::c_int; pub fn setjmp(env: *mut ::nix::libc::c_void) -> ::nix::libc::c_int;
@ -29,39 +29,40 @@ thread_local! {
/// the behavior of call_protected is undefined. /// the behavior of call_protected is undefined.
#[macro_export] #[macro_export]
macro_rules! call_protected { macro_rules! call_protected {
($x:expr) => {unsafe { ($x:expr) => {
use crate::webassembly::ErrorKind; unsafe {
use crate::recovery::{SETJMP_BUFFER, setjmp}; use crate::recovery::{setjmp, SETJMP_BUFFER};
use crate::sighandler::install_sighandler; use crate::sighandler::install_sighandler;
use crate::webassembly::ErrorKind;
use nix::sys::signal::{Signal, SIGFPE, SIGILL, SIGSEGV, SIGBUS}; use crate::nix::sys::signal::{Signal, SIGBUS, SIGFPE, SIGILL, SIGSEGV};
let jmp_buf = SETJMP_BUFFER.with(|buf| buf.get()); let jmp_buf = SETJMP_BUFFER.with(|buf| buf.get());
let prev_jmp_buf = *jmp_buf; let prev_jmp_buf = *jmp_buf;
install_sighandler(); install_sighandler();
let signum = setjmp(jmp_buf as *mut ::nix::libc::c_void); let signum = setjmp(jmp_buf as *mut ::nix::libc::c_void);
if signum != 0 { if signum != 0 {
*jmp_buf = prev_jmp_buf; *jmp_buf = prev_jmp_buf;
let signal = match Signal::from_c_int(signum) { let signal = match Signal::from_c_int(signum) {
Ok(SIGFPE) => "floating-point exception", Ok(SIGFPE) => "floating-point exception",
Ok(SIGILL) => "illegal instruction", Ok(SIGILL) => "illegal instruction",
Ok(SIGSEGV) => "segmentation violation", Ok(SIGSEGV) => "segmentation violation",
Ok(SIGBUS) => "bus error", Ok(SIGBUS) => "bus error",
Err(_) => "error while getting the Signal", Err(_) => "error while getting the Signal",
_ => "unkown trapped signal", _ => "unkown trapped signal",
}; };
Err(ErrorKind::RuntimeError(format!("trap - {}", signal))) Err(ErrorKind::RuntimeError(format!("trap - {}", signal)))
} else { } else {
let ret = $x; // TODO: Switch stack? let ret = $x; // TODO: Switch stack?
*jmp_buf = prev_jmp_buf; *jmp_buf = prev_jmp_buf;
Ok(ret) Ok(ret)
}
} }
}} };
} }
/// Unwinds to last protected_call. /// Unwinds to last protected_call.
pub unsafe fn do_unwind(signum: i32) -> ! { pub unsafe fn do_unwind(signum: i32) -> ! {
// Since do_unwind is only expected to get called from WebAssembly code which doesn't hold any host resources (locks etc.) // Since do_unwind is only expected to get called from WebAssembly code which doesn't hold any host resources (locks etc.)