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> /// 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); } } } }
/// <summary> /// Stores the expected field value. Currently implemented for integers for time being /// </summary> /// <param name="termManager"></param> /// <param name="field"></param> /// <param name="fieldValue"></param> /// <param name="expectedFieldValues"></param> /// <param name="host"></param> /// <param name="condition"></param> /// <param name="binOp"></param> private static void StoreFieldValue(TermManager termManager, Field field, Term fieldValue, SafeDictionary <Field, FieldValueHolder> expectedFieldValues, SafeDictionary <Field, FieldValueHolder> actualFieldValues, SafeList <Field> allFieldsInCondition, IPexComponent host, Term condition, BinaryOperator binOp) { TypeEx fieldType = field.Type; if (fieldType == SystemTypes.Int32) { int value; if (termManager.TryGetI4Constant(fieldValue, out value)) { FieldValueHolder fvh = new FieldValueHolder(FieldValueType.INTEGER); fvh.intValue = value; expectedFieldValues.Add(field, fvh); } return; } //Gathering the expected value for boolean field Term unnegatedCondition; bool bNegated = false; if (termManager.TryGetInnerLogicallyNegatedValue(condition, out unnegatedCondition)) { bNegated = true; } else { unnegatedCondition = condition; } if (fieldType == SystemTypes.Bool) { if (binOp == BinaryOperator.Ceq) { FieldValueHolder fvh = new FieldValueHolder(FieldValueType.BOOLEAN); fvh.boolValue = bNegated; expectedFieldValues.Add(field, fvh); //For boolean, actual value does not matter. However, without //proper entry in the acutal field, it would not considered for further processing if (allFieldsInCondition.Contains(field) && !actualFieldValues.Keys.Contains(field)) { FieldValueHolder fvhtemp = new FieldValueHolder(FieldValueType.BOOLEAN); fvhtemp.boolValue = false; actualFieldValues.Add(field, fvhtemp); } } return; } if (fieldType == SystemTypes.Int16) { short value; if (termManager.TryGetI2Constant(fieldValue, out value)) { FieldValueHolder fvh = new FieldValueHolder(FieldValueType.SHORT); fvh.shortValue = value; expectedFieldValues.Add(field, fvh); } return; } if (fieldType == SystemTypes.Int64) { long value; if (termManager.TryGetI8Constant(fieldValue, out value)) { FieldValueHolder fvh = new FieldValueHolder(FieldValueType.LONG); fvh.longValue = value; expectedFieldValues.Add(field, fvh); } return; } if (field.Type.IsReferenceType) { Object obj = null; termManager.TryGetObject(fieldValue, out obj); FieldValueHolder fvh = new FieldValueHolder(FieldValueType.OBJECT); fvh.objValue = obj; expectedFieldValues.Add(field, fvh); //For reference value the actual value does not matter. However, without //proper entry in the acutal field, it would not considered for further processing if (allFieldsInCondition.Contains(field) && !actualFieldValues.Keys.Contains(field)) { FieldValueHolder fvhtemp = new FieldValueHolder(FieldValueType.OBJECT); fvhtemp.objValue = null; actualFieldValues.Add(field, fvhtemp); } return; } host.Log.LogWarning(WikiTopics.MissingWikiTopic, "TermSolver", "Expected values are computed only for integers, boolean and objects. Requires manual analysis of this sceanario"); }
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> /// 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); } } } }