Cargo fmt

This commit is contained in:
losfair 2019-08-10 02:44:44 +08:00
parent 03665fe74a
commit 2e89f02191
4 changed files with 61 additions and 54 deletions

View File

@ -9,8 +9,8 @@ mod raw {
} }
use crate::codegen::{BreakpointInfo, BreakpointMap}; use crate::codegen::{BreakpointInfo, BreakpointMap};
use crate::state::CodeVersion;
use crate::state::x64::{build_instance_image, read_stack, X64Register, GPR, XMM}; use crate::state::x64::{build_instance_image, read_stack, X64Register, GPR, XMM};
use crate::state::CodeVersion;
use crate::vm; use crate::vm;
use libc::{mmap, mprotect, siginfo_t, MAP_ANON, MAP_PRIVATE, PROT_NONE, PROT_READ, PROT_WRITE}; use libc::{mmap, mprotect, siginfo_t, MAP_ANON, MAP_PRIVATE, PROT_NONE, PROT_READ, PROT_WRITE};
use nix::sys::signal::{ use nix::sys::signal::{
@ -18,7 +18,7 @@ use nix::sys::signal::{
SIGSEGV, SIGTRAP, SIGSEGV, SIGTRAP,
}; };
use std::any::Any; use std::any::Any;
use std::cell::{Cell, UnsafeCell, RefCell}; use std::cell::{Cell, RefCell, UnsafeCell};
use std::ffi::c_void; use std::ffi::c_void;
use std::process; use std::process;
use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::atomic::{AtomicBool, Ordering};

View File

@ -764,16 +764,21 @@ pub mod x64 {
let mut fsm_state: Option<(&FunctionStateMap, MachineState)> = None; let mut fsm_state: Option<(&FunctionStateMap, MachineState)> = None;
for version in versions() { for version in versions() {
match version.msm match version
.msm
.lookup_call_ip(ret_addr as usize, version.base) .lookup_call_ip(ret_addr as usize, version.base)
.or_else(|| version.msm.lookup_trappable_ip(ret_addr as usize, version.base)) .or_else(|| {
version
.msm
.lookup_trappable_ip(ret_addr as usize, version.base)
})
.or_else(|| version.msm.lookup_loop_ip(ret_addr as usize, version.base)) .or_else(|| version.msm.lookup_loop_ip(ret_addr as usize, version.base))
{ {
Some(x) => { Some(x) => {
fsm_state = Some(x); fsm_state = Some(x);
break; break;
}, }
None => {}, None => {}
}; };
} }

View File

@ -1,19 +1,17 @@
use crate::backend::{Compiler, CompilerConfig}; use crate::backend::{Compiler, CompilerConfig};
use crate::import::ImportObject;
use crate::fault::{
catch_unsafe_unwind, ensure_sighandler, with_ctx, push_code_version, pop_code_version,
};
use crate::state::{
x64::invoke_call_return_on_stack, InstanceImage, CodeVersion
};
use crate::vm::Ctx;
use crate::compile_with_config; use crate::compile_with_config;
use crate::instance::Instance; use crate::fault::{
catch_unsafe_unwind, ensure_sighandler, pop_code_version, push_code_version, with_ctx,
};
use crate::fault::{set_wasm_interrupt_on_ctx, was_sigint_triggered_fault}; use crate::fault::{set_wasm_interrupt_on_ctx, was_sigint_triggered_fault};
use crate::import::ImportObject;
use crate::instance::Instance;
use crate::module::{Module, ModuleInfo}; use crate::module::{Module, ModuleInfo};
use crate::state::{x64::invoke_call_return_on_stack, CodeVersion, InstanceImage};
use crate::vm::Ctx;
use std::sync::{Arc, Mutex};
use std::cell::Cell; use std::cell::Cell;
use std::sync::{Arc, Mutex};
struct Defer<F: FnOnce()>(Option<F>); struct Defer<F: FnOnce()>(Option<F>);
impl<F: FnOnce()> Drop for Defer<F> { impl<F: FnOnce()> Drop for Defer<F> {
@ -84,8 +82,7 @@ pub fn run_tiering<F: Fn(InteractiveShellContext) -> ShellExitOperation>(
unsafe { unsafe {
ensure_sighandler(); ensure_sighandler();
let ctx_box = let ctx_box = Arc::new(Mutex::new(CtxWrapper(baseline.context_mut() as *mut _)));
Arc::new(Mutex::new(CtxWrapper(baseline.context_mut() as *mut _)));
// Ensure that the ctx pointer's lifetime is not longer than Instance's. // Ensure that the ctx pointer's lifetime is not longer than Instance's.
let _deferred_ctx_box_cleanup: Defer<_> = { let _deferred_ctx_box_cleanup: Defer<_> = {
let ctx_box = ctx_box.clone(); let ctx_box = ctx_box.clone();
@ -104,12 +101,7 @@ pub fn run_tiering<F: Fn(InteractiveShellContext) -> ShellExitOperation>(
::std::thread::spawn(move || { ::std::thread::spawn(move || {
for backend in optimized_backends { for backend in optimized_backends {
if !ctx_box.lock().unwrap().0.is_null() { if !ctx_box.lock().unwrap().0.is_null() {
do_optimize( do_optimize(&wasm_binary, backend(), &ctx_box, &opt_state);
&wasm_binary,
backend(),
&ctx_box,
&opt_state,
);
} }
} }
}); });
@ -118,7 +110,11 @@ pub fn run_tiering<F: Fn(InteractiveShellContext) -> ShellExitOperation>(
let mut optimized_instances: Vec<Instance> = vec![]; let mut optimized_instances: Vec<Instance> = vec![];
push_code_version(CodeVersion { push_code_version(CodeVersion {
msm: baseline.module.runnable_module.get_module_state_map().unwrap(), msm: baseline
.module
.runnable_module
.get_module_state_map()
.unwrap(),
base: baseline.module.runnable_module.get_code().unwrap().as_ptr() as usize, base: baseline.module.runnable_module.get_code().unwrap().as_ptr() as usize,
}); });
let n_versions: Cell<usize> = Cell::new(1); let n_versions: Cell<usize> = Cell::new(1);
@ -133,10 +129,10 @@ pub fn run_tiering<F: Fn(InteractiveShellContext) -> ShellExitOperation>(
let new_optimized: Option<&mut Instance> = { let new_optimized: Option<&mut Instance> = {
let mut outcome = opt_state.outcome.lock().unwrap(); let mut outcome = opt_state.outcome.lock().unwrap();
if let Some(x) = outcome.take() { if let Some(x) = outcome.take() {
let instance = let instance = x
x.module.instantiate(&import_object).map_err(|e| { .module
format!("Can't instantiate module: {:?}", e) .instantiate(&import_object)
})?; .map_err(|e| format!("Can't instantiate module: {:?}", e))?;
// Keep the optimized code alive. // Keep the optimized code alive.
optimized_instances.push(instance); optimized_instances.push(instance);
optimized_instances.last_mut() optimized_instances.last_mut()
@ -151,8 +147,7 @@ pub fn run_tiering<F: Fn(InteractiveShellContext) -> ShellExitOperation>(
.runnable_module .runnable_module
.get_code() .get_code()
.unwrap() .unwrap()
.as_ptr() .as_ptr() as usize;
as usize;
let target_addresses: Vec<usize> = optimized let target_addresses: Vec<usize> = optimized
.module .module
.runnable_module .runnable_module
@ -161,10 +156,7 @@ pub fn run_tiering<F: Fn(InteractiveShellContext) -> ShellExitOperation>(
.into_iter() .into_iter()
.map(|x| code_ptr + x) .map(|x| code_ptr + x)
.collect(); .collect();
assert_eq!( assert_eq!(target_addresses.len(), module_info.func_assoc.len() - base);
target_addresses.len(),
module_info.func_assoc.len() - base
);
for i in base..module_info.func_assoc.len() { for i in base..module_info.func_assoc.len() {
baseline baseline
.module .module
@ -173,8 +165,17 @@ pub fn run_tiering<F: Fn(InteractiveShellContext) -> ShellExitOperation>(
} }
push_code_version(CodeVersion { push_code_version(CodeVersion {
msm: optimized.module.runnable_module.get_module_state_map().unwrap(), msm: optimized
base: optimized.module.runnable_module.get_code().unwrap().as_ptr() as usize, .module
.runnable_module
.get_module_state_map()
.unwrap(),
base: optimized
.module
.runnable_module
.get_code()
.unwrap()
.as_ptr() as usize,
}); });
n_versions.set(n_versions.get() + 1); n_versions.set(n_versions.get() + 1);
@ -191,8 +192,7 @@ pub fn run_tiering<F: Fn(InteractiveShellContext) -> ShellExitOperation>(
.get_module_state_map() .get_module_state_map()
.unwrap(); .unwrap();
let code_base = let code_base =
baseline.module.runnable_module.get_code().unwrap().as_ptr() baseline.module.runnable_module.get_code().unwrap().as_ptr() as usize;
as usize;
invoke_call_return_on_stack( invoke_call_return_on_stack(
&msm, &msm,
code_base, code_base,
@ -202,17 +202,13 @@ pub fn run_tiering<F: Fn(InteractiveShellContext) -> ShellExitOperation>(
) )
.map(|_| ()) .map(|_| ())
} else { } else {
catch_unsafe_unwind( catch_unsafe_unwind(|| start_raw(baseline.context_mut()), breakpoints.clone())
|| start_raw(baseline.context_mut()),
breakpoints.clone(),
)
} }
}); });
if let Err(e) = ret { if let Err(e) = ret {
if let Some(new_image) = e.downcast_ref::<InstanceImage>() { if let Some(new_image) = e.downcast_ref::<InstanceImage>() {
// Tier switch event // Tier switch event
if !was_sigint_triggered_fault() if !was_sigint_triggered_fault() && opt_state.outcome.lock().unwrap().is_some()
&& opt_state.outcome.lock().unwrap().is_some()
{ {
resume_image = Some(new_image.clone()); resume_image = Some(new_image.clone());
continue; continue;

View File

@ -21,6 +21,8 @@ use wasmer_runtime::{
cache::{Cache as BaseCache, FileSystemCache, WasmHash, WASMER_VERSION_HASH}, cache::{Cache as BaseCache, FileSystemCache, WasmHash, WASMER_VERSION_HASH},
Func, Value, Func, Value,
}; };
#[cfg(feature = "managed")]
use wasmer_runtime_core::tiering::{run_tiering, InteractiveShellContext, ShellExitOperation};
use wasmer_runtime_core::{ use wasmer_runtime_core::{
self, self,
backend::{Backend, Compiler, CompilerConfig, MemoryBoundCheckMode}, backend::{Backend, Compiler, CompilerConfig, MemoryBoundCheckMode},
@ -31,8 +33,6 @@ use wasmer_runtime_core::{
use wasmer_singlepass_backend::SinglePassCompiler; use wasmer_singlepass_backend::SinglePassCompiler;
#[cfg(feature = "wasi")] #[cfg(feature = "wasi")]
use wasmer_wasi; use wasmer_wasi;
#[cfg(feature = "managed")]
use wasmer_runtime_core::tiering::{InteractiveShellContext, ShellExitOperation, run_tiering};
// stub module to make conditional compilation happy // stub module to make conditional compilation happy
#[cfg(not(feature = "wasi"))] #[cfg(not(feature = "wasi"))]
@ -515,9 +515,8 @@ fn execute_wasm(options: &Run) -> Result<(), String> {
.map_err(|e| format!("Can't instantiate module: {:?}", e))?; .map_err(|e| format!("Can't instantiate module: {:?}", e))?;
let start: Func<(), ()> = instance.func("_start").map_err(|e| format!("{:?}", e))?; let start: Func<(), ()> = instance.func("_start").map_err(|e| format!("{:?}", e))?;
let start_raw: extern "C" fn(&mut wasmer_runtime_core::vm::Ctx) = unsafe { let start_raw: extern "C" fn(&mut wasmer_runtime_core::vm::Ctx) =
::std::mem::transmute(start.get_vm_func()) unsafe { ::std::mem::transmute(start.get_vm_func()) };
};
#[cfg(feature = "managed")] #[cfg(feature = "managed")]
run_tiering( run_tiering(
@ -527,16 +526,23 @@ fn execute_wasm(options: &Run) -> Result<(), String> {
let mut f = File::open(path).unwrap(); let mut f = File::open(path).unwrap();
let mut out: Vec<u8> = vec![]; let mut out: Vec<u8> = vec![];
f.read_to_end(&mut out).unwrap(); f.read_to_end(&mut out).unwrap();
Some(wasmer_runtime_core::state::InstanceImage::from_bytes(&out).expect("failed to decode image")) Some(
wasmer_runtime_core::state::InstanceImage::from_bytes(&out)
.expect("failed to decode image"),
)
} else { } else {
None None
}, },
&import_object, &import_object,
start_raw, start_raw,
&mut instance, &mut instance,
options.optimized_backends.iter().map(|&backend| -> Box<dyn Fn() -> Box<dyn Compiler> + Send> { options
Box::new(move || get_compiler_by_backend(backend).unwrap()) .optimized_backends
}).collect(), .iter()
.map(|&backend| -> Box<dyn Fn() -> Box<dyn Compiler> + Send> {
Box::new(move || get_compiler_by_backend(backend).unwrap())
})
.collect(),
interactive_shell, interactive_shell,
)?; )?;