Search in sources :

Example 11 with HasType

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

the class ValueDeclaration method setType.

@Override
public void setType(Type type, Collection<HasType> root) {
    if (!TypeManager.isTypeSystemActive()) {
        TypeManager.getInstance().cacheType(this, type);
        return;
    }
    if (root == null) {
        root = new ArrayList<>();
    }
    if (type == null || root.contains(this) || TypeManager.getInstance().isUnknown(type) || (this.type instanceof FunctionPointerType && !(type instanceof FunctionPointerType))) {
        return;
    }
    Type oldType = this.type;
    type = type.duplicate();
    type.setQualifier(this.type.getQualifier().merge(type.getQualifier()));
    Set<Type> subTypes = new HashSet<>();
    for (Type t : getPossibleSubTypes()) {
        if (!t.isSimilar(type)) {
            subTypes.add(t);
        }
    }
    subTypes.add(type);
    this.type = TypeManager.getInstance().registerType(TypeManager.getInstance().getCommonType(subTypes).orElse(type));
    Set<Type> newSubtypes = new HashSet<>();
    for (var s : subTypes) {
        if (TypeManager.getInstance().isSupertypeOf(this.type, s)) {
            newSubtypes.add(TypeManager.getInstance().registerType(s));
        }
    }
    setPossibleSubTypes(newSubtypes);
    if (Objects.equals(oldType, type)) {
        // Nothing changed, so we do not have to notify the listeners.
        return;
    }
    // Add current node to the set of "triggers" to detect potential loops.
    root.add(this);
    // Notify all listeners about the changed type
    for (var l : typeListeners) {
        if (!l.equals(this)) {
            l.typeChanged(this, root, oldType);
        }
    }
}
Also used : FunctionPointerType(de.fraunhofer.aisec.cpg.graph.types.FunctionPointerType) UnknownType(de.fraunhofer.aisec.cpg.graph.types.UnknownType) Type(de.fraunhofer.aisec.cpg.graph.types.Type) HasType(de.fraunhofer.aisec.cpg.graph.HasType) ReferenceType(de.fraunhofer.aisec.cpg.graph.types.ReferenceType) FunctionPointerType(de.fraunhofer.aisec.cpg.graph.types.FunctionPointerType)

Example 12 with HasType

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

the class ValueDeclaration method resetTypes.

@Override
public void resetTypes(Type type) {
    Set<Type> oldSubTypes = new HashSet<>(getPossibleSubTypes());
    Type oldType = this.type;
    this.type = type;
    setPossibleSubTypes(new HashSet<>(List.of(type)));
    List<HasType> root = new ArrayList<>(List.of(this));
    if (!Objects.equals(oldType, type)) {
        this.typeListeners.stream().filter(l -> !l.equals(this)).forEach(l -> l.typeChanged(this, root, oldType));
    }
    if (oldSubTypes.size() != 1 || !oldSubTypes.contains(type))
        this.typeListeners.stream().filter(l -> !l.equals(this)).forEach(l -> l.possibleSubTypesChanged(this, root, oldSubTypes));
}
Also used : java.util(java.util) UnknownType(de.fraunhofer.aisec.cpg.graph.types.UnknownType) TypeManager(de.fraunhofer.aisec.cpg.graph.TypeManager) Type(de.fraunhofer.aisec.cpg.graph.types.Type) Predicate(java.util.function.Predicate) Transient(org.neo4j.ogm.annotation.Transient) Collectors(java.util.stream.Collectors) HasType(de.fraunhofer.aisec.cpg.graph.HasType) ReferenceType(de.fraunhofer.aisec.cpg.graph.types.ReferenceType) ToStringBuilder(org.apache.commons.lang3.builder.ToStringBuilder) Node(de.fraunhofer.aisec.cpg.graph.Node) FunctionPointerType(de.fraunhofer.aisec.cpg.graph.types.FunctionPointerType) NotNull(org.jetbrains.annotations.NotNull) UnknownType(de.fraunhofer.aisec.cpg.graph.types.UnknownType) Type(de.fraunhofer.aisec.cpg.graph.types.Type) HasType(de.fraunhofer.aisec.cpg.graph.HasType) ReferenceType(de.fraunhofer.aisec.cpg.graph.types.ReferenceType) FunctionPointerType(de.fraunhofer.aisec.cpg.graph.types.FunctionPointerType) HasType(de.fraunhofer.aisec.cpg.graph.HasType)

Example 13 with HasType

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

the class ArrayCreationExpression method typeChanged.

@Override
public void typeChanged(HasType src, Collection<HasType> root, Type oldType) {
    if (!TypeManager.isTypeSystemActive()) {
        return;
    }
    Type previous = this.type;
    setType(src.getPropagationType(), root);
    if (!previous.equals(this.type)) {
        this.type.setTypeOrigin(Type.Origin.DATAFLOW);
    }
}
Also used : HasType(de.fraunhofer.aisec.cpg.graph.HasType) Type(de.fraunhofer.aisec.cpg.graph.types.Type)

Example 14 with HasType

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

the class UnaryOperator method getsDataFromInput.

private boolean getsDataFromInput(TypeListener curr, TypeListener target) {
    List<TypeListener> worklist = new ArrayList<>();
    worklist.add(curr);
    while (!worklist.isEmpty()) {
        TypeListener tl = worklist.remove(0);
        if (!checked.contains(tl)) {
            checked.add(tl);
            if (tl == target) {
                return true;
            }
            if (curr instanceof HasType) {
                worklist.addAll(((HasType) curr).getTypeListeners());
            }
        }
    }
    return false;
}
Also used : TypeListener(de.fraunhofer.aisec.cpg.graph.HasType.TypeListener) HasType(de.fraunhofer.aisec.cpg.graph.HasType)

Example 15 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, 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

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