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 import sun.hotspot.gc.GC; 47 48 public class IncompatibleOptions { 49 static final String COOPS_DUMP_WARNING = 50 "Cannot dump shared archive when UseCompressedOops or UseCompressedClassPointers is off"; 51 static final String COOPS_EXEC_WARNING = 52 "UseCompressedOops and UseCompressedClassPointers must be on for UseSharedSpaces"; 53 static final String GC_WARNING = 54 "Archived java heap is not supported"; 55 static final String OBJ_ALIGNMENT_MISMATCH = 56 "The shared archive file's ObjectAlignmentInBytes of .* does not equal the current ObjectAlignmentInBytes of"; 57 static final String COMPACT_STRING_MISMATCH = 58 "The shared archive file's CompactStrings setting .* does not equal the current CompactStrings setting"; 59 60 static String appJar; 61 62 public static void main(String[] args) throws Exception { 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 "-XX:+UseCompressedOops", 119 collectorOption, 120 "-XX:SharedArchiveConfigFile=" + TestCommon.getSourceFile("SharedStringsBasic.txt"), 121 extraOption); 122 123 if (expectedWarning != null) 124 output.shouldContain(expectedWarning); 125 126 if (expectedToFail) { 127 Asserts.assertNE(output.getExitValue(), 0, 128 "JVM is expected to fail, but did not"); 129 } 130 } 131 132 static void testExec(int testCaseNr, String collectorOption, String extraOption, 133 String expectedWarning, boolean expectedToFail) throws Exception { 134 135 OutputAnalyzer output; 136 System.out.println("Testcase: " + testCaseNr); 137 138 // needed, otherwise system considers empty extra option as a 139 // main class param, and fails with "Could not find or load main class" 140 if (!extraOption.isEmpty()) { 141 output = TestCommon.exec(appJar, "-XX:+UseCompressedOops", 142 collectorOption, "-Xlog:cds", extraOption, "HelloString"); 143 } else { 144 output = TestCommon.exec(appJar, "-XX:+UseCompressedOops", 145 collectorOption, "-Xlog:cds", "HelloString"); 146 } 147 148 if (expectedWarning != null) 149 output.shouldMatch(expectedWarning); 150 151 if (expectedToFail) 152 Asserts.assertNE(output.getExitValue(), 0); 153 else 154 SharedStringsUtils.checkExec(output); 155 } 156 }