@@ -20,16 +20,16 @@ predicate narrowerThanOrEqualTo(ArithExpr exp, NumType numType) {
2020 exists ( CastingExpr cast | cast .getAChildExpr ( ) = exp | numType .widerThanOrEqualTo ( cast .getType ( ) ) )
2121}
2222
23- private Guard sizeGuard ( SsaVariable v , boolean branch , boolean upper ) {
23+ private Guard sizeGuard ( Expr e , boolean branch , boolean upper ) {
2424 exists ( ComparisonExpr comp | comp = result |
25- comp .getLesserOperand ( ) = ssaRead ( v , 0 ) and
25+ comp .getLesserOperand ( ) = e and
2626 (
2727 branch = true and upper = true
2828 or
2929 branch = false and upper = false
3030 )
3131 or
32- comp .getGreaterOperand ( ) = ssaRead ( v , 0 ) and
32+ comp .getGreaterOperand ( ) = e and
3333 (
3434 branch = true and upper = false
3535 or
@@ -38,7 +38,7 @@ private Guard sizeGuard(SsaVariable v, boolean branch, boolean upper) {
3838 or
3939 exists ( MethodCall ma |
4040 ma .getMethod ( ) instanceof MethodAbs and
41- ma .getArgument ( 0 ) = ssaRead ( v , 0 ) and
41+ ma .getArgument ( 0 ) = e and
4242 (
4343 comp .getLesserOperand ( ) = ma and branch = true
4444 or
@@ -49,7 +49,7 @@ private Guard sizeGuard(SsaVariable v, boolean branch, boolean upper) {
4949 or
5050 // overflow test
5151 exists ( AddExpr add , VarRead use , Expr pos |
52- use = ssaRead ( v , 0 ) and
52+ use = e and
5353 add .hasOperands ( use , pos ) and
5454 positive ( use ) and
5555 positive ( pos ) and
@@ -65,70 +65,38 @@ private Guard sizeGuard(SsaVariable v, boolean branch, boolean upper) {
6565 )
6666 )
6767 or
68- result .isEquality ( ssaRead ( v , 0 ) , _, branch ) and
68+ result .isEquality ( e , _, branch ) and
6969 ( 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- )
7770}
7871
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 )
8274}
8375
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 )
10278}
10379
10480/**
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.
10682 */
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
11385 or
114- negative ( e )
86+ negative ( n . asExpr ( ) )
11587 or
116- e .( MethodCall ) .getMethod ( ) instanceof MethodMathMin
88+ n . asExpr ( ) .( MethodCall ) .getMethod ( ) instanceof MethodMathMin
11789}
11890
11991/**
12092 * Holds if `e` is bounded in a way that is likely to prevent underflow.
12193 */
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
12896 or
129- positive ( e )
97+ positive ( n . asExpr ( ) )
13098 or
131- e .( MethodCall ) .getMethod ( ) instanceof MethodMathMax
99+ n . asExpr ( ) .( MethodCall ) .getMethod ( ) instanceof MethodMathMax
132100}
133101
134102/** 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) {
182150/** Holds if `n` is likely guarded against overflow. */
183151predicate overflowBarrier ( DataFlow:: Node n ) {
184152 n .getType ( ) instanceof BooleanType or
185- guardedLessThanSomething ( n . asExpr ( ) ) or
153+ guardedLessThanSomething ( n ) or
186154 unlikelyNode ( n ) or
187155 upcastToWiderType ( n .asExpr ( ) ) or
188156 overflowIrrelevant ( n .asExpr ( ) )
@@ -191,7 +159,7 @@ predicate overflowBarrier(DataFlow::Node n) {
191159/** Holds if `n` is likely guarded against underflow. */
192160predicate underflowBarrier ( DataFlow:: Node n ) {
193161 n .getType ( ) instanceof BooleanType or
194- guardedGreaterThanSomething ( n . asExpr ( ) ) or
162+ guardedGreaterThanSomething ( n ) or
195163 unlikelyNode ( n ) or
196164 upcastToWiderType ( n .asExpr ( ) ) or
197165 overflowIrrelevant ( n .asExpr ( ) )
@@ -210,7 +178,6 @@ predicate overflowSink(ArithExpr exp, VarAccess use) {
210178 exp instanceof PostIncExpr or
211179 exp instanceof MulExpr
212180 ) and
213- not guardedLessThanSomething ( use ) and
214181 // Exclude widening conversions of tainted values due to binary numeric promotion (JLS 5.6.2)
215182 // unless there is an enclosing cast down to a narrower type.
216183 narrowerThanOrEqualTo ( exp , use .getType ( ) ) and
@@ -230,7 +197,6 @@ predicate underflowSink(ArithExpr exp, VarAccess use) {
230197 exp instanceof PostDecExpr or
231198 exp instanceof MulExpr
232199 ) and
233- not guardedGreaterThanSomething ( use ) and
234200 // Exclude widening conversions of tainted values due to binary numeric promotion (JLS 5.6.2)
235201 // unless there is an enclosing cast down to a narrower type.
236202 narrowerThanOrEqualTo ( exp , use .getType ( ) ) and
0 commit comments