Skip to content

Commit

Permalink
Fixing operator code bug in control-flow sensitive DFG
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Mar 14, 2024
1 parent 305809e commit 8a49957
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import de.fraunhofer.aisec.cpg.graph.AccessValues
import de.fraunhofer.aisec.cpg.graph.HasOperatorCode
import de.fraunhofer.aisec.cpg.graph.Node
import de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration
import de.fraunhofer.aisec.cpg.graph.firstAssignment
import de.fraunhofer.aisec.cpg.graph.statements.expressions.*
import de.fraunhofer.aisec.cpg.graph.statements.expressions.AssignExpression
import org.slf4j.Logger
Expand Down Expand Up @@ -110,14 +109,9 @@ open class ValueEvaluator(
}

protected fun handleVariableDeclaration(node: VariableDeclaration, depth: Int): Any? {
// If we have an initializer, we can use it
if (node.initializer != null) {
return evaluateInternal(node.initializer, depth + 1)
}

// Otherwise, we can try the firstAssignment. However, we actually should just use the DFG,
// but that does not seem to work?
return evaluateInternal(node.firstAssignment, depth + 1)
// If we have an initializer, we can use it. However, we actually should just use the DFG
// instead and do something similar to handleReference
return evaluateInternal(node.initializer, depth + 1)
}

/** Under certain circumstances, an assignment can also be used as an expression. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,8 @@ open class ControlFlowSensitiveDFGPass(ctx: TranslationContext) : EOGStarterPass

protected fun isSimpleAssignment(currentNode: Node): Boolean {
contract { returns(true) implies (currentNode is AssignExpression) }
return currentNode is AssignExpression && currentNode.operatorCode == "="
return currentNode is AssignExpression &&
(currentNode.operatorCode == "=" || currentNode.operatorCode == ":=")
}

/** Checks if the node is an increment or decrement operator (e.g. i++, i--, ++i, --i) */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ class ExpressionTest {
}

@Test
fun testShortAssgin() {
fun testShortAssign() {
val topLevel = Path.of("src", "test", "resources", "golang")
val tu =
TestUtils.analyzeAndGetFirstTU(
Expand All @@ -192,8 +192,12 @@ class ExpressionTest {
assertNotNull(lit5)
println(lit5.printDFG())

val x = tu.refs("x").firstOrNull()
val x = tu.refs("x").lastOrNull()
assertNotNull(x)
println(x.printDFG())

val paths = x.followPrevDFGEdgesUntilHit { it == lit5 }
assertEquals(3, paths.fulfilled.firstOrNull()?.size)

assertEquals(5, x.evaluate())
}
}

0 comments on commit 8a49957

Please sign in to comment.