Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9530eb6

Browse files
committed
Java: Switch to built-in gcd.
1 parent 6f82714 commit 9530eb6

2 files changed

Lines changed: 7 additions & 41 deletions

File tree

java/ql/src/semmle/code/java/dataflow/ModulusAnalysis.qll

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -165,21 +165,6 @@ private predicate maxPhiInputRank(SsaPhiNode phi, int rix) {
165165
rix = max(int r | rankedPhiInput(phi, _, _, r))
166166
}
167167

168-
private int gcdLim() { result = 128 }
169-
170-
/**
171-
* Gets the greatest common divisor of `x` and `y`. This is restricted to small
172-
* inputs and the case when `x` and `y` are not relatively prime.
173-
*/
174-
private int gcd(int x, int y) {
175-
result != 1 and
176-
result = x.abs() and
177-
y = 0 and
178-
x in [-gcdLim() .. gcdLim()]
179-
or
180-
result = gcd(y, x % y) and y != 0 and x in [-gcdLim() .. gcdLim()]
181-
}
182-
183168
/**
184169
* Gets the remainder of `val` modulo `mod`.
185170
*
@@ -203,7 +188,7 @@ private predicate phiSelfModulus(
203188
edge.phiInput(phi, inp) and
204189
phibound.getSsa() = phi and
205190
ssaModulus(inp, edge, phibound, v, m) and
206-
mod = gcd(m, v) and
191+
mod = m.gcd(v) and
207192
mod != 1
208193
)
209194
}
@@ -233,14 +218,14 @@ private predicate phiModulusRankStep(SsaPhiNode phi, Bound b, int val, int mod,
233218
rankedPhiInput(phi, inp, edge, rix) and
234219
phiModulusRankStep(phi, b, v1, m1, rix - 1) and
235220
ssaModulus(inp, edge, b, v2, m2) and
236-
mod = gcd(gcd(m1, m2), v1 - v2)
221+
mod = m1.gcd(m2).gcd(v1 - v2)
237222
)
238223
or
239224
exists(int m2 |
240225
rankedPhiInput(phi, inp, edge, rix) and
241226
phiModulusRankStep(phi, b, v1, m1, rix - 1) and
242227
phiSelfModulus(phi, inp, edge, m2) and
243-
mod = gcd(m1, m2)
228+
mod = m1.gcd(m2)
244229
)
245230
)
246231
}
@@ -301,15 +286,15 @@ predicate exprModulus(Expr e, Bound b, int val, int mod) {
301286
cond = e and
302287
condExprBranchModulus(cond, true, b, v1, m1) and
303288
condExprBranchModulus(cond, false, b, v2, m2) and
304-
mod = gcd(gcd(m1, m2), v1 - v2) and
289+
mod = m1.gcd(m2).gcd(v1 - v2) and
305290
mod != 1 and
306291
val = remainder(v1, mod)
307292
)
308293
or
309294
exists(Bound b1, Bound b2, int v1, int v2, int m1, int m2 |
310295
addModulus(e, true, b1, v1, m1) and
311296
addModulus(e, false, b2, v2, m2) and
312-
mod = gcd(m1, m2) and
297+
mod = m1.gcd(m2) and
313298
mod != 1 and
314299
val = remainder(v1 + v2, mod)
315300
|
@@ -321,7 +306,7 @@ predicate exprModulus(Expr e, Bound b, int val, int mod) {
321306
exists(int v1, int v2, int m1, int m2 |
322307
subModulus(e, true, b, v1, m1) and
323308
subModulus(e, false, any(ZeroBound zb), v2, m2) and
324-
mod = gcd(m1, m2) and
309+
mod = m1.gcd(m2) and
325310
mod != 1 and
326311
val = remainder(v1 - v2, mod)
327312
)

java/ql/src/semmle/code/java/dataflow/RangeAnalysis.qll

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -151,25 +151,6 @@ private predicate boundCondition(
151151
)
152152
}
153153

154-
private predicate gcdInput(int x, int y) {
155-
exists(ComparisonExpr comp, Bound b |
156-
exprModulus(comp.getLesserOperand(), b, _, x) and
157-
exprModulus(comp.getGreaterOperand(), b, _, y)
158-
)
159-
or
160-
exists(int x0, int y0 |
161-
gcdInput(x0, y0) and
162-
x = y0 and
163-
y = x0 % y0
164-
)
165-
}
166-
167-
private int gcd(int x, int y) {
168-
result = x.abs() and y = 0 and gcdInput(x, y)
169-
or
170-
result = gcd(y, x % y) and y != 0 and gcdInput(x, y)
171-
}
172-
173154
/**
174155
* Holds if `comp` is a comparison between `x` and `y` for which `y - x` has a
175156
* fixed value modulo some `mod > 1`, such that the comparison can be
@@ -187,7 +168,7 @@ private predicate modulusComparison(ComparisonExpr comp, boolean testIsTrue, int
187168
// thus `k - 1 < y - x` with `0 <= k - 1 < mod`.
188169
exprModulus(comp.getLesserOperand(), b, v1, mod1) and
189170
exprModulus(comp.getGreaterOperand(), b, v2, mod2) and
190-
mod = gcd(mod1, mod2) and
171+
mod = mod1.gcd(mod2) and
191172
mod != 1 and
192173
(testIsTrue = true or testIsTrue = false) and
193174
(

0 commit comments

Comments
 (0)