Nicolas Geoffray | 78f4fa7 | 2015-06-12 09:35:05 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
| 17 | import java.lang.reflect.Field; |
| 18 | import java.lang.reflect.Method; |
| 19 | import java.util.List; |
| 20 | |
| 21 | class MyClassLoader extends ClassLoader { |
| 22 | MyClassLoader() throws Exception { |
| 23 | super(MyClassLoader.class.getClassLoader()); |
| 24 | |
| 25 | // Some magic to get access to the pathList field of BaseDexClassLoader. |
| 26 | ClassLoader loader = getClass().getClassLoader(); |
| 27 | Class<?> baseDexClassLoader = loader.getClass().getSuperclass(); |
| 28 | Field f = baseDexClassLoader.getDeclaredField("pathList"); |
| 29 | f.setAccessible(true); |
| 30 | Object pathList = f.get(loader); |
| 31 | |
| 32 | // Some magic to get access to the dexField field of pathList. |
| 33 | f = pathList.getClass().getDeclaredField("dexElements"); |
| 34 | f.setAccessible(true); |
| 35 | dexElements = (Object[]) f.get(pathList); |
| 36 | dexFileField = dexElements[0].getClass().getDeclaredField("dexFile"); |
| 37 | dexFileField.setAccessible(true); |
| 38 | } |
| 39 | |
| 40 | Object[] dexElements; |
| 41 | Field dexFileField; |
| 42 | |
| 43 | protected Class<?> loadClass(String className, boolean resolve) throws ClassNotFoundException { |
| 44 | System.out.println("Request for " + className); |
| 45 | |
| 46 | // We're only going to handle LoadedByMyClassLoader. |
| 47 | if (className != "LoadedByMyClassLoader") { |
| 48 | return getParent().loadClass(className); |
| 49 | } |
| 50 | |
| 51 | // Mimic what DexPathList.findClass is doing. |
| 52 | try { |
| 53 | for (Object element : dexElements) { |
| 54 | Object dex = dexFileField.get(element); |
| 55 | Method method = dex.getClass().getDeclaredMethod( |
| 56 | "loadClassBinaryName", String.class, ClassLoader.class, List.class); |
| 57 | |
| 58 | if (dex != null) { |
| 59 | Class clazz = (Class)method.invoke(dex, className, this, null); |
| 60 | if (clazz != null) { |
| 61 | return clazz; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | } catch (Exception e) { /* Ignore */ } |
| 66 | return null; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | class LoadedByMyClassLoader { |
| 71 | /// CHECK-START: void LoadedByMyClassLoader.bar() inliner (before) |
| 72 | /// CHECK: LoadClass |
| 73 | /// CHECK-NEXT: ClinitCheck |
| 74 | /// CHECK-NEXT: InvokeStaticOrDirect |
| 75 | /// CHECK-NEXT: LoadClass |
| 76 | /// CHECK-NEXT: ClinitCheck |
| 77 | /// CHECK-NEXT: StaticFieldGet |
| 78 | /// CHECK-NEXT: LoadString |
| 79 | /// CHECK-NEXT: NullCheck |
| 80 | /// CHECK-NEXT: InvokeVirtual |
| 81 | |
| 82 | /// CHECK-START: void LoadedByMyClassLoader.bar() inliner (after) |
| 83 | /// CHECK: LoadClass |
| 84 | /// CHECK-NEXT: ClinitCheck |
Nicolas Geoffray | 9714a6e | 2015-06-23 12:09:55 +0100 | [diff] [blame^] | 85 | /* We inlined FirstSeenByMyClassLoader.$inline$bar */ |
Nicolas Geoffray | 78f4fa7 | 2015-06-12 09:35:05 +0100 | [diff] [blame] | 86 | /// CHECK-NEXT: LoadClass |
| 87 | /// CHECK-NEXT: ClinitCheck |
| 88 | /// CHECK-NEXT: StaticFieldGet |
| 89 | /// CHECK-NEXT: LoadString |
| 90 | /// CHECK-NEXT: NullCheck |
| 91 | /// CHECK-NEXT: InvokeVirtual |
| 92 | |
| 93 | /// CHECK-START: void LoadedByMyClassLoader.bar() register (before) |
Nicolas Geoffray | 9714a6e | 2015-06-23 12:09:55 +0100 | [diff] [blame^] | 94 | /* Load and initialize FirstSeenByMyClassLoader */ |
Nicolas Geoffray | 78f4fa7 | 2015-06-12 09:35:05 +0100 | [diff] [blame] | 95 | /// CHECK: LoadClass gen_clinit_check:true |
| 96 | /* Load and initialize System */ |
| 97 | /// CHECK-NEXT: LoadClass gen_clinit_check:true |
| 98 | /// CHECK-NEXT: StaticFieldGet |
| 99 | /// CHECK-NEXT: LoadString |
| 100 | /// CHECK-NEXT: NullCheck |
| 101 | /// CHECK-NEXT: InvokeVirtual |
| 102 | public static void bar() { |
Nicolas Geoffray | 9714a6e | 2015-06-23 12:09:55 +0100 | [diff] [blame^] | 103 | FirstSeenByMyClassLoader.$inline$bar(); |
Nicolas Geoffray | 78f4fa7 | 2015-06-12 09:35:05 +0100 | [diff] [blame] | 104 | System.out.println("In between the two calls."); |
Nicolas Geoffray | 9714a6e | 2015-06-23 12:09:55 +0100 | [diff] [blame^] | 105 | FirstSeenByMyClassLoader.$noinline$bar(); |
Nicolas Geoffray | 78f4fa7 | 2015-06-12 09:35:05 +0100 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
Nicolas Geoffray | f7714e6 | 2015-06-19 10:45:44 +0100 | [diff] [blame] | 109 | public class Main { |
Nicolas Geoffray | 78f4fa7 | 2015-06-12 09:35:05 +0100 | [diff] [blame] | 110 | public static void main(String[] args) throws Exception { |
| 111 | MyClassLoader o = new MyClassLoader(); |
| 112 | Class foo = o.loadClass("LoadedByMyClassLoader"); |
| 113 | Method m = foo.getDeclaredMethod("bar"); |
| 114 | m.invoke(null); |
| 115 | } |
Nicolas Geoffray | 78f4fa7 | 2015-06-12 09:35:05 +0100 | [diff] [blame] | 116 | } |