1 /*
2 * Copyright (c) 2013, 2025, 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:+UnlockExperimentalVMOptions",
78 "-XX:+UnlockDiagnosticVMOptions",
79 "-XX:-UseCompactObjectHeaders",
80 "-XX:CompressedClassSpaceBaseAddress=" + forceAddressString,
81 "-Xmx128m",
82 "-Xlog:metaspace*",
83 "-version");
84 OutputAnalyzer output = new OutputAnalyzer(pb.start());
85
86 output.reportDiagnosticSummary();
87
88 // We ignore cases where we were not able to map at the force address
89 if (!output.contains("Successfully forced class space address to " + forceAddressString)) {
90 throw new SkippedException("Skipping because we cannot force ccs to " + forceAddressString);
91 }
92
93 if (Platform.isAArch64()) {
94 output.shouldHaveExitValue(1);
95 output.shouldContain("Error occurred during initialization of VM");
96 output.shouldContain("CompressedClassSpaceBaseAddress=" + forceAddressString +
97 " given with shift 0, cannot be used to encode class pointers");
98 } else {
99 output.shouldHaveExitValue(0);
100 }
101 }
102
103 final static long K = 1024;
104 final static long M = K * 1024;
105 final static long G = M * 1024;
106 public static void main(String[] args) throws Exception {
107 // Test ccs nestling right at the end of the 4G range
108 // Expecting base=0, shift=0
109 test(4 * G - 128 * M, false, 128 * M, 0, 0);
110
111 // Test ccs nestling right at the end of the 32G range
112 // Expecting:
113 // - non-aarch64: base=0, shift=3
114 // - aarch64: base to start of class range, shift 0
115 if (Platform.isAArch64()) {
116 // The best we can do on aarch64 is to be *near* the end of the 32g range, since a valid encoding base
117 // on aarch64 must be 4G aligned, and the max. class space size is 3G.
118 long forceAddress = 0x7_0000_0000L; // 28g, and also a valid EOR immediate
119 test(forceAddress, false, 3 * G, forceAddress, 0);
120 } else {
121 test(32 * G - 128 * M, false, 128 * M, 0, 3);
122 }
123
124 // Test ccs starting *below* 4G, but extending upwards beyond 4G. All platforms except aarch64 should pick
125 // zero based encoding. On aarch64, this test is excluded since the only valid mode would be XOR, but bit
126 // pattern for base and bit pattern would overlap.
127 if (!Platform.isAArch64()) {
128 test(4 * G - 128 * M, false, 2 * 128 * M, 0, 3);
129 }
130 // add more...
131
132 // Compact Object Header Mode:
133 // On aarch64 and x64 we expect the VM to chose the smallest possible shift value needed to cover
134 // the encoding range. We expect the encoding Base to start at the class space start - but to enforce that,
135 // we choose a high address.
136 if (Platform.isAArch64() || Platform.isX64() || Platform.isRISCV64()) {
137 long forceAddress = 32 * G;
138
139 long ccsSize = 128 * M;
140 int expectedShift = 9;
141 test(forceAddress, true, ccsSize, forceAddress, expectedShift);
142
143 ccsSize = 512 * M;
144 expectedShift = 10;
145 test(forceAddress, true, ccsSize, forceAddress, expectedShift);
146
147 ccsSize = G;
148 expectedShift = 10;
149 test(forceAddress, true, ccsSize, forceAddress, expectedShift);
150
151 ccsSize = 3 * G;
152 expectedShift = 10;
153 test(forceAddress, true, ccsSize, forceAddress, expectedShift);
154 }
155
156 // Test failure for -XX:CompressedClassBaseAddress and -Xshare:off
157 testFailure("0x0000040001000000");
158
159 }
160 }