Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes to DFG #1382

Draft
wants to merge 155 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
155 commits
Select commit Hold shift + click to select a range
55224ab
Experimental support to run passes in parallel
oxisto Nov 23, 2023
4b78fc3
Experimental support to run passes in parallel
oxisto Nov 23, 2023
a754912
Consider READWRITE of variables as last write location
KuechA Sep 29, 2023
fc0839f
Some fixes for pointer DFG
KuechA Oct 2, 2023
35505d4
Fix bugs
KuechA Oct 10, 2023
78a0979
More fixes
KuechA Oct 11, 2023
b654c02
Fix bugs
KuechA Oct 11, 2023
42b5bbc
Be more open!
KuechA Oct 24, 2023
bbf0629
Hotfix
KuechA Oct 27, 2023
0bd1413
Fix problem of unconnected dfg for unknown functions
KuechA Oct 27, 2023
d676dd7
Fix bug caused by inconsistencies
KuechA Oct 27, 2023
3713d7b
Use comment matcher also in CXX frontend
KuechA Nov 10, 2023
1d540c4
Try more fixes
KuechA Nov 16, 2023
a3841c2
Try more efficient flags
KuechA Nov 21, 2023
4f2db1d
Added comment to be careful about replacing
oxisto Nov 21, 2023
26e1e49
Fixed NPE
oxisto Nov 21, 2023
e909f92
more parallel
oxisto Nov 23, 2023
4f8c894
Rebased with parallel passes
oxisto Nov 24, 2023
34332d1
++
oxisto Nov 28, 2023
33d2d1a
No more DFG Edges to FunctionDeclaration
morbitzer Nov 28, 2023
e395527
Merge branch 'main' into small-dfg-fix
KuechA Jan 11, 2024
ba40f18
Added continueAfterHit flag to followNextDFGEdgesUntilHit
morbitzer Jan 17, 2024
006a8d8
Merge branch 'main' into small-dfg-fix
KuechA Mar 18, 2024
52bdc7e
Merge branch 'main' into small-dfg-fix
KuechA Mar 19, 2024
4ac2dbb
intellij formatting
morbitzer Mar 19, 2024
9075a5e
public methods#
KuechA Mar 20, 2024
6ec8133
Merge branch 'main' into small-dfg-fix
oxisto Mar 27, 2024
521771e
Multi path followDFG
KuechA Apr 4, 2024
bcb9be2
Merge branch 'main' into small-dfg-fix
oxisto Apr 10, 2024
945d414
Merge remote-tracking branch 'origin/main' into small-dfg-fix
oxisto Apr 10, 2024
65c59f9
Merge branch 'main' into small-dfg-fix
KuechA Apr 11, 2024
48d969f
Fix missing import
KuechA Apr 25, 2024
7abad4c
fixed broken EOG in do-while loops
morbitzer Apr 29, 2024
59f2b3e
prevent Functionsummaries from breaking in case of not enough arguments
morbitzer May 5, 2024
3edb188
Prevent CDG from looping endlessly
morbitzer May 5, 2024
5d726eb
fixed merge conflict
morbitzer May 5, 2024
532d2d8
switched alreadySeen to hashCode-List to avoid endless loops
morbitzer May 5, 2024
131f7cd
Merge branch 'main' into small-dfg-fix
KuechA May 7, 2024
bfe9bd6
Only propagate type updates in a `Reference` from FULL previous DFG n…
oxisto May 8, 2024
212c560
Merge branch 'dfg-propagate-fix' into small-dfg-fix
oxisto May 8, 2024
93b36d8
fixed broken alreadySeen checks
morbitzer May 13, 2024
0d628e2
avoid double DFG-Edges
morbitzer May 13, 2024
6ea4323
Node.followNextFullDFGEdgesUntilHit now doesn't stop anymore in neste…
morbitzer May 14, 2024
8ec6b2d
Very ugly hack to avoid loops
oxisto May 21, 2024
01e98d5
Merge remote-tracking branch 'origin/main' into small-dfg-fix
oxisto May 28, 2024
a326770
IncompleteType now has a language
oxisto May 28, 2024
472d4a4
Merge remote-tracking branch 'origin/main' into small-dfg-fix
oxisto Jun 3, 2024
bb6057d
fixed a typo
morbitzer Jun 3, 2024
7d01b9f
Merge branch 'main' into small-dfg-fix
morbitzer Jun 14, 2024
8784d6b
fixy fix
oxisto Jun 17, 2024
63804f4
Differentiate between CallingContexts for edgeProperties
morbitzer Jun 18, 2024
46cd61a
Trying to fix edgePropertiesMap with Aliases
morbitzer Jun 25, 2024
bc1fd32
Started adding PointerDFs
morbitzer Jul 22, 2024
101f617
Remove unneeded DFG-Edges for pointer access
morbitzer Jul 22, 2024
cce64b9
Revert "Remove unneeded DFG-Edges for pointer access"
morbitzer Jul 23, 2024
548b016
Revert "Started adding PointerDFs"
morbitzer Jul 23, 2024
fc41e34
Added PointerDataFlows
morbitzer Jul 26, 2024
983203f
Changed unwrapReference to not remove UnaryOperator
morbitzer Aug 2, 2024
08d4d06
started working on pointer stuff
morbitzer Aug 7, 2024
626c10e
removed valueAccess flag from PointerDataflowGranularity
morbitzer Sep 4, 2024
178213e
Added/Fixed PointerDataFlows
morbitzer Sep 4, 2024
49373d0
started changing DFG for C(++)
morbitzer Sep 4, 2024
d54eff1
Revert "started changing DFG for C(++)"
morbitzer Sep 5, 2024
abae76c
Revert "Added/Fixed PointerDataFlows"
morbitzer Sep 5, 2024
33a7f2d
Introduced PointerReference and PointerDereference Node
morbitzer Sep 5, 2024
8957f84
adapted unwrapReference for PointerReference/PointerDereference
morbitzer Sep 5, 2024
fe4c49b
added missing PointerDereference/PointerReference classes
morbitzer Sep 5, 2024
d3c930c
started adapting AliasCFS Pass to new pointer magic
morbitzer Sep 9, 2024
a713c3f
PointerDataFlowGranularity for PointerReferences
morbitzer Sep 10, 2024
c7a7556
Consider PointerReferences for AliasCFSDFG
morbitzer Sep 23, 2024
2c68aca
Pointer Stuff
morbitzer Oct 7, 2024
a38b853
Merge branch 'main' into small-dfg-fix
maximiliankaul Oct 9, 2024
2c1c054
add prev/nextPDG shorthand
maximiliankaul Oct 10, 2024
0e915fb
Merge branch 'mk/pdgshorthand' into small-dfg-fix
maximiliankaul Oct 10, 2024
fddcc87
Merge branch 'main' into small-dfg-fix
maximiliankaul Oct 10, 2024
218bb19
hashCode and equals for custom granularities
morbitzer Oct 14, 2024
104e91c
fixed hashCode()
morbitzer Oct 14, 2024
25cfa8b
replaced hashCodes with data class
morbitzer Oct 14, 2024
2e4c223
determine granularity based on edgePropertiesMap
morbitzer Oct 14, 2024
31546ab
added input to pointer(de)reference
morbitzer Oct 15, 2024
b899cc4
Try a new eog iteration
KuechA Oct 16, 2024
18edf60
Merge branch 'main' into small-dfg-fix
maximiliankaul Oct 30, 2024
3ba4f87
Fix one test
KuechA Oct 30, 2024
59f108f
Comment out test which does not make sense
KuechA Oct 30, 2024
833630c
Merge branch 'main' into small-dfg-fix
maximiliankaul Nov 6, 2024
0c96c4c
use refers to instead of aliases (which are not known at this point)
maximiliankaul Nov 6, 2024
1c5a7fe
allow other code to rewrite findAndSetProperties
maximiliankaul Nov 6, 2024
ee424f6
Document ignored tests
KuechA Nov 12, 2024
9178d65
Update fixpoint iteration to be faster
KuechA Nov 12, 2024
bfe6f66
Type propagation
KuechA Nov 12, 2024
bc5c5d4
Try to fix tests but without effect
KuechA Nov 12, 2024
c010192
Started on Points to Analysis
morbitzer Nov 15, 2024
b370219
continued on PointsToPass
morbitzer Nov 19, 2024
0bd9b5e
add location to BinaryOperator.toString()
maximiliankaul Nov 19, 2024
ab46dd1
continued again on PointsToPassTest
morbitzer Nov 19, 2024
e887a4b
initial version of PointerPassTest working
morbitzer Nov 20, 2024
9514d44
Added PointsToPassTest
morbitzer Nov 20, 2024
642d4fe
added conditions test to PointsToTest
morbitzer Nov 21, 2024
13c5396
started working on struct-support for PointsToPass
morbitzer Nov 21, 2024
a3d37b0
finished struct support for PointsToPass
morbitzer Nov 22, 2024
0e15fae
added array support for PointsToPass (no test yet)
morbitzer Nov 22, 2024
51513aa
Added Test for PointerPassTest for Arrays
morbitzer Nov 25, 2024
d23367c
started on memcpy support for PointsToPass
morbitzer Nov 25, 2024
de718ae
finished memcpy support for PointsToPass
morbitzer Nov 25, 2024
5e98082
Added PointerToPointer test for PointersToPass
morbitzer Nov 26, 2024
2df3476
TODOs for context sensitive analysis
KuechA Nov 27, 2024
6e04278
Initial try with EOG ordering in pass
KuechA Nov 27, 2024
9290c62
Update
KuechA Nov 27, 2024
518074f
Update
KuechA Nov 27, 2024
aa86831
started using PlaceholderMemoryValue and UnknownMemoryValue
morbitzer Nov 27, 2024
1863196
fix goto / label missing names
maximiliankaul Nov 28, 2024
242edfb
code review
maximiliankaul Nov 28, 2024
add4710
test++
maximiliankaul Nov 28, 2024
6f484a4
test++
maximiliankaul Nov 28, 2024
13ada9d
Added FunctionSummaries to PointsToPass
morbitzer Dec 3, 2024
f927edc
continued on FunctionSummaries
morbitzer Dec 3, 2024
93e9ad3
more functionSummaries handling
morbitzer Dec 4, 2024
505fcab
cleaned up
morbitzer Dec 4, 2024
a9ebfcb
Merge branch 'main' into small-dfg-fix
KuechA Dec 4, 2024
60b9c0c
Fix spotless
KuechA Dec 5, 2024
252c688
Fix query test
KuechA Dec 5, 2024
adefc32
Fix query test
KuechA Dec 5, 2024
1745f1f
Revert "Fix query test"
KuechA Dec 5, 2024
398b3ec
Revert "Fix query test"
KuechA Dec 5, 2024
1c51494
Revert "Fix spotless"
KuechA Dec 5, 2024
bacfefa
Merge branch 'main' into small-dfg-fix
KuechA Dec 5, 2024
478dc01
Improved on Function Summaries
morbitzer Dec 5, 2024
c55f51c
moved memoryValue to prevDFG
morbitzer Dec 9, 2024
ef0559d
got things running again
morbitzer Dec 9, 2024
3349ec6
continued on functionSummaries
morbitzer Dec 9, 2024
f9dc30b
tests work again but ugly
morbitzer Dec 10, 2024
451bf8d
Fix some bugs
KuechA Dec 10, 2024
1948ca3
Add value only once if unknown
KuechA Dec 10, 2024
c521a21
First working version of function Summaries
morbitzer Dec 10, 2024
d2224e2
finished functionSummaries
morbitzer Dec 11, 2024
3bfe2fc
fixed resolving of literals
morbitzer Dec 11, 2024
2558fb5
fixed GhidraTest
morbitzer Dec 12, 2024
c9df04a
attached function Summaries to functionDeclaration
morbitzer Dec 12, 2024
831547a
FunctionSummaries now also include partial writes
morbitzer Dec 13, 2024
63013f6
Merge branch 'main' into small-dfg-fix
KuechA Dec 17, 2024
92533e6
Add sanity check
KuechA Dec 17, 2024
b80db86
Some fixes
KuechA Dec 17, 2024
6ed5357
draw dfg edges via call expressions
morbitzer Dec 18, 2024
fde0c20
bugfix
morbitzer Dec 18, 2024
c3e20d4
More size checks
KuechA Dec 18, 2024
baa2a73
Fix missing edges to parameter values
KuechA Dec 18, 2024
2a98f1a
add check
KuechA Dec 18, 2024
70d0b10
better function summaries processing
morbitzer Dec 19, 2024
d637020
Remove duplicated code
KuechA Dec 19, 2024
9e05ca1
initial version using a set
KuechA Dec 19, 2024
f40fd06
working ghidra test
morbitzer Dec 19, 2024
e423e5b
more logical updateValues
morbitzer Dec 20, 2024
5a510e3
include pointer-to-pointer in functionSummary
morbitzer Dec 20, 2024
d8b34bd
finished ghidraTest
morbitzer Dec 21, 2024
28c332f
functionSummaries now also include the names on partial writes
morbitzer Dec 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ import de.fraunhofer.aisec.cpg.graph.edges.flows.EvaluationOrder
import de.fraunhofer.aisec.cpg.graph.statements.IfStatement
import de.fraunhofer.aisec.cpg.graph.statements.WhileStatement
import de.fraunhofer.aisec.cpg.helpers.*
import de.fraunhofer.aisec.cpg.helpers.LatticeElement
import de.fraunhofer.aisec.cpg.passes.configuration.DependsOn

/**
* A [Pass] which uses a simple logic to determine constant values and mark unreachable code regions
* by setting the [EvaluationOrder.unreachable] property to true.
*/
@DependsOn(ControlFlowSensitiveDFGPass::class)
@DependsOn(ControlFlowSensitiveDFGPass::class, softDependency = true)
class UnreachableEOGPass(ctx: TranslationContext) : TranslationUnitPass(ctx) {
override fun cleanup() {
// Nothing to do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,16 @@ fun dataFlow(
from: Node,
predicate: (Node) -> Boolean,
collectFailedPaths: Boolean = true,
findAllPossiblePaths: Boolean = true
findAllPossiblePaths: Boolean = true,
continueAfterHit: Boolean = false
): QueryTree<Boolean> {
val evalRes =
from.followNextFullDFGEdgesUntilHit(collectFailedPaths, findAllPossiblePaths, predicate)
from.followNextFullDFGEdgesUntilHit(
collectFailedPaths,
findAllPossiblePaths,
continueAfterHit,
predicate
)
val allPaths = evalRes.fulfilled.map { QueryTree(it) }.toMutableList()
if (collectFailedPaths) allPaths.addAll(evalRes.failed.map { QueryTree(it) })
return QueryTree(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ private constructor(
registerPass<DynamicInvokeResolver>()
registerPass<EvaluationOrderGraphPass>() // creates EOG
registerPass<TypeResolver>()
registerPass<ControlFlowSensitiveDFGPass>()
// registerPass<ControlFlowSensitiveDFGPass>()
registerPass<FilenameMapper>()
registerPass<ResolveCallExpressionAmbiguityPass>()
registerPass<ResolveMemberExpressionAmbiguityPass>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,48 @@ fun MetadataProvider.newReference(
return node
}

/**
* Creates a new [PointerReference]. The [MetadataProvider] receiver will be used to fill different
* meta-data using [Node.applyMetadata]. Calling this extension function outside of Kotlin requires
* an appropriate [MetadataProvider], such as a [LanguageFrontend] as an additional prepended
* argument.
*/
@JvmOverloads
fun MetadataProvider.newPointerReference(
name: CharSequence?,
type: Type = unknownType(),
rawNode: Any? = null
): PointerReference {
val node = PointerReference()
node.applyMetadata(this, name, rawNode, true)

node.type = type

log(node)
return node
}

/**
* Creates a new [PointerReference]. The [MetadataProvider] receiver will be used to fill different
* meta-data using [Node.applyMetadata]. Calling this extension function outside of Kotlin requires
* an appropriate [MetadataProvider], such as a [LanguageFrontend] as an additional prepended
* argument.
*/
@JvmOverloads
fun MetadataProvider.newPointerDereference(
name: CharSequence?,
type: Type = unknownType(),
rawNode: Any? = null
): PointerDereference {
val node = PointerDereference()
node.applyMetadata(this, name, rawNode, true)

node.type = type

log(node)
return node
}

/**
* Creates a new [DeleteExpression]. The [MetadataProvider] receiver will be used to fill different
* meta-data using [Node.applyMetadata]. Calling this extension function outside of Kotlin requires
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ fun Node.followXUntilHit(
x: (Node) -> List<Node>,
collectFailedPaths: Boolean = true,
findAllPossiblePaths: Boolean = true,
continueAfterHit: Boolean = false,
predicate: (Node) -> Boolean
): FulfilledAndFailedPaths {
// Looks complicated but at least it's not recursive...
Expand Down Expand Up @@ -517,9 +518,18 @@ fun Node.followXUntilHit(
}
// The next node is new in the current path (i.e., there's no loop), so we add the path
// with the next step to the worklist.
// For our daily dose of special magic, we check that the path reaching the next node
// differs. If the path is different, we do accept seeing the same node multiple times.
val indexedPath =
currentPath
.mapIndexed { index, node -> if (node == next) Pair(index, node) else null }
.filterNotNull()
if (
next !in currentPath &&
(findAllPossiblePaths ||
(indexedPath.isEmpty() ||
indexedPath.all {
it.first == 0 || currentNode != currentPath[it.first - 1]
}) &&
((findAllPossiblePaths && currentPath.count { it == next } <= 2) ||
(next !in alreadySeenNodes && worklist.none { next in it }))
) {
worklist.add(nextPath)
Expand All @@ -542,12 +552,14 @@ fun Node.followXUntilHit(
fun Node.followNextFullDFGEdgesUntilHit(
collectFailedPaths: Boolean = true,
findAllPossiblePaths: Boolean = true,
continueAfterHit: Boolean = true,
predicate: (Node) -> Boolean
): FulfilledAndFailedPaths {
return followXUntilHit(
x = { currentNode -> currentNode.nextFullDFG },
collectFailedPaths = collectFailedPaths,
findAllPossiblePaths = findAllPossiblePaths,
continueAfterHit = continueAfterHit,
predicate = predicate
)
}
Expand Down Expand Up @@ -1001,8 +1013,8 @@ private fun Node.eogDistanceTo(to: Node): Int {
fun Expression?.unwrapReference(): Reference? {
return when {
this is Reference -> this
this is UnaryOperator && (this.operatorCode == "*" || this.operatorCode == "&") ->
this.input.unwrapReference()
this is PointerReference -> this
this is PointerDereference -> this
this is CastExpression -> this.expression.unwrapReference()
else -> null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ package de.fraunhofer.aisec.cpg.graph
import de.fraunhofer.aisec.cpg.graph.edges.Edge
import de.fraunhofer.aisec.cpg.graph.edges.flows.Dataflow
import de.fraunhofer.aisec.cpg.graph.edges.flows.PartialDataflowGranularity
import de.fraunhofer.aisec.cpg.graph.edges.flows.PointerDataflowGranularity
import de.fraunhofer.aisec.cpg.helpers.identitySetOf
import kotlin.reflect.KProperty1

Expand Down Expand Up @@ -115,6 +116,8 @@ private fun Edge<Node>.label(): String {
var granularity = this.granularity
if (granularity is PartialDataflowGranularity) {
builder.append(" (partial, ${granularity.partialTarget?.name})")
} else if (granularity is PointerDataflowGranularity) {
builder.append(" (pointer, ${granularity.pointerTarget.name})")
} else {
builder.append(" (full)")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import kotlin.uuid.Uuid
*/
class Name(
/** The local name (sometimes also called simple name) without any namespace information. */
val localName: String,
var localName: String,
/** The parent name, e.g., the namespace this name lives in. */
val parent: Name? = null,
/** A potential namespace delimiter, usually either `.` or `::`. */
Expand Down
12 changes: 6 additions & 6 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 @@ -178,21 +178,21 @@ abstract class Node :

/** Incoming data flow edges */
@Relationship(value = "DFG", direction = Relationship.Direction.INCOMING)
@PopulatedByPass(DFGPass::class, ControlFlowSensitiveDFGPass::class)
@PopulatedByPass(DFGPass::class, PointsToPass::class)
var prevDFGEdges: Dataflows<Node> =
Dataflows<Node>(this, mirrorProperty = Node::nextDFGEdges, outgoing = false)
protected set

/** Virtual property for accessing [prevDFGEdges] without property edges. */
@PopulatedByPass(DFGPass::class, ControlFlowSensitiveDFGPass::class)
@PopulatedByPass(DFGPass::class, PointsToPass::class)
var prevDFG by unwrapping(Node::prevDFGEdges)

/**
* Virtual property for accessing [nextDFGEdges] that have a
* [de.fraunhofer.aisec.cpg.graph.edges.flows.FullDataflowGranularity].
*/
@DoNotPersist
@PopulatedByPass(DFGPass::class, ControlFlowSensitiveDFGPass::class)
@PopulatedByPass(DFGPass::class, PointsToPass::class, ControlFlowSensitiveDFGPass::class)
val prevFullDFG: List<Node>
get() {
return prevDFGEdges
Expand All @@ -201,22 +201,22 @@ abstract class Node :
}

/** Outgoing data flow edges */
@PopulatedByPass(DFGPass::class, ControlFlowSensitiveDFGPass::class)
@PopulatedByPass(DFGPass::class, PointsToPass::class)
@Relationship(value = "DFG", direction = Relationship.Direction.OUTGOING)
var nextDFGEdges: Dataflows<Node> =
Dataflows<Node>(this, mirrorProperty = Node::prevDFGEdges, outgoing = true)
protected set

/** Virtual property for accessing [nextDFGEdges] without property edges. */
@PopulatedByPass(DFGPass::class, ControlFlowSensitiveDFGPass::class)
@PopulatedByPass(DFGPass::class, PointsToPass::class)
var nextDFG by unwrapping(Node::nextDFGEdges)

/**
* Virtual property for accessing [nextDFGEdges] that have a
* [de.fraunhofer.aisec.cpg.graph.edges.flows.FullDataflowGranularity].
*/
@DoNotPersist
@PopulatedByPass(DFGPass::class, ControlFlowSensitiveDFGPass::class)
@PopulatedByPass(DFGPass::class, PointsToPass::class, ControlFlowSensitiveDFGPass::class)
val nextFullDFG: List<Node>
get() {
return nextDFGEdges.filter { it.granularity is FullDataflowGranularity }.map { it.end }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2021, Fraunhofer AISEC. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* $$$$$$\ $$$$$$$\ $$$$$$\
* $$ __$$\ $$ __$$\ $$ __$$\
* $$ / \__|$$ | $$ |$$ / \__|
* $$ | $$$$$$$ |$$ |$$$$\
* $$ | $$ ____/ $$ |\_$$ |
* $$ | $$\ $$ | $$ | $$ |
* \$$$$$ |$$ | \$$$$$ |
* \______/ \__| \______/
*
*/
package de.fraunhofer.aisec.cpg.graph

/** A Dataflow for a pointer can have different types: Either to its address, or to its value. */
enum class PointerAccess {
ADDRESS,
VALUE
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fun LanguageProvider.autoType(): Type {
}

fun MetadataProvider?.incompleteType(): Type {
return IncompleteType()
return IncompleteType((this as? LanguageProvider)?.language)
}

/** Returns a [PointerType] that describes an array reference to the current type. */
Expand Down
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.graph.statements.expressions.MemoryAddress
import de.fraunhofer.aisec.cpg.persistence.DoNotPersist
import org.neo4j.ogm.annotation.NodeEntity

Expand All @@ -46,4 +47,12 @@ abstract class Declaration : Node() {
get() {
return this.name.localName
}

/**
* Each Declaration allocates new memory, AKA a new address, so we create a new MemoryAddress
* node
*/
open lateinit var memoryAddress: MemoryAddress

fun memoryAddressIsInitialized() = ::memoryAddress.isInitialized
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ open class FunctionDeclaration : ValueDeclaration(), DeclarationHolder, EOGStart
return if (isDefinition) this else field
}

/**
* Saves the information on which parameter(s) of the function are modified by the function.
* This is interesting since we need to add DFG edges between the modified parameter and the
* respective argument(s). For each [ParameterDeclaration] as well as the
* [MethodDeclaration.receiver] that has some incoming DFG-edge within this
* [FunctionDeclaration], we store all previous DFG nodes. The map stores a Pair of Nodes and
* Booleans. The Node indicates the new source value, and the Boolean indicates if the node
* should be dereferenced. Additionally, we use the String to indicate sub-accesses, i.e. to
* parts of a struct or to array-expressions
*/
var functionSummary = mutableMapOf<Node, MutableSet<Triple<Node, Boolean, String>>>()

/** Returns true, if this function has a [body] statement. */
fun hasBody(): Boolean {
return body != null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,22 @@
package de.fraunhofer.aisec.cpg.graph.declarations

import de.fraunhofer.aisec.cpg.graph.HasDefault
import de.fraunhofer.aisec.cpg.graph.Name
import de.fraunhofer.aisec.cpg.graph.edges.ast.astOptionalEdgeOf
import de.fraunhofer.aisec.cpg.graph.edges.unwrapping
import de.fraunhofer.aisec.cpg.graph.statements.expressions.Expression
import de.fraunhofer.aisec.cpg.graph.statements.expressions.ParameterMemoryValue
import java.util.*
import org.neo4j.ogm.annotation.Relationship

/** A declaration of a function or nontype template parameter. */
class ParameterDeclaration : ValueDeclaration(), HasDefault<Expression?> {
var isVariadic = false

var memoryValue: ParameterMemoryValue = ParameterMemoryValue(Name("value")) /*.apply {
memoryAddress = this@ParameterDeclaration.memoryAddress
}*/

@Relationship(value = "DEFAULT", direction = Relationship.Direction.OUTGOING)
var defaultValueEdge = astOptionalEdgeOf<Expression>()
private var defaultValue by unwrapping(ParameterDeclaration::defaultValueEdge)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ import de.fraunhofer.aisec.cpg.graph.edges.ast.astEdgesOf
import de.fraunhofer.aisec.cpg.graph.edges.ast.astOptionalEdgeOf
import de.fraunhofer.aisec.cpg.graph.edges.unwrapping
import de.fraunhofer.aisec.cpg.graph.scopes.GlobalScope
import de.fraunhofer.aisec.cpg.graph.statements.expressions.ConstructExpression
import de.fraunhofer.aisec.cpg.graph.statements.expressions.Expression
import de.fraunhofer.aisec.cpg.graph.statements.expressions.Reference
import de.fraunhofer.aisec.cpg.graph.statements.expressions.*
import de.fraunhofer.aisec.cpg.graph.types.AutoType
import de.fraunhofer.aisec.cpg.graph.types.HasType
import de.fraunhofer.aisec.cpg.graph.types.TupleType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,18 @@ package de.fraunhofer.aisec.cpg.graph.edges.flows

import com.fasterxml.jackson.annotation.JsonIgnore
import de.fraunhofer.aisec.cpg.graph.Node
import de.fraunhofer.aisec.cpg.graph.PointerAccess
import de.fraunhofer.aisec.cpg.graph.declarations.*
import de.fraunhofer.aisec.cpg.graph.declarations.Declaration
import de.fraunhofer.aisec.cpg.graph.declarations.FieldDeclaration
import de.fraunhofer.aisec.cpg.graph.declarations.TupleDeclaration
import de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration
import de.fraunhofer.aisec.cpg.graph.edges.Edge
import de.fraunhofer.aisec.cpg.graph.edges.collections.EdgeSet
import de.fraunhofer.aisec.cpg.graph.edges.collections.MirroredEdgeCollection
import de.fraunhofer.aisec.cpg.graph.statements.expressions.*
import de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression
import de.fraunhofer.aisec.cpg.graph.statements.expressions.MemberExpression
import de.fraunhofer.aisec.cpg.graph.types.HasType
import de.fraunhofer.aisec.cpg.helpers.neo4j.DataflowGranularityConverter
import kotlin.reflect.KProperty
Expand All @@ -53,12 +60,21 @@ sealed interface Granularity
*/
data object FullDataflowGranularity : Granularity

/**
* This dataflow granularity denotes that the value or address of a pointer is flowing from
* [Dataflow.start] to [Dataflow.end].
*/
data class PointerDataflowGranularity(
/** Does the Dataflow affect the pointer's address or its value? */
val pointerTarget: PointerAccess
) : Granularity

/**
* This dataflow granularity denotes that not the "whole" object is flowing from [Dataflow.start] to
* [Dataflow.end] but only parts of it. Common examples include [MemberExpression] nodes, where we
* model a dataflow to the base, but only partially scoped to a particular field.
*/
class PartialDataflowGranularity(
data class PartialDataflowGranularity(
/** The target that is affected by this partial dataflow. */
val partialTarget: Declaration?
) : Granularity
Expand All @@ -80,6 +96,14 @@ fun partial(target: Declaration?): PartialDataflowGranularity {
return PartialDataflowGranularity(target)
}

/**
* Creates a new [PointerDataflowGranularity]. The [ValueAccess] is specified if the pointer's value
* is accessed, or its address.
*/
fun pointer(access: PointerAccess): PointerDataflowGranularity {
return PointerDataflowGranularity(access)
}

/**
* This edge class defines a flow of data between [start] and [end]. The flow can have a certain
* [granularity].
Expand Down
Loading
Loading