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 8062280
  27  * @summary C2: inlining failure due to access checks being too strict
  28  * @modules java.base/jdk.internal.misc
  29  * @library /testlibrary
  30  * @run main/othervm MHInlineTest
  31  */
  32 import java.lang.invoke.*;
  33 import jdk.test.lib.*;
  34 import static jdk.test.lib.Asserts.*;
  35 
  36 public class MHInlineTest {
  37     public static void main(String[] args) throws Exception {
  38         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  39                 "-XX:+IgnoreUnrecognizedVMOptions", "-showversion",
  40                 "-server", "-XX:-TieredCompilation", "-Xbatch",
  41                 "-XX:+PrintCompilation", "-XX:+UnlockDiagnosticVMOptions", "-XX:+PrintInlining",
  42                 "-XX:CompileCommand=dontinline,MHInlineTest::test*",
  43                     "MHInlineTest$Launcher");
  44 
  45         OutputAnalyzer analyzer = new OutputAnalyzer(pb.start());
  46 
  47         analyzer.shouldHaveExitValue(0);
  48 
  49         // The test is applicable only to C2 (present in Server VM).
  50         if (analyzer.getStderr().contains("Server VM")) {
  51             analyzer.shouldContain("MHInlineTest$B::public_x (3 bytes)   inline (hot)");
  52             analyzer.shouldContain("MHInlineTest$B::protected_x (3 bytes)   inline (hot)");
  53             analyzer.shouldContain("MHInlineTest$B::package_x (3 bytes)   inline (hot)");
  54             analyzer.shouldContain("MHInlineTest$A::package_final_x (3 bytes)   inline (hot)");
  55             analyzer.shouldContain("MHInlineTest$B::private_x (3 bytes)   inline (hot)");
  56             analyzer.shouldContain("MHInlineTest$B::private_static_x (3 bytes)   inline (hot)");
  57             analyzer.shouldContain("MHInlineTest$A::package_static_x (3 bytes)   inline (hot)");
  58         }
  59     }
  60 
  61     static class A {
  62         public static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
  63 
  64         public Class<?>         public_x() { return A.class; }
  65         protected Class<?>   protected_x() { return A.class; }
  66         Class<?>               package_x() { return A.class; }
  67         final Class<?>   package_final_x() { return A.class; }
  68 
  69         static Class<?> package_static_x() { return A.class; }
  70     }
  71 
  72     static class B extends A {
  73         public static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
  74 
  75         @Override public    Class<?>    public_x() { return B.class; }
  76         @Override protected Class<?> protected_x() { return B.class; }
  77         @Override Class<?>             package_x() { return B.class; }
  78 
  79         private   Class<?>             private_x() { return B.class; }
  80         static    Class<?>      private_static_x() { return B.class; }
  81     }
  82 
  83     static final MethodHandle A_PUBLIC_X;
  84     static final MethodHandle A_PROTECTED_X;
  85     static final MethodHandle A_PACKAGE_X;
  86     static final MethodHandle A_PACKAGE_STATIC_X;
  87     static final MethodHandle A_PACKAGE_FINAL_X;
  88 
  89     static final MethodHandle B_PRIVATE_X;
  90     static final MethodHandle B_PRIVATE_STATIC_X;
  91 
  92     static {
  93         try {
  94             MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
  95 
  96             A_PUBLIC_X = LOOKUP.findVirtual(
  97                     A.class, "public_x", MethodType.methodType(Class.class));
  98             A_PROTECTED_X = LOOKUP.findVirtual(
  99                     A.class, "protected_x", MethodType.methodType(Class.class));
 100             A_PACKAGE_X = LOOKUP.findVirtual(
 101                     A.class, "package_x", MethodType.methodType(Class.class));
 102             A_PACKAGE_FINAL_X = LOOKUP.findVirtual(
 103                     A.class, "package_final_x", MethodType.methodType(Class.class));
 104             A_PACKAGE_STATIC_X = LOOKUP.findStatic(
 105                     A.class, "package_static_x", MethodType.methodType(Class.class));
 106 
 107             B_PRIVATE_X = B.LOOKUP.findVirtual(
 108                     B.class, "private_x", MethodType.methodType(Class.class));
 109             B_PRIVATE_STATIC_X = B.LOOKUP.findStatic(
 110                     B.class, "private_static_x", MethodType.methodType(Class.class));
 111         } catch (Exception e) {
 112             throw new Error(e);
 113         }
 114     }
 115 
 116     static final A a = new B();
 117 
 118     private static void testPublicMH() {
 119         try {
 120             Class<?> r = (Class<?>)A_PUBLIC_X.invokeExact(a);
 121             assertEquals(r, B.class);
 122         } catch (Throwable throwable) {
 123             throw new Error(throwable);
 124         }
 125     }
 126 
 127     private static void testProtectedMH() {
 128         try {
 129             Class<?> r = (Class<?>)A_PROTECTED_X.invokeExact(a);
 130             assertEquals(r, B.class);
 131         } catch (Throwable throwable) {
 132             throw new Error(throwable);
 133         }
 134     }
 135 
 136     private static void testPackageMH() {
 137         try {
 138             Class<?> r = (Class<?>)A_PACKAGE_X.invokeExact(a);
 139             assertEquals(r, B.class);
 140         } catch (Throwable throwable) {
 141             throw new Error(throwable);
 142         }
 143     }
 144 
 145     private static void testPackageFinalMH() {
 146         try {
 147             Class<?> r = (Class<?>)A_PACKAGE_FINAL_X.invokeExact(a);
 148             assertEquals(r, A.class);
 149         } catch (Throwable throwable) {
 150             throw new Error(throwable);
 151         }
 152     }
 153 
 154     private static void testPackageStaticMH() {
 155         try {
 156             Class<?> r = (Class<?>)A_PACKAGE_STATIC_X.invokeExact();
 157             assertEquals(r, A.class);
 158         } catch (Throwable throwable) {
 159             throw new Error(throwable);
 160         }
 161     }
 162 
 163     private static void testPrivateMH() {
 164         try {
 165             Class<?> r = (Class<?>)B_PRIVATE_X.invokeExact((B)a);
 166             assertEquals(r, B.class);
 167         } catch (Throwable throwable) {
 168             throw new Error(throwable);
 169         }
 170     }
 171 
 172     private static void testPrivateStaticMH() {
 173         try {
 174             Class<?> r = (Class<?>)B_PRIVATE_STATIC_X.invokeExact();
 175             assertEquals(r, B.class);
 176         } catch (Throwable throwable) {
 177             throw new Error(throwable);
 178         }
 179     }
 180 
 181     static class Launcher {
 182         public static void main(String[] args) throws Exception {
 183             for (int i = 0; i < 20_000; i++) {
 184                 testPublicMH();
 185             }
 186             for (int i = 0; i < 20_000; i++) {
 187                 testProtectedMH();
 188             }
 189             for (int i = 0; i < 20_000; i++) {
 190                 testPackageMH();
 191             }
 192             for (int i = 0; i < 20_000; i++) {
 193                 testPackageFinalMH();
 194             }
 195             for (int i = 0; i < 20_000; i++) {
 196                 testPackageStaticMH();
 197             }
 198             for (int i = 0; i < 20_000; i++) {
 199                 testPrivateMH();
 200             }
 201             for (int i = 0; i < 20_000; i++) {
 202                 testPrivateStaticMH();
 203             }
 204         }
 205     }
 206 }