@@ -7,6 +7,18 @@ private import SSA
77private import semmle.code.java.controlflow.internal.GuardsLogic
88private import semmle.code.java.dataflow.internal.rangeanalysis.SsaReadPositionCommon
99private import semmle.code.java.Constants
10+ private import semmle.code.java.dataflow.RangeAnalysis
11+ private import codeql.rangeanalysis.internal.RangeUtils
12+
13+ private module U = MakeUtils< Sem , IntDelta > ;
14+
15+ private predicate backEdge = U:: backEdge / 3 ;
16+
17+ predicate ssaRead = U:: ssaRead / 2 ;
18+
19+ predicate guardDirectlyControlsSsaRead = U:: guardDirectlyControlsSsaRead / 3 ;
20+
21+ predicate guardControlsSsaRead = U:: guardControlsSsaRead / 3 ;
1022
1123/**
1224 * Holds if `v` is an input to `phi` that is not along a back edge, and the
@@ -145,79 +157,6 @@ class ConstantStringExpr extends Expr {
145157 string getStringValue ( ) { constantStringExpr ( this , result ) }
146158}
147159
148- bindingset [ f]
149- private predicate okInt ( float f ) { - 2 .pow ( 31 ) <= f and f <= 2 .pow ( 31 ) - 1 }
150-
151- /**
152- * Gets an expression that equals `v - d`.
153- */
154- Expr ssaRead ( SsaVariable v , int delta ) {
155- result = v .getAUse ( ) and delta = 0
156- or
157- exists ( int d1 , ConstantIntegerExpr c |
158- result .( AddExpr ) .hasOperands ( ssaRead ( v , d1 ) , c ) and
159- delta = d1 - c .getIntValue ( ) and
160- okInt ( d1 .( float ) - c .getIntValue ( ) .( float ) )
161- )
162- or
163- exists ( SubExpr sub , int d1 , ConstantIntegerExpr c |
164- result = sub and
165- sub .getLeftOperand ( ) = ssaRead ( v , d1 ) and
166- sub .getRightOperand ( ) = c and
167- delta = d1 + c .getIntValue ( ) and
168- okInt ( d1 .( float ) + c .getIntValue ( ) .( float ) )
169- )
170- or
171- v .( SsaExplicitUpdate ) .getDefiningExpr ( ) .( PreIncExpr ) = result and delta = 0
172- or
173- v .( SsaExplicitUpdate ) .getDefiningExpr ( ) .( PreDecExpr ) = result and delta = 0
174- or
175- v .( SsaExplicitUpdate ) .getDefiningExpr ( ) .( PostIncExpr ) = result and delta = 1 // x++ === ++x - 1
176- or
177- v .( SsaExplicitUpdate ) .getDefiningExpr ( ) .( PostDecExpr ) = result and delta = - 1 // x-- === --x + 1
178- or
179- v .( SsaExplicitUpdate ) .getDefiningExpr ( ) .( Assignment ) = result and delta = 0
180- or
181- result .( AssignExpr ) .getSource ( ) = ssaRead ( v , delta )
182- }
183-
184- /**
185- * Holds if `inp` is an input to `phi` along a back edge.
186- */
187- predicate backEdge ( SsaPhiNode phi , SsaVariable inp , SsaReadPositionPhiInputEdge edge ) {
188- edge .phiInput ( phi , inp ) and
189- // Conservatively assume that every edge is a back edge if we don't have dominance information.
190- (
191- phi .getBasicBlock ( ) .bbDominates ( edge .getOrigBlock ( ) ) or
192- not hasDominanceInformation ( edge .getOrigBlock ( ) )
193- )
194- }
195-
196- /**
197- * Holds if `guard` directly controls the position `controlled` with the
198- * value `testIsTrue`.
199- */
200- predicate guardDirectlyControlsSsaRead ( Guard guard , SsaReadPosition controlled , boolean testIsTrue ) {
201- guard .directlyControls ( controlled .( SsaReadPositionBlock ) .getBlock ( ) , testIsTrue )
202- or
203- exists ( SsaReadPositionPhiInputEdge controlledEdge | controlledEdge = controlled |
204- guard .directlyControls ( controlledEdge .getOrigBlock ( ) , testIsTrue ) or
205- guard .hasBranchEdge ( controlledEdge .getOrigBlock ( ) , controlledEdge .getPhiBlock ( ) , testIsTrue )
206- )
207- }
208-
209- /**
210- * Holds if `guard` controls the position `controlled` with the value `testIsTrue`.
211- */
212- predicate guardControlsSsaRead ( Guard guard , SsaReadPosition controlled , boolean testIsTrue ) {
213- guardDirectlyControlsSsaRead ( guard , controlled , testIsTrue )
214- or
215- exists ( Guard guard0 , boolean testIsTrue0 |
216- implies_v2 ( guard0 , testIsTrue0 , guard , testIsTrue ) and
217- guardControlsSsaRead ( guard0 , controlled , testIsTrue0 )
218- )
219- }
220-
221160/**
222161 * Gets a condition that tests whether `v` equals `e + delta`.
223162 *
0 commit comments