Skip to content

Commit 3af91d5

Browse files
authored
Merge pull request #301 from aschackmull/java/modulus-analysis
Approved by yh-semmle
2 parents 0ddb702 + 3dc9071 commit 3af91d5

File tree

10 files changed

+718
-109
lines changed

10 files changed

+718
-109
lines changed

change-notes/1.19/analysis-java.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44

55
## New queries
66

7-
| **Query** | **Tags** | **Purpose** |
8-
|-----------------------------------------------|------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
7+
| **Query** | **Tags** | **Purpose** |
8+
|-----------------------------|-----------|--------------------------------------------------------------------|
99

1010
## Changes to existing queries
1111

12-
| **Query** | **Expected impact** | **Change** |
13-
| Unreachable catch clause (`java/unreachable-catch-clause`) | Fewer false-positive results | This rule now accounts for calls to generic methods that throw generic exceptions. |
12+
| **Query** | **Expected impact** | **Change** |
13+
|----------------------------|------------------------|------------------------------------------------------------------|
14+
| Array index out of bounds (`java/index-out-of-bounds`) | Fewer false positive results | False positives involving arrays with a length evenly divisible by 3 or some greater number and an index being increased with a similar stride length are no longer reported. |
15+
| Unreachable catch clause (`java/unreachable-catch-clause`) | Fewer false positive results | This rule now accounts for calls to generic methods that throw generic exceptions. |
1416

1517
## Changes to QL libraries
1618

19+
* The `ParityAnalysis` library is replaced with the more general `ModulusAnalysis` library, which improves the range analysis.
20+

java/ql/src/filters/ImportAdditionalLibraries.ql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import java
1111
import semmle.code.java.dataflow.Guards
12+
import semmle.code.java.dataflow.ParityAnalysis
1213
import semmle.code.java.security.DataFlow
1314

1415
from File f, string tag
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+
}

0 commit comments

Comments
 (0)