Skip to content

Commit b3526ad

Browse files
committed
[GR-54967] Avoid zero modulus when calling BigInteger.modPow.
PullRequest: graal/18225
2 parents fe960a5 + d588ab8 commit b3526ad

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

compiler/src/jdk.graal.compiler.test/src/jdk/graal/compiler/core/test/GraalCompilerTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,28 +1773,28 @@ protected CanonicalizerPhase createCanonicalizerPhase() {
17731773
public static final String SEED_PROPERTY_NAME = "test.graal.random.seed";
17741774

17751775
/**
1776-
* Globally shared, lazily initialized random generator.
1776+
* Globally shared, lazily initialized random seed.
17771777
*/
1778-
private static volatile Random randomGenerator;
1778+
private static volatile Long randomSeed;
17791779

17801780
/**
1781-
* Returns a global {@link java.util.Random} generator. The generator is seeded with the value
1782-
* specified by {@link #SEED_PROPERTY_NAME} if it exists.
1781+
* Returns a {@link java.util.Random} generator with a global seed specified by
1782+
* {@link #SEED_PROPERTY_NAME} if it exists.
17831783
*
17841784
* The used seed printed to stdout for reproducing test failures.
17851785
*/
17861786
public static Random getRandomInstance() {
1787-
if (randomGenerator == null) {
1787+
if (randomSeed == null) {
17881788
synchronized (GraalCompilerTest.class) {
1789-
if (randomGenerator == null) {
1789+
if (randomSeed == null) {
17901790
var seedLong = Long.getLong(SEED_PROPERTY_NAME);
17911791
var seed = seedLong != null ? seedLong : new Random().nextLong();
17921792
System.out.printf("Random generator seed: %d%n", seed);
17931793
System.out.printf("To re-run test with same seed, set \"-D%s=%d\" on command line.%n", SEED_PROPERTY_NAME, seed);
1794-
randomGenerator = new Random(seed);
1794+
randomSeed = seed;
17951795
}
17961796
}
17971797
}
1798-
return randomGenerator;
1798+
return new Random(randomSeed);
17991799
}
18001800
}

compiler/src/jdk.graal.compiler.test/src/jdk/graal/compiler/hotspot/test/BigIntegerIntrinsicsTest.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ public void testSquareToLen() {
121121
BigInteger big2 = randomBig(i);
122122

123123
// squareToLen is exercised via the call path modPow -> oddModPow -> montgomerySquare
124-
expectedResults.put(Pair.create(big1, big2), big1.modPow(bigTwo, big2));
124+
if (big2.signum() > 0) {
125+
expectedResults.put(Pair.create(big1, big2), big1.modPow(bigTwo, big2));
126+
}
125127
}
126128

127129
InstalledCode intrinsic = getCode(getResolvedJavaMethod(BigInteger.class, "squareToLen"), null, true, true, GraalCompilerTest.getInitialOptions());
@@ -151,19 +153,21 @@ public void testMontgomery() throws ClassNotFoundException {
151153
BigInteger big1 = randomBig(i);
152154
BigInteger big2 = randomBig(i);
153155

154-
// Invoke BigInteger BigInteger.modPow(BigExp, BigInteger)
155-
BigInteger res1 = (BigInteger) tin.invokeJava(big1, bigTwo, big2);
156+
if (big2.signum() > 0) {
157+
// Invoke BigInteger BigInteger.modPow(BigExp, BigInteger)
158+
BigInteger res1 = (BigInteger) tin.invokeJava(big1, bigTwo, big2);
156159

157-
// Invoke BigInteger testMontgomeryAux(BigInteger, BigExp, BigInteger)
158-
BigInteger res2 = (BigInteger) tin.invokeTest(big1, bigTwo, big2);
160+
// Invoke BigInteger testMontgomeryAux(BigInteger, BigExp, BigInteger)
161+
BigInteger res2 = (BigInteger) tin.invokeTest(big1, bigTwo, big2);
159162

160-
assertDeepEquals(res1, res2);
163+
assertDeepEquals(res1, res2);
161164

162-
// Invoke BigInteger testMontgomeryAux(BigInteger, BigExp, BigInteger)
163-
// through code handle.
164-
BigInteger res3 = (BigInteger) tin.invokeCode(big1, bigTwo, big2);
165+
// Invoke BigInteger testMontgomeryAux(BigInteger, BigExp, BigInteger)
166+
// through code handle.
167+
BigInteger res3 = (BigInteger) tin.invokeCode(big1, bigTwo, big2);
165168

166-
assertDeepEquals(res1, res3);
169+
assertDeepEquals(res1, res3);
170+
}
167171
}
168172
}
169173

0 commit comments

Comments
 (0)