1 /*
 2  * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
 3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  *
 5  * This code is free software; you can redistribute it and/or modify it
 6  * under the terms of the GNU General Public License version 2 only, as
 7  * published by the Free Software Foundation.
 8  *
 9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  *
23  */
24 
25 /*
26  * @test
27  * @bug 8312181
28  * @summary Archive an old class, which fails verification and is a supertype of another
29  *          class, in the base archive. Perform a dynamic dump with the base archive
30  *          containing the old class and its subclass in "unlinked" state. VM should
31  *          not crash in this scenario.
32  * @requires vm.cds
33  * @library /test/lib /test/hotspot/jtreg/runtime/cds/appcds/test-classes /test/hotspot/jtreg/runtime/cds/appcds
34  * @compile test-classes/VerifierTroublev49.jasm
35  * @compile test-classes/ChildOldSuper.java
36  * @compile test-classes/VerifierTroubleApp.java
37  * @build jdk.test.whitebox.WhiteBox
38  * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar oldsuper-fail-verifier.jar ChildOldSuper VerifierTroublev49 VerifierTroubleApp
39  * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
40  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. OldClassVerifierTrouble
41  */
42 
43 import java.io.File;
44 import jdk.test.lib.cds.CDSOptions;
45 import jdk.test.lib.cds.CDSTestUtils;
46 import jdk.test.lib.process.OutputAnalyzer;
47 import jdk.test.lib.helpers.ClassFileInstaller;
48 
49 public class OldClassVerifierTrouble extends DynamicArchiveTestBase {
50     static final String appClass = "VerifierTroubleApp";
51     static final String baseArchiveClass = "OldSuperVerifierTrouble";
52 
53     public static void main(String[] args) throws Exception {
54         runTest(OldClassVerifierTrouble::testCustomBase);
55     }
56 
57     static void testCustomBase() throws Exception {
58         String topArchiveName = getNewArchiveName("top");
59         doTestCustomBase(topArchiveName);
60     }
61 
62     private static void doTestCustomBase(String topArchiveName) throws Exception {
63         String appJar = ClassFileInstaller.getJarPath("oldsuper-fail-verifier.jar");
64 
65         // create a custom base archive containing an old class
66         OutputAnalyzer output = TestCommon.dump(appJar,
67             TestCommon.list("VerifierTroubleApp", "VerifierTroublev49", "ChildOldSuper"),
68             "-Xlog:class+load,cds+class=debug",
69             "-XX:-PreloadSharedClasses");
70         TestCommon.checkDump(output);
71         // Check the ChildOldSuper and VerifierTroublev49 are being dumped into the base archive.
72         output.shouldMatch(".cds.class.*klass.*0x.*app.*ChildOldSuper.*unlinked")
73               .shouldMatch(".cds.class.*klass.*0x.*app.*VerifierTroublev49.*unlinked");
74 
75         String baseArchiveName = TestCommon.getCurrentArchiveName();
76 
77         // create a dynamic archive with the custom base archive.
78         // The old class is in the base archive and will be
79         // accessed from VerifierTroubleApp.
80         // Linking VerifierTroublev49 would result in java.lang.VerifyError.
81         dump2(baseArchiveName, topArchiveName,
82               "-Xlog:cds,cds+dynamic,class+load,cds+class=debug",
83               "-XX:-PreloadSharedClasses",
84               "-cp", appJar,
85               appClass)
86             .assertAbnormalExit(out -> {
87                     out.shouldContain("VerifierTroublev49 source: shared objects file")
88                        .shouldContain("ChildOldSuper source: shared objects file")
89                        .shouldContain("java.lang.VerifyError: " +
90                                       "(class: VerifierTroublev49, method: doit signature: ()Ljava/lang/String;)" +
91                                       " Wrong return type in function");
92                 });
93     }
94 }