Java: Minor fix to modulus analysis to handle constant expressions and not only compile time constants

This commit is contained in:
Tamas Vajk
2020-09-25 17:15:47 +02:00
parent 5688210249
commit 40a7f5aa1f
3 changed files with 5 additions and 4 deletions

View File

@@ -68,11 +68,11 @@ private Expr modExpr(Expr arg, int mod) {
exists(RemExpr rem |
result = rem and
arg = rem.getLeftOperand() and
rem.getRightOperand().(CompileTimeConstantExpr).getIntValue() = mod and
rem.getRightOperand().(ConstantIntegerExpr).getIntValue() = mod and
mod >= 2
)
or
exists(CompileTimeConstantExpr c |
exists(ConstantIntegerExpr c |
mod = 2.pow([1 .. 30]) and
c.getIntValue() = mod - 1 and
result.(AndBitwiseExpr).hasOperands(arg, c)
@@ -84,7 +84,7 @@ private Expr modExpr(Expr arg, int mod) {
* its `testIsTrue` branch.
*/
private Guard moduloCheck(SsaVariable v, int val, int mod, boolean testIsTrue) {
exists(Expr rem, CompileTimeConstantExpr c, int r, boolean polarity |
exists(Expr rem, ConstantIntegerExpr c, int r, boolean polarity |
result.isEquality(rem, c, polarity) and
c.getIntValue() = r and
rem = modExpr(v.getAUse(), mod) and

View File

@@ -22,6 +22,7 @@
| ModulusAnalysis.java:12:25:12:29 | seven | 0 | 7 | 0 |
| ModulusAnalysis.java:12:25:12:29 | seven | SSA def(seven) | 0 | 0 |
| ModulusAnalysis.java:13:32:13:34 | mul | 0 | 3 | 42 |
| ModulusAnalysis.java:13:32:13:34 | mul | 0 | 7 | 43 |
| ModulusAnalysis.java:13:32:13:34 | mul | SSA def(mul) | 0 | 0 |
| ModulusAnalysis.java:16:17:18:23 | ...?...:... | 0 | 3 | 4 |
| ModulusAnalysis.java:17:15:17:15 | i | SSA init(i) | 0 | 0 |

View File

@@ -10,7 +10,7 @@ class ModulusAnalysis
int seven = 7;
if (mul % c2 == seven) {
System.out.println(mul); // congruent 3 mod 42
System.out.println(mul); // congruent 3 mod 42, 7 mod 43
}
int j = cond