示例#1
0
        private bool TryHelperFor_MatchkY(Expression x, INumericalAbstractDomainQuery <Variable, Expression> adom,
                                          Expression y, Rational k, out Expression leqExp)
        {
            Contract.Requires(adom != null);
            Contract.Requires(y != null);
            Contract.Requires(!object.ReferenceEquals(k, null));

            Contract.Ensures(!Contract.Result <bool>() || Contract.ValueAtReturn(out leqExp) != null);

            var yPositive = adom.CheckIfGreaterEqualThanZero(y);

            if (k >= 1 && yPositive.IsNormal())
            {
                if (yPositive.BoxedElement)
                {
                    leqExp = expManager.Encoder.CompoundExpressionFor(ExpressionType.Bool, ExpressionOperator.LessEqualThan, y, x);
                    return(true);
                }
                else
                {
                    leqExp = expManager.Encoder.CompoundExpressionFor(ExpressionType.Bool, ExpressionOperator.LessEqualThan, x, y);
                    return(true);
                }
            }

            leqExp = default(Expression);
            return(false);
        }
示例#2
0
        public void Assign(Expression x, Expression exp, INumericalAbstractDomainQuery <Variable, Expression> preState)
        {
            // Infer some x >= 0 invariants which requires the information from the two domains
            // The information should be inferred in the pre-state
            List <Expression> geqZero;

            // if (this.ExpressionManager.Encoder!= null)
            {
                geqZero = new GreaterEqualThanZeroConstraints <Variable, Expression>(this.ExpressionManager).InferConstraints(x, exp, preState);
                Contract.Assert(Contract.ForAll(geqZero, condition => condition != null));
            }

/*      else
 *    {
 *      geqZero = new List<Expression>();
 *      // F: we do not decompile ForAll in the case below
 *      Contract.Assume(Contract.ForAll(geqZero, condition => condition != null));
 *    }
 */
            Contract.Assert(Contract.ForAll(geqZero, condition => condition != null));

            this.Right.Assign(x, exp, preState);
            this.Left.Assign(x, exp, preState);

            // The information is assumed in the post-state...
            foreach (var condition in geqZero)
            {
                this.TestTrue(condition);
            }
        }
示例#3
0
 public void Assign(Expression x, Expression exp, INumericalAbstractDomainQuery <Variable, Expression> preState)
 {
     for (int i = 0; i < this.disjuncts.Length; i++)
     {
         this.disjuncts[i].Assign(x, exp, preState);
     }
 }
示例#4
0
        public void Assign(Expression x, Expression exp, INumericalAbstractDomainQuery <Variable, Expression> preState)
        {
            Interval intvForExp = preState.BoundsFor(exp).AsInterval;

            Variable xVar = decoder.UnderlyingVariable(x);

            AssumeInInterval(xVar, intvForExp);
        }
示例#5
0
        private void Helper_Match_kY(
            Expression x, INumericalAbstractDomainQuery <Variable, Expression> oracleDomain, List <Expression> result, Expression y)
        {
            Contract.Requires(result != null);

            Expression ltExp;

            if (oracleDomain.BoundsFor(y).LowerBound >= 1)
            {
                ltExp = expManager.Encoder.CompoundExpressionFor(ExpressionType.Bool, ExpressionOperator.LessThan, y, x);
                result.Add(ltExp);

                result.AddRange(UpperBounds(y, oracleDomain.UpperBoundsFor(x, false)));
            }
        }
示例#6
0
        /// <summary>
        /// Checks if <code>e1 leq e2</code> using CheckLessThan of <code>dom</code>
        /// </summary>
        public static FlatAbstractDomain <bool> HelperForCheckLessEqualThan <Variable, Expression>(
            this INumericalAbstractDomainQuery <Variable, Expression> dom, Expression e1, Expression e2)
        {
            Contract.Requires(dom != null);
            Contract.Ensures(Contract.Result <FlatAbstractDomain <bool> >() != null);

            var result = dom.CheckIfLessThan(e1, e2);

            if (!result.IsNormal() || result.BoxedElement)
            {
                return(result);
            }
            else // if (result.BoxedElement == false)
            {
                return(CheckOutcome.Top);
            }
        }
示例#7
0
        public List <Expression> InferConstraints(Expression x, Expression exp, INumericalAbstractDomainQuery <Variable, Expression> adom)
        {
            Contract.Ensures(Contract.Result <List <Expression> >() != null);

            var result = new List <Expression>();

            if (adom == null)
            {
                return(result);
            }

            // This may appear straightforward and repeatitive, but sometimes the CheckIfxxx are more precise than what the abstract domain can track
            // (because of the refinement of the expressions, and the fact that they are "goal directed")
            // As a consequence, we add explicitely this information to the abstract domain
            var isGeqZero = adom.CheckIfGreaterEqualThanZero(exp);

            if (isGeqZero.IsNormal())
            {
                var zero = ExpressionManager.Encoder.ConstantFor(0);
                if (isGeqZero.IsTrue())
                {
                    // Discovered the disequality : 0 <= x

                    var newConstraint = ExpressionManager.Encoder.CompoundExpressionFor(ExpressionType.Bool, ExpressionOperator.GreaterEqualThan, x, zero);
                    result.Add(newConstraint);
                }
                else
                {
                    // Discovered the disequality : x < 0"

                    var newConstraint = ExpressionManager.Encoder.CompoundExpressionFor(ExpressionType.Bool, ExpressionOperator.LessThan, x, zero);
                    result.Add(newConstraint);
                }
            }

            return(result);
        }
示例#8
0
            public override Set <Expression> VisitShiftRight(Expression left, Expression right, Expression original, INumericalAbstractDomainQuery <Variable, Expression> data)
            {
                if (this.AreInfinities(left, right))
                {
                    return(new Set <Expression>());
                }

                var bounds = data.BoundsFor(right).AsInterval;
                int val;
                Polynomial <Variable, Expression> pol;

                if (bounds.IsFiniteAndInt32Singleton(out val) && val > 0 &&
                    Polynomial <Variable, Expression> .TryToPolynomialForm(left, this.Decoder, out pol) &&
                    pol.IsLinear && !pol.Relation.HasValue && pol.Left.Length <= (1 << val))
                {
                    var ub = null as IEnumerable <Expression>;

                    foreach (var m in pol.Left)
                    {
                        Variable v;
                        if (m.IsVariable(out v) && data.BoundsFor(v).LowerBound >= 0)
                        {
                            var upperbounds = data.UpperBoundsFor(v, true);
                            ub = ub == null ? upperbounds : ub.Intersect(upperbounds);

                            if (ub.Any())
                            {
                                continue;
                            }
                        }
                        return(new Set <Expression>());
                    }

                    // If we reach this point, we have meaningful constraints

                    return(new Set <Expression>(ub.ApplyToAll(exp => expManager.Encoder.CompoundExpressionFor(ExpressionType.Bool, ExpressionOperator.LessThan, x, exp))));
                }

                return(new Set <Expression>());
            }
示例#9
0
            public override Set <Expression> VisitModulus(Expression left, Expression right, Expression original, INumericalAbstractDomainQuery <Variable, Expression> data)
            {
                var result = new Set <Expression>();

                if (this.AreInfinities(left, right))
                {
                    return(result);
                }

                if (data.BoundsFor(right).LowerBound >= 0)
                {
                    result.Add(expManager.Encoder.CompoundExpressionFor(ExpressionType.Bool, ExpressionOperator.LessThan, x, right));     // Then assume x < right
                }

                return(result);
            }
示例#10
0
        public List <Expression> InferConstraints(Expression x, Expression exp, INumericalAbstractDomainQuery <Variable, Expression> oracleDomain)
        {
            Polynomial <Variable, Expression> expAsPol;
            Expression ltExp;

            var result = new List <Expression>();
            var xExp   = new Pair <Expression, Expression>(x, exp);

            #region 0. Fetch the cache
            Cache_Entry <Expression> cached;
            if (cache.TryGetValue(xExp, out cached))
            {
                switch (cached.Match_case)
                {
                case Cache_Entry.MatchCase.YMinusK:
                    result.Add(cached.ResultExpression);
                    result.AddRange(UpperBounds(cached.X, oracleDomain.UpperBoundsFor(cached.Y, false)));
                    break;

                case Cache_Entry.MatchCase.YPlusK:
                    result.Add(cached.ResultExpression);
                    result.AddRange(UpperBounds(cached.Y, oracleDomain.UpperBoundsFor(cached.X, false)));
                    break;

                case Cache_Entry.MatchCase.kY:
                    Helper_Match_kY(cached.X, oracleDomain, result, cached.Y);
                    break;

                case Cache_Entry.MatchCase.NotLinear:
                    // do nothing: this case essentially avoids the computation in the other branchs that we know to be unfruitfull
                    break;

                default:
                    // error?
                    break;
                }
            }
            #endregion

            #region 1. If failed, Try to put the r-exp in a polynomial (with simplifications, etc.)
            else if (Polynomial <Variable, Expression> .TryToPolynomialForm(exp, expManager.Decoder, out expAsPol))
            {
                if (expAsPol.IsLinear)
                {
                    // If it is in the form y - k, with k constant, then we add the constraint
                    Variable y;
                    Rational k;
                    if (expAsPol.TryMatch_YMinusK(out y, out k) && !x.Equals(y))
                    {
                        var yExp = expManager.Encoder.VariableFor(y);

                        ltExp = expManager.Encoder.CompoundExpressionFor(ExpressionType.Bool, ExpressionOperator.LessThan, x, yExp);
                        result.Add(ltExp);

                        result.AddRange(UpperBounds(x, oracleDomain.UpperBoundsFor(y, false)));

                        // update the cache
                        cache.Add(xExp, Cache_Entry.For(Cache_Entry.MatchCase.YMinusK, x, yExp, k, ltExp));
                    }
                    // If it is in the form y + k, with k constant, we add the constraint "y < x" to the back constraits
                    else if (expAsPol.TryMatch_YPlusK(out y, out k) && !x.Equals(y))
                    {
                        var yExp = expManager.Encoder.VariableFor(y);

                        ltExp = expManager.Encoder.CompoundExpressionFor(ExpressionType.Bool, ExpressionOperator.LessThan, yExp, x);

                        result.Add(ltExp);
                        result.AddRange(UpperBounds(yExp, oracleDomain.UpperBoundsFor(x, false)));

                        // update the cache
                        cache.Add(xExp, Cache_Entry.For(Cache_Entry.MatchCase.YPlusK, x, yExp, k, ltExp));
                    }
                    // If it is in the form k*y, with k constant, if k > 1 we add the constraint "x > y"
                    else if (expAsPol.TryMatch_kY(out y, out k) && !x.Equals(y))
                    {
                        var yExp = expManager.Encoder.VariableFor(y);
                        if (k > 1)
                        {
                            Helper_Match_kY(x, oracleDomain, result, yExp);
                            cache.Add(xExp, Cache_Entry.For(Cache_Entry.MatchCase.kY, x, yExp, k, default(Expression)));
                        }
                        else
                        {
                            cache.Add(xExp, Cache_Entry.ForNonLinear <Expression>());
                        }
                    }
                }
            }
            #endregion

            #region 2. Special treatment for reminder and non polynomial expressions

            result.AddRange(new TreatNonPolynomialCases(x, expManager).Visit(exp, oracleDomain));

            #endregion

            return(result);
        }
示例#11
0
            public override Set <Expression> VisitOr(Expression left, Expression right, Expression original, INumericalAbstractDomainQuery <Variable, Expression> data)
            {
                var result = new Set <Expression>();

                if (data == null || left == null || right == null)
                {
                    return(result);
                }

                if (data.CheckIfGreaterEqualThanZero(left).IsTrue() && data.CheckIfGreaterEqualThanZero(right).IsTrue())
                {
                    result.Add(expManager.Encoder.CompoundExpressionFor(ExpressionType.Bool, ExpressionOperator.LessEqualThan, left, x));
                    result.Add(expManager.Encoder.CompoundExpressionFor(ExpressionType.Bool, ExpressionOperator.LessEqualThan, right, x));
                }

                return(result);
            }
示例#12
0
 public void Assign(Expression x, Expression exp, INumericalAbstractDomainQuery <Variable, Expression> preState)
 {
     embedded = UnderlyingPolyhedra.Constrain(embedded, Converter.Box(encoder.CompoundExpressionFor(ExpressionType.Bool, ExpressionOperator.Equal, x, exp), decoder));
 }
示例#13
0
        public ArraySegmentationEnvironment <AbstractDomain, Variable, Expression> TestTrueInformationForTheSegments(INumericalAbstractDomainQuery <Variable, Expression> oracle)
        {
            #region Contracts
            Contract.Requires(oracle != null);

            Contract.Ensures(Contract.Result <ArraySegmentationEnvironment <AbstractDomain, Variable, Expression> >() != null);
            #endregion

            var result = Factory();

            foreach (var pair in this.Elements)
            {
                Contract.Assume(pair.Value != null);
                var reduced = pair.Value.TestTrueInformationForTheSegments(oracle);
                result.AddElement(pair.Key, reduced);
            }

            return(result);
        }
 public void Assign(Expression x, Expression exp, INumericalAbstractDomainQuery <Variable, Expression> preState)
 {
     this.Left.Assign(x, exp, preState);
     this.Right.Assign(x, exp /*, preState*/);
 }
 void IIntervalAbstraction <Variable, Expression> .Assign(Expression x, Expression exp, INumericalAbstractDomainQuery <Variable, Expression> preState)
 {
     this.Assign(x, exp, preState);
 }
 public void Assign(Expression x, Expression exp, INumericalAbstractDomainQuery <Variable, Expression> preState)
 {
     // do nothing
 }
示例#17
0
            public override Set <Expression> VisitAddition(Expression left, Expression right, Expression original, INumericalAbstractDomainQuery <Variable, Expression> data)
            {
                var result = new Set <Expression>();

                if (this.AreInfinities(left, right))
                {
                    return(result);
                }

                // It is x = left + right,
                if (data.BoundsFor(right).LowerBound > 0)
                {
                    result.Add(expManager.Encoder.CompoundExpressionFor(ExpressionType.Bool, ExpressionOperator.LessThan, left, x));     // Then assume left < x
                    result.Add(expManager.Encoder.CompoundExpressionFor(ExpressionType.Bool, ExpressionOperator.LessThan, this.Decoder.Stripped(left), x));
                }
                // It is x = left + (- Abs(right))
                else if (data.BoundsFor(right).UpperBound < 0)
                {
                    result.Add(expManager.Encoder.CompoundExpressionFor(ExpressionType.Bool, ExpressionOperator.LessThan, x, left));   // The assume x < left
                    result.Add(expManager.Encoder.CompoundExpressionFor(ExpressionType.Bool, ExpressionOperator.LessThan, x, this.Decoder.Stripped(left)));
                }

                if (data.BoundsFor(left).LowerBound > 0)
                {
                    result.Add(expManager.Encoder.CompoundExpressionFor(ExpressionType.Bool, ExpressionOperator.LessThan, right, x));     // Then assume right < x
                    result.Add(expManager.Encoder.CompoundExpressionFor(ExpressionType.Bool, ExpressionOperator.LessThan, this.Decoder.Stripped(right), x));
                }
                // It is x = -(Abs(left)) + right
                else if (data.BoundsFor(left).UpperBound < 0)
                {
                    result.Add(expManager.Encoder.CompoundExpressionFor(ExpressionType.Bool, ExpressionOperator.LessThan, x, right));   // The assume x < right
                    result.Add(expManager.Encoder.CompoundExpressionFor(ExpressionType.Bool, ExpressionOperator.LessThan, x, this.Decoder.Stripped(right)));
                }

                return(result);
            }
示例#18
0
 protected override Set <Expression> Default(INumericalAbstractDomainQuery <Variable, Expression> data)
 {
     return(new Set <Expression>());
 }
示例#19
0
            public override Set <Expression> VisitAddition(Expression left, Expression right, Expression original, INumericalAbstractDomainQuery <Variable, Expression> data)
            {
                var result = new Set <Expression>();

                if (data == null)
                {
                    return(result);
                }

                // x := left + right
                if (data.BoundsFor(right).LowerBound >= 0)
                {
                    result.Add(expManager.Encoder.CompoundExpressionFor(ExpressionType.Bool, ExpressionOperator.LessEqualThan, left, x));     // Then assume left <= x
                }

                if (data.BoundsFor(left).LowerBound >= 0)
                {
                    result.Add(expManager.Encoder.CompoundExpressionFor(ExpressionType.Bool, ExpressionOperator.LessEqualThan, right, x));     // Then assume right <= x
                }

                return(result);
            }
示例#20
0
        //[SuppressMessage("Microsoft.Contracts", "Ensures-106-407")]
        public List <Expression> InferConstraints(Expression x, Expression exp, INumericalAbstractDomainQuery <Variable, Expression> adom)
        {
            Contract.Assert(adom != null);

            var xExp   = new Pair <Expression, Expression>(x, exp);
            var result = new List <Expression>();

            Polynomial <Variable, Expression> expAsPol;

            var decoder = expManager.Decoder;
            var encoder = expManager.Encoder;

            if (Polynomial <Variable, Expression> .TryToPolynomialForm(exp, decoder, out expAsPol))
            {
                if (expAsPol.IsLinear)
                {
                    #region The cases
                    Expression leqExp;
                    Variable   y;
                    Rational   k;

                    // If it is in the form y - k, with k constant, then we add the constraint " x <= y "
                    if (expAsPol.TryMatch_YMinusK(out y, out k))
                    {
                        var yExp = encoder.VariableFor(y);

                        leqExp = encoder.CompoundExpressionFor(ExpressionType.Bool, ExpressionOperator.LessEqualThan, x, yExp);

                        Contract.Assert(leqExp != null);

                        result.Add(leqExp);  // add the constraint x <= y
                    }
                    // If it is in the form y + k, with k constant, we add the constraint "y <= x"
                    else if (expAsPol.TryMatch_YPlusK(out y, out k))
                    {
                        var yExp = encoder.VariableFor(y);

                        leqExp = encoder.CompoundExpressionFor(ExpressionType.Bool, ExpressionOperator.LessEqualThan, yExp, x);

                        Contract.Assert(leqExp != null);

                        result.Add(leqExp); // As we know x, this is a clever encoding for the constraint y <= x

                        // x = y + 1 then if y < z => x <= z
                        if (k == 1)
                        {
                            result.AddRange(UpperBoundsNonStrict(x, adom.UpperBoundsFor(y, true)));
                        }
                    }
                    // If it is in the form k * y then it add the constaints "y <= x" or "x <= y"
                    else if (expAsPol.TryMatch_kY(out y, out k))
                    {
                        var yExp = encoder.VariableFor(y);

                        Contract.Assert(yExp != null);

                        if (TryHelperFor_MatchkY(x, adom, yExp, k, out leqExp))
                        {
                            // F: this should be some bug, it is not taking into account the postcondition
                            Contract.Assume(leqExp != null);

                            result.Add(leqExp);
                        }
                    }
                    #endregion
                }
            }

            var nonPolynomial = new TreatNonPolynomialCases(x, expManager).Visit(exp, adom);

            Contract.Assert(nonPolynomial != null);

            result.AddRange(nonPolynomial);

            Contract.Assume(Contract.ForAll(result, e => e != null));

            return(result);
        }
示例#21
0
        /// <summary>
        /// We only add less equals
        /// </summary>
        public NonRelationalValueAbstraction <Variable, Expression> AssumeInformationFrom <Exp>(INumericalAbstractDomainQuery <Variable, Exp> oracle)
        {
            var result = this;

            if (this.IsNormal())
            {
                #region Update <
                if (this.StrictUpperBounds.IsNormal())
                {
                    var newConstraints = new Set <Variable>(this.StrictUpperBounds.Values);

                    foreach (var x in this.StrictUpperBounds.Values)
                    {
                        // Add S such that x <= S
                        var newBounds = oracle.UpperBoundsFor(x, true).ApplyToAll(oracle.ToVariable);
                        Contract.Assert(newBounds != null);
                        newConstraints.AddRange(newBounds);
                    }

                    result = result.Update(ADomains.StrictUpperBounds, new SetOfConstraints <Variable>(newConstraints, false));
                }
                #endregion

                #region Update <=
                if (this.WeakUpperBounds.IsNormal())
                {
                    var newConstraints = new Set <Variable>(this.WeakUpperBounds.Values);

                    foreach (var x in this.WeakUpperBounds.Values)
                    {
                        // Add S such that x <= S
                        var newBounds = oracle.UpperBoundsFor(x, false).ApplyToAll(oracle.ToVariable);
                        Contract.Assert(newBounds != null);
                        newConstraints.AddRange(newBounds);
                    }

                    result = result.Update(ADomains.WeakUpperBounds, new SetOfConstraints <Variable>(newConstraints, false));
                }
                #endregion
            }

            return(result);
        }