1 /*
   2  * Copyright (c) 2014, 2017, 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 Testing use of UseAppCDS flag
  28  * AppCDS does not support uncompressed oops
  29  * @requires (vm.opt.UseCompressedOops == null) | (vm.opt.UseCompressedOops == true)
  30  * @library /test/lib
  31  * @modules java.base/jdk.internal.misc
  32  *          java.management
  33  *          jdk.jartool/sun.tools.jar
  34  * @build UseAppCDS_Test
  35  * @run main UseAppCDS
  36  */
  37 
  38 import jdk.test.lib.JDKToolLauncher;
  39 import jdk.test.lib.cds.CDSTestUtils;
  40 import jdk.test.lib.process.OutputAnalyzer;
  41 import jdk.test.lib.process.ProcessTools;
  42 
  43 import java.util.ArrayList;
  44 import java.util.List;
  45 import java.io.*;
  46 
  47 public class UseAppCDS {
  48 
  49     // Class UseAppCDS_Test is loaded by the App loader
  50 
  51     static final String TEST_OUT = "UseAppCDS_Test.main--executed";
  52 
  53     private static final String TESTJAR = "./test.jar";
  54     private static final String TESTNAME = "UseAppCDS_Test";
  55     private static final String TESTCLASS = TESTNAME + ".class";
  56 
  57     private static final String CLASSES_DIR = System.getProperty("test.classes", ".");
  58     private static final String CLASSLIST_FILE = "./UseAppCDS.classlist";
  59     private static final String ARCHIVE_FILE = "./shared.jsa";
  60     private static final String BOOTCLASS = "java.lang.Class";
  61 
  62     public static void main(String[] args) throws Exception {
  63 
  64         // First create a jar file for the application "test" class
  65         JDKToolLauncher jar = JDKToolLauncher.create("jar")
  66             .addToolArg("-cf")
  67             .addToolArg(TESTJAR)
  68             .addToolArg("-C")
  69             .addToolArg(CLASSES_DIR)
  70             .addToolArg(TESTCLASS);
  71 
  72         ProcessBuilder pb = new ProcessBuilder(jar.getCommand());
  73         TestCommon.executeAndLog(pb, "jar01").shouldHaveExitValue(0);
  74 
  75         pb = new ProcessBuilder(jar.getCommand());
  76         TestCommon.executeAndLog(pb, "jar02").shouldHaveExitValue(0);
  77 
  78         // In all tests the BOOTCLASS should be loaded/dumped/used
  79 
  80         // Test 1: No AppCDS - dumping loaded classes excludes the "test" classes
  81         dumpLoadedClasses(false, new String[] { BOOTCLASS },
  82                           new String[] { TESTNAME });
  83 
  84         // Test 2:    AppCDS - dumping loaded classes includes "test" classes
  85         dumpLoadedClasses(true, new String[] { BOOTCLASS, TESTNAME },
  86                           new String[0]);
  87 
  88         // Next tests rely on the classlist we just dumped
  89 
  90         // Test 3: No AppCDS - "test" classes in classlist ignored when dumping
  91         dumpArchive(false, new String[] { BOOTCLASS },
  92                     new String[] { TESTNAME});
  93 
  94         // Test 4:    AppCDS - "test" classes in classlist are dumped
  95         dumpArchive(true, new String[] { BOOTCLASS, TESTNAME },
  96                     new String[0]);
  97 
  98         // Next tests rely on the archive we just dumped
  99 
 100         // Test 5: No AppCDS - Using archive containing "test" classes ignores them
 101         useArchive(false, new String[] { BOOTCLASS },
 102                    new String[] { TESTNAME });
 103 
 104         // Test 6:    AppCDS - Using archive containing "test" classes loads them
 105         useArchive(true, new String[] { BOOTCLASS, TESTNAME },
 106                    new String[0]);
 107     }
 108 
 109     public static List<String> toClassNames(String filename) throws IOException {
 110         ArrayList<String> classes = new ArrayList<>();
 111         BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
 112         for (; ; ) {
 113             String line = br.readLine();
 114             if (line == null)
 115                 break;
 116             classes.add(line.replaceAll("/", "."));
 117         }
 118         return classes;
 119     }
 120 
 121     static void dumpLoadedClasses(boolean useAppCDS, String[] expectedClasses,
 122                                   String[] unexpectedClasses) throws Exception {
 123         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
 124             true,
 125             "-XX:DumpLoadedClassList=" + CLASSLIST_FILE,
 126             "-cp",
 127             TESTJAR,
 128             useAppCDS ? "-XX:+UseAppCDS" : "-XX:-UseAppCDS",
 129             TESTNAME,
 130             TEST_OUT);
 131 
 132         OutputAnalyzer output = TestCommon.executeAndLog(pb, "dump-loaded-classes")
 133             .shouldHaveExitValue(0).shouldContain(TEST_OUT);
 134 
 135         List<String> dumpedClasses = toClassNames(CLASSLIST_FILE);
 136 
 137         for (String clazz : expectedClasses) {
 138             if (!dumpedClasses.contains(clazz)) {
 139                 throw new RuntimeException(clazz + " missing in " +
 140                                            CLASSLIST_FILE);
 141             }
 142         }
 143         for (String clazz : unexpectedClasses) {
 144             if (dumpedClasses.contains(clazz)) {
 145                 throw new RuntimeException("Unexpectedly found " + clazz +
 146                                            " in " + CLASSLIST_FILE);
 147             }
 148         }
 149     }
 150 
 151     static void dumpArchive(boolean useAppCDS, String[] expectedClasses,
 152                             String[] unexpectedClasses) throws Exception {
 153         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
 154             true,
 155             useAppCDS ? "-XX:-UnlockDiagnosticVMOptions" :
 156                         "-XX:+UnlockDiagnosticVMOptions",
 157             "-cp",
 158             TESTJAR,
 159             useAppCDS ? "-XX:+UseAppCDS" : "-XX:-UseAppCDS",
 160             "-XX:SharedClassListFile=" + CLASSLIST_FILE,
 161             "-XX:SharedArchiveFile=" + ARCHIVE_FILE,
 162             "-Xlog:cds",
 163             "-Xshare:dump");
 164 
 165         OutputAnalyzer output = TestCommon.executeAndLog(pb, "dump-archive")
 166             .shouldHaveExitValue(0);
 167 
 168         for (String clazz : expectedClasses) {
 169             String failed = "Preload Warning: Cannot find " + clazz;
 170             output.shouldNotContain(failed);
 171         }
 172         for (String clazz : unexpectedClasses) {
 173             String failed = "Preload Warning: Cannot find " + clazz;
 174             output.shouldContain(failed);
 175         }
 176     }
 177 
 178     static void useArchive(boolean useAppCDS, String[] expectedClasses,
 179                            String[] unexpectedClasses) throws Exception {
 180         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
 181             true,
 182             useAppCDS ? "-XX:-UnlockDiagnosticVMOptions" :
 183                         "-XX:+UnlockDiagnosticVMOptions",
 184             "-cp",
 185             TESTJAR,
 186             useAppCDS ? "-XX:+UseAppCDS" : "-XX:-UseAppCDS",
 187             "-XX:SharedArchiveFile=" + ARCHIVE_FILE,
 188             "-verbose:class",
 189             "-Xshare:on",
 190             TESTNAME,
 191             TEST_OUT );
 192 
 193         OutputAnalyzer output = TestCommon.executeAndLog(pb, "use-archive");
 194         if (CDSTestUtils.isUnableToMap(output))
 195             System.out.println("Unable to map: test case skipped");
 196         else
 197             output.shouldHaveExitValue(0).shouldContain(TEST_OUT);
 198 
 199         // Quote the class name in the regex as it may contain $
 200         String prefix = ".class,load. ";
 201         String archive_suffix = ".*source: shared objects file.*";
 202         String jar_suffix = ".*source: .*\\.jar";
 203 
 204         for (String clazz : expectedClasses) {
 205             String pattern = prefix + clazz + archive_suffix;
 206             try {
 207                 output.shouldMatch(pattern);
 208             } catch (Exception e) {
 209                 TestCommon.checkCommonExecExceptions(output, e);
 210             }
 211         }
 212 
 213         for (String clazz : unexpectedClasses) {
 214             String pattern = prefix + clazz + archive_suffix;
 215             try {
 216                 output.shouldNotMatch(pattern);
 217             } catch (Exception e) {
 218                 TestCommon.checkCommonExecExceptions(output, e);
 219             }
 220             pattern = prefix + clazz + jar_suffix;
 221             try {
 222                 output.shouldMatch(pattern);
 223             } catch (Exception e) {
 224                 TestCommon.checkCommonExecExceptions(output, e);
 225             }
 226         }
 227     }
 228 }