private static string prettyPrintPathCondition(IPexPathComponent host, Term[] pathConditions) { var writer = new SafeStringWriter(); var codeWriter = host.Services.TestManager.Language.CreateBodyWriter( writer, VisibilityContext.Private, 100); var emitter = new TermEmitter( host.ExplorationServices.TermManager, new NameCreator()); for (int i = pathConditions.Length - 1; i >= 0; i--) { var pathCondition = pathConditions[i]; if (!emitter.TryEvaluate( new Term[] { pathCondition }, 10000, // bound on size of expression we are going to pretty-print codeWriter)) { writer.WriteLine("(expression too big; consider using environment variable {0})", ExtendedReflectionEnvironmentSettings.NoCodeEmitterSizeLimit.Name); } else { writer.WriteLine("path condition: " + pathCondition.GetType()); codeWriter.Return(SystemTypes.Bool); } } return(writer.ToString()); }
/// <summary> /// Pretty prints the path condition of the execution node. /// </summary> /// <param name="host">Host of the Pex Path Component</param> /// <param name="node">The execution node to map</param> /// <returns>The pretty printed path condition string</returns> private Task <string> PrettyPrintPathCondition(TermEmitter emitter, IMethodBodyWriter mbw, SafeStringWriter ssw, IExecutionNode node) { MessageBox.Show("PrettyPrintPathCondition()"); var task = Task.Factory.StartNew(() => { string output = ""; try { if (node.GetPathCondition().Conjuncts.Count != 0) { if (emitter.TryEvaluate(node.GetPathCondition().Conjuncts, 2000, mbw)) // TODO Perf leak { for (int i = 0; i < node.GetPathCondition().Conjuncts.Count - 1; i++) { mbw.ShortCircuitAnd(); } mbw.Statement(); var safeString = ssw.ToString(); output = safeString.Remove(ssw.Length - 3); } } } catch (Exception) { } return(output); }); return(task); }
/// <summary> /// Pretty prints the path condition of the given execution node. /// </summary> /// <param name="node"></param> /// <returns></returns> private string PrettyPrintPathCondition(IExecutionNode node) { string output = ""; try { if (node.GetPathCondition().Conjuncts.Count != 0) { TermEmitter termEmitter = new TermEmitter(pathComponent.GetService <TermManager>()); SafeStringWriter safeStringWriter = new SafeStringWriter(); IMethodBodyWriter methodBodyWriter = pathComponent.GetService <IPexTestManager>().Language.CreateBodyWriter(safeStringWriter, VisibilityContext.Private, 2000); if (termEmitter.TryEvaluate(node.GetPathCondition().Conjuncts, 2000, methodBodyWriter)) { for (int i = 0; i < node.GetPathCondition().Conjuncts.Count - 1; i++) { methodBodyWriter.ShortCircuitAnd(); } methodBodyWriter.Statement(); output = safeStringWriter.ToString().Remove(safeStringWriter.ToString().Count() - 3); } } } catch (Exception e) { // TODO : Exception handling? } return(output); }
private static string prettyPrintTerm(IPexExplorationComponent host, Term term, TypeEx type) { var writer = new SafeStringWriter(); var codeWriter = host.Services.TestManager.Language.CreateBodyWriter( writer, VisibilityContext.Private, 100); var emitter = new TermEmitter( host.ExplorationServices.TermManager, new NameCreator()); if (!emitter.TryEvaluate( new Term[] { term }, 10000, // bound on size of expression we are going to pretty-print codeWriter)) { writer.WriteLine("(expression too big; consider using environment variable {0})", ExtendedReflectionEnvironmentSettings.NoCodeEmitterSizeLimit.Name); } else { codeWriter.Return(type); } return(writer.ToString()); }
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); }
private void DumpProblem() { System.Text.StringBuilder sb = new System.Text.StringBuilder(); foreach (var problemEventArgs in PexProblemEventArgsList) { CodeLocation location = problemEventArgs.FlippedLocation; Log.Dump("My Category", "flipped location: " + location, null); SequencePoint sp; Services.SymbolManager.TryGetSequencePoint(location.Method, location.Offset, out sp); sb.AppendLine("======================================================="); sb.AppendLine("result: " + problemEventArgs.Result); sb.AppendLine("flipped location: " + sp.Document + " line: " + sp.Line); var flippedCondition = problemEventArgs.Suffix; // sb.AppendLine("suffix term: " + ((ISymbolId)flippedCondition).Description); var location1 = problemEventArgs.ParentOfFlipped.CodeLocation; Services.SymbolManager.TryGetSequencePoint(location1.Method, location1.Offset, out sp); sb.AppendLine("ParentOfFlipped location: " + sp.Document + " line: " + sp.Line); var stringWriter = new StringWriter(); var bodyWriter = this.Services.LanguageManager.DefaultLanguage.CreateBodyWriter(stringWriter, VisibilityContext. Private); var extractor = new ResultTrackConditionExtractor(problemEventArgs.TermManager); extractor.VisitTerm(default(TVoid), flippedCondition); var emitter = new TermEmitter(problemEventArgs.TermManager, new NameCreator()); if (emitter.TryEvaluate(Indexable.One(flippedCondition), 1000, bodyWriter)) { bodyWriter.Return(SystemTypes.Bool); } Log.Dump("My Category", "condition", stringWriter.ToString()); sb.AppendLine(stringWriter.ToString()); } DumpInfoToDebugFile(sb.ToString(), problemIssueFileName); }
private static string prettyPrintPathCondition(IPexPathComponent host, Term[] pathConditions) { var writer = new SafeStringWriter(); var codeWriter = host.Services.TestManager.Language.CreateBodyWriter( writer, VisibilityContext.Private, 100); var emitter = new TermEmitter( host.ExplorationServices.TermManager, new NameCreator()); for (int i = pathConditions.Length - 1; i >= 0; i--) { var pathCondition = pathConditions[i]; if (!emitter.TryEvaluate( new Term[] {pathCondition}, 10000, // bound on size of expression we are going to pretty-print codeWriter)) { writer.WriteLine("(expression too big; consider using environment variable {0})", ExtendedReflectionEnvironmentSettings.NoCodeEmitterSizeLimit.Name); } else { writer.WriteLine("path condition: " + pathCondition.GetType()); codeWriter.Return(SystemTypes.Bool); } } return writer.ToString(); }
public void AfterRun(IPexPathComponent host, object data) { MessageBox.Show("AfterRun"); // Getting the executions nodes in the current path var nodesInPath = host.PathServices.CurrentExecutionNodeProvider.ReversedNodes.Reverse().ToArray(); // Getting the sequence id of the current run var runId = host.ExplorationServices.Driver.Runs; // Iterating over the nodes in the path foreach (var node in nodesInPath) { var vertex = new CFGNode(node.UniqueIndex, false); // Adding the method name this early in order to color edges string methodName = null; int offset = 0; if (node.CodeLocation.Method == null) { if (node.InCodeBranch.Method != null) { methodName = node.InCodeBranch.Method.FullName; } } else { methodName = node.CodeLocation.Method.FullName; offset = node.CodeLocation.Offset; } // Setting the method name vertex.MethodName = methodName; // Setting the offset vertex.ILOffset = (uint)offset; var nodeIndex = nodesInPath.ToList().IndexOf(node); if (nodeIndex > 0) { var prevNode = nodesInPath [nodeIndex - 1]; // If there is no edge between the previous and the current node if (!(Edges.ContainsKey(prevNode.UniqueIndex) && Edges [prevNode.UniqueIndex].ContainsKey(node.UniqueIndex))) { var prevVertex = Vertices [prevNode.UniqueIndex]; var edge = new CFGEdge(new Random().Next(), prevVertex, vertex); Dictionary <int, CFGEdge> outEdges = null; if (Edges.TryGetValue(prevNode.UniqueIndex, out outEdges)) { outEdges.Add(node.UniqueIndex, edge); } else { Edges.Add(prevNode.UniqueIndex, new Dictionary <int, CFGEdge>()); Edges [prevNode.UniqueIndex].Add(node.UniqueIndex, edge); } // Edge coloring based on unit border detection if (UnitNamespace != null) { // Checking if pointing into the unit from outside if (!(prevVertex.MethodName.StartsWith(UnitNamespace + ".") || prevVertex.MethodName.Equals(UnitNamespace)) && (vertex.MethodName.StartsWith(UnitNamespace + ".") || vertex.MethodName.Equals(UnitNamespace))) { edge.Color = CFGEdge.EdgeColor.Green; } // Checking if pointing outside the unit from inside if ((prevVertex.MethodName.StartsWith(UnitNamespace + ".") || prevVertex.MethodName.Equals(UnitNamespace)) && !(vertex.MethodName.StartsWith(UnitNamespace + ".") || vertex.MethodName.Equals(UnitNamespace))) { edge.Color = CFGEdge.EdgeColor.Red; } } } } // If the node is new then it is added to the list and the metadata is filled if (!Vertices.ContainsKey(node.UniqueIndex)) { Vertices.Add(node.UniqueIndex, vertex); // Adding source code mapping vertex.SourceCodeMappingString = MapToSourceCodeLocationString(host, node); // Setting the border based on mapping existence vertex.Border = vertex.SourceCodeMappingString == null ? CFGNode.NodeBorder.Single : CFGNode.NodeBorder.Double; // Setting the color if (nodesInPath.LastOrDefault() == node) { if (!EmittedTestResult.ContainsKey(runId)) { vertex.Color = CFGNode.NodeColor.Orange; } else { if (EmittedTestResult [runId].Item1) { vertex.Color = CFGNode.NodeColor.Red; } else { vertex.Color = CFGNode.NodeColor.Green; } vertex.GenerateTestCode = EmittedTestResult [runId].Item2; } } else { vertex.Color = CFGNode.NodeColor.White; } // Setting the default shape vertex.Shape = CFGNode.NodeShape.Rectangle; // Adding path condition tasks and getting the required services TermEmitter termEmitter = new TermEmitter(host.GetService <TermManager>()); SafeStringWriter safeStringWriter = new SafeStringWriter(); IMethodBodyWriter methodBodyWriter = host.GetService <IPexTestManager>().Language.CreateBodyWriter(safeStringWriter, VisibilityContext.Private, 2000); PrettyPathConditionTasks.Add(vertex.Id, PrettyPrintPathCondition(termEmitter, methodBodyWriter, safeStringWriter, node)); // Setting the status vertex.Status = node.ExhaustedReason.ToString(); // Collecting the parent nodes for the later incremental path condition calculation if (nodeIndex > 0) { ParentNodes.Add(vertex.Id, nodesInPath [nodeIndex - 1].UniqueIndex); } } // Adding the Id of the run Vertices [node.UniqueIndex].Runs += (runId + ";"); } }
void Log_ExplorationBoundaryHandler(ExplorationBoundaryEventArgs e) { var database = Host.GetService<ProblemTrackDatabase>(); ProblemEventArgs successfulFlippedPathCondition = database.CurrentSuccessfulFlippedPathCondition; StringBuilder log = database.SimpleLog; SequencePoint sp; if (successfulFlippedPathCondition == null) { return; } CodeLocation location = successfulFlippedPathCondition.FlippedLocation; Host.Services.SymbolManager.TryGetSequencePoint(location.Method, location.Offset, out sp); StringBuilder sb = new StringBuilder("/////////////////////////////////// \n"); log.AppendLine("exception: " + e.Kind + " e.TargetName: " + e.TargetName + " message: " + e); sb.AppendLine("flipped location: " + sp.Document + " line: " + sp.Line); var branchInfo = new BranchInfo("",0,0,0,"",0); try { branchInfo = new BranchInfo(sp.Document, sp.Line, sp.Column, sp.EndColumn, location.Method.FullName, location.Offset); } catch (Exception) { } var flippedCondition = successfulFlippedPathCondition.Suffix; var stringWriter = new StringWriter(); var bodyWriter = this.Host.Services.LanguageManager.DefaultLanguage.CreateBodyWriter(stringWriter, VisibilityContext. Private); var emitter = new TermEmitter(successfulFlippedPathCondition.TermManager, new NameCreator()); if (emitter.TryEvaluate(Indexable.One(flippedCondition), 1000, bodyWriter)) { bodyWriter.Return(SystemTypes.Bool); } stringWriter.WriteLine(); stringWriter.WriteLine("Feasible prefixes:"); if (successfulFlippedPathCondition.FeasiblePrefix != null && successfulFlippedPathCondition.FeasiblePrefix.Length > 0) { var bodyWriter2 = this.Host.Services.LanguageManager.DefaultLanguage.CreateBodyWriter(stringWriter, VisibilityContext. Private); foreach (Term prefix in successfulFlippedPathCondition.FeasiblePrefix) { if (emitter.TryEvaluate(Indexable.One(prefix), 1000, bodyWriter2)) { bodyWriter2.Return(SystemTypes.Bool); } } } else { stringWriter.WriteLine("No feasible prefixes."); } this.Host.Log.Dump("My Category", "condition", stringWriter.ToString()); sb.AppendLine(stringWriter.ToString()); sb.AppendLine("///////////////////////////////////"); var issue = new BoundaryProblem(e.Kind.ToString(), e.TargetName.ToString(), e.Message, branchInfo, stringWriter.ToString()); Host.GetService<ProblemTrackDatabase>().BoundaryIssues.Add(issue); log.AppendLine(sb.ToString()); // e. }
private void Log_ProblemHandler(Microsoft.ExtendedReflection.Logging.ProblemEventArgs e) { RecordFlipCount(e); if (e.Result == TryGetModelResult.Success) { Host.GetService <ProblemTrackDatabase>().SimpleLog.AppendLine("flipped result: " + e.Result); Host.GetService <ProblemTrackDatabase>().CurrentSuccessfulFlippedPathCondition = e; return; } if (e.Result != TryGetModelResult.Success) { Host.GetService <ProblemTrackDatabase>().SimpleLog.AppendLine("flipped result: " + e.Result); // return; } try { System.Text.StringBuilder sb = new System.Text.StringBuilder(); CodeLocation location = e.FlippedLocation; var database = Host.GetService <ProblemTrackDatabase>(); database.PexProblemEventArgsList.Add(e); // e.Result == TryGetModelResult. this.Host.Log.Dump("My Category", "flipped location: " + location, null); SequencePoint sp; Host.Services.SymbolManager.TryGetSequencePoint(location.Method, location.Offset, out sp); sb.AppendLine("flipped location: " + sp.Document + " line: " + sp.Line + " offset: " + location.Offset); sb.AppendLine("e.ParentOfFlipped.InCodeBranch: " + e.ParentOfFlipped.InCodeBranch); e.ParentOfFlipped.OutCodeBranches.ToList().ForEach(x => sb.AppendLine("out: " + x)); var flippedCondition = e.Suffix; var parentOfFlipped = e.ParentOfFlipped.CodeLocation; Host.Services.SymbolManager.TryGetSequencePoint(parentOfFlipped.Method, parentOfFlipped.Offset, out sp); sb.AppendLine("parent flipped location: " + sp.Document + " line: " + sp.Line + " offset: " + parentOfFlipped.Offset); var stringWriter = new StringWriter(); var bodyWriter = this.Host.Services.LanguageManager.DefaultLanguage.CreateBodyWriter(stringWriter, VisibilityContext . Private); var emitter = new TermEmitter(e.TermManager, new NameCreator()); if (emitter.TryEvaluate(Indexable.One(flippedCondition), 1000, bodyWriter)) { bodyWriter.Return(SystemTypes.Bool); } string flippedTerm = stringWriter.ToString(); stringWriter.WriteLine(); stringWriter.WriteLine("Feasible prefixes:"); if (e.FeasiblePrefix != null && e.FeasiblePrefix.Length > 0) { var bodyWriter2 = this.Host.Services.LanguageManager.DefaultLanguage.CreateBodyWriter(stringWriter, VisibilityContext . Private); foreach (Term prefix in e.FeasiblePrefix) { if (emitter.TryEvaluate(Indexable.One(prefix), 1000, bodyWriter2)) { bodyWriter2.Return(SystemTypes.Bool); } } } else { stringWriter.WriteLine("No feasible prefixes."); } string feasiblePrefix = stringWriter.ToString().Substring(stringWriter.ToString().IndexOf("Feasible prefixes:")); this.Host.Log.Dump("My Category", "condition", stringWriter.ToString()); sb.AppendLine(stringWriter.ToString()); int returnIndex = flippedTerm.IndexOf("return"); sb.AppendLine("flipped term: " + flippedTerm.Substring(returnIndex)); string targetObjectType = null; bool infeasible = false; if (flippedTerm.Contains("!=") && flippedTerm.Contains("null")) { targetObjectType = flippedTerm.Substring(flippedTerm.IndexOf("(") + 1, flippedTerm.IndexOf(")") - flippedTerm.IndexOf("(") - 1); sb.AppendLine("targetObjectType: " + targetObjectType); int index = flippedTerm.IndexOf("!=") - 2; int length = 0; while (flippedTerm[index] != ' ') { index--; length++; } string variable = flippedTerm.Substring(index + 1, length); sb.AppendLine("variable for targetObjectType: " + variable); string infeasibleCheck = variable + ".GetType() != typeof(" + targetObjectType + ")"; string conflictCheck = variable + " == (" + targetObjectType + ")null"; sb.AppendLine("test for infeasible: " + infeasibleCheck); if (feasiblePrefix.Contains(infeasibleCheck)) { sb.AppendLine("found infeasible constraint: " + infeasibleCheck); infeasible = true; } else if (feasiblePrefix.Contains(conflictCheck)) { sb.AppendLine("found conflict constraint: " + conflictCheck); infeasible = true; } else if (targetObjectType.Contains("(") || targetObjectType.Contains(")") || targetObjectType.Contains("=")) { sb.AppendLine("found wrong object type: " + targetObjectType); infeasible = true; } else { var branchInfo = new BranchInfo(sp.Document, sp.Line, sp.Column, sp.EndColumn, location.Method.FullName, location.Offset); if (database.TargetObjectTypes.ContainsKey(targetObjectType)) { database.TargetObjectTypes[targetObjectType].Add(branchInfo); } else { database.TargetObjectTypes.Add(targetObjectType, new HashSet <BranchInfo> { branchInfo }); } } } IEnumerable <Field> fields = GetInvolvedFields(e.TermManager, flippedCondition); IEnumerable <TypeEx> types = GetInvolvedObjectTypes(e.TermManager, flippedCondition); var simpleLog = database.SimpleLog; var errorLog = database.ErrorLog; Field target; TypeEx declaringType; simpleLog.AppendLine("============Log Problem================"); simpleLog.AppendLine("result: " + e.Result); simpleLog.AppendLine(sb.ToString()); TypeEx targetType; if ( !ObjectCreationProblemAnalyzer.GetTargetExplorableField(fields.Reverse(), out target, out declaringType, Host, out targetType)) { simpleLog.AppendLine("can not analyze"); } simpleLog.AppendLine("failed term: \n" + stringWriter.ToString()); fields.ToList().ForEach(x => simpleLog.AppendLine("involved field: " + x)); foreach (var f in fields) { simpleLog.AppendLine("involved field: "); simpleLog.AppendLine("f.FullName:" + f.FullName); simpleLog.AppendLine("f.Definition.FullName" + f.Definition.FullName); simpleLog.AppendLine("f.InstanceFieldMapType:" + f.InstanceFieldMapType.FullName); TypeEx type; // type. f.TryGetDeclaringType(out type); simpleLog.AppendLine("f.TryGetDeclaringType: " + type.FullName); } types.ToList().ForEach(x => simpleLog.AppendLine("found object type: " + x)); types.ToList().ForEach(x => Host.GetService <ProblemTrackDatabase>().FoundTypes.Add(x.FullName)); fields.ToList().ForEach(x => Host.GetService <ProblemTrackDatabase>().FoundTypes.Add(x.Type.FullName)); fields.ToList().ForEach(x => { TypeEx decType; if (x.TryGetDeclaringType(out decType)) { Host.GetService <ProblemTrackDatabase>().FoundTypes.Add( decType.FullName); } ; }); simpleLog.AppendLine("target field: " + target); if (fields != null && fields.Count() > 0) { CreateCandidateObjectCreationProblem(database, location, sp, stringWriter, simpleLog, fields, target, errorLog, targetType, targetObjectType); } if (fields == null || fields.Count() == 0 && targetObjectType != null && !infeasible) { CreateCandidateObjectCreationProblemForSingleType(stringWriter, sp, location, targetObjectType, database, simpleLog, errorLog); } simpleLog.AppendLine("============end Log Problem================"); simpleLog.AppendLine(); } catch (Exception ex) { Host.GetService <ProblemTrackDatabase>().ErrorLog.AppendLine("Error in problem observer: " + ex); } // DumpInfoToDebugFile(sb.ToString(),outputFile); }
void Log_ExplorationBoundaryHandler(ExplorationBoundaryEventArgs e) { var database = Host.GetService <ProblemTrackDatabase>(); ProblemEventArgs successfulFlippedPathCondition = database.CurrentSuccessfulFlippedPathCondition; StringBuilder log = database.SimpleLog; SequencePoint sp; if (successfulFlippedPathCondition == null) { return; } CodeLocation location = successfulFlippedPathCondition.FlippedLocation; Host.Services.SymbolManager.TryGetSequencePoint(location.Method, location.Offset, out sp); StringBuilder sb = new StringBuilder("/////////////////////////////////// \n"); log.AppendLine("exception: " + e.Kind + " e.TargetName: " + e.TargetName + " message: " + e); sb.AppendLine("flipped location: " + sp.Document + " line: " + sp.Line); var branchInfo = new BranchInfo("", 0, 0, 0, "", 0); try { branchInfo = new BranchInfo(sp.Document, sp.Line, sp.Column, sp.EndColumn, location.Method.FullName, location.Offset); } catch (Exception) { } var flippedCondition = successfulFlippedPathCondition.Suffix; var stringWriter = new StringWriter(); var bodyWriter = this.Host.Services.LanguageManager.DefaultLanguage.CreateBodyWriter(stringWriter, VisibilityContext. Private); var emitter = new TermEmitter(successfulFlippedPathCondition.TermManager, new NameCreator()); if (emitter.TryEvaluate(Indexable.One(flippedCondition), 1000, bodyWriter)) { bodyWriter.Return(SystemTypes.Bool); } stringWriter.WriteLine(); stringWriter.WriteLine("Feasible prefixes:"); if (successfulFlippedPathCondition.FeasiblePrefix != null && successfulFlippedPathCondition.FeasiblePrefix.Length > 0) { var bodyWriter2 = this.Host.Services.LanguageManager.DefaultLanguage.CreateBodyWriter(stringWriter, VisibilityContext. Private); foreach (Term prefix in successfulFlippedPathCondition.FeasiblePrefix) { if (emitter.TryEvaluate(Indexable.One(prefix), 1000, bodyWriter2)) { bodyWriter2.Return(SystemTypes.Bool); } } } else { stringWriter.WriteLine("No feasible prefixes."); } this.Host.Log.Dump("My Category", "condition", stringWriter.ToString()); sb.AppendLine(stringWriter.ToString()); sb.AppendLine("///////////////////////////////////"); var issue = new BoundaryProblem(e.Kind.ToString(), e.TargetName.ToString(), e.Message, branchInfo, stringWriter.ToString()); Host.GetService <ProblemTrackDatabase>().BoundaryIssues.Add(issue); log.AppendLine(sb.ToString()); // e. }