1 /*
  2  * Copyright (c) 2020, 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  * @test
 26  * @bug 8232069
 27  * @summary Testing different combination of CompressedOops and CompressedClassPointers
 28  * @requires vm.cds
 29  * @requires vm.gc == "null"
 30  * @requires vm.bits == 64
 31  * @library /test/lib /test/hotspot/jtreg/runtime/cds/appcds
 32  * @compile test-classes/Hello.java
 33  * @modules java.base/jdk.internal.misc
 34  * @run driver TestCombinedCompressedFlags
 35  */
 36 
 37 import jdk.test.lib.process.OutputAnalyzer;
 38 import java.util.List;
 39 import java.util.ArrayList;
 40 
 41 public class TestCombinedCompressedFlags {
 42     public static String HELLO_STRING = "Hello World";
 43     public static String EXEC_ABNORMAL_MSG = "Unable to use shared archive.";
 44     public static final int PASS = 0;
 45     public static final int FAIL = 1;
 46 
 47     static class ConfArg {
 48         public boolean useCompressedOops;            // UseCompressedOops
 49         public boolean useCompressedClassPointers;   // UseCompressedClassPointers
 50         public String  msg;
 51         public int code;
 52         public ConfArg(boolean useCompressedOops, boolean useCompressedClassPointers, String msg, int code) {
 53             this.useCompressedOops = useCompressedOops;
 54             this.useCompressedClassPointers = useCompressedClassPointers;
 55             this.msg  = msg;
 56             this.code = code;
 57         }
 58     }
 59 
 60     static class RunArg {
 61         public ConfArg dumpArg;
 62         public List<ConfArg> execArgs;
 63         public RunArg(ConfArg arg) {
 64             dumpArg = arg;
 65             initExecArgs();
 66         }
 67         private void initExecArgs() {
 68            /* The combinations have four cases.
 69             *          UseCompressedOops   UseCompressedClassPointers  Result
 70             *    1.
 71             *    dump: on                  on
 72             *    test: on                  on                          Pass
 73             *          on                  off                         Fail
 74             *          off                 on                          Fail
 75             *          off                 off                         Fail
 76             *    2.
 77             *    dump: on                  off
 78             *    test: on                  off                         Pass
 79             *          on                  on                          Fail
 80             *          off                 on                          Pass
 81             *          off                 off                         Fail
 82             *    3.
 83             *    dump: off                 on
 84             *    test: off                 on                          Pass
 85             *          on                  on                          Fail
 86             *          on                  off                         Fail
 87             *    4.
 88             *    dump: off                 off
 89             *    test: off                 off                         Pass
 90             *          on                  on                          Fail
 91             *          on                  off                         Fail
 92             **/
 93             execArgs = new ArrayList<ConfArg>();
 94             if (dumpArg.useCompressedOops && dumpArg.useCompressedClassPointers) {
 95                 execArgs
 96                     .add(new ConfArg(true, true, HELLO_STRING, PASS));
 97                 execArgs
 98                     .add(new ConfArg(true, false, EXEC_ABNORMAL_MSG, FAIL));
 99                 execArgs
100                     .add(new ConfArg(false, true, EXEC_ABNORMAL_MSG, FAIL));
101                 execArgs
102                     .add(new ConfArg(false, false, EXEC_ABNORMAL_MSG, FAIL));
103 
104             }  else if(dumpArg.useCompressedOops && !dumpArg.useCompressedClassPointers) {
105                 execArgs
106                     .add(new ConfArg(true, false, HELLO_STRING, PASS));
107                 execArgs
108                     .add(new ConfArg(true, true, EXEC_ABNORMAL_MSG, FAIL));
109                 execArgs
110                     .add(new ConfArg(false, true, EXEC_ABNORMAL_MSG, FAIL));
111                 execArgs
112                     .add(new ConfArg(false, false, EXEC_ABNORMAL_MSG, FAIL));
113 
114             } else if (!dumpArg.useCompressedOops && dumpArg.useCompressedClassPointers) {
115                 execArgs
116                     .add(new ConfArg(false, true, HELLO_STRING, PASS));
117                 execArgs
118                     .add(new ConfArg(true, true, EXEC_ABNORMAL_MSG, FAIL));
119                 execArgs
120                     .add(new ConfArg(true, false, EXEC_ABNORMAL_MSG, FAIL));
121             } else if (!dumpArg.useCompressedOops && !dumpArg.useCompressedClassPointers) {
122                 execArgs
123                     .add(new ConfArg(false, false, HELLO_STRING, PASS));
124                 execArgs
125                     .add(new ConfArg(true, true, EXEC_ABNORMAL_MSG, FAIL));
126                 execArgs
127                     .add(new ConfArg(true, false, EXEC_ABNORMAL_MSG, FAIL));
128             }
129         }
130     }
131 
132     public static String getCompressedOopsArg(boolean on) {
133         if (on) return "-XX:+UseCompressedOops";
134         else    return "-XX:-UseCompressedOops";
135     }
136 
137     public static String getCompressedClassPointersArg(boolean on) {
138         if (on) return "-XX:+UseCompressedClassPointers";
139         else    return "-XX:-UseCompressedClassPointers";
140     }
141 
142     public static List<RunArg> runList;
143 
144     public static void configureRunArgs() {
145         runList = new ArrayList<RunArg>();
146         runList
147             .add(new RunArg(new ConfArg(true, true, null, PASS)));
148         runList
149             .add(new RunArg(new ConfArg(true, false, null, PASS)));
150         runList
151             .add(new RunArg(new ConfArg(false, true, null, PASS)));
152         runList
153             .add(new RunArg(new ConfArg(false, false, null, PASS)));
154     }
155 
156     public static void main(String[] args) throws Exception {
157         String helloJar = JarBuilder.build("hello", "Hello");
158         configureRunArgs();
159         OutputAnalyzer out;
160         for (RunArg t: runList) {
161             out = TestCommon
162                 .dump(helloJar,
163                       new String[] {"Hello"},
164                       getCompressedOopsArg(t.dumpArg.useCompressedOops),
165                       getCompressedClassPointersArg(t.dumpArg.useCompressedClassPointers),
166                       "-Xlog:cds",
167                       "-XX:NativeMemoryTracking=detail");
168             out.shouldContain("Dumping shared data to file:");
169             out.shouldHaveExitValue(0);
170 
171             for (ConfArg c : t.execArgs) {
172                 out = TestCommon.exec(helloJar,
173                                       "-cp",
174                                       helloJar,
175                                       "-Xlog:cds",
176                                       "-XX:NativeMemoryTracking=detail",
177                                       getCompressedOopsArg(c.useCompressedOops),
178                                       getCompressedClassPointersArg(c.useCompressedClassPointers),
179                                       "Hello");
180                 out.shouldContain(c.msg);
181                 out.shouldHaveExitValue(c.code);
182             }
183         }
184     }
185 }