Skip to content

Commit d347597

Browse files
committed
Unify obtaining SecureRandom instance
1 parent abb47de commit d347597

File tree

3 files changed

+26
-33
lines changed

3 files changed

+26
-33
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/PosixModuleBuiltins.java

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import static com.oracle.graal.python.runtime.exception.PythonErrorType.ValueError;
3434

3535
import java.math.BigInteger;
36-
import java.security.NoSuchAlgorithmException;
3736
import java.security.SecureRandom;
3837
import java.util.ArrayList;
3938
import java.util.Collections;
@@ -1998,27 +1997,18 @@ int system(PBytes command,
19981997
@GenerateNodeFactory
19991998
@TypeSystemReference(PythonArithmeticTypes.class)
20001999
abstract static class URandomNode extends PythonUnaryClinicBuiltinNode {
2001-
private static SecureRandom secureRandom;
2002-
2003-
private static SecureRandom createRandomInstance() {
2004-
try {
2005-
return SecureRandom.getInstance("NativePRNGNonBlocking");
2006-
} catch (NoSuchAlgorithmException e) {
2007-
throw new IllegalStateException(e);
2008-
}
2009-
}
2010-
20112000
@Specialization
2012-
@TruffleBoundary(allowInlining = true)
20132001
PBytes urandom(int size) {
2014-
if (secureRandom == null) {
2015-
secureRandom = createRandomInstance();
2016-
}
20172002
byte[] bytes = new byte[size];
2018-
secureRandom.nextBytes(bytes);
2003+
nextBytes(getContext().getSecureRandom(), bytes);
20192004
return factory().createBytes(bytes);
20202005
}
20212006

2007+
@TruffleBoundary
2008+
private static void nextBytes(SecureRandom secureRandom, byte[] bytes) {
2009+
secureRandom.nextBytes(bytes);
2010+
}
2011+
20222012
@Override
20232013
protected ArgumentClinicProvider getArgumentClinic() {
20242014
return PosixModuleBuiltinsClinicProviders.URandomNodeClinicProviderGen.INSTANCE;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/random/RandomBuiltins.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import java.math.BigInteger;
4747
import java.nio.ByteBuffer;
4848
import java.nio.ByteOrder;
49-
import java.security.NoSuchAlgorithmException;
5049
import java.security.SecureRandom;
5150
import java.util.List;
5251

@@ -89,20 +88,10 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
8988
@GenerateNodeFactory
9089
@TypeSystemReference(PythonArithmeticTypes.class)
9190
public abstract static class SeedNode extends PythonBuiltinNode {
92-
93-
private static SecureRandom secureRandom;
94-
9591
@Specialization
9692
@TruffleBoundary
9793
PNone seedNone(PRandom random, @SuppressWarnings("unused") PNone none) {
98-
if (secureRandom == null) {
99-
try {
100-
secureRandom = SecureRandom.getInstance("NativePRNGNonBlocking");
101-
} catch (NoSuchAlgorithmException e) {
102-
seedLong(random, System.currentTimeMillis());
103-
return PNone.NONE;
104-
}
105-
}
94+
SecureRandom secureRandom = getContext().getSecureRandom();
10695
int[] seed = new int[PRandom.N];
10796
for (int i = 0; i < seed.length; ++i) {
10897
seed[i] = secureRandom.nextInt();

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonContext.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,8 @@ PythonThreadState getThreadState() {
421421
private final ThreadGroup threadGroup = new ThreadGroup(GRAALPYTHON_THREADS);
422422
private final IDUtils idUtils = new IDUtils();
423423

424+
@CompilationFinal private SecureRandom secureRandom;
425+
424426
// Equivalent of _Py_HashSecret
425427
@CompilationFinal(dimensions = 1) private byte[] hashSecret = new byte[24];
426428

@@ -1079,6 +1081,22 @@ public long getPerfCounterStart() {
10791081
return perfCounterStart;
10801082
}
10811083

1084+
/**
1085+
* Get a SecureRandom instance using a non-blocking source.
1086+
*/
1087+
public SecureRandom getSecureRandom() {
1088+
assert !ImageInfo.inImageBuildtimeCode();
1089+
if (secureRandom == null) {
1090+
CompilerDirectives.transferToInterpreterAndInvalidate();
1091+
try {
1092+
secureRandom = SecureRandom.getInstance("NativePRNGNonBlocking");
1093+
} catch (NoSuchAlgorithmException e) {
1094+
throw new RuntimeException("Unable to obtain entropy source for random number generation (NativePRNGNonBlocking)", e);
1095+
}
1096+
}
1097+
return secureRandom;
1098+
}
1099+
10821100
public byte[] getHashSecret() {
10831101
assert !ImageInfo.inImageBuildtimeCode();
10841102
return hashSecret;
@@ -1271,11 +1289,7 @@ private void setupRuntimeInformation(boolean isPatching) {
12711289
private void initializeHashSecret() {
12721290
String hashSeed = getOption(PythonOptions.HashSeed);
12731291
if (hashSeed.equals("random")) {
1274-
try {
1275-
SecureRandom.getInstance("NativePRNGNonBlocking").nextBytes(hashSecret);
1276-
} catch (NoSuchAlgorithmException e) {
1277-
throw new RuntimeException("Unable to obtain entropy source for hash randomization (NativePRNGNonBlocking)");
1278-
}
1292+
getSecureRandom().nextBytes(hashSecret);
12791293
} else {
12801294
try {
12811295
long hashSeedValue = Long.parseLong(hashSeed);

0 commit comments

Comments
 (0)