1 /*
   2  * Copyright (c) 2019, 2020, 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  * @summary A few edge cases where there's no class to be included in the dynamic archive.
  28  * @requires vm.cds & !vm.graal.enabled
  29  * @comment The test assumes that when "java -version" is executed, only a very limited number
  30  *          of classes are loaded, and all of those are loaded from the default shared archive.
  31  *
  32  *          However, when graal is used as the JIT, many extra classes are loaded during VM start-up.
  33  *          Some of those are loaded dynamically from jrt:/. Some classes are also defined by
  34  *          LambdaMetafactory. This causes complexity that cannot be easily handled by this test.
  35  *
  36  *          The VM code covered by this test can be sufficiently tested with C1/C2. So there's no need
  37  *          to bend over backwards to run this test with graal.
  38  *
  39  * @library /test/lib /test/hotspot/jtreg/runtime/cds/appcds /test/hotspot/jtreg/runtime/cds/appcds/dynamicArchive/test-classes
  40  * @build StrConcatApp
  41  * @build sun.hotspot.WhiteBox
  42  * @run driver ClassFileInstaller -jar strConcatApp.jar StrConcatApp
  43  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  44  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. NoClassToArchive
  45  */
  46 
  47 import java.io.File;
  48 import jdk.test.lib.process.OutputAnalyzer;
  49 import jdk.test.lib.process.ProcessTools;
  50 
  51 public class NoClassToArchive extends DynamicArchiveTestBase {
  52     static final String warningMessage =
  53         "There is no class to be included in the dynamic archive";
  54     static final String classList = System.getProperty("test.classes") +
  55         File.separator + "NoClassToArchive.list";
  56     static final String appClass = "StrConcatApp";
  57 
  58     public static void main(String[] args) throws Exception {
  59         runTest(NoClassToArchive::testDefaultBase);
  60         runTest(NoClassToArchive::testCustomBase);
  61     }
  62 
  63     // (1) Test with default base archive + top archive
  64     static void testDefaultBase() throws Exception {
  65         String topArchiveName = getNewArchiveName("top");
  66         doTest(topArchiveName);
  67     }
  68 
  69     // (2) Test with custom base archive + top archive
  70     static void testCustomBase() throws Exception {
  71         String topArchiveName = getNewArchiveName("top2");
  72         String baseArchiveName = getNewArchiveName("base");
  73         doTestCustomBase(baseArchiveName, topArchiveName);
  74     }
  75 
  76     private static void checkWarning(OutputAnalyzer output) throws Exception {
  77         if (output.firstMatch("bytes: [0-9]+ checksum: [0-9a-f]+") != null) {
  78             // Patterns like this indicate that a class was not loaded from CDS archive:
  79             // [info ][class,load] jdk.internal.module.DefaultRoots$$Lambda$1/0x00007f80c4512048 source: jdk.internal.module.DefaultRoots
  80             // [debug][class,load]  klass: 0x0000000800b77cf8 super: 0x0000000800007450 interfaces: 0x0000000800162538
  81             //                      loader: [loader data: 0x00007f80f416a5b0 of 'bootstrap'] bytes: 403 checksum: 753e58aa
  82             System.out.println("test skipped: this platform uses non-archived classes when running -version");
  83         } else {
  84             output.shouldContain(warningMessage);
  85         }
  86     }
  87 
  88     private static void doTest(String topArchiveName) throws Exception {
  89         dump(topArchiveName,
  90              "-Xlog:cds",
  91              "-Xlog:cds+dynamic=debug",
  92              "-Xlog:class+load=trace",
  93              "-version")
  94             .assertNormalExit(output -> checkWarning(output));
  95 
  96         dump(topArchiveName,
  97              "-Xlog:cds",
  98              "-Xlog:cds+dynamic=debug",
  99              "-Xlog:class+load=trace",
 100              "-help")
 101             .assertNormalExit(output -> {
 102                     // some classes will be loaded from the java.base module
 103                     output.shouldContain("java.text.MessageFormat source: jrt:/java.base");
 104                 });
 105     }
 106 
 107     private static void doTestCustomBase(String baseArchiveName, String topArchiveName) throws Exception {
 108         String appJar = ClassFileInstaller.getJarPath("strConcatApp.jar");
 109         // dump class list by running the StrConcatApp
 110         ProcessBuilder pb = ProcessTools.createTestJvm(
 111             "-XX:DumpLoadedClassList=" + classList,
 112             "-cp",
 113             appJar,
 114             appClass);
 115         OutputAnalyzer output = TestCommon.executeAndLog(pb, "dumpClassList");
 116         TestCommon.checkExecReturn(output, 0, true, "length = 0");
 117 
 118         // create a custom base archive based on the class list
 119         TestCommon.dumpBaseArchive(baseArchiveName, "-XX:SharedClassListFile=" + classList);
 120 
 121         // create a dynamic archive with the custom base archive
 122         // no class should be included in the dynamic archive
 123         dump2(baseArchiveName, topArchiveName,
 124               "-Xlog:cds",
 125               "-Xlog:cds+dynamic=debug",
 126               "-Xlog:class+load=trace",
 127               "-version")
 128             .assertNormalExit(out -> checkWarning(out));
 129     }
 130 }