/
InstrumentationMemberAccessor.java
425 lines (400 loc) · 18.6 KB
/
InstrumentationMemberAccessor.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
* Copyright (c) 2020 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.util.reflection;
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.agent.ByteBuddyAgent;
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
import net.bytebuddy.implementation.MethodCall;
import org.mockito.exceptions.base.MockitoInitializationException;
import org.mockito.internal.SuppressSignatureCheck;
import org.mockito.plugins.MemberAccessor;
import java.lang.instrument.Instrumentation;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.*;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static org.mockito.internal.util.StringUtil.join;
@SuppressSignatureCheck
class InstrumentationMemberAccessor implements MemberAccessor {
private static final Map<Class<?>, Class<?>> WRAPPERS = new HashMap<>();
private static final Instrumentation INSTRUMENTATION;
private static final Dispatcher DISPATCHER;
private static final Throwable INITIALIZATION_ERROR;
static {
WRAPPERS.put(boolean.class, Boolean.class);
WRAPPERS.put(byte.class, Byte.class);
WRAPPERS.put(short.class, Short.class);
WRAPPERS.put(char.class, Character.class);
WRAPPERS.put(int.class, Integer.class);
WRAPPERS.put(long.class, Long.class);
WRAPPERS.put(float.class, Float.class);
WRAPPERS.put(double.class, Double.class);
Instrumentation instrumentation;
Dispatcher dispatcher;
Throwable throwable;
try {
instrumentation = ByteBuddyAgent.install();
// We need to generate a dispatcher instance that is located in a distinguished class
// loader to create a unique (unnamed) module to which we can open other packages to.
// This way, we assure that classes within Mockito's module (which might be a shared,
// unnamed module) do not face escalated privileges where tests might pass that would
// otherwise fail without Mockito's opening.
dispatcher =
new ByteBuddy()
.subclass(Dispatcher.class)
.method(named("getLookup"))
.intercept(MethodCall.invoke(MethodHandles.class.getMethod("lookup")))
.method(named("getModule"))
.intercept(
MethodCall.invoke(Class.class.getMethod("getModule"))
.onMethodCall(
MethodCall.invoke(
Object.class.getMethod("getClass"))))
.method(named("setAccessible"))
.intercept(
MethodCall.invoke(
AccessibleObject.class.getMethod(
"setAccessible", boolean.class))
.onArgument(0)
.withArgument(1))
.method(named("invokeWithArguments"))
.intercept(
MethodCall.invoke(
MethodHandle.class.getMethod(
"invokeWithArguments", Object[].class))
.onArgument(0)
.withArgument(1))
.make()
.load(
InstrumentationMemberAccessor.class.getClassLoader(),
ClassLoadingStrategy.Default.WRAPPER)
.getLoaded()
.getConstructor()
.newInstance();
throwable = null;
} catch (Throwable t) {
instrumentation = null;
dispatcher = null;
throwable = t;
}
INSTRUMENTATION = instrumentation;
DISPATCHER = dispatcher;
INITIALIZATION_ERROR = throwable;
}
@SuppressWarnings(value = "unused")
private final MethodHandle getModule;
@SuppressWarnings(value = "unused")
private final MethodHandle isOpen;
@SuppressWarnings(value = "unused")
private final MethodHandle redefineModule;
@SuppressWarnings(value = "unused")
private final MethodHandle privateLookupIn;
InstrumentationMemberAccessor() {
if (INITIALIZATION_ERROR != null) {
throw new MockitoInitializationException(
join(
"Could not initialize the Mockito instrumentation member accessor",
"",
"This is unexpected on JVMs from Java 9 or later - possibly, the instrumentation API could not be resolved"),
INITIALIZATION_ERROR);
}
try {
Class<?> module = Class.forName("java.lang.Module");
getModule =
MethodHandles.publicLookup()
.findVirtual(Class.class, "getModule", MethodType.methodType(module));
isOpen =
MethodHandles.publicLookup()
.findVirtual(
module,
"isOpen",
MethodType.methodType(boolean.class, String.class));
redefineModule =
MethodHandles.publicLookup()
.findVirtual(
Instrumentation.class,
"redefineModule",
MethodType.methodType(
void.class,
module,
Set.class,
Map.class,
Map.class,
Set.class,
Map.class));
privateLookupIn =
MethodHandles.publicLookup()
.findStatic(
MethodHandles.class,
"privateLookupIn",
MethodType.methodType(
MethodHandles.Lookup.class,
Class.class,
MethodHandles.Lookup.class));
} catch (Throwable t) {
throw new MockitoInitializationException(
"Could not resolve instrumentation invoker", t);
}
}
@Override
public Object newInstance(Constructor<?> constructor, Object... arguments)
throws InstantiationException, InvocationTargetException {
return newInstance(constructor, ConstructionDispatcher::newInstance, arguments);
}
@Override
public Object newInstance(
Constructor<?> constructor, OnConstruction onConstruction, Object... arguments)
throws InstantiationException, InvocationTargetException {
if (Modifier.isAbstract(constructor.getDeclaringClass().getModifiers())) {
throw new InstantiationException(
"Cannot instantiate abstract " + constructor.getDeclaringClass().getTypeName());
}
assureArguments(constructor, null, null, arguments, constructor.getParameterTypes());
try {
Object module =
DISPATCHER.invokeWithArguments(
getModule.bindTo(constructor.getDeclaringClass()));
String packageName = constructor.getDeclaringClass().getPackage().getName();
assureOpen(module, packageName);
MethodHandle handle =
((MethodHandles.Lookup)
DISPATCHER.invokeWithArguments(
privateLookupIn,
constructor.getDeclaringClass(),
DISPATCHER.getLookup()))
.unreflectConstructor(constructor);
AtomicBoolean thrown = new AtomicBoolean();
Object value =
onConstruction.invoke(
() -> {
try {
return DISPATCHER.invokeWithArguments(handle, arguments);
} catch (Throwable throwable) {
thrown.set(true);
return throwable;
}
});
if (thrown.get()) {
throw new InvocationTargetException((Throwable) value);
} else {
return value;
}
} catch (InvocationTargetException e) {
throw e;
} catch (Throwable t) {
throw new IllegalStateException(
"Could not construct "
+ constructor
+ " with arguments "
+ Arrays.toString(arguments),
t);
}
}
@Override
public Object invoke(Method method, Object target, Object... arguments)
throws InvocationTargetException {
assureArguments(
method,
Modifier.isStatic(method.getModifiers()) ? null : target,
method.getDeclaringClass(),
arguments,
method.getParameterTypes());
try {
Object module =
DISPATCHER.invokeWithArguments(getModule.bindTo(method.getDeclaringClass()));
String packageName = method.getDeclaringClass().getPackage().getName();
assureOpen(module, packageName);
MethodHandle handle =
((MethodHandles.Lookup)
DISPATCHER.invokeWithArguments(
privateLookupIn,
method.getDeclaringClass(),
DISPATCHER.getLookup()))
.unreflect(method);
if (!Modifier.isStatic(method.getModifiers())) {
handle = handle.bindTo(target);
}
if (handle.isVarargsCollector()) {
handle = handle.asFixedArity();
}
try {
return DISPATCHER.invokeWithArguments(handle, arguments);
} catch (Throwable t) {
throw new InvocationTargetException(t);
}
} catch (InvocationTargetException e) {
throw e;
} catch (Throwable t) {
throw new IllegalStateException(
"Could not invoke "
+ method
+ " on "
+ target
+ " with arguments "
+ Arrays.toString(arguments),
t);
}
}
@Override
public Object get(Field field, Object target) {
assureArguments(
field,
Modifier.isStatic(field.getModifiers()) ? null : target,
field.getDeclaringClass(),
new Object[0],
new Class<?>[0]);
try {
Object module =
DISPATCHER.invokeWithArguments(getModule.bindTo(field.getDeclaringClass()));
String packageName = field.getDeclaringClass().getPackage().getName();
assureOpen(module, packageName);
MethodHandle handle =
((MethodHandles.Lookup)
DISPATCHER.invokeWithArguments(
privateLookupIn,
field.getDeclaringClass(),
DISPATCHER.getLookup()))
.unreflectGetter(field);
if (!Modifier.isStatic(field.getModifiers())) {
handle = handle.bindTo(target);
}
return DISPATCHER.invokeWithArguments(handle);
} catch (Throwable t) {
throw new IllegalStateException("Could not read " + field + " on " + target, t);
}
}
@Override
public void set(Field field, Object target, Object value) throws IllegalAccessException {
assureArguments(
field,
Modifier.isStatic(field.getModifiers()) ? null : target,
field.getDeclaringClass(),
new Object[] {value},
new Class<?>[] {field.getType()});
boolean illegalAccess = false;
try {
Object module =
DISPATCHER.invokeWithArguments(getModule.bindTo(field.getDeclaringClass()));
String packageName = field.getDeclaringClass().getPackage().getName();
assureOpen(module, packageName);
// Method handles do not allow setting final fields where setAccessible(true)
// is required before unreflecting.
boolean isFinal;
if (Modifier.isFinal(field.getModifiers())) {
isFinal = true;
try {
DISPATCHER.setAccessible(field, true);
} catch (Throwable ignored) {
illegalAccess =
true; // To distinguish from propagated illegal access exception.
throw new IllegalAccessException(
"Could not make final field " + field + " accessible");
}
} else {
isFinal = false;
}
try {
MethodHandle handle =
((MethodHandles.Lookup)
DISPATCHER.invokeWithArguments(
privateLookupIn,
field.getDeclaringClass(),
DISPATCHER.getLookup()))
.unreflectSetter(field);
if (!Modifier.isStatic(field.getModifiers())) {
handle = handle.bindTo(target);
}
DISPATCHER.invokeWithArguments(handle, value);
} finally {
if (isFinal) {
DISPATCHER.setAccessible(field, false);
}
}
} catch (Throwable t) {
if (illegalAccess) {
throw (IllegalAccessException) t;
} else {
throw new IllegalStateException("Could not read " + field + " on " + target, t);
}
}
}
private void assureOpen(Object module, String packageName) throws Throwable {
// It would be more reliable to check if a module's package already is opened to
// the dispatcher module from before. Unfortunately, there is no reliable check
// for doing so since the isOpen(String, Module) method always returns true
// if the second argument is an unnamed module. Therefore, for now, we need
// to reopen packages even if they are already opened to the dispatcher module.
if (!(Boolean) DISPATCHER.invokeWithArguments(isOpen, module, packageName)) {
DISPATCHER.invokeWithArguments(
redefineModule.bindTo(INSTRUMENTATION),
module,
Collections.emptySet(),
Collections.emptyMap(),
Collections.singletonMap(
packageName, Collections.singleton(DISPATCHER.getModule())),
Collections.emptySet(),
Collections.emptyMap());
}
}
private static void assureArguments(
AccessibleObject target,
Object owner,
Class<?> type,
Object[] values,
Class<?>[] types) {
if (owner != null) {
if (!type.isAssignableFrom(owner.getClass())) {
throw new IllegalArgumentException("Cannot access " + target + " on " + owner);
}
}
if (types.length != values.length) {
throw new IllegalArgumentException(
"Incorrect number of arguments for "
+ target
+ ": expected "
+ types.length
+ " but recevied "
+ values.length);
}
for (int index = 0; index < values.length; index++) {
if (values[index] == null) {
if (types[index].isPrimitive()) {
throw new IllegalArgumentException(
"Cannot assign null to primitive type "
+ types[index].getTypeName()
+ " for "
+ index
+ " parameter of "
+ target);
}
} else {
Class<?> resolved = WRAPPERS.getOrDefault(types[index], types[index]);
if (!resolved.isAssignableFrom(values[index].getClass())) {
throw new IllegalArgumentException(
"Cannot assign value of type "
+ values[index].getClass()
+ " to "
+ resolved
+ " for "
+ index
+ " parameter of "
+ target);
}
}
}
}
public interface Dispatcher {
MethodHandles.Lookup getLookup();
Object getModule();
void setAccessible(AccessibleObject target, boolean value);
// Used to avoid invoke/invokeExact being exposed to Android where this class should
// never be loaded. Since the invocation happens from the generated code, the Android
// build pipeline does not fail.
Object invokeWithArguments(MethodHandle handle, Object... arguments) throws Throwable;
}
}