From 3d0e6c9bc8c6ee2c55d294c54da00d8e25772057 Mon Sep 17 00:00:00 2001 From: Alexander Kuechler Date: Tue, 5 Nov 2024 14:04:48 +0100 Subject: [PATCH 1/3] CDG pass update --- .../cpg/helpers/functional/BasicLattices.kt | 2 +- .../cpg/passes/ControlDependenceGraphPass.kt | 170 +++++++++++------- 2 files changed, 110 insertions(+), 62 deletions(-) diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/helpers/functional/BasicLattices.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/helpers/functional/BasicLattices.kt index 115cbbdd17..f42e1d4b9d 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/helpers/functional/BasicLattices.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/helpers/functional/BasicLattices.kt @@ -61,7 +61,7 @@ inline fun emptyPowersetLattice() = PowersetLattice(setOf()) * Implements the [LatticeElement] for a lattice over a set of nodes. The lattice itself is * constructed by the powerset. */ -class PowersetLattice(elements: Set) : LatticeElement>(elements) { +open class PowersetLattice(elements: Set) : LatticeElement>(elements) { override fun lub(other: LatticeElement>) = PowersetLattice(this.elements.union(other.elements)) diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/ControlDependenceGraphPass.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/ControlDependenceGraphPass.kt index a8f78479ce..0e9c72c1f5 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/ControlDependenceGraphPass.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/ControlDependenceGraphPass.kt @@ -38,9 +38,15 @@ import de.fraunhofer.aisec.cpg.graph.statements.ReturnStatement import de.fraunhofer.aisec.cpg.graph.statements.expressions.ConditionalExpression import de.fraunhofer.aisec.cpg.graph.statements.expressions.ShortCircuitOperator import de.fraunhofer.aisec.cpg.helpers.* -import de.fraunhofer.aisec.cpg.helpers.LatticeElement +import de.fraunhofer.aisec.cpg.helpers.functional.LatticeElement +import de.fraunhofer.aisec.cpg.helpers.functional.MapLattice +import de.fraunhofer.aisec.cpg.helpers.functional.PowersetLattice +import de.fraunhofer.aisec.cpg.helpers.functional.PowersetLatticeT +import de.fraunhofer.aisec.cpg.helpers.functional.iterateEOGClean import de.fraunhofer.aisec.cpg.passes.configuration.DependsOn import java.util.* +import kotlin.collections.component1 +import kotlin.collections.component2 /** This pass builds the Control Dependence Graph (CDG) by iterating through the EOG. */ @DependsOn(EvaluationOrderGraphPass::class) @@ -88,19 +94,19 @@ open class ControlDependenceGraphPass(ctx: TranslationContext) : EOGStarterPass( // Maps nodes to their "cdg parent" (i.e. the dominator) and also has the information // through which path it is reached. If all outgoing paths of the node's dominator result in // the node, we use the dominator's state instead (i.e., we move the node one layer upwards) - val startState = PrevEOGState() - val identityMap = IdentityHashMap>() - identityMap[startNode] = identitySetOf(startNode) - startState.push(startNode, PrevEOGLattice(identityMap)) - val finalState = iterateEOG(startNode.nextEOGEdges, startState, ::handleEdge) ?: return + var startState = PrevEOGState(mapOf()) + val identityMap = IdentityHashMap>() + identityMap[startNode] = PowersetLattice(identitySetOf(startNode)) + startState = startState.push(startNode, PrevEOGLattice(identityMap)) + val finalState = iterateEOGClean(startNode.nextEOGEdges, startState, ::handleEdge) val branchingNodeConditionals = getBranchingNodeConditions(startNode) // Collect the information, identify merge points, etc. This is not really efficient yet :( - for ((node, dominatorPaths) in finalState) { + for ((node, dominatorPaths) in finalState.elements) { val dominatorsList = dominatorPaths.elements.entries - .map { (k, v) -> Pair(k, v.toMutableSet()) } + .map { (k, v) -> Pair(k, v.elements.toMutableSet()) } .toMutableList() val finalDominators = mutableListOf>>() val conditionKeys = @@ -117,13 +123,13 @@ open class ControlDependenceGraphPass(ctx: TranslationContext) : EOGStarterPass( // want this. Move it one layer up. for (k1 in conditionKeys) { dominatorsList.removeIf { k1 == it.first } - finalState[k1]?.elements?.forEach { (newK, newV) -> + finalState.elements[k1]?.elements?.forEach { (newK, newV) -> val entry = dominatorsList.firstOrNull { it.first == newK } entry?.let { dominatorsList.remove(entry) - val update = entry.second.addAll(newV) + val update = entry.second.addAll(newV.elements) if (update) dominatorsList.add(entry) else finalDominators.add(entry) - } ?: dominatorsList.add(Pair(newK, newV.toMutableSet())) + } ?: dominatorsList.add(Pair(newK, newV.elements.toMutableSet())) } } } @@ -136,19 +142,22 @@ open class ControlDependenceGraphPass(ctx: TranslationContext) : EOGStarterPass( // We are reachable from all the branches of a branching node. Add this parent // to the worklist or update an existing entry. Also consider already existing // entries in finalDominators list and update it (if necessary) - val newDominatorMap = finalState[k]?.elements + val newDominatorMap = finalState.elements[k]?.elements newDominatorMap?.forEach { (newK, newV) -> when { dominatorsList.any { it.first == newK } -> { // Entry exists => update it - dominatorsList.first { it.first == newK }.second.addAll(newV) + dominatorsList + .first { it.first == newK } + .second + .addAll(newV.elements) } finalDominators.any { it.first == newK } -> { // Entry in final dominators => Delete it and add it to the worklist // (but only if something changed) val entry = finalDominators.first { it.first == newK } finalDominators.remove(entry) - val update = entry.second.addAll(newV) + val update = entry.second.addAll(newV.elements) if ( update && alreadySeen.none { @@ -160,17 +169,17 @@ open class ControlDependenceGraphPass(ctx: TranslationContext) : EOGStarterPass( else finalDominators.add(entry) } alreadySeen.none { - it.first == newK && it.second.containsAll(newV) + it.first == newK && it.second.containsAll(newV.elements) } -> { // We don't have an entry yet => add a new one - val newEntry = Pair(newK, newV.toMutableSet()) + val newEntry = Pair(newK, newV.elements.toMutableSet()) dominatorsList.add(newEntry) } else -> { // Not sure what to do, there seems to be a cycle but this entry is // not in finalDominators for some reason. Add to finalDominators // now. - finalDominators.add(Pair(newK, newV.toMutableSet())) + finalDominators.add(Pair(newK, newV.elements.toMutableSet())) } } } @@ -263,9 +272,10 @@ open class ControlDependenceGraphPass(ctx: TranslationContext) : EOGStarterPass( * Returns the updated state and true because we always expect an update of the state. */ fun handleEdge( - currentEdge: Edge, - currentState: State>> -): State>> { + currentEdge: EvaluationOrder, + currentState: LatticeElement>>>>> +): LatticeElement>>>>> { + var newState = currentState as? PrevEOGState ?: return currentState // Check if we start in a branching node and if this edge leads to the conditional // branch. In this case, the next node will move "one layer downwards" in the CDG. if (currentEdge.start is BranchingNode) { // && currentEdge.isConditionalBranch()) { @@ -275,15 +285,16 @@ fun handleEdge( val prevPathLattice = PrevEOGLattice( IdentityHashMap( - currentState[currentEdge.start]?.elements?.filter { (k, _) -> - k == currentEdge.start - } + newState.elements[currentEdge.start] + ?.elements + ?.filter { (k, _) -> k == currentEdge.start } + ?.mapValues { (_, v) -> PowersetLattice(v.elements) } ) ) - val map = IdentityHashMap>() - map[currentEdge.start] = identitySetOf(currentEdge.end) - val newPath = PrevEOGLattice(map).lub(prevPathLattice) as PrevEOGLattice - currentState.push(currentEdge.end, newPath) + val map = IdentityHashMap>() + map[currentEdge.start] = PowersetLattice(identitySetOf(currentEdge.end)) + val newPath = PrevEOGLattice(map).lub(prevPathLattice) + newState = newState.push(currentEdge.end, newPath) } else { // We did not start in a branching node, so for the next node, we have the same path // (last branching + first end node) as for the start node of this edge. @@ -292,14 +303,18 @@ fun handleEdge( // have "end" as the first node in the "branch". val state = PrevEOGLattice( - currentState[currentEdge.start]?.elements - ?: IdentityHashMap( - mutableMapOf(Pair(currentEdge.start, identitySetOf(currentEdge.end))) - ) + IdentityHashMap( + newState.elements[currentEdge.start]?.elements?.mapValues { (_, v) -> + PowersetLattice(v.elements) + } + ?: mutableMapOf( + Pair(currentEdge.start, PowersetLattice(identitySetOf(currentEdge.end))) + ) + ) ) - currentState.push(currentEdge.end, state) + newState = newState.push(currentEdge.end, state) } - return currentState + return newState } /** @@ -358,40 +373,73 @@ private fun IfStatement.allBranchesFromMyThenBranchGoThrough(node: Node?): Boole * Implements the [LatticeElement] over a set of nodes and their set of "nextEOG" nodes which reach * this node. */ -class PrevEOGLattice(override val elements: IdentityHashMap>) : - LatticeElement>>(elements) { +class PrevEOGLattice(elements: Map>) : + MapLattice>(elements) { - override fun lub( - other: LatticeElement>> - ): LatticeElement>> { - val newMap = IdentityHashMap(other.elements.mapValues { (_, v) -> v.toIdentitySet() }) - for ((key, value) in this.elements) { - newMap.computeIfAbsent(key, ::identitySetOf).addAll(value) - } - return PrevEOGLattice(newMap) + override fun lub(other: LatticeElement>>>): PrevEOGLattice { + return PrevEOGLattice( + this.elements.entries.fold( + other.elements.mapValues { (_, v) -> PowersetLattice(v.elements) } + ) { current, (thisKey, thisValue) -> + val mutableMap = current.toMutableMap() + mutableMap.compute(thisKey) { k, v -> + PowersetLattice((if (v == null) thisValue else thisValue.lub(v)).elements) + } + mutableMap + } + ) } - override fun duplicate() = PrevEOGLattice(IdentityHashMap(this.elements)) - - override fun compareTo(other: LatticeElement>>): Int { - return if ( - this.elements.keys.containsAll(other.elements.keys) && - this.elements.all { (k, v) -> v.containsAll(other.elements[k] ?: identitySetOf()) } - ) { - if ( - this.elements.keys.size > (other.elements.keys.size) || - this.elements.any { (k, v) -> v.size > (other.elements[k]?.size ?: 0) } - ) - 1 - else 0 - } else { - -1 - } - } + override fun duplicate() = + PrevEOGLattice(this.elements.mapValues { (_, v) -> PowersetLattice(v.elements) }) } /** * A state which actually holds a state for all [Edge]s. It maps the node to its * [BranchingNode]-parent and the path through which it is reached. */ -class PrevEOGState : State>>() +class PrevEOGState(elements: Map) : + MapLattice>>(elements) { + + override fun lub( + other: LatticeElement>>>>> + ): PrevEOGState { + return PrevEOGState( + this.elements + .mapValues { (_, v) -> + PrevEOGLattice(v.elements.mapValues { (_, v2) -> PowersetLattice(v2.elements) }) + } + .entries + .fold( + other.elements.mapValues { (_, v) -> + PrevEOGLattice( + v.elements.mapValues { (_, v2) -> PowersetLattice(v2.elements) } + ) + } + ) { current, (thisKey, thisValue) -> + val mutableMap = current.toMutableMap() + mutableMap.compute(thisKey) { k, v -> + if (v == null) thisValue + else + thisValue.lub( + PrevEOGLattice( + v.elements.mapValues { (_, v2) -> PowersetLattice(v2.elements) } + ) + ) + } + mutableMap + } + ) + } + + override fun duplicate() = + PrevEOGState( + this.elements.mapValues { (_, v) -> + PrevEOGLattice(v.elements.mapValues { (_, v2) -> PowersetLattice(v2.elements) }) + } + ) + + fun push(newNode: Node, newEOGLattice: PrevEOGLattice): PrevEOGState { + return this.lub(MapLattice(mapOf(Pair(newNode, newEOGLattice)))) + } +} From 5331a8a57558e4a7595a6edfe3b72bb2bc4731a7 Mon Sep 17 00:00:00 2001 From: Alexander Kuechler Date: Tue, 5 Nov 2024 14:17:11 +0100 Subject: [PATCH 2/3] Cleanup imports --- .../fraunhofer/aisec/cpg/passes/ControlDependenceGraphPass.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/ControlDependenceGraphPass.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/ControlDependenceGraphPass.kt index 0e9c72c1f5..a83322dc16 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/ControlDependenceGraphPass.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/ControlDependenceGraphPass.kt @@ -37,12 +37,12 @@ import de.fraunhofer.aisec.cpg.graph.statements.IfStatement import de.fraunhofer.aisec.cpg.graph.statements.ReturnStatement import de.fraunhofer.aisec.cpg.graph.statements.expressions.ConditionalExpression import de.fraunhofer.aisec.cpg.graph.statements.expressions.ShortCircuitOperator -import de.fraunhofer.aisec.cpg.helpers.* import de.fraunhofer.aisec.cpg.helpers.functional.LatticeElement import de.fraunhofer.aisec.cpg.helpers.functional.MapLattice import de.fraunhofer.aisec.cpg.helpers.functional.PowersetLattice import de.fraunhofer.aisec.cpg.helpers.functional.PowersetLatticeT import de.fraunhofer.aisec.cpg.helpers.functional.iterateEOGClean +import de.fraunhofer.aisec.cpg.helpers.identitySetOf import de.fraunhofer.aisec.cpg.passes.configuration.DependsOn import java.util.* import kotlin.collections.component1 From fcea695c16102181f5f77cbeba82436c67e44084 Mon Sep 17 00:00:00 2001 From: Alexander Kuechler Date: Tue, 19 Nov 2024 10:44:54 +0100 Subject: [PATCH 3/3] More generics --- .../aisec/cpg/helpers/IdentitySet.kt | 6 +- .../cpg/helpers/functional/BasicLattices.kt | 90 +++++++------- .../cpg/passes/ControlDependenceGraphPass.kt | 113 +++++++++--------- 3 files changed, 111 insertions(+), 98 deletions(-) diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/helpers/IdentitySet.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/helpers/IdentitySet.kt index d6228a0421..b38749c450 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/helpers/IdentitySet.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/helpers/IdentitySet.kt @@ -60,9 +60,9 @@ open class IdentitySet : MutableSet { } override fun equals(other: Any?): Boolean { - if (other !is IdentitySet<*>) return false - val otherSet = other as? IdentitySet<*> - return otherSet != null && this.containsAll(otherSet) && otherSet.containsAll(this) + if (other !is Set<*>) return false + val otherSet = other + return this.containsAll(otherSet) && otherSet.containsAll(this) } override fun add(element: T): Boolean { diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/helpers/functional/BasicLattices.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/helpers/functional/BasicLattices.kt index 20c1871bd2..fad488c9a0 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/helpers/functional/BasicLattices.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/helpers/functional/BasicLattices.kt @@ -25,7 +25,10 @@ */ package de.fraunhofer.aisec.cpg.helpers.functional +import de.fraunhofer.aisec.cpg.helpers.IdentitySet +import de.fraunhofer.aisec.cpg.helpers.identitySetOf import de.fraunhofer.aisec.cpg.helpers.toIdentitySet +import java.util.IdentityHashMap import kotlin.Pair import kotlin.collections.component1 import kotlin.collections.component2 @@ -42,33 +45,36 @@ import kotlin.collections.toMap * Implementations of this class have to implement the comparator, the least upper bound of two * lattices. */ -abstract class LatticeElement(val elements: T) : Comparable> { +interface LatticeElement : Comparable> { + val elements: T + /** * Computes the least upper bound of this lattice and [other]. It returns a new object and does * not modify either of the objects. */ - abstract fun lub(other: LatticeElement): LatticeElement + fun lub(other: LatticeElement): LatticeElement /** Duplicates the object, i.e., makes a deep copy. */ - abstract fun duplicate(): LatticeElement + fun duplicate(): LatticeElement } -typealias PowersetLatticeT = LatticeElement> +typealias PowersetLatticeT = PowersetLattice, V> -inline fun emptyPowersetLattice() = PowersetLattice(setOf()) +inline fun emptyPowersetLattice() = + PowersetLattice, V>(identitySetOf()) /** * Implements the [LatticeElement] for a lattice over a set of nodes. The lattice itself is * constructed by the powerset. */ -open class PowersetLattice(elements: Set) : LatticeElement>(elements) { - override fun lub(other: LatticeElement>) = - PowersetLattice(this.elements.union(other.elements)) +open class PowersetLattice, T>(override val elements: V) : LatticeElement { + override fun lub(other: LatticeElement) = + PowersetLattice(this.elements.union(other.elements).toIdentitySet()) - override fun duplicate(): LatticeElement> = - PowersetLattice(this.elements.toIdentitySet()) + override fun duplicate(): LatticeElement = + PowersetLattice(this.elements.toIdentitySet() as V) - override fun compareTo(other: LatticeElement>): Int { + override fun compareTo(other: LatticeElement): Int { return if (this.elements == other.elements) { 0 } else if (this.elements.containsAll(other.elements)) { @@ -79,8 +85,7 @@ open class PowersetLattice(elements: Set) : LatticeElement>(element } override fun equals(other: Any?): Boolean { - // The call of `toSet` ensures that we don't get stuck for different types of sets. - return other is PowersetLattice && this.elements.toSet() == other.elements.toSet() + return other is PowersetLattice && this.elements == other.elements } override fun hashCode(): Int { @@ -88,40 +93,44 @@ open class PowersetLattice(elements: Set) : LatticeElement>(element } } -typealias MapLatticeT = LatticeElement> +typealias MapLatticeT = MapLattice -inline fun emptyMapLattice() = MapLattice(mapOf()) +inline fun , T> emptyMapLattice() = + MapLattice(IdentityHashMap()) /** Implements the [LatticeElement] for a lattice over a map of nodes to another lattice. */ -open class MapLattice(elements: Map>) : - LatticeElement>>(elements) { +open class MapLattice, T>(override val elements: IdentityHashMap) : + LatticeElement> { + override fun lub( - other: LatticeElement>> - ): LatticeElement>> { + other: LatticeElement> + ): LatticeElement> { val allKeys = other.elements.keys.union(this.elements.keys) val newMap = - allKeys.fold(mutableMapOf>()) { current, key -> - val otherValue = other.elements[key] - val thisValue = this.elements[key] + allKeys.fold(IdentityHashMap()) { current, key -> + val otherValue: V? = other.elements[key] + val thisValue: V? = this.elements[key] val newValue = if (thisValue != null && otherValue != null) { thisValue.lub(otherValue) } else if (thisValue != null) { thisValue } else otherValue - newValue?.let { current[key] = it } + (newValue as? V?)?.let { current[key] = it } current } return MapLattice(newMap) } - override fun duplicate(): LatticeElement>> { + override fun duplicate(): LatticeElement> { return MapLattice( - this.elements.map { (k, v) -> Pair>(k, v.duplicate()) }.toMap() + IdentityHashMap( + this.elements.map { (k, v) -> Pair(k, v.duplicate() as V) }.toMap() + ) ) } - override fun compareTo(other: LatticeElement>>): Int { + override fun compareTo(other: LatticeElement>): Int { if (this.elements == other.elements) return 0 if ( this.elements.keys.containsAll(other.elements.keys) && @@ -134,7 +143,7 @@ open class MapLattice(elements: Map>) : } override fun equals(other: Any?): Boolean { - return other is MapLattice && this.elements == other.elements + return other is MapLattice && this.elements == other.elements } override fun hashCode(): Int { @@ -142,24 +151,23 @@ open class MapLattice(elements: Map>) : } } -open class TupleLattice(elements: Pair, LatticeElement>) : - LatticeElement, LatticeElement>>(elements) { - override fun lub( - other: LatticeElement, LatticeElement>> - ): LatticeElement, LatticeElement>> { +open class TupleLattice, V : LatticeElement, S, T>( + override val elements: Pair +) : LatticeElement> { + override fun lub(other: LatticeElement>): LatticeElement> { return TupleLattice( Pair( - this.elements.first.lub(other.elements.first), - this.elements.second.lub(other.elements.second) + this.elements.first.lub(other.elements.first) as U, + this.elements.second.lub(other.elements.second) as V ) ) } - override fun duplicate(): LatticeElement, LatticeElement>> { - return TupleLattice(Pair(elements.first.duplicate(), elements.second.duplicate())) + override fun duplicate(): LatticeElement> { + return TupleLattice(Pair(elements.first.duplicate() as U, elements.second.duplicate() as V)) } - override fun compareTo(other: LatticeElement, LatticeElement>>): Int { + override fun compareTo(other: LatticeElement>): Int { if ( this.elements.first == other.elements.first && this.elements.second == other.elements.second @@ -174,7 +182,7 @@ open class TupleLattice(elements: Pair, LatticeElement) return false + if (other !is TupleLattice) return false return other.elements.first == this.elements.first && other.elements.second == this.elements.second } @@ -189,10 +197,10 @@ open class TupleLattice(elements: Pair, LatticeElement( - elements: Triple, LatticeElement, LatticeElement> -) : LatticeElement, LatticeElement, LatticeElement>>(elements) { + override val elements: Triple, LatticeElement, LatticeElement> +) : LatticeElement, LatticeElement, LatticeElement>> { override fun lub( - other: LatticeElement, LatticeElement, LatticeElement>> + other: LatticeElement, LatticeElement, LatticeElement>> ): LatticeElement, LatticeElement, LatticeElement>> { return TripleLattice( Triple( diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/ControlDependenceGraphPass.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/ControlDependenceGraphPass.kt index a83322dc16..691d323af7 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/ControlDependenceGraphPass.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/ControlDependenceGraphPass.kt @@ -37,8 +37,10 @@ import de.fraunhofer.aisec.cpg.graph.statements.IfStatement import de.fraunhofer.aisec.cpg.graph.statements.ReturnStatement import de.fraunhofer.aisec.cpg.graph.statements.expressions.ConditionalExpression import de.fraunhofer.aisec.cpg.graph.statements.expressions.ShortCircuitOperator +import de.fraunhofer.aisec.cpg.helpers.IdentitySet import de.fraunhofer.aisec.cpg.helpers.functional.LatticeElement import de.fraunhofer.aisec.cpg.helpers.functional.MapLattice +import de.fraunhofer.aisec.cpg.helpers.functional.MapLatticeT import de.fraunhofer.aisec.cpg.helpers.functional.PowersetLattice import de.fraunhofer.aisec.cpg.helpers.functional.PowersetLatticeT import de.fraunhofer.aisec.cpg.helpers.functional.iterateEOGClean @@ -98,7 +100,9 @@ open class ControlDependenceGraphPass(ctx: TranslationContext) : EOGStarterPass( val identityMap = IdentityHashMap>() identityMap[startNode] = PowersetLattice(identitySetOf(startNode)) startState = startState.push(startNode, PrevEOGLattice(identityMap)) + log.debug("Iterating EOG of {}", startNode) val finalState = iterateEOGClean(startNode.nextEOGEdges, startState, ::handleEdge) + log.debug("Done iterating EOG of {}", startNode) val branchingNodeConditionals = getBranchingNodeConditions(startNode) @@ -273,8 +277,9 @@ open class ControlDependenceGraphPass(ctx: TranslationContext) : EOGStarterPass( */ fun handleEdge( currentEdge: EvaluationOrder, - currentState: LatticeElement>>>>> -): LatticeElement>>>>> { + currentState: + LatticeElement>>>>> +): LatticeElement>>>>> { var newState = currentState as? PrevEOGState ?: return currentState // Check if we start in a branching node and if this edge leads to the conditional // branch. In this case, the next node will move "one layer downwards" in the CDG. @@ -373,73 +378,73 @@ private fun IfStatement.allBranchesFromMyThenBranchGoThrough(node: Node?): Boole * Implements the [LatticeElement] over a set of nodes and their set of "nextEOG" nodes which reach * this node. */ -class PrevEOGLattice(elements: Map>) : - MapLattice>(elements) { +class PrevEOGLattice(elements: IdentityHashMap>) : + MapLatticeT, IdentitySet>(elements) { - override fun lub(other: LatticeElement>>>): PrevEOGLattice { - return PrevEOGLattice( - this.elements.entries.fold( - other.elements.mapValues { (_, v) -> PowersetLattice(v.elements) } - ) { current, (thisKey, thisValue) -> - val mutableMap = current.toMutableMap() - mutableMap.compute(thisKey) { k, v -> - PowersetLattice((if (v == null) thisValue else thisValue.lub(v)).elements) - } - mutableMap - } + override fun lub( + other: LatticeElement>> + ): PrevEOGLattice { + val powerset = PowersetLattice(IdentitySet()) + MapLattice, Node>, IdentitySet>( + IdentityHashMap() ) + val allKeys = other.elements.keys.union(this.elements.keys) + val newMap = + allKeys.fold(IdentityHashMap>()) { current, key -> + val otherValue = other.elements[key] + val thisValue = this.elements[key] + val newValue = + if (thisValue != null && otherValue != null) { + thisValue.lub(otherValue) + } else if (thisValue != null) { + thisValue + } else otherValue + newValue?.let { current[key] = it } + current + } + return PrevEOGLattice(newMap) } override fun duplicate() = - PrevEOGLattice(this.elements.mapValues { (_, v) -> PowersetLattice(v.elements) }) + PrevEOGLattice( + IdentityHashMap( + this.elements.mapValues { (_, v) -> PowersetLattice(v.elements) }.toMap() + ) + ) } /** * A state which actually holds a state for all [Edge]s. It maps the node to its * [BranchingNode]-parent and the path through which it is reached. */ -class PrevEOGState(elements: Map) : - MapLattice>>(elements) { +class PrevEOGState(elements: IdentityHashMap) : + MapLattice>(elements) { - override fun lub( - other: LatticeElement>>>>> - ): PrevEOGState { - return PrevEOGState( - this.elements - .mapValues { (_, v) -> - PrevEOGLattice(v.elements.mapValues { (_, v2) -> PowersetLattice(v2.elements) }) - } - .entries - .fold( - other.elements.mapValues { (_, v) -> - PrevEOGLattice( - v.elements.mapValues { (_, v2) -> PowersetLattice(v2.elements) } - ) - } - ) { current, (thisKey, thisValue) -> - val mutableMap = current.toMutableMap() - mutableMap.compute(thisKey) { k, v -> - if (v == null) thisValue - else - thisValue.lub( - PrevEOGLattice( - v.elements.mapValues { (_, v2) -> PowersetLattice(v2.elements) } - ) - ) - } - mutableMap - } - ) + override fun lub(other: LatticeElement>): PrevEOGState { + val allKeys = other.elements.keys.union(this.elements.keys) + val newMap = + allKeys.fold(IdentityHashMap()) { current, key -> + val otherValue = other.elements[key] + val thisValue = this.elements[key] + val newValue = + if (thisValue != null && otherValue != null) { + thisValue.lub(otherValue) + } else if (thisValue != null) { + thisValue + } else otherValue + newValue?.let { current[key] = it } + current + } + return PrevEOGState(newMap) } - override fun duplicate() = - PrevEOGState( - this.elements.mapValues { (_, v) -> - PrevEOGLattice(v.elements.mapValues { (_, v2) -> PowersetLattice(v2.elements) }) - } - ) + override fun duplicate() = PrevEOGState(this.elements) fun push(newNode: Node, newEOGLattice: PrevEOGLattice): PrevEOGState { - return this.lub(MapLattice(mapOf(Pair(newNode, newEOGLattice)))) + return this.lub( + PrevEOGState( + IdentityHashMap(mutableMapOf(Pair(newNode, newEOGLattice))) + ) + ) } }