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