public static INumericalAbstractDomain <Variable, Expression> TestTrueEqual <Variable, Expression>(
            this INumericalAbstractDomain <Variable, Expression> aState,
            Expression exp, SetOfConstraints <Variable> equalities,
            IExpressionEncoder <Variable, Expression> encoder)
        {
            Contract.Requires(aState != null);
            Contract.Requires(exp != null);
            Contract.Requires(equalities != null);
            Contract.Requires(encoder != null);

            Contract.Ensures(Contract.Result <INumericalAbstractDomain <Variable, Expression> >() != null);

            if (equalities.IsNormal())
            {
                var newSet = new Set <Expression>();
                foreach (var v in equalities.Values)
                {
                    newSet.Add(encoder.VariableFor(v));
                }

                return(aState.TestTrueEqual(exp, newSet));
            }
            else
            {
                return(aState);
            }
        }
 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 static INumericalAbstractDomain <Variable, Expression> AssumeInInterval <Variable, Expression>(
            this INumericalAbstractDomain <Variable, Expression> aState,
            Expression exp, Interval intv, IExpressionEncoder <Variable, Expression> encoder)
        {
            Contract.Requires(aState != null);
            Contract.Requires(exp != null);
            Contract.Requires(intv != null);
            Contract.Requires(encoder != null);

            if (aState.IsBottom || intv.IsTop)
            {
                return(aState);
            }

            if (intv.IsBottom)
            {
                return(aState.Bottom as INumericalAbstractDomain <Variable, Expression>);
            }

            // a <= exp
            if (!intv.LowerBound.IsInfinity)
            {
                aState = aState.TestTrueLessEqualThan(intv.LowerBound.ToExpression(encoder), exp);
            }
            // b <= exp
            if (!intv.UpperBound.IsInfinity)
            {
                aState = aState.TestTrueLessEqualThan(exp, intv.UpperBound.ToExpression(encoder));
            }

            return(aState);
        }
        public static INumericalEnvironmentDomain <TVar, TExpr> AssumeInInterval <TVar, TExpr> (
            this INumericalEnvironmentDomain <TVar, TExpr> domain, TExpr expr, Interval intv,
            IExpressionEncoder <TVar, TExpr> encoder)
        {
            if (!domain.IsNormal())
            {
                return(domain);
            }

            if (intv.IsBottom)
            {
                return(domain.Bottom);
            }

            if (!intv.LowerBound.IsInfinity)
            {
                domain = domain.AssumeLessEqualThan(intv.LowerBound.ToExpression(encoder), expr);
            }

            if (!intv.UpperBound.IsInfinity)
            {
                domain = domain.AssumeLessEqualThan(expr, intv.LowerBound.ToExpression(encoder));
            }

            return(domain);
        }
 public override Expression Convert <Expression>(IExpressionEncoder <Variable, Expression> encoder)
 {
     return(encoder.CompoundExpressionFor(ExpressionType.Int32,
                                          ExpressionOperator.Addition,
                                          encoder.VariableFor(var),
                                          encoder.ConstantFor(value)));
 }
        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);
        }
 public SimpleArrayPropertiesAbstractDomain(SimpleArrayPropertiesAbstractDomain <Variable, Expression> source)
     : base(source)
 {
     this.arrayAbstractor = source.arrayAbstractor;
     this.encoder         = source.encoder;
     this.decoder         = source.decoder;
 }
        public bool TryGetEncoder(out IExpressionEncoder <Variable, Expression> encoder)
        {
            Contract.Ensures(!Contract.Result <bool>() || Contract.ValueAtReturn(out encoder) != null);

            encoder = this.encoder;

            return(encoder != null);
        }
        /// <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 #10
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();
        }
 public StripeForUnsafeCode(
     BoxedExpressionDecoder decoder,
     IExpressionEncoder <BoxedExpression> encoder,
     IExpressionContext <APC, Local, Parameter, Method, Field, Type, ExternalExpression, Variable> context,
     IDecodeMetaData <Local, Parameter, Method, Field, Property, Type, Attribute, Assembly> mdDecoder)
     : base(decoder, mdDecoder, encoder)
 {
     this.context = context;
     this.decoder = decoder;
 }
Beispiel #12
0
 public SubPolyhedraAnalysisForUnsafeCode(
     string methodName,
     IMethodDriver <APC, Local, Parameter, Method, Field, Property, Type, Attribute, Assembly, ExternalExpression, Variable, ILogOptions> mdriver,
     List <Analyzers.Unsafe.UnsafeOptions> options,
     IOverallUnsafeStatistics overallStats
     )
     : base(methodName, mdriver, options, overallStats)
 {
     this.encoder = BoxedExpressionEncoder.Encoder(this.DecoderForMetaData, this.Context);
 }
        public static INumericalAbstractDomain <Variable, Expression> TestTrue <Variable, Expression>(
            this INumericalAbstractDomain <Variable, Expression> aState,
            Variable v, NonRelationalValueAbstraction <Variable, Expression> nonRelationalValue,
            IExpressionEncoder <Variable, Expression> encoder)
        {
            Contract.Requires(v != null);
            Contract.Requires(aState != null);
            Contract.Requires(nonRelationalValue != null);
            Contract.Requires(encoder != null);

            Contract.Ensures(Contract.Result <INumericalAbstractDomain <Variable, Expression> >() != null);

            if (nonRelationalValue.IsBottom)
            {
                Contract.Assume(aState.Bottom as INumericalAbstractDomain <Variable, Expression> != null); // F: Adding the assumption as we do not track types

                return(aState.Bottom as INumericalAbstractDomain <Variable, Expression>);
            }

            if (nonRelationalValue.IsTop)
            {
                return(aState);
            }

            var result = aState;

            // Assume the interval
            if (nonRelationalValue.Interval.IsNormal())
            {
                result.AssumeInDisInterval(v, nonRelationalValue.Interval);
            }

            var vExp = encoder.VariableFor(v);

            // Assume the equalities
            if (nonRelationalValue.Equalities.IsNormal())
            {
                result = result.TestTrueEqual(vExp, nonRelationalValue.Equalities, encoder);
            }

            // Assume the strict upper bounds
            if (nonRelationalValue.StrictUpperBounds.IsNormal())
            {
                result = result.TestTrueLessThan(vExp, nonRelationalValue.StrictUpperBounds, encoder);
            }

            // Assume the weak upper bounds
            if (nonRelationalValue.WeakUpperBounds.IsNormal())
            {
                result = result.TestTrueLessEqualThan(vExp, nonRelationalValue.WeakUpperBounds, encoder);
            }

            return(result);
        }
Beispiel #14
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;
        }
        public static INumericalAbstractDomain <Variable, Expression> TestTrueLessThan <Variable, Expression>(
            this INumericalAbstractDomain <Variable, Expression> aState,
            Expression exp, Set <Variable> uppBounds,
            IExpressionEncoder <Variable, Expression> encoder)
        {
            Contract.Requires(aState != null);
            Contract.Requires(exp != null);
            Contract.Requires(uppBounds != null);
            Contract.Requires(encoder != null);

            Contract.Ensures(Contract.Result <INumericalAbstractDomain <Variable, Expression> >() != null);

            return(aState.TestTrueLessThan(exp, uppBounds.ConvertAll(v => encoder.VariableFor(v))));
        }
Beispiel #16
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 #17
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;
        }
 public StripeIntervalsKarrForUnsafeCode(
     StripeWithIntervalsForUnsafeCode /*!*/ left,
     LinearEqualitiesForUnsafeCode /*!*/ right,
     BoxedExpressionDecoder <Type, ExternalExpression> decoder,
     IExpressionEncoder <BoxedExpression> encoder,
     IExpressionContext <APC, Local, Parameter, Method, Field, Type, ExternalExpression, Variable> context,
     IDecodeMetaData <Local, Parameter, Method, Field, Property, Type, Attribute, Assembly> mdDecoder
     )
     : base(left, right, decoder, encoder)
 {
     this.decoder   = decoder;
     this.context   = context;
     this.mdDecoder = mdDecoder;
 }
        /// <summary>
        /// Construct left - right, or left iff right == 0
        /// </summary>
        public static Expression SmartSubtraction <Variable, Expression>(this IExpressionEncoder <Variable, Expression> encoder,
                                                                         Expression left, Expression right, IExpressionDecoder <Variable, Expression> decoder)
        {
            Contract.Requires(decoder != null);
            Contract.Requires(encoder != null);

            int value;

            if (decoder.IsConstantInt(right, out value) && value == 0)
            {
                return(left);
            }

            return(encoder.CompoundExpressionFor(ExpressionType.Int32, ExpressionOperator.Subtraction, left, right));
        }
Beispiel #20
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);
        }
        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 #22
0
                /// <summary>
                /// Given the length of a pointer that is null in one of the two branches of a join, add it to the domain all the contraints
                /// of the length that are on the other branch of the join
                /// </summary>
                /// <param name="pc">The pc at the join point</param>
                /// <param name="length">The length of the null pointer</param>
                /// <param name="result">The state on which we have to add the constraints</param>
                private void RefineWithNullPointerInformation(APC pc, BoxedExpression length, LinearEqualitiesForUnsafeCode thiscloned, LinearEqualitiesForUnsafeCode prevcloned, ref LinearEqualitiesForUnsafeCode result)
                {
                    Set <Polynomial <BoxedExpression> > eq = thiscloned.EqualsTo(length);

                    foreach (Polynomial <BoxedExpression> pol in eq)
                    {
                        IExpressionEncoder <BoxedExpression> encoder = BoxedExpressionEncoder.Encoder(this.mdDecoder, this.context);
                        BoxedExpression right = pol.ToPureExpression(encoder);
                        result = new LinearEqualitiesForUnsafeCode(result.TestTrueEqual(length, right), this.bdecoder, this.context, this.mdDecoder);
                    }
                    eq = prevcloned.EqualsTo(length);
                    foreach (Polynomial <BoxedExpression> pol in eq)
                    {
                        IExpressionEncoder <BoxedExpression> encoder = BoxedExpressionEncoder.Encoder(this.mdDecoder, this.context);
                        BoxedExpression right = pol.ToPureExpression(encoder);
                        result = new LinearEqualitiesForUnsafeCode(result.TestTrueEqual(length, right), this.bdecoder, this.context, this.mdDecoder);
                    }
                }
Beispiel #23
0
        public static TExpr ToExpression <TVar, TExpr>(this Rational value, IExpressionEncoder <TVar, TExpr> encoder)
        {
            if (value.IsInteger)
            {
                return(encoder.ConstantFor((long)value));
            }
            if (value.IsPlusInfinity)
            {
                return(encoder.CompoundFor(ExpressionType.Int32, ExpressionOperator.Div,
                                           encoder.ConstantFor(1L), encoder.ConstantFor(0L)));
            }
            if (value.IsMinusInfinity)
            {
                return(encoder.CompoundFor(ExpressionType.Int32, ExpressionOperator.Div,
                                           encoder.ConstantFor(-1L), encoder.ConstantFor(0L)));
            }

            TExpr l = encoder.ConstantFor(value.Up);
            TExpr r = encoder.ConstantFor(value.Down);

            return(encoder.CompoundFor(ExpressionType.Int32, ExpressionOperator.Div, l, r));
        }
 public StripeIntervalsKarr(StripeWithIntervals <Variable, Expression, MetaDataDecoder> /*!*/ left, LinearEqualitiesEnvironment <Variable, Expression> /*!*/ right, IExpressionDecoder <Variable, Expression> decoder, IExpressionEncoder <Variable, Expression> encoder, Logger Log)
     : base(left, right, Log)
 {
     this.decoder = decoder;
     this.encoder = encoder;
 }
        /// <summary>
        /// Perform some rewritings to the boxed expressions, so that is is simplified, and it is nicer for a human reader
        /// </summary>
        public static BoxedExpression MakeItPrettier <Variable>(this BoxedExpression be, IExpressionDecoder <BoxedVariable <Variable>, BoxedExpression> decoder, IExpressionEncoder <BoxedVariable <Variable>, BoxedExpression> encoder)
        {
            Contract.Requires(encoder != null);

            var result = be;

            // 0. Do not do any work on MinValue <= exp, as -exp <= -MinValue is wrong
            BinaryOperator  bop;
            BoxedExpression left, right;
            int             maybeMinInt; long maybeMinLong;

            if (be.IsBinaryExpression(out bop, out left, out right) &&
                (bop == BinaryOperator.Cle || bop == BinaryOperator.Clt || bop == BinaryOperator.Cle_Un || bop == BinaryOperator.Clt_Un) &&
                ((left.IsConstantInt(out maybeMinInt) && maybeMinInt == Int32.MinValue) || (left.IsConstantInt64(out maybeMinLong) && maybeMinLong == Int64.MinValue))
                )
            {
                return(result);
            }

            // 1. If it is a polynomial, do some arithmetic simplification, puts all the variables on one side, etc.
            Polynomial <BoxedVariable <Variable>, BoxedExpression> pol;

            if (Polynomial <BoxedVariable <Variable>, BoxedExpression> .TryToPolynomialForm(be, decoder, out pol))
            {
                if (pol.IsTautology)
                {
                    return(encoder.ConstantFor(true));
                }

                // m1 - m2 == 0 becomes m1 == m2
                BoxedVariable <Variable> x, y;
                Rational k;
                if (pol.TryMatch_XMinusYEqualk3(out x, out y, out k) && k.IsZero)
                {
                    var xExp = encoder.VariableFor(x);
                    var yExp = encoder.VariableFor(y);
                    return(encoder.CompoundExpressionFor(ExpressionType.Bool, ExpressionOperator.Equal, xExp, yExp));
                }

                result = pol.ToPureExpression(encoder);
            }

            // To do: add some more pretty printing there

            result = MakeItPrettier(result, x => encoder.ConstantFor(x));

            return(result);
        }
 public IntervalsForUnsafeCode(BoxedExpressionDecoder decoder, IExpressionEncoder <BoxedExpression> encoder, IExpressionContext <APC, Local, Parameter, Method, Field, Type, ExternalExpression, Variable> context)
     : base(decoder, encoder)
 {
     this.decoder = decoder;
     this.context = context;
 }
Beispiel #27
0
 public SimpleArrayAbstraction(SimpleArrayAbstraction <Variable, Expression> source)
 {
     this.encoder = source.encoder;
     this.decoder = source.decoder;
     this.content = new Dictionary <int, IAbstractDomainForEnvironments <Variable, Expression> >(source.content);
 }
Beispiel #28
0
 public SimpleArrayAbstraction(IExpressionEncoder <Variable, Expression> encoder, IExpressionDecoder <Variable, Expression> decoder)
 {
     this.encoder = encoder;
     this.decoder = decoder;
     this.content = new Dictionary <int, IAbstractDomainForEnvironments <Variable, Expression> >();
 }
Beispiel #29
0
 public SimpleArrayAbstractDomain(INumericalAbstractDomain <Variable, Expression> indexes, SimpleArrayPropertiesAbstractDomain <Variable, Expression> contents, IExpressionDecoder <Variable, Expression> decoder, IExpressionEncoder <Variable, Expression> encoder)
     : base(indexes, contents)
 {
     this.encoder = encoder;
     this.decoder = decoder;
 }
 private SymbolicExpressionsAbstractDomain(FunctionalAbstractDomain <Expression, FlatAbstractDomainWithComparer <Expression> > var2exp, IExpressionDecoder <Expression> decoder, IExpressionEncoder <Expression> encoder)
 {
     this.decoder = decoder;
     this.encoder = encoder;
     this.var2exp = var2exp;
 }