Path: blob/master/test/hotspot/jtreg/compiler/jsr292/MHInlineTest.java
41149 views
/*1* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 806228026* @summary C2: inlining failure due to access checks being too strict27*28* @requires vm.flagless29* @modules java.base/jdk.internal.misc30* @library /test/lib /31*32* @run driver compiler.jsr292.MHInlineTest33*/3435package compiler.jsr292;3637import jdk.test.lib.process.OutputAnalyzer;38import jdk.test.lib.process.ProcessTools;39import jtreg.SkippedException;4041import java.lang.invoke.MethodHandle;42import java.lang.invoke.MethodHandles;43import java.lang.invoke.MethodType;4445import static jdk.test.lib.Asserts.assertEquals;4647public class MHInlineTest {48public static void main(String[] args) throws Exception {49ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(50"-XX:+IgnoreUnrecognizedVMOptions", "-showversion",51"-XX:-TieredCompilation", "-Xbatch",52"-XX:+PrintCompilation", "-XX:+UnlockDiagnosticVMOptions", "-XX:+PrintInlining",53"-XX:CompileCommand=dontinline,compiler.jsr292.MHInlineTest::test*",54Launcher.class.getName());5556OutputAnalyzer analyzer = new OutputAnalyzer(pb.start());5758analyzer.shouldHaveExitValue(0);5960// The test is applicable only to C2 (present in Server VM).61if (analyzer.getStderr().contains("Server VM")) {62analyzer.shouldContain("compiler.jsr292.MHInlineTest$B::public_x (3 bytes) inline (hot)");63analyzer.shouldContain("compiler.jsr292.MHInlineTest$B::protected_x (3 bytes) inline (hot)");64analyzer.shouldContain("compiler.jsr292.MHInlineTest$B::package_x (3 bytes) inline (hot)");65analyzer.shouldContain("compiler.jsr292.MHInlineTest$A::package_final_x (3 bytes) inline (hot)");66analyzer.shouldContain("compiler.jsr292.MHInlineTest$B::private_x (3 bytes) inline (hot)");67analyzer.shouldContain("compiler.jsr292.MHInlineTest$B::private_static_x (3 bytes) inline (hot)");68analyzer.shouldContain("compiler.jsr292.MHInlineTest$A::package_static_x (3 bytes) inline (hot)");69} else {70throw new SkippedException("The test is applicable only to C2 (present in Server VM)");71}72}7374static class A {75public static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();7677public Class<?> public_x() { return A.class; }78protected Class<?> protected_x() { return A.class; }79Class<?> package_x() { return A.class; }80final Class<?> package_final_x() { return A.class; }8182static Class<?> package_static_x() { return A.class; }83}8485static class B extends A {86public static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();8788@Override public Class<?> public_x() { return B.class; }89@Override protected Class<?> protected_x() { return B.class; }90@Override Class<?> package_x() { return B.class; }9192private Class<?> private_x() { return B.class; }93static Class<?> private_static_x() { return B.class; }94}9596static final MethodHandle A_PUBLIC_X;97static final MethodHandle A_PROTECTED_X;98static final MethodHandle A_PACKAGE_X;99static final MethodHandle A_PACKAGE_STATIC_X;100static final MethodHandle A_PACKAGE_FINAL_X;101102static final MethodHandle B_PRIVATE_X;103static final MethodHandle B_PRIVATE_STATIC_X;104105static {106try {107MethodHandles.Lookup LOOKUP = MethodHandles.lookup();108109A_PUBLIC_X = LOOKUP.findVirtual(110A.class, "public_x", MethodType.methodType(Class.class));111A_PROTECTED_X = LOOKUP.findVirtual(112A.class, "protected_x", MethodType.methodType(Class.class));113A_PACKAGE_X = LOOKUP.findVirtual(114A.class, "package_x", MethodType.methodType(Class.class));115A_PACKAGE_FINAL_X = LOOKUP.findVirtual(116A.class, "package_final_x", MethodType.methodType(Class.class));117A_PACKAGE_STATIC_X = LOOKUP.findStatic(118A.class, "package_static_x", MethodType.methodType(Class.class));119120B_PRIVATE_X = B.LOOKUP.findVirtual(121B.class, "private_x", MethodType.methodType(Class.class));122B_PRIVATE_STATIC_X = B.LOOKUP.findStatic(123B.class, "private_static_x", MethodType.methodType(Class.class));124} catch (Exception e) {125throw new Error(e);126}127}128129static final A a = new B();130131private static void testPublicMH() {132try {133Class<?> r = (Class<?>)A_PUBLIC_X.invokeExact(a);134assertEquals(r, B.class);135} catch (Throwable throwable) {136throw new Error(throwable);137}138}139140private static void testProtectedMH() {141try {142Class<?> r = (Class<?>)A_PROTECTED_X.invokeExact(a);143assertEquals(r, B.class);144} catch (Throwable throwable) {145throw new Error(throwable);146}147}148149private static void testPackageMH() {150try {151Class<?> r = (Class<?>)A_PACKAGE_X.invokeExact(a);152assertEquals(r, B.class);153} catch (Throwable throwable) {154throw new Error(throwable);155}156}157158private static void testPackageFinalMH() {159try {160Class<?> r = (Class<?>)A_PACKAGE_FINAL_X.invokeExact(a);161assertEquals(r, A.class);162} catch (Throwable throwable) {163throw new Error(throwable);164}165}166167private static void testPackageStaticMH() {168try {169Class<?> r = (Class<?>)A_PACKAGE_STATIC_X.invokeExact();170assertEquals(r, A.class);171} catch (Throwable throwable) {172throw new Error(throwable);173}174}175176private static void testPrivateMH() {177try {178Class<?> r = (Class<?>)B_PRIVATE_X.invokeExact((B)a);179assertEquals(r, B.class);180} catch (Throwable throwable) {181throw new Error(throwable);182}183}184185private static void testPrivateStaticMH() {186try {187Class<?> r = (Class<?>)B_PRIVATE_STATIC_X.invokeExact();188assertEquals(r, B.class);189} catch (Throwable throwable) {190throw new Error(throwable);191}192}193194static class Launcher {195public static void main(String[] args) throws Exception {196for (int i = 0; i < 20_000; i++) {197testPublicMH();198}199for (int i = 0; i < 20_000; i++) {200testProtectedMH();201}202for (int i = 0; i < 20_000; i++) {203testPackageMH();204}205for (int i = 0; i < 20_000; i++) {206testPackageFinalMH();207}208for (int i = 0; i < 20_000; i++) {209testPackageStaticMH();210}211for (int i = 0; i < 20_000; i++) {212testPrivateMH();213}214for (int i = 0; i < 20_000; i++) {215testPrivateStaticMH();216}217}218}219}220221222