Skip to content

Commit

Permalink
remove the "getInitialRange" method for values and instead mark decla…
Browse files Browse the repository at this point in the history
…rations as operations with effect
  • Loading branch information
CodingDepot committed Oct 14, 2024
1 parent beb4d38 commit f984a2c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,12 @@ class AbstractEvaluator {
targetName = node.name.toString()
targetType = getType(node)
val initializer = getInitializerOf(node, targetType)!!
val initialRange = getInitialRange(initializer, targetType)

// evaluate effect of each operation on the list until we reach "node"
val startState = IntervalState()
startState.push(initializer, IntervalLattice(initialRange))
startState.push(initializer, IntervalLattice(LatticeInterval.BOTTOM))
// TODO: terminates too early since it already knows the state of the first node
// -> mark declarations as node with effect in Integer and start with BOTTOM node!
val finalState = iterateEOG(initializer, startState, ::handleNode, goalNode)
// TODO: null-safety
return finalState!![node]!!.elements
Expand All @@ -84,6 +85,8 @@ class AbstractEvaluator {
state: State<Node, LatticeInterval>,
worklist: Worklist<Node, Node, LatticeInterval>
): State<Node, LatticeInterval> {
// TODO: we must not override the current state before they are checked by the worklist!
// otherwise it will seem as if nothing changed
// If the current node is already done
// (prevents infinite loop and unnecessary double-checking)
if (worklist.isDone(currentNode)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ package de.fraunhofer.aisec.cpg.analysis.abstracteval.value

import de.fraunhofer.aisec.cpg.analysis.abstracteval.LatticeInterval
import de.fraunhofer.aisec.cpg.graph.Node
import de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration
import de.fraunhofer.aisec.cpg.graph.statements.expressions.InitializerListExpression
import de.fraunhofer.aisec.cpg.graph.statements.expressions.Literal
import de.fraunhofer.aisec.cpg.graph.statements.expressions.NewArrayExpression
Expand All @@ -37,15 +38,13 @@ import org.apache.commons.lang3.NotImplementedException
class Array<T> : Value {
override fun applyEffect(current: LatticeInterval, node: Node, name: String): LatticeInterval {
// There are no functions that change the size of a Java array without destroying it
if (node is VariableDeclaration && node.initializer != null) {
val initValue = getSize(node.initializer!!)
return LatticeInterval.Bounded(initValue, initValue)
}
return current
}

override fun getInitialRange(initializer: Node): LatticeInterval {
// Consider multi-dimensional arrays (matrices)
val size = getSize(initializer)
return LatticeInterval.Bounded(size, size)
}

private fun getSize(node: Node): Int {
return when (node) {
// TODO: could be more performant if you detect that all initializers are Literals
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,22 @@ package de.fraunhofer.aisec.cpg.analysis.abstracteval.value

import de.fraunhofer.aisec.cpg.analysis.abstracteval.LatticeInterval
import de.fraunhofer.aisec.cpg.graph.Node
import de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration
import de.fraunhofer.aisec.cpg.graph.statements.expressions.AssignExpression
import de.fraunhofer.aisec.cpg.graph.statements.expressions.Literal
import de.fraunhofer.aisec.cpg.graph.statements.expressions.UnaryOperator
import org.apache.commons.lang3.NotImplementedException

class Integer : Value {
override fun applyEffect(current: LatticeInterval, node: Node, name: String): LatticeInterval {
// TODO: recursively evaluate right-hand-side to narrow down results
if (node is VariableDeclaration && node.initializer != null) {
val initValue =
when (val init = node.initializer) {
is Literal<*> -> init.value as? Int ?: throw NotImplementedException()
else -> throw NotImplementedException()
}
return LatticeInterval.Bounded(initValue, initValue)
}
if (node is UnaryOperator) {
if (node.input.code == name) {
return when (node.operatorCode) {
Expand Down Expand Up @@ -83,13 +91,4 @@ class Integer : Value {
}
return current
}

override fun getInitialRange(initializer: Node): LatticeInterval {
val value =
when (initializer) {
is Literal<*> -> initializer.value as? Int ?: throw NotImplementedException()
else -> throw NotImplementedException()
}
return LatticeInterval.Bounded(value, value)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ package de.fraunhofer.aisec.cpg.analysis.abstracteval.value

import de.fraunhofer.aisec.cpg.analysis.abstracteval.LatticeInterval
import de.fraunhofer.aisec.cpg.graph.Node
import de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration
import de.fraunhofer.aisec.cpg.graph.statements.expressions.MemberCallExpression
import de.fraunhofer.aisec.cpg.graph.statements.expressions.MemberExpression
import de.fraunhofer.aisec.cpg.graph.statements.expressions.NewExpression
Expand All @@ -35,6 +36,19 @@ import org.apache.commons.lang3.NotImplementedException

class MutableList : Value {
override fun applyEffect(current: LatticeInterval, node: Node, name: String): LatticeInterval {
if (node is VariableDeclaration && node.initializer != null) {
when (val init = node.initializer) {
is MemberCallExpression -> {
val size = init.arguments.size
return LatticeInterval.Bounded(size, size)
}
is NewExpression -> {
// TODO: could have a collection as argument!
return LatticeInterval.Bounded(0, 0)
}
else -> throw NotImplementedException()
}
}
// TODO: state can also be estimated by conditions! (if (l.size < 3) ...)
// TODO: assignment -> new size
// State can only be directly changed via MemberCalls (add, clear, ...)
Expand Down Expand Up @@ -81,18 +95,4 @@ class MutableList : Value {
else -> current
}
}

override fun getInitialRange(initializer: Node): LatticeInterval {
when (initializer) {
is MemberCallExpression -> {
val size = initializer.arguments.size
return LatticeInterval.Bounded(size, size)
}
is NewExpression -> {
// TODO: could have a collection as argument!
return LatticeInterval.Bounded(0, 0)
}
else -> throw NotImplementedException()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ interface Value {
return when (node) {
null -> null!!
is Reference -> getInitializer(node.refersTo)
is VariableDeclaration -> node.initializer!!
is VariableDeclaration -> node
else -> getInitializer(node.prevDFG.firstOrNull())
}
}

fun getInitialRange(initializer: Node): LatticeInterval
}

0 comments on commit f984a2c

Please sign in to comment.