Esempio n. 1
0
        private void DuplicateAndCheck(Expr original, IExprBuilder builder)
        {
            var duplicator = GetDuplicator(builder);
            var copy       = (Expr)duplicator.Visit(original);

            // Check they are structually equal
            Assert.IsTrue(original.Equals(copy));

            // Check the roots are different instances
            Assert.AreNotSame(original, copy);

            // Verify the nodes are disjoint, allow constants to be shared though
            var c           = new DuplicationVerifier(/*ignoreDuplicateConstants=*/ true, /*ignoreDuplicateSymbolics=*/ false);
            var sharedNodes = c.CheckDuplication(original, copy);

            Assert.AreEqual(0, sharedNodes.Count);

            // Verify all nodes are immutable
            var iv = new ImmutableExprVerifier();

            iv.Visit(copy);
            Assert.AreEqual(0, iv.NonImmutableNodes.Count);

            // Type check both Expr
            var TC = new TypecheckingContext(this);

            original.Typecheck(TC);
            copy.Typecheck(TC);
        }
Esempio n. 2
0
        public void FunctionCall()
        {
            var fc = CreateFunctionCall("bv8slt", Microsoft.Boogie.Type.Bool, new List <Microsoft.Boogie.Type>()
            {
                BasicType.GetBvType(8),
                BasicType.GetBvType(8)
            });

            var constantBv = new LiteralExpr(Token.NoToken, BigNum.FromInt(0), 8);
            var nary       = new NAryExpr(Token.NoToken, fc, new List <Expr>()
            {
                constantBv, constantBv
            });

            // Get shallow type (this was broken when this test was written)
            Assert.AreEqual(BasicType.Bool, nary.ShallowType);

            // Deep type check (this was not broken before writing this test)
            Assert.IsNull(nary.Type);

            var tc = new TypecheckingContext(this);

            nary.Typecheck(tc);

            Assert.AreEqual(BasicType.Bool, nary.Type);
        }
        protected void CheckIsReal(Expr result)
        {
            Assert.IsTrue(result.ShallowType.IsReal);
            Assert.IsNotNull(result.Type);
            Assert.IsTrue(result.Type.IsReal);
            var TC = new TypecheckingContext(this);

            result.Typecheck(TC);
        }
        protected void CheckType(Expr e, Predicate <Microsoft.Boogie.Type> p)
        {
            Assert.IsNotNull(e.ShallowType);
            Assert.IsTrue(p(e.ShallowType));
            Assert.IsNotNull(e.Type);
            Assert.AreEqual(e.ShallowType, e.Type);
            var TC = new TypecheckingContext(this);

            e.Typecheck(TC);
        }
Esempio n. 5
0
    public void ProtectedExprType()
    {
      var e = GetUnTypedImmutableNAry();

      // Now Typecheck
      // Even though it's immutable we allow the TypeCheck field to be set if the Expr has never been type checked
      var TC = new TypecheckingContext(this);
      e.Typecheck(TC);
      Assert.IsNotNull(e.Type);
      Assert.IsTrue(e.Type.IsBool);
    }
Esempio n. 6
0
    public void ProtectedExprTypeChangeTypeSucceed()
    {
      var e = GetUnTypedImmutableNAry();

      // Now Typecheck
      // Even though it's immutable we allow the TypeCheck field to be set if the Expr has never been type checked
      var TC = new TypecheckingContext(this);
      e.Typecheck(TC);
      Assert.IsNotNull(e.Type);
      Assert.IsTrue(e.Type.IsBool);

      // Trying to assign the same Type should succeed
      e.Type = BasicType.Bool;
    }
Esempio n. 7
0
    public void ProtectedExprChangeTypeFail()
    {
      var e = GetUnTypedImmutableNAry();

      // Now Typecheck
      // Even though it's immutable we allow the TypeCheck field to be set if the Expr has never been type checked
      var TC = new TypecheckingContext(this);
      e.Typecheck(TC);
      Assert.IsNotNull(e.Type);
      Assert.IsTrue(e.Type.IsBool);

      // Trying to modify the Type to a different Type now should fail
      Assert.Throws(typeof(InvalidOperationException), () => e.Type = BasicType.Int);
    }
        protected void CheckIsBoolType(Expr result)
        {
            var shallowType = result.ShallowType;

            Assert.IsNotNull(shallowType);
            Assert.IsTrue(shallowType.IsBool);
            var t = result.Type;

            Assert.IsNotNull(t);
            Assert.IsTrue(t.IsBool);
            var TC = new TypecheckingContext(this);

            result.Typecheck(TC);
        }
        protected void CheckIsBvType(Expr result, int width)
        {
            var shallowType = result.ShallowType;

            Assert.IsNotNull(shallowType);
            Assert.IsTrue(shallowType.IsBv);
            Assert.AreEqual(width, shallowType.BvBits);
            var t = result.Type;

            Assert.IsNotNull(t);
            Assert.IsTrue(t.IsBv);
            Assert.AreEqual(width, t.BvBits);
            var TC = new TypecheckingContext(this);

            result.Typecheck(TC);
        }
Esempio n. 10
0
        public void IfThenElseSimple()
        {
            var builder   = GetSimpleBuilder();
            var condition = builder.False;
            var constant0 = builder.ConstantInt(5);
            var constant1 = builder.ConstantInt(2);
            var result    = builder.IfThenElse(condition, constant0, constant1);

            Assert.AreEqual("(if false then 5 else 2)", result.ToString());
            Assert.IsNotNull(result.Type);
            Assert.IsTrue(result.Type.IsInt);
            Assert.IsTrue(result.ShallowType.IsInt);
            var TC = new TypecheckingContext(this);

            result.Typecheck(TC);
        }
Esempio n. 11
0
        /*
         #region _TESTING_NEW_STUFF_
         *   CommandLineOptions.Clo.vcVariety = CommandLineOptions.VCVariety.Block;
         *   //VCExpr wp = Wlp.Block(block, SuccCorrect, context); // Computes wp.S.true
         *
         *   CommandLineOptions.Clo.vcVariety = CommandLineOptions.VCVariety.Doomed;
         #endregion
         *
         */

        VCExpr GenerateEVC(Implementation impl, out Dictionary <int, Absy> label2absy, Checker ch, out int assertionCount)
        {
            Contract.Requires(impl != null);
            Contract.Requires(ch != null);
            Contract.Ensures(Contract.Result <VCExpr>() != null);

            TypecheckingContext tc = new TypecheckingContext(null);

            impl.Typecheck(tc);
            label2absy = new Dictionary <int, Absy>();
            VCExpr vc;

            switch (CommandLineOptions.Clo.vcVariety)
            {
            case CommandLineOptions.VCVariety.Doomed:
                vc = LetVC(cce.NonNull(impl.Blocks[0]), label2absy, ch.TheoremProver.Context, out assertionCount);
                break;

            default:
                Contract.Assert(false); throw new cce.UnreachableException(); // unexpected enumeration value
            }
            return(vc);
        }
Esempio n. 12
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));
      }
    }
Esempio n. 13
0
        public Microsoft.Boogie.Type Typecheck(IList <Expr> args, out TypeParamInstantiation tpInstantiation, TypecheckingContext tc)
        {
            Debug.Assert(args.Count == ArgumentCount);
            // We can assume the expressions in args have already been type checked for us by NAryExpr.TypeCheck()

            // I'm not sure if this is right
            tpInstantiation = SimpleTypeParamInstantiation.EMPTY;
            var firstExpr = args[0];

            for (int index = 1; index < args.Count; ++index)
            {
                Debug.Assert(args[index].Type != null);
                if (!firstExpr.Type.Unify(args[index].Type))
                {
                    // Type checking failed
                    tc.Error(Token, String.Format("Failed to unify type {0} with {1} (argument index {2}",
                                                  firstExpr.Type.ToString(), args[index].Type.ToString(), index));
                    return(null);
                }
            }

            return(this.ShallowType(args));
        }