8199875: Require first parameter type of a condy bootstrap to be Lookup

Reviewed-by: jrose
This commit is contained in:
Paul Sandoz 2018-04-11 11:11:13 -07:00
parent 62c94b1751
commit d3760023e6
3 changed files with 101 additions and 53 deletions

View File

@ -49,6 +49,18 @@ public final class ConstantBootstraps {
Object info, Object info,
// Caller information: // Caller information:
Class<?> callerClass) { Class<?> callerClass) {
// Restrict bootstrap methods to those whose first parameter is Lookup
// The motivation here is, in the future, to possibly support BSMs
// that do not accept the meta-data of lookup/name/type, thereby
// allowing the co-opting of existing methods to be used as BSMs as
// long as the static arguments can be passed as method arguments
MethodType mt = bootstrapMethod.type();
if (mt.parameterCount() < 2 ||
!MethodHandles.Lookup.class.isAssignableFrom(mt.parameterType(0))) {
throw new BootstrapMethodError(
"Invalid bootstrap method declared for resolving a dynamic constant: " + bootstrapMethod);
}
// BSMI.invoke handles all type checking and exception translation. // BSMI.invoke handles all type checking and exception translation.
// If type is not a reference type, the JVM is expecting a boxed // If type is not a reference type, the JVM is expecting a boxed
// version, and will manage unboxing on the other side. // version, and will manage unboxing on the other side.

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -122,8 +122,11 @@
* On success the call site then becomes permanently linked to the {@code invokedynamic} * On success the call site then becomes permanently linked to the {@code invokedynamic}
* instruction. * instruction.
* <p> * <p>
* For a dynamically-computed constant, the result of the bootstrap method is cached * For a dynamically-computed constant, the first parameter of the bootstrap
* as the resolved constant value. * method must be assignable to {@code MethodHandles.Lookup}. If this condition
* is not met, a {@code BootstrapMethodError} is thrown.
* On success the result of the bootstrap method is cached as the resolved
* constant value.
* <p> * <p>
* If an exception, {@code E} say, occurs during execution of the bootstrap method, then * If an exception, {@code E} say, occurs during execution of the bootstrap method, then
* resolution fails and terminates abnormally. {@code E} is rethrown if the type of * resolution fails and terminates abnormally. {@code E} is rethrown if the type of
@ -171,16 +174,25 @@
* <h2>Types of bootstrap methods</h2> * <h2>Types of bootstrap methods</h2>
* For a dynamically-computed call site, the bootstrap method is invoked with parameter * For a dynamically-computed call site, the bootstrap method is invoked with parameter
* types {@code MethodHandles.Lookup}, {@code String}, {@code MethodType}, and the types * types {@code MethodHandles.Lookup}, {@code String}, {@code MethodType}, and the types
* of any static arguments; the return type is {@code CallSite}. For a * of any static arguments; the return type is {@code CallSite}.
* dynamically-computed constant, the bootstrap method is invoked with parameter types * <p>
* For a dynamically-computed constant, the bootstrap method is invoked with parameter types
* {@code MethodHandles.Lookup}, {@code String}, {@code Class}, and the types of any * {@code MethodHandles.Lookup}, {@code String}, {@code Class}, and the types of any
* static arguments; the return type is the type represented by the {@code Class}. * static arguments; the return type is the type represented by the {@code Class}.
* * <p>
* Because {@link java.lang.invoke.MethodHandle#invoke MethodHandle.invoke} allows for * Because {@link java.lang.invoke.MethodHandle#invoke MethodHandle.invoke} allows for
* adaptations between the invoked method type and the method handle's method type, * adaptations between the invoked method type and the bootstrap method handle's method type,
* there is flexibility in the declaration of the bootstrap method. * there is flexibility in the declaration of the bootstrap method.
* For example, the first argument could be {@code Object} * For a dynamically-computed constant the first parameter type of the bootstrap method handle
* instead of {@code MethodHandles.Lookup}, and the return type * must be assignable to {@code MethodHandles.Lookup}, other than that constraint the same degree
* of flexibility applies to bootstrap methods of dynamically-computed call sites and
* dynamically-computed constants.
* Note: this constraint allows for the future possibility where the bootstrap method is
* invoked with just the parameter types of static arguments, thereby supporting a wider
* range of methods compatible with the static arguments (such as methods that don't declare
* or require the lookup, name, and type meta-data parameters).
* <p> For example, for dynamically-computed call site, a the first argument
* could be {@code Object} instead of {@code MethodHandles.Lookup}, and the return type
* could also be {@code Object} instead of {@code CallSite}. * could also be {@code Object} instead of {@code CallSite}.
* (Note that the types and number of the stacked arguments limit * (Note that the types and number of the stacked arguments limit
* the legal kinds of bootstrap methods to appropriately typed * the legal kinds of bootstrap methods to appropriately typed
@ -227,7 +239,10 @@
* {@code String} and {@code Integer} (or {@code int}), respectively. * {@code String} and {@code Integer} (or {@code int}), respectively.
* The second-to-last example assumes that all extra arguments are of type * The second-to-last example assumes that all extra arguments are of type
* {@code String}. * {@code String}.
* The other examples work with all types of extra arguments. * The other examples work with all types of extra arguments. Note that all
* the examples except the second and third also work with dynamically-computed
* constants if the return type is changed to be compatible with the
* constant's declared type (such as {@code Object}, which is always compatible).
* <p> * <p>
* Since dynamically-computed constants can be provided as static arguments to bootstrap * Since dynamically-computed constants can be provided as static arguments to bootstrap
* methods, there are no limitations on the types of bootstrap arguments. * methods, there are no limitations on the types of bootstrap arguments.

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -23,7 +23,7 @@
/* /*
* @test * @test
* @bug 8186046 * @bug 8186046 8199875
* @summary Test basic invocation of bootstrap methods * @summary Test basic invocation of bootstrap methods
* @library /lib/testlibrary/bytecode /java/lang/invoke/common * @library /lib/testlibrary/bytecode /java/lang/invoke/common
* @build jdk.experimental.bytecode.BasicClassBuilder test.java.lang.invoke.lib.InstructionHelper * @build jdk.experimental.bytecode.BasicClassBuilder test.java.lang.invoke.lib.InstructionHelper
@ -40,8 +40,10 @@ import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType; import java.lang.invoke.MethodType;
import java.lang.invoke.WrongMethodTypeException; import java.lang.invoke.WrongMethodTypeException;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.stream.IntStream; import java.util.stream.IntStream;
import java.util.stream.Stream;
import static java.lang.invoke.MethodType.methodType; import static java.lang.invoke.MethodType.methodType;
@ -63,85 +65,85 @@ public class CondyBSMInvocation {
} }
} }
static MethodHandle[] bsms(String bsmName) {
return Stream.of(CondyBSMInvocation.class.getDeclaredMethods()).
filter(m -> m.getName().equals(bsmName)).
map(m -> {
try {
return MethodHandles.lookup().unreflect(m);
} catch (IllegalAccessException e) {
throw new RuntimeException();
}
}).toArray(MethodHandle[]::new);
}
public static Object _bsm() { public static Object shape_bsm() {
return "0"; return "0";
} }
public static Object _bsm(Object a1) { public static Object shape_bsm(Object a1) {
return "0"; return "0";
} }
// Note: when pull mode is supported for a BSM this test case public static Object shape_bsm(Object... args) {
// will fail and must be removed return "0";
public static Object _bsm(Object a1, Object a2) { }
public static Object shape_bsm(Object a1, Object a2) {
return "0";
}
public static Object shape_bsm(Object a1, Object... args) {
return "0";
}
public static Object shape_bsm(Object a1, Object a2, Object a3) {
return "0";
}
public static Object shape_bsm(MethodHandles.Lookup a1) {
return "0"; return "0";
} }
@Test @Test
public void testWrongArity() throws Throwable { public void testWrongShape() throws Throwable {
for (int i = 0; i < 3; i++) { for (MethodHandle bsm : bsms("shape_bsm")) {
final int n = i;
MethodType mt = methodType(Object.class)
.appendParameterTypes(Collections.nCopies(n, Object.class));
MethodHandle mh = InstructionHelper.ldcDynamicConstant( MethodHandle mh = InstructionHelper.ldcDynamicConstant(
L, "name", Object.class, L, "name", Object.class,
"_bsm", mt, "shape_bsm", bsm.type(),
S -> IntStream.range(0, n).forEach(S::add) S -> {}
); );
try { try {
Object r = mh.invoke(); Object r = mh.invoke();
Assert.fail("BootstrapMethodError expected to be thrown for arrity " + n); Assert.fail("BootstrapMethodError expected to be thrown for " + bsm);
} catch (BootstrapMethodError e) { } catch (BootstrapMethodError e) {
Throwable t = e.getCause();
Assert.assertTrue(WrongMethodTypeException.class.isAssignableFrom(t.getClass()));
} }
} }
} }
public static Object _bsm(String[] ss) { public static Object sig_bsm(MethodHandles.Lookup a1, String[] a2) {
return "0"; return "0";
} }
public static Object _bsm(String a1, String a2, String a3) { public static Object sig_bsm(MethodHandles.Lookup a1, String a2, String a3) {
return "0"; return "0";
} }
@Test @Test
public void testWrongSignature() throws Throwable { public void testWrongSignature() throws Throwable {
{ for (MethodHandle bsm : bsms("sig_bsm")) {
MethodHandle mh = InstructionHelper.ldcDynamicConstant( MethodHandle mh = InstructionHelper.ldcDynamicConstant(
L, "name", Object.class, L, "name", Object.class,
"_bsm", methodType(Object.class, String[].class), "sig_bsm", bsm.type(),
S -> {} S -> {}
); );
try { try {
Object r = mh.invoke(); Object r = mh.invoke();
Assert.fail("BootstrapMethodError expected to be thrown"); Assert.fail("BootstrapMethodError expected to be thrown for " + bsm);
} } catch (BootstrapMethodError e) {
catch (BootstrapMethodError e) {
Throwable t = e.getCause();
Assert.assertTrue(WrongMethodTypeException.class.isAssignableFrom(t.getClass()));
}
}
{
MethodHandle mh = InstructionHelper.ldcDynamicConstant(
L, "name", Object.class,
"_bsm", methodType(Object.class, String.class, String.class, String.class),
S -> {}
);
try {
Object r = mh.invoke();
Assert.fail("BootstrapMethodError expected to be thrown");
}
catch (BootstrapMethodError e) {
Throwable t = e.getCause();
Assert.assertTrue(ClassCastException.class.isAssignableFrom(t.getClass()));
} }
} }
} }
@ -193,6 +195,12 @@ public class CondyBSMInvocation {
return "7"; return "7";
} }
public static Object bsm(MethodHandles.Lookup l, Object... args) {
Object[] staticArgs = Arrays.copyOfRange(args, 2, args.length);
assertAll(staticArgs);
return Integer.toString(staticArgs.length);
}
static void assertAll(Object... as) { static void assertAll(Object... as) {
for (int i = 0; i < as.length; i++) { for (int i = 0; i < as.length; i++) {
Assert.assertEquals(as[i], i); Assert.assertEquals(as[i], i);
@ -214,6 +222,19 @@ public class CondyBSMInvocation {
Object r = mh.invoke(); Object r = mh.invoke();
Assert.assertEquals(r, Integer.toString(n)); Assert.assertEquals(r, Integer.toString(n));
} }
{
MethodType mt = methodType(Object.class, MethodHandles.Lookup.class, Object[].class);
MethodHandle mh = InstructionHelper.ldcDynamicConstant(
L, "name", Object.class,
"bsm", mt,
S -> IntStream.range(0, 9).forEach(S::add)
);
Object r = mh.invoke();
Assert.assertEquals(r, Integer.toString(9));
}
} }
@Test @Test