Skip to content

Commit 7ddd083

Browse files
authored
More spec cleanup and json generation (#1204)
* Delete script that is no longer needed * Boilerplate for `schema2json` * demonstrate access to schema * json4s example code * Output node properties, taking into account inheritance * Remove PROPAGATE edge and ALIAS property * Remove `DetachedTrackingPoint` * Remove ARRAY_INITIALIZER * format * Move `MetaData` into its own class * Make version field accessible
1 parent cb79950 commit 7ddd083

File tree

15 files changed

+162
-197
lines changed

15 files changed

+162
-197
lines changed

build.sbt

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ lazy val console = Projects.console
7575
lazy val fuzzyc2cpg = Projects.fuzzyc2cpg
7676
lazy val fuzzyc2cpgtests = Projects.fuzzyc2cpgtests
7777
lazy val macros = Projects.macros
78+
lazy val schema2json = Projects.schema2json
7879

7980
// Once sbt-scalafmt is at version > 2.x, use scalafmtAll
8081
addCommandAlias("format", ";scalafixAll OrganizeImports;scalafmt;test:scalafmt")

codepropertygraph/src/test/scala/io/shiftleft/codepropertygraph/cpgloading/DiffGraphTest.scala

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package io.shiftleft.codepropertygraph.cpgloading
22

33
import io.shiftleft.OverflowDbTestInstance
44
import io.shiftleft.codepropertygraph.generated._
5-
import io.shiftleft.codepropertygraph.generated.edges.Propagate
6-
import io.shiftleft.codepropertygraph.generated.nodes.{MethodParameterIn, MethodParameterOut, NewNode, StoredNode}
5+
import io.shiftleft.codepropertygraph.generated.edges.ReachingDef
6+
import io.shiftleft.codepropertygraph.generated.nodes.{Identifier, MethodParameterIn, NewNode, StoredNode}
77
import io.shiftleft.passes.{DiffGraph, IntervalKeyPool}
88
import org.scalatest.matchers.should.Matchers
99
import org.scalatest.wordspec.AnyWordSpec
@@ -18,8 +18,8 @@ class DiffGraphTest extends AnyWordSpec with Matchers {
1818
// setup existing graph
1919
// add x and y nodes to graph
2020
val x = graph + (MethodParameterIn.Label, MethodParameterIn.Properties.Code -> "x")
21-
val y = graph + (MethodParameterOut.Label, MethodParameterOut.Properties.Code -> "old y code")
22-
val x2y = x --- (Propagate.Label, Propagate.Properties.Alias -> true) --> y
21+
val y = graph + (Identifier.Label, Identifier.Properties.Code -> "old y code")
22+
val x2y = x --- (ReachingDef.Label, ReachingDef.Properties.Variable -> "true") --> y
2323

2424
// make diffgraph
2525
val diffBuilder = DiffGraph.newBuilder
@@ -39,15 +39,15 @@ class DiffGraphTest extends AnyWordSpec with Matchers {
3939
diffBuilder.addNodeProperty(y.asInstanceOf[StoredNode], PropertyNames.ORDER, Int.box(123))
4040
diffBuilder.addNodeProperty(y.asInstanceOf[StoredNode], PropertyNames.CODE, "new y code")
4141

42-
diffBuilder.addEdgeProperty(x2y, PropertyNames.ALIAS, JBoolean.FALSE)
42+
diffBuilder.addEdgeProperty(x2y, PropertyNames.VARIABLE, JBoolean.FALSE)
4343
val diffGraph = diffBuilder.build()
4444
// apply diffgraph with undoable = true
4545
val appliedDiffGraph = DiffGraph.Applier.applyDiff(diffGraph, graph, true, None)
4646
val inverseDiffGraph = appliedDiffGraph.inverseDiffGraph.get
4747
val changes = inverseDiffGraph.iterator.toList
4848
import DiffGraph.Change._
4949
val List(
50-
SetEdgeProperty(_, PropertyNames.ALIAS, JBoolean.TRUE), // restore old edge property value
50+
SetEdgeProperty(_, PropertyNames.VARIABLE, "true"), // restore old edge property value
5151
SetNodeProperty(_, PropertyNames.CODE, "old y code"), // restore old Y property value
5252
RemoveNodeProperty(_, PropertyNames.ORDER), // remove newly added property
5353
RemoveEdge(_), // remove x -> a

codepropertygraph/src/test/scala/io/shiftleft/passes/CpgOverlayIntegrationTest.scala

+10-10
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package io.shiftleft.passes
33
import io.shiftleft.OverflowDbTestInstance
44
import io.shiftleft.codepropertygraph.Cpg
55
import io.shiftleft.codepropertygraph.generated._
6-
import io.shiftleft.codepropertygraph.generated.edges.Propagate
7-
import io.shiftleft.codepropertygraph.generated.nodes.{MethodParameterIn, MethodParameterOut, StoredNode}
6+
import io.shiftleft.codepropertygraph.generated.edges.ReachingDef
7+
import io.shiftleft.codepropertygraph.generated.nodes.{Identifier, MethodParameterIn, StoredNode}
88
import org.scalatest.matchers.should.Matchers
99
import org.scalatest.wordspec.AnyWordSpec
1010
import overflowdb._
@@ -45,13 +45,13 @@ class CpgOverlayIntegrationTest extends AnyWordSpec with Matchers {
4545
// 1) add a new node
4646
val addNodeInverse = applyDiffAndGetInverse(cpg)(
4747
_.addNode(
48-
nodes.NewMethodParameterOut().code(null)
48+
nodes.NewIdentifier().code(null)
4949
))
5050
cpg.graph.nodeCount shouldBe 2
5151
val additionalNode = cpg.graph.V
52-
.label(MethodParameterOut.Label)
52+
.label(Identifier.Label)
5353
.collectFirst {
54-
case mpe: MethodParameterOut if mpe.code == null => mpe
54+
case mpe: nodes.Identifier if mpe.code == null => mpe
5555
}
5656
.get
5757
.asInstanceOf[StoredNode]
@@ -61,15 +61,15 @@ class CpgOverlayIntegrationTest extends AnyWordSpec with Matchers {
6161
_.addEdge(
6262
src = initialNode,
6363
dst = additionalNode,
64-
edgeLabel = Propagate.Label,
65-
properties = Seq(Propagate.PropertyNames.Alias -> Boolean.box(true))
64+
edgeLabel = ReachingDef.Label,
65+
properties = Seq(ReachingDef.PropertyNames.Variable -> "true")
6666
))
6767
val addEdge2Inverse = applyDiffAndGetInverse(cpg)(
6868
_.addEdge(
6969
src = initialNode,
7070
dst = additionalNode,
71-
edgeLabel = Propagate.Label,
72-
properties = Seq(Propagate.PropertyNames.Alias -> Boolean.box(false))
71+
edgeLabel = ReachingDef.Label,
72+
properties = Seq(ReachingDef.PropertyNames.Variable -> "false")
7373
))
7474
def initialNodeOutEdges = initialNode.outE.toList
7575
initialNodeOutEdges.size shouldBe 2
@@ -95,7 +95,7 @@ class CpgOverlayIntegrationTest extends AnyWordSpec with Matchers {
9595
// 2) remove edges - they don't have ids and are therefor disambiguated by their property hash
9696
DiffGraph.Applier.applyDiff(addEdge2Inverse, cpg)
9797
initialNodeOutEdges.size shouldBe 1
98-
initialNode.outE.property(Properties.ALIAS).toList shouldBe List(true)
98+
initialNode.outE.property(Properties.VARIABLE).toList shouldBe List("true")
9999
DiffGraph.Applier.applyDiff(addEdge1Inverse, cpg)
100100
initialNodeOutEdges.size shouldBe 0
101101

dataflowengineoss/src/main/scala/io/shiftleft/dataflowengineoss/language/nodemethods/TrackingPointMethods.scala

+2-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ class TrackingPointMethods[NodeType <: nodes.TrackingPoint](val node: NodeType)
2222
* */
2323
def astNode: nodes.AstNode =
2424
node match {
25-
case n: nodes.AstNode => n
26-
case n: nodes.DetachedTrackingPoint => n.cfgNode
27-
case _ => ??? //TODO markus/fabs?
25+
case n: nodes.AstNode => n
26+
case _ => ??? //TODO markus/fabs?
2827
}
2928

3029
def reachableBy[NodeType <: nodes.TrackingPoint](sourceTravs: Traversal[NodeType]*)(

dataflowengineoss/src/main/scala/io/shiftleft/dataflowengineoss/language/package.scala

+3-6
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,9 @@ package object language {
2222
implicit def trackingPointToAstNodeMethods(node: nodes.TrackingPoint) =
2323
new AstNodeMethods(trackingPointToAstNode(node))
2424

25-
implicit def trackingPointToAstNode(node: nodes.TrackingPoint): nodes.AstNode =
26-
node match {
27-
case n: nodes.AstNode => n
28-
case n: nodes.DetachedTrackingPoint => n.cfgNode
29-
case _ => ??? //TODO markus/fabs?
30-
}
25+
implicit def trackingPointToAstNode(node: nodes.TrackingPoint): nodes.AstNode = {
26+
node.astNode
27+
}
3128

3229
implicit def toDdgNodeDot[A](a: A)(implicit f: A => Traversal[nodes.Method]): DdgNodeDot =
3330
new DdgNodeDot(f(a))

project/Projects.scala

+1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ object Projects {
1313
lazy val fuzzyc2cpg = project.in(file("fuzzyc2cpg"))
1414
lazy val fuzzyc2cpgtests = project.in(file("fuzzyc2cpg-tests"))
1515
lazy val macros = project.in(file("macros"))
16+
lazy val schema2json = project.in(file("schema2json"))
1617
}

regenerate-test-cpgs.sh

-47
This file was deleted.

schema/src/main/scala/io/shiftleft/codepropertygraph/schema/Base.scala

+2-40
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ object Base {
2020
valueType = ValueTypes.STRING,
2121
cardinality = Cardinality.One,
2222
comment = """A version, given as a string. Used, for example, in the META_DATA node to
23-
|indicate which version of the CPG spec this CPG conforms to
24-
|""".stripMargin
23+
|indicate which version of the CPG spec this CPG conforms to
24+
|""".stripMargin
2525
)
2626
.protoId(13)
2727

@@ -310,33 +310,6 @@ object Base {
310310
.addProperties(name, signature)
311311
.extendz(cfgNode)
312312

313-
val language = builder
314-
.addProperty(
315-
name = "LANGUAGE",
316-
valueType = ValueTypes.STRING,
317-
cardinality = Cardinality.One,
318-
comment = "the CPG language frontend that generated this CPG"
319-
)
320-
.protoId(19)
321-
322-
val overlays = builder
323-
.addProperty(
324-
name = "OVERLAYS",
325-
valueType = ValueTypes.STRING,
326-
cardinality = Cardinality.List,
327-
comment = "Names of overlays applied to this graph, in order of application"
328-
)
329-
.protoId(118)
330-
331-
val metaData: NodeType = builder
332-
.addNodeType(
333-
name = "META_DATA",
334-
comment = """Node to save meta data about the graph on its properties.
335-
|Exactly one node of this type per graph""".stripMargin
336-
)
337-
.protoId(39)
338-
.addProperties(language, version, overlays, hash)
339-
340313
val file: NodeType = builder
341314
.addNodeType(
342315
name = "FILE",
@@ -578,14 +551,6 @@ object Base {
578551
.addProperties(name, signature, fullName, methodFullName)
579552
.extendz(astNode)
580553

581-
val arrayInitializer: NodeType = builder
582-
.addNodeType(
583-
name = "ARRAY_INITIALIZER",
584-
comment = "Initialization construct for arrays"
585-
)
586-
.protoId(14)
587-
.extendz(astNode)
588-
589554
val methodRef: NodeType = builder
590555
.addNodeType(
591556
name = "METHOD_REF",
@@ -885,7 +850,6 @@ object Base {
885850
controlStructure
886851
.addOutEdge(edge = ast, inNode = literal, cardinalityIn = Cardinality.One)
887852
.addOutEdge(edge = ast, inNode = modifier)
888-
.addOutEdge(edge = ast, inNode = arrayInitializer)
889853
.addOutEdge(edge = ast, inNode = callNode, cardinalityIn = Cardinality.One)
890854
.addOutEdge(edge = ast, inNode = local)
891855
.addOutEdge(edge = ast, inNode = identifier, cardinalityIn = Cardinality.ZeroOrOne)
@@ -907,7 +871,6 @@ object Base {
907871
.addOutEdge(edge = condition, inNode = jumpTarget)
908872
.addOutEdge(edge = condition, inNode = unknown)
909873
.addOutEdge(edge = condition, inNode = controlStructure)
910-
.addOutEdge(edge = condition, inNode = arrayInitializer)
911874
.addOutEdge(edge = cfg, inNode = callNode)
912875
.addOutEdge(edge = cfg, inNode = identifier)
913876
.addOutEdge(edge = cfg, inNode = fieldIdentifier)
@@ -948,7 +911,6 @@ object Base {
948911
.addOutEdge(edge = ast, inNode = literal)
949912
.addOutEdge(edge = ast, inNode = member)
950913
.addOutEdge(edge = ast, inNode = modifier)
951-
.addOutEdge(edge = ast, inNode = arrayInitializer)
952914
.addOutEdge(edge = ast, inNode = callNode)
953915
.addOutEdge(edge = ast, inNode = local)
954916
.addOutEdge(edge = ast, inNode = identifier)

schema/src/main/scala/io/shiftleft/codepropertygraph/schema/CpgSchema.scala

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import overflowdb.schema.{Schema, SchemaBuilder}
99
class CpgSchema(builder: SchemaBuilder) {
1010
// the foundation
1111
val base = Base(builder)
12+
val metaData = MetaData(builder, base)
1213
val enhancements = Enhancements(builder, base)
1314

1415
// everything else

schema/src/main/scala/io/shiftleft/codepropertygraph/schema/Enhancements.scala

-31
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,6 @@ object Enhancements {
7070
)
7171
.protoId(57)
7272

73-
val alias = builder
74-
.addProperty(
75-
name = "ALIAS",
76-
valueType = ValueTypes.BOOLEAN,
77-
cardinality = Cardinality.One,
78-
comment = "Defines whether a PROPAGATE edge creates an alias"
79-
)
80-
.protoId(1)
81-
8273
val variable = builder
8374
.addProperty(
8475
name = "VARIABLE",
@@ -153,14 +144,6 @@ object Enhancements {
153144
)
154145
.protoId(28)
155146

156-
val propagate = builder
157-
.addEdgeType(
158-
name = "PROPAGATE",
159-
comment = "Encodes propagation of data from on node to another. The ALIAS property is deprecated."
160-
)
161-
.protoId(1)
162-
.addProperties(alias)
163-
164147
val reachingDef = builder
165148
.addEdgeType(
166149
name = "REACHING_DEF",
@@ -192,18 +175,6 @@ object Enhancements {
192175

193176
// node types
194177

195-
val detachedTrackingPoint: NodeType = builder
196-
.addNodeType(
197-
name = "DETACHED_TRACKING_POINT",
198-
comment = ""
199-
)
200-
.protoId(1001)
201-
.addProperties()
202-
.extendz(trackingPoint)
203-
204-
detachedTrackingPoint
205-
.addContainedNode(cfgNode, "cfgNode", Cardinality.One)
206-
207178
val binding: NodeType = builder
208179
.addNodeType(
209180
name = "BINDING",
@@ -779,8 +750,6 @@ object Enhancements {
779750
.addOutEdge(edge = reachingDef, inNode = methodRef)
780751

781752
methodParameterIn
782-
.addOutEdge(edge = propagate, inNode = methodParameterOut)
783-
.addOutEdge(edge = propagate, inNode = methodReturn)
784753
.addOutEdge(edge = evalType, inNode = tpe, cardinalityOut = Cardinality.One)
785754
.addOutEdge(edge = reachingDef, inNode = callNode)
786755
.addOutEdge(edge = reachingDef, inNode = ret)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package io.shiftleft.codepropertygraph.schema
2+
3+
import overflowdb.schema.{Cardinality, NodeType, SchemaBuilder}
4+
import overflowdb.storage.ValueTypes
5+
6+
object MetaData {
7+
8+
def apply(builder: SchemaBuilder, base: Base.Schema) = new Schema(builder, base)
9+
10+
class Schema(builder: SchemaBuilder, base: Base.Schema) {
11+
import base._
12+
13+
val overlays = builder
14+
.addProperty(
15+
name = "OVERLAYS",
16+
valueType = ValueTypes.STRING,
17+
cardinality = Cardinality.List,
18+
comment = "Names of overlays applied to this graph, in order of application"
19+
)
20+
.protoId(118)
21+
22+
val language = builder
23+
.addProperty(
24+
name = "LANGUAGE",
25+
valueType = ValueTypes.STRING,
26+
cardinality = Cardinality.One,
27+
comment = "the CPG language frontend that generated this CPG"
28+
)
29+
.protoId(19)
30+
31+
val metaData: NodeType = builder
32+
.addNodeType(
33+
name = "META_DATA",
34+
comment = """Node to save meta data about the graph on its properties.
35+
|Exactly one node of this type per graph""".stripMargin
36+
)
37+
.protoId(39)
38+
.addProperties(language, version, overlays, hash)
39+
40+
}
41+
42+
}

schema2json.sh

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
./schema2json/target/universal/stage/bin/schema2json

0 commit comments

Comments
 (0)