Skip to content
This repository has been archived by the owner on Dec 3, 2021. It is now read-only.

Commit

Permalink
Fix JAR as Input Bug (#13)
Browse files Browse the repository at this point in the history
* Fixed jar unpacking bug

* Removed println
  • Loading branch information
DavidBakerEffendi authored Sep 16, 2021
1 parent 30b3dcf commit 6c684e0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
34 changes: 27 additions & 7 deletions src/main/scala/io/joern/jimple2cpg/Jimple2Cpg.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import java.nio.file.Files
import java.util.zip.ZipFile
import scala.jdk.CollectionConverters.EnumerationHasAsScala
import scala.language.postfixOps
import scala.util.Using
import scala.util.{Failure, Success, Using}

object Jimple2Cpg {
val language = "JIMPLEPARSER"
Expand All @@ -48,8 +48,8 @@ class Jimple2Cpg {
outputPath: Option[String] = None
): Cpg = {
configureSoot(sourceCodePath)
val cpg = newEmptyCpg(outputPath)

val cpg = newEmptyCpg(outputPath)
val metaDataKeyPool = new IntervalKeyPool(1, 100)
val typesKeyPool = new IntervalKeyPool(100, 1000100)
val methodKeyPool = new IntervalKeyPool(first = 1000100, last = Long.MaxValue)
Expand All @@ -60,10 +60,21 @@ class Jimple2Cpg {
val archiveFileExtensions = Set(".jar", ".war")
// Unpack any archives on the path onto the source code path as project root
val archives = SourceFiles.determine(Set(sourceCodePath), archiveFileExtensions)
archives.map(new ZipFile(_)).foreach(unzipArchive(_, sourceCodePath))
// Load source files
val sourceFileNames = SourceFiles.determine(Set(sourceCodePath), sourceFileExtensions)
val astCreator = new AstCreationPass(sourceCodePath, sourceFileNames, cpg, methodKeyPool)
// Load source files and unpack archives if necessary
val sourceFileNames = (archives
.map(new ZipFile(_))
.flatMap(x => {
unzipArchive(x, sourceCodePath) match {
case Failure(e) =>
logger.error(s"Error extracting files from archive at ${x.getName}", e); null
case Success(value) => value
}
})
.map(_.getAbsolutePath) ++ SourceFiles.determine(
Set(sourceCodePath),
sourceFileExtensions
)).distinct
val astCreator = new AstCreationPass(sourceCodePath, sourceFileNames, cpg, methodKeyPool)
astCreator.createAndApply()

new CfgCreationPass(cpg).createAndApply()
Expand Down Expand Up @@ -126,7 +137,16 @@ class Jimple2Cpg {
.filter(!_.isDirectory)
.filter(_.getName.contains(".class"))
.flatMap(entry => {
val destFile = new JFile(sourceCodePath + JFile.separator + entry.getName)
val sourceCodePathFile = new JFile(sourceCodePath)
// Handle the case if the input source code path is an archive itself
val destFile = if (sourceCodePathFile.isDirectory) {
new JFile(sourceCodePath + JFile.separator + entry.getName)
} else {
new JFile(
sourceCodePathFile.getParentFile.getAbsolutePath + JFile.separator + entry.getName
)
}
// dirName accounts for nested directories as a result of JAR package structure
val dirName = destFile.getAbsolutePath
.substring(0, destFile.getAbsolutePath.lastIndexOf(JFile.separator))
// Create directory path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package io.joern.jimple2cpg.unpacking

import io.joern.jimple2cpg.Jimple2Cpg
import io.shiftleft.codepropertygraph.Cpg
import io.shiftleft.semanticcpg.language.{toMethodTraversalExtGen, toNodeTypeStarters, toNodeTypeStartersOperatorExtension}
import io.shiftleft.semanticcpg.language.{toMethodTraversalExtGen, toNodeTypeStarters}
import org.scalatest.BeforeAndAfterAll
import org.scalatest.matchers.must.Matchers
import org.scalatest.matchers.should.Matchers.convertToAnyShouldWrapper
Expand Down

0 comments on commit 6c684e0

Please sign in to comment.