Skip to content

Commit a1aeb99

Browse files
committed
Java: Apply deadcode guard to data flow nodes.
1 parent e8dbd65 commit a1aeb99

File tree

5 files changed

+19
-3
lines changed

5 files changed

+19
-3
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ signature int getIntValSig(Expr e);
1717
*/
1818
module CalculateConstants<getBoolValSig/1 getBoolVal, getIntValSig/1 getIntVal> {
1919
/** Gets the value of a constant boolean expression. */
20+
pragma[assume_small_delta]
2021
boolean calculateBooleanValue(Expr e) {
2122
// No casts relevant to booleans.
2223
// `!` is the only unary operator that evaluates to a boolean.
@@ -98,6 +99,7 @@ module CalculateConstants<getBoolValSig/1 getBoolVal, getIntValSig/1 getIntVal>
9899
}
99100

100101
/** Gets the value of a constant integer expression. */
102+
pragma[assume_small_delta]
101103
int calculateIntValue(Expr e) {
102104
exists(IntegralType t | e.getType() = t | t.getName().toLowerCase() != "long") and
103105
(

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ private predicate primitiveOrString(Type t) {
131131
* See JLS v8, section 15.28 (Constant Expressions).
132132
*/
133133
class CompileTimeConstantExpr extends Expr {
134+
pragma[assume_small_delta]
134135
CompileTimeConstantExpr() {
135136
primitiveOrString(this.getType()) and
136137
(
@@ -180,6 +181,7 @@ class CompileTimeConstantExpr extends Expr {
180181
/**
181182
* Gets the string value of this expression, where possible.
182183
*/
184+
pragma[assume_small_delta]
183185
pragma[nomagic]
184186
string getStringValue() {
185187
result = this.(StringLiteral).getValue()
@@ -205,6 +207,8 @@ class CompileTimeConstantExpr extends Expr {
205207
/**
206208
* Gets the boolean value of this expression, where possible.
207209
*/
210+
pragma[assume_small_delta]
211+
pragma[nomagic]
208212
boolean getBooleanValue() {
209213
// Literal value.
210214
result = this.(BooleanLiteral).getBooleanValue()

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ private module SsaImpl {
384384
private predicate intraInstanceCallEdge(Callable c1, Method m2) {
385385
exists(MethodAccess ma, RefType t1 |
386386
ma.getCaller() = c1 and
387-
m2 = viableImpl(ma) and
387+
m2 = viableImpl_v2(ma) and
388388
not m2.isStatic() and
389389
(
390390
not exists(ma.getQualifier()) or
@@ -402,7 +402,7 @@ private module SsaImpl {
402402
}
403403

404404
private Callable tgt(Call c) {
405-
result = viableImpl(c)
405+
result = viableImpl_v2(c)
406406
or
407407
result = getRunnerTarget(c)
408408
or

java/ql/lib/semmle/code/java/dataflow/internal/DataFlowNodes.qll

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,26 @@ private import DataFlowPrivate
77
private import DataFlowUtil
88
private import FlowSummaryImpl as FlowSummaryImpl
99
private import DataFlowImplCommon as DataFlowImplCommon
10+
private import semmle.code.java.controlflow.Guards
11+
private import semmle.code.java.dataflow.RangeUtils
1012

1113
/** Gets a string for approximating the name of a field. */
1214
string approximateFieldContent(FieldContent fc) { result = fc.getField().getName().prefix(1) }
1315

16+
private predicate deadcode(Expr e) {
17+
exists(Guard g, boolean b |
18+
g.(ConstantBooleanExpr).getBooleanValue() = b and
19+
g.controls(e.getBasicBlock(), b.booleanNot())
20+
)
21+
}
22+
1423
cached
1524
private module Cached {
1625
cached
1726
newtype TNode =
1827
TExprNode(Expr e) {
1928
DataFlowImplCommon::forceCachingInSameStage() and
29+
not deadcode(e) and
2030
not e.getType() instanceof VoidType and
2131
not e.getParent*() instanceof Annotation
2232
} or

java/ql/lib/semmle/code/java/dispatch/WrappedInvocation.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ private predicate runner(Method m, int n, Method runmethod) {
3434
private Expr getRunnerArgument(MethodAccess ma, Method runmethod) {
3535
exists(Method runner, int param |
3636
runner(runner, param, runmethod) and
37-
viableImpl(ma) = runner and
37+
viableImpl_v2(ma) = runner and
3838
result = ma.getArgument(param)
3939
)
4040
or

0 commit comments

Comments
 (0)