public BasicBlock CreateBasicBlock(Block block) { var ucfgBlock = CreateBlockWithId(blockIdProvider.Get(block)); ucfgBlock.Instructions.AddRange(block.Instructions.SelectMany(instructionFactory.CreateFrom)); if (block is JumpBlock jumpBlock && jumpBlock.JumpNode is ReturnStatementSyntax returnStatement) { ucfgBlock.Ret = new Return { Location = returnStatement.GetUcfgLocation(), ReturnedExpression = returnStatement.Expression != null ? expressionService.GetExpression(returnStatement.Expression).Expression : expressionService.CreateConstant().Expression }; } if (block is ExitBlock exit) { ucfgBlock.Ret = new Return { Location = null, ReturnedExpression = expressionService.CreateConstant().Expression }; } if (ucfgBlock.TerminatorCase == BasicBlock.TerminatorOneofCase.None) { // No return was created from JumpBlock or ExitBlock, wire up the successor blocks ucfgBlock.Jump = CreateJump(block.SuccessorBlocks.Select(blockIdProvider.Get).ToArray()); } return(ucfgBlock); }
private void WriteNode(Block block, SyntaxNode terminator = null) { var header = block.GetType().Name.SplitCamelCaseToWords().First().ToUpperInvariant(); if (terminator != null) { // shorten the text var terminatorType = terminator.Kind().ToString().Replace("Syntax", string.Empty); header += ":" + terminatorType; } writer.WriteNode(blockId.Get(block), header, block.Instructions.Select(i => i.ToString()).ToArray()); }
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(blockBuilder.CreateBasicBlock)); ucfg.Parameters.AddRange(methodSymbol.GetParameters().Select(p => p.Name)); if (syntaxNode is BaseMethodDeclarationSyntax methodDeclaration && TaintAnalysisEntryPointDetector.IsEntryPoint(methodSymbol)) { var entryPointBlock = blockBuilder.CreateEntryPointBlock(methodDeclaration, methodSymbol, blockIdProvider.Get(cfg.EntryBlock)); ucfg.BasicBlocks.Add(entryPointBlock); ucfg.Entries.Add(entryPointBlock.Id); } else { ucfg.Entries.Add(blockIdProvider.Get(cfg.EntryBlock)); } return(ucfg); }
public void Get_Returns_Same_Id_For_Same_Block() { var block = new TemporaryBlock(); blockId.Get(block).Should().Be("0"); blockId.Get(block).Should().Be("0"); blockId.Get(block).Should().Be("0"); }