def symex_step(self, art): # Pick out src obj gss = art.get_cur_state().get_active_state() src = gss.source # pc becomes an insn number, lookup insns. Select current insn insns = src.prog.get_instructions() localpc = src.pc - insns[0].location_number insn = src.prog.get_instructions()[localpc] normal = True if insn.type == gptypes.FUNCTION_CALL: # Pick out function calls call = esbmc.downcast_expr(insn.code) sym = esbmc.downcast_expr(call.function) # Pick out the desired function if sym.name.as_string() == 'c::foobar': # Build a constant for 'one' bigint = esbmc.BigInt(1) ubv = esbmc.type.unsignedbv.make(32) one = esbmc.expr.constant_int.make(ubv, bigint) # Assign one to the given return symbol exobj.symex_assign_symbol(call.return_sym, one, gss.guard) # Increment the program counter _past_ the call we just # interpreted, set normal to indicate this shouldn't be # interpreted by the usual symex_step operation. src.pc += 1 normal = False if normal: super(ExState2, self).symex_step(art)
def test_iter_detach(self): import esbmc # Build a nontrivial expr foo = self.make_int() ubv = self.make_unsigned() idt = esbmc.irep_idt("fgasfd") lev = esbmc.expr.symbol_renaming.level0 sym = esbmc.expr.symbol.make(ubv, idt, lev, 0, 0, 0, 0) add = esbmc.expr.add.make(ubv, foo, sym) addclone = add.clone() it = iter(addclone) obj1 = next(it) self.assertTrue(obj1 == foo, "Object iterator obj1 not as expected") obj2 = next(it) self.assertTrue(obj2 == sym, "Object iterator obj2 not as expected") try: next(it) self.assertTrue( False, "Object iterator should not have completed 3rd time") except StopIteration: pass obj2 = esbmc.downcast_expr(obj2) self.assertTrue(esbmc.expr.expr_ids.symbol == obj2.expr_id, "Downcasted symbol should be sym id'd") obj2.iknowwhatimdoing_level2_num = 1 self.assertTrue(obj2.level2_num != sym.level2_num, "Iterated object should have detached from original")
def test_iter_detach(self): import esbmc # Build a nontrivial expr foo = self.make_int() ubv = self.make_unsigned() idt = esbmc.irep_idt("fgasfd") lev = esbmc.expr.symbol_renaming.level0 sym = esbmc.expr.symbol.make(ubv, idt, lev, 0, 0, 0, 0) add = esbmc.expr.add.make(ubv, foo, sym) addclone = add.clone() it = iter(addclone) obj1 = next(it) self.assertTrue(obj1 == foo, "Object iterator obj1 not as expected") obj2 = next(it) self.assertTrue(obj2 == sym, "Object iterator obj2 not as expected") try: next(it) self.assertTrue(False, "Object iterator should not have completed 3rd time") except StopIteration: pass obj2 = esbmc.downcast_expr(obj2) self.assertTrue(esbmc.expr.expr_ids.symbol == obj2.expr_id, "Downcasted symbol should be sym id'd") obj2.iknowwhatimdoing_level2_num = 1 self.assertTrue(obj2.level2_num != sym.level2_num, "Iterated object should have detached from original")
def test_first_insn(self): import esbmc theinsn = self.insns[0] self.assertTrue(theinsn.type == esbmc.goto_programs.goto_program_instruction_type.OTHER, "Wrong insn type") code = esbmc.downcast_expr(theinsn.code) self.assertTrue(code.expr_id == esbmc.expr.expr_ids.code_decl, "decl insn has wrong expr type") self.assertTrue(code.value.as_string() == "main::main::1::i", "decl insn has wrong expr value") self.assertTrue(theinsn.function.as_string() == "main", "decl insn has wrong function name")
def test_downcast(self): import esbmc val = self.make_int(1) val1 = self.make_int(2) add = esbmc.expr.add.make(val.type, val, val1) # Fields should all have type expr2t self.assertTrue(type(add.side_1) == esbmc.expr.expr2t, "Wrong field type in expr") downcasted = esbmc.downcast_expr(add.side_1) self.assertTrue(type(downcasted) == esbmc.expr.constant_int, "Downcast failed") # Should be brought into python as a different object self.assertFalse(downcasted is val, "downcasted object should be new pyref")
def test_first_insn(self): import esbmc theinsn = self.insns[0] self.assertTrue( theinsn.type == esbmc.goto_programs.goto_program_instruction_type.OTHER, "Wrong insn type") code = esbmc.downcast_expr(theinsn.code) self.assertTrue(code.expr_id == esbmc.expr.expr_ids.code_decl, "decl insn has wrong expr type") self.assertTrue(code.value.as_string() == "main::main::1::i", "decl insn has wrong expr value") self.assertTrue(theinsn.function.as_string() == "main", "decl insn has wrong function name")
def test_sym_compare(self): import esbmc # Build an expr we indirectly access and a symbol foo = self.make_int() ubv = self.make_unsigned() add = esbmc.expr.add.make(ubv, foo, foo) idt = esbmc.irep_idt("fgasfd") lev = esbmc.expr.symbol_renaming.level0 sym = esbmc.expr.symbol.make(ubv, idt, lev, 0, 0, 0, 0) # Problem: when we downcast this b.p knows that 'sym' is a symbol2tc. # But it doesn't know for some reason that it can just cast that to # a expr2tc, via inheritance. Test for this -- __eq__ returns # NotImplemented when the operator== call construction fails. sym = esbmc.downcast_expr(sym) foo2 = add.side_1 self.assertFalse(foo2.__eq__(sym) == NotImplemented, "Downcasted expr should be castable to expr2tc")
def test_more_fields(self): import esbmc theinsn = self.insns[0] self.assertTrue(esbmc.downcast_expr(theinsn.guard).value == True, "insn guard should be true") # No access to labels field right no # This has the value 99 right now, but we can't really assert that # because it'll change when we edit... anything self.assertTrue(theinsn.location_number > 0, "Wrong insn number") self.assertTrue(theinsn.loop_number == 0, "Loop number doesn't exist?") # This seems to be a useless field self.assertTrue(theinsn.target_number == 4294967295, "Target number doesn't exist?") norm = theinsn.to_string() wloc = theinsn.to_string(True) self.assertTrue(norm != wloc, "var changing flags should change to_string output") self.assertTrue(theinsn.is_other(), "is_other method of insn should work")
def test_more_fields(self): import esbmc theinsn = self.insns[0] self.assertTrue( esbmc.downcast_expr(theinsn.guard).value == True, "insn guard should be true") # No access to labels field right no # This has the value 99 right now, but we can't really assert that # because it'll change when we edit... anything self.assertTrue(theinsn.location_number > 0, "Wrong insn number") self.assertTrue(theinsn.loop_number == 0, "Loop number doesn't exist?") # This seems to be a useless field self.assertTrue(theinsn.target_number == 4294967295, "Target number doesn't exist?") norm = theinsn.to_string() wloc = theinsn.to_string(True) self.assertTrue(norm != wloc, "var changing flags should change to_string output") self.assertTrue(theinsn.is_other(), "is_other method of insn should work")
def test_downcast_none(self): import esbmc downcasted = esbmc.downcast_expr(None) self.assertTrue(downcasted == None, "Downcast of none should be none")