--- old/src/java.base/share/classes/java/lang/invoke/MethodHandle.java 2017-11-07 11:59:16.000000000 -0800 +++ new/src/java.base/share/classes/java/lang/invoke/MethodHandle.java 2017-11-07 11:59:15.000000000 -0800 @@ -886,9 +886,13 @@ * to the target method handle. * (The array may also be null when zero elements are required.) *

- * If, when the adapter is called, the supplied array argument does - * not have the correct number of elements, the adapter will throw - * an {@link IllegalArgumentException} instead of invoking the target. + * When the adapter is called, the length of the supplied {@code array} + * argument is queried as if by {@code array.length} or {@code arrayLength} + * bytecode. If the adapter accepts zero-length trailing array argument, + * the supplied {@code array} argument can either be a zero-length array or + * {@code null}; otherwise, the adapter will throw a {@code NullPointerException} + * if {@code array} is {@code null} and throw an {@link IllegalArgumentException} + * if {@code array} does not have the correct number of elements. *

* Here are some simple examples of array-spreading method handles: *

{@code
--- old/src/java.base/share/classes/java/lang/invoke/MethodHandleImpl.java	2017-11-07 11:59:18.000000000 -0800
+++ new/src/java.base/share/classes/java/lang/invoke/MethodHandleImpl.java	2017-11-07 11:59:17.000000000 -0800
@@ -664,6 +664,7 @@
     static void checkSpreadArgument(Object av, int n) {
         if (av == null) {
             if (n == 0)  return;
+            throw new NullPointerException("null array reference");
         } else if (av instanceof Object[]) {
             int len = ((Object[])av).length;
             if (len == n)  return;
--- old/src/java.base/share/classes/java/lang/invoke/MethodHandles.java	2017-11-07 11:59:20.000000000 -0800
+++ new/src/java.base/share/classes/java/lang/invoke/MethodHandles.java	2017-11-07 11:59:20.000000000 -0800
@@ -2514,14 +2514,20 @@
     }
 
     /**
-     * Produces a method handle constructing arrays of a desired type.
+     * Produces a method handle constructing arrays of a desired type,
+     * as if by the {@code anewarray} bytecode.
      * The return type of the method handle will be the array type.
      * The type of its sole argument will be {@code int}, which specifies the size of the array.
+     *
+     * 

If the returned method handle is invoked with a negative + * array size, {@code NegativeArraySizeException} will be thrown. + * * @param arrayClass an array type * @return a method handle which can create arrays of the given type * @throws NullPointerException if the argument is {@code null} * @throws IllegalArgumentException if {@code arrayClass} is not an array type * @see java.lang.reflect.Array#newInstance(Class, int) + * @jvms 6.5 {@code anewarray} Instruction * @since 9 */ public static @@ -2535,13 +2541,19 @@ } /** - * Produces a method handle returning the length of an array. + * Produces a method handle returning the length of an array, + * as if by the {@code arrayLength} bytecode. * The type of the method handle will have {@code int} as return type, * and its sole argument will be the array type. + * + *

If the returned method handle is invoked with a {@code null} + * array reference, {@code NullPointerPointer} will be thrown. + * * @param arrayClass an array type * @return a method handle which can retrieve the length of an array of the given array type * @throws NullPointerException if the argument is {@code null} * @throws IllegalArgumentException if arrayClass is not an array type + * @jvms 6.5 {@code arrayLength} Instruction * @since 9 */ public static @@ -2550,14 +2562,24 @@ } /** - * Produces a method handle giving read access to elements of an array. + * Produces a method handle giving read access to elements of an array, + * as if by the {@code aaload} bytecode. * The type of the method handle will have a return type of the array's * element type. Its first argument will be the array type, * and the second will be {@code int}. + * + *

When the returned method handle is invoked, + * the array reference and array index are checked. + * {@code NullPointerPointer} will be thrown if the array reference + * is {@code null} and {@code ArrayIndexOutOfBoundsException} will be + * thrown if the index is negative or if it is greater than or equal to + * the length of the array. + * * @param arrayClass an array type * @return a method handle which can load values from the given array type * @throws NullPointerException if the argument is null * @throws IllegalArgumentException if arrayClass is not an array type + * @jvms 6.5 {@code aaload} Instruction */ public static MethodHandle arrayElementGetter(Class arrayClass) throws IllegalArgumentException { @@ -2565,14 +2587,24 @@ } /** - * Produces a method handle giving write access to elements of an array. + * Produces a method handle giving write access to elements of an array, + * as if by the {@code astore} bytecode. * The type of the method handle will have a void return type. * Its last argument will be the array's element type. * The first and second arguments will be the array type and int. + * + *

When the returned method handle is invoked, + * the array reference and array index are checked. + * {@code NullPointerPointer} will be thrown if the array reference + * is {@code null} and {@code ArrayIndexOutOfBoundsException} will be + * thrown if the index is negative or if it is greater than or equal to + * the length of the array. + * * @param arrayClass the class of an array * @return a method handle which can store values into the array type * @throws NullPointerException if the argument is null * @throws IllegalArgumentException if arrayClass is not an array type + * @jvms 6.5 {@code aastore} Instruction */ public static MethodHandle arrayElementSetter(Class arrayClass) throws IllegalArgumentException { @@ -2603,6 +2635,14 @@ * and atomic update access modes compare values using their bitwise * representation (see {@link Float#floatToRawIntBits} and * {@link Double#doubleToRawLongBits}, respectively). + * + *

When the returned {@code VarHandle} is invoked, + * the array reference and array index are checked. + * {@code NullPointerPointer} will be thrown if the array reference + * is {@code null} and {@code ArrayIndexOutOfBoundsException} will be + * thrown if the index is negative or if it is greater than or equal to + * the length of the array. + * * @apiNote * Bitwise comparison of {@code float} values or {@code double} values, * as performed by the numeric and atomic update access modes, differ --- old/test/jdk/java/lang/invoke/ArrayConstructorTest.java 2017-11-07 11:59:23.000000000 -0800 +++ new/test/jdk/java/lang/invoke/ArrayConstructorTest.java 2017-11-07 11:59:22.000000000 -0800 @@ -88,4 +88,11 @@ assertEquals(17, a.length); } + @Test(expectedExceptions = {NegativeArraySizeException.class}) + public static void testArrayConstructorNegativeIndex() throws Throwable { + MethodHandle h = MethodHandles.arrayConstructor(String[].class); + assertEquals(methodType(String[].class, int.class), h.type()); + h.invoke(-1); // throws exception + } + } --- old/test/jdk/java/lang/invoke/ArrayLengthTest.java 2017-11-07 11:59:24.000000000 -0800 +++ new/test/jdk/java/lang/invoke/ArrayLengthTest.java 2017-11-07 11:59:24.000000000 -0800 @@ -78,4 +78,9 @@ MethodHandles.arrayLength(null); } + @Test(expectedExceptions = NullPointerException.class) + public void testNullReference() throws Throwable { + MethodHandle arrayLength = MethodHandles.arrayLength(String[].class); + arrayLength.invokeExact(null); + } } --- /dev/null 2017-11-07 11:59:27.000000000 -0800 +++ new/test/jdk/java/lang/invoke/InvokeMethodHandleWithBadArgument.java 2017-11-07 11:59:25.000000000 -0800 @@ -0,0 +1,163 @@ +/* + * 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. + */ + +/* + * @test + * @bug 8157246 + * @run testng/othervm test.java.lang.invoke.InvokeMethodHandleWithBadArgument + */ + +package test.java.lang.invoke; + +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; +import java.lang.invoke.MethodHandles.Lookup; +import java.lang.invoke.MethodType; +import java.lang.invoke.VarHandle; + +import static java.lang.invoke.MethodType.methodType; + +import static org.testng.AssertJUnit.*; + +import org.testng.annotations.*; + +/** + * Tests invocation of MethodHandle with invalid leading argument such as + * MethodHandle, VarHandle, and array object + */ +public class InvokeMethodHandleWithBadArgument { + // ---- null array reference ---- + + @Test(expectedExceptions = {NullPointerException.class}) + public static void testAsSpreaderPosInvokeWithNull() throws Throwable { + MethodHandle spreader = MH_spread.asSpreader(1, int[].class, 3); + spreader.invoke("A", null, "B"); + } + + @Test(expectedExceptions = {NullPointerException.class}) + public static void testAsSpreaderInvokeWithNull() throws Throwable { + MethodHandle spreader = MH_String_equals.asSpreader(String[].class, 2); + assert ((boolean) spreader.invokeExact(new String[]{"me", "me"})); + boolean eq = (boolean) spreader.invokeExact((String[]) null); + } + + // ---- incorrect array element count ---- + @Test(expectedExceptions = {IllegalArgumentException.class}) + public static void testAsSpreaderPosInvokeWithBadElementCount() throws Throwable { + MethodHandle spreader = MH_spread.asSpreader(1, int[].class, 3); + spreader.invoke("A", new int[]{1, 2}, "B"); + } + + @Test(expectedExceptions = {IllegalArgumentException.class}) + public static void testAsSpreaderInvokeWithBadElementCount() throws Throwable { + MethodHandle spreader = MH_String_equals.asSpreader(String[].class, 2); + assert (!(boolean) spreader.invokeExact(new String[]{"me", "thee"})); + boolean eq = (boolean) spreader.invokeExact(new String[0]); + } + + // ---- spread no argument ---- + @Test + public static void testAsSpreaderPosInvokeWithZeroLength() throws Throwable { + MethodHandle spreader = MH_spread.asSpreader(1, int[].class, 0); + assert("A123B".equals(spreader.invoke("A", (int[])null, 1, 2, 3, "B"))); + } + + @Test + public static void testAsSpreaderInvokeWithZeroLength() throws Throwable { + MethodHandle spreader = MH_String_equals.asSpreader(String[].class, 0); + assert ((boolean) spreader.invokeExact("me", "me", new String[0])); + boolean eq = (boolean) spreader.invokeExact("me", (Object)"me", (String[]) null); + } + + // ---- invokers with null method/var handle argument ---- + @Test(expectedExceptions = {NullPointerException.class}) + public static void testInvokerWithNull() throws Throwable { + MethodType type = methodType(int.class, int.class, int.class); + MethodHandle invoker = MethodHandles.invoker(type); + assert((int) invoker.invoke(MH_add, 1, 2) == 3); + int sum = (int)invoker.invoke((MethodHandle)null, 1, 2); + } + + @Test(expectedExceptions = {NullPointerException.class}) + public static void testExactInvokerWithNull() throws Throwable { + MethodType type = methodType(int.class, int.class, int.class); + MethodHandle invoker = MethodHandles.exactInvoker(type); + assert((int) invoker.invoke(MH_add, 1, 2) == 3); + int sum = (int)invoker.invokeExact((MethodHandle)null, 1, 2); + } + + @Test(expectedExceptions = {NullPointerException.class}) + public static void testSpreadInvokerWithNull() throws Throwable { + MethodType type = methodType(boolean.class, String.class, String.class); + MethodHandle invoker = MethodHandles.spreadInvoker(type, 0); + assert ((boolean) invoker.invoke(MH_spread, new String[]{"me", "me"})); + boolean eq = (boolean) invoker.invoke((MethodHandle)null, new String[]{"me", "me"}); + } + + @Test(expectedExceptions = {NullPointerException.class}) + public static void testVarHandleInvokerWithNull() throws Throwable { + VarHandle.AccessMode am = VarHandle.AccessMode.GET; + MethodHandle invoker = MethodHandles.varHandleInvoker(am, VH_array.accessModeType(am)); + assert ((int) invoker.invoke(VH_array, array, 3) == 3); + int value = (int)invoker.invoke((VarHandle)null, array, 3); + } + + @Test(expectedExceptions = {NullPointerException.class}) + public static void testVarHandleExactInvokerWithNull() throws Throwable { + VarHandle.AccessMode am = VarHandle.AccessMode.GET; + MethodHandle invoker = MethodHandles.varHandleExactInvoker(am, VH_array.accessModeType(am)); + assert ((int) invoker.invoke(VH_array, array, 3) == 3); + int value = (int)invoker.invokeExact((VarHandle)null, array, 3); + } + + static final Lookup LOOKUP = MethodHandles.lookup(); + static final MethodHandle MH_add; + static final MethodHandle MH_spread; + static final MethodHandle MH_String_equals; + static final VarHandle VH_array; + + static final int[] array = new int[] { 0, 1, 2, 3, 4, 5}; + static { + try { + Class arrayClass = Class.forName("[I"); + VH_array = MethodHandles.arrayElementVarHandle(arrayClass); + MH_add = LOOKUP.findStatic(InvokeMethodHandleWithBadArgument.class, "add", + methodType(int.class, int.class, int.class)); + MH_spread = LOOKUP.findStatic(InvokeMethodHandleWithBadArgument.class, "spread", + methodType(String.class, String.class, int.class, int.class, int.class, String.class)); + MH_String_equals = LOOKUP.findVirtual(String.class, "equals", methodType(boolean.class, Object.class)); + } catch (Exception e) { + throw new ExceptionInInitializerError(e); + } + } + + static String spread(String s1, int i1, int i2, int i3, String s2) { + return s1 + i1 + i2 + i3 + s2; + } + + static int add(int x, int y) { + return x+y; + } +}