Skip to content

Commit 3608280

Browse files
committed
Java: Implement shared range analysis signatures.
1 parent c1c4a5b commit 3608280

File tree

6 files changed

+414
-29
lines changed

6 files changed

+414
-29
lines changed

java/ql/lib/qlpack.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ upgrades: upgrades
88
dependencies:
99
codeql/dataflow: ${workspace}
1010
codeql/mad: ${workspace}
11+
codeql/rangeanalysis: ${workspace}
1112
codeql/regex: ${workspace}
1213
codeql/tutorial: ${workspace}
1314
codeql/typetracking: ${workspace}

java/ql/lib/semmle/code/java/Type.qll

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,6 +1090,24 @@ class PrimitiveType extends Type, @primitive {
10901090
override string getAPrimaryQlClass() { result = "PrimitiveType" }
10911091
}
10921092

1093+
private int getByteSize(PrimitiveType t) {
1094+
t.hasName("boolean") and result = 1
1095+
or
1096+
t.hasName("byte") and result = 1
1097+
or
1098+
t.hasName("char") and result = 2
1099+
or
1100+
t.hasName("short") and result = 2
1101+
or
1102+
t.hasName("int") and result = 4
1103+
or
1104+
t.hasName("float") and result = 4
1105+
or
1106+
t.hasName("long") and result = 8
1107+
or
1108+
t.hasName("double") and result = 8
1109+
}
1110+
10931111
/** The type of the `null` literal. */
10941112
class NullType extends Type, @primitive {
10951113
NullType() { this.hasName("<nulltype>") }
@@ -1282,6 +1300,12 @@ class IntegralType extends Type {
12821300
name = ["byte", "char", "short", "int", "long"]
12831301
)
12841302
}
1303+
1304+
/** Gets the size in bytes of this numeric type. */
1305+
final int getByteSize() {
1306+
result = getByteSize(this) or
1307+
result = getByteSize(this.(BoxedType).getPrimitiveType())
1308+
}
12851309
}
12861310

12871311
/** A boolean type, which may be either a primitive or a boxed type. */

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

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,8 @@ abstract class Bound extends TBound {
2525
/** Gets an expression that equals this bound. */
2626
Expr getExpr() { result = this.getExpr(0) }
2727

28-
/**
29-
* Holds if this element is at the specified location.
30-
* The location spans column `sc` of line `sl` to
31-
* column `ec` of line `el` in file `path`.
32-
* For more information, see
33-
* [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
34-
*/
35-
predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) {
36-
path = "" and sl = 0 and sc = 0 and el = 0 and ec = 0
37-
}
28+
/** Gets the location of this bound. */
29+
abstract Location getLocation();
3830
}
3931

4032
/**
@@ -45,6 +37,8 @@ class ZeroBound extends Bound, TBoundZero {
4537
override string toString() { result = "0" }
4638

4739
override Expr getExpr(int delta) { result.(ConstantIntegerExpr).getIntValue() = delta }
40+
41+
override Location getLocation() { result.hasLocationInfo("", 0, 0, 0, 0) }
4842
}
4943

5044
/**
@@ -58,9 +52,7 @@ class SsaBound extends Bound, TBoundSsa {
5852

5953
override Expr getExpr(int delta) { result = this.getSsa().getAUse() and delta = 0 }
6054

61-
override predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) {
62-
this.getSsa().getLocation().hasLocationInfo(path, sl, sc, el, ec)
63-
}
55+
override Location getLocation() { result = this.getSsa().getLocation() }
6456
}
6557

6658
/**
@@ -72,7 +64,5 @@ class ExprBound extends Bound, TBoundExpr {
7264

7365
override Expr getExpr(int delta) { this = TBoundExpr(result) and delta = 0 }
7466

75-
override predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) {
76-
this.getExpr().getLocation().hasLocationInfo(path, sl, sc, el, ec)
77-
}
67+
override Location getLocation() { result = this.getExpr().getLocation() }
7868
}

0 commit comments

Comments
 (0)