Example #1
0
        public void RemoveNoConstraintsBasedOnVars()
        {
            IConstraintManager CM      = new ConstraintManager();
            IExprBuilder       builder = GetBuilder();

            // Dummy Boogie variable
            var bv8TypeIdent = new TypedIdent(Token.NoToken, "bv8", Microsoft.Boogie.Type.GetBvType(8));
            var dummyVarBv   = new GlobalVariable(Token.NoToken, bv8TypeIdent);

            // dummyVar needs a programLocation, otherwise SymbolicVariable constructor raises an exception
            var progLoc = new ProgramLocation(dummyVarBv);

            dummyVarBv.SetMetadata <ProgramLocation>((int)Symbooglix.Annotation.AnnotationIndex.PROGRAM_LOCATION, progLoc);

            var s0 = new SymbolicVariable("s0", dummyVarBv).Expr;
            var s1 = new SymbolicVariable("s1", dummyVarBv).Expr;
            var s2 = new SymbolicVariable("s2", dummyVarBv).Expr;

            // Construct some constraints
            Expr c0 = builder.Eq(builder.BVAND(s0, s1), builder.ConstantBV(0, 8));
            Expr c1 = builder.Eq(s2, builder.ConstantBV(1, 8));

            CM.AddConstraint(c0, progLoc);
            CM.AddConstraint(c1, progLoc);

            var mockSolver      = new MockSolver();
            var indepenceSolver = new Symbooglix.Solver.ConstraintIndependenceSolver(mockSolver);

            Expr queryExpr = builder.Eq(builder.BVAND(s1, s2), builder.ConstantBV(0, 8));

            indepenceSolver.ComputeSatisfiability(new Solver.Query(CM, new Constraint(queryExpr)));

            // Check no constraints were removed
            Assert.AreEqual(2, mockSolver.Constraints.Count);
            Assert.AreSame(queryExpr, mockSolver.QueryExpr);

            bool c0Found = false;
            bool c1Found = false;

            foreach (var constraint in mockSolver.Constraints)
            {
                if (c0 == constraint.Condition)
                {
                    c0Found = true;
                }

                if (c1 == constraint.Condition)
                {
                    c1Found = true;
                }
            }

            Assert.IsTrue(c0Found);
            Assert.IsTrue(c1Found);
        }
 public virtual Expr BVAND(Expr lhs, Expr rhs)
 {
     return(UB.BVAND(lhs, rhs));
 }
Example #3
0
        public void RemoveNoConstraintsBasedOnVarsAndFunctions()
        {
            IConstraintManager CM      = new ConstraintManager();
            IExprBuilder       builder = GetBuilder();

            // Dummy Boogie variable
            var bv8Type      = Microsoft.Boogie.Type.GetBvType(8);
            var bv8TypeIdent = new TypedIdent(Token.NoToken, "bv8", bv8Type);
            var dummyVarBv   = new GlobalVariable(Token.NoToken, bv8TypeIdent);

            // dummyVar needs a programLocation, otherwise SymbolicVariable constructor raises an exception
            var progLoc = new ProgramLocation(dummyVarBv);

            dummyVarBv.SetMetadata <ProgramLocation>((int)Symbooglix.Annotation.AnnotationIndex.PROGRAM_LOCATION, progLoc);

            var s0 = new SymbolicVariable("s0", dummyVarBv).Expr;
            var s1 = new SymbolicVariable("s1", dummyVarBv).Expr;
            var s2 = new SymbolicVariable("s2", dummyVarBv).Expr;

            // Construct some constraints
            Expr c0 = builder.Eq(builder.BVAND(s0, s1), builder.ConstantBV(0, 8));
            Expr c1 = builder.Eq(s2, builder.ConstantBV(1, 8));

            var FCB        = new FunctionCallBuilder();
            var foobarFunc = FCB.CreateUninterpretedFunctionCall("foobar", bv8Type, new List <Microsoft.Boogie.Type>()
            {
                bv8Type
            });
            // foobar(0bv8) == 0bv8
            Expr c2 = builder.Eq(builder.UFC(foobarFunc, builder.ConstantBV(0, 8)), builder.ConstantBV(0, 8));

            CM.AddConstraint(c0, progLoc);
            CM.AddConstraint(c1, progLoc);
            CM.AddConstraint(c2, progLoc);

            var mockSolver      = new MockSolver();
            var indepenceSolver = new Symbooglix.Solver.ConstraintIndependenceSolver(mockSolver);

            // The query expression uses the "foobar" function so we need to keep constraints on that function
            Expr queryExpr = builder.And(builder.Eq(builder.BVAND(s1, s2), builder.ConstantBV(0, 8)),
                                         builder.NotEq(builder.UFC(foobarFunc, s1), s1)
                                         );

            indepenceSolver.ComputeSatisfiability(new Solver.Query(CM, new Constraint(queryExpr)));

            // Check no constraints were removed
            Assert.AreEqual(3, mockSolver.Constraints.Count);
            Assert.AreSame(queryExpr, mockSolver.QueryExpr);

            bool c0Found = false;
            bool c1Found = false;
            bool c2Found = false;

            foreach (var constraint in mockSolver.Constraints)
            {
                if (c0 == constraint.Condition)
                {
                    c0Found = true;
                }

                if (c1 == constraint.Condition)
                {
                    c1Found = true;
                }

                if (c2 == constraint.Condition)
                {
                    c2Found = true;
                }
            }

            Assert.IsTrue(c0Found);
            Assert.IsTrue(c1Found);
            Assert.IsTrue(c2Found);
        }
Example #4
0
        protected Expr HandleBvBuiltIns(FunctionCall fc, string builtin, List <Expr> newArgs)
        {
            Debug.Assert(builtin.Length > 0);

            // We grab for first word because the bvbuiltin
            // might be "zeroextend 16", we don't care about the number
            string firstWord = builtin.Split(' ')[0];

            Debug.Assert(firstWord.Length > 0);

            // Handle `(_ zero_extend x)` and `(_ sign_extend x)` style builtins
            if (firstWord.StartsWith("(_"))
            {
                var words = builtin.Split(' ');
                if (words.Length < 2)
                {
                    throw new ArgumentException("Malformed bvbuiltin name \"" + builtin + "\"");
                }
                firstWord = words[1];
            }

            int retWidth = 0;

            switch (firstWord)
            {
            // arithmetic
            case "bvadd":
                Debug.Assert(newArgs.Count == 2);
                return(Builder.BVADD(newArgs[0], newArgs[1]));

            case "bvsub":
                Debug.Assert(newArgs.Count == 2);
                return(Builder.BVSUB(newArgs[0], newArgs[1]));

            case "bvmul":
                Debug.Assert(newArgs.Count == 2);
                return(Builder.BVMUL(newArgs[0], newArgs[1]));

            case "bvudiv":
                Debug.Assert(newArgs.Count == 2);
                return(Builder.BVUDIV(newArgs[0], newArgs[1]));

            case "bvurem":
                Debug.Assert(newArgs.Count == 2);
                return(Builder.BVUREM(newArgs[0], newArgs[1]));

            case "bvsdiv":
                Debug.Assert(newArgs.Count == 2);
                return(Builder.BVSDIV(newArgs[0], newArgs[1]));

            case "bvsrem":
                Debug.Assert(newArgs.Count == 2);
                return(Builder.BVSREM(newArgs[0], newArgs[1]));

            case "bvsmod":
                Debug.Assert(newArgs.Count == 2);
                return(Builder.BVSMOD(newArgs[0], newArgs[1]));

            case "sign_extend":
                Debug.Assert(newArgs.Count == 1);
                Debug.Assert(fc.Func.OutParams.Count == 1);
                retWidth = fc.Func.OutParams[0].TypedIdent.Type.BvBits;
                return(Builder.BVSEXT(newArgs[0], retWidth));

            case "zero_extend":
                Debug.Assert(newArgs.Count == 1);
                Debug.Assert(fc.Func.OutParams.Count == 1);
                retWidth = fc.Func.OutParams[0].TypedIdent.Type.BvBits;
                return(Builder.BVZEXT(newArgs[0], retWidth));

            case "bvneg":
                Debug.Assert(newArgs.Count == 1);
                return(Builder.BVNEG(newArgs[0]));

            // bitwise logical
            case "bvand":
                Debug.Assert(newArgs.Count == 2);
                return(Builder.BVAND(newArgs[0], newArgs[1]));

            case "bvor":
                Debug.Assert(newArgs.Count == 2);
                return(Builder.BVOR(newArgs[0], newArgs[1]));

            case "bvnot":
                Debug.Assert(newArgs.Count == 1);
                return(Builder.BVNOT(newArgs[0]));

            case "bvxor":
                Debug.Assert(newArgs.Count == 2);
                return(Builder.BVXOR(newArgs[0], newArgs[1]));

            // shift
            case "bvshl":
                Debug.Assert(newArgs.Count == 2);
                return(Builder.BVSHL(newArgs[0], newArgs[1]));

            case "bvlshr":
                Debug.Assert(newArgs.Count == 2);
                return(Builder.BVLSHR(newArgs[0], newArgs[1]));

            case "bvashr":
                Debug.Assert(newArgs.Count == 2);
                return(Builder.BVASHR(newArgs[0], newArgs[1]));

            // Comparison
            case "bvult":
                Debug.Assert(newArgs.Count == 2);
                return(Builder.BVULT(newArgs[0], newArgs[1]));

            case "bvule":
                Debug.Assert(newArgs.Count == 2);
                return(Builder.BVULE(newArgs[0], newArgs[1]));

            case "bvugt":
                Debug.Assert(newArgs.Count == 2);
                return(Builder.BVUGT(newArgs[0], newArgs[1]));

            case "bvuge":
                Debug.Assert(newArgs.Count == 2);
                return(Builder.BVUGE(newArgs[0], newArgs[1]));

            case "bvslt":
                Debug.Assert(newArgs.Count == 2);
                return(Builder.BVSLT(newArgs[0], newArgs[1]));

            case "bvsle":
                Debug.Assert(newArgs.Count == 2);
                return(Builder.BVSLE(newArgs[0], newArgs[1]));

            case "bvsgt":
                Debug.Assert(newArgs.Count == 2);
                return(Builder.BVSGT(newArgs[0], newArgs[1]));

            case "bvsge":
                Debug.Assert(newArgs.Count == 2);
                return(Builder.BVSGE(newArgs[0], newArgs[1]));

            default:
                throw new NotImplementedException("\"" + firstWord + "\" bvbuiltin not supported!");
            }
        }