Skip to content

Commit 40bdfd6

Browse files
authored
Cleanup MetaData and FuzzyC File creation (#1066)
* Move `POLICY_DIRECTORIES` into enhancements.json * MetaData-block cleanup * Do not create file nodes in fuzzyc frontend * Do not run `FilenameCompat` for C CPGs * Rename `FileLinker` to `FileCreationPass` and move * Minor cleanups
1 parent 0ee1ec5 commit 40bdfd6

14 files changed

Lines changed: 67 additions & 79 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package io.shiftleft.fuzzyc2cpg.querying
2+
3+
import io.shiftleft.fuzzyc2cpg.testfixtures.FuzzyCCodeToCpgSuite
4+
import io.shiftleft.semanticcpg.language._
5+
6+
class MetaDataNodeTests extends FuzzyCCodeToCpgSuite {
7+
8+
override val code: String =
9+
"""
10+
|
11+
|""".stripMargin
12+
13+
"should contain exactly one node with all mandatory fields set" in {
14+
cpg.metaData.l match {
15+
case List(x) =>
16+
x.language shouldBe "C"
17+
x.version shouldBe "0.1"
18+
x.overlays shouldBe List("semanticcpg")
19+
case _ => fail
20+
}
21+
}
22+
23+
}

fuzzyc2cpg-tests/src/test/scala/io/shiftleft/fuzzyc2cpg/testfixtures/FuzzyCCodeToCpgSuite.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.shiftleft.fuzzyc2cpg.testfixtures
22

33
import java.io.File
4-
54
import io.shiftleft.codepropertygraph.Cpg
65
import io.shiftleft.fuzzyc2cpg.FuzzyC2Cpg
76
import io.shiftleft.semanticcpg.testfixtures.{CodeToCpgFixture, LanguageFrontend}

fuzzyc2cpg/src/main/scala/io/shiftleft/fuzzyc2cpg/passes/AstCreationPass.scala

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package io.shiftleft.fuzzyc2cpg.passes
22

33
import io.shiftleft.codepropertygraph.Cpg
4-
import io.shiftleft.codepropertygraph.generated.{EdgeTypes, nodes}
4+
import io.shiftleft.codepropertygraph.generated.nodes
55
import io.shiftleft.fuzzyc2cpg.passes.astcreation.{AntlrCModuleParserDriver, AstVisitor}
66
import io.shiftleft.fuzzyc2cpg.{Defines, Global}
77
import io.shiftleft.passes.{DiffGraph, IntervalKeyPool, ParallelCpgPass}
@@ -23,26 +23,20 @@ class AstCreationPass(filenames: List[String], cpg: Cpg, keyPool: IntervalKeyPoo
2323

2424
val diffGraph = DiffGraph.newBuilder
2525
val absolutePath = new java.io.File(filename).toPath.toAbsolutePath.normalize().toString
26-
val fileNode = nodes.NewFile(name = absolutePath, order = 0)
27-
diffGraph.addNode(fileNode)
2826
val namespaceBlock = nodes.NewNamespaceBlock(
2927
name = Defines.globalNamespaceName,
30-
fullName = CMetaDataPass.getGlobalNamespaceBlockFullName(Some(fileNode.name))
28+
fullName = CMetaDataPass.getGlobalNamespaceBlockFullName(Some(absolutePath)),
29+
filename = absolutePath
3130
)
32-
diffGraph.addNode(fileNode)
3331
diffGraph.addNode(namespaceBlock)
34-
diffGraph.addEdge(fileNode, namespaceBlock, EdgeTypes.AST)
35-
36-
val driver = createDriver(fileNode, namespaceBlock)
32+
val driver = createDriver(namespaceBlock)
3733
tryToParse(driver, filename, diffGraph)
3834
}
3935

40-
private def createDriver(fileNode: nodes.NewFile,
41-
namespaceBlock: nodes.NewNamespaceBlock): AntlrCModuleParserDriver = {
36+
private def createDriver(namespaceBlock: nodes.NewNamespaceBlock): AntlrCModuleParserDriver = {
4237
val driver = new AntlrCModuleParserDriver()
4338
val astVisitor = new AstVisitor(driver, namespaceBlock, global)
4439
driver.addObserver(astVisitor)
45-
driver.setFileNode(fileNode)
4640
driver
4741
}
4842

fuzzyc2cpg/src/main/scala/io/shiftleft/fuzzyc2cpg/passes/CMetaDataPass.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import io.shiftleft.passes.{CpgPass, DiffGraph, KeyPool}
1313
class CMetaDataPass(cpg: Cpg, keyPool: Option[KeyPool] = None) extends CpgPass(cpg, keyPool = keyPool) {
1414
override def run(): Iterator[DiffGraph] = {
1515
def addMetaDataNode(diffGraph: DiffGraph.Builder): Unit = {
16-
val metaNode = nodes.NewMetaData(language = Languages.C)
16+
val metaNode = nodes.NewMetaData(language = Languages.C, version = "0.1")
1717
diffGraph.addNode(metaNode)
1818
}
1919

fuzzyc2cpg/src/main/scala/io/shiftleft/fuzzyc2cpg/passes/astcreation/AntlrParserDriver.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,13 @@ abstract public class AntlrParserDriver {
5353
private CommonParserContext context = null;
5454
public DiffGraph.Builder cpg;
5555
private final List<AntlrParserDriverObserver> observers = new ArrayList<>();
56-
private NewFile fileNode;
56+
5757
private Parser antlrParser;
5858

5959
public AntlrParserDriver() {
6060
super();
6161
}
6262

63-
public void setFileNode(NewFile fileNode) {
64-
this.fileNode = fileNode;
65-
}
66-
6763
public abstract ParseTree parseTokenStreamImpl(TokenSubStream tokens);
6864

6965
public abstract Lexer createLexer(CharStream input);
@@ -95,10 +91,10 @@ private void handleHiddenTokens(String filename) {
9591
String text = token.getText();
9692
NewComment commentNode = new NewComment(
9793
new Some<>(line),
98-
text
94+
text,
95+
filename
9996
);
10097
cpg.addNode(commentNode);
101-
cpg.addEdge(fileNode, commentNode, EdgeTypes.AST, List$.MODULE$.empty());
10298
}
10399
}
104100

fuzzyc2cpg/src/main/scala/io/shiftleft/fuzzyc2cpg/passes/astcreation/AstCreator.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ private[astcreation] class AstCreator(diffGraph: DiffGraph.Builder,
116116
lineNumberEnd = location.endLine,
117117
columnNumberEnd = location.endPos,
118118
signature = signature,
119+
filename = namespaceBlock.filename
119120
)
120121

121122
addAndConnectAsAstChild(method)
@@ -628,7 +629,8 @@ private[astcreation] class AstCreator(diffGraph: DiffGraph.Builder,
628629
name = identifierDecl.getName.getEscapedCodeStr,
629630
fullName = identifierDecl.getName.getEscapedCodeStr,
630631
isExternal = false,
631-
aliasTypeFullName = Some(registerType(declTypeName))
632+
aliasTypeFullName = Some(registerType(declTypeName)),
633+
filename = namespaceBlock.filename
632634
)
633635
diffGraph.addNode(aliasTypeDecl)
634636
connectAstChild(aliasTypeDecl)
@@ -785,7 +787,8 @@ private[astcreation] class AstCreator(diffGraph: DiffGraph.Builder,
785787
name = name,
786788
fullName = name,
787789
isExternal = false,
788-
inheritsFromTypeFullName = baseClassList
790+
inheritsFromTypeFullName = baseClassList,
791+
filename = namespaceBlock.filename
789792
)
790793

791794
diffGraph.addNode(typeDecl)

fuzzyc2cpg/src/test/scala/io/shiftleft/fuzzyc2cpg/CpgTestFixture.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import io.shiftleft.codepropertygraph.Cpg
44
import io.shiftleft.fuzzyc2cpg.passes.{AstCreationPass, CMetaDataPass, StubRemovalPass}
55
import io.shiftleft.passes.IntervalKeyPool
66
import io.shiftleft.semanticcpg.language._
7-
import io.shiftleft.semanticcpg.passes.CfgCreationPass
7+
import io.shiftleft.semanticcpg.passes.{CfgCreationPass, FileCreationPass}
88
import io.shiftleft.x2cpg.SourceFiles
99
import overflowdb.traversal.TraversalSource
1010

@@ -22,6 +22,7 @@ case class CpgTestFixture(projectName: String) {
2222
new CfgCreationPass(cpg, cfgKeyPool).createAndApply()
2323
}
2424
new StubRemovalPass(cpg).createAndApply()
25+
new FileCreationPass(cpg).createAndApply()
2526

2627
def traversalSource = TraversalSource(cpg.graph)
2728

fuzzyc2cpg/src/test/scala/io/shiftleft/fuzzyc2cpg/ProgramStructureTests.scala

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package io.shiftleft.fuzzyc2cpg
22

3-
import java.nio.file.Paths
4-
53
import io.shiftleft.codepropertygraph.generated.NodeKeys
64
import io.shiftleft.proto.cpg.Cpg.CpgStruct.Node.NodeType
75
import org.scalatest.matchers.should.Matchers
@@ -23,32 +21,6 @@ class ProgramStructureTests extends AnyWordSpec with Matchers {
2321
namespaceBlocks.size shouldBe 1
2422
}
2523

26-
"contain one file node" in {
27-
val fileName = fixture.traversalSource
28-
.label(NodeType.FILE.toString)
29-
.property(NodeKeys.NAME)
30-
.headOption
31-
fileName.isDefined shouldBe true
32-
fileName.head should not contain ".."
33-
34-
// Construct a platform-independent path.
35-
val path = Paths
36-
.get("src", "test", "resources", "testcode", "structure", "structure.c")
37-
.toString
38-
39-
fileName.head should not be path
40-
fileName.head should endWith(path)
41-
}
42-
43-
"contain AST edge from file node to namespace block" in {
44-
val nodes = fixture.traversalSource
45-
.label(NodeType.FILE.toString)
46-
.out("AST")
47-
.hasLabel(NodeType.NAMESPACE_BLOCK.toString)
48-
.l
49-
nodes.size shouldBe 1
50-
}
51-
5224
"contain type-decl node" in {
5325
val nodes = fixture.traversalSource.label(NodeType.TYPE_DECL.toString).l
5426
nodes.size should be > 0

fuzzyc2cpg/src/test/scala/io/shiftleft/fuzzyc2cpg/passes/AstCreationPassTests.scala

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ class AstCreationPassTests extends AnyWordSpec with Matchers {
2222
new AstCreationPass(filenames, cpg, new IntervalKeyPool(1, 1000))
2323
.createAndApply()
2424

25-
"create one File node per file name with absolute path in `name`" in {
26-
cpg.file.name.toSet shouldBe expectedFilenameFields.toSet
27-
}
28-
2925
"create one NamespaceBlock per file" in {
3026
val expectedNamespaceFullNames = expectedFilenameFields.map(f => s"$f:<global>").toSet
3127
cpg.namespaceBlock.fullName.toSet shouldBe expectedNamespaceFullNames

schema/src/main/resources/schemas/base.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
{ "id": 19, "name" : "LANGUAGE", "comment" : "The programming language this graph originates from", "valueType" : "string", "cardinality" : "one"},
1010
{ "id" : 13, "name": "VERSION", "comment" : "A version, given as a string", "valueType" : "string", "cardinality" : "one"},
1111
{ "id" : 118, "name" : "OVERLAYS", "comment" : "Names of overlays applied to this graph, in order of application", "valueType" : "string", "cardinality" : "list"},
12-
{ "id" : 119, "name" : "POLICY_DIRECTORIES", "comment": "Sub directories of the policy directory that should be loaded when processing the CPG", "valueType" : "string", "cardinality" : "list"},
1312
{ "id" : 120, "name" : "HASH", "comment" : "Hash value of the artifact that this CPG is built from.", "valueType": "string", "cardinality" : "zeroOrOne"},
1413

1514
// Properties that indicate where the code can be found
@@ -68,7 +67,7 @@
6867
{
6968
"id" : 39,
7069
"name" : "META_DATA",
71-
"keys" : ["LANGUAGE", "VERSION", "OVERLAYS", "POLICY_DIRECTORIES", "HASH"],
70+
"keys" : ["LANGUAGE", "VERSION", "OVERLAYS", "HASH"],
7271
"comment" : "Node to save meta data about the graph on its properties. Exactly one node of this type per graph",
7372
"outEdges" : []
7473
},

0 commit comments

Comments
 (0)