Skip to content

Commit

Permalink
lint fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Prabhu Subramanian <[email protected]>
  • Loading branch information
prabhu committed Nov 10, 2023
1 parent a1629e0 commit 2ea205b
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 59 deletions.
4 changes: 3 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ ThisBuild / organization := "io.appthreat"
ThisBuild / version := "1.6.0"
ThisBuild / scalaVersion := "3.3.1"

val chenVersion = "0.6.3"
val chenVersion = "1.0.0"

lazy val atom = Projects.atom

Expand Down Expand Up @@ -170,6 +170,8 @@ Global / onChangedBuildSource := ReloadOnSourceChanges
Compile / doc / sources := Seq.empty
Compile / packageDoc / publishArtifact := false

wartremoverWarnings ++= Seq(Wart.NoNeedImport, Wart.ArrayEquals, Wart.Any, Wart.FinalCaseClass, Wart.FinalVal, Wart.ToString, Wart.TryPartial)

githubOwner := "appthreat"
githubRepository := "atom"
githubSuppressPublicationWarning := true
Expand Down
1 change: 1 addition & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ addSbtPlugin("com.github.sbt" % "sbt-native-packager" % "1.9.16")
addSbtPlugin("io.shiftleft" % "sbt-overflowdb" % "2.29")
addSbtPlugin("com.codecommit" % "sbt-github-packages" % "0.5.2")
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "2.1.1")
addSbtPlugin("org.wartremover" % "sbt-wartremover" % "3.1.5")
3 changes: 2 additions & 1 deletion src/main/scala/io/appthreat/atom/Atom.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import io.shiftleft.codepropertygraph.generated.Languages
import io.shiftleft.semanticcpg.layers.LayerCreatorContext
import scopt.OptionParser

import java.util.Locale
import scala.language.postfixOps
import scala.util.{Failure, Properties, Success}

Expand Down Expand Up @@ -363,7 +364,7 @@ object Atom:
.withMethodAnnotationFilter(x.methodAnnotationFilter)

private def generateAtom(config: BaseConfig, language: String): Either[String, String] =
generateForLanguage(language.toUpperCase, config)
generateForLanguage(language.toUpperCase(Locale.ROOT), config)

private def generateForLanguage(language: String, config: BaseConfig): Either[String, String] =
val outputAtomFile = config match
Expand Down
32 changes: 10 additions & 22 deletions src/main/scala/io/appthreat/atom/dataflows/DataFlowGraph.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@ private class DataFlowGraph(nodes: Set[Option[DFNode]]):
// Maximum number of data-flow paths to compute
private val MAX_PATHS = 100

private val USEFUL_PATH_LABELS = List("METHOD_PARAMETER_IN", "CALL")

def paths: Set[Path] =
implicit val finalSet: mutable.Set[Path] = mutable.Set.empty
implicit val nMap: Map[Long, DFNode] = nodes.map(x => x.get.id -> x.get).toMap
nodes.foreach { n =>
val currPath = List(n.get.id)
val currLabelPath = List(n.get.label)
follow(currPath, currLabelPath, n.get.out.flatMap(nMap.get))
val currPath = List(n.get.id)
follow(currPath, n.get.out.flatMap(nMap.get))
}
finalSet.toSet

Expand All @@ -32,39 +29,30 @@ private class DataFlowGraph(nodes: Set[Option[DFNode]]):
private def isSubList[A](lst: List[A])(implicit finalSet: mutable.Set[Path]): Boolean =
finalSet.filterNot(_.size < lst.size).exists(xs => isSubList(lst, xs))

/** A given path is useful if it starts with a METHOD_PARAMETER_IN contains at least 1 CALL and
* a METHOD_PARAMETER_IN nodes
*/
private def isUsefulPath(finalSet: mutable.Set[Path], path: List[String]): Boolean =
path.last == "METHOD_PARAMETER_IN" || (path.contains(
"CALL"
) && path.size > 5) || path.count(x =>
x != "IDENTIFIER"
) > 2

/** Is there an existing path that starts and ends with the same node
*/
private def isDuplicate(finalSet: mutable.Set[Path], path: Path): Boolean =
finalSet.exists(apath => apath.head == path.head && apath.last == path.last)
finalSet.exists(apath =>
apath.headOption == path.headOption && apath.lastOption == path.lastOption
)

private def follow(currPath: List[Long], currLabelPath: List[String], outNodes: Set[DFNode])(
private def follow(currPath: List[Long], outNodes: Set[DFNode])(
implicit
nMap: Map[Long, DFNode],
finalSet: mutable.Set[Path]
): Unit =
outNodes.foreach { x =>
val path = currPath :+ x.id
val labelPath = currLabelPath :+ x.label
val queue = x.out.filterNot(currPath.contains)
val path = currPath :+ x.id
val queue = x.out.filterNot(currPath.contains)
if queue.isEmpty then
if !isDuplicate(finalSet, path) && !isSubList(path) then
finalSet.add(path)
else if finalSet.size < MAX_PATHS then
follow(path, labelPath, queue.flatMap(nMap.get))
follow(path, queue.flatMap(nMap.get))
}
end DataFlowGraph

private case class DFNode(
private final case class DFNode(
id: Long,
isExternal: Boolean,
label: String,
Expand Down
64 changes: 30 additions & 34 deletions src/main/scala/io/appthreat/atom/passes/SafeConcurrentCpgPass.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,35 +43,31 @@ abstract class SafeConcurrentCpgPass[T <: AnyRef](
writerThread.setName("Writer")
writerThread.start()
implicit val ec: ExecutionContext = ExecutionContextProvider.getExecutionContext
var done = false
try
while !done || completedParts < nParts do
if completionQueue.size < producerQueueCapacity && partIter.hasNext then
val next = partIter.next()
completionQueue.prepend(Future.apply {
val builder = new DiffGraphBuilder
runOnPart(builder, next.asInstanceOf[T])
builder
})
else if completionQueue.nonEmpty then
val future = completionQueue.removeLast()
val res = Await.result(future, Duration.Inf).build()
nDiff += res.size
writer.queue.put(Some(res))
completedParts += 1
else
writer.queue.put(None)
completedParts += 1
done = true
finally
try
var done = false
while !done || completedParts < nParts do
if completionQueue.size < producerQueueCapacity && partIter.hasNext then
val next = partIter.next()
completionQueue.prepend(Future.apply {
val builder = new DiffGraphBuilder
runOnPart(builder, next.asInstanceOf[T])
builder
})
else if completionQueue.nonEmpty then
val future = completionQueue.removeLast()
val res = Await.result(future, Duration.Inf).build()
nDiff += res.size
writer.queue.put(Some(res))
completedParts += 1
else {
writer.queue.put(None)
completedParts += 1
done = true
}
writerThread.join()
finally
try
writerThread.join()
finally finish()
finally {
// pass
}
finish()
end try
end createApplySerializeAndStore

Expand All @@ -83,11 +79,12 @@ abstract class SafeConcurrentCpgPass[T <: AnyRef](
)

override def run(): Unit =
try
nDiffT = 0
var terminate = false
var index: Int = 0
while !terminate do
var terminate = false
var index: Int = 0
nDiffT = 0
var hadErrors = false
while !terminate do
try
queue.take() match
case None =>
terminate = true
Expand All @@ -96,8 +93,7 @@ abstract class SafeConcurrentCpgPass[T <: AnyRef](
.applyDiff(cpg.graph, diffGraph, keyPool.orNull, null)
.transitiveModifications()
index += 1
finally {
// pass
}
finally
hadErrors = true
end Writer
end SafeConcurrentCpgPass
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ object ReachableSlicing:
val addedPaths = mutable.Set[String]()
val purls = mutable.Set[String]()
path.elements.foreach { astNode =>
val lineNumber = astNode.lineNumber.getOrElse("").toString
val lineNumber = astNode.lineNumber.map(_.intValue())
val fileName = astNode.file.name.headOption.getOrElse("").replace("<unknown>", "")
var fileLocation = s"${fileName}#${lineNumber}"
var tags: String = tagAsString(astNode.tag)
Expand Down

0 comments on commit 2ea205b

Please sign in to comment.