Path: blob/master/test/hotspot/jtreg/compiler/jvmci/compilerToVM/ResolveFieldInPoolTest.java
41153 views
/*1* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 813870826* @requires vm.jvmci27* @library /test/lib /28* @library ../common/patches29* @modules java.base/jdk.internal.access30* java.base/jdk.internal.misc31* java.base/jdk.internal.reflect32* java.base/jdk.internal.org.objectweb.asm33* java.base/jdk.internal.org.objectweb.asm.tree34* jdk.internal.vm.ci/jdk.vm.ci.hotspot35* jdk.internal.vm.ci/jdk.vm.ci.meta36* jdk.internal.vm.ci/jdk.vm.ci.runtime37*38* @build jdk.internal.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper sun.hotspot.WhiteBox39* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox40* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions41* -XX:+WhiteBoxAPI -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI42* -XX:-UseJVMCICompiler43* compiler.jvmci.compilerToVM.ResolveFieldInPoolTest44*/4546package compiler.jvmci.compilerToVM;4748import compiler.jvmci.compilerToVM.ConstantPoolTestCase.ConstantTypes;49import compiler.jvmci.compilerToVM.ConstantPoolTestCase.TestedCPEntry;50import compiler.jvmci.compilerToVM.ConstantPoolTestCase.Validator;51import compiler.jvmci.compilerToVM.ConstantPoolTestsHelper.DummyClasses;52import jdk.internal.misc.Unsafe;53import jdk.internal.org.objectweb.asm.Opcodes;54import jdk.test.lib.Asserts;55import jdk.vm.ci.hotspot.CompilerToVMHelper;56import jdk.vm.ci.hotspot.HotSpotResolvedObjectType;57import jdk.vm.ci.meta.ConstantPool;5859import java.lang.reflect.Field;60import java.util.HashMap;61import java.util.Map;6263import static compiler.jvmci.compilerToVM.ConstantPoolTestCase.ConstantTypes.CONSTANT_FIELDREF;6465/**66* Test for {@code jdk.vm.ci.hotspot.CompilerToVM.resolveFieldInPool} method67*/68public class ResolveFieldInPoolTest {6970private static final Unsafe UNSAFE = Unsafe.getUnsafe();7172public static void main(String[] args) throws Exception {73Map<ConstantTypes, Validator> typeTests = new HashMap<>();74typeTests.put(CONSTANT_FIELDREF, ResolveFieldInPoolTest::validate);75ConstantPoolTestCase testCase = new ConstantPoolTestCase(typeTests);76testCase.test();77// The next "Class.forName" and repeating "testCase.test()"78// are here for the following reason.79// The first test run is without dummy class initialization,80// which means no constant pool cache exists.81// The second run is with initialized class (with constant pool cache available).82// Some CompilerToVM methods require different input83// depending on whether CP cache exists or not.84for (DummyClasses dummy : DummyClasses.values()) {85Class.forName(dummy.klass.getName());86}87testCase.test();88}8990private static void validate(ConstantPool constantPoolCTVM,91ConstantTypes cpType,92DummyClasses dummyClass,93int cpi) {94TestedCPEntry entry = cpType.getTestedCPEntry(dummyClass, cpi);95if (entry == null) {96return;97}98int index = cpi;99String cached = "";100int cpci = dummyClass.getCPCacheIndex(cpi);101if (cpci != ConstantPoolTestsHelper.NO_CP_CACHE_PRESENT) {102index = cpci;103cached = "cached ";104}105for (int j = 0; j < entry.opcodes.length; j++) {106int[] info = new int[3];107HotSpotResolvedObjectType fieldToVerify108= CompilerToVMHelper.resolveFieldInPool(constantPoolCTVM,109index,110entry.methods == null ? null : entry.methods[j],111entry.opcodes[j],112info);113String msg = String.format("Object returned by resolveFieldInPool method"114+ " for %sindex %d should not be null",115cached,116index);117Asserts.assertNotNull(fieldToVerify, msg);118String classNameToRefer = entry.klass;119String fieldToVerifyKlassToString = fieldToVerify.klass().toValueString();120if (!fieldToVerifyKlassToString.contains(classNameToRefer)) {121msg = String.format("String representation \"%s\" of the object"122+ " returned by resolveFieldInPool method"123+ " for index %d does not contain a field's class name,"124+ " should contain %s",125fieldToVerifyKlassToString,126index,127classNameToRefer);128throw new AssertionError(msg);129}130msg = String.format("Access flags returned by resolveFieldInPool"131+ " method are wrong for the field %s.%s"132+ " at %sindex %d",133entry.klass,134entry.name,135cached,136index);137Asserts.assertEQ(info[0], entry.accFlags, msg);138if (cpci == -1) {139return;140}141Class classOfTheField = null;142Field fieldToRefer = null;143try {144classOfTheField = Class.forName(classNameToRefer.replaceAll("/", "\\."));145fieldToRefer = classOfTheField.getDeclaredField(entry.name);146fieldToRefer.setAccessible(true);147} catch (Exception ex) {148throw new Error("Unexpected exception", ex);149}150int offsetToRefer;151if ((entry.accFlags & Opcodes.ACC_STATIC) != 0) {152offsetToRefer = (int) UNSAFE.staticFieldOffset(fieldToRefer);153} else {154offsetToRefer = (int) UNSAFE.objectFieldOffset(fieldToRefer);155}156msg = String.format("Field offset returned by resolveFieldInPool"157+ " method is wrong for the field %s.%s"158+ " at %sindex %d",159entry.klass,160entry.name,161cached,162index);163Asserts.assertEQ(info[1], offsetToRefer, msg);164}165}166}167168169