blob: 005074f8c16067aa7870aac3d6890e7e66f60621 [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;
20import java.util.Arrays;
Andreas Gampe70f16392017-01-16 14:20:10 -080021import java.util.Comparator;
Andreas Gampee492ae32016-10-28 19:34:57 -070022
23public class Main {
24 public static void main(String[] args) throws Exception {
Andreas Gampee492ae32016-10-28 19:34:57 -070025 doTest();
26 }
27
28 public static void doTest() throws Exception {
29 testClass("java.lang.Object");
30 testClass("java.lang.String");
31 testClass("java.lang.Math");
32 testClass("java.util.List");
33
34 testClass(getProxyClass());
35
36 testClass(int.class);
37 testClass(double[].class);
Andreas Gampe4fd66ec2017-01-05 14:42:13 -080038
39 testClassType(int.class);
40 testClassType(getProxyClass());
41 testClassType(Runnable.class);
42 testClassType(String.class);
43
44 testClassType(int[].class);
45 testClassType(Runnable[].class);
46 testClassType(String[].class);
Andreas Gampeac587272017-01-05 15:21:34 -080047
48 testClassFields(Integer.class);
49 testClassFields(int.class);
50 testClassFields(String[].class);
Andreas Gampeff9d2092017-01-06 09:12:49 -080051
Andreas Gampe18fee4d2017-01-06 11:36:35 -080052 testClassMethods(Integer.class);
53 testClassMethods(int.class);
54 testClassMethods(String[].class);
55
Andreas Gampeff9d2092017-01-06 09:12:49 -080056 testClassStatus(int.class);
57 testClassStatus(String[].class);
58 testClassStatus(Object.class);
59 testClassStatus(TestForNonInit.class);
60 try {
61 System.out.println(TestForInitFail.dummy);
62 } catch (ExceptionInInitializerError e) {
63 }
64 testClassStatus(TestForInitFail.class);
Andreas Gampe8b07e472017-01-06 14:20:39 -080065
66 testInterfaces(int.class);
67 testInterfaces(String[].class);
68 testInterfaces(Object.class);
69 testInterfaces(InfA.class);
70 testInterfaces(InfB.class);
71 testInterfaces(InfC.class);
72 testInterfaces(ClassA.class);
73 testInterfaces(ClassB.class);
74 testInterfaces(ClassC.class);
Andreas Gampe8f5b6032017-01-06 15:50:55 -080075
76 testClassLoader(String.class);
77 testClassLoader(String[].class);
78 testClassLoader(InfA.class);
79 testClassLoader(getProxyClass());
Andreas Gampe70f16392017-01-16 14:20:10 -080080
81 testClassLoaderClasses();
Andreas Gampe812a2442017-01-19 22:04:46 -080082
83 System.out.println();
84
85 testClassVersion();
Andreas Gampee6377462017-01-20 17:37:50 -080086
87 System.out.println();
88
89 testClassEvents();
Andreas Gampee492ae32016-10-28 19:34:57 -070090 }
91
92 private static Class<?> proxyClass = null;
93
94 private static Class<?> getProxyClass() throws Exception {
95 if (proxyClass != null) {
96 return proxyClass;
97 }
98
99 proxyClass = Proxy.getProxyClass(Main.class.getClassLoader(), new Class[] { Runnable.class });
100 return proxyClass;
101 }
102
103 private static void testClass(String className) throws Exception {
104 Class<?> base = Class.forName(className);
105 testClass(base);
106 }
107
108 private static void testClass(Class<?> base) throws Exception {
109 String[] result = getClassSignature(base);
110 System.out.println(Arrays.toString(result));
Andreas Gampe64013e52017-01-06 13:07:19 -0800111 int mod = getClassModifiers(base);
112 if (mod != base.getModifiers()) {
113 throw new RuntimeException("Unexpected modifiers: " + base.getModifiers() + " vs " + mod);
114 }
115 System.out.println(Integer.toHexString(mod));
Andreas Gampee492ae32016-10-28 19:34:57 -0700116 }
117
Andreas Gampe4fd66ec2017-01-05 14:42:13 -0800118 private static void testClassType(Class<?> c) throws Exception {
119 boolean isInterface = isInterface(c);
120 boolean isArray = isArrayClass(c);
Alex Lighte4a88632017-01-10 07:41:24 -0800121 boolean isModifiable = isModifiableClass(c);
122 System.out.println(c.getName() + " interface=" + isInterface + " array=" + isArray +
123 " modifiable=" + isModifiable);
Andreas Gampe4fd66ec2017-01-05 14:42:13 -0800124 }
125
Andreas Gampeac587272017-01-05 15:21:34 -0800126 private static void testClassFields(Class<?> c) throws Exception {
127 System.out.println(Arrays.toString(getClassFields(c)));
128 }
129
Andreas Gampe18fee4d2017-01-06 11:36:35 -0800130 private static void testClassMethods(Class<?> c) throws Exception {
131 System.out.println(Arrays.toString(getClassMethods(c)));
132 }
133
Andreas Gampeff9d2092017-01-06 09:12:49 -0800134 private static void testClassStatus(Class<?> c) {
135 System.out.println(c + " " + Integer.toBinaryString(getClassStatus(c)));
136 }
137
Andreas Gampe8b07e472017-01-06 14:20:39 -0800138 private static void testInterfaces(Class<?> c) {
139 System.out.println(c + " " + Arrays.toString(getImplementedInterfaces(c)));
140 }
141
Andreas Gampe8f5b6032017-01-06 15:50:55 -0800142 private static boolean IsBootClassLoader(ClassLoader l) {
143 // Hacky check for Android's fake boot classloader.
144 return l.getClass().getName().equals("java.lang.BootClassLoader");
145 }
146
147 private static void testClassLoader(Class<?> c) {
148 Object cl = getClassLoader(c);
149 System.out.println(c + " " + (cl != null ? cl.getClass().getName() : "null"));
150 if (cl == null) {
151 if (c.getClassLoader() != null && !IsBootClassLoader(c.getClassLoader())) {
152 throw new RuntimeException("Expected " + c.getClassLoader() + ", but got null.");
153 }
154 } else {
155 if (!(cl instanceof ClassLoader)) {
156 throw new RuntimeException("Unexpected \"classloader\": " + cl + " (" + cl.getClass() +
157 ")");
158 }
159 if (cl != c.getClassLoader()) {
160 throw new RuntimeException("Unexpected classloader: " + c.getClassLoader() + " vs " + cl);
161 }
162 }
163 }
164
Andreas Gampe70f16392017-01-16 14:20:10 -0800165 private static void testClassLoaderClasses() throws Exception {
166 ClassLoader boot = ClassLoader.getSystemClassLoader().getParent();
167 while (boot.getParent() != null) {
168 boot = boot.getParent();
169 }
170
171 System.out.println();
172 System.out.println("boot <- src <- src-ex (A,B)");
173 ClassLoader cl1 = create(create(boot, DEX1), DEX2);
174 Class.forName("B", false, cl1);
175 Class.forName("A", false, cl1);
176 printClassLoaderClasses(cl1);
177
178 System.out.println();
179 System.out.println("boot <- src (B) <- src-ex (A, List)");
180 ClassLoader cl2 = create(create(boot, DEX1), DEX2);
181 Class.forName("A", false, cl2);
182 Class.forName("java.util.List", false, cl2);
183 Class.forName("B", false, cl2.getParent());
184 printClassLoaderClasses(cl2);
185
186 System.out.println();
187 System.out.println("boot <- src+src-ex (A,B)");
188 ClassLoader cl3 = create(boot, DEX1, DEX2);
189 Class.forName("B", false, cl3);
190 Class.forName("A", false, cl3);
191 printClassLoaderClasses(cl3);
192
193 // Check that the boot classloader dumps something non-empty.
194 Class<?>[] bootClasses = getClassLoaderClasses(boot);
195 if (bootClasses.length == 0) {
196 throw new RuntimeException("No classes initiated by boot classloader.");
197 }
198 // Check that at least java.util.List is loaded.
199 boolean foundList = false;
200 for (Class<?> c : bootClasses) {
201 if (c == java.util.List.class) {
202 foundList = true;
203 break;
204 }
205 }
206 if (!foundList) {
207 System.out.println(Arrays.toString(bootClasses));
208 throw new RuntimeException("Could not find class java.util.List.");
209 }
210 }
211
Andreas Gampe812a2442017-01-19 22:04:46 -0800212 private static void testClassVersion() {
213 System.out.println(Arrays.toString(getClassVersion(Main.class)));
214 }
215
Andreas Gampee6377462017-01-20 17:37:50 -0800216 private static void testClassEvents() throws Exception {
217 ClassLoader cl = Main.class.getClassLoader();
218 while (cl.getParent() != null) {
219 cl = cl.getParent();
220 }
221 final ClassLoader boot = cl;
222
Andreas Gampee2744c62017-02-08 16:28:59 +0000223 // The JIT may deeply inline and load some classes. Preload these for test determinism.
224 final String PRELOAD_FOR_JIT[] = {
225 "java.nio.charset.CoderMalfunctionError",
226 "java.util.NoSuchElementException"
227 };
228 for (String s : PRELOAD_FOR_JIT) {
229 Class.forName(s);
230 }
231
Andreas Gampe19958592017-01-23 17:29:07 -0800232 Runnable r = new Runnable() {
233 @Override
234 public void run() {
235 try {
236 ClassLoader cl6 = create(boot, DEX1, DEX2);
237 System.out.println("C, true");
238 Class.forName("C", true, cl6);
239 } catch (Exception e) {
240 throw new RuntimeException(e);
241 }
242 }
243 };
244
Andreas Gampe41526612017-01-23 22:48:15 -0800245 Thread dummyThread = new Thread();
246 dummyThread.start();
247 dummyThread.join();
248
249 ensureJitCompiled(Main.class, "testClassEvents");
250
Andreas Gampee2744c62017-02-08 16:28:59 +0000251 enableClassLoadPreparePrintEvents(true);
Andreas Gampee6377462017-01-20 17:37:50 -0800252
253 ClassLoader cl1 = create(boot, DEX1, DEX2);
254 System.out.println("B, false");
255 Class.forName("B", false, cl1);
256
257 ClassLoader cl2 = create(boot, DEX1, DEX2);
258 System.out.println("B, true");
259 Class.forName("B", true, cl2);
260
261 ClassLoader cl3 = create(boot, DEX1, DEX2);
262 System.out.println("C, false");
263 Class.forName("C", false, cl3);
264 System.out.println("A, false");
265 Class.forName("A", false, cl3);
266
267 ClassLoader cl4 = create(boot, DEX1, DEX2);
268 System.out.println("C, true");
269 Class.forName("C", true, cl4);
270 System.out.println("A, true");
271 Class.forName("A", true, cl4);
272
273 ClassLoader cl5 = create(boot, DEX1, DEX2);
274 System.out.println("A, true");
275 Class.forName("A", true, cl5);
276 System.out.println("C, true");
277 Class.forName("C", true, cl5);
278
Andreas Gampee6377462017-01-20 17:37:50 -0800279 Thread t = new Thread(r, "TestRunner");
280 t.start();
281 t.join();
282
Andreas Gampee2744c62017-02-08 16:28:59 +0000283 enableClassLoadPreparePrintEvents(false);
284
285 // Note: the JIT part of this test is about the JIT pulling in a class not yet touched by
286 // anything else in the system. This could be the verifier or the interpreter. We
287 // block the interpreter by calling ensureJitCompiled. The verifier, however, must
288 // run in configurations where dex2oat didn't verify the class itself. So explicitly
289 // check whether the class has been already loaded, and skip then.
290 // TODO: Add multiple configurations to the run script once that becomes easier to do.
291 if (hasJit() && !isLoadedClass("Main$ClassD")) {
292 testClassEventsJit();
293 }
Andreas Gampe691051b2017-02-09 09:15:24 -0800294
295 testClassLoadPrepareEquality();
Andreas Gampee2744c62017-02-08 16:28:59 +0000296 }
297
298 private static void testClassEventsJit() throws Exception {
299 enableClassLoadSeenEvents(true);
300
301 testClassEventsJitImpl();
302
303 enableClassLoadSeenEvents(false);
304
305 if (!hadLoadEvent()) {
306 throw new RuntimeException("Did not get expected load event.");
307 }
308 }
309
310 private static void testClassEventsJitImpl() throws Exception {
311 ensureJitCompiled(Main.class, "testClassEventsJitImpl");
312
313 if (ClassD.x != 1) {
314 throw new RuntimeException("Unexpected value");
315 }
Andreas Gampee6377462017-01-20 17:37:50 -0800316 }
317
Andreas Gampe691051b2017-02-09 09:15:24 -0800318 private static void testClassLoadPrepareEquality() throws Exception {
Andreas Gampea67354b2017-02-10 16:18:30 -0800319 setEqualityEventStorageClass(ClassF.class);
320
Andreas Gampe691051b2017-02-09 09:15:24 -0800321 enableClassLoadPrepareEqualityEvents(true);
322
323 Class.forName("Main$ClassE");
324
325 enableClassLoadPrepareEqualityEvents(false);
326 }
327
Andreas Gampe70f16392017-01-16 14:20:10 -0800328 private static void printClassLoaderClasses(ClassLoader cl) {
329 for (;;) {
330 if (cl == null || !cl.getClass().getName().startsWith("dalvik.system")) {
331 break;
332 }
333
334 ClassLoader saved = cl;
335 for (;;) {
336 if (cl == null || !cl.getClass().getName().startsWith("dalvik.system")) {
337 break;
338 }
339 String s = cl.toString();
340 int index1 = s.indexOf("zip file");
341 int index2 = s.indexOf(']', index1);
342 if (index2 < 0) {
343 throw new RuntimeException("Unexpected classloader " + s);
344 }
345 String zip_file = s.substring(index1, index2);
346 int index3 = zip_file.indexOf('"');
347 int index4 = zip_file.indexOf('"', index3 + 1);
348 if (index4 < 0) {
349 throw new RuntimeException("Unexpected classloader " + s);
350 }
351 String paths = zip_file.substring(index3 + 1, index4);
352 String pathArray[] = paths.split(":");
353 for (String path : pathArray) {
354 int index5 = path.lastIndexOf('/');
355 System.out.print(path.substring(index5 + 1));
356 System.out.print('+');
357 }
358 System.out.print(" -> ");
359 cl = cl.getParent();
360 }
361 System.out.println();
362 Class<?> classes[] = getClassLoaderClasses(saved);
363 Arrays.sort(classes, new ClassNameComparator());
364 System.out.println(Arrays.toString(classes));
365
366 cl = saved.getParent();
367 }
368 }
369
Alex Lighte4a88632017-01-10 07:41:24 -0800370 private static native boolean isModifiableClass(Class<?> c);
Andreas Gampee492ae32016-10-28 19:34:57 -0700371 private static native String[] getClassSignature(Class<?> c);
Andreas Gampe4fd66ec2017-01-05 14:42:13 -0800372
373 private static native boolean isInterface(Class<?> c);
374 private static native boolean isArrayClass(Class<?> c);
Andreas Gampeac587272017-01-05 15:21:34 -0800375
Andreas Gampe64013e52017-01-06 13:07:19 -0800376 private static native int getClassModifiers(Class<?> c);
377
Andreas Gampeac587272017-01-05 15:21:34 -0800378 private static native Object[] getClassFields(Class<?> c);
Andreas Gampe18fee4d2017-01-06 11:36:35 -0800379 private static native Object[] getClassMethods(Class<?> c);
Andreas Gampe70f16392017-01-16 14:20:10 -0800380 private static native Class<?>[] getImplementedInterfaces(Class<?> c);
Andreas Gampeff9d2092017-01-06 09:12:49 -0800381
382 private static native int getClassStatus(Class<?> c);
383
Andreas Gampe8f5b6032017-01-06 15:50:55 -0800384 private static native Object getClassLoader(Class<?> c);
385
Andreas Gampe70f16392017-01-16 14:20:10 -0800386 private static native Class<?>[] getClassLoaderClasses(ClassLoader cl);
387
Andreas Gampe812a2442017-01-19 22:04:46 -0800388 private static native int[] getClassVersion(Class<?> c);
389
Andreas Gampee2744c62017-02-08 16:28:59 +0000390 private static native void enableClassLoadPreparePrintEvents(boolean b);
Andreas Gampee6377462017-01-20 17:37:50 -0800391
Andreas Gampee2744c62017-02-08 16:28:59 +0000392 private static native void ensureJitCompiled(Class<?> c, String name);
393
394 private static native boolean hasJit();
395 private static native boolean isLoadedClass(String name);
396 private static native void enableClassLoadSeenEvents(boolean b);
397 private static native boolean hadLoadEvent();
Andreas Gampe41526612017-01-23 22:48:15 -0800398
Andreas Gampea67354b2017-02-10 16:18:30 -0800399 private static native void setEqualityEventStorageClass(Class<?> c);
Andreas Gampe691051b2017-02-09 09:15:24 -0800400 private static native void enableClassLoadPrepareEqualityEvents(boolean b);
401
Andreas Gampeff9d2092017-01-06 09:12:49 -0800402 private static class TestForNonInit {
403 public static double dummy = Math.random(); // So it can't be compile-time initialized.
404 }
405
406 private static class TestForInitFail {
407 public static int dummy = ((int)Math.random())/0; // So it throws when initializing.
408 }
Andreas Gampe8b07e472017-01-06 14:20:39 -0800409
410 public static interface InfA {
411 }
412 public static interface InfB extends InfA {
413 }
414 public static interface InfC extends InfB {
415 }
416
417 public abstract static class ClassA implements InfA {
418 }
419 public abstract static class ClassB extends ClassA implements InfB {
420 }
421 public abstract static class ClassC implements InfA, InfC {
422 }
Andreas Gampe70f16392017-01-16 14:20:10 -0800423
Andreas Gampee2744c62017-02-08 16:28:59 +0000424 public static class ClassD {
425 static int x = 1;
426 }
427
Andreas Gampe691051b2017-02-09 09:15:24 -0800428 public static class ClassE {
429 public void foo() {
430 }
431 public void bar() {
432 }
433 }
434
Andreas Gampea67354b2017-02-10 16:18:30 -0800435 public static class ClassF {
436 public static Object STATIC = null;
Andreas Gampe52784ac2017-02-13 18:10:09 -0800437 public static Reference<Object> WEAK = null;
Andreas Gampea67354b2017-02-10 16:18:30 -0800438 }
439
Andreas Gampe70f16392017-01-16 14:20:10 -0800440 private static final String DEX1 = System.getenv("DEX_LOCATION") + "/912-classes.jar";
441 private static final String DEX2 = System.getenv("DEX_LOCATION") + "/912-classes-ex.jar";
442
443 private static ClassLoader create(ClassLoader parent, String... elements) throws Exception {
444 // Note: We use a PathClassLoader, as we do not care about code performance. We only load
445 // the classes, and they're empty.
446 Class<?> pathClassLoaderClass = Class.forName("dalvik.system.PathClassLoader");
447 Constructor<?> pathClassLoaderInit = pathClassLoaderClass.getConstructor(String.class,
448 ClassLoader.class);
449 String path = String.join(":", elements);
450 return (ClassLoader) pathClassLoaderInit.newInstance(path, parent);
451 }
452
453 private static class ClassNameComparator implements Comparator<Class<?>> {
454 public int compare(Class<?> c1, Class<?> c2) {
455 return c1.getName().compareTo(c2.getName());
456 }
457 }
Andreas Gampee492ae32016-10-28 19:34:57 -0700458}