-
Notifications
You must be signed in to change notification settings - Fork 296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unify Common Test Fixture Properties (Note Possible API Changes) #4038
Changes from all commits
81bb132
840e6cd
783cd9f
167c8aa
a77c207
1cf8f8f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package io.joern.dataflowengineoss.testfixtures | ||
|
||
import io.joern.dataflowengineoss.DefaultSemantics | ||
import io.joern.dataflowengineoss.layers.dataflows.{OssDataFlow, OssDataFlowOptions} | ||
import io.joern.dataflowengineoss.queryengine.EngineContext | ||
import io.joern.dataflowengineoss.semanticsloader.{FlowSemantic, Semantics} | ||
import io.joern.x2cpg.testfixtures.TestCpg | ||
import io.shiftleft.semanticcpg.layers.LayerCreatorContext | ||
|
||
/** Extends the capabilities of the test CPG to handle the configuration of data-flow enhancements. | ||
*/ | ||
trait SemanticTestCpg { this: TestCpg => | ||
|
||
protected var _withOssDataflow = false | ||
protected var _extraFlows = List.empty[FlowSemantic] | ||
protected implicit var context: EngineContext = EngineContext() | ||
|
||
/** Allows one to enable data-flow analysis capabilities to the TestCpg. | ||
*/ | ||
def withOssDataflow(value: Boolean = true): this.type = { | ||
_withOssDataflow = value | ||
this | ||
} | ||
|
||
/** Allows one to add additional semantics to the engine context during PDG creation. | ||
*/ | ||
def withExtraFlows(value: List[FlowSemantic] = List.empty): this.type = { | ||
_extraFlows = value | ||
this | ||
} | ||
|
||
/** Some frontends require OSS data-flow to execute after post-processing, so we choose to expose this method without | ||
* defining where it's executed. | ||
*/ | ||
def applyOssDataFlow(): Unit = { | ||
if (_withOssDataflow) { | ||
val context = new LayerCreatorContext(this) | ||
val options = new OssDataFlowOptions(extraFlows = _extraFlows) | ||
new OssDataFlow(options).run(context) | ||
this.context = EngineContext(Semantics.fromList(DefaultSemantics().elements ++ _extraFlows)) | ||
} | ||
} | ||
|
||
} | ||
|
||
/** Allows the tests to make use of the data-flow engine and any additional semantics. | ||
*/ | ||
trait SemanticCpgTestFixture(extraFlows: List[FlowSemantic] = List.empty) { | ||
|
||
implicit val context: EngineContext = EngineContext(Semantics.fromList(DefaultSemantics().elements ++ extraFlows)) | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
name := "c2cpg" | ||
|
||
dependsOn(Projects.semanticcpg, Projects.dataflowengineoss % Test, Projects.x2cpg % "compile->compile;test->test") | ||
dependsOn(Projects.semanticcpg, Projects.dataflowengineoss % "compile->compile;test->test", Projects.x2cpg % "compile->compile;test->test") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here, but feel free to keep as is, I'll clean this up after this PR is in |
||
|
||
libraryDependencies ++= Seq( | ||
"org.scala-lang.modules" %% "scala-parallel-collections" % "1.0.4", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,14 @@ | ||
package io.joern.javasrc2cpg.testfixtures | ||
|
||
import io.joern.dataflowengineoss.layers.dataflows.{OssDataFlow, OssDataFlowOptions} | ||
import io.joern.dataflowengineoss.queryengine.EngineContext | ||
import io.joern.dataflowengineoss.semanticsloader.FlowSemantic | ||
import io.joern.dataflowengineoss.testfixtures.{SemanticCpgTestFixture, SemanticTestCpg} | ||
import io.joern.javasrc2cpg.{Config, JavaSrc2Cpg} | ||
import io.joern.x2cpg.X2Cpg | ||
import io.joern.x2cpg.testfixtures.{Code2CpgFixture, LanguageFrontend, TestCpg} | ||
import io.joern.x2cpg.testfixtures.{Code2CpgFixture, DefaultTestCpg, LanguageFrontend, TestCpg} | ||
import io.shiftleft.codepropertygraph.Cpg | ||
import io.shiftleft.codepropertygraph.generated.PropertyNames | ||
import io.shiftleft.codepropertygraph.generated.nodes.{Expression, Literal, StoredNode} | ||
import io.shiftleft.codepropertygraph.generated.nodes.{Expression, Literal} | ||
import io.shiftleft.semanticcpg.language.* | ||
import io.shiftleft.semanticcpg.layers.LayerCreatorContext | ||
|
||
import java.io.File | ||
|
||
|
@@ -23,31 +22,29 @@ trait JavaSrcFrontend extends LanguageFrontend { | |
} | ||
} | ||
|
||
class JavaSrcTestCpg(enableTypeRecovery: Boolean = false) extends TestCpg with JavaSrcFrontend { | ||
private var _withOssDataflow = false | ||
class JavaSrcTestCpg(enableTypeRecovery: Boolean = false) | ||
extends DefaultTestCpg | ||
with JavaSrcFrontend | ||
with SemanticTestCpg { | ||
|
||
def withOssDataflow(value: Boolean = true): this.type = { | ||
_withOssDataflow = value | ||
this | ||
} | ||
|
||
override def applyPasses(): Unit = { | ||
X2Cpg.applyDefaultOverlays(this) | ||
override protected def applyPasses(): Unit = { | ||
super.applyPasses() | ||
if (enableTypeRecovery) JavaSrc2Cpg.typeRecoveryPasses(this).foreach(_.createAndApply()) | ||
if (_withOssDataflow) { | ||
val context = new LayerCreatorContext(this) | ||
val options = new OssDataFlowOptions() | ||
new OssDataFlow(options).run(context) | ||
} | ||
applyOssDataFlow() | ||
} | ||
|
||
} | ||
|
||
class JavaSrcCode2CpgFixture(withOssDataflow: Boolean = false, enableTypeRecovery: Boolean = false) | ||
extends Code2CpgFixture(() => new JavaSrcTestCpg(enableTypeRecovery).withOssDataflow(withOssDataflow)) { | ||
class JavaSrcCode2CpgFixture( | ||
withOssDataflow: Boolean = false, | ||
extraFlows: List[FlowSemantic] = List.empty, | ||
enableTypeRecovery: Boolean = false | ||
) extends Code2CpgFixture(() => | ||
new JavaSrcTestCpg(enableTypeRecovery).withOssDataflow(withOssDataflow).withExtraFlows(extraFlows) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. interesting formatting... did scalafmt come up with that? in that case we need to update our config i guess... :) |
||
) | ||
with SemanticCpgTestFixture(extraFlows) { | ||
|
||
implicit val resolver: ICallResolver = NoResolve | ||
implicit lazy val engineContext: EngineContext = EngineContext() | ||
implicit val resolver: ICallResolver = NoResolve | ||
|
||
def getConstSourceSink( | ||
cpg: Cpg, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
;
separator led to problems with intellij in the past - splitting them up was the fix back then. Not sure if the problem still exists, but better play it safe.Just noticed that this occurs in other places again - I'll send a PR for that after this is merged.