/
SandboxClassLoader.java
208 lines (183 loc) · 7.05 KB
/
SandboxClassLoader.java
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package org.robolectric.internal.bytecode;
import static com.google.common.base.StandardSystemProperty.JAVA_CLASS_PATH;
import static com.google.common.base.StandardSystemProperty.PATH_SEPARATOR;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.atomic.AtomicInteger;
import javax.inject.Inject;
import org.robolectric.util.Logger;
import org.robolectric.util.PerfStatsCollector;
import org.robolectric.util.Util;
/**
* Class loader that modifies the bytecode of Android classes to insert calls to Robolectric's
* shadow classes.
*/
public class SandboxClassLoader extends URLClassLoader {
// The directory where instrumented class files will be dumped
private static final String DUMP_CLASSES_PROPERTY = "robolectric.dumpClassesDirectory";
private static final AtomicInteger DUMP_CLASSES_COUNTER = new AtomicInteger();
private final InstrumentationConfiguration config;
private final ResourceProvider resourceProvider;
private final ClassInstrumentor classInstrumentor;
private final ClassNodeProvider classNodeProvider;
private final String dumpClassesDirectory;
/** Constructor for use by tests. */
SandboxClassLoader(InstrumentationConfiguration config) {
this(config, new UrlResourceProvider(), new ClassInstrumentor(new ShadowDecorator()));
}
@Inject
public SandboxClassLoader(
InstrumentationConfiguration config,
ResourceProvider resourceProvider,
ClassInstrumentor classInstrumentor) {
this(
Thread.currentThread().getContextClassLoader(),
config,
resourceProvider,
classInstrumentor);
}
public SandboxClassLoader(
ClassLoader erstwhileClassLoader,
InstrumentationConfiguration config,
ResourceProvider resourceProvider,
ClassInstrumentor classInstrumentor) {
super(getClassPathUrls(erstwhileClassLoader), erstwhileClassLoader);
this.config = config;
this.resourceProvider = resourceProvider;
this.classInstrumentor = classInstrumentor;
classNodeProvider =
new ClassNodeProvider() {
@Override
protected byte[] getClassBytes(String internalClassName) throws ClassNotFoundException {
return getByteCode(internalClassName);
}
};
this.dumpClassesDirectory = System.getProperty(DUMP_CLASSES_PROPERTY, "");
}
private static URL[] getClassPathUrls(ClassLoader classloader) {
if (classloader instanceof URLClassLoader) {
return ((URLClassLoader) classloader).getURLs();
}
return parseJavaClassPath();
}
// TODO(https://github.com/google/guava/issues/2956): Use a public API once one is available.
private static URL[] parseJavaClassPath() {
ImmutableList.Builder<URL> urls = ImmutableList.builder();
for (String entry : Splitter.on(PATH_SEPARATOR.value()).split(JAVA_CLASS_PATH.value())) {
try {
try {
urls.add(new File(entry).toURI().toURL());
} catch (SecurityException e) { // File.toURI checks to see if the file is a directory
urls.add(new URL("file", null, new File(entry).getAbsolutePath()));
}
} catch (MalformedURLException e) {
Logger.strict("malformed classpath entry: " + entry, e);
}
}
return urls.build().toArray(new URL[0]);
}
@Override
public URL getResource(String name) {
if (config.shouldAcquireResource(name)) {
return resourceProvider.getResource(name);
}
URL fromParent = super.getResource(name);
if (fromParent != null) {
return fromParent;
}
return resourceProvider.getResource(name);
}
private InputStream getClassBytesAsStreamPreferringLocalUrls(String resName) {
InputStream fromUrlsClassLoader = resourceProvider.getResourceAsStream(resName);
if (fromUrlsClassLoader != null) {
return fromUrlsClassLoader;
}
return super.getResourceAsStream(resName);
}
@Override
public Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
synchronized (getClassLoadingLock(name)) {
Class<?> loadedClass = findLoadedClass(name);
if (loadedClass != null) {
return loadedClass;
}
if (config.shouldAcquire(name)) {
loadedClass =
PerfStatsCollector.getInstance()
.measure("load sandboxed class", () -> maybeInstrumentClass(name));
} else {
loadedClass = getParent().loadClass(name);
}
if (resolve) {
resolveClass(loadedClass);
}
return loadedClass;
}
}
protected Class<?> maybeInstrumentClass(String className) throws ClassNotFoundException {
final byte[] origClassBytes = getByteCode(className);
try {
final byte[] bytes;
ClassDetails classDetails = new ClassDetails(origClassBytes);
if (config.shouldInstrument(classDetails)) {
bytes = classInstrumentor.instrument(classDetails, config, classNodeProvider);
maybeDumpClassBytes(classDetails, bytes);
} else {
bytes = postProcessUninstrumentedClass(classDetails);
}
ensurePackage(className);
return defineClass(className, bytes, 0, bytes.length);
} catch (Exception e) {
throw new ClassNotFoundException("couldn't load " + className, e);
} catch (OutOfMemoryError e) {
System.err.println("[ERROR] couldn't load " + className + " in " + this);
throw e;
}
}
private void maybeDumpClassBytes(ClassDetails classDetails, byte[] classBytes) {
if (!Strings.isNullOrEmpty(dumpClassesDirectory)) {
String outputClassName =
classDetails.getName() + "-robo-instrumented-" + DUMP_CLASSES_COUNTER.getAndIncrement();
Path path = Paths.get(dumpClassesDirectory, outputClassName + ".class");
try {
Files.write(path, classBytes);
} catch (IOException e) {
throw new AssertionError(e);
}
}
}
protected byte[] postProcessUninstrumentedClass(ClassDetails classDetails) {
return classDetails.getClassBytes();
}
protected byte[] getByteCode(String className) throws ClassNotFoundException {
String classFilename = className.replace('.', '/') + ".class";
try (InputStream classBytesStream = getClassBytesAsStreamPreferringLocalUrls(classFilename)) {
if (classBytesStream == null) {
throw new ClassNotFoundException(className);
}
return Util.readBytes(classBytesStream);
} catch (IOException e) {
throw new ClassNotFoundException("couldn't load " + className, e);
}
}
private void ensurePackage(final String className) {
int lastDotIndex = className.lastIndexOf('.');
if (lastDotIndex != -1) {
String pckgName = className.substring(0, lastDotIndex);
Package pckg = getPackage(pckgName);
if (pckg == null) {
definePackage(pckgName, null, null, null, null, null, null, null);
}
}
}
}