ForkJoinParallelCpgPass

io.shiftleft.passes.ForkJoinParallelCpgPass
abstract class ForkJoinParallelCpgPass[T <: AnyRef](cpg: Cpg, outname: String = ...) extends ForkJoinParallelCpgPassWithAccumulator[T, Null]

A parallel CPG pass using the fork/join model.

Instead of returning an Iterator, generateParts returns an Array. This means that the entire collection of parts must live on the heap at the same time; on the other hand, there are no possible issues with iterator invalidation, e.g. when running over all METHOD nodes and deleting some of them.

Instead of streaming writes as ParallelCpgPass do, all runOnPart invocations read the initial state of the graph. Then all changes (accumulated in the DiffGraphBuilders) are merged into a single change, and applied in one go.

In other words, the parallelism follows the fork/join parallel map-reduce (java: collect, scala: aggregate) model. The effect is identical as if one were to sequentially run runOnPart on all output elements of generateParts in sequential order, with the same builder.

This simplifies semantics and makes it easy to reason about possible races.

Note that ForkJoinParallelCpgPass never writes intermediate results, so one must consider peak memory consumption when porting from ParallelCpgPass.

Initialization and cleanup of external resources or large datastructures can be done in the init and finish methods. This may be better than using the constructor or GC, because e.g. SCPG chains of passes construct passes eagerly, and releases them only when the entire chain has run.

This is a simplified form of ForkJoinParallelCpgPassWithAccumulator that does not use an accumulator.

Type parameters

T

the type of each part produced by generateParts

Value parameters

cpg

the code property graph to modify

outname

optional output name

Attributes

Graph
Supertypes
trait CpgPassBase
class Object
trait Matchable
class Any
Known subtypes
class CpgPass

Members list

Type members

Inherited types

type DiffGraphBuilder = DiffGraphBuilder

Attributes

Inherited from:
ForkJoinParallelCpgPassWithAccumulator

Value members

Abstract methods

def runOnPart(builder: DiffGraphBuilder, part: T): Unit

Process a single part and record graph modifications in the provided builder.

Process a single part and record graph modifications in the provided builder.

Value parameters

builder

the DiffGraphBuilder that accumulates graph modifications

part

the part to process, as produced by generateParts

Attributes

Concrete methods

override def createAccumulator(): Null

Create a fresh accumulator instance. Called once per parallel worker thread.

Create a fresh accumulator instance. Called once per parallel worker thread.

Attributes

Definition Classes
override def mergeAccumulator(left: Null, accumulator: Null): Unit

Merge the accumulator (right) into left. Called during the combine phase of fork/join.

Merge the accumulator (right) into left. Called during the combine phase of fork/join.

Attributes

Definition Classes
override def onAccumulatorComplete(builder: DiffGraphBuilder, accumulator: Null): Unit

Called once after all parts are processed and accumulators are merged. Use to record additional graph changes based on the fully merged accumulator.

Called once after all parts are processed and accumulators are merged. Use to record additional graph changes based on the fully merged accumulator.

Value parameters

accumulator

the final merged accumulator

builder

the DiffGraphBuilder for any additional modifications

Attributes

Definition Classes
override def runOnPart(builder: DiffGraphBuilder, part: T, acc: Null): Unit

Process a single part, recording graph changes in builder and side results in accumulator.

Process a single part, recording graph changes in builder and side results in accumulator.

Value parameters

accumulator

the thread-local accumulator for this worker

builder

the DiffGraphBuilder that accumulates graph modifications

part

the part to process

Attributes

Definition Classes

Inherited methods

protected def baseLogger: Logger

Attributes

Inherited from:
CpgPassBase
override def createAndApply(): Unit

Creates a new DiffGraphBuilder, runs the pass (init, generateParts, runOnPart, finish), applies all accumulated changes to the graph, and logs timing information. Exceptions during execution are logged and re-thrown.

Creates a new DiffGraphBuilder, runs the pass (init, generateParts, runOnPart, finish), applies all accumulated changes to the graph, and logs timing information. Exceptions during execution are logged and re-thrown.

Attributes

Definition Classes
Inherited from:
ForkJoinParallelCpgPassWithAccumulator
def finish(): Unit

Called once after all parts have been processed (in a finally block). Use to release resources acquired in init.

Called once after all parts have been processed (in a finally block). Use to release resources acquired in init.

Attributes

Inherited from:
ForkJoinParallelCpgPassWithAccumulator
protected def generateOutFileName(prefix: String, outName: String, index: Int): String

Attributes

Inherited from:
CpgPassBase
def init(): Unit

Called once before generateParts. Use to set up large data structures or acquire external resources.

Called once before generateParts. Use to set up large data structures or acquire external resources.

Attributes

Inherited from:
ForkJoinParallelCpgPassWithAccumulator
def isParallel: Boolean

Override and return false to disable parallel execution. Useful for debugging.

Override and return false to disable parallel execution. Useful for debugging.

Attributes

Inherited from:
ForkJoinParallelCpgPassWithAccumulator
def name: String

Name of the pass. By default it is inferred from the name of the class, override if needed.

Name of the pass. By default it is inferred from the name of the class, override if needed.

Attributes

Inherited from:
CpgPassBase
override def runWithBuilder(externalBuilder: DiffGraphBuilder): Int

Runs the full pass lifecycle (init, generateParts, parallel runOnPart, accumulator merge, finish) and absorbs all changes into externalBuilder without applying them to the graph. The caller is responsible for applying the builder.

Runs the full pass lifecycle (init, generateParts, parallel runOnPart, accumulator merge, finish) and absorbs all changes into externalBuilder without applying them to the graph. The caller is responsible for applying the builder.

Value parameters

externalBuilder

the builder to absorb all generated changes into

Attributes

Returns

the number of parts that were processed

Definition Classes
Inherited from:
ForkJoinParallelCpgPassWithAccumulator
def runWithBuilderLogged(builder: DiffGraphBuilder): Int

Wraps runWithBuilder with logging and exception handling. Use with caution — API is unstable.

Wraps runWithBuilder with logging and exception handling. Use with caution — API is unstable.

Value parameters

builder

the DiffGraphBuilder to absorb changes into

Attributes

Returns

the number of parts processed, or -1 if the pass threw an exception

Inherited from:
CpgPassBase
protected def withStartEndTimesLogged[A](fun: => A): A

Executes fun while logging the pass start and completion time (including duration via MDC).

Executes fun while logging the pass start and completion time (including duration via MDC).

Type parameters

A

the return type of the wrapped computation

Value parameters

fun

the computation to execute

Attributes

Returns

the result of fun

Inherited from:
CpgPassBase

Inherited and Abstract methods

def generateParts(): Array[_ <: AnyRef]

Generate an array of parts to be processed in parallel by runOnPart.

Generate an array of parts to be processed in parallel by runOnPart.

Attributes

Inherited from:
ForkJoinParallelCpgPassWithAccumulator

Deprecated and Inherited methods

override def createApplySerializeAndStore(serializedCpg: SerializedCpg, prefix: String = ...): Unit

Attributes

Deprecated
Please use createAndApply
Definition Classes
Inherited from:
ForkJoinParallelCpgPassWithAccumulator
protected def store(overlay: GeneratedMessageV3, name: String, serializedCpg: SerializedCpg): Unit

Attributes

Deprecated
true
Inherited from:
CpgPassBase