-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathspecdec.rs
More file actions
263 lines (217 loc) · 7.99 KB
/
specdec.rs
File metadata and controls
263 lines (217 loc) · 7.99 KB
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
use perfect::*;
use perfect::events::*;
use perfect::stats::*;
use perfect::asm::Emitter;
use perfect::experiments::decoder::*;
use perfect::util::disas_bytes;
use rand::prelude::*;
use rand::Rng;
use rand::distributions::{ Distribution, Standard };
use iced_x86::{
Decoder, DecoderOptions, Instruction, Formatter, IntelFormatter
};
fn main() {
let mut harness = HarnessConfig::default_zen2().emit();
//SpeculativeDecodeFuzz::run(&mut harness);
SpeculativeDecodeExhaustive::<3>::run(&mut harness);
}
/// Try to speculatively evaluate random x86_64 instruction encodings.
///
/// Test
/// ====
///
/// - Perform a random instruction in the shadow of a costly misprediction.
///
/// - If we observe that a marker instruction (ie. PREFETCH) after the tested
/// instruction is speculatively dispatched, this means that the encoding
/// is probably valid
///
/// - If we observe that a marker instruction is *not* dispatched, this
/// probably means that the speculative path was cancelled due to some
/// kind of exception
///
///
pub struct SpeculativeDecodeFuzz;
const SIZE: usize = 16;
impl MispredictedReturnTemplate<[u8; SIZE]> for SpeculativeDecodeFuzz {}
impl SpeculativeDecodeFuzz {
fn emit_random_instr(f: &mut X64Assembler, input: [u8; SIZE]) {
dynasm!(f ; .bytes input );
}
fn run(harness: &mut PerfectHarness) {
let mut events = EventSet::new();
events.add(Zen2Event::LsPrefInstrDisp(0x1));
let opts = MispredictedReturnOptions::zen2_defaults()
.speculative_epilogue_fn(Some(|f, input| {
dynasm!(f
; nop
; nop
; nop
; nop
; prefetch [rax]
);
for _ in 0..128 {
dynasm!(f; int3);
}
}))
.post_prologue_fn(Some(|f, input| {
dynasm!(f ; mov rcx, 2);
}))
.prologue_fn(Some(|f, input| {
dynasm!(f
);
}))
.rdpmc_strat(RdpmcStrategy::Gpr(Gpr::R15));
let mut cases = Vec::new();
let mut rng = thread_rng();
// Generate some random-ish x86 instruction encodings
for _ in 0..4096 {
let enc: RandomEncoding<16> = rng.gen();
cases.push(enc.as_bytes());
}
// Generate totally random 16-byte blocks
//for _ in 0..4096 {
// cases.push(rng.gen());
//}
for case in cases.iter() {
let asm = Self::emit(opts, *case, Self::emit_random_instr);
let asm_reader = asm.reader();
let asm_tgt_buf = asm_reader.lock();
let asm_tgt_ptr = asm_tgt_buf.ptr(AssemblyOffset(0));
let asm_fn: MeasuredFn = unsafe {
std::mem::transmute(asm_tgt_ptr)
};
for event in events.iter() {
let desc = event.as_desc();
let results = harness.measure(asm_fn,
&desc, 8, InputMethod::Fixed(0, 0)
).unwrap();
// Ignore cases which are completely garbage
if results.get_min() == 0 {
continue;
}
// Create a buffer with NOP padding at the end (reflecting
// the actual bytes we speculatively decoded).
let mut buf = [0x90u8; 20];
let mut buf = Vec::new();
buf.extend_from_slice(case);
buf.extend_from_slice(&[0x90; 4]);
//buf[..16].copy_from_slice(case);
let dis = disas_bytes(&buf);
let maybe_invalid = dis.iter().filter(|x| x.1).count() != 0;
if !maybe_invalid {
continue;
}
if dis.len() != 2 {
continue;
}
println!("input={:02x?}", buf);
for (istr, invalid, bytes) in dis.iter() {
let mut bstr = String::new();
for b in bytes.iter() {
bstr.push_str(&format!("{:02x}", b));
}
println!(" {:32} {} ", bstr, istr);
if *invalid {
decompose_encoding(&bytes);
}
}
println!();
}
}
}
}
pub struct SpeculativeDecodeExhaustive<const S: usize>;
impl <const S: usize> MispredictedReturnTemplate<[u8; S]> for SpeculativeDecodeExhaustive<S> {}
impl <const S: usize> SpeculativeDecodeExhaustive<S> {
fn emit_instr(f: &mut X64Assembler, input: [u8; S]) {
dynasm!(f ; .bytes input );
}
fn run(harness: &mut PerfectHarness) {
let mut events = EventSet::new();
events.add(Zen2Event::LsPrefInstrDisp(0x1));
let opts = MispredictedReturnOptions::zen2_defaults()
.speculative_epilogue_fn(Some(|f, input| {
dynasm!(f
; nop
; nop
; nop
; nop
; prefetch [rax]
);
for _ in 0..128 {
dynasm!(f; int3);
}
}))
.post_prologue_fn(Some(|f, input| {
dynasm!(f ; mov rcx, 2);
}))
.prologue_fn(Some(|f, input| {
dynasm!(f
);
}))
.rdpmc_strat(RdpmcStrategy::Gpr(Gpr::R15));
let mut cases = Vec::new();
for x in (0x00u8..=0xffu8).permutations(S) {
let arr: [u8; S] = x.try_into().unwrap();
cases.push(arr);
}
for case in cases.iter() {
let asm = Self::emit(opts, *case, Self::emit_instr);
let asm_reader = asm.reader();
let asm_tgt_buf = asm_reader.lock();
let asm_tgt_ptr = asm_tgt_buf.ptr(AssemblyOffset(0));
let asm_fn: MeasuredFn = unsafe {
std::mem::transmute(asm_tgt_ptr)
};
for event in events.iter() {
let desc = event.as_desc();
let results = harness.measure(asm_fn,
&desc, 8, InputMethod::Fixed(0, 0)
).unwrap();
// Ignore cases which are completely garbage
if results.get_min() == 0 {
continue;
}
// Create a buffer with NOP padding at the end (reflecting
// the actual bytes we speculatively decoded).
let mut buf = [0x90u8; 20];
let mut buf = Vec::new();
buf.extend_from_slice(case);
buf.extend_from_slice(&[0x90; 4]);
//buf[..16].copy_from_slice(case);
let dis = disas_bytes(&buf);
let maybe_invalid = dis.iter().filter(|x| x.1).count() != 0;
if !maybe_invalid {
continue;
}
println!("input={:02x?}", buf);
for (istr, invalid, bytes) in dis.iter() {
let mut bstr = String::new();
for b in bytes.iter() {
bstr.push_str(&format!("{:02x}", b));
}
//println!(" {:32} {} ", bstr, istr);
if *invalid {
//decompose_encoding(&bytes);
}
}
//println!();
}
}
}
}
fn decompose_encoding(bytes: &[u8]) {
for start in (0..bytes.len()).rev() {
let rem = bytes.len() - start;
for len in (0..=rem).rev() {
let slice = &bytes[start..start+len];
let dis = disas_bytes(slice);
if dis.len() == 0 { continue; }
let maybe_invalid = dis.iter().filter(|x| x.1).count() != 0;
if !maybe_invalid {
println!(" {:02x?}", dis);
}
}
}
}