Exemplo n.º 1
0
    def test3 (self):
        """Defined only on the then branch of the if statement"""
        prg1 = "havoc x ; if x > 10 then { x := x + 1; z := 10}  else y := x + 1 ; x := z + 1"
        ast1 = ast.parse_string (prg1)

        uv = undef_visitor.UndefVisitor ()
        uv.check (ast1)
Exemplo n.º 2
0
 def test_havoc(self):
     prg2 = "havoc x, y, z"
     ast2 = ast.parse_string(prg2)
     uv = undef_visitor.UndefVisitor()
     uv.check(ast2)
     #check if there is an empty set
     self.assertEquals(0, len(uv.get_undefs()))
Exemplo n.º 3
0
    def test2 (self):
        """"Variable defined only on the else branch of the if-statement"""
        prg1 = "havoc x ; if x > 10 then y := x + 1 else { x := x+1 ; z := 10 }; x := z + 1"
        ast1 = ast.parse_string (prg1)

        uv = undef_visitor.UndefVisitor ()
        uv.check (ast1)
Exemplo n.º 4
0
 def test_skip(self):
     prg3 = "skip"
     ast3 = ast.parse_string(prg3)
     uv = undef_visitor.UndefVisitor()
     uv.check(ast3)
     #check if there is an empty set
     self.assertEquals(0, len(uv.get_undefs()))
Exemplo n.º 5
0
    def test10 (self):
        """havoc is a definition"""
        prg1 = "havoc x ; y := x"
        ast1 = ast.parse_string (prg1)

        uv = undef_visitor.UndefVisitor ()
        uv.check (ast1)
Exemplo n.º 6
0
    def test1 (self):
        prg1 = "x := 10; y := x + z"
        ast1 = ast.parse_string (prg1)

        uv = undef_visitor.UndefVisitor ()
        uv.check (ast1)
        self.assertEquals (set ([ast.IntVar('z')]), uv.get_undefs ())
Exemplo n.º 7
0
 def test_while(self):
     prg7 = "while true do x := k +3 "
     ast7 = ast.parse_string(prg7)
     uv = undef_visitor.UndefVisitor()
     uv.check(ast7)
     #check if there is an empty set
     self.assertEquals(set([ast.IntVar('k')]), uv.get_undefs())
Exemplo n.º 8
0
 def test_if(self):
     prg6 = "if true then x := z + 5 else x := 6"
     ast6 = ast.parse_string(prg6)
     uv = undef_visitor.UndefVisitor()
     uv.check(ast6)
     #check if there is an empty set
     self.assertEquals(set([ast.IntVar('z')]), uv.get_undefs())
    def test3(self):
        """Defined only on the then branch of the if statement"""
        prg1 = "havoc x ; if x > 10 then { x := x + 1; z := 10}  else y := x + 1 ; x := z + 1"
        ast1 = ast.parse_string(prg1)

        uv = undef_visitor.UndefVisitor()
        uv.check(ast1)
        self.assertEquals(set([ast.IntVar('z')]), uv.get_undefs())
Exemplo n.º 10
0
    def test5 (self):
        """Defined inside body of a loop"""
        prg1 = "while true do x := 1 ; y := x"
        ast1 = ast.parse_string (prg1)

        uv = undef_visitor.UndefVisitor ()
        uv.check (ast1)
        self.assertEquals (set ([ast.IntVar('x')]), uv.get_undefs ())
Exemplo n.º 11
0
    def test6 (self):
        """Undefined use in a conditional"""
        prg1 = "if x > 0 then skip else skip"
        ast1 = ast.parse_string (prg1)

        uv = undef_visitor.UndefVisitor ()
        uv.check (ast1)
        self.assertEquals (set ([ast.IntVar('x')]), uv.get_undefs ())
Exemplo n.º 12
0
    def test7 (self):
        """Undefined use in a loop condition"""
        prg1 = "while x > 0 do skip"
        ast1 = ast.parse_string (prg1)

        uv = undef_visitor.UndefVisitor ()
        uv.check (ast1)
        self.assertEquals (set ([ast.IntVar('x')]), uv.get_undefs ())
    def test10(self):
        """havoc is a definition"""
        prg1 = "havoc x ; y := x"
        ast1 = ast.parse_string(prg1)

        uv = undef_visitor.UndefVisitor()
        uv.check(ast1)
        self.assertEquals(set(), uv.get_undefs())
Exemplo n.º 14
0
    def test4 (self):
        """Use an undefined variable to re-define itself"""
        prg1 = "x:=x+1"
        ast1 = ast.parse_string (prg1)

        uv = undef_visitor.UndefVisitor ()
        uv.check (ast1)
        self.assertEquals (set ([ast.IntVar('x')]), uv.get_undefs ())
Exemplo n.º 15
0
    def test9 (self):
        """Undefined use in assert"""
        prg1 = "assert x > 0"
        ast1 = ast.parse_string (prg1)

        uv = undef_visitor.UndefVisitor ()
        uv.check (ast1)
        self.assertEquals (set ([ast.IntVar('x')]), uv.get_undefs ())
Exemplo n.º 16
0
 def test_assume(self):
     prg5 = "assume x < 3"
     ast5 = ast.parse_string(prg5)
     print ast5
     uv = undef_visitor.UndefVisitor()
     uv.check(ast5)
     #check if there is an empty set
     self.assertEquals(set([ast.IntVar('x')]), uv.get_undefs())
Exemplo n.º 17
0
 def test_assert(self):
     prg4 = "assert x < y"
     ast4 = ast.parse_string(prg4)
     uv = undef_visitor.UndefVisitor()
     uv.check(ast4)
     #check if there is an empty set
     self.assertEquals(set([ast.IntVar('x'),
                            ast.IntVar('y')]), uv.get_undefs())
Exemplo n.º 18
0
    def test_one(self):
        prg1 = "x := 10; y := x + z;havoc a;a:=b+c"
        ast1 = ast.parse_string(prg1)

        uv = undef_visitor.UndefVisitor()
        uv.check(ast1)
        self.assertEquals(
            set([ast.IntVar('z'),
                 ast.IntVar('b'),
                 ast.IntVar('c')]), uv.get_undefs())
Exemplo n.º 19
0
 def test_while_if(self):
     prg1 = "x := 5; " \
        "while x >= 0 do {" \
        "  if false then {" \
        "    if x > 1 then m := 0" \
        "    else m := 1" \
        "  } " \
        "};" \
        "y := m + k"
     ast1 = ast.parse_string(prg1)
     uv = undef_visitor.UndefVisitor()
     uv.check(ast1)
     self.assertEquals({ast.IntVar('m'), ast.IntVar('k')}, uv.get_undefs())
Exemplo n.º 20
0
    def test_two(self):
        prg1 = """havoc x;
					if x > 10 then
						y:=x+1

					else
						z := 10 ;
					x := z + 1"""
        ast1 = ast.parse_string(prg1)

        uv = undef_visitor.UndefVisitor()
        uv.check(ast1)
        self.assertEquals(set([ast.IntVar('z')]), uv.get_undefs())
Exemplo n.º 21
0
    def test_three(self):
        prg1 = """havoc x;
					if x > 10 then
						z:=11

					else
						z := 10 ;
					x := z + 1"""
        ast1 = ast.parse_string(prg1)

        uv = undef_visitor.UndefVisitor()
        uv.check(ast1)
        self.assertEquals(set(), uv.get_undefs())
Exemplo n.º 22
0
    def test_while (self):
        prg1 = """
            x := 10;
            assume x > 0;
            while x > 0 do
                x := x -1;
            print_state
        """
        ast1 = ast.parse_string (prg1)

        uv = undef_visitor.UndefVisitor ()
        uv.check (ast1)
        # UNCOMMENT to run the test
        self.assertEquals (set ([]), uv.get_undefs ())
Exemplo n.º 23
0
    def test_havoc (self):
        prg1 = """
            havoc x, y;
            if x > y then
                x := y
            else
                y := x;
            assert x = z;
            print_state
        """
        ast1 = ast.parse_string (prg1)

        uv = undef_visitor.UndefVisitor ()
        uv.check (ast1)
        # UNCOMMENT to run the test
        self.assertEquals (set ([ast.IntVar('z')]), uv.get_undefs ())
Exemplo n.º 24
0
 def test_undef_visitor(self):
     prg1 = """
             a := 1;
             b := -1;
            if a > 1 and a >= b then b := 3; 
            if a <= 1 or b < 3 then a := c; 
            if false and not a = 1 then a := 1 else e := 2; 
            skip;
            print_state;
            while (a < 3) and true do {a := a + 1};
            assume b = -1;
            havoc c,d;
            assert a = 3;
            d := a + e
         """
     ast1 = ast.parse_string(prg1)
     uv = undef_visitor.UndefVisitor()
     uv.check(ast1)
     self.assertEquals(set([ast.IntVar('c'),
                            ast.IntVar('e')]), uv.get_undefs())
Exemplo n.º 25
0
    def visit_WhileStmt_inv(self, node, *args, **kwargs):
        """" Symbolic execution of while loops with invariants """
        uv = undef_visitor.UndefVisitor()
        uv.check(node.body)
        vars = uv.get_undefs()
        c = node.inv.args[0]
        vars = list(vars)
        st = kwargs['state']
        for i in node.body.stmts:
            c = i

        prg = "assert " + str(node.inv) + ";havoc " + str(vars[0]) + "," + str(
            vars[1]) + "; assume " + str(node.inv.args[0]) + "; if " + str(
                node.cond) + "then {" + str(node.body.stmts[0]) + ";" + str(
                    node.body.stmts[1]) + ";assert " + str(
                        node.inv) + "; assume false}"
        #prg = "assert "+str(node.inv.args[0]);
        ast1 = ast.parse_string(prg)
        for i in self._run_Stmts(ast1.stmts, st):
            yield i
Exemplo n.º 26
0
    def test_one(self):
        prg1 = "x := 10; y := x + z"
        ast1 = ast.parse_string(prg1)

        uv = undef_visitor.UndefVisitor()
        uv.check(ast1)