Skip to content

Commit fb07f5c

Browse files
committed
Experiment with flattening graphs for textual edit distance based match
Signed-off-by: Nir Izraeli <[email protected]>
1 parent 42815ff commit fb07f5c

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import idaapi
2+
3+
from .vector import Vector
4+
5+
6+
class FlatGraphVector(Vector):
7+
type = 'flatgraph_editdistance'
8+
type_version = 0
9+
10+
def __init__(self, *args, **kwargs):
11+
super(FlatGraphVector, self).__init__(*args, **kwargs)
12+
self.flow_chart = idaapi.FlowChart(idaapi.get_func(self.offset))
13+
self.visited = set()
14+
self.items = list[self.flow_chart.size]
15+
16+
def _bb_size(self, bb):
17+
if bb.endEA > bb.startEA:
18+
return bb.endEA - bb.startEA
19+
20+
raise ValueError("while flattening graph, a basicblock that ends before "
21+
"it starts encountered at {:x}".format(self.offset))
22+
23+
def _bb_value(self, bb):
24+
# TODO: this should be something that's uncorellated with the order of
25+
# basic blocks
26+
return self._bb_size(bb)
27+
28+
def _append_bbs(self, *bbs):
29+
for bb in bbs:
30+
self.items.append(self._bb_value(bb))
31+
32+
def _find_head(self):
33+
def is_head(bb):
34+
return len(bb.preds()) == 0
35+
36+
heads = filter(is_head, self.flow_chart)
37+
if len(heads) == 1:
38+
return heads[0]
39+
40+
msg = ("flattening graphs with head count other than 1 is not supported, "
41+
"got {} head-count for {:x}".format(len(heads), self.offset))
42+
raise ValueError(msg)
43+
44+
def _sort_siblings(self, siblings):
45+
return sorted(siblings, key=self._bb_size)
46+
47+
def _recurse_bb(self, bb):
48+
if bb in self.visited:
49+
return []
50+
51+
self.visited.add(bb)
52+
siblings = self._sort_siblings(bb.succs())
53+
self._append_bbs(*siblings)
54+
55+
for sibling in siblings:
56+
self._recurse_siblings(sibling)
57+
58+
def _data(self):
59+
head = self._find_head()
60+
self._recurse_bb(head)
61+
return self.items

0 commit comments

Comments
 (0)