public void Consolidate(ThreadFlow threadFlow)
        {
            if (threadFlow.Locations != null)
            {
                TotalThreadFlowLocations += threadFlow.Locations.Count;

                for (int i = 0; i < threadFlow.Locations.Count; ++i)
                {
                    ThreadFlowLocation tfl = threadFlow.Locations[i];
                    Trim(tfl.Location);

                    if (tfl != null && tfl.Index == -1)
                    {
                        if (!_uniqueThreadFlowLocations.TryGetValue(tfl, out int tflIndex))
                        {
                            tflIndex = _run.ThreadFlowLocations.Count;
                            _run.ThreadFlowLocations.Add(tfl);
                            _uniqueThreadFlowLocations[tfl] = tflIndex;
                        }

                        threadFlow.Locations[i] = new ThreadFlowLocation()
                        {
                            Index = tflIndex
                        };
                    }
                }
            }
        }
Beispiel #2
0
 public Threadline(ThreadFlow flow, int order)
 {
     ThreadID = flow.ThreadID;
     Order    = order;
     Name     = flow.Name;
     IsAlive  = flow.IsAlive;
 }
Beispiel #3
0
        internal static List <AnalysisStepNode> Convert(CodeFlow codeFlow, Run run, int resultId, int runIndex)
        {
            var root = new AnalysisStepNode(resultId: resultId, runIndex: runIndex)
            {
                Children = new List <AnalysisStepNode>(),
            };

            ThreadFlow threadFlow = codeFlow.ThreadFlows?[0];

            if (threadFlow != null)
            {
                int lastNestingLevel         = 0;
                AnalysisStepNode lastParent  = root;
                AnalysisStepNode lastNewNode = null;

                foreach (ThreadFlowLocation location in threadFlow.Locations)
                {
                    ArtifactLocation artifactLocation = location.Location?.PhysicalLocation?.ArtifactLocation;

                    if (artifactLocation != null)
                    {
                        Uri uri = location.Location?.PhysicalLocation?.ArtifactLocation?.Uri;

                        if (uri == null && artifactLocation.Index > -1)
                        {
                            artifactLocation.Uri = run.Artifacts[artifactLocation.Index].Location.Uri;
                        }
                    }

                    var newNode = new AnalysisStepNode(resultId: resultId, runIndex: runIndex)
                    {
                        Location = location,
                        Children = new List <AnalysisStepNode>(),
                    };

                    if (location.NestingLevel > lastNestingLevel)
                    {
                        // The previous node was a call, so this new node's parent is that node
                        lastParent = lastNewNode;
                    }
                    else if (location.NestingLevel < lastNestingLevel)
                    {
                        // The previous node was a return, so this new node's parent is the previous node's grandparent
                        lastParent = lastNewNode.Parent.Parent;
                    }

                    newNode.Parent = lastParent;
                    lastParent.Children.Add(newNode);
                    lastNewNode      = newNode;
                    lastNestingLevel = location.NestingLevel;
                }

                root.Children.ForEach(n => n.Parent = null);
            }

            return(root.Children);
        }
Beispiel #4
0
        private void Visit(ThreadFlow threadFlow, string threadFlowPointer)
        {
            Analyze(threadFlow, threadFlowPointer);

            if (threadFlow.Locations != null)
            {
                ThreadFlowLocation[] threadFlowLocations = threadFlow.Locations.ToArray();
                string threadFlowLocationsPointer        = threadFlowPointer.AtProperty(SarifPropertyName.Locations);

                for (int i = 0; i < threadFlowLocations.Length; ++i)
                {
                    Visit(threadFlowLocations[i], threadFlowLocationsPointer.AtIndex(i));
                }
            }
        }
        internal CodeFlow CreateCodeFlow(CodeFlowVersionOne v1CodeFlow)
        {
            CodeFlow codeFlow = null;

            if (v1CodeFlow != null)
            {
                codeFlow = new CodeFlow
                {
                    Message    = CreateMessage(v1CodeFlow.Message),
                    Properties = v1CodeFlow.Properties
                };

                if (v1CodeFlow.Locations != null && v1CodeFlow.Locations.Count > 0)
                {
                    _threadFlowLocationNestingLevel = 0;
                    int executionOrder       = 0;
                    var threadFlowDictionary = new Dictionary <int, ThreadFlow>();

                    foreach (AnnotatedCodeLocationVersionOne v1CodeLocation in v1CodeFlow.Locations)
                    {
                        ThreadFlow threadFlow;
                        int        threadId = v1CodeLocation.ThreadId;

                        if (!threadFlowDictionary.TryGetValue(threadId, out threadFlow))
                        {
                            threadFlow = new ThreadFlow
                            {
                                Id        = threadId.ToString(CultureInfo.InvariantCulture),
                                Locations = new List <ThreadFlowLocation>()
                            };
                            threadFlowDictionary.Add(threadId, threadFlow);
                        }

                        ThreadFlowLocation tfl = CreateThreadFlowLocation(v1CodeLocation);
                        tfl.Step           = threadFlow.Locations.Count + 1;
                        tfl.ExecutionOrder = ++executionOrder;
                        threadFlow.Locations.Add(tfl);
                    }

                    codeFlow.ThreadFlows = threadFlowDictionary.Values.ToList();
                }
            }

            return(codeFlow);
        }
        private void VerifyCodeFlowFlatList(IList <AnalysisStepNode> analysisStepNodes, CodeFlow codeFlow, Run run)
        {
            ThreadFlow threadFlow = codeFlow?.ThreadFlows?[0];

            if (threadFlow != null)
            {
                analysisStepNodes.Should().NotBeEmpty();
                analysisStepNodes.Count.Should().Be(threadFlow.Locations.Count);

                int minLevel = threadFlow.Locations.Min(l => l.NestingLevel);

                for (int i = 0; i < analysisStepNodes.Count; i++)
                {
                    AnalysisStepNode   node     = analysisStepNodes[i];
                    ThreadFlowLocation location = threadFlow.Locations[i];

                    node.Parent.Should().BeNull(); // flat list item doesn't have parent

                    ArtifactLocation artifactLocation = location.Location?.PhysicalLocation?.ArtifactLocation;
                    if (artifactLocation != null)
                    {
                        if (artifactLocation.Uri == null)
                        {
                            if (artifactLocation.Index > -1 && run?.Artifacts != null)
                            {
                                node.Location.Location.PhysicalLocation.ArtifactLocation.Uri
                                .Should()
                                .Be(run.Artifacts[artifactLocation.Index].Location.Uri);
                            }
                        }
                        else
                        {
                            node.Location.Location.PhysicalLocation.ArtifactLocation.Uri.Should().Be(artifactLocation.Uri);
                        }
                    }

                    node.NestingLevel.Should().Be(location.NestingLevel - minLevel);
                }
            }
            else
            {
                analysisStepNodes.Should().BeEmpty();
            }
        }
Beispiel #7
0
        internal static List <CallTreeNode> Convert(CodeFlow codeFlow)
        {
            var root = new CallTreeNode {
                Children = new List <CallTreeNode>()
            };
            ThreadFlow threadFlow = codeFlow.ThreadFlows?[0];

            if (threadFlow != null)
            {
                int          lastNestingLevel = 0;
                CallTreeNode lastParent       = root;
                CallTreeNode lastNewNode      = null;

                foreach (CodeFlowLocation location in threadFlow.Locations)
                {
                    var newNode = new CallTreeNode
                    {
                        Location = location,
                        Children = new List <CallTreeNode>()
                    };

                    if (location.NestingLevel > lastNestingLevel)
                    {
                        // The previous node was a call, so this new node's parent is that node
                        lastParent = lastNewNode;
                    }
                    else if (location.NestingLevel < lastNestingLevel)
                    {
                        // The previous node was a return, so this new node's parent is the previous node's grandparent
                        lastParent = lastNewNode.Parent.Parent;
                    }

                    newNode.Parent = lastParent;
                    lastParent.Children.Add(newNode);
                    lastNewNode      = newNode;
                    lastNestingLevel = location.NestingLevel;
                }

                root.Children.ForEach(n => n.Parent = null);
            }

            return(root.Children);
        }
        private void Visit(ThreadFlow threadFlow, string threadFlowPointer)
        {
            Analyze(threadFlow, threadFlowPointer);

            if (threadFlow.Message != null)
            {
                Visit(threadFlow.Message, threadFlowPointer.AtProperty(SarifPropertyName.Message));
            }

            if (threadFlow.Locations != null)
            {
                string threadFlowLocationsPointer = threadFlowPointer.AtProperty(SarifPropertyName.Locations);

                for (int i = 0; i < threadFlow.Locations.Count; ++i)
                {
                    Visit(threadFlow.Locations[i], threadFlowLocationsPointer.AtIndex(i));
                }
            }
        }
        protected override void Analyze(ThreadFlow threadFlow, string threadFlowPointer)
        {
            var    pointer         = new JsonPointer(threadFlowPointer);
            JToken threadFlowToken = pointer.Evaluate(Context.InputLogToken);

            JProperty locationsProperty = threadFlowToken.Children <JProperty>()
                                          .FirstOrDefault(prop => prop.Name.Equals(SarifPropertyName.Locations, StringComparison.Ordinal));

            if (locationsProperty != null)
            {
                JArray threadFlowLocationsArray   = locationsProperty.Value as JArray;
                string threadFlowLocationsPointer = threadFlowPointer.AtProperty(SarifPropertyName.Locations);

                ReportMissingStepProperty(
                    threadFlowLocationsArray,
                    threadFlowLocationsPointer);

                ReportInvalidStepValues(
                    threadFlow.Locations.ToArray(),
                    threadFlowLocationsArray,
                    threadFlowLocationsPointer);
            }
        }
Beispiel #10
0
        internal static List <AnalysisStepNode> ToFlatList(CodeFlow codeFlow, Run run, int resultId, int runIndex)
        {
            var results = new List <AnalysisStepNode>();

            ThreadFlow threadFlow = codeFlow.ThreadFlows?[0];

            if (threadFlow != null)
            {
                // nestingLevel is an integer value which is greater than or equals to 0
                // according to schema http://json.schemastore.org/sarif-2.1.0-rtm.5.
                // min nesting level can be used to offset nesting level starts from value greater than 0.e
                int minNestingLevel = threadFlow.Locations.Min(l => l.NestingLevel);

                foreach (ThreadFlowLocation location in threadFlow.Locations)
                {
                    ArtifactLocation artifactLocation = location.Location?.PhysicalLocation?.ArtifactLocation;

                    if (artifactLocation != null &&
                        artifactLocation.Uri == null &&
                        artifactLocation.Index > -1)
                    {
                        artifactLocation.Uri = run.Artifacts[artifactLocation.Index].Location.Uri;
                    }

                    var newNode = new AnalysisStepNode(resultId: resultId, runIndex: runIndex)
                    {
                        Location     = location,
                        Children     = new List <AnalysisStepNode>(),
                        NestingLevel = location.NestingLevel - minNestingLevel,
                    };

                    results.Add(newNode);
                }
            }

            return(results);
        }
Beispiel #11
0
        private bool AddToTimeline(ThreadFlow flow, StackItem item)
        {
            // do stuff with item
            Threadline timeline;

            if (!Threadlines.TryGetValue(flow.ThreadID, out timeline))
            {
                timeline = new Threadline(flow, ThreadOrder++);
                Threadlines[flow.ThreadID] = timeline;
            }

            timeline.IsAlive = flow.IsAlive; // update

            var node = NodeModels[item.NodeID];

            if (node.Show &&
                (CenterMap.Contains(node.ID) ||
                 (ShowOutside && !node.XNode.External) ||
                 (ShowExternal && node.XNode.External)))
            {
                timeline.Sequence.Add(item);

                if (item.Depth > timeline.Deepest)
                {
                    timeline.Deepest = item.Depth;
                }

                timeline.DepthSet.Add(item.Depth);

                return(true);
            }
            else
            {
                return(false);
            }
        }
Beispiel #12
0
 protected override void Analyze(ThreadFlow threadFlow, string threadFlowPointer)
 {
     CheckArraySize(
         threadFlow.Locations?.Count,
         threadFlowPointer.AtProperty(SarifPropertyName.Locations));
 }
Beispiel #13
0
 protected virtual void Analyze(ThreadFlow threadFlow, string threadFlowPointer)
 {
 }