• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2#
3# Copyright (C) 2018 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# Stop if something fails.
18set -e
19
20# Write out the source file.
21
22mkdir src
23cat >src/Main.java <<EOF
24/*
25 * Copyright (C) 2018 The Android Open Source Project
26 *
27 * Licensed under the Apache License, Version 2.0 (the "License");
28 * you may not use this file except in compliance with the License.
29 * You may obtain a copy of the License at
30 *
31 *      http://www.apache.org/licenses/LICENSE-2.0
32 *
33 * Unless required by applicable law or agreed to in writing, software
34 * distributed under the License is distributed on an "AS IS" BASIS,
35 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
36 * See the License for the specific language governing permissions and
37 * limitations under the License.
38 */
39
40EOF
41
42for i in {0..8192}; do echo "class Level1Class$i { }" >>src/Main.java; done
43for i in {0..1024}; do echo "class Level2Class$i extends Level1Class0 { }" >>src/Main.java; done
44
45cat >>src/Main.java <<EOF
46class Level3Class0 extends Level2Class0 { }
47class Level4Class0 extends Level3Class0 { }
48class Level5Class0 extends Level4Class0 { }
49class Level6Class0 extends Level5Class0 { }
50class Level7Class0 extends Level6Class0 { }
51class Level8Class0 extends Level7Class0 { }
52class Level9Class0 extends Level8Class0 { }
53
54public class Main {
55  public static void main(String[] args) throws Exception {
56    // 8193 classes at level 1 make sure we shall have an overflow if there are 13 or
57    // less bits for the level 1 character. 1025 classes at level 2 similarly guarantees
58    // an overflow if the number of bits for level 2 character is 10 or less. To test
59    // type checks also for the depth overflow, we provide a hierarchy 9 levels deep.
60
61    // Make sure the bitstrings are initialized.
62    for (int i = 0; i <= 8192; ++i) {
63      Class.forName("Level1Class" + i).newInstance();
64    }
65    for (int i = 0; i <= 1024; ++i) {
66      Class.forName("Level2Class" + i).newInstance();
67    }
68
69    // Note: Using a different class for tests so that verification of Main.main() does
70    // not try to resolve classes used by the tests. This guarantees uninitialized type
71    // check bitstrings when we enter Main.main() and start initializing them above.
72    Helper.testInstanceOf();
73    Helper.testCheckCast();
74  }
75}
76
77class Helper {
78  public static void testInstanceOf() throws Exception {
79    for (int i = 1; i <= 9; ++i) {
80      Object o = createInstance("Level" + i + "Class0");
81      assertTrue(o instanceof Level1Class0);
82      if (o instanceof Level2Class0) {
83        assertFalse(i < 2);
84      } else {
85        assertTrue(i < 2);
86      }
87      if (o instanceof Level3Class0) {
88        assertFalse(i < 3);
89      } else {
90        assertTrue(i < 3);
91      }
92      if (o instanceof Level4Class0) {
93        assertFalse(i < 4);
94      } else {
95        assertTrue(i < 4);
96      }
97      if (o instanceof Level5Class0) {
98        assertFalse(i < 5);
99      } else {
100        assertTrue(i < 5);
101      }
102      if (o instanceof Level6Class0) {
103        assertFalse(i < 6);
104      } else {
105        assertTrue(i < 6);
106      }
107      if (o instanceof Level7Class0) {
108        assertFalse(i < 7);
109      } else {
110        assertTrue(i < 7);
111      }
112      if (o instanceof Level8Class0) {
113        assertFalse(i < 8);
114      } else {
115        assertTrue(i < 8);
116      }
117      if (o instanceof Level9Class0) {
118        assertFalse(i < 9);
119      } else {
120        assertTrue(i < 9);
121      }
122    }
123
124    assertTrue(createInstance("Level1Class8192") instanceof Level1Class8192);
125    assertFalse(createInstance("Level1Class8192") instanceof Level1Class0);
126    assertTrue(createInstance("Level2Class1024") instanceof Level2Class1024);
127    assertTrue(createInstance("Level2Class1024") instanceof Level1Class0);
128    assertFalse(createInstance("Level2Class1024") instanceof Level2Class0);
129  }
130
131  public static void testCheckCast() throws Exception {
132    for (int i = 1; i <= 9; ++i) {
133      Object o = createInstance("Level" + i + "Class0");
134      Level1Class0 l1c0 = (Level1Class0) o;
135      try {
136        Level2Class0 l2c0 = (Level2Class0) o;
137        assertFalse(i < 2);
138      } catch (ClassCastException cce) {
139        assertTrue(i < 2);
140      }
141      try {
142        Level3Class0 l3c0 = (Level3Class0) o;
143        assertFalse(i < 3);
144      } catch (ClassCastException cce) {
145        assertTrue(i < 3);
146      }
147      try {
148        Level4Class0 l4c0 = (Level4Class0) o;
149        assertFalse(i < 4);
150      } catch (ClassCastException cce) {
151        assertTrue(i < 4);
152      }
153      try {
154        Level5Class0 l5c0 = (Level5Class0) o;
155        assertFalse(i < 5);
156      } catch (ClassCastException cce) {
157        assertTrue(i < 5);
158      }
159      try {
160        Level6Class0 l6c0 = (Level6Class0) o;
161        assertFalse(i < 6);
162      } catch (ClassCastException cce) {
163        assertTrue(i < 6);
164      }
165      try {
166        Level7Class0 l7c0 = (Level7Class0) o;
167        assertFalse(i < 7);
168      } catch (ClassCastException cce) {
169        assertTrue(i < 7);
170      }
171      try {
172        Level8Class0 l8c0 = (Level8Class0) o;
173        assertFalse(i < 8);
174      } catch (ClassCastException cce) {
175        assertTrue(i < 8);
176      }
177      try {
178        Level9Class0 l9c0 = (Level9Class0) o;
179        assertFalse(i < 9);
180      } catch (ClassCastException cce) {
181        assertTrue(i < 9);
182      }
183    }
184
185    Level1Class8192 l1c8192 = (Level1Class8192) createInstance("Level1Class8192");
186    try {
187      Level1Class0 l1c0 = (Level1Class0) createInstance("Level1Class8192");
188      throw new AssertionError("Unexpected");
189    } catch (ClassCastException expected) {}
190    Level2Class1024 l2c1024 = (Level2Class1024) createInstance("Level2Class1024");
191    Level1Class0 l1c0 = (Level1Class0) createInstance("Level2Class1024");
192    try {
193      Level2Class0 l2c0 = (Level2Class0) createInstance("Level2Class1024");
194      throw new AssertionError("Unexpected");
195    } catch (ClassCastException expected) {}
196  }
197
198  public static Object createInstance(String className) throws Exception {
199    return Class.forName(className).newInstance();
200  }
201
202  public static void assertTrue(boolean value) throws Exception {
203    if (!value) {
204      throw new AssertionError();
205    }
206  }
207
208  public static void assertFalse(boolean value) throws Exception {
209    if (value) {
210      throw new AssertionError();
211    }
212  }
213}
214EOF
215
216./default-build "$@"
217