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