Пример #1
0
        public void MultipleConditionalEdgesToSameNodeIsAllowed()
        {
            var graph = new ControlFlowGraph <int>(IntArchitecture.Instance);

            var n1 = new ControlFlowNode <int>(1);
            var n2 = new ControlFlowNode <int>(2);
            var n3 = new ControlFlowNode <int>(3);

            graph.Nodes.AddRange(new[]
            {
                n1, n2, n3
            });

            n1.ConnectWith(n2);
            n1.ConnectWith(n3, ControlFlowEdgeType.Conditional);
            n1.ConnectWith(n3, ControlFlowEdgeType.Conditional);
            n1.ConnectWith(n3, ControlFlowEdgeType.Conditional);
            n1.ConnectWith(n3, ControlFlowEdgeType.Conditional);

            Assert.Same(n2, n1.UnconditionalNeighbour);
            Assert.Equal(new[]
            {
                n3, n3, n3, n3
            }, n1.ConditionalEdges.Select(e => e.Target));
        }
Пример #2
0
        public static ControlFlowGraph <DummyInstruction> CreateIfElse()
        {
            var graph = new ControlFlowGraph <DummyInstruction>(DummyArchitecture.Instance);

            var n1 = new ControlFlowNode <DummyInstruction>(0,
                                                            DummyInstruction.Op(0, 0, 1),
                                                            DummyInstruction.JmpCond(1, 3));

            var n2 = new ControlFlowNode <DummyInstruction>(2,
                                                            DummyInstruction.Jmp(2, 4));

            var n3 = new ControlFlowNode <DummyInstruction>(3,
                                                            DummyInstruction.Op(3, 0, 0));

            var n4 = new ControlFlowNode <DummyInstruction>(4,
                                                            DummyInstruction.Ret(4));

            graph.Nodes.AddRange(new[] { n1, n2, n3, n4 });
            graph.Entrypoint = n1;

            n1.ConnectWith(n2);
            n1.ConnectWith(n3, ControlFlowEdgeType.Conditional);
            n2.ConnectWith(n4);
            n3.ConnectWith(n4);

            return(graph);
        }
Пример #3
0
        public static ControlFlowGraph <DummyInstruction> CreateSwitch()
        {
            var graph = new ControlFlowGraph <DummyInstruction>(DummyArchitecture.Instance);

            var n1 = new ControlFlowNode <DummyInstruction>(0,
                                                            DummyInstruction.Switch(0, 2, 3, 4, 5));
            var n2 = new ControlFlowNode <DummyInstruction>(1,
                                                            DummyInstruction.Jmp(1, 5));
            var n3 = new ControlFlowNode <DummyInstruction>(2,
                                                            DummyInstruction.Jmp(2, 5));
            var n4 = new ControlFlowNode <DummyInstruction>(3,
                                                            DummyInstruction.Jmp(3, 5));
            var n5 = new ControlFlowNode <DummyInstruction>(4,
                                                            DummyInstruction.Jmp(4, 5));
            var n6 = new ControlFlowNode <DummyInstruction>(5,
                                                            DummyInstruction.Ret(5));

            graph.Nodes.AddRange(new[] { n1, n2, n3, n4, n5, n6 });
            graph.Entrypoint = n1;

            n1.ConnectWith(n2);
            n1.ConnectWith(n3, ControlFlowEdgeType.Conditional);
            n1.ConnectWith(n4, ControlFlowEdgeType.Conditional);
            n1.ConnectWith(n5, ControlFlowEdgeType.Conditional);
            n2.ConnectWith(n6);
            n3.ConnectWith(n6);
            n4.ConnectWith(n6);
            n5.ConnectWith(n6);

            return(graph);
        }
        public void ConditionalBranchTargetChangeToAnotherNodeHeaderShouldUpdateConditionalEdge()
        {
            var cfg = BuildControlFlowGraph(new[]
            {
                DummyInstruction.Push(0, 1),
                DummyInstruction.JmpCond(1, 20),

                DummyInstruction.Jmp(2, 20),

                DummyInstruction.Ret(20)
            }, 0);

            // Add a new node to use as a branch target.
            var newTarget = new ControlFlowNode <DummyInstruction>(100, DummyInstruction.Jmp(100, 20));

            cfg.Nodes.Add(newTarget);
            newTarget.ConnectWith(cfg.Nodes[20]);

            // Update branch target.
            cfg.Nodes[0].Contents.Footer.Operands[0] = 100L;

            Assert.True(cfg.UpdateFlowControl(DummyArchitecture.Instance.SuccessorResolver));
            Assert.Single(cfg.Nodes[0].ConditionalEdges);
            Assert.True(cfg.Nodes[0].ConditionalEdges.Contains(cfg.Nodes[100]));
        }
Пример #5
0
        public void DisconnectNodeShouldRemoveEdge(ControlFlowEdgeType edgeType, bool removeSourceNode)
        {
            var graph = new ControlFlowGraph <int>(IntArchitecture.Instance);

            var n1 = new ControlFlowNode <int>(0, 0);
            var n2 = new ControlFlowNode <int>(1, 1);

            graph.Nodes.AddRange(new[]
            {
                n1,
                n2
            });

            n1.ConnectWith(n2, edgeType);

            if (removeSourceNode)
            {
                n1.Disconnect();
            }
            else
            {
                n2.Disconnect();
            }

            Assert.Empty(n1.GetOutgoingEdges());
            Assert.Empty(n2.GetIncomingEdges());
        }
Пример #6
0
        public void SplittingNodeShouldPreserveParentRegion()
        {
            var graph = new ControlFlowGraph <int>(IntArchitecture.Instance);
            var n1    = new ControlFlowNode <int>(0, 0, 2, 3, 4);
            var n2    = new ControlFlowNode <int>(5, 5, 6, 7, 8);

            var region = new ExceptionHandlerRegion <int>();

            graph.Regions.Add(region);

            var handler = new HandlerRegion <int>();

            region.Handlers.Add(handler);

            graph.Nodes.AddRange(new[] { n1, n2 });
            region.ProtectedRegion.Nodes.Add(n1);
            handler.Contents.Nodes.Add(n2);

            n1.ConnectWith(n2, ControlFlowEdgeType.Abnormal);
            graph.Entrypoint = n1;

            var(first, second) = n1.SplitAtIndex(2);
            Assert.Equal(first.ParentRegion, second.ParentRegion);
            Assert.Equal(first.ParentRegion, region.ProtectedRegion);
            Assert.Equal(2, region.ProtectedRegion.Nodes.Count);
        }
Пример #7
0
        public static ControlFlowGraph <DummyInstruction> CreatePath()
        {
            var graph = new ControlFlowGraph <DummyInstruction>(DummyArchitecture.Instance);

            var n1 = new ControlFlowNode <DummyInstruction>(0,
                                                            DummyInstruction.Op(0, 0, 0));

            var n2 = new ControlFlowNode <DummyInstruction>(1,
                                                            DummyInstruction.Op(1, 0, 0));

            var n3 = new ControlFlowNode <DummyInstruction>(2,
                                                            DummyInstruction.Op(2, 0, 0));

            var n4 = new ControlFlowNode <DummyInstruction>(3,
                                                            DummyInstruction.Ret(3));

            graph.Nodes.AddRange(new[] { n1, n2, n3, n4 });
            graph.Entrypoint = n1;

            n1.ConnectWith(n2);
            n2.ConnectWith(n3);
            n3.ConnectWith(n4);

            return(graph);
        }
Пример #8
0
        public void AddMultipleEdges()
        {
            var graph = new ControlFlowGraph <int>(IntArchitecture.Instance);

            var n1 = new ControlFlowNode <int>(0, 0);
            var n2 = new ControlFlowNode <int>(1, 1);
            var n3 = new ControlFlowNode <int>(2, 2);

            graph.Nodes.AddRange(new[] { n1, n2, n3 });

            var edges = new HashSet <ControlFlowEdge <int> >
            {
                n1.ConnectWith(n2, ControlFlowEdgeType.Conditional),
                n1.ConnectWith(n3),
                n2.ConnectWith(n3),
            };

            Assert.Equal(edges, new HashSet <ControlFlowEdge <int> >(graph.GetEdges()));
        }
Пример #9
0
        public void AddEdge()
        {
            var graph = new ControlFlowGraph <int>(IntArchitecture.Instance);

            var n1 = new ControlFlowNode <int>(0, 0);
            var n2 = new ControlFlowNode <int>(1, 1);

            graph.Nodes.AddRange(new[] { n1, n2 });
            n1.ConnectWith(n2);

            Assert.Single(graph.GetEdges());
        }
Пример #10
0
        public static ControlFlowGraph <DummyInstruction> CreateIfElseNested()
        {
            var graph = new ControlFlowGraph <DummyInstruction>(DummyArchitecture.Instance);

            var n1 = new ControlFlowNode <DummyInstruction>(0,
                                                            DummyInstruction.Op(0, 0, 1),
                                                            DummyInstruction.JmpCond(1, 7));

            var n2 = new ControlFlowNode <DummyInstruction>(2,
                                                            DummyInstruction.Op(2, 0, 1),
                                                            DummyInstruction.JmpCond(3, 4));

            var n3 = new ControlFlowNode <DummyInstruction>(4,
                                                            DummyInstruction.Jmp(4, 6));

            var n4 = new ControlFlowNode <DummyInstruction>(5,
                                                            DummyInstruction.Jmp(5, 6));

            var n5 = new ControlFlowNode <DummyInstruction>(6,
                                                            DummyInstruction.Jmp(6, 7));

            var n6 = new ControlFlowNode <DummyInstruction>(7,
                                                            DummyInstruction.Ret(7));

            graph.Nodes.AddRange(new[] { n1, n2, n3, n4, n5, n6 });
            graph.Entrypoint = n1;

            n1.ConnectWith(n2);
            n1.ConnectWith(n6, ControlFlowEdgeType.Conditional);
            n2.ConnectWith(n3);
            n2.ConnectWith(n4, ControlFlowEdgeType.Conditional);
            n3.ConnectWith(n5);
            n4.ConnectWith(n5);
            n5.ConnectWith(n6);

            return(graph);
        }
Пример #11
0
        public void RemoveNodeShouldRemoveOutgoingEdges()
        {
            var graph = new ControlFlowGraph <int>(IntArchitecture.Instance);

            var n1 = new ControlFlowNode <int>(1);
            var n2 = new ControlFlowNode <int>(2);

            graph.Nodes.Add(n1);
            graph.Nodes.Add(n2);

            n1.ConnectWith(n2);

            Assert.Single(n1.GetOutgoingEdges());
            Assert.Single(n2.GetIncomingEdges());

            graph.Nodes.Remove(n1);

            Assert.Empty(n1.GetOutgoingEdges());
            Assert.Empty(n2.GetIncomingEdges());
        }