Skip to content

Commit f66a7ca

Browse files
committed
Guards: Update Guards-CFG integration to recognize MatchSuccessors.
1 parent 8d5f5d0 commit f66a7ca

16 files changed

Lines changed: 264 additions & 306 deletions

File tree

‎cpp/ql/lib/semmle/code/cpp/controlflow/IRGuards.qll‎

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -160,23 +160,6 @@ module GuardsInput implements SharedGuards::InputSig<Cpp::Location, Instruction,
160160
*/
161161
SwitchEdge getEdge() { result = edge }
162162

163-
/**
164-
* Holds if this case takes control-flow from `bb1` to `bb2` when
165-
* the case matches the scrutinee.
166-
*/
167-
predicate matchEdge(BasicBlock bb1, BasicBlock bb2) {
168-
switch.getBlock() = bb1 and
169-
this.getBasicBlock() = bb2
170-
}
171-
172-
/**
173-
* Holds if case takes control-flow from `bb1` to `bb2` when the
174-
* case does not match the scrutinee.
175-
*
176-
* This predicate never holds for C/C++.
177-
*/
178-
predicate nonMatchEdge(BasicBlock bb1, BasicBlock bb2) { none() }
179-
180163
/**
181164
* Gets the scrutinee expression.
182165
*/
@@ -188,16 +171,28 @@ module GuardsInput implements SharedGuards::InputSig<Cpp::Location, Instruction,
188171
predicate isDefaultCase() { edge.isDefault() }
189172

190173
/**
191-
* Gets the constant expression of this case.
174+
* Gets the expression matched by this case.
192175
*/
193-
ConstantExpr asConstantCase() {
176+
AstNode getAPattern() {
194177
// Note: This only has a value if there is a unique value for the case.
195178
// So the will not be a result when using the GCC case range extension.
196179
// Instead, we model these using the `LogicInput_v1::rangeGuard` predicate.
197180
result = this and exists(this.getEdge().getValue())
198181
}
199182
}
200183

184+
/**
185+
* Holds if the case `c` takes control-flow from `bb1` to `bb2` when
186+
* the case matches the scrutinee.
187+
*/
188+
predicate caseMatchEdge(Case c, BasicBlock bb1, BasicBlock bb2) {
189+
exists(SwitchInstruction switch |
190+
switch.getSuccessor(any(SwitchEdge e)) = c and
191+
switch.getBlock() = bb1 and
192+
c.getBasicBlock() = bb2
193+
)
194+
}
195+
201196
abstract private class BinExpr extends Expr instanceof BinaryInstruction {
202197
Expr getAnOperand() { result = super.getAnInput() }
203198
}

‎csharp/ql/lib/semmle/code/csharp/controlflow/Guards.qll‎

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,7 @@ private module GuardsInput implements SharedGuards::InputSig<Location, ControlFl
8585

8686
predicate isDefaultCase() { this instanceof DefaultCase }
8787

88-
ConstantExpr asConstantCase() { super.getPattern() = result }
89-
90-
predicate matchEdge(BasicBlock bb1, BasicBlock bb2) {
91-
bb1.getASuccessor(any(MatchingSuccessor s | s.getValue() = true)) = bb2 and
92-
bb1.getLastNode() = super.getPattern().getControlFlowNode()
93-
}
94-
95-
predicate nonMatchEdge(BasicBlock bb1, BasicBlock bb2) {
96-
bb1.getASuccessor(any(MatchingSuccessor s | s.getValue() = false)) = bb2 and
97-
bb1.getLastNode() = super.getPattern().getControlFlowNode()
98-
}
88+
AstNode getAPattern() { super.getPattern() = result }
9989
}
10090

10191
abstract private class BinExpr extends BinaryOperation { }
@@ -221,6 +211,7 @@ private module LogicInput implements GuardsImpl::LogicInputSig {
221211
isNull = false
222212
)
223213
or
214+
// TODO: Remove this superfluous case once CFG changes for IsExpr lands.
224215
guard =
225216
any(PatternMatch pm |
226217
e instanceof DereferenceableExpr and
@@ -238,6 +229,22 @@ private module LogicInput implements GuardsImpl::LogicInputSig {
238229
)
239230
)
240231
or
232+
exists(PatternMatch pm |
233+
guard = pm.getPattern() and
234+
e instanceof DereferenceableExpr and
235+
e = pm.getExpr()
236+
|
237+
val.isMatch(patternMatchesNull(pm.getPattern()).booleanNot()) and
238+
isNull = false
239+
or
240+
exists(TypePatternExpr tpe |
241+
// E.g. `x is string` where `x` has type `string`
242+
typePattern(pm, tpe, tpe.getCheckedType()) and
243+
val.isMatch(false) and
244+
isNull = true
245+
)
246+
)
247+
or
241248
e.(DereferenceableExpr).hasNullableType() and
242249
guard =
243250
any(PropertyAccess pa |

‎csharp/ql/test/library-tests/controlflow/guards/BooleanGuardedExpr.expected‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,6 @@
7171
| Guards.cs:208:17:208:17 | access to parameter o | Guards.cs:203:13:203:21 | ... != ... | Guards.cs:203:13:203:13 | access to parameter o | true |
7272
| Guards.cs:269:13:269:14 | access to parameter o1 | Guards.cs:268:13:268:41 | call to operator == | Guards.cs:268:13:268:14 | access to parameter o1 | true |
7373
| Guards.cs:271:13:271:14 | access to parameter o1 | Guards.cs:270:13:270:42 | call to operator == | Guards.cs:270:13:270:14 | access to parameter o1 | true |
74-
| Guards.cs:279:17:279:17 | access to parameter o | Guards.cs:278:13:279:28 | ... => ... | Guards.cs:279:17:279:17 | access to parameter o | true |
75-
| Guards.cs:281:17:281:17 | access to local variable a | Guards.cs:280:13:281:28 | ... => ... | Guards.cs:280:28:280:28 | access to local variable a | true |
76-
| Guards.cs:281:17:281:17 | access to local variable a | Guards.cs:280:13:281:28 | ... => ... | Guards.cs:281:17:281:17 | access to local variable a | true |
77-
| Guards.cs:283:17:283:17 | access to parameter o | Guards.cs:278:13:279:28 | ... => ... | Guards.cs:279:17:279:17 | access to parameter o | false |
78-
| Guards.cs:283:17:283:17 | access to parameter o | Guards.cs:282:13:283:28 | ... => ... | Guards.cs:283:17:283:17 | access to parameter o | true |
79-
| Guards.cs:285:17:285:17 | access to parameter o | Guards.cs:278:13:279:28 | ... => ... | Guards.cs:279:17:279:17 | access to parameter o | false |
80-
| Guards.cs:285:17:285:17 | access to parameter o | Guards.cs:282:13:283:28 | ... => ... | Guards.cs:283:17:283:17 | access to parameter o | false |
81-
| Guards.cs:285:17:285:17 | access to parameter o | Guards.cs:284:13:285:28 | ... => ... | Guards.cs:285:17:285:17 | access to parameter o | true |
82-
| Guards.cs:287:17:287:17 | access to parameter o | Guards.cs:278:13:279:28 | ... => ... | Guards.cs:279:17:279:17 | access to parameter o | false |
83-
| Guards.cs:287:17:287:17 | access to parameter o | Guards.cs:282:13:283:28 | ... => ... | Guards.cs:283:17:283:17 | access to parameter o | false |
84-
| Guards.cs:287:17:287:17 | access to parameter o | Guards.cs:284:13:285:28 | ... => ... | Guards.cs:285:17:285:17 | access to parameter o | false |
8574
| Guards.cs:342:27:342:27 | access to parameter b | Guards.cs:341:20:341:20 | access to parameter b | Guards.cs:341:20:341:20 | access to parameter b | false |
8675
| Guards.cs:343:31:343:31 | access to local variable s | Guards.cs:342:13:342:21 | ... != ... | Guards.cs:342:13:342:13 | access to local variable s | true |
8776
| Guards.cs:349:13:349:13 | access to parameter o | Guards.cs:348:13:348:25 | ... is ... | Guards.cs:348:13:348:13 | access to parameter o | true |

‎go/ql/lib/semmle/go/controlflow/Guards.qll‎

Lines changed: 3 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -97,64 +97,13 @@ private module GuardsInput implements
9797
class Case extends AstNode {
9898
G::ExpressionSwitchStmt switch;
9999

100-
Case() {
101-
exists(switch.getExpr()) and
102-
this = switch.getACase()
103-
or
104-
not exists(switch.getExpr()) and
105-
(
106-
this = switch.getANonDefaultCase().getAnExpr()
107-
or
108-
this = switch.getDefault()
109-
)
110-
}
100+
Case() { this = switch.getACase() }
111101

112-
Expr getSwitchExpr() {
113-
result = switch.getExpr()
114-
or
115-
not exists(switch.getExpr()) and result = this
116-
}
102+
Expr getSwitchExpr() { result = switch.getExpr() }
117103

118104
predicate isDefaultCase() { this = switch.getDefault() }
119105

120-
ConstantExpr asConstantCase() {
121-
exists(G::CaseClause cc |
122-
this = cc and
123-
cc.getNumExpr() = 1 and
124-
result = cc.getExpr(0)
125-
)
126-
}
127-
128-
predicate matchEdge(CfgImpl::Cfg::BasicBlock bb1, CfgImpl::Cfg::BasicBlock bb2) {
129-
exists(Expr caseExpr |
130-
caseExpr = this.(G::CaseClause).getAnExpr()
131-
or
132-
caseExpr = this
133-
|
134-
caseExpressionBranch(caseExpr, bb1,
135-
any(MatchingSuccessor successor |
136-
bb1.getASuccessor(successor) = bb2 and successor.getValue() = true
137-
))
138-
)
139-
}
140-
141-
predicate nonMatchEdge(CfgImpl::Cfg::BasicBlock bb1, CfgImpl::Cfg::BasicBlock bb2) {
142-
exists(G::CaseClause cc, int last, Expr caseExpr |
143-
cc = this and
144-
last = max(int i | exists(cc.getExpr(i))) and
145-
caseExpr = cc.getExpr(last)
146-
|
147-
caseExpressionBranch(caseExpr, bb1,
148-
any(MatchingSuccessor successor |
149-
bb1.getASuccessor(successor) = bb2 and successor.getValue() = false
150-
))
151-
)
152-
or
153-
caseExpressionBranch(this, bb1,
154-
any(MatchingSuccessor successor |
155-
bb1.getASuccessor(successor) = bb2 and successor.getValue() = false
156-
))
157-
}
106+
AstNode getAPattern() { result = this.(G::CaseClause).getAnExpr() }
158107
}
159108

160109
additional predicate caseExpressionBranch(
@@ -168,20 +117,6 @@ private module GuardsInput implements
168117
)
169118
}
170119

171-
predicate equalityBranchEdge(
172-
Expr left, Expr right, CfgImpl::Cfg::BasicBlock bb1, CfgImpl::Cfg::BasicBlock bb2, boolean equal
173-
) {
174-
exists(G::CaseClause cc, G::ExpressionSwitchStmt switch |
175-
cc = switch.getACase() and
176-
left = switch.getExpr() and
177-
right = cc.getAnExpr() and
178-
caseExpressionBranch(right, bb1,
179-
any(MatchingSuccessor successor |
180-
bb1.getASuccessor(successor) = bb2 and successor.getValue() = equal
181-
))
182-
)
183-
}
184-
185120
class AndExpr = G::LandExpr;
186121

187122
class OrExpr = G::LorExpr;

‎go/ql/test/library-tests/semmle/go/controlflow/Guards/Guards.expected‎

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,8 @@ controlsResult
44
| guards.go:12:7:12:15 | ...<... | zero | false |
55
| guards.go:14:7:14:16 | ...==... | positive | false |
66
| guards.go:14:7:14:16 | ...==... | zero | true |
7-
| guards.go:23:2:24:21 | case clause | tagged one or two | false |
8-
| guards.go:23:2:24:21 | case clause | tagged other | false |
9-
| guards.go:23:2:24:21 | case clause | tagged zero | true |
10-
| guards.go:25:2:26:27 | case clause | tagged other | false |
11-
| guards.go:34:2:35:30 | case clause | tagged false boolean | true |
127
| guards.go:34:7:34:11 | value | tagged false boolean | false |
13-
| guards.go:38:2:39:29 | case clause | tagged true boolean | true |
148
| guards.go:38:7:38:11 | value | tagged true boolean | true |
15-
| guards.go:42:2:43:33 | case clause | tagged false comparison | true |
169
| guards.go:42:7:42:17 | ...<... | tagged false comparison | false |
1710
| guards.go:48:5:48:5 | a | compound true | true |
1811
| guards.go:48:5:48:17 | ...&&... | compound false | false |
@@ -38,10 +31,6 @@ controlsResult
3831
| guards.go:118:5:118:32 | ...==... | errors new | true |
3932
| guards.go:122:5:122:32 | ...==... | fmt errorf | true |
4033
| guards.go:126:5:126:28 | ...==... | source non-nil return | true |
41-
| guards.go:137:2:138:26 | case clause | default case | false |
42-
| guards.go:137:2:138:26 | case clause | non-default case | true |
43-
| guards.go:148:2:149:28 | case clause | case after default | true |
44-
| guards.go:148:2:149:28 | case clause | default before case | false |
4534
| guards.go:162:7:162:11 | value | expressionless case | true |
4635
| guards.go:162:7:162:11 | value | expressionless default | false |
4736
| guards.go:183:5:183:21 | call to isNotNil | non-variadic wrapper | true |
@@ -62,29 +51,35 @@ valueControlsResult
6251
| guards.go:12:7:12:11 | value | negative | Upper bound -1 |
6352
| guards.go:12:7:12:11 | value | positive | Lower bound 0 |
6453
| guards.go:12:7:12:11 | value | zero | Lower bound 0 |
54+
| guards.go:12:7:12:15 | ...<... | negative | match |
6555
| guards.go:12:7:12:15 | ...<... | negative | true |
6656
| guards.go:12:7:12:15 | ...<... | positive | false |
57+
| guards.go:12:7:12:15 | ...<... | positive | no match |
6758
| guards.go:12:7:12:15 | ...<... | zero | false |
59+
| guards.go:12:7:12:15 | ...<... | zero | no match |
6860
| guards.go:14:7:14:11 | value | positive | not 0 |
6961
| guards.go:14:7:14:11 | value | zero | 0 |
7062
| guards.go:14:7:14:16 | ...==... | positive | false |
63+
| guards.go:14:7:14:16 | ...==... | positive | no match |
64+
| guards.go:14:7:14:16 | ...==... | zero | match |
7165
| guards.go:14:7:14:16 | ...==... | zero | true |
7266
| guards.go:22:9:22:13 | value | tagged one or two | not 0 |
7367
| guards.go:22:9:22:13 | value | tagged other | not 0 |
7468
| guards.go:22:9:22:13 | value | tagged other | not 1 |
7569
| guards.go:22:9:22:13 | value | tagged other | not 2 |
7670
| guards.go:22:9:22:13 | value | tagged zero | 0 |
77-
| guards.go:23:2:24:21 | case clause | tagged one or two | false |
78-
| guards.go:23:2:24:21 | case clause | tagged other | false |
79-
| guards.go:23:2:24:21 | case clause | tagged zero | true |
80-
| guards.go:25:2:26:27 | case clause | tagged other | false |
81-
| guards.go:34:2:35:30 | case clause | tagged false boolean | true |
71+
| guards.go:23:7:23:7 | 0 | tagged one or two | no match |
72+
| guards.go:23:7:23:7 | 0 | tagged other | no match |
73+
| guards.go:23:7:23:7 | 0 | tagged zero | match |
74+
| guards.go:25:7:25:7 | 1 | tagged other | no match |
75+
| guards.go:25:10:25:10 | 2 | tagged other | no match |
8276
| guards.go:34:7:34:11 | value | tagged false boolean | false |
83-
| guards.go:38:2:39:29 | case clause | tagged true boolean | true |
77+
| guards.go:34:7:34:11 | value | tagged false boolean | match |
78+
| guards.go:38:7:38:11 | value | tagged true boolean | match |
8479
| guards.go:38:7:38:11 | value | tagged true boolean | true |
85-
| guards.go:42:2:43:33 | case clause | tagged false comparison | true |
8680
| guards.go:42:7:42:12 | number | tagged false comparison | Lower bound 10 |
8781
| guards.go:42:7:42:17 | ...<... | tagged false comparison | false |
82+
| guards.go:42:7:42:17 | ...<... | tagged false comparison | match |
8883
| guards.go:48:5:48:5 | a | compound true | true |
8984
| guards.go:48:5:48:17 | ...&&... | compound false | false |
9085
| guards.go:48:5:48:17 | ...&&... | compound true | true |
@@ -140,14 +135,16 @@ valueControlsResult
140135
| guards.go:126:14:126:28 | call to returnsNonNil | source non-nil return | no exception |
141136
| guards.go:136:9:136:13 | value | default case | not 0 |
142137
| guards.go:136:9:136:13 | value | non-default case | 0 |
143-
| guards.go:137:2:138:26 | case clause | default case | false |
144-
| guards.go:137:2:138:26 | case clause | non-default case | true |
138+
| guards.go:137:7:137:7 | 0 | default case | no match |
139+
| guards.go:137:7:137:7 | 0 | non-default case | match |
145140
| guards.go:145:9:145:13 | value | case after default | 0 |
146141
| guards.go:145:9:145:13 | value | default before case | not 0 |
147-
| guards.go:148:2:149:28 | case clause | case after default | true |
148-
| guards.go:148:2:149:28 | case clause | default before case | false |
142+
| guards.go:148:7:148:7 | 0 | case after default | match |
143+
| guards.go:148:7:148:7 | 0 | default before case | no match |
144+
| guards.go:162:7:162:11 | value | expressionless case | match |
149145
| guards.go:162:7:162:11 | value | expressionless case | true |
150146
| guards.go:162:7:162:11 | value | expressionless default | false |
147+
| guards.go:162:7:162:11 | value | expressionless default | no match |
151148
| guards.go:183:5:183:21 | call to isNotNil | non-variadic wrapper | no exception |
152149
| guards.go:183:5:183:21 | call to isNotNil | non-variadic wrapper | true |
153150
| guards.go:183:14:183:20 | pointer | non-variadic wrapper | not null |
@@ -193,8 +190,6 @@ valueControlsResult
193190
ensuresEqResult
194191
| guards.go:14:7:14:16 | ...==... | 0 = value | true |
195192
| guards.go:14:7:14:16 | ...==... | value = 0 | true |
196-
| guards.go:23:2:24:21 | case clause | 0 = value | true |
197-
| guards.go:23:2:24:21 | case clause | value = 0 | true |
198193
| guards.go:56:5:56:20 | ...==... | 0 = type conversion | true |
199194
| guards.go:56:5:56:20 | ...==... | type conversion = 0 | true |
200195
| guards.go:59:5:59:19 | ...==... | 0 = type conversion | true |
@@ -217,12 +212,6 @@ ensuresEqResult
217212
| guards.go:122:5:122:32 | ...==... | value = call to Errorf | true |
218213
| guards.go:126:5:126:28 | ...==... | call to returnsNonNil = value | true |
219214
| guards.go:126:5:126:28 | ...==... | value = call to returnsNonNil | true |
220-
| guards.go:137:2:138:26 | case clause | 0 = value | true |
221-
| guards.go:137:2:138:26 | case clause | value = 0 | true |
222-
| guards.go:148:2:149:28 | case clause | 0 = value | true |
223-
| guards.go:148:2:149:28 | case clause | value = 0 | true |
224-
| guards.go:171:2:172:13 | case clause | 0 = value | true |
225-
| guards.go:171:2:172:13 | case clause | value = 0 | true |
226215
| guards.go:179:9:179:22 | ...!=... | nil = pointer | false |
227216
| guards.go:179:9:179:22 | ...!=... | pointer = nil | false |
228217
| guards.go:189:10:189:23 | ...!=... | nil = pointer | false |

0 commit comments

Comments
 (0)