public override bool SuggestAnalysisSpecificPostconditions(
     ContractInferenceManager inferenceManager,
     IFixpointInfo <APC, ArrayState> fixpointInfo,
     List <BoxedExpression> postconditions)
 {
     return(false);
 }
Example #2
0
 public override bool SuggestAnalysisSpecificPostconditions(
     ContractInferenceManager inferenceManager,
     IFixpointInfo <APC, EnumDefined <BoxedVariable <Variable>, Type, BoxedExpression> > fixpointInfo,
     List <BoxedExpression> postconditions)
 {
     return(false);
 }
                public override void SuggestPrecondition(ContractInferenceManager inferenceManager, IFixpointInfo <APC, ArrayState> fixpointInfo)
                {
                    Contract.Assume(inferenceManager != null);
                    ArrayState postState;
                    var        pc = this.Context.MethodContext.CFG.NormalExit;

                    if (PreState(pc, fixpointInfo, out postState))
                    {
                        Contract.Assume(postState != null);

                        var mySubState = Select(postState);

                        if (mySubState.IsNormal())
                        {
                            foreach (var segmentation in mySubState.Elements)
                            {
                                Contract.Assume(segmentation.Key != null);
                                Contract.Assume(segmentation.Value != null);

                                if (segmentation.Value.IsNormal())
                                {
                                    foreach (var pre in GetForAllVisibleOutsideOfTheMethod(segmentation.Key, segmentation.Value))
                                    {
                                        var pcAfterRequires = this.Context.MethodContext.CFG.EntryAfterRequires;
                                        inferenceManager.AddPreconditionOrAssume(new EmptyProofObligation(pcAfterRequires), new List <BoxedExpression>()
                                        {
                                            pre
                                        }, ProofOutcome.Top);
                                    }
                                }
                            }
                        }
                    }
                }
Example #4
0
        public double Validate(IOutputResults output, ContractInferenceManager inferenceManager, IFactQuery <HighLevelExpression, Variable> query)
        {
            Contract.Requires(output != null);
            Contract.Requires(inferenceManager != null);
            Contract.Requires(query != null);

            throw new NotImplementedException();
        }
        public double Validate(IOutputResults output, ContractInferenceManager inferenceManager, IFactQuery <HighLevelExpression, Variable> query)
        {
            var result = 0.0; // is this used?

            foreach (var obl in this.underlying)
            {
                obl.Validate(output, inferenceManager, query);
            }
            return(result);
        }
        public double Validate(IOutputResults output, ContractInferenceManager inferenceManager, IFactQuery <HighLevelExpression, Variable> query)
        {
            var options           = output.LogOptions;
            var total_obligations = 0;
            var validated         = 0;

            var outcomes = new List <Pair <ProofObligation, ProofOutcome> >();

            foreach (var obl in obligations)
            {
                if (obl.IsAlreadyValidated)
                {
                    continue;
                }

                var outcome = obl.Validate(query, inferenceManager, output);

                // Discard unreached implicit obligations in contracts
                if (outcome == ProofOutcome.Bottom && obl.PC.InsideContract)
                {
                    continue;
                }

                total_obligations++;

                if (outcome == ProofOutcome.True)
                {
                    validated++;
                }

                this.stats.Add(outcome);

                outcomes.Add(new Pair <ProofObligation, ProofOutcome>(obl, outcome));
            }

            foreach (var pair in outcomes)
            {
                if (options.PrintOutcome(pair.Two))
                {
                    pair.One.EmitOutcome(pair.Two, output);
                }
            }

            return(total_obligations != 0 ? ((double)validated) / total_obligations : 1.0);
        }
        public ProofOutcome Validate(IFactQuery <Expression, Variable> query, ContractInferenceManager inferenceManager, IOutputResults output)
        {
            ProofOutcome outcome;

            if (query.IsUnreachable(this.PCForValidation))
            {
                outcome = ProofOutcome.Bottom;
            }
            else
            {
                outcome = ValidateInternal(query, inferenceManager, output);

                if (outcome == ProofOutcome.Top || outcome == ProofOutcome.False)
                {
                    if (this.Condition != null && this.TryInferPrecondition && !output.IsMasked(this.GetWitness(outcome)))
                    {
                        InferredConditions inferredPreConditions;

                        var inferencer = inferenceManager.PreCondition.Inference;
                        if (inferencer.TryInferConditions(this, inferenceManager.CodeFixesManager, out inferredPreConditions))
                        {
                            var context = inferredPreConditions.PushToContractManager(inferenceManager, inferencer.ShouldAddAssumeFalse, this, ref outcome, output.LogOptions);

                            this.HasASufficientAndNecessaryCondition = inferredPreConditions.HasASufficientCondition;
                            this.AdditionalInformationOnTheWarning.AddRange(context);
                        }
                        else if (inferencer.ShouldAddAssumeFalse)
                        {
                            inferenceManager.Assumptions.AddEntryAssumes(this, new BoxedExpression[] { BoxedExpression.ConstFalse });
                        }
                    }
                }
            }

            this.Outcome = outcome;

            return(outcome);
        }
 protected override ProofOutcome ValidateInternal(IFactQuery <BoxedExpression, Variable> query, ContractInferenceManager inferenceManager, IOutputResults output)
 {
     return(query.IsTrue(this.PC, this.condition));
 }
                protected override sealed ProofOutcome ValidateInternal(IFactQuery <BoxedExpression, Variable> query, ContractInferenceManager inferenceManager, IOutputResults output)
                {
                    if (this.MethodDriver.Options.TraceChecks)
                    {
                        output.WriteLine("Validating proof obligation: {0}", this.Condition != null ? this.Condition.ToString() : "<null?>");
                    }

                    var result = ValidateInternalSpecific(query, inferenceManager, output);

                    if (result != ProofOutcome.Top)
                    {
                        return(result);
                    }

                    var condition = this.Condition;

                    if (condition != null)
                    {
                        WeakestPreconditionProver.AdditionalInfo why;
                        if (TryDischargeProofObligationWithWeakestPreconditions(condition, query, inferenceManager, output, out why))
                        {
                            return(ProofOutcome.True);
                        }
                        else
                        {
                            this.AdditionalInformationOnTheWarning.AddRange(WarningContextFetcher.InferContext(this.PC, condition, this.Context, this.DecoderForMetaData.IsBoolean));
                            this.AdditionalInformationOnTheWarning.AddRange(why.GetWarningContexts());
                        }
                    }
                    return(ProofOutcome.Top);
                }
                public override void SuggestPrecondition(ContractInferenceManager inferenceManager, IFixpointInfo <APC, ArrayState> fixpointinfo)
                {
                    Contract.Assume(inferenceManager != null, "Assume for inheritance");

                    if (
                        !this.Options.LogOptions.SuggestRequiresPurityForArrays &&
                        !this.Options.LogOptions.PropagateRequiresPurityForArrays)
                    {
                        return;
                    }

                    ArrayState returnState;

                    if (PreState(this.Context.MethodContext.CFG.NormalExit, fixpointinfo, out returnState))
                    {
                        Contract.Assume(returnState != null);
                        var mySubState = Select(returnState);

#pragma warning disable 0219
                        var first = true; // used in the commented part below
#pragma warning restore

                        foreach (var element in mySubState.Elements)
                        {
#if false // Make it true to debug the content of the result state
                            if (ContainsUnModifiedSegment(element.Value))
                            {
                                if (first)
                                {
                                    Console.WriteLine("Method: {0}", this.Context.MethodContext.CurrentMethod);
                                    first = false;
                                }

                                var arrayName = this.VariablesToPrettyNames(this.Context.MethodContext.CFG.EntryAfterRequires, element.Key);
                                if (arrayName != null)
                                {
                                    var preconditions = element.Value.PrettyPrint(
                                        arrayName,
                                        (var => this.VariablesToPrettyNames(this.Context.MethodContext.CFG.EntryAfterRequires, var)),
                                        (ael => this.PrettyPrint(arrayName, ael))
                                        );
                                    foreach (var str in preconditions)
                                    {
                                        output.WriteLine("Effect on the input array segment: {0}", str);
                                    }
                                }
                            }
#endif
                            var exp = ToBoxedExpression(this.Context.MethodContext.CFG.Entry, element.Key);
                            var pre = PreconditionSuggestion.ExpressionInPreState(exp, this.Context,
                                                                                  this.DecoderForMetaData, this.Context.MethodContext.CFG.EntryAfterRequires,
                                                                                  allowedKinds: ExpressionInPreStateKind.MethodPrecondition);

                            Contract.Assume(element.Value != null);

                            if (pre != null &&
                                AllSegmentsUnmodified(element.Value) &&
                                !this.DecoderForContracts.IsPure(this.Context.MethodContext.CurrentMethod)
                                )
                            {
                                Parameter p;
                                int       pos;
                                Contract.Assume(element.Key != null, "Assuming the precondition");
                                if (TryParameterFor(element.Key, out p, out pos) && !this.DecoderForMetaData.IsPure(this.Context.MethodContext.CurrentMethod, p))
                                {
                                    // TODO: wire those suggestions to the contract inference manager
                                    if (this.Options.LogOptions.SuggestRequiresPurityForArrays)
                                    {
                                        inferenceManager.Output.Suggestion(ClousotSuggestion.Kind.Requires, ClousotSuggestion.Kind.Requires.Message(), this.Context.MethodContext.CFG.EntryAfterRequires,
                                                                           string.Format("Consider adding the [Pure] attribute to the parameter {0}", pre.ToString()), null, ClousotSuggestion.ExtraSuggestionInfo.None);
                                    }

                                    if (this.Options.LogOptions.PropagateRequiresPurityForArrays && pos <= 64)
                                    {
                                        this.DecoderForMetaData.AddPure(this.Context.MethodContext.CurrentMethod, pos);
                                    }
                                }
                            }
                        }
                    }
                }
Example #11
0
                protected override ProofOutcome ValidateInternalSpecific(IFactQuery <BoxedExpression, Variable> query, ContractInferenceManager inferenceManager, IOutputResults output)
                {
                    // First, we check if arg != MinValue

                    object value;

                    if (!this.DecoderForMetaData.TryGetMinValueForType(this.typeOfArg, out value))
                    {
                        return(ProofOutcome.Top);
                    }

                    var condition = this.Condition;

                    if (condition != null) // this.Condition may return null
                    {
                        var result = query.IsTrue(this.PC, condition);

                        if (result != ProofOutcome.Top)
                        {
                            return(result);
                        }
                    }
                    // We check a sufficient condition: if arg is >= 0, then it is not the negation of a negative "extreme"
                    var greaterThanZero = query.IsGreaterEqualToZero(this.PC, this.arg);

                    return((greaterThanZero == ProofOutcome.True || greaterThanZero == ProofOutcome.Bottom)
            ? greaterThanZero : ProofOutcome.Top);
                }
Example #12
0
                protected override ProofOutcome ValidateInternalSpecific(IFactQuery <BoxedExpression, Variable> query, ContractInferenceManager inferenceManager, IOutputResults output)
                {
                    // We check the condition
                    var condition = this.Condition;

                    if (condition == null)
                    {
                        return(ProofOutcome.Top);
                    }

                    return(query.IsTrue(this.PC, condition));
                }
Example #13
0
 protected override ProofOutcome ValidateInternalSpecific(IFactQuery <BoxedExpression, Variable> query, ContractInferenceManager inferenceManager, IOutputResults output)
 {
     return(query.IsNonZero(this.PC, this.denominator));
 }
                private bool TryDischargeProofObligationWithWeakestPreconditions(BoxedExpression condition, IFactQuery <BoxedExpression, Variable> query, ContractInferenceManager inferenceManager, IOutputResults output, out WeakestPreconditionProver.AdditionalInfo why)
                {
                    var options = output.LogOptions;

                    // Try to validate the assertion by WP inference
                    if (options.UseWeakestPreconditions)
                    {
                        var  driver = this.MethodDriver;
                        bool messageAlreadyPrinted;
                        if (driver.SyntacticComplexity.ShouldAvoidWPComputation(out messageAlreadyPrinted))
                        {
                            if (!messageAlreadyPrinted)
                            {
                                output.WriteLine("Skipping backwards computation for this method ({0}) as cccheck thinks it will cause a timeout", driver.MetaDataDecoder.Name(driver.CurrentMethod));
                            }
                            this.AdditionalInformationOnTheWarning.Add(new WarningContext(WarningContext.ContextType.WPSkippedBecauseAdaptiveAnalysis));
                        }
                        else
                        {
                            var path = WeakestPreconditionProver.Discharge(this.PC, condition, options.MaxPathSize, this.MethodDriver, query, inferenceManager, out why);
                            return(path == null);
                        }
                    }
                    why = WeakestPreconditionProver.AdditionalInfo.None;
                    return(false);
                }
 virtual public void SuggestPrecondition(ContractInferenceManager inferenceManager, IFixpointInfo <APC, ArrayState> fixpointInfo)
 {
     // does nothing by default
 }
 /// <summary>
 /// Does nothing, as a plugin as no fixpoint
 /// </summary>
 public override void SuggestPrecondition(ContractInferenceManager inferenceManager)
 {
     return;
 }
                    protected override ProofOutcome ValidateInternal(IFactQuery <BoxedExpression, Variable> query, ContractInferenceManager inferenceManager, IOutputResults output)
                    {
                        ProofOutcome outcome;

                        if ((outcome = query.IsVariableDefinedForType(this.PC, this.value, this.type)) != ProofOutcome.Top)
                        {
                            return(outcome);
                        }

                        return(ProofOutcome.Top);
                    }
                    protected override ProofOutcome ValidateInternalSpecific(IFactQuery <BoxedExpression, Variable> query, ContractInferenceManager inferenceManager, IOutputResults output)
                    {
                        Contract.Assume(query != null, "should be a precondition");

                        if (this.uncheckable)
                        {
                            return(ProofOutcome.Top);
                        }

                        return(query.IsTrue(this.PC, condition));
                    }
 protected override ProofOutcome ValidateInternalSpecific(IFactQuery <BoxedExpression, Variable> query, ContractInferenceManager inferenceManager, IOutputResults output)
 {
     // Check if size >= 0
     return(query.IsGreaterEqualToZero(this.PC, this.size));
 }
 public override bool SuggestAnalysisSpecificPostconditions(ContractInferenceManager inferenceManager, IFixpointInfo <APC, DisjunctiveRefinement> fixpointInfo, List <BoxedExpression> postconditions)
 {
     // does nothing
     return(false);
 }
Example #21
0
                /// <summary>
                /// If we cannot validate the precision of the operands, we try to suggest an explicit cast
                /// </summary>
                protected override ProofOutcome ValidateInternalSpecific(IFactQuery <BoxedExpression, Variable> query, ContractInferenceManager inferenceManager, IOutputResults output)
                {
                    var outcome = query.HaveSameFloatType(PC, this.left, this.right);

                    switch (outcome)
                    {
                    case ProofOutcome.Top:
                    {
                        ConcreteFloat leftType, rightType;

                        if (query.TryGetFloatType(this.PC, this.left, out leftType) && query.TryGetFloatType(this.PC, this.right, out rightType))
                        {
                            APC conditionPC;
                            if (!this.MethodDriver.AdditionalSyntacticInformation.VariableDefinitions.TryGetValue(this.var, out conditionPC))
                            {
                                conditionPC = this.PC;
                            }

                            if (inferenceManager.CodeFixesManager.TrySuggestFloatingPointComparisonFix(this, conditionPC, this.left, this.right, leftType, rightType))
                            {
                                // do something? Returning true seems a bad idea
                            }
                        }

                        return(outcome);
                    }

                    default:
                    {
                        return(outcome);
                    }
                    }
                }
                protected override ProofOutcome ValidateInternalSpecific(IFactQuery <BoxedExpression, Variable> query, ContractInferenceManager inferenceManager, IOutputResults output)
                {
                    // Check if expForIndex >= 0
                    return(query.IsGreaterEqualToZero(this.PC, index));

#if false
                    // Try to use weakest preconditions
                    if (result == ProofOutcome.Top)
                    {
                        if (CanAssumeLowerBoundPrecondition(output, this.index))
                        {
                            result = ProofOutcome.True;
                        }
                        else
                        {
                            // if we have lb <= i, then we can suggest 0 <= lb
                            //
                            foreach (Variable lb in query.LowerBounds(this.PC, index, false))
                            {
                                BoxedExpression lbbox = BoxedExpression.Var(lb);
                                if (CanAssumeLowerBoundPrecondition(output, lbbox))
                                {
                                    result = ProofOutcome.True;
                                }
                            }
                        }
                    }

                    return(result);
#endif
                }
Example #23
0
                protected override ProofOutcome ValidateInternalSpecific(IFactQuery <BoxedExpression, Variable> query, ContractInferenceManager inferenceManager, IOutputResults output)
                {
                    object minValue;

                    if (!this.DecoderForMetaData.TryGetMinValueForType(this.TypeOp1, out minValue))
                    {
                        return(ProofOutcome.Top);
                    }

                    // first, we check that Op1 != MinValue
                    var condition1 = BoxedExpression.Binary(BinaryOperator.Cne_Un, this.Op1, BoxedExpression.Const(minValue, this.TypeOp1, this.DecoderForMetaData));
                    var resultOp1  = query.IsTrue(this.PC, condition1);

                    // second, we check Op2 != -1
                    var condition2 = BoxedExpression.Binary(BinaryOperator.Cne_Un, this.Op2, BoxedExpression.Const(-1, this.DecoderForMetaData.System_Int32, this.DecoderForMetaData));
                    var resultOp2  = query.IsTrue(this.PC, condition2);

                    // One of the two conditions is true, so it is ok!
                    if (resultOp1 == ProofOutcome.True || resultOp2 == ProofOutcome.True)
                    {
                        return(ProofOutcome.True);
                    }

                    // Both conditions are false, so division is definitely an overflow
                    if (resultOp1 == ProofOutcome.False && resultOp2 == ProofOutcome.False)
                    {
                        return(ProofOutcome.False);
                    }

                    return(ProofOutcome.Top);
                }
                protected override ProofOutcome ValidateInternalSpecific(IFactQuery <BoxedExpression, Variable> query, ContractInferenceManager inferenceManager, IOutputResults output)
                {
                    var outcome = query.IsLessThan(this.PC, this.left, this.right);

                    switch (outcome)
                    {
                    case ProofOutcome.Top:
                    {
                        // Let us see if there is a possible off-by-one
                        var offByOne = query.IsTrue(this.PC, BoxedExpression.Binary(BinaryOperator.Cle, this.left, this.right));
                        if (offByOne == ProofOutcome.True)
                        {
                            this.AdditionalInformationOnTheWarning.Add(new WarningContext(WarningContext.ContextType.OffByOne));
                        }

                        return(outcome);
                    }

                    case ProofOutcome.Bottom:
                    case ProofOutcome.False:
                    case ProofOutcome.True:
                    {
                        return(outcome);
                    }

                    default:
                    {
                        Contract.Assert(false);
                        return(outcome); // unreached
                    }
                    }
                }
Example #25
0
                protected override ProofOutcome ValidateInternalSpecific(IFactQuery <BoxedExpression, Variable> query, ContractInferenceManager inferenceManager, IOutputResults output)
                {
                    // If it is a variable or a constant, then there is nothing to do, and we return true.

                    if (this.exp.IsConstant || this.exp.IsVariable)
                    {
                        return(ProofOutcome.True);
                    }

                    return(base.ValidateInternalSpecific(query, inferenceManager, output));
                }
                public override bool SuggestAnalysisSpecificPostconditions(
                    ContractInferenceManager inferenceManager,
                    IFixpointInfo <APC, ArrayState> fixpointInfo, List <BoxedExpression> postconditions)
                {
                    Contract.Assert(this.MethodDriver.CFG != null);
                    var exitPC = this.MethodDriver.CFG.NormalExit;

                    ArrayState exitState;

                    if (fixpointInfo.PostState(exitPC, out exitState))
                    {
                        var mySubState = Select(exitState);
                        foreach (var pair in mySubState.Elements)
                        {
                            // Try to get a variable name for it
                            var variable = ReadVariableInPostState(exitPC, pair.Key);

                            if (variable == null)
                            {
                                // we fail to get a name in the post state, let us see if it is a constant
                                Int64 k;
                                var   intv = exitState.Numerical.BoundsFor(pair.Key);
                                if (intv.IsSingleton && intv.LowerBound.TryInt64(out k))
                                {
                                    variable = BoxedExpression.Const(k, this.DecoderForMetaData.System_Int64, this.DecoderForMetaData);
                                }
                                else
                                {
                                    continue;
                                }
                            }

                            Contract.Assert(variable != null);

                            Contract.Assume(pair.Value != null);

                            if (pair.Value.IsNormal() && pair.Value.IsUnmodifiedFromEntry.IsTrue())
                            {
                                foreach (var arrayBoxed in pair.Value.Left.Values)
                                {
                                    Contract.Assume(arrayBoxed != null);

                                    var arrayType = GetTypeOrObject(exitPC, arrayBoxed);

                                    var array = ReadVariableInPostState(exitPC, arrayBoxed);
                                    if (array != null)
                                    {
                                        // If we have one or more definite values for the index expression, we use them
                                        if (pair.Value.Right.IsNormal())
                                        {
                                            foreach (var indexBoxed in pair.Value.Right.Values)
                                            {
                                                BoxedExpression newPost = null;

                                                Contract.Assume(indexBoxed != null);
                                                var index = ReadVariableInPostState(exitPC, indexBoxed);

                                                if (index == null)
                                                {
                                                    newPost = GenerateTheExists(exitPC, variable, arrayType, array);
                                                }
                                                else
                                                {
                                                    #region Generate arr[index] == variable
                                                    var arrayLoad = new BoxedExpression.ArrayIndexExpression <Type>(array, index, arrayType);

                                                    newPost = BoxedExpression.Binary(BinaryOperator.Ceq, arrayLoad, variable);
                                                    #endregion
                                                }

                                                postconditions.AddIfNotNull(newPost);
                                            }
                                        }
                                        // else, if we know it is top, we can simp,y project over it
                                        else if (pair.Value.Right.IsTop)
                                        {
                                            postconditions.AddIfNotNull(GenerateTheExists(exitPC, variable, arrayType, array));
                                        }
                                    }
                                }
                            }
                        }
                    }

                    return(false);
                }
Example #27
0
 public override bool SuggestAnalysisSpecificPostconditions(ContractInferenceManager inferenceManager, IFixpointInfo <APC, SimpleStringAbstractDomain <BoxedVariable <Variable>, BoxedExpression> > fixpointInfo, List <BoxedExpression> postconditions)
 {
     // does nothing
     return(false);
 }
 /// <summary>
 /// Try to validate the embodied proof obligation and return the outcome.
 /// The IFactQuery interface is for future logic based implementation of fact querying.
 /// The validation can make queries or use internal state to answer the outcome.
 /// </summary>
 protected abstract ProofOutcome ValidateInternal(IFactQuery <Expression, Variable> query, ContractInferenceManager inferenceManager, IOutputResults output);
        protected override ProofOutcome ValidateInternal(IFactQuery <Exp, Var> query, ContractInferenceManager inferenceManager, IOutputResults output)
        {
            Contract.Requires(query != null);
            Contract.Requires(inferenceManager != null);

            throw new NotImplementedException();
        }
 abstract protected ProofOutcome ValidateInternalSpecific(IFactQuery <BoxedExpression, Variable> query, ContractInferenceManager contractManager, IOutputResults output);