Skip to content

Commit a13b6ed

Browse files
authored
Merge pull request github#12536 from hvitved/dataflow/call-enclosing-callable-consistency-check
Data flow: Add consistency check for `DataFlowCall::getEnclosingCallable`
2 parents b3a49ab + 404ead8 commit a13b6ed

File tree

40 files changed

+215
-0
lines changed

40 files changed

+215
-0
lines changed

cpp/ql/lib/experimental/semmle/code/cpp/ir/dataflow/internal/DataFlowImplConsistency.qll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ module Consistency {
1818
/** Holds if `n` should be excluded from the consistency test `uniqueEnclosingCallable`. */
1919
predicate uniqueEnclosingCallableExclude(Node n) { none() }
2020

21+
/** Holds if `call` should be excluded from the consistency test `uniqueCallEnclosingCallable`. */
22+
predicate uniqueCallEnclosingCallableExclude(DataFlowCall call) { none() }
23+
2124
/** Holds if `n` should be excluded from the consistency test `uniqueNodeLocation`. */
2225
predicate uniqueNodeLocationExclude(Node n) { none() }
2326

@@ -86,6 +89,15 @@ module Consistency {
8689
)
8790
}
8891

92+
query predicate uniqueCallEnclosingCallable(DataFlowCall call, string msg) {
93+
exists(int c |
94+
c = count(call.getEnclosingCallable()) and
95+
c != 1 and
96+
not any(ConsistencyConfiguration conf).uniqueCallEnclosingCallableExclude(call) and
97+
msg = "Call should have one enclosing callable but has " + c + "."
98+
)
99+
}
100+
89101
query predicate uniqueType(Node n, string msg) {
90102
exists(int c |
91103
n instanceof RelevantNode and

cpp/ql/lib/semmle/code/cpp/dataflow/internal/DataFlowImplConsistency.qll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ module Consistency {
1818
/** Holds if `n` should be excluded from the consistency test `uniqueEnclosingCallable`. */
1919
predicate uniqueEnclosingCallableExclude(Node n) { none() }
2020

21+
/** Holds if `call` should be excluded from the consistency test `uniqueCallEnclosingCallable`. */
22+
predicate uniqueCallEnclosingCallableExclude(DataFlowCall call) { none() }
23+
2124
/** Holds if `n` should be excluded from the consistency test `uniqueNodeLocation`. */
2225
predicate uniqueNodeLocationExclude(Node n) { none() }
2326

@@ -86,6 +89,15 @@ module Consistency {
8689
)
8790
}
8891

92+
query predicate uniqueCallEnclosingCallable(DataFlowCall call, string msg) {
93+
exists(int c |
94+
c = count(call.getEnclosingCallable()) and
95+
c != 1 and
96+
not any(ConsistencyConfiguration conf).uniqueCallEnclosingCallableExclude(call) and
97+
msg = "Call should have one enclosing callable but has " + c + "."
98+
)
99+
}
100+
89101
query predicate uniqueType(Node n, string msg) {
90102
exists(int c |
91103
n instanceof RelevantNode and

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/DataFlowImplConsistency.qll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ module Consistency {
1818
/** Holds if `n` should be excluded from the consistency test `uniqueEnclosingCallable`. */
1919
predicate uniqueEnclosingCallableExclude(Node n) { none() }
2020

21+
/** Holds if `call` should be excluded from the consistency test `uniqueCallEnclosingCallable`. */
22+
predicate uniqueCallEnclosingCallableExclude(DataFlowCall call) { none() }
23+
2124
/** Holds if `n` should be excluded from the consistency test `uniqueNodeLocation`. */
2225
predicate uniqueNodeLocationExclude(Node n) { none() }
2326

@@ -86,6 +89,15 @@ module Consistency {
8689
)
8790
}
8891

92+
query predicate uniqueCallEnclosingCallable(DataFlowCall call, string msg) {
93+
exists(int c |
94+
c = count(call.getEnclosingCallable()) and
95+
c != 1 and
96+
not any(ConsistencyConfiguration conf).uniqueCallEnclosingCallableExclude(call) and
97+
msg = "Call should have one enclosing callable but has " + c + "."
98+
)
99+
}
100+
89101
query predicate uniqueType(Node n, string msg) {
90102
exists(int c |
91103
n instanceof RelevantNode and

cpp/ql/test/library-tests/dataflow/dataflow-tests/dataflow-consistency.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
uniqueEnclosingCallable
2+
uniqueCallEnclosingCallable
23
uniqueType
34
uniqueNodeLocation
45
missingLocation

cpp/ql/test/library-tests/dataflow/dataflow-tests/dataflow-ir-consistency.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
uniqueEnclosingCallable
2+
uniqueCallEnclosingCallable
23
uniqueType
34
uniqueNodeLocation
45
| BarrierGuard.cpp:2:11:2:13 | (unnamed parameter 0) | Node should have one location but has 6. |

cpp/ql/test/library-tests/dataflow/fields/dataflow-consistency.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ uniqueEnclosingCallable
55
| C.cpp:10:20:10:29 | new | Node should have one enclosing callable but has 0. |
66
| C.cpp:35:24:35:33 | 0 | Node should have one enclosing callable but has 0. |
77
| C.cpp:35:24:35:33 | new | Node should have one enclosing callable but has 0. |
8+
uniqueCallEnclosingCallable
89
uniqueType
910
uniqueNodeLocation
1011
missingLocation

cpp/ql/test/library-tests/dataflow/fields/dataflow-ir-consistency.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
uniqueEnclosingCallable
2+
uniqueCallEnclosingCallable
23
uniqueType
34
uniqueNodeLocation
45
| E.cpp:15:31:15:33 | buf | Node should have one location but has 2. |

cpp/ql/test/library-tests/syntax-zoo/dataflow-consistency.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ uniqueEnclosingCallable
88
| misc.c:210:24:210:24 | 0 | Node should have one enclosing callable but has 0. |
99
| misc.c:210:24:210:28 | ... + ... | Node should have one enclosing callable but has 0. |
1010
| misc.c:210:28:210:28 | 1 | Node should have one enclosing callable but has 0. |
11+
uniqueCallEnclosingCallable
1112
uniqueType
1213
uniqueNodeLocation
1314
| break_labels.c:2:11:2:11 | i | Node should have one location but has 4. |

cpp/ql/test/library-tests/syntax-zoo/dataflow-ir-consistency.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
uniqueEnclosingCallable
2+
uniqueCallEnclosingCallable
23
uniqueType
34
uniqueNodeLocation
45
| aggregateinitializer.c:1:6:1:6 | AliasedDefinition | Node should have one location but has 20. |

csharp/ql/consistency-queries/DataFlowConsistency.ql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import csharp
22
import cil
33
import semmle.code.csharp.dataflow.internal.DataFlowPrivate
44
import semmle.code.csharp.dataflow.internal.DataFlowPublic
5+
import semmle.code.csharp.dataflow.internal.DataFlowDispatch
56
import semmle.code.csharp.dataflow.internal.DataFlowImplConsistency::Consistency
67

78
private class MyConsistencyConfiguration extends ConsistencyConfiguration {
@@ -14,6 +15,15 @@ private class MyConsistencyConfiguration extends ConsistencyConfiguration {
1415
)
1516
}
1617

18+
override predicate uniqueCallEnclosingCallableExclude(DataFlowCall call) {
19+
// TODO: Remove once static initializers are folded into the
20+
// static constructors
21+
exists(ControlFlow::Node cfn |
22+
cfn.getElement() = any(FieldOrProperty f | f.isStatic()).getAChild+() and
23+
cfn = call.getControlFlowNode()
24+
)
25+
}
26+
1727
override predicate uniqueNodeLocationExclude(Node n) {
1828
// Methods with multiple implementations
1929
n instanceof ParameterNode

0 commit comments

Comments
 (0)