1 /*
2  * Copyright (c) 2015, 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  * @requires vm.jvmci
27  * @requires vm.simpleArch == "x64" | vm.simpleArch == "aarch64"
28  * @library /
29  * @modules jdk.internal.vm.ci/jdk.vm.ci.hotspot
30  *          jdk.internal.vm.ci/jdk.vm.ci.meta
31  *          jdk.internal.vm.ci/jdk.vm.ci.code
32  *          jdk.internal.vm.ci/jdk.vm.ci.code.site
33  *          jdk.internal.vm.ci/jdk.vm.ci.runtime
34  *          jdk.internal.vm.ci/jdk.vm.ci.aarch64
35  *          jdk.internal.vm.ci/jdk.vm.ci.amd64
36  * @compile CodeInstallationTest.java DebugInfoTest.java TestAssembler.java TestHotSpotVMConfig.java amd64/AMD64TestAssembler.java aarch64/AArch64TestAssembler.java
37  * @run junit/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI -XX:-UseJVMCICompiler jdk.vm.ci.code.test.DataPatchTest
38  */
39 
40 package jdk.vm.ci.code.test;
41 
42 import jdk.vm.ci.code.Register;
43 import jdk.vm.ci.code.site.DataSectionReference;
44 import jdk.vm.ci.hotspot.HotSpotConstant;
45 import jdk.vm.ci.meta.ResolvedJavaType;
46 import org.junit.Assume;
47 import org.junit.Test;
48 
49 /**
50  * Test code installation with data patches.
51  */
52 public class DataPatchTest extends CodeInstallationTest {
53 
getConstClass()54     public static Class<?> getConstClass() {
55         return DataPatchTest.class;
56     }
57 
test(TestCompiler compiler)58     private void test(TestCompiler compiler) {
59         test(compiler, getMethod("getConstClass"));
60     }
61 
62     @Test
testInlineObject()63     public void testInlineObject() {
64         test(asm -> {
65             ResolvedJavaType type = metaAccess.lookupJavaType(getConstClass());
66             HotSpotConstant c = (HotSpotConstant) constantReflection.asJavaClass(type);
67             Register ret = asm.emitLoadPointer(c);
68             asm.emitPointerRet(ret);
69         });
70     }
71 
72     @Test
testInlineNarrowObject()73     public void testInlineNarrowObject() {
74         Assume.assumeTrue(config.useCompressedOops);
75         test(asm -> {
76             ResolvedJavaType type = metaAccess.lookupJavaType(getConstClass());
77             HotSpotConstant c = (HotSpotConstant) constantReflection.asJavaClass(type);
78             Register compressed = asm.emitLoadPointer((HotSpotConstant) c.compress());
79             Register ret = asm.emitUncompressPointer(compressed, config.narrowOopBase, config.narrowOopShift);
80             asm.emitPointerRet(ret);
81         });
82     }
83 
84     @Test
testDataSectionReference()85     public void testDataSectionReference() {
86         test(asm -> {
87             ResolvedJavaType type = metaAccess.lookupJavaType(getConstClass());
88             HotSpotConstant c = (HotSpotConstant) constantReflection.asJavaClass(type);
89             DataSectionReference ref = asm.emitDataItem(c);
90             Register ret = asm.emitLoadPointer(ref);
91             asm.emitPointerRet(ret);
92         });
93     }
94 
95     @Test
testNarrowDataSectionReference()96     public void testNarrowDataSectionReference() {
97         Assume.assumeTrue(config.useCompressedOops);
98         test(asm -> {
99             ResolvedJavaType type = metaAccess.lookupJavaType(getConstClass());
100             HotSpotConstant c = (HotSpotConstant) constantReflection.asJavaClass(type);
101             HotSpotConstant cCompressed = (HotSpotConstant) c.compress();
102             DataSectionReference ref = asm.emitDataItem(cCompressed);
103             Register compressed = asm.emitLoadNarrowPointer(ref);
104             Register ret = asm.emitUncompressPointer(compressed, config.narrowOopBase, config.narrowOopShift);
105             asm.emitPointerRet(ret);
106         });
107     }
108 
109     @Test
testInlineMetadata()110     public void testInlineMetadata() {
111         test(asm -> {
112             ResolvedJavaType type = metaAccess.lookupJavaType(getConstClass());
113             Register klass = asm.emitLoadPointer((HotSpotConstant) constantReflection.asObjectHub(type));
114             Register ret = asm.emitLoadPointer(asm.emitLoadPointer(klass, config.classMirrorHandleOffset), 0);
115             asm.emitPointerRet(ret);
116         });
117     }
118 
119     @Test
testInlineNarrowMetadata()120     public void testInlineNarrowMetadata() {
121         Assume.assumeTrue(config.useCompressedClassPointers);
122         test(asm -> {
123             ResolvedJavaType type = metaAccess.lookupJavaType(getConstClass());
124             HotSpotConstant hub = (HotSpotConstant) constantReflection.asObjectHub(type);
125             Register narrowKlass = asm.emitLoadPointer((HotSpotConstant) hub.compress());
126             Register klass = asm.emitUncompressPointer(narrowKlass, config.narrowKlassBase, config.narrowKlassShift);
127             Register ret = asm.emitLoadPointer(asm.emitLoadPointer(klass, config.classMirrorHandleOffset), 0);
128             asm.emitPointerRet(ret);
129         });
130     }
131 
132     @Test
testMetadataInDataSection()133     public void testMetadataInDataSection() {
134         test(asm -> {
135             ResolvedJavaType type = metaAccess.lookupJavaType(getConstClass());
136             HotSpotConstant hub = (HotSpotConstant) constantReflection.asObjectHub(type);
137             DataSectionReference ref = asm.emitDataItem(hub);
138             Register klass = asm.emitLoadPointer(ref);
139             Register ret = asm.emitLoadPointer(asm.emitLoadPointer(klass, config.classMirrorHandleOffset), 0);
140             asm.emitPointerRet(ret);
141         });
142     }
143 
144     @Test
testNarrowMetadataInDataSection()145     public void testNarrowMetadataInDataSection() {
146         Assume.assumeTrue(config.useCompressedClassPointers);
147         test(asm -> {
148             ResolvedJavaType type = metaAccess.lookupJavaType(getConstClass());
149             HotSpotConstant hub = (HotSpotConstant) constantReflection.asObjectHub(type);
150             HotSpotConstant narrowHub = (HotSpotConstant) hub.compress();
151             DataSectionReference ref = asm.emitDataItem(narrowHub);
152             Register narrowKlass = asm.emitLoadNarrowPointer(ref);
153             Register klass = asm.emitUncompressPointer(narrowKlass, config.narrowKlassBase, config.narrowKlassShift);
154             Register ret = asm.emitLoadPointer(asm.emitLoadPointer(klass, config.classMirrorHandleOffset), 0);
155             asm.emitPointerRet(ret);
156         });
157     }
158 }
159