Skip to content

Commit

Permalink
Added label/goto statements
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Jan 2, 2024
1 parent 281675f commit a83714b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ import de.fraunhofer.aisec.cpg.graph.types.Type
import de.fraunhofer.aisec.cpg.sarif.PhysicalLocation
import java.io.File
import sootup.core.inputlocation.AnalysisInputLocation
import sootup.core.model.Body
import sootup.core.model.SootMethod
import sootup.core.model.SourceType
import sootup.core.types.ArrayType
import sootup.core.types.UnknownType
import sootup.core.util.printer.NormalStmtPrinter
import sootup.java.bytecode.inputlocation.JavaClassPathAnalysisInputLocation
import sootup.java.bytecode.interceptors.*
import sootup.java.core.JavaSootClass
Expand All @@ -55,6 +57,12 @@ class JVMLanguageFrontend(
val statementHandler = StatementHandler(this)
val expressionHandler = ExpressionHandler(this)

lateinit var view: JavaView

var body: Body? = null

var printer: NormalStmtPrinter? = null

/**
* Because of a limitation in SootUp, we can only specify the whole classpath for soot to parse.
* But in the CPG we need to specify one file. In this case, we take the
Expand All @@ -76,31 +84,35 @@ class JVMLanguageFrontend(
val project = JavaProject.builder(language).addInputLocation(inputLocation).build()
project.createView()
}*/
val view =
if (file.extension == "class") {
val inputLocation: AnalysisInputLocation<JavaSootClass> =
JavaClassPathAnalysisInputLocation(
ctx.config.topLevel!!.path,
SourceType.Library,
listOf(
NopEliminator(),
CastAndReturnInliner(),
UnreachableCodeEliminator(),
Aggregator(),
CopyPropagator(),
// ConditionalBranchFolder(),
EmptySwitchEliminator(),
TypeAssigner(),
LocalNameStandardizer()
view =
when (file.extension) {
"class" -> {
val inputLocation: AnalysisInputLocation<JavaSootClass> =
JavaClassPathAnalysisInputLocation(
ctx.config.topLevel!!.path,
SourceType.Library,
listOf(
NopEliminator(),
CastAndReturnInliner(),
UnreachableCodeEliminator(),
Aggregator(),
CopyPropagator(),
// ConditionalBranchFolder(),
EmptySwitchEliminator(),
TypeAssigner(),
LocalNameStandardizer()
)
)
)
JavaView(inputLocation)
} else if (file.extension == "java") {
val inputLocation: AnalysisInputLocation<JavaSootClass> =
JavaSourcePathAnalysisInputLocation(ctx.config.topLevel!!.path)
JavaView(inputLocation)
} else {
throw TranslationException("unsupported file")
JavaView(inputLocation)
}
"java" -> {
val inputLocation: AnalysisInputLocation<JavaSootClass> =
JavaSourcePathAnalysisInputLocation(ctx.config.topLevel!!.path)
JavaView(inputLocation)
}
else -> {
throw TranslationException("unsupported file")
}
}

// This contains the whole directory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@
package de.fraunhofer.aisec.cpg.frontends

import de.fraunhofer.aisec.cpg.graph.*
import de.fraunhofer.aisec.cpg.graph.builder.label
import de.fraunhofer.aisec.cpg.graph.statements.GotoStatement
import de.fraunhofer.aisec.cpg.graph.statements.IfStatement
import de.fraunhofer.aisec.cpg.graph.statements.ReturnStatement
import de.fraunhofer.aisec.cpg.graph.statements.Statement
import de.fraunhofer.aisec.cpg.graph.statements.expressions.*
import sootup.core.jimple.common.stmt.*
import sootup.core.model.Body
import sootup.core.util.printer.NormalStmtPrinter

class StatementHandler(frontend: JVMLanguageFrontend) :
Handler<Statement, Any, JVMLanguageFrontend>(::ProblemExpression, frontend) {
Expand All @@ -50,6 +52,12 @@ class StatementHandler(frontend: JVMLanguageFrontend) :
private fun handleBody(body: Body): Block {
val block = newBlock(rawNode = body)

val printer = NormalStmtPrinter()
printer.initializeSootMethod(body.stmtGraph)

frontend.printer = printer
frontend.body = body

// Parse locals, these are always at the beginning of the function
for (local in body.locals) {
val decl = frontend.declarationHandler.handle(local)
Expand All @@ -65,6 +73,13 @@ class StatementHandler(frontend: JVMLanguageFrontend) :

// Parse statements
for (sootStmt in body.stmts) {
val label = printer.labels[sootStmt]
if (label != null) {
val stmt = newLabelStatement()
stmt.label = label
block += stmt
}

handle(sootStmt)?.let { block += it }
}

Expand All @@ -85,15 +100,30 @@ class StatementHandler(frontend: JVMLanguageFrontend) :
frontend.expressionHandler.handle(ifStmt.condition)
?: newProblemExpression("missing condition")

// TODO: parse statements
// TODO: insert basic block instead?
frontend.body?.let {
val target = ifStmt.getTargetStmts(it).firstOrNull()
val label = frontend.printer?.labels?.get(target)
if (label != null) {
val goto = newGotoStatement()
goto.labelName = label
stmt.thenStatement = goto
}
}

return stmt
}

private fun handleGotoStmt(gotoStmt: JGotoStmt): GotoStatement {
val stmt = newGotoStatement(rawNode = gotoStmt)

// TODO: parse statements
frontend.body?.let {
val target = gotoStmt.getTargetStmts(it).firstOrNull()
val label = frontend.printer?.labels?.get(target)
if (label != null) {
stmt.labelName = label
}
}

return stmt
}
Expand Down

0 comments on commit a83714b

Please sign in to comment.