public static bool TryMatchVarPlusConst <Variable, Expression>(this IExpressionDecoder <Variable, Expression> decoder,
                                                                       Expression exp, out Variable var, out int k)
        {
            Contract.Requires(decoder != null);

            if (decoder.IsBinaryExpression(exp) && decoder.OperatorFor(exp) == ExpressionOperator.Addition)
            {
                var left  = decoder.LeftExpressionFor(exp);
                var right = decoder.RightExpressionFor(exp);

                if (decoder.IsConstantInt(left, out k) && decoder.IsVariable(right))
                {
                    var = decoder.UnderlyingVariable(right);

                    return(true);
                }

                if (decoder.IsConstantInt(right, out k) && decoder.IsVariable(left))
                {
                    var = decoder.UnderlyingVariable(left);

                    return(true);
                }
            }

            var = default(Variable);
            k   = default(int);
            return(false);
        }
            public TryConvertRead(IExpressionDecoder <Variable, Expression> decoder)
                : base(decoder)
            {
                Contract.Requires(decoder != null);

                result = default(NormalizedExpression <Variable>);
            }
        private static bool IsConstantTrueOrFalse <Variable, Expression>(
            Expression e, bool t, IExpressionDecoder <Variable, Expression> decoder)
        {
            Contract.Requires(decoder != null);

            bool  boolValue;
            Int32 intValue;

            if (decoder.IsConstant(e))
            {
                if (decoder.TryValueOf <bool>(e, ExpressionType.Bool, out boolValue))
                {
                    return(boolValue == t);
                }
                else if (decoder.IsConstantInt(e, out intValue))
                {
                    return((intValue != 0) == t);
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }
 public SimpleArrayPropertiesAbstractDomain(SimpleArrayPropertiesAbstractDomain <Variable, Expression> source)
     : base(source)
 {
     this.arrayAbstractor = source.arrayAbstractor;
     this.encoder         = source.encoder;
     this.decoder         = source.decoder;
 }
        public SubPoly(IExpressionDecoder <Expression> decoder, IExpressionEncoder <Expression> encoder)
        {
            IntervalEnvironment <Expression>         intv  = new IntervalEnvironment <Expression>(decoder, encoder);
            LinearEqualitiesEnvironment <Expression> lineq = new LinearEqualitiesEnvironment <Expression>(decoder, encoder, true, false);

            this.subpoly = new SubPolyhedra <Expression>(lineq, intv, decoder, encoder);
        }
 protected IntervalEnvironmentBase
     (IExpressionDecoder <TVar, TExpr> decoder,
     EnvironmentDomain <TVar, TInterval> varsToInterval)
 {
     Decoder           = decoder;
     vars_to_intervals = varsToInterval;
 }
 public SimpleArrayPropertiesAbstractDomain(IExpressionEncoder <Variable, Expression> encoder, IExpressionDecoder <Variable, Expression> decoder)
     : base()
 {
     this.arrayAbstractor = new SimpleArrayAbstraction <Variable, Expression>(encoder, decoder);
     this.encoder         = encoder;
     this.decoder         = decoder;
 }
 public ExpressionManagerWithEncoder(TimeOutChecker timeout, IExpressionDecoder <Variable, Expression> decoder, IExpressionEncoder <Variable, Expression> encoder, Logger log = null)
     : base(timeout, decoder, encoder)
 {
     Contract.Requires(timeout != null);
     Contract.Requires(decoder != null);
     Contract.Requires(encoder != null);
 }
        public static bool Match_E1aritopE2eq0 <Variable, Expression>(
            this IExpressionDecoder <Variable, Expression> decoder,
            Expression e1, Expression e2,
            out ExpressionOperator op, out Expression e11, out Expression e12)
        {
            Contract.Requires(decoder != null);

            Int32 value;

            if (decoder.IsConstantInt(e2, out value) && value == 0)
            {
                op = decoder.OperatorFor(e1);
                if (op.Equals(ExpressionOperator.Addition) || op.Equals(ExpressionOperator.Subtraction))
                {
                    e11 = decoder.LeftExpressionFor(e1);
                    e12 = decoder.RightExpressionFor(e1);
                    return(true);
                }
            }

            op  = default(ExpressionOperator);
            e11 = e12 = default(Expression);

            return(false);
        }
        /// <summary>
        /// Construct an empty abstract domain of symbolic expressions
        /// </summary>
        public SymbolicExpressionsAbstractDomain(IExpressionDecoder <Expression> decoder, IExpressionEncoder <Expression> encoder)
        {
            this.decoder = decoder;
            this.encoder = encoder;
            this.var2exp = new SimpleFunctional <Expression, FlatAbstractDomainWithComparer <Expression> >();

            InitStaticFields();
        }
Beispiel #11
0
        public static void Init(IExpressionDecoder <Variable, Expression> decoder, IExpressionEncoder <Variable, Expression> encoder)
        {
            linearfactory       = new LinearExpFactory <Expression>(decoder, encoder);
            propfactory         = new PropFactory <Expression>(decoder, encoder);
            UnderlyingPolyhedra = new AI.PolyhedraLattice(linearfactory, propfactory);

            UnderlyingPolyhedra.Validate();
        }
Beispiel #12
0
 public EvaluateExpressionVisitor(IExpressionDecoder <TVar, TExpr> decoder)
     : base(decoder)
 {
     occurences  = new VariableOccurences(decoder);
     constToIntv =
         new ConstToIntervalEvaluator
         <IntervalContextBase <TInterval, TNumeric>, TVar, TExpr, TInterval, TNumeric> (
             decoder);
 }
 private SimpleStringAbstractDomain(SimpleStringAbstractDomain <Variable, Expression> /*!*/ source)
 {
     decoder = source.decoder;
     foreach (var x in source.Keys)
     {
         var cloned = source[x].Clone() as IStringAbstraction;
         this[x] = cloned;
     }
 }
Beispiel #14
0
        private static string ConstantPrint <Variable, Expression>(Expression exp, IExpressionDecoder <Variable, Expression> decoder, int height)
        {
            Contract.Ensures(Contract.Result <string>() != null);

            var tmp = new VisitorForConstantPrint <Variable, Expression>(decoder).Visit(exp, Unit.Value);

            Contract.Assume(tmp != null); // F: to shut off Clousot for now

            return(tmp);
        }
            public IntervalTestVisitor(IExpressionDecoder <TVar, TExpr> decoder)
            {
                true_visitor =
                    new IntervalAssumeTrueVisitor <TVar, TExpr, TInterval, TNumeric> (decoder);
                false_visitor =
                    new IntervalAssumeFalseVisitor <TVar, TExpr, TInterval, TNumeric> (decoder);

                true_visitor.FalseVisitor = false_visitor;
                false_visitor.TrueVisitor = true_visitor;
            }
Beispiel #16
0
            public static IDictionary <TVar, Sequence <TInterval> > LessThan <TEnv, TVar, TExpr, TInterval>
                (TExpr left, TExpr right, IExpressionDecoder <TVar, TExpr> decoder, TEnv env, out bool isBottom)
                where TEnv : IIntervalEnvironment <TVar, TExpr, TInterval, Rational>
                where TVar : IEquatable <TVar>
                where TInterval : IntervalBase <TInterval, Rational>
            {
                isBottom = false;
                var result = new Dictionary <TVar, Sequence <TInterval> > ();

                var leftIntv  = env.Eval(left);
                var rightIntv = env.Eval(right);

                var rightVar  = decoder.UnderlyingVariable(right);
                var successor = IsFloat(left, decoder) || IsFloat(right, decoder) ? Rational.Zero : Rational.One;

                TInterval refinedIntv;

                if (TryRefineKLessThanRight <TEnv, TVar, TExpr, TInterval> (leftIntv, rightVar, successor, env, out refinedIntv) && !refinedIntv.IsSinglePoint)
                {
                    AddToResult(result, rightVar, refinedIntv);
                }

                if (successor.IsZero)
                {
                    return(result);
                }

                var leftVar = decoder.UnderlyingVariable(left);

                if (TryRefineLessThan <TEnv, TVar, TExpr, TInterval> (leftVar, rightIntv, env, out refinedIntv) && !refinedIntv.IsSinglePoint)
                {
                    AddToResult(result, leftVar, refinedIntv);
                }

                Polynomial <TVar, TExpr> poly;
                Polynomial <TVar, TExpr> leftPoly;
                Polynomial <TVar, TExpr> rightPoly;

                if (Polynomial <TVar, TExpr> .TryBuildFrom(left, decoder, out leftPoly) &&
                    Polynomial <TVar, TExpr> .TryBuildFrom(right, decoder, out rightPoly) &&
                    Polynomial <TVar, TExpr> .TryToPolynomial(ExpressionOperator.LessThan, leftPoly, rightPoly, out poly) &&
                    poly.IsLinear)
                {
                    if (poly.Left.Length == 1)
                    {
                        return(TestTrueLessEqualThan_AxLtK(poly, env, result, out isBottom));
                    }
                    if (poly.Left.Length == 2)
                    {
                        return(TestTrueLessEqualThan_AxByLtK(poly, env, result, out isBottom));
                    }
                }

                return(result);
            }
Beispiel #17
0
            static bool IsFloat <TVar, TExpr> (TExpr expr, IExpressionDecoder <TVar, TExpr> decoder)
            {
                if (decoder == null)
                {
                    return(false);
                }

                var type = decoder.TypeOf(expr);

                return(type == ExpressionType.Float32 || type == ExpressionType.Float64);
            }
        private static bool IsFloat <Variable, Expression>(Expression exp, IExpressionDecoder <Variable, Expression> decoder)
        {
            if (decoder == null)
            {
                return(false);
            }

            var type = decoder.TypeOf(exp);

            return(type == ExpressionType.Float32 || type == ExpressionType.Float64);
        }
 public StripeWithIntervalsForUnsafeCode(
     IntervalsForUnsafeCode /*!*/ left,
     StripeForUnsafeCode /*!*/ right,
     IExpressionDecoder <BoxedExpression> decoder,
     IExpressionContext <APC, Local, Parameter, Method, Field, Type, ExternalExpression, Variable> context,
     IDecodeMetaData <Local, Parameter, Method, Field, Property, Type, Attribute, Assembly> mdDecoder
     )
     : base(left, right, decoder)
 {
     this.context   = context;
     this.mdDecoder = mdDecoder;
 }
Beispiel #20
0
        private SimpleDisequalities(SimpleDisequalities <Variable, Expression> original)
            : base(original)
        {
            decoder = original.decoder;
            encoder = original.encoder;

            evalConstant = original.evalConstant;

            testTrueVisitor  = original.testTrueVisitor;
            testFalseVisitor = original.testFalseVisitor;

            checkIfHoldsVisitor = original.checkIfHoldsVisitor;
        }
Beispiel #21
0
        /// <returns>A string representation of the expression exp</returns>
        public static string ToString <Variable, Expression>(Expression exp, IExpressionDecoder <Variable, Expression> decoder)
        {
            Contract.Ensures(Contract.Result <string>() != null);

            Contract.Assume(!ToStringInvokedFromTheOutside); // This method can be invoked just from the outside of the class

            ToStringInvokedFromTheOutside = true;

            var result = ToString(exp, decoder, 0);

            ToStringInvokedFromTheOutside = false;

            return(result);
        }
Beispiel #22
0
        private PolyhedraEnvironment(IExpressionDecoder <Variable, Expression> decoder, IExpressionEncoder <Variable, Expression> encoder, AI.PolyhedraLattice.Element value, IntervalEnvironment <Variable, Expression> intv)
        {
            this.decoder = decoder;
            this.encoder = encoder;
            embedded     = value;
            this.intv    = intv;

            testTrueVisitor = new PolyhedraEnvironment <Expression> .PolyhedraTestTrueVisitor(decoder);

            testFalseVisitor = new PolyhedraEnvironment <Expression> .PolyhedraTestFalseVisitor(decoder);

            testTrueVisitor.FalseVisitor = testFalseVisitor;
            testFalseVisitor.TrueVisitor = testTrueVisitor;
        }
Beispiel #23
0
        public PolyhedraEnvironment(IExpressionDecoder <Variable, Expression> decoder, IExpressionEncoder <Variable, Expression> encoder)
        {
            this.decoder = decoder;
            this.encoder = encoder;
            embedded     = UnderlyingPolyhedra.Top;
            intv         = new IntervalEnvironment <Variable, Expression>(decoder, encoder);

            testTrueVisitor = new PolyhedraEnvironment <Expression> .PolyhedraTestTrueVisitor(decoder);

            testFalseVisitor = new PolyhedraEnvironment <Expression> .PolyhedraTestFalseVisitor(decoder);

            testTrueVisitor.FalseVisitor = testFalseVisitor;
            testFalseVisitor.TrueVisitor = testTrueVisitor;
        }
Beispiel #24
0
            public static IDictionary <TVar, Sequence <TInterval> > GreaterEqualThanZero <TEnv, TVar, TExpr, TInterval>
                (TExpr expr, IExpressionDecoder <TVar, TExpr> decoder, TEnv env)
                where TEnv : IIntervalEnvironment <TVar, TExpr, TInterval, Rational>
                where TVar : IEquatable <TVar>
                where TInterval : IntervalBase <TInterval, Rational>
            {
                var result   = new Dictionary <TVar, Sequence <TInterval> > ();
                var variable = decoder.UnderlyingVariable(expr);

                AddToResult(result, variable, env.Eval(expr).Meet(env.Context.Positive));

                if (!decoder.IsVariable(expr))
                {
                    Polynomial <TVar, TExpr> zeroPoly;                    // poly(0)
                    if (!Polynomial <TVar, TExpr> .TryToPolynomial(new[] { Monomial <TVar> .From(Rational.Zero) }, out zeroPoly))
                    {
                        throw new AbstractInterpretationException(
                                  "It can never be the case that the conversion of a list of monomials into a polynomial fails.");
                    }

                    Polynomial <TVar, TExpr> exprPoly;                    // poly(expr)
                    Polynomial <TVar, TExpr> fullPoly;                    // '0 <= poly(expr)' polynome
                    if (Polynomial <TVar, TExpr> .TryBuildFrom(expr, decoder, out exprPoly) &&
                        Polynomial <TVar, TExpr> .TryToPolynomial(ExpressionOperator.LessEqualThan, zeroPoly, exprPoly, out fullPoly) &&
                        fullPoly.IsIntervalForm)
                    {
                        var  k = fullPoly.Left[0].Coeff;                        // k != 0
                        TVar x;
                        fullPoly.Left[0].IsSingleVariable(out x);

                        Rational constraint;
                        if (Rational.TryDivide(fullPoly.Right[0].Coeff, k, out constraint))
                        {
                            TInterval interval;
                            if (k > 0L)                             // +x <= constraint
                            {
                                interval = env.Eval(x).Meet(env.Context.For(Rational.MinusInfinity, constraint));
                            }
                            else                             // -x <= -constraint ==> x >= constraint
                            {
                                interval = env.Eval(x).Meet(env.Context.For(constraint, Rational.PlusInfinity));
                            }

                            AddToResult(result, x, interval);
                        }
                    }
                }
                return(result);
            }
        static bool IsVariableEqConstant(Expression guard, IExpressionDecoder <Variable, Expression> decoder)
        {
            switch (decoder.OperatorFor(guard))
            {
            case ExpressionOperator.Equal:
            case ExpressionOperator.Equal_Obj:
                Expression left  = decoder.LeftExpressionFor(guard);
                Expression right = decoder.RightExpressionFor(guard);

                return((decoder.IsVariable(left) && decoder.IsConstant(right)) || (decoder.IsVariable(right) && decoder.IsConstant(right)));

            default:
                return(false);
            }
        }
        public ExpressionManager(TimeOutChecker timeout, IExpressionDecoder <Variable, Expression> decoder, IExpressionEncoder <Variable, Expression> encoder = null, Logger log = null)
        {
            Contract.Requires(decoder != null);

            Contract.Ensures(this.decoder == decoder);
            Contract.Ensures(this.encoder == encoder);
            Contract.Ensures(this.TimeOut != null);

            this.TimeOut = timeout ?? DFARoot.TimeOut;

            Contract.Assume(this.TimeOut != null, "weakeness with ?? ?");

            this.decoder = decoder;
            this.encoder = encoder;
            this.Log     = log == null ? VoidLogger.Log : log;
        }
Beispiel #27
0
        /// <summary>
        ///   Construct a new domain
        /// </summary>
        public SimpleDisequalities(IExpressionDecoder <Variable, Expression> /*!*/ decoder,
                                   IExpressionEncoder <Variable, Expression> /*!*/ encoder)
        {
            this.decoder = decoder;
            this.encoder = encoder;

            evalConstant = new ConstantVisitor(decoder);

            testTrueVisitor  = new SimpleDisequalitiesTestTrueVisitor(this.decoder, evalConstant);
            testFalseVisitor = new SimpleDisequalitiesTestFalseVisitor(this.decoder);

            testTrueVisitor.FalseVisitor = testFalseVisitor;
            testFalseVisitor.TrueVisitor = testTrueVisitor;

            checkIfHoldsVisitor = new SimpleDisequalitiesCheckIfHoldsVisitor(this.decoder, evalConstant);
        }
        static public bool TryConvertFrom <Expression>(
            Expression exp, IExpressionDecoder <Variable, Expression> decoder,
            out NormalizedExpression <Variable> result)
        {
            Contract.Requires(exp != null);

            if (decoder == null)
            {
                result = default(NormalizedExpression <Variable>);
                return(false);
            }

            var reader = new TryConvertRead <Expression>(decoder);

            return(reader.TryConvert(exp, out result));
        }
Beispiel #29
0
        public static bool TryGetAThreshold <TVar, TExpr> (TExpr e, IExpressionDecoder <TVar, TExpr> decoder, out List <int> thresholds)
        {
            var visitor = new GetThresholdVisitor <TVar, TExpr> (decoder);

            if (visitor.Visit(e, Dummy.Value))
            {
                thresholds = new List <int> ();
                foreach (var threshold in visitor.Thresholds)
                {
                    thresholds.Add(threshold - 1);
                    thresholds.Add(threshold);
                    thresholds.Add(threshold + 1);
                }
                return(true);
            }

            return(false.Without(out thresholds));
        }
Beispiel #30
0
 static public Exp <E> Box <E>(E exp, IExpressionDecoder <E> decoder)
 {
     if (decoder.IsVariable(exp))
     {
         return(new Var <E>(exp, decoder));
     }
     else
     {
         switch (decoder.OperatorFor(exp))
         {
         case ExpressionOperator.ConvertToInt32:
         case ExpressionOperator.ConvertToUInt16:
         case ExpressionOperator.ConvertToUInt32:
         case ExpressionOperator.ConvertToUInt8:
             return(Box(decoder.LeftExpressionFor(exp), decoder));
         }
         return(new Exp <E>(exp, decoder));
     }
 }