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

[ruby] Make <body> Call Static Dispatch #4726

Merged
merged 2 commits into from
Jul 3, 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 @@ -171,7 +171,7 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) {
Ast(typeRefNode(node, code(node), node.typeFullName))
}

protected def astForMemberCall(node: MemberCall): Ast = {
protected def astForMemberCall(node: MemberCall, isStatic: Boolean = false): Ast = {

def createMemberCall(n: MemberCall): Ast = {
val baseAst = astForExpression(n.target) // this wil be something like self.Foo
Expand All @@ -195,11 +195,15 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) {
}
.getOrElse(XDefines.Any -> XDefines.DynamicCallUnknownFullName)
val argumentAsts = n.arguments.map(astForMethodCallArgument)
val dispatchType = DispatchTypes.DYNAMIC_DISPATCH
val dispatchType = if (isStatic) DispatchTypes.STATIC_DISPATCH else DispatchTypes.DYNAMIC_DISPATCH

val call = callNode(n, code(n), n.methodName, XDefines.DynamicCallUnknownFullName, dispatchType)
if methodFullName != XDefines.DynamicCallUnknownFullName then call.possibleTypes(Seq(methodFullName))
callAst(call, argumentAsts, base = Option(baseAst), receiver = Option(receiverAst))
if (isStatic) {
callAst(call, argumentAsts, base = Option(baseAst)).copy(receiverEdges = Nil)
} else {
callAst(call, argumentAsts, base = Option(baseAst), receiver = Option(receiverAst))
}
}

def determineMemberAccessBase(target: RubyNode): RubyNode = target match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,22 @@ trait AstForTypesCreator(implicit withSchemaValidation: ValidationMode) { this:
.withChildren(fieldSingletonMemberNodes.map(_._2))
val bodyMemberCallAst =
node.bodyMemberCall match {
case Some(bodyMemberCall) => astForMemberCall(bodyMemberCall)
case Some(bodyMemberCall) => astForTypeDeclBodyCall(bodyMemberCall, classFullName)
case None => Ast()
}

(typeDeclAst :: singletonTypeDeclAst :: Nil).foreach(Ast.storeInDiffGraph(_, diffGraph))
prefixAst :: bodyMemberCallAst :: Nil
}

private def astForTypeDeclBodyCall(node: TypeDeclBodyCall, typeFullName: String): Ast = {
val callAst = astForMemberCall(node.toMemberCall, isStatic = true)
callAst.nodes.collectFirst {
case c: NewCall if c.name == Defines.TypeDeclBody => c.methodFullName(s"$typeFullName:${Defines.TypeDeclBody}")
}
callAst
}

private def createTypeRefPointer(typeDecl: NewTypeDecl): Ast = {
if (scope.isSurroundedByProgramScope) {
// We aim to preserve whether it's a `class` or `module` in the `code` property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ object RubyIntermediateAst {
def name: RubyNode
def baseClass: Option[RubyNode]
def body: RubyNode
def bodyMemberCall: Option[MemberCall]
def bodyMemberCall: Option[TypeDeclBodyCall]
}

final case class ModuleDeclaration(
name: RubyNode,
body: RubyNode,
fields: List[RubyNode & RubyFieldIdentifier],
bodyMemberCall: Option[MemberCall]
bodyMemberCall: Option[TypeDeclBodyCall]
)(span: TextSpan)
extends RubyNode(span)
with TypeDeclaration {
Expand All @@ -71,7 +71,7 @@ object RubyIntermediateAst {
baseClass: Option[RubyNode],
body: RubyNode,
fields: List[RubyNode & RubyFieldIdentifier],
bodyMemberCall: Option[MemberCall]
bodyMemberCall: Option[TypeDeclBodyCall]
)(span: TextSpan)
extends RubyNode(span)
with TypeDeclaration
Expand All @@ -82,7 +82,7 @@ object RubyIntermediateAst {
name: RubyNode,
baseClass: Option[RubyNode],
body: RubyNode,
bodyMemberCall: Option[MemberCall] = None
bodyMemberCall: Option[TypeDeclBodyCall] = None
)(span: TextSpan)
extends RubyNode(span)
with AnonymousTypeDeclaration
Expand All @@ -91,7 +91,7 @@ object RubyIntermediateAst {
name: RubyNode,
baseClass: Option[RubyNode],
body: RubyNode,
bodyMemberCall: Option[MemberCall] = None
bodyMemberCall: Option[TypeDeclBodyCall] = None
)(span: TextSpan)
extends RubyNode(span)
with AnonymousTypeDeclaration
Expand Down Expand Up @@ -368,6 +368,19 @@ object RubyIntermediateAst {
) extends RubyNode(span)
with RubyCall

/** Special class for `<body>` calls of type decls.
*/
final case class TypeDeclBodyCall(target: RubyNode, typeName: String)(span: TextSpan)
extends RubyNode(span)
with RubyCall {

def toMemberCall: MemberCall = MemberCall(target, op, Defines.TypeDeclBody, arguments)(span)

def arguments: List[RubyNode] = Nil

def op: String = "::"
}

final case class MemberCallWithBlock(
target: RubyNode,
op: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1058,15 +1058,13 @@ class RubyNodeCreator extends RubyParserBaseVisitor[RubyNode] {
)(ctx.toTextSpan)
}

private def createBodyMemberCall(name: String, textSpan: TextSpan): MemberCall = {
MemberCall(
private def createBodyMemberCall(name: String, textSpan: TextSpan): TypeDeclBodyCall = {
TypeDeclBodyCall(
MemberAccess(SelfIdentifier()(textSpan.spanStart(Defines.Self)), "::", name)(
textSpan.spanStart(s"${Defines.Self}::$name")
),
"::",
Defines.TypeDeclBody,
List.empty
)(textSpan.spanStart(s"${Defines.Self}::$name::<body>"))
name
)(textSpan.spanStart(s"${Defines.Self}::$name::${Defines.TypeDeclBody}"))
}

/** Lowers all MethodDeclaration found in SingletonClassDeclaration to SingletonMethodDeclaration.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package io.joern.rubysrc2cpg.querying
import io.joern.rubysrc2cpg.passes.{GlobalTypes, Defines as RubyDefines}
import io.joern.rubysrc2cpg.testfixtures.RubyCode2CpgFixture
import io.joern.x2cpg.Defines
import io.shiftleft.codepropertygraph.generated.Operators
import io.shiftleft.codepropertygraph.generated.{DispatchTypes, Operators}
import io.shiftleft.codepropertygraph.generated.nodes.*
import io.shiftleft.semanticcpg.language.*

Expand Down Expand Up @@ -578,6 +578,23 @@ class ClassTests extends RubyCode2CpgFixture {
case xs => fail(s"Expected TypeDecl for Foo, instead got ${xs.name.mkString(", ")}")
}
}

"call the body method" in {
inside(cpg.call.nameExact(RubyDefines.TypeDeclBody).headOption) {
case Some(bodyCall) =>
bodyCall.dispatchType shouldBe DispatchTypes.STATIC_DISPATCH
bodyCall.methodFullName shouldBe s"Test0.rb:<global>::program.Foo:${RubyDefines.TypeDeclBody}"

bodyCall.receiver.isEmpty shouldBe true
inside(bodyCall.argumentOption(0)) {
case Some(selfArg: Call) =>
selfArg.name shouldBe Operators.fieldAccess
selfArg.code shouldBe "self::Foo"
case None => fail("Expected `self` argument")
}
case None => fail("Expected <body> call")
}
}
}

"Class Variables in Class and Methods" should {
Expand Down
Loading