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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
|
/*
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.lang.invoke;
import sun.invoke.util.Wrapper;
import java.lang.invoke.AbstractConstantGroup.BSCIWithCache;
import java.util.Arrays;
import static java.lang.invoke.BootstrapCallInfo.makeBootstrapCallInfo;
import static java.lang.invoke.ConstantGroup.makeConstantGroup;
import static java.lang.invoke.MethodHandleNatives.*;
import static java.lang.invoke.MethodHandleStatics.TRACE_METHOD_LINKAGE;
import static java.lang.invoke.MethodHandles.Lookup;
import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP;
final class BootstrapMethodInvoker {
/**
* Factored code for invoking a bootstrap method for invokedynamic
* or a dynamic constant.
* @param resultType the expected return type (either CallSite or a constant type)
* @param bootstrapMethod the BSM to call
* @param name the method name or constant name
* @param type the method type or constant type
* @param info information passed up from the JVM, to derive static arguments
* @param callerClass the class containing the resolved method call or constant load
* @param <T> the expected return type
* @return the expected value, either a CallSite or a constant value
*/
static <T> T invoke(Class<T> resultType,
MethodHandle bootstrapMethod,
// Callee information:
String name, Object type,
// Extra arguments for BSM, if any:
Object info,
// Caller information:
Class<?> callerClass) {
MethodHandles.Lookup caller = IMPL_LOOKUP.in(callerClass);
Object result;
boolean pullMode = isPullModeBSM(bootstrapMethod); // default value is false
boolean vmIsPushing = !staticArgumentsPulled(info); // default value is true
MethodHandle pullModeBSM;
// match the VM with the BSM
if (vmIsPushing) {
// VM is pushing arguments at us
pullModeBSM = null;
if (pullMode) {
bootstrapMethod = pushMePullYou(bootstrapMethod, true);
}
} else {
// VM wants us to pull args from it
pullModeBSM = pullMode ? bootstrapMethod :
pushMePullYou(bootstrapMethod, false);
bootstrapMethod = null;
}
try {
// As an optimization we special case various known BSMs,
// such as LambdaMetafactory::metafactory and
// StringConcatFactory::makeConcatWithConstants.
//
// By providing static type information or even invoking
// exactly, we avoid emitting code to perform runtime
// checking.
if (info == null) {
// VM is allowed to pass up a null meaning no BSM args
if (type instanceof Class<?> c) {
result = bootstrapMethod.invoke(caller, name, c);
} else {
result = bootstrapMethod.invoke(caller, name, (MethodType)type);
}
}
else if (!info.getClass().isArray()) {
// VM is allowed to pass up a single BSM arg directly
// Call to StringConcatFactory::makeConcatWithConstants
// with empty constant arguments?
if (isStringConcatFactoryBSM(bootstrapMethod.type())) {
result = (CallSite)bootstrapMethod
.invokeExact(caller, name, (MethodType)type,
(String)info, new Object[0]);
} else {
info = maybeReBox(info);
if (type instanceof Class<?> c) {
result = bootstrapMethod.invoke(caller, name, c, info);
} else {
result = bootstrapMethod.invoke(caller, name, (MethodType)type, info);
}
}
}
else if (info.getClass() == int[].class) {
// VM is allowed to pass up a pair {argc, index}
// referring to 'argc' BSM args at some place 'index'
// in the guts of the VM (associated with callerClass).
// The format of this index pair is private to the
// handshake between the VM and this class only.
// This supports "pulling" of arguments.
// The VM is allowed to do this for any reason.
// The code in this method makes up for any mismatches.
BootstrapCallInfo<Object> bsci
= new VM_BSCI<>(bootstrapMethod, name, type, caller, (int[])info);
// Pull-mode API is (Lookup, BootstrapCallInfo) -> Object
result = pullModeBSM.invoke(caller, bsci);
}
else {
// VM is allowed to pass up a full array of resolved BSM args
Object[] argv = (Object[]) info;
MethodType bsmType = bootstrapMethod.type();
if (isLambdaMetafactoryIndyBSM(bsmType) && argv.length == 3) {
result = (CallSite)bootstrapMethod
.invokeExact(caller, name, (MethodType)type, (MethodType)argv[0],
(MethodHandle)argv[1], (MethodType)argv[2]);
} else if (isLambdaMetafactoryCondyBSM(bsmType) && argv.length == 3) {
result = bootstrapMethod
.invokeExact(caller, name, (Class<?>)type, (MethodType)argv[0],
(MethodHandle)argv[1], (MethodType)argv[2]);
} else if (isStringConcatFactoryBSM(bsmType) && argv.length >= 1) {
String recipe = (String)argv[0];
Object[] shiftedArgs = Arrays.copyOfRange(argv, 1, argv.length);
maybeReBoxElements(shiftedArgs);
result = (CallSite)bootstrapMethod.invokeExact(caller, name, (MethodType)type, recipe, shiftedArgs);
} else if (isLambdaMetafactoryAltMetafactoryBSM(bsmType)) {
maybeReBoxElements(argv);
result = (CallSite)bootstrapMethod.invokeExact(caller, name, (MethodType)type, argv);
} else {
maybeReBoxElements(argv);
if (type instanceof Class<?> c) {
result = switch (argv.length) {
case 0 -> bootstrapMethod.invoke(caller, name, c);
case 1 -> bootstrapMethod.invoke(caller, name, c, argv[0]);
case 2 -> bootstrapMethod.invoke(caller, name, c, argv[0], argv[1]);
case 3 -> bootstrapMethod.invoke(caller, name, c, argv[0], argv[1], argv[2]);
case 4 -> bootstrapMethod.invoke(caller, name, c, argv[0], argv[1], argv[2], argv[3]);
case 5 -> bootstrapMethod.invoke(caller, name, c, argv[0], argv[1], argv[2], argv[3], argv[4]);
case 6 -> bootstrapMethod.invoke(caller, name, c, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]);
default -> invokeWithManyArguments(bootstrapMethod, caller, name, type, argv);
};
} else {
MethodType mt = (MethodType) type;
result = switch (argv.length) {
case 0 -> bootstrapMethod.invoke(caller, name, mt);
case 1 -> bootstrapMethod.invoke(caller, name, mt, argv[0]);
case 2 -> bootstrapMethod.invoke(caller, name, mt, argv[0], argv[1]);
case 3 -> bootstrapMethod.invoke(caller, name, mt, argv[0], argv[1], argv[2]);
case 4 -> bootstrapMethod.invoke(caller, name, mt, argv[0], argv[1], argv[2], argv[3]);
case 5 -> bootstrapMethod.invoke(caller, name, mt, argv[0], argv[1], argv[2], argv[3], argv[4]);
case 6 -> bootstrapMethod.invoke(caller, name, mt, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]);
default -> invokeWithManyArguments(bootstrapMethod, caller, name, type, argv);
};
}
}
}
return widenAndCast(result, resultType);
}
catch (Error e) {
// Pass through an Error, including BootstrapMethodError, any other
// form of linkage error, such as IllegalAccessError if the bootstrap
// method is inaccessible, or say ThreadDeath/OutOfMemoryError
// See the "Linking Exceptions" section for the invokedynamic
// instruction in JVMS 6.5.
throw e;
}
catch (Throwable ex) {
// Wrap anything else in BootstrapMethodError
throw new BootstrapMethodError("bootstrap method initialization exception", ex);
}
}
/**
* If resultType is a reference type, do Class::cast on the result through
* an identity function of that type, as-type converted to return
* the corresponding reference wrapper type for resultType.
* Works like {@code MethodHandles.identity(resultType).invoke((Object)result)}.
*
* This utility function enforces type correctness of bootstrap method results.
* It is also used to enforce type correctness in other dependently-typed
* methods, such as classData.
*/
static <T> T widenAndCast(Object result, Class<T> resultType) throws Throwable {
if (!resultType.isPrimitive()) {
return resultType.cast(result);
}
Class<T> wrapperType = Wrapper.asWrapperType(resultType);
if (wrapperType.isInstance(result)) {
@SuppressWarnings("unchecked")
T wrapper = (T) result;
return wrapper;
}
// Non-reference conversions are more than just plain casts.
// By pushing the value through a funnel of the form (T x)->x,
// the boxed result can be widened as needed. See MH::asType.
// Note that this might widen byte into int, float into double, etc
MethodHandle funnel = MethodHandles.identity(resultType);
result = funnel.invoke(result);
// Now it is the wrapper type for resultType.
return wrapperType.cast(result);
}
private static Object invokeWithManyArguments(MethodHandle bootstrapMethod, Lookup caller,
String name, Object type, Object[] argv) throws Throwable {
final int NON_SPREAD_ARG_COUNT = 3; // (caller, name, type)
final int MAX_SAFE_SIZE = MethodType.MAX_MH_ARITY / 2 - NON_SPREAD_ARG_COUNT;
if (argv.length >= MAX_SAFE_SIZE) {
// to be on the safe side, use invokeWithArguments which handles jumbo lists
Object[] newargv = new Object[NON_SPREAD_ARG_COUNT + argv.length];
newargv[0] = caller;
newargv[1] = name;
newargv[2] = type;
System.arraycopy(argv, 0, newargv, NON_SPREAD_ARG_COUNT, argv.length);
return bootstrapMethod.invokeWithArguments(newargv);
} else {
MethodType invocationType = MethodType.genericMethodType(NON_SPREAD_ARG_COUNT + argv.length);
MethodHandle typedBSM = bootstrapMethod.asType(invocationType);
MethodHandle spreader = invocationType.invokers().spreadInvoker(NON_SPREAD_ARG_COUNT);
return spreader.invokeExact(typedBSM, (Object) caller, (Object) name, type, argv);
}
}
private static final MethodType LMF_INDY_MT = MethodType.methodType(CallSite.class,
Lookup.class, String.class, MethodType.class, MethodType.class, MethodHandle.class, MethodType.class);
private static final MethodType LMF_ALT_MT = MethodType.methodType(CallSite.class,
Lookup.class, String.class, MethodType.class, Object[].class);
private static final MethodType LMF_CONDY_MT = MethodType.methodType(Object.class,
Lookup.class, String.class, Class.class, MethodType.class, MethodHandle.class, MethodType.class);
private static final MethodType SCF_MT = MethodType.methodType(CallSite.class,
Lookup.class, String.class, MethodType.class, String.class, Object[].class);
/**
* @return true iff the BSM method type exactly matches
* {@see java.lang.invoke.StringConcatFactory#makeConcatWithConstants(MethodHandles.Lookup,
* String,MethodType,String,Object...))}
*/
private static boolean isStringConcatFactoryBSM(MethodType bsmType) {
return bsmType == SCF_MT;
}
/**
* @return true iff the BSM method type exactly matches
* {@see java.lang.invoke.LambdaMetafactory#metafactory(
* MethodHandles.Lookup,String,Class,MethodType,MethodHandle,MethodType)}
*/
private static boolean isLambdaMetafactoryCondyBSM(MethodType bsmType) {
return bsmType == LMF_CONDY_MT;
}
/**
* @return true iff the BSM method type exactly matches
* {@see java.lang.invoke.LambdaMetafactory#metafactory(
* MethodHandles.Lookup,String,MethodType,MethodType,MethodHandle,MethodType)}
*/
private static boolean isLambdaMetafactoryIndyBSM(MethodType bsmType) {
return bsmType == LMF_INDY_MT;
}
/**
* @return true iff the BSM method type exactly matches
* {@see java.lang.invoke.LambdaMetafactory#altMetafactory(
* MethodHandles.Lookup,String,MethodType,Object[])}
*/
private static boolean isLambdaMetafactoryAltMetafactoryBSM(MethodType bsmType) {
return bsmType == LMF_ALT_MT;
}
/** The JVM produces java.lang.Integer values to box
* CONSTANT_Integer boxes but does not intern them.
* Let's intern them. This is slightly wrong for
* a {@code CONSTANT_Dynamic} which produces an
* un-interned integer (e.g., {@code new Integer(0)}).
*/
private static Object maybeReBox(Object x) {
if (x instanceof Integer) {
int xi = (int) x;
if (xi == (byte) xi)
x = xi; // must rebox; see JLS 5.1.7
}
return x;
}
private static void maybeReBoxElements(Object[] xa) {
for (int i = 0; i < xa.length; i++) {
xa[i] = maybeReBox(xa[i]);
}
}
/** Canonical VM-aware implementation of BootstrapCallInfo.
* Knows how to dig into the JVM for lazily resolved (pull-mode) constants.
*/
private static final class VM_BSCI<T> extends BSCIWithCache<T> {
private final int[] indexInfo;
private final Class<?> caller; // for index resolution only
VM_BSCI(MethodHandle bsm, String name, T type,
Lookup lookup, int[] indexInfo) {
super(bsm, name, type, indexInfo[0]);
if (!lookup.hasFullPrivilegeAccess()) //D.I.D.
throw new AssertionError("bad Lookup object");
this.caller = lookup.lookupClass();
this.indexInfo = indexInfo;
// scoop up all the easy stuff right away:
prefetchIntoCache(0, size());
}
@Override Object fillCache(int i) {
Object[] buf = { null };
copyConstants(i, i+1, buf, 0);
Object res = wrapNull(buf[0]);
cache[i] = res;
int next = i + 1;
if (next < cache.length && cache[next] == null)
maybePrefetchIntoCache(next, false); // try to prefetch
return res;
}
@Override public int copyConstants(int start, int end,
Object[] buf, int pos) {
int i = start, bufi = pos;
while (i < end) {
Object x = cache[i];
if (x == null) break;
buf[bufi++] = unwrapNull(x);
i++;
}
// give up at first null and grab the rest in one big block
if (i >= end) return i;
Object[] temp = new Object[end - i];
if (TRACE_METHOD_LINKAGE) {
System.out.println("resolving more BSM arguments: " +
Arrays.asList(caller.getSimpleName(), Arrays.toString(indexInfo), i, end));
}
copyOutBootstrapArguments(caller, indexInfo,
i, end, temp, 0,
true, null);
for (Object x : temp) {
x = maybeReBox(x);
buf[bufi++] = x;
cache[i++] = wrapNull(x);
}
if (end < cache.length && cache[end] == null)
maybePrefetchIntoCache(end, true); // try to prefetch
return i;
}
private static final int MIN_PF = 4;
private void maybePrefetchIntoCache(int i, boolean bulk) {
int len = cache.length;
assert(0 <= i && i <= len);
int pfLimit = i;
if (bulk) pfLimit += i; // exponential prefetch expansion
// try to prefetch at least MIN_PF elements
if (pfLimit < i + MIN_PF) pfLimit = i + MIN_PF;
if (pfLimit > len || pfLimit < 0) pfLimit = len;
// stop prefetching where cache is more full than empty
int empty = 0, nonEmpty = 0, lastEmpty = i;
for (int j = i; j < pfLimit; j++) {
if (cache[j] == null) {
empty++;
lastEmpty = j;
} else {
nonEmpty++;
if (nonEmpty > empty) {
pfLimit = lastEmpty + 1;
break;
}
if (pfLimit < len) pfLimit++;
}
}
if (bulk && empty < MIN_PF && pfLimit < len)
return; // not worth the effort
prefetchIntoCache(i, pfLimit);
}
private void prefetchIntoCache(int i, int pfLimit) {
if (pfLimit <= i) return; // corner case
Object[] temp = new Object[pfLimit - i];
if (TRACE_METHOD_LINKAGE) {
System.out.println("prefetching BSM arguments: " +
Arrays.asList(caller.getSimpleName(), Arrays.toString(indexInfo), i, pfLimit));
}
copyOutBootstrapArguments(caller, indexInfo,
i, pfLimit, temp, 0,
false, NOT_PRESENT);
for (Object x : temp) {
if (x != NOT_PRESENT && cache[i] == null) {
cache[i] = wrapNull(maybeReBox(x));
}
i++;
}
}
}
/*non-public*/
static final class PushAdapter {
// skeleton for push-mode BSM which wraps a pull-mode BSM:
static Object pushToBootstrapMethod(MethodHandle pullModeBSM,
MethodHandles.Lookup lookup, String name, Object type,
Object... arguments) throws Throwable {
ConstantGroup cons = makeConstantGroup(Arrays.asList(arguments));
BootstrapCallInfo<?> bsci = makeBootstrapCallInfo(pullModeBSM, name, type, cons);
if (TRACE_METHOD_LINKAGE)
System.out.println("pull-mode BSM gets pushed arguments from fake BSCI");
return pullModeBSM.invoke(lookup, bsci);
}
static final MethodHandle MH_pushToBootstrapMethod;
static {
final Class<?> THIS_CLASS = PushAdapter.class;
try {
MH_pushToBootstrapMethod = IMPL_LOOKUP
.findStatic(THIS_CLASS, "pushToBootstrapMethod",
MethodType.methodType(Object.class, MethodHandle.class,
Lookup.class, String.class, Object.class, Object[].class));
} catch (Throwable ex) {
throw new InternalError(ex);
}
}
}
/*non-public*/
static final class PullAdapter {
// skeleton for pull-mode BSM which wraps a push-mode BSM:
static Object pullFromBootstrapMethod(MethodHandle pushModeBSM,
MethodHandles.Lookup lookup,
BootstrapCallInfo<?> bsci)
throws Throwable {
int argc = bsci.size();
switch (argc) {
case 0:
return pushModeBSM.invoke(lookup, bsci.invocationName(), bsci.invocationType());
case 1:
return pushModeBSM.invoke(lookup, bsci.invocationName(), bsci.invocationType(),
bsci.get(0));
case 2:
return pushModeBSM.invoke(lookup, bsci.invocationName(), bsci.invocationType(),
bsci.get(0), bsci.get(1));
case 3:
return pushModeBSM.invoke(lookup, bsci.invocationName(), bsci.invocationType(),
bsci.get(0), bsci.get(1), bsci.get(2));
case 4:
return pushModeBSM.invoke(lookup, bsci.invocationName(), bsci.invocationType(),
bsci.get(0), bsci.get(1), bsci.get(2), bsci.get(3));
case 5:
return pushModeBSM.invoke(lookup, bsci.invocationName(), bsci.invocationType(),
bsci.get(0), bsci.get(1), bsci.get(2), bsci.get(3), bsci.get(4));
case 6:
return pushModeBSM.invoke(lookup, bsci.invocationName(), bsci.invocationType(),
bsci.get(0), bsci.get(1), bsci.get(2), bsci.get(3), bsci.get(4), bsci.get(5));
default:
final int NON_SPREAD_ARG_COUNT = 3; // (lookup, name, type)
final int MAX_SAFE_SIZE = MethodType.MAX_MH_ARITY / 2 - NON_SPREAD_ARG_COUNT;
if (argc >= MAX_SAFE_SIZE) {
// to be on the safe side, use invokeWithArguments which handles jumbo lists
Object[] newargv = new Object[NON_SPREAD_ARG_COUNT + argc];
newargv[0] = lookup;
newargv[1] = bsci.invocationName();
newargv[2] = bsci.invocationType();
bsci.copyConstants(0, argc, newargv, NON_SPREAD_ARG_COUNT);
return pushModeBSM.invokeWithArguments(newargv);
}
MethodType invocationType = MethodType.genericMethodType(NON_SPREAD_ARG_COUNT + argc);
MethodHandle typedBSM = pushModeBSM.asType(invocationType);
MethodHandle spreader = invocationType.invokers().spreadInvoker(NON_SPREAD_ARG_COUNT);
Object[] argv = new Object[argc];
bsci.copyConstants(0, argc, argv, 0);
return spreader.invokeExact(typedBSM, (Object) lookup, (Object) bsci.invocationName(), bsci.invocationType(), argv);
}
}
static final MethodHandle MH_pullFromBootstrapMethod;
static {
final Class<?> THIS_CLASS = PullAdapter.class;
try {
MH_pullFromBootstrapMethod = IMPL_LOOKUP
.findStatic(THIS_CLASS, "pullFromBootstrapMethod",
MethodType.methodType(Object.class, MethodHandle.class,
Lookup.class, BootstrapCallInfo.class));
} catch (Throwable ex) {
throw new InternalError(ex);
}
}
}
/** Given a push-mode BSM (taking one argument) convert it to a
* pull-mode BSM (taking N pre-resolved arguments).
* This method is used when, in fact, the JVM is passing up
* pre-resolved arguments, but the BSM is expecting lazy stuff.
* Or, when goToPushMode is true, do the reverse transform.
* (The two transforms are exactly inverse.)
*/
static MethodHandle pushMePullYou(MethodHandle bsm, boolean goToPushMode) {
if (TRACE_METHOD_LINKAGE) {
System.out.println("converting BSM of type " + bsm.type() + " to "
+ (goToPushMode ? "push mode" : "pull mode"));
}
assert(isPullModeBSM(bsm) == goToPushMode); // there must be a change
if (goToPushMode) {
return PushAdapter.MH_pushToBootstrapMethod.bindTo(bsm).withVarargs(true);
} else {
return PullAdapter.MH_pullFromBootstrapMethod.bindTo(bsm).withVarargs(false);
}
}
}
|