xref: /aosp_15_r20/art/test/616-cha-interface-default/src/Main.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2017 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker class Main1 implements Base {
18*795d594fSAndroid Build Coastguard Worker }
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker class Main2 extends Main1 {
foobar()21*795d594fSAndroid Build Coastguard Worker     public void foobar() {}
22*795d594fSAndroid Build Coastguard Worker }
23*795d594fSAndroid Build Coastguard Worker 
24*795d594fSAndroid Build Coastguard Worker class Main3 implements Base {
foo(int i)25*795d594fSAndroid Build Coastguard Worker     public int foo(int i) {
26*795d594fSAndroid Build Coastguard Worker         if (i != 3) {
27*795d594fSAndroid Build Coastguard Worker             printError("error3");
28*795d594fSAndroid Build Coastguard Worker         }
29*795d594fSAndroid Build Coastguard Worker         return -(i + 10);
30*795d594fSAndroid Build Coastguard Worker     }
31*795d594fSAndroid Build Coastguard Worker }
32*795d594fSAndroid Build Coastguard Worker 
33*795d594fSAndroid Build Coastguard Worker public class Main {
34*795d594fSAndroid Build Coastguard Worker     static Base sMain1;
35*795d594fSAndroid Build Coastguard Worker     static Base sMain2;
36*795d594fSAndroid Build Coastguard Worker     static Base sMain3;
37*795d594fSAndroid Build Coastguard Worker 
38*795d594fSAndroid Build Coastguard Worker     static boolean sIsOptimizing = true;
39*795d594fSAndroid Build Coastguard Worker     static boolean sHasJIT = true;
40*795d594fSAndroid Build Coastguard Worker     static volatile boolean sOtherThreadStarted;
41*795d594fSAndroid Build Coastguard Worker 
assertSingleImplementation(Class<?> clazz, String method_name, boolean b)42*795d594fSAndroid Build Coastguard Worker     private static void assertSingleImplementation(Class<?> clazz, String method_name, boolean b) {
43*795d594fSAndroid Build Coastguard Worker         if (hasSingleImplementation(clazz, method_name) != b) {
44*795d594fSAndroid Build Coastguard Worker             System.out.println(clazz + "." + method_name +
45*795d594fSAndroid Build Coastguard Worker                     " doesn't have single implementation value of " + b);
46*795d594fSAndroid Build Coastguard Worker         }
47*795d594fSAndroid Build Coastguard Worker     }
48*795d594fSAndroid Build Coastguard Worker 
getValue(Class<?> cls)49*795d594fSAndroid Build Coastguard Worker     static int getValue(Class<?> cls) {
50*795d594fSAndroid Build Coastguard Worker         if (cls == Main1.class || cls == Main2.class) {
51*795d594fSAndroid Build Coastguard Worker             return 1;
52*795d594fSAndroid Build Coastguard Worker         }
53*795d594fSAndroid Build Coastguard Worker         return 3;
54*795d594fSAndroid Build Coastguard Worker     }
55*795d594fSAndroid Build Coastguard Worker 
56*795d594fSAndroid Build Coastguard Worker     // sMain1.foo()/sMain2.foo() will be always be Base.foo() before Main3 is loaded/linked.
57*795d594fSAndroid Build Coastguard Worker     // So sMain1.foo() can be devirtualized to Base.foo() and be inlined.
58*795d594fSAndroid Build Coastguard Worker     // After Helper.createMain3() which links in Main3, live testImplement() on stack
59*795d594fSAndroid Build Coastguard Worker     // should be deoptimized.
testImplement(boolean createMain3, boolean wait, boolean setHasJIT)60*795d594fSAndroid Build Coastguard Worker     static void testImplement(boolean createMain3, boolean wait, boolean setHasJIT) {
61*795d594fSAndroid Build Coastguard Worker         if (setHasJIT) {
62*795d594fSAndroid Build Coastguard Worker             if (isInterpreted()) {
63*795d594fSAndroid Build Coastguard Worker                 sHasJIT = false;
64*795d594fSAndroid Build Coastguard Worker             }
65*795d594fSAndroid Build Coastguard Worker             return;
66*795d594fSAndroid Build Coastguard Worker         }
67*795d594fSAndroid Build Coastguard Worker 
68*795d594fSAndroid Build Coastguard Worker         if (createMain3 && (sIsOptimizing || sHasJIT)) {
69*795d594fSAndroid Build Coastguard Worker             assertIsManaged();
70*795d594fSAndroid Build Coastguard Worker         }
71*795d594fSAndroid Build Coastguard Worker 
72*795d594fSAndroid Build Coastguard Worker         if (sMain1.foo(getValue(sMain1.getClass())) != 11) {
73*795d594fSAndroid Build Coastguard Worker             System.out.println("11 expected.");
74*795d594fSAndroid Build Coastguard Worker         }
75*795d594fSAndroid Build Coastguard Worker         if (sMain1.$noinline$bar() != -1) {
76*795d594fSAndroid Build Coastguard Worker             System.out.println("-1 expected.");
77*795d594fSAndroid Build Coastguard Worker         }
78*795d594fSAndroid Build Coastguard Worker         if (sMain2.foo(getValue(sMain2.getClass())) != 11) {
79*795d594fSAndroid Build Coastguard Worker             System.out.println("11 expected.");
80*795d594fSAndroid Build Coastguard Worker         }
81*795d594fSAndroid Build Coastguard Worker 
82*795d594fSAndroid Build Coastguard Worker         if (createMain3) {
83*795d594fSAndroid Build Coastguard Worker             // Wait for the other thread to start.
84*795d594fSAndroid Build Coastguard Worker             while (!sOtherThreadStarted);
85*795d594fSAndroid Build Coastguard Worker             // Create an Main2 instance and assign it to sMain2.
86*795d594fSAndroid Build Coastguard Worker             // sMain1 is kept the same.
87*795d594fSAndroid Build Coastguard Worker             sMain3 = Helper.createMain3();
88*795d594fSAndroid Build Coastguard Worker             // Wake up the other thread.
89*795d594fSAndroid Build Coastguard Worker             synchronized(Main.class) {
90*795d594fSAndroid Build Coastguard Worker                 Main.class.notify();
91*795d594fSAndroid Build Coastguard Worker             }
92*795d594fSAndroid Build Coastguard Worker         } else if (wait) {
93*795d594fSAndroid Build Coastguard Worker             // This is the other thread.
94*795d594fSAndroid Build Coastguard Worker             synchronized(Main.class) {
95*795d594fSAndroid Build Coastguard Worker                 sOtherThreadStarted = true;
96*795d594fSAndroid Build Coastguard Worker                 // Wait for Main2 to be linked and deoptimization is triggered.
97*795d594fSAndroid Build Coastguard Worker                 try {
98*795d594fSAndroid Build Coastguard Worker                     Main.class.wait();
99*795d594fSAndroid Build Coastguard Worker                 } catch (Exception e) {
100*795d594fSAndroid Build Coastguard Worker                 }
101*795d594fSAndroid Build Coastguard Worker             }
102*795d594fSAndroid Build Coastguard Worker         }
103*795d594fSAndroid Build Coastguard Worker 
104*795d594fSAndroid Build Coastguard Worker         // There should be a deoptimization here right after Main3 is linked by
105*795d594fSAndroid Build Coastguard Worker         // calling Helper.createMain3(), even though sMain1 didn't change.
106*795d594fSAndroid Build Coastguard Worker         // The behavior here would be different if inline-cache is used, which
107*795d594fSAndroid Build Coastguard Worker         // doesn't deoptimize since sMain1 still hits the type cache.
108*795d594fSAndroid Build Coastguard Worker         if (sMain1.foo(getValue(sMain1.getClass())) != 11) {
109*795d594fSAndroid Build Coastguard Worker             System.out.println("11 expected.");
110*795d594fSAndroid Build Coastguard Worker         }
111*795d594fSAndroid Build Coastguard Worker         if ((createMain3 || wait) && sHasJIT && !sIsOptimizing) {
112*795d594fSAndroid Build Coastguard Worker             // This method should be deoptimized right after Main3 is created.
113*795d594fSAndroid Build Coastguard Worker             assertIsInterpreted();
114*795d594fSAndroid Build Coastguard Worker         }
115*795d594fSAndroid Build Coastguard Worker 
116*795d594fSAndroid Build Coastguard Worker         if (sMain3 != null) {
117*795d594fSAndroid Build Coastguard Worker             if (sMain3.foo(getValue(sMain3.getClass())) != -13) {
118*795d594fSAndroid Build Coastguard Worker                 System.out.println("-13 expected.");
119*795d594fSAndroid Build Coastguard Worker             }
120*795d594fSAndroid Build Coastguard Worker         }
121*795d594fSAndroid Build Coastguard Worker     }
122*795d594fSAndroid Build Coastguard Worker 
123*795d594fSAndroid Build Coastguard Worker     // Test scenarios under which CHA-based devirtualization happens,
124*795d594fSAndroid Build Coastguard Worker     // and class loading that implements a method can invalidate compiled code.
main(String[] args)125*795d594fSAndroid Build Coastguard Worker     public static void main(String[] args) {
126*795d594fSAndroid Build Coastguard Worker         System.loadLibrary(args[0]);
127*795d594fSAndroid Build Coastguard Worker 
128*795d594fSAndroid Build Coastguard Worker         if (isInterpreted()) {
129*795d594fSAndroid Build Coastguard Worker             sIsOptimizing = false;
130*795d594fSAndroid Build Coastguard Worker         }
131*795d594fSAndroid Build Coastguard Worker 
132*795d594fSAndroid Build Coastguard Worker         // sMain1 is an instance of Main1.
133*795d594fSAndroid Build Coastguard Worker         // sMain2 is an instance of Main2.
134*795d594fSAndroid Build Coastguard Worker         // Neither Main1 nor Main2 override default method Base.foo().
135*795d594fSAndroid Build Coastguard Worker         // Main3 hasn't bee loaded yet.
136*795d594fSAndroid Build Coastguard Worker         sMain1 = new Main1();
137*795d594fSAndroid Build Coastguard Worker         sMain2 = new Main2();
138*795d594fSAndroid Build Coastguard Worker 
139*795d594fSAndroid Build Coastguard Worker         ensureJitCompiled(Main.class, "testImplement");
140*795d594fSAndroid Build Coastguard Worker         testImplement(false, false, true);
141*795d594fSAndroid Build Coastguard Worker 
142*795d594fSAndroid Build Coastguard Worker         if (sHasJIT && !sIsOptimizing) {
143*795d594fSAndroid Build Coastguard Worker             assertSingleImplementation(Base.class, "foo", true);
144*795d594fSAndroid Build Coastguard Worker             assertSingleImplementation(Main1.class, "foo", true);
145*795d594fSAndroid Build Coastguard Worker         } else {
146*795d594fSAndroid Build Coastguard Worker             // Main3 is verified ahead-of-time so it's linked in already.
147*795d594fSAndroid Build Coastguard Worker         }
148*795d594fSAndroid Build Coastguard Worker 
149*795d594fSAndroid Build Coastguard Worker         // Create another thread that also calls sMain1.foo().
150*795d594fSAndroid Build Coastguard Worker         // Try to test suspend and deopt another thread.
151*795d594fSAndroid Build Coastguard Worker         new Thread() {
152*795d594fSAndroid Build Coastguard Worker             public void run() {
153*795d594fSAndroid Build Coastguard Worker                 testImplement(false, true, false);
154*795d594fSAndroid Build Coastguard Worker             }
155*795d594fSAndroid Build Coastguard Worker         }.start();
156*795d594fSAndroid Build Coastguard Worker 
157*795d594fSAndroid Build Coastguard Worker         // This will create Main3 instance in the middle of testImplement().
158*795d594fSAndroid Build Coastguard Worker         testImplement(true, false, false);
159*795d594fSAndroid Build Coastguard Worker         assertSingleImplementation(Base.class, "foo", false);
160*795d594fSAndroid Build Coastguard Worker         assertSingleImplementation(Main1.class, "foo", true);
161*795d594fSAndroid Build Coastguard Worker         assertSingleImplementation(sMain3.getClass(), "foo", true);
162*795d594fSAndroid Build Coastguard Worker     }
163*795d594fSAndroid Build Coastguard Worker 
ensureJitCompiled(Class<?> itf, String method_name)164*795d594fSAndroid Build Coastguard Worker     private static native void ensureJitCompiled(Class<?> itf, String method_name);
assertIsInterpreted()165*795d594fSAndroid Build Coastguard Worker     private static native void assertIsInterpreted();
assertIsManaged()166*795d594fSAndroid Build Coastguard Worker     private static native void assertIsManaged();
isInterpreted()167*795d594fSAndroid Build Coastguard Worker     private static native boolean isInterpreted();
hasSingleImplementation(Class<?> clazz, String method_name)168*795d594fSAndroid Build Coastguard Worker     private static native boolean hasSingleImplementation(Class<?> clazz, String method_name);
169*795d594fSAndroid Build Coastguard Worker }
170*795d594fSAndroid Build Coastguard Worker 
171*795d594fSAndroid Build Coastguard Worker // Put createMain3() in another class to avoid class loading due to verifier.
172*795d594fSAndroid Build Coastguard Worker class Helper {
createMain3()173*795d594fSAndroid Build Coastguard Worker     static Base createMain3() {
174*795d594fSAndroid Build Coastguard Worker         return new Main3();
175*795d594fSAndroid Build Coastguard Worker     }
176*795d594fSAndroid Build Coastguard Worker }
177