Packages

  • package root
    Definition Classes
    root
  • package io
    Definition Classes
    root
  • package shiftleft
    Definition Classes
    io
  • package semanticcpg

    Domain specific language for querying code property graphs

    Domain specific language for querying code property graphs

    This is the API reference for the CPG query language, a language to mine code for defects and vulnerabilities both interactively on a code analysis shell (REPL), or using non-interactive scripts.

    Queries written in the CPG query language express graph traversals (see https://en.wikipedia.org/wiki/Graph_traversal). Similar to the standard graph traversal language "Gremlin" (see https://en.wikipedia.org/wiki/Gremlin_(programming_language))) these traversals are formulated as sequences of primitive language elements referred to as "steps". You can think of a step as a small program, similar to a unix shell utility, however, instead of processing lines one by one, the step processes nodes of the graph.

    Starting a traversal

    All traversals begin by selecting a set of start nodes, e.g.,

    cpg.method

    will start the traversal at all methods, while

    cpg.local

    will start at all local variables. The complete list of starting points can be found at

    io.shiftleft.codepropertygraph.Cpg

    Lazy evaluation

    Queries are lazily evaluated, e.g., cpg.method creates a traversal which you can add more steps to. You can, for example, evaluate the traversal by converting it to a list:

    cpg.method.toList

    Since toList is such a common operation, we provide the shorthand l, meaning that

    cpg.method.l

    provides the same result as the former query.

    Properties

    Nodes have "properties", key-value pairs where keys are strings and values are primitive data types such as strings, integers, or Booleans. Properties of nodes can be selected based on their key, e.g.,

    cpg.method.name

    traverses to all method names. Nodes can also be filtered based on properties, e.g.,

    cpg.method.name(".*exec.*")

    traverse to all methods where name matches the regular expression ".*exec.*". You can see a complete list of properties by browsing to the API documentation of the corresponding step. For example, you can find the properties of method nodes at io.shiftleft.semanticcpg.language.types.structure.MethodTraversal.

    Side effects

    Useful if you want to mutate something outside the traversal, or simply debug it: This prints all typeDecl names as it traverses the graph and increments i for each one.

    var i = 0
    cpg.typeDecl.sideEffect{typeTemplate => println(typeTemplate.name); i = i + 1}.exec

    [advanced] Selecting multiple things from your traversal

    If you are interested in multiple things along the way of your traversal, you label anything using the as modulator, and use select at the end. Note that the compiler automatically derived the correct return type as a tuple of the labelled steps, in this case with two elements.

    cpg.method.as("method").definingTypeDecl.as("classDef").select.toList
    // return type: List[(Method, TypeDecl)]

    [advanced] For comprehensions

    You can always start a new traversal from a node, e.g.,

    val someMethod = cpg.method.head
    someMethod.start.parameter.toList

    You can use this e.g. in a for comprehension, which is (in this context) essentially an alternative way to select multiple intermediate things. It is more expressive, but more computationally expensive.

    val query = for {
      method <- cpg.method
      param <- method.start.parameter
    } yield (method.name, param.name)
    
    query.toList
    Definition Classes
    shiftleft
  • package passes
    Definition Classes
    semanticcpg
  • package cfgcreation
    Definition Classes
    passes
  • Cfg
  • CfgCreator
  • CfgEdge

case class Cfg(entryNode: Option[CfgNode] = None, edges: List[CfgEdge] = List(), fringe: List[(CfgNode, CfgEdgeType)] = List(), labeledNodes: Map[String, CfgNode] = Map(), breaks: List[CfgNode] = List(), continues: List[CfgNode] = List(), caseLabels: List[CfgNode] = List(), gotos: List[(CfgNode, String)] = List()) extends Product with Serializable

A control flow graph that is under construction, consisting of:

entryNode

the control flow graph's first node, that is, the node to which a CFG that appends this CFG should attach itself to.

edges

control flow edges between nodes of the code property graph.

fringe

nodes of the CFG for which an outgoing edge type is already known but the destination node is not. These nodes are connected when another CFG is appended to this CFG. In addition to these three core building blocks, we store labels and jump statements that have not been resolved and may be resolvable as parent sub trees or sibblings are translated.

labeledNodes

labels contained in the abstract syntax tree from which this CPG was generated

breaks

unresolved breaks collected along the way

continues

unresolved continues collected along the way

caseLabels

labels beginning with "case"

gotos

unresolved gotos collected along the way

Linear Supertypes
Serializable, Product, Equals, AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. Cfg
  2. Serializable
  3. Product
  4. Equals
  5. AnyRef
  6. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. Protected

Instance Constructors

  1. new Cfg(entryNode: Option[CfgNode] = None, edges: List[CfgEdge] = List(), fringe: List[(CfgNode, CfgEdgeType)] = List(), labeledNodes: Map[String, CfgNode] = Map(), breaks: List[CfgNode] = List(), continues: List[CfgNode] = List(), caseLabels: List[CfgNode] = List(), gotos: List[(CfgNode, String)] = List())

    entryNode

    the control flow graph's first node, that is, the node to which a CFG that appends this CFG should attach itself to.

    edges

    control flow edges between nodes of the code property graph.

    fringe

    nodes of the CFG for which an outgoing edge type is already known but the destination node is not. These nodes are connected when another CFG is appended to this CFG. In addition to these three core building blocks, we store labels and jump statements that have not been resolved and may be resolvable as parent sub trees or sibblings are translated.

    labeledNodes

    labels contained in the abstract syntax tree from which this CPG was generated

    breaks

    unresolved breaks collected along the way

    continues

    unresolved continues collected along the way

    caseLabels

    labels beginning with "case"

    gotos

    unresolved gotos collected along the way

Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##: Int
    Definition Classes
    AnyRef → Any
  3. def ++(other: Cfg): Cfg

    Create a new CFG in which other is appended to this CFG.

    Create a new CFG in which other is appended to this CFG. All nodes of the fringe are connected to other's entry node and the new fringe is other's fringe. The diffgraphs, jumps, and labels are the sum of those present in this and other.

  4. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  5. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  6. val breaks: List[CfgNode]
  7. val caseLabels: List[CfgNode]
  8. def clone(): AnyRef
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.CloneNotSupportedException]) @native() @HotSpotIntrinsicCandidate()
  9. val continues: List[CfgNode]
  10. val edges: List[CfgEdge]
  11. val entryNode: Option[CfgNode]
  12. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  13. val fringe: List[(CfgNode, CfgEdgeType)]
  14. final def getClass(): Class[_ <: AnyRef]
    Definition Classes
    AnyRef → Any
    Annotations
    @native() @HotSpotIntrinsicCandidate()
  15. val gotos: List[(CfgNode, String)]
  16. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  17. val labeledNodes: Map[String, CfgNode]
  18. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  19. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native() @HotSpotIntrinsicCandidate()
  20. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native() @HotSpotIntrinsicCandidate()
  21. def productElementNames: Iterator[String]
    Definition Classes
    Product
  22. final def synchronized[T0](arg0: => T0): T0
    Definition Classes
    AnyRef
  23. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  24. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException]) @native()
  25. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  26. def withFringeEdgeType(cfgEdgeType: CfgEdgeType): Cfg
  27. def withResolvedGotos(): Cfg

    Upon completing traversal of the abstract syntax tree, this method creates CFG edges between gotos and respective labels.

Deprecated Value Members

  1. def finalize(): Unit
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.Throwable]) @Deprecated
    Deprecated

Inherited from Serializable

Inherited from Product

Inherited from Equals

Inherited from AnyRef

Inherited from Any

Ungrouped