Skip to content

Commit 8659bed

Browse files
committed
Java: Extract Bound class to its own file.
1 parent 6dfbb72 commit 8659bed

File tree

2 files changed

+60
-56
lines changed

2 files changed

+60
-56
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import java
2+
private import SSA
3+
private import RangeUtils
4+
5+
private newtype TBound =
6+
TBoundZero() or
7+
TBoundSsa(SsaVariable v) { v.getSourceVariable().getType() instanceof IntegralType } or
8+
TBoundExpr(Expr e) { e.(FieldRead).getField() instanceof ArrayLengthField and not exists(SsaVariable v | e = v.getAUse()) }
9+
10+
/**
11+
* A bound that may be inferred for an expression plus/minus an integer delta.
12+
*/
13+
abstract class Bound extends TBound {
14+
abstract string toString();
15+
/** Gets an expression that equals this bound plus `delta`. */
16+
abstract Expr getExpr(int delta);
17+
/** Gets an expression that equals this bound. */
18+
Expr getExpr() {
19+
result = getExpr(0)
20+
}
21+
predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) {
22+
path = "" and sl = 0 and sc = 0 and el = 0 and ec = 0
23+
}
24+
}
25+
26+
/**
27+
* The bound that corresponds to the integer 0. This is used to represent all
28+
* integer bounds as bounds are always accompanied by an added integer delta.
29+
*/
30+
class ZeroBound extends Bound, TBoundZero {
31+
override string toString() { result = "0" }
32+
override Expr getExpr(int delta) { result.(ConstantIntegerExpr).getIntValue() = delta }
33+
}
34+
35+
/**
36+
* A bound corresponding to the value of an SSA variable.
37+
*/
38+
class SsaBound extends Bound, TBoundSsa {
39+
/** Gets the SSA variable that equals this bound. */
40+
SsaVariable getSsa() { this = TBoundSsa(result) }
41+
override string toString() { result = getSsa().toString() }
42+
override Expr getExpr(int delta) { result = getSsa().getAUse() and delta = 0 }
43+
override predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) {
44+
getSsa().getLocation().hasLocationInfo(path, sl, sc, el, ec)
45+
}
46+
}
47+
48+
/**
49+
* A bound that corresponds to the value of a specific expression that might be
50+
* interesting, but isn't otherwise represented by the value of an SSA variable.
51+
*/
52+
class ExprBound extends Bound, TBoundExpr {
53+
override string toString() { result = getExpr().toString() }
54+
override Expr getExpr(int delta) { this = TBoundExpr(result) and delta = 0 }
55+
override predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) {
56+
getExpr().hasLocationInfo(path, sl, sc, el, ec)
57+
}
58+
}

java/ql/src/semmle/code/java/dataflow/RangeAnalysis.qll

Lines changed: 2 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ private import ParityAnalysis
7272
private import semmle.code.java.Reflection
7373
private import semmle.code.java.Collections
7474
private import semmle.code.java.Maps
75+
import Bound
7576

7677
cached private module RangeAnalysisCache {
7778

@@ -409,61 +410,6 @@ private predicate boundFlowStepDiv(Expr e2, Expr e1, int factor) {
409410
)
410411
}
411412

412-
private newtype TBound =
413-
TBoundZero() or
414-
TBoundSsa(SsaVariable v) { v.getSourceVariable().getType() instanceof IntegralType } or
415-
TBoundExpr(Expr e) { e.(FieldRead).getField() instanceof ArrayLengthField and not exists(SsaVariable v | e = v.getAUse()) }
416-
417-
/**
418-
* A bound that may be inferred for an expression plus/minus an integer delta.
419-
*/
420-
abstract class Bound extends TBound {
421-
abstract string toString();
422-
/** Gets an expression that equals this bound plus `delta`. */
423-
abstract Expr getExpr(int delta);
424-
/** Gets an expression that equals this bound. */
425-
Expr getExpr() {
426-
result = getExpr(0)
427-
}
428-
predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) {
429-
path = "" and sl = 0 and sc = 0 and el = 0 and ec = 0
430-
}
431-
}
432-
433-
/**
434-
* The bound that corresponds to the integer 0. This is used to represent all
435-
* integer bounds as bounds are always accompanied by an added integer delta.
436-
*/
437-
class ZeroBound extends Bound, TBoundZero {
438-
override string toString() { result = "0" }
439-
override Expr getExpr(int delta) { result.(ConstantIntegerExpr).getIntValue() = delta }
440-
}
441-
442-
/**
443-
* A bound corresponding to the value of an SSA variable.
444-
*/
445-
class SsaBound extends Bound, TBoundSsa {
446-
/** Gets the SSA variable that equals this bound. */
447-
SsaVariable getSsa() { this = TBoundSsa(result) }
448-
override string toString() { result = getSsa().toString() }
449-
override Expr getExpr(int delta) { result = getSsa().getAUse() and delta = 0 }
450-
override predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) {
451-
getSsa().getLocation().hasLocationInfo(path, sl, sc, el, ec)
452-
}
453-
}
454-
455-
/**
456-
* A bound that corresponds to the value of a specific expression that might be
457-
* interesting, but isn't otherwise represented by the value of an SSA variable.
458-
*/
459-
class ExprBound extends Bound, TBoundExpr {
460-
override string toString() { result = getExpr().toString() }
461-
override Expr getExpr(int delta) { this = TBoundExpr(result) and delta = 0 }
462-
override predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) {
463-
getExpr().hasLocationInfo(path, sl, sc, el, ec)
464-
}
465-
}
466-
467413
/**
468414
* Holds if `b + delta` is a valid bound for `v` at `pos`.
469415
* - `upper = true` : `v <= b + delta`
@@ -632,7 +578,7 @@ private predicate baseBound(Expr e, int b, boolean upper) {
632578
*/
633579
private predicate safeNarrowingCast(NarrowingCastExpr cast, boolean upper) {
634580
exists(int bound |
635-
bounded(cast.getExpr(), TBoundZero(), bound, upper, _, _, _)
581+
bounded(cast.getExpr(), any(ZeroBound zb), bound, upper, _, _, _)
636582
|
637583
upper = true and bound <= cast.getUpperBound() or
638584
upper = false and bound >= cast.getLowerBound()

0 commit comments

Comments
 (0)