From 98801c4f9d46ae624c98e4aa1a92c56568763f1c Mon Sep 17 00:00:00 2001 From: Christian Banse Date: Fri, 6 Sep 2024 00:57:54 +0200 Subject: [PATCH 1/2] Some improvements to pass annotations * Removes dependency to DFG pass from GoExtraPass * Adds dependency to dynamic invoke pass to PDG * Makes soft dependency the default for ExecuteBefore. Also names soft to softDependency to be consistent with ExecuteBefore --- .../src/main/kotlin/de/fraunhofer/aisec/cpg/passes/Pass.kt | 4 ++-- .../fraunhofer/aisec/cpg/passes/ProgramDependenceGraphPass.kt | 1 + .../de/fraunhofer/aisec/cpg/passes/ReplaceCallCastPass.kt | 2 +- .../aisec/cpg/passes/configuration/ExecuteBefore.kt | 4 ++-- .../aisec/cpg/passes/configuration/PassOrderingHelper.kt | 2 +- .../main/kotlin/de/fraunhofer/aisec/cpg/passes/GoExtraPass.kt | 2 +- 6 files changed, 8 insertions(+), 7 deletions(-) diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/Pass.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/Pass.kt index 350d596221..ee91444425 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/Pass.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/Pass.kt @@ -401,7 +401,7 @@ val KClass>.hardDependencies: Set>> val KClass>.softExecuteBefore: Set>> get() { return this.findAnnotations() - .filter { it.soft == true } + .filter { it.softDependency == true } .map { it.other } .toSet() } @@ -409,7 +409,7 @@ val KClass>.softExecuteBefore: Set>> val KClass>.hardExecuteBefore: Set>> get() { return this.findAnnotations() - .filter { it.soft == false } + .filter { it.softDependency == false } .map { it.other } .toSet() } diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/ProgramDependenceGraphPass.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/ProgramDependenceGraphPass.kt index 34eeb6ffb4..a848bd51b5 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/ProgramDependenceGraphPass.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/ProgramDependenceGraphPass.kt @@ -44,6 +44,7 @@ import de.fraunhofer.aisec.cpg.processing.strategy.Strategy @DependsOn(ControlDependenceGraphPass::class) @DependsOn(DFGPass::class) @DependsOn(ControlFlowSensitiveDFGPass::class, softDependency = true) +@DependsOn(DynamicInvokeResolver::class) class ProgramDependenceGraphPass(ctx: TranslationContext) : TranslationUnitPass(ctx) { private val visitor = object : IVisitor() { diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/ReplaceCallCastPass.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/ReplaceCallCastPass.kt index 2ee116fbec..5786b3609b 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/ReplaceCallCastPass.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/ReplaceCallCastPass.kt @@ -75,7 +75,7 @@ class ReplaceCallCastPass(ctx: TranslationContext) : TranslationUnitPass(ctx) { // cast expression. And this is only really necessary, if the function call has a single // argument. var callee = call.callee - if (parent != null && callee != null && call.arguments.size == 1) { + if (parent != null && call.arguments.size == 1) { val language = parent.language var pointer = false diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/configuration/ExecuteBefore.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/configuration/ExecuteBefore.kt index 959b95bd73..1a0c59249f 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/configuration/ExecuteBefore.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/configuration/ExecuteBefore.kt @@ -30,10 +30,10 @@ import kotlin.reflect.KClass /** * Register a dependency for the annotated pass. This ensures that the annotated pass is executed - * before [other] pass. The [soft] flag decides whether to treat this as a hard dependency + * before [other] pass. The [softDependency] flag decides whether to treat this as a hard dependency * (resulting in the pass being registered if not present) or not. */ @Retention(AnnotationRetention.RUNTIME) @Target(AnnotationTarget.CLASS) @Repeatable -annotation class ExecuteBefore(val other: KClass>, val soft: Boolean = false) +annotation class ExecuteBefore(val other: KClass>, val softDependency: Boolean = true) diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/configuration/PassOrderingHelper.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/configuration/PassOrderingHelper.kt index 4baa63f930..62ffa62a84 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/configuration/PassOrderingHelper.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/configuration/PassOrderingHelper.kt @@ -156,7 +156,7 @@ class PassOrderingHelper { // take care of [ExecuteBefore] dependencies for (dep in newElement.findAnnotations()) { - if (!dep.soft) { + if (!dep.softDependency) { addToWorkingList(dep.other) } } diff --git a/cpg-language-go/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/GoExtraPass.kt b/cpg-language-go/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/GoExtraPass.kt index 61cf609f87..2df5eeb357 100644 --- a/cpg-language-go/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/GoExtraPass.kt +++ b/cpg-language-go/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/GoExtraPass.kt @@ -109,8 +109,8 @@ import de.fraunhofer.aisec.cpg.passes.inference.startInference */ @ExecuteBefore(SymbolResolver::class) @ExecuteBefore(EvaluationOrderGraphPass::class) -@ExecuteBefore(DFGPass::class) @DependsOn(ImportResolver::class) +@DependsOn(TypeResolver::class) class GoExtraPass(ctx: TranslationContext) : ComponentPass(ctx) { private lateinit var walker: SubgraphWalker.ScopedWalker From 13a7a98cdbae79b3e98e1a99e8a8e109cfc60520 Mon Sep 17 00:00:00 2001 From: Christian Banse Date: Fri, 6 Sep 2024 00:59:54 +0200 Subject: [PATCH 2/2] Removed extra whitespace before mermaid graph --- .../kotlin/de/fraunhofer/aisec/cpg/TranslationConfiguration.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/TranslationConfiguration.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/TranslationConfiguration.kt index 5d2e481a4b..df8de34c7f 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/TranslationConfiguration.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/TranslationConfiguration.kt @@ -657,7 +657,7 @@ private constructor( log.info("Passes before enforcing order: {}", passes.map { it.simpleName }) val orderingHelper = PassOrderingHelper(passes) log.info( - "The following mermaid graph represents the pass dependencies: \n ${buildMermaid(passes)}" + "The following mermaid graph represents the pass dependencies: \n${buildMermaid(passes)}" ) return orderingHelper.order()