Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for ast out edge for init methods #162

Merged
merged 3 commits into from
Dec 31, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@ import io.joern.x2cpg.utils.NodeBuilders.newBindingNode
import io.joern.x2cpg.utils.NodeBuilders.newIdentifierNode
import io.joern.x2cpg.utils.NodeBuilders.newMethodReturnNode
import io.joern.x2cpg.utils.NodeBuilders.newModifierNode
import io.shiftleft.codepropertygraph.generated.DispatchTypes
import io.shiftleft.codepropertygraph.generated.EdgeTypes
import io.shiftleft.codepropertygraph.generated.Operators
import io.shiftleft.codepropertygraph.generated.{DispatchTypes, EdgeTypes, ModifierTypes, NodeTypes, Operators}
import io.shiftleft.codepropertygraph.generated.nodes.NewBlock
import io.shiftleft.codepropertygraph.generated.nodes.NewCall
import io.shiftleft.codepropertygraph.generated.nodes.NewMethod
import io.shiftleft.codepropertygraph.generated.nodes.NewTypeDecl
import io.shiftleft.codepropertygraph.generated.ModifierTypes
import io.shiftleft.semanticcpg.language.*
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.psi.*
Expand Down Expand Up @@ -188,7 +185,7 @@ trait AstForDeclarationsCreator(implicit withSchemaValidation: ValidationMode) {
val modifiers = if (isAbstract(ktClass)) List(Ast(NodeBuilders.newModifierNode(ModifierTypes.ABSTRACT))) else Nil

val children = methodAsts ++ List(constructorAst) ++ membersFromPrimaryCtorAsts ++ secondaryConstructorAsts ++
_componentNMethodAsts.toList ++ memberAsts ++ annotationAsts ++ modifiers
_componentNMethodAsts.toList ++ memberAsts ++ annotationAsts ++ modifiers ++ innerTypeDeclAsts.toSeq
val ast = Ast(typeDecl).withChildren(children)

(List(ctorBindingInfo) ++ bindingsInfo ++ componentNBindingsInfo).foreach(bindingInfoQueue.prepend)
Expand All @@ -214,7 +211,7 @@ trait AstForDeclarationsCreator(implicit withSchemaValidation: ValidationMode) {
methodAstParentStack.pop()
scope.popScope()

Seq(finalAst.withChildren(annotations.map(astForAnnotationEntry))) ++ companionObjectAsts ++ innerTypeDeclAsts
Seq(finalAst.withChildren(annotations.map(astForAnnotationEntry))) ++ companionObjectAsts
}

private def memberSetCallAst(param: KtParameter, classFullName: String)(implicit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ import io.shiftleft.codepropertygraph.generated.nodes.{
FieldIdentifier,
Identifier,
Method,
MethodParameterIn
MethodParameterIn,
MethodReturn,
StoredNode
}
import io.shiftleft.codepropertygraph.generated.DispatchTypes
import io.shiftleft.semanticcpg.language.*
import io.shiftleft.semanticcpg.language.types.structure.FileTraversal
import io.joern.dataflowengineoss.language._

class TypeDeclTests extends KotlinCode2CpgFixture(withOssDataflow = false) {
class TypeDeclTests extends KotlinCode2CpgFixture(withOssDataflow = true) {
"CPG for code with class declaration using unresolved types which are available in imports" should {
val cpg = code("""
|package no.such.pkg
Expand Down Expand Up @@ -198,7 +201,7 @@ class TypeDeclTests extends KotlinCode2CpgFixture(withOssDataflow = false) {
val cpg = code("""
|package mypkg
|
|import java.lang.Object
|import java.lang.x
|
|class Foo: Object {
| val z: Int = 1
Expand Down Expand Up @@ -451,4 +454,43 @@ class TypeDeclTests extends KotlinCode2CpgFixture(withOssDataflow = false) {
firstCallOfSecondaryCtor.methodFullName shouldBe "mypkg.QClass.<init>:void()"
}
}
"CPG for code with an anonymous object and an inner class nested inside" should {
val cpg = code("""
|package au.gov.health.covidsafe.bluetooth.gatt
|class GattServer constructor(val context: Context, serviceUUIDString: String) {
| private val gattServerCallback = object : BluetoothGattServerCallback() {
| inner class ReadRequestEncryptedPayload(val timestamp: Long, val modelP: String, val msg: String)
|}}
|""".stripMargin)

"have correct ast structure for anonymous object" in {
val List(anonymousObjTypeDecl) = cpg.typeDecl("anonymous_obj").l

anonymousObjTypeDecl.name shouldBe "anonymous_obj"
anonymousObjTypeDecl.fullName shouldBe "au.gov.health.covidsafe.bluetooth.gatt.GattServer.gattServerCallback$object$1"

val constructorMethod = anonymousObjTypeDecl.astChildren.isMethod.l
constructorMethod.fullName.l shouldBe List(
"au.gov.health.covidsafe.bluetooth.gatt.GattServer.gattServerCallback$object$1.<init>:void()"
)
constructorMethod.ast.isParameter.size shouldBe 1
constructorMethod.ast.count(_.isInstanceOf[MethodReturn]) shouldBe 1

}

"have correct ast structure for the inner class" in {
val List(innerClassTypeDecl) = cpg.typeDecl("ReadRequestEncryptedPayload").l

innerClassTypeDecl.name shouldBe "ReadRequestEncryptedPayload"
innerClassTypeDecl.fullName shouldBe "au.gov.health.covidsafe.bluetooth.gatt.GattServer.gattServerCallback$object$1.ReadRequestEncryptedPayload"

val constructorMethod = innerClassTypeDecl.astChildren.isMethod.l
constructorMethod.fullName.l shouldBe List(
"au.gov.health.covidsafe.bluetooth.gatt.GattServer.gattServerCallback.<init>:void(long,java.lang.String,java.lang.String)"
)
constructorMethod.ast.isParameter.size shouldBe 4
constructorMethod.ast.count(_.isInstanceOf[MethodReturn]) shouldBe 1

}
}
}
Loading