@@ -20,16 +20,16 @@ predicate narrowerThanOrEqualTo(ArithExpr exp, NumType numType) {
20
20
exists ( CastingExpr cast | cast .getAChildExpr ( ) = exp | numType .widerThanOrEqualTo ( cast .getType ( ) ) )
21
21
}
22
22
23
- private Guard sizeGuard ( SsaVariable v , boolean branch , boolean upper ) {
23
+ private Guard sizeGuard ( Expr e , boolean branch , boolean upper ) {
24
24
exists ( ComparisonExpr comp | comp = result |
25
- comp .getLesserOperand ( ) = ssaRead ( v , 0 ) and
25
+ comp .getLesserOperand ( ) = e and
26
26
(
27
27
branch = true and upper = true
28
28
or
29
29
branch = false and upper = false
30
30
)
31
31
or
32
- comp .getGreaterOperand ( ) = ssaRead ( v , 0 ) and
32
+ comp .getGreaterOperand ( ) = e and
33
33
(
34
34
branch = true and upper = false
35
35
or
@@ -38,7 +38,7 @@ private Guard sizeGuard(SsaVariable v, boolean branch, boolean upper) {
38
38
or
39
39
exists ( MethodCall ma |
40
40
ma .getMethod ( ) instanceof MethodAbs and
41
- ma .getArgument ( 0 ) = ssaRead ( v , 0 ) and
41
+ ma .getArgument ( 0 ) = e and
42
42
(
43
43
comp .getLesserOperand ( ) = ma and branch = true
44
44
or
@@ -49,7 +49,7 @@ private Guard sizeGuard(SsaVariable v, boolean branch, boolean upper) {
49
49
or
50
50
// overflow test
51
51
exists ( AddExpr add , VarRead use , Expr pos |
52
- use = ssaRead ( v , 0 ) and
52
+ use = e and
53
53
add .hasOperands ( use , pos ) and
54
54
positive ( use ) and
55
55
positive ( pos ) and
@@ -65,70 +65,38 @@ private Guard sizeGuard(SsaVariable v, boolean branch, boolean upper) {
65
65
)
66
66
)
67
67
or
68
- result .isEquality ( ssaRead ( v , 0 ) , _, branch ) and
68
+ result .isEquality ( e , _, branch ) and
69
69
( upper = true or upper = false )
70
- or
71
- exists ( MethodCall call , Method m , int ix |
72
- call = result and
73
- call .getArgument ( ix ) = ssaRead ( v , 0 ) and
74
- call .getMethod ( ) .getSourceDeclaration ( ) = m and
75
- m = customSizeGuard ( ix , branch , upper )
76
- )
77
70
}
78
71
79
- private Guard derivedSizeGuard ( SsaVariable v , boolean branch , boolean upper ) {
80
- result = sizeGuard ( v , branch , upper ) or
81
- exists ( boolean branch0 | implies_v3 ( result , branch , derivedSizeGuard ( v , branch0 , upper ) , branch0 ) )
72
+ private predicate sizeGuardLessThan ( Guard g , Expr e , boolean branch ) {
73
+ g = sizeGuard ( e , branch , true )
82
74
}
83
75
84
- private Method customSizeGuard ( int index , boolean retval , boolean upper ) {
85
- exists ( Parameter p , SsaImplicitInit v |
86
- result .getReturnType ( ) .( PrimitiveType ) .hasName ( "boolean" ) and
87
- not result .isOverridable ( ) and
88
- p .getCallable ( ) = result and
89
- not p .isVarargs ( ) and
90
- p .getType ( ) instanceof NumericOrCharType and
91
- p .getPosition ( ) = index and
92
- v .isParameterDefinition ( p ) and
93
- forex ( ReturnStmt ret |
94
- ret .getEnclosingCallable ( ) = result and
95
- exists ( Expr res | res = ret .getResult ( ) |
96
- not res .( BooleanLiteral ) .getBooleanValue ( ) = retval .booleanNot ( )
97
- )
98
- |
99
- ret .getResult ( ) = derivedSizeGuard ( v , retval , upper )
100
- )
101
- )
76
+ private predicate sizeGuardGreaterThan ( Guard g , Expr e , boolean branch ) {
77
+ g = sizeGuard ( e , branch , false )
102
78
}
103
79
104
80
/**
105
- * Holds if `e ` is bounded in a way that is likely to prevent overflow.
81
+ * Holds if `n ` is bounded in a way that is likely to prevent overflow.
106
82
*/
107
- predicate guardedLessThanSomething ( Expr e ) {
108
- exists ( SsaVariable v , Guard guard , boolean branch |
109
- e = v .getAUse ( ) and
110
- guard = sizeGuard ( v .getAPhiInputOrPriorDef * ( ) , branch , true ) and
111
- guard .controls ( e .getBasicBlock ( ) , branch )
112
- )
83
+ predicate guardedLessThanSomething ( DataFlow:: Node n ) {
84
+ DataFlow:: BarrierGuard< sizeGuardLessThan / 3 > :: getABarrierNode ( ) = n
113
85
or
114
- negative ( e )
86
+ negative ( n . asExpr ( ) )
115
87
or
116
- e .( MethodCall ) .getMethod ( ) instanceof MethodMathMin
88
+ n . asExpr ( ) .( MethodCall ) .getMethod ( ) instanceof MethodMathMin
117
89
}
118
90
119
91
/**
120
92
* Holds if `e` is bounded in a way that is likely to prevent underflow.
121
93
*/
122
- predicate guardedGreaterThanSomething ( Expr e ) {
123
- exists ( SsaVariable v , Guard guard , boolean branch |
124
- e = v .getAUse ( ) and
125
- guard = sizeGuard ( v .getAPhiInputOrPriorDef * ( ) , branch , false ) and
126
- guard .controls ( e .getBasicBlock ( ) , branch )
127
- )
94
+ predicate guardedGreaterThanSomething ( DataFlow:: Node n ) {
95
+ DataFlow:: BarrierGuard< sizeGuardGreaterThan / 3 > :: getABarrierNode ( ) = n
128
96
or
129
- positive ( e )
97
+ positive ( n . asExpr ( ) )
130
98
or
131
- e .( MethodCall ) .getMethod ( ) instanceof MethodMathMax
99
+ n . asExpr ( ) .( MethodCall ) .getMethod ( ) instanceof MethodMathMax
132
100
}
133
101
134
102
/** Holds if `e` occurs in a context where it will be upcast to a wider type. */
@@ -182,7 +150,7 @@ private predicate unlikelyNode(DataFlow::Node n) {
182
150
/** Holds if `n` is likely guarded against overflow. */
183
151
predicate overflowBarrier ( DataFlow:: Node n ) {
184
152
n .getType ( ) instanceof BooleanType or
185
- guardedLessThanSomething ( n . asExpr ( ) ) or
153
+ guardedLessThanSomething ( n ) or
186
154
unlikelyNode ( n ) or
187
155
upcastToWiderType ( n .asExpr ( ) ) or
188
156
overflowIrrelevant ( n .asExpr ( ) )
@@ -191,7 +159,7 @@ predicate overflowBarrier(DataFlow::Node n) {
191
159
/** Holds if `n` is likely guarded against underflow. */
192
160
predicate underflowBarrier ( DataFlow:: Node n ) {
193
161
n .getType ( ) instanceof BooleanType or
194
- guardedGreaterThanSomething ( n . asExpr ( ) ) or
162
+ guardedGreaterThanSomething ( n ) or
195
163
unlikelyNode ( n ) or
196
164
upcastToWiderType ( n .asExpr ( ) ) or
197
165
overflowIrrelevant ( n .asExpr ( ) )
@@ -210,7 +178,6 @@ predicate overflowSink(ArithExpr exp, VarAccess use) {
210
178
exp instanceof PostIncExpr or
211
179
exp instanceof MulExpr
212
180
) and
213
- not guardedLessThanSomething ( use ) and
214
181
// Exclude widening conversions of tainted values due to binary numeric promotion (JLS 5.6.2)
215
182
// unless there is an enclosing cast down to a narrower type.
216
183
narrowerThanOrEqualTo ( exp , use .getType ( ) ) and
@@ -230,7 +197,6 @@ predicate underflowSink(ArithExpr exp, VarAccess use) {
230
197
exp instanceof PostDecExpr or
231
198
exp instanceof MulExpr
232
199
) and
233
- not guardedGreaterThanSomething ( use ) and
234
200
// Exclude widening conversions of tainted values due to binary numeric promotion (JLS 5.6.2)
235
201
// unless there is an enclosing cast down to a narrower type.
236
202
narrowerThanOrEqualTo ( exp , use .getType ( ) ) and
0 commit comments