Skip to content

Commit

Permalink
Use the new atom frontends for C
Browse files Browse the repository at this point in the history
Signed-off-by: Prabhu Subramanian <prabhu@appthreat.com>
  • Loading branch information
prabhu committed Sep 9, 2023
1 parent 56e616f commit 34bdc35
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 29 deletions.
6 changes: 3 additions & 3 deletions src/main/scala/io/appthreat/atom/Atom.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import io.appthreat.atom.dataflows.{DataFlowGraph, OssDataFlow, OssDataFlowOptio
import io.appthreat.atom.parsedeps.{AtomSlice, parseDependencies}
import io.appthreat.atom.passes.{SafeJSTypeRecoveryPass, TypeHintPass}
import io.appthreat.atom.slicing.*
import io.appthreat.atom.frontends.C2Atom
import io.appthreat.atom.frontends.{C2Atom, H2Atom}
import io.joern.c2cpg.{C2Cpg, Config as CConfig}
import io.joern.javasrc2cpg.{JavaSrc2Cpg, Config as JavaConfig}
import io.joern.jimple2cpg.{Jimple2Cpg, Config as JimpleConfig}
Expand Down Expand Up @@ -302,7 +302,7 @@ object Atom {

(language match {
case "H" | "HPP" =>
new C2Atom()
new H2Atom()
.createCpg(
CConfig(includeComments = false, logProblems = false, includePathsAutoDiscovery = false)
.withLogPreprocessor(false)
Expand All @@ -312,7 +312,7 @@ object Atom {
.withIgnoredFilesRegex(".*(test|docs|examples|samples|mocks).*")
)
case Languages.C | Languages.NEWC | "CPP" | "C++" =>
new C2Cpg()
new C2Atom()
.createCpgWithOverlays(
CConfig(includeComments = false, logProblems = false, includePathsAutoDiscovery = false)
.withLogPreprocessor(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ class AstCreationPass(cpg: Cpg, config: Config) extends ConcurrentWriterCpgPass[
override def runOnPart(diffGraph: DiffGraphBuilder, filename: String): Unit = {
val path = Paths.get(filename).toAbsolutePath
val relPath = SourceFiles.toRelativePath(path.toString, config.inputPath)
val (_, _) = TimeUtils.time {
try {
val parseResult = parser.parse(path)
parseResult match {
case Some(translationUnit) =>
val localDiff =
new AstCreator(relPath, config, translationUnit, file2OffsetTable)(config.schemaValidation).createAst()
diffGraph.absorb(localDiff)
true
case None =>
false
}
} catch {
case e: Throwable =>
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/main/scala/io/appthreat/atom/frontends/C2Atom.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package io.appthreat.atom.frontends

import io.joern.c2cpg.Config
import io.joern.c2cpg.datastructures.CGlobal
import io.joern.c2cpg.passes.TypeDeclNodePass
import io.shiftleft.codepropertygraph.Cpg
import io.shiftleft.codepropertygraph.generated.Languages
import io.joern.x2cpg.passes.frontend.MetaDataPass
import io.joern.x2cpg.passes.frontend.{MetaDataPass, TypeNodePass}
import io.joern.x2cpg.X2Cpg.withNewEmptyCpg
import io.joern.x2cpg.X2CpgFrontend
import io.shiftleft.semanticcpg.layers.{LayerCreator, LayerCreatorContext}

import scala.util.Try

Expand All @@ -16,6 +17,8 @@ class C2Atom extends X2CpgFrontend[Config] {
withNewEmptyCpg(config.outputPath, config) { (cpg, config) =>
new MetaDataPass(cpg, Languages.NEWC, config.inputPath).createAndApply()
new AstCreationPass(cpg, config).createAndApply()
TypeNodePass.withRegisteredTypes(CGlobal.typesSeen(), cpg).createAndApply()
new TypeDeclNodePass(cpg)(config.schemaValidation).createAndApply()
}
}

Expand Down
21 changes: 21 additions & 0 deletions src/main/scala/io/appthreat/atom/frontends/H2Atom.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.appthreat.atom.frontends

import io.joern.c2cpg.Config
import io.shiftleft.codepropertygraph.Cpg
import io.shiftleft.codepropertygraph.generated.Languages
import io.joern.x2cpg.passes.frontend.MetaDataPass
import io.joern.x2cpg.X2Cpg.withNewEmptyCpg
import io.joern.x2cpg.X2CpgFrontend

import scala.util.Try

class H2Atom extends X2CpgFrontend[Config] {

def createCpg(config: Config): Try[Cpg] = {
withNewEmptyCpg(config.outputPath, config) { (cpg, config) =>
new MetaDataPass(cpg, Languages.NEWC, config.inputPath).createAndApply()
new AstCreationPass(cpg, config).createAndApply()
}
}

}
13 changes: 10 additions & 3 deletions src/main/scala/io/appthreat/atom/slicing/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package io.appthreat.atom
import better.files.File
import io.circe.{Decoder, Encoder, HCursor, Json}
import io.shiftleft.codepropertygraph.generated.PropertyNames
import io.shiftleft.codepropertygraph.generated.nodes._
import io.shiftleft.semanticcpg.language._
import io.shiftleft.codepropertygraph.generated.nodes.*
import io.shiftleft.semanticcpg.language.*
import overflowdb.PropertyKey

import java.util.regex.Pattern
import scala.collection.concurrent.TrieMap

package object slicing {

Expand Down Expand Up @@ -400,6 +401,7 @@ package object slicing {
object DefComponent {

val unresolvedCallPattern: Pattern = Pattern.compile("^(<unknown|ANY).*$")
private val unknownMethodDeclCache = new TrieMap[String, DefComponent]()

/** Attempts to generate an [[DefComponent]] from the given CPG node.
*
Expand Down Expand Up @@ -477,7 +479,12 @@ package object slicing {
label = annotation.label
)
case x: AstNode =>
UnknownDef(x.code, typeFullName, lineNumber, columnNumber)
var methodDecl = x.code.takeWhile(_ != ')')
if (methodDecl.contains("(") && !methodDecl.endsWith(")")) methodDecl = methodDecl + ")"
unknownMethodDeclCache.getOrElseUpdate(
s"${methodDecl}|${lineNumber}|${columnNumber}",
UnknownDef(methodDecl, typeFullName, lineNumber, columnNumber)
)
}
}
}
Expand Down
30 changes: 15 additions & 15 deletions wrapper/nodejs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions wrapper/nodejs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@appthreat/atom",
"version": "1.1.10",
"version": "1.1.11",
"description": "Create atom (⚛) representation for your application, packages and libraries",
"exports": "./index.js",
"type": "module",
Expand All @@ -9,12 +9,12 @@
"lint": "eslint *.mjs *.js"
},
"dependencies": {
"@babel/parser": "^7.22.15",
"@babel/parser": "^7.22.16",
"typescript": "^5.2.2",
"yargs": "^17.7.2"
},
"devDependencies": {
"eslint": "^8.48.0"
"eslint": "^8.49.0"
},
"bin": {
"atom": "./index.js",
Expand Down

0 comments on commit 34bdc35

Please sign in to comment.