blob: 52a5194138667750e4a901c1f6471f69df8564b3 [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
Andreas Gampee2744c62017-02-08 16:28:59 +0000222 // The JIT may deeply inline and load some classes. Preload these for test determinism.
223 final String PRELOAD_FOR_JIT[] = {
224 "java.nio.charset.CoderMalfunctionError",
225 "java.util.NoSuchElementException"
226 };
227 for (String s : PRELOAD_FOR_JIT) {
228 Class.forName(s);
229 }
230
Andreas Gampe19958592017-01-23 17:29:07 -0800231 Runnable r = new Runnable() {
232 @Override
233 public void run() {
234 try {
235 ClassLoader cl6 = create(boot, DEX1, DEX2);
236 System.out.println("C, true");
237 Class.forName("C", true, cl6);
238 } catch (Exception e) {
239 throw new RuntimeException(e);
240 }
241 }
242 };
243
Andreas Gampe41526612017-01-23 22:48:15 -0800244 Thread dummyThread = new Thread();
245 dummyThread.start();
246 dummyThread.join();
247
248 ensureJitCompiled(Main.class, "testClassEvents");
249
Andreas Gampee2744c62017-02-08 16:28:59 +0000250 enableClassLoadPreparePrintEvents(true);
Andreas Gampee6377462017-01-20 17:37:50 -0800251
252 ClassLoader cl1 = create(boot, DEX1, DEX2);
253 System.out.println("B, false");
254 Class.forName("B", false, cl1);
255
256 ClassLoader cl2 = create(boot, DEX1, DEX2);
257 System.out.println("B, true");
258 Class.forName("B", true, cl2);
259
260 ClassLoader cl3 = create(boot, DEX1, DEX2);
261 System.out.println("C, false");
262 Class.forName("C", false, cl3);
263 System.out.println("A, false");
264 Class.forName("A", false, cl3);
265
266 ClassLoader cl4 = create(boot, DEX1, DEX2);
267 System.out.println("C, true");
268 Class.forName("C", true, cl4);
269 System.out.println("A, true");
270 Class.forName("A", true, cl4);
271
272 ClassLoader cl5 = create(boot, DEX1, DEX2);
273 System.out.println("A, true");
274 Class.forName("A", true, cl5);
275 System.out.println("C, true");
276 Class.forName("C", true, cl5);
277
Andreas Gampee6377462017-01-20 17:37:50 -0800278 Thread t = new Thread(r, "TestRunner");
279 t.start();
280 t.join();
281
Andreas Gampee2744c62017-02-08 16:28:59 +0000282 enableClassLoadPreparePrintEvents(false);
283
284 // Note: the JIT part of this test is about the JIT pulling in a class not yet touched by
285 // anything else in the system. This could be the verifier or the interpreter. We
286 // block the interpreter by calling ensureJitCompiled. The verifier, however, must
287 // run in configurations where dex2oat didn't verify the class itself. So explicitly
288 // check whether the class has been already loaded, and skip then.
289 // TODO: Add multiple configurations to the run script once that becomes easier to do.
290 if (hasJit() && !isLoadedClass("Main$ClassD")) {
291 testClassEventsJit();
292 }
Andreas Gampe691051b2017-02-09 09:15:24 -0800293
294 testClassLoadPrepareEquality();
Andreas Gampee2744c62017-02-08 16:28:59 +0000295 }
296
297 private static void testClassEventsJit() throws Exception {
298 enableClassLoadSeenEvents(true);
299
300 testClassEventsJitImpl();
301
302 enableClassLoadSeenEvents(false);
303
304 if (!hadLoadEvent()) {
305 throw new RuntimeException("Did not get expected load event.");
306 }
307 }
308
309 private static void testClassEventsJitImpl() throws Exception {
310 ensureJitCompiled(Main.class, "testClassEventsJitImpl");
311
312 if (ClassD.x != 1) {
313 throw new RuntimeException("Unexpected value");
314 }
Andreas Gampee6377462017-01-20 17:37:50 -0800315 }
316
Andreas Gampe691051b2017-02-09 09:15:24 -0800317 private static void testClassLoadPrepareEquality() throws Exception {
Andreas Gampea67354b2017-02-10 16:18:30 -0800318 setEqualityEventStorageClass(ClassF.class);
319
Andreas Gampe691051b2017-02-09 09:15:24 -0800320 enableClassLoadPrepareEqualityEvents(true);
321
322 Class.forName("Main$ClassE");
323
324 enableClassLoadPrepareEqualityEvents(false);
325 }
326
Andreas Gampe70f16392017-01-16 14:20:10 -0800327 private static void printClassLoaderClasses(ClassLoader cl) {
328 for (;;) {
329 if (cl == null || !cl.getClass().getName().startsWith("dalvik.system")) {
330 break;
331 }
332
333 ClassLoader saved = cl;
334 for (;;) {
335 if (cl == null || !cl.getClass().getName().startsWith("dalvik.system")) {
336 break;
337 }
338 String s = cl.toString();
339 int index1 = s.indexOf("zip file");
340 int index2 = s.indexOf(']', index1);
341 if (index2 < 0) {
342 throw new RuntimeException("Unexpected classloader " + s);
343 }
344 String zip_file = s.substring(index1, index2);
345 int index3 = zip_file.indexOf('"');
346 int index4 = zip_file.indexOf('"', index3 + 1);
347 if (index4 < 0) {
348 throw new RuntimeException("Unexpected classloader " + s);
349 }
350 String paths = zip_file.substring(index3 + 1, index4);
351 String pathArray[] = paths.split(":");
352 for (String path : pathArray) {
353 int index5 = path.lastIndexOf('/');
354 System.out.print(path.substring(index5 + 1));
355 System.out.print('+');
356 }
357 System.out.print(" -> ");
358 cl = cl.getParent();
359 }
360 System.out.println();
361 Class<?> classes[] = getClassLoaderClasses(saved);
362 Arrays.sort(classes, new ClassNameComparator());
363 System.out.println(Arrays.toString(classes));
364
365 cl = saved.getParent();
366 }
367 }
368
Alex Lighte4a88632017-01-10 07:41:24 -0800369 private static native boolean isModifiableClass(Class<?> c);
Andreas Gampee492ae32016-10-28 19:34:57 -0700370 private static native String[] getClassSignature(Class<?> c);
Andreas Gampe4fd66ec2017-01-05 14:42:13 -0800371
372 private static native boolean isInterface(Class<?> c);
373 private static native boolean isArrayClass(Class<?> c);
Andreas Gampeac587272017-01-05 15:21:34 -0800374
Andreas Gampe64013e52017-01-06 13:07:19 -0800375 private static native int getClassModifiers(Class<?> c);
376
Andreas Gampeac587272017-01-05 15:21:34 -0800377 private static native Object[] getClassFields(Class<?> c);
Andreas Gampe18fee4d2017-01-06 11:36:35 -0800378 private static native Object[] getClassMethods(Class<?> c);
Andreas Gampe70f16392017-01-16 14:20:10 -0800379 private static native Class<?>[] getImplementedInterfaces(Class<?> c);
Andreas Gampeff9d2092017-01-06 09:12:49 -0800380
381 private static native int getClassStatus(Class<?> c);
382
Andreas Gampe8f5b6032017-01-06 15:50:55 -0800383 private static native Object getClassLoader(Class<?> c);
384
Andreas Gampe70f16392017-01-16 14:20:10 -0800385 private static native Class<?>[] getClassLoaderClasses(ClassLoader cl);
386
Andreas Gampe812a2442017-01-19 22:04:46 -0800387 private static native int[] getClassVersion(Class<?> c);
388
Andreas Gampee2744c62017-02-08 16:28:59 +0000389 private static native void enableClassLoadPreparePrintEvents(boolean b);
Andreas Gampee6377462017-01-20 17:37:50 -0800390
Andreas Gampee2744c62017-02-08 16:28:59 +0000391 private static native void ensureJitCompiled(Class<?> c, String name);
392
393 private static native boolean hasJit();
394 private static native boolean isLoadedClass(String name);
395 private static native void enableClassLoadSeenEvents(boolean b);
396 private static native boolean hadLoadEvent();
Andreas Gampe41526612017-01-23 22:48:15 -0800397
Andreas Gampea67354b2017-02-10 16:18:30 -0800398 private static native void setEqualityEventStorageClass(Class<?> c);
Andreas Gampe691051b2017-02-09 09:15:24 -0800399 private static native void enableClassLoadPrepareEqualityEvents(boolean b);
400
Andreas Gampeff9d2092017-01-06 09:12:49 -0800401 private static class TestForNonInit {
402 public static double dummy = Math.random(); // So it can't be compile-time initialized.
403 }
404
405 private static class TestForInitFail {
406 public static int dummy = ((int)Math.random())/0; // So it throws when initializing.
407 }
Andreas Gampe8b07e472017-01-06 14:20:39 -0800408
409 public static interface InfA {
410 }
411 public static interface InfB extends InfA {
412 }
413 public static interface InfC extends InfB {
414 }
415
416 public abstract static class ClassA implements InfA {
417 }
418 public abstract static class ClassB extends ClassA implements InfB {
419 }
420 public abstract static class ClassC implements InfA, InfC {
421 }
Andreas Gampe70f16392017-01-16 14:20:10 -0800422
Andreas Gampee2744c62017-02-08 16:28:59 +0000423 public static class ClassD {
424 static int x = 1;
425 }
426
Andreas Gampe691051b2017-02-09 09:15:24 -0800427 public static class ClassE {
428 public void foo() {
429 }
430 public void bar() {
431 }
432 }
433
Andreas Gampea67354b2017-02-10 16:18:30 -0800434 public static class ClassF {
435 public static Object STATIC = null;
436 }
437
Andreas Gampe70f16392017-01-16 14:20:10 -0800438 private static final String DEX1 = System.getenv("DEX_LOCATION") + "/912-classes.jar";
439 private static final String DEX2 = System.getenv("DEX_LOCATION") + "/912-classes-ex.jar";
440
441 private static ClassLoader create(ClassLoader parent, String... elements) throws Exception {
442 // Note: We use a PathClassLoader, as we do not care about code performance. We only load
443 // the classes, and they're empty.
444 Class<?> pathClassLoaderClass = Class.forName("dalvik.system.PathClassLoader");
445 Constructor<?> pathClassLoaderInit = pathClassLoaderClass.getConstructor(String.class,
446 ClassLoader.class);
447 String path = String.join(":", elements);
448 return (ClassLoader) pathClassLoaderInit.newInstance(path, parent);
449 }
450
451 private static class ClassNameComparator implements Comparator<Class<?>> {
452 public int compare(Class<?> c1, Class<?> c2) {
453 return c1.getName().compareTo(c2.getName());
454 }
455 }
Andreas Gampee492ae32016-10-28 19:34:57 -0700456}