Skip to content

Commit

Permalink
Merge branch 'main' into renovate/org.jetbrains.kotlinx-kover-gradle-…
Browse files Browse the repository at this point in the history
…plugin-0.x
  • Loading branch information
oxisto authored Dec 13, 2024
2 parents 348124c + 03ceabd commit ee3222b
Show file tree
Hide file tree
Showing 45 changed files with 940 additions and 173 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
go-version: 1.21
- name: Setup neo4j
run: |
docker run -d --env NEO4J_AUTH=neo4j/password -p7474:7474 -p7687:7687 neo4j || true
docker run -d --env NEO4J_AUTH=neo4j/password -p7474:7474 -p7687:7687 -e NEO4JLABS_PLUGINS='["apoc"]' neo4j:5 || true
- name: Determine Version
run: |
# determine version from tag
Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ Instead of manually generating or editing the `gradle.properties` file, you can

### For Visualization Purposes

In order to get familiar with the graph itself, you can use the subproject [cpg-neo4j](https://github.com/Fraunhofer-AISEC/cpg/tree/master/cpg-neo4j). It uses this library to generate the CPG for a set of user-provided code files. The graph is then persisted to a [Neo4j](https://neo4j.com/) graph database. The advantage this has for the user, is that Neo4j's visualization software [Neo4j Browser](https://neo4j.com/developer/neo4j-browser/) can be used to graphically look at the CPG nodes and edges, instead of their Java representations.
In order to get familiar with the graph itself, you can use the subproject [cpg-neo4j](./cpg-neo4j). It uses this library to generate the CPG for a set of user-provided code files. The graph is then persisted to a [Neo4j](https://neo4j.com/) graph database. The advantage this has for the user, is that Neo4j's visualization software [Neo4j Browser](https://neo4j.com/developer/neo4j-browser/) can be used to graphically look at the CPG nodes and edges, instead of their Java representations.

Please make sure, that the [APOC](https://neo4j.com/labs/apoc/) plugin is enabled on your neo4j server. It is used in mass-creating nodes and relationships.

For example using docker:
```
docker run -p 7474:7474 -p 7687:7687 -d -e NEO4J_AUTH=neo4j/password -e NEO4JLABS_PLUGINS='["apoc"]' neo4j:5
```

### As Library

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import de.fraunhofer.aisec.cpg.graph.Node
import de.fraunhofer.aisec.cpg.passes.*
import de.fraunhofer.aisec.cpg.passes.configuration.*
import de.fraunhofer.aisec.cpg.passes.inference.DFGFunctionSummaries
import de.fraunhofer.aisec.cpg.persistence.DoNotPersist
import java.io.File
import java.nio.file.Path
import java.util.*
Expand All @@ -51,6 +52,7 @@ import org.slf4j.LoggerFactory
* The configuration for the [TranslationManager] holds all information that is used during the
* translation.
*/
@DoNotPersist
class TranslationConfiguration
private constructor(
/** Definition of additional symbols, mostly useful for C++. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@
package de.fraunhofer.aisec.cpg

import de.fraunhofer.aisec.cpg.graph.Component
import de.fraunhofer.aisec.cpg.persistence.DoNotPersist

/**
* The translation context holds all necessary managers and configurations needed during the
* translation process.
*/
@DoNotPersist
class TranslationContext(
/** The configuration for this translation. */
val config: TranslationConfiguration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import de.fraunhofer.aisec.cpg.graph.edges.unwrapping
import de.fraunhofer.aisec.cpg.helpers.MeasurementHolder
import de.fraunhofer.aisec.cpg.helpers.StatisticsHolder
import de.fraunhofer.aisec.cpg.passes.Pass
import de.fraunhofer.aisec.cpg.persistence.DoNotPersist
import java.util.*
import java.util.concurrent.ConcurrentHashMap
import org.neo4j.ogm.annotation.Relationship
Expand Down Expand Up @@ -92,6 +93,7 @@ class TranslationResult(
* @return the list of all translation units.
*/
@Deprecated(message = "translation units of individual components should be accessed instead")
@DoNotPersist
val translationUnits: List<TranslationUnitDeclaration>
get() {
if (components.size == 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,6 @@ class Assignment(

/** The holder of this assignment */
@JsonIgnore val holder: AssignmentHolder
) : Edge<Node>(value, target as Node)
) : Edge<Node>(value, target as Node) {
override var labels = setOf("ASSIGMENT")
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ fun <T : Edge<Node>> Node.printGraph(
private fun Edge<Node>.label(): String {
val builder = StringBuilder()
builder.append("\"")
builder.append(this.label)
builder.append(this.labels.joinToString(","))

if (this is Dataflow) {
var granularity = this.granularity
Expand Down
11 changes: 9 additions & 2 deletions cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/Node.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ import de.fraunhofer.aisec.cpg.helpers.SubgraphWalker
import de.fraunhofer.aisec.cpg.helpers.neo4j.LocationConverter
import de.fraunhofer.aisec.cpg.helpers.neo4j.NameConverter
import de.fraunhofer.aisec.cpg.passes.*
import de.fraunhofer.aisec.cpg.persistence.DoNotPersist
import de.fraunhofer.aisec.cpg.processing.IVisitable
import de.fraunhofer.aisec.cpg.sarif.PhysicalLocation
import java.util.*
import kotlin.uuid.Uuid
import org.apache.commons.lang3.builder.ToStringBuilder
import org.apache.commons.lang3.builder.ToStringStyle
import org.neo4j.ogm.annotation.*
Expand Down Expand Up @@ -166,7 +168,7 @@ abstract class Node :
var astChildren: List<Node> = listOf()
get() = SubgraphWalker.getAstChildren(this)

@Transient var astParent: Node? = null
@DoNotPersist @Transient var astParent: Node? = null

/** Virtual property for accessing [prevEOGEdges] without property edges. */
@PopulatedByPass(EvaluationOrderGraphPass::class) var prevEOG by unwrapping(Node::prevEOGEdges)
Expand All @@ -189,6 +191,7 @@ abstract class Node :
* Virtual property for accessing [nextDFGEdges] that have a
* [de.fraunhofer.aisec.cpg.graph.edges.flows.FullDataflowGranularity].
*/
@DoNotPersist
@PopulatedByPass(DFGPass::class, ControlFlowSensitiveDFGPass::class)
val prevFullDFG: List<Node>
get() {
Expand All @@ -212,6 +215,7 @@ abstract class Node :
* Virtual property for accessing [nextDFGEdges] that have a
* [de.fraunhofer.aisec.cpg.graph.edges.flows.FullDataflowGranularity].
*/
@DoNotPersist
@PopulatedByPass(DFGPass::class, ControlFlowSensitiveDFGPass::class)
val nextFullDFG: List<Node>
get() {
Expand Down Expand Up @@ -252,7 +256,10 @@ abstract class Node :
var isImplicit = false

/** Required field for object graph mapping. It contains the node id. */
@Id @GeneratedValue var id: Long? = null
@DoNotPersist @Id @GeneratedValue var legacyId: Long? = null

/** Will replace [legacyId] */
var id: Uuid = Uuid.random()

/** Index of the argument if this node is used in a function call or parameter list. */
var argumentIndex = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@
*/
package de.fraunhofer.aisec.cpg.graph

/** This interface represents all objects that can be persisted in a graph database. */
interface Persistable
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ package de.fraunhofer.aisec.cpg.graph.declarations

import de.fraunhofer.aisec.cpg.graph.Node
import de.fraunhofer.aisec.cpg.graph.scopes.Symbol
import de.fraunhofer.aisec.cpg.persistence.DoNotPersist
import org.neo4j.ogm.annotation.NodeEntity

/**
Expand All @@ -40,6 +41,7 @@ import org.neo4j.ogm.annotation.NodeEntity
*/
@NodeEntity
abstract class Declaration : Node() {
@DoNotPersist
val symbol: Symbol
get() {
return this.name.localName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import org.neo4j.ogm.annotation.Relationship
*/
class FieldDeclaration : VariableDeclaration() {
/** Specifies, whether this field declaration is also a definition, i.e. has an initializer. */
private var isDefinition = false
var isDefinition = false

/** If this is only a declaration, this provides a link to the definition of the field. */
@Relationship(value = "DEFINES")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ import de.fraunhofer.aisec.cpg.graph.statements.*
import de.fraunhofer.aisec.cpg.graph.statements.expressions.Block
import de.fraunhofer.aisec.cpg.graph.statements.expressions.Expression
import de.fraunhofer.aisec.cpg.graph.types.Type
import de.fraunhofer.aisec.cpg.persistence.DoNotPersist
import java.util.*
import org.apache.commons.lang3.builder.ToStringBuilder
import org.neo4j.ogm.annotation.Relationship

/** Represents the declaration or definition of a function. */
open class FunctionDeclaration : ValueDeclaration(), DeclarationHolder, EOGStarterHolder {
@Relationship("BODY") var bodyEdge = astOptionalEdgeOf<Statement>()
/** The function body. Usually a [Block]. */
/** The function body. Usualfly a [Block]. */
var body by unwrapping(FunctionDeclaration::bodyEdge)

/** The list of function parameters. */
Expand Down Expand Up @@ -134,16 +135,6 @@ open class FunctionDeclaration : ValueDeclaration(), DeclarationHolder, EOGStart
return parameters.map { it.default }
}

val defaultParameterSignature: List<Type> // TODO: What's this property?
get() =
parameters.map {
if (it.default != null) {
it.type
} else {
unknownType()
}
}

val signatureTypes: List<Type>
get() = parameters.map { it.type }

Expand All @@ -154,6 +145,7 @@ open class FunctionDeclaration : ValueDeclaration(), DeclarationHolder, EOGStart
.toString()
}

@DoNotPersist
override val eogStarters: List<Node>
get() = listOfNotNull(this)

Expand All @@ -179,11 +171,11 @@ open class FunctionDeclaration : ValueDeclaration(), DeclarationHolder, EOGStart
}
}

@DoNotPersist
override val declarations: List<Declaration>
get() {
val list = ArrayList<Declaration>()
list.addAll(parameters)
list.addAll(records)
return list
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import de.fraunhofer.aisec.cpg.graph.*
import de.fraunhofer.aisec.cpg.graph.edges.ast.astEdgesOf
import de.fraunhofer.aisec.cpg.graph.edges.unwrapping
import de.fraunhofer.aisec.cpg.graph.statements.Statement
import de.fraunhofer.aisec.cpg.persistence.DoNotPersist
import java.util.Objects
import org.neo4j.ogm.annotation.Relationship

Expand Down Expand Up @@ -75,6 +76,7 @@ class NamespaceDeclaration : Declaration(), DeclarationHolder, StatementHolder,

override var statements by unwrapping(NamespaceDeclaration::statementEdges)

@DoNotPersist
override val eogStarters: List<Node>
get() {
val list = mutableListOf<Node>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import de.fraunhofer.aisec.cpg.graph.statements.Statement
import de.fraunhofer.aisec.cpg.graph.types.DeclaresType
import de.fraunhofer.aisec.cpg.graph.types.ObjectType
import de.fraunhofer.aisec.cpg.graph.types.Type
import de.fraunhofer.aisec.cpg.persistence.DoNotPersist
import org.apache.commons.lang3.builder.ToStringBuilder
import org.neo4j.ogm.annotation.Relationship
import org.neo4j.ogm.annotation.Transient
Expand Down Expand Up @@ -120,6 +121,7 @@ open class RecordDeclaration :
templateEdges.removeIf { it.end == templateDeclaration }
}

@DoNotPersist
override val declarations: List<Declaration>
get() {
val list = ArrayList<Declaration>()
Expand Down Expand Up @@ -162,6 +164,7 @@ open class RecordDeclaration :
.toString()
}

@DoNotPersist
override val eogStarters: List<Node>
get() {
val list = mutableListOf<Node>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import de.fraunhofer.aisec.cpg.graph.Node
import de.fraunhofer.aisec.cpg.graph.edges.Edge.Companion.propertyEqualsList
import de.fraunhofer.aisec.cpg.graph.edges.ast.astEdgesOf
import de.fraunhofer.aisec.cpg.graph.edges.unwrapping
import de.fraunhofer.aisec.cpg.persistence.DoNotPersist
import org.neo4j.ogm.annotation.NodeEntity
import org.neo4j.ogm.annotation.Relationship

Expand Down Expand Up @@ -81,6 +82,7 @@ abstract class TemplateDeclaration : Declaration(), DeclarationHolder {
return defaults
}

@DoNotPersist
override val declarations: List<Declaration>
get() {
val list = ArrayList<Declaration>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import de.fraunhofer.aisec.cpg.graph.edges.Edge.Companion.propertyEqualsList
import de.fraunhofer.aisec.cpg.graph.edges.ast.astEdgesOf
import de.fraunhofer.aisec.cpg.graph.edges.unwrapping
import de.fraunhofer.aisec.cpg.graph.statements.Statement
import de.fraunhofer.aisec.cpg.persistence.DoNotPersist
import java.util.*
import org.apache.commons.lang3.builder.ToStringBuilder
import org.neo4j.ogm.annotation.Relationship
Expand Down Expand Up @@ -74,6 +75,7 @@ class TranslationUnitDeclaration :
.toString()
}

@DoNotPersist
override val eogStarters: List<Node>
get() {
val list = mutableListOf<Node>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,9 @@ import de.fraunhofer.aisec.cpg.graph.types.TupleType
class TupleDeclaration : VariableDeclaration() {
/** The list of elements in this tuple. */
var elementEdges =
astEdgesOf<VariableDeclaration>(
onAdd = { registerTypeObserver(it.end) },
onRemove = { unregisterTypeObserver(it.end) }
)
astEdgesOf<VariableDeclaration>(onAdd = { registerTypeObserver(it.end) }) {
unregisterTypeObserver(it.end)
}
var elements by unwrapping(TupleDeclaration::elementEdges)

override var name: Name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ open class VariableDeclaration : ValueDeclaration(), HasInitializer, HasType.Typ
if (value is Reference) {
value.resolutionHelper = this
}
}
},
)
/** The (optional) initializer of the declaration. */
override var initializer by unwrapping(VariableDeclaration::initializerEdge)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ abstract class Edge<NodeType : Node> : Persistable, Cloneable {
end = edge.end
}

@Transient open val label: String = "EDGE"
abstract var labels: Set<String>

/**
* The index of this node, if it is stored in an
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ open class AstEdge<T : Node>(start: Node, end: T) : Edge<T>(start, end) {
init {
end.astParent = start
}

override var labels: Set<String> = setOf("AST")
}

/** Creates an [AstEdges] container starting from this node. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,7 @@ class TemplateArgument<NodeType : Node>(

/** A container for [TemplateArgument] edges. */
class TemplateArguments<NodeType : Node>(thisRef: Node) :
AstEdges<NodeType, TemplateArgument<NodeType>>(thisRef, init = ::TemplateArgument)
AstEdges<NodeType, TemplateArgument<NodeType>>(
thisRef,
init = { start, end -> TemplateArgument(start, end) }
)
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class ControlDependence(
dependence = DependenceType.CONTROL
}

override var labels = setOf("CDG")

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is ControlDependence) return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ open class Dataflow(
@JsonIgnore
var granularity: Granularity = default()
) : Edge<Node>(start, end) {
override val label: String = "DFG"
override var labels = setOf("DFG")

override fun equals(other: Any?): Boolean {
if (this === other) return true
Expand Down Expand Up @@ -134,7 +134,7 @@ class ContextSensitiveDataflow(
val callingContext: CallingContext
) : Dataflow(start, end, granularity) {

override val label: String = "DFG"
override var labels = setOf("DFG")

override fun equals(other: Any?): Boolean {
if (this === other) return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ class EvaluationOrder(
result = 31 * result + branch.hashCode()
return result
}

override var labels = setOf("EOG")
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class Invoke(
*/
var dynamicInvoke: Boolean = false,
) : Edge<FunctionDeclaration>(start, end) {
override var labels = setOf("INVOKES")

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is Invoke) return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class Usage(
result = 31 * result + access.hashCode()
return result
}

override var labels = setOf("USAGE")
}

/** A container for [Usage] edges. [NodeType] is necessary because of the Neo4J OGM. */
Expand Down
Loading

0 comments on commit ee3222b

Please sign in to comment.