1 /*
   2  * Copyright (c) 2015, 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 // 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/appcds
  40  * @build sun.hotspot.WhiteBox
  41  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  42  * @build HelloString
  43  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. IncompatibleOptions 0
  44  */
  45 
  46 
  47 /*
  48  * @test
  49  * @requires vm.cds.archived.java.heap
  50  * @requires (vm.gc=="null")
  51  * @library /test/lib /test/hotspot/jtreg/runtime/cds/appcds
  52  * @build sun.hotspot.WhiteBox
  53  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  54  * @build HelloString
  55  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. IncompatibleOptions 1
  56  */
  57 
  58 /*
  59  * @test
  60  * @requires vm.cds.archived.java.heap
  61  * @requires (vm.gc=="null")
  62  * @library /test/lib /test/hotspot/jtreg/runtime/cds/appcds
  63  * @build sun.hotspot.WhiteBox
  64  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  65  * @build HelloString
  66  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. IncompatibleOptions 2
  67  */
  68 
  69 
  70 import jdk.test.lib.Asserts;
  71 import jdk.test.lib.Platform;
  72 import jdk.test.lib.process.OutputAnalyzer;
  73 
  74 import sun.hotspot.code.Compiler;
  75 import sun.hotspot.gc.GC;
  76 
  77 public class IncompatibleOptions {
  78     static final String COOPS_DUMP_WARNING =
  79         "Cannot dump shared archive when UseCompressedOops or UseCompressedClassPointers is off";
  80     static final String GC_WARNING =
  81         "Archived java heap is not supported";
  82     static final String OBJ_ALIGNMENT_MISMATCH =
  83         "The shared archive file's ObjectAlignmentInBytes of .* does not equal the current ObjectAlignmentInBytes of";
  84     static final String COMPACT_STRING_MISMATCH =
  85         "The shared archive file's CompactStrings setting .* does not equal the current CompactStrings setting";
  86     static final String COMPRESSED_OOPS_NOT_CONSISTENT =
  87         "The saved state of UseCompressedOops and UseCompressedClassPointers is different from runtime, CDS will be disabled.";
  88     static String appJar;
  89     static String[] vmOptionsPrefix = {};
  90 
  91     public static void main(String[] args) throws Exception {
  92         String[] noargs = {};
  93         SharedStringsUtils.run(Integer.parseInt(args[0]), 3, noargs, IncompatibleOptions::test);
  94         // Add a new @test block if you get an assert ----^ about this number. See
  95         // SharedStringsUtils.java for details.
  96     }
  97 
  98     public static void test(String[] args_ignored) throws Exception {
  99         vmOptionsPrefix = SharedStringsUtils.getChildVMOptionsPrefix();
 100         appJar = JarBuilder.build("IncompatibleOptions", "HelloString");
 101 
 102         // Uncompressed OOPs
 103         testDump(1, "-XX:+UseG1GC", "-XX:-UseCompressedOops", null, false);
 104         if (GC.Z.isSupported()) {
 105             testDump(1, "-XX:+UseZGC", "-XX:-UseCompressedOops", null, false);
 106         }
 107 
 108         // incompatible GCs
 109         testDump(2, "-XX:+UseParallelGC", "", GC_WARNING, false);
 110         testDump(3, "-XX:+UseSerialGC", "", GC_WARNING, false);
 111 
 112         // ======= archive with compressed oops, run w/o
 113         testDump(5, "-XX:+UseG1GC", "-XX:+UseCompressedOops", null, false);
 114         testExec(5, "-XX:+UseG1GC", "-XX:-UseCompressedOops",
 115                  COMPRESSED_OOPS_NOT_CONSISTENT, true);
 116 
 117         // NOTE: No warning is displayed, by design
 118         // Still run, to ensure no crash or exception
 119         testExec(6, "-XX:+UseParallelGC", "", "", false);
 120         testExec(7, "-XX:+UseSerialGC", "", "", false);
 121 
 122         // Test various oops encodings, by varying ObjectAlignmentInBytes and heap sizes
 123         testDump(9, "-XX:+UseG1GC", "-XX:ObjectAlignmentInBytes=8", null, false);
 124         testExec(9, "-XX:+UseG1GC", "-XX:ObjectAlignmentInBytes=16",
 125                  OBJ_ALIGNMENT_MISMATCH, true);
 126 
 127         // Max heap size for compressed oops is around 31G. UseCompressedOops is turned on
 128         // by default when heap size is under 31G, but will be turned off when heap size
 129         // is greater than that. This test leads to inconsistency of UseCompressedOops at dump
 130         // time and runtime.
 131         testDump(10, "-XX:+UseG1GC", "-Xmx1g", null, false);
 132         testExec(10, "-XX:+UseG1GC", "-Xmx32g", null, true);
 133 
 134         // CompactStrings must match between dump time and run time
 135         testDump(11, "-XX:+UseG1GC", "-XX:-CompactStrings", null, false);
 136         testExec(11, "-XX:+UseG1GC", "-XX:+CompactStrings",
 137                  COMPACT_STRING_MISMATCH, true);
 138         testDump(12, "-XX:+UseG1GC", "-XX:+CompactStrings", null, false);
 139         testExec(12, "-XX:+UseG1GC", "-XX:-CompactStrings",
 140                  COMPACT_STRING_MISMATCH, true);
 141     }
 142 
 143     static void testDump(int testCaseNr, String collectorOption, String extraOption,
 144         String expectedWarning, boolean expectedToFail) throws Exception {
 145 
 146         System.out.println("Testcase: " + testCaseNr);
 147         OutputAnalyzer output = TestCommon.dump(appJar, TestCommon.list("Hello"),
 148             TestCommon.concat(vmOptionsPrefix,
 149                 "-XX:+UseCompressedOops",
 150                 collectorOption,
 151                 "-XX:SharedArchiveConfigFile=" + TestCommon.getSourceFile("SharedStringsBasic.txt"),
 152                 "-Xlog:cds,cds+hashtables",
 153                 extraOption));
 154 
 155         if (expectedWarning != null) {
 156             output.shouldContain(expectedWarning);
 157         }
 158 
 159         if (expectedToFail) {
 160             Asserts.assertNE(output.getExitValue(), 0,
 161             "JVM is expected to fail, but did not");
 162         }
 163     }
 164 
 165     static void testExec(int testCaseNr, String collectorOption, String extraOption,
 166         String expectedWarning, boolean expectedToFail) throws Exception {
 167 
 168         OutputAnalyzer output;
 169         System.out.println("Testcase: " + testCaseNr);
 170 
 171         // needed, otherwise system considers empty extra option as a
 172         // main class param, and fails with "Could not find or load main class"
 173         if (!extraOption.isEmpty()) {
 174             output = TestCommon.exec(appJar,
 175                 TestCommon.concat(vmOptionsPrefix,
 176                     "-XX:+UseCompressedOops",
 177                     collectorOption, "-Xlog:cds", extraOption, "HelloString"));
 178         } else {
 179             output = TestCommon.exec(appJar,
 180                 TestCommon.concat(vmOptionsPrefix,
 181                     "-XX:+UseCompressedOops",
 182                     collectorOption, "-Xlog:cds", "HelloString"));
 183         }
 184 
 185         if (expectedWarning != null) {
 186             output.shouldMatch(expectedWarning);
 187         }
 188 
 189         if (expectedToFail) {
 190             Asserts.assertNE(output.getExitValue(), 0);
 191         } else {
 192             SharedStringsUtils.checkExec(output);
 193         }
 194     }
 195 }