From 19298b728629dd59e0142f4f5888811deb9b5bd9 Mon Sep 17 00:00:00 2001 From: Vojin Jovanovic Date: Thu, 24 Dec 2020 01:54:29 +0100 Subject: [PATCH] Backport GR-27649 --- .../svm/core/jdk/RandomNumbersFeature.java | 44 ------------------- .../core/jdk/ThreadLocalRandomAccessors.java | 2 +- .../hosted/jdk/JDKInitializationFeature.java | 9 +++- .../com/oracle/truffle/api/TruffleFile.java | 13 +++++- 4 files changed, 20 insertions(+), 48 deletions(-) delete mode 100644 substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/RandomNumbersFeature.java diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/RandomNumbersFeature.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/RandomNumbersFeature.java deleted file mode 100644 index a45e4fd8f6cc..000000000000 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/RandomNumbersFeature.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (c) 2019, 2019, 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 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 { - - @Override - public void duringSetup(DuringSetupAccess access) { - /* - * 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. - */ - ImageSingletons.lookup(RuntimeClassInitializationSupport.class).rerunInitialization(access.findClassByName("java.lang.Math$RandomNumberGeneratorHolder"), "for random number generator"); - } -} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/ThreadLocalRandomAccessors.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/ThreadLocalRandomAccessors.java index 28ca4acdd76e..c2a93a91a76b 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/ThreadLocalRandomAccessors.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/ThreadLocalRandomAccessors.java @@ -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; } diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/jdk/JDKInitializationFeature.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/jdk/JDKInitializationFeature.java index 249b1868ffdf..50bd1db6fe08 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/jdk/JDKInitializationFeature.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/jdk/JDKInitializationFeature.java @@ -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"); } } diff --git a/truffle/src/com.oracle.truffle.api/src/com/oracle/truffle/api/TruffleFile.java b/truffle/src/com.oracle.truffle.api/src/com/oracle/truffle/api/TruffleFile.java index 00279e411bc0..40670ad0eef8 100644 --- a/truffle/src/com.oracle.truffle.api/src/com/oracle/truffle/api/TruffleFile.java +++ b/truffle/src/com.oracle.truffle.api/src/com/oracle/truffle/api/TruffleFile.java @@ -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); @@ -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 {