Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 1 | /* |
| 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 Gampe | 70f1639 | 2017-01-16 14:20:10 -0800 | [diff] [blame] | 17 | import java.lang.reflect.Constructor; |
Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 18 | import java.lang.reflect.Proxy; |
| 19 | import java.util.Arrays; |
Andreas Gampe | 70f1639 | 2017-01-16 14:20:10 -0800 | [diff] [blame] | 20 | import java.util.Comparator; |
Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 21 | |
| 22 | public class Main { |
| 23 | public static void main(String[] args) throws Exception { |
Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 24 | doTest(); |
| 25 | } |
| 26 | |
| 27 | public static void doTest() throws Exception { |
| 28 | testClass("java.lang.Object"); |
| 29 | testClass("java.lang.String"); |
| 30 | testClass("java.lang.Math"); |
| 31 | testClass("java.util.List"); |
| 32 | |
| 33 | testClass(getProxyClass()); |
| 34 | |
| 35 | testClass(int.class); |
| 36 | testClass(double[].class); |
Andreas Gampe | 4fd66ec | 2017-01-05 14:42:13 -0800 | [diff] [blame] | 37 | |
| 38 | testClassType(int.class); |
| 39 | testClassType(getProxyClass()); |
| 40 | testClassType(Runnable.class); |
| 41 | testClassType(String.class); |
| 42 | |
| 43 | testClassType(int[].class); |
| 44 | testClassType(Runnable[].class); |
| 45 | testClassType(String[].class); |
Andreas Gampe | ac58727 | 2017-01-05 15:21:34 -0800 | [diff] [blame] | 46 | |
| 47 | testClassFields(Integer.class); |
| 48 | testClassFields(int.class); |
| 49 | testClassFields(String[].class); |
Andreas Gampe | ff9d209 | 2017-01-06 09:12:49 -0800 | [diff] [blame] | 50 | |
Andreas Gampe | 18fee4d | 2017-01-06 11:36:35 -0800 | [diff] [blame] | 51 | testClassMethods(Integer.class); |
| 52 | testClassMethods(int.class); |
| 53 | testClassMethods(String[].class); |
| 54 | |
Andreas Gampe | ff9d209 | 2017-01-06 09:12:49 -0800 | [diff] [blame] | 55 | testClassStatus(int.class); |
| 56 | testClassStatus(String[].class); |
| 57 | testClassStatus(Object.class); |
| 58 | testClassStatus(TestForNonInit.class); |
| 59 | try { |
| 60 | System.out.println(TestForInitFail.dummy); |
| 61 | } catch (ExceptionInInitializerError e) { |
| 62 | } |
| 63 | testClassStatus(TestForInitFail.class); |
Andreas Gampe | 8b07e47 | 2017-01-06 14:20:39 -0800 | [diff] [blame] | 64 | |
| 65 | testInterfaces(int.class); |
| 66 | testInterfaces(String[].class); |
| 67 | testInterfaces(Object.class); |
| 68 | testInterfaces(InfA.class); |
| 69 | testInterfaces(InfB.class); |
| 70 | testInterfaces(InfC.class); |
| 71 | testInterfaces(ClassA.class); |
| 72 | testInterfaces(ClassB.class); |
| 73 | testInterfaces(ClassC.class); |
Andreas Gampe | 8f5b603 | 2017-01-06 15:50:55 -0800 | [diff] [blame] | 74 | |
| 75 | testClassLoader(String.class); |
| 76 | testClassLoader(String[].class); |
| 77 | testClassLoader(InfA.class); |
| 78 | testClassLoader(getProxyClass()); |
Andreas Gampe | 70f1639 | 2017-01-16 14:20:10 -0800 | [diff] [blame] | 79 | |
| 80 | testClassLoaderClasses(); |
Andreas Gampe | 812a244 | 2017-01-19 22:04:46 -0800 | [diff] [blame] | 81 | |
| 82 | System.out.println(); |
| 83 | |
| 84 | testClassVersion(); |
Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame^] | 85 | |
| 86 | System.out.println(); |
| 87 | |
| 88 | testClassEvents(); |
Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | private static Class<?> proxyClass = null; |
| 92 | |
| 93 | private static Class<?> getProxyClass() throws Exception { |
| 94 | if (proxyClass != null) { |
| 95 | return proxyClass; |
| 96 | } |
| 97 | |
| 98 | proxyClass = Proxy.getProxyClass(Main.class.getClassLoader(), new Class[] { Runnable.class }); |
| 99 | return proxyClass; |
| 100 | } |
| 101 | |
| 102 | private static void testClass(String className) throws Exception { |
| 103 | Class<?> base = Class.forName(className); |
| 104 | testClass(base); |
| 105 | } |
| 106 | |
| 107 | private static void testClass(Class<?> base) throws Exception { |
| 108 | String[] result = getClassSignature(base); |
| 109 | System.out.println(Arrays.toString(result)); |
Andreas Gampe | 64013e5 | 2017-01-06 13:07:19 -0800 | [diff] [blame] | 110 | int mod = getClassModifiers(base); |
| 111 | if (mod != base.getModifiers()) { |
| 112 | throw new RuntimeException("Unexpected modifiers: " + base.getModifiers() + " vs " + mod); |
| 113 | } |
| 114 | System.out.println(Integer.toHexString(mod)); |
Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Andreas Gampe | 4fd66ec | 2017-01-05 14:42:13 -0800 | [diff] [blame] | 117 | private static void testClassType(Class<?> c) throws Exception { |
| 118 | boolean isInterface = isInterface(c); |
| 119 | boolean isArray = isArrayClass(c); |
Alex Light | e4a8863 | 2017-01-10 07:41:24 -0800 | [diff] [blame] | 120 | boolean isModifiable = isModifiableClass(c); |
| 121 | System.out.println(c.getName() + " interface=" + isInterface + " array=" + isArray + |
| 122 | " modifiable=" + isModifiable); |
Andreas Gampe | 4fd66ec | 2017-01-05 14:42:13 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Andreas Gampe | ac58727 | 2017-01-05 15:21:34 -0800 | [diff] [blame] | 125 | private static void testClassFields(Class<?> c) throws Exception { |
| 126 | System.out.println(Arrays.toString(getClassFields(c))); |
| 127 | } |
| 128 | |
Andreas Gampe | 18fee4d | 2017-01-06 11:36:35 -0800 | [diff] [blame] | 129 | private static void testClassMethods(Class<?> c) throws Exception { |
| 130 | System.out.println(Arrays.toString(getClassMethods(c))); |
| 131 | } |
| 132 | |
Andreas Gampe | ff9d209 | 2017-01-06 09:12:49 -0800 | [diff] [blame] | 133 | private static void testClassStatus(Class<?> c) { |
| 134 | System.out.println(c + " " + Integer.toBinaryString(getClassStatus(c))); |
| 135 | } |
| 136 | |
Andreas Gampe | 8b07e47 | 2017-01-06 14:20:39 -0800 | [diff] [blame] | 137 | private static void testInterfaces(Class<?> c) { |
| 138 | System.out.println(c + " " + Arrays.toString(getImplementedInterfaces(c))); |
| 139 | } |
| 140 | |
Andreas Gampe | 8f5b603 | 2017-01-06 15:50:55 -0800 | [diff] [blame] | 141 | private static boolean IsBootClassLoader(ClassLoader l) { |
| 142 | // Hacky check for Android's fake boot classloader. |
| 143 | return l.getClass().getName().equals("java.lang.BootClassLoader"); |
| 144 | } |
| 145 | |
| 146 | private static void testClassLoader(Class<?> c) { |
| 147 | Object cl = getClassLoader(c); |
| 148 | System.out.println(c + " " + (cl != null ? cl.getClass().getName() : "null")); |
| 149 | if (cl == null) { |
| 150 | if (c.getClassLoader() != null && !IsBootClassLoader(c.getClassLoader())) { |
| 151 | throw new RuntimeException("Expected " + c.getClassLoader() + ", but got null."); |
| 152 | } |
| 153 | } else { |
| 154 | if (!(cl instanceof ClassLoader)) { |
| 155 | throw new RuntimeException("Unexpected \"classloader\": " + cl + " (" + cl.getClass() + |
| 156 | ")"); |
| 157 | } |
| 158 | if (cl != c.getClassLoader()) { |
| 159 | throw new RuntimeException("Unexpected classloader: " + c.getClassLoader() + " vs " + cl); |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
Andreas Gampe | 70f1639 | 2017-01-16 14:20:10 -0800 | [diff] [blame] | 164 | private static void testClassLoaderClasses() throws Exception { |
| 165 | ClassLoader boot = ClassLoader.getSystemClassLoader().getParent(); |
| 166 | while (boot.getParent() != null) { |
| 167 | boot = boot.getParent(); |
| 168 | } |
| 169 | |
| 170 | System.out.println(); |
| 171 | System.out.println("boot <- src <- src-ex (A,B)"); |
| 172 | ClassLoader cl1 = create(create(boot, DEX1), DEX2); |
| 173 | Class.forName("B", false, cl1); |
| 174 | Class.forName("A", false, cl1); |
| 175 | printClassLoaderClasses(cl1); |
| 176 | |
| 177 | System.out.println(); |
| 178 | System.out.println("boot <- src (B) <- src-ex (A, List)"); |
| 179 | ClassLoader cl2 = create(create(boot, DEX1), DEX2); |
| 180 | Class.forName("A", false, cl2); |
| 181 | Class.forName("java.util.List", false, cl2); |
| 182 | Class.forName("B", false, cl2.getParent()); |
| 183 | printClassLoaderClasses(cl2); |
| 184 | |
| 185 | System.out.println(); |
| 186 | System.out.println("boot <- src+src-ex (A,B)"); |
| 187 | ClassLoader cl3 = create(boot, DEX1, DEX2); |
| 188 | Class.forName("B", false, cl3); |
| 189 | Class.forName("A", false, cl3); |
| 190 | printClassLoaderClasses(cl3); |
| 191 | |
| 192 | // Check that the boot classloader dumps something non-empty. |
| 193 | Class<?>[] bootClasses = getClassLoaderClasses(boot); |
| 194 | if (bootClasses.length == 0) { |
| 195 | throw new RuntimeException("No classes initiated by boot classloader."); |
| 196 | } |
| 197 | // Check that at least java.util.List is loaded. |
| 198 | boolean foundList = false; |
| 199 | for (Class<?> c : bootClasses) { |
| 200 | if (c == java.util.List.class) { |
| 201 | foundList = true; |
| 202 | break; |
| 203 | } |
| 204 | } |
| 205 | if (!foundList) { |
| 206 | System.out.println(Arrays.toString(bootClasses)); |
| 207 | throw new RuntimeException("Could not find class java.util.List."); |
| 208 | } |
| 209 | } |
| 210 | |
Andreas Gampe | 812a244 | 2017-01-19 22:04:46 -0800 | [diff] [blame] | 211 | private static void testClassVersion() { |
| 212 | System.out.println(Arrays.toString(getClassVersion(Main.class))); |
| 213 | } |
| 214 | |
Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame^] | 215 | private static void testClassEvents() throws Exception { |
| 216 | ClassLoader cl = Main.class.getClassLoader(); |
| 217 | while (cl.getParent() != null) { |
| 218 | cl = cl.getParent(); |
| 219 | } |
| 220 | final ClassLoader boot = cl; |
| 221 | |
| 222 | enableClassLoadEvents(true); |
| 223 | |
| 224 | ClassLoader cl1 = create(boot, DEX1, DEX2); |
| 225 | System.out.println("B, false"); |
| 226 | Class.forName("B", false, cl1); |
| 227 | |
| 228 | ClassLoader cl2 = create(boot, DEX1, DEX2); |
| 229 | System.out.println("B, true"); |
| 230 | Class.forName("B", true, cl2); |
| 231 | |
| 232 | ClassLoader cl3 = create(boot, DEX1, DEX2); |
| 233 | System.out.println("C, false"); |
| 234 | Class.forName("C", false, cl3); |
| 235 | System.out.println("A, false"); |
| 236 | Class.forName("A", false, cl3); |
| 237 | |
| 238 | ClassLoader cl4 = create(boot, DEX1, DEX2); |
| 239 | System.out.println("C, true"); |
| 240 | Class.forName("C", true, cl4); |
| 241 | System.out.println("A, true"); |
| 242 | Class.forName("A", true, cl4); |
| 243 | |
| 244 | ClassLoader cl5 = create(boot, DEX1, DEX2); |
| 245 | System.out.println("A, true"); |
| 246 | Class.forName("A", true, cl5); |
| 247 | System.out.println("C, true"); |
| 248 | Class.forName("C", true, cl5); |
| 249 | |
| 250 | Runnable r = new Runnable() { |
| 251 | @Override |
| 252 | public void run() { |
| 253 | try { |
| 254 | ClassLoader cl6 = create(boot, DEX1, DEX2); |
| 255 | System.out.println("C, true"); |
| 256 | Class.forName("C", true, cl6); |
| 257 | } catch (Exception e) { |
| 258 | throw new RuntimeException(e); |
| 259 | } |
| 260 | } |
| 261 | }; |
| 262 | Thread t = new Thread(r, "TestRunner"); |
| 263 | t.start(); |
| 264 | t.join(); |
| 265 | |
| 266 | enableClassLoadEvents(false); |
| 267 | } |
| 268 | |
Andreas Gampe | 70f1639 | 2017-01-16 14:20:10 -0800 | [diff] [blame] | 269 | private static void printClassLoaderClasses(ClassLoader cl) { |
| 270 | for (;;) { |
| 271 | if (cl == null || !cl.getClass().getName().startsWith("dalvik.system")) { |
| 272 | break; |
| 273 | } |
| 274 | |
| 275 | ClassLoader saved = cl; |
| 276 | for (;;) { |
| 277 | if (cl == null || !cl.getClass().getName().startsWith("dalvik.system")) { |
| 278 | break; |
| 279 | } |
| 280 | String s = cl.toString(); |
| 281 | int index1 = s.indexOf("zip file"); |
| 282 | int index2 = s.indexOf(']', index1); |
| 283 | if (index2 < 0) { |
| 284 | throw new RuntimeException("Unexpected classloader " + s); |
| 285 | } |
| 286 | String zip_file = s.substring(index1, index2); |
| 287 | int index3 = zip_file.indexOf('"'); |
| 288 | int index4 = zip_file.indexOf('"', index3 + 1); |
| 289 | if (index4 < 0) { |
| 290 | throw new RuntimeException("Unexpected classloader " + s); |
| 291 | } |
| 292 | String paths = zip_file.substring(index3 + 1, index4); |
| 293 | String pathArray[] = paths.split(":"); |
| 294 | for (String path : pathArray) { |
| 295 | int index5 = path.lastIndexOf('/'); |
| 296 | System.out.print(path.substring(index5 + 1)); |
| 297 | System.out.print('+'); |
| 298 | } |
| 299 | System.out.print(" -> "); |
| 300 | cl = cl.getParent(); |
| 301 | } |
| 302 | System.out.println(); |
| 303 | Class<?> classes[] = getClassLoaderClasses(saved); |
| 304 | Arrays.sort(classes, new ClassNameComparator()); |
| 305 | System.out.println(Arrays.toString(classes)); |
| 306 | |
| 307 | cl = saved.getParent(); |
| 308 | } |
| 309 | } |
| 310 | |
Alex Light | e4a8863 | 2017-01-10 07:41:24 -0800 | [diff] [blame] | 311 | private static native boolean isModifiableClass(Class<?> c); |
Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 312 | private static native String[] getClassSignature(Class<?> c); |
Andreas Gampe | 4fd66ec | 2017-01-05 14:42:13 -0800 | [diff] [blame] | 313 | |
| 314 | private static native boolean isInterface(Class<?> c); |
| 315 | private static native boolean isArrayClass(Class<?> c); |
Andreas Gampe | ac58727 | 2017-01-05 15:21:34 -0800 | [diff] [blame] | 316 | |
Andreas Gampe | 64013e5 | 2017-01-06 13:07:19 -0800 | [diff] [blame] | 317 | private static native int getClassModifiers(Class<?> c); |
| 318 | |
Andreas Gampe | ac58727 | 2017-01-05 15:21:34 -0800 | [diff] [blame] | 319 | private static native Object[] getClassFields(Class<?> c); |
Andreas Gampe | 18fee4d | 2017-01-06 11:36:35 -0800 | [diff] [blame] | 320 | private static native Object[] getClassMethods(Class<?> c); |
Andreas Gampe | 70f1639 | 2017-01-16 14:20:10 -0800 | [diff] [blame] | 321 | private static native Class<?>[] getImplementedInterfaces(Class<?> c); |
Andreas Gampe | ff9d209 | 2017-01-06 09:12:49 -0800 | [diff] [blame] | 322 | |
| 323 | private static native int getClassStatus(Class<?> c); |
| 324 | |
Andreas Gampe | 8f5b603 | 2017-01-06 15:50:55 -0800 | [diff] [blame] | 325 | private static native Object getClassLoader(Class<?> c); |
| 326 | |
Andreas Gampe | 70f1639 | 2017-01-16 14:20:10 -0800 | [diff] [blame] | 327 | private static native Class<?>[] getClassLoaderClasses(ClassLoader cl); |
| 328 | |
Andreas Gampe | 812a244 | 2017-01-19 22:04:46 -0800 | [diff] [blame] | 329 | private static native int[] getClassVersion(Class<?> c); |
| 330 | |
Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame^] | 331 | private static native void enableClassLoadEvents(boolean b); |
| 332 | |
Andreas Gampe | ff9d209 | 2017-01-06 09:12:49 -0800 | [diff] [blame] | 333 | private static class TestForNonInit { |
| 334 | public static double dummy = Math.random(); // So it can't be compile-time initialized. |
| 335 | } |
| 336 | |
| 337 | private static class TestForInitFail { |
| 338 | public static int dummy = ((int)Math.random())/0; // So it throws when initializing. |
| 339 | } |
Andreas Gampe | 8b07e47 | 2017-01-06 14:20:39 -0800 | [diff] [blame] | 340 | |
| 341 | public static interface InfA { |
| 342 | } |
| 343 | public static interface InfB extends InfA { |
| 344 | } |
| 345 | public static interface InfC extends InfB { |
| 346 | } |
| 347 | |
| 348 | public abstract static class ClassA implements InfA { |
| 349 | } |
| 350 | public abstract static class ClassB extends ClassA implements InfB { |
| 351 | } |
| 352 | public abstract static class ClassC implements InfA, InfC { |
| 353 | } |
Andreas Gampe | 70f1639 | 2017-01-16 14:20:10 -0800 | [diff] [blame] | 354 | |
| 355 | private static final String DEX1 = System.getenv("DEX_LOCATION") + "/912-classes.jar"; |
| 356 | private static final String DEX2 = System.getenv("DEX_LOCATION") + "/912-classes-ex.jar"; |
| 357 | |
| 358 | private static ClassLoader create(ClassLoader parent, String... elements) throws Exception { |
| 359 | // Note: We use a PathClassLoader, as we do not care about code performance. We only load |
| 360 | // the classes, and they're empty. |
| 361 | Class<?> pathClassLoaderClass = Class.forName("dalvik.system.PathClassLoader"); |
| 362 | Constructor<?> pathClassLoaderInit = pathClassLoaderClass.getConstructor(String.class, |
| 363 | ClassLoader.class); |
| 364 | String path = String.join(":", elements); |
| 365 | return (ClassLoader) pathClassLoaderInit.newInstance(path, parent); |
| 366 | } |
| 367 | |
| 368 | private static class ClassNameComparator implements Comparator<Class<?>> { |
| 369 | public int compare(Class<?> c1, Class<?> c2) { |
| 370 | return c1.getName().compareTo(c2.getName()); |
| 371 | } |
| 372 | } |
Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 373 | } |