Skip to content

Commit

Permalink
Added instanceof operator
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Jan 6, 2024
1 parent 02d7dfe commit 4d4b7c8
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, Fraunhofer AISEC. All rights reserved.
* Copyright (c) 2024, Fraunhofer AISEC. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,8 +23,9 @@
* \______/ \__| \______/
*
*/
package de.fraunhofer.aisec.cpg.frontends
package de.fraunhofer.aisec.cpg.frontends.jvm

import de.fraunhofer.aisec.cpg.frontends.Handler
import de.fraunhofer.aisec.cpg.graph.*
import de.fraunhofer.aisec.cpg.graph.declarations.*
import sootup.core.jimple.basic.Local
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, Fraunhofer AISEC. All rights reserved.
* Copyright (c) 2024, Fraunhofer AISEC. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,12 +23,12 @@
* \______/ \__| \______/
*
*/
package de.fraunhofer.aisec.cpg.frontends
package de.fraunhofer.aisec.cpg.frontends.jvm

import de.fraunhofer.aisec.cpg.frontends.Handler
import de.fraunhofer.aisec.cpg.graph.*
import de.fraunhofer.aisec.cpg.graph.statements.expressions.*
import de.fraunhofer.aisec.cpg.graph.types.FunctionType
import de.fraunhofer.aisec.cpg.passes.SymbolResolver
import sootup.core.jimple.basic.Local
import sootup.core.jimple.basic.Value
import sootup.core.jimple.common.constant.*
Expand Down Expand Up @@ -99,6 +99,9 @@ class ExpressionHandler(frontend: JVMLanguageFrontend) :
// Unary operator
map.put(JNegExpr::class.java) { handleNegExpr(it as JNegExpr) }

// Special operators, which we need to model as binary operators
map.put(JInstanceOfExpr::class.java) { handleInstanceOfExpr(it as JInstanceOfExpr) }

// Constants
map.put(BooleanConstant::class.java) { handleBooleanConstant(it as BooleanConstant) }
map.put(FloatConstant::class.java) { handleFloatConstant(it as FloatConstant) }
Expand Down Expand Up @@ -276,6 +279,16 @@ class ExpressionHandler(frontend: JVMLanguageFrontend) :
return op
}

private fun handleInstanceOfExpr(instanceOfExpr: JInstanceOfExpr): BinaryOperator {
val op = newBinaryOperator("instanceof", rawNode = instanceOfExpr)
op.lhs = handle(instanceOfExpr.op) ?: newProblemExpression("missing lhs")

val type = frontend.typeOf(instanceOfExpr.type)
op.rhs = newTypeExpression(type.name, type, rawNode = type)

return op
}

private fun handleBooleanConstant(constant: BooleanConstant) =
newLiteral(
constant.equalEqual(BooleanConstant.getTrue()),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, Fraunhofer AISEC. All rights reserved.
* Copyright (c) 2024, Fraunhofer AISEC. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,8 +23,9 @@
* \______/ \__| \______/
*
*/
package de.fraunhofer.aisec.cpg.frontends
package de.fraunhofer.aisec.cpg.frontends.jvm

import de.fraunhofer.aisec.cpg.frontends.Language
import de.fraunhofer.aisec.cpg.graph.types.*
import kotlin.reflect.KClass

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, Fraunhofer AISEC. All rights reserved.
* Copyright (c) 2024, Fraunhofer AISEC. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,10 +23,12 @@
* \______/ \__| \______/
*
*/
package de.fraunhofer.aisec.cpg.frontends
package de.fraunhofer.aisec.cpg.frontends.jvm

import de.fraunhofer.aisec.cpg.TranslationConfiguration
import de.fraunhofer.aisec.cpg.TranslationContext
import de.fraunhofer.aisec.cpg.frontends.Language
import de.fraunhofer.aisec.cpg.frontends.LanguageFrontend
import de.fraunhofer.aisec.cpg.frontends.TranslationException
import de.fraunhofer.aisec.cpg.graph.*
import de.fraunhofer.aisec.cpg.graph.declarations.NamespaceDeclaration
import de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, Fraunhofer AISEC. All rights reserved.
* Copyright (c) 2024, Fraunhofer AISEC. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,15 +23,17 @@
* \______/ \__| \______/
*
*/
package de.fraunhofer.aisec.cpg.frontends
package de.fraunhofer.aisec.cpg.frontends.jvm

import de.fraunhofer.aisec.cpg.frontends.Handler
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 de.fraunhofer.aisec.cpg.graph.statements.expressions.AssignExpression
import de.fraunhofer.aisec.cpg.graph.statements.expressions.Block
import de.fraunhofer.aisec.cpg.graph.statements.expressions.ProblemExpression
import sootup.core.jimple.common.stmt.*
import sootup.core.model.Body
import sootup.core.util.printer.NormalStmtPrinter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* \______/ \__| \______/
*
*/
package de.fraunhofer.aisec.cpg.frontends
package de.fraunhofer.aisec.cpg.frontends.jvm

import de.fraunhofer.aisec.cpg.*
import de.fraunhofer.aisec.cpg.TestUtils.assertInvokes
Expand Down Expand Up @@ -243,7 +243,7 @@ class JVMLanguageFrontendTest {
assertLocalName("ExtendedClass", doSomethingCall1.arguments.firstOrNull()?.type)
assertInvokes(doSomethingCall1, appDoSomething)

val extended = appInit.variables["\$r3"]
val extended = appInit.variables["\$r4"]
assertNotNull(extended)

val getMyProperty =
Expand All @@ -254,6 +254,7 @@ class JVMLanguageFrontendTest {
it.base in extended.usages
}]
assertNotNull(getMyProperty)
assertInvokes(getMyProperty, baseClass.methods["getMyProperty"])

val setMyProperty =
appInit.calls[
Expand All @@ -263,6 +264,7 @@ class JVMLanguageFrontendTest {
it.base in extended.usages
}]
assertNotNull(setMyProperty)
assertInvokes(setMyProperty, extendedClass.methods["setMyProperty"])
}

@Test
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public Application() {
base = new AnotherExtendedClass();
}
base.setMyProperty(10);

if(base instanceof ExtendedClass) {
System.out.println("Is extended!");
}
}

public void doSomething(MyInterface i) {
Expand Down

0 comments on commit 4d4b7c8

Please sign in to comment.