public EVException([CallerFilePath] string callerFilePath = "",
                    [CallerLineNumber] int callerLineNumber = 0, 
                    [CallerMemberName] string callerMemberName = "")
 {
     CodeLocation codeLocation = new CodeLocation(callerFilePath, callerLineNumber, callerMemberName);
     this.AddNamedProperty("EVExceptionConstructionLocation", codeLocation.ToString());
 }
        public EVException([CallerFilePath] string callerFilePath     = "",
                           [CallerLineNumber] int callerLineNumber    = 0,
                           [CallerMemberName] string callerMemberName = "")
        {
            CodeLocation codeLocation = new CodeLocation(callerFilePath, callerLineNumber, callerMemberName);

            this.AddNamedProperty("EVExceptionConstructionLocation", codeLocation.ToString());
        }
Exemple #3
0
        public override string ToString()
        {
            var s = new StringBuilder();

            s.Append("At: ");
            s.AppendLine(CodeLocation.ToString());

            s.AppendLine("Code: ");
            s.AppendLine(CodeText);

            return(s.ToString());
        }
        public static TException Trace <TException>(this TException ex, [CallerFilePath] string callerFilePath = "",
                                                    [CallerLineNumber] int callerLineNumber = 0, [CallerMemberName] string callerMemberName = "") where TException : Exception
        {
            ExceptionPayload payload = ex.GetOrCreatePayload();

            CodeLocation codeLocation = new CodeLocation(callerFilePath, callerLineNumber, callerMemberName);

            ex.AddNamedProperty("TraceLocation", codeLocation.ToString());

            if (!payload.Dirty)
            {
                Tracer.Debug("Detected potential bug: logging exception which has already been logged. Duplicate exception log follows.");
            }

            Tracer.LogException(ex);
            payload.Dirty = false;
            return(ex);
        }
        public PersistentUncoveredLocationStore(CodeLocation cl,
            TypeEx explorableType, int termIndex, int fitnessvalue, FactorySuggestionStore fss)
        {
            this.CodeLocation = cl.ToString();
            this.MethodSignature = MethodOrFieldAnalyzer.GetMethodSignature(cl.Method);
            TypeDefinition declaringType;
            if (!cl.Method.TryGetDeclaringType(out declaringType))
            {
                //TODO:Error
            }

            this.ExplorableType = explorableType.ToString();
            this.Offset = cl.Offset;
            this.AssemblyShortName = declaringType.Module.Assembly.Location;
            this.declaringTypeStr = declaringType.ToString();
            this.Fitnessvalue = fitnessvalue;
            this.TermIndex = termIndex;
            this.parentfss = fss;
        }
Exemple #6
0
        public PersistentUncoveredLocationStore(CodeLocation cl,
                                                TypeEx explorableType, int termIndex, int fitnessvalue, FactorySuggestionStore fss)
        {
            this.CodeLocation    = cl.ToString();
            this.MethodSignature = MethodOrFieldAnalyzer.GetMethodSignature(cl.Method);
            TypeDefinition declaringType;

            if (!cl.Method.TryGetDeclaringType(out declaringType))
            {
                //TODO:Error
            }

            this.ExplorableType    = explorableType.ToString();
            this.Offset            = cl.Offset;
            this.AssemblyShortName = declaringType.Module.Assembly.Location;
            this.declaringTypeStr  = declaringType.ToString();
            this.Fitnessvalue      = fitnessvalue;
            this.TermIndex         = termIndex;
            this.parentfss         = fss;
        }
Exemple #7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="e"></param>
        void Log_ProblemHandler(Microsoft.ExtendedReflection.Logging.ProblemEventArgs e)
        {
            //TODO: Xusheng's code for additional OCI issues
            if (e.Result == TryGetModelResult.Success)
            {
                return;
            }

            CodeLocation location = e.FlippedLocation;
            var          term     = e.Suffix;

            SafeDictionary <Field, FieldValueHolder> fieldValues;
            SafeList <TypeEx> allFieldTypes;
            SafeList <Field>  fields = TargetBranchAnalyzer.GetInvolvedFields(this.host, e.TermManager, term, out fieldValues, out allFieldTypes);

            //Not an object creation issue
            if (fields == null || fields.Count == 0)
            {
                return;
            }
            this.host.Log.LogMessage("ProblemHandler", "Recorded an issue at code location " + location.ToString());
            if (!PexMeConstants.USE_TERM_SOLVER)
            {
                //A heuristic to choose the explorable type
                this.tba.HandleTargetBranch(location, term, e.TermManager, allFieldTypes[0]);
            }
        }
Exemple #8
0
        private static IEnumerable <KeyValuePair <string, string> > GetMetadata(CodeLocation dataLocation, string[] record, Dictionary <string, int> metadataColumns)
        {
            if (dataLocation != CodeLocation.Unknown)
            {
                yield return(new KeyValuePair <string, string>(MetadataKeys.DataLocation, dataLocation.ToString()));
            }

            if (metadataColumns != null)
            {
                foreach (KeyValuePair <string, int> metadataColumn in metadataColumns)
                {
                    // Ignore metadata columns that are absent.
                    int columnIndex = metadataColumn.Value;
                    if (columnIndex < record.Length)
                    {
                        yield return(new KeyValuePair <string, string>(metadataColumn.Key, record[columnIndex]));
                    }
                }
            }
        }
Exemple #9
0
 public override string ToString()
 {
     return(location.ToString() + ": " + message);
 }
Exemple #10
0
 public InterpreterException(CodeLocation location, string message)
     : base(location.ToString() + ": " + message)
 {
     this.location = location;
     this.message  = message;
 }
Exemple #11
0
        /// <summary>
        /// Adds a field to an unsuccessful code location.
        /// </summary>
        /// <param name="location"></param>
        /// <param name="fields"></param>
        public void AddFieldsOfUncoveredCodeLocations(CodeLocation location, SafeList <Field> fields, FieldModificationType fmt,
                                                      Term condition, string terms, int fitnessval, TypeEx explorableType, SafeList <TypeEx> allFieldTypes)
        {
            //No need to process this location.
            if (fields.Count == 0)
            {
                return;
            }

            Field  targetField;
            TypeEx declaringType;   //This declaring type is considered as explorable type in the rest of the analysis

            if (!PexMeFactoryGuesser.GetTargetExplorableField(this, fields, out targetField, out declaringType))
            {
                this.Log.LogError(WikiTopics.MissingWikiTopic, "factoryguesser",
                                  "Failed to retrieve the target field for uncovered location " + location.ToString());
                return;
            }

            //Compare the declaring type and actual explorable type.
            //If there is a inheritance relation, use the actual one
            if (explorableType.IsAssignableTo(declaringType))
            {
                declaringType = explorableType;
            }

            var uclskey = UncoveredCodeLocationStore.GetKey(location.ToString(), declaringType.ToString(), condition.UniqueIndex);
            UncoveredCodeLocationStoreList uclslist;

            if (!this.unCoveredLocationDic.TryGetValue(uclskey, out uclslist))
            {
                uclslist                           = new UncoveredCodeLocationStoreList();
                uclslist.Location                  = location;
                uclslist.ExplorableType            = declaringType.ToString();
                uclslist.TermIndex                 = condition.UniqueIndex;
                this.unCoveredLocationDic[uclskey] = uclslist;
            }

            var ucls = new UncoveredCodeLocationStore();

            ucls.Location       = location;
            ucls.ExplorableType = declaringType;
            ucls.TargetField    = targetField;
            ucls.AllFields.AddRange(fields);
            ucls.AllFieldTypes.AddRange(allFieldTypes);
            ucls.TermIndex = condition.UniqueIndex;
            //add the sequence of method calls
            ucls.MethodCallSequence = new MethodSignatureSequence();
            foreach (var m in this.LastExecutedFactoryMethodCallSequence)
            {
                ucls.MethodCallSequence.Sequence.Add(MethodOrFieldAnalyzer.GetMethodSignature(m));
            }
            ucls.IsADefectDetectingSequence = this.DefectDetectingSequence;
            ucls.CUTMethodCallSequence      = this.LastExecutedCUTMethodCallSequence;

            if (!uclslist.StoreList.Contains(ucls))
            {
                ucls.TextualTerms.Add(terms);
                ucls.DesiredFieldModificationType = fmt;
                ucls.Fitnessvalue = fitnessval;
                uclslist.StoreList.Add(ucls);
            }
        }
        /// <summary>
        /// Adds a field to an unsuccessful code location.         
        /// </summary>
        /// <param name="location"></param>
        /// <param name="fields"></param>
        public void AddFieldsOfUncoveredCodeLocations(CodeLocation location, SafeList<Field> fields, FieldModificationType fmt, 
            Term condition, string terms, int fitnessval, TypeEx explorableType, SafeList<TypeEx> allFieldTypes)
        {
            //No need to process this location.
            if (fields.Count == 0)
            {
                return;
            }

            Field targetField;
            TypeEx declaringType;   //This declaring type is considered as explorable type in the rest of the analysis
            if (!PexMeFactoryGuesser.GetTargetExplorableField(this, fields, out targetField, out declaringType))
            {
                this.Log.LogError(WikiTopics.MissingWikiTopic, "factoryguesser",
                   "Failed to retrieve the target field for uncovered location " + location.ToString());
                return;
            }

            //Compare the declaring type and actual explorable type.
            //If there is a inheritance relation, use the actual one
            if (explorableType.IsAssignableTo(declaringType))
            {
                declaringType = explorableType;
            }

            var uclskey = UncoveredCodeLocationStore.GetKey(location.ToString(), declaringType.ToString(), condition.UniqueIndex);
            UncoveredCodeLocationStoreList uclslist;
            if (!this.unCoveredLocationDic.TryGetValue(uclskey, out uclslist))
            {
                uclslist = new UncoveredCodeLocationStoreList();
                uclslist.Location = location;
                uclslist.ExplorableType = declaringType.ToString();
                uclslist.TermIndex = condition.UniqueIndex;
                this.unCoveredLocationDic[uclskey] = uclslist;
            }

            var ucls = new UncoveredCodeLocationStore();
            ucls.Location = location;
            ucls.ExplorableType = declaringType;
            ucls.TargetField = targetField;
            ucls.AllFields.AddRange(fields);
            ucls.AllFieldTypes.AddRange(allFieldTypes);
            ucls.TermIndex = condition.UniqueIndex;
            //add the sequence of method calls
            ucls.MethodCallSequence = new MethodSignatureSequence();
            foreach (var m in this.LastExecutedFactoryMethodCallSequence)
            {
                ucls.MethodCallSequence.Sequence.Add(MethodOrFieldAnalyzer.GetMethodSignature(m));
            }
            ucls.IsADefectDetectingSequence = this.DefectDetectingSequence;
            ucls.CUTMethodCallSequence = this.LastExecutedCUTMethodCallSequence;

            if (!uclslist.StoreList.Contains(ucls))
            {
                ucls.TextualTerms.Add(terms);
                ucls.DesiredFieldModificationType = fmt;
                ucls.Fitnessvalue = fitnessval;
                uclslist.StoreList.Add(ucls);
            }
        }
Exemple #13
0
        /// <summary>
        /// Gets called when an un-explored branch is encountered during program execution
        /// </summary>
        /// <param name="executionNode"></param>
        /// <param name="explorableType"></param>
        public void HandleTargetBranch(CodeLocation location, Term condition, TermManager termManager, TypeEx explorableType)
        {
            var accessedFields = new SafeList <Field>();

            if (PexMeConstants.IGNORE_UNCOV_BRANCH_IN_SYSTEM_LIB)
            {
                if (IsUncoveredLocationInSystemLib(location))
                {
                    this.host.Log.LogWarning(WikiTopics.MissingWikiTopic, "uncoveredlocation",
                                             "Ignoring the uncovered location " + location.ToString() + ", since it is in system library");
                    return;
                }
            }

            Term unnegatedCondition;
            bool bNegated = false;

            if (termManager.TryGetInnerLogicallyNegatedValue(condition, out unnegatedCondition))
            {
                bNegated = true;
            }
            else
            {
                unnegatedCondition = condition;
            }

            var            culpritFields = new SafeList <Field>();
            Term           left, right;
            BinaryOperator binOp;

            SafeStringBuilder sbTerm = new SafeStringBuilder();

            this.ConvertTermToText(new SafeStringWriter(sbTerm), condition, termManager);

            //Handling only binary conditions. TODO: Needs to check what are the other conditions
            //The related code is in TermSolver function
            if (!termManager.TryGetBinary(unnegatedCondition, out binOp, out left, out right))
            {
                this.host.Log.LogWarning(WikiTopics.MissingWikiTopic, PexMeLogCategories.Term,
                                         "Handling only binary operations in terms");
                return;
            }

            if (PexMeConstants.USE_TERM_SOLVER)
            {
                //TODO: Temporarily ignoring the scenario where both the sides are symbolic values
                if (!termManager.IsValue(left) && !termManager.IsValue(right))
                {
                    this.host.Log.LogWarning(WikiTopics.MissingWikiTopic, PexMeLogCategories.Term,
                                             "Handling only binary operations where atleast one side of the condition is concrete. Current expression has both sides symbolic");
                    return;
                }

                SafeDictionary <Field, FieldValueHolder> expectedFieldValues;
                SafeDictionary <Field, FieldValueHolder> actualFieldValues;
                SafeList <Field>  allFieldsInCondition;
                SafeList <TypeEx> allFieldTypes;
                TermSolver.SolveTerm(this.explorationComponent, condition, binOp,
                                     out actualFieldValues, out expectedFieldValues, out allFieldsInCondition, out allFieldTypes);

                //Compute an intersection to identify culprit fields
                List <Field> actualKeys   = actualFieldValues.Keys.ToList();
                List <Field> expectedKeys = expectedFieldValues.Keys.ToList();

                AddToCulpritField(allFieldsInCondition, culpritFields);
                if (culpritFields.Count == 0)
                {
                    this.host.Log.LogWarning(WikiTopics.MissingWikiTopic, PexMeLogCategories.Term,
                                             "Failed to retrieve culprit fields from the uncovered branch");
                }
                else
                {
                    foreach (Field field in culpritFields)
                    {
                        FieldModificationType fieldfmt;
                        int fitnessval;
                        FitnessMeasure.ComputeFitnessValue(field, actualFieldValues[field], expectedFieldValues[field], this.host, out fieldfmt, out fitnessval);

                        if (fieldfmt == FieldModificationType.UNKNOWN)
                        {
                            continue;
                        }
                        this.pmd.AddFieldsOfUncoveredCodeLocations(location, allFieldsInCondition, fieldfmt,
                                                                   condition, sbTerm.ToString(), fitnessval, explorableType, allFieldTypes);
                    }
                }
            }
            else
            {
                FieldModificationType fmt;
                if (!termManager.IsValue(left) && !termManager.IsValue(right))
                {
                    SafeDictionary <Field, FieldValueHolder> leftFieldValues;
                    SafeList <TypeEx> leftFieldTypes;
                    var leftAccessedFields = GetInvolvedFields(this.host, termManager, left, out leftFieldValues, out leftFieldTypes);
                    if (leftAccessedFields.Count > 0)
                    {
                        AddToCulpritField(leftAccessedFields, culpritFields);
                    }

                    SafeDictionary <Field, FieldValueHolder> rightFieldValues;
                    SafeList <TypeEx> rightFieldTypes;
                    var rightAccessedFields = GetInvolvedFields(this.host, termManager, right, out rightFieldValues, out rightFieldTypes);
                    if (rightAccessedFields.Count > 0)
                    {
                        AddToCulpritField(rightAccessedFields, culpritFields);
                    }

                    int fitnessval;
                    this.handleNoConstantsInTerm(termManager, left, right, binOp, bNegated,
                                                 culpritFields, unnegatedCondition, out fmt, out fitnessval);

                    //TODO: fitnessval can be different from left and right handside terms. Needs to deal with this later
                    this.pmd.AddFieldsOfUncoveredCodeLocations(location, leftAccessedFields,
                                                               fmt, condition, sbTerm.ToString(), fitnessval, explorableType, leftFieldTypes);
                    this.pmd.AddFieldsOfUncoveredCodeLocations(location, rightAccessedFields,
                                                               fmt, condition, sbTerm.ToString(), fitnessval, explorableType, rightFieldTypes);
                }
                else
                {
                    Term non_constant_term = null;
                    if (termManager.IsValue(left))
                    {
                        non_constant_term = right;
                    }
                    else if (termManager.IsValue(right))
                    {
                        non_constant_term = left;
                    }
                    else
                    {
                        SafeDebug.AssumeNotNull(null, "Control should not come here!!!");
                    }


                    //Get accessed fields in the uncovered branching condition
                    SafeDictionary <Field, FieldValueHolder> fieldValues;
                    SafeList <TypeEx> fieldTypes;
                    accessedFields = GetInvolvedFields(this.host, termManager, non_constant_term, out fieldValues, out fieldTypes);
                    if (accessedFields.Count != 0)
                    {
                        AddToCulpritField(accessedFields, culpritFields);
                    }

                    if (culpritFields.Count == 0)
                    {
                        this.host.Log.LogWarning(WikiTopics.MissingWikiTopic, PexMeLogCategories.Term,
                                                 "Failed to retrieve culprit fields from the uncovered branch");
                    }
                    else
                    {
                        int fitnessval;
                        this.handleAConstantInTerm(termManager, left, right, binOp, bNegated, fieldValues, culpritFields[0], out fmt, out fitnessval);
                        this.pmd.AddFieldsOfUncoveredCodeLocations(location, accessedFields,
                                                                   fmt, condition, sbTerm.ToString(), fitnessval, explorableType, fieldTypes);
                    }
                }
            }
        }
Exemple #14
0
        private static IEnumerable<KeyValuePair<string, string>> GetMetadata(CodeLocation dataLocation, string[] record, Dictionary<string, int> metadataColumns)
        {
            if (dataLocation != CodeLocation.Unknown)
                yield return new KeyValuePair<string, string>(MetadataKeys.DataLocation, dataLocation.ToString());

            if (metadataColumns != null)
            {
                foreach (KeyValuePair<string, int> metadataColumn in metadataColumns)
                {
                    // Ignore metadata columns that are absent.
                    int columnIndex = metadataColumn.Value;
                    if (columnIndex < record.Length)
                        yield return new KeyValuePair<string, string>(metadataColumn.Key, record[columnIndex]);
                }
            }
        }
        /// <summary>
        /// Gets called when an un-explored branch is encountered during program execution
        /// </summary>
        /// <param name="executionNode"></param>
        /// <param name="explorableType"></param>
        public void HandleTargetBranch(CodeLocation location, Term condition, TermManager termManager, TypeEx explorableType)
        {
            var accessedFields = new SafeList<Field>();

            if (PexMeConstants.IGNORE_UNCOV_BRANCH_IN_SYSTEM_LIB)
            {
                if(IsUncoveredLocationInSystemLib(location))
                {
                    this.host.Log.LogWarning(WikiTopics.MissingWikiTopic, "uncoveredlocation",
                        "Ignoring the uncovered location " + location.ToString() + ", since it is in system library");
                    return;
                }
            }

            Term unnegatedCondition;
            bool bNegated = false;
            if (termManager.TryGetInnerLogicallyNegatedValue(condition, out unnegatedCondition))
                bNegated = true;
            else
                unnegatedCondition = condition;

            var culpritFields = new SafeList<Field>();
            Term left, right;
            BinaryOperator binOp;

            SafeStringBuilder sbTerm = new SafeStringBuilder();
            this.ConvertTermToText(new SafeStringWriter(sbTerm), condition, termManager);

            //Handling only binary conditions. TODO: Needs to check what are the other conditions
            //The related code is in TermSolver function
            if (!termManager.TryGetBinary(unnegatedCondition, out binOp, out left, out right))
            {
                this.host.Log.LogWarning(WikiTopics.MissingWikiTopic, PexMeLogCategories.Term,
                    "Handling only binary operations in terms");
                return;
            }

            if (PexMeConstants.USE_TERM_SOLVER)
            {
                //TODO: Temporarily ignoring the scenario where both the sides are symbolic values
                if (!termManager.IsValue(left) && !termManager.IsValue(right))
                {
                    this.host.Log.LogWarning(WikiTopics.MissingWikiTopic, PexMeLogCategories.Term,
                       "Handling only binary operations where atleast one side of the condition is concrete. Current expression has both sides symbolic");
                    return;
                }

                SafeDictionary<Field, FieldValueHolder> expectedFieldValues;
                SafeDictionary<Field, FieldValueHolder> actualFieldValues;
                SafeList<Field> allFieldsInCondition;
                SafeList<TypeEx> allFieldTypes;
                TermSolver.SolveTerm(this.explorationComponent, condition, binOp,
                    out actualFieldValues, out expectedFieldValues, out allFieldsInCondition, out allFieldTypes);

                //Compute an intersection to identify culprit fields
                List<Field> actualKeys = actualFieldValues.Keys.ToList();
                List<Field> expectedKeys = expectedFieldValues.Keys.ToList();

                AddToCulpritField(allFieldsInCondition, culpritFields);
                if (culpritFields.Count == 0)
                {
                    this.host.Log.LogWarning(WikiTopics.MissingWikiTopic, PexMeLogCategories.Term,
                        "Failed to retrieve culprit fields from the uncovered branch");
                }
                else
                {
                    foreach (Field field in culpritFields)
                    {
                        FieldModificationType fieldfmt;
                        int fitnessval;
                        FitnessMeasure.ComputeFitnessValue(field, actualFieldValues[field], expectedFieldValues[field], this.host, out fieldfmt, out fitnessval);

                        if (fieldfmt == FieldModificationType.UNKNOWN)
                            continue;
                        this.pmd.AddFieldsOfUncoveredCodeLocations(location, allFieldsInCondition, fieldfmt,
                            condition, sbTerm.ToString(), fitnessval, explorableType, allFieldTypes);
                    }
                }
            }
            else
            {
                FieldModificationType fmt;
                if (!termManager.IsValue(left) && !termManager.IsValue(right))
                {
                    SafeDictionary<Field, FieldValueHolder> leftFieldValues;
                    SafeList<TypeEx> leftFieldTypes;
                    var leftAccessedFields = GetInvolvedFields(this.host, termManager, left, out leftFieldValues, out leftFieldTypes);
                    if (leftAccessedFields.Count > 0)
                        AddToCulpritField(leftAccessedFields, culpritFields);

                    SafeDictionary<Field, FieldValueHolder> rightFieldValues;
                    SafeList<TypeEx> rightFieldTypes;
                    var rightAccessedFields = GetInvolvedFields(this.host, termManager, right, out rightFieldValues, out rightFieldTypes);
                    if (rightAccessedFields.Count > 0)
                        AddToCulpritField(rightAccessedFields, culpritFields);

                    int fitnessval;
                    this.handleNoConstantsInTerm(termManager, left, right, binOp, bNegated,
                        culpritFields, unnegatedCondition, out fmt, out fitnessval);

                    //TODO: fitnessval can be different from left and right handside terms. Needs to deal with this later
                    this.pmd.AddFieldsOfUncoveredCodeLocations(location, leftAccessedFields,
                        fmt, condition, sbTerm.ToString(), fitnessval, explorableType, leftFieldTypes);
                    this.pmd.AddFieldsOfUncoveredCodeLocations(location, rightAccessedFields,
                        fmt, condition, sbTerm.ToString(), fitnessval, explorableType, rightFieldTypes);
                }
                else
                {
                    Term non_constant_term = null;
                    if (termManager.IsValue(left))
                        non_constant_term = right;
                    else if (termManager.IsValue(right))
                        non_constant_term = left;
                    else
                        SafeDebug.AssumeNotNull(null, "Control should not come here!!!");

                    //Get accessed fields in the uncovered branching condition
                    SafeDictionary<Field, FieldValueHolder> fieldValues;
                    SafeList<TypeEx> fieldTypes;
                    accessedFields = GetInvolvedFields(this.host, termManager, non_constant_term, out fieldValues, out fieldTypes);
                    if (accessedFields.Count != 0)
                        AddToCulpritField(accessedFields, culpritFields);

                    if (culpritFields.Count == 0)
                    {
                        this.host.Log.LogWarning(WikiTopics.MissingWikiTopic, PexMeLogCategories.Term,
                            "Failed to retrieve culprit fields from the uncovered branch");
                    }
                    else
                    {
                        int fitnessval;
                        this.handleAConstantInTerm(termManager, left, right, binOp, bNegated, fieldValues, culpritFields[0], out fmt, out fitnessval);
                        this.pmd.AddFieldsOfUncoveredCodeLocations(location, accessedFields,
                            fmt, condition, sbTerm.ToString(), fitnessval, explorableType, fieldTypes);
                    }
                }
            }
        }