summaryrefslogtreecommitdiff
path: root/test/616-cha-regression-proxy-method/src/Main.java
blob: 176f80b1bf7cc52adbb6ef3b273fc4add24f7a0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

class Main1 {
  void foo(int i) {
    if (i != 1) {
      printError("error1");
    }
  }

  void printError(String msg) {
    System.out.println(msg);
  }
}

class Main2 extends Main1 {
  void foo(int i) {
    if (i != 2) {
      printError("error2");
    }
  }
}

class Proxied implements Runnable {
  public void run() {
    synchronized(Main.class) {
      Main.sOtherThreadStarted = true;
      // Wait for Main2 to be linked and deoptimization is triggered.
      try {
        Main.class.wait();
      } catch (Exception e) {
      }
    }
  }
}

class MyInvocationHandler implements InvocationHandler {
  private final Proxied proxied;

  public MyInvocationHandler(Proxied proxied) {
    this.proxied = proxied;
  }

  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    return method.invoke(proxied, args);
  }
}

public class Main {
  static Main1 sMain1;
  static Main1 sMain2;
  static volatile boolean sOtherThreadStarted;

  // sMain1.foo() will be always be Main1.foo() before Main2 is loaded/linked.
  // So sMain1.foo() can be devirtualized to Main1.foo() and be inlined.
  // After Helper.createMain2() which links in Main2, live testOverride() on stack
  // should be deoptimized.
  static void testOverride() {
    sMain1.foo(sMain1.getClass() == Main1.class ? 1 : 2);

    // Wait for the other thread to start.
    while (!sOtherThreadStarted);
    // Create an Main2 instance and assign it to sMain2.
    // sMain1 is kept the same.
    sMain2 = Helper.createMain2();
    // Wake up the other thread.
    synchronized(Main.class) {
      Main.class.notify();
    }

    // There should be a deoptimization here right after Main2 is linked by
    // calling Helper.createMain2(), even though sMain1 didn't change.
    // The behavior here would be different if inline-cache is used, which
    // doesn't deoptimize since sMain1 still hits the type cache.
    sMain1.foo(sMain1.getClass() == Main1.class ? 1 : 2);
    if (sMain2 != null) {
      sMain2.foo(sMain2.getClass() == Main1.class ? 1 : 2);
    }
  }

  // Test scenarios under which CHA-based devirtualization happens,
  // and class loading that overrides a method can invalidate compiled code.
  // Also create a proxy method such that a proxy method's frame is visited
  // during stack walking.
  public static void main(String[] args) {
    System.loadLibrary(args[0]);
    // sMain1 is an instance of Main1. Main2 hasn't bee loaded yet.
    sMain1 = new Main1();

    // Create another thread that calls a proxy method.
    new Thread() {
      public void run() {
        Runnable proxy = (Runnable)Proxy.newProxyInstance(
            Proxied.class.getClassLoader(),
            new Class[] { Runnable.class },
            new MyInvocationHandler(new Proxied()));
        proxy.run();
      }
    }.start();

    ensureJitCompiled(Main.class, "testOverride");
    // This will create Main2 instance in the middle of testOverride().
    testOverride();
  }

  private static native void ensureJitCompiled(Class<?> itf, String method_name);
}

// Put createMain2() in another class to avoid class loading due to verifier.
class Helper {
  static Main1 createMain2() {
    return new Main2();
  }
}