From 9e79f4338ad7cae991ee73902898e24782874ad5 Mon Sep 17 00:00:00 2001 From: Fabrice Desclaux Date: Mon, 20 Feb 2017 14:41:08 +0100 Subject: Core/Objc: improuve internal type representation --- example/expression/expr_c.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) (limited to 'example/expression/expr_c.py') diff --git a/example/expression/expr_c.py b/example/expression/expr_c.py index b3e59658..e2986c55 100644 --- a/example/expression/expr_c.py +++ b/example/expression/expr_c.py @@ -4,10 +4,9 @@ Parse C expression to access variables and retrieve information: * variable type """ -from miasm2.core.ctypesmngr import CTypesManagerNotPacked +from miasm2.core.ctypesmngr import CTypeStruct, CAstTypes, CTypePtr from miasm2.arch.x86.ctype import CTypeAMD64_unk -from miasm2.core.objc import CHandler -from miasm2.core.objc import ObjCPtr +from miasm2.core.objc import CTypesManagerNotPacked, CHandler from miasm2.expression.expression import ExprId @@ -31,25 +30,22 @@ def test(): """ # Type manager for x86 64: structures not packed - my_types = CTypeAMD64_unk() - types_mngr = CTypesManagerNotPacked(my_types.types) + base_types = CTypeAMD64_unk() + types_ast = CAstTypes() # Add C types definition - types_mngr.add_c_decl(text) + types_ast.add_c_decl(text) - # Create the ptr variable with type "struct rectangle*" - void_ptr = types_mngr.void_ptr - rectangle = types_mngr.get_type(('rectangle',)) - ptr_rectangle = ObjCPtr('noname', rectangle, - void_ptr.align, void_ptr.size) + types_mngr = CTypesManagerNotPacked(types_ast, base_types) + # Create the ptr variable with type "struct rectangle*" + ptr_rectangle = types_mngr.get_objc(CTypePtr(CTypeStruct('rectangle'))) ptr = ExprId('ptr', 64) expr_types = {ptr.name: ptr_rectangle} mychandler = CHandler(types_mngr, expr_types) - # Parse some C accesses c_acceses = ["ptr->width", "ptr->length", -- cgit 1.4.1 From 47fb50d611b5ba8eb7e4dc827b522123642ca3b4 Mon Sep 17 00:00:00 2001 From: Fabrice Desclaux Date: Mon, 15 May 2017 21:25:57 +0200 Subject: Example: clean code --- example/expression/access_c.py | 116 ++++++++++++++++++++--------------------- example/expression/expr_c.py | 86 +++++++++++++++--------------- 2 files changed, 98 insertions(+), 104 deletions(-) (limited to 'example/expression/expr_c.py') diff --git a/example/expression/access_c.py b/example/expression/access_c.py index 8a1e2962..9ba33822 100644 --- a/example/expression/access_c.py +++ b/example/expression/access_c.py @@ -127,62 +127,60 @@ class MyCHandler(CHandler): exprToAccessC_cls = MyExprToAccessC -def test(data): - # Digest C informations - text = """ - struct human { - unsigned short age; - unsigned int height; - char name[50]; - }; - - struct ll_human { - struct ll_human* next; - struct human human; - }; - """ - - base_types = CTypeAMD64_unk() - types_ast = CAstTypes() - types_ast.add_c_decl(text) - - types_mngr = CTypesManagerNotPacked(types_ast, base_types) - - # Analyze binary - cont = Container.fallback_container(data, None, addr=0) - - machine = Machine("x86_64") - dis_engine, ira = machine.dis_engine, machine.ira - - mdis = dis_engine(cont.bin_stream, symbol_pool=cont.symbol_pool) - addr_head = 0 - blocks = mdis.dis_multibloc(addr_head) - lbl_head = mdis.symbol_pool.getby_offset(addr_head) - - ir_arch_a = ira(mdis.symbol_pool) - for block in blocks: - ir_arch_a.add_bloc(block) - - open('graph_irflow.dot', 'w').write(ir_arch_a.graph.dot()) - - # Main function's first argument's type is "struct ll_human*" - ptr_llhuman = types_mngr.get_objc(CTypePtr(CTypeStruct('ll_human'))) - arg0 = ExprId('ptr', 64) - ctx = {ir_arch_a.arch.regs.RDI: arg0} - expr_types = {arg0.name: ptr_llhuman} - - mychandler = MyCHandler(types_mngr, expr_types) - - for expr in get_funcs_arg0(ctx, ir_arch_a, lbl_head): - print "Access:", expr - target_types = mychandler.expr_to_types(expr) - for target_type in target_types: - print '\tType:', target_type - c_strs = mychandler.expr_to_c(expr) - for c_str in c_strs: - print "\tC access:", c_str - print - - -if __name__ == '__main__': - test(open(sys.argv[1]).read()) + +data = open(sys.argv[1]).read() +# Digest C informations +text = """ +struct human { + unsigned short age; + unsigned int height; + char name[50]; +}; + +struct ll_human { + struct ll_human* next; + struct human human; +}; +""" + +base_types = CTypeAMD64_unk() +types_ast = CAstTypes() +types_ast.add_c_decl(text) + +types_mngr = CTypesManagerNotPacked(types_ast, base_types) + +# Analyze binary +cont = Container.fallback_container(data, None, addr=0) + +machine = Machine("x86_64") +dis_engine, ira = machine.dis_engine, machine.ira + +mdis = dis_engine(cont.bin_stream, symbol_pool=cont.symbol_pool) +addr_head = 0 +blocks = mdis.dis_multibloc(addr_head) +lbl_head = mdis.symbol_pool.getby_offset(addr_head) + +ir_arch_a = ira(mdis.symbol_pool) +for block in blocks: + ir_arch_a.add_bloc(block) + +open('graph_irflow.dot', 'w').write(ir_arch_a.graph.dot()) + +# Main function's first argument's type is "struct ll_human*" +ptr_llhuman = types_mngr.get_objc(CTypePtr(CTypeStruct('ll_human'))) +arg0 = ExprId('ptr', 64) +ctx = {ir_arch_a.arch.regs.RDI: arg0} +expr_types = {arg0.name: ptr_llhuman} + +mychandler = MyCHandler(types_mngr, expr_types) + +for expr in get_funcs_arg0(ctx, ir_arch_a, lbl_head): + print "Access:", expr + target_types = mychandler.expr_to_types(expr) + for target_type in target_types: + print '\tType:', target_type + c_strs = mychandler.expr_to_c(expr) + for c_str in c_strs: + print "\tC access:", c_str + print + diff --git a/example/expression/expr_c.py b/example/expression/expr_c.py index e2986c55..7adc7b50 100644 --- a/example/expression/expr_c.py +++ b/example/expression/expr_c.py @@ -10,57 +10,53 @@ from miasm2.core.objc import CTypesManagerNotPacked, CHandler from miasm2.expression.expression import ExprId -def test(): - """ - C manipulation example - """ - - # Digest C informations - text = """ - struct line { - char color[20]; - int size; - }; - - struct rectangle { - unsigned int width; - unsigned int length; - struct line* line; - }; - """ +""" +C manipulation example +""" - # Type manager for x86 64: structures not packed - base_types = CTypeAMD64_unk() - types_ast = CAstTypes() +# Digest C informations +text = """ +struct line { + char color[20]; + int size; +}; + +struct rectangle { + unsigned int width; + unsigned int length; + struct line* line; +}; +""" - # Add C types definition - types_ast.add_c_decl(text) +# Type manager for x86 64: structures not packed +base_types = CTypeAMD64_unk() +types_ast = CAstTypes() - types_mngr = CTypesManagerNotPacked(types_ast, base_types) +# Add C types definition +types_ast.add_c_decl(text) - # Create the ptr variable with type "struct rectangle*" - ptr_rectangle = types_mngr.get_objc(CTypePtr(CTypeStruct('rectangle'))) +types_mngr = CTypesManagerNotPacked(types_ast, base_types) - ptr = ExprId('ptr', 64) - expr_types = {ptr.name: ptr_rectangle} +# Create the ptr variable with type "struct rectangle*" +ptr_rectangle = types_mngr.get_objc(CTypePtr(CTypeStruct('rectangle'))) - mychandler = CHandler(types_mngr, expr_types) +ptr = ExprId('ptr', 64) +expr_types = {ptr.name: ptr_rectangle} - # Parse some C accesses - c_acceses = ["ptr->width", - "ptr->length", - "ptr->line", - "ptr->line->color", - "ptr->line->color[3]", - "ptr->line->size" - ] +mychandler = CHandler(types_mngr, expr_types) - for c_str in c_acceses: - expr = mychandler.c_to_expr(c_str) - c_type = mychandler.c_to_type(c_str) - print 'C access:', c_str - print '\tExpr:', expr - print '\tType:', c_type +# Parse some C accesses +c_acceses = ["ptr->width", + "ptr->length", + "ptr->line", + "ptr->line->color", + "ptr->line->color[3]", + "ptr->line->size" + ] -if __name__ == '__main__': - test() +for c_str in c_acceses: + expr = mychandler.c_to_expr(c_str) + c_type = mychandler.c_to_type(c_str) + print 'C access:', c_str + print '\tExpr:', expr + print '\tType:', c_type -- cgit 1.4.1