Пример #1
0
        private void GatherDebuggingInfoFromInsufficiency(IExecutionNode executionNode, TermManager termManager,
                                                          Term condition, TypeEx explorableType)
        {
            var sb = new SafeStringBuilder();

            sb.AppendLine("condition:");
            sb.AppendLine();
            this.tba.ConvertTermToText(new SafeStringWriter(sb), condition, this.ExplorationServices.TermManager);
            sb.AppendLine();
            var swriter = new TermSExpWriter(termManager, new SafeStringWriter(sb), true, false);

            swriter.Write(condition);
            sb.AppendLine();
            sb.AppendLine("location:");
            sb.AppendLine();
            sb.AppendLine(executionNode.CodeLocation.ToString());
            sb.AppendLine();
            sb.AppendLine("properties:");

            Term unnegatedCondition;

            if (termManager.TryGetInnerLogicallyNegatedValue(condition, out unnegatedCondition))
            {
                sb.AppendLine("negated");
            }
            else
            {
                unnegatedCondition = condition;
            }

            var            targetFieldValues = new SafeDictionary <Field, object>();
            Term           left, right;
            BinaryOperator binOp;

            if (termManager.TryGetBinary(unnegatedCondition, out binOp, out left, out right))
            {
                sb.AppendFormat("binary relation: {0}", binOp);
                sb.AppendLine();

                if (!termManager.IsValue(left) && !termManager.IsValue(right))
                {
                    sb.AppendLine("No constant on either left side or right side.");
                    return;
                }

                Term non_constant_term = null;
                Term constant_term     = null;
                if (termManager.IsValue(left))
                {
                    non_constant_term = right;
                    constant_term     = left;
                }
                else if (termManager.IsValue(right))
                {
                    non_constant_term = left;
                    constant_term     = right;
                }

                sb.AppendLine("against constant");
                if (constant_term == null || termManager.IsDefaultValue(constant_term))
                {
                    sb.AppendLine("against default value ('null' for references)");
                }

                int value;
                if (constant_term != null && termManager.TryGetI4Constant(constant_term, out value))
                {
                    sb.AppendLine("against integer: " + value);
                }

                Term           objectValue;
                ObjectProperty objectProperty;
                if (constant_term != null && termManager.TryGetObjectProperty(constant_term, out objectValue, out objectProperty))
                {
                    sb.AppendLine("against object property: object=" + objectValue + ", property=" + objectProperty);
                }

                sb.AppendLine(" involving fields: ");
                SafeDictionary <Field, FieldValueHolder> innerFieldValues;
                SafeList <TypeEx> innerFieldTypes;
                SafeList <Field>  fs = TargetBranchAnalyzer.GetInvolvedFields(this, termManager,
                                                                              non_constant_term, out innerFieldValues, out innerFieldTypes);
                foreach (var f in fs)
                {
                    sb.AppendLine(f.FullName);
                }
            }

            sb.AppendLine("Executed method call sequence");
            if (this.pmd.LastExecutedFactoryMethodCallSequence != null)
            {
                foreach (var m in this.pmd.LastExecutedFactoryMethodCallSequence)
                {
                    sb.AppendLine("\t" + m);
                }
            }

            this.Log.Dump("foo", "insufficiency for " + (explorableType != null ? explorableType.FullName : "?"), sb.ToString());
            return;
        }
Пример #2
0
        internal void AddResult(Method method, Field f, Term[] indices, Term fieldValue)
        {
            /* SafeDictionary<Field, SafeSet<Term>> methodResults;
             * if (!this.FieldAccessInfoContainer.Results.TryGetValue(method, out methodResults))
             *  this.FieldAccessInfoContainer.Results[method] = methodResults = new SafeDictionary<Field, SafeSet<Term>>();
             * SafeSet<Term> fieldResults;
             * if (!methodResults.TryGetValue(f, out fieldResults))
             *  methodResults[f] = fieldResults = new SafeSet<Term>();
             */
            //TODO why do we need it?

            /*
             * if (database == null)
             * {
             *  database = new SeqexDatabase();
             *  FieldAccessInfoObj = database.FieldAccessInfoObj;
             * }
             */
            string arrayIndex = "";

            using (SomeRewriter someRewriter = new SomeRewriter(this.termManager))
            {
                fieldValue = someRewriter.VisitTerm(default(TVoid), fieldValue);
                //update the field value to accomodate array-type field
                //if (indices.Length == 0)//not an array-type field
                if (indices.Length == 1) //is an array-type field
                {
                    arrayIndex = " at index of " + indices[0].UniqueIndex.ToString();
                }
            }

            System.Collections.Generic.Dictionary <string, HashSet <string> > methodResults;
            if (!this.FieldAccessInfoObj.Results.TryGetValue(method.FullName, out methodResults))
            {
                this.FieldAccessInfoObj.Results[method.FullName] =
                    methodResults = new System.Collections.Generic.Dictionary <string, HashSet <string> >();
            }
            HashSet <string> fieldResults;

            if (!methodResults.TryGetValue(f.FullName, out fieldResults))
            {
                methodResults[f.FullName] = fieldResults = new HashSet <string>();
            }

            var sb      = new SafeStringBuilder();
            var swriter = new TermSExpWriter(this.ExplorationServices.TermManager, new SafeStringWriter(sb), true, false);

            swriter.Write(fieldValue);
            sb.Append(arrayIndex);

            int value;

            if (termManager.TryGetI4Constant(fieldValue, out value))
            {
                sb.Append("  constant value: " + value);
            }
            else if (termManager.IsDefaultValue(fieldValue))
            {
                sb.Append("  null reference");
            }
            else
            {
                sb.Append("  not-null reference");
            }

            fieldResults.Add(sb.ToString());
        }
Пример #3
0
        /// <summary>
        /// Adds a monitored field to the database. Updates two kinds of hashmaps
        /// a. Field to Method mapper, which gives what the methods modifying a given field
        /// b. Method to Field mapper, which gives what fields are modified by each method (later used to identify a minimized set of methods)
        /// </summary>
        /// <param name="tm"></param>
        /// <param name="method"></param>
        /// <param name="f"></param>
        /// <param name="indices"></param>
        /// <param name="fieldValue"></param>
        public void AddMonitoredField(TermManager tm, Method method, Field f, Term[] indices, Term fieldValue, Term initialValue)
        {
            string arrayIndex = "";

            using (PexMeTermRewriter pexmeRewriter = new PexMeTermRewriter(tm))
            {
                fieldValue = pexmeRewriter.VisitTerm(default(TVoid), fieldValue); //update the field value to accomodate array-type field
                //if (indices.Length == 0) //not an array-type field
                if (indices.Length == 1)                                          //is an array-type field
                {
                    arrayIndex = " at index of " + indices[0].UniqueIndex.ToString();
                }

                if (initialValue != null)
                {
                    initialValue = pexmeRewriter.VisitTerm(default(TVoid), initialValue);
                }
            }

            //Updating the method store
            MethodStore ms;

            if (!methodDic.TryGetValue(method, out ms))
            {
                ms                = new MethodStore();
                ms.methodName     = method;
                methodDic[method] = ms;
            }

            ms.WriteFields.Add(f);
            //TODO: Gather information of read fields

            //Updating the field store
            FieldStore fs;

            if (!fieldDic.TryGetValue(f, out fs))
            {
                fs           = new FieldStore();
                fs.FieldName = f;
                fieldDic[f]  = fs;
            }

            TypeEx declaringType;

            if (!method.TryGetDeclaringType(out declaringType))
            {
                this.Log.LogError(WikiTopics.MissingWikiTopic, "monitorfield",
                                  "Failed to get the declaring type for the method " + method.FullName);
                return;
            }

            SafeSet <Method> writeMethods;

            if (!fs.WriteMethods.TryGetValue(declaringType, out writeMethods))
            {
                writeMethods = new SafeSet <Method>();
                fs.WriteMethods[declaringType] = writeMethods;
            }
            writeMethods.Add(method);

            var sb      = new SafeStringBuilder();
            var swriter = new TermSExpWriter(tm, new SafeStringWriter(sb), true, false);

            swriter.Write(fieldValue);
            sb.Append(arrayIndex);

            int value;

            if (tm.TryGetI4Constant(fieldValue, out value))
            {
                int initialval;
                if (initialValue != null)
                {
                    tm.TryGetI4Constant(initialValue, out initialval);
                }
                else
                {
                    initialval = 0;
                }

                sb.Append("  constant value: " + value);

                if (f.Type.ToString() != "System.Boolean")
                {
                    if (value < initialval)
                    {
                        fs.ModificationTypeDictionary[method] = FieldModificationType.DECREMENT;
                    }
                    else if (value > initialval)
                    {
                        fs.ModificationTypeDictionary[method] = FieldModificationType.INCREMENT;

                        if (value == initialval + 1)
                        {
                            fs.PreciseModificationTypeDictionary[method] = FieldModificationType.INCREMENT_ONE;
                        }
                    }
                    else
                    {
                        fs.ModificationTypeDictionary[method] = FieldModificationType.UNKNOWN;
                    }
                }
                else
                {
                    if (value == 0)
                    {
                        fs.ModificationTypeDictionary[method] = FieldModificationType.FALSE_SET;
                    }
                    else
                    {
                        fs.ModificationTypeDictionary[method] = FieldModificationType.TRUE_SET;
                    }
                }
            }
            else if (tm.IsDefaultValue(fieldValue))
            {
                if (initialValue != null && !tm.IsDefaultValue(initialValue))
                {
                    fs.ModificationTypeDictionary[method] = FieldModificationType.NULL_SET;
                }
                else
                {
                    fs.ModificationTypeDictionary[method] = FieldModificationType.UNKNOWN;
                }

                sb.Append("  null reference ");
            }
            else
            {
                if (initialValue == null)
                {
                    fs.ModificationTypeDictionary[method] = FieldModificationType.NON_NULL_SET;
                }
                else
                {
                    fs.ModificationTypeDictionary[method] = FieldModificationType.UNKNOWN;
                }

                sb.Append("  not-null reference ");
            }

            fs.FieldValues.Add(sb.ToString());
        }
        /// <summary>
        /// Adds a monitored field to the database. Updates two kinds of hashmaps
        /// a. Field to Method mapper, which gives what the methods modifying a given field
        /// b. Method to Field mapper, which gives what fields are modified by each method (later used to identify a minimized set of methods)
        /// </summary>
        /// <param name="tm"></param>
        /// <param name="method"></param>
        /// <param name="f"></param>
        /// <param name="indices"></param>
        /// <param name="fieldValue"></param>
        public void AddMonitoredField(TermManager tm, Method method, Field f, Term[] indices, Term fieldValue, Term initialValue)
        {
            string arrayIndex = "";
            using (PexMeTermRewriter pexmeRewriter = new PexMeTermRewriter(tm))
            {
                fieldValue = pexmeRewriter.VisitTerm(default(TVoid), fieldValue); //update the field value to accomodate array-type field
                //if (indices.Length == 0) //not an array-type field
                if (indices.Length == 1) //is an array-type field
                {
                    arrayIndex = " at index of " + indices[0].UniqueIndex.ToString();
                }

                if(initialValue != null)
                    initialValue = pexmeRewriter.VisitTerm(default(TVoid), initialValue);
            }

            //Updating the method store
            MethodStore ms;
            if(!methodDic.TryGetValue(method, out ms))
            {
                ms = new MethodStore();
                ms.methodName = method;
                methodDic[method] = ms;
            }

            ms.WriteFields.Add(f);
            //TODO: Gather information of read fields

            //Updating the field store
            FieldStore fs;
            if (!fieldDic.TryGetValue(f, out fs))
            {
                fs = new FieldStore();
                fs.FieldName = f;
                fieldDic[f] = fs;
            }

            TypeEx declaringType;
            if (!method.TryGetDeclaringType(out declaringType))
            {
                this.Log.LogError(WikiTopics.MissingWikiTopic, "monitorfield",
                    "Failed to get the declaring type for the method " + method.FullName);
                return;
            }

            SafeSet<Method> writeMethods;
            if (!fs.WriteMethods.TryGetValue(declaringType, out writeMethods))
            {
                writeMethods = new SafeSet<Method>();
                fs.WriteMethods[declaringType] = writeMethods;
            }
            writeMethods.Add(method);

            var sb = new SafeStringBuilder();
            var swriter = new TermSExpWriter(tm, new SafeStringWriter(sb), true, false);
            swriter.Write(fieldValue);
            sb.Append(arrayIndex);

            int value;
            if (tm.TryGetI4Constant(fieldValue, out value))
            {
                int initialval;
                if (initialValue != null)
                    tm.TryGetI4Constant(initialValue, out initialval);
                else
                    initialval = 0;

                sb.Append("  constant value: " + value);

                if (f.Type.ToString() != "System.Boolean")
                {
                    if (value < initialval)
                        fs.ModificationTypeDictionary[method] = FieldModificationType.DECREMENT;
                    else if (value > initialval)
                    {
                        fs.ModificationTypeDictionary[method] = FieldModificationType.INCREMENT;

                        if (value == initialval + 1)
                            fs.PreciseModificationTypeDictionary[method] = FieldModificationType.INCREMENT_ONE;
                    }
                    else
                        fs.ModificationTypeDictionary[method] = FieldModificationType.UNKNOWN;
                }
                else
                {
                    if (value == 0)
                        fs.ModificationTypeDictionary[method] = FieldModificationType.FALSE_SET;
                    else
                        fs.ModificationTypeDictionary[method] = FieldModificationType.TRUE_SET;
                }
            }
            else if (tm.IsDefaultValue(fieldValue))
            {
                if (initialValue != null && !tm.IsDefaultValue(initialValue))
                    fs.ModificationTypeDictionary[method] = FieldModificationType.NULL_SET;
                else
                    fs.ModificationTypeDictionary[method] = FieldModificationType.UNKNOWN;

                sb.Append("  null reference ");
            }
            else
            {
                if (initialValue == null)
                    fs.ModificationTypeDictionary[method] = FieldModificationType.NON_NULL_SET;
                else
                    fs.ModificationTypeDictionary[method] = FieldModificationType.UNKNOWN;

                sb.Append("  not-null reference ");
            }

            fs.FieldValues.Add(sb.ToString());
        }
Пример #5
0
        /// <summary>
        /// OBSOLETE: Handles a scenario where there is a term in the condition
        /// </summary>
        /// <param name="left"></param>
        /// <param name="right"></param>
        /// <param name="binOp"></param>
        /// <param name="fmt"></param>
        private void handleAConstantInTerm(TermManager termManager, Term left, Term right,
                                           BinaryOperator binOp, bool bNegated, SafeDictionary <Field, FieldValueHolder> fieldValues, Field culpritField,
                                           out FieldModificationType fmt, out int fitnessval)
        {
            fitnessval = Int32.MaxValue;
            fmt        = FieldModificationType.UNKNOWN;
            Term non_constant_term = null;
            Term constant_term     = null;

            bool bleftRightMaintainted = true;

            if (termManager.IsValue(left))
            {
                non_constant_term     = right;
                constant_term         = left;
                bleftRightMaintainted = true;
            }
            else if (termManager.IsValue(right))
            {
                non_constant_term     = left;
                constant_term         = right;
                bleftRightMaintainted = false;
            }

            int value;

            if (termManager.TryGetI4Constant(constant_term, out value))
            {
                fmt = FieldModificationType.INCREMENT;

                FieldValueHolder fvh;
                if (fieldValues.TryGetValue(culpritField, out fvh))
                {
                    int non_constant_field_value = fvh.intValue;    //TODO: Assuming that the fieldType is Int32
                    if (bleftRightMaintainted)
                    {
                        fitnessval = FitnessMeasure.ComputeFitnessValue(this.host, binOp, value, non_constant_field_value, bNegated);
                    }
                    else
                    {
                        fitnessval = FitnessMeasure.ComputeFitnessValue(this.host, binOp, non_constant_field_value, value, bNegated);
                    }
                }
                else
                {
                    this.host.Log.LogWarning(WikiTopics.MissingWikiTopic, "fitness measure",
                                             "Failed to retrieve value for field " + culpritField.ToString());
                }
            }
            else if (termManager.IsDefaultValue(constant_term))
            {
                if (binOp == BinaryOperator.Ceq)
                {
                    if (culpritField.Type.ToString() == "System.Boolean")
                    {
                        fmt = bNegated ? FieldModificationType.TRUE_SET : FieldModificationType.FALSE_SET;
                    }
                    else
                    {
                        fmt = bNegated ? FieldModificationType.NON_NULL_SET : FieldModificationType.NULL_SET;
                    }
                }
            }

            Term           objectValue;
            ObjectProperty objectProperty;

            if (termManager.TryGetObjectProperty(constant_term, out objectValue, out objectProperty))
            {
                //TODO??? How to handle this scenario?
            }
        }
        private void GatherDebuggingInfoFromInsufficiency(IExecutionNode executionNode, TermManager termManager, 
            Term condition, TypeEx explorableType)
        {
            var sb = new SafeStringBuilder();
            sb.AppendLine("condition:");
            sb.AppendLine();
            this.tba.ConvertTermToText(new SafeStringWriter(sb), condition, this.ExplorationServices.TermManager);
            sb.AppendLine();
            var swriter = new TermSExpWriter(termManager, new SafeStringWriter(sb), true, false);
            swriter.Write(condition);
            sb.AppendLine();
            sb.AppendLine("location:");
            sb.AppendLine();
            sb.AppendLine(executionNode.CodeLocation.ToString());
            sb.AppendLine();
            sb.AppendLine("properties:");

            Term unnegatedCondition;
            if (termManager.TryGetInnerLogicallyNegatedValue(condition, out unnegatedCondition))
                sb.AppendLine("negated");
            else
                unnegatedCondition = condition;

            var targetFieldValues = new SafeDictionary<Field, object>();
            Term left, right;
            BinaryOperator binOp;
            if (termManager.TryGetBinary(unnegatedCondition, out binOp, out left, out right))
            {
                sb.AppendFormat("binary relation: {0}", binOp);
                sb.AppendLine();

                if (!termManager.IsValue(left) && !termManager.IsValue(right))
                {
                    sb.AppendLine("No constant on either left side or right side.");
                    return;
                }

                Term non_constant_term = null;
                Term constant_term = null;
                if (termManager.IsValue(left))
                {
                    non_constant_term = right;
                    constant_term = left;
                }
                else if (termManager.IsValue(right))
                {
                    non_constant_term = left;
                    constant_term = right;
                }

                sb.AppendLine("against constant");
                if (constant_term == null || termManager.IsDefaultValue(constant_term))
                {
                    sb.AppendLine("against default value ('null' for references)");
                }

                int value;
                if (constant_term != null && termManager.TryGetI4Constant(constant_term, out value))
                {
                    sb.AppendLine("against integer: " + value);
                }

                Term objectValue;
                ObjectProperty objectProperty;
                if (constant_term != null && termManager.TryGetObjectProperty(constant_term, out objectValue, out objectProperty))
                {
                    sb.AppendLine("against object property: object=" + objectValue + ", property=" + objectProperty);
                }

                sb.AppendLine(" involving fields: ");
                SafeDictionary<Field, FieldValueHolder> innerFieldValues;
                SafeList<TypeEx> innerFieldTypes;
                SafeList<Field> fs = TargetBranchAnalyzer.GetInvolvedFields(this, termManager,
                    non_constant_term, out innerFieldValues, out innerFieldTypes);
                foreach (var f in fs)
                {
                    sb.AppendLine(f.FullName);
                }
            }

            sb.AppendLine("Executed method call sequence");
            if(this.pmd.LastExecutedFactoryMethodCallSequence != null)
                foreach (var m in this.pmd.LastExecutedFactoryMethodCallSequence)
                {
                    sb.AppendLine("\t" + m);
                }

            this.Log.Dump("foo", "insufficiency for " + (explorableType != null ? explorableType.FullName : "?"), sb.ToString());
            return;
        }
        /// <summary>
        /// OBSOLETE: Handles a scenario where there is a term in the condition
        /// </summary>
        /// <param name="left"></param>
        /// <param name="right"></param>
        /// <param name="binOp"></param>
        /// <param name="fmt"></param>
        private void handleAConstantInTerm(TermManager termManager, Term left, Term right,
            BinaryOperator binOp, bool bNegated, SafeDictionary<Field, FieldValueHolder> fieldValues, Field culpritField,
            out FieldModificationType fmt, out int fitnessval)
        {
            fitnessval = Int32.MaxValue;
            fmt = FieldModificationType.UNKNOWN;
            Term non_constant_term = null;
            Term constant_term = null;

            bool bleftRightMaintainted = true;
            if (termManager.IsValue(left))
            {
                non_constant_term = right;
                constant_term = left;
                bleftRightMaintainted = true;
            }
            else if (termManager.IsValue(right))
            {
                non_constant_term = left;
                constant_term = right;
                bleftRightMaintainted = false;
            }

            int value;
            if (termManager.TryGetI4Constant(constant_term, out value))
            {
                fmt = FieldModificationType.INCREMENT;

                FieldValueHolder fvh;
                if (fieldValues.TryGetValue(culpritField, out fvh))
                {
                    int non_constant_field_value = fvh.intValue;    //TODO: Assuming that the fieldType is Int32
                    if (bleftRightMaintainted)
                        fitnessval = FitnessMeasure.ComputeFitnessValue(this.host, binOp, value, non_constant_field_value, bNegated);
                    else
                        fitnessval = FitnessMeasure.ComputeFitnessValue(this.host, binOp, non_constant_field_value, value, bNegated);
                }
                else
                    this.host.Log.LogWarning(WikiTopics.MissingWikiTopic, "fitness measure",
                        "Failed to retrieve value for field " + culpritField.ToString());
            }
            else if (termManager.IsDefaultValue(constant_term))
            {
                if (binOp == BinaryOperator.Ceq)
                {
                    if (culpritField.Type.ToString() == "System.Boolean")
                    {
                        fmt = bNegated ? FieldModificationType.TRUE_SET : FieldModificationType.FALSE_SET;
                    }
                    else
                        fmt = bNegated ? FieldModificationType.NON_NULL_SET : FieldModificationType.NULL_SET;
                }
            }

            Term objectValue;
            ObjectProperty objectProperty;
            if (termManager.TryGetObjectProperty(constant_term, out objectValue, out objectProperty))
            {
                //TODO??? How to handle this scenario?
            }
        }