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