From 455cfbe8b7aed7cb5be8e1b9aa1917a9f7d51821 Mon Sep 17 00:00:00 2001 From: Ajax Date: Mon, 25 Jan 2016 11:05:29 +0100 Subject: BasicBlocks: update examples with the new API --- example/disasm/callback.py | 4 ++-- example/disasm/file.py | 6 ++---- example/disasm/full.py | 10 ++++------ example/disasm/function.py | 10 ++++------ 4 files changed, 12 insertions(+), 18 deletions(-) (limited to 'example/disasm') diff --git a/example/disasm/callback.py b/example/disasm/callback.py index 6c77023e..4a7507dd 100644 --- a/example/disasm/callback.py +++ b/example/disasm/callback.py @@ -63,5 +63,5 @@ blocks_after = mdis.dis_multibloc(0) print "\n".join(str(block) for block in blocks_after) # Ensure the callback has been called -assert blocks[0].lines[0].name == "CALL" -assert blocks_after[0].lines[0].name == "PUSH" +assert blocks.heads()[0].lines[0].name == "CALL" +assert blocks_after.heads()[0].lines[0].name == "PUSH" diff --git a/example/disasm/file.py b/example/disasm/file.py index 1b9347d8..db5cd96b 100644 --- a/example/disasm/file.py +++ b/example/disasm/file.py @@ -1,6 +1,5 @@ import sys from miasm2.arch.x86.disasm import dis_x86_32 -from miasm2.core.asmbloc import bloc2graph from miasm2.analysis.binary import Container from pdb import pm @@ -14,7 +13,6 @@ cont = Container.from_stream(open(sys.argv[1])) mdis = dis_x86_32(cont.bin_stream) # Inform the engine to avoid disassembling null instructions mdis.dont_dis_nulstart_bloc = True -blocs = mdis.dis_multibloc(addr) +blocks = mdis.dis_multibloc(addr) -graph = bloc2graph(blocs) -open('graph.dot', 'w').write(graph) +open('graph.dot', 'w').write(blocks.dot()) diff --git a/example/disasm/full.py b/example/disasm/full.py index 33b2f41f..25e3a018 100644 --- a/example/disasm/full.py +++ b/example/disasm/full.py @@ -4,7 +4,7 @@ from argparse import ArgumentParser from pdb import pm from miasm2.analysis.binary import Container -from miasm2.core.asmbloc import log_asmbloc, asm_label, bloc2graph +from miasm2.core.asmbloc import log_asmbloc, asm_label, BasicBlocks from miasm2.expression.expression import ExprId from miasm2.core.interval import interval from miasm2.analysis.machine import Machine @@ -142,15 +142,13 @@ while not finish and todo: # Generate dotty graph -all_blocs = [] +all_blocs = BasicBlocks() for blocs in all_funcs_blocs.values(): all_blocs += blocs - # for b in blocs: - # print b + log.info('generate graph file') -g = bloc2graph(all_blocs, True) -open('graph_execflow.dot', 'w').write(g) +open('graph_execflow.dot', 'w').write(all_blocs.dot(label=True)) log.info('generate intervals') diff --git a/example/disasm/function.py b/example/disasm/function.py index a1a9b393..1fe1754f 100644 --- a/example/disasm/function.py +++ b/example/disasm/function.py @@ -1,5 +1,4 @@ from miasm2.arch.x86.disasm import dis_x86_32 -from miasm2.core.asmbloc import bloc2graph # MOV EAX, 0x1337BEEF # MOV ECX, 0x4 @@ -9,10 +8,9 @@ from miasm2.core.asmbloc import bloc2graph # RET shellcode = '\xb8\xef\xbe7\x13\xb9\x04\x00\x00\x00\xc1\xc0\x08\xe2\xfb\xc3' mdis = dis_x86_32(shellcode) -blocs = mdis.dis_multibloc(0) +blocks = mdis.dis_multibloc(0) -for bloc in blocs: - print bloc +for block in blocks: + print block -graph = bloc2graph(blocs) -open('graph.dot', 'w').write(graph) +open('graph.dot', 'w').write(blocks.dot()) -- cgit 1.4.1 From d6222c4383891c6706ce70ec7750b42ee24e1cfc Mon Sep 17 00:00:00 2001 From: Ajax Date: Tue, 26 Jan 2016 17:15:22 +0100 Subject: Rename BasicBlocks -> AsmCFG, more comprehensible, include "graph" --- example/disasm/full.py | 4 ++-- miasm2/core/asmbloc.py | 38 +++++++++++++++++++------------------- miasm2/core/parse_asm.py | 2 +- test/core/asmbloc.py | 8 ++++---- 4 files changed, 26 insertions(+), 26 deletions(-) (limited to 'example/disasm') diff --git a/example/disasm/full.py b/example/disasm/full.py index 25e3a018..0b0069c6 100644 --- a/example/disasm/full.py +++ b/example/disasm/full.py @@ -4,7 +4,7 @@ from argparse import ArgumentParser from pdb import pm from miasm2.analysis.binary import Container -from miasm2.core.asmbloc import log_asmbloc, asm_label, BasicBlocks +from miasm2.core.asmbloc import log_asmbloc, asm_label, AsmCFG from miasm2.expression.expression import ExprId from miasm2.core.interval import interval from miasm2.analysis.machine import Machine @@ -142,7 +142,7 @@ while not finish and todo: # Generate dotty graph -all_blocs = BasicBlocks() +all_blocs = AsmCFG() for blocs in all_funcs_blocs.values(): all_blocs += blocs diff --git a/miasm2/core/asmbloc.py b/miasm2/core/asmbloc.py index 7390536b..9553d14d 100644 --- a/miasm2/core/asmbloc.py +++ b/miasm2/core/asmbloc.py @@ -532,7 +532,7 @@ def dis_bloc_all(mnemo, pool_bin, offset, job_done, symbol_pool, dont_dis=[], attrib={}): log_asmbloc.info("dis bloc all") if blocs is None: - blocs = BasicBlocks() + blocs = AsmCFG() todo = [offset] bloc_cpt = 0 @@ -576,7 +576,7 @@ def dis_bloc_all(mnemo, pool_bin, offset, job_done, symbol_pool, dont_dis=[], return blocs -class BasicBlocks(DiGraph): +class AsmCFG(DiGraph): """Directed graph standing for a ASM Control Flow Graph with: - nodes: asm_bloc - edges: constraints between blocks, synchronized with asm_bloc's "bto" @@ -584,16 +584,16 @@ class BasicBlocks(DiGraph): Specialized the .dot export and force the relation between block to be uniq, and associated with a constraint. - Offer helpers on BasicBlocks management, such as research by label, sanity + Offer helpers on AsmCFG management, such as research by label, sanity checking and mnemonic size guessing. """ # Internal structure for pending management - BasicBlocksPending = namedtuple("BasicBlocksPending", - ["waiter", "constraint"]) + AsmCFGPending = namedtuple("AsmCFGPending", + ["waiter", "constraint"]) def __init__(self, *args, **kwargs): - super(BasicBlocks, self).__init__(*args, **kwargs) + super(AsmCFG, self).__init__(*args, **kwargs) # Edges -> constraint self.edges2constraint = {} # Expected asm_label -> set( (src, dst), constraint ) @@ -603,20 +603,20 @@ class BasicBlocks(DiGraph): # Compatibility with old list API def append(self, *args, **kwargs): - raise DeprecationWarning("BasicBlocks is a graph, use add_node") + raise DeprecationWarning("AsmCFG is a graph, use add_node") def remove(self, *args, **kwargs): - raise DeprecationWarning("BasicBlocks is a graph, use del_node") + raise DeprecationWarning("AsmCFG is a graph, use del_node") def __getitem__(self, *args, **kwargs): - raise DeprecationWarning("Order of BasicBlocks elements is not reliable") + raise DeprecationWarning("Order of AsmCFG elements is not reliable") def __iter__(self): """Iterator on asm_bloc composing the current graph""" return iter(self._nodes) def __len__(self): - """Return the number of blocks in BasicBlocks""" + """Return the number of blocks in AsmCFG""" return len(self._nodes) # Manage graph with associated constraints @@ -635,7 +635,7 @@ class BasicBlocks(DiGraph): # Add edge self.edges2constraint[(src, dst)] = constraint - super(BasicBlocks, self).add_edge(src, dst) + super(AsmCFG, self).add_edge(src, dst) def add_uniq_edge(self, src, dst, constraint): """Add an edge from @src to @dst if it doesn't already exist""" @@ -653,7 +653,7 @@ class BasicBlocks(DiGraph): # Del edge del self.edges2constraint[(src, dst)] - super(BasicBlocks, self).del_edge(src, dst) + super(AsmCFG, self).del_edge(src, dst) def add_node(self, block): """Add the block @block to the current instance, if it is not already in @@ -664,7 +664,7 @@ class BasicBlocks(DiGraph): aforementionned destinations. `self.pendings` indicates which blocks are not yet resolved. """ - status = super(BasicBlocks, self).add_node(block) + status = super(AsmCFG, self).add_node(block) if not status: return status @@ -681,8 +681,8 @@ class BasicBlocks(DiGraph): None) if dst is None: # Block is yet unknown, add it to pendings - to_add = self.BasicBlocksPending(waiter=block, - constraint=constraint.c_t) + to_add = self.AsmCFGPending(waiter=block, + constraint=constraint.c_t) self._pendings.setdefault(constraint.label, set()).add(to_add) else: @@ -692,7 +692,7 @@ class BasicBlocks(DiGraph): return status def del_node(self, block): - super(BasicBlocks, self).del_node(block) + super(AsmCFG, self).del_node(block) del self._label2block[block.label] def merge(self, graph): @@ -778,7 +778,7 @@ class BasicBlocks(DiGraph): # Helpers @property def pendings(self): - """Dictionnary of label -> set(BasicBlocksPending instance) indicating + """Dictionnary of label -> set(AsmCFGPending instance) indicating which label are missing in the current instance. A label is missing if a block which is already in nodes has constraints with him (thanks to its .bto) and the corresponding block is not yet in @@ -814,8 +814,8 @@ class BasicBlocks(DiGraph): if dst is None: # Missing destination, add to pendings self._pendings.setdefault(constraint.label, - set()).add(self.BasicBlocksPending(block, - constraint.c_t)) + set()).add(self.AsmCFGPending(block, + constraint.c_t)) continue edge = (block, dst) edges.append(edge) diff --git a/miasm2/core/parse_asm.py b/miasm2/core/parse_asm.py index 304d0673..aefa6df9 100644 --- a/miasm2/core/parse_asm.py +++ b/miasm2/core/parse_asm.py @@ -232,7 +232,7 @@ def parse_txt(mnemo, attrib, txt, symbol_pool=None): cur_block = None state = STATE_NO_BLOC i = 0 - blocks = asmbloc.BasicBlocks() + blocks = asmbloc.AsmCFG() block_to_nlink = None block_may_link = False delayslot = 0 diff --git a/test/core/asmbloc.py b/test/core/asmbloc.py index 26d935b1..45f7f27f 100644 --- a/test/core/asmbloc.py +++ b/test/core/asmbloc.py @@ -2,7 +2,7 @@ from pdb import pm from miasm2.arch.x86.disasm import dis_x86_32 from miasm2.analysis.binary import Container -from miasm2.core.asmbloc import BasicBlocks, asm_constraint, asm_bloc, \ +from miasm2.core.asmbloc import AsmCFG, asm_constraint, asm_bloc, \ asm_label, asm_block_bad, asm_constraint_to, asm_constraint_next, \ bbl_simplifier from miasm2.core.graph import DiGraphSimplifier @@ -32,8 +32,8 @@ assert len(blocks) == 17 assert len(blocks.heads()) == 1 assert len(blocks.heads()[0].lines) == len(first_block.lines) -# Test BasicBlocks -assert isinstance(blocks, BasicBlocks) +# Test AsmCFG +assert isinstance(blocks, AsmCFG) assert len(blocks.pendings) == 0 assert len(blocks.nodes()) == 17 assert len(blocks.edges2constraint) == len(blocks.edges()) @@ -182,7 +182,7 @@ assert len(blocks.pendings) == 1 assert my_block_dst.label in blocks.pendings assert len(blocks.pendings[my_block_dst.label]) == 1 pending = list(blocks.pendings[my_block_dst.label])[0] -assert isinstance(pending, blocks.BasicBlocksPending) +assert isinstance(pending, blocks.AsmCFGPending) assert pending.waiter == my_block_src assert pending.constraint == asm_constraint.c_to ### Sanity check must fail -- cgit 1.4.1