Skip to content
Open
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 @@ -12,6 +12,7 @@ import io.shiftleft.codepropertygraph.generated.nodes.{NewCall, NewIdentifier, N
import org.slf4j.LoggerFactory
import io.shiftleft.codepropertygraph.generated.DiffGraphBuilder

import scala.annotation.tailrec
import scala.collection.mutable

object MethodParameters {
Expand Down Expand Up @@ -1836,6 +1837,18 @@ class PythonAstVisitor(
("and", Operators.logicalAnd)
}

@tailrec
private def mayHaveSideEffects(expr: ast.iexpr): Boolean = {
expr match {
case attr: ast.Attribute =>
mayHaveSideEffects(attr.value)
case attr: ast.Name =>
false
case _ =>
true
}
}

/** TODO For now this function compromises on the correctness of the lowering in order to get some data flow tracking
* going.
* 1. For constructs like x.func() we assume x to be the instance which is passed into func. This is not true since
Expand All @@ -1862,7 +1875,7 @@ class PythonAstVisitor(
createXDotYCall(
() => convert(attribute.value),
attribute.attr,
xMayHaveSideEffects = !attribute.value.isInstanceOf[ast.Name],
xMayHaveSideEffects = mayHaveSideEffects(attribute.value),
lineAndColOf(call),
argumentNodes,
keywordArgNodes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import io.shiftleft.semanticcpg.language.*
import java.io.File

class CallCpgTests extends PySrc2CpgFixture(withOssDataflow = false) {

"call on identifier" should {
lazy val cpg = code("""func(a, b)""".stripMargin, "test.py")

Expand Down Expand Up @@ -173,6 +172,48 @@ class CallCpgTests extends PySrc2CpgFixture(withOssDataflow = false) {
}
}

"call on member chain" should {
lazy val cpg = code("""x.y.func(a, b)""".stripMargin, "test.py")

"test call node properties" in {
val callNode = cpg.call.codeExact("x.y.func(a, b)").head
callNode.name shouldBe "func"
callNode.signature shouldBe ""
callNode.dispatchType shouldBe DispatchTypes.DYNAMIC_DISPATCH
callNode.lineNumber shouldBe Some(1)
}

"test call receiver" in {
val callNode = cpg.call.codeExact("x.y.func(a, b)").head
val callReceiver = callNode.receiver.head
callReceiver.code shouldBe "x.y.func"

callNode.astChildren.order(0).head shouldBe callReceiver
callNode.start.argument.b should not contain callReceiver
}

"test call instance param" in {
val callNode = cpg.call.codeExact("x.y.func(a, b)").head
val instanceArg = callNode.argument(0)
instanceArg.code shouldBe "x.y"

callNode.astChildren.order(1).head shouldBe instanceArg
}

"test call arguments" in {
val callNode = cpg.call.codeExact("x.y.func(a, b)").head
val arg1 = callNode.argument(1)
arg1.code shouldBe "a"

callNode.astChildren.order(2).head shouldBe arg1

val arg2 = callNode.argument(2)
arg2.code shouldBe "b"

callNode.astChildren.order(3).head shouldBe arg2
}
}

"call following a definition within the same module" should {
lazy val cpg = code(
"""
Expand Down
Loading