Example #1
0
        public UCFG Create(SyntaxNode syntaxNode, IMethodSymbol methodSymbol, IControlFlowGraph cfg)
        {
            try
            {
                var ucfg = new UCFG
                {
                    Location = syntaxNode.GetUcfgLocation(),
                    MethodId = methodSymbol.ToUcfgMethodId()
                };

                ucfg.BasicBlocks.AddRange(cfg.Blocks.Select(this.blockBuilder.CreateBasicBlock));
                ucfg.Parameters.AddRange(methodSymbol.GetParameters().Select(p => p.Name));

                if (syntaxNode is BaseMethodDeclarationSyntax methodDeclaration &&
                    TaintAnalysisEntryPointDetector.IsEntryPoint(methodSymbol))
                {
                    var entryPointBlock = this.blockBuilder.CreateEntryPointBlock(methodDeclaration, methodSymbol, this.blockIdProvider.Get(cfg.EntryBlock));
                    ucfg.BasicBlocks.Add(entryPointBlock);
                    ucfg.Entries.Add(entryPointBlock.Id);
                }
                else
                {
                    ucfg.Entries.Add(this.blockIdProvider.Get(cfg.EntryBlock));
                }

                return(ucfg);
            }
Example #2
0
 protected /*for testing*/ virtual void WriteProtobuf(UCFG ucfg, string fileName)
 {
     using (var stream = File.Create(fileName))
     {
         ucfg.WriteTo(stream);
     }
 }
 private static string GetProtobufMethodId(string protobufPath)
 {
     File.Exists(protobufPath).Should().BeTrue($"File {protobufPath} must exist.");
     using (var stream = File.OpenRead(protobufPath))
     {
         var ucfg = new UCFG();
         ucfg.MergeFrom(stream);
         return(ucfg.MethodId);
     }
 }
Example #4
0
        public static string Serialize(UCFG ucfg)
        {
            var stringBuilder = new StringBuilder();

            using (var writer = new StringWriter(stringBuilder))
            {
                Serialize(ucfg, writer);
            }
            return(stringBuilder.ToString());
        }
Example #5
0
        internal /*for testing*/ static bool IsValid(UCFG ucfg)
        {
            var existingBlockIds = new HashSet <string>(ucfg.BasicBlocks.Select(b => b.Id));

            return(ucfg.BasicBlocks.All(HasTerminator) &&
                   ucfg.BasicBlocks.All(JumpsToExistingBlock) &&
                   ucfg.Entries.All(existingBlockIds.Contains));

            bool HasTerminator(BasicBlock block) =>
            block.Jump != null || block.Ret != null;

            bool JumpsToExistingBlock(BasicBlock block) =>
            block.Jump == null || block.Jump.Destinations.All(existingBlockIds.Contains);
        }
        public void Ucfg_Block_Without_Terminator_IsInvalid()
        {
            var ucfg = new UCFG
            {
                Entries     = { "0" },
                BasicBlocks =
                {
                    new BasicBlock {
                        Id = "0"
                    },                           // no Jump or Ret
                },
            };

            ReviewSqlQueriesForSecurityVulnerabilities.IsValid(ucfg).Should().BeFalse();
        }
        public void Ucfg_Block_Without_Terminator_IsInvalid()
        {
            var ucfg = new UCFG
            {
                Entries     = { "0" },
                BasicBlocks =
                {
                    new BasicBlock {
                        Id = "0"
                    },                           // no Jump or Ret
                },
            };

            UcfgGenerator.IsValid(ucfg).Should().BeFalse();
        }
        public UCFG Build(SemanticModel semanticModel, SyntaxNode syntaxNode, IMethodSymbol methodSymbol, IControlFlowGraph cfg)
        {
            var ucfg = new UCFG
            {
                MethodId = GetMethodId(methodSymbol),
                Location = GetLocation(syntaxNode),
            };

            ucfg.BasicBlocks.AddRange(cfg.Blocks.Select(b => CreateBasicBlock(b, semanticModel)));
            ucfg.Parameters.AddRange(methodSymbol.GetParameters().Select(p => p.Name));

            if (syntaxNode is BaseMethodDeclarationSyntax methodDeclaration &&
                EntryPointRecognizer.IsEntryPoint(methodSymbol))
            {
                var entryPointBlock = CreateEntryPointBlock(semanticModel, methodDeclaration, methodSymbol, blockId.Get(cfg.EntryBlock));
                ucfg.BasicBlocks.Add(entryPointBlock);
                ucfg.Entries.Add(entryPointBlock.Id);
            }
        public void Ucfg_Block_With_Jump_IsValid()
        {
            var ucfg = new UCFG
            {
                Entries     = { "0" },
                BasicBlocks =
                {
                    new BasicBlock
                    {
                        Id   = "0",
                        Jump = new Jump{
                            Destinations ={ "0"                        }
                        },
                    },
                },
            };

            ReviewSqlQueriesForSecurityVulnerabilities.IsValid(ucfg).Should().BeTrue();
        }
        public void Ucfg_Block_With_Missing_Jump_Destinations_IsInvalid()
        {
            var ucfg = new UCFG
            {
                Entries     = { "0" },
                BasicBlocks =
                {
                    new BasicBlock
                    {
                        Id   = "0",
                        Jump = new Jump{
                            Destinations ={ "1"                        }
                        },                                          // There is no such block
                    },
                },
            };

            UcfgGenerator.IsValid(ucfg).Should().BeFalse();
        }
        public void Ucfg_Block_With_Jump_IsValid()
        {
            var ucfg = new UCFG
            {
                Entries     = { "0" },
                BasicBlocks =
                {
                    new BasicBlock
                    {
                        Id   = "0",
                        Jump = new Jump{
                            Destinations ={ "0"                        }
                        },
                    },
                },
            };

            UcfgGenerator.IsValid(ucfg).Should().BeTrue();
        }
        public void Ucfg_Block_With_Missing_Jump_Destinations_IsInvalid()
        {
            var ucfg = new UCFG
            {
                Entries     = { "0" },
                BasicBlocks =
                {
                    new BasicBlock
                    {
                        Id   = "0",
                        Jump = new Jump{
                            Destinations ={ "1"                        }
                        },                                          // There is no such block
                    },
                },
            };

            ReviewSqlQueriesForSecurityVulnerabilities.IsValid(ucfg).Should().BeFalse();
        }
Example #13
0
        private static void VerifyInstructions(string codeSnippet, UCFG ucfg)
        {
            var expectedInstructions = UcfgInstructionCollector.Collect(codeSnippet).ToList();

            var actualInstructions = ucfg.BasicBlocks
                                     .SelectMany(b => b.Instructions)
                                     .Select(UcfgTestHelper.ToTestString)
                                     .ToList();

            Console.WriteLine("Expected instructions:{0}{1}", Environment.NewLine, string.Join(Environment.NewLine, expectedInstructions));
            Console.WriteLine();
            Console.WriteLine("Actual instructions:{0}{1}", Environment.NewLine, string.Join(Environment.NewLine, actualInstructions));

            // Fluent assertion bug: just comparing the collections gives an incorrect error message if the expected
            // list is empty but the actual list isn't ("Expected collection to be equal to {empty}, but found empty collection.")
            // To avoid this, do an assertion on the number of items first.
            actualInstructions.Count.Should().Be(expectedInstructions.Count);

            actualInstructions.Should().Equal(expectedInstructions);
        }
Example #14
0
            public void Visit(string methodName, UCFG ucfg)
            {
                writer.WriteGraphStart(methodName);

                writer.WriteNode(EntryBlockId, EntryBlockId, ucfg.Parameters.ToArray());

                foreach (var entry in ucfg.Entries)
                {
                    writer.WriteEdge(EntryBlockId, entry, string.Empty);
                }

                foreach (var block in ucfg.BasicBlocks)
                {
                    Visit(block);
                }

                writer.WriteNode(ExitBlockId, ExitBlockId);

                writer.WriteGraphEnd();
            }
        public void Ucfg_Block_With_Ret_IsValid()
        {
            var ucfg = new UCFG
            {
                Entries     = { "0" },
                BasicBlocks =
                {
                    new BasicBlock
                    {
                        Id  = "0",
                        Ret = new Return
                        {
                            ReturnedExpression = new Expression{
                                Var = new Variable{
                                    Name = "a"
                                }
                            }
                        },
                    },
                },
            };

            UcfgGenerator.IsValid(ucfg).Should().BeTrue();
        }
        public void Ucfg_Block_With_Ret_IsValid()
        {
            var ucfg = new UCFG
            {
                Entries     = { "0" },
                BasicBlocks =
                {
                    new BasicBlock
                    {
                        Id  = "0",
                        Ret = new Return
                        {
                            ReturnedExpression = new Expression{
                                Var = new Variable{
                                    Name = "a"
                                }
                            }
                        },
                    },
                },
            };

            ReviewSqlQueriesForSecurityVulnerabilities.IsValid(ucfg).Should().BeTrue();
        }
Example #17
0
 public static IList <Instruction> GetEntryPointInstructions(UCFG ucfg)
 {
     return(ucfg.BasicBlocks.SelectMany(
                b => b.Instructions.Where(i => i.Assigncall?.MethodId == UcfgBuiltInMethodId.EntryPoint)).ToList());
 }
Example #18
0
 public static void Serialize(UCFG ucfg, TextWriter writer)
 {
     new UcfgWalker(new DotWriter(writer)).Visit(ucfg.MethodId, ucfg);
 }