Skip to content

Commit

Permalink
More objects
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Feb 18, 2025
1 parent 81a5638 commit 78166a1
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ sealed class AnalysisType {
* The predicate must hold, i.e., all paths fulfill the property/requirement. No path violates the
* property/requirement.
*/
class MustAnalysis() : AnalysisType() {
object Must : AnalysisType() {
override fun createQueryTree(
evalRes: FulfilledAndFailedPaths,
startNode: Node,
Expand All @@ -89,7 +89,7 @@ class MustAnalysis() : AnalysisType() {
/**
* The predicate may hold, i.e., there is at least one path which fulfills the property/requirement.
*/
class MayAnalysis() : AnalysisType() {
object May : AnalysisType() {
override fun createQueryTree(
evalRes: FulfilledAndFailedPaths,
startNode: Node,
Expand All @@ -112,9 +112,9 @@ class MayAnalysis() : AnalysisType() {
*
* The interpretation of the analysis result can be configured as must analysis (all paths have to
* fulfill the criterion) or may analysis (at least one path has to fulfill the criterion) by
* setting the [type] parameter (default: [MayAnalysis]). Note that this only reasons about existing
* DFG paths, and it might not be sufficient if you actually want a guarantee that some action
* always happens with the data. In this case, you may need to check the [executionPath].
* setting the [type] parameter (default: [May]). Note that this only reasons about existing DFG
* paths, and it might not be sufficient if you actually want a guarantee that some action always
* happens with the data. In this case, you may need to check the [executionPath].
*
* The [sensitivities] can also be configured to a certain extent. The analysis can be run as
* interprocedural or intraprocedural analysis. If [earlyTermination] is not `null`, this can be
Expand All @@ -123,15 +123,15 @@ class MayAnalysis() : AnalysisType() {
fun dataFlow(
startNode: Node,
direction: AnalysisDirection = Forward(GraphToFollow.DFG),
type: AnalysisType = MayAnalysis(),
type: AnalysisType = May,
vararg sensitivities: AnalysisSensitivity = FieldSensitive + ContextSensitive,
scope: AnalysisScope = Interprocedural(),
verbose: Boolean = true,
earlyTermination: ((Node) -> Boolean)? = null,
predicate: (Node) -> Boolean,
): QueryTree<Boolean> {
val collectFailedPaths = type is MustAnalysis || verbose
val findAllPossiblePaths = type is MustAnalysis || verbose
val collectFailedPaths = type == Must || verbose
val findAllPossiblePaths = type == Must || verbose
val earlyTermination = { n: Node, ctx: Context -> earlyTermination?.let { it(n) } == true }

val evalRes =
Expand Down Expand Up @@ -162,7 +162,7 @@ fun dataFlow(
*
* The interpretation of the analysis result can be configured as must analysis (all paths have to
* fulfill the criterion) or may analysis (at least one path has to fulfill the criterion) by
* setting the [type] parameter (default: [MayAnalysis]).
* setting the [type] parameter (default: [May]).
*
* The analysis can be run as interprocedural or intraprocedural analysis by setting [scope]. If
* [earlyTermination] is not `null`, this can be used as a criterion to make the query fail if this
Expand All @@ -171,14 +171,14 @@ fun dataFlow(
fun executionPath(
startNode: Node,
direction: AnalysisDirection = Forward(GraphToFollow.EOG),
type: AnalysisType = MayAnalysis(),
type: AnalysisType = May,
scope: AnalysisScope = Interprocedural(),
verbose: Boolean = true,
earlyTermination: ((Node) -> Boolean)? = null,
predicate: (Node) -> Boolean,
): QueryTree<Boolean> {
val collectFailedPaths = type is MustAnalysis || verbose
val findAllPossiblePaths = type is MustAnalysis || verbose
val collectFailedPaths = type == Must || verbose
val findAllPossiblePaths = type == Must || verbose
val earlyTermination = { n: Node, ctx: Context -> earlyTermination?.let { it(n) } == true }

val evalRes =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class DataflowQueriesTest {
startNode = literal5,
direction = Forward(GraphToFollow.DFG),
scope = Intraprocedural(),
type = MayAnalysis(),
type = May,
predicate = { (it.astParent as? CallExpression)?.name?.localName == "baz" },
)
assertTrue(
Expand Down Expand Up @@ -95,7 +95,7 @@ class DataflowQueriesTest {
startNode = literal5,
direction = Forward(GraphToFollow.DFG),
scope = Intraprocedural(1),
type = MayAnalysis(),
type = May,
predicate = { (it.astParent as? CallExpression)?.name?.localName == "baz" },
)
assertFalse(queryResultMayAMax1.value, "The path is just too short to arrive in baz.")
Expand Down Expand Up @@ -125,7 +125,7 @@ class DataflowQueriesTest {
startNode = literal5,
direction = Forward(GraphToFollow.DFG),
scope = Intraprocedural(),
type = MustAnalysis(),
type = Must,
predicate = { (it.astParent as? CallExpression)?.name?.localName == "baz" },
)
assertFalse(
Expand Down Expand Up @@ -154,7 +154,7 @@ class DataflowQueriesTest {
startNode = refB,
direction = Forward(GraphToFollow.DFG),
scope = Intraprocedural(),
type = MayAnalysis(),
type = May,
predicate = { (it.astParent as? CallExpression)?.name?.localName == "baz" },
)
assertTrue(
Expand Down Expand Up @@ -183,7 +183,7 @@ class DataflowQueriesTest {
startNode = refB,
direction = Forward(GraphToFollow.DFG),
scope = Intraprocedural(),
type = MustAnalysis(),
type = Must,
predicate = { (it.astParent as? CallExpression)?.name?.localName == "baz" },
)
assertFalse(
Expand Down Expand Up @@ -229,7 +229,7 @@ class DataflowQueriesTest {
startNode = refB,
direction = Bidirectional(GraphToFollow.DFG),
scope = Intraprocedural(),
type = MayAnalysis(),
type = May,
predicate = {
(it as? Literal<*>)?.value == 5 ||
(it.astParent as? CallExpression)?.name?.localName == "baz"
Expand Down Expand Up @@ -261,7 +261,7 @@ class DataflowQueriesTest {
startNode = refB,
direction = Bidirectional(GraphToFollow.DFG),
scope = Intraprocedural(),
type = MustAnalysis(),
type = Must,
predicate = {
(it as? Literal<*>)?.value == 5 ||
(it.astParent as? CallExpression)?.name?.localName == "baz"
Expand Down Expand Up @@ -326,7 +326,7 @@ class DataflowQueriesTest {
startNode = bazARef,
direction = Backward(GraphToFollow.DFG),
scope = Intraprocedural(),
type = MayAnalysis(),
type = May,
predicate = { (it as? Literal<*>)?.value == 5 },
)
assertTrue(
Expand Down Expand Up @@ -354,7 +354,7 @@ class DataflowQueriesTest {
startNode = bazARef,
direction = Backward(GraphToFollow.DFG),
scope = Intraprocedural(1),
type = MayAnalysis(),
type = May,
predicate = { (it as? Literal<*>)?.value == 5 },
)
assertFalse(
Expand Down Expand Up @@ -387,7 +387,7 @@ class DataflowQueriesTest {
startNode = bazARef,
direction = Backward(GraphToFollow.DFG),
scope = Intraprocedural(),
type = MustAnalysis(),
type = Must,
predicate = { (it as? Literal<*>)?.value == 5 },
)
assertFalse(
Expand Down Expand Up @@ -416,7 +416,7 @@ class DataflowQueriesTest {
startNode = refB,
direction = Backward(GraphToFollow.DFG),
scope = Intraprocedural(),
type = MayAnalysis(),
type = May,
predicate = { (it as? Literal<*>)?.value == 5 },
)
assertFalse(
Expand Down Expand Up @@ -445,7 +445,7 @@ class DataflowQueriesTest {
startNode = refB,
direction = Backward(GraphToFollow.DFG),
scope = Intraprocedural(),
type = MustAnalysis(),
type = Must,
predicate = { (it as? Literal<*>)?.value == 5 },
)
assertFalse(
Expand Down Expand Up @@ -474,7 +474,7 @@ class DataflowQueriesTest {
startNode = refB,
direction = Backward(GraphToFollow.DFG),
scope = Intraprocedural(),
type = MayAnalysis(),
type = May,
predicate = { (it as? Literal<*>)?.value == "bla" },
)
assertTrue(
Expand Down Expand Up @@ -503,7 +503,7 @@ class DataflowQueriesTest {
startNode = refB,
direction = Backward(GraphToFollow.DFG),
scope = Intraprocedural(),
type = MustAnalysis(),
type = Must,
predicate = { (it as? Literal<*>)?.value == "bla" },
)
assertFalse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class ExecutionOrderQueriesTest {
executionPath(
startNode = literal5,
direction = Forward(GraphToFollow.EOG),
type = MayAnalysis(),
type = May,
scope = Intraprocedural(),
predicate = { node -> node == literal10 },
)
Expand All @@ -63,7 +63,7 @@ class ExecutionOrderQueriesTest {
executionPath(
startNode = literal5,
direction = Forward(GraphToFollow.EOG),
type = MustAnalysis(),
type = Must,
scope = Intraprocedural(),
predicate = { node -> node == literal10 },
)
Expand All @@ -76,7 +76,7 @@ class ExecutionOrderQueriesTest {
executionPath(
startNode = literal5,
direction = Forward(GraphToFollow.EOG),
type = MustAnalysis(),
type = Must,
scope = Intraprocedural(),
predicate = { node -> node == callBaz },
)
Expand All @@ -99,7 +99,7 @@ class ExecutionOrderQueriesTest {
executionPath(
startNode = callBaz,
direction = Backward(GraphToFollow.EOG),
type = MayAnalysis(),
type = May,
scope = Intraprocedural(),
predicate = { node -> node == literal10 },
)
Expand All @@ -112,7 +112,7 @@ class ExecutionOrderQueriesTest {
executionPath(
startNode = callBaz,
direction = Backward(GraphToFollow.EOG),
type = MustAnalysis(),
type = Must,
scope = Intraprocedural(),
predicate = { node -> node == literal10 },
)
Expand All @@ -125,7 +125,7 @@ class ExecutionOrderQueriesTest {
executionPath(
startNode = callBaz,
direction = Backward(GraphToFollow.EOG),
type = MustAnalysis(),
type = Must,
scope = Intraprocedural(),
predicate = { node -> node == literal5 },
)
Expand All @@ -152,7 +152,7 @@ class ExecutionOrderQueriesTest {
executionPath(
startNode = literal10,
direction = Bidirectional(GraphToFollow.EOG),
type = MayAnalysis(),
type = May,
scope = Intraprocedural(),
predicate = { node -> node == callBaz },
)
Expand All @@ -165,7 +165,7 @@ class ExecutionOrderQueriesTest {
executionPath(
startNode = literal10,
direction = Bidirectional(GraphToFollow.EOG),
type = MayAnalysis(),
type = May,
scope = Intraprocedural(),
predicate = { node -> node == literal5 },
)
Expand All @@ -181,7 +181,7 @@ class ExecutionOrderQueriesTest {
executionPath(
startNode = literal10,
direction = Bidirectional(GraphToFollow.EOG),
type = MustAnalysis(),
type = Must,
scope = Intraprocedural(),
predicate = { node -> node == callBaz },
)
Expand All @@ -194,7 +194,7 @@ class ExecutionOrderQueriesTest {
executionPath(
startNode = literal10,
direction = Bidirectional(GraphToFollow.EOG),
type = MustAnalysis(),
type = Must,
scope = Intraprocedural(),
predicate = { node -> node == literal5 },
)
Expand All @@ -207,7 +207,7 @@ class ExecutionOrderQueriesTest {
executionPath(
startNode = literal10,
direction = Bidirectional(GraphToFollow.EOG),
type = MustAnalysis(),
type = Must,
scope = Intraprocedural(),
predicate = { node -> node == callBaz || node == literal5 },
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import de.fraunhofer.aisec.cpg.graph.concepts.memory.Memory
import de.fraunhofer.aisec.cpg.graph.concepts.memory.MemoryManagementMode
import de.fraunhofer.aisec.cpg.graph.edges.*
import de.fraunhofer.aisec.cpg.graph.statements.expressions.DeleteExpression
import de.fraunhofer.aisec.cpg.query.MustAnalysis
import de.fraunhofer.aisec.cpg.query.dataFlow
import de.fraunhofer.aisec.cpg.query.executionPath
import de.fraunhofer.aisec.cpg.test.analyze
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ sealed class AnalysisDirection(val graphToFollow: GraphToFollow) {
}
}

/** Follow the order of the EOG */
/** Follow the order of the [graphToFollow] */
class Forward(graphToFollow: GraphToFollow) : AnalysisDirection(graphToFollow) {
override fun pickNextStep(
currentNode: Node,
Expand Down Expand Up @@ -285,7 +285,7 @@ class Forward(graphToFollow: GraphToFollow) : AnalysisDirection(graphToFollow) {
}
}

/** Against the order of the EOG */
/** Against the order of the [graphToFollow] */
class Backward(graphToFollow: GraphToFollow) : AnalysisDirection(graphToFollow) {
override fun pickNextStep(
currentNode: Node,
Expand Down

0 comments on commit 78166a1

Please sign in to comment.