blob: 6c8858ab6564659f40cdba9f95ab3d92b4bcc9f4 [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 Gampe46651672017-04-07 09:00:04 -070026 art.Main.bindAgentJNIForClass(Main.class);
Andreas Gampee492ae32016-10-28 19:34:57 -070027 doTest();
28 }
29
30 public static void doTest() throws Exception {
31 testClass("java.lang.Object");
32 testClass("java.lang.String");
33 testClass("java.lang.Math");
34 testClass("java.util.List");
35
36 testClass(getProxyClass());
37
38 testClass(int.class);
39 testClass(double[].class);
Andreas Gampe4fd66ec2017-01-05 14:42:13 -080040
41 testClassType(int.class);
42 testClassType(getProxyClass());
43 testClassType(Runnable.class);
44 testClassType(String.class);
Alex Light09f274f2017-02-21 15:00:48 -080045 testClassType(ArrayList.class);
Andreas Gampe4fd66ec2017-01-05 14:42:13 -080046
47 testClassType(int[].class);
48 testClassType(Runnable[].class);
49 testClassType(String[].class);
Andreas Gampeac587272017-01-05 15:21:34 -080050
51 testClassFields(Integer.class);
52 testClassFields(int.class);
53 testClassFields(String[].class);
Andreas Gampeff9d2092017-01-06 09:12:49 -080054
Andreas Gampe18fee4d2017-01-06 11:36:35 -080055 testClassMethods(Integer.class);
56 testClassMethods(int.class);
57 testClassMethods(String[].class);
58
Andreas Gampeff9d2092017-01-06 09:12:49 -080059 testClassStatus(int.class);
60 testClassStatus(String[].class);
61 testClassStatus(Object.class);
62 testClassStatus(TestForNonInit.class);
63 try {
64 System.out.println(TestForInitFail.dummy);
65 } catch (ExceptionInInitializerError e) {
66 }
67 testClassStatus(TestForInitFail.class);
Andreas Gampe8b07e472017-01-06 14:20:39 -080068
69 testInterfaces(int.class);
70 testInterfaces(String[].class);
71 testInterfaces(Object.class);
72 testInterfaces(InfA.class);
73 testInterfaces(InfB.class);
74 testInterfaces(InfC.class);
75 testInterfaces(ClassA.class);
76 testInterfaces(ClassB.class);
77 testInterfaces(ClassC.class);
Andreas Gampe8f5b6032017-01-06 15:50:55 -080078
79 testClassLoader(String.class);
80 testClassLoader(String[].class);
81 testClassLoader(InfA.class);
82 testClassLoader(getProxyClass());
Andreas Gampe70f16392017-01-16 14:20:10 -080083
84 testClassLoaderClasses();
Andreas Gampe812a2442017-01-19 22:04:46 -080085
86 System.out.println();
87
88 testClassVersion();
Andreas Gampee6377462017-01-20 17:37:50 -080089
90 System.out.println();
91
92 testClassEvents();
Andreas Gampee492ae32016-10-28 19:34:57 -070093 }
94
95 private static Class<?> proxyClass = null;
96
97 private static Class<?> getProxyClass() throws Exception {
98 if (proxyClass != null) {
99 return proxyClass;
100 }
101
102 proxyClass = Proxy.getProxyClass(Main.class.getClassLoader(), new Class[] { Runnable.class });
103 return proxyClass;
104 }
105
106 private static void testClass(String className) throws Exception {
107 Class<?> base = Class.forName(className);
108 testClass(base);
109 }
110
111 private static void testClass(Class<?> base) throws Exception {
112 String[] result = getClassSignature(base);
113 System.out.println(Arrays.toString(result));
Andreas Gampe64013e52017-01-06 13:07:19 -0800114 int mod = getClassModifiers(base);
115 if (mod != base.getModifiers()) {
116 throw new RuntimeException("Unexpected modifiers: " + base.getModifiers() + " vs " + mod);
117 }
118 System.out.println(Integer.toHexString(mod));
Andreas Gampee492ae32016-10-28 19:34:57 -0700119 }
120
Andreas Gampe4fd66ec2017-01-05 14:42:13 -0800121 private static void testClassType(Class<?> c) throws Exception {
122 boolean isInterface = isInterface(c);
123 boolean isArray = isArrayClass(c);
Alex Lighte4a88632017-01-10 07:41:24 -0800124 boolean isModifiable = isModifiableClass(c);
125 System.out.println(c.getName() + " interface=" + isInterface + " array=" + isArray +
126 " modifiable=" + isModifiable);
Andreas Gampe4fd66ec2017-01-05 14:42:13 -0800127 }
128
Andreas Gampeac587272017-01-05 15:21:34 -0800129 private static void testClassFields(Class<?> c) throws Exception {
130 System.out.println(Arrays.toString(getClassFields(c)));
131 }
132
Andreas Gampe18fee4d2017-01-06 11:36:35 -0800133 private static void testClassMethods(Class<?> c) throws Exception {
134 System.out.println(Arrays.toString(getClassMethods(c)));
135 }
136
Andreas Gampeff9d2092017-01-06 09:12:49 -0800137 private static void testClassStatus(Class<?> c) {
138 System.out.println(c + " " + Integer.toBinaryString(getClassStatus(c)));
139 }
140
Andreas Gampe8b07e472017-01-06 14:20:39 -0800141 private static void testInterfaces(Class<?> c) {
142 System.out.println(c + " " + Arrays.toString(getImplementedInterfaces(c)));
143 }
144
Andreas Gampe8f5b6032017-01-06 15:50:55 -0800145 private static boolean IsBootClassLoader(ClassLoader l) {
146 // Hacky check for Android's fake boot classloader.
147 return l.getClass().getName().equals("java.lang.BootClassLoader");
148 }
149
150 private static void testClassLoader(Class<?> c) {
151 Object cl = getClassLoader(c);
152 System.out.println(c + " " + (cl != null ? cl.getClass().getName() : "null"));
153 if (cl == null) {
154 if (c.getClassLoader() != null && !IsBootClassLoader(c.getClassLoader())) {
155 throw new RuntimeException("Expected " + c.getClassLoader() + ", but got null.");
156 }
157 } else {
158 if (!(cl instanceof ClassLoader)) {
159 throw new RuntimeException("Unexpected \"classloader\": " + cl + " (" + cl.getClass() +
160 ")");
161 }
162 if (cl != c.getClassLoader()) {
163 throw new RuntimeException("Unexpected classloader: " + c.getClassLoader() + " vs " + cl);
164 }
165 }
166 }
167
Andreas Gampe70f16392017-01-16 14:20:10 -0800168 private static void testClassLoaderClasses() throws Exception {
169 ClassLoader boot = ClassLoader.getSystemClassLoader().getParent();
170 while (boot.getParent() != null) {
171 boot = boot.getParent();
172 }
173
174 System.out.println();
175 System.out.println("boot <- src <- src-ex (A,B)");
176 ClassLoader cl1 = create(create(boot, DEX1), DEX2);
177 Class.forName("B", false, cl1);
178 Class.forName("A", false, cl1);
179 printClassLoaderClasses(cl1);
180
181 System.out.println();
182 System.out.println("boot <- src (B) <- src-ex (A, List)");
183 ClassLoader cl2 = create(create(boot, DEX1), DEX2);
184 Class.forName("A", false, cl2);
185 Class.forName("java.util.List", false, cl2);
186 Class.forName("B", false, cl2.getParent());
187 printClassLoaderClasses(cl2);
188
189 System.out.println();
190 System.out.println("boot <- src+src-ex (A,B)");
191 ClassLoader cl3 = create(boot, DEX1, DEX2);
192 Class.forName("B", false, cl3);
193 Class.forName("A", false, cl3);
194 printClassLoaderClasses(cl3);
195
196 // Check that the boot classloader dumps something non-empty.
197 Class<?>[] bootClasses = getClassLoaderClasses(boot);
198 if (bootClasses.length == 0) {
199 throw new RuntimeException("No classes initiated by boot classloader.");
200 }
201 // Check that at least java.util.List is loaded.
202 boolean foundList = false;
203 for (Class<?> c : bootClasses) {
204 if (c == java.util.List.class) {
205 foundList = true;
206 break;
207 }
208 }
209 if (!foundList) {
210 System.out.println(Arrays.toString(bootClasses));
211 throw new RuntimeException("Could not find class java.util.List.");
212 }
213 }
214
Andreas Gampe812a2442017-01-19 22:04:46 -0800215 private static void testClassVersion() {
216 System.out.println(Arrays.toString(getClassVersion(Main.class)));
217 }
218
Andreas Gampee6377462017-01-20 17:37:50 -0800219 private static void testClassEvents() throws Exception {
220 ClassLoader cl = Main.class.getClassLoader();
221 while (cl.getParent() != null) {
222 cl = cl.getParent();
223 }
224 final ClassLoader boot = cl;
225
Andreas Gampee2744c62017-02-08 16:28:59 +0000226 // The JIT may deeply inline and load some classes. Preload these for test determinism.
227 final String PRELOAD_FOR_JIT[] = {
228 "java.nio.charset.CoderMalfunctionError",
229 "java.util.NoSuchElementException"
230 };
231 for (String s : PRELOAD_FOR_JIT) {
232 Class.forName(s);
233 }
234
Andreas Gampe19958592017-01-23 17:29:07 -0800235 Runnable r = new Runnable() {
236 @Override
237 public void run() {
238 try {
239 ClassLoader cl6 = create(boot, DEX1, DEX2);
240 System.out.println("C, true");
241 Class.forName("C", true, cl6);
242 } catch (Exception e) {
243 throw new RuntimeException(e);
244 }
245 }
246 };
247
Andreas Gampe41526612017-01-23 22:48:15 -0800248 Thread dummyThread = new Thread();
249 dummyThread.start();
250 dummyThread.join();
251
252 ensureJitCompiled(Main.class, "testClassEvents");
253
Andreas Gampee2744c62017-02-08 16:28:59 +0000254 enableClassLoadPreparePrintEvents(true);
Andreas Gampee6377462017-01-20 17:37:50 -0800255
256 ClassLoader cl1 = create(boot, DEX1, DEX2);
257 System.out.println("B, false");
258 Class.forName("B", false, cl1);
259
260 ClassLoader cl2 = create(boot, DEX1, DEX2);
261 System.out.println("B, true");
262 Class.forName("B", true, cl2);
263
264 ClassLoader cl3 = create(boot, DEX1, DEX2);
265 System.out.println("C, false");
266 Class.forName("C", false, cl3);
267 System.out.println("A, false");
268 Class.forName("A", false, cl3);
269
270 ClassLoader cl4 = create(boot, DEX1, DEX2);
271 System.out.println("C, true");
272 Class.forName("C", true, cl4);
273 System.out.println("A, true");
274 Class.forName("A", true, cl4);
275
276 ClassLoader cl5 = create(boot, DEX1, DEX2);
277 System.out.println("A, true");
278 Class.forName("A", true, cl5);
279 System.out.println("C, true");
280 Class.forName("C", true, cl5);
281
Andreas Gampee6377462017-01-20 17:37:50 -0800282 Thread t = new Thread(r, "TestRunner");
283 t.start();
284 t.join();
285
Andreas Gampe6cfd4c92017-04-06 08:03:32 -0700286 // Check creation of arrays and proxies.
287 Proxy.getProxyClass(Main.class.getClassLoader(), new Class[] { Comparable.class });
288 Class.forName("[LMain;");
289
Andreas Gampee2744c62017-02-08 16:28:59 +0000290 enableClassLoadPreparePrintEvents(false);
291
292 // Note: the JIT part of this test is about the JIT pulling in a class not yet touched by
293 // anything else in the system. This could be the verifier or the interpreter. We
294 // block the interpreter by calling ensureJitCompiled. The verifier, however, must
295 // run in configurations where dex2oat didn't verify the class itself. So explicitly
296 // check whether the class has been already loaded, and skip then.
297 // TODO: Add multiple configurations to the run script once that becomes easier to do.
298 if (hasJit() && !isLoadedClass("Main$ClassD")) {
299 testClassEventsJit();
300 }
Andreas Gampe691051b2017-02-09 09:15:24 -0800301
302 testClassLoadPrepareEquality();
Andreas Gampee2744c62017-02-08 16:28:59 +0000303 }
304
305 private static void testClassEventsJit() throws Exception {
306 enableClassLoadSeenEvents(true);
307
308 testClassEventsJitImpl();
309
310 enableClassLoadSeenEvents(false);
311
312 if (!hadLoadEvent()) {
313 throw new RuntimeException("Did not get expected load event.");
314 }
315 }
316
317 private static void testClassEventsJitImpl() throws Exception {
318 ensureJitCompiled(Main.class, "testClassEventsJitImpl");
319
320 if (ClassD.x != 1) {
321 throw new RuntimeException("Unexpected value");
322 }
Andreas Gampee6377462017-01-20 17:37:50 -0800323 }
324
Andreas Gampe691051b2017-02-09 09:15:24 -0800325 private static void testClassLoadPrepareEquality() throws Exception {
Andreas Gampea67354b2017-02-10 16:18:30 -0800326 setEqualityEventStorageClass(ClassF.class);
327
Andreas Gampe691051b2017-02-09 09:15:24 -0800328 enableClassLoadPrepareEqualityEvents(true);
329
330 Class.forName("Main$ClassE");
331
332 enableClassLoadPrepareEqualityEvents(false);
333 }
334
Andreas Gampe70f16392017-01-16 14:20:10 -0800335 private static void printClassLoaderClasses(ClassLoader cl) {
336 for (;;) {
337 if (cl == null || !cl.getClass().getName().startsWith("dalvik.system")) {
338 break;
339 }
340
341 ClassLoader saved = cl;
342 for (;;) {
343 if (cl == null || !cl.getClass().getName().startsWith("dalvik.system")) {
344 break;
345 }
346 String s = cl.toString();
347 int index1 = s.indexOf("zip file");
348 int index2 = s.indexOf(']', index1);
349 if (index2 < 0) {
350 throw new RuntimeException("Unexpected classloader " + s);
351 }
352 String zip_file = s.substring(index1, index2);
353 int index3 = zip_file.indexOf('"');
354 int index4 = zip_file.indexOf('"', index3 + 1);
355 if (index4 < 0) {
356 throw new RuntimeException("Unexpected classloader " + s);
357 }
358 String paths = zip_file.substring(index3 + 1, index4);
359 String pathArray[] = paths.split(":");
360 for (String path : pathArray) {
361 int index5 = path.lastIndexOf('/');
362 System.out.print(path.substring(index5 + 1));
363 System.out.print('+');
364 }
365 System.out.print(" -> ");
366 cl = cl.getParent();
367 }
368 System.out.println();
369 Class<?> classes[] = getClassLoaderClasses(saved);
370 Arrays.sort(classes, new ClassNameComparator());
371 System.out.println(Arrays.toString(classes));
372
373 cl = saved.getParent();
374 }
375 }
376
Alex Lighte4a88632017-01-10 07:41:24 -0800377 private static native boolean isModifiableClass(Class<?> c);
Andreas Gampee492ae32016-10-28 19:34:57 -0700378 private static native String[] getClassSignature(Class<?> c);
Andreas Gampe4fd66ec2017-01-05 14:42:13 -0800379
380 private static native boolean isInterface(Class<?> c);
381 private static native boolean isArrayClass(Class<?> c);
Andreas Gampeac587272017-01-05 15:21:34 -0800382
Andreas Gampe64013e52017-01-06 13:07:19 -0800383 private static native int getClassModifiers(Class<?> c);
384
Andreas Gampeac587272017-01-05 15:21:34 -0800385 private static native Object[] getClassFields(Class<?> c);
Andreas Gampe18fee4d2017-01-06 11:36:35 -0800386 private static native Object[] getClassMethods(Class<?> c);
Andreas Gampe70f16392017-01-16 14:20:10 -0800387 private static native Class<?>[] getImplementedInterfaces(Class<?> c);
Andreas Gampeff9d2092017-01-06 09:12:49 -0800388
389 private static native int getClassStatus(Class<?> c);
390
Andreas Gampe8f5b6032017-01-06 15:50:55 -0800391 private static native Object getClassLoader(Class<?> c);
392
Andreas Gampe70f16392017-01-16 14:20:10 -0800393 private static native Class<?>[] getClassLoaderClasses(ClassLoader cl);
394
Andreas Gampe812a2442017-01-19 22:04:46 -0800395 private static native int[] getClassVersion(Class<?> c);
396
Andreas Gampee2744c62017-02-08 16:28:59 +0000397 private static native void enableClassLoadPreparePrintEvents(boolean b);
Andreas Gampee6377462017-01-20 17:37:50 -0800398
Andreas Gampee2744c62017-02-08 16:28:59 +0000399 private static native void ensureJitCompiled(Class<?> c, String name);
400
401 private static native boolean hasJit();
402 private static native boolean isLoadedClass(String name);
403 private static native void enableClassLoadSeenEvents(boolean b);
404 private static native boolean hadLoadEvent();
Andreas Gampe41526612017-01-23 22:48:15 -0800405
Andreas Gampea67354b2017-02-10 16:18:30 -0800406 private static native void setEqualityEventStorageClass(Class<?> c);
Andreas Gampe691051b2017-02-09 09:15:24 -0800407 private static native void enableClassLoadPrepareEqualityEvents(boolean b);
408
Andreas Gampeff9d2092017-01-06 09:12:49 -0800409 private static class TestForNonInit {
410 public static double dummy = Math.random(); // So it can't be compile-time initialized.
411 }
412
413 private static class TestForInitFail {
414 public static int dummy = ((int)Math.random())/0; // So it throws when initializing.
415 }
Andreas Gampe8b07e472017-01-06 14:20:39 -0800416
417 public static interface InfA {
418 }
419 public static interface InfB extends InfA {
420 }
421 public static interface InfC extends InfB {
422 }
423
424 public abstract static class ClassA implements InfA {
425 }
426 public abstract static class ClassB extends ClassA implements InfB {
427 }
428 public abstract static class ClassC implements InfA, InfC {
429 }
Andreas Gampe70f16392017-01-16 14:20:10 -0800430
Andreas Gampee2744c62017-02-08 16:28:59 +0000431 public static class ClassD {
432 static int x = 1;
433 }
434
Andreas Gampe691051b2017-02-09 09:15:24 -0800435 public static class ClassE {
436 public void foo() {
437 }
438 public void bar() {
439 }
440 }
441
Andreas Gampea67354b2017-02-10 16:18:30 -0800442 public static class ClassF {
443 public static Object STATIC = null;
Andreas Gampe52784ac2017-02-13 18:10:09 -0800444 public static Reference<Object> WEAK = null;
Andreas Gampea67354b2017-02-10 16:18:30 -0800445 }
446
Andreas Gampe70f16392017-01-16 14:20:10 -0800447 private static final String DEX1 = System.getenv("DEX_LOCATION") + "/912-classes.jar";
448 private static final String DEX2 = System.getenv("DEX_LOCATION") + "/912-classes-ex.jar";
449
450 private static ClassLoader create(ClassLoader parent, String... elements) throws Exception {
451 // Note: We use a PathClassLoader, as we do not care about code performance. We only load
452 // the classes, and they're empty.
453 Class<?> pathClassLoaderClass = Class.forName("dalvik.system.PathClassLoader");
454 Constructor<?> pathClassLoaderInit = pathClassLoaderClass.getConstructor(String.class,
455 ClassLoader.class);
456 String path = String.join(":", elements);
457 return (ClassLoader) pathClassLoaderInit.newInstance(path, parent);
458 }
459
460 private static class ClassNameComparator implements Comparator<Class<?>> {
461 public int compare(Class<?> c1, Class<?> c2) {
462 return c1.getName().compareTo(c2.getName());
463 }
464 }
Andreas Gampee492ae32016-10-28 19:34:57 -0700465}