blob: 643b08076e0fda00fe54871c5a5d93fa0d337225 [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 Gampe52784ac2017-02-13 18:10:09 -080017import java.lang.ref.Reference;
Andreas Gampe70f16392017-01-16 14:20:10 -080018import java.lang.reflect.Constructor;
Andreas Gampee492ae32016-10-28 19:34:57 -070019import java.lang.reflect.Proxy;
Alex Light09f274f2017-02-21 15:00:48 -080020import java.util.ArrayList;
Andreas Gampee492ae32016-10-28 19:34:57 -070021import java.util.Arrays;
Andreas Gampe70f16392017-01-16 14:20:10 -080022import java.util.Comparator;
Andreas Gampee492ae32016-10-28 19:34:57 -070023
24public class Main {
25 public static void main(String[] args) throws Exception {
Andreas Gampee492ae32016-10-28 19:34:57 -070026 doTest();
27 }
28
29 public static void doTest() throws Exception {
30 testClass("java.lang.Object");
31 testClass("java.lang.String");
32 testClass("java.lang.Math");
33 testClass("java.util.List");
34
35 testClass(getProxyClass());
36
37 testClass(int.class);
38 testClass(double[].class);
Andreas Gampe4fd66ec2017-01-05 14:42:13 -080039
40 testClassType(int.class);
41 testClassType(getProxyClass());
42 testClassType(Runnable.class);
43 testClassType(String.class);
Alex Light09f274f2017-02-21 15:00:48 -080044 testClassType(ArrayList.class);
Andreas Gampe4fd66ec2017-01-05 14:42:13 -080045
46 testClassType(int[].class);
47 testClassType(Runnable[].class);
48 testClassType(String[].class);
Andreas Gampeac587272017-01-05 15:21:34 -080049
50 testClassFields(Integer.class);
51 testClassFields(int.class);
52 testClassFields(String[].class);
Andreas Gampeff9d2092017-01-06 09:12:49 -080053
Andreas Gampe18fee4d2017-01-06 11:36:35 -080054 testClassMethods(Integer.class);
55 testClassMethods(int.class);
56 testClassMethods(String[].class);
57
Andreas Gampeff9d2092017-01-06 09:12:49 -080058 testClassStatus(int.class);
59 testClassStatus(String[].class);
60 testClassStatus(Object.class);
61 testClassStatus(TestForNonInit.class);
62 try {
63 System.out.println(TestForInitFail.dummy);
64 } catch (ExceptionInInitializerError e) {
65 }
66 testClassStatus(TestForInitFail.class);
Andreas Gampe8b07e472017-01-06 14:20:39 -080067
68 testInterfaces(int.class);
69 testInterfaces(String[].class);
70 testInterfaces(Object.class);
71 testInterfaces(InfA.class);
72 testInterfaces(InfB.class);
73 testInterfaces(InfC.class);
74 testInterfaces(ClassA.class);
75 testInterfaces(ClassB.class);
76 testInterfaces(ClassC.class);
Andreas Gampe8f5b6032017-01-06 15:50:55 -080077
78 testClassLoader(String.class);
79 testClassLoader(String[].class);
80 testClassLoader(InfA.class);
81 testClassLoader(getProxyClass());
Andreas Gampe70f16392017-01-16 14:20:10 -080082
83 testClassLoaderClasses();
Andreas Gampe812a2442017-01-19 22:04:46 -080084
85 System.out.println();
86
87 testClassVersion();
Andreas Gampee6377462017-01-20 17:37:50 -080088
89 System.out.println();
90
91 testClassEvents();
Andreas Gampee492ae32016-10-28 19:34:57 -070092 }
93
94 private static Class<?> proxyClass = null;
95
96 private static Class<?> getProxyClass() throws Exception {
97 if (proxyClass != null) {
98 return proxyClass;
99 }
100
101 proxyClass = Proxy.getProxyClass(Main.class.getClassLoader(), new Class[] { Runnable.class });
102 return proxyClass;
103 }
104
105 private static void testClass(String className) throws Exception {
106 Class<?> base = Class.forName(className);
107 testClass(base);
108 }
109
110 private static void testClass(Class<?> base) throws Exception {
111 String[] result = getClassSignature(base);
112 System.out.println(Arrays.toString(result));
Andreas Gampe64013e52017-01-06 13:07:19 -0800113 int mod = getClassModifiers(base);
114 if (mod != base.getModifiers()) {
115 throw new RuntimeException("Unexpected modifiers: " + base.getModifiers() + " vs " + mod);
116 }
117 System.out.println(Integer.toHexString(mod));
Andreas Gampee492ae32016-10-28 19:34:57 -0700118 }
119
Andreas Gampe4fd66ec2017-01-05 14:42:13 -0800120 private static void testClassType(Class<?> c) throws Exception {
121 boolean isInterface = isInterface(c);
122 boolean isArray = isArrayClass(c);
Alex Lighte4a88632017-01-10 07:41:24 -0800123 boolean isModifiable = isModifiableClass(c);
124 System.out.println(c.getName() + " interface=" + isInterface + " array=" + isArray +
125 " modifiable=" + isModifiable);
Andreas Gampe4fd66ec2017-01-05 14:42:13 -0800126 }
127
Andreas Gampeac587272017-01-05 15:21:34 -0800128 private static void testClassFields(Class<?> c) throws Exception {
129 System.out.println(Arrays.toString(getClassFields(c)));
130 }
131
Andreas Gampe18fee4d2017-01-06 11:36:35 -0800132 private static void testClassMethods(Class<?> c) throws Exception {
133 System.out.println(Arrays.toString(getClassMethods(c)));
134 }
135
Andreas Gampeff9d2092017-01-06 09:12:49 -0800136 private static void testClassStatus(Class<?> c) {
137 System.out.println(c + " " + Integer.toBinaryString(getClassStatus(c)));
138 }
139
Andreas Gampe8b07e472017-01-06 14:20:39 -0800140 private static void testInterfaces(Class<?> c) {
141 System.out.println(c + " " + Arrays.toString(getImplementedInterfaces(c)));
142 }
143
Andreas Gampe8f5b6032017-01-06 15:50:55 -0800144 private static boolean IsBootClassLoader(ClassLoader l) {
145 // Hacky check for Android's fake boot classloader.
146 return l.getClass().getName().equals("java.lang.BootClassLoader");
147 }
148
149 private static void testClassLoader(Class<?> c) {
150 Object cl = getClassLoader(c);
151 System.out.println(c + " " + (cl != null ? cl.getClass().getName() : "null"));
152 if (cl == null) {
153 if (c.getClassLoader() != null && !IsBootClassLoader(c.getClassLoader())) {
154 throw new RuntimeException("Expected " + c.getClassLoader() + ", but got null.");
155 }
156 } else {
157 if (!(cl instanceof ClassLoader)) {
158 throw new RuntimeException("Unexpected \"classloader\": " + cl + " (" + cl.getClass() +
159 ")");
160 }
161 if (cl != c.getClassLoader()) {
162 throw new RuntimeException("Unexpected classloader: " + c.getClassLoader() + " vs " + cl);
163 }
164 }
165 }
166
Andreas Gampe70f16392017-01-16 14:20:10 -0800167 private static void testClassLoaderClasses() throws Exception {
168 ClassLoader boot = ClassLoader.getSystemClassLoader().getParent();
169 while (boot.getParent() != null) {
170 boot = boot.getParent();
171 }
172
173 System.out.println();
174 System.out.println("boot <- src <- src-ex (A,B)");
175 ClassLoader cl1 = create(create(boot, DEX1), DEX2);
176 Class.forName("B", false, cl1);
177 Class.forName("A", false, cl1);
178 printClassLoaderClasses(cl1);
179
180 System.out.println();
181 System.out.println("boot <- src (B) <- src-ex (A, List)");
182 ClassLoader cl2 = create(create(boot, DEX1), DEX2);
183 Class.forName("A", false, cl2);
184 Class.forName("java.util.List", false, cl2);
185 Class.forName("B", false, cl2.getParent());
186 printClassLoaderClasses(cl2);
187
188 System.out.println();
189 System.out.println("boot <- src+src-ex (A,B)");
190 ClassLoader cl3 = create(boot, DEX1, DEX2);
191 Class.forName("B", false, cl3);
192 Class.forName("A", false, cl3);
193 printClassLoaderClasses(cl3);
194
195 // Check that the boot classloader dumps something non-empty.
196 Class<?>[] bootClasses = getClassLoaderClasses(boot);
197 if (bootClasses.length == 0) {
198 throw new RuntimeException("No classes initiated by boot classloader.");
199 }
200 // Check that at least java.util.List is loaded.
201 boolean foundList = false;
202 for (Class<?> c : bootClasses) {
203 if (c == java.util.List.class) {
204 foundList = true;
205 break;
206 }
207 }
208 if (!foundList) {
209 System.out.println(Arrays.toString(bootClasses));
210 throw new RuntimeException("Could not find class java.util.List.");
211 }
212 }
213
Andreas Gampe812a2442017-01-19 22:04:46 -0800214 private static void testClassVersion() {
215 System.out.println(Arrays.toString(getClassVersion(Main.class)));
216 }
217
Andreas Gampee6377462017-01-20 17:37:50 -0800218 private static void testClassEvents() throws Exception {
219 ClassLoader cl = Main.class.getClassLoader();
220 while (cl.getParent() != null) {
221 cl = cl.getParent();
222 }
223 final ClassLoader boot = cl;
224
Andreas Gampee2744c62017-02-08 16:28:59 +0000225 // The JIT may deeply inline and load some classes. Preload these for test determinism.
226 final String PRELOAD_FOR_JIT[] = {
227 "java.nio.charset.CoderMalfunctionError",
228 "java.util.NoSuchElementException"
229 };
230 for (String s : PRELOAD_FOR_JIT) {
231 Class.forName(s);
232 }
233
Andreas Gampe19958592017-01-23 17:29:07 -0800234 Runnable r = new Runnable() {
235 @Override
236 public void run() {
237 try {
238 ClassLoader cl6 = create(boot, DEX1, DEX2);
239 System.out.println("C, true");
240 Class.forName("C", true, cl6);
241 } catch (Exception e) {
242 throw new RuntimeException(e);
243 }
244 }
245 };
246
Andreas Gampe41526612017-01-23 22:48:15 -0800247 Thread dummyThread = new Thread();
248 dummyThread.start();
249 dummyThread.join();
250
251 ensureJitCompiled(Main.class, "testClassEvents");
252
Andreas Gampee2744c62017-02-08 16:28:59 +0000253 enableClassLoadPreparePrintEvents(true);
Andreas Gampee6377462017-01-20 17:37:50 -0800254
255 ClassLoader cl1 = create(boot, DEX1, DEX2);
256 System.out.println("B, false");
257 Class.forName("B", false, cl1);
258
259 ClassLoader cl2 = create(boot, DEX1, DEX2);
260 System.out.println("B, true");
261 Class.forName("B", true, cl2);
262
263 ClassLoader cl3 = create(boot, DEX1, DEX2);
264 System.out.println("C, false");
265 Class.forName("C", false, cl3);
266 System.out.println("A, false");
267 Class.forName("A", false, cl3);
268
269 ClassLoader cl4 = create(boot, DEX1, DEX2);
270 System.out.println("C, true");
271 Class.forName("C", true, cl4);
272 System.out.println("A, true");
273 Class.forName("A", true, cl4);
274
275 ClassLoader cl5 = create(boot, DEX1, DEX2);
276 System.out.println("A, true");
277 Class.forName("A", true, cl5);
278 System.out.println("C, true");
279 Class.forName("C", true, cl5);
280
Andreas Gampee6377462017-01-20 17:37:50 -0800281 Thread t = new Thread(r, "TestRunner");
282 t.start();
283 t.join();
284
Andreas Gampe6cfd4c92017-04-06 08:03:32 -0700285 // Check creation of arrays and proxies.
286 Proxy.getProxyClass(Main.class.getClassLoader(), new Class[] { Comparable.class });
287 Class.forName("[LMain;");
288
Andreas Gampee2744c62017-02-08 16:28:59 +0000289 enableClassLoadPreparePrintEvents(false);
290
291 // Note: the JIT part of this test is about the JIT pulling in a class not yet touched by
292 // anything else in the system. This could be the verifier or the interpreter. We
293 // block the interpreter by calling ensureJitCompiled. The verifier, however, must
294 // run in configurations where dex2oat didn't verify the class itself. So explicitly
295 // check whether the class has been already loaded, and skip then.
296 // TODO: Add multiple configurations to the run script once that becomes easier to do.
297 if (hasJit() && !isLoadedClass("Main$ClassD")) {
298 testClassEventsJit();
299 }
Andreas Gampe691051b2017-02-09 09:15:24 -0800300
301 testClassLoadPrepareEquality();
Andreas Gampee2744c62017-02-08 16:28:59 +0000302 }
303
304 private static void testClassEventsJit() throws Exception {
305 enableClassLoadSeenEvents(true);
306
307 testClassEventsJitImpl();
308
309 enableClassLoadSeenEvents(false);
310
311 if (!hadLoadEvent()) {
312 throw new RuntimeException("Did not get expected load event.");
313 }
314 }
315
316 private static void testClassEventsJitImpl() throws Exception {
317 ensureJitCompiled(Main.class, "testClassEventsJitImpl");
318
319 if (ClassD.x != 1) {
320 throw new RuntimeException("Unexpected value");
321 }
Andreas Gampee6377462017-01-20 17:37:50 -0800322 }
323
Andreas Gampe691051b2017-02-09 09:15:24 -0800324 private static void testClassLoadPrepareEquality() throws Exception {
Andreas Gampea67354b2017-02-10 16:18:30 -0800325 setEqualityEventStorageClass(ClassF.class);
326
Andreas Gampe691051b2017-02-09 09:15:24 -0800327 enableClassLoadPrepareEqualityEvents(true);
328
329 Class.forName("Main$ClassE");
330
331 enableClassLoadPrepareEqualityEvents(false);
332 }
333
Andreas Gampe70f16392017-01-16 14:20:10 -0800334 private static void printClassLoaderClasses(ClassLoader cl) {
335 for (;;) {
336 if (cl == null || !cl.getClass().getName().startsWith("dalvik.system")) {
337 break;
338 }
339
340 ClassLoader saved = cl;
341 for (;;) {
342 if (cl == null || !cl.getClass().getName().startsWith("dalvik.system")) {
343 break;
344 }
345 String s = cl.toString();
346 int index1 = s.indexOf("zip file");
347 int index2 = s.indexOf(']', index1);
348 if (index2 < 0) {
349 throw new RuntimeException("Unexpected classloader " + s);
350 }
351 String zip_file = s.substring(index1, index2);
352 int index3 = zip_file.indexOf('"');
353 int index4 = zip_file.indexOf('"', index3 + 1);
354 if (index4 < 0) {
355 throw new RuntimeException("Unexpected classloader " + s);
356 }
357 String paths = zip_file.substring(index3 + 1, index4);
358 String pathArray[] = paths.split(":");
359 for (String path : pathArray) {
360 int index5 = path.lastIndexOf('/');
361 System.out.print(path.substring(index5 + 1));
362 System.out.print('+');
363 }
364 System.out.print(" -> ");
365 cl = cl.getParent();
366 }
367 System.out.println();
368 Class<?> classes[] = getClassLoaderClasses(saved);
369 Arrays.sort(classes, new ClassNameComparator());
370 System.out.println(Arrays.toString(classes));
371
372 cl = saved.getParent();
373 }
374 }
375
Alex Lighte4a88632017-01-10 07:41:24 -0800376 private static native boolean isModifiableClass(Class<?> c);
Andreas Gampee492ae32016-10-28 19:34:57 -0700377 private static native String[] getClassSignature(Class<?> c);
Andreas Gampe4fd66ec2017-01-05 14:42:13 -0800378
379 private static native boolean isInterface(Class<?> c);
380 private static native boolean isArrayClass(Class<?> c);
Andreas Gampeac587272017-01-05 15:21:34 -0800381
Andreas Gampe64013e52017-01-06 13:07:19 -0800382 private static native int getClassModifiers(Class<?> c);
383
Andreas Gampeac587272017-01-05 15:21:34 -0800384 private static native Object[] getClassFields(Class<?> c);
Andreas Gampe18fee4d2017-01-06 11:36:35 -0800385 private static native Object[] getClassMethods(Class<?> c);
Andreas Gampe70f16392017-01-16 14:20:10 -0800386 private static native Class<?>[] getImplementedInterfaces(Class<?> c);
Andreas Gampeff9d2092017-01-06 09:12:49 -0800387
388 private static native int getClassStatus(Class<?> c);
389
Andreas Gampe8f5b6032017-01-06 15:50:55 -0800390 private static native Object getClassLoader(Class<?> c);
391
Andreas Gampe70f16392017-01-16 14:20:10 -0800392 private static native Class<?>[] getClassLoaderClasses(ClassLoader cl);
393
Andreas Gampe812a2442017-01-19 22:04:46 -0800394 private static native int[] getClassVersion(Class<?> c);
395
Andreas Gampee2744c62017-02-08 16:28:59 +0000396 private static native void enableClassLoadPreparePrintEvents(boolean b);
Andreas Gampee6377462017-01-20 17:37:50 -0800397
Andreas Gampee2744c62017-02-08 16:28:59 +0000398 private static native void ensureJitCompiled(Class<?> c, String name);
399
400 private static native boolean hasJit();
401 private static native boolean isLoadedClass(String name);
402 private static native void enableClassLoadSeenEvents(boolean b);
403 private static native boolean hadLoadEvent();
Andreas Gampe41526612017-01-23 22:48:15 -0800404
Andreas Gampea67354b2017-02-10 16:18:30 -0800405 private static native void setEqualityEventStorageClass(Class<?> c);
Andreas Gampe691051b2017-02-09 09:15:24 -0800406 private static native void enableClassLoadPrepareEqualityEvents(boolean b);
407
Andreas Gampeff9d2092017-01-06 09:12:49 -0800408 private static class TestForNonInit {
409 public static double dummy = Math.random(); // So it can't be compile-time initialized.
410 }
411
412 private static class TestForInitFail {
413 public static int dummy = ((int)Math.random())/0; // So it throws when initializing.
414 }
Andreas Gampe8b07e472017-01-06 14:20:39 -0800415
416 public static interface InfA {
417 }
418 public static interface InfB extends InfA {
419 }
420 public static interface InfC extends InfB {
421 }
422
423 public abstract static class ClassA implements InfA {
424 }
425 public abstract static class ClassB extends ClassA implements InfB {
426 }
427 public abstract static class ClassC implements InfA, InfC {
428 }
Andreas Gampe70f16392017-01-16 14:20:10 -0800429
Andreas Gampee2744c62017-02-08 16:28:59 +0000430 public static class ClassD {
431 static int x = 1;
432 }
433
Andreas Gampe691051b2017-02-09 09:15:24 -0800434 public static class ClassE {
435 public void foo() {
436 }
437 public void bar() {
438 }
439 }
440
Andreas Gampea67354b2017-02-10 16:18:30 -0800441 public static class ClassF {
442 public static Object STATIC = null;
Andreas Gampe52784ac2017-02-13 18:10:09 -0800443 public static Reference<Object> WEAK = null;
Andreas Gampea67354b2017-02-10 16:18:30 -0800444 }
445
Andreas Gampe70f16392017-01-16 14:20:10 -0800446 private static final String DEX1 = System.getenv("DEX_LOCATION") + "/912-classes.jar";
447 private static final String DEX2 = System.getenv("DEX_LOCATION") + "/912-classes-ex.jar";
448
449 private static ClassLoader create(ClassLoader parent, String... elements) throws Exception {
450 // Note: We use a PathClassLoader, as we do not care about code performance. We only load
451 // the classes, and they're empty.
452 Class<?> pathClassLoaderClass = Class.forName("dalvik.system.PathClassLoader");
453 Constructor<?> pathClassLoaderInit = pathClassLoaderClass.getConstructor(String.class,
454 ClassLoader.class);
455 String path = String.join(":", elements);
456 return (ClassLoader) pathClassLoaderInit.newInstance(path, parent);
457 }
458
459 private static class ClassNameComparator implements Comparator<Class<?>> {
460 public int compare(Class<?> c1, Class<?> c2) {
461 return c1.getName().compareTo(c2.getName());
462 }
463 }
Andreas Gampee492ae32016-10-28 19:34:57 -0700464}