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