diff options
| author | Fabrice Desclaux <fabrice.desclaux@cea.fr> | 2017-08-06 00:46:32 +0200 |
|---|---|---|
| committer | Fabrice Desclaux <fabrice.desclaux@cea.fr> | 2017-08-08 09:25:56 +0200 |
| commit | 4b200beb1bda68ee94844a81bd9d618da634f8e9 (patch) | |
| tree | 4f37057224ba4a26108a699a43bb740adb072571 /example/expression | |
| parent | 9ada9bd6b907d47b5819a0f96d386578a9da91a1 (diff) | |
| download | miasm-4b200beb1bda68ee94844a81bd9d618da634f8e9.tar.gz miasm-4b200beb1bda68ee94844a81bd9d618da634f8e9.zip | |
Example: update api
Diffstat (limited to 'example/expression')
| -rw-r--r-- | example/expression/access_c.py | 55 | ||||
| -rw-r--r-- | example/expression/expr_c.py | 9 | ||||
| -rw-r--r-- | example/expression/expr_reduce.py | 10 |
3 files changed, 27 insertions, 47 deletions
diff --git a/example/expression/access_c.py b/example/expression/access_c.py index 48da53ff..f285eb55 100644 --- a/example/expression/access_c.py +++ b/example/expression/access_c.py @@ -1,16 +1,3 @@ -import sys - -from miasm2.analysis.machine import Machine -from miasm2.analysis.binary import Container -from miasm2.expression.expression import ExprOp, ExprCompose, ExprId, ExprInt -from miasm2.analysis.depgraph import DependencyGraph - -from miasm2.arch.x86.ctype import CTypeAMD64_unk - -from miasm2.core.objc import CTypeAnalyzer, ExprToAccessC, CHandler -from miasm2.core.objc import CTypesManagerNotPacked -from miasm2.core.ctypesmngr import CAstTypes, CTypePtr, CTypeStruct - """ This example demonstrates the recovering of possible C types for an arbitrary @@ -54,6 +41,20 @@ ExprCompose(var1, 0) => var1 """ +import sys + +from miasm2.analysis.machine import Machine +from miasm2.analysis.binary import Container +from miasm2.expression.expression import ExprOp, ExprCompose, ExprId, ExprInt +from miasm2.analysis.depgraph import DependencyGraph + +from miasm2.arch.x86.ctype import CTypeAMD64_unk + +from miasm2.core.objc import ExprToAccessC, CHandler +from miasm2.core.objc import CTypesManagerNotPacked +from miasm2.core.ctypesmngr import CAstTypes, CTypePtr, CTypeStruct + + def find_call(ira): """Returns (irb, index) which call""" @@ -74,24 +75,10 @@ def find_call(ira): yield irb, index -class MyCTypeAnalyzer(CTypeAnalyzer): - """Custom CTypeAnalyzer to complete type analysis""" - - def reduce_compose(self, node, _): - """Custom reduction rule: {XXX, 0} -> typeof(XXX)""" - if not (isinstance(node.expr, ExprCompose) and - len(node.expr.args) == 2 and - node.expr.args[1].is_int(0)): - return None - return node.args[0].info - - reduction_rules = CTypeAnalyzer.reduction_rules + [reduce_compose] - - class MyExprToAccessC(ExprToAccessC): """Custom ExprToAccessC to complete expression traduction to C""" - def reduce_compose(self, node, _): + def reduce_compose(self, node, **kwargs): """Custom reduction rule: {XXX, 0} -> XXX""" if not (isinstance(node.expr, ExprCompose) and len(node.expr.args) == 2 and @@ -123,7 +110,6 @@ def get_funcs_arg0(ctx, ira, lbl_head): class MyCHandler(CHandler): """Custom CHandler to add complementary C handling rules""" - cTypeAnalyzer_cls = MyCTypeAnalyzer exprToAccessC_cls = MyExprToAccessC @@ -177,11 +163,6 @@ 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 - + for c_str, ctype in mychandler.expr_to_c_and_types(expr): + print '\taccess:', c_str + print '\tc type:', ctype diff --git a/example/expression/expr_c.py b/example/expression/expr_c.py index 7adc7b50..ca92153a 100644 --- a/example/expression/expr_c.py +++ b/example/expression/expr_c.py @@ -41,9 +41,8 @@ types_mngr = CTypesManagerNotPacked(types_ast, base_types) 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) +c_context = {ptr.name: ptr_rectangle} +mychandler = CHandler(types_mngr, {}) # Parse some C accesses c_acceses = ["ptr->width", @@ -55,8 +54,8 @@ c_acceses = ["ptr->width", ] for c_str in c_acceses: - expr = mychandler.c_to_expr(c_str) - c_type = mychandler.c_to_type(c_str) + expr = mychandler.c_to_expr(c_str, c_context) + c_type = mychandler.c_to_type(c_str, c_context) print 'C access:', c_str print '\tExpr:', expr print '\tType:', c_type diff --git a/example/expression/expr_reduce.py b/example/expression/expr_reduce.py index b5fc96c8..bb94ceb9 100644 --- a/example/expression/expr_reduce.py +++ b/example/expression/expr_reduce.py @@ -20,7 +20,7 @@ class StructLookup(ExprReducer): FIELD_A_PTR = "FIELD_A_PTR" FIELD_A = "FIELD_A" - def reduce_int(self, node, _): + def reduce_int(self, node, **kwargs): """ Reduction: int -> CST """ @@ -28,7 +28,7 @@ class StructLookup(ExprReducer): return self.CST return None - def reduce_ptr_struct(self, node, _): + def reduce_ptr_struct(self, node, **kwargs): """ Reduction: ECX -> FIELD_A_PTR """ @@ -36,7 +36,7 @@ class StructLookup(ExprReducer): return self.FIELD_A_PTR return None - def reduce_ptr_plus_int(self, node, _): + def reduce_ptr_plus_int(self, node, **kwargs): """ Reduction: ECX + CST -> FIELD_A_PTR """ @@ -46,7 +46,7 @@ class StructLookup(ExprReducer): return self.FIELD_A_PTR return None - def reduce_cst_op(self, node, _): + def reduce_cst_op(self, node, **kwargs): """ Reduction: CST + CST -> CST """ @@ -56,7 +56,7 @@ class StructLookup(ExprReducer): return self.CST return None - def reduce_at_struct_ptr(self, node, _): + def reduce_at_struct_ptr(self, node, **kwargs): """ Reduction: @FIELD_A_PTR -> FIELD_A """ |