1 /*
   2  * Copyright (c) 2015, 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 // NOTE: the test takes a long time for each VM option combination, so we split
  26 // it into 3 @test parts, so that they can be executed in parallel. If you make a
  27 // change, please ensure all @test blocks are in sync.
  28 
  29 
  30 /*
  31  * @test
  32  * @summary Test options that are incompatible with use of shared strings
  33  *          Also test mismatch in oops encoding between dump time and run time
  34  * @requires vm.cds.archived.java.heap
  35  * @comment This test explicitly chooses the type of GC to be used by sub-processes. It may conflict with the GC type set
  36  * via the -vmoptions command line option of JTREG. vm.gc==null will help the test case to discard the explicitly passed
  37  * vm options.
  38  * @requires (vm.gc=="null")
  39  * @library /test/lib /test/hotspot/jtreg/runtime/cds
  40  * @modules jdk.jartool/sun.tools.jar
  41  * @build sun.hotspot.WhiteBox
  42  * @run driver ClassFileInstaller sun.hotspot.WhiteBox sun.hotspot.WhiteBox$WhiteBoxPermission
  43  * @build HelloString
  44  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. IncompatibleOptions 0
  45  */
  46 
  47 
  48 /*
  49  * @test
  50  * @requires vm.cds.archived.java.heap
  51  * @requires (vm.gc=="null")
  52  * @library /test/lib /test/hotspot/jtreg/runtime/cds
  53  * @modules jdk.jartool/sun.tools.jar
  54  * @build sun.hotspot.WhiteBox
  55  * @run driver ClassFileInstaller sun.hotspot.WhiteBox sun.hotspot.WhiteBox$WhiteBoxPermission
  56  * @build HelloString
  57  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. IncompatibleOptions 1
  58  */
  59 
  60 /*
  61  * @test
  62  * @requires vm.cds.archived.java.heap
  63  * @requires (vm.gc=="null")
  64  * @library /test/lib /test/hotspot/jtreg/runtime/cds
  65  * @modules jdk.jartool/sun.tools.jar
  66  * @build sun.hotspot.WhiteBox
  67  * @run driver ClassFileInstaller sun.hotspot.WhiteBox sun.hotspot.WhiteBox$WhiteBoxPermission
  68  * @build HelloString
  69  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. IncompatibleOptions 2
  70  */
  71 
  72 
  73 import jdk.test.lib.Asserts;
  74 import jdk.test.lib.Platform;
  75 import jdk.test.lib.process.OutputAnalyzer;
  76 
  77 import sun.hotspot.code.Compiler;
  78 import sun.hotspot.gc.GC;
  79 
  80 public class IncompatibleOptions {
  81     static final String COOPS_DUMP_WARNING =
  82         "Cannot dump shared archive when UseCompressedOops or UseCompressedClassPointers is off";
  83     static final String COOPS_EXEC_WARNING =
  84         "UseCompressedOops and UseCompressedClassPointers must be on for UseSharedSpaces";
  85     static final String GC_WARNING =
  86         "Archived java heap is not supported";
  87     static final String OBJ_ALIGNMENT_MISMATCH =
  88         "The shared archive file's ObjectAlignmentInBytes of .* does not equal the current ObjectAlignmentInBytes of";
  89     static final String COMPACT_STRING_MISMATCH =
  90         "The shared archive file's CompactStrings setting .* does not equal the current CompactStrings setting";
  91 
  92     static String appJar;
  93     static String[] vmOptionsPrefix = {};
  94 
  95     public static void main(String[] args) throws Exception {
  96         String[] noargs = {};
  97         SharedStringsUtils.run(Integer.parseInt(args[0]), 3, noargs, IncompatibleOptions::test);
  98         // Add a new @test block if you get an assert ----^ about this number. See
  99         // SharedStringsUtils.java for details.
 100     }
 101 
 102     public static void test(String[] args_ignored) throws Exception {
 103         vmOptionsPrefix = SharedStringsUtils.getChildVMOptionsPrefix();
 104         appJar = JarBuilder.build("IncompatibleOptions", "HelloString");
 105 
 106         // Uncompressed OOPs
 107         testDump(1, "-XX:+UseG1GC", "-XX:-UseCompressedOops", COOPS_DUMP_WARNING, true);
 108         if (GC.Z.isSupported()) { // ZGC is included in build.
 109             testDump(1, "-XX:+UnlockExperimentalVMOptions", "-XX:+UseZGC", COOPS_DUMP_WARNING, true);
 110         }
 111 
 112         // incompatible GCs
 113         testDump(2, "-XX:+UseParallelGC", "", GC_WARNING, false);
 114         testDump(3, "-XX:+UseSerialGC", "", GC_WARNING, false);
 115         if (!Compiler.isGraalEnabled()) { // Graal does not support CMS
 116             testDump(4, "-XX:+UseConcMarkSweepGC", "", GC_WARNING, false);
 117         }
 118 
 119         // ======= archive with compressed oops, run w/o
 120         testDump(5, "-XX:+UseG1GC", "-XX:+UseCompressedOops", null, false);
 121         testExec(5, "-XX:+UseG1GC", "-XX:-UseCompressedOops",
 122                  COOPS_EXEC_WARNING, true);
 123 
 124         // NOTE: No warning is displayed, by design
 125         // Still run, to ensure no crash or exception
 126         testExec(6, "-XX:+UseParallelGC", "", "", false);
 127         testExec(7, "-XX:+UseSerialGC", "", "", false);
 128         if (!Compiler.isGraalEnabled()) { // Graal does not support CMS
 129             testExec(8, "-XX:+UseConcMarkSweepGC", "", "", false);
 130         }
 131 
 132         // Test various oops encodings, by varying ObjectAlignmentInBytes and heap sizes
 133         testDump(9, "-XX:+UseG1GC", "-XX:ObjectAlignmentInBytes=8", null, false);
 134         testExec(9, "-XX:+UseG1GC", "-XX:ObjectAlignmentInBytes=16",
 135                  OBJ_ALIGNMENT_MISMATCH, true);
 136 
 137         // See JDK-8081416 - Oops encoding mismatch with shared strings
 138         // produces unclear or incorrect warning
 139         // Correct the test case once the above is fixed
 140         // @ignore JDK-8081416 - for tracking purposes
 141         // for now, run test as is until the proper behavior is determined
 142         testDump(10, "-XX:+UseG1GC", "-Xmx1g", null, false);
 143         testExec(10, "-XX:+UseG1GC", "-Xmx32g", null, true);
 144 
 145         // CompactStrings must match between dump time and run time
 146         testDump(11, "-XX:+UseG1GC", "-XX:-CompactStrings", null, false);
 147         testExec(11, "-XX:+UseG1GC", "-XX:+CompactStrings",
 148                  COMPACT_STRING_MISMATCH, true);
 149         testDump(12, "-XX:+UseG1GC", "-XX:+CompactStrings", null, false);
 150         testExec(12, "-XX:+UseG1GC", "-XX:-CompactStrings",
 151                  COMPACT_STRING_MISMATCH, true);
 152     }
 153 
 154     static void testDump(int testCaseNr, String collectorOption, String extraOption,
 155         String expectedWarning, boolean expectedToFail) throws Exception {
 156 
 157         System.out.println("Testcase: " + testCaseNr);
 158         OutputAnalyzer output = TestCommon.dump(appJar, TestCommon.list("Hello"),
 159             TestCommon.concat(vmOptionsPrefix,
 160                 "-XX:+UseCompressedOops",
 161                 collectorOption,
 162                 "-XX:SharedArchiveConfigFile=" + TestCommon.getSourceFile("SharedStringsBasic.txt"),
 163                 "-Xlog:cds,cds+hashtables",
 164                 extraOption));
 165 
 166         if (expectedWarning != null) {
 167             output.shouldContain(expectedWarning);
 168         }
 169 
 170         if (expectedToFail) {
 171             Asserts.assertNE(output.getExitValue(), 0,
 172             "JVM is expected to fail, but did not");
 173         }
 174     }
 175 
 176     static void testExec(int testCaseNr, String collectorOption, String extraOption,
 177         String expectedWarning, boolean expectedToFail) throws Exception {
 178 
 179         OutputAnalyzer output;
 180         System.out.println("Testcase: " + testCaseNr);
 181 
 182         // needed, otherwise system considers empty extra option as a
 183         // main class param, and fails with "Could not find or load main class"
 184         if (!extraOption.isEmpty()) {
 185             output = TestCommon.exec(appJar,
 186                 TestCommon.concat(vmOptionsPrefix,
 187                     "-XX:+UseCompressedOops",
 188                     collectorOption, "-Xlog:cds", extraOption, "HelloString"));
 189         } else {
 190             output = TestCommon.exec(appJar,
 191                 TestCommon.concat(vmOptionsPrefix,
 192                     "-XX:+UseCompressedOops",
 193                     collectorOption, "-Xlog:cds", "HelloString"));
 194         }
 195 
 196         if (expectedWarning != null) {
 197             output.shouldMatch(expectedWarning);
 198         }
 199 
 200         if (expectedToFail) {
 201             Asserts.assertNE(output.getExitValue(), 0);
 202         } else {
 203             SharedStringsUtils.checkExec(output);
 204         }
 205     }
 206 }