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
use std::cmp::Ordering;
use std::collections::{BTreeMap, BTreeSet};
use std::hash::{Hash, Hasher};
use crate::binary;
#[derive(Clone, Debug)]
pub struct Gadget<'a> {
pub instrs: Vec<iced_x86::Instruction>,
pub full_matches: BTreeSet<u64>,
pub partial_matches: BTreeMap<u64, Vec<&'a binary::Binary>>,
}
impl<'a> Gadget<'a> {
pub fn new(instrs: Vec<iced_x86::Instruction>, full_matches: BTreeSet<u64>) -> Gadget<'a> {
Gadget {
instrs,
full_matches,
partial_matches: BTreeMap::new(),
}
}
pub fn last_instr(&self) -> Option<&iced_x86::Instruction> {
self.instrs.iter().next_back()
}
pub fn first_full_match(&self) -> Option<u64> {
match self.full_matches.iter().next() {
Some(addr) => Some(*addr),
None => None,
}
}
pub fn add_partial_match(&mut self, addr: u64, bin: &'a binary::Binary) {
match self.partial_matches.get_mut(&addr) {
Some(bins) => bins.push(bin),
None => {
self.partial_matches.insert(addr, vec![bin]);
}
};
}
fn min_addr(&self) -> Option<&u64> {
if let Some(min_full) = self.full_matches.iter().next() {
Some(min_full)
} else if let Some(min_part) = self.partial_matches.keys().next() {
Some(min_part)
} else {
None
}
}
}
impl Eq for Gadget<'_> {}
impl PartialEq for Gadget<'_> {
fn eq(&self, other: &Self) -> bool {
self.instrs == other.instrs
}
}
impl Ord for Gadget<'_> {
fn cmp(&self, other: &Self) -> Ordering {
if let Some(self_min_addr) = self.min_addr() {
if let Some(other_min_addr) = other.min_addr() {
(*self_min_addr).cmp(other_min_addr)
} else {
Ordering::Greater
}
} else {
if other.min_addr().is_some() {
Ordering::Less
} else {
Ordering::Equal
}
}
}
}
impl PartialOrd for Gadget<'_> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Hash for Gadget<'_> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.instrs.hash(state);
}
}