1 /*
  2  * Copyright (c) 2024, 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 CDS should fail to load if production time GC flags do not match training run.
 28  * @requires vm.flagless
 29  * @requires vm.cds.write.archived.java.heap
 30  * @library /test/jdk/lib/testlibrary /test/lib
 31  * @build LeydenGCFlags
 32  * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar app.jar HelloApp
 33  * @run driver LeydenGCFlags
 34  */
 35 
 36 import jdk.test.lib.cds.CDSAppTester;
 37 import jdk.test.lib.helpers.ClassFileInstaller;
 38 import jdk.test.lib.process.OutputAnalyzer;
 39 import jdk.test.lib.StringArrayUtils;
 40 
 41 public class LeydenGCFlags {
 42     static final String appJar = ClassFileInstaller.getJarPath("app.jar");
 43     static final String mainClass = "HelloApp";
 44     static final String ERROR_GC_SUPPORTED = "Cannot create the CacheDataStore: UseCompressedClassPointers must be enabled, and collector must be G1, Parallel, or Serial";
 45     static final String ERROR_GC_MISMATCH = "CDS archive has preloaded classes. It cannot be used because GC used during dump time (.*) is not the same as runtime (.*)";
 46     static final String ERROR_COOP_MISMATCH = "Disable Startup Code Cache: 'HelloApp.cds.code' was created with CompressedOops::shift.. = .* vs current .*";
 47 
 48     static String trainingArgs[];
 49     static String productionArgs[];
 50     static boolean shouldFailDump;
 51     static boolean shouldFailRun;
 52     static String productFailPattern;
 53 
 54     public static void main(String[] args) throws Exception {
 55         // ZGC not supported for now
 56         fail_dump("-XX:+UseZGC",  "-Xmx8g",  ERROR_GC_SUPPORTED);
 57 
 58         // Serial and Parallel collector are allowed to be used, as long as the same one is used
 59         // between training and production
 60         good("-XX:+UseSerialGC",   "-XX:+UseSerialGC");
 61         good("-XX:+UseParallelGC", "-XX:+UseParallelGC");
 62 
 63         // Fail if production uses a different collector than training
 64         fail_run("-XX:+UseParallelGC", "-XX:+UseG1GC",        ERROR_GC_MISMATCH );
 65         fail_run(null,                 "-XX:+UseParallelGC",  ERROR_GC_MISMATCH );
 66 
 67 
 68        if (false) { // Disabled for now, as on MacOS we cannot guarantee to get differnt coop encodings
 69         // Different oop encodings
 70         fail_run(array("-XX:-UseCompatibleCompressedOops", "-Xmx128m"), 
 71                  array("-XX:-UseCompatibleCompressedOops","-Xmx8g"),
 72                  ERROR_COOP_MISMATCH);
 73         fail_run(array("-XX:-UseCompatibleCompressedOops", "-Xmx8g"),
 74                  array("-XX:-UseCompatibleCompressedOops","-Xmx128m"),
 75                  ERROR_COOP_MISMATCH);
 76        }
 77 
 78         // FIXME -- this causes
 79         // java.lang.invoke.WrongMethodTypeException: handle's method type ()Object but found ()Object
 80      /* fail_run(null,                 "NoSuchApp"); */
 81     }
 82 
 83     static String[] array(String... strings) {
 84         return strings;
 85     }
 86     static void good(Object t, Object p) throws Exception {
 87         shouldFailDump = false;
 88         shouldFailRun = false;
 89         run(t, p);
 90     }
 91 
 92     static void fail_dump(Object t, Object p, String regexp) throws Exception {
 93         shouldFailDump = true;
 94         shouldFailRun  = true;
 95         productFailPattern = regexp;
 96         trainingArgs = makeArgs(t);
 97         productionArgs = makeArgs(p);
 98         Tester tester = new Tester();
 99         tester.setCheckExitValue(false);
100         tester.run(new String[] {"LEYDEN_TRAINONLY"} );
101     }
102 
103     static void fail_run(Object t, Object p, String regexp) throws Exception {
104         shouldFailDump = false;
105         shouldFailRun  = true;
106         productFailPattern = regexp;
107         run(t, p);
108     }
109 
110     static void run(Object t, Object p) throws Exception {
111         trainingArgs = makeArgs(t);
112         productionArgs = makeArgs(p);
113         Tester tester = new Tester();
114         tester.run(new String[] {"LEYDEN"} );
115     }
116 
117     static String[] makeArgs(Object o) {
118         if (o == null) {
119             return new String[0];
120         }
121         if (o instanceof String[]) {
122             return (String[])o;
123         } else {
124             return new String[] { (String)o };
125         }
126     }
127 
128     static class Tester extends CDSAppTester {
129         public Tester() {
130             super(mainClass);
131         }
132 
133         @Override
134         public String classpath(RunMode runMode) {
135             return appJar;
136         }
137 
138         @Override
139         public String[] appCommandLine(RunMode runMode) {
140             if (runMode.isProductionRun()) {
141                 return StringArrayUtils.concat(productionArgs, "-Xshare:auto", mainClass);
142             } else {
143                 return StringArrayUtils.concat(trainingArgs, mainClass);
144             }
145         }
146 
147         @Override
148         public void checkExecution(OutputAnalyzer out, RunMode runMode) {
149             if (shouldFailDump) {
150                 if (runMode != RunMode.PRODUCTION) {
151                     out.shouldMatch(productFailPattern);
152                     out.shouldHaveExitValue(1);
153                 }
154             } else if (shouldFailRun) {
155                 if (runMode == RunMode.PRODUCTION) {
156                     out.shouldMatch(productFailPattern);
157                     //out.shouldHaveExitValue(1); TODO VM should enable -Xshare:on by default
158                 }
159             } else {
160                 if (runMode != RunMode.TRAINING1) {
161                     out.shouldContain("Hello Leyden");
162                 }
163 
164                 if (runMode == RunMode.TRAINING || runMode == RunMode.TRAINING1) {
165                     // We should dump the heap even if the collector is not G1
166                     out.shouldContain("Shared file region (hp)");
167                 }
168 
169             }
170         }
171     }
172 }
173 
174 class HelloApp {
175     public static void main(String args[]) {
176         System.out.println("Hello Leyden");
177     }
178 }