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