public void Dump() { var termManager = this.ExplorationServices.TermManager; SafeStringBuilder sb = new SafeStringBuilder(); foreach (var kvp in this.FieldAccessInfoObj.Results) { var method = kvp.Key; var methodResults = kvp.Value; sb.AppendLine(); sb.AppendLine("method:" + method /*.FullName*/); foreach (var kvp2 in methodResults) { var field = kvp2.Key; var fieldResults = kvp2.Value; sb.AppendLine(" modifies field:" + field /*.FullName*/); sb.AppendLine(" with new values:"); foreach (var newFieldValueTerm in fieldResults) { var swriter = new TermSExpWriter(termManager, new SafeStringWriter(sb), true, false); swriter.Write(newFieldValueTerm); sb.AppendLine(); } } } this.Log.Dump("fields", "field changes", sb.ToString()); //database.DumpFieldAccessInfoIntoToFile(); }
private SafeStringBuilder DumpFieldAccessInfoToDebugFile() { SafeStringBuilder sb = new SafeStringBuilder(); foreach (var meth in FieldAccessInfoObj.Results.Keys) { sb.AppendLine("Method: " + meth); System.Collections.Generic.Dictionary <string, HashSet <string> > mapInfo; if (FieldAccessInfoObj.Results.TryGetValue(meth, out mapInfo)) { foreach (var field in mapInfo.Keys) { sb.AppendLine(" -- Field: " + field); HashSet <string> terms; if (mapInfo.TryGetValue(field, out terms)) { foreach (var t in terms) { sb.AppendLine(" term: " + t); } } } } } return(sb); }
public void DumpFieldAccessInfoIntoToFile() { DumpInfoToFile(FieldAccessInfoObj, FieldAcccessFileName); if (isDebug) { SafeStringBuilder sb = DumpFieldAccessInfoToDebugFile(); DumpInfoToDebugFile(sb.ToString(), FieldAcccessFileName + ".txt"); } }
public void Dump() { //aggregate the coverage database.AccumulateMaxCoverage(this.ExplorationServices.Driver.TotalCoverageBuilder); //for debugging purpose in report SafeStringBuilder sb = new SafeStringBuilder(); HashSet <CodeLocation> coveredLocations = new HashSet <CodeLocation>(); foreach (var cl in database.FieldsForUnsuccessfullyFlippedCodeLocations.Keys) { sb.AppendLine("about code location" + cl.ToString()); MethodDefinitionBodyInstrumentationInfo info; if (cl.Method.TryGetBodyInstrumentationInfo(out info)) { ISymbolManager sm = this.Services.SymbolManager; SequencePoint sp; sm.TryGetSequencePoint(null, 0, out sp); int coveredBranchesCount = 0; foreach (var outgoingBranchLabel in info.GetOutgoingBranchLabels(cl.Offset)) { CodeBranch codeBranch = new CodeBranch(cl.Method, outgoingBranchLabel); if (!codeBranch.IsBranch) // is explicit branch? { coveredBranchesCount = 2; // if not, pretend we covered it continue; } CoverageDomain domain; int[] hits; if ( this.ExplorationServices.Driver.TotalCoverageBuilder.TryGetMethodHits(cl.Method, out domain, out hits) && outgoingBranchLabel < hits.Length && hits[outgoingBranchLabel] > 0) //we have branches not being covered for the code location { sb.AppendLine(" outgoing branch " + outgoingBranchLabel + " hit"); coveredBranchesCount++; } } if (coveredBranchesCount > 1) //the location has been covered { coveredLocations.Add(cl); } } } foreach (var cl in coveredLocations) { database.FieldsForUnsuccessfullyFlippedCodeLocations.Remove(cl); } this.Log.Dump("foo", "insufficiency summary", sb.ToString()); //database.DumpInSufficientObjectFactoryFieldInfoIToFile(); }
public TerminalTester(TimeInterval?timeout = null, EndLineOption endLineOption = EndLineOption.TreatLineFeedAsEndLine, bool removeColors = true) { GlobalTimeout = timeout ?? TimeInterval.FromSeconds(DefaultSecondsTimeout); this.endLineOption = endLineOption; this.removeColors = removeColors; charEvent = new AutoResetEvent(false); lines = new List <Line>(); currentLineBuffer = new SafeStringBuilder(); sgrDecodingBuffer = new SafeStringBuilder(); report = new SafeStringBuilder(); }
public void DumpInSufficientObjectFactoryFieldInfoIToFile() { ConvertInfoFromFieldsForUnsuccessfullyFlippedCodeLocationsToInsufficientObjectFactoryFieldInfo(); DumpInfoToFile(InsufficientObjectFactoryFieldInfoObj, InsufficientObjectFactoryFieldFileName); DumpInfoToFile(insufficientFieldInfoWithBranchInfo, insufficientFieldInfoWithBranchInfoFileName); if (isDebug) { SafeStringBuilder sb = DumpInSufficientObjectFactoryFieldInfoToDebugFile(); DumpInfoToDebugFile(sb.ToString(), InsufficientObjectFactoryFieldFileName + ".txt"); } }
//we dump info to files when we finish all exploration public void AfterExecution() { FilterOutCoveredCodeLocations(); this.GetService <ProblemTrackDatabase>().FieldsForUnsuccessfullyFlippedCodeLocations = this.FieldsForUnsuccessfullyFlippedCodeLocations; DumpFieldAccessInfoIntoToFile(); DumpInSufficientObjectFactoryFieldInfoIToFile(); if (isDebug) { SafeStringBuilder sb = DumpFactoryMethodInfoToDebugFile(); DumpInfoToDebugFile(sb.ToString(), FactoryMethodDebugFileName); } }
private void ConvertInfoFromFieldsForUnsuccessfullyFlippedCodeLocationsToInsufficientObjectFactoryFieldInfo() { InsufficientObjectFactoryFieldInfoObj.ReleventFields.Clear(); InsufficientObjectFactoryFieldInfoObj.CodeLocationToRelevantFields.Clear(); SafeStringBuilder sb = new SafeStringBuilder(); foreach (var cl in FieldsForUnsuccessfullyFlippedCodeLocations.Keys) { HashSet <string> fs = new HashSet <string>(); HashSet <string> fs2 = new HashSet <string>(); SafeSet <Field> fields; SequencePoint sequencePoint; Services.SymbolManager.TryGetSequencePoint(cl.Method, cl.Offset, out sequencePoint); fs.Add("column: " + sequencePoint.Column); fs.Add("end column: " + sequencePoint.EndColumn); fs.Add("sp2: " + sequencePoint.Document + " line: " + sequencePoint.Line); fs.Add("offset: " + sequencePoint.Offset); fs.Add("cl offset: " + cl.Offset); var branchInfo = new BranchInfo(sequencePoint.Document, sequencePoint.Line, sequencePoint.Column, sequencePoint.EndColumn, cl.Method.FullName, cl.Offset); if (FieldsForUnsuccessfullyFlippedCodeLocations.TryGetValue(cl, out fields)) { foreach (var f in fields) { InsufficientObjectFactoryFieldInfoObj.ReleventFields.Add(f.FullName); insufficientFieldInfoWithBranchInfo.ReleventFields.Add(f.FullName); fs.Add(f.FullName); fs2.Add(f.FullName); } } if (InsufficientObjectFactoryFieldInfoObj.CodeLocationToRelevantFields.ContainsKey(cl.ToString())) { InsufficientObjectFactoryFieldInfoObj.CodeLocationToRelevantFields.Remove(cl.ToString()); } if (insufficientFieldInfoWithBranchInfo.CodeLocationToRelevantFields.ContainsKey(branchInfo)) { insufficientFieldInfoWithBranchInfo.CodeLocationToRelevantFields.Remove(branchInfo); } InsufficientObjectFactoryFieldInfoObj.CodeLocationToRelevantFields.Add(cl.ToString(), fs); insufficientFieldInfoWithBranchInfo.CodeLocationToRelevantFields.Add(branchInfo, fs2); } }
private SafeStringBuilder DumpFactoryMethodInfoToDebugFile() { SafeStringBuilder sb = new SafeStringBuilder(); sb.AppendLine("Synthesized factory methods ===="); foreach (var type in FactoryMethodInfo.Keys) { sb.AppendLine("Class type: " + type); SafeList <string> mList; if (FactoryMethodInfo.TryGetValue(type, out mList)) { foreach (var m in mList) { sb.AppendLine(m); sb.AppendLine(""); } } } return(sb); }
/// <summary> /// Returns a signature for the method including its name and parameters. /// Return type is not included /// </summary> /// <param name="method"></param> /// <returns></returns> public static string GetMethodSignature(MethodDefinition methoddef) { SafeStringBuilder sb = new SafeStringBuilder(); sb.Append(methoddef.FullName); sb.Append("("); int paramCount = 0, numParams = methoddef.ParameterTypes.Count; foreach (var parametertype in methoddef.ParameterTypes) { sb.Append(parametertype.ToString()); paramCount++; if (paramCount != numParams) { sb.Append(","); } } sb.Append(")"); return(sb.ToString()); }
private SafeStringBuilder DumpInSufficientObjectFactoryFieldInfoToDebugFile() { SafeStringBuilder sb = new SafeStringBuilder(); sb.AppendLine("Relevant fields for non-covered branches ===="); foreach (var field in InsufficientObjectFactoryFieldInfoObj.ReleventFields) { sb.AppendLine(field); } sb.AppendLine(); sb.AppendLine("Detailed code locations and relevant fields ~~~~"); foreach (var cl in InsufficientObjectFactoryFieldInfoObj.CodeLocationToRelevantFields.Keys) { sb.AppendLine(); sb.AppendLine("Code location: " + cl); HashSet <string> fs; if (InsufficientObjectFactoryFieldInfoObj.CodeLocationToRelevantFields.TryGetValue(cl.ToString(), out fs)) { foreach (var f in fs) { sb.AppendLine("field: " + f); } } SafeSet <string> terms; if (TermsForUnsuccessfullyFlippedCodeLocations.TryGetValue(cl, out terms)) { foreach (var t in terms) { sb.AppendLine("path condition term: "); sb.AppendLine(t); } } } return(sb); }
/// <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); } } } }
public void LogExplorableInsufficiency(IExecutionNode executionNode, TypeEx explorableType) { var termManager = this.ExplorationServices.TermManager; var accessedFields = new SafeSet <Field>(); var condition = executionNode.SuccessorLabelToExplore; SafeStringBuilder sb = new SafeStringBuilder(); sb.AppendLine("condition:"); sb.AppendLine(); dumpHelper(new SafeStringWriter(sb), condition); 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; } Term left, right; BinaryOperator binOp; if (termManager.TryGetBinary(unnegatedCondition, out binOp, out left, out right)) { sb.AppendFormat("binary relation: {0}", binOp); sb.AppendLine(); // binOp==BinaryOperator.Ceq tests for equality bool isLeftValue = termManager.IsValue(left); if (termManager.IsValue(left) || termManager.IsValue(right)) { sb.AppendLine("against constant"); if (termManager.IsDefaultValue(left) || termManager.IsDefaultValue(right)) { sb.AppendLine("against default value ('null' for references)"); } int value; if (termManager.TryGetI4Constant(left, out value) || termManager.TryGetI4Constant(right, out value)) { sb.AppendLine("against integer: " + value); } Term objectValue; ObjectProperty objectProperty; if (termManager.TryGetObjectProperty(left, out objectValue, out objectProperty) || termManager.TryGetObjectProperty(right, out objectValue, out objectProperty)) { sb.AppendLine("against object property: object=" + objectValue + ", property=" + objectProperty); } //the following determins what fields are involved on the other side of the constraint Term t; if (isLeftValue) { t = right; } else { t = left; } sb.AppendLine(" involving fields: "); SafeSet <Field> fs = this.GetInvolvedFields(termManager, t); foreach (var f in fs) { sb.AppendLine("f.FullName:" + f.FullName); sb.AppendLine("f.Definition.FullName" + f.Definition.FullName); sb.AppendLine("f.InstanceFieldMapType:" + f.InstanceFieldMapType.FullName); TypeEx type; // type. f.TryGetDeclaringType(out type); sb.AppendLine("f.TryGetDeclaringType: " + type.FullName); } } } sb.AppendLine(); sb.AppendLine("fields:"); sb.AppendLine(); SafeSet <Field> fields = this.GetInvolvedFields(termManager, condition); foreach (var f in fields) { sb.AppendLine(f.FullName); accessedFields.Add(f); } this.Log.Dump("foo", "insufficiency for " + (explorableType != null ? explorableType.FullName : "?"), sb.ToString()); StringBuilder simpleLog = this.GetService <ProblemTrackDatabase>().SimpleLog; simpleLog.AppendLine("insufficiency for " + (explorableType != null ? explorableType.FullName : "?") + sb); //this.unsuccessfullyFlippedCodeLocations.Add(executionNode.CodeLocation); SafeSet <Field> existingAssessedFields; if ( !this.database.FieldsForUnsuccessfullyFlippedCodeLocations.TryGetValue(executionNode.CodeLocation, out existingAssessedFields)) { database.FieldsForUnsuccessfullyFlippedCodeLocations.Add(executionNode.CodeLocation, accessedFields); } else { existingAssessedFields.AddRange(accessedFields); } SafeStringBuilder sbTerm = new SafeStringBuilder(); dumpHelper(new SafeStringWriter(sbTerm), condition); SafeSet <string> terms; if ( !this.database.TermsForUnsuccessfullyFlippedCodeLocations.TryGetValue( executionNode.CodeLocation.ToString(), out terms)) { terms = new SafeSet <string>(); terms.Add(sbTerm.ToString()); database.TermsForUnsuccessfullyFlippedCodeLocations.Add(executionNode.CodeLocation.ToString(), terms); } else { terms.Add(sbTerm.ToString()); } }
/// <summary> /// Returns a signature for the method including its name and parameters. /// Return type is not included /// </summary> /// <param name="method"></param> /// <returns></returns> public static string GetMethodSignature(MethodDefinition methoddef) { SafeStringBuilder sb = new SafeStringBuilder(); sb.Append(methoddef.FullName); sb.Append("("); int paramCount = 0, numParams = methoddef.ParameterTypes.Count; foreach (var parametertype in methoddef.ParameterTypes) { sb.Append(parametertype.ToString()); paramCount++; if (paramCount != numParams) sb.Append(","); } sb.Append(")"); return sb.ToString(); }
private void ConvertInfoFromFieldsForUnsuccessfullyFlippedCodeLocationsToInsufficientObjectFactoryFieldInfo() { InsufficientObjectFactoryFieldInfoObj.ReleventFields.Clear(); InsufficientObjectFactoryFieldInfoObj.CodeLocationToRelevantFields.Clear(); SafeStringBuilder sb = new SafeStringBuilder(); foreach (var cl in FieldsForUnsuccessfullyFlippedCodeLocations.Keys) { HashSet<string> fs = new HashSet<string>(); HashSet<string> fs2 = new HashSet<string>(); SafeSet<Field> fields; SequencePoint sequencePoint; Services.SymbolManager.TryGetSequencePoint(cl.Method, cl.Offset, out sequencePoint); fs.Add("column: " + sequencePoint.Column); fs.Add("end column: " + sequencePoint.EndColumn); fs.Add("sp2: " + sequencePoint.Document + " line: " + sequencePoint.Line); fs.Add("offset: " + sequencePoint.Offset); fs.Add("cl offset: " + cl.Offset); var branchInfo = new BranchInfo(sequencePoint.Document, sequencePoint.Line, sequencePoint.Column, sequencePoint.EndColumn, cl.Method.FullName, cl.Offset); if (FieldsForUnsuccessfullyFlippedCodeLocations.TryGetValue(cl, out fields)) { foreach (var f in fields) { InsufficientObjectFactoryFieldInfoObj.ReleventFields.Add(f.FullName); insufficientFieldInfoWithBranchInfo.ReleventFields.Add(f.FullName); fs.Add(f.FullName); fs2.Add(f.FullName); } } if (InsufficientObjectFactoryFieldInfoObj.CodeLocationToRelevantFields.ContainsKey(cl.ToString())) { InsufficientObjectFactoryFieldInfoObj.CodeLocationToRelevantFields.Remove(cl.ToString()); } if (insufficientFieldInfoWithBranchInfo.CodeLocationToRelevantFields.ContainsKey(branchInfo)) { insufficientFieldInfoWithBranchInfo.CodeLocationToRelevantFields.Remove(branchInfo); } InsufficientObjectFactoryFieldInfoObj.CodeLocationToRelevantFields.Add(cl.ToString(), fs); insufficientFieldInfoWithBranchInfo.CodeLocationToRelevantFields.Add(branchInfo, fs2); } }
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()); }
private SafeStringBuilder DumpFactoryMethodInfoToDebugFile() { SafeStringBuilder sb = new SafeStringBuilder(); sb.AppendLine("Synthesized factory methods ===="); foreach (var type in FactoryMethodInfo.Keys) { sb.AppendLine("Class type: " + type); SafeList<string> mList; if (FactoryMethodInfo.TryGetValue(type, out mList)) { foreach (var m in mList) { sb.AppendLine(m); sb.AppendLine(""); } } } return sb; }
private SafeStringBuilder DumpInSufficientObjectFactoryFieldInfoToDebugFile() { SafeStringBuilder sb = new SafeStringBuilder(); sb.AppendLine("Relevant fields for non-covered branches ===="); foreach (var field in InsufficientObjectFactoryFieldInfoObj.ReleventFields) { sb.AppendLine(field); } sb.AppendLine(); sb.AppendLine("Detailed code locations and relevant fields ~~~~"); foreach (var cl in InsufficientObjectFactoryFieldInfoObj.CodeLocationToRelevantFields.Keys) { sb.AppendLine(); sb.AppendLine("Code location: " + cl); HashSet<string> fs; if (InsufficientObjectFactoryFieldInfoObj.CodeLocationToRelevantFields.TryGetValue(cl.ToString(), out fs)) { foreach (var f in fs) { sb.AppendLine("field: " + f); } } SafeSet<string> terms; if (TermsForUnsuccessfullyFlippedCodeLocations.TryGetValue(cl, out terms)) { foreach (var t in terms) { sb.AppendLine("path condition term: "); sb.AppendLine(t); } } } return sb; }
private SafeStringBuilder DumpFieldAccessInfoToDebugFile() { SafeStringBuilder sb = new SafeStringBuilder(); foreach (var meth in FieldAccessInfoObj.Results.Keys) { sb.AppendLine("Method: " + meth); System.Collections.Generic.Dictionary<string, HashSet<string>> mapInfo; if (FieldAccessInfoObj.Results.TryGetValue(meth, out mapInfo)) { foreach (var field in mapInfo.Keys) { sb.AppendLine(" -- Field: " + field); HashSet<string> terms; if (mapInfo.TryGetValue(field, out terms)) { foreach (var t in terms) { sb.AppendLine(" term: " + t); } } } } } return sb; }
/// <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> /// 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()); }
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; }
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> /// 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()); }