-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Program Dependency Graph (#1227)
* Try to use a more formal worklist EOG worklist iteration for the CFSensitiveDFG * Add new SplitsControlFlow interface * New unreachable EOG edges pass * Handle AssignExpressions in ControlFlowSensitiveDFGPass * Mark statements which should be irrelevant because the same edges are set in the normal DFGPass * Remove setting unnecessary (already existing) edges from CFSensitiveDFGPass * ConditionalExpression splits control flow * Fix issue 1141 * Fix issue 1141 * Consistency * Rename class and add fields * Generate CDG * Fix problem with very strange EOG self reference of literals * Test if statements and cdg * Add test for foreach loop * Reduce sonar warnings * Fix failing tests * Use HasShortcircuitOperators to generate ShortcircuitOperator objects in Expression Builder * Little fix * Integrate review feedback * More review feedback * Update cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/helpers/EOGWorklist.kt Co-authored-by: Christian Banse <christian.banse@aisec.fraunhofer.de> * Merge two iterateEOG functions * Fluent DSL for loops * Fix redundant stuff * Revert "Fluent DSL for loops" This reverts commit 4ecc621. * Get rid of "expectedUpdate" * Change method signatures * Small cleanup * Rename lattice to latticeelement * Remove nullable properties * Nicer code * Documentation * first draft for generating pdg property edges * change delegate constructor parameter to KProperty1 for consistency * change PDGEdges properties from using delegates to custom getters * add EventListener to set pdg edges before neo4j serialization * change to adding the PDG edges through a new Pass * fix typos * add test for the ProgramDependenceGraphPass * add more tests * add documentation * fix format violation * remove unused delegate class * rename pass to ProgramDependenceGraphPass and add documentation * rename pdgSetter to visitor * add new edge property DEPENDENCE * use new DEPENDENCE property for PDGEdges and change addAllPrevPDGEdges function to add the PropertyEdge to both nodes which makes addAllNextPDGEdges unnecessary * change test to not rely on hashCode for comparing the lists * Fix problems of merge * change to use DFG PropertyEdges instead of the nodes * change to use sets for storing PDG and adjust PropertyEdgeSetDelegate to use Collection instead of List * fix types in test --------- Co-authored-by: Alexander Kuechler <alexander.kuechler@aisec.fraunhofer.de> Co-authored-by: KuechA <31155350+KuechA@users.noreply.github.com> Co-authored-by: Christian Banse <christian.banse@aisec.fraunhofer.de> Co-authored-by: Christian Banse <oxisto@aybaze.com>
- Loading branch information
1 parent
38d963f
commit 7d720c8
Showing
5 changed files
with
301 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/ProgramDependenceGraphPass.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright (c) 2023, Fraunhofer AISEC. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* $$$$$$\ $$$$$$$\ $$$$$$\ | ||
* $$ __$$\ $$ __$$\ $$ __$$\ | ||
* $$ / \__|$$ | $$ |$$ / \__| | ||
* $$ | $$$$$$$ |$$ |$$$$\ | ||
* $$ | $$ ____/ $$ |\_$$ | | ||
* $$ | $$\ $$ | $$ | $$ | | ||
* \$$$$$ |$$ | \$$$$$ | | ||
* \______/ \__| \______/ | ||
* | ||
*/ | ||
package de.fraunhofer.aisec.cpg.passes | ||
|
||
import de.fraunhofer.aisec.cpg.TranslationContext | ||
import de.fraunhofer.aisec.cpg.graph.* | ||
import de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration | ||
import de.fraunhofer.aisec.cpg.graph.edge.DependenceType | ||
import de.fraunhofer.aisec.cpg.passes.order.DependsOn | ||
import de.fraunhofer.aisec.cpg.processing.IVisitor | ||
import de.fraunhofer.aisec.cpg.processing.strategy.Strategy | ||
|
||
/** | ||
* This pass collects the dependence information of each node into a Program Dependence Graph (PDG) | ||
* by traversing through the AST. | ||
*/ | ||
@DependsOn(ControlDependenceGraphPass::class) | ||
@DependsOn(DFGPass::class) | ||
@DependsOn(ControlFlowSensitiveDFGPass::class, softDependency = true) | ||
class ProgramDependenceGraphPass(ctx: TranslationContext) : TranslationUnitPass(ctx) { | ||
private val visitor = | ||
object : IVisitor<Node>() { | ||
/** | ||
* Collects the data and control dependence edges of a node and adds them to the program | ||
* dependence edges | ||
*/ | ||
override fun visit(t: Node) { | ||
t.addAllPrevPDGEdges(t.prevDFGEdges, DependenceType.DATA) | ||
t.addAllPrevPDGEdges(t.prevCDGEdges, DependenceType.CONTROL) | ||
} | ||
} | ||
|
||
override fun accept(tu: TranslationUnitDeclaration) { | ||
tu.statements.forEach(::handle) | ||
tu.namespaces.forEach(::handle) | ||
tu.declarations.forEach(::handle) | ||
} | ||
|
||
override fun cleanup() { | ||
// Nothing to do | ||
} | ||
|
||
private fun handle(node: Node) { | ||
node.accept(Strategy::AST_FORWARD, visitor) | ||
} | ||
} |
184 changes: 184 additions & 0 deletions
184
cpg-core/src/test/kotlin/de/fraunhofer/aisec/cpg/passes/ProgramDependenceGraphPassTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
/* | ||
* Copyright (c) 2023, Fraunhofer AISEC. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* $$$$$$\ $$$$$$$\ $$$$$$\ | ||
* $$ __$$\ $$ __$$\ $$ __$$\ | ||
* $$ / \__|$$ | $$ |$$ / \__| | ||
* $$ | $$$$$$$ |$$ |$$$$\ | ||
* $$ | $$ ____/ $$ |\_$$ | | ||
* $$ | $$\ $$ | $$ | $$ | | ||
* \$$$$$ |$$ | \$$$$$ | | ||
* \______/ \__| \______/ | ||
* | ||
*/ | ||
package de.fraunhofer.aisec.cpg.passes | ||
|
||
import de.fraunhofer.aisec.cpg.* | ||
import de.fraunhofer.aisec.cpg.frontends.TestLanguage | ||
import de.fraunhofer.aisec.cpg.frontends.TestLanguageFrontend | ||
import de.fraunhofer.aisec.cpg.graph.Node | ||
import de.fraunhofer.aisec.cpg.graph.builder.* | ||
import de.fraunhofer.aisec.cpg.graph.edge.DependenceType | ||
import de.fraunhofer.aisec.cpg.graph.edge.Properties | ||
import de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge | ||
import de.fraunhofer.aisec.cpg.graph.functions | ||
import de.fraunhofer.aisec.cpg.graph.get | ||
import de.fraunhofer.aisec.cpg.processing.IVisitor | ||
import de.fraunhofer.aisec.cpg.processing.strategy.Strategy | ||
import java.util.* | ||
import java.util.stream.Stream | ||
import kotlin.test.assertNotNull | ||
import kotlin.test.assertTrue | ||
import org.junit.jupiter.params.ParameterizedTest | ||
import org.junit.jupiter.params.provider.Arguments | ||
import org.junit.jupiter.params.provider.MethodSource | ||
|
||
class ProgramDependenceGraphPassTest { | ||
|
||
@ParameterizedTest(name = "test if pdg of {1} is equal to the union of cdg and dfg") | ||
@MethodSource("provideTranslationResultForPDGTest") | ||
fun `test if pdg is equal to union of cdg and dfg`(result: TranslationResult, name: String) { | ||
assertNotNull(result) | ||
val main = result.functions["main"] | ||
assertNotNull(main) | ||
|
||
main.accept( | ||
Strategy::AST_FORWARD, | ||
object : IVisitor<Node>() { | ||
override fun visit(t: Node) { | ||
val expectedPrevEdges = | ||
t.prevCDGEdges.map { | ||
it.apply { addProperty(Properties.DEPENDENCE, DependenceType.CONTROL) } | ||
} + | ||
t.prevDFG.map { | ||
PropertyEdge(it, t).apply { | ||
addProperty(Properties.DEPENDENCE, DependenceType.DATA) | ||
} | ||
} | ||
assertTrue( | ||
"prevPDGEdges did not contain all prevCDGEdges and edges to all prevDFG.\n" + | ||
"expectedPrevEdges: ${expectedPrevEdges.sortedBy { it.hashCode() }}\n" + | ||
"actualPrevEdges: ${t.prevPDGEdges.sortedBy { it.hashCode() }}" | ||
) { | ||
compareCollectionWithoutOrder(expectedPrevEdges, t.prevPDGEdges) | ||
} | ||
|
||
val expectedNextEdges = | ||
t.nextCDGEdges.map { | ||
it.apply { addProperty(Properties.DEPENDENCE, DependenceType.CONTROL) } | ||
} + | ||
t.nextDFG.map { | ||
PropertyEdge(t, it).apply { | ||
addProperty(Properties.DEPENDENCE, DependenceType.DATA) | ||
} | ||
} | ||
assertTrue( | ||
"nextPDGEdges did not contain all nextCDGEdges and edges to all nextDFG." + | ||
"\nexpectedNextEdges: ${expectedNextEdges.sortedBy { it.hashCode() }}" + | ||
"\nactualNextEdges: ${t.nextPDGEdges.sortedBy { it.hashCode() }}" | ||
) { | ||
compareCollectionWithoutOrder(expectedNextEdges, t.nextPDGEdges) | ||
} | ||
} | ||
} | ||
) | ||
} | ||
|
||
private fun <T> compareCollectionWithoutOrder( | ||
expected: Collection<T>, | ||
actual: Collection<T> | ||
): Boolean { | ||
val expectedWithDuplicatesGrouped = expected.groupingBy { it }.eachCount() | ||
val actualWithDuplicatesGrouped = actual.groupingBy { it }.eachCount() | ||
|
||
return expected.size == actual.size && | ||
expectedWithDuplicatesGrouped == actualWithDuplicatesGrouped | ||
} | ||
|
||
companion object { | ||
fun testFrontend(config: TranslationConfiguration): TestLanguageFrontend { | ||
val ctx = TranslationContext(config, ScopeManager(), TypeManager()) | ||
val language = config.languages.filterIsInstance<TestLanguage>().first() | ||
return TestLanguageFrontend(language.namespaceDelimiter, language, ctx) | ||
} | ||
|
||
@JvmStatic | ||
fun provideTranslationResultForPDGTest() = | ||
Stream.of( | ||
Arguments.of(getIfTest(), "if statement"), | ||
Arguments.of(getWhileLoopTest(), "while loop") | ||
) | ||
|
||
private fun getIfTest() = | ||
testFrontend( | ||
TranslationConfiguration.builder() | ||
.registerLanguage(TestLanguage("::")) | ||
.defaultPasses() | ||
.registerPass<ControlDependenceGraphPass>() | ||
.registerPass<ProgramDependenceGraphPass>() | ||
.build() | ||
) | ||
.build { | ||
translationResult { | ||
translationUnit("if.cpp") { | ||
// The main method | ||
function("main", t("int")) { | ||
body { | ||
declare { variable("i", t("int")) { call("rand") } } | ||
ifStmt { | ||
condition { ref("i") lt literal(0, t("int")) } | ||
thenStmt { | ||
ref("i") assign (ref("i") * literal(-1, t("int"))) | ||
} | ||
} | ||
returnStmt { ref("i") } | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun getWhileLoopTest() = | ||
testFrontend( | ||
TranslationConfiguration.builder() | ||
.registerLanguage(TestLanguage("::")) | ||
.defaultPasses() | ||
.registerPass<ControlDependenceGraphPass>() | ||
.registerPass<ProgramDependenceGraphPass>() | ||
.build() | ||
) | ||
.build { | ||
translationResult { | ||
translationUnit("loop.cpp") { | ||
// The main method | ||
function("main", t("int")) { | ||
body { | ||
declare { variable("i", t("int")) { call("rand") } } | ||
whileStmt { | ||
whileCondition { ref("i") gt literal(0, t("int")) } | ||
loopBody { | ||
call("printf") { literal("#", t("string")) } | ||
ref("i").dec() | ||
} | ||
} | ||
call("printf") { literal("\n", t("string")) } | ||
returnStmt { literal(0, t("int")) } | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |