xref: /aosp_15_r20/art/test/616-cha-regression-proxy-method/src/Main.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2016 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 import java.lang.reflect.InvocationHandler;
18*795d594fSAndroid Build Coastguard Worker import java.lang.reflect.Method;
19*795d594fSAndroid Build Coastguard Worker import java.lang.reflect.Proxy;
20*795d594fSAndroid Build Coastguard Worker 
21*795d594fSAndroid Build Coastguard Worker class Main1 {
foo(int i)22*795d594fSAndroid Build Coastguard Worker     void foo(int i) {
23*795d594fSAndroid Build Coastguard Worker         if (i != 1) {
24*795d594fSAndroid Build Coastguard Worker             printError("error1");
25*795d594fSAndroid Build Coastguard Worker         }
26*795d594fSAndroid Build Coastguard Worker     }
27*795d594fSAndroid Build Coastguard Worker 
printError(String msg)28*795d594fSAndroid Build Coastguard Worker     void printError(String msg) {
29*795d594fSAndroid Build Coastguard Worker         System.out.println(msg);
30*795d594fSAndroid Build Coastguard Worker     }
31*795d594fSAndroid Build Coastguard Worker }
32*795d594fSAndroid Build Coastguard Worker 
33*795d594fSAndroid Build Coastguard Worker class Main2 extends Main1 {
foo(int i)34*795d594fSAndroid Build Coastguard Worker     void foo(int i) {
35*795d594fSAndroid Build Coastguard Worker         if (i != 2) {
36*795d594fSAndroid Build Coastguard Worker             printError("error2");
37*795d594fSAndroid Build Coastguard Worker         }
38*795d594fSAndroid Build Coastguard Worker     }
39*795d594fSAndroid Build Coastguard Worker }
40*795d594fSAndroid Build Coastguard Worker 
41*795d594fSAndroid Build Coastguard Worker class Proxied implements Runnable {
run()42*795d594fSAndroid Build Coastguard Worker     public void run() {
43*795d594fSAndroid Build Coastguard Worker         synchronized(Main.class) {
44*795d594fSAndroid Build Coastguard Worker             Main.sOtherThreadStarted = true;
45*795d594fSAndroid Build Coastguard Worker             // Wait for Main2 to be linked and deoptimization is triggered.
46*795d594fSAndroid Build Coastguard Worker             try {
47*795d594fSAndroid Build Coastguard Worker                 Main.class.wait();
48*795d594fSAndroid Build Coastguard Worker             } catch (Exception e) {
49*795d594fSAndroid Build Coastguard Worker             }
50*795d594fSAndroid Build Coastguard Worker         }
51*795d594fSAndroid Build Coastguard Worker     }
52*795d594fSAndroid Build Coastguard Worker }
53*795d594fSAndroid Build Coastguard Worker 
54*795d594fSAndroid Build Coastguard Worker class MyInvocationHandler implements InvocationHandler {
55*795d594fSAndroid Build Coastguard Worker     private final Proxied proxied;
56*795d594fSAndroid Build Coastguard Worker 
MyInvocationHandler(Proxied proxied)57*795d594fSAndroid Build Coastguard Worker     public MyInvocationHandler(Proxied proxied) {
58*795d594fSAndroid Build Coastguard Worker         this.proxied = proxied;
59*795d594fSAndroid Build Coastguard Worker     }
60*795d594fSAndroid Build Coastguard Worker 
invoke(Object proxy, Method method, Object[] args)61*795d594fSAndroid Build Coastguard Worker     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
62*795d594fSAndroid Build Coastguard Worker         return method.invoke(proxied, args);
63*795d594fSAndroid Build Coastguard Worker     }
64*795d594fSAndroid Build Coastguard Worker }
65*795d594fSAndroid Build Coastguard Worker 
66*795d594fSAndroid Build Coastguard Worker public class Main {
67*795d594fSAndroid Build Coastguard Worker     static Main1 sMain1;
68*795d594fSAndroid Build Coastguard Worker     static Main1 sMain2;
69*795d594fSAndroid Build Coastguard Worker     static volatile boolean sOtherThreadStarted;
70*795d594fSAndroid Build Coastguard Worker 
71*795d594fSAndroid Build Coastguard Worker     // sMain1.foo() will be always be Main1.foo() before Main2 is loaded/linked.
72*795d594fSAndroid Build Coastguard Worker     // So sMain1.foo() can be devirtualized to Main1.foo() and be inlined.
73*795d594fSAndroid Build Coastguard Worker     // After Helper.createMain2() which links in Main2, live testOverride() on stack
74*795d594fSAndroid Build Coastguard Worker     // should be deoptimized.
testOverride()75*795d594fSAndroid Build Coastguard Worker     static void testOverride() {
76*795d594fSAndroid Build Coastguard Worker         sMain1.foo(sMain1.getClass() == Main1.class ? 1 : 2);
77*795d594fSAndroid Build Coastguard Worker 
78*795d594fSAndroid Build Coastguard Worker         // Wait for the other thread to start.
79*795d594fSAndroid Build Coastguard Worker         while (!sOtherThreadStarted);
80*795d594fSAndroid Build Coastguard Worker         // Create an Main2 instance and assign it to sMain2.
81*795d594fSAndroid Build Coastguard Worker         // sMain1 is kept the same.
82*795d594fSAndroid Build Coastguard Worker         sMain2 = Helper.createMain2();
83*795d594fSAndroid Build Coastguard Worker         // Wake up the other thread.
84*795d594fSAndroid Build Coastguard Worker         synchronized(Main.class) {
85*795d594fSAndroid Build Coastguard Worker             Main.class.notify();
86*795d594fSAndroid Build Coastguard Worker         }
87*795d594fSAndroid Build Coastguard Worker 
88*795d594fSAndroid Build Coastguard Worker         // There should be a deoptimization here right after Main2 is linked by
89*795d594fSAndroid Build Coastguard Worker         // calling Helper.createMain2(), even though sMain1 didn't change.
90*795d594fSAndroid Build Coastguard Worker         // The behavior here would be different if inline-cache is used, which
91*795d594fSAndroid Build Coastguard Worker         // doesn't deoptimize since sMain1 still hits the type cache.
92*795d594fSAndroid Build Coastguard Worker         sMain1.foo(sMain1.getClass() == Main1.class ? 1 : 2);
93*795d594fSAndroid Build Coastguard Worker         if (sMain2 != null) {
94*795d594fSAndroid Build Coastguard Worker             sMain2.foo(sMain2.getClass() == Main1.class ? 1 : 2);
95*795d594fSAndroid Build Coastguard Worker         }
96*795d594fSAndroid Build Coastguard Worker     }
97*795d594fSAndroid Build Coastguard Worker 
98*795d594fSAndroid Build Coastguard Worker     // Test scenarios under which CHA-based devirtualization happens,
99*795d594fSAndroid Build Coastguard Worker     // and class loading that overrides a method can invalidate compiled code.
100*795d594fSAndroid Build Coastguard Worker     // Also create a proxy method such that a proxy method's frame is visited
101*795d594fSAndroid Build Coastguard Worker     // during stack walking.
main(String[] args)102*795d594fSAndroid Build Coastguard Worker     public static void main(String[] args) {
103*795d594fSAndroid Build Coastguard Worker         System.loadLibrary(args[0]);
104*795d594fSAndroid Build Coastguard Worker         // sMain1 is an instance of Main1. Main2 hasn't bee loaded yet.
105*795d594fSAndroid Build Coastguard Worker         sMain1 = new Main1();
106*795d594fSAndroid Build Coastguard Worker 
107*795d594fSAndroid Build Coastguard Worker         // Create another thread that calls a proxy method.
108*795d594fSAndroid Build Coastguard Worker         new Thread() {
109*795d594fSAndroid Build Coastguard Worker             public void run() {
110*795d594fSAndroid Build Coastguard Worker                 Runnable proxy = (Runnable)Proxy.newProxyInstance(
111*795d594fSAndroid Build Coastguard Worker                         Proxied.class.getClassLoader(),
112*795d594fSAndroid Build Coastguard Worker                         new Class[] { Runnable.class },
113*795d594fSAndroid Build Coastguard Worker                         new MyInvocationHandler(new Proxied()));
114*795d594fSAndroid Build Coastguard Worker                 proxy.run();
115*795d594fSAndroid Build Coastguard Worker             }
116*795d594fSAndroid Build Coastguard Worker         }.start();
117*795d594fSAndroid Build Coastguard Worker 
118*795d594fSAndroid Build Coastguard Worker         ensureJitCompiled(Main.class, "testOverride");
119*795d594fSAndroid Build Coastguard Worker         // This will create Main2 instance in the middle of testOverride().
120*795d594fSAndroid Build Coastguard Worker         testOverride();
121*795d594fSAndroid Build Coastguard Worker     }
122*795d594fSAndroid Build Coastguard Worker 
ensureJitCompiled(Class<?> itf, String method_name)123*795d594fSAndroid Build Coastguard Worker     private static native void ensureJitCompiled(Class<?> itf, String method_name);
124*795d594fSAndroid Build Coastguard Worker }
125*795d594fSAndroid Build Coastguard Worker 
126*795d594fSAndroid Build Coastguard Worker // Put createMain2() in another class to avoid class loading due to verifier.
127*795d594fSAndroid Build Coastguard Worker class Helper {
createMain2()128*795d594fSAndroid Build Coastguard Worker     static Main1 createMain2() {
129*795d594fSAndroid Build Coastguard Worker         return new Main2();
130*795d594fSAndroid Build Coastguard Worker     }
131*795d594fSAndroid Build Coastguard Worker }
132