1 /* 2 * Copyright (C) 2017 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 17 interface Iface { foo(int i)18 public void foo(int i); 19 } 20 21 abstract class Base implements Iface { 22 // Iface.foo(int) will be added as a miranda method. 23 printError(String msg)24 void printError(String msg) { 25 System.out.println(msg); 26 } 27 } 28 29 class Main1 extends Base { foo(int i)30 public void foo(int i) { 31 if (i != 1) { 32 printError("error1"); 33 } 34 } 35 } 36 37 class Main2 extends Main1 { foo(int i)38 public void foo(int i) { 39 if (i != 2) { 40 printError("error2"); 41 } 42 } 43 } 44 45 public class Main { 46 static Base sMain1; 47 static Base sMain2; 48 49 static boolean sIsOptimizing = true; 50 static boolean sHasJIT = true; 51 static volatile boolean sOtherThreadStarted; 52 assertSingleImplementation(Class<?> clazz, String method_name, boolean b)53 private static void assertSingleImplementation(Class<?> clazz, String method_name, boolean b) { 54 if (hasSingleImplementation(clazz, method_name) != b) { 55 System.out.println(clazz + "." + method_name + 56 " doesn't have single implementation value of " + b); 57 } 58 } 59 60 // sMain1.foo() will be always be Main1.foo() before Main2 is loaded/linked. 61 // So sMain1.foo() can be devirtualized to Main1.foo() and be inlined. 62 // After Helper.createMain2() which links in Main2, live testOverride() on stack 63 // should be deoptimized. testOverride(boolean createMain2, boolean wait)64 static void testOverride(boolean createMain2, boolean wait) { 65 if (createMain2 && (sIsOptimizing || sHasJIT)) { 66 assertIsManaged(); 67 } 68 69 sMain1.foo(sMain1.getClass() == Main1.class ? 1 : 2); 70 71 if (createMain2) { 72 // Wait for the other thread to start. 73 while (!sOtherThreadStarted); 74 // Create an Main2 instance and assign it to sMain2. 75 // sMain1 is kept the same. 76 sMain2 = Helper.createMain2(); 77 // Wake up the other thread. 78 synchronized(Main.class) { 79 Main.class.notify(); 80 } 81 } else if (wait) { 82 // This is the other thread. 83 synchronized(Main.class) { 84 sOtherThreadStarted = true; 85 // Wait for Main2 to be linked and deoptimization is triggered. 86 try { 87 Main.class.wait(); 88 } catch (Exception e) { 89 } 90 } 91 } 92 93 // There should be a deoptimization here right after Main2 is linked by 94 // calling Helper.createMain2(), even though sMain1 didn't change. 95 // The behavior here would be different if inline-cache is used, which 96 // doesn't deoptimize since sMain1 still hits the type cache. 97 sMain1.foo(sMain1.getClass() == Main1.class ? 1 : 2); 98 if ((createMain2 || wait) && sHasJIT && !sIsOptimizing) { 99 // This method should be deoptimized right after Main2 is created. 100 assertIsInterpreted(); 101 } 102 103 if (sMain2 != null) { 104 sMain2.foo(sMain2.getClass() == Main1.class ? 1 : 2); 105 } 106 } 107 108 // Test scenarios under which CHA-based devirtualization happens, 109 // and class loading that overrides a method can invalidate compiled code. main(String[] args)110 public static void main(String[] args) { 111 System.loadLibrary(args[0]); 112 113 sIsOptimizing = isAotCompiled(Main.class, "testOverride"); 114 sHasJIT = hasJit(); 115 116 // sMain1 is an instance of Main1. Main2 hasn't bee loaded yet. 117 sMain1 = new Main1(); 118 119 ensureJitCompiled(Main.class, "testOverride"); 120 121 if (sHasJIT && !sIsOptimizing) { 122 assertSingleImplementation(Base.class, "foo", true); 123 assertSingleImplementation(Main1.class, "foo", true); 124 } else { 125 // Main2 is verified ahead-of-time so it's linked in already. 126 } 127 128 // Create another thread that also calls sMain1.foo(). 129 // Try to test suspend and deopt another thread. 130 new Thread() { 131 public void run() { 132 testOverride(false, true); 133 } 134 }.start(); 135 136 // This will create Main2 instance in the middle of testOverride(). 137 testOverride(true, false); 138 assertSingleImplementation(Base.class, "foo", false); 139 assertSingleImplementation(Main1.class, "foo", false); 140 } 141 hasJit()142 private static native boolean hasJit(); isAotCompiled(Class<?> cls, String methodName)143 private native static boolean isAotCompiled(Class<?> cls, String methodName); ensureJitCompiled(Class<?> itf, String method_name)144 private static native void ensureJitCompiled(Class<?> itf, String method_name); assertIsInterpreted()145 private static native void assertIsInterpreted(); assertIsManaged()146 private static native void assertIsManaged(); isInterpreted()147 private static native boolean isInterpreted(); hasSingleImplementation(Class<?> clazz, String method_name)148 private static native boolean hasSingleImplementation(Class<?> clazz, String method_name); 149 } 150 151 // Put createMain2() in another class to avoid class loading due to verifier. 152 class Helper { createMain2()153 static Main1 createMain2() { 154 return new Main2(); 155 } 156 } 157