author | chegar |
Tue, 22 Mar 2016 15:26:07 +0000 | |
changeset 36694 | 182a5e7a519e |
parent 36508 | 5f9eee6b383b |
child 38152 | 80e5da81fb2c |
permissions | -rw-r--r-- |
33160 | 1 |
/* |
2 |
* Copyright (c) 2015, 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 |
* @bug 8136421 |
|
35148 | 28 |
* @requires (os.simpleArch == "x64" | os.simpleArch == "sparcv9" | os.simpleArch == "aarch64") |
33730
30e064828045
8140189: [TESTBUG] Get rid of "@library /../../test/lib" in jtreg tests
cjplummer
parents:
33581
diff
changeset
|
29 |
* @library /testlibrary /test/lib / |
36508 | 30 |
* @library ../common/patches |
31 |
* @modules jdk.vm.ci/jdk.vm.ci.hotspot |
|
32 |
* jdk.vm.ci/jdk.vm.ci.meta |
|
33 |
* @build jdk.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper |
|
34 |
* @build jdk.vm.ci/jdk.vm.ci.hotspot.PublicMetaspaceWrapperObject |
|
35 |
* @build compiler.jvmci.compilerToVM.GetConstantPoolTest |
|
33160 | 36 |
* @build sun.hotspot.WhiteBox |
37 |
* @run main ClassFileInstaller sun.hotspot.WhiteBox |
|
38 |
* sun.hotspot.WhiteBox$WhiteBoxPermission |
|
36508 | 39 |
* @run main/othervm -Xbootclasspath/a:. |
40 |
* -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI |
|
41 |
* -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI |
|
42 |
* compiler.jvmci.compilerToVM.GetConstantPoolTest |
|
33160 | 43 |
*/ |
44 |
package compiler.jvmci.compilerToVM; |
|
45 |
||
46 |
import java.lang.reflect.Field; |
|
33632 | 47 |
import jdk.vm.ci.meta.ConstantPool; |
33160 | 48 |
import jdk.vm.ci.hotspot.CompilerToVMHelper; |
33632 | 49 |
import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod; |
50 |
import jdk.vm.ci.hotspot.HotSpotResolvedObjectType; |
|
51 |
import jdk.vm.ci.hotspot.PublicMetaspaceWrapperObject; |
|
33160 | 52 |
import jdk.test.lib.Utils; |
53 |
import sun.hotspot.WhiteBox; |
|
54 |
import sun.misc.Unsafe; |
|
55 |
||
56 |
/** |
|
57 |
* Tests for jdk.vm.ci.hotspot.CompilerToVM::getConstantPool method |
|
58 |
*/ |
|
59 |
public class GetConstantPoolTest { |
|
60 |
private static enum TestCase { |
|
61 |
NULL_BASE { |
|
62 |
@Override |
|
33632 | 63 |
ConstantPool getConstantPool() { |
33160 | 64 |
return CompilerToVMHelper.getConstantPool(null, |
65 |
getPtrToCpAddress()); |
|
66 |
} |
|
67 |
}, |
|
68 |
JAVA_METHOD_BASE { |
|
69 |
@Override |
|
33632 | 70 |
ConstantPool getConstantPool() { |
71 |
HotSpotResolvedJavaMethod methodInstance |
|
33160 | 72 |
= CompilerToVMHelper.getResolvedJavaMethodAtSlot( |
73 |
TEST_CLASS, 0); |
|
74 |
Field field; |
|
75 |
try { |
|
33632 | 76 |
// jdk.vm.ci.hotspot.HotSpotResolvedJavaMethodImpl.metaspaceMethod |
77 |
field = methodInstance.getClass() |
|
78 |
.getDeclaredField("metaspaceMethod"); |
|
33160 | 79 |
field.setAccessible(true); |
80 |
field.set(methodInstance, getPtrToCpAddress()); |
|
81 |
} catch (ReflectiveOperationException e) { |
|
33632 | 82 |
throw new Error("TESTBUG : " + e, e); |
33160 | 83 |
} |
84 |
||
85 |
return CompilerToVMHelper.getConstantPool(methodInstance, 0L); |
|
86 |
} |
|
87 |
}, |
|
88 |
CONSTANT_POOL_BASE { |
|
89 |
@Override |
|
33632 | 90 |
ConstantPool getConstantPool() { |
91 |
ConstantPool cpInst; |
|
33160 | 92 |
try { |
93 |
cpInst = CompilerToVMHelper.getConstantPool(null, |
|
94 |
getPtrToCpAddress()); |
|
33632 | 95 |
Field field = CompilerToVMHelper.HotSpotConstantPoolClass() |
33160 | 96 |
.getDeclaredField("metaspaceConstantPool"); |
97 |
field.setAccessible(true); |
|
98 |
field.set(cpInst, getPtrToCpAddress()); |
|
99 |
} catch (ReflectiveOperationException e) { |
|
100 |
throw new Error("TESTBUG : " + e.getMessage(), e); |
|
101 |
} |
|
102 |
return CompilerToVMHelper.getConstantPool(cpInst, 0L); |
|
103 |
} |
|
104 |
}, |
|
105 |
CONSTANT_POOL_BASE_IN_TWO { |
|
106 |
@Override |
|
33632 | 107 |
ConstantPool getConstantPool() { |
33160 | 108 |
long ptr = getPtrToCpAddress(); |
33632 | 109 |
ConstantPool cpInst; |
33160 | 110 |
try { |
111 |
cpInst = CompilerToVMHelper.getConstantPool(null, ptr); |
|
33632 | 112 |
Field field = CompilerToVMHelper.HotSpotConstantPoolClass() |
33160 | 113 |
.getDeclaredField("metaspaceConstantPool"); |
114 |
field.setAccessible(true); |
|
115 |
field.set(cpInst, ptr / 2L); |
|
116 |
} catch (ReflectiveOperationException e) { |
|
117 |
throw new Error("TESTBUG : " + e.getMessage(), e); |
|
118 |
} |
|
119 |
return CompilerToVMHelper.getConstantPool(cpInst, |
|
120 |
ptr - ptr / 2L); |
|
121 |
} |
|
122 |
}, |
|
123 |
CONSTANT_POOL_BASE_ZERO { |
|
124 |
@Override |
|
33632 | 125 |
ConstantPool getConstantPool() { |
33160 | 126 |
long ptr = getPtrToCpAddress(); |
33632 | 127 |
ConstantPool cpInst; |
33160 | 128 |
try { |
129 |
cpInst = CompilerToVMHelper.getConstantPool(null, ptr); |
|
33632 | 130 |
Field field = CompilerToVMHelper.HotSpotConstantPoolClass() |
33160 | 131 |
.getDeclaredField("metaspaceConstantPool"); |
132 |
field.setAccessible(true); |
|
133 |
field.set(cpInst, 0L); |
|
134 |
} catch (ReflectiveOperationException e) { |
|
135 |
throw new Error("TESTBUG : " + e.getMessage(), e); |
|
136 |
} |
|
137 |
return CompilerToVMHelper.getConstantPool(cpInst, ptr); |
|
138 |
} |
|
139 |
}, |
|
140 |
OBJECT_TYPE_BASE { |
|
141 |
@Override |
|
33632 | 142 |
ConstantPool getConstantPool() { |
143 |
HotSpotResolvedObjectType type |
|
144 |
= HotSpotResolvedObjectType.fromObjectClass( |
|
33160 | 145 |
OBJECT_TYPE_BASE.getClass()); |
146 |
long ptrToClass = UNSAFE.getKlassPointer(OBJECT_TYPE_BASE); |
|
147 |
return CompilerToVMHelper.getConstantPool(type, |
|
148 |
getPtrToCpAddress() - ptrToClass); |
|
149 |
} |
|
150 |
}, |
|
151 |
; |
|
33632 | 152 |
abstract ConstantPool getConstantPool(); |
33160 | 153 |
} |
154 |
||
155 |
private static final WhiteBox WB = WhiteBox.getWhiteBox(); |
|
156 |
private static final Unsafe UNSAFE = Utils.getUnsafe(); |
|
33632 | 157 |
|
33160 | 158 |
private static final Class TEST_CLASS = GetConstantPoolTest.class; |
159 |
private static final long CP_ADDRESS |
|
160 |
= WB.getConstantPool(GetConstantPoolTest.class); |
|
161 |
||
162 |
public void test(TestCase testCase) { |
|
163 |
System.out.println(testCase.name()); |
|
33632 | 164 |
ConstantPool cp = testCase.getConstantPool(); |
33160 | 165 |
String cpStringRep = cp.toString(); |
33632 | 166 |
String cpClassSimpleName |
167 |
= CompilerToVMHelper.HotSpotConstantPoolClass().getSimpleName(); |
|
168 |
if (!cpStringRep.contains(cpClassSimpleName) |
|
33160 | 169 |
|| !cpStringRep.contains(TEST_CLASS.getName())) { |
170 |
String msg = String.format("%s : " |
|
171 |
+ " Constant pool is not valid." |
|
172 |
+ " String representation should contain \"%s\" and \"%s\"", |
|
33632 | 173 |
testCase.name(), cpClassSimpleName, |
33160 | 174 |
TEST_CLASS.getName()); |
175 |
throw new AssertionError(msg); |
|
176 |
} |
|
177 |
} |
|
178 |
||
179 |
public static void main(String[] args) { |
|
180 |
GetConstantPoolTest test = new GetConstantPoolTest(); |
|
181 |
for (TestCase testCase : TestCase.values()) { |
|
182 |
test.test(testCase); |
|
183 |
} |
|
184 |
testObjectBase(); |
|
185 |
testMetaspaceWrapperBase(); |
|
186 |
} |
|
187 |
||
188 |
private static void testObjectBase() { |
|
189 |
try { |
|
33632 | 190 |
Object cp = CompilerToVMHelper.getConstantPool(new Object(), 0L); |
33160 | 191 |
throw new AssertionError("Test OBJECT_BASE." |
192 |
+ " Expected IllegalArgumentException has not been caught"); |
|
193 |
} catch (IllegalArgumentException iae) { |
|
194 |
// expected |
|
195 |
} |
|
196 |
} |
|
197 |
private static void testMetaspaceWrapperBase() { |
|
198 |
try { |
|
33632 | 199 |
Object cp = CompilerToVMHelper.getConstantPool( |
200 |
new PublicMetaspaceWrapperObject() { |
|
33160 | 201 |
@Override |
202 |
public long getMetaspacePointer() { |
|
203 |
return getPtrToCpAddress(); |
|
204 |
} |
|
205 |
}, 0L); |
|
206 |
throw new AssertionError("Test METASPACE_WRAPPER_BASE." |
|
207 |
+ " Expected IllegalArgumentException has not been caught"); |
|
208 |
} catch (IllegalArgumentException iae) { |
|
209 |
// expected |
|
210 |
} |
|
211 |
} |
|
212 |
||
213 |
private static long getPtrToCpAddress() { |
|
214 |
Field field; |
|
215 |
try { |
|
216 |
field = TEST_CLASS.getDeclaredField("CP_ADDRESS"); |
|
217 |
} catch (NoSuchFieldException nsfe) { |
|
218 |
throw new Error("TESTBUG : cannot find field \"CP_ADDRESS\" : " |
|
219 |
+ nsfe.getMessage(), nsfe); |
|
220 |
} |
|
221 |
Object base = UNSAFE.staticFieldBase(field); |
|
222 |
return WB.getObjectAddress(base) + UNSAFE.staticFieldOffset(field); |
|
223 |
} |
|
224 |
} |