Skip to content

Commit 6042ade

Browse files
committed
move identical java and cs bound.qll to shared library
1 parent f18cdcf commit 6042ade

3 files changed

Lines changed: 125 additions & 71 deletions

File tree

java/ql/lib/semmle/code/java/dataflow/Bound.qll

Lines changed: 8 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,67 +4,16 @@
44
overlay[local?]
55
module;
66

7-
private import internal.rangeanalysis.BoundSpecific
7+
private import java as J
8+
private import internal.rangeanalysis.BoundSpecific as BoundSpecific
9+
private import codeql.rangeanalysis.Bound as SharedBound
810

9-
private newtype TBound =
10-
TBoundZero() or
11-
TBoundSsa(SsaVariable v) { v.getSourceVariable().getType() instanceof IntegralType } or
12-
TBoundExpr(Expr e) {
13-
interestingExprBound(e) and
14-
not exists(SsaVariable v | e = v.getAUse())
15-
}
11+
module BoundInstantiation = SharedBound::Bound<J::Location, BoundSpecific::BoundDefs>;
1612

17-
/**
18-
* A bound that may be inferred for an expression plus/minus an integer delta.
19-
*/
20-
abstract class Bound extends TBound {
21-
/** Gets a textual representation of this bound. */
22-
abstract string toString();
23-
24-
/** Gets an expression that equals this bound plus `delta`. */
25-
abstract Expr getExpr(int delta);
26-
27-
/** Gets an expression that equals this bound. */
28-
Expr getExpr() { result = this.getExpr(0) }
29-
30-
/** Gets the location of this bound. */
31-
abstract Location getLocation();
32-
}
33-
34-
/**
35-
* The bound that corresponds to the integer 0. This is used to represent all
36-
* integer bounds as bounds are always accompanied by an added integer delta.
37-
*/
38-
class ZeroBound extends Bound, TBoundZero {
39-
override string toString() { result = "0" }
40-
41-
override Expr getExpr(int delta) { result.(ConstantIntegerExpr).getIntValue() = delta }
13+
class Bound = BoundInstantiation::Bound;
4214

43-
override Location getLocation() { result.hasLocationInfo("", 0, 0, 0, 0) }
44-
}
45-
46-
/**
47-
* A bound corresponding to the value of an SSA variable.
48-
*/
49-
class SsaBound extends Bound, TBoundSsa {
50-
/** Gets the SSA variable that equals this bound. */
51-
SsaVariable getSsa() { this = TBoundSsa(result) }
52-
53-
override string toString() { result = this.getSsa().toString() }
54-
55-
override Expr getExpr(int delta) { result = this.getSsa().getAUse() and delta = 0 }
56-
57-
override Location getLocation() { result = this.getSsa().getLocation() }
58-
}
59-
60-
/**
61-
* A bound that corresponds to the value of a specific expression that might be
62-
* interesting, but isn't otherwise represented by the value of an SSA variable.
63-
*/
64-
class ExprBound extends Bound, TBoundExpr {
65-
override string toString() { result = this.getExpr().toString() }
15+
class ZeroBound = BoundInstantiation::ZeroBound;
6616

67-
override Expr getExpr(int delta) { this = TBoundExpr(result) and delta = 0 }
17+
class SsaBound = BoundInstantiation::SsaBound;
6818

69-
override Location getLocation() { result = this.getExpr().getLocation() }
70-
}
19+
class ExprBound = BoundInstantiation::ExprBound;

java/ql/lib/semmle/code/java/dataflow/internal/rangeanalysis/BoundSpecific.qll

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,26 @@ module;
77
private import java as J
88
private import semmle.code.java.dataflow.SSA as Ssa
99
private import semmle.code.java.dataflow.RangeUtils as RU
10+
private import codeql.rangeanalysis.Bound as SharedBound
1011

11-
class SsaVariable extends Ssa::SsaDefinition {
12-
/** Gets a use of this variable. */
13-
Expr getAUse() { result = super.getARead() }
14-
}
12+
module BoundDefs implements SharedBound::BoundDefinitions<J::Location> {
13+
class SsaVariable extends Ssa::SsaDefinition {
14+
/** Gets a use of this variable. */
15+
Expr getAUse() { result = super.getARead() }
16+
}
1517

16-
class Expr = J::Expr;
18+
class SsaSourceVariable = Ssa::SourceVariable;
1719

18-
class Location = J::Location;
20+
class Type = J::Type;
1921

20-
class IntegralType = J::IntegralType;
22+
class Expr = J::Expr;
2123

22-
class ConstantIntegerExpr = RU::ConstantIntegerExpr;
24+
class IntegralType = J::IntegralType;
2325

24-
/** Holds if `e` is a bound expression and it is not an SSA variable read. */
25-
predicate interestingExprBound(Expr e) {
26-
e.(J::FieldRead).getField() instanceof J::ArrayLengthField
27-
}
26+
class ConstantIntegerExpr = RU::ConstantIntegerExpr;
27+
28+
/** Holds if `e` is a bound expression and it is not an SSA variable read. */
29+
predicate interestingExprBound(Expr e) {
30+
e.(J::FieldRead).getField() instanceof J::ArrayLengthField
31+
}
32+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* Provides classes for representing abstract bounds for use in, for example, range analysis.
3+
*/
4+
5+
private import codeql.util.Location
6+
7+
signature module BoundDefinitions<LocationSig Location> {
8+
class Type;
9+
class IntegralType extends Type;
10+
11+
class ConstantIntegerExpr extends Expr {
12+
int getIntValue();
13+
}
14+
15+
class SsaSourceVariable {
16+
Type getType();
17+
}
18+
19+
class SsaVariable {
20+
SsaSourceVariable getSourceVariable();
21+
string toString();
22+
Location getLocation();
23+
Expr getAUse();
24+
}
25+
26+
class Expr {
27+
string toString();
28+
Location getLocation();
29+
}
30+
31+
predicate interestingExprBound(Expr e);
32+
}
33+
34+
overlay[local?]
35+
module Bound<LocationSig Location, BoundDefinitions<Location> Defs> {
36+
private import Defs
37+
38+
private newtype TBound =
39+
TBoundZero() or
40+
TBoundSsa(SsaVariable v) { v.getSourceVariable().getType() instanceof IntegralType } or
41+
TBoundExpr(Expr e) {
42+
interestingExprBound(e) and
43+
not exists(SsaVariable v | e = v.getAUse())
44+
}
45+
46+
/**
47+
* A bound that may be inferred for an expression plus/minus an integer delta.
48+
*/
49+
abstract class Bound extends TBound {
50+
/** Gets a textual representation of this bound. */
51+
abstract string toString();
52+
53+
/** Gets an expression that equals this bound plus `delta`. */
54+
abstract Expr getExpr(int delta);
55+
56+
/** Gets an expression that equals this bound. */
57+
Expr getExpr() { result = this.getExpr(0) }
58+
59+
/** Gets the location of this bound. */
60+
abstract Location getLocation();
61+
}
62+
63+
/**
64+
* The bound that corresponds to the integer 0. This is used to represent all
65+
* integer bounds as bounds are always accompanied by an added integer delta.
66+
*/
67+
class ZeroBound extends Bound, TBoundZero {
68+
override string toString() { result = "0" }
69+
70+
override Expr getExpr(int delta) { result.(ConstantIntegerExpr).getIntValue() = delta }
71+
72+
override Location getLocation() { result.hasLocationInfo("", 0, 0, 0, 0) }
73+
}
74+
75+
/**
76+
* A bound corresponding to the value of an SSA variable.
77+
*/
78+
class SsaBound extends Bound, TBoundSsa {
79+
/** Gets the SSA variable that equals this bound. */
80+
SsaVariable getSsa() { this = TBoundSsa(result) }
81+
82+
override string toString() { result = this.getSsa().toString() }
83+
84+
override Expr getExpr(int delta) { result = this.getSsa().getAUse() and delta = 0 }
85+
86+
override Location getLocation() { result = this.getSsa().getLocation() }
87+
}
88+
89+
/**
90+
* A bound that corresponds to the value of a specific expression that might be
91+
* interesting, but isn't otherwise represented by the value of an SSA variable.
92+
*/
93+
class ExprBound extends Bound, TBoundExpr {
94+
override string toString() { result = this.getExpr().toString() }
95+
96+
override Expr getExpr(int delta) { this = TBoundExpr(result) and delta = 0 }
97+
98+
override Location getLocation() { result = this.getExpr().getLocation() }
99+
}
100+
}

0 commit comments

Comments
 (0)