diff --git a/cpg-language-jvm/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/DeclarationHandler.kt b/cpg-language-jvm/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/jvm/DeclarationHandler.kt similarity index 97% rename from cpg-language-jvm/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/DeclarationHandler.kt rename to cpg-language-jvm/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/jvm/DeclarationHandler.kt index 90f41d75a53..b11cfa5f995 100644 --- a/cpg-language-jvm/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/DeclarationHandler.kt +++ b/cpg-language-jvm/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/jvm/DeclarationHandler.kt @@ -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. @@ -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 diff --git a/cpg-language-jvm/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/ExpressionHandler.kt b/cpg-language-jvm/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/jvm/ExpressionHandler.kt similarity index 95% rename from cpg-language-jvm/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/ExpressionHandler.kt rename to cpg-language-jvm/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/jvm/ExpressionHandler.kt index 1c5d526584f..34dad13438d 100644 --- a/cpg-language-jvm/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/ExpressionHandler.kt +++ b/cpg-language-jvm/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/jvm/ExpressionHandler.kt @@ -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. @@ -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.* @@ -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) } @@ -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()), diff --git a/cpg-language-jvm/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/JVMLanguage.kt b/cpg-language-jvm/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/jvm/JVMLanguage.kt similarity index 93% rename from cpg-language-jvm/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/JVMLanguage.kt rename to cpg-language-jvm/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/jvm/JVMLanguage.kt index 5bbfca828ca..ec50e0c1966 100644 --- a/cpg-language-jvm/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/JVMLanguage.kt +++ b/cpg-language-jvm/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/jvm/JVMLanguage.kt @@ -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. @@ -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 diff --git a/cpg-language-jvm/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/JVMLanguageFrontend.kt b/cpg-language-jvm/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/jvm/JVMLanguageFrontend.kt similarity index 96% rename from cpg-language-jvm/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/JVMLanguageFrontend.kt rename to cpg-language-jvm/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/jvm/JVMLanguageFrontend.kt index a0277dc84d0..a0924abefe7 100644 --- a/cpg-language-jvm/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/JVMLanguageFrontend.kt +++ b/cpg-language-jvm/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/jvm/JVMLanguageFrontend.kt @@ -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. @@ -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 diff --git a/cpg-language-jvm/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/StatementHandler.kt b/cpg-language-jvm/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/jvm/StatementHandler.kt similarity index 93% rename from cpg-language-jvm/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/StatementHandler.kt rename to cpg-language-jvm/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/jvm/StatementHandler.kt index d71a7eadfb2..a3cb6305f53 100644 --- a/cpg-language-jvm/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/StatementHandler.kt +++ b/cpg-language-jvm/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/jvm/StatementHandler.kt @@ -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. @@ -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 diff --git a/cpg-language-jvm/src/test/kotlin/de/fraunhofer/aisec/cpg/frontends/JVMLanguageFrontendTest.kt b/cpg-language-jvm/src/test/kotlin/de/fraunhofer/aisec/cpg/frontends/jvm/JVMLanguageFrontendTest.kt similarity index 98% rename from cpg-language-jvm/src/test/kotlin/de/fraunhofer/aisec/cpg/frontends/JVMLanguageFrontendTest.kt rename to cpg-language-jvm/src/test/kotlin/de/fraunhofer/aisec/cpg/frontends/jvm/JVMLanguageFrontendTest.kt index 331a24906e3..d1b19d9f2e0 100644 --- a/cpg-language-jvm/src/test/kotlin/de/fraunhofer/aisec/cpg/frontends/JVMLanguageFrontendTest.kt +++ b/cpg-language-jvm/src/test/kotlin/de/fraunhofer/aisec/cpg/frontends/jvm/JVMLanguageFrontendTest.kt @@ -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 @@ -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 = @@ -254,6 +254,7 @@ class JVMLanguageFrontendTest { it.base in extended.usages }] assertNotNull(getMyProperty) + assertInvokes(getMyProperty, baseClass.methods["getMyProperty"]) val setMyProperty = appInit.calls[ @@ -263,6 +264,7 @@ class JVMLanguageFrontendTest { it.base in extended.usages }] assertNotNull(setMyProperty) + assertInvokes(setMyProperty, extendedClass.methods["setMyProperty"]) } @Test diff --git a/cpg-language-jvm/src/test/resources/class/inheritance/mypackage/Application.class b/cpg-language-jvm/src/test/resources/class/inheritance/mypackage/Application.class index 1f45e172e7a..0104bfda3e3 100644 Binary files a/cpg-language-jvm/src/test/resources/class/inheritance/mypackage/Application.class and b/cpg-language-jvm/src/test/resources/class/inheritance/mypackage/Application.class differ diff --git a/cpg-language-jvm/src/test/resources/class/inheritance/mypackage/Application.java b/cpg-language-jvm/src/test/resources/class/inheritance/mypackage/Application.java index 0890e26e88d..83416910826 100644 --- a/cpg-language-jvm/src/test/resources/class/inheritance/mypackage/Application.java +++ b/cpg-language-jvm/src/test/resources/class/inheritance/mypackage/Application.java @@ -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) {