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