blob: 4f23eec4a05f89e577329cf876e93ec19e342dae (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
class MyClassLoader extends ClassLoader {
MyClassLoader() throws Exception {
super(MyClassLoader.class.getClassLoader());
// Some magic to get access to the pathList field of BaseDexClassLoader.
ClassLoader loader = getClass().getClassLoader();
Class<?> baseDexClassLoader = loader.getClass().getSuperclass();
Field f = baseDexClassLoader.getDeclaredField("pathList");
f.setAccessible(true);
Object pathList = f.get(loader);
// Some magic to get access to the dexField field of pathList.
f = pathList.getClass().getDeclaredField("dexElements");
f.setAccessible(true);
dexElements = (Object[]) f.get(pathList);
dexFileField = dexElements[0].getClass().getDeclaredField("dexFile");
dexFileField.setAccessible(true);
}
Object[] dexElements;
Field dexFileField;
protected Class<?> loadClass(String className, boolean resolve) throws ClassNotFoundException {
System.out.println("Request for " + className);
// We're only going to handle LoadedByMyClassLoader.
if (className != "LoadedByMyClassLoader") {
return getParent().loadClass(className);
}
// Mimic what DexPathList.findClass is doing.
try {
for (Object element : dexElements) {
Object dex = dexFileField.get(element);
Method method = dex.getClass().getDeclaredMethod(
"loadClassBinaryName", String.class, ClassLoader.class, List.class);
if (dex != null) {
Class clazz = (Class)method.invoke(dex, className, this, null);
if (clazz != null) {
return clazz;
}
}
}
} catch (Exception e) { /* Ignore */ }
return null;
}
}
class LoadedByMyClassLoader {
/// CHECK-START: void LoadedByMyClassLoader.bar() inliner (before)
/// CHECK: LoadClass
/// CHECK-NEXT: ClinitCheck
/// CHECK-NEXT: InvokeStaticOrDirect
/// CHECK-NEXT: LoadClass
/// CHECK-NEXT: ClinitCheck
/// CHECK-NEXT: StaticFieldGet
/// CHECK-NEXT: LoadString
/// CHECK-NEXT: NullCheck
/// CHECK-NEXT: InvokeVirtual
/// CHECK-START: void LoadedByMyClassLoader.bar() inliner (after)
/// CHECK: LoadClass
/// CHECK-NEXT: ClinitCheck
/* We inlined Main.$inline$bar */
/// CHECK-NEXT: LoadClass
/// CHECK-NEXT: ClinitCheck
/// CHECK-NEXT: StaticFieldGet
/// CHECK-NEXT: LoadString
/// CHECK-NEXT: NullCheck
/// CHECK-NEXT: InvokeVirtual
/// CHECK-START: void LoadedByMyClassLoader.bar() register (before)
/* Load and initialize Main */
/// CHECK: LoadClass gen_clinit_check:true
/* Load and initialize System */
/// CHECK-NEXT: LoadClass gen_clinit_check:true
/// CHECK-NEXT: StaticFieldGet
/// CHECK-NEXT: LoadString
/// CHECK-NEXT: NullCheck
/// CHECK-NEXT: InvokeVirtual
public static void bar() {
Main.$inline$bar();
System.out.println("In between the two calls.");
Main.$noinline$bar();
}
}
public class Main {
public static void main(String[] args) throws Exception {
MyClassLoader o = new MyClassLoader();
Class foo = o.loadClass("LoadedByMyClassLoader");
Method m = foo.getDeclaredMethod("bar");
m.invoke(null);
}
public static void $inline$bar() {
}
public static void $noinline$bar() {
try {
System.out.println("In $noinline$bar");
} catch (Throwable t) { /* Ignore */ }
}
}
|