blob: cbf2392a739415e888b4d148c1a9a7eda3175795 [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 {
24 System.loadLibrary(args[1]);
25
26 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);
44
45 testClassType(int[].class);
46 testClassType(Runnable[].class);
47 testClassType(String[].class);
Andreas Gampeac587272017-01-05 15:21:34 -080048
49 testClassFields(Integer.class);
50 testClassFields(int.class);
51 testClassFields(String[].class);
Andreas Gampeff9d2092017-01-06 09:12:49 -080052
Andreas Gampe18fee4d2017-01-06 11:36:35 -080053 testClassMethods(Integer.class);
54 testClassMethods(int.class);
55 testClassMethods(String[].class);
56
Andreas Gampeff9d2092017-01-06 09:12:49 -080057 testClassStatus(int.class);
58 testClassStatus(String[].class);
59 testClassStatus(Object.class);
60 testClassStatus(TestForNonInit.class);
61 try {
62 System.out.println(TestForInitFail.dummy);
63 } catch (ExceptionInInitializerError e) {
64 }
65 testClassStatus(TestForInitFail.class);
Andreas Gampe8b07e472017-01-06 14:20:39 -080066
67 testInterfaces(int.class);
68 testInterfaces(String[].class);
69 testInterfaces(Object.class);
70 testInterfaces(InfA.class);
71 testInterfaces(InfB.class);
72 testInterfaces(InfC.class);
73 testInterfaces(ClassA.class);
74 testInterfaces(ClassB.class);
75 testInterfaces(ClassC.class);
Andreas Gampe8f5b6032017-01-06 15:50:55 -080076
77 testClassLoader(String.class);
78 testClassLoader(String[].class);
79 testClassLoader(InfA.class);
80 testClassLoader(getProxyClass());
Andreas Gampe70f16392017-01-16 14:20:10 -080081
82 testClassLoaderClasses();
Andreas Gampe812a2442017-01-19 22:04:46 -080083
84 System.out.println();
85
86 testClassVersion();
Andreas Gampee492ae32016-10-28 19:34:57 -070087 }
88
89 private static Class<?> proxyClass = null;
90
91 private static Class<?> getProxyClass() throws Exception {
92 if (proxyClass != null) {
93 return proxyClass;
94 }
95
96 proxyClass = Proxy.getProxyClass(Main.class.getClassLoader(), new Class[] { Runnable.class });
97 return proxyClass;
98 }
99
100 private static void testClass(String className) throws Exception {
101 Class<?> base = Class.forName(className);
102 testClass(base);
103 }
104
105 private static void testClass(Class<?> base) throws Exception {
106 String[] result = getClassSignature(base);
107 System.out.println(Arrays.toString(result));
Andreas Gampe64013e52017-01-06 13:07:19 -0800108 int mod = getClassModifiers(base);
109 if (mod != base.getModifiers()) {
110 throw new RuntimeException("Unexpected modifiers: " + base.getModifiers() + " vs " + mod);
111 }
112 System.out.println(Integer.toHexString(mod));
Andreas Gampee492ae32016-10-28 19:34:57 -0700113 }
114
Andreas Gampe4fd66ec2017-01-05 14:42:13 -0800115 private static void testClassType(Class<?> c) throws Exception {
116 boolean isInterface = isInterface(c);
117 boolean isArray = isArrayClass(c);
Alex Lighte4a88632017-01-10 07:41:24 -0800118 boolean isModifiable = isModifiableClass(c);
119 System.out.println(c.getName() + " interface=" + isInterface + " array=" + isArray +
120 " modifiable=" + isModifiable);
Andreas Gampe4fd66ec2017-01-05 14:42:13 -0800121 }
122
Andreas Gampeac587272017-01-05 15:21:34 -0800123 private static void testClassFields(Class<?> c) throws Exception {
124 System.out.println(Arrays.toString(getClassFields(c)));
125 }
126
Andreas Gampe18fee4d2017-01-06 11:36:35 -0800127 private static void testClassMethods(Class<?> c) throws Exception {
128 System.out.println(Arrays.toString(getClassMethods(c)));
129 }
130
Andreas Gampeff9d2092017-01-06 09:12:49 -0800131 private static void testClassStatus(Class<?> c) {
132 System.out.println(c + " " + Integer.toBinaryString(getClassStatus(c)));
133 }
134
Andreas Gampe8b07e472017-01-06 14:20:39 -0800135 private static void testInterfaces(Class<?> c) {
136 System.out.println(c + " " + Arrays.toString(getImplementedInterfaces(c)));
137 }
138
Andreas Gampe8f5b6032017-01-06 15:50:55 -0800139 private static boolean IsBootClassLoader(ClassLoader l) {
140 // Hacky check for Android's fake boot classloader.
141 return l.getClass().getName().equals("java.lang.BootClassLoader");
142 }
143
144 private static void testClassLoader(Class<?> c) {
145 Object cl = getClassLoader(c);
146 System.out.println(c + " " + (cl != null ? cl.getClass().getName() : "null"));
147 if (cl == null) {
148 if (c.getClassLoader() != null && !IsBootClassLoader(c.getClassLoader())) {
149 throw new RuntimeException("Expected " + c.getClassLoader() + ", but got null.");
150 }
151 } else {
152 if (!(cl instanceof ClassLoader)) {
153 throw new RuntimeException("Unexpected \"classloader\": " + cl + " (" + cl.getClass() +
154 ")");
155 }
156 if (cl != c.getClassLoader()) {
157 throw new RuntimeException("Unexpected classloader: " + c.getClassLoader() + " vs " + cl);
158 }
159 }
160 }
161
Andreas Gampe70f16392017-01-16 14:20:10 -0800162 private static void testClassLoaderClasses() throws Exception {
163 ClassLoader boot = ClassLoader.getSystemClassLoader().getParent();
164 while (boot.getParent() != null) {
165 boot = boot.getParent();
166 }
167
168 System.out.println();
169 System.out.println("boot <- src <- src-ex (A,B)");
170 ClassLoader cl1 = create(create(boot, DEX1), DEX2);
171 Class.forName("B", false, cl1);
172 Class.forName("A", false, cl1);
173 printClassLoaderClasses(cl1);
174
175 System.out.println();
176 System.out.println("boot <- src (B) <- src-ex (A, List)");
177 ClassLoader cl2 = create(create(boot, DEX1), DEX2);
178 Class.forName("A", false, cl2);
179 Class.forName("java.util.List", false, cl2);
180 Class.forName("B", false, cl2.getParent());
181 printClassLoaderClasses(cl2);
182
183 System.out.println();
184 System.out.println("boot <- src+src-ex (A,B)");
185 ClassLoader cl3 = create(boot, DEX1, DEX2);
186 Class.forName("B", false, cl3);
187 Class.forName("A", false, cl3);
188 printClassLoaderClasses(cl3);
189
190 // Check that the boot classloader dumps something non-empty.
191 Class<?>[] bootClasses = getClassLoaderClasses(boot);
192 if (bootClasses.length == 0) {
193 throw new RuntimeException("No classes initiated by boot classloader.");
194 }
195 // Check that at least java.util.List is loaded.
196 boolean foundList = false;
197 for (Class<?> c : bootClasses) {
198 if (c == java.util.List.class) {
199 foundList = true;
200 break;
201 }
202 }
203 if (!foundList) {
204 System.out.println(Arrays.toString(bootClasses));
205 throw new RuntimeException("Could not find class java.util.List.");
206 }
207 }
208
Andreas Gampe812a2442017-01-19 22:04:46 -0800209 private static void testClassVersion() {
210 System.out.println(Arrays.toString(getClassVersion(Main.class)));
211 }
212
Andreas Gampe70f16392017-01-16 14:20:10 -0800213 private static void printClassLoaderClasses(ClassLoader cl) {
214 for (;;) {
215 if (cl == null || !cl.getClass().getName().startsWith("dalvik.system")) {
216 break;
217 }
218
219 ClassLoader saved = cl;
220 for (;;) {
221 if (cl == null || !cl.getClass().getName().startsWith("dalvik.system")) {
222 break;
223 }
224 String s = cl.toString();
225 int index1 = s.indexOf("zip file");
226 int index2 = s.indexOf(']', index1);
227 if (index2 < 0) {
228 throw new RuntimeException("Unexpected classloader " + s);
229 }
230 String zip_file = s.substring(index1, index2);
231 int index3 = zip_file.indexOf('"');
232 int index4 = zip_file.indexOf('"', index3 + 1);
233 if (index4 < 0) {
234 throw new RuntimeException("Unexpected classloader " + s);
235 }
236 String paths = zip_file.substring(index3 + 1, index4);
237 String pathArray[] = paths.split(":");
238 for (String path : pathArray) {
239 int index5 = path.lastIndexOf('/');
240 System.out.print(path.substring(index5 + 1));
241 System.out.print('+');
242 }
243 System.out.print(" -> ");
244 cl = cl.getParent();
245 }
246 System.out.println();
247 Class<?> classes[] = getClassLoaderClasses(saved);
248 Arrays.sort(classes, new ClassNameComparator());
249 System.out.println(Arrays.toString(classes));
250
251 cl = saved.getParent();
252 }
253 }
254
Alex Lighte4a88632017-01-10 07:41:24 -0800255 private static native boolean isModifiableClass(Class<?> c);
Andreas Gampee492ae32016-10-28 19:34:57 -0700256 private static native String[] getClassSignature(Class<?> c);
Andreas Gampe4fd66ec2017-01-05 14:42:13 -0800257
258 private static native boolean isInterface(Class<?> c);
259 private static native boolean isArrayClass(Class<?> c);
Andreas Gampeac587272017-01-05 15:21:34 -0800260
Andreas Gampe64013e52017-01-06 13:07:19 -0800261 private static native int getClassModifiers(Class<?> c);
262
Andreas Gampeac587272017-01-05 15:21:34 -0800263 private static native Object[] getClassFields(Class<?> c);
Andreas Gampe18fee4d2017-01-06 11:36:35 -0800264 private static native Object[] getClassMethods(Class<?> c);
Andreas Gampe70f16392017-01-16 14:20:10 -0800265 private static native Class<?>[] getImplementedInterfaces(Class<?> c);
Andreas Gampeff9d2092017-01-06 09:12:49 -0800266
267 private static native int getClassStatus(Class<?> c);
268
Andreas Gampe8f5b6032017-01-06 15:50:55 -0800269 private static native Object getClassLoader(Class<?> c);
270
Andreas Gampe70f16392017-01-16 14:20:10 -0800271 private static native Class<?>[] getClassLoaderClasses(ClassLoader cl);
272
Andreas Gampe812a2442017-01-19 22:04:46 -0800273 private static native int[] getClassVersion(Class<?> c);
274
Andreas Gampeff9d2092017-01-06 09:12:49 -0800275 private static class TestForNonInit {
276 public static double dummy = Math.random(); // So it can't be compile-time initialized.
277 }
278
279 private static class TestForInitFail {
280 public static int dummy = ((int)Math.random())/0; // So it throws when initializing.
281 }
Andreas Gampe8b07e472017-01-06 14:20:39 -0800282
283 public static interface InfA {
284 }
285 public static interface InfB extends InfA {
286 }
287 public static interface InfC extends InfB {
288 }
289
290 public abstract static class ClassA implements InfA {
291 }
292 public abstract static class ClassB extends ClassA implements InfB {
293 }
294 public abstract static class ClassC implements InfA, InfC {
295 }
Andreas Gampe70f16392017-01-16 14:20:10 -0800296
297 private static final String DEX1 = System.getenv("DEX_LOCATION") + "/912-classes.jar";
298 private static final String DEX2 = System.getenv("DEX_LOCATION") + "/912-classes-ex.jar";
299
300 private static ClassLoader create(ClassLoader parent, String... elements) throws Exception {
301 // Note: We use a PathClassLoader, as we do not care about code performance. We only load
302 // the classes, and they're empty.
303 Class<?> pathClassLoaderClass = Class.forName("dalvik.system.PathClassLoader");
304 Constructor<?> pathClassLoaderInit = pathClassLoaderClass.getConstructor(String.class,
305 ClassLoader.class);
306 String path = String.join(":", elements);
307 return (ClassLoader) pathClassLoaderInit.newInstance(path, parent);
308 }
309
310 private static class ClassNameComparator implements Comparator<Class<?>> {
311 public int compare(Class<?> c1, Class<?> c2) {
312 return c1.getName().compareTo(c2.getName());
313 }
314 }
Andreas Gampee492ae32016-10-28 19:34:57 -0700315}