forked from joernio/joern
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[gosrc2cpg] - Package level TypeDecl constructor handling (joernio#3899)
1. Pending Package level TypeDecl constructor handling is added to handle the initialisation of global variables with the respective RHS side. Handling this was a little tricky as global variables might get defined in different files. It is being handled by caching the package level global variable assignment statement AST mapped against the package TypeDecl fullname. We create this package-level constructor Method node in a separate pass by using this cached information. 2. Added simple data flow test as well. TODO: 1. Need to add more unit tests around package level variable Lambda expression initialisation as well as data flow tests for the same. 2. More unit tests around the general package-level variable declaration. 3. Handling of closure use case. 4. More data flow tests around lambda expression handling.
- Loading branch information
1 parent
7682b74
commit cdcaf26
Showing
12 changed files
with
203 additions
and
17 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
51 changes: 51 additions & 0 deletions
51
...c2cpg/src/main/scala/io/joern/gosrc2cpg/astcreation/AstForPackageConstructorCreator.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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package io.joern.gosrc2cpg.astcreation | ||
|
||
import io.joern.x2cpg.{Ast, AstCreatorBase, ValidationMode, Defines as XDefines} | ||
import io.shiftleft.codepropertygraph.generated.{EvaluationStrategies, NodeTypes} | ||
import io.shiftleft.codepropertygraph.generated.nodes.{NewBlock, NewMethod, NewMethodReturn} | ||
import org.apache.commons.lang.StringUtils | ||
import overflowdb.BatchedUpdate.DiffGraphBuilder | ||
|
||
import scala.collection.immutable.Set | ||
|
||
class AstForPackageConstructorCreator(val pacakgePath: String, statements: Set[(Ast, String)])(implicit | ||
withSchemaValidation: ValidationMode | ||
) extends AstCreatorBase(pacakgePath) { | ||
|
||
override def createAst(): DiffGraphBuilder = { | ||
val name = StringUtils.normalizeSpace(s"$pacakgePath${XDefines.StaticInitMethodName}") | ||
val fakeGlobalMethod = | ||
NewMethod() | ||
.name(name) | ||
.code(name) | ||
.fullName(name) | ||
.filename(pacakgePath) | ||
.astParentType(NodeTypes.TYPE_DECL) | ||
.astParentFullName(pacakgePath) | ||
.isExternal(false) | ||
.lineNumber(0) | ||
.columnNumber(0) | ||
.lineNumberEnd(0) | ||
.columnNumberEnd(0) | ||
|
||
val blockNode_ = NewBlock() | ||
.code(name) | ||
.typeFullName(Defines.voidTypeName) | ||
.lineNumber(0) | ||
.columnNumber(0) | ||
|
||
val declsAsts = statements.map(_._1).toList | ||
setArgumentIndices(declsAsts) | ||
|
||
val methodReturn = NewMethodReturn() | ||
.typeFullName(Defines.voidTypeName) | ||
.code("RET") | ||
.evaluationStrategy(EvaluationStrategies.BY_VALUE) | ||
.lineNumber(0) | ||
.columnNumber(0) | ||
val ctorAst = methodAst(fakeGlobalMethod, Seq.empty, blockAst(blockNode_, declsAsts), methodReturn) | ||
Ast.storeInDiffGraph(ctorAst, diffGraph) | ||
diffGraph | ||
} | ||
|
||
} |
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
27 changes: 27 additions & 0 deletions
27
...rontends/gosrc2cpg/src/main/scala/io/joern/gosrc2cpg/passes/PackageCtorCreationPass.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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package io.joern.gosrc2cpg.passes | ||
|
||
import io.joern.gosrc2cpg.Config | ||
import io.joern.gosrc2cpg.astcreation.AstForPackageConstructorCreator | ||
import io.joern.gosrc2cpg.datastructures.GoGlobal | ||
import io.joern.x2cpg.Ast | ||
import io.shiftleft.codepropertygraph.Cpg | ||
import io.shiftleft.passes.ConcurrentWriterCpgPass | ||
|
||
import scala.jdk.CollectionConverters.* | ||
|
||
class PackageCtorCreationPass(cpg: Cpg, config: Config) | ||
extends ConcurrentWriterCpgPass[(String, Set[(Ast, String)])](cpg) { | ||
override def generateParts(): Array[(String, Set[(Ast, String)])] = | ||
GoGlobal.pkgLevelVarAndConstantAstMap | ||
.keys() | ||
.asScala | ||
.map(key => (key, GoGlobal.pkgLevelVarAndConstantAstMap.get(key))) | ||
.toArray | ||
|
||
override def runOnPart(diffGraph: DiffGraphBuilder, part: (String, Set[(Ast, String)])): Unit = { | ||
val (packageStr, statementAsts) = part | ||
val packageCtorAstCreator = new AstForPackageConstructorCreator(packageStr, statementAsts)(config.schemaValidation) | ||
val localDiff = packageCtorAstCreator.createAst() | ||
diffGraph.absorb(localDiff) | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...tends/gosrc2cpg/src/test/scala/io/joern/go2cpg/dataflow/GlobalVariableDataflowTests.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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package io.joern.go2cpg.dataflow | ||
|
||
import io.joern.go2cpg.testfixtures.GoCodeToCpgSuite | ||
import io.joern.go2cpg.testfixtures.GoCodeToCpgSuite | ||
import io.joern.x2cpg.Defines | ||
import io.shiftleft.codepropertygraph.generated.Operators | ||
import io.shiftleft.semanticcpg.language.* | ||
import io.joern.dataflowengineoss.language.* | ||
import io.joern.go2cpg.testfixtures.GoCodeToCpgSuite | ||
import io.shiftleft.semanticcpg.language.* | ||
import java.io.File | ||
class GlobalVariableDataflowTests extends GoCodeToCpgSuite(withOssDataflow = true) { | ||
|
||
"Global variable declaration check" should { | ||
val cpg = code(""" | ||
|package main | ||
|const ( | ||
| FooConst = "Test" | ||
|) | ||
|var ( | ||
| BarVar = 100 | ||
|) | ||
|func main() { | ||
| println(FooConst) | ||
|} | ||
|""".stripMargin) | ||
"get the data flow from global variable to println sink" in { | ||
val source = cpg.literal("\"Test\"") | ||
val sink = cpg.call("println") | ||
sink.reachableByFlows(source).size shouldBe 1 | ||
} | ||
} | ||
} |
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