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