예제 #1
0
        public void InstallObjectInvariants(List <BoxedExpression> objectInvariants, Type type)
        {
            Contract.Requires(objectInvariants != null);

            if (objectInvariants.Count == 0)
            {
                return;
            }
            // Ordering by ToString should avoid in most of the cases the non-determinism problem we have
            // when the order of inferred invariants, causing the hash to change in the caching.
            var ordered = objectInvariants.OrderBy(inv => inv.ToString()).ToList();


            // TODO: COMMENT
            Console.WriteLine("Installing the object invariants");
            foreach (var e in ordered)
            {
                Console.WriteLine("   {0}", e);
            }
            // END TODO

            var seq = BoxedExpression.Sequence(ordered);
            var pc  = new BoxedExpression.PC(seq, 0);

            if (this.MethodCache.AddInvariant(type, pc, ClousotExpressionCodeProvider <Local, Parameter, Method, Field, Type> .Decoder))
            {
                foreach (var inv in ordered)
                {
                    this.mContractsHandlerMgr.RegisterClassInvariant(type, inv);
                }
            }
        }
예제 #2
0
        public void InstallPostconditions(List <BoxedExpression.AssertExpression> inferredPostconditions, Method method)
        {
            Contract.Requires(inferredPostconditions != null);

            if (inferredPostconditions.Count == 0)
            {
                return;
            }
            //this.Output.WriteLine("Trying to install {0} inferred postconditions.", inferredPostconditions.Count);
            // Ordering by ToString should avoid in most of the cases the non-determinism problem we have
            // when the order of inferred post conditions changes, causing the hash to change in the caching.
            var ordered = inferredPostconditions.OrderBy(post => post.ToString()).ToList();
            var seq     = BoxedExpression.Sequence(ordered.Cast <BoxedExpression>());

            BoxedExpression.PC postStart = new BoxedExpression.PC(seq, 0);
            bool result = this.MethodCache.AddPostCondition(method, postStart, ClousotExpressionCodeProvider <Local, Parameter, Method, Field, Type> .Decoder);

            if (result)
            {
                foreach (var post in ordered)
                {
                    this.mContractsHandlerMgr.RegisterMethodPostCondition(method, post.Condition);
                }
            }
        }
예제 #3
0
        public void InstallEntryAssumes(ICFG cfg, IEnumerable <Pair <BoxedExpression, Method> > inferredAssumes, Method method)
        {
            Contract.Requires(inferredAssumes != null);

            foreach (var inferredAssume in inferredAssumes)
            {
                var        assume     = inferredAssume.One;
                var        dummyAPC   = cfg.Entry; // TODO: do something better here! we don't know the APC yet, so we just pick some legitimate APC to avoid null ptr deref when looking up src context
                Provenance provenance = null;
                var        be         = new BoxedExpression.AssumeExpression(assume, "assume", dummyAPC, provenance, assume.ToString <Type>(type => OutputPrettyCS.TypeHelper.TypeFullName(this.MetaDataDecoder, type)));
                var        pc         = new BoxedExpression.PC(be, 0);

                this.MethodCache.AddEntryAssume(cfg, method, pc, ClousotExpressionCodeProvider <Local, Parameter, Method, Field, Type> .Decoder);
            }
        }
예제 #4
0
        /// <summary>
        /// Responsibility to make sure the assume can be properly decoded lies with the serializer/deserializer.
        /// The SER/DES should have enough detail and checks to make sure a deserialized expression can be decoded without
        /// failing due to stack mismatch or failed type assumptions.
        /// </summary>
        public void InstallCalleeAssumes(ICFG cfg, IEnumerable <Pair <BoxedExpression, Method> > inferredAssumes, Method method)
        {
            Contract.Requires(inferredAssumes != null);

#if false
            if (!inferredAssumes.Any())
            {
                return;
            }
            //this.Output.WriteLine("Trying to install {0} inferred assumes.", inferredAssumes.Count);

            // We lookup by name
            var locs   = this.MetaDataDecoder.Locals(method).Enumerate().Select(local => this.MetaDataDecoder.Name(local));
            var paramz = this.MetaDataDecoder.Parameters(method).Enumerate().Select(parameter => this.MetaDataDecoder.Name(parameter));
            var fields = this.MetaDataDecoder.Fields(this.MetaDataDecoder.DeclaringType(method)).Select(m => this.MetaDataDecoder.Name(method));
#endif
            // TODO: can the name / expr matching get mixed up during deserialization?
            foreach (var inferredAssume in inferredAssumes)
            {
                var assume = inferredAssume.One;
                //this.Output.WriteLine("Installing assume " + assume);
#if false
                #region Ensure that all var's in the assume expression still exist in the new version of the function

                // Check that the variables appearing in the assume to install exists in the scope.
                // We use the name of the variables to make sure they are the same variable
                var foundAllVariables = assume.Variables().TrueForAll(v =>
                {
                    var vToString = v.ToString();

                    // check for var in locs
                    var found = locs.Any(loc => vToString == loc);

                    // check for var in params
                    found = found || paramz.Any(param => vToString == param);
                    //string paramStr = this.MetaDataDecoder.ParameterType(paramz[j]) + " " + v.ToString(); // create str with same format as parameter string; hacky, but needed

                    // check for var in fields
                    found = found || fields.Any(fName =>
                    {
                        return(vToString == fName || // "normal" case
                               vToString == String.Format("this.{0}", fName)); // case where field prefixed with "this"
                    });

                    return(found);
                });
                if (!foundAllVariables)
                {
                    continue;
                }
                #endregion
                // found all locals, going to try to install this assume
#endif
                var        dummyAPC   = cfg.Entry; // TODO: do something better here! we don't know the APC yet, so we just pick some legitimate APC to avoid null ptr deref when looking up src context
                Provenance provenance = null;
                var        be         = new BoxedExpression.AssumeExpression(assume, "assume", dummyAPC, provenance, assume.ToString <Type>(type => OutputPrettyCS.TypeHelper.TypeFullName(this.MetaDataDecoder, type)));
                var        pc         = new BoxedExpression.PC(be, 0);

                var calleeName = inferredAssume.Two;

                this.MethodCache.AddCalleeAssumeAsPostCondition(cfg, method, calleeName, pc, ClousotExpressionCodeProvider <Local, Parameter, Method, Field, Type> .Decoder);
            }
        }