/// <summary>
 /// Checks that LessEqual(l, r, out result) == trivialSuccess && result == resultCheck;
 /// </summary>
 static void AssertLessEqualResultFor (FlatDomain<int> l, FlatDomain<int> r, bool trivialSuccess, bool resultCheck)
 {
         bool result;
         Assert.That (l.TryTrivialLessEqual (r, out result), Is.EqualTo (trivialSuccess));
         if (trivialSuccess)
                 Assert.That (result, Is.EqualTo (resultCheck));
 }
            public static void ValidateAssertions(IFactQuery <BoxedExpression, TVariable> facts, IMethodDriver <TExpression, TVariable> driver, List <string> proofResults)
            {
                APC entryAfterRequires = driver.ContextProvider.MethodContext.CFG.EntryAfterRequires;

                if (facts.IsUnreachable(entryAfterRequires))
                {
                    proofResults.Add("Method precondition is unsatisfiable");
                    return;
                }

                object assertStats;

                foreach (AssertionObligation obl in GetAssertions(driver, out assertStats))
                {
                    FlatDomain <bool> outcome = facts.IsTrue(obl.Apc, BoxedExpression.For(driver.ContextProvider.ExpressionContext.Refine(obl.Apc, obl.Condition), driver.ExpressionDecoder));

                    string pc = obl.Apc.ToString();

                    if (outcome.IsNormal())
                    {
                        proofResults.Add(string.Format("Assertion at point {0} is {1}", pc, outcome.IsTrue() ? "true" : "false"));
                    }
                    else if (outcome.IsTop)
                    {
                        proofResults.Add("Assertion at point " + pc + " is unproven");
                    }
                    else
                    {
                        proofResults.Add("Assertion at point " + pc + " is unreachable");
                    }
                }
            }
Beispiel #3
0
        public Result Decode <Data, Result, Visitor> (LabeledSymbol <APC, TSymbolicValue> expr, Visitor visitor, Data data)
            where Visitor : ISymbolicExpressionVisitor <LabeledSymbol <APC, TSymbolicValue>, LabeledSymbol <APC, TSymbolicValue>, TSymbolicValue, Data, Result>
        {
            ExprDomain <TSymbolicValue> ifFound;

            if (!this.parent.PreStateLookup(expr.ReadAt, out ifFound) || ifFound.IsBottom)
            {
                return(visitor.SymbolicConstant(expr, expr.Symbol, data));
            }

            FlatDomain <Expr <TSymbolicValue> > aExpr = ifFound [expr.Symbol];

            if (aExpr.IsNormal)
            {
                return(aExpr.Concrete.Decode <Data, Result, ExpressionDecoderAdapter <TSymbolicValue, Data, Result, Visitor> >
                           (expr.ReadAt, expr.Symbol,
                           new ExpressionDecoderAdapter <TSymbolicValue, Data, Result, Visitor> (visitor), data));
            }

            TypeNode type;
            object   constant;

            if (this.parent.ValueLayer.ILDecoder.ContextProvider.ValueContext.IsConstant(expr.ReadAt, expr.Symbol, out type, out constant))
            {
                return(visitor.LoadConst(expr, type, constant, expr.Symbol, data));
            }

            return(visitor.SymbolicConstant(expr, expr.Symbol, data));
        }
 /// <summary>
 /// Checks that Join(l, r, out result) == trivialSuccess && check(result);
 /// </summary>
 static void AssertJoinResultFor (FlatDomain<int> l, FlatDomain<int> r, bool trivialSuccess, Func<FlatDomain<int>, bool> check)
 {
         FlatDomain<int> result;
         Assert.That (l.TryTrivialJoin (r, out result), Is.EqualTo (trivialSuccess));
         if (trivialSuccess)
                 Assert.That (check (result));
 }
Beispiel #5
0
        public AbstractType Join(AbstractType that, bool widening, out bool weaker)
        {
            if (that.IsZero)
            {
                weaker = false;
                if (this.value.IsBottom)
                {
                    return(new AbstractType(that.value, IsZero));
                }
                return(this);
            }
            if (IsZero)
            {
                weaker = true;
                if (that.value.IsBottom)
                {
                    return(new AbstractType(this.value, that.IsZero));
                }
                return(that);
            }

            FlatDomain <TypeNode> resultType = this.value.Join(that.value, widening, out weaker);

            return(new AbstractType(resultType, false));
        }
Beispiel #6
0
        public ProofOutcome IsNonNull(APC pc, V variable)
        {
            Domain <E, V> domain;

            if (!PreStateLookup(pc, out domain) || domain.NonNulls.IsBottom)
            {
                return(ProofOutcome.Bottom);
            }
            if (domain.IsNonNull(variable))
            {
                return(ProofOutcome.True);
            }
            if (ContextProvider.ValueContext.IsZero(pc, variable) || domain.IsNull(variable))
            {
                return(ProofOutcome.False);
            }

            FlatDomain <TypeNode> aType = ContextProvider.ValueContext.GetType(pc, variable);

            if (aType.IsNormal && MetaDataProvider.IsManagedPointer(aType.Concrete))
            {
                return(ProofOutcome.True);
            }

            return(ProofOutcome.Top);
        }
Beispiel #7
0
        public static FlatDomain <bool> Negate(this FlatDomain <bool> o)
        {
            if (o.IsNormal())
            {
                return(!o.IsTrue());
            }

            return(o);
        }
Beispiel #8
0
        /// <summary>
        /// Checks that LessEqual(l, r, out result) == trivialSuccess && result == resultCheck;
        /// </summary>
        static void AssertLessEqualResultFor(FlatDomain <int> l, FlatDomain <int> r, bool trivialSuccess, bool resultCheck)
        {
            bool result;

            Assert.That(l.TryTrivialLessEqual(r, out result), Is.EqualTo(trivialSuccess));
            if (trivialSuccess)
            {
                Assert.That(result, Is.EqualTo(resultCheck));
            }
        }
Beispiel #9
0
        /// <summary>
        /// Checks that Join(l, r, out result) == trivialSuccess && check(result);
        /// </summary>
        static void AssertJoinResultFor(FlatDomain <int> l, FlatDomain <int> r, bool trivialSuccess, Func <FlatDomain <int>, bool> check)
        {
            FlatDomain <int> result;

            Assert.That(l.TryTrivialJoin(r, out result), Is.EqualTo(trivialSuccess));
            if (trivialSuccess)
            {
                Assert.That(check(result));
            }
        }
        public FlatDomain <bool> Isinst(E orig, TypeNode type, V dest, E obj, bool polarity)
        {
            if (!polarity)
            {
                return(this.analysis.IsNull(this.pc, dest));
            }
            FlatDomain <bool> outcome = this.analysis.IsNonNull(this.pc, dest);

            return(outcome.IsTrue() ? outcome : this.Recurse(true, obj));
        }
Beispiel #11
0
        public FlatDomain <bool> IsTrueImply(APC pc, Sequence <BoxedExpression> positiveAssumptions, Sequence <BoxedExpression> negativeAssumptions, BoxedExpression goal)
        {
            FlatDomain <bool> outcome = IsTrue(pc, goal);

            if (outcome.IsTrue() || outcome.IsBottom)
            {
                return(outcome);
            }

            return(ProofOutcome.Top);
        }
Beispiel #12
0
        public IEnumerable <Pair <Dummy, TSymValue> > Successors(TSymValue node)
        {
            FlatDomain <Expr <TSymValue> > expr = this.expressions[node];

            if (expr.IsNormal())
            {
                foreach (TSymValue sv in expr.Value.Variables)
                {
                    yield return(new Pair <Dummy, TSymValue> (Dummy.Value, sv));
                }
            }
        }
Beispiel #13
0
        public override Domain <E, V> LoadField(APC pc, Field field, V dest, V obj, Domain <E, V> data)
        {
            Domain <E, V>         domain = AssumeNonNull(obj, data);
            FlatDomain <TypeNode> aType  = ContextProvider.ValueContext.GetType(ContextProvider.MethodContext.CFG.Next(pc), dest);

            if (aType.IsNormal && MetaDataProvider.IsManagedPointer(aType.Concrete))
            {
                domain = AssumeNonNull(dest, domain);
            }

            return(domain);
        }
Beispiel #14
0
 public override Domain <E, V> Isinst(APC pc, TypeNode type, V dest, V obj, Domain <E, V> data)
 {
     if (data.IsNonNull(obj))
     {
         FlatDomain <TypeNode> aType = ContextProvider.ValueContext.GetType(pc, obj);
         if (aType.IsNormal && MetaDataProvider.DerivesFrom(aType.Concrete, type))
         {
             return(AssumeNonNull(dest, data));
         }
     }
     return(data);
 }
Beispiel #15
0
        public override ExprDomain <TSymValue> Assert(APC pc, EdgeTag tag, TSymValue condition, ExprDomain <TSymValue> data)
        {
            FlatDomain <Expr <TSymValue> > expression = data [condition];

            if (expression.IsNormal())
            {
                data = expression.Value.Decode <ExprDomain <TSymValue>, ExprDomain <TSymValue>, AssumeDecoder <TSymValue> >
                           (pc, condition, new AssumeDecoder <TSymValue> (true), data);
            }

            return(data);
        }
Beispiel #16
0
        public override ExprDomain <TSymValue> Assume(APC pc, EdgeTag tag, TSymValue condition, ExprDomain <TSymValue> data)
        {
            FlatDomain <Expr <TSymValue> > aExpression = data [condition];

            if (aExpression.IsNormal())
            {
                bool truth = tag != EdgeTag.False;
                data = aExpression.Value.Decode <ExprDomain <TSymValue>, ExprDomain <TSymValue>, AssumeDecoder <TSymValue> >
                           (pc, condition, new AssumeDecoder <TSymValue> (truth), data);
            }

            return(data);
        }
        public bool TryGetType(E expr, out TypeNode type)
        {
            FlatDomain <TypeNode> aType = ContextProvider.ExpressionContext.GetType(expr);

            if (aType.IsNormal())
            {
                type = aType.Value;
                return(true);
            }

            type = null;
            return(false);
        }
        public override FlatDomain <bool> IsNonNull(APC pc, BoxedExpression expr)
        {
            Variable v;

            if (TryVariable(expr, out v))
            {
                FlatDomain <bool> proofOutcome = this.FactBase.IsNonNull(pc, v);
                if (!proofOutcome.IsTop)
                {
                    return(proofOutcome);
                }
            }

            if (expr.IsConstant)
            {
                object constant = expr.Constant;
                if (constant == null)
                {
                    return(ProofOutcome.False);
                }
                if (constant is string)
                {
                    return(ProofOutcome.True);
                }

                long?longValue = constant.ConvertToLong();
                if (longValue.HasValue)
                {
                    return(longValue != 0 ? ProofOutcome.True : ProofOutcome.False);
                }

                return(ProofOutcome.Top);
            }

            BinaryOperator  op;
            BoxedExpression left;
            BoxedExpression right;

            if (expr.IsBinaryExpression(out op, out left, out right))
            {
                if ((op == BinaryOperator.Ceq || op == BinaryOperator.Cobjeq) && IsNull(pc, right).IsTrue())
                {
                    return(IsNull(pc, left));
                }
                if (op == BinaryOperator.Cne_Un && IsNull(pc, right).IsTrue())
                {
                    return(IsNonNull(pc, left));
                }
            }
            return(ProofOutcome.Top);
        }
Beispiel #19
0
        public virtual FlatDomain <bool> IsNull(APC pc, BoxedExpression expr)
        {
            Variable v;

            if (TryVariable(expr, out v))
            {
                FlatDomain <bool> outcome = this.FactBase.IsNull(pc, v);
                if (!outcome.IsTop)
                {
                    return(outcome);
                }
            }
            return(ProofOutcome.Top);
        }
Beispiel #20
0
        public FlatDomain <bool> IsTrue(APC pc, BoxedExpression expr)
        {
            FlatDomain <bool> res = ProofOutcome.Top;

            foreach (var factQuery in elements)
            {
                var outcome = factQuery.IsTrue(pc, expr);

                if (outcome.IsTrue() || outcome.IsBottom)
                {
                    return(outcome);
                }
                if (outcome.IsFalse())
                {
                    res = outcome;
                }
            }
            if (!res.IsTop)
            {
                return(res);
            }

            BinaryOperator  op;
            BoxedExpression left;
            BoxedExpression right;

            if (expr.IsBinaryExpression(out op, out left, out right))
            {
                if ((op == BinaryOperator.Ceq || op == BinaryOperator.Cobjeq) && this.IsRelational(left) && this.IsNull(pc, right).IsTrue())
                {
                    var outcome = this.IsTrue(pc, left);
                    return(outcome.Negate());
                }
                int leftInt;
                int rightInt;
                if (op == BinaryOperator.Ceq && left.IsConstantIntOrNull(out leftInt) && right.IsConstantIntOrNull(out rightInt))
                {
                    return(leftInt == rightInt ? ProofOutcome.True : ProofOutcome.False);
                }
            }

            if (expr.IsUnary && expr.UnaryOperator == UnaryOperator.Not)
            {
                var outcome = this.IsTrue(pc, expr.UnaryArgument);
                return(outcome.Negate());
            }

            return(ProofOutcome.Top);
        }
Beispiel #21
0
        public ExprDomain <SymbolicValue> EdgeConversion(APC @from, APC to, bool isJoinPoint, EdgeData sourceTargetMap, ExprDomain <SymbolicValue> originalState)
        {
            if (sourceTargetMap == null)
            {
                return(originalState);
            }

            if (DebugOptions.Debug)
            {
                Console.WriteLine("====Expression analysis Parallel assign====");
                DumpMap(sourceTargetMap);
                DumpExpressions("original expressions", originalState);
            }

            ExprDomain <SymbolicValue> result = originalState.Empty();
            ExprDomain <SymbolicValue> domain = originalState.Empty();

            foreach (SymbolicValue sv in originalState.Keys)
            {
                Expr <SymbolicValue> expression = originalState [sv].Concrete.Substitute(sourceTargetMap);
                if (expression != null)
                {
                    domain = domain.Add(sv, expression);
                }
            }

            foreach (SymbolicValue sv in sourceTargetMap.Keys)
            {
                FlatDomain <Expr <SymbolicValue> > expressionDomain = domain [sv];
                if (expressionDomain.IsNormal)
                {
                    Expr <SymbolicValue> expression = expressionDomain.Concrete;
                    foreach (SymbolicValue sub in sourceTargetMap[sv].AsEnumerable())
                    {
                        result = result.Add(sub, expression);
                    }
                }
            }

            if (DebugOptions.Debug)
            {
                DumpExpressions("new expressions", result);
            }
            return(result);
        }
Beispiel #22
0
 public ExprDomain <SymbolicValue> Binary(APC pc, BinaryOperator op, SymbolicValue dest, SymbolicValue s1, SymbolicValue s2, ExprDomain <SymbolicValue> data)
 {
     if (this.truth && op.IsEqualityOperator())
     {
         if (!data.HasRefinement(s1))
         {
             FlatDomain <Expr <SymbolicValue> > expression2 = data [s2];
             if (expression2.IsNormal && !data.IsReachableFrom(s2, s1))
             {
                 return(data.Add(s1, expression2.Concrete));
             }
         }
         else if (!data.HasRefinement(s2))
         {
             FlatDomain <Expr <SymbolicValue> > expression1 = data [s1];
             if (expression1.IsNormal && !data.IsReachableFrom(s1, s2))
             {
                 return(data.Add(s2, expression1.Concrete));
             }
         }
     }
     if (!this.truth && op == BinaryOperator.Cne_Un)
     {
         if (!data.HasRefinement(s1))
         {
             FlatDomain <Expr <SymbolicValue> > expression2 = data [s2];
             if (expression2.IsNormal && !data.IsReachableFrom(s2, s1))
             {
                 return(data.Add(s1, expression2.Concrete));
             }
         }
         else if (!data.HasRefinement(s2))
         {
             FlatDomain <Expr <SymbolicValue> > expression1 = data [s1];
             if (expression1.IsNormal && !data.IsReachableFrom(s1, s2))
             {
                 return(data.Add(s2, expression1.Concrete));
             }
         }
     }
     return(data);
 }
Beispiel #23
0
 private void DumpExpressions(string header, ExprDomain <SymbolicValue> state)
 {
     Console.WriteLine("--- {0} ---", header);
     foreach (SymbolicValue index in state.Keys)
     {
         FlatDomain <Expr <SymbolicValue> > domain = state [index];
         if (domain.IsNormal)
         {
             Console.WriteLine("{0} -> {1}", index, domain.Concrete);
         }
         else if (domain.IsTop)
         {
             Console.WriteLine("{0} -> (Top)", index);
         }
         else if (domain.IsBottom)
         {
             Console.WriteLine("{0} -> (Bot)", index);
         }
     }
 }
Beispiel #24
0
 public static bool IsFalse(this FlatDomain <bool> o)
 {
     return(o.IsNormal() && !o.Value);
 }
Beispiel #25
0
 public AbstractType With(FlatDomain <TypeNode> type)
 {
     return(new AbstractType(type, this.IsZero));
 }
Beispiel #26
0
 public AbstractType(FlatDomain <TypeNode> value, bool isZero) : this()
 {
     IsZero     = isZero;
     this.value = value;
 }