Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallow Random/SplittableRandom in the image heap and initialize relevant classes at runtime #2993

Closed
wants to merge 7 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
import java.lang.reflect.Field;
import java.nio.Buffer;
import java.nio.MappedByteBuffer;
import java.util.Random;
import java.util.SplittableRandom;
import java.util.concurrent.ThreadLocalRandom;

import com.oracle.svm.core.util.VMError;
import com.oracle.svm.util.ReflectionUtil;
Expand All @@ -55,6 +58,13 @@ public interface DisallowedObjectReporter {
}

public static void check(Object obj, DisallowedObjectReporter reporter) {
/* Random/SplittableRandom can not be in the image heap. */
if (((obj instanceof Random) && !(obj instanceof ThreadLocalRandom)) || obj instanceof SplittableRandom) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughtfull--thanks!

throw reporter.raise("Detected an instance of Random/SplittableRandom class in the image heap. " +
"Instances created during image generation have cached seed values and don't behave as expected.",
obj, "Try avoiding to initialize the class that caused initialization of the object.");
}

/* Started Threads can not be in the image heap. */
if (obj instanceof Thread) {
final Thread asThread = (Thread) obj;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static AtomicLong getSeeder() {
return SINGLETON.getOrInitializeSeeder();
}

/** The setter is necessary if ThreadLocalRandom is initilized at run time. */
/** The setter is necessary if ThreadLocalRandom is initialized at run time. */
public static void setSeeder(AtomicLong value) {
SINGLETON.seeder = value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,14 @@ public void afterRegistration(AfterRegistrationAccess access) {
RuntimeClassInitialization.initializeAtBuildTime("sun.security.x509", "Core JDK classes are initialized at build time");
RuntimeClassInitialization.initializeAtBuildTime("sun.security.smartcardio", "Core JDK classes are initialized at build time");

// contains a SecureRandom reference, therefore it can't be included in the image heap
// contain Random references, therefore can't be included in the image heap
RuntimeClassInitialization.initializeAtRunTime(com.sun.jndi.dns.DnsClient.class);
RuntimeClassInitialization.initializeAtRunTime("sun.net.www.protocol.http.DigestAuthentication$Parameters");
RuntimeClassInitialization.initializeAtRunTime("sun.security.krb5.KrbServiceLocator");

// The random number provider classes should be reinitialized at runtime to reset their
// values properly. Otherwise the numbers generated will be fixed for each generated image.
RuntimeClassInitialization.initializeAtRunTime("java.lang.Math$RandomNumberGeneratorHolder");
RuntimeClassInitialization.initializeAtRunTime("java.lang.StrictMath$RandomNumberGeneratorHolder");
}
}