blob: 14bc7a393194b2f57caface0d403f836e1e105c1 [file] [log] [blame]
Mingyao Yang7b9a83f2016-12-13 12:28:31 -08001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import java.lang.reflect.InvocationHandler;
18import java.lang.reflect.Method;
19import java.lang.reflect.Proxy;
20
21class Main1 {
Vladimir Marko31ad1d82022-11-14 12:50:22 +000022 void foo(int i) {
23 if (i != 1) {
24 printError("error1");
25 }
Mingyao Yang7b9a83f2016-12-13 12:28:31 -080026 }
Mingyao Yang7b9a83f2016-12-13 12:28:31 -080027
Vladimir Marko31ad1d82022-11-14 12:50:22 +000028 void printError(String msg) {
29 System.out.println(msg);
30 }
Mingyao Yang7b9a83f2016-12-13 12:28:31 -080031}
32
33class Main2 extends Main1 {
Vladimir Marko31ad1d82022-11-14 12:50:22 +000034 void foo(int i) {
35 if (i != 2) {
36 printError("error2");
37 }
Mingyao Yang7b9a83f2016-12-13 12:28:31 -080038 }
Mingyao Yang7b9a83f2016-12-13 12:28:31 -080039}
40
41class Proxied implements Runnable {
Vladimir Marko31ad1d82022-11-14 12:50:22 +000042 public void run() {
43 synchronized(Main.class) {
44 Main.sOtherThreadStarted = true;
45 // Wait for Main2 to be linked and deoptimization is triggered.
46 try {
47 Main.class.wait();
48 } catch (Exception e) {
49 }
50 }
Mingyao Yang7b9a83f2016-12-13 12:28:31 -080051 }
Mingyao Yang7b9a83f2016-12-13 12:28:31 -080052}
53
54class MyInvocationHandler implements InvocationHandler {
Vladimir Marko31ad1d82022-11-14 12:50:22 +000055 private final Proxied proxied;
Mingyao Yang7b9a83f2016-12-13 12:28:31 -080056
Vladimir Marko31ad1d82022-11-14 12:50:22 +000057 public MyInvocationHandler(Proxied proxied) {
58 this.proxied = proxied;
59 }
Mingyao Yang7b9a83f2016-12-13 12:28:31 -080060
Vladimir Marko31ad1d82022-11-14 12:50:22 +000061 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
62 return method.invoke(proxied, args);
63 }
Mingyao Yang7b9a83f2016-12-13 12:28:31 -080064}
65
66public class Main {
Vladimir Marko31ad1d82022-11-14 12:50:22 +000067 static Main1 sMain1;
68 static Main1 sMain2;
69 static volatile boolean sOtherThreadStarted;
Mingyao Yang7b9a83f2016-12-13 12:28:31 -080070
Vladimir Marko31ad1d82022-11-14 12:50:22 +000071 // sMain1.foo() will be always be Main1.foo() before Main2 is loaded/linked.
72 // So sMain1.foo() can be devirtualized to Main1.foo() and be inlined.
73 // After Helper.createMain2() which links in Main2, live testOverride() on stack
74 // should be deoptimized.
75 static void testOverride() {
76 sMain1.foo(sMain1.getClass() == Main1.class ? 1 : 2);
Mingyao Yang7b9a83f2016-12-13 12:28:31 -080077
Vladimir Marko31ad1d82022-11-14 12:50:22 +000078 // Wait for the other thread to start.
79 while (!sOtherThreadStarted);
80 // Create an Main2 instance and assign it to sMain2.
81 // sMain1 is kept the same.
82 sMain2 = Helper.createMain2();
83 // Wake up the other thread.
84 synchronized(Main.class) {
85 Main.class.notify();
86 }
87
88 // There should be a deoptimization here right after Main2 is linked by
89 // calling Helper.createMain2(), even though sMain1 didn't change.
90 // The behavior here would be different if inline-cache is used, which
91 // doesn't deoptimize since sMain1 still hits the type cache.
92 sMain1.foo(sMain1.getClass() == Main1.class ? 1 : 2);
93 if (sMain2 != null) {
94 sMain2.foo(sMain2.getClass() == Main1.class ? 1 : 2);
95 }
Mingyao Yang7b9a83f2016-12-13 12:28:31 -080096 }
97
Vladimir Marko31ad1d82022-11-14 12:50:22 +000098 // Test scenarios under which CHA-based devirtualization happens,
99 // and class loading that overrides a method can invalidate compiled code.
100 // Also create a proxy method such that a proxy method's frame is visited
101 // during stack walking.
102 public static void main(String[] args) {
103 System.loadLibrary(args[0]);
104 // sMain1 is an instance of Main1. Main2 hasn't bee loaded yet.
105 sMain1 = new Main1();
106
107 // Create another thread that calls a proxy method.
108 new Thread() {
109 public void run() {
110 Runnable proxy = (Runnable)Proxy.newProxyInstance(
111 Proxied.class.getClassLoader(),
112 new Class[] { Runnable.class },
113 new MyInvocationHandler(new Proxied()));
114 proxy.run();
115 }
116 }.start();
117
118 ensureJitCompiled(Main.class, "testOverride");
119 // This will create Main2 instance in the middle of testOverride().
120 testOverride();
Mingyao Yang7b9a83f2016-12-13 12:28:31 -0800121 }
Mingyao Yang7b9a83f2016-12-13 12:28:31 -0800122
Vladimir Marko31ad1d82022-11-14 12:50:22 +0000123 private static native void ensureJitCompiled(Class<?> itf, String method_name);
Mingyao Yang7b9a83f2016-12-13 12:28:31 -0800124}
125
126// Put createMain2() in another class to avoid class loading due to verifier.
Orion Hodson2731eb42020-07-24 12:10:12 +0100127class Helper {
Vladimir Marko31ad1d82022-11-14 12:50:22 +0000128 static Main1 createMain2() {
129 return new Main2();
130 }
Mingyao Yang7b9a83f2016-12-13 12:28:31 -0800131}