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