1 /*
   2  * Copyright (c) 2018, 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 Test dumping with limited metaspace with loading of JVMCI related classes.
  28  *          VM should not crash but CDS dump will abort upon failure in allocating metaspace.
  29  * @requires vm.cds & vm.graal.enabled
  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/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  36  *      -XX:+TieredCompilation -XX:+UseJVMCICompiler -Djvmci.Compiler=graal
  37  *      GraalWithLimitedMetaspace
  38  */
  39 
  40 import jdk.test.lib.JDKToolLauncher;
  41 import jdk.test.lib.cds.CDSTestUtils;
  42 import jdk.test.lib.process.OutputAnalyzer;
  43 import jdk.test.lib.process.ProcessTools;
  44 
  45 import java.util.ArrayList;
  46 import java.util.List;
  47 import java.io.*;
  48 
  49 public class GraalWithLimitedMetaspace {
  50 
  51     // Class UseAppCDS_Test is loaded by the App loader
  52 
  53     static final String TEST_OUT = "UseAppCDS_Test.main--executed";
  54 
  55     private static final String TESTJAR = "./test.jar";
  56     private static final String TESTNAME = "UseAppCDS_Test";
  57     private static final String TESTCLASS = TESTNAME + ".class";
  58 
  59     private static final String CLASSES_DIR = System.getProperty("test.classes", ".");
  60     private static final String CLASSLIST_FILE = "./GraalWithLimitedMetaspace.classlist";
  61     private static final String ARCHIVE_FILE = "./GraalWithLimitedMetaspace.jsa";
  62     private static final String BOOTCLASS = "java.lang.Class";
  63 
  64     public static void main(String[] args) throws Exception {
  65 
  66         // First create a jar file for the application "test" class
  67         JDKToolLauncher jar = JDKToolLauncher.create("jar")
  68             .addToolArg("-cf")
  69             .addToolArg(TESTJAR)
  70             .addToolArg("-C")
  71             .addToolArg(CLASSES_DIR)
  72             .addToolArg(TESTCLASS);
  73 
  74         ProcessBuilder pb = new ProcessBuilder(jar.getCommand());
  75         TestCommon.executeAndLog(pb, "jar01").shouldHaveExitValue(0);
  76 
  77         // dump loaded classes into a classlist file
  78         dumpLoadedClasses(new String[] { BOOTCLASS, TESTNAME },
  79                           new String[0]);
  80 
  81 
  82         // create an archive using the classlist
  83         dumpArchive();
  84 
  85     }
  86 
  87     public static List<String> toClassNames(String filename) throws IOException {
  88         ArrayList<String> classes = new ArrayList<>();
  89         try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filename)))) {
  90             for (; ; ) {
  91                 String line = br.readLine();
  92                 if (line == null) {
  93                     break;
  94                 }
  95                 classes.add(line.replaceAll("/", "."));
  96             }
  97         }
  98         return classes;
  99     }
 100 
 101     static void dumpLoadedClasses(String[] expectedClasses,
 102                                   String[] unexpectedClasses) throws Exception {
 103         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true,
 104           TestCommon.makeCommandLineForAppCDS(
 105             "-XX:DumpLoadedClassList=" + CLASSLIST_FILE,
 106             // trigger JVMCI runtime init so that JVMCI classes will be
 107             // included in the classlist
 108             "-XX:+EagerJVMCI",
 109             "-cp",
 110             TESTJAR,
 111             "-XX:+UseAppCDS",
 112             TESTNAME,
 113             TEST_OUT));
 114 
 115         OutputAnalyzer output = TestCommon.executeAndLog(pb, "dump-loaded-classes")
 116             .shouldHaveExitValue(0)
 117             .shouldContain(TEST_OUT);
 118 
 119         List<String> dumpedClasses = toClassNames(CLASSLIST_FILE);
 120 
 121         for (String clazz : expectedClasses) {
 122             if (!dumpedClasses.contains(clazz)) {
 123                 throw new RuntimeException(clazz + " missing in " +
 124                                            CLASSLIST_FILE);
 125             }
 126         }
 127         for (String clazz : unexpectedClasses) {
 128             if (dumpedClasses.contains(clazz)) {
 129                 throw new RuntimeException("Unexpectedly found " + clazz +
 130                                            " in " + CLASSLIST_FILE);
 131             }
 132         }
 133     }
 134 
 135     static void dumpArchive() throws Exception {
 136         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true,
 137           TestCommon.makeCommandLineForAppCDS(
 138             "-cp",
 139             TESTJAR,
 140             "-XX:+UseAppCDS",
 141             "-XX:SharedClassListFile=" + CLASSLIST_FILE,
 142             "-XX:SharedArchiveFile=" + ARCHIVE_FILE,
 143             "-Xlog:cds",
 144             "-Xshare:dump",
 145             "-XX:MetaspaceSize=12M",
 146             "-XX:MaxMetaspaceSize=12M"));
 147 
 148         OutputAnalyzer output = TestCommon.executeAndLog(pb, "dump-archive")
 149             .shouldHaveExitValue(1)
 150             .shouldContain("Failed allocating metaspace object type");
 151     }
 152 
 153 }