diff --git a/src/hotspot/share/cds/metaspaceShared.cpp b/src/hotspot/share/cds/metaspaceShared.cpp index 6e24b70d782..37a4de8ff1f 100644 --- a/src/hotspot/share/cds/metaspaceShared.cpp +++ b/src/hotspot/share/cds/metaspaceShared.cpp @@ -792,7 +792,7 @@ bool MetaspaceShared::try_link_class(JavaThread* current, InstanceKlass* ik) { ExceptionMark em(current); JavaThread* THREAD = current; // For exception macros. Arguments::assert_is_dumping_archive(); - if (ik->is_loaded() && !ik->is_linked() && ik->can_be_verified_at_dumptime() && + if (!ik->is_shared() && ik->is_loaded() && !ik->is_linked() && ik->can_be_verified_at_dumptime() && !SystemDictionaryShared::has_class_failed_verification(ik)) { bool saved = BytecodeVerificationLocal; if (ik->is_shared_unregistered_class() && ik->class_loader() == nullptr) { diff --git a/test/hotspot/jtreg/runtime/cds/appcds/dynamicArchive/OldClassVerifierTrouble.java b/test/hotspot/jtreg/runtime/cds/appcds/dynamicArchive/OldClassVerifierTrouble.java new file mode 100644 index 00000000000..afce5668124 --- /dev/null +++ b/test/hotspot/jtreg/runtime/cds/appcds/dynamicArchive/OldClassVerifierTrouble.java @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2023, 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 8312181 + * @summary Archive an old class, which fails verification and is a supertype of another + * class, in the base archive. Perform a dynamic dump with the base archive + * containing the old class and its subclass in "unlinked" state. VM should + * not crash in this scenario. + * @requires vm.cds + * @library /test/lib /test/hotspot/jtreg/runtime/cds/appcds/test-classes /test/hotspot/jtreg/runtime/cds/appcds + * @compile test-classes/VerifierTroublev49.jasm + * @compile test-classes/ChildOldSuper.java + * @compile test-classes/VerifierTroubleApp.java + * @build jdk.test.whitebox.WhiteBox + * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar oldsuper-fail-verifier.jar ChildOldSuper VerifierTroublev49 VerifierTroubleApp + * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox + * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. OldClassVerifierTrouble + */ + +import java.io.File; +import jdk.test.lib.cds.CDSOptions; +import jdk.test.lib.cds.CDSTestUtils; +import jdk.test.lib.process.OutputAnalyzer; +import jdk.test.lib.helpers.ClassFileInstaller; + +public class OldClassVerifierTrouble extends DynamicArchiveTestBase { + static final String appClass = "VerifierTroubleApp"; + static final String baseArchiveClass = "OldSuperVerifierTrouble"; + + public static void main(String[] args) throws Exception { + runTest(OldClassVerifierTrouble::testCustomBase); + } + + static void testCustomBase() throws Exception { + String topArchiveName = getNewArchiveName("top"); + doTestCustomBase(topArchiveName); + } + + private static void doTestCustomBase(String topArchiveName) throws Exception { + String appJar = ClassFileInstaller.getJarPath("oldsuper-fail-verifier.jar"); + + // create a custom base archive containing an old class + OutputAnalyzer output = TestCommon.dump(appJar, + TestCommon.list("VerifierTroubleApp", "VerifierTroublev49", "ChildOldSuper"), + "-Xlog:class+load,cds+class=debug"); + TestCommon.checkDump(output); + // Check the ChildOldSuper and VerifierTroublev49 are being dumped into the base archive. + output.shouldMatch(".cds.class.*klass.*0x.*app.*ChildOldSuper.*unlinked") + .shouldMatch(".cds.class.*klass.*0x.*app.*VerifierTroublev49.*unlinked"); + + String baseArchiveName = TestCommon.getCurrentArchiveName(); + + // create a dynamic archive with the custom base archive. + // The old class is in the base archive and will be + // accessed from VerifierTroubleApp. + // Linking VerifierTroublev49 would result in java.lang.VerifyError. + dump2(baseArchiveName, topArchiveName, + "-Xlog:cds,cds+dynamic,class+load,cds+class=debug", + "-cp", appJar, + appClass) + .assertAbnormalExit(out -> { + out.shouldContain("VerifierTroublev49 source: shared objects file") + .shouldContain("ChildOldSuper source: shared objects file") + .shouldContain("java.lang.VerifyError: " + + "(class: VerifierTroublev49, method: doit signature: ()Ljava/lang/String;)" + + " Wrong return type in function"); + }); + } +} diff --git a/test/hotspot/jtreg/runtime/cds/appcds/dynamicArchive/test-classes/ChildOldSuper.java b/test/hotspot/jtreg/runtime/cds/appcds/dynamicArchive/test-classes/ChildOldSuper.java new file mode 100644 index 00000000000..c2f6ea604d7 --- /dev/null +++ b/test/hotspot/jtreg/runtime/cds/appcds/dynamicArchive/test-classes/ChildOldSuper.java @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023, 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. + * + */ +public class ChildOldSuper extends VerifierTroublev49 { + public String doit() { + return super.doit(); + } +} diff --git a/test/hotspot/jtreg/runtime/cds/appcds/dynamicArchive/test-classes/VerifierTroubleApp.java b/test/hotspot/jtreg/runtime/cds/appcds/dynamicArchive/test-classes/VerifierTroubleApp.java new file mode 100644 index 00000000000..7cc560fc733 --- /dev/null +++ b/test/hotspot/jtreg/runtime/cds/appcds/dynamicArchive/test-classes/VerifierTroubleApp.java @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023, 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. + * + */ +public class VerifierTroubleApp { + public static void main(String args[]) throws Throwable { + ChildOldSuper c = new ChildOldSuper(); + System.out.println(c); + } +} diff --git a/test/hotspot/jtreg/runtime/cds/appcds/dynamicArchive/test-classes/VerifierTroublev49.jasm b/test/hotspot/jtreg/runtime/cds/appcds/dynamicArchive/test-classes/VerifierTroublev49.jasm new file mode 100644 index 00000000000..37ad9c54c28 --- /dev/null +++ b/test/hotspot/jtreg/runtime/cds/appcds/dynamicArchive/test-classes/VerifierTroublev49.jasm @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. + * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. + */ + +// During CDS static dump time, this class will be stored in "unlinked" state. +// This class should not be stored in a dynamic archive because it will fail verification. + +super public class VerifierTroublev49 + version 49:0 +{ + + +public Method "":"()V" + stack 1 locals 1 +{ + aload_0; + invokespecial Method java/lang/Object."":"()V"; + return; +} + + /* + * The following method tries to return an Object as a String. + * Verifier should fail. + */ +public Method doit:"()Ljava/lang/String;" + stack 2 locals 1 +{ + new class java/lang/Object; + dup; + invokespecial Method java/lang/Object."":"()V"; + astore_0; + aload_0; + areturn; // tries to return an Object as a String +} + +} // end Class VerifierTroublev49