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 (os.simpleArch == "x64" | os.simpleArch == "sparcv9" | os.simpleArch == "aarch64")
  28  * @library / /testlibrary /test/lib
  29  * @library ../common/patches
  30  * @modules jdk.vm.ci.hotspot/jdk.vm.ci.hotspot
  31  * @build jdk.vm.ci.hotspot/jdk.vm.ci.hotspot.CompilerToVMHelper
  32  *        jdk.vm.ci.hotspot/jdk.vm.ci.hotspot.PublicMetaspaceWrapperObject
  33  * @build compiler.jvmci.compilerToVM.GetResolvedJavaMethodTest
  34  * @run main ClassFileInstaller
  35  *      sun.hotspot.WhiteBox
  36  *      sun.hotspot.WhiteBox$WhiteBoxPermission
  37  * @run main/othervm -Xbootclasspath/a:.
  38  *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  39  *                   -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  40  *                   compiler.jvmci.compilerToVM.GetResolvedJavaMethodTest
  41  */
  42 
  43 package compiler.jvmci.compilerToVM;
  44 
  45 import jdk.vm.ci.hotspot.CompilerToVMHelper;
  46 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
  47 import jdk.vm.ci.hotspot.PublicMetaspaceWrapperObject;
  48 import jdk.test.lib.Asserts;
  49 import jdk.test.lib.Utils;
  50 import sun.hotspot.WhiteBox;
  51 import sun.misc.Unsafe;
  52 
  53 import java.lang.reflect.Field;
  54 
  55 public class GetResolvedJavaMethodTest {
  56     private static enum TestCase {
  57         NULL_BASE {
  58             @Override
  59             HotSpotResolvedJavaMethod getResolvedJavaMethod() {
  60                 return CompilerToVMHelper.getResolvedJavaMethod(
  61                         null, getPtrToMethod());
  62             }
  63         },
  64         JAVA_METHOD_BASE {
  65             @Override
  66             HotSpotResolvedJavaMethod getResolvedJavaMethod() {
  67                 HotSpotResolvedJavaMethod methodInstance
  68                         = CompilerToVMHelper.getResolvedJavaMethodAtSlot(
  69                                 TEST_CLASS, 0);
  70                 try {
  71                     METASPACE_METHOD_FIELD.set(methodInstance,
  72                             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             HotSpotResolvedJavaMethod getResolvedJavaMethod() {
  83                 long ptr = getPtrToMethod();
  84                 HotSpotResolvedJavaMethod methodInstance
  85                         = CompilerToVMHelper.getResolvedJavaMethodAtSlot(
  86                         TEST_CLASS, 0);
  87                 try {
  88                     METASPACE_METHOD_FIELD.set(methodInstance, ptr / 2L);
  89                 } catch (ReflectiveOperationException e) {
  90                     throw new Error("TESTBUG : " + e, e);
  91                 }
  92                 return CompilerToVMHelper.getResolvedJavaMethod(methodInstance,
  93                         ptr - ptr / 2L);
  94             }
  95         },
  96         JAVA_METHOD_BASE_ZERO {
  97             @Override
  98             HotSpotResolvedJavaMethod getResolvedJavaMethod() {
  99                 long ptr = getPtrToMethod();
 100                 HotSpotResolvedJavaMethod methodInstance
 101                         = CompilerToVMHelper.getResolvedJavaMethodAtSlot(
 102                         TEST_CLASS, 0);
 103                 try {
 104                     METASPACE_METHOD_FIELD.set(methodInstance, 0L);
 105                 } catch (ReflectiveOperationException e) {
 106                     throw new Error("TESTBUG : " + e, e);
 107                 }
 108                 return CompilerToVMHelper.getResolvedJavaMethod(methodInstance,
 109                         ptr);
 110             }
 111         }
 112         ;
 113         abstract HotSpotResolvedJavaMethod getResolvedJavaMethod();
 114     }
 115 
 116     private static final Unsafe UNSAFE = Utils.getUnsafe();
 117     private static final WhiteBox WB = WhiteBox.getWhiteBox();
 118     private static final Field METASPACE_METHOD_FIELD;
 119     private static final Class<?> TEST_CLASS = GetResolvedJavaMethodTest.class;
 120     private static final long PTR;
 121     static  {
 122         HotSpotResolvedJavaMethod method
 123                 = CompilerToVMHelper.getResolvedJavaMethodAtSlot(TEST_CLASS, 0);
 124         try {
 125             // jdk.vm.ci.hotspot.HotSpotResolvedJavaMethodImpl.metaspaceMethod
 126             METASPACE_METHOD_FIELD = method.getClass()
 127                     .getDeclaredField("metaspaceMethod");
 128             METASPACE_METHOD_FIELD.setAccessible(true);
 129             PTR = (long) METASPACE_METHOD_FIELD.get(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 }