Skip to content

Commit f97118f

Browse files
committed
Guards: Generalise Guard.isEquality to use GuardValue.
1 parent 9a66ef1 commit f97118f

13 files changed

Lines changed: 53 additions & 37 deletions

File tree

‎csharp/ql/lib/semmle/code/csharp/dataflow/internal/rangeanalysis/RangeUtils.qll‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ private module Impl {
9494
exists(Expr e1_, Expr e2_ |
9595
e1 = unique(ExprNode cfn | hasChild(this, e1_, _, cfn) | cfn) and
9696
e2 = unique(ExprNode cfn | hasChild(this, e2_, _, cfn) | cfn) and
97-
super.isEquality(e1_, e2_, polarity)
97+
super.isEquality(e1_, e2_, any(G::GuardValue gv | gv.asBooleanValue() = polarity))
9898
)
9999
}
100100
}

‎csharp/ql/test/library-tests/dataflow/barrier-guards/barrier-flow.ql‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import csharp
66
import semmle.code.csharp.controlflow.Guards
77

88
private predicate stringConstCompare(Guard guard, Expr testedNode, GuardValue value) {
9-
guard.isEquality(any(StringLiteral lit), testedNode, value.asBooleanValue())
9+
guard.isEquality(any(StringLiteral lit), testedNode, value)
1010
}
1111

1212
class StringConstCompareBarrier extends DataFlow::Node {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import csharp
22
import semmle.code.csharp.controlflow.Guards
33

4-
from Guard guard, Expr e1, Expr e2, boolean eqval
4+
from Guard guard, Expr e1, Expr e2, GuardValue eqval
55
where guard.isEquality(e1, e2, eqval)
66
select guard, eqval, e1, e2

‎go/ql/lib/semmle/go/controlflow/Guards.qll‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -545,14 +545,14 @@ predicate guardEnsures(Expr e, boolean b, BasicBlock bb) { e.(Guard).controls(bb
545545

546546
/** Holds if `guard` evaluating to `branch` ensures that `i = j` holds. */
547547
predicate guardEnsuresEq(Guard guard, boolean branch, DataFlow::Node i, DataFlow::Node j) {
548-
guard.isEquality(i.asExpr(), j.asExpr(), branch)
548+
guard.isEquality(i.asExpr(), j.asExpr(), any(GuardValue gv | gv.asBooleanValue() = branch))
549549
}
550550

551551
/** Holds if `guard` evaluating to `branch` ensures that `i != j` holds. */
552552
predicate guardEnsuresNeq(Guard guard, boolean branch, DataFlow::Node i, DataFlow::Node j) {
553-
exists(boolean eqval |
553+
exists(GuardValue eqval |
554554
guard.isEquality(i.asExpr(), j.asExpr(), eqval) and
555-
branch = eqval.booleanNot()
555+
branch = eqval.getDualValue().asBooleanValue()
556556
)
557557
}
558558

‎java/ql/lib/semmle/code/java/dataflow/RangeAnalysis.qll‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@ module Sem implements Semantic<Location> {
208208

209209
class Guard extends G::Guards_v2::Guard {
210210
Expr asExpr() { result = this }
211+
212+
predicate isEquality(Expr e1, Expr e2, boolean eqval) {
213+
super.isEquality(e1, e2, any(G::GuardValue gv | gv.asBooleanValue() = eqval))
214+
}
211215
}
212216

213217
class Type = J::Type;

‎java/ql/lib/semmle/code/java/dataflow/internal/rangeanalysis/ModulusAnalysisSpecific.qll‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ module Private {
1919

2020
class Expr = J::Expr;
2121

22-
class Guard = G::Guards_v2::Guard;
22+
class Guard extends G::Guards_v2::Guard {
23+
predicate isEquality(Expr e1, Expr e2, boolean eqval) {
24+
super.isEquality(e1, e2, any(G::GuardValue gv | gv.asBooleanValue() = eqval))
25+
}
26+
}
2327

2428
class ConstantIntegerExpr = RU::ConstantIntegerExpr;
2529

‎java/ql/lib/semmle/code/java/dataflow/internal/rangeanalysis/SignAnalysisSpecific.qll‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ module Private {
1515

1616
class ConstantIntegerExpr = RU::ConstantIntegerExpr;
1717

18-
class Guard = G::Guards_v2::Guard;
18+
class Guard extends G::Guards_v2::Guard {
19+
predicate isEquality(Expr e1, Expr e2, boolean eqval) {
20+
super.isEquality(e1, e2, any(G::GuardValue gv | gv.asBooleanValue() = eqval))
21+
}
22+
}
1923

2024
class SsaVariable = Ssa::SsaDefinition;
2125

‎java/ql/lib/semmle/code/java/os/OSCheck.qll‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ private Guard isOsFromSystemPropertyEqualityCheck(
7474
any(Literal literal |
7575
(literal instanceof CharacterLiteral or literal instanceof StringLiteral) and
7676
literal.getValue() = compareToLiteral
77-
), branch)
77+
), any(GuardValue gv | gv.asBooleanValue() = branch))
7878
}
7979

8080
private class IsWindowsFromPathSeparator extends IsWindowsGuard {

‎java/ql/lib/semmle/code/java/security/AndroidIntentRedirection.qll‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,19 @@ private class ExternalIntentRedirectionSanitizer extends IntentRedirectionSaniti
4848
private class DefaultIntentRedirectionSanitizer extends IntentRedirectionSanitizer {
4949
DefaultIntentRedirectionSanitizer() {
5050
this.getType() instanceof TypeIntent and
51-
exists(MethodCall ma, Method m, Guard g, boolean branch |
51+
exists(MethodCall ma, Method m, Guard g, GuardValue branch |
5252
ma.getMethod() = m and
5353
m.getDeclaringType() instanceof TypeComponentName and
5454
m.hasName("getPackageName") and
5555
g.isEquality(ma, _, branch) and
56-
g.controls(this.asExpr().getBasicBlock(), branch)
56+
g.valueControls(this.asExpr().getBasicBlock(), branch)
5757
) and
58-
exists(MethodCall ma, Method m, Guard g, boolean branch |
58+
exists(MethodCall ma, Method m, Guard g, GuardValue branch |
5959
ma.getMethod() = m and
6060
m.getDeclaringType() instanceof TypeComponentName and
6161
m.hasName("getClassName") and
6262
g.isEquality(ma, _, branch) and
63-
g.controls(this.asExpr().getBasicBlock(), branch)
63+
g.valueControls(this.asExpr().getBasicBlock(), branch)
6464
)
6565
}
6666
}

‎java/ql/lib/semmle/code/java/security/ArithmeticCommon.qll‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,24 +64,25 @@ private Guard sizeGuard(Expr e, boolean branch, boolean upper) {
6464
branch = true
6565
)
6666
)
67-
or
68-
result.isEquality(e, _, branch) and
69-
(upper = true or upper = false)
7067
}
7168

72-
private predicate sizeGuardLessThan(Guard g, Expr e, boolean branch) {
73-
g = sizeGuard(e, branch, true)
69+
private predicate sizeGuardLessThan(Guard g, Expr e, GuardValue gv) {
70+
g = sizeGuard(e, gv.asBooleanValue(), true)
71+
or
72+
g.isEquality(e, _, gv)
7473
}
7574

76-
private predicate sizeGuardGreaterThan(Guard g, Expr e, boolean branch) {
77-
g = sizeGuard(e, branch, false)
75+
private predicate sizeGuardGreaterThan(Guard g, Expr e, GuardValue gv) {
76+
g = sizeGuard(e, gv.asBooleanValue(), false)
77+
or
78+
g.isEquality(e, _, gv)
7879
}
7980

8081
/**
8182
* Holds if `n` is bounded in a way that is likely to prevent overflow.
8283
*/
8384
predicate guardedLessThanSomething(DataFlow::Node n) {
84-
DataFlow::BarrierGuard<sizeGuardLessThan/3>::getABarrierNode() = n
85+
DataFlow::BarrierGuardValue<sizeGuardLessThan/3>::getABarrierNode() = n
8586
or
8687
negative(n.asExpr())
8788
or
@@ -92,7 +93,7 @@ predicate guardedLessThanSomething(DataFlow::Node n) {
9293
* Holds if `e` is bounded in a way that is likely to prevent underflow.
9394
*/
9495
predicate guardedGreaterThanSomething(DataFlow::Node n) {
95-
DataFlow::BarrierGuard<sizeGuardGreaterThan/3>::getABarrierNode() = n
96+
DataFlow::BarrierGuardValue<sizeGuardGreaterThan/3>::getABarrierNode() = n
9697
or
9798
positive(n.asExpr())
9899
or

0 commit comments

Comments
 (0)