def test_decl_1(): co_simple = get_co(SIMPLE_PROGRAM) assert co_simple is not None bytecode_object = BytecodeObject('<string>') bytecode_object.parse_code(co_simple) assert len(bytecode_object.declarations) == 12 assert len([ k for k in bytecode_object.declarations if isinstance(k, MethodDeclaration) and k.is_lambda ]) == 3 assert len([ k for k in bytecode_object.declarations if isinstance(k, TypeDeclaration) ]) == 3 assert len([ k for k in bytecode_object.declarations if isinstance(k, ModuleDeclaration) ]) == 1 module = bytecode_object.main_module assert len(module.imports) == 2
def test_simple_types(): co_simple = get_co(SIMPLE_TYPES_CASE) assert co_simple is not None bytecode_object = BytecodeObject('<string>') bytecode_object.parse_code(co_simple) cfg = ControlFlow(bytecode_object.main_module) types = TypeInference(cfg)
def test_def_use(): co_simple = get_co(DEF_USE_SIMPLE) assert co_simple is not None bytecode_object = BytecodeObject('<string>') bytecode_object.parse_code(co_simple) cfg = ControlFlow(bytecode_object.main_module) def_use = DefUse(cfg)
def test_callgraph(): co_simple = get_co(SIMPLE_PROGRAM) assert co_simple is not None bytecode_object = BytecodeObject('<string>') bytecode_object.parse_code(co_simple) callgraph = CallGraph() callgraph.process(bytecode_object.main_module)
def test_conditions(): logger.debug("test_conditions") co_simple = get_co(CONDITION_CASE) assert co_simple is not None bytecode_object = BytecodeObject('<string>') bytecode_object.parse_code(co_simple) for decl in bytecode_object.declarations: cflow = ControlFlow(decl) cflow.block_constraints
def test_bz_1_reproducer(): co_simple = get_co(REDUCED_TC) assert co_simple is not None bytecode_object = BytecodeObject('<string>') bytecode_object.parse_code(co_simple) for decl in bytecode_object.declarations: cflow = ControlFlow(decl) assert len(cflow.graph.roots()) == 1 logger.debug("cflow := \n%s", cflow.graph.to_dot()) doms = cflow.dominators
def test_if_statements(): co_simple = get_co(IF_STMTS_CASE) assert co_simple is not None bytecode_object = BytecodeObject('<string>') bytecode_object.parse_code(co_simple) assert len(bytecode_object.declarations) == 1 for decl in bytecode_object.declarations: cflow = ControlFlow(decl) assert cflow.blocks is not None assert len(cflow.dominators.dom) > 0
def test_cflow1(): co_simple = get_co(SIMPLE_PROGRAM) assert co_simple is not None bytecode_object = BytecodeObject('<string>') bytecode_object.parse_code(co_simple) assert len(bytecode_object.declarations) == 9 for decl in bytecode_object.declarations: cflow = ControlFlow(decl) assert cflow.blocks is not None assert len(cflow.dominators.dom) > 0
def test_loop_breaks(): logger.debug("test_loop_breaks") co_simple = get_co(LOOP_BREAK_CASE) assert co_simple is not None bytecode_object = BytecodeObject('<string>') bytecode_object.parse_code(co_simple) assert len(bytecode_object.declarations) == 2 for decl in bytecode_object.declarations: cflow = ControlFlow(decl) assert cflow.blocks is not None assert len(cflow.dominators.dom) > 0
def test_list_comprehension(): logger.debug("test_list_comprehension") co_simple = get_co(LIST_COMP_CASE) assert co_simple is not None bytecode_object = BytecodeObject('<string>') bytecode_object.parse_code(co_simple) for decl in bytecode_object.declarations: cflow = ControlFlow(decl) for block in cflow.blocks: for stmt in block.statements: if stmt.native is not None: logger.debug("%s", stmt)
def test_constraint_equivalence(): logger.debug("test_conditions") co_simple = get_co(CONSTRAINT_EQ_CASE) assert co_simple is not None bytecode_object = BytecodeObject('<string>') bytecode_object.parse_code(co_simple) for decl in bytecode_object.declarations: cflow = ControlFlow(decl) all_constraints = list() for block in cflow.block_constraints: logger.debug("Cstr: %s", cflow.block_constraints[block].tree) all_constraints.append(cflow.block_constraints[block].tree) for cstr1, cstr2 in get_pairs(all_constraints): assert cstr1 == cstr2
def test_decl_1(): co_simple = get_co(SIMPLE_PROGRAM) assert co_simple is not None bytecode_object = BytecodeObject('<string>') bytecode_object.parse_code(co_simple) assert len(bytecode_object.declarations) == 12 assert len([k for k in bytecode_object.declarations if isinstance(k, MethodDeclaration) and k.is_lambda]) == 3 assert len([k for k in bytecode_object.declarations if isinstance(k, TypeDeclaration)]) == 3 assert len([k for k in bytecode_object.declarations if isinstance(k, ModuleDeclaration)]) == 1 module = bytecode_object.main_module assert len(module.imports) == 2
def test_inheritance(): co_simple = get_co(INHERITANCE_CASE) assert co_simple is not None bytecode_object = BytecodeObject('<string>') bytecode_object.parse_code(co_simple) TEST_CASE = { 'Base': 0, 'OtherBase': 0, 'NewBase': 0, 'Child1': 1, 'Child2': 3 } for decl in iter_decl(bytecode_object.main_module): if not isinstance(decl, TypeDeclaration): continue assert len(decl.superclasses) == TEST_CASE[decl.type_name]
def test_ast(): co_simple = get_co(SIMPLE_PROGRAM) assert co_simple is not None bytecode_object = BytecodeObject('<string>') bytecode_object.parse_code(co_simple) assert len(bytecode_object.declarations) == 10 for decl in bytecode_object.declarations: cflow = ControlFlow(decl) assert cflow.blocks is not None for block in cflow.blocks: for stmt in block.statements: if stmt.native is not None: logger.debug("%s", stmt) logger.debug("%s", dump(stmt.native))
def test_block_visitor(): co_simple = get_co(SIMPLE_PROGRAM) assert co_simple is not None bytecode_object = BytecodeObject('<string>') bytecode_object.parse_code(co_simple) class BlockPrinterVisitor(BlockVisitor): def __init__(self): BlockVisitor.__init__(self) def new_control_flow(self): logger.debug("Received new CFG: %s", self.control_flow) def visit(self, block): logger.debug("Visiting block: %s", block) logger.debug('\n' + show_bytecode(block.bytecode)) visitor = BlockPrinterVisitor() bytecode_object.accept(visitor)
def test_cflow1(): co_simple = get_co(SIMPLE_PROGRAM) assert co_simple is not None bytecode_object = BytecodeObject('<string>') bytecode_object.parse_code(co_simple) assert len(bytecode_object.declarations) == 11 for decl in bytecode_object.declarations: cflow = ControlFlow(decl) assert cflow.blocks is not None assert len(cflow.graph.roots()) == 1 assert len(cflow.dominators.dom) > 0 cflow.block_constraints is not None for block in cflow.block_constraints: cstr = cflow.block_constraints[block] assert cstr.tree is not None logger.debug("Constraint: %s", cstr) cdg = cflow.control_dependence
def test_parse_calls(): co_simple = get_co(CALL_STRUCTURES_CASE) assert co_simple is not None bytecode_object = BytecodeObject('<string>') bytecode_object.parse_code(co_simple) callgraph = CallGraph() callgraph.process(bytecode_object.main_module) block_calls = callgraph.block_calls all_names = set() all_calls = set() for block in block_calls: for index in block_calls[block]: all_calls.add(index) all_names.add('.'.join(block_calls[block][index])) assert len(all_names) == 1 assert len(all_calls) == 9
def test_nested_methods_lambdas(): co_simple = get_co(NESTED_METHS_PROGRAM) assert co_simple is not None bytecode_object = BytecodeObject('<string>') bytecode_object.parse_code(co_simple) assert len(bytecode_object.declarations) == 14 nested_1_decl = bytecode_object.get_decl(method_name='nested_1') assert nested_1_decl is not None assert len(nested_1_decl.children) == 2 one_method_decl = bytecode_object.get_decl(method_name='one_method') assert one_method_decl is not None assert len(one_method_decl.children) == 3 nested_1_2_decl = bytecode_object.get_decl(method_name='nested_1_2') assert nested_1_2_decl is not None assert len(nested_1_2_decl.children) == 0