def test_12(self):
        ''' takes long time test > in while'''
        prg1 = "havoc x; while x > 4 do {p:=1}"
        ast1 = ast.parse_string(prg1)
        sym = wlang.sym.SymExec()
        st = wlang.sym.SymState()
        out = [s for s in sym.run(ast1, st)]

        if out is None:
            print('[symexec]: no output states')
        else:
            count = 0
            for state in out:
                count = count + 1
                print('[symexec]: symbolic state reached')
                #print (state)
            print('[symexec]: found', count, 'symbolic states')
        self.assertEquals(len(out), 1)
    def test_13(self):
        ''' takes long time test <><=>= = in while'''
        prg1 = "havoc x,y,s,q; while x <16 do {x:=x+2}; s:=3;while s <= 9 do {s:=s+2}; y:=0 while y >= -9 do {y:=y-3}; q:=10;while q = 4 do {s:=1}; while q < 16 do {q:=q+2};assert x>10"
        ast1 = ast.parse_string(prg1)
        sym = wlang.sym.SymExec()
        st = wlang.sym.SymState()
        out = [s for s in sym.run(ast1, st)]

        if out is None:
            print('[symexec]: no output states')
        else:
            count = 0
            for state in out:
                count = count + 1
                print('[symexec]: symbolic state reached')
                #print (state)
            print('[symexec]: found', count, 'symbolic states')
        self.assertEquals(len(out), 11)
Beispiel #3
0
    def test_07(self):
        prg1 = """
            havoc x, y;
            if x >= y or x <= y then
              z := x
            else
              z := y;

            if true or x > y then
                 z := x
               else
                 z := y
        """
        ast1 = ast.parse_string(prg1)
        sym = wlang.sym.SymExec()
        st = wlang.sym.SymState()
        out = [s for s in sym.run(ast1, st)]
        self.assertEquals(len(out), 1)
Beispiel #4
0
 def test_arithmetic_exp(self):
     prog = """
         x := ((5 * 2) + 5) - ((-20 / 2) + 5);
         print_state
     """
     # test parser
     ast1 = ast.parse_string(prog)
     printed = str(ast1)
     interp = wlang.int.Interpreter()
     st = wlang.int.State()
     st = interp.run(ast1, st)
     self.assertIsNotNone(st)
     # x is defined
     self.assertIn('x', st.env)
     # x is 10
     self.assertEquals(st.env['x'], 20)
     # no other variables in the state
     self.assertEquals(len(st.env), 1)
Beispiel #5
0
 def test_havoc_stmt(self):
     prog = """
         havoc x, y;
         print_state
     """
     # test parser
     ast1 = ast.parse_string(prog)
     printed = str(ast1)
     interp = wlang.int.Interpreter()
     st = wlang.int.State()
     st = interp.run(ast1, st)
     self.assertIsNotNone(st)
     # x is defined
     self.assertIn('x', st.env)
     self.assertIn('y', st.env)
     # x is 10
     # no other variables in the state
     self.assertEquals(len(st.env), 2)
Beispiel #6
0
 def test_assume_stmt(self):
     prog = """
         x := 10;
         assume x = 10
     """
     # test parser
     ast1 = ast.parse_string(prog)
     printed = str(ast1)
     interp = wlang.int.Interpreter()
     st = wlang.int.State()
     st = interp.run(ast1, st)
     self.assertIsNotNone(st)
     # x is defined
     self.assertIn('x', st.env)
     # x is 10
     self.assertEquals(st.env['x'], 10)
     # no other variables in the state
     self.assertEquals(len(st.env), 1)
Beispiel #7
0
 def test_two(self):
     #Here we covered while statement,subtraction,multilication,addition,skip,block statements, statements list, assignment,print
     prg1 = "x:=1;y:=((x*100)/x)+1;if(x=1) then skip;while(x=1) do {res:=1;x:=x-1}; print_state"
     # test parser
     ast1 = ast.parse_string(prg1)
     interp = wlang.int.Interpreter()
     st = wlang.int.State()
     st = interp.run(ast1, st)
     self.assertIsNotNone(st)
     # x,y,res is defined
     self.assertIn('x', st.env)
     self.assertIn('res', st.env)
     self.assertIn('y', st.env)
     # x is 0 , res is 1,y is 101
     self.assertEquals(st.env['x'], 0)
     self.assertEquals(st.env['y'], 101)
     self.assertEquals(st.env['res'], 1)
     # no other variables in the state
     self.assertEquals(len(st.env), 3)
Beispiel #8
0
 def test_one(self):
     #Here we covered block statements, statements list, assignment, boolean Expression, if statement, print,bfactor,batom,relational operators
     prg1 = "{x := 10;y:=5};{if(x>y or x=10) then res:=1 else skip ;print_state}"
     # test parser
     ast1 = ast.parse_string(prg1)
     interp = wlang.int.Interpreter()
     st = wlang.int.State()
     st = interp.run(ast1, st)
     self.assertIsNotNone(st)
     # x,y,res is defined
     self.assertIn('x', st.env)
     self.assertIn('y', st.env)
     self.assertIn('res', st.env)
     # x is 10 , y is 5 , res is 1
     self.assertEquals(st.env['x'], 10)
     self.assertEquals(st.env['y'], 5)
     self.assertEquals(st.env['res'], 1)
     # no other variables in the state
     self.assertEquals(len(st.env), 3)
Beispiel #9
0
 def test_five(self):
     #Here we covered both branches of if statement ,along with various elements
     prg1 = "{x := 10;y:=5};{if(x>y or x=10) then res:=1 else skip ;if(x<10) then skip else res:=1;print_state}"
     # test parser
     ast1 = ast.parse_string(prg1)
     interp = wlang.int.Interpreter()
     st = wlang.int.State()
     st = interp.run(ast1, st)
     self.assertIsNotNone(st)
     # x,y,res is defined
     self.assertIn('x', st.env)
     self.assertIn('y', st.env)
     self.assertIn('res', st.env)
     # x is 10 , y is 5 , res is 1
     self.assertEquals(st.env['x'], 10)
     self.assertEquals(st.env['y'], 5)
     self.assertEquals(st.env['res'], 1)
     # no other variables in the state
     self.assertEquals(len(st.env), 3)
 def test_stats_visitor(self):
     prg1 = """{a := 1;
           b := -1;
           c := a + b; d := b - a;
           e := a * b; f := a / b};
           if a > 1 and a >= b then b := 3; 
           if a <= 1 or b < 3 then a := 1; 
           if false and not a = 1 then a := 1 else print_state; 
           skip;
           print_state;
           while (a < 3) and true do {a := a + 1};
           assume b = -1;
           havoc c,d;
           assert a = 3"""
     ast1 = ast.parse_string(prg1)
     sv = stats_visitor.StatsVisitor()
     sv.visit(ast1)
     self.assertEquals(sv.get_num_stmts(), 20)
     self.assertEquals(sv.get_num_vars(), 6)
Beispiel #11
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
Beispiel #12
0
 def test_10(self):
     prg1 = """
         havoc x, y;
         assume y >= 0;
         c := 0;
         r := x;
         while c < y
         inv c < y and r = x + c
         do
         {
           r := r + 1;
           c := c + 1
         };
         assert r = x + y
     """
     ast1 = ast.parse_string(prg1)
     sym = wlang.sym.SymExec()
     st = wlang.sym.SymState()
     out = [s for s in sym.run(ast1, st)]
     self.assertEquals(len(out), 0)
Beispiel #13
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())
Beispiel #14
0
    def test_04(self):
        prg1 = """
            x:=0;
            y:=0;
            z:=0;
            havoc a, b, c;

            if not a = 0 then {
              x := -2
            };

            if b < 5 then {
              if a = 0 and not(c = 0) then
                y := 1;
              z := 2
            }
        """
        ast1 = ast.parse_string(prg1)
        sym = wlang.sym.SymExec()
        st = wlang.sym.SymState()
        out = [s for s in sym.run(ast1, st)]
        self.assertEquals(len(out), 5)
 def test_three(self):
     ''' takes long time test */'''
     prg1 = "havoc x; r:=0;if x>8 then r:=x-7 else r:=x-5; if x>5 then r:=x*2 else r:=x/3"
     ast1 = ast.parse_string(prg1)
     sym = wlang.sym.SymExec()
     st = wlang.sym.SymState()
     out = [s for s in sym.run(ast1, st)]
     list_result = []
     if out is None:
         print('[symexec]: no output states')
     else:
         count = 0
         for state in out:
             count = count + 1
             print('[symexec]: symbolic state reached')
             #print (state)
             result = state.pick_concerete()
             list_result.append(result)
         print('[symexec]: found', count, 'symbolic states')
     #for result in list_result:
     #  print(result)
     self.assertEquals(len(out), 3)
 def test_two(self):
     ''' takes short time assume and assert'''
     prg1 = "havoc x;assume x > 10;assert x > 15"
     ast1 = ast.parse_string(prg1)
     sym = wlang.sym.SymExec()
     st = wlang.sym.SymState()
     out = [s for s in sym.run(ast1, st)]
     list_result = []
     if out is None:
         print('[symexec]: no output states')
     else:
         count = 0
         for state in out:
             count = count + 1
             print('[symexec]: symbolic state reached')
             #print (state)
             result = state.pick_concerete()
             list_result.append(result)
         print('[symexec]: found', count, 'symbolic states')
     #for result in list_result:
     # print(result)
     self.assertEquals(len(out), 1)
 def test_14(self):
     ''' takes long time test if and <><= >= ='''
     prg1 = "havoc x; if 8<x then r:=x-7 else r:=x-5;if 10>x then r:=x-7;if 5>=x then r:=x-7;if x<=10 then r:=x-7 else r:=x-5;if 12<=x then r:=x-7;if x>=8 then r:=x-7 else r:=x-5;if x<12 then r:=x-7;if x<8 then r:=x-7 else r:=x-5;if 10=x then r:=x-7;if x=8 then r:=x-7 else r:=x-5"
     ast1 = ast.parse_string(prg1)
     sym = wlang.sym.SymExec()
     st = wlang.sym.SymState()
     out = [s for s in sym.run(ast1, st)]
     list_result = []
     if out is None:
         print('[symexec]: no output states')
     else:
         count = 0
         for state in out:
             count = count + 1
             print('[symexec]: symbolic state reached')
             #print (state)
             result = state.pick_concerete()
             list_result.append(result)
         print('[symexec]: found', count, 'symbolic states')
     #for result in list_result:
     #print(result)
     self.assertEquals(len(out), 7)
 def test_four(self):
     ''' takes short time test ???'''
     prg1 = "x:=10; while x > 4 do {x:=x-3};if x>8 then r:=x+7 else r:=x+5;if x<8 then s:=1;if x>=8 then s:=1;if x=8 then s:=1;if x<=8 then s:=1"
     ast1 = ast.parse_string(prg1)
     sym = wlang.sym.SymExec()
     st = wlang.sym.SymState()
     out = [s for s in sym.run(ast1, st)]
     list_result = []
     if out is None:
         print('[symexec]: no output states')
     else:
         count = 0
         for state in out:
             count = count + 1
             print('[symexec]: symbolic state reached')
             #print (state)
             result = state.pick_concerete()
             list_result.append(result)
         print('[symexec]: found', count, 'symbolic states')
     #for result in list_result:
     #   print(result)
     self.assertEquals(len(out), 1)
 def test_nine(self):
     ''' takes ??? time test assume '''
     prg1 = "havoc x,y,z,e; assume x>10;assume y<5;assume x<=20;assume y>=2;assume z=1;skip;print_state"
     ast1 = ast.parse_string(prg1)
     sym = wlang.sym.SymExec()
     st = wlang.sym.SymState()
     out = [s for s in sym.run(ast1, st)]
     list_result = []
     if out is None:
         print('[symexec]: no output states')
     else:
         count = 0
         for state in out:
             count = count + 1
             print('[symexec]: symbolic state reached')
             #print (state)
             result = state.pick_concerete()
             list_result.append(result)
         print('[symexec]: found', count, 'symbolic states')
     #for result in list_result:
     #print(result)
     self.assertEquals(len(out), 1)
 def test_eleven(self):
     ''' takes short ime test not '''
     prg1 = "x := 10; { p := 25; x := 1; n := 5 };  if not x>n then p :=10 "
     ast1 = ast.parse_string(prg1)
     sym = wlang.sym.SymExec()
     st = wlang.sym.SymState()
     out = [s for s in sym.run(ast1, st)]
     list_result = []
     if out is None:
         print('[symexec]: no output states')
     else:
         count = 0
         for state in out:
             count = count + 1
             print('[symexec]: symbolic state reached')
             #print (state)
             result = state.pick_concerete()
             list_result.append(result)
         print('[symexec]: found', count, 'symbolic states')
     #for result in list_result:
     #print(result)
     self.assertEquals(len(out), 1)
 def test_eight(self):
     ''' takes short time test assert ><=<>'''
     prg1 = "havoc x,y,z,e; if x > 4 then assert x<2 else assert x<5;if y > 4 then assert y>2 else assert y>=5;if z > 1 then assert z<=2 else assert z=5"
     ast1 = ast.parse_string(prg1)
     sym = wlang.sym.SymExec()
     st = wlang.sym.SymState()
     out = [s for s in sym.run(ast1, st)]
     list_result = []
     if out is None:
         print('[symexec]: no output states')
     else:
         count = 0
         for state in out:
             count = count + 1
             print('[symexec]: symbolic state reached')
             #print (state)
             result = state.pick_concerete()
             list_result.append(result)
         print('[symexec]: found', count, 'symbolic states')
     #for result in list_result:
     #print(result)
     self.assertEquals(len(out), 1)
Beispiel #22
0
 def test_08(self):
     prg1 = """
         havoc x, y;
         assume y >= 0;
         c := 0;
         r := x;
         while c < y
         inv c <= y and r = x + c
         do
         {
           r := r + 1;
           c := c + 1
         };
         assert r = x + y
     """
     ast1 = ast.parse_string(prg1)
     sym = wlang.sym.SymExec()
     st = wlang.sym.SymState()
     out = [s for s in sym.run(ast1, st)]
     self.assertEquals(
         reduce((lambda x, y: x or y), map((lambda x: x.is_error()), out)),
         False)
     self.assertEquals(len(out), 1)
 def test_five(self):
     ''' takes long time test <=>= in if'''
     prg1 = "x:=10; if x>8 then r:=x-7 else r:=x-5;if x>10 then r:=x-7;if x<=5 then r:=x-7;if x<=10 then r:=x-7 else r:=x-5;if x>=12 then r:=x-7;if x>=8 then r:=x-7 else r:=x-5;if x<12 then r:=x-7;if x<8 then r:=x-7 else r:=x-5;if x=10 then r:=x-7;if x=8 then r:=x-7 else r:=x-5"
     ast1 = ast.parse_string(prg1)
     sym = wlang.sym.SymExec()
     st = wlang.sym.SymState()
     out = [s for s in sym.run(ast1, st)]
     list_result = []
     if out is None:
         print('[symexec]: no output states')
     else:
         count = 0
         for state in out:
             count = count + 1
             print('[symexec]: symbolic state reached')
             #print (state)
             print(state.__repr__())
             result = state.pick_concerete()
             list_result.append(result)
         print('[symexec]: found', count, 'symbolic states')
     #for result in list_result:
     #print(result)
     self.assertEquals(len(out), 1)
Beispiel #24
0
 def test_if_stmt(self):
     prog = """
         if true or true then
             x := 10;
         if false and true then
             x := 10;
         if not true and true then
             x := 10
         else
             x := 0
     """
     # test parser
     ast1 = ast.parse_string(prog)
     printed = str(ast1)
     interp = wlang.int.Interpreter()
     st = wlang.int.State()
     st = interp.run(ast1, st)
     self.assertIsNotNone(st)
     # x is defined
     self.assertIn('x', st.env)
     # x is 10
     self.assertEquals(st.env['x'], 0)
     # no other variables in the state
     self.assertEquals(len(st.env), 1)
Beispiel #25
0
 def test_while2(self):
     prg2 = "{ p := 0; x := 1; n :=3 }; while x <= n do { x := x+1; p := p + n }"
     ast2 = ast.parse_string(prg2)
     print ast2
     interp2 = wlang.int.Interpreter()
     print repr(ast2)
     print str(ast2)
     self.assertTrue(ast2 == ast2)
     self.assertTrue(ast2.stmts[1] == ast2.stmts[1])
     #create a state
     st = wlang.int.State()
     #run ast2 under the state
     st = interp2.run(ast2, st)
     self.assertIsNotNone(st)
     # x, p, n are defined (state environment)
     self.assertIn('x', st.env)
     self.assertIn('p', st.env)
     self.assertIn('n', st.env)
     # x is 4
     self.assertEquals(st.env['x'], 4)
     self.assertEquals(st.env['p'], 9)
     self.assertEquals(st.env['n'], 3)
     # no other variables in the state
     self.assertEquals(len(st.env), 3)
Beispiel #26
0
 def test_Havoc_My(self):
     prg2 = "havoc x"
     ast2 = ast.parse_string(prg2)
     sv = MyVisitorAst2()
     sv.visit(ast2)
 def test_pick_concrete(self):
     prg1 = "x :=  1; while x < 5 do {x := x + 1}"
     ast1 = ast.parse_string(prg1)
     sym = wlang.sym.SymExec()
     st = wlang.sym.SymState(z3.Solver())
     self.assertIsNotNone(st.pick_concrete())
Beispiel #28
0
 def test_While_My_Parser(self):
     prg2 = "while true do x := 99"
     ast2 = ast.parse_string(prg2)
     testLang = parser.WhileLangSemantics()
     self.assertEquals(parser.WhileLangSemantics.start(testLang, ast2),
                       ast2)
     self.assertEquals(parser.WhileLangSemantics.stmt_list(testLang, ast2),
                       ast2)
     self.assertEquals(parser.WhileLangSemantics.stmt(testLang, ast2), ast2)
     self.assertEquals(parser.WhileLangSemantics.asgn_stmt(testLang, ast2),
                       ast2)
     self.assertEquals(parser.WhileLangSemantics.block_stmt(testLang, ast2),
                       ast2)
     self.assertEquals(parser.WhileLangSemantics.skip_stmt(testLang, ast2),
                       ast2)
     self.assertEquals(
         parser.WhileLangSemantics.print_state_stmt(testLang, ast2), ast2)
     self.assertEquals(parser.WhileLangSemantics.if_stmt(testLang, ast2),
                       ast2)
     self.assertEquals(parser.WhileLangSemantics.while_stmt(testLang, ast2),
                       ast2)
     self.assertEquals(
         parser.WhileLangSemantics.assert_stmt(testLang, ast2), ast2)
     self.assertEquals(
         parser.WhileLangSemantics.assume_stmt(testLang, ast2), ast2)
     self.assertEquals(parser.WhileLangSemantics.havoc_stmt(testLang, ast2),
                       ast2)
     self.assertEquals(parser.WhileLangSemantics.bexp(testLang, ast2), ast2)
     self.assertEquals(parser.WhileLangSemantics.bterm(testLang, ast2),
                       ast2)
     self.assertEquals(parser.WhileLangSemantics.bfactor(testLang, ast2),
                       ast2)
     self.assertEquals(parser.WhileLangSemantics.batom(testLang, ast2),
                       ast2)
     self.assertEquals(parser.WhileLangSemantics.var_list(testLang, ast2),
                       ast2)
     self.assertEquals(parser.WhileLangSemantics.rexp(testLang, ast2), ast2)
     self.assertEquals(parser.WhileLangSemantics.rop(testLang, ast2), ast2)
     self.assertEquals(parser.WhileLangSemantics.aexp(testLang, ast2), ast2)
     self.assertEquals(parser.WhileLangSemantics.addition(testLang, ast2),
                       ast2)
     self.assertEquals(
         parser.WhileLangSemantics.subtraction(testLang, ast2), ast2)
     self.assertEquals(parser.WhileLangSemantics.term(testLang, ast2), ast2)
     self.assertEquals(parser.WhileLangSemantics.mult(testLang, ast2), ast2)
     self.assertEquals(parser.WhileLangSemantics.division(testLang, ast2),
                       ast2)
     self.assertEquals(parser.WhileLangSemantics.neg_number(testLang, ast2),
                       ast2)
     self.assertEquals(parser.WhileLangSemantics.atom(testLang, ast2), ast2)
     self.assertEquals(parser.WhileLangSemantics.name(testLang, ast2), ast2)
     self.assertEquals(parser.WhileLangSemantics.number(testLang, ast2),
                       ast2)
     self.assertEquals(parser.WhileLangSemantics.INT(testLang, ast2), ast2)
     self.assertEquals(parser.WhileLangSemantics.NAME(testLang, ast2), ast2)
     self.assertEquals(parser.WhileLangSemantics.NEWLINE(testLang, ast2),
                       ast2)
     self.assertEquals(parser.WhileLangSemantics.factor(testLang, ast2),
                       ast2)
     self.assertEquals(parser.WhileLangSemantics.bool_const(testLang, ast2),
                       ast2)
     sv = MyVisitorAst2()
     sv.visit(ast2)
Beispiel #29
0
 def test_Print_My(self):
     prg2 = "print_state"
     ast2 = ast.parse_string(prg2)
     sv = MyVisitorAst2()
     sv.visit(ast2)
Beispiel #30
0
 def test_Assert_My(self):
     prg2 = "assert y>5"
     ast2 = ast.parse_string(prg2)
     sv = MyVisitorAst2()
     sv.visit(ast2)