Skip to content

Commit e3ad3fa

Browse files
[jssrc2cpg] Use ForkJoinParallelCpgPassWithAccumulator (#5883)
1 parent ec56591 commit e3ad3fa

File tree

13 files changed

+43
-41
lines changed

13 files changed

+43
-41
lines changed

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name := "joern"
22
ThisBuild / organization := "io.joern"
33
ThisBuild / scalaVersion := "3.6.4"
44

5-
val cpgVersion = "1.7.58"
5+
val cpgVersion = "1.7.60"
66

77
lazy val joerncli = Projects.joerncli
88
lazy val querydb = Projects.querydb

joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/CGlobal.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ class CGlobal extends Global {
1919
// 4) in this map with value HeaderFileParserLanguage.Both -> included from at least one C and one C++ source file, so we will have to parse it twice
2020
val headerIncludes: ConcurrentHashMap[String, HeaderFileParserLanguage] = new ConcurrentHashMap()
2121

22-
def typesSeen(): List[String] = {
23-
usedTypes.keys().asScala.toList
22+
def typesSeen(): Set[String] = {
23+
usedTypes.keys().asScala.toSet
2424
}
2525

2626
def unhandledMethodDeclarations(): Map[String, MethodInfo] = {

joern-cli/frontends/ghidra2cpg/src/main/scala/io/joern/ghidra2cpg/Ghidra2Cpg.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ class Ghidra2Cpg extends X2CpgFrontend {
125125
new ReturnEdgesPass(cpg).createAndApply()
126126
}
127127

128-
TypeNodePass.withRegisteredTypes(Types.types.toList, cpg).createAndApply()
128+
TypeNodePass.withRegisteredTypes(Types.types.toSet, cpg).createAndApply()
129129
new JumpPass(cpg).createAndApply()
130130
new LiteralPass(cpg, flatProgramAPI).createAndApply()
131131
}

joern-cli/frontends/javasrc2cpg/src/main/scala/io/joern/javasrc2cpg/JavaSrc2Cpg.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class JavaSrc2Cpg extends X2CpgFrontend {
2929
new OuterClassRefPass(cpg).createAndApply()
3030
JavaConfigFileCreationPass(cpg, config = config).createAndApply()
3131
if (!config.skipTypeInfPass) {
32-
TypeNodePass.withRegisteredTypes(astCreationPass.global.usedTypes.keys().asScala.toList, cpg).createAndApply()
32+
TypeNodePass.withRegisteredTypes(astCreationPass.global.usedTypes.keys().asScala.toSet, cpg).createAndApply()
3333
new TypeInferencePass(cpg).createAndApply()
3434
}
3535
}

joern-cli/frontends/jimple2cpg/src/main/scala/io/joern/jimple2cpg/Jimple2Cpg.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ class Jimple2Cpg extends X2CpgFrontend {
126126

127127
val global = globalFromAstCreation()
128128
TypeNodePass
129-
.withRegisteredTypes(global.usedTypes.keys().asScala.toList, cpg)
129+
.withRegisteredTypes(global.usedTypes.keys().asScala.toSet, cpg)
130130
.createAndApply()
131131
DeclarationRefPass(cpg).createAndApply()
132132
JavaConfigFileCreationPass(cpg, Option(tmpDir.toString), config).createAndApply()

joern-cli/frontends/jssrc2cpg/src/main/scala/io/joern/jssrc2cpg/astcreation/AstCreator.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import io.joern.jssrc2cpg.parser.BabelNodeInfo
77
import io.joern.x2cpg.Ast
88
import io.joern.x2cpg.AstCreatorBase
99
import io.joern.x2cpg.ValidationMode
10-
import io.joern.x2cpg.datastructures.Global
1110
import io.joern.x2cpg.datastructures.Stack.*
1211
import io.joern.x2cpg.datastructures.VariableScopeManager
1312
import io.joern.x2cpg.frontendspecific.jssrc2cpg.Defines
@@ -26,7 +25,7 @@ import ujson.Value
2625

2726
import scala.collection.mutable
2827

29-
class AstCreator(val config: Config, val global: Global, val parserResult: ParseResult)(implicit
28+
class AstCreator(val config: Config, val usedTypes: mutable.HashSet[String], val parserResult: ParseResult)(implicit
3029
withSchemaValidation: ValidationMode
3130
) extends AstCreatorBase[BabelNodeInfo, AstCreator](parserResult.filename)
3231
with AstForExpressionsCreator

joern-cli/frontends/jssrc2cpg/src/main/scala/io/joern/jssrc2cpg/astcreation/AstCreatorHelper.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ trait AstCreatorHelper(implicit withSchemaValidation: ValidationMode) { this: As
107107
}
108108

109109
protected def registerType(typeFullName: String): Unit = {
110-
global.usedTypes.putIfAbsent(typeFullName, true)
110+
usedTypes.add(typeFullName)
111111
}
112112

113113
protected def codeForNodes(nodes: Seq[NewNode]): Option[String] = nodes.collectFirst {

joern-cli/frontends/jssrc2cpg/src/main/scala/io/joern/jssrc2cpg/passes/AstCreationPass.scala

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,33 @@ import io.joern.jssrc2cpg.astcreation.AstCreator
55
import io.joern.jssrc2cpg.parser.BabelJsonParser
66
import io.joern.jssrc2cpg.utils.AstGenRunner.AstGenRunnerResult
77
import io.joern.x2cpg.ValidationMode
8-
import io.joern.x2cpg.datastructures.Global
9-
import io.joern.x2cpg.frontendspecific.jssrc2cpg.Defines
10-
import io.joern.x2cpg.utils.Report
11-
import io.joern.x2cpg.utils.TimeUtils
8+
import io.joern.x2cpg.utils.{Report, TimeUtils}
129
import io.shiftleft.codepropertygraph.generated.Cpg
13-
import io.shiftleft.passes.ForkJoinParallelCpgPass
10+
import io.shiftleft.passes.ForkJoinParallelCpgPassWithAccumulator
1411
import io.shiftleft.utils.IOUtils
15-
import org.slf4j.Logger
16-
import org.slf4j.LoggerFactory
12+
import org.slf4j.{Logger, LoggerFactory}
1713

1814
import java.nio.file.Paths
19-
import scala.jdk.CollectionConverters.*
20-
import scala.util.Failure
21-
import scala.util.Success
22-
import scala.util.Try
15+
import scala.collection.mutable
16+
import scala.util.{Failure, Success, Try}
2317

2418
class AstCreationPass(cpg: Cpg, astGenRunnerResult: AstGenRunnerResult, config: Config, report: Report = new Report())(
2519
implicit withSchemaValidation: ValidationMode
26-
) extends ForkJoinParallelCpgPass[(String, String)](cpg) {
20+
) extends ForkJoinParallelCpgPassWithAccumulator[(String, String), mutable.HashSet[String]](cpg) {
2721

2822
private val logger: Logger = LoggerFactory.getLogger(classOf[AstCreationPass])
2923

30-
private val global = new Global()
24+
private var collectedTypes: Set[String] = Set.empty
3125

32-
def typesSeen(): List[String] = global.usedTypes.keys().asScala.filterNot(_ == Defines.Any).toList
26+
def typesSeen(): Set[String] = collectedTypes
27+
28+
override def createAccumulator(): mutable.HashSet[String] = mutable.HashSet.empty[String]
29+
30+
override def mergeAccumulator(left: mutable.HashSet[String], right: mutable.HashSet[String]): Unit = left ++= right
31+
32+
override def onAccumulatorComplete(builder: DiffGraphBuilder, accumulator: mutable.HashSet[String]): Unit = {
33+
collectedTypes = accumulator.toSet
34+
}
3335

3436
override def generateParts(): Array[(String, String)] = astGenRunnerResult.parsedFiles.toArray
3537

@@ -47,15 +49,19 @@ class AstCreationPass(cpg: Cpg, astGenRunnerResult: AstGenRunnerResult, config:
4749
}
4850
}
4951

50-
override def runOnPart(diffGraph: DiffGraphBuilder, input: (String, String)): Unit = {
52+
override def runOnPart(
53+
diffGraph: DiffGraphBuilder,
54+
input: (String, String),
55+
usedTypes: mutable.HashSet[String]
56+
): Unit = {
5157
val (rootPath, jsonFilename) = input
5258
val parseResultMaybe = BabelJsonParser.readFile(Paths.get(rootPath), Paths.get(jsonFilename))
5359
val ((gotCpg, filename), duration) = TimeUtils.time {
5460
parseResultMaybe match {
5561
case Success(parseResult) =>
5662
report.addReportInfo(parseResult.filename, parseResult.fileLoc, parsed = true)
5763
Try {
58-
val localDiff = new AstCreator(config, global, parseResult).createAst()
64+
val localDiff = new AstCreator(config, usedTypes, parseResult).createAst()
5965
diffGraph.absorb(localDiff)
6066
} match {
6167
case Failure(exception) =>

joern-cli/frontends/jssrc2cpg/src/main/scala/io/joern/jssrc2cpg/passes/JavaScriptTypeNodePass.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import scala.collection.mutable
99

1010
object JavaScriptTypeNodePass {
1111

12-
def withRegisteredTypes(registeredTypes: List[String], cpg: Cpg): TypeNodePass = {
12+
def withRegisteredTypes(registeredTypes: Set[String], cpg: Cpg): TypeNodePass = {
1313
new TypeNodePass(registeredTypes, cpg, getTypesFromCpg = false) {
1414

1515
override def fullToShortName(typeName: String): String = {

joern-cli/frontends/kotlin2cpg/src/main/scala/io/joern/kotlin2cpg/Kotlin2Cpg.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ class Kotlin2Cpg extends X2CpgFrontend with UsesService {
245245

246246
val kotlinAstCreatorTypes = astCreator.usedTypes()
247247
val javaAstCreatorTypes = runJavaSrcInterop(cpg, config, filesWithJavaExtension, kotlinAstCreatorTypes)
248-
val allAstCreatorTypes = (kotlinAstCreatorTypes.toSet ++ javaAstCreatorTypes.toSet).toList
248+
val allAstCreatorTypes = kotlinAstCreatorTypes.toSet ++ javaAstCreatorTypes.toSet
249249

250250
TypeNodePass.withRegisteredTypes(allAstCreatorTypes, cpg).createAndApply()
251251

0 commit comments

Comments
 (0)