43972
|
1 |
/*
|
58299
|
2 |
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
|
43972
|
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 |
*/
|
50858
|
23 |
|
|
24 |
|
43972
|
25 |
package org.graalvm.compiler.replacements.test.classfile;
|
|
26 |
|
|
27 |
import static org.graalvm.compiler.bytecode.Bytecodes.ALOAD;
|
|
28 |
import static org.graalvm.compiler.bytecode.Bytecodes.ANEWARRAY;
|
|
29 |
import static org.graalvm.compiler.bytecode.Bytecodes.ASTORE;
|
|
30 |
import static org.graalvm.compiler.bytecode.Bytecodes.BIPUSH;
|
|
31 |
import static org.graalvm.compiler.bytecode.Bytecodes.CHECKCAST;
|
|
32 |
import static org.graalvm.compiler.bytecode.Bytecodes.DLOAD;
|
|
33 |
import static org.graalvm.compiler.bytecode.Bytecodes.DSTORE;
|
|
34 |
import static org.graalvm.compiler.bytecode.Bytecodes.FLOAD;
|
|
35 |
import static org.graalvm.compiler.bytecode.Bytecodes.FSTORE;
|
|
36 |
import static org.graalvm.compiler.bytecode.Bytecodes.GETFIELD;
|
|
37 |
import static org.graalvm.compiler.bytecode.Bytecodes.GETSTATIC;
|
|
38 |
import static org.graalvm.compiler.bytecode.Bytecodes.GOTO;
|
|
39 |
import static org.graalvm.compiler.bytecode.Bytecodes.GOTO_W;
|
|
40 |
import static org.graalvm.compiler.bytecode.Bytecodes.IFEQ;
|
|
41 |
import static org.graalvm.compiler.bytecode.Bytecodes.IFGE;
|
|
42 |
import static org.graalvm.compiler.bytecode.Bytecodes.IFGT;
|
|
43 |
import static org.graalvm.compiler.bytecode.Bytecodes.IFLE;
|
|
44 |
import static org.graalvm.compiler.bytecode.Bytecodes.IFLT;
|
|
45 |
import static org.graalvm.compiler.bytecode.Bytecodes.IFNE;
|
|
46 |
import static org.graalvm.compiler.bytecode.Bytecodes.IFNONNULL;
|
|
47 |
import static org.graalvm.compiler.bytecode.Bytecodes.IFNULL;
|
|
48 |
import static org.graalvm.compiler.bytecode.Bytecodes.IF_ACMPEQ;
|
|
49 |
import static org.graalvm.compiler.bytecode.Bytecodes.IF_ACMPNE;
|
|
50 |
import static org.graalvm.compiler.bytecode.Bytecodes.IF_ICMPEQ;
|
|
51 |
import static org.graalvm.compiler.bytecode.Bytecodes.IF_ICMPGE;
|
|
52 |
import static org.graalvm.compiler.bytecode.Bytecodes.IF_ICMPGT;
|
|
53 |
import static org.graalvm.compiler.bytecode.Bytecodes.IF_ICMPLE;
|
|
54 |
import static org.graalvm.compiler.bytecode.Bytecodes.IF_ICMPLT;
|
|
55 |
import static org.graalvm.compiler.bytecode.Bytecodes.IF_ICMPNE;
|
|
56 |
import static org.graalvm.compiler.bytecode.Bytecodes.ILOAD;
|
|
57 |
import static org.graalvm.compiler.bytecode.Bytecodes.INSTANCEOF;
|
|
58 |
import static org.graalvm.compiler.bytecode.Bytecodes.INVOKEDYNAMIC;
|
|
59 |
import static org.graalvm.compiler.bytecode.Bytecodes.INVOKEINTERFACE;
|
|
60 |
import static org.graalvm.compiler.bytecode.Bytecodes.INVOKESPECIAL;
|
|
61 |
import static org.graalvm.compiler.bytecode.Bytecodes.INVOKESTATIC;
|
|
62 |
import static org.graalvm.compiler.bytecode.Bytecodes.INVOKEVIRTUAL;
|
|
63 |
import static org.graalvm.compiler.bytecode.Bytecodes.ISTORE;
|
|
64 |
import static org.graalvm.compiler.bytecode.Bytecodes.JSR;
|
|
65 |
import static org.graalvm.compiler.bytecode.Bytecodes.JSR_W;
|
|
66 |
import static org.graalvm.compiler.bytecode.Bytecodes.LDC;
|
|
67 |
import static org.graalvm.compiler.bytecode.Bytecodes.LDC2_W;
|
|
68 |
import static org.graalvm.compiler.bytecode.Bytecodes.LDC_W;
|
|
69 |
import static org.graalvm.compiler.bytecode.Bytecodes.LLOAD;
|
|
70 |
import static org.graalvm.compiler.bytecode.Bytecodes.LOOKUPSWITCH;
|
|
71 |
import static org.graalvm.compiler.bytecode.Bytecodes.LSTORE;
|
|
72 |
import static org.graalvm.compiler.bytecode.Bytecodes.MULTIANEWARRAY;
|
|
73 |
import static org.graalvm.compiler.bytecode.Bytecodes.NEW;
|
|
74 |
import static org.graalvm.compiler.bytecode.Bytecodes.NEWARRAY;
|
|
75 |
import static org.graalvm.compiler.bytecode.Bytecodes.PUTFIELD;
|
|
76 |
import static org.graalvm.compiler.bytecode.Bytecodes.PUTSTATIC;
|
|
77 |
import static org.graalvm.compiler.bytecode.Bytecodes.RET;
|
|
78 |
import static org.graalvm.compiler.bytecode.Bytecodes.SIPUSH;
|
|
79 |
import static org.graalvm.compiler.bytecode.Bytecodes.TABLESWITCH;
|
|
80 |
|
|
81 |
import java.io.File;
|
|
82 |
import java.io.IOException;
|
|
83 |
import java.lang.reflect.Executable;
|
|
84 |
import java.lang.reflect.Method;
|
|
85 |
import java.util.Enumeration;
|
|
86 |
import java.util.Formatter;
|
|
87 |
import java.util.zip.ZipEntry;
|
|
88 |
import java.util.zip.ZipFile;
|
|
89 |
|
58299
|
90 |
import org.graalvm.compiler.test.ModuleSupport;
|
52578
|
91 |
import org.graalvm.compiler.test.SubprocessUtil;
|
43972
|
92 |
import org.junit.Assert;
|
|
93 |
import org.junit.Assume;
|
52578
|
94 |
import org.junit.Before;
|
43972
|
95 |
import org.junit.Test;
|
|
96 |
|
|
97 |
import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;
|
|
98 |
import org.graalvm.compiler.api.test.Graal;
|
|
99 |
import org.graalvm.compiler.bytecode.Bytecode;
|
|
100 |
import org.graalvm.compiler.bytecode.BytecodeDisassembler;
|
|
101 |
import org.graalvm.compiler.bytecode.BytecodeLookupSwitch;
|
|
102 |
import org.graalvm.compiler.bytecode.BytecodeStream;
|
|
103 |
import org.graalvm.compiler.bytecode.BytecodeSwitch;
|
|
104 |
import org.graalvm.compiler.bytecode.BytecodeTableSwitch;
|
|
105 |
import org.graalvm.compiler.bytecode.Bytecodes;
|
|
106 |
import org.graalvm.compiler.bytecode.ResolvedJavaMethodBytecode;
|
|
107 |
import org.graalvm.compiler.core.test.GraalCompilerTest;
|
|
108 |
import org.graalvm.compiler.phases.VerifyPhase;
|
|
109 |
import org.graalvm.compiler.phases.util.Providers;
|
|
110 |
import org.graalvm.compiler.replacements.classfile.ClassfileBytecode;
|
|
111 |
import org.graalvm.compiler.replacements.classfile.ClassfileBytecodeProvider;
|
|
112 |
import org.graalvm.compiler.runtime.RuntimeProvider;
|
55509
|
113 |
import org.graalvm.compiler.serviceprovider.JavaVersionUtil;
|
43972
|
114 |
|
|
115 |
import jdk.vm.ci.meta.ConstantPool;
|
|
116 |
import jdk.vm.ci.meta.JavaField;
|
|
117 |
import jdk.vm.ci.meta.JavaMethodProfile.ProfiledMethod;
|
|
118 |
import jdk.vm.ci.meta.JavaType;
|
|
119 |
import jdk.vm.ci.meta.MetaAccessProvider;
|
|
120 |
import jdk.vm.ci.meta.ResolvedJavaField;
|
|
121 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
|
122 |
import jdk.vm.ci.meta.ResolvedJavaType;
|
|
123 |
|
|
124 |
/**
|
|
125 |
* Tests that bytecode exposed via {@link ClassfileBytecode} objects is the same as the bytecode
|
|
126 |
* (modulo minor differences in constant pool resolution) obtained directly from
|
|
127 |
* {@link ResolvedJavaMethod} objects.
|
|
128 |
*/
|
|
129 |
public class ClassfileBytecodeProviderTest extends GraalCompilerTest {
|
|
130 |
|
52578
|
131 |
@Before
|
|
132 |
public void checkJavaAgent() {
|
|
133 |
assumeManagementLibraryIsLoadable();
|
|
134 |
Assume.assumeFalse("Java Agent found -> skipping", SubprocessUtil.isJavaAgentAttached());
|
|
135 |
}
|
|
136 |
|
43972
|
137 |
private static boolean shouldProcess(String classpathEntry) {
|
|
138 |
if (classpathEntry.endsWith(".jar")) {
|
|
139 |
String name = new File(classpathEntry).getName();
|
|
140 |
return name.contains("jvmci") || name.contains("graal");
|
|
141 |
}
|
|
142 |
return false;
|
|
143 |
}
|
|
144 |
|
54601
|
145 |
/**
|
|
146 |
* Keep test time down by only sampling a limited number of class files per jar.
|
|
147 |
*/
|
|
148 |
private static final int CLASSES_PER_JAR = 250;
|
|
149 |
|
58299
|
150 |
/**
|
|
151 |
* Magic token to denote the classes in the Java runtime image (i.e. in the {@code jrt:/} file
|
|
152 |
* system).
|
|
153 |
*/
|
|
154 |
public static final String JRT_CLASS_PATH_ENTRY = "<jrt>";
|
|
155 |
|
43972
|
156 |
@Test
|
|
157 |
public void test() {
|
|
158 |
RuntimeProvider rt = Graal.getRequiredCapability(RuntimeProvider.class);
|
|
159 |
Providers providers = rt.getHostBackend().getProviders();
|
|
160 |
MetaAccessProvider metaAccess = providers.getMetaAccess();
|
|
161 |
|
|
162 |
Assume.assumeTrue(VerifyPhase.class.desiredAssertionStatus());
|
58299
|
163 |
String bootclasspath;
|
|
164 |
if (JavaVersionUtil.JAVA_SPEC <= 8) {
|
|
165 |
String propertyName = "sun.boot.class.path";
|
|
166 |
bootclasspath = System.getProperty(propertyName);
|
|
167 |
Assert.assertNotNull("Cannot find value of " + propertyName, bootclasspath);
|
|
168 |
} else {
|
|
169 |
bootclasspath = JRT_CLASS_PATH_ENTRY;
|
|
170 |
}
|
43972
|
171 |
|
|
172 |
for (String path : bootclasspath.split(File.pathSeparator)) {
|
|
173 |
if (shouldProcess(path)) {
|
|
174 |
try {
|
58299
|
175 |
if (path.equals(JRT_CLASS_PATH_ENTRY)) {
|
|
176 |
for (String className : ModuleSupport.getJRTGraalClassNames()) {
|
|
177 |
if (isGSON(className)) {
|
|
178 |
/*
|
|
179 |
* GSON classes are compiled with old JDK
|
|
180 |
*/
|
|
181 |
continue;
|
|
182 |
}
|
|
183 |
try {
|
|
184 |
checkClass(metaAccess, getSnippetReflection(), className);
|
|
185 |
} catch (ClassNotFoundException e) {
|
|
186 |
throw new AssertionError(e);
|
|
187 |
}
|
|
188 |
}
|
|
189 |
} else {
|
|
190 |
final ZipFile zipFile = new ZipFile(new File(path));
|
|
191 |
int index = 0;
|
|
192 |
int step = zipFile.size() > CLASSES_PER_JAR ? zipFile.size() / CLASSES_PER_JAR : 1;
|
|
193 |
for (final Enumeration<? extends ZipEntry> entry = zipFile.entries(); entry.hasMoreElements();) {
|
|
194 |
final ZipEntry zipEntry = entry.nextElement();
|
|
195 |
if ((index % step) == 0) {
|
|
196 |
String name = zipEntry.getName();
|
|
197 |
if (name.endsWith(".class") && !name.equals("module-info.class") && !name.startsWith("META-INF/versions/")) {
|
|
198 |
String className = name.substring(0, name.length() - ".class".length()).replace('/', '.');
|
|
199 |
if (isInNativeImage(className)) {
|
|
200 |
/*
|
|
201 |
* Native image requires non-graalsdk classes to be present
|
|
202 |
* in the classpath.
|
|
203 |
*/
|
|
204 |
continue;
|
|
205 |
}
|
|
206 |
if (isGSON(className)) {
|
|
207 |
/* uses old class format */
|
|
208 |
continue;
|
|
209 |
}
|
|
210 |
try {
|
|
211 |
checkClass(metaAccess, getSnippetReflection(), className);
|
|
212 |
} catch (ClassNotFoundException e) {
|
|
213 |
throw new AssertionError(e);
|
|
214 |
}
|
54601
|
215 |
}
|
43972
|
216 |
}
|
58299
|
217 |
index++;
|
43972
|
218 |
}
|
|
219 |
}
|
|
220 |
} catch (IOException ex) {
|
|
221 |
Assert.fail(ex.toString());
|
|
222 |
}
|
|
223 |
}
|
|
224 |
}
|
|
225 |
}
|
|
226 |
|
46680
|
227 |
private static boolean isInNativeImage(String className) {
|
|
228 |
return className.startsWith("org.graalvm.nativeimage");
|
|
229 |
}
|
|
230 |
|
52910
|
231 |
private static boolean isGSON(String className) {
|
|
232 |
return className.contains("com.google.gson");
|
|
233 |
}
|
|
234 |
|
43972
|
235 |
protected void checkClass(MetaAccessProvider metaAccess, SnippetReflectionProvider snippetReflection, String className) throws ClassNotFoundException {
|
54084
|
236 |
if (className.equals("jdk.vm.ci.services.JVMCIClassLoaderFactory")) {
|
|
237 |
// JVMCIClassLoaderFactory must only be initialized by the VM
|
|
238 |
return;
|
|
239 |
}
|
43972
|
240 |
Class<?> c = Class.forName(className, true, getClass().getClassLoader());
|
|
241 |
ClassfileBytecodeProvider cbp = new ClassfileBytecodeProvider(metaAccess, snippetReflection);
|
|
242 |
for (Method method : c.getDeclaredMethods()) {
|
|
243 |
checkMethod(cbp, metaAccess, method);
|
|
244 |
}
|
|
245 |
}
|
|
246 |
|
|
247 |
private static void checkMethod(ClassfileBytecodeProvider cbp, MetaAccessProvider metaAccess, Executable executable) {
|
|
248 |
ResolvedJavaMethod method = metaAccess.lookupJavaMethod(executable);
|
|
249 |
if (method.hasBytecodes()) {
|
|
250 |
Bytecode actual = getBytecode(cbp, method);
|
54084
|
251 |
if (actual != null) {
|
|
252 |
ResolvedJavaMethodBytecode expected = new ResolvedJavaMethodBytecode(method);
|
|
253 |
new BytecodeComparer(expected, actual).compare();
|
|
254 |
}
|
43972
|
255 |
}
|
|
256 |
}
|
|
257 |
|
|
258 |
protected static Bytecode getBytecode(ClassfileBytecodeProvider cbp, ResolvedJavaMethod method) {
|
|
259 |
try {
|
|
260 |
return cbp.getBytecode(method);
|
54084
|
261 |
} catch (UnsupportedClassVersionError e) {
|
|
262 |
// This can happen when a library containing old class files
|
|
263 |
// is bundled into a Graal jar (GR-12672).
|
|
264 |
return null;
|
43972
|
265 |
} catch (Throwable e) {
|
|
266 |
throw new AssertionError(String.format("Error getting bytecode for %s", method.format("%H.%n(%p)")), e);
|
|
267 |
}
|
|
268 |
}
|
|
269 |
|
|
270 |
static class BytecodeComparer {
|
|
271 |
|
|
272 |
private Bytecode expected;
|
|
273 |
private Bytecode actual;
|
|
274 |
private ConstantPool eCp;
|
|
275 |
private ConstantPool aCp;
|
|
276 |
BytecodeStream eStream;
|
|
277 |
BytecodeStream aStream;
|
|
278 |
int bci = -1;
|
|
279 |
|
|
280 |
BytecodeComparer(Bytecode expected, Bytecode actual) {
|
|
281 |
this.expected = expected;
|
|
282 |
this.actual = actual;
|
|
283 |
this.eCp = expected.getConstantPool();
|
|
284 |
this.aCp = actual.getConstantPool();
|
|
285 |
Assert.assertEquals(expected.getMethod().toString(), expected.getCodeSize(), actual.getCodeSize());
|
|
286 |
this.eStream = new BytecodeStream(expected.getCode());
|
|
287 |
this.aStream = new BytecodeStream(actual.getCode());
|
|
288 |
}
|
|
289 |
|
|
290 |
public void compare() {
|
|
291 |
try {
|
|
292 |
compare0();
|
|
293 |
} catch (Throwable e) {
|
|
294 |
BytecodeDisassembler dis = new BytecodeDisassembler(true, false);
|
|
295 |
Formatter msg = new Formatter();
|
|
296 |
msg.format("Error comparing bytecode for %s", expected.getMethod().format("%H.%n(%p)"));
|
|
297 |
if (bci >= 0) {
|
|
298 |
msg.format("%nexpected: %s", dis.disassemble(expected, bci, eStream.nextBCI() - 1));
|
|
299 |
msg.format("%nactual: %s", dis.disassemble(actual, bci, aStream.nextBCI() - 1));
|
|
300 |
}
|
|
301 |
throw new AssertionError(msg.toString(), e);
|
|
302 |
}
|
|
303 |
}
|
|
304 |
|
|
305 |
public void compare0() {
|
|
306 |
int opcode = eStream.currentBC();
|
|
307 |
ResolvedJavaMethod method = expected.getMethod();
|
|
308 |
while (opcode != Bytecodes.END) {
|
|
309 |
bci = eStream.currentBCI();
|
|
310 |
int actualOpcode = aStream.currentBC();
|
|
311 |
if (opcode != actualOpcode) {
|
|
312 |
Assert.assertEquals(opcode, actualOpcode);
|
|
313 |
}
|
|
314 |
if (eStream.nextBCI() > bci + 1) {
|
|
315 |
switch (opcode) {
|
|
316 |
case BIPUSH:
|
|
317 |
Assert.assertEquals(eStream.readByte(), aStream.readByte());
|
|
318 |
break;
|
|
319 |
case SIPUSH:
|
|
320 |
Assert.assertEquals(eStream.readShort(), aStream.readShort());
|
|
321 |
break;
|
|
322 |
case NEW:
|
|
323 |
case CHECKCAST:
|
|
324 |
case INSTANCEOF:
|
|
325 |
case ANEWARRAY: {
|
|
326 |
ResolvedJavaType e = lookupType(eCp, eStream.readCPI(), opcode);
|
|
327 |
ResolvedJavaType a = lookupType(aCp, aStream.readCPI(), opcode);
|
|
328 |
assertEqualTypes(e, a);
|
|
329 |
break;
|
|
330 |
}
|
|
331 |
case GETSTATIC:
|
|
332 |
case PUTSTATIC:
|
|
333 |
case GETFIELD:
|
|
334 |
case PUTFIELD: {
|
|
335 |
ResolvedJavaField e = lookupField(eCp, eStream.readCPI(), method, opcode);
|
|
336 |
ResolvedJavaField a = lookupField(aCp, aStream.readCPI(), method, opcode);
|
|
337 |
assertEqualFields(e, a);
|
|
338 |
break;
|
|
339 |
}
|
|
340 |
case INVOKEVIRTUAL:
|
|
341 |
case INVOKESPECIAL:
|
|
342 |
case INVOKESTATIC: {
|
|
343 |
ResolvedJavaMethod e = lookupMethod(eCp, eStream.readCPI(), opcode);
|
|
344 |
ResolvedJavaMethod a = lookupMethodOrNull(aCp, aStream.readCPI(), opcode);
|
|
345 |
assertEqualMethods(e, a);
|
|
346 |
break;
|
|
347 |
}
|
|
348 |
case INVOKEINTERFACE: {
|
|
349 |
ResolvedJavaMethod e = lookupMethod(eCp, eStream.readCPI(), opcode);
|
|
350 |
ResolvedJavaMethod a = lookupMethod(aCp, aStream.readCPI(), opcode);
|
|
351 |
assertEqualMethods(e, a);
|
|
352 |
break;
|
|
353 |
}
|
|
354 |
case INVOKEDYNAMIC: {
|
|
355 |
// INVOKEDYNAMIC is not supported by ClassfileBytecodeProvider
|
|
356 |
return;
|
|
357 |
}
|
|
358 |
case LDC:
|
|
359 |
case LDC_W:
|
|
360 |
case LDC2_W: {
|
|
361 |
Object e = lookupConstant(eCp, eStream.readCPI(), opcode);
|
|
362 |
Object a = lookupConstant(aCp, aStream.readCPI(), opcode);
|
|
363 |
assertEqualsConstants(e, a);
|
|
364 |
break;
|
|
365 |
}
|
|
366 |
case RET:
|
|
367 |
case ILOAD:
|
|
368 |
case LLOAD:
|
|
369 |
case FLOAD:
|
|
370 |
case DLOAD:
|
|
371 |
case ALOAD:
|
|
372 |
case ISTORE:
|
|
373 |
case LSTORE:
|
|
374 |
case FSTORE:
|
|
375 |
case DSTORE:
|
|
376 |
case ASTORE: {
|
|
377 |
Assert.assertEquals(eStream.readLocalIndex(), aStream.readLocalIndex());
|
|
378 |
break;
|
|
379 |
}
|
|
380 |
case IFEQ:
|
|
381 |
case IFNE:
|
|
382 |
case IFLT:
|
|
383 |
case IFGE:
|
|
384 |
case IFGT:
|
|
385 |
case IFLE:
|
|
386 |
case IF_ICMPEQ:
|
|
387 |
case IF_ICMPNE:
|
|
388 |
case IF_ICMPLT:
|
|
389 |
case IF_ICMPGE:
|
|
390 |
case IF_ICMPGT:
|
|
391 |
case IF_ICMPLE:
|
|
392 |
case IF_ACMPEQ:
|
|
393 |
case IF_ACMPNE:
|
|
394 |
case GOTO:
|
|
395 |
case JSR:
|
|
396 |
case IFNULL:
|
|
397 |
case IFNONNULL:
|
|
398 |
case GOTO_W:
|
|
399 |
case JSR_W: {
|
|
400 |
Assert.assertEquals(eStream.readBranchDest(), aStream.readBranchDest());
|
|
401 |
break;
|
|
402 |
}
|
|
403 |
case LOOKUPSWITCH:
|
|
404 |
case TABLESWITCH: {
|
|
405 |
BytecodeSwitch e = opcode == LOOKUPSWITCH ? new BytecodeLookupSwitch(eStream, bci) : new BytecodeTableSwitch(eStream, bci);
|
|
406 |
BytecodeSwitch a = opcode == LOOKUPSWITCH ? new BytecodeLookupSwitch(aStream, bci) : new BytecodeTableSwitch(aStream, bci);
|
|
407 |
Assert.assertEquals(e.numberOfCases(), a.numberOfCases());
|
|
408 |
for (int i = 0; i < e.numberOfCases(); i++) {
|
|
409 |
Assert.assertEquals(e.keyAt(i), a.keyAt(i));
|
|
410 |
Assert.assertEquals(e.targetAt(i), a.targetAt(i));
|
|
411 |
}
|
|
412 |
Assert.assertEquals(e.defaultTarget(), a.defaultTarget());
|
|
413 |
Assert.assertEquals(e.defaultOffset(), a.defaultOffset());
|
|
414 |
break;
|
|
415 |
}
|
|
416 |
case NEWARRAY: {
|
|
417 |
Assert.assertEquals(eStream.readLocalIndex(), aStream.readLocalIndex());
|
|
418 |
break;
|
|
419 |
}
|
|
420 |
case MULTIANEWARRAY: {
|
|
421 |
ResolvedJavaType e = lookupType(eCp, eStream.readCPI(), opcode);
|
|
422 |
ResolvedJavaType a = lookupType(aCp, aStream.readCPI(), opcode);
|
|
423 |
Assert.assertEquals(e, a);
|
|
424 |
break;
|
|
425 |
}
|
|
426 |
}
|
|
427 |
}
|
|
428 |
eStream.next();
|
|
429 |
aStream.next();
|
|
430 |
opcode = eStream.currentBC();
|
|
431 |
}
|
|
432 |
}
|
|
433 |
|
|
434 |
static Object lookupConstant(ConstantPool cp, int cpi, int opcode) {
|
|
435 |
cp.loadReferencedType(cpi, opcode);
|
|
436 |
return cp.lookupConstant(cpi);
|
|
437 |
}
|
|
438 |
|
|
439 |
static ResolvedJavaField lookupField(ConstantPool cp, int cpi, ResolvedJavaMethod method, int opcode) {
|
|
440 |
cp.loadReferencedType(cpi, opcode);
|
|
441 |
return (ResolvedJavaField) cp.lookupField(cpi, method, opcode);
|
|
442 |
}
|
|
443 |
|
|
444 |
static ResolvedJavaMethod lookupMethod(ConstantPool cp, int cpi, int opcode) {
|
|
445 |
cp.loadReferencedType(cpi, opcode);
|
|
446 |
return (ResolvedJavaMethod) cp.lookupMethod(cpi, opcode);
|
|
447 |
}
|
|
448 |
|
|
449 |
static ResolvedJavaMethod lookupMethodOrNull(ConstantPool cp, int cpi, int opcode) {
|
|
450 |
try {
|
|
451 |
return lookupMethod(cp, cpi, opcode);
|
|
452 |
} catch (NoSuchMethodError e) {
|
|
453 |
// A method hidden to reflection
|
|
454 |
return null;
|
|
455 |
}
|
|
456 |
}
|
|
457 |
|
|
458 |
static ResolvedJavaType lookupType(ConstantPool cp, int cpi, int opcode) {
|
|
459 |
cp.loadReferencedType(cpi, opcode);
|
|
460 |
return (ResolvedJavaType) cp.lookupType(cpi, opcode);
|
|
461 |
}
|
|
462 |
|
|
463 |
static void assertEqualsConstants(Object e, Object a) {
|
|
464 |
if (!e.equals(a)) {
|
|
465 |
Assert.assertEquals(String.valueOf(e), String.valueOf(a));
|
|
466 |
}
|
|
467 |
}
|
|
468 |
|
|
469 |
static void assertEqualFields(JavaField e, JavaField a) {
|
|
470 |
if (!e.equals(a)) {
|
|
471 |
Assert.assertEquals(e.format("%H.%n %T"), a.format("%H.%n %T"));
|
|
472 |
}
|
|
473 |
}
|
|
474 |
|
|
475 |
static void assertEqualTypes(JavaType e, JavaType a) {
|
|
476 |
if (!e.equals(a)) {
|
|
477 |
Assert.assertEquals(e.toJavaName(), a.toJavaName());
|
|
478 |
}
|
|
479 |
}
|
|
480 |
|
|
481 |
static void assertEqualMethods(ResolvedJavaMethod e, ResolvedJavaMethod a) {
|
|
482 |
if (a != null) {
|
|
483 |
if (!e.equals(a)) {
|
|
484 |
if (!e.equals(a)) {
|
|
485 |
if (!e.getDeclaringClass().equals(a.getDeclaringClass())) {
|
|
486 |
|
|
487 |
if (!typesAreRelated(e, a)) {
|
|
488 |
throw new AssertionError(String.format("%s and %s are unrelated", a.getDeclaringClass().toJavaName(), e.getDeclaringClass().toJavaName()));
|
|
489 |
}
|
|
490 |
}
|
|
491 |
Assert.assertEquals(e.getName(), a.getName());
|
|
492 |
Assert.assertEquals(e.getSignature(), a.getSignature());
|
|
493 |
} else {
|
|
494 |
Assert.assertEquals(e, a);
|
|
495 |
}
|
|
496 |
}
|
|
497 |
}
|
|
498 |
}
|
|
499 |
|
|
500 |
/**
|
|
501 |
* The VM can resolve references to methods not available via reflection. For example, the
|
|
502 |
* javap output for {@link ProfiledMethod#toString()} includes:
|
|
503 |
*
|
|
504 |
* <pre>
|
|
505 |
* 16: invokeinterface #40, 1 // InterfaceMethod jdk/vm/ci/meta/ResolvedJavaMethod.getName:()Ljava/lang/String;
|
|
506 |
* </pre>
|
|
507 |
*
|
|
508 |
* When resolving via {@code HotSpotConstantPool}, we get:
|
|
509 |
*
|
|
510 |
* <pre>
|
|
511 |
* 16: invokeinterface#4, 1 // jdk.vm.ci.meta.ResolvedJavaMethod.getName:()java.lang.String
|
|
512 |
* </pre>
|
|
513 |
*
|
|
514 |
* However resolving via {@code ClassfileConstantPool}, we get:
|
|
515 |
*
|
|
516 |
* <pre>
|
|
517 |
* 16: invokeinterface#40, 1 // jdk.vm.ci.meta.JavaMethod.getName:()java.lang.String
|
|
518 |
* </pre>
|
|
519 |
*
|
|
520 |
* since the latter relies on {@link ResolvedJavaType#getDeclaredMethods()} which only
|
|
521 |
* returns methods originating from class files.
|
|
522 |
*
|
|
523 |
* We accept such differences for the purpose of this test if the declaring class of two
|
|
524 |
* otherwise similar methods are related (i.e. one is a subclass of the other).
|
|
525 |
*/
|
|
526 |
protected static boolean typesAreRelated(ResolvedJavaMethod e, ResolvedJavaMethod a) {
|
|
527 |
return a.getDeclaringClass().isAssignableFrom(e.getDeclaringClass()) || e.getDeclaringClass().isAssignableFrom(a.getDeclaringClass());
|
|
528 |
}
|
|
529 |
}
|
|
530 |
}
|