-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathanalysis.rs
More file actions
469 lines (418 loc) · 13.1 KB
/
Copy pathanalysis.rs
File metadata and controls
469 lines (418 loc) · 13.1 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
use super::RomRegion;
// use super::
use crate::utils::*;
use crate::INSTRUCTION_SIZE;
use rabbitizer;
use enum_map::Enum;
use enum_map::EnumMap;
// use strum_macros::EnumIter; // 0.17.1
use num_enum::TryFromPrimitive;
struct RegisterState {
initialized: bool,
}
impl Default for RegisterState {
fn default() -> Self {
RegisterState { initialized: false }
}
}
#[derive(Enum, Clone, Copy, Hash)]
#[allow(non_camel_case_types)]
#[derive(Debug, Eq, PartialEq, TryFromPrimitive)]
#[repr(u32)]
pub enum MipsGpr {
zero = 0,
at = 1,
v0 = 2,
v1 = 3,
a0 = 4,
a1 = 5,
a2 = 6,
a3 = 7,
t0 = 8,
t1 = 9,
t2 = 10,
t3 = 11,
t4 = 12,
t5 = 13,
t6 = 14,
t7 = 15,
s0 = 16,
s1 = 17,
s2 = 18,
s3 = 19,
s4 = 20,
s5 = 21,
s6 = 22,
s7 = 23,
t8 = 24,
t9 = 25,
k0 = 26,
k1 = 27,
gp = 28,
sp = 29,
fp = 30,
ra = 31,
}
#[derive(Enum, Clone, Copy, Hash)]
#[allow(non_camel_case_types)]
#[derive(Debug, Eq, PartialEq, TryFromPrimitive)]
#[repr(u32)]
pub enum MipsFpr {
fv0 = 0,
fv0f = 1,
fv1 = 2,
fv1f = 3,
ft0 = 4,
ft0f = 5,
ft1 = 6,
ft1f = 7,
ft2 = 8,
ft2f = 9,
ft3 = 10,
ft3f = 11,
fa0 = 12,
fa0f = 13,
fa1 = 14,
fa1f = 15,
ft4 = 16,
ft4f = 17,
ft5 = 18,
ft5f = 19,
fs0 = 20,
fs0f = 21,
fs1 = 22,
fs1f = 23,
fs2 = 24,
fs2f = 25,
fs3 = 26,
fs3f = 27,
fs4 = 28,
fs4f = 29,
fs5 = 30,
fs5f = 31,
}
#[derive(Enum, Clone, Copy, Hash)]
#[allow(non_camel_case_types)]
#[derive(Debug, Eq, PartialEq, TryFromPrimitive)]
#[repr(u32)]
pub enum MipsCop0r {
Index = 0,
Random = 1,
EntryLo0 = 2,
EntryLo1 = 3,
Context = 4,
PageMask = 5,
Wired = 6,
// Reserved07 = 7,
BadVaddr = 8,
Count = 9,
EntryHi = 10,
Compare = 11,
Status = 12,
Cause = 13,
EPC = 14,
PRevID = 15,
Config = 16,
LLAddr = 17,
WatchLo = 18,
WatchHi = 19,
XContext = 20,
// Reserved21 = 21,
// Reserved22 = 22,
// Reserved23 = 23,
// Reserved24 = 24,
// Reserved25 = 25,
PErr = 26,
CacheErr = 27,
TagLo = 28,
TagHi = 29,
ErrorEPC = 30,
// Reserved31 = 31,
}
pub struct MyInstruction(pub rabbitizer::Instruction);
impl MyInstruction {
pub fn new(word: u32) -> Self {
Self(rabbitizer::Instruction::new(word, 0))
}
pub fn new_rsp(word: u32) -> Self {
Self(rabbitizer::Instruction::new_rsp(word, 0))
}
pub fn rs(&self) -> MipsGpr {
((self.0.raw() >> 21) & 0x1F).try_into().unwrap()
}
pub fn rt(&self) -> MipsGpr {
((self.0.raw() >> 16) & 0x1F).try_into().unwrap()
}
pub fn rd(&self) -> MipsGpr {
((self.0.raw() >> 11) & 0x1F).try_into().unwrap()
}
pub fn fs(&self) -> MipsFpr {
((self.0.raw() >> 21) & 0x1F).try_into().unwrap()
}
pub fn ft(&self) -> MipsFpr {
((self.0.raw() >> 16) & 0x1F).try_into().unwrap()
}
pub fn fd(&self) -> MipsFpr {
((self.0.raw() >> 11) & 0x1F).try_into().unwrap()
}
pub fn sa(&self) -> u32 {
(self.0.raw() >> 6) & 0x1F
}
pub fn op(&self) -> u32 {
(self.0.raw() >> 16) & 0x1F
}
pub fn code(&self) -> u32 {
(self.0.raw() >> 6) & 0xFFFFF
}
pub fn code_upper(&self) -> u32 {
(self.0.raw() >> 16) & 0x3FF
}
pub fn code_lower(&self) -> u32 {
(self.0.raw() >> 6) & 0x3FF
}
pub fn instr_get_cop0_rd(&self) -> Result<MipsCop0r, u32> {
let reg_num = (self.0.raw() >> 11) & 0x1F;
let maybe_enum = reg_num.try_into();
maybe_enum.map_err(|_| reg_num)
}
// Checks if an instruction has the given operand as an input
pub fn has_operand_input(&self, operand: rabbitizer::OperandType) -> bool {
let id = self.0.instr_id();
// If the instruction has the given operand and doesn't modify it, then it's an input
if self.0.has_operand_alias(operand) {
match operand {
rabbitizer::OperandType::cpu_rd => return !self.0.modifies_rd(),
rabbitizer::OperandType::cpu_rt => return !self.0.modifies_rt(),
rabbitizer::OperandType::cpu_rs => {
// rs is always an input
return true;
}
rabbitizer::OperandType::cpu_fd => {
// fd is never an input
return false;
}
rabbitizer::OperandType::cpu_ft => {
// ft is always an input except for lwc1 and ldc1
return !matches!(
id,
rabbitizer::InstrId::cpu_lwc1 | rabbitizer::InstrId::cpu_ldc1
);
}
rabbitizer::OperandType::cpu_fs => {
// fs is always an input, except for mtc1 and dmtc1
return !matches!(
id,
rabbitizer::InstrId::cpu_mtc1 | rabbitizer::InstrId::cpu_dmtc1
);
}
_ => return false,
}
}
return false;
}
fn has_zero_output(&self) -> bool {
if self.0.modifies_rd() {
let rd = self.rd();
if rd == MipsGpr::zero {
return true;
}
}
if self.0.modifies_rt() {
let rt = self.rt();
if rt == MipsGpr::zero {
return true;
}
}
false
}
}
// Treat $v0 and $fv0 as an initialized register
// gcc will use these for the first uninitialized variable reference for ints and floats respectively,
// so enabling this option won't reject gcc functions that begin with a reference to an uninitialized local variable.
const WEAK_UNINITIALIZED_CHECK: bool = true;
// Checks if an instruction references an uninitialized register
fn references_uninitialized(
my_instruction: &MyInstruction,
gpr_reg_states: &EnumMap<MipsGpr, RegisterState>,
fpr_reg_states: &EnumMap<MipsFpr, RegisterState>,
) -> bool {
// For each operand type, check if the instruction uses that operand as an input and whether the corresponding register is initialized
if my_instruction.has_operand_input(rabbitizer::OperandType::cpu_rs) {
let rs = my_instruction.rs();
if !gpr_reg_states[rs].initialized {
return true;
}
}
if my_instruction.has_operand_input(rabbitizer::OperandType::cpu_rt) {
let rt = my_instruction.rt();
if !gpr_reg_states[rt].initialized {
return true;
}
}
if my_instruction.has_operand_input(rabbitizer::OperandType::cpu_rd) {
let rd = my_instruction.rd();
if !gpr_reg_states[rd].initialized {
return true;
}
}
if my_instruction.has_operand_input(rabbitizer::OperandType::cpu_fs) {
let fs = my_instruction.fs();
if !fpr_reg_states[fs].initialized {
return true;
}
}
if my_instruction.has_operand_input(rabbitizer::OperandType::cpu_ft) {
let ft = my_instruction.ft();
if !fpr_reg_states[ft].initialized {
return true;
}
}
if my_instruction.has_operand_input(rabbitizer::OperandType::cpu_fd) {
let fd = my_instruction.fd();
if !fpr_reg_states[fd].initialized {
return true;
}
}
false
}
// Check if this instruction is (probably) invalid when at the beginning of a region of code
fn is_invalid_start_instruction(
my_instruction: &MyInstruction,
gpr_reg_states: &EnumMap<MipsGpr, RegisterState>,
fpr_reg_states: &EnumMap<MipsFpr, RegisterState>,
) -> bool {
let id = my_instruction.0.instr_id();
// println!(" {}", my_instruction.0.disassemble(None, 0) );
// Check if this is a valid instruction to begin with
if !super::is_valid(my_instruction) {
// println!("Invalid instruction");
return true;
}
match id {
// Code probably won't start with a nop (some functions do, but it'll just be one nop that can be recovered later)
rabbitizer::InstrId::cpu_nop => {
// println!("nop");
return true;
}
// Code shouldn't jump to $zero
rabbitizer::InstrId::cpu_jr => {
if my_instruction.rs() == MipsGpr::zero {
// println!("jump to $zero");
return true;
}
}
// Shifts with $zero as the input and a non-zero shift amount are likely not real code
rabbitizer::InstrId::cpu_sll
| rabbitizer::InstrId::cpu_srl
| rabbitizer::InstrId::cpu_sra
| rabbitizer::InstrId::cpu_dsll
| rabbitizer::InstrId::cpu_dsll32
| rabbitizer::InstrId::cpu_dsrl
| rabbitizer::InstrId::cpu_dsrl32
| rabbitizer::InstrId::cpu_dsra
| rabbitizer::InstrId::cpu_dsra32 => {
// println!(
// "test {:?} {:?} {:?}\n",
// id,
// my_instruction.instr_get_rt(),
// my_instruction.instr_get_sa()
// );
if (my_instruction.rt() == MipsGpr::zero)
&& (my_instruction.sa() != 0)
{
// println!("Shift with $zero as input and non-zero sa");
return true;
}
}
// Code probably won't start with mthi or mtlo
rabbitizer::InstrId::cpu_mthi | rabbitizer::InstrId::cpu_mtlo => {
// println!("starts with mthi or mtlo");
return true;
}
// Code shouldn't start with branches based on the cop1 condition flag (it won't have been set yet)
rabbitizer::InstrId::cpu_bc1t
| rabbitizer::InstrId::cpu_bc1f
| rabbitizer::InstrId::cpu_bc1tl
| rabbitizer::InstrId::cpu_bc1fl => {
// println!("branch from cop1 condition flag");
return true;
}
// add/sub and addi are good indicators that the bytes aren't actually instructions, since addu/subu and addiu would normally be used
rabbitizer::InstrId::cpu_add
| rabbitizer::InstrId::cpu_addi
| rabbitizer::InstrId::cpu_sub => {
// println!("add/sub/addi");
return true;
}
_ => {}
}
// Code shouldn't output to $zero
if my_instruction.has_zero_output() {
// println!("has zero output");
return true;
}
// Code shouldn't start with an unconditional branch
if my_instruction.0.is_unconditional_branch() {
// println!("unconditional branch");
return true;
}
// Code shouldn't start with a linked jump, as it'd need to save the return address first
if my_instruction.0.does_link() {
// println!("does link");
return true;
}
// Code shouldn't start with a store relative to $ra
if my_instruction
.0
.has_operand(rabbitizer::OperandType::cpu_immediate_base)
&& my_instruction.rs() == MipsGpr::ra
{
// println!("store relative to $ra");
return true;
}
// Code shouldn't start with a reference to a register that isn't initialized
if references_uninitialized(&my_instruction, &gpr_reg_states, &fpr_reg_states) {
// println!("references uninitialized");
return true;
}
false
}
pub fn count_invalid_start_instructions(region: &RomRegion, rom_bytes: &[u8]) -> usize {
let mut gpr_reg_states: EnumMap<MipsGpr, RegisterState> = EnumMap::default();
let mut fpr_reg_states: EnumMap<MipsFpr, RegisterState> = EnumMap::default();
// Zero is always initialized (it's zero)
gpr_reg_states[MipsGpr::zero].initialized = true;
// The stack pointer and return address always initialized
gpr_reg_states[MipsGpr::sp].initialized = true;
gpr_reg_states[MipsGpr::ra].initialized = true;
// Treat all arg registers as initialized
gpr_reg_states[MipsGpr::a0].initialized = true;
gpr_reg_states[MipsGpr::a1].initialized = true;
gpr_reg_states[MipsGpr::a2].initialized = true;
gpr_reg_states[MipsGpr::a3].initialized = true;
// Treat $v0 as initialized for gcc if enabled
if WEAK_UNINITIALIZED_CHECK {
gpr_reg_states[MipsGpr::v0].initialized = true;
}
// FPRs
// Treat all arg registers as initialized
fpr_reg_states[MipsFpr::fa0].initialized = true;
fpr_reg_states[MipsFpr::fa0f].initialized = true;
fpr_reg_states[MipsFpr::fa1].initialized = true;
fpr_reg_states[MipsFpr::fa1f].initialized = true;
// Treat $fv0 as initialized for gcc if enabled
if WEAK_UNINITIALIZED_CHECK {
fpr_reg_states[MipsFpr::fv0].initialized = true;
fpr_reg_states[MipsFpr::fv0f].initialized = true;
}
let mut instr_index = 0;
for chunk in rom_bytes[region.rom_start()..].chunks_exact(INSTRUCTION_SIZE) {
let my_instruction = MyInstruction(rabbitizer::Instruction::new(read_be_word(chunk), 0));
if !is_invalid_start_instruction(&my_instruction, &gpr_reg_states, &fpr_reg_states) {
break;
}
instr_index += 1;
}
// println!("instr_index: {}", instr_index);
return instr_index;
}