1 /*
  2  * Copyright (c) 2021, 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 8275868
 27  * @library / /test/lib
 28  * @summary Testing that ciReplay inlining does not fail with unresolved signature classes.
 29  * @requires vm.flightRecorder != true & vm.compMode != "Xint" & vm.compMode != "Xcomp" & vm.debug == true & vm.compiler2.enabled
 30  * @modules java.base/jdk.internal.misc
 31  * @run driver compiler.ciReplay.TestInliningProtectionDomain
 32  */
 33 
 34 package compiler.ciReplay;
 35 
 36 import jdk.test.lib.Asserts;
 37 
 38 import java.util.List;
 39 
 40 public class TestInliningProtectionDomain extends InliningBase {
 41 
 42     public static void main(String[] args) {
 43         new TestInliningProtectionDomain(ProtectionDomainTestCompiledBefore.class, true);
 44         new TestInliningProtectionDomain(ProtectionDomainTestNoOtherCompilationPublic.class, false);
 45         new TestInliningProtectionDomain(ProtectionDomainTestNoOtherCompilationPrivate.class, false);
 46         new TestInliningProtectionDomain(ProtectionDomainTestNoOtherCompilationPrivateString.class, false);
 47     }
 48 
 49     public TestInliningProtectionDomain(Class<?> testClass, boolean compileBar) {
 50         super(testClass);
 51         if (compileBar) {
 52             commandLineNormal.add("-XX:CompileCommand=compileonly," + testClass.getName() + "::bar");
 53         }
 54         runTest();
 55     }
 56 
 57     @Override
 58     public void testAction() {
 59         positiveTest(commandLineReplay);
 60         String entryString = getTestClass() + " " + "test";
 61         boolean inlineFails = testClass == ProtectionDomainTestNoOtherCompilationPrivate.class;
 62         int inlineeCount = inlineFails ? 1 : 5;
 63 
 64         List<InlineEntry> inlineesNormal = parseLogFile(LOG_FILE_NORMAL, entryString, "compile_id='" + getCompileIdFromFile(getReplayFileName()), inlineeCount);
 65         List<InlineEntry> inlineesReplay = parseLogFile(LOG_FILE_REPLAY, entryString, "test ()V", inlineeCount);
 66         verifyLists(inlineesNormal, inlineesReplay, inlineeCount);
 67 
 68         if (inlineFails) {
 69             Asserts.assertTrue(inlineesNormal.get(0).compare("compiler.ciReplay.ProtectionDomainTestNoOtherCompilationPrivate", "bar", inlineesNormal.get(0).isUnloadedSignatureClasses()));
 70             Asserts.assertTrue(inlineesReplay.get(0).compare("compiler.ciReplay.ProtectionDomainTestNoOtherCompilationPrivate", "bar", inlineesReplay.get(0).isDisallowedByReplay()));
 71         } else {
 72             Asserts.assertTrue(inlineesNormal.get(4).compare("compiler.ciReplay.InliningBar", "bar2", inlineesNormal.get(4).isNormalInline()));
 73             Asserts.assertTrue(inlineesReplay.get(4).compare("compiler.ciReplay.InliningBar", "bar2", inlineesReplay.get(4).isForcedByReplay() || inlineesReplay.get(4).isForcedIncrementalInlineByReplay()));
 74         }
 75     }
 76 }
 77 
 78 class ProtectionDomainTestCompiledBefore {
 79     public static void main(String[] args) {
 80         for (int i = 0; i < 10000; i++) {
 81             bar(); // Ensure that bar() was compiled
 82         }
 83         for (int i = 0; i < 10000; i++) {
 84             test();
 85         }
 86     }
 87 
 88     public static void test() {
 89         bar();
 90     }
 91 
 92     // Integer should be resolved for the protection domain of this class because the separate compilation of bar() in
 93     // the normal run will resolve all classes in the signature. Inlining succeeds.
 94     private static Integer bar() {
 95         InliningFoo.foo();
 96         return null;
 97     }
 98 }
 99 
100 class ProtectionDomainTestNoOtherCompilationPublic {
101     public static void main(String[] args) {
102         for (int i = 0; i < 10000; i++) {
103             test();
104         }
105     }
106 
107     public static void test() {
108         bar(); // Not compiled before separately
109     }
110 
111     // Integer should be resolved for the protection domain of this class because getDeclaredMethods is called in normal run
112     // when validating main() method. In this process, all public methods of this class are visited and its signature classes
113     // are resolved. Inlining of bar() succeeds.
114     public static Integer bar() {
115         InliningFoo.foo();
116         return null;
117     }
118 }
119 
120 class ProtectionDomainTestNoOtherCompilationPrivate {
121     public static void main(String[] args) {
122         for (int i = 0; i < 10000; i++) {
123             test();
124         }
125     }
126 
127     public static void test() {
128         bar(); // Not compiled before separately
129     }
130 
131     // Integer should be unresolved for the protection domain of this class even though getDeclaredMethods is called in normal
132     // run when validating main() method. In this process, only public methods of this class are visited and its signature
133     // classes are resolved. Since this method is private, the signature classes are not resolved for this protection domain.
134     // Inlining of bar() should fail in normal run with "unresolved signature classes". Therefore, replay compilation should
135     // also not inline bar().
136     private static Integer bar() {
137         InliningFoo.foo();
138         return null;
139     }
140 }
141 
142 class ProtectionDomainTestNoOtherCompilationPrivateString {
143     public static void main(String[] args) {
144         for (int i = 0; i < 10000; i++) {
145             test();
146         }
147     }
148 
149     public static void test() {
150         bar(); // Not compiled before separately
151     }
152 
153     // Integer should be resovled for the protection domain of this class because getDeclaredMethods is called in normal run
154     // when validating main() method. In this process, public methods of this class are visited and its signature classes
155     // are resolved. bar() is private and not visited in this process (i.e. no resolution of String). But since main()
156     // has String[] as parameter, the String class will be resolved for this protection domain. Inlining of bar() succeeds.
157     private static String bar() {
158         InliningFoo.foo();
159         return null;
160     }
161 }
162 
163 class InliningFoo {
164     public static void foo() {
165         foo2();
166     }
167 
168     private static void foo2() {
169         InliningBar.bar();
170     }
171 }
172 
173 
174 class InliningBar {
175     public static void bar() {
176         bar2();
177     }
178 
179     private static void bar2() {}
180 }