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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
//! Shared utility layer for all architecture-specific compilers
use crate::compiler::Executable;
use crate::CompilerError;
use arrayvec::ArrayVec;
use dynasmrt::mmap::MutableBuffer;
use dynasmrt::{
components::PatchLoc, relocations::Relocation, AssemblyOffset, DynamicLabel, DynasmApi,
DynasmLabelApi,
};
use std::marker::PhantomData;
pub(crate) use dynasmrt::mmap::ExecutableBuffer;
/// Our own simple replacement for [`dynasmrt::Assembler`]
///
/// The default assembler in [`dynasmrt`] has a ton of features we don't need,
/// but more importantly it will panic if it can't make its memory region
/// executable. This is a no-go for us, since there is a fallback available.
///
/// Our needs are simple: a single un-named label, no relocations, and
/// no modification after finalization. However, we do need to handle runtime
/// mmap errors thoroughly.
#[derive(Debug, Clone)]
pub(crate) struct Assembler<R: Relocation, const S: usize> {
/// Temporary fixed capacity buffer for assembling code
buffer: ArrayVec<u8, S>,
/// Address of the last "target" label, if any
target: Option<AssemblyOffset>,
/// Relocations are applied immediately and not stored.
phantom: PhantomData<R>,
}
impl<R: Relocation, const S: usize> Assembler<R, S> {
/// Return the entry point as an [`AssemblyOffset`].
#[inline(always)]
pub(crate) fn entry() -> AssemblyOffset {
AssemblyOffset(0)
}
/// Size of the code stored so far, in bytes
#[inline(always)]
pub(crate) fn len(&self) -> usize {
self.buffer.len()
}
/// Make a new assembler with a temporary buffer but no executable buffer.
#[inline(always)]
pub(crate) fn new() -> Self {
Self {
buffer: ArrayVec::new(),
target: None,
phantom: PhantomData,
}
}
/// Return a new [`Executable`] with the code that's been written so far.
///
/// This may fail if we can't allocate some memory, fill it, and mark
/// it as executable. For example, a Linux platform with policy to restrict
/// `mprotect` will show runtime errors at this point.
///
/// Performance note: Semantically it makes more sense to consume the
/// `Assembler` instance here, passing it by value. This can result in a
/// memcpy that doesn't optimize out, which is a dramatic increase to
/// the memory bandwidth required in compilation. We avoid that extra
/// copy by only passing a reference.
#[inline(always)]
pub(crate) fn finalize(&self) -> Result<Executable, CompilerError> {
// We never execute code from the buffer until it's complete, and we use
// a freshly mmap'ed buffer for each program. Because of this, we don't
// need to explicitly clear the icache even on platforms that would
// normally want this. If we reuse buffers in the future, this will need
// architecture-specific support for icache clearing when a new program
// is finalized into a buffer we previously ran.
let mut mut_buf = MutableBuffer::new(self.buffer.len())?;
mut_buf.set_len(self.buffer.len());
mut_buf[..].copy_from_slice(&self.buffer);
Ok(Executable {
buffer: mut_buf.make_exec()?,
})
}
}
impl std::fmt::Debug for Executable {
/// Debug an [`Executable`] by hex-dumping its contents.
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Executable[{}; {} ]",
std::env::consts::ARCH,
hex::encode(&self.buffer[..])
)
}
}
// Reluctantly implement just enough of [`DynasmLabelApi`] for our single backward label.
impl<R: Relocation, const S: usize> DynasmLabelApi for Assembler<R, S> {
type Relocation = R;
#[inline(always)]
fn local_label(&mut self, name: &'static str) {
debug_assert_eq!(name, "target");
self.target = Some(self.offset());
}
#[inline(always)]
fn backward_relocation(
&mut self,
name: &'static str,
target_offset: isize,
field_offset: u8,
ref_offset: u8,
kind: R,
) {
debug_assert_eq!(name, "target");
let target = self
.target
.expect("generated programs always have a target before branch");
// Apply the relocation immediately without storing it.
let loc = PatchLoc::new(self.offset(), target_offset, field_offset, ref_offset, kind);
let buf = &mut self.buffer[loc.range(0)];
loc.patch(buf, 0, target.0)
.expect("program relocations are always in range");
}
fn global_label(&mut self, _name: &'static str) {
unreachable!();
}
fn dynamic_label(&mut self, _id: DynamicLabel) {
unreachable!();
}
fn bare_relocation(&mut self, _target: usize, _field_offset: u8, _ref_offset: u8, _kind: R) {
unreachable!();
}
fn global_relocation(
&mut self,
_name: &'static str,
_target_offset: isize,
_field_offset: u8,
_ref_offset: u8,
_kind: R,
) {
unreachable!();
}
fn dynamic_relocation(
&mut self,
_id: DynamicLabel,
_target_offset: isize,
_field_offset: u8,
_ref_offset: u8,
_kind: R,
) {
unreachable!();
}
fn forward_relocation(
&mut self,
_name: &'static str,
_target_offset: isize,
_field_offset: u8,
_ref_offset: u8,
_kind: R,
) {
unreachable!();
}
}
impl<R: Relocation, const S: usize> Extend<u8> for Assembler<R, S> {
#[inline(always)]
fn extend<T: IntoIterator<Item = u8>>(&mut self, iter: T) {
self.buffer.extend(iter);
}
}
impl<'a, R: Relocation, const S: usize> Extend<&'a u8> for Assembler<R, S> {
#[inline(always)]
fn extend<T: IntoIterator<Item = &'a u8>>(&mut self, iter: T) {
for byte in iter {
self.buffer.push(*byte);
}
}
}
impl<R: Relocation, const S: usize> DynasmApi for Assembler<R, S> {
#[inline(always)]
fn offset(&self) -> AssemblyOffset {
AssemblyOffset(self.buffer.len())
}
#[inline(always)]
fn push(&mut self, byte: u8) {
self.buffer.push(byte);
}
fn align(&mut self, _alignment: usize, _with: u8) {
unreachable!();
}
}
|