1 /* 2 * Copyright (C) 2018 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 public class Main { main(String args[])18 public static void main(String args[]) { 19 simpleTest(); 20 hierarchyTest(); 21 } 22 simpleTest()23 public static void simpleTest() { 24 // Partial initialization of Bad; ignoring the error. 25 Error badClinit = null; 26 try { 27 new Bad(11); 28 } catch (Error e) { 29 badClinit = e; 30 } 31 // Call foo() on the escaped instance of Bad. 32 try { 33 bad.foo(); 34 } catch (NoClassDefFoundError ncdfe) { 35 // On RI, the NCDFE has no cause. On ART, the badClinit is the cause. 36 if (ncdfe.getCause() == badClinit || ncdfe.getCause() == null) { 37 System.out.println("Caught NoClassDefFoundError."); 38 } else { 39 ncdfe.printStackTrace(); 40 } 41 } 42 // Call bar() on the escaped instance of Bad. 43 try { 44 bad.bar(); 45 } catch (NoClassDefFoundError ncdfe) { 46 // On RI, the NCDFE has no cause. On ART, the badClinit is the cause. 47 if (ncdfe.getCause() == badClinit || ncdfe.getCause() == null) { 48 System.out.println("Caught NoClassDefFoundError."); 49 } else { 50 ncdfe.printStackTrace(); 51 } 52 } 53 54 // Test that we handle bad instance correctly in the resolution trampoline. 55 bad.$noinline$testResolutionTrampoline(); 56 } 57 hierarchyTest()58 public static void hierarchyTest() { 59 // Partial initialization of BadSuper; ignoring the error. Fully initializes BadSub. 60 Error badClinit = null; 61 try { 62 new BadSuper(0); 63 } catch (Error e) { 64 badClinit = e; 65 } 66 // Call BadSuper.foo() on the escaped instance of BadSuper. 67 try { 68 badSuper.foo(); 69 } catch (NoClassDefFoundError ncdfe) { 70 // On RI, the NCDFE has no cause. On ART, the badClinit is the cause. 71 if (ncdfe.getCause() == badClinit || ncdfe.getCause() == null) { 72 System.out.println("Caught NoClassDefFoundError."); 73 } else { 74 ncdfe.printStackTrace(); 75 } 76 } 77 78 // Call BadSub.bar() on the escaped instance of BadSub. 79 try { 80 badSub.bar(); 81 } catch (NoClassDefFoundError ncdfe) { 82 // On RI, the NCDFE has no cause. On ART, the badClinit is the cause. 83 if (ncdfe.getCause() == badClinit || ncdfe.getCause() == null) { 84 System.out.println("Caught NoClassDefFoundError."); 85 } else { 86 ncdfe.printStackTrace(); 87 } 88 } 89 90 // Test that we can even create instances of BadSub with erroneous superclass BadSuper. 91 try { 92 new BadSub(-1, -2).bar(); 93 } catch (NoClassDefFoundError ncdfe) { 94 // On RI, the NCDFE has no cause. On ART, the badClinit is the cause. 95 if (ncdfe.getCause() == badClinit || ncdfe.getCause() == null) { 96 System.out.println("Caught NoClassDefFoundError."); 97 } else { 98 ncdfe.printStackTrace(); 99 } 100 } 101 102 // Test that we cannot create instances of BadSuper from BadSub. 103 try { 104 badSub.allocSuper(11111); // Should throw. 105 System.out.println("Allocated BadSuper!"); 106 } catch (NoClassDefFoundError ncdfe) { 107 // On RI, the NCDFE has no cause. On ART, the badClinit is the cause. 108 if (ncdfe.getCause() == badClinit || ncdfe.getCause() == null) { 109 System.out.println("Caught NoClassDefFoundError."); 110 } else { 111 ncdfe.printStackTrace(); 112 } 113 } 114 } 115 116 public static Bad bad; 117 118 public static BadSuper badSuper; 119 public static BadSub badSub; 120 } 121 122 class Bad { 123 static { 124 // Create an instance of Bad and let it escape in Main.bad. 125 Main.bad = new Bad(33); 126 staticValue = 42; 127 if (true) { throw new Error("Bad <clinit>"); } 128 } foo()129 public void foo() { 130 System.out.println("Bad.foo()"); 131 System.out.println("Bad.instanceValue = " + instanceValue); 132 System.out.println("Bad.staticValue = " + staticValue); 133 } bar()134 public void bar() { 135 System.out.println("Bad.bar()"); 136 System.out.println("Bad.staticValue [indirect] = " + Helper.$inline$getBadStaticValue()); 137 } Bad(int iv)138 public Bad(int iv) { instanceValue = iv; } 139 public int instanceValue; 140 public static int staticValue; 141 142 public static class Helper { $inline$getBadStaticValue()143 public static int $inline$getBadStaticValue() { 144 return Bad.staticValue; 145 } 146 } 147 $noinline$testResolutionTrampoline()148 public void $noinline$testResolutionTrampoline() { 149 // The first call to private method uses the resolution trampoline when AOT-compiled. 150 $noinline$testResolutionTrampolineCallee(); 151 } 152 $noinline$testResolutionTrampolineCallee()153 private void $noinline$testResolutionTrampolineCallee() { 154 System.out.println("Bad.$noinline$testResolutionTrampolineCallee()"); 155 } 156 } 157 158 class BadSuper { 159 static { 160 Main.badSuper = new BadSuper(1); 161 Main.badSub = new BadSub(11, 111); // Fully initializes BadSub. 162 BadSuper.superStaticValue = 42; 163 BadSub.subStaticValue = 4242; 164 if (true) { throw new Error("Bad <clinit>"); } 165 } foo()166 public void foo() { 167 System.out.println("BadSuper.foo()"); 168 System.out.println("BadSuper.superInstanceValue = " + superInstanceValue); 169 System.out.println("BadSuper.superStaticValue = " + superStaticValue); 170 } BadSuper(int superiv)171 public BadSuper(int superiv) { superInstanceValue = superiv; } 172 public int superInstanceValue; 173 public static int superStaticValue; 174 } 175 176 // Note: If we tried to initialize BadSub before BadSuper, it would end up erroneous 177 // because the superclass fails initialization. However, since we start initializing the 178 // BadSuper first, BadSub is initialized successfully while BadSuper is "initializing" 179 // and remains initialized after the BadSuper's class initializer throws. 180 class BadSub extends BadSuper { bar()181 public void bar() { 182 System.out.println("BadSub.bar()"); 183 System.out.println("BadSub.subInstanceValue = " + subInstanceValue); 184 System.out.println("BadSub.subStaticValue = " + subStaticValue); 185 System.out.println("BadSuper.superInstanceValue = " + superInstanceValue); 186 System.out.println("BadSuper.superStaticValue = " + superStaticValue); 187 } allocSuper(int superiv)188 public BadSuper allocSuper(int superiv) { 189 System.out.println("BadSub.allocSuper(.)"); 190 return new BadSuper(superiv); 191 } BadSub(int subiv, int superiv)192 public BadSub(int subiv, int superiv) { 193 super(superiv); 194 subInstanceValue = subiv; 195 } 196 public int subInstanceValue; 197 public static int subStaticValue; 198 } 199