Skip to content

Commit 4eceb2b

Browse files
committed
Merge pull request #347 from serpilliere/dg_has_loop
Dg has loop
2 parents 1eec0a1 + 8c436da commit 4eceb2b

4 files changed

Lines changed: 33 additions & 5 deletions

File tree

miasm2/analysis/depgraph.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,10 @@ def relevant_labels(self):
252252

253253
@property
254254
def has_loop(self):
255-
"""True if current dictionary has a loop"""
255+
"""True iff there is at least one data dependencies cycle (regarding
256+
the associated depgraph)"""
256257
if self._has_loop is None:
257-
self._has_loop = (len(self.relevant_labels) !=
258-
len(set(self.relevant_labels)))
258+
self._has_loop = self.graph.has_loop()
259259
return self._has_loop
260260

261261
def irblock_slice(self, irb):

miasm2/core/graph.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,34 @@ def walk_depth_first_backward(self, head):
478478
"""Performs a depth first search on the reversed graph from @head"""
479479
return self._walk_generic_first(head, -1, self.predecessors_iter)
480480

481+
def has_loop(self):
482+
"""Return True if the graph contains at least a cycle"""
483+
todo = list(self.nodes())
484+
# tested nodes
485+
done = set()
486+
# current DFS nodes
487+
current = set()
488+
while todo:
489+
node = todo.pop()
490+
if node in done:
491+
continue
492+
493+
if node in current:
494+
# DFS branch end
495+
for succ in self.successors_iter(node):
496+
if succ in current:
497+
return True
498+
# A node cannot be in current AND in done
499+
current.remove(node)
500+
done.add(node)
501+
else:
502+
# Launch DFS from node
503+
todo.append(node)
504+
current.add(node)
505+
todo += self.successors(node)
506+
507+
return False
508+
481509
def compute_natural_loops(self, head):
482510
"""
483511
Computes all natural loops in the graph.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[{"EAX": "0x1", "has_loop": false}, {"EAX": "0x2", "has_loop": true}]
1+
[{"EAX": "0x1", "has_loop": false}, {"EAX": "0x2", "has_loop": false}]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[{"has_loop": false, "EAX": "0x1", "satisfiability": true, "constraints": {"EAX_init": "0xffffffff"}}, {"has_loop": true, "EAX": "0x2", "satisfiability": false, "constraints": {}}]
1+
[{"has_loop": false, "EAX": "0x1", "satisfiability": true, "constraints": {"EAX_init": "0xffffffff"}}, {"has_loop": false, "EAX": "0x2", "satisfiability": false, "constraints": {}}]

0 commit comments

Comments
 (0)