Exemplo n.º 1
0
        public VCExpr GetSummaryExpr(Dictionary <string, VCExpr> incarnations, VCExpressionGenerator gen)
        {
            VCExpr ret = VCExpressionGenerator.True;

            if (val.Values.Any(v => v == null))
            {
                return(VCExpressionGenerator.False);
            }

            foreach (var v in vars)
            {
                var consts = val[v.Name];
                Debug.Assert(consts != null);

                if (consts.Count == 0)
                {
                    continue;
                }

                var vexpr = VCExpressionGenerator.False;
                consts.Iter(c => vexpr = gen.OrSimp(vexpr, gen.Eq(incarnations[v.Name], gen.Integer(Microsoft.BaseTypes.BigNum.FromInt(c)))));
                ret = gen.AndSimp(ret, vexpr);
            }

            return(ret);
        }
Exemplo n.º 2
0
Arquivo: Wlp.cs Projeto: wrwg/boogie
        /// <summary>
        /// Computes the wlp for an assert or assume command "cmd".
        /// </summary>
        internal static VCExpr Cmd(Block b, Cmd cmd, VCExpr N, VCContext ctxt)
        {
            Contract.Requires(cmd != null);
            Contract.Requires(N != null);
            Contract.Requires(ctxt != null);
            Contract.Ensures(Contract.Result <VCExpr>() != null);

            VCExpressionGenerator gen = ctxt.Ctxt.ExprGen;

            Contract.Assert(gen != null);
            if (cmd is AssertCmd)
            {
                AssertCmd ac = (AssertCmd)cmd;

                var isFullyVerified = false;
                if (ac.VerifiedUnder != null)
                {
                    var litExpr = ac.VerifiedUnder as LiteralExpr;
                    isFullyVerified = litExpr != null && litExpr.IsTrue;
                }

                if (!isFullyVerified)
                {
                    ctxt.Ctxt.BoogieExprTranslator.isPositiveContext = !ctxt.Ctxt.BoogieExprTranslator.isPositiveContext;
                }

                VCExpr C = ctxt.Ctxt.BoogieExprTranslator.Translate(ac.Expr);

                VCExpr VU = null;
                if (!isFullyVerified)
                {
                    if (ac.VerifiedUnder != null)
                    {
                        VU = ctxt.Ctxt.BoogieExprTranslator.Translate(ac.VerifiedUnder);

                        if (CommandLineOptions.Clo.RunDiagnosticsOnTimeout)
                        {
                            ctxt.Ctxt.TimeoutDiagnosticIDToAssertion[ctxt.Ctxt.TimeoutDiagnosticsCount] =
                                new Tuple <AssertCmd, TransferCmd>(ac, b.TransferCmd);
                            VU = gen.Or(VU,
                                        gen.Function(VCExpressionGenerator.TimeoutDiagnosticsOp,
                                                     gen.Integer(BigNum.FromInt(ctxt.Ctxt.TimeoutDiagnosticsCount++))));
                        }
                    }
                    else if (CommandLineOptions.Clo.RunDiagnosticsOnTimeout)
                    {
                        ctxt.Ctxt.TimeoutDiagnosticIDToAssertion[ctxt.Ctxt.TimeoutDiagnosticsCount] =
                            new Tuple <AssertCmd, TransferCmd>(ac, b.TransferCmd);
                        VU = gen.Function(VCExpressionGenerator.TimeoutDiagnosticsOp,
                                          gen.Integer(BigNum.FromInt(ctxt.Ctxt.TimeoutDiagnosticsCount++)));
                    }

                    ctxt.Ctxt.BoogieExprTranslator.isPositiveContext = !ctxt.Ctxt.BoogieExprTranslator.isPositiveContext;
                }

                {
                    var subsumption = Subsumption(ac);
                    if (subsumption == CommandLineOptions.SubsumptionOption.Always ||
                        (subsumption == CommandLineOptions.SubsumptionOption.NotForQuantifiers && !(C is VCExprQuantifier)))
                    {
                        N = gen.ImpliesSimp(C, N, false);
                    }

                    if (isFullyVerified)
                    {
                        return(N);
                    }
                    else if (VU != null)
                    {
                        C = gen.OrSimp(VU, C);
                    }

                    int id = ac.UniqueId;
                    if (ctxt.Label2absy != null)
                    {
                        ctxt.Label2absy[id] = ac;
                    }

                    ctxt.AssertionCount++;

                    if (ctxt.ControlFlowVariableExpr == null)
                    {
                        Contract.Assert(ctxt.Label2absy != null);
                        return(gen.AndSimp(C, N));
                    }
                    else
                    {
                        VCExpr controlFlowFunctionAppl = gen.ControlFlowFunctionApplication(ctxt.ControlFlowVariableExpr,
                                                                                            gen.Integer(BigNum.FromInt(b.UniqueId)));
                        Contract.Assert(controlFlowFunctionAppl != null);
                        VCExpr assertFailure = gen.Eq(controlFlowFunctionAppl, gen.Integer(BigNum.FromInt(-ac.UniqueId)));
                        return(gen.AndSimp(gen.Implies(assertFailure, C), N));
                    }
                }
            }
            else if (cmd is AssumeCmd)
            {
                AssumeCmd ac = (AssumeCmd)cmd;

                if (CommandLineOptions.Clo.StratifiedInlining > 0)
                {
                    // Label the assume if it is a procedure call
                    NAryExpr naryExpr = ac.Expr as NAryExpr;
                    if (naryExpr != null)
                    {
                        if (naryExpr.Fun is FunctionCall)
                        {
                            int id = ac.UniqueId;
                            ctxt.Label2absy[id] = ac;
                            return(MaybeWrapWithOptimization(ctxt, gen, ac.Attributes,
                                                             gen.ImpliesSimp(ctxt.Ctxt.BoogieExprTranslator.Translate(ac.Expr), N)));
                        }
                    }
                }

                var expr = ctxt.Ctxt.BoogieExprTranslator.Translate(ac.Expr);

                var aid = QKeyValue.FindStringAttribute(ac.Attributes, "id");
                if (aid != null)
                {
                    var isTry = QKeyValue.FindBoolAttribute(ac.Attributes, "try");
                    var v     = gen.Variable((isTry ? "try$$" : "assume$$") + aid, Microsoft.Boogie.Type.Bool);
                    expr = gen.Function(VCExpressionGenerator.NamedAssumeOp, v, gen.ImpliesSimp(v, expr));
                }

                var soft       = QKeyValue.FindBoolAttribute(ac.Attributes, "soft");
                var softWeight = QKeyValue.FindIntAttribute(ac.Attributes, "soft", 0);
                if ((soft || 0 < softWeight) && aid != null)
                {
                    var v = gen.Variable("soft$$" + aid, Microsoft.Boogie.Type.Bool);
                    expr = gen.Function(new VCExprSoftOp(Math.Max(softWeight, 1)), v, gen.ImpliesSimp(v, expr));
                }

                return(MaybeWrapWithOptimization(ctxt, gen, ac.Attributes, gen.ImpliesSimp(expr, N)));
            }
            else
            {
                Console.WriteLine(cmd.ToString());
                Contract.Assert(false);
                throw new cce.UnreachableException(); // unexpected command
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Computes the wlp for an assert or assume command "cmd".
        /// </summary>
        public static VCExpr Cmd(Block b, Cmd cmd, VCExpr N, VCContext ctxt)
        {
            Contract.Requires(cmd != null);
            Contract.Requires(N != null);
            Contract.Requires(ctxt != null);
            Contract.Ensures(Contract.Result <VCExpr>() != null);

            VCExpressionGenerator gen = ctxt.Ctxt.ExprGen;

            Contract.Assert(gen != null);
            if (cmd is AssertCmd)
            {
                AssertCmd ac = (AssertCmd)cmd;

                var isFullyVerified = false;
                if (ac.VerifiedUnder != null)
                {
                    var litExpr = ac.VerifiedUnder as LiteralExpr;
                    isFullyVerified = litExpr != null && litExpr.IsTrue;
                }

                if (!isFullyVerified)
                {
                    ctxt.Ctxt.BoogieExprTranslator.isPositiveContext = !ctxt.Ctxt.BoogieExprTranslator.isPositiveContext;
                }

                VCExpr C = ctxt.Ctxt.BoogieExprTranslator.Translate(ac.Expr);

                VCExpr VU = null;
                if (!isFullyVerified)
                {
                    if (ac.VerifiedUnder != null)
                    {
                        VU = ctxt.Ctxt.BoogieExprTranslator.Translate(ac.VerifiedUnder);
                    }
                    ctxt.Ctxt.BoogieExprTranslator.isPositiveContext = !ctxt.Ctxt.BoogieExprTranslator.isPositiveContext;
                }

                VCExpr R = null;
                if (CommandLineOptions.Clo.vcVariety == CommandLineOptions.VCVariety.Doomed)
                {
                    R = gen.Implies(C, N);
                }
                else
                {
                    var subsumption = Subsumption(ac);
                    if (subsumption == CommandLineOptions.SubsumptionOption.Always ||
                        (subsumption == CommandLineOptions.SubsumptionOption.NotForQuantifiers && !(C is VCExprQuantifier)))
                    {
                        N = gen.ImpliesSimp(C, N, false);
                    }

                    if (isFullyVerified)
                    {
                        return(N);
                    }
                    else if (VU != null)
                    {
                        C = gen.OrSimp(VU, C);
                    }

                    int id = ac.UniqueId;
                    if (ctxt.Label2absy != null)
                    {
                        ctxt.Label2absy[id] = ac;
                    }

                    ctxt.AssertionCount++;

                    if (ctxt.ControlFlowVariableExpr == null)
                    {
                        Contract.Assert(ctxt.Label2absy != null);
                        R = gen.AndSimp(gen.LabelNeg(cce.NonNull(id.ToString()), C), N);
                    }
                    else
                    {
                        VCExpr controlFlowFunctionAppl = gen.ControlFlowFunctionApplication(ctxt.ControlFlowVariableExpr, gen.Integer(BigNum.FromInt(b.UniqueId)));
                        Contract.Assert(controlFlowFunctionAppl != null);
                        VCExpr assertFailure = gen.Eq(controlFlowFunctionAppl, gen.Integer(BigNum.FromInt(-ac.UniqueId)));
                        if (ctxt.Label2absy == null)
                        {
                            R = gen.AndSimp(gen.Implies(assertFailure, C), N);
                        }
                        else
                        {
                            R = gen.AndSimp(gen.LabelNeg(cce.NonNull(id.ToString()), gen.Implies(assertFailure, C)), N);
                        }
                    }
                }
                return(R);
            }
            else if (cmd is AssumeCmd)
            {
                AssumeCmd ac = (AssumeCmd)cmd;

                if (CommandLineOptions.Clo.StratifiedInlining > 0)
                {
                    // Label the assume if it is a procedure call
                    NAryExpr naryExpr = ac.Expr as NAryExpr;
                    if (naryExpr != null)
                    {
                        if (naryExpr.Fun is FunctionCall)
                        {
                            int id = ac.UniqueId;
                            ctxt.Label2absy[id] = ac;
                            return(gen.ImpliesSimp(gen.LabelPos(cce.NonNull("si_fcall_" + id.ToString()), ctxt.Ctxt.BoogieExprTranslator.Translate(ac.Expr)), N));
                        }
                    }
                }
                return(gen.ImpliesSimp(ctxt.Ctxt.BoogieExprTranslator.Translate(ac.Expr), N));
            }
            else
            {
                Console.WriteLine(cmd.ToString());
                Contract.Assert(false); throw new cce.UnreachableException(); // unexpected command
            }
        }
Exemplo n.º 4
0
    public void GenerateVCBoolControl()
    {
      Debug.Assert(!initialized);
      Debug.Assert(CommandLineOptions.Clo.SIBoolControlVC);

      // fix names for exit variables
      var outputVariables = new List<Variable>();
      var assertConjuncts = new List<Expr>();
      foreach (Variable v in impl.OutParams)
      {
        Constant c = new Constant(Token.NoToken,
          new TypedIdent(Token.NoToken, impl.Name + "_" + v.Name, v.TypedIdent.Type));
        outputVariables.Add(c);
        Expr eqExpr = Expr.Eq(new IdentifierExpr(Token.NoToken, c), new IdentifierExpr(Token.NoToken, v));
        assertConjuncts.Add(eqExpr);
      }

      foreach (IdentifierExpr e in impl.Proc.Modifies)
      {
        if (e.Decl == null) continue;
        Variable v = e.Decl;
        Constant c = new Constant(Token.NoToken,
          new TypedIdent(Token.NoToken, impl.Name + "_" + v.Name, v.TypedIdent.Type));
        outputVariables.Add(c);
        Expr eqExpr = Expr.Eq(new IdentifierExpr(Token.NoToken, c), new IdentifierExpr(Token.NoToken, v));
        assertConjuncts.Add(eqExpr);
      }

      exitAssertCmd = new AssumeCmd(Token.NoToken, Expr.BinaryTreeAnd(assertConjuncts));
      (exitAssertCmd as AssumeCmd).Attributes = new QKeyValue(Token.NoToken, "exitAssert", new List<object>(), null);

      // no need for label2absy
      label2absy = new Dictionary<int, Absy>();

      // Passify
      Program program = vcgen.program;
      ProverInterface proverInterface = vcgen.prover;
      vcgen.ConvertCFG2DAG(impl);
      vcgen.PassifyImpl(impl, out mvInfo);

      VCExpressionGenerator gen = proverInterface.VCExprGen;
      var exprGen = proverInterface.Context.ExprGen;
      var translator = proverInterface.Context.BoogieExprTranslator;

      // add a boolean variable at each call site
      vcgen.InstrumentCallSites(impl);

      // typecheck
      var tc = new TypecheckingContext(null);
      impl.Typecheck(tc);

      ///////////////////
      // Generate the VC
      ///////////////////

      // block -> bool variable
      blockToControlVar = new Dictionary<Block, VCExprVar>();
      foreach (var b in impl.Blocks)
        blockToControlVar.Add(b, gen.Variable(b.Label + "_holds", Bpl.Type.Bool));

      vcexpr = VCExpressionGenerator.True;
      foreach (var b in impl.Blocks)
      {
        // conjoin all assume cmds
        VCExpr c = VCExpressionGenerator.True;
        foreach (var cmd in b.Cmds)
        {
          var acmd = cmd as AssumeCmd;
          if (acmd == null)
          {
            Debug.Assert(cmd is AssertCmd && (cmd as AssertCmd).Expr is LiteralExpr &&
                         ((cmd as AssertCmd).Expr as LiteralExpr).IsTrue);
            continue;
          }

          var expr = translator.Translate(acmd.Expr);
          c = gen.AndSimp(c, expr);
        }

        // block implies a disjunction of successors
        Debug.Assert(!(b.TransferCmd is ReturnExprCmd), "Not supported");
        var gc = b.TransferCmd as GotoCmd;
        if (gc != null)
        {
          VCExpr succ = VCExpressionGenerator.False;
          foreach (var sb in gc.labelTargets)
            succ = gen.OrSimp(succ, blockToControlVar[sb]);
          c = gen.AndSimp(c, succ);
        }
        else
        {
          // nothing to do
        }

        vcexpr = gen.AndSimp(vcexpr, gen.Eq(blockToControlVar[b], c));
      }

      // assert start block
      vcexpr = gen.AndSimp(vcexpr, blockToControlVar[impl.Blocks[0]]);

      //Console.WriteLine("VC of {0}: {1}", impl.Name, vcexpr);
      // Collect other information
      callSites = vcgen.CollectCallSites(impl);
      recordProcCallSites = vcgen.CollectRecordProcedureCallSites(impl);

      // record interface variables
      privateExprVars = new List<VCExprVar>();
      foreach (Variable v in impl.LocVars)
      {
        privateExprVars.Add(translator.LookupVariable(v));
      }

      foreach (Variable v in impl.OutParams)
      {
        privateExprVars.Add(translator.LookupVariable(v));
      }

      privateExprVars.AddRange(blockToControlVar.Values);

      interfaceExprVars = new List<VCExprVar>();
      foreach (Variable v in program.GlobalVariables)
      {
        interfaceExprVars.Add(translator.LookupVariable(v));
      }

      foreach (Variable v in impl.InParams)
      {
        interfaceExprVars.Add(translator.LookupVariable(v));
      }

      foreach (Variable v in outputVariables)
      {
        interfaceExprVars.Add(translator.LookupVariable(v));
      }
    }
Exemplo n.º 5
0
            public VCExpr get(Term arg)
            {
                if (memo.ContainsKey(arg))
                {
                    return(memo[arg]);
                }
                VCExpr res = null;

                switch (arg.GetKind())
                {
                case TermKind.Numeral:
                    var numstr = arg.GetNumeralString();
                    if (arg.GetSort().GetSortKind() == SortKind.Int)
                    {
                        res = gen.Integer(Basetypes.BigNum.FromString(numstr));
                    }
                    else
                    {
                        res = gen.Real(Basetypes.BigDec.FromString(numstr));
                    }
                    break;

                case TermKind.App:
                    var args   = arg.GetAppArgs();
                    var vcargs = new VCExpr[args.Length];
                    for (int i = 0; i < args.Length; i++)
                    {
                        vcargs[i] = get(args[i]);
                    }

                    switch (arg.GetAppDecl().GetKind())
                    {
                    case DeclKind.Add:
                        if (vcargs.Length == 0)
                        {
                            if (arg.GetSort().GetSortKind() == SortKind.Int)
                            {
                                res = gen.Integer(Basetypes.BigNum.ZERO);
                            }
                            else
                            {
                                res = gen.Real(Basetypes.BigDec.ZERO);
                            }
                        }
                        else
                        {
                            res = vcargs[0];
                            for (int k = 1; k < vcargs.Length; k++)
                            {
                                res = gen.Add(res, vcargs[k]);
                            }
                        }
                        break;

                    case DeclKind.And:
                        res = VCExpressionGenerator.True;
                        for (int i = 0; i < vcargs.Length; i++)
                        {
                            res = gen.AndSimp(res, vcargs[i]);
                        }
                        break;

                    case DeclKind.Div:
                        Debug.Assert(vcargs.Length == 2);
                        res = gen.Function(VCExpressionGenerator.RealDivOp, vcargs[0], vcargs[1]);
                        break;

                    case DeclKind.Eq:
                        Debug.Assert(vcargs.Length == 2);
                        res = gen.Eq(vcargs[0], vcargs[1]);
                        break;

                    case DeclKind.False:
                        res = VCExpressionGenerator.False;
                        break;

                    case DeclKind.Ge:
                        Debug.Assert(vcargs.Length == 2);
                        res = gen.Function(VCExpressionGenerator.GeOp, vcargs[0], vcargs[1]);
                        break;

                    case DeclKind.Gt:
                        Debug.Assert(vcargs.Length == 2);
                        res = gen.Gt(vcargs[0], vcargs[1]);
                        break;

                    case DeclKind.IDiv:
                        Debug.Assert(vcargs.Length == 2);
                        res = gen.Function(VCExpressionGenerator.DivOp, vcargs[0], vcargs[1]);
                        break;

                    case DeclKind.Iff:
                        Debug.Assert(vcargs.Length == 2);
                        var l = create_let(args[0], vcargs[0]);
                        var r = create_let(args[1], vcargs[1]);
                        return(gen.And(gen.Implies(l, r), gen.Implies(r, l)));

                    case DeclKind.Implies:
                        Debug.Assert(vcargs.Length == 2);
                        res = gen.Implies(vcargs[0], vcargs[1]);
                        break;

                    case DeclKind.Ite:
                        Debug.Assert(vcargs.Length == 3);
                        res = gen.Function(VCExpressionGenerator.IfThenElseOp, vcargs[0], vcargs[1], vcargs[2]);
                        break;

                    case DeclKind.Le:
                        Debug.Assert(vcargs.Length == 2);
                        res = gen.Function(VCExpressionGenerator.LeOp, vcargs[0], vcargs[1]);
                        break;

                    case DeclKind.Lt:
                        Debug.Assert(vcargs.Length == 2);
                        res = gen.Function(VCExpressionGenerator.LtOp, vcargs[0], vcargs[1]);
                        break;

                    case DeclKind.Mod:
                        Debug.Assert(vcargs.Length == 2);
                        res = gen.Function(VCExpressionGenerator.ModOp, vcargs[0], vcargs[1]);
                        break;

                    case DeclKind.Mul:
                        Debug.Assert(vcargs.Length == 2);
                        res = gen.Function(VCExpressionGenerator.MulOp, vcargs[0], vcargs[1]);
                        break;

                    case DeclKind.Not:
                        Debug.Assert(vcargs.Length == 1);
                        res = gen.Not(vcargs[0]);
                        break;

                    case DeclKind.Or:
                        res = VCExpressionGenerator.False;
                        for (int i = 0; i < vcargs.Length; i++)
                        {
                            res = gen.OrSimp(res, vcargs[i]);
                        }
                        break;

                    case DeclKind.Select:
                        Debug.Assert(vcargs.Length == 2);
                        res = gen.Select(vcargs[0], vcargs[1]);
                        break;

                    case DeclKind.Store:
                        Debug.Assert(vcargs.Length == 3);
                        res = gen.Store(vcargs[0], vcargs[1], vcargs[2]);
                        break;

                    case DeclKind.Sub:
                        Debug.Assert(vcargs.Length == 2);
                        res = gen.Function(VCExpressionGenerator.SubOp, vcargs[0], vcargs[1]);
                        break;

                    case DeclKind.True:
                        res = VCExpressionGenerator.True;
                        break;

                    case DeclKind.Uminus:
                        Debug.Assert(vcargs.Length == 1);
                        var argzero = null;
                        if (vcargs[0].Type.IsInt)
                        {
                            argzero = gen.Integer(Basetypes.BigNum.ZERO);
                        }
                        else
                        {
                            argzero = gen.Real(Basetypes.BigDec.ZERO);
                        }
                        res = gen.Function(VCExpressionGenerator.SubOp, argzero, vcargs[0]);
                        break;

                    case DeclKind.ToInt:
                        Debug.Assert(vcargs.Length == 1);
                        res = gen.Function(VCExpressionGenerator.ToIntOp, vcargs[0]);
                        break;

                    case DeclKind.ToReal:
                        Debug.Assert(vcargs.Length == 1);
                        res = gen.Function(VCExpressionGenerator.ToRealOp, vcargs[0]);
                        break;

                    case DeclKind.Uninterpreted:
                        var name = arg.GetAppDecl().GetDeclName();
                        if (args.Length == 0)
                        {     // a 0-ary constant is a VCExprVar
                            if (!constants_inv.TryGetValue(arg, out res))
                            {
                                throw new Exception("Z3 returned unknown constant: " + name);
                            }
                        }
                        else
                        {
                            Function f;
                            if (!functions_inv.TryGetValue(arg.GetAppDecl(), out f))
                            {
                                throw new Exception("Z3 returned unknown function: " + name);
                            }
                            List <VCExpr> vcargsList = new List <VCExpr>(vcargs);
                            res = gen.Function(f, vcargsList);
                        }
                        break;

                    default:
                        throw new Exception("Unknown Z3 operator");
                    }
                    break;

                default:
                    Debug.Assert(false);
                    throw new Exception("Unknown Z3 AST kind");
                }

                memo.Add(arg, res);
                return(res);
            }