/// <summary> /// simplifies the term based on the contents of the term. all /// </summary> /// <param name="host"></param> /// <param name="termManager"></param> /// <param name="condition"></param> /// <param name="binOp"></param> private static Term SimplifyTerm(IPexExplorationComponent host, TermManager termManager, Term condition) { Term left, right; BinaryOperator binOp; if (!termManager.TryGetBinary(condition, out binOp, out left, out right)) { return(condition); } if (!IsInteger(termManager, left) || !IsInteger(termManager, right)) { return(condition); } //Check whether the term is of the form x > 20, where one side is a constant. then no simplification needs to be done if (termManager.IsValue(left)) { //one side is constant. so just return over here return(condition); } if (termManager.IsValue(right)) { //one side is constant. so just return over here return(condition); } //none of the sides are concrete. both sides are symbolic. //find out which side can be more controlled based on the variables //contained on that side SafeList <Field> allFieldsInLeftCondition = new SafeList <Field>(); SafeList <TypeEx> allFieldTypes = new SafeList <TypeEx>(); SafeDictionary <Field, FieldValueHolder> leftfields = new SafeDictionary <Field, FieldValueHolder>(); VariablesCollector.Collect(termManager, left, leftfields, allFieldsInLeftCondition, allFieldTypes); //TODO: How to get the concrete value of the other side, could be either left //or right. and make a term out of the concrete value int lvalue; if (termManager.TryGetI4Constant(left, out lvalue)) { } int rvalue; if (termManager.TryGetI4Constant(right, out rvalue)) { } return(condition); }
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> /// 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? } }
/// <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); } } } }