public async Task SaveInvoice() { var termMgr = new TermManager(); var invoice = await termMgr.SaveInvoice(this.Invoice); this.InvoiceSaved(invoice); }
VariablesCollector(TermManager termManager, SafeDictionary <Field, FieldValueHolder> fieldValues, SafeList <Field> allFields, SafeList <TypeEx> allFieldTypes) : base(termManager, OnCollection.Fail) { this.FieldValues = fieldValues; this.Fields = allFields; this.Types = allFieldTypes; }
public LogManager(string logPrefix, IArithmeticSolvingContext context) { this.termManager = context.Host.ExplorationServices.TermManager; this.context = context; this.overwrite = false; MakeUniqueFileName(logPrefix); }
// ********************************************************************** public QuikTerminal(TermManager mgr) { this.mgr = mgr; msg = new StringBuilder(256); connecting = new Timer(TryConnect); }
public LogManager(string logPrefix, bool overwrite) { this.termManager = null; this.context = null; this.overwrite = overwrite; MakeUniqueFileName(logPrefix); }
private IEnumerable <TypeEx> GetInvolvedObjectTypes(TermManager manager, Term term) { using (var ofc = new ObjectFieldCollector(manager)) { ofc.VisitTerm(default(TVoid), term); return(ofc.Types); } }
private SafeSet <Field> GetInvolvedFields(TermManager termManager, Term t) { using (var ofc = new ObjectFieldCollector(termManager)) { ofc.VisitTerm(default(TVoid), t); return(ofc.Fields); } }
public static SafeList <TypeEx> GetInvolvedTypes(IPexComponent host, TermManager termManager, Term t) { using (var ofc = new ObjectFieldCollector(host, termManager)) { ofc.VisitTerm(default(TVoid), t); return(ofc.Types); } }
public static SafeList<TypeEx> GetInvolvedTypes(IPexComponent host, TermManager termManager, Term t) { using (var ofc = new ObjectFieldCollector(host, termManager)) { ofc.VisitTerm(default(TVoid), t); return ofc.Types; } }
public async Task GetActiveTerm() { var termMgr = new TermManager(); var termList = await termMgr.GetTerms(null); // There will always only be just one active term this.ActiveTerm = termList.FirstOrDefault(x => x.IsActive); }
public static ISymbolIdWithType[] Collect(TermManager termManager, Term term, SafeDictionary <Field, FieldValueHolder> fieldValues, SafeList <Field> fields, SafeList <TypeEx> allFieldTypes) { using (var collector = new VariablesCollector(termManager, fieldValues, fields, allFieldTypes)) { collector.VisitTerm(default(TVoid), term); return(collector.variables.ToArray()); } }
public async Task LoadTermDetails() { var termMgr = new TermManager(); this.TermDetail = await termMgr.GetTermDetail(this.Term.TermID); //this.TermInvoice = await termMgr.GetTermInvoiceByTerm() //this.IsBusy = false; }
public static SafeList<Field> GetInvolvedFields(IPexComponent host, TermManager termManager, Term t, out SafeDictionary<Field, FieldValueHolder> fieldValues, out SafeList<TypeEx> allFieldTypes) { using (var ofc = new ObjectFieldCollector(host, termManager)) { ofc.VisitTerm(default(TVoid), t); fieldValues = ofc.FieldValues; allFieldTypes = ofc.Types; return ofc.Fields; } }
public static SafeList <Field> GetInvolvedFields(IPexComponent host, TermManager termManager, Term t, out SafeDictionary <Field, FieldValueHolder> fieldValues, out SafeList <TypeEx> allFieldTypes) { using (var ofc = new ObjectFieldCollector(host, termManager)) { ofc.VisitTerm(default(TVoid), t); fieldValues = ofc.FieldValues; allFieldTypes = ofc.Types; return(ofc.Fields); } }
/// <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); }
public async Task GetTerms() { this.TermList.Clear(); var termMgr = new TermManager(); var termList = await termMgr.GetTerms(null); foreach (var term in termList) { this.TermList.Add(term); } this.IsBusy = false; }
/// <summary> /// Checks whether the term includes integer constants /// </summary> /// <param name="termManager"></param> /// <param name="condition"></param> /// <returns></returns> private static bool IsInteger(TermManager termManager, Term condition) { //further processing is required only for integer types switch (termManager.GetLayoutKind(condition)) { case LayoutKind.I1: case LayoutKind.I2: case LayoutKind.I4: case LayoutKind.I8: return(true); default: return(false); } }
public void InitializeCustomSolver(IArithmeticSolvingContext context) { this.context = context; this.modelBuilder = context.CreateArithmeticModelBuilder(context.InitialModel); this.startTime = DateTime.Now; this.fitnessEvaluations = 0; this.termManager = context.Host.ExplorationServices.TermManager; this.random = context.Random; if (!this.initialized && this.isLoggingEnabled) { this.logManager = new LogManager(this.name + "_" + this.explorationName, context); this.initialized = true; } }
public void ConvertTermToText(TextWriter writer, Term term, TermManager termManager) { var emitter = new TermEmitter(termManager, new NameCreator()); IMethodBodyWriter codeWriter = this.services.TestManager.Language.CreateBodyWriter( writer, VisibilityContext.Private, 100); if (!emitter.TryEvaluate( new Term[] { term }, 10000, // bound on size of expression we are going to pretty-print codeWriter)) { writer.WriteLine("expression too big"); return; } codeWriter.Return(SystemTypes.Bool); }
public async Task SaveTerm() { var termMgr = new TermManager(); await termMgr.SaveTerm(this.Term); }
/// <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 TermController(TermManager termManager, CatalogValueManager catalogValueManager) { m_termManager = termManager; m_catalogValueManager = catalogValueManager; }
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()); }
/// <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"); }
/// <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? } }
protected override void Initialize() { database = this.GetService <SeqexDatabase>(); FieldAccessInfoObj = database.FieldAccessInfoObj; this.termManager = this.ExplorationServices.TermManager; }
/// <summary> /// OBSOLETE: /// </summary> /// <param name="termManager"></param> /// <param name="left"></param> /// <param name="right"></param> /// <param name="binOp"></param> /// <param name="bNegated"></param> /// <param name="culpritFields"></param> /// <param name="completeTerm"></param> /// <param name="fmt"></param> /// <param name="fitnessval"></param> private void handleNoConstantsInTerm(TermManager termManager, Term left, Term right, BinaryOperator binOp, bool bNegated, SafeList<Field> culpritFields, Term completeTerm, out FieldModificationType fmt, out int fitnessval) { fmt = FieldModificationType.UNKNOWN; //Term termUnderAnalysis = null; //Term otherTerm = null; //Field instanceField; //if (termManager.TryGetInstanceField(left, out instanceField) && culpritFields.Contains(instanceField)) //{ // termUnderAnalysis = left; // otherTerm = right; //} //else //{ // if (termManager.TryGetInstanceField(right, out instanceField)) // if (culpritFields.Contains(instanceField)) // { // termUnderAnalysis = right; // otherTerm = left; // } //} //if (termUnderAnalysis == null) // return; //object value; //if (termManager.TryGetObject(left, out value)) //{ // if (value == null && binOp == BinaryOperator.Ceq) // { // fmt = bNegated ? FieldModificationType.NON_NULL_SET : FieldModificationType.NULL_SET; // } // else if (value is int || value is Int16 || value is Int32 || value is Int64) // { // fmt = FieldModificationType.INCREMENT; //TODO: Needs to get actual values and decide based on that // } //} else if (termManager.TryGetObject(right, out value)) //{ // if (value == null && binOp == BinaryOperator.Ceq) // { // fmt = bNegated ? FieldModificationType.NON_NULL_SET : FieldModificationType.NULL_SET; // } // else if (value is int || value is Int16 || value is Int32 || value is Int64) // { // fmt = FieldModificationType.INCREMENT; //TODO: Needs to get actual values and decide based on that // } //} //TODO: A worst fix to proceed further fitnessval = Int32.MaxValue; if (culpritFields.Count == 0) return; var culprittype = culpritFields[0].Type; if (culprittype.IsReferenceType) fmt = FieldModificationType.NON_NULL_SET; else { var typestr = culprittype.ToString(); if (typestr == "System.Boolean") fmt = FieldModificationType.TRUE_SET; else if (typestr == "System.Int32" || typestr == "System.Int64" || typestr == "System.Int16") { SafeDictionary<Field, FieldValueHolder> fieldValues; //TermSolver.SolveTerm(this.ter, completeTerm, out fieldValues); fmt = FieldModificationType.INCREMENT; } } }
/// <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> /// Adds a monitored field to the database. Updates two kinds of hashmaps /// a. Field to Method mapper, which gives what the methods modifying a given field /// b. Method to Field mapper, which gives what fields are modified by each method (later used to identify a minimized set of methods) /// </summary> /// <param name="tm"></param> /// <param name="method"></param> /// <param name="f"></param> /// <param name="indices"></param> /// <param name="fieldValue"></param> public void AddMonitoredField(TermManager tm, Method method, Field f, Term[] indices, Term fieldValue, Term initialValue) { string arrayIndex = ""; using (PexMeTermRewriter pexmeRewriter = new PexMeTermRewriter(tm)) { fieldValue = pexmeRewriter.VisitTerm(default(TVoid), fieldValue); //update the field value to accomodate array-type field //if (indices.Length == 0) //not an array-type field if (indices.Length == 1) //is an array-type field { arrayIndex = " at index of " + indices[0].UniqueIndex.ToString(); } if (initialValue != null) { initialValue = pexmeRewriter.VisitTerm(default(TVoid), initialValue); } } //Updating the method store MethodStore ms; if (!methodDic.TryGetValue(method, out ms)) { ms = new MethodStore(); ms.methodName = method; methodDic[method] = ms; } ms.WriteFields.Add(f); //TODO: Gather information of read fields //Updating the field store FieldStore fs; if (!fieldDic.TryGetValue(f, out fs)) { fs = new FieldStore(); fs.FieldName = f; fieldDic[f] = fs; } TypeEx declaringType; if (!method.TryGetDeclaringType(out declaringType)) { this.Log.LogError(WikiTopics.MissingWikiTopic, "monitorfield", "Failed to get the declaring type for the method " + method.FullName); return; } SafeSet <Method> writeMethods; if (!fs.WriteMethods.TryGetValue(declaringType, out writeMethods)) { writeMethods = new SafeSet <Method>(); fs.WriteMethods[declaringType] = writeMethods; } writeMethods.Add(method); var sb = new SafeStringBuilder(); var swriter = new TermSExpWriter(tm, new SafeStringWriter(sb), true, false); swriter.Write(fieldValue); sb.Append(arrayIndex); int value; if (tm.TryGetI4Constant(fieldValue, out value)) { int initialval; if (initialValue != null) { tm.TryGetI4Constant(initialValue, out initialval); } else { initialval = 0; } sb.Append(" constant value: " + value); if (f.Type.ToString() != "System.Boolean") { if (value < initialval) { fs.ModificationTypeDictionary[method] = FieldModificationType.DECREMENT; } else if (value > initialval) { fs.ModificationTypeDictionary[method] = FieldModificationType.INCREMENT; if (value == initialval + 1) { fs.PreciseModificationTypeDictionary[method] = FieldModificationType.INCREMENT_ONE; } } else { fs.ModificationTypeDictionary[method] = FieldModificationType.UNKNOWN; } } else { if (value == 0) { fs.ModificationTypeDictionary[method] = FieldModificationType.FALSE_SET; } else { fs.ModificationTypeDictionary[method] = FieldModificationType.TRUE_SET; } } } else if (tm.IsDefaultValue(fieldValue)) { if (initialValue != null && !tm.IsDefaultValue(initialValue)) { fs.ModificationTypeDictionary[method] = FieldModificationType.NULL_SET; } else { fs.ModificationTypeDictionary[method] = FieldModificationType.UNKNOWN; } sb.Append(" null reference "); } else { if (initialValue == null) { fs.ModificationTypeDictionary[method] = FieldModificationType.NON_NULL_SET; } else { fs.ModificationTypeDictionary[method] = FieldModificationType.UNKNOWN; } sb.Append(" not-null reference "); } fs.FieldValues.Add(sb.ToString()); }
/// <summary> /// OBSOLETE: /// </summary> /// <param name="termManager"></param> /// <param name="left"></param> /// <param name="right"></param> /// <param name="binOp"></param> /// <param name="bNegated"></param> /// <param name="culpritFields"></param> /// <param name="completeTerm"></param> /// <param name="fmt"></param> /// <param name="fitnessval"></param> private void handleNoConstantsInTerm(TermManager termManager, Term left, Term right, BinaryOperator binOp, bool bNegated, SafeList <Field> culpritFields, Term completeTerm, out FieldModificationType fmt, out int fitnessval) { fmt = FieldModificationType.UNKNOWN; //Term termUnderAnalysis = null; //Term otherTerm = null; //Field instanceField; //if (termManager.TryGetInstanceField(left, out instanceField) && culpritFields.Contains(instanceField)) //{ // termUnderAnalysis = left; // otherTerm = right; //} //else //{ // if (termManager.TryGetInstanceField(right, out instanceField)) // if (culpritFields.Contains(instanceField)) // { // termUnderAnalysis = right; // otherTerm = left; // } //} //if (termUnderAnalysis == null) // return; //object value; //if (termManager.TryGetObject(left, out value)) //{ // if (value == null && binOp == BinaryOperator.Ceq) // { // fmt = bNegated ? FieldModificationType.NON_NULL_SET : FieldModificationType.NULL_SET; // } // else if (value is int || value is Int16 || value is Int32 || value is Int64) // { // fmt = FieldModificationType.INCREMENT; //TODO: Needs to get actual values and decide based on that // } //} else if (termManager.TryGetObject(right, out value)) //{ // if (value == null && binOp == BinaryOperator.Ceq) // { // fmt = bNegated ? FieldModificationType.NON_NULL_SET : FieldModificationType.NULL_SET; // } // else if (value is int || value is Int16 || value is Int32 || value is Int64) // { // fmt = FieldModificationType.INCREMENT; //TODO: Needs to get actual values and decide based on that // } //} //TODO: A worst fix to proceed further fitnessval = Int32.MaxValue; if (culpritFields.Count == 0) { return; } var culprittype = culpritFields[0].Type; if (culprittype.IsReferenceType) { fmt = FieldModificationType.NON_NULL_SET; } else { var typestr = culprittype.ToString(); if (typestr == "System.Boolean") { fmt = FieldModificationType.TRUE_SET; } else if (typestr == "System.Int32" || typestr == "System.Int64" || typestr == "System.Int16") { SafeDictionary <Field, FieldValueHolder> fieldValues; //TermSolver.SolveTerm(this.ter, completeTerm, out fieldValues); fmt = FieldModificationType.INCREMENT; } } }
public ObjectFieldCollector(TermManager termManager) : base(termManager, TermInternalizingRewriter <TVoid> .OnCollection.Fail) { }
public PexMeTermRewriter(TermManager termManager) : base(termManager, OnCollection.Fail) { }
public ObjectFieldCollector(IPexComponent host, TermManager termManager) : base(termManager, TermInternalizingRewriter <TVoid> .OnCollection.Fail) { this.host = host; }
protected override void Initialize() { base.Initialize(); this.pmd = this.GetService<IPexMeDynamicDatabase>(); this.termManager = this.ExplorationServices.TermManager; }
public SomeRewriter(TermManager termManager) : base(termManager, OnCollection.Fail) { }
/// <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); } } } }