1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use crate::translator::{irlibcall_to_libcall, irreloc_to_relocationkind};
use cranelift_codegen::binemit;
use cranelift_codegen::ir::LibCall;
use cranelift_codegen::ir::{self, ExternalName};
use cranelift_entity::EntityRef as CraneliftEntityRef;
use wasmer_compiler::{JumpTable, Relocation, RelocationTarget, TrapInformation};
use wasmer_compiler::{RelocationKind, SectionIndex};
use wasmer_types::entity::EntityRef;
use wasmer_types::{FunctionIndex, LocalFunctionIndex, ModuleInfo};
use wasmer_vm::TrapCode;
pub(crate) struct RelocSink<'a> {
module: &'a ModuleInfo,
local_func_index: LocalFunctionIndex,
pub func_relocs: Vec<Relocation>,
pub probestack_trampoline_relocation_target: Option<SectionIndex>,
}
impl<'a> binemit::RelocSink for RelocSink<'a> {
fn reloc_external(
&mut self,
offset: binemit::CodeOffset,
_source_loc: ir::SourceLoc,
reloc: binemit::Reloc,
name: &ExternalName,
addend: binemit::Addend,
) {
let reloc_target = if let ExternalName::User { namespace, index } = *name {
debug_assert_eq!(namespace, 0);
RelocationTarget::LocalFunc(
self.module
.local_func_index(FunctionIndex::from_u32(index))
.expect("The provided function should be local"),
)
} else if let ExternalName::LibCall(libcall) = *name {
match (libcall, self.probestack_trampoline_relocation_target) {
(LibCall::Probestack, Some(probestack_trampoline_relocation_target)) => {
self.func_relocs.push(Relocation {
kind: RelocationKind::X86CallPCRel4,
reloc_target: RelocationTarget::CustomSection(
probestack_trampoline_relocation_target,
),
offset: offset,
addend: addend,
});
return;
}
_ => RelocationTarget::LibCall(irlibcall_to_libcall(libcall)),
}
} else {
panic!("unrecognized external name")
};
self.func_relocs.push(Relocation {
kind: irreloc_to_relocationkind(reloc),
reloc_target,
offset,
addend,
});
}
fn reloc_constant(
&mut self,
_code_offset: binemit::CodeOffset,
_reloc: binemit::Reloc,
_constant_offset: ir::ConstantOffset,
) {
}
fn reloc_jt(&mut self, offset: binemit::CodeOffset, reloc: binemit::Reloc, jt: ir::JumpTable) {
self.func_relocs.push(Relocation {
kind: irreloc_to_relocationkind(reloc),
reloc_target: RelocationTarget::JumpTable(
self.local_func_index,
JumpTable::new(jt.index()),
),
offset,
addend: 0,
});
}
}
impl<'a> RelocSink<'a> {
pub fn new(
module: &'a ModuleInfo,
func_index: FunctionIndex,
probestack_trampoline_relocation_target: Option<SectionIndex>,
) -> Self {
let local_func_index = module
.local_func_index(func_index)
.expect("The provided function should be local");
Self {
module,
local_func_index,
func_relocs: Vec::new(),
probestack_trampoline_relocation_target,
}
}
}
pub(crate) struct TrapSink {
pub traps: Vec<TrapInformation>,
}
impl TrapSink {
pub fn new() -> Self {
Self { traps: Vec::new() }
}
}
impl binemit::TrapSink for TrapSink {
fn trap(
&mut self,
code_offset: binemit::CodeOffset,
_source_loc: ir::SourceLoc,
trap_code: ir::TrapCode,
) {
self.traps.push(TrapInformation {
code_offset,
trap_code: translate_ir_trapcode(trap_code),
});
}
}
fn translate_ir_trapcode(trap: ir::TrapCode) -> TrapCode {
match trap {
ir::TrapCode::StackOverflow => TrapCode::StackOverflow,
ir::TrapCode::HeapOutOfBounds => TrapCode::HeapAccessOutOfBounds,
ir::TrapCode::HeapMisaligned => TrapCode::HeapMisaligned,
ir::TrapCode::TableOutOfBounds => TrapCode::TableAccessOutOfBounds,
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::UnreachableCodeReached => TrapCode::UnreachableCodeReached,
ir::TrapCode::Interrupt => unimplemented!("Interrupts not supported"),
ir::TrapCode::User(_user_code) => unimplemented!("User trap code not supported"),
}
}