blob: ea3c49c87b0314184fe3f4ee9606a3a4ad25affa [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 Gampee492ae32016-10-28 19:34:57 -070083 }
84
85 private static Class<?> proxyClass = null;
86
87 private static Class<?> getProxyClass() throws Exception {
88 if (proxyClass != null) {
89 return proxyClass;
90 }
91
92 proxyClass = Proxy.getProxyClass(Main.class.getClassLoader(), new Class[] { Runnable.class });
93 return proxyClass;
94 }
95
96 private static void testClass(String className) throws Exception {
97 Class<?> base = Class.forName(className);
98 testClass(base);
99 }
100
101 private static void testClass(Class<?> base) throws Exception {
102 String[] result = getClassSignature(base);
103 System.out.println(Arrays.toString(result));
Andreas Gampe64013e52017-01-06 13:07:19 -0800104 int mod = getClassModifiers(base);
105 if (mod != base.getModifiers()) {
106 throw new RuntimeException("Unexpected modifiers: " + base.getModifiers() + " vs " + mod);
107 }
108 System.out.println(Integer.toHexString(mod));
Andreas Gampee492ae32016-10-28 19:34:57 -0700109 }
110
Andreas Gampe4fd66ec2017-01-05 14:42:13 -0800111 private static void testClassType(Class<?> c) throws Exception {
112 boolean isInterface = isInterface(c);
113 boolean isArray = isArrayClass(c);
Alex Lighte4a88632017-01-10 07:41:24 -0800114 boolean isModifiable = isModifiableClass(c);
115 System.out.println(c.getName() + " interface=" + isInterface + " array=" + isArray +
116 " modifiable=" + isModifiable);
Andreas Gampe4fd66ec2017-01-05 14:42:13 -0800117 }
118
Andreas Gampeac587272017-01-05 15:21:34 -0800119 private static void testClassFields(Class<?> c) throws Exception {
120 System.out.println(Arrays.toString(getClassFields(c)));
121 }
122
Andreas Gampe18fee4d2017-01-06 11:36:35 -0800123 private static void testClassMethods(Class<?> c) throws Exception {
124 System.out.println(Arrays.toString(getClassMethods(c)));
125 }
126
Andreas Gampeff9d2092017-01-06 09:12:49 -0800127 private static void testClassStatus(Class<?> c) {
128 System.out.println(c + " " + Integer.toBinaryString(getClassStatus(c)));
129 }
130
Andreas Gampe8b07e472017-01-06 14:20:39 -0800131 private static void testInterfaces(Class<?> c) {
132 System.out.println(c + " " + Arrays.toString(getImplementedInterfaces(c)));
133 }
134
Andreas Gampe8f5b6032017-01-06 15:50:55 -0800135 private static boolean IsBootClassLoader(ClassLoader l) {
136 // Hacky check for Android's fake boot classloader.
137 return l.getClass().getName().equals("java.lang.BootClassLoader");
138 }
139
140 private static void testClassLoader(Class<?> c) {
141 Object cl = getClassLoader(c);
142 System.out.println(c + " " + (cl != null ? cl.getClass().getName() : "null"));
143 if (cl == null) {
144 if (c.getClassLoader() != null && !IsBootClassLoader(c.getClassLoader())) {
145 throw new RuntimeException("Expected " + c.getClassLoader() + ", but got null.");
146 }
147 } else {
148 if (!(cl instanceof ClassLoader)) {
149 throw new RuntimeException("Unexpected \"classloader\": " + cl + " (" + cl.getClass() +
150 ")");
151 }
152 if (cl != c.getClassLoader()) {
153 throw new RuntimeException("Unexpected classloader: " + c.getClassLoader() + " vs " + cl);
154 }
155 }
156 }
157
Andreas Gampe70f16392017-01-16 14:20:10 -0800158 private static void testClassLoaderClasses() throws Exception {
159 ClassLoader boot = ClassLoader.getSystemClassLoader().getParent();
160 while (boot.getParent() != null) {
161 boot = boot.getParent();
162 }
163
164 System.out.println();
165 System.out.println("boot <- src <- src-ex (A,B)");
166 ClassLoader cl1 = create(create(boot, DEX1), DEX2);
167 Class.forName("B", false, cl1);
168 Class.forName("A", false, cl1);
169 printClassLoaderClasses(cl1);
170
171 System.out.println();
172 System.out.println("boot <- src (B) <- src-ex (A, List)");
173 ClassLoader cl2 = create(create(boot, DEX1), DEX2);
174 Class.forName("A", false, cl2);
175 Class.forName("java.util.List", false, cl2);
176 Class.forName("B", false, cl2.getParent());
177 printClassLoaderClasses(cl2);
178
179 System.out.println();
180 System.out.println("boot <- src+src-ex (A,B)");
181 ClassLoader cl3 = create(boot, DEX1, DEX2);
182 Class.forName("B", false, cl3);
183 Class.forName("A", false, cl3);
184 printClassLoaderClasses(cl3);
185
186 // Check that the boot classloader dumps something non-empty.
187 Class<?>[] bootClasses = getClassLoaderClasses(boot);
188 if (bootClasses.length == 0) {
189 throw new RuntimeException("No classes initiated by boot classloader.");
190 }
191 // Check that at least java.util.List is loaded.
192 boolean foundList = false;
193 for (Class<?> c : bootClasses) {
194 if (c == java.util.List.class) {
195 foundList = true;
196 break;
197 }
198 }
199 if (!foundList) {
200 System.out.println(Arrays.toString(bootClasses));
201 throw new RuntimeException("Could not find class java.util.List.");
202 }
203 }
204
205 private static void printClassLoaderClasses(ClassLoader cl) {
206 for (;;) {
207 if (cl == null || !cl.getClass().getName().startsWith("dalvik.system")) {
208 break;
209 }
210
211 ClassLoader saved = cl;
212 for (;;) {
213 if (cl == null || !cl.getClass().getName().startsWith("dalvik.system")) {
214 break;
215 }
216 String s = cl.toString();
217 int index1 = s.indexOf("zip file");
218 int index2 = s.indexOf(']', index1);
219 if (index2 < 0) {
220 throw new RuntimeException("Unexpected classloader " + s);
221 }
222 String zip_file = s.substring(index1, index2);
223 int index3 = zip_file.indexOf('"');
224 int index4 = zip_file.indexOf('"', index3 + 1);
225 if (index4 < 0) {
226 throw new RuntimeException("Unexpected classloader " + s);
227 }
228 String paths = zip_file.substring(index3 + 1, index4);
229 String pathArray[] = paths.split(":");
230 for (String path : pathArray) {
231 int index5 = path.lastIndexOf('/');
232 System.out.print(path.substring(index5 + 1));
233 System.out.print('+');
234 }
235 System.out.print(" -> ");
236 cl = cl.getParent();
237 }
238 System.out.println();
239 Class<?> classes[] = getClassLoaderClasses(saved);
240 Arrays.sort(classes, new ClassNameComparator());
241 System.out.println(Arrays.toString(classes));
242
243 cl = saved.getParent();
244 }
245 }
246
Alex Lighte4a88632017-01-10 07:41:24 -0800247 private static native boolean isModifiableClass(Class<?> c);
Andreas Gampee492ae32016-10-28 19:34:57 -0700248 private static native String[] getClassSignature(Class<?> c);
Andreas Gampe4fd66ec2017-01-05 14:42:13 -0800249
250 private static native boolean isInterface(Class<?> c);
251 private static native boolean isArrayClass(Class<?> c);
Andreas Gampeac587272017-01-05 15:21:34 -0800252
Andreas Gampe64013e52017-01-06 13:07:19 -0800253 private static native int getClassModifiers(Class<?> c);
254
Andreas Gampeac587272017-01-05 15:21:34 -0800255 private static native Object[] getClassFields(Class<?> c);
Andreas Gampe18fee4d2017-01-06 11:36:35 -0800256 private static native Object[] getClassMethods(Class<?> c);
Andreas Gampe70f16392017-01-16 14:20:10 -0800257 private static native Class<?>[] getImplementedInterfaces(Class<?> c);
Andreas Gampeff9d2092017-01-06 09:12:49 -0800258
259 private static native int getClassStatus(Class<?> c);
260
Andreas Gampe8f5b6032017-01-06 15:50:55 -0800261 private static native Object getClassLoader(Class<?> c);
262
Andreas Gampe70f16392017-01-16 14:20:10 -0800263 private static native Class<?>[] getClassLoaderClasses(ClassLoader cl);
264
Andreas Gampeff9d2092017-01-06 09:12:49 -0800265 private static class TestForNonInit {
266 public static double dummy = Math.random(); // So it can't be compile-time initialized.
267 }
268
269 private static class TestForInitFail {
270 public static int dummy = ((int)Math.random())/0; // So it throws when initializing.
271 }
Andreas Gampe8b07e472017-01-06 14:20:39 -0800272
273 public static interface InfA {
274 }
275 public static interface InfB extends InfA {
276 }
277 public static interface InfC extends InfB {
278 }
279
280 public abstract static class ClassA implements InfA {
281 }
282 public abstract static class ClassB extends ClassA implements InfB {
283 }
284 public abstract static class ClassC implements InfA, InfC {
285 }
Andreas Gampe70f16392017-01-16 14:20:10 -0800286
287 private static final String DEX1 = System.getenv("DEX_LOCATION") + "/912-classes.jar";
288 private static final String DEX2 = System.getenv("DEX_LOCATION") + "/912-classes-ex.jar";
289
290 private static ClassLoader create(ClassLoader parent, String... elements) throws Exception {
291 // Note: We use a PathClassLoader, as we do not care about code performance. We only load
292 // the classes, and they're empty.
293 Class<?> pathClassLoaderClass = Class.forName("dalvik.system.PathClassLoader");
294 Constructor<?> pathClassLoaderInit = pathClassLoaderClass.getConstructor(String.class,
295 ClassLoader.class);
296 String path = String.join(":", elements);
297 return (ClassLoader) pathClassLoaderInit.newInstance(path, parent);
298 }
299
300 private static class ClassNameComparator implements Comparator<Class<?>> {
301 public int compare(Class<?> c1, Class<?> c2) {
302 return c1.getName().compareTo(c2.getName());
303 }
304 }
Andreas Gampee492ae32016-10-28 19:34:57 -0700305}