Skip to content
This repository was archived by the owner on Oct 15, 2020. It is now read-only.

Commit 689c44d

Browse files
authored
Fix concurrency issue when registering types (#215)
1 parent dddf4a6 commit 689c44d

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

src/main/scala/io/shiftleft/fuzzyc2cpg/FuzzyC2Cpg.scala

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@ import io.shiftleft.proto.cpg.Cpg.CpgStruct.Node
1111
import io.shiftleft.proto.cpg.Cpg.CpgStruct.Node.NodeType
1212
import io.shiftleft.proto.cpg.Cpg.{CpgStruct, NodePropertyName}
1313
import java.nio.file.{Files, Path}
14-
import java.util.concurrent.LinkedBlockingQueue
14+
import java.util.concurrent.{ConcurrentHashMap, LinkedBlockingQueue}
15+
1516
import io.shiftleft.passes.KeyPool
1617

17-
import scala.collection.mutable
1818
import scala.collection.mutable.ListBuffer
1919
import scala.collection.parallel.CollectionConverters._
2020
import scala.util.control.NonFatal
21+
import scala.jdk.CollectionConverters._
2122

22-
case class Global(usedTypes: mutable.Set[String] = new mutable.HashSet[String])
23+
case class Global(usedTypes: ConcurrentHashMap[String, Boolean] = new ConcurrentHashMap[String, Boolean]())
2324

2425
class FuzzyC2Cpg(outputModuleFactory: CpgOutputModuleFactory) {
2526
import FuzzyC2Cpg.logger
@@ -119,7 +120,7 @@ class FuzzyC2Cpg(outputModuleFactory: CpgOutputModuleFactory) {
119120
}
120121
}
121122

122-
private def addTypeNodes(usedTypes: mutable.Set[String], keyPool: KeyPool): Unit = {
123+
private def addTypeNodes(usedTypes: ConcurrentHashMap[String, Boolean], keyPool: KeyPool): Unit = {
123124
val cpg = CpgStruct.newBuilder()
124125
val outputModule = outputModuleFactory.create()
125126
outputModule.setOutputIdentifier("__types__")
@@ -158,8 +159,14 @@ class FuzzyC2Cpg(outputModuleFactory: CpgOutputModuleFactory) {
158159
.build
159160
}
160161

161-
private def createTypeNodes(usedTypes: mutable.Set[String], keyPool: KeyPool, cpg: CpgStruct.Builder): Unit = {
162-
usedTypes.toList.sorted
162+
private def createTypeNodes(usedTypes: ConcurrentHashMap[String, Boolean],
163+
keyPool: KeyPool,
164+
cpg: CpgStruct.Builder): Unit = {
165+
usedTypes
166+
.keys()
167+
.asScala
168+
.toList
169+
.sorted
163170
.foreach { typeName =>
164171
val node = newNode(NodeType.TYPE)
165172
.setKey(keyPool.next)

src/main/scala/io/shiftleft/fuzzyc2cpg/astnew/AstToCpgConverter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,7 @@ class AstToCpgConverter[NodeBuilderType, NodeType, EdgeBuilderType, EdgeType](
884884
}
885885

886886
private def registerType(typeName: String): String = {
887-
global.usedTypes += typeName
887+
global.usedTypes.put(typeName, true)
888888
typeName
889889
}
890890

src/main/scala/io/shiftleft/fuzzyc2cpg/passes/astcreation/AstCreator.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ private[astcreation] class AstCreator(diffGraph: DiffGraph.Builder,
849849
}
850850

851851
private def registerType(typeName: String): String = {
852-
global.usedTypes += typeName
852+
global.usedTypes.put(typeName, true)
853853
typeName
854854
}
855855

0 commit comments

Comments
 (0)