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       checkNcdfe(ncdfe, badClinit);
36     }
37     // Call bar() on the escaped instance of Bad.
38     try {
39       bad.bar();
40     } catch (NoClassDefFoundError ncdfe) {
41       checkNcdfe(ncdfe, badClinit);
42     }
43 
44     // Test that we handle bad instance correctly in the resolution trampoline.
45     bad.$noinline$testResolutionTrampoline();
46   }
47 
hierarchyTest()48   public static void hierarchyTest() {
49     // Partial initialization of BadSuper; ignoring the error. Fully initializes BadSub.
50     Error badClinit = null;
51     try {
52       new BadSuper(0);
53     } catch (Error e) {
54       badClinit = e;
55     }
56     // Call BadSuper.foo() on the escaped instance of BadSuper.
57     try {
58       badSuper.foo();
59     } catch (NoClassDefFoundError ncdfe) {
60       checkNcdfe(ncdfe, badClinit);
61     }
62 
63     // Call BadSub.bar() on the escaped instance of BadSub.
64     try {
65       badSub.bar();
66     } catch (NoClassDefFoundError ncdfe) {
67       checkNcdfe(ncdfe, badClinit);
68     }
69 
70     // Test that we can even create instances of BadSub with erroneous superclass BadSuper.
71     try {
72       new BadSub(-1, -2).bar();
73     } catch (NoClassDefFoundError ncdfe) {
74       checkNcdfe(ncdfe, badClinit);
75     }
76 
77     // Test that we cannot create instances of BadSuper from BadSub.
78     try {
79       badSub.allocSuper(11111);  // Should throw.
80       System.out.println("Allocated BadSuper!");
81     } catch (NoClassDefFoundError ncdfe) {
82       checkNcdfe(ncdfe, badClinit);
83     }
84   }
85 
checkNcdfe(NoClassDefFoundError ncdfe, Throwable expectedCause)86   private static void checkNcdfe(NoClassDefFoundError ncdfe, Throwable expectedCause) {
87     // On RI, the NCDFE has no cause. On ART, the badClinit is the cause.
88     if (ncdfe.getCause() == expectedCause || ncdfe.getCause() == null) {
89       System.out.println("Caught NoClassDefFoundError.");
90       return;
91     }
92     // On newer RI, the NCDFE has an ExceptionInInitializerError cause which contains the string of
93     // the original error, but have a null cause and exception.
94     if (ncdfe.getCause() instanceof ExceptionInInitializerError) {
95       ExceptionInInitializerError cause = (ExceptionInInitializerError) ncdfe.getCause();
96       if (cause.getException() == null) {
97         System.out.println("Caught NoClassDefFoundError.");
98         return;
99       }
100     }
101     ncdfe.printStackTrace();
102   }
103 
104   public static Bad bad;
105 
106   public static BadSuper badSuper;
107   public static BadSub badSub;
108 }
109 
110 class Bad {
111   static {
112     // Create an instance of Bad and let it escape in Main.bad.
113     Main.bad = new Bad(33);
114     staticValue = 42;
115     if (true) { throw new Error("Bad <clinit>"); }
116   }
foo()117   public void foo() {
118     System.out.println("Bad.foo()");
119     System.out.println("Bad.instanceValue = " + instanceValue);
120     System.out.println("Bad.staticValue = " + staticValue);
121   }
bar()122   public void bar() {
123     System.out.println("Bad.bar()");
124     System.out.println("Bad.staticValue [indirect] = " + Helper.$inline$getBadStaticValue());
125   }
Bad(int iv)126   public Bad(int iv) { instanceValue = iv; }
127   public int instanceValue;
128   public static int staticValue;
129 
130   public static class Helper {
$inline$getBadStaticValue()131     public static int $inline$getBadStaticValue() {
132       return Bad.staticValue;
133     }
134   }
135 
$noinline$testResolutionTrampoline()136   public void $noinline$testResolutionTrampoline() {
137     // The first call to private method uses the resolution trampoline when AOT-compiled.
138     $noinline$testResolutionTrampolineCallee();
139   }
140 
$noinline$testResolutionTrampolineCallee()141   private void $noinline$testResolutionTrampolineCallee() {
142     System.out.println("Bad.$noinline$testResolutionTrampolineCallee()");
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