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