Skip to content

Commit

Permalink
Initialize ThreadLocalRandom.seeder at run time.
Browse files Browse the repository at this point in the history
  • Loading branch information
cstancu committed Sep 10, 2020
1 parent 5375374 commit 1b7b2c8
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 64 deletions.
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@
*/
package com.oracle.svm.core.jdk;

import java.util.concurrent.ThreadLocalRandom;

import com.oracle.svm.core.annotate.AutomaticFeature;
import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.hosted.Feature;
import org.graalvm.nativeimage.impl.RuntimeClassInitializationSupport;

import com.oracle.svm.core.annotate.AutomaticFeature;

@AutomaticFeature
public class RandomNumbersFeature implements Feature {

Expand All @@ -41,6 +40,5 @@ public void duringSetup(DuringSetupAccess access) {
* values properly. Otherwise the numbers generated will be fixed for each generated image.
*/
ImageSingletons.lookup(RuntimeClassInitializationSupport.class).rerunInitialization(access.findClassByName("java.lang.Math$RandomNumberGeneratorHolder"), "for random number generator");
ImageSingletons.lookup(RuntimeClassInitializationSupport.class).rerunInitialization(ThreadLocalRandom.class, "for random number generator");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,76 +34,28 @@
@TargetClass(java.util.SplittableRandom.class)
final class Target_java_util_SplittableRandom {

/**
* 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(SplittableRandomAccessors.class)//
private static AtomicLong defaultGen;

@Alias
static native long mix64(long z);
}

public class SplittableRandomAccessors {

/*
* We run this code 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");
}
});
public class SplittableRandomAccessors extends RandomAccessors {

private static volatile AtomicLong defaultGen;
private static final SplittableRandomAccessors SINGLETON = new SplittableRandomAccessors();

/** The get-accessor for SplittableRandom.defaultGen. */
public static AtomicLong getDefaultGen() {
AtomicLong result = defaultGen;
if (result == null) {
result = initialize();
}
return result;
}

private static class Lock {
return SINGLETON.getOrInitializeSeeder();
}

private static final Lock lock = new Lock();

// Checkstyle: allow synchronization
private static AtomicLong initialize() {
/**
* Lock on an instance instead of a java.lang.Class object because SVM currently uses a
* secondary storage map for locking on classes, which in this particular case can lead to
* recursive locking problems when this code is called from the constructor of
* JavaVMOperation.
*/
synchronized (lock) {
AtomicLong result = defaultGen;
if (result != null) {
return result;
}

/*
* The code below to compute the seed is taken from the original
* SplittableRandom.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 = Target_java_util_SplittableRandom.mix64(System.currentTimeMillis()) ^ Target_java_util_SplittableRandom.mix64(System.nanoTime());
}

result = new AtomicLong(seed);
defaultGen = result;
return result;
}
@Override
long mix64(long l) {
return Target_java_util_SplittableRandom.mix64(l);
}
// Checkstyle: disallow synchronization
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,10 @@ public class MultiThreadedMonitorSupport extends MonitorSupport {
* The map access in MultiThreadedMonitorSupport.getOrCreateMonitorFromMap() calls
* System.identityHashCode() which on the slow path calls
* IdentityHashCodeSupport.generateIdentityHashCode(). The hashcode generation calls
* SplittableRandomAccessors.initialize() which synchronizes on the a Lock object.
* SplittableRandomAccessors.initialize() which synchronizes on an instance of
* SplittableRandomAccessors.
*/
monitorTypes.add(Class.forName("com.oracle.svm.core.jdk.SplittableRandomAccessors$Lock"));
monitorTypes.add(Class.forName("com.oracle.svm.core.jdk.SplittableRandomAccessors"));

FORCE_MONITOR_SLOT_TYPES = Collections.unmodifiableSet(monitorTypes);
} catch (ClassNotFoundException e) {
Expand Down

0 comments on commit 1b7b2c8

Please sign in to comment.