|
| 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 | +} |
0 commit comments