Skip to content

Commit

Permalink
8257241: CDS should not handle disableEagerInitialization for archive…
Browse files Browse the repository at this point in the history
…d lambda proxy classes

Reviewed-by: iklam, redestad, mchung
  • Loading branch information
calvinccheung committed Dec 2, 2020
1 parent 7104400 commit 3da30e9
Show file tree
Hide file tree
Showing 9 changed files with 260 additions and 50 deletions.
7 changes: 2 additions & 5 deletions src/hotspot/share/classfile/systemDictionaryShared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1684,8 +1684,7 @@ InstanceKlass* SystemDictionaryShared::get_shared_nest_host(InstanceKlass* lambd
}

InstanceKlass* SystemDictionaryShared::prepare_shared_lambda_proxy_class(InstanceKlass* lambda_ik,
InstanceKlass* caller_ik,
bool initialize, TRAPS) {
InstanceKlass* caller_ik, TRAPS) {
Handle class_loader(THREAD, caller_ik->class_loader());
Handle protection_domain;
PackageEntry* pkg_entry = get_package_entry_from_class_name(class_loader, caller_ik->name());
Expand Down Expand Up @@ -1726,9 +1725,7 @@ InstanceKlass* SystemDictionaryShared::prepare_shared_lambda_proxy_class(Instanc
SystemDictionary::post_class_load_event(&class_load_start_event, loaded_lambda, ClassLoaderData::class_loader_data(class_loader()));
}

if (initialize) {
loaded_lambda->initialize(CHECK_NULL);
}
loaded_lambda->initialize(CHECK_NULL);

return loaded_lambda;
}
Expand Down
3 changes: 1 addition & 2 deletions src/hotspot/share/classfile/systemDictionaryShared.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,7 @@ class SystemDictionaryShared: public SystemDictionary {
Symbol* instantiated_method_type) NOT_CDS_RETURN_(NULL);
static InstanceKlass* get_shared_nest_host(InstanceKlass* lambda_ik) NOT_CDS_RETURN_(NULL);
static InstanceKlass* prepare_shared_lambda_proxy_class(InstanceKlass* lambda_ik,
InstanceKlass* caller_ik,
bool initialize, TRAPS) NOT_CDS_RETURN_(NULL);
InstanceKlass* caller_ik, TRAPS) NOT_CDS_RETURN_(NULL);
static bool check_linking_constraints(InstanceKlass* klass, TRAPS) NOT_CDS_RETURN_(false);
static void record_linking_constraint(Symbol* name, InstanceKlass* klass,
Handle loader1, Handle loader2, TRAPS) NOT_CDS_RETURN;
Expand Down
3 changes: 1 addition & 2 deletions src/hotspot/share/include/jvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,7 @@ JVM_LookupLambdaProxyClassFromArchive(JNIEnv* env, jclass caller,
jobject invokedType,
jobject methodType,
jobject implMethodMember,
jobject instantiatedMethodType,
jboolean initialize);
jobject instantiatedMethodType);

JNIEXPORT jboolean JNICALL
JVM_IsCDSDumpingEnabled(JNIEnv* env);
Expand Down
5 changes: 2 additions & 3 deletions src/hotspot/share/prims/jvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3825,8 +3825,7 @@ JVM_ENTRY(jclass, JVM_LookupLambdaProxyClassFromArchive(JNIEnv* env,
jobject invokedType,
jobject methodType,
jobject implMethodMember,
jobject instantiatedMethodType,
jboolean initialize))
jobject instantiatedMethodType))
JVMWrapper("JVM_LookupLambdaProxyClassFromArchive");
#if INCLUDE_CDS

Expand Down Expand Up @@ -3860,7 +3859,7 @@ JVM_ENTRY(jclass, JVM_LookupLambdaProxyClassFromArchive(JNIEnv* env,
method_type, m, instantiated_method_type);
jclass jcls = NULL;
if (lambda_ik != NULL) {
InstanceKlass* loaded_lambda = SystemDictionaryShared::prepare_shared_lambda_proxy_class(lambda_ik, caller_ik, initialize, THREAD);
InstanceKlass* loaded_lambda = SystemDictionaryShared::prepare_shared_lambda_proxy_class(lambda_ik, caller_ik, THREAD);
jcls = loaded_lambda == NULL ? NULL : (jclass) JNIHandles::make_local(THREAD, loaded_lambda->java_mirror());
}
return jcls;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,37 +271,37 @@ public Constructor<?>[] run() {
* registers the lambda proxy class for including into the CDS archive.
*/
private Class<?> spinInnerClass() throws LambdaConversionException {
// include lambda proxy class in CDS archive at dump time
if (CDS.isDumpingArchive()) {
Class<?> innerClass = generateInnerClass();
LambdaProxyClassArchive.register(targetClass,
samMethodName,
invokedType,
samMethodType,
implMethod,
instantiatedMethodType,
isSerializable,
markerInterfaces,
additionalBridges,
innerClass);
return innerClass;
}
// CDS does not handle disableEagerInitialization.
if (!disableEagerInitialization) {
// include lambda proxy class in CDS archive at dump time
if (CDS.isDumpingArchive()) {
Class<?> innerClass = generateInnerClass();
LambdaProxyClassArchive.register(targetClass,
samMethodName,
invokedType,
samMethodType,
implMethod,
instantiatedMethodType,
isSerializable,
markerInterfaces,
additionalBridges,
innerClass);
return innerClass;
}

// load from CDS archive if present
Class<?> innerClass = LambdaProxyClassArchive.find(targetClass,
samMethodName,
invokedType,
samMethodType,
implMethod,
instantiatedMethodType,
isSerializable,
markerInterfaces,
additionalBridges,
!disableEagerInitialization);
if (innerClass == null) {
innerClass = generateInnerClass();
// load from CDS archive if present
Class<?> innerClass = LambdaProxyClassArchive.find(targetClass,
samMethodName,
invokedType,
samMethodType,
implMethod,
instantiatedMethodType,
isSerializable,
markerInterfaces,
additionalBridges);
if (innerClass != null) return innerClass;
}
return innerClass;
return generateInnerClass();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ private static native Class<?> findFromArchive(Class<?> caller,
MethodType invokedType,
MethodType samMethodType,
MemberName implMethod,
MethodType instantiatedMethodType,
boolean initialize);
MethodType instantiatedMethodType);

/**
* Registers the lambdaProxyClass into CDS archive.
Expand Down Expand Up @@ -101,16 +100,15 @@ static Class<?> find(Class<?> caller,
MethodType instantiatedMethodType,
boolean isSerializable,
Class<?>[] markerInterfaces,
MethodType[] additionalBridges,
boolean initialize) {
MethodType[] additionalBridges) {
if (CDS.isDumpingArchive())
throw new IllegalStateException("cannot load class from CDS archive at dump time");

if (!loadedByBuiltinLoader(caller) || !initialize ||
if (!loadedByBuiltinLoader(caller) ||
!CDS.isSharingEnabled() || isSerializable || markerInterfaces.length > 0 || additionalBridges.length > 0)
return null;

return findFromArchive(caller, invokedName, invokedType, samMethodType,
implMethod.internalMemberName(), instantiatedMethodType, initialize);
implMethod.internalMemberName(), instantiatedMethodType);
}
}
5 changes: 2 additions & 3 deletions src/java.base/share/native/libjava/LambdaProxyClassArchive.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ Java_java_lang_invoke_LambdaProxyClassArchive_findFromArchive(JNIEnv *env, jclas
jobject invokedType,
jobject methodType,
jobject implMethodMember,
jobject instantiatedMethodType,
jboolean initialize) {
jobject instantiatedMethodType) {
return JVM_LookupLambdaProxyClassFromArchive(env, caller, invokedName, invokedType,
methodType, implMethodMember,
instantiatedMethodType, initialize);
instantiatedMethodType);
}
133 changes: 133 additions & 0 deletions test/hotspot/jtreg/runtime/cds/appcds/LambdaEagerInit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* Copyright (c) 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.
*
* 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.
*
*/

/*
* @test
* @bug 8257241
* @summary Run the LambdaEagerInitTest.java test in static CDS archive mode.
* Create a custom base archive with the -Djdk.internal.lambda.disableEagerInitialization=true property.
* Run with the custom base archive with and without specifying the property.
* With the disableEagerInit set to true during dump time, lambda proxy classes
* will not be archived. During runtime, lambda proxy classes will not be loaded
* from the archive.
* Run with the default CDS archive, lambda proxy classes will be loaded
* from the archive if the property is not set.
* @requires vm.cds
* @library /test/lib /test/hotspot/jtreg/runtime/cds/appcds test-classes
* @run main/othervm LambdaEagerInit
*/

import java.io.File;

import jdk.test.lib.cds.CDSOptions;
import jdk.test.lib.cds.CDSTestUtils;
import jdk.test.lib.process.OutputAnalyzer;

public class LambdaEagerInit {
public static void main(String[] args) throws Exception {
createArchiveWithEagerInitializationEnabled();
testWithEagerInitializationEnabled();
testWithEagerInitializationDisabled();
// Skip testing with default CDS archive on aarch64 platform because
// default archive isn't being generated on that platform.
if (!("aarch64".equals(System.getProperty("os.arch")))) {
testDefaultArchiveWithEagerInitializationEnabled();
testDefaultArchiveWithEagerInitializationDisabled();
}
}

private static final String classDir = System.getProperty("test.classes");
private static final String mainClass = LambdaEagerInitTest.class.getName();
private static final String testProperty = "-Djdk.internal.lambda.disableEagerInitialization=true";
private static final String lambdaNotLoadedFromArchive =
".class.load. java.util.stream.Collectors[$][$]Lambda[$].*/0x.*source:.*java.*util.*stream.*Collectors";
private static final String lambdaLoadedFromArchive =
".class.load. java.util.stream.Collectors[$][$]Lambda[$].*/0x.*source:.*shared.*objects.*file";
private static final String cdsLoadedLambdaProxy = ".cds.*Loaded.*lambda.*proxy";
private static final String archiveName = mainClass + ".jsa";
private static String appJar;

static void createArchiveWithEagerInitializationEnabled() throws Exception {
appJar = JarBuilder.build("lambda_eager", new File(classDir), null);

// create base archive with the -Djdk.internal.lambda.disableEagerInitialization=true property
CDSOptions opts = (new CDSOptions())
.addPrefix(testProperty,
"-Xlog:class+load,cds")
.setArchiveName(archiveName);
CDSTestUtils.createArchiveAndCheck(opts);
}

static void testWithEagerInitializationEnabled() throws Exception {
// run with custom base archive with the -Djdk.internal.lambda.disableEagerInitialization=true property
CDSOptions runOpts = (new CDSOptions())
.addPrefix("-cp", appJar, testProperty, "-Xlog:class+load,cds=debug")
.setArchiveName(archiveName)
.setUseVersion(false)
.addSuffix(mainClass);
OutputAnalyzer output = CDSTestUtils.runWithArchive(runOpts);
output.shouldMatch(lambdaNotLoadedFromArchive)
.shouldNotMatch(cdsLoadedLambdaProxy)
.shouldHaveExitValue(0);
}

static void testWithEagerInitializationDisabled() throws Exception {
// run with custom base archive without the -Djdk.internal.lambda.disableEagerInitialization=true property
CDSOptions runOpts = (new CDSOptions())
.addPrefix("-cp", appJar, "-Xlog:class+load,cds=debug")
.setArchiveName(archiveName)
.setUseVersion(false)
.addSuffix(mainClass);
OutputAnalyzer output = CDSTestUtils.runWithArchive(runOpts);
output.shouldMatch(lambdaNotLoadedFromArchive)
.shouldNotMatch(cdsLoadedLambdaProxy)
.shouldHaveExitValue(0);
}

static void testDefaultArchiveWithEagerInitializationEnabled() throws Exception {
// run with default CDS archive with the -Djdk.internal.lambda.disableEagerInitialization=true property
CDSOptions runOpts = (new CDSOptions())
.addPrefix("-cp", appJar, testProperty, "-Xlog:class+load,cds=debug")
.setUseSystemArchive(true)
.setUseVersion(false)
.addSuffix(mainClass);
OutputAnalyzer output = CDSTestUtils.runWithArchive(runOpts);
output.shouldMatch(lambdaNotLoadedFromArchive)
.shouldNotMatch(cdsLoadedLambdaProxy)
.shouldHaveExitValue(0);
}

static void testDefaultArchiveWithEagerInitializationDisabled() throws Exception {
// run with default CDS archive without the -Djdk.internal.lambda.disableEagerInitialization=true property
CDSOptions runOpts = (new CDSOptions())
.addPrefix("-cp", appJar, "-Xlog:class+load,cds=debug")
.setUseSystemArchive(true)
.setUseVersion(false)
.addSuffix(mainClass);
OutputAnalyzer output = CDSTestUtils.runWithArchive(runOpts);
output.shouldMatch(lambdaLoadedFromArchive)
.shouldMatch(cdsLoadedLambdaProxy)
.shouldHaveExitValue(0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright (c) 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.
*
* 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.
*/

/*
* This file is copied from open/test/jdk/java/lang/invoke/lambda.
* It is being used as the main class for the appcds/LambdaEagerInit.java test.
*/

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

import static jdk.test.lib.Asserts.*;

public class LambdaEagerInitTest {

interface H {Object m(String s);}

private static Set<String> allowedStaticFields(boolean nonCapturing) {
Set<String> s = new HashSet<>();
if (Boolean.getBoolean("jdk.internal.lambda.disableEagerInitialization")) {
if (nonCapturing) s.add("LAMBDA_INSTANCE$");
}
return s;
}

private void nonCapturingLambda() {
H la = s -> s;
assertEquals("hi", la.m("hi"));
Class<? extends H> c1 = la.getClass();
verifyLambdaClass(la.getClass(), true);
}

private void capturingLambda() {
H la = s -> concat(s, "foo");
assertEquals("hi foo", la.m("hi"));
verifyLambdaClass(la.getClass(), false);
}

private void verifyLambdaClass(Class<?> c, boolean nonCapturing) {
Set<String> staticFields = new HashSet<>();
Set<String> instanceFields = new HashSet<>();
for (Field f : c.getDeclaredFields()) {
if (Modifier.isStatic(f.getModifiers())) {
staticFields.add(f.getName());
} else {
instanceFields.add(f.getName());
}
}
assertEquals(instanceFields.size(), nonCapturing ? 0 : 1, "Unexpected instance fields");
assertEquals(staticFields, allowedStaticFields(nonCapturing), "Unexpected static fields");
}

private String concat(String... ss) {
return Arrays.stream(ss).collect(Collectors.joining(" "));
}

public static void main(String[] args) {
LambdaEagerInitTest test = new LambdaEagerInitTest();
test.nonCapturingLambda();
test.capturingLambda();
}
}

1 comment on commit 3da30e9

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.