-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a new frontend to parse only header files in c/c++ (#74)
Signed-off-by: Prabhu Subramanian <prabhu@appthreat.com>
- Loading branch information
Showing
9 changed files
with
192 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/main/scala/io/appthreat/atom/frontends/AstCreationPass.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package io.appthreat.atom.frontends | ||
|
||
import io.joern.c2cpg.Config | ||
import io.joern.c2cpg.astcreation.AstCreator | ||
import io.joern.c2cpg.parser.FileDefaults | ||
import io.joern.c2cpg.utils.TimeUtils | ||
import io.shiftleft.codepropertygraph.Cpg | ||
import io.shiftleft.passes.ConcurrentWriterCpgPass | ||
import io.joern.x2cpg.SourceFiles | ||
|
||
import java.nio.file.Paths | ||
import java.util.concurrent.ConcurrentHashMap | ||
import java.util.regex.Pattern | ||
import scala.util.matching.Regex | ||
|
||
class AstCreationPass(cpg: Cpg, config: Config) extends ConcurrentWriterCpgPass[String](cpg) { | ||
|
||
private val file2OffsetTable: ConcurrentHashMap[String, Array[Int]] = new ConcurrentHashMap() | ||
private val parser: CdtParser = new CdtParser(config) | ||
|
||
private val EscapedFileSeparator = Pattern.quote(java.io.File.separator) | ||
private val DefaultIgnoredFolders: List[Regex] = List( | ||
"\\..*".r, | ||
s"(.*[$EscapedFileSeparator])?tests?[$EscapedFileSeparator].*".r, | ||
s"(.*[$EscapedFileSeparator])?CMakeFiles[$EscapedFileSeparator].*".r | ||
) | ||
|
||
override def generateParts(): Array[String] = | ||
SourceFiles | ||
.determine( | ||
config.inputPath, | ||
FileDefaults.HEADER_FILE_EXTENSIONS, | ||
config.withDefaultIgnoredFilesRegex(DefaultIgnoredFolders) | ||
) | ||
.toArray | ||
|
||
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 { | ||
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 | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
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 io.shiftleft.semanticcpg.layers.{LayerCreator, LayerCreatorContext} | ||
|
||
import scala.util.Try | ||
|
||
class C2Atom 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() | ||
} | ||
} | ||
|
||
} |
94 changes: 94 additions & 0 deletions
94
src/main/scala/io/appthreat/atom/frontends/CdtParser.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package io.appthreat.atom.frontends | ||
|
||
import better.files.File | ||
import io.appthreat.atom.frontends.CdtParser.ParseResult | ||
import io.joern.c2cpg.Config | ||
import io.joern.c2cpg.parser.{CustomFileContentProvider, FileDefaults, HeaderFileFinder, ParserConfig} | ||
import io.shiftleft.utils.IOUtils | ||
import org.eclipse.cdt.core.dom.ast.gnu.c.GCCLanguage | ||
import org.eclipse.cdt.core.dom.ast.gnu.cpp.GPPLanguage | ||
import org.eclipse.cdt.core.dom.ast.{IASTPreprocessorStatement, IASTTranslationUnit} | ||
import org.eclipse.cdt.core.model.ILanguage | ||
import org.eclipse.cdt.core.parser.{DefaultLogService, ScannerInfo} | ||
import org.eclipse.cdt.core.parser.FileContent | ||
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor | ||
import org.slf4j.LoggerFactory | ||
|
||
import java.nio.file.{NoSuchFileException, Path} | ||
import scala.jdk.CollectionConverters.* | ||
|
||
object CdtParser { | ||
|
||
private case class ParseResult(translationUnit: Option[IASTTranslationUnit], failure: Option[Throwable] = None) | ||
|
||
def readFileAsFileContent(path: Path): FileContent = { | ||
val lines = IOUtils.readLinesInFile(path).mkString("\n").toArray | ||
FileContent.create(path.toString, true, lines) | ||
} | ||
|
||
} | ||
|
||
class CdtParser(config: Config) { | ||
|
||
import io.joern.c2cpg.parser.CdtParser._ | ||
|
||
private val headerFileFinder = new HeaderFileFinder(config.inputPath) | ||
private val parserConfig = ParserConfig.fromConfig(config) | ||
private val definedSymbols = parserConfig.definedSymbols.asJava | ||
private val includePaths = parserConfig.userIncludePaths | ||
private val log = new DefaultLogService | ||
|
||
// enables parsing of code behind disabled preprocessor defines: | ||
private val opts: Int = ILanguage.OPTION_PARSE_INACTIVE_CODE | ||
|
||
private def createParseLanguage(file: Path): ILanguage = { | ||
if (FileDefaults.isCPPFile(file.toString)) { | ||
GPPLanguage.getDefault | ||
} else { | ||
GCCLanguage.getDefault | ||
} | ||
} | ||
|
||
private def createScannerInfo(file: Path): ScannerInfo = { | ||
val additionalIncludes = | ||
if (FileDefaults.isCPPFile(file.toString)) parserConfig.systemIncludePathsCPP | ||
else parserConfig.systemIncludePathsC | ||
new ScannerInfo(definedSymbols, (includePaths ++ additionalIncludes).map(_.toString).toArray) | ||
} | ||
|
||
private def parseInternal(file: Path): ParseResult = { | ||
val realPath = File(file) | ||
if (realPath.isRegularFile) { // handling potentially broken symlinks | ||
try { | ||
val fileContent = readFileAsFileContent(realPath.path) | ||
val fileContentProvider = new CustomFileContentProvider(headerFileFinder) | ||
val lang = createParseLanguage(realPath.path) | ||
val scannerInfo = createScannerInfo(realPath.path) | ||
val translationUnit = lang.getASTTranslationUnit(fileContent, scannerInfo, fileContentProvider, null, opts, log) | ||
ParseResult(Option(translationUnit)) | ||
} catch { | ||
case u: UnsupportedClassVersionError => | ||
System.exit(1) | ||
ParseResult(None, failure = Option(u)) // return value to make the compiler happy | ||
case e: Throwable => | ||
ParseResult(None, failure = Option(e)) | ||
} | ||
} else { | ||
ParseResult( | ||
None, | ||
failure = Option(new NoSuchFileException(s"File '$realPath' does not exist. Check for broken symlinks!")) | ||
) | ||
} | ||
} | ||
|
||
def parse(file: Path): Option[IASTTranslationUnit] = { | ||
val parseResult = parseInternal(file) | ||
parseResult match { | ||
case ParseResult(Some(t), _) => | ||
Option(t) | ||
case ParseResult(_, _) => | ||
None | ||
} | ||
} | ||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters