Path: blob/master/test/hotspot/jtreg/compiler/jvmci/compilerToVM/GetResolvedJavaMethodTest.java
41153 views
/*1* Copyright (c) 2015, 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 813642126* @requires vm.jvmci27* @library / /test/lib28* @library ../common/patches29* @ignore 824962130* @modules java.base/jdk.internal.misc:+open31* @modules jdk.internal.vm.ci/jdk.vm.ci.hotspot:+open32*33* @build jdk.internal.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper34* jdk.internal.vm.ci/jdk.vm.ci.hotspot.PublicMetaspaceWrapperObject35* sun.hotspot.WhiteBox36* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox37* @run main/othervm -Xbootclasspath/a:.38* -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI39* -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI40* -XX:-UseJVMCICompiler41* compiler.jvmci.compilerToVM.GetResolvedJavaMethodTest42*/4344package compiler.jvmci.compilerToVM;4546import jdk.internal.misc.Unsafe;47import jdk.test.lib.Asserts;48import jdk.vm.ci.hotspot.CompilerToVMHelper;49import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;50import jdk.vm.ci.hotspot.PublicMetaspaceWrapperObject;51import sun.hotspot.WhiteBox;5253import java.lang.reflect.Field;54import java.lang.reflect.Method;5556public class GetResolvedJavaMethodTest {57private static enum TestCase {58NULL_BASE {59@Override60HotSpotResolvedJavaMethod getResolvedJavaMethod() {61return CompilerToVMHelper.getResolvedJavaMethod(62null, getPtrToMethod());63}64},65JAVA_METHOD_BASE {66@Override67HotSpotResolvedJavaMethod getResolvedJavaMethod() {68HotSpotResolvedJavaMethod methodInstance = TEST_METHOD;69try {70METASPACE_METHOD_FIELD.set(methodInstance,71getPtrToMethod());72} catch (ReflectiveOperationException e) {73throw new Error("TEST BUG : " + e, e);74}75return CompilerToVMHelper.getResolvedJavaMethod(76methodInstance, 0L);77}78},79JAVA_METHOD_BASE_IN_TWO {80@Override81HotSpotResolvedJavaMethod getResolvedJavaMethod() {82long ptr = getPtrToMethod();83HotSpotResolvedJavaMethod methodInstance = TEST_METHOD;84try {85METASPACE_METHOD_FIELD.set(methodInstance, ptr / 2L);86} catch (ReflectiveOperationException e) {87throw new Error("TESTBUG : " + e, e);88}89return CompilerToVMHelper.getResolvedJavaMethod(methodInstance,90ptr - ptr / 2L);91}92},93JAVA_METHOD_BASE_ZERO {94@Override95HotSpotResolvedJavaMethod getResolvedJavaMethod() {96long ptr = getPtrToMethod();97HotSpotResolvedJavaMethod methodInstance = TEST_METHOD;98try {99METASPACE_METHOD_FIELD.set(methodInstance, 0L);100} catch (ReflectiveOperationException e) {101throw new Error("TESTBUG : " + e, e);102}103return CompilerToVMHelper.getResolvedJavaMethod(methodInstance,104ptr);105}106}107;108abstract HotSpotResolvedJavaMethod getResolvedJavaMethod();109}110111private static final Unsafe UNSAFE = Unsafe.getUnsafe();112private static final WhiteBox WB = WhiteBox.getWhiteBox();113private static final Field METASPACE_METHOD_FIELD;114private static final Class<?> TEST_CLASS = GetResolvedJavaMethodTest.class;115private static final HotSpotResolvedJavaMethod TEST_METHOD;116private static final long PTR;117static {118try {119Method method = TEST_CLASS.getDeclaredMethod("test", TestCase.class);120TEST_METHOD = CompilerToVMHelper.asResolvedJavaMethod(method);121} catch (NoSuchMethodException e) {122throw new Error("TESTBUG : " + e, e);123}124try {125// jdk.vm.ci.hotspot.HotSpotResolvedJavaMethodImpl.metaspaceMethod126METASPACE_METHOD_FIELD = TEST_METHOD.getClass()127.getDeclaredField("metaspaceMethod");128METASPACE_METHOD_FIELD.setAccessible(true);129PTR = (long) METASPACE_METHOD_FIELD.get(TEST_METHOD);130} catch (ReflectiveOperationException e) {131throw new Error("TESTBUG : " + e, e);132}133134}135136private static long getPtrToMethod() {137Field field;138try {139field = TEST_CLASS.getDeclaredField("PTR");140} catch (NoSuchFieldException e) {141throw new Error("TEST BUG : " + e, e);142}143Object base = UNSAFE.staticFieldBase(field);144return WB.getObjectAddress(base) + UNSAFE.staticFieldOffset(field);145}146147public void test(TestCase testCase) {148System.out.println(testCase.name());149HotSpotResolvedJavaMethod result = testCase.getResolvedJavaMethod();150Asserts.assertNotNull(result, testCase + " : got null");151Asserts.assertEQ(TEST_CLASS,152CompilerToVMHelper.getMirror(result.getDeclaringClass()),153testCase + " : unexpected declaring class");154}155156public static void main(String[] args) {157GetResolvedJavaMethodTest test = new GetResolvedJavaMethodTest();158for (TestCase testCase : TestCase.values()) {159test.test(testCase);160}161testObjectBase();162testMetaspaceWrapperBase();163}164165private static void testMetaspaceWrapperBase() {166try {167HotSpotResolvedJavaMethod method168= CompilerToVMHelper.getResolvedJavaMethod(169new PublicMetaspaceWrapperObject() {170@Override171public long getMetaspacePointer() {172return getPtrToMethod();173}174}, 0L);175throw new AssertionError("Test METASPACE_WRAPPER_BASE."176+ " Expected IllegalArgumentException has not been caught");177} catch (IllegalArgumentException e) {178// expected179}180}181182private static void testObjectBase() {183try {184HotSpotResolvedJavaMethod method185= CompilerToVMHelper.getResolvedJavaMethod(new Object(), 0L);186throw new AssertionError("Test OBJECT_BASE."187+ " Expected IllegalArgumentException has not been caught");188} catch (IllegalArgumentException e) {189// expected190}191}192}193194195