Skip to content

Commit

Permalink
Merge pull request #1447 from cea-sec/helper-depgraph
Browse files Browse the repository at this point in the history
Depgraph: add `.address_to_location` to help find the line and loc of an address
  • Loading branch information
serpilliere committed Apr 23, 2023
2 parents 20aff86 + d32bf63 commit 230d528
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
9 changes: 2 additions & 7 deletions example/symbol_exec/depgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,11 @@

# Build information
target_addr = int(args.target_addr, 0)
current_loc_key = next(iter(ircfg.getby_offset(target_addr)))
assignblk_index = 0
current_block = ircfg.get_block(current_loc_key)
for assignblk_index, assignblk in enumerate(current_block):
if assignblk.instr.offset == target_addr:
break
target = dg.address_to_location(target_addr)

# Enumerate solutions
json_solutions = []
for sol_nb, sol in enumerate(dg.get(current_block.loc_key, elements, assignblk_index, set())):
for sol_nb, sol in enumerate(dg.get(target["loc_key"], elements, target["line_nb"], set())):
fname = "sol_%d.dot" % sol_nb
with open(fname, "w") as fdesc:
fdesc.write(sol.graph.dot())
Expand Down
18 changes: 18 additions & 0 deletions miasm/analysis/depgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,3 +639,21 @@ def get_from_depnodes(self, depnodes, heads):
lead = list(depnodes)[0]
elements = set(depnode.element for depnode in depnodes)
return self.get(lead.loc_key, elements, lead.line_nb, heads)

def address_to_location(self, address):
"""Helper to retrieve the .get() arguments, ie.
assembly address -> irblock's location key and line number
"""
current_loc_key = next(iter(self._ircfg.getby_offset(address)))
assignblk_index = 0
current_block = self._ircfg.get_block(current_loc_key)
for assignblk_index, assignblk in enumerate(current_block):
if assignblk.instr.offset == address:
break
else:
return None

return {
"loc_key": current_block.loc_key,
"line_nb": assignblk_index,
}

0 comments on commit 230d528

Please sign in to comment.