Skip to content

Commit

Permalink
[GR-27649] Disallow Random/SplittableRandom in the image heap and ini…
Browse files Browse the repository at this point in the history
…tialize relevant classes at runtime.

PullRequest: graal/7695
  • Loading branch information
vjovanov committed Dec 24, 2020
2 parents d7cc62a + 5108878 commit a975ef5
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 55 deletions.
3 changes: 2 additions & 1 deletion substratevm/mx.substratevm/mx_substratevm_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,8 @@ def collect_unique_dependencies(self, path, benchmark, exclude_libs):
'-Dnative-image.benchmark.skip-agent-assertions=true',
'-Dnative-image.benchmark.extra-image-build-argument=--allow-incomplete-classpath',
'-Dnative-image.benchmark.extra-image-build-argument=--report-unsupported-elements-at-runtime',
'-Dnative-image.benchmark.extra-image-build-argument=-H:-EnableLoggingFeature'
'-Dnative-image.benchmark.extra-image-build-argument=-H:-EnableLoggingFeature',
'-Dnative-image.benchmark.extra-image-build-argument=--initialize-at-run-time=org.apache.fop.render.rtf.rtflib.rtfdoc.RtfList'
],
'batik': ['-Dnative-image.benchmark.extra-image-build-argument=--allow-incomplete-classpath']
}
Expand Down
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) {
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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1756,7 +1756,7 @@ private void checkFileOperationPreconditions() throws IOException {
}

private static TruffleFile createUniquePath(TruffleFile targetDirectory, String prefix, String suffix) {
long n = TempFileRandomHolder.RANDOM.nextLong();
long n = TempFileRandomHolder.getRandom().nextLong();
n = n == Long.MIN_VALUE ? Long.MAX_VALUE : Math.abs(n);
String name = prefix + Long.toString(n) + suffix;
TruffleFile result = targetDirectory.resolve(name);
Expand Down Expand Up @@ -1784,7 +1784,16 @@ private static boolean isEmptyPath(Path path) {
}

private static final class TempFileRandomHolder {
static final Random RANDOM = new Random();
private static Random RANDOM;

static Random getRandom() {
if (RANDOM == null) {
/* We don't want RANDOM seeds in the image heap. */
RANDOM = new Random();
}
return RANDOM;
}

}

private static final class AttributeGroup {
Expand Down
10 changes: 5 additions & 5 deletions vm/mx.vm/mx_vm_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,14 +315,15 @@ def __exit__(self, tp, value, tb):
mx.log(mx.current_mx_command())

if len(self.stages_till_now[:-1]) > 0:
mx.log(mx.colorize('--------- To only prepare the benchmark add the following to the previous command: ', 'green'))
mx.log(mx.colorize('--------- To only prepare the benchmark add the following to the end of the previous command: ', 'green'))
mx.log('-Dnative-image.benchmark.stages=' + ','.join(self.stages_till_now[:-1]))

mx.log(mx.colorize('--------- To only run the failed stage add the following to the previous command: ', 'green'))
mx.log(mx.colorize('--------- To only run the failed stage add the following to the end of the previous command: ', 'green'))
mx.log('-Dnative-image.benchmark.stages=' + self.current_stage)

mx.log(mx.colorize('--------- Additional params that can be used for the benchmark are with -Dnative-image.benchmark.<param>: ', 'green'))
mx.log(', '.join(self.config.params))
mx.log(mx.colorize('--------- Additional arguments that can be used for debugging the benchmark go after the final --: ', 'green'))
for param in self.config.params:
mx.log('-Dnative-image.benchmark.' + param + '=')

self.separator_line()
if self.non_zero_is_fatal:
Expand Down Expand Up @@ -457,7 +458,6 @@ def run_java(self, args, out=None, err=None, cwd=None, nonZeroIsFatal=False):
if self.is_llvm:
base_image_build_args += ['-H:CompilerBackend=llvm', '-H:Features=org.graalvm.home.HomeFinderFeature', '-H:DeadlockWatchdogInterval=0']
base_image_build_args += config.extra_image_build_arguments

if not self.hotspot_pgo:
# Native Image profile collection
i = 0
Expand Down
2 changes: 1 addition & 1 deletion vm/mx.vm/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
},
{
"name": "fastr",
"version": "7deda0b4e79c0e730c3ca7f2bfb8d7c4f7263ed4",
"version": "27704cc6978fb61dee3efba2d2bf89b6a2903617",
"dynamic": True,
"urls": [
{"url": "https://github.com/oracle/fastr.git", "kind": "git"},
Expand Down

0 comments on commit a975ef5

Please sign in to comment.