Example #1
0
        private void CollectBlocks(ControlFlowGraph graph, ICollection <ILInstruction> instructions, ICollection <long> blockHeaders)
        {
            Node         currentNode  = null;
            ILBasicBlock currentBlock = null;

            foreach (var instruction in instructions.OrderBy(x => x.Offset))
            {
                // If current instruction is a basic block header, we start a new block.
                if (currentNode == null || blockHeaders.Contains(instruction.Offset))
                {
                    currentNode  = graph.Nodes.Add(graph.GetNodeName(instruction.Offset));
                    currentBlock = new ILBasicBlock();
                    currentNode.UserData[ILBasicBlock.BasicBlockProperty] = currentBlock;
                }

                // Add instruction to current block.
                currentBlock.Instructions.Add(instruction);

                // If next offset is also a header, we also create a new block.
                // This check is necessary as blocks might not appear in sequence after each other.
                if (blockHeaders.Contains(instruction.Offset + instruction.Size))
                {
                    currentNode = null;
                }
            }
        }
Example #2
0
        private Node GetNode(ControlFlowGraph graph, long offset)
        {
            string name = graph.GetNodeName(offset);

            if (!graph.Nodes.TryGetNode(name, out var node))
            {
                Logger.Error(Tag, $"Reference to an unexplored basic block IL_{offset:X4} found. Inserting dummy node.");
                node = graph.Nodes.Add(name);
            }

            return(node);
        }