Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ ThisBuild / resolvers ++= Seq(
Resolver.mavenLocal,
"Sonatype OSS" at "https://oss.sonatype.org/content/repositories/public",
"Atlassian" at "https://packages.atlassian.com/mvn/maven-atlassian-external",
"Gradle Releases" at "https://repo.gradle.org/gradle/libs-releases/"
"Gradle Releases" at "https://repo.gradle.org/gradle/libs-releases/",
"Google Maven" at "https://maven.google.com/"
)

ThisBuild / Test / fork := true
Expand Down
1 change: 1 addition & 0 deletions joern-cli/frontends/jimple2cpg/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ libraryDependencies ++= Seq(
"org.scalatest" %% "scalatest" % Versions.scalatest % Test,
"org.benf" % "cfr" % Versions.cfr
)
dependencyOverrides += "com.google.guava" % "guava" % "33.5.0-jre" // required currently because of the dependencies of soot 4.7.1

enablePlugins(JavaAppPackaging, LauncherJarPlugin)
trapExit := false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class Jimple2Cpg extends X2CpgFrontend {
case _ =>
Options.v().set_src_prec(Options.src_prec_apk_c_j)
}
Options.v().set_process_multiple_dex(true)
// workaround for Soot's bug while parsing large apk.
// see: https://github.com/soot-oss/soot/issues/1256
Options.v().setPhaseOption("jb", "use-original-names:false")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ class AstCreator(
typeName
}

protected def sootTypeToString(t: soot.Type): String = {
t.toString.replace("\'", "")
}

/** Entry point of AST creation. Translates a compilation unit created by JavaParser into a DiffGraph containing the
* corresponding CPG AST.
*/
Expand Down Expand Up @@ -119,7 +123,7 @@ class AstCreator(
.code(arrRef.toString())
.lineNumber(line(parentUnit))
.columnNumber(column(parentUnit))
.typeFullName(registerType(arrRef.getType.toQuotedString))
.typeFullName(registerType(sootTypeToString(arrRef.getType)))

val astChildren = astsForValue(arrRef.getBase, parentUnit) ++ astsForValue(arrRef.getIndex, parentUnit)
Ast(indexAccess)
Expand All @@ -129,7 +133,7 @@ class AstCreator(

protected def astForLocal(local: soot.Local, parentUnit: SUnit): Ast = {
val name = local.getName
val typeFullName = registerType(local.getType.toQuotedString)
val typeFullName = registerType(sootTypeToString(local.getType))
Ast(
NewIdentifier()
.name(name)
Expand All @@ -145,7 +149,7 @@ class AstCreator(
NewIdentifier()
.code(x.toString())
.name(x.toString())
.typeFullName(registerType(x.getType.toQuotedString))
.typeFullName(registerType(sootTypeToString(x.getType)))
.lineNumber(line(parentUnit))
.columnNumber(column(parentUnit))
)
Expand All @@ -156,16 +160,16 @@ class AstCreator(
NewIdentifier()
.name("this")
.code("this")
.typeFullName(registerType(method.getType.toQuotedString))
.dynamicTypeHintFullName(Seq(registerType(method.getType.toQuotedString)))
.typeFullName(registerType(sootTypeToString(method.getType)))
.dynamicTypeHintFullName(Seq(registerType(sootTypeToString(method.getType))))
)
}

protected def createThisNode(method: SootMethod, builder: NewNode): Ast = createThisNode(method.makeRef(), builder)

protected def createThisNode(method: SootMethodRef, builder: NewNode): Ast = {
if (!method.isStatic || method.isConstructor) {
val parentType = registerType(Try(method.getDeclaringClass.getType.toQuotedString).getOrElse("ANY"))
val parentType = registerType(Try(sootTypeToString(method.getDeclaringClass.getType)).getOrElse("ANY"))
Ast(builder match {
case x: NewIdentifier =>
x.name("this")
Expand Down Expand Up @@ -205,7 +209,7 @@ class AstCreator(
val fieldAccessBlock = NewCall()
.name(Operators.fieldAccess)
.code(s"$leftOpString.${fieldRef.getFieldRef.name()}")
.typeFullName(registerType(fieldRef.getType.toQuotedString))
.typeFullName(registerType(sootTypeToString(fieldRef.getType)))
.methodFullName(Operators.fieldAccess)
.dispatchType(DispatchTypes.STATIC_DISPATCH)
.lineNumber(line(parentUnit))
Expand All @@ -219,7 +223,7 @@ class AstCreator(
.columnNumber(column(parentUnit))
.name(leftOpString)
.code(leftOpString)
.typeFullName(registerType(leftOpType.toQuotedString)),
.typeFullName(registerType(sootTypeToString(leftOpType))),
NewFieldIdentifier()
.order(2)
.argumentIndex(2)
Expand All @@ -241,7 +245,7 @@ class AstCreator(
.columnNumber(column(parentUnit))
.name(caughtException.toString())
.code(caughtException.toString())
.typeFullName(registerType(caughtException.getType.toQuotedString))
.typeFullName(registerType(sootTypeToString(caughtException.getType)))
)
}

Expand All @@ -251,7 +255,7 @@ class AstCreator(
Ast(
NewLiteral()
.code(s"${x.value.parseAsJavaType}.class")
.typeFullName(registerType(x.getType.toQuotedString))
.typeFullName(registerType(sootTypeToString(x.getType)))
)
case _: NullConstant =>
Ast(
Expand All @@ -263,7 +267,7 @@ class AstCreator(
Ast(
NewLiteral()
.code(constant.toString)
.typeFullName(registerType(constant.getType.toQuotedString))
.typeFullName(registerType(sootTypeToString(constant.getType)))
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ trait AstForMethodsCreator(implicit withSchemaValidation: ValidationMode) { this
methodAstWithAnnotations(
methodNode
.lineNumberEnd(methodBody.toString.split('\n').filterNot(_.isBlank).length)
.code(methodBody.toString),
.code(methodBody.toString.replace("\'", "")),
parameterAsts,
astForMethodBody(methodBody, bodyStatementsInfo),
astForMethodReturn(methodDeclaration),
Expand Down Expand Up @@ -146,14 +146,14 @@ trait AstForMethodsCreator(implicit withSchemaValidation: ValidationMode) { this
}

private def astForMethodReturn(methodDeclaration: SootMethod): NewMethodReturn = {
val typeFullName = registerType(methodDeclaration.getReturnType.toQuotedString)
val typeFullName = registerType(sootTypeToString(methodDeclaration.getReturnType))
methodReturnNode(methodDeclaration, typeFullName)
}

private def createMethodNode(methodDeclaration: SootMethod, typeDecl: RefType) = {
val name = methodDeclaration.getName
val fullName = methodFullName(typeDecl, methodDeclaration)
val methodDeclType = registerType(methodDeclaration.getReturnType.toQuotedString)
val methodDeclType = registerType(sootTypeToString(methodDeclaration.getReturnType))
val code = if (!methodDeclaration.isConstructor) {
s"$methodDeclType $name${paramListSignature(methodDeclaration, withParams = true)}"
} else {
Expand All @@ -168,19 +168,19 @@ trait AstForMethodsCreator(implicit withSchemaValidation: ValidationMode) { this
Option(signature),
filename,
Option(NodeTypes.TYPE_DECL),
Option(typeDecl.toQuotedString)
Option(sootTypeToString(typeDecl))
)
}

private def methodFullName(typeDecl: RefType, methodDeclaration: SootMethod): String = {
val typeName = registerType(typeDecl.toQuotedString)
val returnType = registerType(methodDeclaration.getReturnType.toQuotedString)
val typeName = registerType(sootTypeToString(typeDecl))
val returnType = registerType(sootTypeToString(methodDeclaration.getReturnType))
val methodName = methodDeclaration.getName
s"$typeName.$methodName:$returnType${paramListSignature(methodDeclaration)}"
}

private def paramListSignature(methodDeclaration: SootMethod, withParams: Boolean = false) = {
val paramTypes = methodDeclaration.getParameterTypes.asScala.map(x => registerType(x.toQuotedString))
val paramTypes = methodDeclaration.getParameterTypes.asScala.map(x => registerType(sootTypeToString(x)))

val paramNames =
if (!methodDeclaration.isPhantom && Try(methodDeclaration.retrieveActiveBody()).isSuccess)
Expand All @@ -198,7 +198,7 @@ trait AstForMethodsCreator(implicit withSchemaValidation: ValidationMode) { this

protected def astForParameterRef(parameterRef: ParameterRef, parentUnit: SUnit): Ast = {
val name = s"@parameter${parameterRef.getIndex}"
Ast(identifierNode(parentUnit, name, name, registerType(parameterRef.getType.toQuotedString)))
Ast(identifierNode(parentUnit, name, name, registerType(sootTypeToString(parameterRef.getType))))
}

private def astForParameter(
Expand All @@ -207,7 +207,7 @@ trait AstForMethodsCreator(implicit withSchemaValidation: ValidationMode) { this
methodDeclaration: SootMethod,
parameterAnnotations: Map[String, VisibilityAnnotationTag]
): Ast = {
val typeFullName = registerType(parameter.getType.toQuotedString)
val typeFullName = registerType(sootTypeToString(parameter.getType))

val paramAst = Ast(
parameterInNode(
Expand Down Expand Up @@ -236,7 +236,7 @@ trait AstForMethodsCreator(implicit withSchemaValidation: ValidationMode) { this
val jimpleLocals = body.getLocals.asScala.filterNot(l => jimpleParams.contains(l) || l.getName == "this").toList
val locals = jimpleLocals.map { local =>
val name = local.getName
val typeFullName = registerType(local.getType.toQuotedString)
val typeFullName = registerType(sootTypeToString(local.getType))
val code = s"$typeFullName $name"
Ast(localNode(body, name, code, typeFullName))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ trait AstForTypeDeclsCreator(implicit withSchemaValidation: ValidationMode) { th
/** Creates the AST root for type declarations and acts as the entry point for method generation.
*/
protected def astForTypeDecl(typ: RefType, namespaceBlockFullName: String): Ast = {
val fullName = registerType(typ.toQuotedString)
val fullName = registerType(sootTypeToString(typ))
val shortName = typ.getSootClass.getShortJavaStyleName
val clz = typ.getSootClass
val code = new mutable.StringBuilder()
Expand Down Expand Up @@ -59,7 +59,7 @@ trait AstForTypeDeclsCreator(implicit withSchemaValidation: ValidationMode) { th
}

private def astForField(field: SootField): Ast = {
val typeFullName = registerType(field.getType.toQuotedString)
val typeFullName = registerType(sootTypeToString(field.getType))
val name = field.getName
val annotations = field.getTags.asScala
.collect { case x: VisibilityAnnotationTag => x }
Expand All @@ -69,7 +69,7 @@ trait AstForTypeDeclsCreator(implicit withSchemaValidation: ValidationMode) { th
tag.getConstant.toString
}
val code =
if (field.getDeclaration.contains("enum")) name
if (field.getQuotedDeclaration.contains("enum")) name
else {
constantValue match {
case Some(value) => s"$typeFullName $name = $value"
Expand All @@ -87,11 +87,11 @@ trait AstForTypeDeclsCreator(implicit withSchemaValidation: ValidationMode) { th
*/
private def inheritedAndImplementedClasses(clazz: SootClass): (List[String], List[String]) = {
val implementsTypeFullName = clazz.getInterfaces.asScala.map { (i: SootClass) =>
registerType(i.getType.toQuotedString)
registerType(sootTypeToString(i.getType))
}.toList
val inheritsFromTypeFullName =
if (clazz.hasSuperclass && clazz.getSuperclass.getType.toQuotedString != "java.lang.Object") {
List(registerType(clazz.getSuperclass.getType.toQuotedString))
if (clazz.hasSuperclass && sootTypeToString(clazz.getSuperclass.getType) != "java.lang.Object") {
List(registerType(sootTypeToString(clazz.getSuperclass.getType)))
} else if (implementsTypeFullName.isEmpty) {
List(registerType("java.lang.Object"))
} else List()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) {
}

val signature =
s"${registerType(callee.getReturnType.toQuotedString)}(${(for (i <- 0 until callee.getParameterTypes.size())
yield registerType(callee.getParameterType(i).toQuotedString)).mkString(",")})"
s"${registerType(sootTypeToString(callee.getReturnType))}(${(for (i <- 0 until callee.getParameterTypes.size())
yield registerType(sootTypeToString(callee.getParameterType(i)))).mkString(",")})"
val thisAsts = invokeExpr match {
case expr: InstanceInvokeExpr => astsForValue(expr.getBase, parentUnit)
case _ => Seq(createThisNode(callee, NewIdentifier()))
Expand All @@ -89,7 +89,7 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) {
else
callee.getName

val calleeType = registerType(callee.getDeclaringClass.getType.toQuotedString)
val calleeType = registerType(sootTypeToString(callee.getDeclaringClass.getType))
val callType =
if (callee.isConstructor) "void"
else calleeType
Expand Down Expand Up @@ -134,7 +134,7 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) {
case u: NewMultiArrayExpr =>
astForArrayCreateExpr(x, u.getSizes.asScala, parentUnit)
case _ =>
val parentType = registerType(x.getType.toQuotedString)
val parentType = registerType(sootTypeToString(x.getType))
Ast(
NewCall()
.name(Operators.alloc)
Expand All @@ -151,7 +151,7 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) {
private def astForArrayCreateExpr(arrayInitExpr: Expr, sizes: Iterable[Value], parentUnit: soot.Unit): Ast = {
// Jimple does not have Operators.arrayInitializer
// to enforce 3 address code form
val arrayBaseType = registerType(arrayInitExpr.getType.toQuotedString)
val arrayBaseType = registerType(sootTypeToString(arrayInitExpr.getType))
val code = s"new ${arrayBaseType.substring(0, arrayBaseType.indexOf('['))}${sizes.map(s => s"[$s]").mkString}"
val callBlock = NewCall()
.name(Operators.alloc)
Expand All @@ -172,7 +172,7 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) {
.methodFullName(methodName)
.code(unaryExpr.toString())
.dispatchType(DispatchTypes.STATIC_DISPATCH)
.typeFullName(registerType(unaryExpr.getType.toQuotedString))
.typeFullName(registerType(sootTypeToString(unaryExpr.getType)))
.lineNumber(line(parentUnit))
.columnNumber(column(parentUnit))

Expand All @@ -190,10 +190,10 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) {

val valueAsts = unaryExpr match {
case instanceOfExpr: InstanceOfExpr =>
val t = registerType(instanceOfExpr.getCheckType.toQuotedString)
val t = registerType(sootTypeToString(instanceOfExpr.getCheckType))
astsForValue(op, parentUnit) ++ astForTypeRef(t)
case castExpr: CastExpr =>
val t = registerType(castExpr.getCastType.toQuotedString)
val t = registerType(sootTypeToString(castExpr.getCastType))
astForTypeRef(t) ++ astsForValue(op, parentUnit)
case _ => astsForValue(op, parentUnit)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ trait AstForStatementsCreator(implicit withSchemaValidation: ValidationMode) { t
assignStmt,
Operators.assignment,
s"$lhsCode = $rhsCode",
Option(registerType(leftOp.getType.toQuotedString))
Option(registerType(leftOp.getType.toString))
)
Seq(callAst(assignment, identifier ++ initAsts))
}
Expand Down
Loading
Loading