Skip to content

Commit

Permalink
fixup initialisation order of cpg generators (joernio#5137)
Browse files Browse the repository at this point in the history
We trigger the post-processing directly if a cpg is loaded on startup
(e.g. `./joern cpg.bin`). In that case, the typeRecoveryConfig has not
been initialized. There's a reason why in Scala you have to explicitly
make an effort to start with `null`, because more often than not
you'll be missing out some random edge case... lazy val is the better
alternative

fixes joernio#4999 (again...)
  • Loading branch information
mpollmeier authored and khemrajrathore committed Dec 13, 2024
1 parent 96fdeb0 commit 5839ae7
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,20 @@ import io.joern.x2cpg.passes.frontend.XTypeRecoveryConfig
import io.shiftleft.codepropertygraph.generated.Cpg

import java.nio.file.Path
import scala.compiletime.uninitialized
import scala.util.Try

/** Source-based front-end for Java
*/
case class JavaSrcCpgGenerator(config: FrontendConfig, rootPath: Path) extends CpgGenerator {
private lazy val command: Path = if (isWin) rootPath.resolve("javasrc2cpg.bat") else rootPath.resolve("javasrc2cpg")
private var enableTypeRecovery = false
private var typeRecoveryConfig: XTypeRecoveryConfig = uninitialized
private lazy val cmdLineArgs = config.cmdLineParams.toSeq
private lazy val enableTypeRecovery = cmdLineArgs.exists(_ == s"--${javasrc2cpg.ParameterNames.EnableTypeRecovery}")
private lazy val typeRecoveryConfig = XTypeRecoveryConfig.parse(cmdLineArgs)

/** Generate a CPG for the given input path. Returns the output path, or None, if no CPG was generated.
*/
override def generate(inputPath: String, outputPath: String = "cpg.bin"): Try[String] = {
val arguments = config.cmdLineParams.toSeq ++ Seq(inputPath, "--output", outputPath)
enableTypeRecovery = arguments.exists(_ == s"--${javasrc2cpg.ParameterNames.EnableTypeRecovery}")
if (enableTypeRecovery) typeRecoveryConfig = XTypeRecoveryConfig.parse(arguments)
val arguments = cmdLineArgs ++ Seq(inputPath, "--output", outputPath)
runShellCommand(command.toString, arguments).map(_ => outputPath)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,15 @@ import io.joern.x2cpg.passes.frontend.XTypeRecoveryConfig
import io.shiftleft.codepropertygraph.generated.Cpg

import java.nio.file.Path
import scala.compiletime.uninitialized
import scala.util.Try

case class JsSrcCpgGenerator(config: FrontendConfig, rootPath: Path) extends CpgGenerator {
private lazy val command: Path = if (isWin) rootPath.resolve("jssrc2cpg.bat") else rootPath.resolve("jssrc2cpg.sh")
private var typeRecoveryConfig: XTypeRecoveryConfig = uninitialized
private lazy val typeRecoveryConfig = XTypeRecoveryConfig.parse(config.cmdLineParams.toSeq)

/** Generate a CPG for the given input path. Returns the output path, or None, if no CPG was generated.
*/
override def generate(inputPath: String, outputPath: String = "cpg.bin.zip"): Try[String] = {
typeRecoveryConfig = XTypeRecoveryConfig.parse(config.cmdLineParams.toSeq)

if (File(inputPath).isDirectory) {
invoke(inputPath, outputPath)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,23 @@ import io.shiftleft.codepropertygraph.generated.Cpg
import scopt.OParser

import java.nio.file.Path
import scala.compiletime.uninitialized
import scala.util.Try

case class PhpCpgGenerator(config: FrontendConfig, rootPath: Path) extends CpgGenerator {
private lazy val command: Path = if (isWin) rootPath.resolve("php2cpg.bat") else rootPath.resolve("php2cpg")
private var typeRecoveryConfig: XTypeRecoveryConfig = uninitialized
private var setKnownTypesConfig: XTypeStubsParserConfig = uninitialized

override def generate(inputPath: String, outputPath: String): Try[String] = {
val cmdLineArgs = config.cmdLineParams.toSeq
typeRecoveryConfig = XTypeRecoveryConfig.parse(cmdLineArgs)
setKnownTypesConfig = OParser
private lazy val command: Path = if (isWin) rootPath.resolve("php2cpg.bat") else rootPath.resolve("php2cpg")
private lazy val cmdLineArgs = config.cmdLineParams.toSeq
private lazy val typeRecoveryConfig = XTypeRecoveryConfig.parse(cmdLineArgs)
private lazy val setKnownTypesConfig: XTypeStubsParserConfig = {
OParser
.parse(XTypeStubsParser.parserOptions2, cmdLineArgs, XTypeStubsParserConfig())
.getOrElse(
throw new RuntimeException(
s"unable to parse XTypeStubsParserConfig from commandline arguments ${cmdLineArgs.mkString(" ")}"
)
)
}

override def generate(inputPath: String, outputPath: String): Try[String] = {
val arguments = List(inputPath) ++ Seq("-o", outputPath) ++ config.cmdLineParams
runShellCommand(command.toString, arguments).map(_ => outputPath)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ import scala.util.Try
import scala.compiletime.uninitialized

case class PythonSrcCpgGenerator(config: FrontendConfig, rootPath: Path) extends CpgGenerator {
private lazy val command: Path = if (isWin) rootPath.resolve("pysrc2cpg.bat") else rootPath.resolve("pysrc2cpg")
private var typeRecoveryConfig: XTypeRecoveryConfig = uninitialized
private lazy val command: Path = if (isWin) rootPath.resolve("pysrc2cpg.bat") else rootPath.resolve("pysrc2cpg")
private lazy val typeRecoveryConfig = XTypeRecoveryConfig.parse(config.cmdLineParams.toSeq)

/** Generate a CPG for the given input path. Returns the output path, or None, if no CPG was generated.
*/
override def generate(inputPath: String, outputPath: String = "cpg.bin.zip"): Try[String] = {
typeRecoveryConfig = XTypeRecoveryConfig.parse(config.cmdLineParams.toSeq)
val arguments = Seq(inputPath, "-o", outputPath) ++ config.cmdLineParams
runShellCommand(command.toString, arguments).map(_ => outputPath)
}
Expand All @@ -30,7 +29,6 @@ case class PythonSrcCpgGenerator(config: FrontendConfig, rootPath: Path) extends

override def applyPostProcessingPasses(cpg: Cpg): Cpg = {
pysrc2cpg.postProcessingPasses(cpg, typeRecoveryConfig).foreach(_.createAndApply())

cpg
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import io.joern.x2cpg.passes.frontend.XTypeRecoveryConfig
import io.shiftleft.codepropertygraph.generated.Cpg

import java.nio.file.Path
import scala.compiletime.uninitialized
import scala.util.Try

case class SwiftSrcCpgGenerator(config: FrontendConfig, rootPath: Path) extends CpgGenerator {
private lazy val command: Path =
if (isWin) rootPath.resolve("swiftsrc2cpg.bat") else rootPath.resolve("swiftsrc2cpg.sh")
private var typeRecoveryConfig: XTypeRecoveryConfig = uninitialized

private lazy val typeRecoveryConfig = XTypeRecoveryConfig.parse(config.cmdLineParams.toSeq)

/** Generate a CPG for the given input path. Returns the output path, or None, if no CPG was generated.
*/
Expand All @@ -29,7 +29,6 @@ case class SwiftSrcCpgGenerator(config: FrontendConfig, rootPath: Path) extends

private def invoke(inputPath: String, outputPath: String): Try[String] = {
val arguments = Seq(inputPath, "--output", outputPath) ++ config.cmdLineParams
typeRecoveryConfig = XTypeRecoveryConfig.parse(config.cmdLineParams.toSeq)
runShellCommand(command.toString, arguments).map(_ => outputPath)
}

Expand Down

0 comments on commit 5839ae7

Please sign in to comment.