Skip to content
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

Fine-Tune Type Prop Queries & Added Iterations CLI Opt #3230

Merged
merged 4 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.joern.console.cpgcreation

import io.joern.console.FrontendConfig
import io.joern.pysrc2cpg._
import io.joern.pysrc2cpg.*
import io.joern.x2cpg.X2Cpg
import io.joern.x2cpg.passes.base.AstLinkerPass
import io.joern.x2cpg.passes.callgraph.NaiveCallLinker
Expand Down Expand Up @@ -31,8 +31,10 @@ case class PythonSrcCpgGenerator(config: FrontendConfig, rootPath: Path) extends
new ImportResolverPass(cpg).createAndApply()
new DynamicTypeHintFullNamePass(cpg).createAndApply()
new PythonInheritanceNamePass(cpg).createAndApply()
new PythonTypeRecoveryPass(cpg, XTypeRecoveryConfig(enabledDummyTypes = !pyConfig.forall(_.disableDummyTypes)))
.createAndApply()
val typeRecoveryConfig = pyConfig match
case Some(config) => XTypeRecoveryConfig(config.typePropagationIterations, !config.disableDummyTypes)
case None => XTypeRecoveryConfig()
new PythonTypeRecoveryPass(cpg, typeRecoveryConfig).createAndApply()
new PythonTypeHintCallLinker(cpg).createAndApply()
new NaiveCallLinker(cpg).createAndApply()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.joern.javasrc2cpg

import io.joern.javasrc2cpg.Frontend._
import io.joern.javasrc2cpg.Frontend.*
import io.joern.x2cpg.passes.frontend.{TypeRecoveryParserConfig, XTypeRecovery}
import io.joern.x2cpg.{X2CpgConfig, X2CpgMain}
import scopt.OParser

Expand All @@ -13,10 +14,10 @@ final case class Config(
delombokJavaHome: Option[String] = None,
delombokMode: Option[String] = None,
enableTypeRecovery: Boolean = false,
disableDummyTypes: Boolean = false,
jdkPath: Option[String] = None,
showEnv: Boolean = false
) extends X2CpgConfig[Config] {
) extends X2CpgConfig[Config]
with TypeRecoveryParserConfig[Config] {
def withInferenceJarPaths(paths: Set[String]): Config = {
copy(inferenceJarPaths = paths).withInheritedFields(this)
}
Expand All @@ -41,10 +42,6 @@ final case class Config(
copy(enableTypeRecovery = value).withInheritedFields(this)
}

def withDisableDummyTypes(value: Boolean): Config = {
copy(disableDummyTypes = value).withInheritedFields(this)
}

def withJdkPath(path: String): Config = {
copy(jdkPath = Some(path)).withInheritedFields(this)
}
Expand Down Expand Up @@ -83,10 +80,7 @@ private object Frontend {
.hidden()
.action((_, c) => c.withEnableTypeRecovery(true))
.text("enable generic type recovery"),
opt[Unit]("no-dummyTypes")
.hidden()
.action((_, c) => c.withDisableDummyTypes(true))
.text("disable generation of dummy types during type recovery"),
XTypeRecovery.parserOptions,
opt[String]("jdk-path")
.action((path, c) => c.withJdkPath(path))
.text("JDK used for resolving builtin Java types. If not set, current classpath will be used"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ private class RecoverForJavaFile(cpg: Cpg, cu: Method, builder: DiffGraphBuilder
}
}

override protected def nodeExistingTypes(storedNode: StoredNode): Seq[String] =
super.nodeExistingTypes(storedNode).filterNot(_.startsWith(Defines.UnresolvedNamespace))

// There seems to be issues with inferring these, often due to situations where super and this are confused on name
// and code properties.
override protected def storeIdentifierTypeInfo(i: Identifier, types: Seq[String]): Unit = if (i.name != "this") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package io.joern.jssrc2cpg
import better.files.File
import io.joern.dataflowengineoss.layers.dataflows.{OssDataFlow, OssDataFlowOptions}
import io.joern.jssrc2cpg.JsSrc2Cpg.postProcessingPasses
import io.joern.jssrc2cpg.passes._
import io.joern.jssrc2cpg.passes.*
import io.joern.jssrc2cpg.utils.AstGenRunner
import io.joern.x2cpg.X2Cpg.withNewEmptyCpg
import io.joern.x2cpg.X2CpgFrontend
Expand Down Expand Up @@ -57,11 +57,14 @@ class JsSrc2Cpg extends X2CpgFrontend[Config] {
object JsSrc2Cpg {

def postProcessingPasses(cpg: Cpg, config: Option[Config] = None): List[CpgPassBase] = {
val typeRecoveryConfig = config match
case Some(config) => XTypeRecoveryConfig(config.typePropagationIterations, !config.disableDummyTypes)
case None => XTypeRecoveryConfig()
List(
new JavaScriptInheritanceNamePass(cpg),
new ConstClosurePass(cpg),
new ImportResolverPass(cpg),
new JavaScriptTypeRecoveryPass(cpg, XTypeRecoveryConfig(enabledDummyTypes = !config.exists(_.disableDummyTypes))),
new JavaScriptTypeRecoveryPass(cpg, typeRecoveryConfig),
new JavaScriptTypeHintCallLinker(cpg),
new NaiveCallLinker(cpg)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,34 @@
package io.joern.jssrc2cpg

import io.joern.jssrc2cpg.Frontend._
import io.joern.jssrc2cpg.Frontend.*
import io.joern.x2cpg.passes.frontend.{TypeRecoveryParserConfig, XTypeRecovery}
import io.joern.x2cpg.utils.Environment
import io.joern.x2cpg.{X2CpgConfig, X2CpgMain}
import scopt.OParser

import java.nio.file.Paths

final case class Config(tsTypes: Boolean = true, disableDummyTypes: Boolean = false) extends X2CpgConfig[Config] {
final case class Config(tsTypes: Boolean = true) extends X2CpgConfig[Config] with TypeRecoveryParserConfig[Config] {

def withTsTypes(value: Boolean): Config = {
copy(tsTypes = value).withInheritedFields(this)
}

def withDisableDummyTypes(value: Boolean): Config = {
copy(disableDummyTypes = value).withInheritedFields(this)
}
}

object Frontend {
implicit val defaultConfig: Config = Config()

val cmdLineParser: OParser[Unit, Config] = {
val builder = OParser.builder[Config]
import builder._
import builder.*
OParser.sequence(
programName("jssrc2cpg"),
opt[Unit]("no-tsTypes")
.hidden()
.action((_, c) => c.withTsTypes(false))
.text("disable generation of types via Typescript"),
opt[Unit]("no-dummyTypes")
.hidden()
.action((_, c) => c.withDisableDummyTypes(true))
.text("disable generation of dummy types during type recovery")
XTypeRecovery.parserOptions
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.joern.pysrc2cpg

import io.joern.pysrc2cpg.Frontend.cmdLineParser
import io.joern.x2cpg.X2CpgMain
import io.joern.x2cpg.passes.frontend.XTypeRecovery
import scopt.OParser

import java.nio.file.Paths
Expand All @@ -20,10 +21,7 @@ private object Frontend {
// Default is specified in Py2CpgOFileSystemConfig because Scopt is a shit library.
.text("Specifies whether venv-dir is ignored. Default to true.")
.action(((value, config) => config.withIgnoreVenvDir(value))),
opt[Unit]("no-dummyTypes")
.hidden()
.action((_, c) => c.withDisableDummyTypes(true))
.text("disable generation of dummy types during type recovery")
XTypeRecovery.parserOptions
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package io.joern.pysrc2cpg

import io.joern.x2cpg.passes.frontend.TypeRecoveryParserConfig
import io.joern.x2cpg.{SourceFiles, X2Cpg, X2CpgConfig, X2CpgFrontend}
import io.shiftleft.codepropertygraph.Cpg
import io.shiftleft.utils.IOUtils
import org.slf4j.LoggerFactory

import java.nio.file._
import java.nio.file.*
import scala.util.Try

case class Py2CpgOnFileSystemConfig(
venvDir: Path = Paths.get(".venv"),
ignoreVenvDir: Boolean = true,
disableDummyTypes: Boolean = false,
requirementsTxt: String = "requirements.txt"
) extends X2CpgConfig[Py2CpgOnFileSystemConfig] {
) extends X2CpgConfig[Py2CpgOnFileSystemConfig]
with TypeRecoveryParserConfig[Py2CpgOnFileSystemConfig] {
def withVenvDir(venvDir: Path): Py2CpgOnFileSystemConfig = {
copy(venvDir = venvDir).withInheritedFields(this)
}
Expand All @@ -22,10 +23,6 @@ case class Py2CpgOnFileSystemConfig(
copy(ignoreVenvDir = value).withInheritedFields(this)
}

def withDisableDummyTypes(value: Boolean): Py2CpgOnFileSystemConfig = {
copy(disableDummyTypes = value).withInheritedFields(this)
}

def withRequirementsTxt(text: String): Py2CpgOnFileSystemConfig = {
copy(requirementsTxt = text).withInheritedFields(this)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ private class RecoverForPythonFile(cpg: Cpg, cu: File, builder: DiffGraphBuilder
if (fa.method.name == "<module>") {
Set(fa.method.fullName)
} else if (fa.method.typeDecl.nonEmpty) {
val parentTypes = fa.method.typeDecl.fullName.toSeq
val baseTypeFullNames = cpg.typeDecl.fullNameExact(parentTypes: _*).inheritsFromTypeFullName.toSeq
(parentTypes ++ baseTypeFullNames).filterNot(_.toLowerCase.matches("(any|object)")).toSet
val parentTypes = fa.method.typeDecl.fullName.toSet
val baseTypeFullNames = cpg.typeDecl.fullNameExact(parentTypes.toSeq: _*).inheritsFromTypeFullName.toSet
(parentTypes ++ baseTypeFullNames).filterNot(_.matches("(?i)(any|object)"))
} else {
super.getFieldParents(fa)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package io.joern.rubysrc2cpg

import io.joern.rubysrc2cpg.Frontend._
import io.joern.rubysrc2cpg.Frontend.*
import io.joern.x2cpg.passes.frontend.{XTypeRecovery, TypeRecoveryParserConfig}
import io.joern.x2cpg.{X2CpgConfig, X2CpgMain}
import scopt.OParser

final case class Config(enableDependencyDownload: Boolean = false) extends X2CpgConfig[Config] {
final case class Config(enableDependencyDownload: Boolean = false)
extends X2CpgConfig[Config]
with TypeRecoveryParserConfig[Config] {

def withEnableDependencyDownload(value: Boolean): Config = {
copy(enableDependencyDownload = value).withInheritedFields(this)
Expand All @@ -23,7 +26,8 @@ private object Frontend {
opt[Unit]("enableDependencyDownload")
.hidden()
.action((_, c) => c.withEnableDependencyDownload(false))
.text("enable dependency download for Unix System only")
.text("enable dependency download for Unix System only"),
XTypeRecovery.parserOptions
)
}
}
Expand Down
Loading