diff --git a/lib/runtime-core/src/fault.rs b/lib/runtime-core/src/fault.rs index 797958556..3cb6e7531 100644 --- a/lib/runtime-core/src/fault.rs +++ b/lib/runtime-core/src/fault.rs @@ -9,8 +9,8 @@ mod raw { } use crate::codegen::{BreakpointInfo, BreakpointMap}; -use crate::state::CodeVersion; use crate::state::x64::{build_instance_image, read_stack, X64Register, GPR, XMM}; +use crate::state::CodeVersion; use crate::vm; use libc::{mmap, mprotect, siginfo_t, MAP_ANON, MAP_PRIVATE, PROT_NONE, PROT_READ, PROT_WRITE}; use nix::sys::signal::{ @@ -18,7 +18,7 @@ use nix::sys::signal::{ SIGSEGV, SIGTRAP, }; use std::any::Any; -use std::cell::{Cell, UnsafeCell, RefCell}; +use std::cell::{Cell, RefCell, UnsafeCell}; use std::ffi::c_void; use std::process; use std::sync::atomic::{AtomicBool, Ordering}; diff --git a/lib/runtime-core/src/state.rs b/lib/runtime-core/src/state.rs index d099768dd..42785d294 100644 --- a/lib/runtime-core/src/state.rs +++ b/lib/runtime-core/src/state.rs @@ -764,16 +764,21 @@ pub mod x64 { let mut fsm_state: Option<(&FunctionStateMap, MachineState)> = None; for version in versions() { - match version.msm + match version + .msm .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)) { Some(x) => { fsm_state = Some(x); break; - }, - None => {}, + } + None => {} }; } diff --git a/lib/runtime-core/src/tiering.rs b/lib/runtime-core/src/tiering.rs index 54472e90c..75688bab4 100644 --- a/lib/runtime-core/src/tiering.rs +++ b/lib/runtime-core/src/tiering.rs @@ -1,19 +1,17 @@ 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::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::import::ImportObject; +use crate::instance::Instance; 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::sync::{Arc, Mutex}; struct Defer(Option); impl Drop for Defer { @@ -84,8 +82,7 @@ pub fn run_tiering ShellExitOperation>( unsafe { ensure_sighandler(); - let ctx_box = - Arc::new(Mutex::new(CtxWrapper(baseline.context_mut() as *mut _))); + let ctx_box = Arc::new(Mutex::new(CtxWrapper(baseline.context_mut() as *mut _))); // Ensure that the ctx pointer's lifetime is not longer than Instance's. let _deferred_ctx_box_cleanup: Defer<_> = { let ctx_box = ctx_box.clone(); @@ -104,12 +101,7 @@ pub fn run_tiering ShellExitOperation>( ::std::thread::spawn(move || { for backend in optimized_backends { if !ctx_box.lock().unwrap().0.is_null() { - do_optimize( - &wasm_binary, - backend(), - &ctx_box, - &opt_state, - ); + do_optimize(&wasm_binary, backend(), &ctx_box, &opt_state); } } }); @@ -118,7 +110,11 @@ pub fn run_tiering ShellExitOperation>( let mut optimized_instances: Vec = vec![]; 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, }); let n_versions: Cell = Cell::new(1); @@ -133,10 +129,10 @@ pub fn run_tiering ShellExitOperation>( let new_optimized: Option<&mut Instance> = { let mut outcome = opt_state.outcome.lock().unwrap(); if let Some(x) = outcome.take() { - let instance = - x.module.instantiate(&import_object).map_err(|e| { - format!("Can't instantiate module: {:?}", e) - })?; + let instance = x + .module + .instantiate(&import_object) + .map_err(|e| format!("Can't instantiate module: {:?}", e))?; // Keep the optimized code alive. optimized_instances.push(instance); optimized_instances.last_mut() @@ -151,8 +147,7 @@ pub fn run_tiering ShellExitOperation>( .runnable_module .get_code() .unwrap() - .as_ptr() - as usize; + .as_ptr() as usize; let target_addresses: Vec = optimized .module .runnable_module @@ -161,10 +156,7 @@ pub fn run_tiering ShellExitOperation>( .into_iter() .map(|x| code_ptr + x) .collect(); - assert_eq!( - target_addresses.len(), - module_info.func_assoc.len() - base - ); + assert_eq!(target_addresses.len(), module_info.func_assoc.len() - base); for i in base..module_info.func_assoc.len() { baseline .module @@ -173,8 +165,17 @@ pub fn run_tiering ShellExitOperation>( } push_code_version(CodeVersion { - msm: optimized.module.runnable_module.get_module_state_map().unwrap(), - base: optimized.module.runnable_module.get_code().unwrap().as_ptr() as usize, + msm: optimized + .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); @@ -191,8 +192,7 @@ pub fn run_tiering ShellExitOperation>( .get_module_state_map() .unwrap(); let code_base = - baseline.module.runnable_module.get_code().unwrap().as_ptr() - as usize; + baseline.module.runnable_module.get_code().unwrap().as_ptr() as usize; invoke_call_return_on_stack( &msm, code_base, @@ -202,17 +202,13 @@ pub fn run_tiering ShellExitOperation>( ) .map(|_| ()) } else { - catch_unsafe_unwind( - || start_raw(baseline.context_mut()), - breakpoints.clone(), - ) + catch_unsafe_unwind(|| start_raw(baseline.context_mut()), breakpoints.clone()) } }); if let Err(e) = ret { if let Some(new_image) = e.downcast_ref::() { // Tier switch event - if !was_sigint_triggered_fault() - && opt_state.outcome.lock().unwrap().is_some() + if !was_sigint_triggered_fault() && opt_state.outcome.lock().unwrap().is_some() { resume_image = Some(new_image.clone()); continue; diff --git a/src/bin/wasmer.rs b/src/bin/wasmer.rs index 0d561ad63..07bf68d5c 100644 --- a/src/bin/wasmer.rs +++ b/src/bin/wasmer.rs @@ -21,6 +21,8 @@ use wasmer_runtime::{ cache::{Cache as BaseCache, FileSystemCache, WasmHash, WASMER_VERSION_HASH}, Func, Value, }; +#[cfg(feature = "managed")] +use wasmer_runtime_core::tiering::{run_tiering, InteractiveShellContext, ShellExitOperation}; use wasmer_runtime_core::{ self, backend::{Backend, Compiler, CompilerConfig, MemoryBoundCheckMode}, @@ -31,8 +33,6 @@ use wasmer_runtime_core::{ use wasmer_singlepass_backend::SinglePassCompiler; #[cfg(feature = "wasi")] use wasmer_wasi; -#[cfg(feature = "managed")] -use wasmer_runtime_core::tiering::{InteractiveShellContext, ShellExitOperation, run_tiering}; // stub module to make conditional compilation happy #[cfg(not(feature = "wasi"))] @@ -515,9 +515,8 @@ fn execute_wasm(options: &Run) -> Result<(), String> { .map_err(|e| format!("Can't instantiate module: {:?}", 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 { - ::std::mem::transmute(start.get_vm_func()) - }; + let start_raw: extern "C" fn(&mut wasmer_runtime_core::vm::Ctx) = + unsafe { ::std::mem::transmute(start.get_vm_func()) }; #[cfg(feature = "managed")] run_tiering( @@ -527,16 +526,23 @@ fn execute_wasm(options: &Run) -> Result<(), String> { let mut f = File::open(path).unwrap(); let mut out: Vec = vec![]; 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 { None }, &import_object, start_raw, &mut instance, - options.optimized_backends.iter().map(|&backend| -> Box Box + Send> { - Box::new(move || get_compiler_by_backend(backend).unwrap()) - }).collect(), + options + .optimized_backends + .iter() + .map(|&backend| -> Box Box + Send> { + Box::new(move || get_compiler_by_backend(backend).unwrap()) + }) + .collect(), interactive_shell, )?;