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 driver ClassFileInstaller -jar test.jar UseAppCDS_Test
  36  * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  37  *      -XX:+TieredCompilation -XX:+UseJVMCICompiler -Djvmci.Compiler=graal
  38  *      GraalWithLimitedMetaspace
  39  */
  40 
  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 CLASSLIST_FILE = "./GraalWithLimitedMetaspace.classlist";
  60     private static final String ARCHIVE_FILE = "./GraalWithLimitedMetaspace.jsa";
  61     private static final String BOOTCLASS = "java.lang.Class";
  62 
  63     public static void main(String[] args) throws Exception {
  64 
  65         // dump loaded classes into a classlist file
  66         dumpLoadedClasses(new String[] { BOOTCLASS, TESTNAME });
  67 
  68 
  69         // create an archive using the classlist
  70         dumpArchive();
  71 
  72     }
  73 
  74     public static List<String> toClassNames(String filename) throws IOException {
  75         ArrayList<String> classes = new ArrayList<>();
  76         try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filename)))) {
  77             for (; ; ) {
  78                 String line = br.readLine();
  79                 if (line == null) {
  80                     break;
  81                 }
  82                 classes.add(line.replaceAll("/", "."));
  83             }
  84         }
  85         return classes;
  86     }
  87 
  88     static void dumpLoadedClasses(String[] expectedClasses) throws Exception {
  89         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true,
  90           TestCommon.makeCommandLineForAppCDS(
  91             "-XX:DumpLoadedClassList=" + CLASSLIST_FILE,
  92             // trigger JVMCI runtime init so that JVMCI classes will be
  93             // included in the classlist
  94             "-XX:+EagerJVMCI",
  95             "-cp",
  96             TESTJAR,
  97             "-XX:+UseAppCDS",
  98             TESTNAME,
  99             TEST_OUT));
 100 
 101         OutputAnalyzer output = TestCommon.executeAndLog(pb, "dump-loaded-classes")
 102             .shouldHaveExitValue(0)
 103             .shouldContain(TEST_OUT);
 104 
 105         List<String> dumpedClasses = toClassNames(CLASSLIST_FILE);
 106 
 107         for (String clazz : expectedClasses) {
 108             if (!dumpedClasses.contains(clazz)) {
 109                 throw new RuntimeException(clazz + " missing in " +
 110                                            CLASSLIST_FILE);
 111             }
 112         }
 113     }
 114 
 115     static void dumpArchive() throws Exception {
 116         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true,
 117           TestCommon.makeCommandLineForAppCDS(
 118             "-cp",
 119             TESTJAR,
 120             "-XX:+UseAppCDS",
 121             "-XX:SharedClassListFile=" + CLASSLIST_FILE,
 122             "-XX:SharedArchiveFile=" + ARCHIVE_FILE,
 123             "-Xlog:cds",
 124             "-Xshare:dump",
 125             "-XX:MetaspaceSize=12M",
 126             "-XX:MaxMetaspaceSize=12M"));
 127 
 128         OutputAnalyzer output = TestCommon.executeAndLog(pb, "dump-archive");
 129         int exitValue = output.getExitValue();
 130         if (exitValue == 1) {
 131             output.shouldContain("Failed allocating metaspace object type");
 132         } else if (exitValue == 0) {
 133             output.shouldContain("Loading classes to share");
 134         } else {
 135             throw new RuntimeException("Unexpected exit value " + exitValue);
 136         }
 137     }
 138 }