diff options
| author | serpilliere <serpilliere@users.noreply.github.com> | 2018-07-03 14:34:41 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-07-03 14:34:41 +0200 |
| commit | c0afde4d7c8ff51eaa31e4a074b9e06f080b3169 (patch) | |
| tree | 967c5856dcf37980cf80a2ea48cc7ee0e008e1bd /test/analysis/depgraph.py | |
| parent | c7ca6a23768178dd49c8fe97d7c7b1e0be02cd2e (diff) | |
| parent | d314460a5a19be1f3334baedf0105d9b72fc8620 (diff) | |
| download | miasm-c0afde4d7c8ff51eaa31e4a074b9e06f080b3169.tar.gz miasm-c0afde4d7c8ff51eaa31e4a074b9e06f080b3169.zip | |
Merge pull request #787 from commial/feature/locationdb
LocationDB
Diffstat (limited to 'test/analysis/depgraph.py')
| -rw-r--r-- | test/analysis/depgraph.py | 134 |
1 files changed, 30 insertions, 104 deletions
diff --git a/test/analysis/depgraph.py b/test/analysis/depgraph.py index 86857182..f0772cca 100644 --- a/test/analysis/depgraph.py +++ b/test/analysis/depgraph.py @@ -1,7 +1,7 @@ """Regression test module for DependencyGraph""" from miasm2.expression.expression import ExprId, ExprInt, ExprAff, ExprCond, \ ExprLoc, LocKey -from miasm2.core.asmblock import AsmSymbolPool +from miasm2.core.locationdb import LocationDB from miasm2.ir.analysis import ira from miasm2.ir.ir import IRBlock, AssignBlock from miasm2.core.graph import DiGraph @@ -10,7 +10,7 @@ from itertools import count from pdb import pm import re -symbol_pool = AsmSymbolPool() +loc_db = LocationDB() EMULATION = True try: @@ -44,13 +44,13 @@ CST33 = ExprInt(0x33, 32) CST35 = ExprInt(0x35, 32) CST37 = ExprInt(0x37, 32) -LBL0 = symbol_pool.add_location("lbl0", 0) -LBL1 = symbol_pool.add_location("lbl1", 1) -LBL2 = symbol_pool.add_location("lbl2", 2) -LBL3 = symbol_pool.add_location("lbl3", 3) -LBL4 = symbol_pool.add_location("lbl4", 4) -LBL5 = symbol_pool.add_location("lbl5", 5) -LBL6 = symbol_pool.add_location("lbl6", 6) +LBL0 = loc_db.add_location("lbl0", 0) +LBL1 = loc_db.add_location("lbl1", 1) +LBL2 = loc_db.add_location("lbl2", 2) +LBL3 = loc_db.add_location("lbl3", 3) +LBL4 = loc_db.add_location("lbl4", 4) +LBL5 = loc_db.add_location("lbl5", 5) +LBL6 = loc_db.add_location("lbl6", 6) def gen_irblock(label, exprs_list): """ Returns an IRBlock. @@ -90,9 +90,9 @@ class IRATest(ira): """Fake IRA class for tests""" - def __init__(self, symbol_pool=None): + def __init__(self, loc_db=None): arch = Arch() - super(IRATest, self).__init__(arch, 32, symbol_pool) + super(IRATest, self).__init__(arch, 32, loc_db) self.IRDst = PC self.ret_reg = R @@ -100,79 +100,6 @@ class IRATest(ira): return set([self.ret_reg, self.sp]) -def bloc2graph(irgraph, label=False, lines=True): - """Render dot graph of @blocks""" - - escape_chars = re.compile('[' + re.escape('{}') + ']') - label_attr = 'colspan="2" align="center" bgcolor="grey"' - edge_attr = 'label = "%s" color="%s" style="bold"' - td_attr = 'align="left"' - block_attr = 'shape="Mrecord" fontname="Courier New"' - - out = ["digraph asm_graph {"] - fix_chars = lambda x: '\\' + x.group() - - # Generate basic blocks - out_blocks = [] - for label in irgraph.graph.nodes(): - if isinstance(label, LocKey): - label_name = irgraph.symbol_pool.loc_key_to_name(label) - else: - label_name = str(label) - - if hasattr(irgraph, 'blocks'): - irblock = irgraph.blocks[label] - else: - irblock = None - if isinstance(label, LocKey): - out_block = '%s [\n' % label_name - else: - out_block = '%s [\n' % label - out_block += "%s " % block_attr - out_block += 'label =<<table border="0" cellborder="0" cellpadding="3">' - - block_label = '<tr><td %s>%s</td></tr>' % ( - label_attr, label_name) - block_html_lines = [] - if lines and irblock is not None: - for assignblk in irblock: - for dst, src in assignblk.iteritems(): - if False: - out_render = "%.8X</td><td %s> " % (0, td_attr) - else: - out_render = "" - out_render += escape_chars.sub(fix_chars, "%s = %s" % (dst, src)) - block_html_lines.append(out_render) - block_html_lines.append(" ") - block_html_lines.pop() - block_html_lines = ('<tr><td %s>' % td_attr + - ('</td></tr><tr><td %s>' % td_attr).join(block_html_lines) + - '</td></tr>') - out_block += "%s " % block_label - out_block += block_html_lines + "</table>> ];" - out_blocks.append(out_block) - - out += out_blocks - # Generate links - for src, dst in irgraph.graph.edges(): - if isinstance(src, LocKey): - src_name = irgraph.symbol_pool.loc_key_to_name(src) - else: - src_name = str(src) - if isinstance(dst, LocKey): - dst_name = irgraph.symbol_pool.loc_key_to_name(dst) - else: - dst_name = str(dst) - - edge_color = "black" - out.append('%s -> %s' % (src_name, - dst_name) + - '[' + edge_attr % ("", edge_color) + '];') - - out.append("}") - return '\n'.join(out) - - def dg2graph(graph, label=False, lines=True): """Render dot graph of @blocks""" @@ -189,7 +116,7 @@ def dg2graph(graph, label=False, lines=True): out_blocks = [] for node in graph.nodes(): if isinstance(node, DependencyNode): - name = symbol_pool.loc_key_to_name(node.loc_key) + name = loc_db.pretty_str(node.loc_key) node_name = "%s %s %s" % (name, node.element, node.line_nb) @@ -232,7 +159,7 @@ DNC3 = DependencyNode(LBL1, C, 0) # graph 1 -G1_IRA = IRATest(symbol_pool) +G1_IRA = IRATest(loc_db) G1_IRB0 = gen_irblock(LBL0, [[ExprAff(C, CST1)]]) G1_IRB1 = gen_irblock(LBL1, [[ExprAff(B, C)]]) @@ -245,7 +172,7 @@ G1_IRA.blocks = dict([(irb.loc_key, irb) for irb in [G1_IRB0, G1_IRB1, G1_IRB2]] # graph 2 -G2_IRA = IRATest(symbol_pool) +G2_IRA = IRATest(loc_db) G2_IRB0 = gen_irblock(LBL0, [[ExprAff(C, CST1)]]) G2_IRB1 = gen_irblock(LBL1, [[ExprAff(B, CST2)]]) @@ -259,7 +186,7 @@ G2_IRA.blocks = dict([(irb.loc_key, irb) for irb in [G2_IRB0, G2_IRB1, G2_IRB2]] # graph 3 -G3_IRA = IRATest(symbol_pool) +G3_IRA = IRATest(loc_db) G3_IRB0 = gen_irblock(LBL0, [[ExprAff(C, CST1)]]) G3_IRB1 = gen_irblock(LBL1, [[ExprAff(B, CST2)]]) @@ -276,7 +203,7 @@ G3_IRA.blocks = dict([(irb.loc_key, irb) for irb in [G3_IRB0, G3_IRB1, # graph 4 -G4_IRA = IRATest(symbol_pool) +G4_IRA = IRATest(loc_db) G4_IRB0 = gen_irblock(LBL0, [[ExprAff(C, CST1)]]) G4_IRB1 = gen_irblock(LBL1, [[ExprAff(C, C + CST2)], @@ -295,7 +222,7 @@ G4_IRA.blocks = dict([(irb.loc_key, irb) for irb in [G4_IRB0, G4_IRB1, G4_IRB2]] # graph 5 -G5_IRA = IRATest(symbol_pool) +G5_IRA = IRATest(loc_db) G5_IRB0 = gen_irblock(LBL0, [[ExprAff(B, CST1)]]) G5_IRB1 = gen_irblock(LBL1, [[ExprAff(B, B + CST2)], @@ -313,7 +240,7 @@ G5_IRA.blocks = dict([(irb.loc_key, irb) for irb in [G5_IRB0, G5_IRB1, G5_IRB2]] # graph 6 -G6_IRA = IRATest(symbol_pool) +G6_IRA = IRATest(loc_db) G6_IRB0 = gen_irblock(LBL0, [[ExprAff(B, CST1)]]) G6_IRB1 = gen_irblock(LBL1, [[ExprAff(A, B)]]) @@ -325,7 +252,7 @@ G6_IRA.blocks = dict([(irb.loc_key, irb) for irb in [G6_IRB0, G6_IRB1]]) # graph 7 -G7_IRA = IRATest(symbol_pool) +G7_IRA = IRATest(loc_db) G7_IRB0 = gen_irblock(LBL0, [[ExprAff(C, CST1)]]) G7_IRB1 = gen_irblock(LBL1, [[ExprAff(B, C)], [ExprAff(A, B)]]) @@ -339,7 +266,7 @@ G7_IRA.blocks = dict([(irb.loc_key, irb) for irb in [G7_IRB0, G7_IRB1, G7_IRB2]] # graph 8 -G8_IRA = IRATest(symbol_pool) +G8_IRA = IRATest(loc_db) G8_IRB0 = gen_irblock(LBL0, [[ExprAff(C, CST1)]]) G8_IRB1 = gen_irblock(LBL1, [[ExprAff(B, C)], [ExprAff(C, D)]]) @@ -355,7 +282,7 @@ G8_IRA.blocks = dict([(irb.loc_key, irb) for irb in [G8_IRB0, G8_IRB1, G8_IRB2]] # graph 10 -G10_IRA = IRATest(symbol_pool) +G10_IRA = IRATest(loc_db) G10_IRB1 = gen_irblock(LBL1, [[ExprAff(B, B + CST2)]]) G10_IRB2 = gen_irblock(LBL2, [[ExprAff(A, B)]]) @@ -367,7 +294,7 @@ G10_IRA.blocks = dict([(irb.loc_key, irb) for irb in [G10_IRB1, G10_IRB2]]) # graph 11 -G11_IRA = IRATest(symbol_pool) +G11_IRA = IRATest(loc_db) G11_IRB0 = gen_irblock(LBL0, [[ExprAff(A, CST1), ExprAff(B, CST2)]]) @@ -383,7 +310,7 @@ G11_IRA.blocks = dict([(irb.loc_key, irb) # graph 12 -G12_IRA = IRATest(symbol_pool) +G12_IRA = IRATest(loc_db) G12_IRB0 = gen_irblock(LBL0, [[ExprAff(B, CST1)]]) G12_IRB1 = gen_irblock(LBL1, [[ExprAff(A, B)], [ExprAff(B, B + CST2)]]) @@ -399,7 +326,7 @@ G12_IRA.blocks = dict([(irb.loc_key, irb) for irb in [G12_IRB0, G12_IRB1, # graph 13 -G13_IRA = IRATest(symbol_pool) +G13_IRA = IRATest(loc_db) G13_IRB0 = gen_irblock(LBL0, [[ExprAff(A, CST1)], #[ExprAff(B, A)], @@ -427,7 +354,7 @@ G13_IRA.blocks = dict([(irb.loc_key, irb) for irb in [G13_IRB0, G13_IRB1, # graph 14 -G14_IRA = IRATest(symbol_pool) +G14_IRA = IRATest(loc_db) G14_IRB0 = gen_irblock(LBL0, [[ExprAff(A, CST1)], [ExprAff(G14_IRA.IRDst, @@ -457,7 +384,7 @@ G14_IRA.blocks = dict([(irb.loc_key, irb) for irb in [G14_IRB0, G14_IRB1, # graph 16 -G15_IRA = IRATest(symbol_pool) +G15_IRA = IRATest(loc_db) G15_IRB0 = gen_irblock(LBL0, [[ExprAff(A, CST1)]]) G15_IRB1 = gen_irblock(LBL1, [[ExprAff(D, A + B)], @@ -474,7 +401,7 @@ G15_IRA.blocks = dict([(irb.loc_key, irb) for irb in [G15_IRB0, G15_IRB1, # graph 16 -G16_IRA = IRATest(symbol_pool) +G16_IRA = IRATest(loc_db) G16_IRB0 = gen_irblock(LBL0, [[ExprAff(A, CST1)]]) G16_IRB1 = gen_irblock(LBL1, [[ExprAff(R, D)]]) @@ -498,7 +425,7 @@ G16_IRA.blocks = dict([(irb.loc_key, irb) for irb in [G16_IRB0, G16_IRB1, # graph 17 -G17_IRA = IRATest(symbol_pool) +G17_IRA = IRATest(loc_db) G17_IRB0 = gen_irblock(LBL0, [[ExprAff(A, CST1), ExprAff(D, CST2)]]) @@ -642,7 +569,7 @@ def flatNode(node): element = int(node.element.arg) else: RuntimeError("Unsupported type '%s'" % type(enode.element)) - name = symbol_pool.loc_key_to_name(node.loc_key) + name = loc_db.pretty_str(node.loc_key) return (name, element, node.line_nb) @@ -741,7 +668,7 @@ def match_results(resultsA, resultsB, nodes): def get_flat_init_depnodes(depnodes): out = [] for node in depnodes: - name = symbol_pool.loc_key_to_name(node.loc_key) + name = loc_db.pretty_str(node.loc_key) out.append((name, node.element.name, node.line_nb, @@ -1026,7 +953,6 @@ for test_nb, test in enumerate([(G1_IRA, G1_INPUT), g_ira, (depnodes, heads) = test open("graph_%02d.dot" % (test_nb + 1), "w").write(g_ira.graph.dot()) - open("graph_%02d.dot" % (test_nb + 1), "w").write(bloc2graph(g_ira)) # Different options suffix_key_list = ["", "_nosimp", "_nomem", "_nocall", |