Skip to content

Commit daa68e3

Browse files
fabsx00Fabian Yamaguchi
andauthored
Make flows via statically dispatched methods work (#2810)
* Make flows via statically dispatched methods work * Fixed IdentifierTests --------- Co-authored-by: Fabian Yamaguchi <[email protected]>
1 parent e2542f3 commit daa68e3

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

joern-cli/frontends/rubysrc2cpg/src/main/scala/io/joern/rubysrc2cpg/astcreation/AstCreator.scala

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,12 +1192,13 @@ class AstCreator(filename: String, global: Global)
11921192
}
11931193

11941194
def astForCallNode(localIdentifier: TerminalNode): Seq[Ast] = {
1195-
val column = localIdentifier.getSymbol().getCharPositionInLine()
1196-
val line = localIdentifier.getSymbol().getLine()
1197-
val name = getActualMethodName(localIdentifier.getText)
1195+
val column = localIdentifier.getSymbol().getCharPositionInLine()
1196+
val line = localIdentifier.getSymbol().getLine()
1197+
val name = getActualMethodName(localIdentifier.getText)
1198+
val methodFullName = s"$filename:$name"
11981199
val callNode = NewCall()
11991200
.name(name)
1200-
.methodFullName(name)
1201+
.methodFullName(methodFullName)
12011202
.signature(localIdentifier.getText())
12021203
.typeFullName(MethodFullNames.UnknownFullName)
12031204
.dispatchType(DispatchTypes.STATIC_DISPATCH)
@@ -1401,11 +1402,13 @@ class AstCreator(filename: String, global: Global)
14011402
val astBody = astForBodyStatementContext(ctx.bodyStatement())
14021403
popScope()
14031404

1405+
// TODO why is there a `callNode` here?
1406+
14041407
val classPath = classStack.toList.mkString(".") + "."
14051408
val methodNode = NewMethod()
14061409
.code(callNode.code)
14071410
.name(callNode.name)
1408-
.fullName(classPath + callNode.name)
1411+
.fullName(s"$filename:${callNode.name}")
14091412
.columnNumber(callNode.columnNumber)
14101413
.lineNumber(callNode.lineNumber)
14111414
.filename(filename)

joern-cli/frontends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/dataflow/DataFlowTests.scala

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,25 @@ class DataFlowTests extends DataFlowCodeToCpgSuite {
3131
sink.reachableByFlows(source).l.size shouldBe 2
3232
}
3333
}
34+
35+
"Flow via call" should {
36+
val cpg = code("""
37+
|def print(content)
38+
|puts content
39+
|end
40+
|
41+
|def main
42+
|n = 1
43+
|print( n )
44+
|end
45+
|""".stripMargin)
46+
47+
"be found" in {
48+
implicit val resolver: ICallResolver = NoResolve
49+
val src = cpg.identifier.name("n").where(_.inCall.name("print")).l
50+
val sink = cpg.method.name("puts").callIn.argument(1).l
51+
sink.reachableByFlows(src).size shouldBe 1
52+
}
53+
}
54+
3455
}

joern-cli/frontends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/querying/CallGraphTests.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.joern.rubysrc2cpg.querying
22

33
import io.joern.rubysrc2cpg.testfixtures.RubyCode2CpgFixture
4+
import io.shiftleft.codepropertygraph.generated.nodes.Method
45
import io.shiftleft.semanticcpg.language._
56

67
class CallGraphTests extends RubyCode2CpgFixture {
@@ -18,8 +19,11 @@ class CallGraphTests extends RubyCode2CpgFixture {
1819
"should identify call from `foo` to `bar`" in {
1920
val List(callToBar) = cpg.call("bar").l
2021
callToBar.name shouldBe "bar"
22+
callToBar.methodFullName.matches(".*Test.*.rb:bar") shouldBe true
2123
callToBar.lineNumber shouldBe Some(7)
22-
cpg.method("bar").caller.name.l shouldBe List("foo")
24+
val List(bar: Method) = cpg.method("bar").internal.l
25+
bar.fullName shouldBe callToBar.methodFullName
26+
bar.caller.name.l shouldBe List("foo")
2327
}
2428

2529
}

joern-cli/frontends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/querying/IdentifierTests.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class IdentifierTests extends RubyCode2CpgFixture {
6767
cpg.method.name("initialize").size shouldBe 1
6868
cpg.method
6969
.name("greet")
70-
.size shouldBe 2 // FIXME the second node is coming in without adding it. Need to check this
70+
.size shouldBe 1
7171
cpg.call.name("puts").size shouldBe 2
7272
cpg.method.name("have_birthday").size shouldBe 1
7373
cpg.identifier.size shouldBe 11
@@ -468,7 +468,7 @@ class IdentifierTests extends RubyCode2CpgFixture {
468468
|""".stripMargin)
469469

470470
"recognise all method nodes" in {
471-
cpg.method.name("yield_with_args_method").l.size shouldBe 2
471+
cpg.method.name("yield_with_args_method").l.size shouldBe 1
472472
// TODO need to figure out how yield block should be connected to the method
473473
}
474474

0 commit comments

Comments
 (0)