1 /* 2 * Copyright (c) 2016, 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 * @bug 8138708 27 * @requires (os.simpleArch == "x64" | os.simpleArch == "sparcv9" | os.simpleArch == "aarch64") 28 * @library /testlibrary /test/lib / 29 * @library ../common/patches 30 * @modules java.base/jdk.internal.misc 31 * java.base/jdk.internal.org.objectweb.asm 32 * java.base/jdk.internal.org.objectweb.asm.tree 33 * jdk.vm.ci/jdk.vm.ci.meta 34 * jdk.vm.ci.hotspot/jdk.vm.ci.hotspot 35 * @build jdk.vm.ci.hotspot/jdk.vm.ci.hotspot.CompilerToVMHelper 36 * @build sun.hotspot.WhiteBox 37 * compiler.jvmci.compilerToVM.ResolveFieldInPoolTest 38 * @run main ClassFileInstaller sun.hotspot.WhiteBox 39 * sun.hotspot.WhiteBox$WhiteBoxPermission 40 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions 41 * -XX:+WhiteBoxAPI -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI 42 * compiler.jvmci.compilerToVM.ResolveFieldInPoolTest 43 */ 44 45 package compiler.jvmci.compilerToVM; 46 47 import compiler.jvmci.compilerToVM.ConstantPoolTestsHelper.DummyClasses; 48 import compiler.jvmci.compilerToVM.ConstantPoolTestCase.ConstantTypes; 49 import static compiler.jvmci.compilerToVM.ConstantPoolTestCase.ConstantTypes.*; 50 import compiler.jvmci.compilerToVM.ConstantPoolTestCase.TestedCPEntry; 51 import compiler.jvmci.compilerToVM.ConstantPoolTestCase.Validator; 52 import java.lang.reflect.Field; 53 import java.util.HashMap; 54 import java.util.Map; 55 import jdk.internal.org.objectweb.asm.Opcodes; 56 import jdk.vm.ci.hotspot.CompilerToVMHelper; 57 import jdk.vm.ci.hotspot.HotSpotResolvedObjectType; 58 import jdk.vm.ci.meta.ConstantPool; 59 import jdk.test.lib.Asserts; 60 import jdk.test.lib.Utils; 61 import sun.misc.Unsafe; 62 63 /** 64 * Test for {@code jdk.vm.ci.hotspot.CompilerToVM.resolveFieldInPool} method 65 */ 66 public class ResolveFieldInPoolTest { 67 68 private static final Unsafe UNSAFE = Utils.getUnsafe(); 69 70 public static void main(String[] args) throws Exception { 71 Map<ConstantTypes, Validator> typeTests = new HashMap<>(); 72 typeTests.put(CONSTANT_FIELDREF, ResolveFieldInPoolTest::validate); 73 ConstantPoolTestCase testCase = new ConstantPoolTestCase(typeTests); 74 testCase.test(); 75 // The next "Class.forName" and repeating "testCase.test()" 76 // are here for the following reason. 77 // The first test run is without dummy class initialization, 78 // which means no constant pool cache exists. 79 // The second run is with initialized class (with constant pool cache available). 80 // Some CompilerToVM methods require different input 81 // depending on whether CP cache exists or not. 82 for (DummyClasses dummy : DummyClasses.values()) { 83 Class.forName(dummy.klass.getName()); 84 } 85 testCase.test(); 86 } 87 88 private static void validate(ConstantPool constantPoolCTVM, 89 ConstantTypes cpType, 90 DummyClasses dummyClass, 91 int cpi) { 92 TestedCPEntry entry = cpType.getTestedCPEntry(dummyClass, cpi); 93 if (entry == null) { 94 return; 95 } 96 int index = cpi; 97 String cached = ""; 98 int cpci = dummyClass.getCPCacheIndex(cpi); 99 if (cpci != ConstantPoolTestsHelper.NO_CP_CACHE_PRESENT) { 100 index = cpci; 101 cached = "cached "; 102 } 103 for (int j = 0; j < entry.opcodes.length; j++) { 104 long[] info = new long[2]; 105 HotSpotResolvedObjectType fieldToVerify 106 = CompilerToVMHelper.resolveFieldInPool(constantPoolCTVM, 107 index, 108 entry.opcodes[j], 109 info); 110 String msg = String.format("Object returned by resolveFieldInPool method" 111 + " for %sindex %d should not be null", 112 cached, 113 index); 114 Asserts.assertNotNull(fieldToVerify, msg); 115 String classNameToRefer = entry.klass; 116 String fieldToVerifyKlassToString = fieldToVerify.klass().toValueString(); 117 if (!fieldToVerifyKlassToString.contains(classNameToRefer)) { 118 msg = String.format("String representation \"%s\" of the object" 119 + " returned by resolveFieldInPool method" 120 + " for index %d does not contain a field's class name," 121 + " should contain %s", 122 fieldToVerifyKlassToString, 123 index, 124 classNameToRefer); 125 throw new AssertionError(msg); 126 } 127 msg = String.format("Access flags returned by resolveFieldInPool" 128 + " method are wrong for the field %s.%s" 129 + " at %sindex %d", 130 entry.klass, 131 entry.name, 132 cached, 133 index); 134 Asserts.assertEQ(info[0], entry.accFlags, msg); 135 if (cpci == -1) { 136 return; 137 } 138 Class classOfTheField = null; 139 Field fieldToRefer = null; 140 try { 141 classOfTheField = Class.forName(classNameToRefer.replaceAll("/", "\\.")); 142 fieldToRefer = classOfTheField.getDeclaredField(entry.name); 143 fieldToRefer.setAccessible(true); 144 } catch (Exception ex) { 145 throw new Error("Unexpected exception", ex); 146 } 147 long offsetToRefer; 148 if ((entry.accFlags & Opcodes.ACC_STATIC) != 0) { 149 offsetToRefer = UNSAFE.staticFieldOffset(fieldToRefer); 150 } else { 151 offsetToRefer = UNSAFE.objectFieldOffset(fieldToRefer); 152 } 153 msg = String.format("Field offset returned by resolveFieldInPool" 154 + " method is wrong for the field %s.%s" 155 + " at %sindex %d", 156 entry.klass, 157 entry.name, 158 cached, 159 index); 160 Asserts.assertEQ(info[1], offsetToRefer, msg); 161 } 162 } 163 }