1 /*
  2  * Copyright (c) 2013, 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  * @test
 26  * @summary Testing that, faced with a given (possibly odd) mapping address of class space, the encoding
 27  *          scheme fits the address
 28  * @requires vm.bits == 64 & !vm.graal.enabled & vm.debug == true
 29  * @requires vm.flagless
 30  * @library /test/lib
 31  * @modules java.base/jdk.internal.misc
 32  *          java.management
 33  * @run driver CompressedClassPointersEncodingScheme
 34  */
 35 
 36 import jdk.test.lib.Platform;
 37 import jdk.test.lib.process.OutputAnalyzer;
 38 import jdk.test.lib.process.ProcessTools;
 39 import jtreg.SkippedException;
 40 
 41 import java.io.IOException;
 42 
 43 public class CompressedClassPointersEncodingScheme {
 44 
 45     private static void test(long forceAddress, boolean COH, long classSpaceSize, long expectedEncodingBase, int expectedEncodingShift) throws IOException {
 46         String forceAddressString = String.format("0x%016X", forceAddress).toLowerCase();
 47         String expectedEncodingBaseString = String.format("0x%016X", expectedEncodingBase).toLowerCase();
 48         ProcessBuilder pb = ProcessTools.createLimitedTestJavaProcessBuilder(
 49                 "-Xshare:off", // to make CompressedClassSpaceBaseAddress work
 50                 "-XX:+UnlockDiagnosticVMOptions",
 51                 "-XX:-UseCompressedOops", // keep VM from optimizing heap location
 52                 "-XX:+UnlockExperimentalVMOptions",
 53                 "-XX:" + (COH ? "+" : "-") + "UseCompactObjectHeaders",
 54                 "-XX:" + (COH ? "+" : "-") + "UseObjectMonitorTable",
 55                 "-XX:CompressedClassSpaceBaseAddress=" + forceAddress,
 56                 "-XX:CompressedClassSpaceSize=" + classSpaceSize,
 57                 "-Xmx128m",
 58                 "-Xlog:metaspace*",
 59                 "-version");
 60         OutputAnalyzer output = new OutputAnalyzer(pb.start());
 61 
 62         output.reportDiagnosticSummary();
 63 
 64         // We ignore cases where we were not able to map at the force address
 65         if (output.contains("reserving class space failed")) {
 66             System.out.println("Skipping because we cannot force ccs to " + forceAddressString);
 67             return;
 68         }
 69 
 70         output.shouldHaveExitValue(0);
 71         output.shouldContain("Narrow klass base: " + expectedEncodingBaseString + ", Narrow klass shift: " + expectedEncodingShift);
 72     }
 73 
 74     private static void testFailure(String forceAddressString) throws IOException {
 75         ProcessBuilder pb = ProcessTools.createLimitedTestJavaProcessBuilder(
 76                 "-Xshare:off", // to make CompressedClassSpaceBaseAddress work
 77                 "-XX:+UnlockDiagnosticVMOptions",
 78                 "-XX:CompressedClassSpaceBaseAddress=" + forceAddressString,
 79                 "-Xmx128m",
 80                 "-Xlog:metaspace*",
 81                 "-version");
 82         OutputAnalyzer output = new OutputAnalyzer(pb.start());
 83 
 84         output.reportDiagnosticSummary();
 85 
 86         // We ignore cases where we were not able to map at the force address
 87         if (!output.contains("Successfully forced class space address to " + forceAddressString)) {
 88             throw new SkippedException("Skipping because we cannot force ccs to " + forceAddressString);
 89         }
 90 
 91         if (Platform.isAArch64()) {
 92             output.shouldHaveExitValue(1);
 93             output.shouldContain("Error occurred during initialization of VM");
 94             output.shouldContain("CompressedClassSpaceBaseAddress=" + forceAddressString +
 95                                  " given with shift 0, cannot be used to encode class pointers");
 96         } else {
 97             output.shouldHaveExitValue(0);
 98         }
 99     }
100 
101     final static long K = 1024;
102     final static long M = K * 1024;
103     final static long G = M * 1024;
104     public static void main(String[] args) throws Exception {
105         // Test ccs nestling right at the end of the 4G range
106         // Expecting base=0, shift=0
107         test(4 * G - 128 * M, false, 128 * M, 0, 0);
108 
109         // Test ccs nestling right at the end of the 32G range
110         // Expecting:
111         // - non-aarch64: base=0, shift=3
112         // - aarch64: base to start of class range, shift 0
113         if (Platform.isAArch64()) {
114             // The best we can do on aarch64 is to be *near* the end of the 32g range, since a valid encoding base
115             // on aarch64 must be 4G aligned, and the max. class space size is 3G.
116             long forceAddress = 0x7_0000_0000L; // 28g, and also a valid EOR immediate
117             test(forceAddress, false, 3 * G, forceAddress, 0);
118         } else {
119             test(32 * G - 128 * M, false, 128 * M, 0, 3);
120         }
121 
122         // Test ccs starting *below* 4G, but extending upwards beyond 4G. All platforms except aarch64 should pick
123         // zero based encoding. On aarch64, this test is excluded since the only valid mode would be XOR, but bit
124         // pattern for base and bit pattern would overlap.
125         if (!Platform.isAArch64()) {
126             test(4 * G - 128 * M, false, 2 * 128 * M, 0, 3);
127         }
128         // add more...
129 
130         // Compact Object Header Mode:
131         // On aarch64 and x64 we expect the VM to chose the smallest possible shift value needed to cover
132         // the encoding range. We expect the encoding Base to start at the class space start - but to enforce that,
133         // we choose a high address.
134         if (Platform.isAArch64() || Platform.isX64() || Platform.isRISCV64()) {
135             long forceAddress = 32 * G;
136 
137             long ccsSize = 128 * M;
138             int expectedShift = 9;
139             test(forceAddress, true, ccsSize, forceAddress, expectedShift);
140 
141             ccsSize = 512 * M;
142             expectedShift = 10;
143             test(forceAddress, true, ccsSize, forceAddress, expectedShift);
144 
145             ccsSize = G;
146             expectedShift = 10;
147             test(forceAddress, true, ccsSize, forceAddress, expectedShift);
148 
149             ccsSize = 3 * G;
150             expectedShift = 10;
151             test(forceAddress, true, ccsSize, forceAddress, expectedShift);
152         }
153 
154         // Test failure for -XX:CompressedClassBaseAddress and -Xshare:off
155         testFailure("0x0000040001000000");
156 
157     }
158 }