GetResolvedJavaMethodTest.java revision 12290:8953c0318163
1/*
2 * Copyright (c) 2015, 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 8136421
27 * @requires (vm.simpleArch == "x64" | vm.simpleArch == "sparcv9" | vm.simpleArch == "aarch64")
28 * @library / /test/lib
29 * @library ../common/patches
30 * @modules java.base/jdk.internal.misc:+open
31 * @modules jdk.vm.ci/jdk.vm.ci.hotspot:+open
32 *
33 * @build jdk.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper
34 *        jdk.vm.ci/jdk.vm.ci.hotspot.PublicMetaspaceWrapperObject
35 *        sun.hotspot.WhiteBox
36 * @run driver ClassFileInstaller sun.hotspot.WhiteBox
37 *                                sun.hotspot.WhiteBox$WhiteBoxPermission
38 * @run main/othervm -Xbootclasspath/a:.
39 *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
40 *                   -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
41 *                   compiler.jvmci.compilerToVM.GetResolvedJavaMethodTest
42 */
43
44package compiler.jvmci.compilerToVM;
45
46import 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;
52
53import java.lang.reflect.Field;
54import java.lang.reflect.Method;
55
56public class GetResolvedJavaMethodTest {
57    private static enum TestCase {
58        NULL_BASE {
59            @Override
60            HotSpotResolvedJavaMethod getResolvedJavaMethod() {
61                return CompilerToVMHelper.getResolvedJavaMethod(
62                        null, getPtrToMethod());
63            }
64        },
65        JAVA_METHOD_BASE {
66            @Override
67            HotSpotResolvedJavaMethod getResolvedJavaMethod() {
68                HotSpotResolvedJavaMethod methodInstance = TEST_METHOD;
69                try {
70                    METASPACE_METHOD_FIELD.set(methodInstance,
71                            getPtrToMethod());
72                } catch (ReflectiveOperationException e) {
73                    throw new Error("TEST BUG : " + e, e);
74                }
75                return CompilerToVMHelper.getResolvedJavaMethod(
76                        methodInstance, 0L);
77            }
78        },
79        JAVA_METHOD_BASE_IN_TWO {
80            @Override
81            HotSpotResolvedJavaMethod getResolvedJavaMethod() {
82                long ptr = getPtrToMethod();
83                HotSpotResolvedJavaMethod methodInstance = TEST_METHOD;
84                try {
85                    METASPACE_METHOD_FIELD.set(methodInstance, ptr / 2L);
86                } catch (ReflectiveOperationException e) {
87                    throw new Error("TESTBUG : " + e, e);
88                }
89                return CompilerToVMHelper.getResolvedJavaMethod(methodInstance,
90                        ptr - ptr / 2L);
91            }
92        },
93        JAVA_METHOD_BASE_ZERO {
94            @Override
95            HotSpotResolvedJavaMethod getResolvedJavaMethod() {
96                long ptr = getPtrToMethod();
97                HotSpotResolvedJavaMethod methodInstance = TEST_METHOD;
98                try {
99                    METASPACE_METHOD_FIELD.set(methodInstance, 0L);
100                } catch (ReflectiveOperationException e) {
101                    throw new Error("TESTBUG : " + e, e);
102                }
103                return CompilerToVMHelper.getResolvedJavaMethod(methodInstance,
104                        ptr);
105            }
106        }
107        ;
108        abstract HotSpotResolvedJavaMethod getResolvedJavaMethod();
109    }
110
111    private static final Unsafe UNSAFE = Unsafe.getUnsafe();
112    private static final WhiteBox WB = WhiteBox.getWhiteBox();
113    private static final Field METASPACE_METHOD_FIELD;
114    private static final Class<?> TEST_CLASS = GetResolvedJavaMethodTest.class;
115    private static final HotSpotResolvedJavaMethod TEST_METHOD;
116    private static final long PTR;
117    static  {
118        try {
119            Method method = TEST_CLASS.getDeclaredMethod("test", TestCase.class);
120            TEST_METHOD = CompilerToVMHelper.asResolvedJavaMethod(method);
121        } catch (NoSuchMethodException e) {
122            throw new Error("TESTBUG : " + e, e);
123        }
124        try {
125            // jdk.vm.ci.hotspot.HotSpotResolvedJavaMethodImpl.metaspaceMethod
126            METASPACE_METHOD_FIELD = TEST_METHOD.getClass()
127                    .getDeclaredField("metaspaceMethod");
128            METASPACE_METHOD_FIELD.setAccessible(true);
129            PTR = (long) METASPACE_METHOD_FIELD.get(TEST_METHOD);
130        } catch (ReflectiveOperationException e) {
131            throw new Error("TESTBUG : " + e, e);
132        }
133
134    }
135
136    private static long getPtrToMethod() {
137        Field field;
138        try {
139            field = TEST_CLASS.getDeclaredField("PTR");
140        } catch (NoSuchFieldException e) {
141            throw new Error("TEST BUG : " + e, e);
142        }
143        Object base = UNSAFE.staticFieldBase(field);
144        return WB.getObjectAddress(base) + UNSAFE.staticFieldOffset(field);
145    }
146
147    public void test(TestCase testCase) {
148        System.out.println(testCase.name());
149        HotSpotResolvedJavaMethod result = testCase.getResolvedJavaMethod();
150        Asserts.assertNotNull(result, testCase + " : got null");
151        Asserts.assertEQ(TEST_CLASS,
152                CompilerToVMHelper.getMirror(result.getDeclaringClass()),
153                testCase + " : unexpected declaring class");
154    }
155
156    public static void main(String[] args) {
157        GetResolvedJavaMethodTest test = new GetResolvedJavaMethodTest();
158        for (TestCase testCase : TestCase.values()) {
159            test.test(testCase);
160        }
161        testObjectBase();
162        testMetaspaceWrapperBase();
163    }
164
165    private static void testMetaspaceWrapperBase() {
166        try {
167            HotSpotResolvedJavaMethod method
168                    = CompilerToVMHelper.getResolvedJavaMethod(
169                            new PublicMetaspaceWrapperObject() {
170                                @Override
171                                public long getMetaspacePointer() {
172                                    return getPtrToMethod();
173                                }
174                            }, 0L);
175            throw new AssertionError("Test METASPACE_WRAPPER_BASE."
176                    + " Expected IllegalArgumentException has not been caught");
177        } catch (IllegalArgumentException e) {
178            // expected
179        }
180    }
181
182    private static void testObjectBase() {
183        try {
184            HotSpotResolvedJavaMethod method
185                    = CompilerToVMHelper.getResolvedJavaMethod(new Object(), 0L);
186            throw new AssertionError("Test OBJECT_BASE."
187                + " Expected IllegalArgumentException has not been caught");
188        } catch (IllegalArgumentException e) {
189            // expected
190        }
191    }
192}
193