2019-01-08 21:04:03 +00:00
|
|
|
//! The relocation package provide two structures: RelocSink, TrapSink.
|
|
|
|
//! This structures are used by Cranelift when compiling functions to mark
|
|
|
|
//! any other calls that this function is doing, so we can "patch" the
|
|
|
|
//! function addrs in runtime with the functions we need.
|
|
|
|
use cranelift_codegen::binemit;
|
2019-02-07 00:26:45 +00:00
|
|
|
use cranelift_codegen::ir::{self, ExternalName, SourceLoc};
|
|
|
|
use wasmer_runtime_core::{
|
|
|
|
structures::TypedIndex,
|
|
|
|
types::{FuncIndex, SigIndex},
|
|
|
|
};
|
2019-01-08 21:04:03 +00:00
|
|
|
|
2019-01-25 23:28:54 +00:00
|
|
|
pub mod call_names {
|
|
|
|
pub const LOCAL_NAMESPACE: u32 = 1;
|
|
|
|
pub const IMPORT_NAMESPACE: u32 = 2;
|
2019-02-07 00:26:45 +00:00
|
|
|
pub const SIG_NAMESPACE: u32 = 3;
|
2019-01-25 23:28:54 +00:00
|
|
|
|
|
|
|
pub const STATIC_MEM_GROW: u32 = 0;
|
|
|
|
pub const STATIC_MEM_SIZE: u32 = 1;
|
|
|
|
pub const SHARED_STATIC_MEM_GROW: u32 = 2;
|
|
|
|
pub const SHARED_STATIC_MEM_SIZE: u32 = 3;
|
|
|
|
pub const DYNAMIC_MEM_GROW: u32 = 4;
|
|
|
|
pub const DYNAMIC_MEM_SIZE: u32 = 5;
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:26:45 +00:00
|
|
|
#[cfg_attr(feature = "cache", derive(Serialize, Deserialize))]
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
|
|
|
pub enum Reloc {
|
|
|
|
Abs8,
|
|
|
|
X86PCRel4,
|
|
|
|
X86CallPCRel4,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(feature = "cache", derive(Serialize, Deserialize))]
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
pub enum LibCall {
|
|
|
|
Probestack,
|
|
|
|
CeilF32,
|
|
|
|
CeilF64,
|
|
|
|
FloorF32,
|
|
|
|
FloorF64,
|
|
|
|
TruncF32,
|
|
|
|
TruncF64,
|
|
|
|
NearestF32,
|
|
|
|
NearestF64,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(feature = "cache", derive(Serialize, Deserialize))]
|
2019-01-08 21:04:03 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2019-02-07 00:26:45 +00:00
|
|
|
pub struct ExternalRelocation {
|
2019-01-08 21:04:03 +00:00
|
|
|
/// The relocation code.
|
2019-02-07 00:26:45 +00:00
|
|
|
pub reloc: Reloc,
|
2019-01-08 21:04:03 +00:00
|
|
|
/// The offset where to apply the relocation.
|
|
|
|
pub offset: binemit::CodeOffset,
|
|
|
|
/// The addend to add to the relocation value.
|
|
|
|
pub addend: binemit::Addend,
|
|
|
|
/// Relocation type.
|
|
|
|
pub target: RelocationType,
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:26:45 +00:00
|
|
|
pub struct LocalRelocation {
|
|
|
|
/// The offset where to apply the relocation.
|
|
|
|
pub offset: binemit::CodeOffset,
|
|
|
|
/// The addend to add to the relocation value.
|
|
|
|
pub addend: binemit::Addend,
|
|
|
|
/// Relocation type.
|
|
|
|
pub target: FuncIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(feature = "cache", derive(Serialize, Deserialize))]
|
2019-01-25 23:28:54 +00:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
pub enum VmCallKind {
|
|
|
|
StaticMemoryGrow,
|
|
|
|
StaticMemorySize,
|
|
|
|
|
|
|
|
SharedStaticMemoryGrow,
|
|
|
|
SharedStaticMemorySize,
|
|
|
|
|
|
|
|
DynamicMemoryGrow,
|
|
|
|
DynamicMemorySize,
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:26:45 +00:00
|
|
|
#[cfg_attr(feature = "cache", derive(Serialize, Deserialize))]
|
2019-01-17 21:09:05 +00:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
pub enum VmCall {
|
2019-01-25 23:28:54 +00:00
|
|
|
Local(VmCallKind),
|
|
|
|
Import(VmCallKind),
|
2019-01-17 21:09:05 +00:00
|
|
|
}
|
|
|
|
|
2019-01-08 21:04:03 +00:00
|
|
|
/// Specify the type of relocation
|
2019-02-07 00:26:45 +00:00
|
|
|
#[cfg_attr(feature = "cache", derive(Serialize, Deserialize))]
|
2019-01-08 21:04:03 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum RelocationType {
|
|
|
|
Intrinsic(String),
|
|
|
|
LibCall(LibCall),
|
2019-01-17 21:09:05 +00:00
|
|
|
VmCall(VmCall),
|
2019-02-07 00:26:45 +00:00
|
|
|
Signature(SigIndex),
|
2019-01-08 21:04:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Implementation of a relocation sink that just saves all the information for later
|
|
|
|
pub struct RelocSink {
|
|
|
|
/// Relocations recorded for the function.
|
2019-02-07 00:26:45 +00:00
|
|
|
pub external_relocs: Vec<ExternalRelocation>,
|
|
|
|
pub local_relocs: Vec<LocalRelocation>,
|
2019-01-08 21:04:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl binemit::RelocSink for RelocSink {
|
|
|
|
fn reloc_ebb(
|
|
|
|
&mut self,
|
|
|
|
_offset: binemit::CodeOffset,
|
|
|
|
_reloc: binemit::Reloc,
|
|
|
|
_ebb_offset: binemit::CodeOffset,
|
|
|
|
) {
|
|
|
|
// This should use the `offsets` field of `ir::Function`.
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
fn reloc_external(
|
|
|
|
&mut self,
|
|
|
|
offset: binemit::CodeOffset,
|
|
|
|
reloc: binemit::Reloc,
|
|
|
|
name: &ExternalName,
|
|
|
|
addend: binemit::Addend,
|
|
|
|
) {
|
2019-02-07 00:26:45 +00:00
|
|
|
let reloc = match reloc {
|
|
|
|
binemit::Reloc::Abs8 => Reloc::Abs8,
|
|
|
|
binemit::Reloc::X86PCRel4 => Reloc::X86PCRel4,
|
|
|
|
binemit::Reloc::X86CallPCRel4 => Reloc::X86CallPCRel4,
|
|
|
|
_ => unimplemented!("unimplented reloc type: {}", reloc),
|
|
|
|
};
|
|
|
|
|
2019-01-08 21:04:03 +00:00
|
|
|
match *name {
|
|
|
|
ExternalName::User {
|
|
|
|
namespace: 0,
|
|
|
|
index,
|
|
|
|
} => {
|
2019-02-07 00:26:45 +00:00
|
|
|
assert_eq!(reloc, Reloc::X86CallPCRel4);
|
|
|
|
self.local_relocs.push(LocalRelocation {
|
2019-01-08 21:04:03 +00:00
|
|
|
offset,
|
|
|
|
addend,
|
2019-02-07 00:26:45 +00:00
|
|
|
target: FuncIndex::new(index as usize),
|
2019-01-17 01:59:12 +00:00
|
|
|
});
|
|
|
|
}
|
2019-01-25 23:28:54 +00:00
|
|
|
ExternalName::User { namespace, index } => {
|
|
|
|
use self::call_names::*;
|
2019-02-07 00:26:45 +00:00
|
|
|
|
|
|
|
let target = match namespace {
|
|
|
|
LOCAL_NAMESPACE => RelocationType::VmCall(VmCall::Local(match index {
|
2019-01-25 23:28:54 +00:00
|
|
|
STATIC_MEM_GROW => VmCallKind::StaticMemoryGrow,
|
|
|
|
STATIC_MEM_SIZE => VmCallKind::StaticMemorySize,
|
|
|
|
|
|
|
|
SHARED_STATIC_MEM_GROW => VmCallKind::SharedStaticMemoryGrow,
|
|
|
|
SHARED_STATIC_MEM_SIZE => VmCallKind::SharedStaticMemorySize,
|
|
|
|
|
|
|
|
DYNAMIC_MEM_GROW => VmCallKind::DynamicMemoryGrow,
|
|
|
|
DYNAMIC_MEM_SIZE => VmCallKind::DynamicMemorySize,
|
|
|
|
_ => unimplemented!(),
|
2019-02-07 00:26:45 +00:00
|
|
|
})),
|
|
|
|
IMPORT_NAMESPACE => RelocationType::VmCall(VmCall::Import(match index {
|
2019-01-25 23:28:54 +00:00
|
|
|
STATIC_MEM_GROW => VmCallKind::StaticMemoryGrow,
|
|
|
|
STATIC_MEM_SIZE => VmCallKind::StaticMemorySize,
|
|
|
|
|
|
|
|
SHARED_STATIC_MEM_GROW => VmCallKind::SharedStaticMemoryGrow,
|
|
|
|
SHARED_STATIC_MEM_SIZE => VmCallKind::SharedStaticMemorySize,
|
|
|
|
|
|
|
|
DYNAMIC_MEM_GROW => VmCallKind::DynamicMemoryGrow,
|
|
|
|
DYNAMIC_MEM_SIZE => VmCallKind::DynamicMemorySize,
|
|
|
|
_ => unimplemented!(),
|
2019-02-07 00:26:45 +00:00
|
|
|
})),
|
|
|
|
SIG_NAMESPACE => RelocationType::Signature(SigIndex::new(index as usize)),
|
2019-01-17 01:59:12 +00:00
|
|
|
_ => unimplemented!(),
|
2019-02-07 00:26:45 +00:00
|
|
|
};
|
|
|
|
self.external_relocs.push(ExternalRelocation {
|
2019-01-17 01:59:12 +00:00
|
|
|
reloc,
|
|
|
|
offset,
|
|
|
|
addend,
|
|
|
|
target,
|
2019-01-08 21:04:03 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
ExternalName::TestCase { length, ascii } => {
|
|
|
|
let (slice, _) = ascii.split_at(length as usize);
|
|
|
|
let name = String::from_utf8(slice.to_vec()).unwrap();
|
2019-02-07 00:26:45 +00:00
|
|
|
self.external_relocs.push(ExternalRelocation {
|
2019-01-08 21:04:03 +00:00
|
|
|
reloc,
|
|
|
|
offset,
|
|
|
|
addend,
|
2019-01-17 01:59:12 +00:00
|
|
|
target: RelocationType::Intrinsic(name),
|
2019-01-08 21:04:03 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
ExternalName::LibCall(libcall) => {
|
2019-02-07 00:26:45 +00:00
|
|
|
let libcall = match libcall {
|
|
|
|
ir::LibCall::CeilF32 => LibCall::CeilF32,
|
|
|
|
ir::LibCall::FloorF32 => LibCall::FloorF32,
|
|
|
|
ir::LibCall::TruncF32 => LibCall::TruncF32,
|
|
|
|
ir::LibCall::NearestF32 => LibCall::NearestF32,
|
|
|
|
ir::LibCall::CeilF64 => LibCall::CeilF64,
|
|
|
|
ir::LibCall::FloorF64 => LibCall::FloorF64,
|
|
|
|
ir::LibCall::TruncF64 => LibCall::TruncF64,
|
|
|
|
ir::LibCall::NearestF64 => LibCall::NearestF64,
|
|
|
|
ir::LibCall::Probestack => LibCall::Probestack,
|
|
|
|
_ => unimplemented!("unimplemented libcall: {}", libcall),
|
|
|
|
};
|
2019-01-08 21:04:03 +00:00
|
|
|
let relocation_type = RelocationType::LibCall(libcall);
|
2019-02-07 00:26:45 +00:00
|
|
|
self.external_relocs.push(ExternalRelocation {
|
2019-01-08 21:04:03 +00:00
|
|
|
reloc,
|
|
|
|
offset,
|
|
|
|
addend,
|
|
|
|
target: relocation_type,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn reloc_jt(
|
|
|
|
&mut self,
|
|
|
|
_offset: binemit::CodeOffset,
|
|
|
|
_reloc: binemit::Reloc,
|
|
|
|
_jt: ir::JumpTable,
|
|
|
|
) {
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:26:45 +00:00
|
|
|
#[cfg_attr(feature = "cache", derive(Serialize, Deserialize))]
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
pub enum TrapCode {
|
|
|
|
StackOverflow,
|
|
|
|
HeapOutOfBounds,
|
|
|
|
TableOutOfBounds,
|
|
|
|
OutOfBounds,
|
|
|
|
IndirectCallToNull,
|
|
|
|
BadSignature,
|
|
|
|
IntegerOverflow,
|
|
|
|
IntegerDivisionByZero,
|
|
|
|
BadConversionToInteger,
|
|
|
|
Interrupt,
|
|
|
|
User(u16),
|
|
|
|
}
|
|
|
|
|
2019-01-08 21:04:03 +00:00
|
|
|
/// Implementation of a relocation sink that just saves all the information for later
|
|
|
|
impl RelocSink {
|
2019-02-07 00:26:45 +00:00
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
external_relocs: Vec::new(),
|
|
|
|
local_relocs: Vec::new(),
|
2019-01-08 21:04:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:26:45 +00:00
|
|
|
#[cfg_attr(feature = "cache", derive(Serialize, Deserialize))]
|
2019-01-19 00:45:30 +00:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
2019-01-08 21:04:03 +00:00
|
|
|
pub struct TrapData {
|
2019-01-19 00:45:30 +00:00
|
|
|
pub trapcode: TrapCode,
|
2019-02-07 00:26:45 +00:00
|
|
|
pub srcloc: u32,
|
2019-01-08 21:04:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Simple implementation of a TrapSink
|
|
|
|
/// that saves the info for later.
|
2019-02-07 00:26:45 +00:00
|
|
|
#[cfg_attr(feature = "cache", derive(Serialize, Deserialize))]
|
2019-01-08 21:04:03 +00:00
|
|
|
pub struct TrapSink {
|
2019-02-07 00:26:45 +00:00
|
|
|
trap_datas: Vec<(usize, TrapData)>,
|
2019-01-08 21:04:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TrapSink {
|
|
|
|
pub fn new() -> TrapSink {
|
|
|
|
TrapSink {
|
2019-02-07 00:26:45 +00:00
|
|
|
trap_datas: Vec::new(),
|
2019-01-08 21:04:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-19 00:45:30 +00:00
|
|
|
pub fn lookup(&self, offset: usize) -> Option<TrapData> {
|
2019-02-07 22:44:28 +00:00
|
|
|
self.trap_datas
|
|
|
|
.iter()
|
|
|
|
.find(|(trap_offset, _)| *trap_offset == offset)
|
|
|
|
.map(|(_, trap_data)| *trap_data)
|
2019-01-19 00:45:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn drain_local(&mut self, current_func_offset: usize, local: &mut LocalTrapSink) {
|
2019-02-07 00:26:45 +00:00
|
|
|
self.trap_datas.extend(
|
|
|
|
local
|
|
|
|
.trap_datas
|
|
|
|
.drain(..)
|
|
|
|
.map(|(offset, trap_data)| (current_func_offset + offset, trap_data)),
|
|
|
|
);
|
2019-01-08 21:04:03 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-19 00:45:30 +00:00
|
|
|
|
|
|
|
pub struct LocalTrapSink {
|
|
|
|
trap_datas: Vec<(usize, TrapData)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LocalTrapSink {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
LocalTrapSink { trap_datas: vec![] }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl binemit::TrapSink for LocalTrapSink {
|
2019-02-07 00:26:45 +00:00
|
|
|
fn trap(&mut self, offset: u32, srcloc: SourceLoc, trapcode: ir::TrapCode) {
|
|
|
|
let trapcode = match trapcode {
|
|
|
|
ir::TrapCode::StackOverflow => TrapCode::StackOverflow,
|
|
|
|
ir::TrapCode::HeapOutOfBounds => TrapCode::HeapOutOfBounds,
|
|
|
|
ir::TrapCode::TableOutOfBounds => TrapCode::TableOutOfBounds,
|
|
|
|
ir::TrapCode::OutOfBounds => TrapCode::OutOfBounds,
|
|
|
|
ir::TrapCode::IndirectCallToNull => TrapCode::IndirectCallToNull,
|
|
|
|
ir::TrapCode::BadSignature => TrapCode::BadSignature,
|
|
|
|
ir::TrapCode::IntegerOverflow => TrapCode::IntegerOverflow,
|
|
|
|
ir::TrapCode::IntegerDivisionByZero => TrapCode::IntegerDivisionByZero,
|
|
|
|
ir::TrapCode::BadConversionToInteger => TrapCode::BadConversionToInteger,
|
|
|
|
ir::TrapCode::Interrupt => TrapCode::Interrupt,
|
|
|
|
ir::TrapCode::User(x) => TrapCode::User(x),
|
|
|
|
};
|
|
|
|
|
|
|
|
self.trap_datas.push((
|
|
|
|
offset as usize,
|
|
|
|
TrapData {
|
|
|
|
trapcode,
|
|
|
|
srcloc: srcloc.bits(),
|
|
|
|
},
|
|
|
|
));
|
2019-01-19 00:45:30 +00:00
|
|
|
}
|
|
|
|
}
|