Search in sources :

Example 1 with PropertyEdge

use of de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge in project cpg by Fraunhofer-AISEC.

the class EOGTest method testBranchProperty.

/**
 * Tests EOG branch edge property in if/else if/else construct
 *
 * @throws Exception
 */
@Test
void testBranchProperty() throws Exception {
    Path topLevel = Path.of("src", "test", "resources", "eog");
    List<TranslationUnitDeclaration> result = TestUtils.analyze(List.of(topLevel.resolve("EOG.java").toFile()), topLevel, true);
    // Test If-Block
    IfStatement firstIf = TestUtils.findByPredicate(TestUtils.subnodesOfType(result, IfStatement.class), l -> l.getLocation().getRegion().getStartLine() == 6).get(0);
    DeclaredReferenceExpression a = TestUtils.findByPredicate(TestUtils.subnodesOfType(result, DeclaredReferenceExpression.class), l -> l.getLocation().getRegion().getStartLine() == 8 && l.getName().equals("a")).get(0);
    DeclaredReferenceExpression b = TestUtils.findByPredicate(TestUtils.subnodesOfType(result, DeclaredReferenceExpression.class), l -> l.getLocation().getRegion().getStartLine() == 7 && l.getName().equals("b")).get(0);
    List<PropertyEdge<Node>> nextEOG = firstIf.getNextEOGEdges();
    assertEquals(2, nextEOG.size());
    for (PropertyEdge<Node> edge : nextEOG) {
        assertEquals(firstIf, edge.getStart());
        if (edge.getEnd().equals(b)) {
            assertEquals(true, edge.getProperty(Properties.BRANCH));
            assertEquals(0, edge.getProperty(Properties.INDEX));
        } else {
            assertEquals(a, edge.getEnd());
            assertEquals(false, edge.getProperty(Properties.BRANCH));
            assertEquals(1, edge.getProperty(Properties.INDEX));
        }
    }
    IfStatement elseIf = TestUtils.findByPredicate(TestUtils.subnodesOfType(result, IfStatement.class), l -> l.getLocation().getRegion().getStartLine() == 8).get(0);
    assertEquals(elseIf, firstIf.getElseStatement());
    DeclaredReferenceExpression b2 = TestUtils.findByPredicate(TestUtils.subnodesOfType(result, DeclaredReferenceExpression.class), l -> l.getLocation().getRegion().getStartLine() == 9 && l.getName().equals("b")).get(0);
    DeclaredReferenceExpression x = TestUtils.findByPredicate(TestUtils.subnodesOfType(result, DeclaredReferenceExpression.class), l -> l.getLocation().getRegion().getStartLine() == 11 && l.getName().equals("x")).get(0);
    nextEOG = elseIf.getNextEOGEdges();
    assertEquals(2, nextEOG.size());
    for (PropertyEdge<Node> edge : nextEOG) {
        assertEquals(elseIf, edge.getStart());
        if (edge.getEnd().equals(b2)) {
            assertEquals(true, edge.getProperty(Properties.BRANCH));
            assertEquals(0, edge.getProperty(Properties.INDEX));
        } else {
            assertEquals(x, edge.getEnd());
            assertEquals(false, edge.getProperty(Properties.BRANCH));
            assertEquals(1, edge.getProperty(Properties.INDEX));
        }
    }
}
Also used : Path(java.nio.file.Path) 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) DeclaredReferenceExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.DeclaredReferenceExpression) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) PropertyEdge(de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Example 2 with PropertyEdge

use of de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge in project cpg by Fraunhofer-AISEC.

the class InitializerListExpression method typeChanged.

@Override
public void typeChanged(HasType src, HasType root, Type oldType) {
    if (!TypeManager.isTypeSystemActive()) {
        return;
    }
    if (!TypeManager.getInstance().isUnknown(this.type) && src.getPropagationType().equals(oldType)) {
        return;
    }
    Type previous = this.type;
    Type newType;
    Set<Type> subTypes;
    if (this.getInitializers().contains(src)) {
        Set<Type> types = this.initializers.parallelStream().map(PropertyEdge::getEnd).map(Expression::getType).filter(Objects::nonNull).map(t -> TypeManager.getInstance().registerType(t.reference(PointerOrigin.ARRAY))).collect(Collectors.toSet());
        Type alternative = !types.isEmpty() ? types.iterator().next() : UnknownType.getUnknownType();
        newType = TypeManager.getInstance().getCommonType(types).orElse(alternative);
        subTypes = new HashSet<>(getPossibleSubTypes());
        subTypes.remove(oldType);
        subTypes.addAll(types);
    } else {
        newType = src.getType();
        subTypes = new HashSet<>(getPossibleSubTypes());
        subTypes.remove(oldType);
        subTypes.add(newType);
    }
    setType(newType, root);
    setPossibleSubTypes(subTypes, root);
    if (!previous.equals(this.type)) {
        this.type.setTypeOrigin(Type.Origin.DATAFLOW);
    }
}
Also used : java.util(java.util) Properties(de.fraunhofer.aisec.cpg.graph.edge.Properties) UnknownType(de.fraunhofer.aisec.cpg.graph.types.UnknownType) TypeListener(de.fraunhofer.aisec.cpg.graph.HasType.TypeListener) TypeManager(de.fraunhofer.aisec.cpg.graph.TypeManager) Type(de.fraunhofer.aisec.cpg.graph.types.Type) Relationship(org.neo4j.ogm.annotation.Relationship) PropertyEdge.unwrap(de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge.unwrap) Collectors(java.util.stream.Collectors) PointerOrigin(de.fraunhofer.aisec.cpg.graph.types.PointerType.PointerOrigin) HasType(de.fraunhofer.aisec.cpg.graph.HasType) SubGraph(de.fraunhofer.aisec.cpg.graph.SubGraph) ToStringBuilder(org.apache.commons.lang3.builder.ToStringBuilder) Node(de.fraunhofer.aisec.cpg.graph.Node) PropertyEdge(de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge) UnknownType(de.fraunhofer.aisec.cpg.graph.types.UnknownType) Type(de.fraunhofer.aisec.cpg.graph.types.Type) HasType(de.fraunhofer.aisec.cpg.graph.HasType) PropertyEdge(de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge)

Example 3 with PropertyEdge

use of de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge in project cpg by Fraunhofer-AISEC.

the class ExpressionList method addExpression.

public void addExpression(Statement expression) {
    if (!this.expressions.isEmpty()) {
        Statement lastExpression = this.expressions.get(this.expressions.size() - 1).getEnd();
        if (lastExpression instanceof HasType)
            ((HasType) lastExpression).unregisterTypeListener(this);
        this.removePrevDFG(lastExpression);
    }
    PropertyEdge<Statement> propertyEdge = new PropertyEdge<>(this, expression);
    propertyEdge.addProperty(Properties.INDEX, this.expressions.size());
    this.expressions.add(propertyEdge);
    this.addPrevDFG(expression);
    if (expression instanceof HasType) {
        ((HasType) expression).registerTypeListener(this);
    }
}
Also used : Statement(de.fraunhofer.aisec.cpg.graph.statements.Statement) HasType(de.fraunhofer.aisec.cpg.graph.HasType) PropertyEdge(de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge)

Example 4 with PropertyEdge

use of de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge in project cpg by Fraunhofer-AISEC.

the class FunctionDeclaration method hasSignature.

public boolean hasSignature(List<Type> targetSignature) {
    List<ParamVariableDeclaration> signature = parameters.stream().map(PropertyEdge::getEnd).sorted(Comparator.comparingInt(ParamVariableDeclaration::getArgumentIndex)).collect(Collectors.toList());
    if (targetSignature.size() < signature.size()) {
        return false;
    } else {
        // signature is a collection of positional arguments, so the order must be preserved
        for (int i = 0; i < signature.size(); i++) {
            ParamVariableDeclaration declared = signature.get(i);
            if (declared.isVariadic() && targetSignature.size() >= signature.size()) {
                // different vararg types, in C++ we can't, as vararg types are not defined here anyways)
                return true;
            }
            Type provided = targetSignature.get(i);
            if (!TypeManager.getInstance().isSupertypeOf(declared.getType(), provided)) {
                return false;
            }
        }
        // vararg has been encountered
        return targetSignature.size() == signature.size();
    }
}
Also used : UnknownType(de.fraunhofer.aisec.cpg.graph.types.UnknownType) Type(de.fraunhofer.aisec.cpg.graph.types.Type) PropertyEdge(de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge)

Example 5 with PropertyEdge

use of de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge in project cpg by Fraunhofer-AISEC.

the class InitializerListExpression method typeChanged.

@Override
public void typeChanged(HasType src, Collection<HasType> root, Type oldType) {
    if (!TypeManager.isTypeSystemActive()) {
        return;
    }
    if (!TypeManager.getInstance().isUnknown(this.type) && src.getPropagationType().equals(oldType)) {
        return;
    }
    Type previous = this.type;
    Type newType;
    Set<Type> subTypes;
    if (this.getInitializers().contains(src)) {
        Set<Type> types = this.initializers.parallelStream().map(PropertyEdge::getEnd).map(Expression::getType).filter(Objects::nonNull).map(t -> TypeManager.getInstance().registerType(t.reference(PointerOrigin.ARRAY))).collect(Collectors.toSet());
        Type alternative = !types.isEmpty() ? types.iterator().next() : UnknownType.getUnknownType();
        newType = TypeManager.getInstance().getCommonType(types).orElse(alternative);
        subTypes = new HashSet<>(getPossibleSubTypes());
        subTypes.remove(oldType);
        subTypes.addAll(types);
    } else {
        newType = src.getType();
        subTypes = new HashSet<>(getPossibleSubTypes());
        subTypes.remove(oldType);
        subTypes.add(newType);
    }
    setType(newType, root);
    setPossibleSubTypes(subTypes, root);
    if (!previous.equals(this.type)) {
        this.type.setTypeOrigin(Type.Origin.DATAFLOW);
    }
}
Also used : java.util(java.util) Properties(de.fraunhofer.aisec.cpg.graph.edge.Properties) UnknownType(de.fraunhofer.aisec.cpg.graph.types.UnknownType) TypeListener(de.fraunhofer.aisec.cpg.graph.HasType.TypeListener) TypeManager(de.fraunhofer.aisec.cpg.graph.TypeManager) Type(de.fraunhofer.aisec.cpg.graph.types.Type) Relationship(org.neo4j.ogm.annotation.Relationship) PropertyEdge.unwrap(de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge.unwrap) Collectors(java.util.stream.Collectors) PointerOrigin(de.fraunhofer.aisec.cpg.graph.types.PointerType.PointerOrigin) HasType(de.fraunhofer.aisec.cpg.graph.HasType) SubGraph(de.fraunhofer.aisec.cpg.graph.SubGraph) ToStringBuilder(org.apache.commons.lang3.builder.ToStringBuilder) Node(de.fraunhofer.aisec.cpg.graph.Node) PropertyEdge(de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge) UnknownType(de.fraunhofer.aisec.cpg.graph.types.UnknownType) Type(de.fraunhofer.aisec.cpg.graph.types.Type) HasType(de.fraunhofer.aisec.cpg.graph.HasType) PropertyEdge(de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge)

Aggregations

PropertyEdge (de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge)5 HasType (de.fraunhofer.aisec.cpg.graph.HasType)3 Properties (de.fraunhofer.aisec.cpg.graph.edge.Properties)3 Type (de.fraunhofer.aisec.cpg.graph.types.Type)3 UnknownType (de.fraunhofer.aisec.cpg.graph.types.UnknownType)3 Collectors (java.util.stream.Collectors)3 TypeListener (de.fraunhofer.aisec.cpg.graph.HasType.TypeListener)2 Node (de.fraunhofer.aisec.cpg.graph.Node)2 SubGraph (de.fraunhofer.aisec.cpg.graph.SubGraph)2 TypeManager (de.fraunhofer.aisec.cpg.graph.TypeManager)2 PropertyEdge.unwrap (de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge.unwrap)2 PointerOrigin (de.fraunhofer.aisec.cpg.graph.types.PointerType.PointerOrigin)2 java.util (java.util)2 BaseTest (de.fraunhofer.aisec.cpg.BaseTest)1 TestUtils (de.fraunhofer.aisec.cpg.TestUtils)1 TranslationException (de.fraunhofer.aisec.cpg.frontends.TranslationException)1 de.fraunhofer.aisec.cpg.graph (de.fraunhofer.aisec.cpg.graph)1 ConstructorDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.ConstructorDeclaration)1 FunctionDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration)1 TranslationUnitDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration)1