blob: 859f80cd80f07d491f8fe63a551e8f21c350a283 [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 Gampee6377462017-01-20 17:37:50 -080085
86 System.out.println();
87
88 testClassEvents();
Andreas Gampee492ae32016-10-28 19:34:57 -070089 }
90
91 private static Class<?> proxyClass = null;
92
93 private static Class<?> getProxyClass() throws Exception {
94 if (proxyClass != null) {
95 return proxyClass;
96 }
97
98 proxyClass = Proxy.getProxyClass(Main.class.getClassLoader(), new Class[] { Runnable.class });
99 return proxyClass;
100 }
101
102 private static void testClass(String className) throws Exception {
103 Class<?> base = Class.forName(className);
104 testClass(base);
105 }
106
107 private static void testClass(Class<?> base) throws Exception {
108 String[] result = getClassSignature(base);
109 System.out.println(Arrays.toString(result));
Andreas Gampe64013e52017-01-06 13:07:19 -0800110 int mod = getClassModifiers(base);
111 if (mod != base.getModifiers()) {
112 throw new RuntimeException("Unexpected modifiers: " + base.getModifiers() + " vs " + mod);
113 }
114 System.out.println(Integer.toHexString(mod));
Andreas Gampee492ae32016-10-28 19:34:57 -0700115 }
116
Andreas Gampe4fd66ec2017-01-05 14:42:13 -0800117 private static void testClassType(Class<?> c) throws Exception {
118 boolean isInterface = isInterface(c);
119 boolean isArray = isArrayClass(c);
Alex Lighte4a88632017-01-10 07:41:24 -0800120 boolean isModifiable = isModifiableClass(c);
121 System.out.println(c.getName() + " interface=" + isInterface + " array=" + isArray +
122 " modifiable=" + isModifiable);
Andreas Gampe4fd66ec2017-01-05 14:42:13 -0800123 }
124
Andreas Gampeac587272017-01-05 15:21:34 -0800125 private static void testClassFields(Class<?> c) throws Exception {
126 System.out.println(Arrays.toString(getClassFields(c)));
127 }
128
Andreas Gampe18fee4d2017-01-06 11:36:35 -0800129 private static void testClassMethods(Class<?> c) throws Exception {
130 System.out.println(Arrays.toString(getClassMethods(c)));
131 }
132
Andreas Gampeff9d2092017-01-06 09:12:49 -0800133 private static void testClassStatus(Class<?> c) {
134 System.out.println(c + " " + Integer.toBinaryString(getClassStatus(c)));
135 }
136
Andreas Gampe8b07e472017-01-06 14:20:39 -0800137 private static void testInterfaces(Class<?> c) {
138 System.out.println(c + " " + Arrays.toString(getImplementedInterfaces(c)));
139 }
140
Andreas Gampe8f5b6032017-01-06 15:50:55 -0800141 private static boolean IsBootClassLoader(ClassLoader l) {
142 // Hacky check for Android's fake boot classloader.
143 return l.getClass().getName().equals("java.lang.BootClassLoader");
144 }
145
146 private static void testClassLoader(Class<?> c) {
147 Object cl = getClassLoader(c);
148 System.out.println(c + " " + (cl != null ? cl.getClass().getName() : "null"));
149 if (cl == null) {
150 if (c.getClassLoader() != null && !IsBootClassLoader(c.getClassLoader())) {
151 throw new RuntimeException("Expected " + c.getClassLoader() + ", but got null.");
152 }
153 } else {
154 if (!(cl instanceof ClassLoader)) {
155 throw new RuntimeException("Unexpected \"classloader\": " + cl + " (" + cl.getClass() +
156 ")");
157 }
158 if (cl != c.getClassLoader()) {
159 throw new RuntimeException("Unexpected classloader: " + c.getClassLoader() + " vs " + cl);
160 }
161 }
162 }
163
Andreas Gampe70f16392017-01-16 14:20:10 -0800164 private static void testClassLoaderClasses() throws Exception {
165 ClassLoader boot = ClassLoader.getSystemClassLoader().getParent();
166 while (boot.getParent() != null) {
167 boot = boot.getParent();
168 }
169
170 System.out.println();
171 System.out.println("boot <- src <- src-ex (A,B)");
172 ClassLoader cl1 = create(create(boot, DEX1), DEX2);
173 Class.forName("B", false, cl1);
174 Class.forName("A", false, cl1);
175 printClassLoaderClasses(cl1);
176
177 System.out.println();
178 System.out.println("boot <- src (B) <- src-ex (A, List)");
179 ClassLoader cl2 = create(create(boot, DEX1), DEX2);
180 Class.forName("A", false, cl2);
181 Class.forName("java.util.List", false, cl2);
182 Class.forName("B", false, cl2.getParent());
183 printClassLoaderClasses(cl2);
184
185 System.out.println();
186 System.out.println("boot <- src+src-ex (A,B)");
187 ClassLoader cl3 = create(boot, DEX1, DEX2);
188 Class.forName("B", false, cl3);
189 Class.forName("A", false, cl3);
190 printClassLoaderClasses(cl3);
191
192 // Check that the boot classloader dumps something non-empty.
193 Class<?>[] bootClasses = getClassLoaderClasses(boot);
194 if (bootClasses.length == 0) {
195 throw new RuntimeException("No classes initiated by boot classloader.");
196 }
197 // Check that at least java.util.List is loaded.
198 boolean foundList = false;
199 for (Class<?> c : bootClasses) {
200 if (c == java.util.List.class) {
201 foundList = true;
202 break;
203 }
204 }
205 if (!foundList) {
206 System.out.println(Arrays.toString(bootClasses));
207 throw new RuntimeException("Could not find class java.util.List.");
208 }
209 }
210
Andreas Gampe812a2442017-01-19 22:04:46 -0800211 private static void testClassVersion() {
212 System.out.println(Arrays.toString(getClassVersion(Main.class)));
213 }
214
Andreas Gampee6377462017-01-20 17:37:50 -0800215 private static void testClassEvents() throws Exception {
216 ClassLoader cl = Main.class.getClassLoader();
217 while (cl.getParent() != null) {
218 cl = cl.getParent();
219 }
220 final ClassLoader boot = cl;
221
222 enableClassLoadEvents(true);
223
224 ClassLoader cl1 = create(boot, DEX1, DEX2);
225 System.out.println("B, false");
226 Class.forName("B", false, cl1);
227
228 ClassLoader cl2 = create(boot, DEX1, DEX2);
229 System.out.println("B, true");
230 Class.forName("B", true, cl2);
231
232 ClassLoader cl3 = create(boot, DEX1, DEX2);
233 System.out.println("C, false");
234 Class.forName("C", false, cl3);
235 System.out.println("A, false");
236 Class.forName("A", false, cl3);
237
238 ClassLoader cl4 = create(boot, DEX1, DEX2);
239 System.out.println("C, true");
240 Class.forName("C", true, cl4);
241 System.out.println("A, true");
242 Class.forName("A", true, cl4);
243
244 ClassLoader cl5 = create(boot, DEX1, DEX2);
245 System.out.println("A, true");
246 Class.forName("A", true, cl5);
247 System.out.println("C, true");
248 Class.forName("C", true, cl5);
249
250 Runnable r = new Runnable() {
251 @Override
252 public void run() {
253 try {
254 ClassLoader cl6 = create(boot, DEX1, DEX2);
255 System.out.println("C, true");
256 Class.forName("C", true, cl6);
257 } catch (Exception e) {
258 throw new RuntimeException(e);
259 }
260 }
261 };
262 Thread t = new Thread(r, "TestRunner");
263 t.start();
264 t.join();
265
266 enableClassLoadEvents(false);
267 }
268
Andreas Gampe70f16392017-01-16 14:20:10 -0800269 private static void printClassLoaderClasses(ClassLoader cl) {
270 for (;;) {
271 if (cl == null || !cl.getClass().getName().startsWith("dalvik.system")) {
272 break;
273 }
274
275 ClassLoader saved = cl;
276 for (;;) {
277 if (cl == null || !cl.getClass().getName().startsWith("dalvik.system")) {
278 break;
279 }
280 String s = cl.toString();
281 int index1 = s.indexOf("zip file");
282 int index2 = s.indexOf(']', index1);
283 if (index2 < 0) {
284 throw new RuntimeException("Unexpected classloader " + s);
285 }
286 String zip_file = s.substring(index1, index2);
287 int index3 = zip_file.indexOf('"');
288 int index4 = zip_file.indexOf('"', index3 + 1);
289 if (index4 < 0) {
290 throw new RuntimeException("Unexpected classloader " + s);
291 }
292 String paths = zip_file.substring(index3 + 1, index4);
293 String pathArray[] = paths.split(":");
294 for (String path : pathArray) {
295 int index5 = path.lastIndexOf('/');
296 System.out.print(path.substring(index5 + 1));
297 System.out.print('+');
298 }
299 System.out.print(" -> ");
300 cl = cl.getParent();
301 }
302 System.out.println();
303 Class<?> classes[] = getClassLoaderClasses(saved);
304 Arrays.sort(classes, new ClassNameComparator());
305 System.out.println(Arrays.toString(classes));
306
307 cl = saved.getParent();
308 }
309 }
310
Alex Lighte4a88632017-01-10 07:41:24 -0800311 private static native boolean isModifiableClass(Class<?> c);
Andreas Gampee492ae32016-10-28 19:34:57 -0700312 private static native String[] getClassSignature(Class<?> c);
Andreas Gampe4fd66ec2017-01-05 14:42:13 -0800313
314 private static native boolean isInterface(Class<?> c);
315 private static native boolean isArrayClass(Class<?> c);
Andreas Gampeac587272017-01-05 15:21:34 -0800316
Andreas Gampe64013e52017-01-06 13:07:19 -0800317 private static native int getClassModifiers(Class<?> c);
318
Andreas Gampeac587272017-01-05 15:21:34 -0800319 private static native Object[] getClassFields(Class<?> c);
Andreas Gampe18fee4d2017-01-06 11:36:35 -0800320 private static native Object[] getClassMethods(Class<?> c);
Andreas Gampe70f16392017-01-16 14:20:10 -0800321 private static native Class<?>[] getImplementedInterfaces(Class<?> c);
Andreas Gampeff9d2092017-01-06 09:12:49 -0800322
323 private static native int getClassStatus(Class<?> c);
324
Andreas Gampe8f5b6032017-01-06 15:50:55 -0800325 private static native Object getClassLoader(Class<?> c);
326
Andreas Gampe70f16392017-01-16 14:20:10 -0800327 private static native Class<?>[] getClassLoaderClasses(ClassLoader cl);
328
Andreas Gampe812a2442017-01-19 22:04:46 -0800329 private static native int[] getClassVersion(Class<?> c);
330
Andreas Gampee6377462017-01-20 17:37:50 -0800331 private static native void enableClassLoadEvents(boolean b);
332
Andreas Gampeff9d2092017-01-06 09:12:49 -0800333 private static class TestForNonInit {
334 public static double dummy = Math.random(); // So it can't be compile-time initialized.
335 }
336
337 private static class TestForInitFail {
338 public static int dummy = ((int)Math.random())/0; // So it throws when initializing.
339 }
Andreas Gampe8b07e472017-01-06 14:20:39 -0800340
341 public static interface InfA {
342 }
343 public static interface InfB extends InfA {
344 }
345 public static interface InfC extends InfB {
346 }
347
348 public abstract static class ClassA implements InfA {
349 }
350 public abstract static class ClassB extends ClassA implements InfB {
351 }
352 public abstract static class ClassC implements InfA, InfC {
353 }
Andreas Gampe70f16392017-01-16 14:20:10 -0800354
355 private static final String DEX1 = System.getenv("DEX_LOCATION") + "/912-classes.jar";
356 private static final String DEX2 = System.getenv("DEX_LOCATION") + "/912-classes-ex.jar";
357
358 private static ClassLoader create(ClassLoader parent, String... elements) throws Exception {
359 // Note: We use a PathClassLoader, as we do not care about code performance. We only load
360 // the classes, and they're empty.
361 Class<?> pathClassLoaderClass = Class.forName("dalvik.system.PathClassLoader");
362 Constructor<?> pathClassLoaderInit = pathClassLoaderClass.getConstructor(String.class,
363 ClassLoader.class);
364 String path = String.join(":", elements);
365 return (ClassLoader) pathClassLoaderInit.newInstance(path, parent);
366 }
367
368 private static class ClassNameComparator implements Comparator<Class<?>> {
369 public int compare(Class<?> c1, Class<?> c2) {
370 return c1.getName().compareTo(c2.getName());
371 }
372 }
Andreas Gampee492ae32016-10-28 19:34:57 -0700373}