Search in sources :

Example 1 with HasType

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

the class UnaryOperator method typeChanged.

@Override
public void typeChanged(HasType src, HasType root, Type oldType) {
    if (!TypeManager.isTypeSystemActive()) {
        return;
    }
    Type previous = this.type;
    if (src == input) {
        Type newType = src.getPropagationType();
        if (operatorCode.equals("*")) {
            newType = newType.dereference();
        } else if (operatorCode.equals("&")) {
            newType = newType.reference(PointerType.PointerOrigin.POINTER);
        }
        setType(newType, root);
    } else {
        // Our input didn't change, so we don't need to (de)reference the type
        setType(src.getPropagationType(), root);
        // Pass the type on to the input in an inversely (de)referenced way
        Type newType = src.getPropagationType();
        if (operatorCode.equals("*")) {
            newType = src.getPropagationType().reference(PointerType.PointerOrigin.POINTER);
        } else if (operatorCode.equals("&")) {
            newType = src.getPropagationType().dereference();
        }
        // not null
        if (input != null) {
            input.setType(newType, this);
        }
    }
    if (!previous.equals(this.type)) {
        this.type.setTypeOrigin(Type.Origin.DATAFLOW);
    }
}
Also used : Type(de.fraunhofer.aisec.cpg.graph.types.Type) HasType(de.fraunhofer.aisec.cpg.graph.HasType) PointerType(de.fraunhofer.aisec.cpg.graph.types.PointerType)

Example 2 with HasType

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

the class CallExpression method typeChanged.

@Override
public void typeChanged(HasType src, HasType root, Type oldType) {
    if (!TypeManager.isTypeSystemActive()) {
        return;
    }
    if (src == base) {
        setFqn(src.getType().getRoot().getTypeName() + "." + this.getName());
    } else {
        Type previous = this.type;
        List<Type> types = invokes.stream().map(PropertyEdge::getEnd).map(FunctionDeclaration::getType).filter(Objects::nonNull).collect(Collectors.toList());
        Type alternative = !types.isEmpty() ? types.get(0) : null;
        Type commonType = TypeManager.getInstance().getCommonType(types).orElse(alternative);
        Set<Type> subTypes = new HashSet<>(getPossibleSubTypes());
        subTypes.remove(oldType);
        subTypes.addAll(types);
        setType(commonType, root);
        setPossibleSubTypes(subTypes, root);
        if (!previous.equals(this.type)) {
            this.type.setTypeOrigin(Type.Origin.DATAFLOW);
        }
    }
}
Also used : FunctionDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration) Type(de.fraunhofer.aisec.cpg.graph.types.Type) HasType(de.fraunhofer.aisec.cpg.graph.HasType)

Example 3 with HasType

use of de.fraunhofer.aisec.cpg.graph.HasType 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 4 with HasType

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

the class VariableDeclaration 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;
    if (src == initializer && initializer instanceof InitializerListExpression) {
        // can be ignored once we have a type
        if (isArray) {
            newType = src.getType();
        } else if (!TypeManager.getInstance().isUnknown(this.type)) {
            return;
        } else {
            newType = src.getType().dereference();
        }
    } else {
        newType = src.getPropagationType();
    }
    setType(newType, root);
    if (!previous.equals(this.type)) {
        this.type.setTypeOrigin(Type.Origin.DATAFLOW);
    }
}
Also used : Type(de.fraunhofer.aisec.cpg.graph.types.Type) HasType(de.fraunhofer.aisec.cpg.graph.HasType) InitializerListExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.InitializerListExpression)

Example 5 with HasType

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

the class CallExpression method typeChanged.

@Override
public void typeChanged(HasType src, Collection<HasType> root, Type oldType) {
    if (!TypeManager.isTypeSystemActive()) {
        return;
    }
    if (src == base) {
        setFqn(src.getType().getRoot().getTypeName() + "." + this.getName());
    } else {
        Type previous = this.type;
        List<Type> types = invokes.stream().map(PropertyEdge::getEnd).map(FunctionDeclaration::getType).filter(Objects::nonNull).collect(Collectors.toList());
        Type alternative = !types.isEmpty() ? types.get(0) : null;
        Type commonType = TypeManager.getInstance().getCommonType(types).orElse(alternative);
        Set<Type> subTypes = new HashSet<>(getPossibleSubTypes());
        subTypes.remove(oldType);
        subTypes.addAll(types);
        setType(commonType, root);
        setPossibleSubTypes(subTypes, root);
        if (!previous.equals(this.type)) {
            this.type.setTypeOrigin(Type.Origin.DATAFLOW);
        }
    }
}
Also used : FunctionDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration) Type(de.fraunhofer.aisec.cpg.graph.types.Type) HasType(de.fraunhofer.aisec.cpg.graph.HasType)

Aggregations

HasType (de.fraunhofer.aisec.cpg.graph.HasType)26 Type (de.fraunhofer.aisec.cpg.graph.types.Type)23 UnknownType (de.fraunhofer.aisec.cpg.graph.types.UnknownType)7 Node (de.fraunhofer.aisec.cpg.graph.Node)5 TypeManager (de.fraunhofer.aisec.cpg.graph.TypeManager)5 java.util (java.util)5 Collectors (java.util.stream.Collectors)5 PropertyEdge (de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge)4 InitializerListExpression (de.fraunhofer.aisec.cpg.graph.statements.expressions.InitializerListExpression)4 ToStringBuilder (org.apache.commons.lang3.builder.ToStringBuilder)4 TypeListener (de.fraunhofer.aisec.cpg.graph.HasType.TypeListener)3 SubGraph (de.fraunhofer.aisec.cpg.graph.SubGraph)3 FunctionPointerType (de.fraunhofer.aisec.cpg.graph.types.FunctionPointerType)3 ReferenceType (de.fraunhofer.aisec.cpg.graph.types.ReferenceType)3 Predicate (java.util.function.Predicate)3 Relationship (org.neo4j.ogm.annotation.Relationship)3 FunctionDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration)2 Properties (de.fraunhofer.aisec.cpg.graph.edge.Properties)2 PropertyEdge.unwrap (de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge.unwrap)2 Statement (de.fraunhofer.aisec.cpg.graph.statements.Statement)2