-
Notifications
You must be signed in to change notification settings - Fork 296
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ruby] Implement RubySummary with CPG Lookahead (#4226)
* Following [#4196](#4196), similarly implemented a look-ahead and summary using an in-memory CPG * Testing that a parenthesis-less call is correctly created as a call instead of identifier * Resolving direct function calls if possible using the scope object * Fixed scope around new pre-parse & loaded builtins
- Loading branch information
1 parent
a69111d
commit 68a09fb
Showing
12 changed files
with
140 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 52 additions & 78 deletions
130
...tends/rubysrc2cpg/src/main/scala/io/joern/rubysrc2cpg/astcreation/AstSummaryVisitor.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,100 +1,74 @@ | ||
package io.joern.rubysrc2cpg.astcreation | ||
|
||
import io.joern.rubysrc2cpg.astcreation.RubyIntermediateAst.{ | ||
ClassDeclaration, | ||
FieldsDeclaration, | ||
MandatoryParameter, | ||
MethodDeclaration, | ||
ModuleDeclaration, | ||
OptionalParameter, | ||
RubyNode, | ||
SimpleIdentifier, | ||
StatementList, | ||
TypeDeclaration | ||
} | ||
import io.joern.rubysrc2cpg.datastructures.{ | ||
NamespaceScope, | ||
RubyField, | ||
RubyMethod, | ||
RubyProgramSummary, | ||
RubyType, | ||
TypeScope | ||
} | ||
import io.joern.rubysrc2cpg.passes.Defines | ||
import io.joern.rubysrc2cpg.astcreation.RubyIntermediateAst.StatementList | ||
import io.joern.rubysrc2cpg.datastructures.{RubyField, RubyMethod, RubyProgramSummary, RubyType} | ||
import io.joern.rubysrc2cpg.parser.RubyNodeCreator | ||
import io.joern.x2cpg.datastructures.ProgramSummary | ||
import io.joern.x2cpg.{ValidationMode, Defines as XDefines} | ||
import io.shiftleft.codepropertygraph.generated.nodes.{NewMember, NewMethod, NewMethodParameterIn} | ||
import io.shiftleft.semanticcpg.language.types.structure.NamespaceTraversal | ||
import io.joern.rubysrc2cpg.passes.Defines | ||
import io.joern.x2cpg.{Ast, ValidationMode} | ||
import io.shiftleft.codepropertygraph.generated.Cpg | ||
import io.shiftleft.codepropertygraph.generated.nodes.{Local, Member, Method, TypeDecl} | ||
import io.shiftleft.semanticcpg.language.* | ||
import overflowdb.{BatchedUpdate, Config} | ||
|
||
trait AstSummaryVisitor(implicit withSchemaValidation: ValidationMode) { this: AstCreator => | ||
import scala.util.Using | ||
|
||
private def baseNamespace: String = s"$relativeFileName:${NamespaceTraversal.globalNamespaceName}" | ||
trait AstSummaryVisitor(implicit withSchemaValidation: ValidationMode) { this: AstCreator => | ||
|
||
def summarize(): RubyProgramSummary = { | ||
val rootNode = new RubyNodeCreator().visit(this.programCtx).asInstanceOf[StatementList] | ||
val fullName = baseNamespace | ||
scope.pushNewScope(NamespaceScope(fullName)) | ||
this.parseLevel = AstParseLevel.SIGNATURES | ||
Using.resource(Cpg.withConfig(Config.withoutOverflow())) { cpg => | ||
// Build and store compilation unit AST | ||
val rootNode = new RubyNodeCreator().visit(programCtx).asInstanceOf[StatementList] | ||
val ast = astForRubyFile(rootNode) | ||
Ast.storeInDiffGraph(ast, diffGraph) | ||
BatchedUpdate.applyDiff(cpg.graph, diffGraph) | ||
|
||
val newMap = scope.newProgramScope | ||
.map { moduleScope => | ||
scope.pushNewScope(moduleScope) | ||
val m = rootNode.statements | ||
.map(visitStatement) | ||
.reduceOption((a, b) => ProgramSummary.combine(a, b)) | ||
.getOrElse(Map.empty) | ||
scope.popScope() | ||
m | ||
} | ||
.getOrElse(Map.empty) | ||
|
||
scope.popScope() | ||
RubyProgramSummary(newMap) | ||
// Summarize findings | ||
summarize(cpg) | ||
} | ||
} | ||
|
||
def withSummary(newSummary: RubyProgramSummary): AstCreator = { | ||
AstCreator(fileName, programCtx, projectRoot, newSummary) | ||
} | ||
|
||
private def visitStatement(stmt: RubyNode): Map[String, Set[RubyType]] = stmt match { | ||
case node: TypeDeclaration => visitTypeDeclaration(node) | ||
case _ => Map.empty | ||
} | ||
private def summarize(cpg: Cpg): RubyProgramSummary = { | ||
def toMethod(m: Method): RubyMethod = { | ||
RubyMethod(m.name, m.parameter.map(x => x.name -> x.typeFullName).l, m.methodReturn.typeFullName) | ||
} | ||
|
||
private def visitTypeDeclaration(classDecl: TypeDeclaration): Map[String, Set[RubyType]] = { | ||
classDecl.name match { | ||
case name: SimpleIdentifier => | ||
val fullName = computeClassFullName(name.text) | ||
Map( | ||
scope.surroundingScopeFullName | ||
.getOrElse(baseNamespace) -> Set(visitTypeLikeDeclaration(fullName, classDecl.body)) | ||
) | ||
case _ => Map.empty | ||
def toField(f: Member): RubyField = { | ||
RubyField(f.name, f.typeFullName) | ||
} | ||
} | ||
|
||
private def visitTypeLikeDeclaration(fullName: String, body: RubyNode): RubyType = { | ||
scope.pushNewScope(TypeScope(fullName)) | ||
val classBody = body.asInstanceOf[StatementList] | ||
val bodyMap = | ||
classBody.statements.flatMap { | ||
case MethodDeclaration(methodName, parameters, _) => | ||
RubyMethod(methodName, visitParameters(parameters), XDefines.Any) :: Nil | ||
case node: FieldsDeclaration => | ||
astsForFieldDeclarations(node).flatMap(_.nodes).collect { | ||
case x: NewMember => RubyField(x.name, x.typeFullName) | ||
case x: NewMethod => RubyMethod(x.name, List.empty, XDefines.Any) // These are getters/setters | ||
} | ||
case _ => Seq.empty | ||
} | ||
scope.popScope() | ||
RubyType(fullName, bodyMap.collect { case x: RubyMethod => x }, bodyMap.collect { case x: RubyField => x }) | ||
} | ||
def toModuleVariable(v: Local): RubyField = { | ||
RubyField(v.name, v.typeFullName) | ||
} | ||
|
||
private def visitParameters(parameters: List[RubyNode]): List[(String, String)] = { | ||
parameters.map(astForParameter(_, -1)).flatMap(_.root).collect { case x: NewMethodParameterIn => | ||
(x.name, x.typeFullName) | ||
def toType(m: TypeDecl): RubyType = { | ||
RubyType(m.fullName, m.method.map(toMethod).l, m.member.map(toField).l) | ||
} | ||
|
||
val mapping = cpg.namespaceBlock.flatMap { namespace => | ||
// Map module functions/variables | ||
val moduleEntry = namespace.fullName -> namespace.method.map { module => | ||
val moduleTypeMap = | ||
RubyType( | ||
module.fullName, | ||
module.block.astChildren.collectAll[Method].map(toMethod).l, | ||
module.local.map(toModuleVariable).l | ||
) | ||
moduleTypeMap | ||
}.toSet | ||
// Map module types | ||
val typeEntries = namespace.method.collectFirst { | ||
case m: Method if m.name == Defines.Program => | ||
s"${namespace.fullName}:${m.name}" -> m.block.astChildren.collectAll[TypeDecl].map(toType).toSet | ||
}.toSeq | ||
|
||
moduleEntry +: typeEntries | ||
}.toMap | ||
RubyProgramSummary(mapping) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.