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-llvm-backend = { path = "lib/llvm-backend", optional = true }
wasmer-wasi = { path = "lib/wasi", optional = true }
kwasm-loader = { path = "lib/kwasm-loader" }
kwasm-loader = { path = "lib/kwasm-loader", optional = true }
[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"]
@ -45,6 +45,7 @@ rustc_version = "0.2.3"
[features]
default = ["fast-tests", "wasi"]
"loader:kwasm" = ["kwasm-loader"]
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

View File

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

View File

@ -5,13 +5,12 @@ use std::thread;
use structopt::StructOpt;
use wasmer::*;
use wasmer_runtime::{
error::RuntimeError,
Func, Value,
Value,
};
use wasmer_runtime_core::{
self,
backend::{Compiler, CompilerConfig, MemoryBoundCheckMode},
loader::{self, Loader, Instance as LoadedInstance, LocalLoader},
backend::{CompilerConfig, MemoryBoundCheckMode},
loader::{Instance as LoadedInstance},
};
use wasmer_singlepass_backend::SinglePassCompiler;
@ -37,6 +36,7 @@ const CMD_RUN_CODE: u32 = 0x901;
const CMD_READ_MEMORY: u32 = 0x902;
const CMD_WRITE_MEMORY: u32 = 0x903;
#[cfg(feature = "loader:kwasm")]
fn handle_client(mut stream: UnixStream) {
let binary_size = stream.read_u32::<LittleEndian>().unwrap();
if binary_size > 1048576 * 16 {
@ -127,6 +127,7 @@ fn handle_client(mut stream: UnixStream) {
}
}
#[cfg(feature = "loader:kwasm")]
fn run_listen(opts: Listen) {
let listener = UnixListener::bind(&opts.socket).unwrap();
for stream in listener.incoming() {
@ -148,6 +149,7 @@ fn run_listen(opts: Listen) {
}
}
#[cfg(feature = "loader:kwasm")]
fn main() {
let options = CLIOptions::from_args();
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::{
self,
backend::{Compiler, CompilerConfig, MemoryBoundCheckMode},
loader::{self, Loader, Instance as LoadedInstance, LocalLoader},
loader::{Instance as LoadedInstance, LocalLoader},
};
#[cfg(feature = "backend:singlepass")]
use wasmer_singlepass_backend::SinglePassCompiler;
@ -116,6 +116,7 @@ struct Run {
#[derive(Debug, Copy, Clone)]
enum LoaderName {
Local,
#[cfg(feature = "loader:kwasm")]
Kernel,
}
@ -123,6 +124,7 @@ impl LoaderName {
pub fn variants() -> &'static [&'static str] {
&[
"local",
#[cfg(feature = "loader:kwasm")]
"kernel",
]
}
@ -133,6 +135,7 @@ impl FromStr for LoaderName {
fn from_str(s: &str) -> Result<LoaderName, String> {
match s.to_lowercase().as_str() {
"local" => Ok(LoaderName::Local),
#[cfg(feature = "loader:kwasm")]
"kernel" => Ok(LoaderName::Kernel),
_ => 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()),
};
#[cfg(feature = "loader:kwasm")]
let is_kernel_loader = if let Some(LoaderName::Kernel) = options.loader {
true
} else {
false
};
#[cfg(not(feature = "loader:kwasm"))]
let is_kernel_loader = false;
let module = if is_kernel_loader {
webassembly::compile_with_config_with(
&wasm_binary[..],
@ -377,6 +384,7 @@ fn execute_wasm(options: &Run) -> Result<(), String> {
let index = instance.resolve_func("_start").unwrap();
let mut ins: Box<LoadedInstance<Error = String>> = match loader {
LoaderName::Local => Box::new(instance.load(LocalLoader).unwrap()),
#[cfg(feature = "loader:kwasm")]
LoaderName::Kernel => Box::new(instance.load(::kwasm_loader::KernelLoader).unwrap()),
};
println!("{:?}", ins.call(index, &args));