Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime-core) Share the definition of Trampoline across all the backends #915

Merged
merged 1 commit into from
Oct 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions lib/clif-backend/src/signal/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use crate::relocation::{TrapData, TrapSink};
use crate::resolver::FuncResolver;
use crate::trampoline::Trampolines;
use crate::{
relocation::{TrapData, TrapSink},
resolver::FuncResolver,
trampoline::Trampolines,
};
use libc::c_void;
use std::{any::Any, cell::Cell, ptr::NonNull, sync::Arc};
use wasmer_runtime_core::{
backend::RunnableModule,
module::ModuleInfo,
typed_func::{Wasm, WasmTrapInfo},
typed_func::{Trampoline, Wasm, WasmTrapInfo},
types::{LocalFuncIndex, SigIndex},
vm,
};
Expand Down Expand Up @@ -59,7 +61,7 @@ impl RunnableModule for Caller {

fn get_trampoline(&self, _: &ModuleInfo, sig_index: SigIndex) -> Option<Wasm> {
unsafe extern "C" fn invoke(
trampoline: unsafe extern "C" fn(*mut vm::Ctx, NonNull<vm::Func>, *const u64, *mut u64),
trampoline: Trampoline,
ctx: *mut vm::Ctx,
func: NonNull<vm::Func>,
args: *const u64,
Expand Down
44 changes: 25 additions & 19 deletions lib/clif-backend/src/signal/windows.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
use crate::relocation::{TrapCode, TrapData};
use crate::signal::{CallProtError, HandlerData};
use crate::trampoline::Trampoline;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is replaced by wasmer_runtime_core::typed_func::Trampoline.

use std::cell::Cell;
use std::ffi::c_void;
use std::ptr::{self, NonNull};
use wasmer_runtime_core::typed_func::WasmTrapInfo;
use wasmer_runtime_core::vm::Ctx;
use wasmer_runtime_core::vm::Func;
use crate::{
relocation::{TrapCode, TrapData},
signal::{CallProtError, HandlerData},
};
use std::{
cell::Cell,
ffi::c_void,
ptr::{self, NonNull},
};
use wasmer_runtime_core::{
typed_func::{Trampoline, WasmTrapInfo},
vm::{Ctx, Func},
};
use wasmer_win_exception_handler::CallProtectedData;
pub use wasmer_win_exception_handler::_call_protected;
use winapi::shared::minwindef::DWORD;
use winapi::um::minwinbase::{
EXCEPTION_ACCESS_VIOLATION, EXCEPTION_ARRAY_BOUNDS_EXCEEDED, EXCEPTION_BREAKPOINT,
EXCEPTION_DATATYPE_MISALIGNMENT, EXCEPTION_FLT_DENORMAL_OPERAND, EXCEPTION_FLT_DIVIDE_BY_ZERO,
EXCEPTION_FLT_INEXACT_RESULT, EXCEPTION_FLT_INVALID_OPERATION, EXCEPTION_FLT_OVERFLOW,
EXCEPTION_FLT_STACK_CHECK, EXCEPTION_FLT_UNDERFLOW, EXCEPTION_GUARD_PAGE,
EXCEPTION_ILLEGAL_INSTRUCTION, EXCEPTION_INT_DIVIDE_BY_ZERO, EXCEPTION_INT_OVERFLOW,
EXCEPTION_INVALID_HANDLE, EXCEPTION_IN_PAGE_ERROR, EXCEPTION_NONCONTINUABLE_EXCEPTION,
EXCEPTION_POSSIBLE_DEADLOCK, EXCEPTION_PRIV_INSTRUCTION, EXCEPTION_SINGLE_STEP,
EXCEPTION_STACK_OVERFLOW,
use winapi::{
shared::minwindef::DWORD,
um::minwinbase::{
EXCEPTION_ACCESS_VIOLATION, EXCEPTION_ARRAY_BOUNDS_EXCEEDED, EXCEPTION_BREAKPOINT,
EXCEPTION_DATATYPE_MISALIGNMENT, EXCEPTION_FLT_DENORMAL_OPERAND,
EXCEPTION_FLT_DIVIDE_BY_ZERO, EXCEPTION_FLT_INEXACT_RESULT,
EXCEPTION_FLT_INVALID_OPERATION, EXCEPTION_FLT_OVERFLOW, EXCEPTION_FLT_STACK_CHECK,
EXCEPTION_FLT_UNDERFLOW, EXCEPTION_GUARD_PAGE, EXCEPTION_ILLEGAL_INSTRUCTION,
EXCEPTION_INT_DIVIDE_BY_ZERO, EXCEPTION_INT_OVERFLOW, EXCEPTION_INVALID_HANDLE,
EXCEPTION_IN_PAGE_ERROR, EXCEPTION_NONCONTINUABLE_EXCEPTION, EXCEPTION_POSSIBLE_DEADLOCK,
EXCEPTION_PRIV_INSTRUCTION, EXCEPTION_SINGLE_STEP, EXCEPTION_STACK_OVERFLOW,
},
};

thread_local! {
Expand Down
10 changes: 3 additions & 7 deletions lib/clif-backend/src/trampoline.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
use crate::cache::TrampolineCache;
use crate::resolver::NoopStackmapSink;
use crate::{cache::TrampolineCache, resolver::NoopStackmapSink};
use cranelift_codegen::{
binemit::{NullTrapSink, Reloc, RelocSink},
cursor::{Cursor, FuncCursor},
ir::{self, InstBuilder},
isa, Context,
};
use std::collections::HashMap;
use std::{iter, mem, ptr::NonNull};
use std::{collections::HashMap, iter, mem};
use wasmer_runtime_core::{
backend::sys::{Memory, Protect},
module::{ExportIndex, ModuleInfo},
typed_func::Trampoline,
types::{FuncSig, SigIndex, Type},
vm,
};

struct NullRelocSink {}
Expand All @@ -28,8 +26,6 @@ impl RelocSink for NullRelocSink {
fn reloc_jt(&mut self, _: u32, _: Reloc, _: ir::JumpTable) {}
}

pub type Trampoline = unsafe extern "C" fn(*mut vm::Ctx, NonNull<vm::Func>, *const u64, *mut u64);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced by wasmer_runtime_core::typed_func::Trampoline.


pub struct Trampolines {
memory: Memory,
offsets: HashMap<SigIndex, usize>,
Expand Down
17 changes: 7 additions & 10 deletions lib/llvm-backend/src/backend.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use super::stackmap::StackmapRegistry;
use crate::intrinsics::Intrinsics;
use crate::structs::{Callbacks, LLVMModule, LLVMResult, MemProtect};
use crate::{
intrinsics::Intrinsics,
structs::{Callbacks, LLVMModule, LLVMResult, MemProtect},
};
use inkwell::{
memory_buffer::MemoryBuffer,
module::Module,
Expand All @@ -27,7 +29,7 @@ use wasmer_runtime_core::{
module::ModuleInfo,
state::ModuleStateMap,
structures::TypedIndex,
typed_func::{Wasm, WasmTrapInfo},
typed_func::{Trampoline, Wasm, WasmTrapInfo},
types::{LocalFuncIndex, SigIndex},
vm, vmcalls,
};
Expand Down Expand Up @@ -57,7 +59,7 @@ extern "C" {

#[allow(improper_ctypes)]
fn invoke_trampoline(
trampoline: unsafe extern "C" fn(*mut vm::Ctx, NonNull<vm::Func>, *const u64, *mut u64),
trampoline: Trampoline,
vmctx_ptr: *mut vm::Ctx,
func_ptr: NonNull<vm::Func>,
params: *const u64,
Expand Down Expand Up @@ -387,12 +389,7 @@ impl RunnableModule for LLVMBackend {
}

fn get_trampoline(&self, _: &ModuleInfo, sig_index: SigIndex) -> Option<Wasm> {
let trampoline: unsafe extern "C" fn(
*mut vm::Ctx,
NonNull<vm::Func>,
*const u64,
*mut u64,
) = unsafe {
let trampoline: Trampoline = unsafe {
let name = if cfg!(target_os = "macos") {
format!("_trmp{}", sig_index.index())
} else {
Expand Down
23 changes: 13 additions & 10 deletions lib/llvm-backend/src/code.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
use crate::{
backend::LLVMBackend,
intrinsics::{CtxType, GlobalCache, Intrinsics, MemoryCache},
read_info::{blocktype_to_type, type_to_type},
stackmap::{StackmapEntry, StackmapEntryKind, StackmapRegistry, ValueSemantic},
state::{ControlFrame, ExtraInfo, IfElseState, State},
trampolines::generate_trampolines,
};
use inkwell::{
builder::Builder,
context::Context,
Expand All @@ -12,9 +20,11 @@ use inkwell::{
AddressSpace, AtomicOrdering, AtomicRMWBinOp, FloatPredicate, IntPredicate, OptimizationLevel,
};
use smallvec::SmallVec;
use std::cell::RefCell;
use std::rc::Rc;
use std::sync::{Arc, RwLock};
use std::{
cell::RefCell,
rc::Rc,
sync::{Arc, RwLock},
};
use wasmer_runtime_core::{
backend::{Backend, CacheGen, CompilerConfig, Token},
cache::{Artifact, Error as CacheError},
Expand All @@ -28,13 +38,6 @@ use wasmer_runtime_core::{
};
use wasmparser::{BinaryReaderError, MemoryImmediate, Operator, Type as WpType};

use crate::backend::LLVMBackend;
use crate::intrinsics::{CtxType, GlobalCache, Intrinsics, MemoryCache};
use crate::read_info::{blocktype_to_type, type_to_type};
use crate::stackmap::{StackmapEntry, StackmapEntryKind, StackmapRegistry, ValueSemantic};
use crate::state::{ControlFrame, ExtraInfo, IfElseState, State};
use crate::trampolines::generate_trampolines;

fn func_sig_to_llvm(
context: &Context,
intrinsics: &Intrinsics,
Expand Down
23 changes: 14 additions & 9 deletions lib/runtime-core/src/typed_func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,21 @@ impl fmt::Display for WasmTrapInfo {
/// of the `Func` struct.
pub trait Kind {}

pub type Trampoline = unsafe extern "C" fn(*mut Ctx, NonNull<vm::Func>, *const u64, *mut u64);
pub type Trampoline = unsafe extern "C" fn(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing has changed here, except arguments are now labelled.

vmctx: *mut Ctx,
func: NonNull<vm::Func>,
args: *const u64,
rets: *mut u64,
);
pub type Invoke = unsafe extern "C" fn(
Trampoline,
*mut Ctx,
NonNull<vm::Func>,
*const u64,
*mut u64,
*mut WasmTrapInfo,
*mut Option<Box<dyn Any>>,
Option<NonNull<c_void>>,
trampoline: Trampoline,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above: Nothing has changed except arguments are now labelled.

vmctx: *mut Ctx,
func: NonNull<vm::Func>,
args: *const u64,
rets: *mut u64,
trap_info: *mut WasmTrapInfo,
user_error: *mut Option<Box<dyn Any>>,
extra: Option<NonNull<c_void>>,
) -> bool;

/// TODO(lachlan): Naming TBD.
Expand Down
42 changes: 18 additions & 24 deletions lib/singlepass-backend/src/codegen_x64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ use smallvec::SmallVec;
use std::{
any::Any,
collections::{BTreeMap, HashMap},
ffi::c_void,
iter, mem,
ptr::NonNull,
slice,
sync::{Arc, RwLock},
usize,
};
use wasmer_runtime_core::{
backend::{
Expand All @@ -25,7 +29,7 @@ use wasmer_runtime_core::{
ModuleStateMap, OffsetInfo, SuspendOffset, WasmAbstractValue,
},
structures::{Map, TypedIndex},
typed_func::Wasm,
typed_func::{Trampoline, Wasm, WasmTrapInfo},
types::{
FuncIndex, FuncSig, GlobalIndex, LocalFuncIndex, LocalOrImport, MemoryIndex, SigIndex,
TableIndex, Type,
Expand Down Expand Up @@ -116,8 +120,8 @@ lazy_static! {
; ret
);
let buf = assembler.finalize().unwrap();
let ret = unsafe { ::std::mem::transmute(buf.ptr(offset)) };
::std::mem::forget(buf);
let ret = unsafe { mem::transmute(buf.ptr(offset)) };
mem::forget(buf);
ret
};
}
Expand Down Expand Up @@ -225,7 +229,7 @@ impl RunnableModule for X64ExecutionContext {
14: 41 ff e3 jmpq *%r11
*/
#[repr(packed)]
struct Trampoline {
struct LocalTrampoline {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This structure has been renamed to avoid naming clashing with wasmer_runtime_core::typed_func::Trampoline.

movabsq_rax: [u8; 2],
addr_rax: u64,
movabsq_r11: [u8; 2],
Expand All @@ -236,7 +240,7 @@ impl RunnableModule for X64ExecutionContext {
self.code.make_writable();

let trampoline = &mut *(self.function_pointers[self.func_import_count + idx].0
as *const Trampoline as *mut Trampoline);
as *const LocalTrampoline as *mut LocalTrampoline);
trampoline.movabsq_rax[0] = 0x48;
trampoline.movabsq_rax[1] = 0xb8;
trampoline.addr_rax = target_address as u64;
Expand All @@ -253,16 +257,8 @@ impl RunnableModule for X64ExecutionContext {
}

fn get_trampoline(&self, _: &ModuleInfo, sig_index: SigIndex) -> Option<Wasm> {
use std::ffi::c_void;
use wasmer_runtime_core::typed_func::WasmTrapInfo;

unsafe extern "C" fn invoke(
_trampoline: unsafe extern "C" fn(
*mut vm::Ctx,
NonNull<vm::Func>,
*const u64,
*mut u64,
),
_trampoline: Trampoline,
ctx: *mut vm::Ctx,
func: NonNull<vm::Func>,
args: *const u64,
Expand All @@ -273,12 +269,10 @@ impl RunnableModule for X64ExecutionContext {
) -> bool {
let rm: &Box<dyn RunnableModule> = &(&*(*ctx).module).runnable_module;
let execution_context =
::std::mem::transmute_copy::<&dyn RunnableModule, &X64ExecutionContext>(&&**rm);
mem::transmute_copy::<&dyn RunnableModule, &X64ExecutionContext>(&&**rm);

let args = std::slice::from_raw_parts(
args,
num_params_plus_one.unwrap().as_ptr() as usize - 1,
);
let args =
slice::from_raw_parts(args, num_params_plus_one.unwrap().as_ptr() as usize - 1);
let args_reverse: SmallVec<[u64; 8]> = args.iter().cloned().rev().collect();
let ret = match protect_unix::call_protected(
|| {
Expand Down Expand Up @@ -1735,7 +1729,7 @@ impl X64FunctionCode {
control_stack: &mut [ControlFrame],
) -> usize {
if !m.track_state {
return ::std::usize::MAX;
return usize::MAX;
}
let last_frame = control_stack.last_mut().unwrap();
let mut diff = m.state.diff(&last_frame.state);
Expand Down Expand Up @@ -1851,7 +1845,7 @@ impl FunctionCodeGenerator<CodegenError> for X64FunctionCode {
Location::GPR(GPR::RAX),
);

assert_eq!(self.machine.state.wasm_inst_offset, ::std::usize::MAX);
assert_eq!(self.machine.state.wasm_inst_offset, usize::MAX);

Ok(())
}
Expand Down Expand Up @@ -4721,7 +4715,7 @@ impl FunctionCodeGenerator<CodegenError> for X64FunctionCode {
|a| {
a.emit_call_location(Location::GPR(GPR::RAX));
},
::std::iter::once(Location::Imm32(memory_index.index() as u32)),
iter::once(Location::Imm32(memory_index.index() as u32)),
None,
);
let ret = self.machine.acquire_locations(
Expand Down Expand Up @@ -4760,8 +4754,8 @@ impl FunctionCodeGenerator<CodegenError> for X64FunctionCode {
|a| {
a.emit_call_location(Location::GPR(GPR::RAX));
},
::std::iter::once(Location::Imm32(memory_index.index() as u32))
.chain(::std::iter::once(param_pages)),
iter::once(Location::Imm32(memory_index.index() as u32))
.chain(iter::once(param_pages)),
None,
);

Expand Down
6 changes: 4 additions & 2 deletions lib/win-exception-handler/src/exception_handling.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::ptr::NonNull;
use wasmer_runtime_core::vm::{Ctx, Func};
use wasmer_runtime_core::{
typed_func::Trampoline,
vm::{Ctx, Func},
};

type Trampoline = unsafe extern "C" fn(*mut Ctx, NonNull<Func>, *const u64, *mut u64);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced by wasmer_runtime_core::typed_func::Trampoline.

type CallProtectedResult = Result<(), CallProtectedData>;

#[repr(C)]
Expand Down