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