Skip to content

Commit

Permalink
Merge pull request #347 from serpilliere/dg_has_loop
Browse files Browse the repository at this point in the history
Dg has loop
  • Loading branch information
commial committed Mar 23, 2016
2 parents 1eec0a1 + 8c436da commit 4eceb2b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
6 changes: 3 additions & 3 deletions miasm2/analysis/depgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,10 @@ def relevant_labels(self):

@property
def has_loop(self):
"""True if current dictionary has a loop"""
"""True iff there is at least one data dependencies cycle (regarding
the associated depgraph)"""
if self._has_loop is None:
self._has_loop = (len(self.relevant_labels) !=
len(set(self.relevant_labels)))
self._has_loop = self.graph.has_loop()
return self._has_loop

def irblock_slice(self, irb):
Expand Down
28 changes: 28 additions & 0 deletions miasm2/core/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,34 @@ def walk_depth_first_backward(self, head):
"""Performs a depth first search on the reversed graph from @head"""
return self._walk_generic_first(head, -1, self.predecessors_iter)

def has_loop(self):
"""Return True if the graph contains at least a cycle"""
todo = list(self.nodes())
# tested nodes
done = set()
# current DFS nodes
current = set()
while todo:
node = todo.pop()
if node in done:
continue

if node in current:
# DFS branch end
for succ in self.successors_iter(node):
if succ in current:
return True
# A node cannot be in current AND in done
current.remove(node)
done.add(node)
else:
# Launch DFS from node
todo.append(node)
current.add(node)
todo += self.successors(node)

return False

def compute_natural_loops(self, head):
"""
Computes all natural loops in the graph.
Expand Down
2 changes: 1 addition & 1 deletion test/analysis/dg_test_06_expected.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"EAX": "0x1", "has_loop": false}, {"EAX": "0x2", "has_loop": true}]
[{"EAX": "0x1", "has_loop": false}, {"EAX": "0x2", "has_loop": false}]
2 changes: 1 addition & 1 deletion test/analysis/dg_test_06_implicit_expected.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"has_loop": false, "EAX": "0x1", "satisfiability": true, "constraints": {"EAX_init": "0xffffffff"}}, {"has_loop": true, "EAX": "0x2", "satisfiability": false, "constraints": {}}]
[{"has_loop": false, "EAX": "0x1", "satisfiability": true, "constraints": {"EAX_init": "0xffffffff"}}, {"has_loop": false, "EAX": "0x2", "satisfiability": false, "constraints": {}}]

0 comments on commit 4eceb2b

Please sign in to comment.