Skip to content

Commit

Permalink
fix for ast out edge for init methods
Browse files Browse the repository at this point in the history
  • Loading branch information
karan-batavia committed Dec 20, 2024
1 parent 6b283cd commit 9e3b86a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
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.*
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

}
}
}

0 comments on commit 9e3b86a

Please sign in to comment.