-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathUselessComparisonTest.ql
235 lines (224 loc) · 7.24 KB
/
UselessComparisonTest.ql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/**
* @name Useless comparison test
* @description A comparison operation that always evaluates to true or always
* evaluates to false may indicate faulty logic and may result in
* dead code.
* @kind problem
* @problem.severity warning
* @precision very-high
* @id java/constant-comparison
* @tags correctness
* logic
* external/cwe/cwe-570
* external/cwe/cwe-571
*/
import semmle.code.java.controlflow.Guards
import semmle.code.java.dataflow.SSA
import semmle.code.java.dataflow.SignAnalysis
import semmle.code.java.dataflow.RangeAnalysis
/** Holds if `cond` always evaluates to `isTrue`. */
predicate constCond(BinaryExpr cond, boolean isTrue, Reason reason) {
exists(
ComparisonExpr comp, Expr lesser, Expr greater, Bound b, int d1, int d2, Reason r1, Reason r2
|
comp = cond and
lesser = comp.getLesserOperand() and
greater = comp.getGreaterOperand() and
bounded(lesser, b, d1, isTrue, r1) and
bounded(greater, b, d2, isTrue.booleanNot(), r2) and
(reason = r1 or reason = r2) and
(
r1 instanceof NoReason and r2 instanceof NoReason
or
not reason instanceof NoReason
)
|
isTrue = true and comp.isStrict() and d1 < d2
or
isTrue = true and not comp.isStrict() and d1 <= d2
or
isTrue = false and comp.isStrict() and d1 >= d2
or
isTrue = false and not comp.isStrict() and d1 > d2
)
or
exists(EqualityTest eq, Expr lhs, Expr rhs |
eq = cond and
lhs = eq.getLeftOperand() and
rhs = eq.getRightOperand()
|
exists(Bound b, int d1, int d2, boolean upper, Reason r1, Reason r2 |
bounded(lhs, b, d1, upper, r1) and
bounded(rhs, b, d2, upper.booleanNot(), r2) and
isTrue = eq.polarity().booleanNot() and
(reason = r1 or reason = r2) and
(
r1 instanceof NoReason and r2 instanceof NoReason
or
not reason instanceof NoReason
)
|
upper = true and d1 < d2 // lhs <= b + d1 < b + d2 <= rhs
or
upper = false and d1 > d2 // lhs >= b + d1 > b + d2 >= rhs
)
or
exists(Bound b, int d, Reason r1, Reason r2, Reason r3, Reason r4 |
bounded(lhs, b, d, true, r1) and
bounded(lhs, b, d, false, r2) and
bounded(rhs, b, d, true, r3) and
bounded(rhs, b, d, false, r4) and
isTrue = eq.polarity()
|
(reason = r1 or reason = r2 or reason = r3 or reason = r4) and
(
r1 instanceof NoReason and
r2 instanceof NoReason and
r3 instanceof NoReason and
r4 instanceof NoReason
or
not reason instanceof NoReason
)
)
)
}
/** Holds if `cond` always evaluates to `isTrue`. */
predicate constCondSimple(BinaryExpr cond, boolean isTrue) {
constCond(cond, isTrue, any(NoReason nr))
}
/** Gets a seemingly positive expression that might be negative due to overflow. */
Expr overFlowCand() {
exists(BinaryExpr bin |
result = bin and
positive(bin.getLeftOperand()) and
positive(bin.getRightOperand())
|
bin instanceof AddExpr or
bin instanceof MulExpr or
bin instanceof LeftShiftExpr
)
or
exists(AssignOp op |
result = op and
positive(op.getDest()) and
positive(op.getRhs())
|
op instanceof AssignAddExpr or
op instanceof AssignMulExpr or
op instanceof AssignLeftShiftExpr
)
or
exists(AddExpr add, CompileTimeConstantExpr c |
result = add and
add.hasOperands(overFlowCand(), c) and
c.getIntValue() >= 0
)
or
exists(AssignAddExpr add, CompileTimeConstantExpr c |
result = add and
add.getDest() = overFlowCand() and
add.getRhs() = c and
c.getIntValue() >= 0
)
or
exists(SsaExplicitUpdate x | result = x.getAUse() and x.getDefiningExpr() = overFlowCand())
or
result.(AssignExpr).getRhs() = overFlowCand()
or
result.(LocalVariableDeclExpr).getInit() = overFlowCand()
or
exists(ConditionalExpr c | c = result |
c.getTrueExpr() = overFlowCand() and
c.getFalseExpr() = overFlowCand()
)
}
predicate positiveOrNegative(Expr e) { positive(e) or negative(e) }
/** Gets an expression that equals `v` plus a positive or negative value. */
Expr increaseOrDecreaseOfVar(SsaVariable v) {
exists(AssignAddExpr add |
result = add and
positiveOrNegative(add.getDest()) and
add.getRhs() = v.getAUse()
)
or
exists(AddExpr add, Expr e |
result = add and
add.hasOperands(v.getAUse(), e) and
positiveOrNegative(e)
)
or
exists(SubExpr sub |
result = sub and
sub.getLeftOperand() = v.getAUse() and
positiveOrNegative(sub.getRightOperand())
)
or
exists(SsaExplicitUpdate x |
result = x.getAUse() and x.getDefiningExpr() = increaseOrDecreaseOfVar(v)
)
or
result.(AssignExpr).getRhs() = increaseOrDecreaseOfVar(v)
or
result.(LocalVariableDeclExpr).getInit() = increaseOrDecreaseOfVar(v)
}
predicate overFlowTest(ComparisonExpr comp) {
(
exists(SsaVariable v | comp.hasOperands(increaseOrDecreaseOfVar(v), v.getAUse()))
or
comp.getLesserOperand() = overFlowCand() and
comp.getGreaterOperand().(IntegerLiteral).getIntValue() = 0
) and
// exclude loop conditions as they are unlikely to be overflow tests
not comp.getEnclosingStmt() instanceof LoopStmt
}
predicate concurrentModificationTest(BinaryExpr test) {
exists(IfStmt ifstmt, ThrowStmt throw, RefType exc |
ifstmt.getCondition() = test and
(ifstmt.getThen() = throw or ifstmt.getThen().(SingletonBlock).getStmt() = throw) and
throw.getExpr().(ClassInstanceExpr).getConstructedType() = exc and
exc.hasQualifiedName("java.util", "ConcurrentModificationException")
)
}
/**
* Holds if `test` and `guard` are equality tests of the same integral variable v with constants `c1` and `c2`.
*/
pragma[nomagic]
predicate guardedTest(EqualityTest test, Guard guard, boolean isEq, int i1, int i2) {
exists(SsaVariable v, CompileTimeConstantExpr c1, CompileTimeConstantExpr c2 |
guard.isEquality(v.getAUse(), c1, isEq) and
test.hasOperands(v.getAUse(), c2) and
i1 = c1.getIntValue() and
i2 = c2.getIntValue() and
v.getSourceVariable().getType() instanceof IntegralType
)
}
/**
* Holds if `guard` implies that `test` always has the value `testIsTrue`.
*/
predicate uselessEqTest(EqualityTest test, boolean testIsTrue, Guard guard) {
exists(boolean guardIsTrue, boolean guardpolarity, int i |
guardedTest(test, guard, guardpolarity, i, i) and
guard.controls(test.getBasicBlock(), guardIsTrue) and
testIsTrue = guardIsTrue.booleanXor(guardpolarity.booleanXor(test.polarity()))
)
}
from BinaryExpr test, boolean testIsTrue, string reason, ExprParent reasonElem
where
(
if uselessEqTest(test, _, _)
then
exists(EqualityTest r |
uselessEqTest(test, testIsTrue, r) and reason = ", because of $@" and reasonElem = r
)
else
if constCondSimple(test, _)
then constCondSimple(test, testIsTrue) and reason = "" and reasonElem = test // dummy reason element
else
exists(CondReason r |
constCond(test, testIsTrue, r) and reason = ", because of $@" and reasonElem = r.getCond()
)
) and
not overFlowTest(test) and
not concurrentModificationTest(test) and
not exists(AssertStmt assert | assert.getExpr() = test.getParent*())
select test, "Test is always " + testIsTrue + reason + ".", reasonElem, "this condition"