Skip to content

Commit 6a1504b

Browse files
committed
C++: Slightly refactor test QL files so that we can add a test which tests the nodes being selected.
1 parent 4d4ca6b commit 6a1504b

File tree

3 files changed

+113
-105
lines changed

3 files changed

+113
-105
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
module AstTest {
2+
import semmle.code.cpp.dataflow.DataFlow
3+
private import semmle.code.cpp.controlflow.Guards
4+
5+
/**
6+
* A `BarrierGuard` that stops flow to all occurrences of `x` within statement
7+
* S in `if (guarded(x)) S`.
8+
*/
9+
// This is tested in `BarrierGuard.cpp`.
10+
predicate testBarrierGuard(GuardCondition g, Expr checked, boolean isTrue) {
11+
g.(FunctionCall).getTarget().getName() = "guarded" and
12+
checked = g.(FunctionCall).getArgument(0) and
13+
isTrue = true
14+
}
15+
16+
/** Common data flow configuration to be used by tests. */
17+
module AstTestAllocationConfig implements DataFlow::ConfigSig {
18+
predicate isSource(DataFlow::Node source) {
19+
source.asExpr().(FunctionCall).getTarget().getName() = "source"
20+
or
21+
source.asParameter().getName().matches("source%")
22+
or
23+
source.asExpr().(FunctionCall).getTarget().getName() = "indirect_source"
24+
or
25+
source.(DataFlow::DefinitionByReferenceNode).getParameter().getName().matches("ref_source%")
26+
or
27+
// Track uninitialized variables
28+
exists(source.asUninitialized())
29+
}
30+
31+
predicate isSink(DataFlow::Node sink) {
32+
exists(FunctionCall call |
33+
call.getTarget().getName() = ["sink", "indirect_sink"] and
34+
sink.asExpr() = call.getAnArgument()
35+
)
36+
}
37+
38+
predicate isBarrier(DataFlow::Node barrier) {
39+
barrier.asExpr().(VariableAccess).getTarget().hasName("barrier") or
40+
barrier = DataFlow::BarrierGuard<testBarrierGuard/3>::getABarrierNode()
41+
}
42+
}
43+
44+
module AstFlow = DataFlow::Global<AstTestAllocationConfig>;
45+
}
46+
47+
module IRTest {
48+
private import cpp
49+
import semmle.code.cpp.ir.dataflow.DataFlow
50+
private import semmle.code.cpp.ir.IR
51+
private import semmle.code.cpp.controlflow.IRGuards
52+
53+
/**
54+
* A `BarrierGuard` that stops flow to all occurrences of `x` within statement
55+
* S in `if (guarded(x)) S`.
56+
*/
57+
// This is tested in `BarrierGuard.cpp`.
58+
predicate testBarrierGuard(IRGuardCondition g, Expr checked, boolean isTrue) {
59+
exists(Call call |
60+
call = g.getUnconvertedResultExpression() and
61+
call.getTarget().hasName("guarded") and
62+
checked = call.getArgument(0) and
63+
isTrue = true
64+
)
65+
}
66+
67+
/** Common data flow configuration to be used by tests. */
68+
module IRTestAllocationConfig implements DataFlow::ConfigSig {
69+
predicate isSource(DataFlow::Node source) {
70+
source.asExpr().(FunctionCall).getTarget().getName() = "source"
71+
or
72+
source.asIndirectExpr(1).(FunctionCall).getTarget().getName() = "indirect_source"
73+
or
74+
source.asParameter().getName().matches("source%")
75+
or
76+
source.(DataFlow::DefinitionByReferenceNode).getParameter().getName().matches("ref_source%")
77+
or
78+
exists(source.asUninitialized())
79+
}
80+
81+
predicate isSink(DataFlow::Node sink) {
82+
exists(FunctionCall call, Expr e | e = call.getAnArgument() |
83+
call.getTarget().getName() = "sink" and
84+
sink.asExpr() = e
85+
or
86+
call.getTarget().getName() = "indirect_sink" and
87+
sink.asIndirectExpr() = e
88+
)
89+
}
90+
91+
predicate isBarrier(DataFlow::Node barrier) {
92+
exists(Expr barrierExpr | barrierExpr in [barrier.asExpr(), barrier.asIndirectExpr()] |
93+
barrierExpr.(VariableAccess).getTarget().hasName("barrier")
94+
)
95+
or
96+
barrier = DataFlow::BarrierGuard<testBarrierGuard/3>::getABarrierNode()
97+
or
98+
barrier = DataFlow::BarrierGuard<testBarrierGuard/3>::getAnIndirectBarrierNode()
99+
}
100+
}
101+
102+
module IRFlow = DataFlow::Global<IRTestAllocationConfig>;
103+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import TestBase
2+
3+
query predicate astFlow(AstTest::DataFlow::Node source, AstTest::DataFlow::Node sink) {
4+
AstTest::AstFlow::flow(source, sink)
5+
}
6+
7+
query predicate irFlow(IRTest::DataFlow::Node source, IRTest::DataFlow::Node sink) {
8+
IRTest::IRFlow::flow(source, sink)
9+
}
Lines changed: 1 addition & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,3 @@
1+
import TestBase
12
import TestUtilities.dataflow.FlowTestCommon
2-
3-
module AstTest {
4-
private import semmle.code.cpp.dataflow.DataFlow
5-
private import semmle.code.cpp.controlflow.Guards
6-
7-
/**
8-
* A `BarrierGuard` that stops flow to all occurrences of `x` within statement
9-
* S in `if (guarded(x)) S`.
10-
*/
11-
// This is tested in `BarrierGuard.cpp`.
12-
predicate testBarrierGuard(GuardCondition g, Expr checked, boolean isTrue) {
13-
g.(FunctionCall).getTarget().getName() = "guarded" and
14-
checked = g.(FunctionCall).getArgument(0) and
15-
isTrue = true
16-
}
17-
18-
/** Common data flow configuration to be used by tests. */
19-
module AstTestAllocationConfig implements DataFlow::ConfigSig {
20-
predicate isSource(DataFlow::Node source) {
21-
source.asExpr().(FunctionCall).getTarget().getName() = "source"
22-
or
23-
source.asParameter().getName().matches("source%")
24-
or
25-
source.asExpr().(FunctionCall).getTarget().getName() = "indirect_source"
26-
or
27-
source.(DataFlow::DefinitionByReferenceNode).getParameter().getName().matches("ref_source%")
28-
or
29-
// Track uninitialized variables
30-
exists(source.asUninitialized())
31-
}
32-
33-
predicate isSink(DataFlow::Node sink) {
34-
exists(FunctionCall call |
35-
call.getTarget().getName() = ["sink", "indirect_sink"] and
36-
sink.asExpr() = call.getAnArgument()
37-
)
38-
}
39-
40-
predicate isBarrier(DataFlow::Node barrier) {
41-
barrier.asExpr().(VariableAccess).getTarget().hasName("barrier") or
42-
barrier = DataFlow::BarrierGuard<testBarrierGuard/3>::getABarrierNode()
43-
}
44-
}
45-
46-
module AstFlow = DataFlow::Global<AstTestAllocationConfig>;
47-
}
48-
49-
module IRTest {
50-
private import cpp
51-
private import semmle.code.cpp.ir.dataflow.DataFlow
52-
private import semmle.code.cpp.ir.IR
53-
private import semmle.code.cpp.controlflow.IRGuards
54-
55-
/**
56-
* A `BarrierGuard` that stops flow to all occurrences of `x` within statement
57-
* S in `if (guarded(x)) S`.
58-
*/
59-
// This is tested in `BarrierGuard.cpp`.
60-
predicate testBarrierGuard(IRGuardCondition g, Expr checked, boolean isTrue) {
61-
exists(Call call |
62-
call = g.getUnconvertedResultExpression() and
63-
call.getTarget().hasName("guarded") and
64-
checked = call.getArgument(0) and
65-
isTrue = true
66-
)
67-
}
68-
69-
/** Common data flow configuration to be used by tests. */
70-
module IRTestAllocationConfig implements DataFlow::ConfigSig {
71-
predicate isSource(DataFlow::Node source) {
72-
source.asExpr().(FunctionCall).getTarget().getName() = "source"
73-
or
74-
source.asIndirectExpr(1).(FunctionCall).getTarget().getName() = "indirect_source"
75-
or
76-
source.asParameter().getName().matches("source%")
77-
or
78-
source.(DataFlow::DefinitionByReferenceNode).getParameter().getName().matches("ref_source%")
79-
or
80-
exists(source.asUninitialized())
81-
}
82-
83-
predicate isSink(DataFlow::Node sink) {
84-
exists(FunctionCall call, Expr e | e = call.getAnArgument() |
85-
call.getTarget().getName() = "sink" and
86-
sink.asExpr() = e
87-
or
88-
call.getTarget().getName() = "indirect_sink" and
89-
sink.asIndirectExpr() = e
90-
)
91-
}
92-
93-
predicate isBarrier(DataFlow::Node barrier) {
94-
exists(Expr barrierExpr | barrierExpr in [barrier.asExpr(), barrier.asIndirectExpr()] |
95-
barrierExpr.(VariableAccess).getTarget().hasName("barrier")
96-
)
97-
or
98-
barrier = DataFlow::BarrierGuard<testBarrierGuard/3>::getABarrierNode()
99-
or
100-
barrier = DataFlow::BarrierGuard<testBarrierGuard/3>::getAnIndirectBarrierNode()
101-
}
102-
}
103-
104-
module IRFlow = DataFlow::Global<IRTestAllocationConfig>;
105-
}
106-
1073
import MakeTest<MergeTests<AstFlowTest<AstTest::AstFlow>, IRFlowTest<IRTest::IRFlow>>>

0 commit comments

Comments
 (0)