8199875: Require first parameter type of a condy bootstrap to be Lookup
authorpsandoz
Wed, 11 Apr 2018 11:11:13 -0700
changeset 49576 535498e7602f
parent 49575 3a2172d8613a
child 49577 faf02d65df7d
8199875: Require first parameter type of a condy bootstrap to be Lookup Reviewed-by: jrose
src/java.base/share/classes/java/lang/invoke/ConstantBootstraps.java
src/java.base/share/classes/java/lang/invoke/package-info.java
test/jdk/java/lang/invoke/condy/CondyBSMInvocation.java
--- a/src/java.base/share/classes/java/lang/invoke/ConstantBootstraps.java	Wed Apr 11 18:45:08 2018 +0200
+++ b/src/java.base/share/classes/java/lang/invoke/ConstantBootstraps.java	Wed Apr 11 11:11:13 2018 -0700
@@ -49,6 +49,18 @@
                                Object info,
                                // Caller information:
                                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.
         // If type is not a reference type, the JVM is expecting a boxed
         // version, and will manage unboxing on the other side.
--- a/src/java.base/share/classes/java/lang/invoke/package-info.java	Wed Apr 11 18:45:08 2018 +0200
+++ b/src/java.base/share/classes/java/lang/invoke/package-info.java	Wed Apr 11 11:11:13 2018 -0700
@@ -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.
  *
  * 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}
  * instruction.
  * <p>
- * For a dynamically-computed constant, the result of the bootstrap method is cached
- * as the resolved constant value.
+ * For a dynamically-computed constant, the first parameter of the bootstrap
+ * 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>
  * 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
@@ -171,16 +174,25 @@
  * <h2>Types of bootstrap methods</h2>
  * For a dynamically-computed call site, the bootstrap method is invoked with parameter
  * types {@code MethodHandles.Lookup}, {@code String}, {@code MethodType}, and the types
- * of any static arguments; the return type is {@code CallSite}. For a
- * dynamically-computed constant, the bootstrap method is invoked with parameter types
+ * of any static arguments; the return type is {@code CallSite}.
+ * <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
  * 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
- * 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.
- * For example, the first argument could be {@code Object}
- * instead of {@code MethodHandles.Lookup}, and the return type
+ * For a dynamically-computed constant the first parameter type of the bootstrap method handle
+ * 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}.
  * (Note that the types and number of the stacked arguments limit
  * the legal kinds of bootstrap methods to appropriately typed
@@ -227,7 +239,10 @@
  * {@code String} and {@code Integer} (or {@code int}), respectively.
  * The second-to-last example assumes that all extra arguments are of type
  * {@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>
  * Since dynamically-computed constants can be provided as static arguments to bootstrap
  * methods, there are no limitations on the types of bootstrap arguments.
--- a/test/jdk/java/lang/invoke/condy/CondyBSMInvocation.java	Wed Apr 11 18:45:08 2018 +0200
+++ b/test/jdk/java/lang/invoke/condy/CondyBSMInvocation.java	Wed Apr 11 11:11:13 2018 -0700
@@ -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.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 8186046
+ * @bug 8186046 8199875
  * @summary Test basic invocation of bootstrap methods
  * @library /lib/testlibrary/bytecode /java/lang/invoke/common
  * @build jdk.experimental.bytecode.BasicClassBuilder test.java.lang.invoke.lib.InstructionHelper
@@ -40,8 +40,10 @@
 import java.lang.invoke.MethodHandles;
 import java.lang.invoke.MethodType;
 import java.lang.invoke.WrongMethodTypeException;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.stream.IntStream;
+import java.util.stream.Stream;
 
 import static java.lang.invoke.MethodType.methodType;
 
@@ -63,85 +65,85 @@
         }
     }
 
+    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";
     }
 
-    public static Object _bsm(Object a1) {
+    public static Object shape_bsm(Object a1) {
+        return "0";
+    }
+
+    public static Object shape_bsm(Object... args) {
         return "0";
     }
 
-    // Note: when pull mode is supported for a BSM this test case
-    //       will fail and must be removed
-    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";
     }
 
     @Test
-    public void testWrongArity() throws Throwable {
-        for (int i = 0; i < 3; i++) {
-            final int n = i;
-            MethodType mt = methodType(Object.class)
-                    .appendParameterTypes(Collections.nCopies(n, Object.class));
+    public void testWrongShape() throws Throwable {
+        for (MethodHandle bsm : bsms("shape_bsm")) {
             MethodHandle mh = InstructionHelper.ldcDynamicConstant(
                     L, "name", Object.class,
-                    "_bsm", mt,
-                    S -> IntStream.range(0, n).forEach(S::add)
+                    "shape_bsm", bsm.type(),
+                    S -> {}
             );
 
             try {
                 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) {
-                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";
     }
 
-    public static Object _bsm(String a1, String a2, String a3) {
+    public static Object sig_bsm(MethodHandles.Lookup a1, String a2, String a3) {
         return "0";
     }
 
     @Test
     public void testWrongSignature() throws Throwable {
-        {
+        for (MethodHandle bsm : bsms("sig_bsm")) {
             MethodHandle mh = InstructionHelper.ldcDynamicConstant(
                     L, "name", Object.class,
-                    "_bsm", methodType(Object.class, String[].class),
+                    "sig_bsm", bsm.type(),
                     S -> {}
             );
 
             try {
                 Object r = mh.invoke();
-                Assert.fail("BootstrapMethodError expected to be thrown");
-            }
-            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()));
+                Assert.fail("BootstrapMethodError expected to be thrown for " + bsm);
+            } catch (BootstrapMethodError e) {
             }
         }
     }
@@ -193,6 +195,12 @@
         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) {
         for (int i = 0; i < as.length; i++) {
             Assert.assertEquals(as[i], i);
@@ -214,6 +222,19 @@
             Object r = mh.invoke();
             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