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