Cleanup loader code.

This commit is contained in:
losfair 2019-05-14 16:02:27 +08:00
parent 2a160c74ad
commit 722ea39877
5 changed files with 379 additions and 299 deletions

636
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -33,7 +33,7 @@ wasmer-runtime-core = { path = "lib/runtime-core" }
wasmer-emscripten = { path = "lib/emscripten" } wasmer-emscripten = { path = "lib/emscripten" }
wasmer-llvm-backend = { path = "lib/llvm-backend", optional = true } wasmer-llvm-backend = { path = "lib/llvm-backend", optional = true }
wasmer-wasi = { path = "lib/wasi", optional = true } wasmer-wasi = { path = "lib/wasi", optional = true }
kwasm-loader = { path = "lib/kwasm-loader" } kwasm-loader = { path = "lib/kwasm-loader", optional = true }
[workspace] [workspace]
members = ["lib/clif-backend", "lib/singlepass-backend", "lib/runtime", "lib/runtime-abi", "lib/runtime-core", "lib/emscripten", "lib/spectests", "lib/win-exception-handler", "lib/runtime-c-api", "lib/llvm-backend", "lib/wasi", "lib/middleware-common", "lib/kwasm-loader", "lib/kwasm-net", "examples/pipe", "examples/wasi-networking", "examples/http-server", "examples/plugin-for-example"] members = ["lib/clif-backend", "lib/singlepass-backend", "lib/runtime", "lib/runtime-abi", "lib/runtime-core", "lib/emscripten", "lib/spectests", "lib/win-exception-handler", "lib/runtime-c-api", "lib/llvm-backend", "lib/wasi", "lib/middleware-common", "lib/kwasm-loader", "lib/kwasm-net", "examples/pipe", "examples/wasi-networking", "examples/http-server", "examples/plugin-for-example"]
@ -45,6 +45,7 @@ rustc_version = "0.2.3"
[features] [features]
default = ["fast-tests", "wasi"] default = ["fast-tests", "wasi"]
"loader:kwasm" = ["kwasm-loader"]
debug = ["wasmer-runtime-core/debug"] debug = ["wasmer-runtime-core/debug"]
extra-debug = ["wasmer-clif-backend/debug", "wasmer-runtime-core/debug"] extra-debug = ["wasmer-clif-backend/debug", "wasmer-runtime-core/debug"]
# This feature will allow cargo test to run much faster # This feature will allow cargo test to run much faster

View File

@ -8,6 +8,7 @@ use crate::{
module::ModuleInfo, module::ModuleInfo,
types::Value, types::Value,
}; };
#[cfg(unix)]
use libc::{ use libc::{
mmap, mprotect, munmap, MAP_ANON, MAP_PRIVATE, PROT_EXEC, PROT_READ, mmap, mprotect, munmap, MAP_ANON, MAP_PRIVATE, PROT_EXEC, PROT_READ,
PROT_WRITE, PROT_WRITE,
@ -81,6 +82,18 @@ pub struct CodeMemory {
size: usize, size: usize,
} }
#[cfg(not(unix))]
impl CodeMemory {
pub fn new(_size: usize) -> CodeMemory {
unimplemented!();
}
pub fn make_executable(&mut self) {
unimplemented!();
}
}
#[cfg(unix)]
impl CodeMemory { impl CodeMemory {
pub fn new(size: usize) -> CodeMemory { pub fn new(size: usize) -> CodeMemory {
fn round_up_to_page_size(size: usize) -> usize { fn round_up_to_page_size(size: usize) -> usize {
@ -113,6 +126,7 @@ impl CodeMemory {
} }
} }
#[cfg(unix)]
impl Drop for CodeMemory { impl Drop for CodeMemory {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { munmap(self.ptr as _, self.size); } unsafe { munmap(self.ptr as _, self.size); }

View File

@ -5,13 +5,12 @@ use std::thread;
use structopt::StructOpt; use structopt::StructOpt;
use wasmer::*; use wasmer::*;
use wasmer_runtime::{ use wasmer_runtime::{
error::RuntimeError, Value,
Func, Value,
}; };
use wasmer_runtime_core::{ use wasmer_runtime_core::{
self, self,
backend::{Compiler, CompilerConfig, MemoryBoundCheckMode}, backend::{CompilerConfig, MemoryBoundCheckMode},
loader::{self, Loader, Instance as LoadedInstance, LocalLoader}, loader::{Instance as LoadedInstance},
}; };
use wasmer_singlepass_backend::SinglePassCompiler; use wasmer_singlepass_backend::SinglePassCompiler;
@ -37,6 +36,7 @@ const CMD_RUN_CODE: u32 = 0x901;
const CMD_READ_MEMORY: u32 = 0x902; const CMD_READ_MEMORY: u32 = 0x902;
const CMD_WRITE_MEMORY: u32 = 0x903; const CMD_WRITE_MEMORY: u32 = 0x903;
#[cfg(feature = "loader:kwasm")]
fn handle_client(mut stream: UnixStream) { fn handle_client(mut stream: UnixStream) {
let binary_size = stream.read_u32::<LittleEndian>().unwrap(); let binary_size = stream.read_u32::<LittleEndian>().unwrap();
if binary_size > 1048576 * 16 { if binary_size > 1048576 * 16 {
@ -127,6 +127,7 @@ fn handle_client(mut stream: UnixStream) {
} }
} }
#[cfg(feature = "loader:kwasm")]
fn run_listen(opts: Listen) { fn run_listen(opts: Listen) {
let listener = UnixListener::bind(&opts.socket).unwrap(); let listener = UnixListener::bind(&opts.socket).unwrap();
for stream in listener.incoming() { for stream in listener.incoming() {
@ -148,6 +149,7 @@ fn run_listen(opts: Listen) {
} }
} }
#[cfg(feature = "loader:kwasm")]
fn main() { fn main() {
let options = CLIOptions::from_args(); let options = CLIOptions::from_args();
match options { match options {
@ -156,3 +158,8 @@ fn main() {
} }
} }
} }
#[cfg(not(feature = "loader:kwasm"))]
fn main() {
panic!("Kwasm loader is not enabled during compilation.");
}

View File

@ -25,7 +25,7 @@ use wasmer_runtime::{
use wasmer_runtime_core::{ use wasmer_runtime_core::{
self, self,
backend::{Compiler, CompilerConfig, MemoryBoundCheckMode}, backend::{Compiler, CompilerConfig, MemoryBoundCheckMode},
loader::{self, Loader, Instance as LoadedInstance, LocalLoader}, loader::{Instance as LoadedInstance, LocalLoader},
}; };
#[cfg(feature = "backend:singlepass")] #[cfg(feature = "backend:singlepass")]
use wasmer_singlepass_backend::SinglePassCompiler; use wasmer_singlepass_backend::SinglePassCompiler;
@ -116,6 +116,7 @@ struct Run {
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
enum LoaderName { enum LoaderName {
Local, Local,
#[cfg(feature = "loader:kwasm")]
Kernel, Kernel,
} }
@ -123,6 +124,7 @@ impl LoaderName {
pub fn variants() -> &'static [&'static str] { pub fn variants() -> &'static [&'static str] {
&[ &[
"local", "local",
#[cfg(feature = "loader:kwasm")]
"kernel", "kernel",
] ]
} }
@ -133,6 +135,7 @@ impl FromStr for LoaderName {
fn from_str(s: &str) -> Result<LoaderName, String> { fn from_str(s: &str) -> Result<LoaderName, String> {
match s.to_lowercase().as_str() { match s.to_lowercase().as_str() {
"local" => Ok(LoaderName::Local), "local" => Ok(LoaderName::Local),
#[cfg(feature = "loader:kwasm")]
"kernel" => Ok(LoaderName::Kernel), "kernel" => Ok(LoaderName::Kernel),
_ => Err(format!("The loader {} doesn't exist", s)), _ => Err(format!("The loader {} doesn't exist", s)),
} }
@ -293,12 +296,16 @@ fn execute_wasm(options: &Run) -> Result<(), String> {
Backend::LLVM => return Err("the llvm backend is not enabled".to_string()), Backend::LLVM => return Err("the llvm backend is not enabled".to_string()),
}; };
#[cfg(feature = "loader:kwasm")]
let is_kernel_loader = if let Some(LoaderName::Kernel) = options.loader { let is_kernel_loader = if let Some(LoaderName::Kernel) = options.loader {
true true
} else { } else {
false false
}; };
#[cfg(not(feature = "loader:kwasm"))]
let is_kernel_loader = false;
let module = if is_kernel_loader { let module = if is_kernel_loader {
webassembly::compile_with_config_with( webassembly::compile_with_config_with(
&wasm_binary[..], &wasm_binary[..],
@ -377,6 +384,7 @@ fn execute_wasm(options: &Run) -> Result<(), String> {
let index = instance.resolve_func("_start").unwrap(); let index = instance.resolve_func("_start").unwrap();
let mut ins: Box<LoadedInstance<Error = String>> = match loader { let mut ins: Box<LoadedInstance<Error = String>> = match loader {
LoaderName::Local => Box::new(instance.load(LocalLoader).unwrap()), LoaderName::Local => Box::new(instance.load(LocalLoader).unwrap()),
#[cfg(feature = "loader:kwasm")]
LoaderName::Kernel => Box::new(instance.load(::kwasm_loader::KernelLoader).unwrap()), LoaderName::Kernel => Box::new(instance.load(::kwasm_loader::KernelLoader).unwrap()),
}; };
println!("{:?}", ins.call(index, &args)); println!("{:?}", ins.call(index, &args));