Skip to content

Commit

Permalink
Merge branch 'main' into small-dfg-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
KuechA authored Feb 6, 2025
2 parents 3d4aac1 + edd5068 commit 6f1d9be
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,20 @@ import de.fraunhofer.aisec.cpg.graph.Node
import de.fraunhofer.aisec.cpg.graph.allChildren
import de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration
import de.fraunhofer.aisec.cpg.graph.declarations.cyclomaticComplexity
import de.fraunhofer.aisec.cpg.graph.edges.Edge
import de.fraunhofer.aisec.cpg.graph.edges.flows.EvaluationOrder
import de.fraunhofer.aisec.cpg.graph.statements.IfStatement
import de.fraunhofer.aisec.cpg.graph.statements.ReturnStatement
import de.fraunhofer.aisec.cpg.graph.statements.expressions.CollectionComprehension
import de.fraunhofer.aisec.cpg.graph.statements.expressions.ComprehensionExpression
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.Lattice
import de.fraunhofer.aisec.cpg.helpers.functional.MapLattice
import de.fraunhofer.aisec.cpg.helpers.functional.PowersetLattice
import de.fraunhofer.aisec.cpg.helpers.toIdentitySet
import de.fraunhofer.aisec.cpg.passes.configuration.DependsOn
import java.util.*
import kotlin.collections.component1
import kotlin.collections.component2
import kotlin.system.measureTimeMillis

/** This pass builds the Control Dependence Graph (CDG) by iterating through the EOG. */
Expand Down Expand Up @@ -87,29 +91,35 @@ open class ControlDependenceGraphPass(ctx: TranslationContext) : EOGStarterPass(
}
log.info("[CDG] Analyzing function ${startNode.name}. Complexity: $c")

val prevEOGState =
PrevEOGState(innerLattice = PrevEOGLattice(innerLattice = PowersetLattice<Node>()))

// 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<Node, IdentitySet<Node>>()
identityMap[startNode] = identitySetOf(startNode)
startState.push(startNode, PrevEOGLattice(identityMap))
var startState: PrevEOGStateElement = prevEOGState.bottom
startState =
prevEOGState.push(
startState,
startNode,
PrevEOGLatticeElement(startNode to PowersetLattice.Element(startNode)),
)
log.debug("Iterating EOG of {}", startNode)
var finalState: State<Node, IdentityHashMap<Node, IdentitySet<Node>>>
val executionTime = measureTimeMillis {
finalState = iterateEOG(startNode.nextEOGEdges, startState, ::handleEdge) ?: return
finalState = prevEOGState.iterateEOG(startNode.nextEOGEdges, startState, ::handleEdge)
}
log.info("[CDG] iterated EOG for ${startNode.name}. Time: $executionTime")

val branchingNodeConditionals = getBranchingNodeConditions(startNode)

// Collect the information, identify merge points, etc. This is not really efficient yet :(
for ((node, dominatorPaths) in finalState) {
val dominatorsList =
dominatorPaths.elements.entries
.map { (k, v) -> Pair(k, v.toMutableSet()) }
.toMutableList()
dominatorPaths.entries.map { (k, v) -> Pair(k, v.toMutableSet()) }.toMutableList()
val finalDominators = mutableListOf<Pair<Node, MutableSet<Node>>>()
val conditionKeys =
dominatorPaths.elements.entries
dominatorPaths.entries
.filter { (k, _) ->
(k as? BranchingNode)?.branchedBy == node ||
node in
Expand All @@ -122,7 +132,7 @@ 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[k1]?.forEach { (newK, newV) ->
val entry = dominatorsList.firstOrNull { it.first == newK }
entry?.let {
dominatorsList.remove(entry)
Expand All @@ -143,7 +153,7 @@ 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[k]
newDominatorMap?.forEach { (newK, newV) ->
when {
dominatorsList.any { it.first == newK } -> {
Expand All @@ -168,14 +178,14 @@ open class ControlDependenceGraphPass(ctx: TranslationContext) : EOGStarterPass(
it == Pair(newK, newV.toMutableSet()).hashCode()
} -> {
// We don't have an entry yet => add a new one
val newEntry = Pair(newK, newV.toMutableSet())
val newEntry = Pair(newK, newV.toIdentitySet())
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.toIdentitySet()))
}
}
}
Expand Down Expand Up @@ -232,8 +242,11 @@ open class ControlDependenceGraphPass(ctx: TranslationContext) : EOGStarterPass(
// For the function declaration, there's only the path through the function declaration
// itself.
Pair(functionDeclaration, setOf(functionDeclaration)),
*functionDeclaration
.allChildren<BranchingNode>()
*(functionDeclaration.allChildren<BranchingNode>() +
functionDeclaration.allChildren<CollectionComprehension>() +
functionDeclaration.allChildren<
ComprehensionExpression
>()) // TODO: May be simplified when resolving issue 2027
.filterIsInstance<Node>()
.map { branchingNode ->
val mergingPoints =
Expand Down Expand Up @@ -269,43 +282,51 @@ 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<Node>,
currentState: State<Node, IdentityHashMap<Node, IdentitySet<Node>>>,
): State<Node, IdentityHashMap<Node, IdentitySet<Node>>> {
lattice: Lattice<PrevEOGStateElement>,
currentEdge: EvaluationOrder,
currentState: PrevEOGStateElement,
): PrevEOGStateElement {
val lattice = lattice as? PrevEOGState ?: return currentState
var newState = currentState

val currentStart = currentEdge.start

// 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()) {
if (
currentStart is BranchingNode ||
currentStart is ComprehensionExpression ||
currentStart.astParent is
ComprehensionExpression && // TODO: May be simplified when resolving issue 2027
currentStart == (currentStart.astParent as ComprehensionExpression).iterable
) { // && currentEdge.isConditionalBranch()) {
// We start in a branching node and end in one of the branches, so we have the
// following state:
// for the branching node "start", we have a path through "end".
val prevPathLattice =
PrevEOGLattice(
IdentityHashMap(
currentState[currentEdge.start]?.elements?.filter { (k, _) ->
k == currentEdge.start
}
)
PrevEOGLatticeElement(
newState[currentStart]
?.filter { (k, _) -> k == currentStart }
?.mapValues { (_, v) -> PowersetLattice.Element(v) } ?: mapOf()
)
val map = IdentityHashMap<Node, IdentitySet<Node>>()
map[currentEdge.start] = identitySetOf(currentEdge.end)
val newPath = PrevEOGLattice(map).lub(prevPathLattice) as PrevEOGLattice
currentState.push(currentEdge.end, newPath)
val map = PrevEOGLatticeElement(currentStart to PowersetLattice.Element(currentEdge.end))

val newPath = lattice.innerLattice.lub(PrevEOGLatticeElement(map), prevPathLattice)
newState = lattice.push(newState, 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.
// If there is no state for the start node (most likely, this is the case for the
// first edge in a function), we generate a new state where we start in "start" end
// have "end" as the first node in the "branch".
val state =
PrevEOGLattice(
currentState[currentEdge.start]?.elements
?: IdentityHashMap(
mutableMapOf(Pair(currentEdge.start, identitySetOf(currentEdge.end)))
)
PrevEOGLatticeElement(
newState[currentStart]?.mapValues { (_, v) -> PowersetLattice.Element(v) }
?: mutableMapOf(Pair(currentStart, PowersetLattice.Element(currentEdge.end)))
)
currentState.push(currentEdge.end, state)
newState = lattice.push(newState, currentEdge.end, state)
}
return currentState
return newState
}

/**
Expand All @@ -326,6 +347,9 @@ private fun EvaluationOrder.isConditionalBranch(): Boolean {
true
} else
(this.start is IfStatement ||
this.start is ComprehensionExpression ||
(this.start.astParent is ComprehensionExpression &&
this.start == (this.start.astParent as ComprehensionExpression).iterable) ||
this.start is ConditionalExpression ||
this.start is ShortCircuitOperator) && branch == false ||
(this.start is IfStatement &&
Expand Down Expand Up @@ -360,44 +384,18 @@ private fun IfStatement.allBranchesFromMyThenBranchGoThrough(node: Node?): Boole
return true
}

/**
* Implements the [LatticeElement] over a set of nodes and their set of "nextEOG" nodes which reach
* this node.
*/
class PrevEOGLattice(override val elements: IdentityHashMap<Node, IdentitySet<Node>>) :
LatticeElement<IdentityHashMap<Node, IdentitySet<Node>>>(elements) {
typealias PrevEOGLatticeElement = MapLattice.Element<Node, PowersetLattice.Element<Node>>

override fun lub(
other: LatticeElement<IdentityHashMap<Node, IdentitySet<Node>>>
): LatticeElement<IdentityHashMap<Node, IdentitySet<Node>>> {
val newMap = IdentityHashMap(other.elements.mapValues { (_, v) -> v.toIdentitySet() })
for ((key, value) in this.elements) {
newMap.computeIfAbsent(key, ::identitySetOf).addAll(value)
}
return PrevEOGLattice(newMap)
}
typealias PrevEOGLattice = MapLattice<Node, PowersetLattice.Element<Node>>

override fun duplicate() = PrevEOGLattice(IdentityHashMap(this.elements))
typealias PrevEOGStateElement = MapLattice.Element<Node, PrevEOGLatticeElement>

override fun compareTo(other: LatticeElement<IdentityHashMap<Node, IdentitySet<Node>>>): 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
}
}
}
typealias PrevEOGState = MapLattice<Node, PrevEOGLatticeElement>

/**
* 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<Node, IdentityHashMap<Node, IdentitySet<Node>>>()
fun PrevEOGState.push(
currentElement: PrevEOGStateElement,
newNode: Node,
newEOGLattice: PrevEOGLatticeElement,
): PrevEOGStateElement {
return this.lub(currentElement, PrevEOGStateElement(newNode to newEOGLattice))
}
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
kotlin = "2.0.20"
kotlin19 = "1.9.10"
neo4j = "4.0.10"
neo4j5 = "5.27.0"
neo4j5 = "5.28.0"
log4j = "2.24.0"
spotless = "7.0.1"
nexus-publish = "2.0.0"
Expand Down

0 comments on commit 6f1d9be

Please sign in to comment.