Skip to content

Commit 8ab331d

Browse files
authored
Remove CpgPass (#1667)
1 parent 1ad9a7a commit 8ab331d

4 files changed

Lines changed: 36 additions & 323 deletions

File tree

codepropertygraph/src/main/scala/io/shiftleft/passes/CpgPass.scala

Lines changed: 22 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -12,89 +12,7 @@ import java.util.function.{BiConsumer, Supplier}
1212
import scala.concurrent.duration.DurationLong
1313
import scala.util.{Failure, Success, Try}
1414

15-
/** Base class for CPG pass - a program, which receives an input graph and outputs a sequence of additive diff graphs.
16-
* These diff graphs can be merged into the original graph ("applied"), they can be serialized into a binary format,
17-
* and finally, they can be added to an existing cpg.bin.zip file.
18-
*
19-
* A pass is provided by inheriting from this class and implementing `run`, a method, which creates the sequence of
20-
* diff graphs from an input graph.
21-
*
22-
* Overview of steps and their meaning:
23-
*
24-
* 1. Create: A sequence of diff graphs is created from the source graph 2. Apply: Each diff graph can be applied to
25-
* the source graph 3. Serialize: After applying a diff graph, the diff graph can be serialized into a CPG overlay
26-
* 4. Store: The CPG overlay can be stored in a serialized CPG.
27-
*
28-
* @param cpg
29-
* the source CPG this pass traverses
30-
*/
31-
@deprecated(message = "Please use SimpleCpgPass as a replacement.", since = "v1.3.508")
32-
abstract class CpgPass(cpg: Cpg, outName: String = "", keyPool: Option[KeyPool] = None) extends CpgPassBase {
33-
34-
/** Main method of pass - to be implemented by child class
35-
*/
36-
def run(): Iterator[DiffGraph]
37-
38-
/** Execute the pass and apply result to the underlying graph
39-
*/
40-
override def createAndApply(): Unit =
41-
withStartEndTimesLogged {
42-
run().foreach(diffGraph => DiffGraph.Applier.applyDiff(diffGraph, cpg, undoable = false, keyPool))
43-
}
44-
45-
/** Execute and create a serialized overlay
46-
* @param inverse
47-
* invert the diffgraph before serializing
48-
*/
49-
def createApplyAndSerialize(inverse: Boolean = false): Iterator[GeneratedMessageV3] =
50-
withStartEndTimesLogged {
51-
val overlays = run().map { diffGraph =>
52-
val appliedDiffGraph = DiffGraph.Applier.applyDiff(diffGraph, cpg, inverse, keyPool)
53-
serialize(appliedDiffGraph, inverse)
54-
}
55-
overlays
56-
}
57-
58-
/** Run a CPG pass to create diff graphs, apply diff graphs, create corresponding overlays and add them to the
59-
* serialized CPG. The name of the overlay is derived from the class name of the pass.
60-
*
61-
* @param serializedCpg
62-
* the destination serialized CPG to add overlays to
63-
* @param inverse
64-
* invert the diffgraph before serializing
65-
* @param prefix
66-
* a prefix to add to the output name
67-
*/
68-
override def createApplySerializeAndStore(
69-
serializedCpg: SerializedCpg,
70-
inverse: Boolean = false,
71-
prefix: String = ""
72-
): Unit = {
73-
if (serializedCpg.isEmpty) {
74-
createAndApply()
75-
} else {
76-
val overlays = createApplyAndSerialize(inverse)
77-
overlays.zipWithIndex.foreach {
78-
case (overlay, index) => {
79-
val name = generateOutFileName(prefix, outName, index)
80-
store(overlay, name, serializedCpg)
81-
}
82-
}
83-
}
84-
}
85-
86-
override def runWithBuilder(builder: BatchedUpdate.DiffGraphBuilder): Int = {
87-
var nParts = 0
88-
for (diff <- run()) {
89-
diff.convertToOdbStyle(cpg, builder)
90-
nParts += 1
91-
}
92-
nParts
93-
}
94-
95-
}
96-
97-
/* SimpleCpgPass is a possible replacement for CpgPass.
15+
/* SimpleCpgPass is a replacement for CpgPass.
9816
*
9917
* Instead of returning an Iterator[DiffGraph], the `run` fuction gets a DiffGraphBuilder as input, and can attach its
10018
* modifications to it (i.e. mutate the builder).
@@ -108,6 +26,26 @@ abstract class CpgPass(cpg: Cpg, outName: String = "", keyPool: Option[KeyPool]
10826
* Initialization and cleanup of external resources or large datastructures can be done in the `init()` and `finish()`
10927
* methods. This may be better than using the constructor or GC, because e.g. SCPG chains of passes construct
11028
* passes eagerly, and releases them only when the entire chain has run.
29+
*
30+
* * * * * * * * * * * * * * * * * * * * * * * * * * * * *
31+
* // description of now-removed `CpgPass` class
32+
*
33+
* Base class for CPG pass - a program, which receives an input graph and outputs a sequence of additive diff graphs.
34+
* These diff graphs can be merged into the original graph ("applied"), they can be serialized into a binary format,
35+
* and finally, they can be added to an existing cpg.bin.zip file.
36+
*
37+
* A pass is provided by inheriting from this class and implementing `run`, a method, which creates the sequence of
38+
* diff graphs from an input graph.
39+
*
40+
* Overview of steps and their meaning:
41+
*
42+
* 1. Create: A sequence of diff graphs is created from the source graph 2. Apply: Each diff graph can be applied to
43+
* the source graph 3. Serialize: After applying a diff graph, the diff graph can be serialized into a CPG overlay
44+
* 4. Store: The CPG overlay can be stored in a serialized CPG.
45+
*
46+
* @param cpg
47+
* the source CPG this pass traverses
48+
*
11149
* */
11250

11351
abstract class SimpleCpgPass(cpg: Cpg, outName: String = "", keyPool: Option[KeyPool] = None)
@@ -266,7 +204,7 @@ abstract class NewStyleCpgPassBase[T <: AnyRef] extends CpgPassBase {
266204
}
267205

268206
object CpgPassBase {
269-
private val baseLogger: Logger = LoggerFactory.getLogger(classOf[CpgPass])
207+
private val baseLogger: Logger = LoggerFactory.getLogger(classOf[CpgPassBase])
270208
}
271209

272210
trait CpgPassBase {

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

Lines changed: 0 additions & 148 deletions
This file was deleted.

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

Lines changed: 0 additions & 78 deletions
This file was deleted.
Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
package io.shiftleft.mypass
22

3-
import gremlin.scala.ScalaGraph
43
import io.shiftleft.SerializedCpg
5-
import io.shiftleft.codepropertygraph.cpgloading.CpgLoader
6-
import io.shiftleft.diffgraph.DiffGraph
7-
import io.shiftleft.passes.{CpgPass, CpgPassRunner}
4+
import io.shiftleft.codepropertygraph.Cpg
5+
import io.shiftleft.codepropertygraph.cpgloading.{CpgLoader, CpgLoaderConfig}
6+
import io.shiftleft.codepropertygraph.generated.nodes.NewFile
7+
import io.shiftleft.passes.SimpleCpgPass
88

9-
class SamplePass(graph : ScalaGraph) extends CpgPass(graph) {
10-
override def run(): Iterator[DiffGraph] = {
11-
val diffGraph = new DiffGraph
12-
// Construct additive diff here
13-
Iterator(diffGraph)
9+
class SamplePass(cpg: Cpg) extends SimpleCpgPass(cpg) {
10+
override def run(builder: DiffGraphBuilder): Unit = {
11+
val fileNode = NewFile().name("foo")
12+
builder.addNode(fileNode)
1413
}
1514
}
1615

1716
object SamplePassMain extends App {
1817
val filename = "src_cpg.bin.zip"
19-
val cpg = CpgLoader.load(filename)
18+
val odbConfig = new overflowdb.Config().withStorageLocation(filename)
19+
val loaderConfig = CpgLoaderConfig().withOverflowConfig(odbConfig)
20+
val cpg = CpgLoader.loadFromOverflowDb(loaderConfig)
2021
val serializedCpg = new SerializedCpg("/tmp/dst.bin.zip")
21-
val runner = new CpgPassRunner(serializedCpg)
22-
val pass = new SamplePass(cpg.graph)
23-
runner.createStoreAndApplyOverlay(pass)
22+
val pass = new SamplePass(cpg)
23+
pass.createApplySerializeAndStore(serializedCpg)
2424
}
25+

0 commit comments

Comments
 (0)