blob: 8c78d71d4d157d115387fb96dcbd78ec41a91acf [file] [log] [blame]
Andreas Gampee492ae32016-10-28 19:34:57 -07001/*
2 * Copyright (C) 2016 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
Andreas Gampe70f16392017-01-16 14:20:10 -080017import java.lang.reflect.Constructor;
Andreas Gampee492ae32016-10-28 19:34:57 -070018import java.lang.reflect.Proxy;
19import java.util.Arrays;
Andreas Gampe70f16392017-01-16 14:20:10 -080020import java.util.Comparator;
Andreas Gampee492ae32016-10-28 19:34:57 -070021
22public class Main {
23 public static void main(String[] args) throws Exception {
Andreas Gampee492ae32016-10-28 19:34:57 -070024 doTest();
25 }
26
27 public static void doTest() throws Exception {
28 testClass("java.lang.Object");
29 testClass("java.lang.String");
30 testClass("java.lang.Math");
31 testClass("java.util.List");
32
33 testClass(getProxyClass());
34
35 testClass(int.class);
36 testClass(double[].class);
Andreas Gampe4fd66ec2017-01-05 14:42:13 -080037
38 testClassType(int.class);
39 testClassType(getProxyClass());
40 testClassType(Runnable.class);
41 testClassType(String.class);
42
43 testClassType(int[].class);
44 testClassType(Runnable[].class);
45 testClassType(String[].class);
Andreas Gampeac587272017-01-05 15:21:34 -080046
47 testClassFields(Integer.class);
48 testClassFields(int.class);
49 testClassFields(String[].class);
Andreas Gampeff9d2092017-01-06 09:12:49 -080050
Andreas Gampe18fee4d2017-01-06 11:36:35 -080051 testClassMethods(Integer.class);
52 testClassMethods(int.class);
53 testClassMethods(String[].class);
54
Andreas Gampeff9d2092017-01-06 09:12:49 -080055 testClassStatus(int.class);
56 testClassStatus(String[].class);
57 testClassStatus(Object.class);
58 testClassStatus(TestForNonInit.class);
59 try {
60 System.out.println(TestForInitFail.dummy);
61 } catch (ExceptionInInitializerError e) {
62 }
63 testClassStatus(TestForInitFail.class);
Andreas Gampe8b07e472017-01-06 14:20:39 -080064
65 testInterfaces(int.class);
66 testInterfaces(String[].class);
67 testInterfaces(Object.class);
68 testInterfaces(InfA.class);
69 testInterfaces(InfB.class);
70 testInterfaces(InfC.class);
71 testInterfaces(ClassA.class);
72 testInterfaces(ClassB.class);
73 testInterfaces(ClassC.class);
Andreas Gampe8f5b6032017-01-06 15:50:55 -080074
75 testClassLoader(String.class);
76 testClassLoader(String[].class);
77 testClassLoader(InfA.class);
78 testClassLoader(getProxyClass());
Andreas Gampe70f16392017-01-16 14:20:10 -080079
80 testClassLoaderClasses();
Andreas Gampe812a2442017-01-19 22:04:46 -080081
82 System.out.println();
83
84 testClassVersion();
Andreas Gampee492ae32016-10-28 19:34:57 -070085 }
86
87 private static Class<?> proxyClass = null;
88
89 private static Class<?> getProxyClass() throws Exception {
90 if (proxyClass != null) {
91 return proxyClass;
92 }
93
94 proxyClass = Proxy.getProxyClass(Main.class.getClassLoader(), new Class[] { Runnable.class });
95 return proxyClass;
96 }
97
98 private static void testClass(String className) throws Exception {
99 Class<?> base = Class.forName(className);
100 testClass(base);
101 }
102
103 private static void testClass(Class<?> base) throws Exception {
104 String[] result = getClassSignature(base);
105 System.out.println(Arrays.toString(result));
Andreas Gampe64013e52017-01-06 13:07:19 -0800106 int mod = getClassModifiers(base);
107 if (mod != base.getModifiers()) {
108 throw new RuntimeException("Unexpected modifiers: " + base.getModifiers() + " vs " + mod);
109 }
110 System.out.println(Integer.toHexString(mod));
Andreas Gampee492ae32016-10-28 19:34:57 -0700111 }
112
Andreas Gampe4fd66ec2017-01-05 14:42:13 -0800113 private static void testClassType(Class<?> c) throws Exception {
114 boolean isInterface = isInterface(c);
115 boolean isArray = isArrayClass(c);
Alex Lighte4a88632017-01-10 07:41:24 -0800116 boolean isModifiable = isModifiableClass(c);
117 System.out.println(c.getName() + " interface=" + isInterface + " array=" + isArray +
118 " modifiable=" + isModifiable);
Andreas Gampe4fd66ec2017-01-05 14:42:13 -0800119 }
120
Andreas Gampeac587272017-01-05 15:21:34 -0800121 private static void testClassFields(Class<?> c) throws Exception {
122 System.out.println(Arrays.toString(getClassFields(c)));
123 }
124
Andreas Gampe18fee4d2017-01-06 11:36:35 -0800125 private static void testClassMethods(Class<?> c) throws Exception {
126 System.out.println(Arrays.toString(getClassMethods(c)));
127 }
128
Andreas Gampeff9d2092017-01-06 09:12:49 -0800129 private static void testClassStatus(Class<?> c) {
130 System.out.println(c + " " + Integer.toBinaryString(getClassStatus(c)));
131 }
132
Andreas Gampe8b07e472017-01-06 14:20:39 -0800133 private static void testInterfaces(Class<?> c) {
134 System.out.println(c + " " + Arrays.toString(getImplementedInterfaces(c)));
135 }
136
Andreas Gampe8f5b6032017-01-06 15:50:55 -0800137 private static boolean IsBootClassLoader(ClassLoader l) {
138 // Hacky check for Android's fake boot classloader.
139 return l.getClass().getName().equals("java.lang.BootClassLoader");
140 }
141
142 private static void testClassLoader(Class<?> c) {
143 Object cl = getClassLoader(c);
144 System.out.println(c + " " + (cl != null ? cl.getClass().getName() : "null"));
145 if (cl == null) {
146 if (c.getClassLoader() != null && !IsBootClassLoader(c.getClassLoader())) {
147 throw new RuntimeException("Expected " + c.getClassLoader() + ", but got null.");
148 }
149 } else {
150 if (!(cl instanceof ClassLoader)) {
151 throw new RuntimeException("Unexpected \"classloader\": " + cl + " (" + cl.getClass() +
152 ")");
153 }
154 if (cl != c.getClassLoader()) {
155 throw new RuntimeException("Unexpected classloader: " + c.getClassLoader() + " vs " + cl);
156 }
157 }
158 }
159
Andreas Gampe70f16392017-01-16 14:20:10 -0800160 private static void testClassLoaderClasses() throws Exception {
161 ClassLoader boot = ClassLoader.getSystemClassLoader().getParent();
162 while (boot.getParent() != null) {
163 boot = boot.getParent();
164 }
165
166 System.out.println();
167 System.out.println("boot <- src <- src-ex (A,B)");
168 ClassLoader cl1 = create(create(boot, DEX1), DEX2);
169 Class.forName("B", false, cl1);
170 Class.forName("A", false, cl1);
171 printClassLoaderClasses(cl1);
172
173 System.out.println();
174 System.out.println("boot <- src (B) <- src-ex (A, List)");
175 ClassLoader cl2 = create(create(boot, DEX1), DEX2);
176 Class.forName("A", false, cl2);
177 Class.forName("java.util.List", false, cl2);
178 Class.forName("B", false, cl2.getParent());
179 printClassLoaderClasses(cl2);
180
181 System.out.println();
182 System.out.println("boot <- src+src-ex (A,B)");
183 ClassLoader cl3 = create(boot, DEX1, DEX2);
184 Class.forName("B", false, cl3);
185 Class.forName("A", false, cl3);
186 printClassLoaderClasses(cl3);
187
188 // Check that the boot classloader dumps something non-empty.
189 Class<?>[] bootClasses = getClassLoaderClasses(boot);
190 if (bootClasses.length == 0) {
191 throw new RuntimeException("No classes initiated by boot classloader.");
192 }
193 // Check that at least java.util.List is loaded.
194 boolean foundList = false;
195 for (Class<?> c : bootClasses) {
196 if (c == java.util.List.class) {
197 foundList = true;
198 break;
199 }
200 }
201 if (!foundList) {
202 System.out.println(Arrays.toString(bootClasses));
203 throw new RuntimeException("Could not find class java.util.List.");
204 }
205 }
206
Andreas Gampe812a2442017-01-19 22:04:46 -0800207 private static void testClassVersion() {
208 System.out.println(Arrays.toString(getClassVersion(Main.class)));
209 }
210
Andreas Gampe70f16392017-01-16 14:20:10 -0800211 private static void printClassLoaderClasses(ClassLoader cl) {
212 for (;;) {
213 if (cl == null || !cl.getClass().getName().startsWith("dalvik.system")) {
214 break;
215 }
216
217 ClassLoader saved = cl;
218 for (;;) {
219 if (cl == null || !cl.getClass().getName().startsWith("dalvik.system")) {
220 break;
221 }
222 String s = cl.toString();
223 int index1 = s.indexOf("zip file");
224 int index2 = s.indexOf(']', index1);
225 if (index2 < 0) {
226 throw new RuntimeException("Unexpected classloader " + s);
227 }
228 String zip_file = s.substring(index1, index2);
229 int index3 = zip_file.indexOf('"');
230 int index4 = zip_file.indexOf('"', index3 + 1);
231 if (index4 < 0) {
232 throw new RuntimeException("Unexpected classloader " + s);
233 }
234 String paths = zip_file.substring(index3 + 1, index4);
235 String pathArray[] = paths.split(":");
236 for (String path : pathArray) {
237 int index5 = path.lastIndexOf('/');
238 System.out.print(path.substring(index5 + 1));
239 System.out.print('+');
240 }
241 System.out.print(" -> ");
242 cl = cl.getParent();
243 }
244 System.out.println();
245 Class<?> classes[] = getClassLoaderClasses(saved);
246 Arrays.sort(classes, new ClassNameComparator());
247 System.out.println(Arrays.toString(classes));
248
249 cl = saved.getParent();
250 }
251 }
252
Alex Lighte4a88632017-01-10 07:41:24 -0800253 private static native boolean isModifiableClass(Class<?> c);
Andreas Gampee492ae32016-10-28 19:34:57 -0700254 private static native String[] getClassSignature(Class<?> c);
Andreas Gampe4fd66ec2017-01-05 14:42:13 -0800255
256 private static native boolean isInterface(Class<?> c);
257 private static native boolean isArrayClass(Class<?> c);
Andreas Gampeac587272017-01-05 15:21:34 -0800258
Andreas Gampe64013e52017-01-06 13:07:19 -0800259 private static native int getClassModifiers(Class<?> c);
260
Andreas Gampeac587272017-01-05 15:21:34 -0800261 private static native Object[] getClassFields(Class<?> c);
Andreas Gampe18fee4d2017-01-06 11:36:35 -0800262 private static native Object[] getClassMethods(Class<?> c);
Andreas Gampe70f16392017-01-16 14:20:10 -0800263 private static native Class<?>[] getImplementedInterfaces(Class<?> c);
Andreas Gampeff9d2092017-01-06 09:12:49 -0800264
265 private static native int getClassStatus(Class<?> c);
266
Andreas Gampe8f5b6032017-01-06 15:50:55 -0800267 private static native Object getClassLoader(Class<?> c);
268
Andreas Gampe70f16392017-01-16 14:20:10 -0800269 private static native Class<?>[] getClassLoaderClasses(ClassLoader cl);
270
Andreas Gampe812a2442017-01-19 22:04:46 -0800271 private static native int[] getClassVersion(Class<?> c);
272
Andreas Gampeff9d2092017-01-06 09:12:49 -0800273 private static class TestForNonInit {
274 public static double dummy = Math.random(); // So it can't be compile-time initialized.
275 }
276
277 private static class TestForInitFail {
278 public static int dummy = ((int)Math.random())/0; // So it throws when initializing.
279 }
Andreas Gampe8b07e472017-01-06 14:20:39 -0800280
281 public static interface InfA {
282 }
283 public static interface InfB extends InfA {
284 }
285 public static interface InfC extends InfB {
286 }
287
288 public abstract static class ClassA implements InfA {
289 }
290 public abstract static class ClassB extends ClassA implements InfB {
291 }
292 public abstract static class ClassC implements InfA, InfC {
293 }
Andreas Gampe70f16392017-01-16 14:20:10 -0800294
295 private static final String DEX1 = System.getenv("DEX_LOCATION") + "/912-classes.jar";
296 private static final String DEX2 = System.getenv("DEX_LOCATION") + "/912-classes-ex.jar";
297
298 private static ClassLoader create(ClassLoader parent, String... elements) throws Exception {
299 // Note: We use a PathClassLoader, as we do not care about code performance. We only load
300 // the classes, and they're empty.
301 Class<?> pathClassLoaderClass = Class.forName("dalvik.system.PathClassLoader");
302 Constructor<?> pathClassLoaderInit = pathClassLoaderClass.getConstructor(String.class,
303 ClassLoader.class);
304 String path = String.join(":", elements);
305 return (ClassLoader) pathClassLoaderInit.newInstance(path, parent);
306 }
307
308 private static class ClassNameComparator implements Comparator<Class<?>> {
309 public int compare(Class<?> c1, Class<?> c2) {
310 return c1.getName().compareTo(c2.getName());
311 }
312 }
Andreas Gampee492ae32016-10-28 19:34:57 -0700313}