Search in sources :

Example 1 with BinaryOperator

use of de.fraunhofer.aisec.cpg.graph.statements.expressions.BinaryOperator in project cpg by Fraunhofer-AISEC.

the class EOGTest method testIf.

/**
 * Tests EOG building in the presence of if/else statements.
 *
 * @param relPath
 * @param refNodeString - Exact string of reference nodes, do not change/insert nodes in the test
 *     file.
 * @throws TranslationException
 */
void testIf(String relPath, String refNodeString) throws Exception {
    List<Node> nodes = translateToNodes(relPath);
    // All BinaryOperators (including If conditions) have only one successor
    List<BinaryOperator> binops = Util.filterCast(nodes, BinaryOperator.class);
    for (BinaryOperator binop : binops) {
        SubgraphWalker.Border binopEOG = SubgraphWalker.getEOGPathEdges(binop);
        assertEquals(1, binopEOG.getExits().size());
    }
    List<IfStatement> ifs = Util.filterCast(nodes, IfStatement.class);
    assertEquals(2, ifs.size());
    ifs.forEach(ifnode -> Assertions.assertNotNull(ifnode.getThenStatement()));
    assertTrue(ifs.stream().anyMatch(node -> node.getElseStatement() == null) && ifs.stream().anyMatch(node -> node.getElseStatement() != null));
    IfStatement ifSimple = ifs.get(0);
    IfStatement ifBranched = ifs.get(1);
    List<Node> prints = nodes.stream().filter(node -> node.getCode().equals(refNodeString)).collect(Collectors.toList());
    SubgraphWalker.Border ifEOG = SubgraphWalker.getEOGPathEdges(ifSimple);
    SubgraphWalker.Border conditionEOG = SubgraphWalker.getEOGPathEdges(ifSimple.getCondition());
    SubgraphWalker.Border thenEOG = SubgraphWalker.getEOGPathEdges(ifSimple.getThenStatement());
    // IfStmt has 2 outgoing EOG edges (for true and false branch)
    assertEquals(2, ifEOG.getExits().size());
    // Assert: Only single entry and exit NODE per block
    assertTrue(conditionEOG.getEntries().size() == 1 && conditionEOG.getExits().size() == 1);
    assertTrue(thenEOG.getEntries().size() == 1 && thenEOG.getExits().size() == 1);
    // Assert: Condition of simple if is preceded by print
    assertTrue(Util.eogConnect(ENTRIES, ifSimple.getCondition(), prints.get(0)));
    // Assert: All EOGs going into the then branch (=the 2nd print stmt) come from the IfStatement
    assertTrue(Util.eogConnect(ENTRIES, ifSimple.getThenStatement(), NODE, ifSimple));
    // Assert: The EOGs going into the second print come either from the then branch or the
    // IfStatement
    assertTrue(Util.eogConnect(NODE, EXITS, ifSimple, prints.get(1)));
    assertTrue(Util.eogConnect(NODE, EXITS, ifSimple, ifSimple.getThenStatement()));
    assertTrue(Util.eogConnect(NODE, EXITS, ifSimple.getThenStatement(), prints.get(1)));
    conditionEOG = SubgraphWalker.getEOGPathEdges(ifBranched.getCondition());
    thenEOG = SubgraphWalker.getEOGPathEdges(ifBranched.getThenStatement());
    SubgraphWalker.Border elseEOG = SubgraphWalker.getEOGPathEdges(ifBranched.getElseStatement());
    // Assert: Only single entry and exit NODE per block
    assertTrue(conditionEOG.getEntries().size() == 1 && conditionEOG.getExits().size() == 1);
    assertTrue(thenEOG.getEntries().size() == 1 && thenEOG.getExits().size() == 1);
    assertTrue(elseEOG.getEntries().size() == 1 && elseEOG.getExits().size() == 1);
    // Assert: Branched if is preceded by the second print
    assertTrue(Util.eogConnect(ENTRIES, ifBranched, prints.get(1)));
    // IfStatement has exactly 2 outgoing EOGS: true (then) and false (else) branch
    assertTrue(Util.eogConnect(NODE, EXITS, ifBranched, ifBranched.getThenStatement()));
    assertTrue(Util.eogConnect(NODE, EXITS, ifBranched, ifBranched.getElseStatement()));
    SubgraphWalker.Border ifBranchedEOG = SubgraphWalker.getEOGPathEdges(ifBranched);
    assertEquals(2, ifBranchedEOG.getExits().size());
    // Assert: EOG going into then branch comes from the condition branch
    assertTrue(Util.eogConnect(ENTRIES, ifBranched.getThenStatement(), NODE, ifBranched));
    // Assert: EOG going into else branch comes from the condition branch
    assertTrue(Util.eogConnect(ENTRIES, ifBranched.getElseStatement(), NODE, ifBranched));
    // Assert: EOG edges going into the third print either come from the then or else branch
    assertTrue(Util.eogConnect(SUBTREE, EXITS, ifBranched, prints.get(2)));
    // Assert: EOG edges going into the branch root node either come from the then or else branch
    assertTrue(Util.eogConnect(NODE, ENTRIES, ifBranched, ifBranched.getCondition()));
}
Also used : EvaluationOrderGraphPass(de.fraunhofer.aisec.cpg.passes.EvaluationOrderGraphPass) Properties(de.fraunhofer.aisec.cpg.graph.edge.Properties) SUBTREE(de.fraunhofer.aisec.cpg.helpers.Util.Connect.SUBTREE) de.fraunhofer.aisec.cpg.graph(de.fraunhofer.aisec.cpg.graph) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) NodeComparator(de.fraunhofer.aisec.cpg.helpers.NodeComparator) ConstructorDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.ConstructorDeclaration) Util(de.fraunhofer.aisec.cpg.helpers.Util) SubgraphWalker(de.fraunhofer.aisec.cpg.helpers.SubgraphWalker) FunctionDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration) PropertyEdge(de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge) Path(java.nio.file.Path) EXITS(de.fraunhofer.aisec.cpg.helpers.Util.Edge.EXITS) PhysicalLocation.locationLink(de.fraunhofer.aisec.cpg.sarif.PhysicalLocation.locationLink) NODE(de.fraunhofer.aisec.cpg.helpers.Util.Connect.NODE) ALL(de.fraunhofer.aisec.cpg.helpers.Util.Quantifier.ALL) DeclaredReferenceExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.DeclaredReferenceExpression) Strategy(de.fraunhofer.aisec.cpg.processing.strategy.Strategy) TestUtils(de.fraunhofer.aisec.cpg.TestUtils) TransactionException(org.neo4j.ogm.exception.TransactionException) ANY(de.fraunhofer.aisec.cpg.helpers.Util.Quantifier.ANY) Collectors(java.util.stream.Collectors) File(java.io.File) BinaryOperator(de.fraunhofer.aisec.cpg.graph.statements.expressions.BinaryOperator) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) ENTRIES(de.fraunhofer.aisec.cpg.helpers.Util.Edge.ENTRIES) TranslationException(de.fraunhofer.aisec.cpg.frontends.TranslationException) List(java.util.List) IVisitor(de.fraunhofer.aisec.cpg.processing.IVisitor) Stream(java.util.stream.Stream) de.fraunhofer.aisec.cpg.graph.statements(de.fraunhofer.aisec.cpg.graph.statements) Assertions(org.junit.jupiter.api.Assertions) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) CallExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression) SubgraphWalker(de.fraunhofer.aisec.cpg.helpers.SubgraphWalker) BinaryOperator(de.fraunhofer.aisec.cpg.graph.statements.expressions.BinaryOperator)

Example 2 with BinaryOperator

use of de.fraunhofer.aisec.cpg.graph.statements.expressions.BinaryOperator in project cpg by Fraunhofer-AISEC.

the class CXXSymbolConfigurationTest method testWithoutSymbols.

@Test
void testWithoutSymbols() throws TranslationException {
    // parse without symbols
    TranslationUnitDeclaration tu = new CXXLanguageFrontend(TranslationConfiguration.builder().defaultPasses().build(), new ScopeManager()).parse(new File("src/test/resources/symbols.cpp"));
    Set<FunctionDeclaration> main = tu.getDeclarationsByName("main", FunctionDeclaration.class);
    assertFalse(main.isEmpty());
    FunctionDeclaration funcDecl = main.iterator().next();
    BinaryOperator binaryOperator = funcDecl.getBodyStatementAs(0, BinaryOperator.class);
    assertNotNull(binaryOperator);
    // without additional symbols, the first line will look like a reference (to something we do not
    // know)
    DeclaredReferenceExpression dre = binaryOperator.getRhsAs(DeclaredReferenceExpression.class);
    assertNotNull(dre);
    assertEquals("HELLO_WORLD", dre.getName());
    binaryOperator = funcDecl.getBodyStatementAs(1, BinaryOperator.class);
    assertNotNull(binaryOperator);
    // without additional symbols, the second line will look like a function call (to something we
    // do not know)
    CallExpression call = binaryOperator.getRhsAs(CallExpression.class);
    assertNotNull(call);
    assertEquals("INCREASE", call.getName());
}
Also used : FunctionDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration) ScopeManager(de.fraunhofer.aisec.cpg.passes.scopes.ScopeManager) DeclaredReferenceExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.DeclaredReferenceExpression) BinaryOperator(de.fraunhofer.aisec.cpg.graph.statements.expressions.BinaryOperator) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) File(java.io.File) CallExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Example 3 with BinaryOperator

use of de.fraunhofer.aisec.cpg.graph.statements.expressions.BinaryOperator in project cpg by Fraunhofer-AISEC.

the class CXXSymbolConfigurationTest method testWithSymbols.

@Test
void testWithSymbols() throws TranslationException {
    // let's try with symbol definitions
    TranslationUnitDeclaration tu = new CXXLanguageFrontend(TranslationConfiguration.builder().symbols(Map.of("HELLO_WORLD", "\"Hello World\"", "INCREASE(X)", "X+1")).defaultPasses().build(), new ScopeManager()).parse(new File("src/test/resources/symbols.cpp"));
    Set<FunctionDeclaration> main = tu.getDeclarationsByName("main", FunctionDeclaration.class);
    assertFalse(main.isEmpty());
    FunctionDeclaration funcDecl = main.iterator().next();
    BinaryOperator binaryOperator = funcDecl.getBodyStatementAs(0, BinaryOperator.class);
    assertNotNull(binaryOperator);
    // should be a literal now
    Literal<?> literal = binaryOperator.getRhsAs(Literal.class);
    assertEquals("Hello World", literal.getValue());
    binaryOperator = funcDecl.getBodyStatementAs(1, BinaryOperator.class);
    assertNotNull(binaryOperator);
    // should be expanded to another binary operation 1+1
    BinaryOperator add = binaryOperator.getRhsAs(BinaryOperator.class);
    assertNotNull(add);
    assertEquals("+", add.getOperatorCode());
    Literal<?> literal2 = add.getLhsAs(Literal.class);
    assertEquals(2, literal2.getValue());
    Literal<?> literal1 = add.getRhsAs(Literal.class);
    assertEquals(1, literal1.getValue());
}
Also used : FunctionDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration) ScopeManager(de.fraunhofer.aisec.cpg.passes.scopes.ScopeManager) BinaryOperator(de.fraunhofer.aisec.cpg.graph.statements.expressions.BinaryOperator) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) File(java.io.File) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Example 4 with BinaryOperator

use of de.fraunhofer.aisec.cpg.graph.statements.expressions.BinaryOperator in project cpg by Fraunhofer-AISEC.

the class EOGTest method testConditionShortCircuit.

@Test
void testConditionShortCircuit() throws Exception {
    List<Node> nodes = translateToNodes("src/test/resources/cfg/ShortCircuit.java");
    List<BinaryOperator> binaryOperators = Util.filterCast(nodes, BinaryOperator.class).stream().filter(bo -> bo.getOperatorCode().equals("&&") || bo.getOperatorCode().equals("||")).collect(Collectors.toList());
    for (BinaryOperator bo : binaryOperators) {
        assertTrue(Util.eogConnect(ALL, SUBTREE, EXITS, bo.getLhs(), SUBTREE, bo.getRhs()));
        assertTrue(Util.eogConnect(ALL, SUBTREE, EXITS, bo.getLhs(), NODE, bo));
        assertTrue(bo.getLhs().getNextEOG().size() == 2);
    }
}
Also used : EvaluationOrderGraphPass(de.fraunhofer.aisec.cpg.passes.EvaluationOrderGraphPass) Properties(de.fraunhofer.aisec.cpg.graph.edge.Properties) SUBTREE(de.fraunhofer.aisec.cpg.helpers.Util.Connect.SUBTREE) de.fraunhofer.aisec.cpg.graph(de.fraunhofer.aisec.cpg.graph) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) NodeComparator(de.fraunhofer.aisec.cpg.helpers.NodeComparator) ConstructorDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.ConstructorDeclaration) Util(de.fraunhofer.aisec.cpg.helpers.Util) SubgraphWalker(de.fraunhofer.aisec.cpg.helpers.SubgraphWalker) FunctionDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration) PropertyEdge(de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge) Path(java.nio.file.Path) EXITS(de.fraunhofer.aisec.cpg.helpers.Util.Edge.EXITS) PhysicalLocation.locationLink(de.fraunhofer.aisec.cpg.sarif.PhysicalLocation.locationLink) NODE(de.fraunhofer.aisec.cpg.helpers.Util.Connect.NODE) ALL(de.fraunhofer.aisec.cpg.helpers.Util.Quantifier.ALL) DeclaredReferenceExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.DeclaredReferenceExpression) Strategy(de.fraunhofer.aisec.cpg.processing.strategy.Strategy) TestUtils(de.fraunhofer.aisec.cpg.TestUtils) TransactionException(org.neo4j.ogm.exception.TransactionException) ANY(de.fraunhofer.aisec.cpg.helpers.Util.Quantifier.ANY) Collectors(java.util.stream.Collectors) File(java.io.File) BinaryOperator(de.fraunhofer.aisec.cpg.graph.statements.expressions.BinaryOperator) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) ENTRIES(de.fraunhofer.aisec.cpg.helpers.Util.Edge.ENTRIES) TranslationException(de.fraunhofer.aisec.cpg.frontends.TranslationException) List(java.util.List) IVisitor(de.fraunhofer.aisec.cpg.processing.IVisitor) Stream(java.util.stream.Stream) de.fraunhofer.aisec.cpg.graph.statements(de.fraunhofer.aisec.cpg.graph.statements) Assertions(org.junit.jupiter.api.Assertions) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) CallExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression) BinaryOperator(de.fraunhofer.aisec.cpg.graph.statements.expressions.BinaryOperator) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Aggregations

BaseTest (de.fraunhofer.aisec.cpg.BaseTest)4 FunctionDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration)4 TranslationUnitDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration)4 BinaryOperator (de.fraunhofer.aisec.cpg.graph.statements.expressions.BinaryOperator)4 File (java.io.File)4 Test (org.junit.jupiter.api.Test)4 CallExpression (de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression)3 DeclaredReferenceExpression (de.fraunhofer.aisec.cpg.graph.statements.expressions.DeclaredReferenceExpression)3 TestUtils (de.fraunhofer.aisec.cpg.TestUtils)2 TranslationException (de.fraunhofer.aisec.cpg.frontends.TranslationException)2 de.fraunhofer.aisec.cpg.graph (de.fraunhofer.aisec.cpg.graph)2 ConstructorDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.ConstructorDeclaration)2 Properties (de.fraunhofer.aisec.cpg.graph.edge.Properties)2 PropertyEdge (de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge)2 de.fraunhofer.aisec.cpg.graph.statements (de.fraunhofer.aisec.cpg.graph.statements)2 NodeComparator (de.fraunhofer.aisec.cpg.helpers.NodeComparator)2 SubgraphWalker (de.fraunhofer.aisec.cpg.helpers.SubgraphWalker)2 Util (de.fraunhofer.aisec.cpg.helpers.Util)2 NODE (de.fraunhofer.aisec.cpg.helpers.Util.Connect.NODE)2 SUBTREE (de.fraunhofer.aisec.cpg.helpers.Util.Connect.SUBTREE)2