Skip to content

Commit f969608

Browse files
committed
Signature tweaks
1 parent 2733797 commit f969608

File tree

3 files changed

+36
-59
lines changed

3 files changed

+36
-59
lines changed

src/gadget/another_iterator.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use jingle::modeling::{ModeledInstruction, ModelingContext};
22
use jingle::sleigh::{Instruction, OpCode};
3+
use tracing::{instrument, trace};
34
use z3::ast::Ast;
45
use z3::{Context, Solver};
56

@@ -41,11 +42,14 @@ where
4142
loop {
4243
let gadget = self.gadgets.next()?;
4344
let gadget_signature = GadgetSignature::from(gadget);
45+
trace!("Evaluating gadget at {:x}", gadget.address());
4446
let is_candidate: Vec<bool> = self
4547
.trace
4648
.iter()
4749
.map(|i| {
48-
gadget_signature.covers(&GadgetSignature::from(&i.instr))
50+
trace!("Checking {} signature vs gadget {}", i.instr.disassembly, gadget);
51+
52+
gadget_signature.covers(&GadgetSignature::from_instr(&i.instr, i))
4953
&& has_compatible_control_flow(&i.instr, gadget)
5054
})
5155
.collect();
@@ -63,6 +67,8 @@ where
6367
}
6468
}
6569
})
70+
}else{
71+
trace!("Could not model gadget: \n{}", gadget)
6672
}
6773
return Some(next_entry);
6874
} else {

src/gadget/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ pub struct Gadget {
2525
}
2626

2727
impl Gadget {
28-
pub fn address(&self) -> Option<u64> {
29-
self.instructions.first().map(|f| f.address)
28+
pub fn address(&self) -> u64 {
29+
self.instructions.first().map(|f| f.address).unwrap()
3030
}
3131

3232
pub fn ops(&self) -> impl Iterator<Item = &PcodeOperation> {

src/gadget/signature.rs

Lines changed: 27 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
use std::cmp::Ordering;
22

3-
use jingle::modeling::ModeledBlock;
4-
use jingle::sleigh::{GeneralizedVarNode, IndirectVarNode, Instruction, VarNode};
5-
63
use crate::gadget::Gadget;
4+
use jingle::modeling::ModeledBlock;
5+
use jingle::sleigh::{GeneralizedVarNode, IndirectVarNode, Instruction, SpaceManager, SpaceType, VarNode};
6+
use tracing::trace;
77

88
#[derive(Clone, Debug)]
99
pub struct GadgetSignature {
1010
outputs: Vec<GeneralizedVarNode>,
11-
#[allow(unused)]
12-
inputs: Vec<GeneralizedVarNode>,
1311
}
1412

1513
impl GadgetSignature {
1614
/// For now this is very naive; just want a very rough filter to make sure we aren't
1715
/// throwing completely pointless work at z3
1816
pub fn covers(&self, other: &GadgetSignature) -> bool {
17+
trace!("{:?} vs {:?}", self.outputs, other.outputs);
1918
varnode_set_covers(&self.outputs, &other.outputs)
2019
}
2120

@@ -45,18 +44,24 @@ impl PartialOrd<GadgetSignature> for GadgetSignature {
4544
}
4645
}
4746
}
48-
impl From<&Instruction> for GadgetSignature {
49-
fn from(value: &Instruction) -> Self {
47+
impl GadgetSignature {
48+
pub(crate) fn from_instr<T: SpaceManager>(value: &Instruction, t: &T) -> Self {
5049
let mut outputs = Vec::new();
51-
let mut inputs = Vec::new();
5250

5351
for op in &value.ops {
5452
if let Some(op) = op.output() {
55-
outputs.push(op);
53+
if let GeneralizedVarNode::Direct(v) = &op {
54+
if let Some(h) = t.get_space_info(v.space_index){
55+
if h._type == SpaceType::IPTR_PROCESSOR{
56+
outputs.push(op);
57+
}
58+
}
59+
} else {
60+
outputs.push(op);
61+
}
5662
}
57-
inputs.extend(op.inputs())
5863
}
59-
Self { outputs, inputs }
64+
Self { outputs }
6065
}
6166
}
6267

@@ -72,21 +77,27 @@ impl<'ctx> From<&ModeledBlock<'ctx>> for GadgetSignature {
7277
inputs.extend(op.inputs())
7378
}
7479
}
75-
Self { outputs, inputs }
80+
Self { outputs }
7681
}
7782
}
7883

7984
impl From<&Gadget> for GadgetSignature {
8085
fn from(value: &Gadget) -> Self {
8186
let mut outputs = Vec::new();
82-
let mut inputs = Vec::new();
8387
for op in value.instructions.iter().flat_map(|i| &i.ops) {
8488
if let Some(op) = op.output() {
85-
outputs.push(op);
89+
if let GeneralizedVarNode::Direct(v) = &op {
90+
if let Some(h) = value.get_space_info(v.space_index){
91+
if h._type == SpaceType::IPTR_PROCESSOR{
92+
outputs.push(op);
93+
}
94+
}
95+
} else {
96+
outputs.push(op);
97+
}
8698
}
87-
inputs.extend(op.inputs())
8899
}
89-
Self { outputs, inputs }
100+
Self { outputs }
90101
}
91102
}
92103

@@ -139,23 +150,13 @@ mod tests {
139150
space_index: 0,
140151
offset: 0,
141152
})],
142-
inputs: vec![Direct(VarNode {
143-
size: 4,
144-
space_index: 0,
145-
offset: 0,
146-
})],
147153
};
148154
let o2 = GadgetSignature {
149155
outputs: vec![Direct(VarNode {
150156
size: 4,
151157
space_index: 0,
152158
offset: 0,
153159
})],
154-
inputs: vec![Direct(VarNode {
155-
size: 4,
156-
space_index: 0,
157-
offset: 0,
158-
})],
159160
};
160161
assert!(o1.covers(&o2));
161162
assert!(o2.covers(&o1));
@@ -172,23 +173,13 @@ mod tests {
172173
space_index: 0,
173174
offset: 0,
174175
})],
175-
inputs: vec![Direct(VarNode {
176-
size: 4,
177-
space_index: 0,
178-
offset: 0,
179-
})],
180176
};
181177
let o2 = GadgetSignature {
182178
outputs: vec![Direct(VarNode {
183179
size: 4,
184180
space_index: 0,
185181
offset: 3,
186182
})],
187-
inputs: vec![Direct(VarNode {
188-
size: 4,
189-
space_index: 0,
190-
offset: 0,
191-
})],
192183
};
193184
assert_ne!(o1, o2);
194185
assert!(!o1.covers(&o2));
@@ -203,23 +194,13 @@ mod tests {
203194
space_index: 0,
204195
offset: 0,
205196
})],
206-
inputs: vec![Direct(VarNode {
207-
size: 4,
208-
space_index: 0,
209-
offset: 0,
210-
})],
211197
};
212198
let o2 = GadgetSignature {
213199
outputs: vec![Direct(VarNode {
214200
size: 4,
215201
space_index: 0,
216202
offset: 4,
217203
})],
218-
inputs: vec![Direct(VarNode {
219-
size: 4,
220-
space_index: 0,
221-
offset: 0,
222-
})],
223204
};
224205
assert!(!o1.covers(&o2));
225206
assert!(!o2.covers(&o1));
@@ -233,11 +214,6 @@ mod tests {
233214
space_index: 0,
234215
offset: 7,
235216
})],
236-
inputs: vec![Direct(VarNode {
237-
size: 4,
238-
space_index: 0,
239-
offset: 0,
240-
})],
241217
};
242218
let o2 = GadgetSignature {
243219
outputs: vec![
@@ -262,11 +238,6 @@ mod tests {
262238
offset: 16,
263239
}),
264240
],
265-
inputs: vec![Direct(VarNode {
266-
size: 4,
267-
space_index: 0,
268-
offset: 0,
269-
})],
270241
};
271242
assert!(o2.covers(&o1));
272243
assert!(!o1.covers(&o2));

0 commit comments

Comments
 (0)