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