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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
extern crate memmap;
extern crate byteorder;
extern crate take_mut;
pub mod common;
pub mod x64;
pub mod x86;
use std::ops::{Deref, DerefMut};
use std::iter::Extend;
use std::sync::{Arc, RwLock, RwLockReadGuard};
use std::io;
use std::error;
use std::fmt;
use memmap::{Mmap, MmapMut};
use byteorder::{ByteOrder, LittleEndian};
#[macro_export]
macro_rules! Pointer {
($e:expr) => {$e as *const _ as _};
}
#[macro_export]
macro_rules! MutPointer {
($e:expr) => {$e as *mut _ as _};
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct AssemblyOffset(pub usize);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct DynamicLabel(usize);
#[derive(Debug)]
pub struct ExecutableBuffer {
length: usize,
buffer: Mmap
}
#[derive(Debug)]
struct MutableBuffer {
length: usize,
buffer: MmapMut
}
impl ExecutableBuffer {
pub fn ptr(&self, offset: AssemblyOffset) -> *const u8 {
&self[offset.0] as *const u8
}
fn new(length: usize, size: usize) -> io::Result<ExecutableBuffer> {
Ok(ExecutableBuffer {
length: length,
buffer: MmapMut::map_anon(size)?.make_exec()?
})
}
fn make_mut(self) -> io::Result<MutableBuffer> {
Ok(MutableBuffer {
length: self.length,
buffer: self.buffer.make_mut()?
})
}
}
impl MutableBuffer {
fn new(length: usize, size: usize) -> io::Result<MutableBuffer> {
Ok(MutableBuffer {
length: length,
buffer: MmapMut::map_anon(size)?
})
}
fn make_exec(self) -> io::Result<ExecutableBuffer> {
Ok(ExecutableBuffer {
length: self.length,
buffer: self.buffer.make_exec()?
})
}
}
impl Deref for ExecutableBuffer {
type Target = [u8];
fn deref(&self) -> &[u8] {
&self.buffer[..self.length]
}
}
impl Deref for MutableBuffer {
type Target = [u8];
fn deref(&self) -> &[u8] {
&self.buffer[..self.length]
}
}
impl DerefMut for MutableBuffer {
fn deref_mut(&mut self) -> &mut [u8] {
&mut self.buffer[..self.length]
}
}
#[derive(Debug, Clone)]
pub struct Executor {
execbuffer: Arc<RwLock<ExecutableBuffer>>
}
impl Executor {
#[inline]
pub fn lock(&self) -> RwLockReadGuard<ExecutableBuffer> {
self.execbuffer.read().unwrap()
}
}
pub trait DynasmApi: Extend<u8> + for<'a> Extend<&'a u8> {
fn offset(&self) -> AssemblyOffset;
fn push(&mut self, byte: u8);
#[inline]
fn push_i8(&mut self, value: i8) {
self.push(value as u8);
}
#[inline]
fn push_i16(&mut self, value: i16) {
let mut buf = [0u8; 2];
LittleEndian::write_i16(&mut buf, value);
self.extend(&buf);
}
#[inline]
fn push_i32(&mut self, value: i32) {
let mut buf = [0u8; 4];
LittleEndian::write_i32(&mut buf, value);
self.extend(&buf);
}
#[inline]
fn push_i64(&mut self, value: i64) {
let mut buf = [0u8; 8];
LittleEndian::write_i64(&mut buf, value);
self.extend(&buf);
}
#[inline]
fn push_u16(&mut self, value: u16) {
let mut buf = [0u8; 2];
LittleEndian::write_u16(&mut buf, value);
self.extend(&buf);
}
#[inline]
fn push_u32(&mut self, value: u32) {
let mut buf = [0u8; 4];
LittleEndian::write_u32(&mut buf, value);
self.extend(&buf);
}
#[inline]
fn push_u64(&mut self, value: u64) {
let mut buf = [0u8; 8];
LittleEndian::write_u64(&mut buf, value);
self.extend(&buf);
}
#[inline]
fn runtime_error(&self, msg: &'static str) -> ! {
panic!(msg);
}
}
pub trait DynasmLabelApi : DynasmApi {
type Relocation;
fn align(&mut self, alignment: usize);
fn local_label( &mut self, name: &'static str);
fn global_label( &mut self, name: &'static str);
fn dynamic_label(&mut self, id: DynamicLabel);
fn forward_reloc( &mut self, name: &'static str, kind: Self::Relocation);
fn backward_reloc(&mut self, name: &'static str, kind: Self::Relocation);
fn global_reloc( &mut self, name: &'static str, kind: Self::Relocation);
fn dynamic_reloc( &mut self, id: DynamicLabel, kind: Self::Relocation);
fn bare_reloc( &mut self, target: usize, kind: Self::Relocation);
}
impl DynasmApi for Vec<u8> {
fn offset(&self) -> AssemblyOffset {
AssemblyOffset(self.len())
}
fn push(&mut self, byte: u8) {
Vec::push(self, byte);
}
}
#[derive(Debug, Clone)]
pub enum DynasmError {
CheckFailed
}
impl fmt::Display for DynasmError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "An assembly modification check failed")
}
}
impl error::Error for DynasmError {
fn description(&self) -> &str {
match *self {
DynasmError::CheckFailed => "An assembly modification offset check failed"
}
}
}