-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initialize ThreadLocalRandom.seeder at run time.
- Loading branch information
Showing
5 changed files
with
174 additions
and
64 deletions.
There are no files selected for viewing
97 changes: 97 additions & 0 deletions
97
substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/RandomAccessors.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* Copyright (c) 2020, 2020, Oracle and/or its affiliates. All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* This code is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. Oracle designates this | ||
* particular file as subject to the "Classpath" exception as provided | ||
* by Oracle in the LICENSE file that accompanied this code. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
* or visit www.oracle.com if you need additional information or have any | ||
* questions. | ||
*/ | ||
|
||
package com.oracle.svm.core.jdk; | ||
|
||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
/** | ||
* RandomAccessors initializes a seeder at run time, on first access. The mechanism is used by both | ||
* SplittableRandomAccessors and ThreadLocalRandomAccessors since they share the same seeder | ||
* initialization logic, but use a different implementation of mix64(). | ||
*/ | ||
public abstract class RandomAccessors { | ||
|
||
/* | ||
* We read the value of java.util.secureRandomSeed deliberately during image generation, so that | ||
* the SecureRandom code is only reachable and included in the image when requested by the | ||
* application. | ||
*/ | ||
private static final boolean SECURE_SEED = java.security.AccessController.doPrivileged( | ||
new java.security.PrivilegedAction<Boolean>() { | ||
@Override | ||
public Boolean run() { | ||
return Boolean.getBoolean("java.util.secureRandomSeed"); | ||
} | ||
}); | ||
|
||
private volatile AtomicLong seeder; | ||
|
||
protected AtomicLong getOrInitializeSeeder() { | ||
AtomicLong result = seeder; | ||
if (result == null) { | ||
result = initialize(); | ||
} | ||
return result; | ||
} | ||
|
||
// Checkstyle: allow synchronization | ||
/** | ||
* It is important that this synchronization is on an instance method and not on a static | ||
* method. A static synchronized method will lock the java.lang.Class object and SVM currently | ||
* uses a secondary storage map for locking on classes. Syncronizing on a class object can lead | ||
* to recursive locking problems when this particular code is called from the constructor of | ||
* JavaVMOperation. | ||
*/ | ||
private synchronized AtomicLong initialize() { | ||
AtomicLong result = seeder; | ||
if (result != null) { | ||
return result; | ||
} | ||
|
||
/* | ||
* The code below to compute the seed is taken from the original | ||
* SplittableRandom.initialSeed()/ThreadLocalRandom.initialSeed() implementation. | ||
*/ | ||
long seed; | ||
if (SECURE_SEED) { | ||
byte[] seedBytes = java.security.SecureRandom.getSeed(8); | ||
seed = seedBytes[0] & 0xffL; | ||
for (int i = 1; i < 8; ++i) { | ||
seed = (seed << 8) | (seedBytes[i] & 0xffL); | ||
} | ||
} else { | ||
seed = mix64(System.currentTimeMillis()) ^ mix64(System.nanoTime()); | ||
} | ||
|
||
result = new AtomicLong(seed); | ||
seeder = result; | ||
return result; | ||
|
||
} | ||
// Checkstyle: disallow synchronization | ||
|
||
abstract long mix64(long l); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
...atevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/ThreadLocalRandomAccessors.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright (c) 2020, 2020, Oracle and/or its affiliates. All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* This code is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. Oracle designates this | ||
* particular file as subject to the "Classpath" exception as provided | ||
* by Oracle in the LICENSE file that accompanied this code. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
* or visit www.oracle.com if you need additional information or have any | ||
* questions. | ||
*/ | ||
|
||
package com.oracle.svm.core.jdk; | ||
|
||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
import com.oracle.svm.core.annotate.Alias; | ||
import com.oracle.svm.core.annotate.InjectAccessors; | ||
import com.oracle.svm.core.annotate.TargetClass; | ||
|
||
@TargetClass(java.util.concurrent.ThreadLocalRandom.class) | ||
final class Target_java_util_concurrent_ThreadLocalRandom { | ||
|
||
/** | ||
* The seed generator for default constructors is initialized at run time, on first access, to | ||
* prevent baking in an initial seed from the build system. | ||
*/ | ||
@Alias @InjectAccessors(ThreadLocalRandomAccessors.class)// | ||
private static AtomicLong seeder; | ||
|
||
@Alias | ||
static native long mix64(long z); | ||
|
||
} | ||
|
||
public class ThreadLocalRandomAccessors extends RandomAccessors { | ||
|
||
private static final ThreadLocalRandomAccessors SINGLETON = new ThreadLocalRandomAccessors(); | ||
|
||
/** The get-accessor for ThreadLocalRandom.seeder. */ | ||
public static AtomicLong getSeeder() { | ||
return SINGLETON.getOrInitializeSeeder(); | ||
} | ||
|
||
@Override | ||
long mix64(long l) { | ||
return Target_java_util_concurrent_ThreadLocalRandom.mix64(l); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters