1 /* 2 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 /* @test 27 * @bug 8160717 28 * @run main/othervm -ea -esa -Djava.lang.invoke.MethodHandle.COMPILE_THRESHOLD=-1 test.java.lang.invoke.LoopCombinatorLongSignatureTest 29 * @run main/othervm -ea -esa test.java.lang.invoke.LoopCombinatorLongSignatureTest 30 */ 31 32 package test.java.lang.invoke; 33 34 import java.lang.invoke.MethodHandle; 35 import java.lang.invoke.MethodHandles; 36 import java.util.Arrays; 37 38 /** 39 * If a loop with an excessive amount of clauses is created, so that the number of parameters to the resulting loop 40 * handle exceeds the allowed maximum, an IAE must be signalled. The test is run first in LambdaForm interpretation mode 41 * and then in default mode, wherein bytecode generation falls back to LFI mode due to excessively long methods. 42 */ 43 public class LoopCombinatorLongSignatureTest { 44 45 static final MethodHandle INIT = MethodHandles.constant(int.class, 0); 46 static final MethodHandle STEP = MethodHandles.identity(int.class); 47 static final MethodHandle PRED_F = MethodHandles.constant(boolean.class, false); 48 static final MethodHandle PRED_T = MethodHandles.constant(boolean.class, true); 49 static final MethodHandle FINI = MethodHandles.identity(int.class); 50 51 static final int ARG_LIMIT = 254; // for internal reasons, this is the maximum allowed number of arguments 52 53 public static void main(String[] args) { 54 for (int loopArgs = 0; loopArgs < 2; ++loopArgs) { 55 testLongSignature(loopArgs, false); 56 testLongSignature(loopArgs, true); 57 } 58 } 59 60 static void testLongSignature(int loopArgs, boolean excessive) { 61 int nClauses = ARG_LIMIT - loopArgs + (excessive ? 1 : 0); 62 63 System.out.print((excessive ? "(EXCESSIVE)" : "(LONG )") + " arguments: " + loopArgs + ", clauses: " + nClauses + " -> "); 64 65 // extend init to denote what arguments the loop should accept 66 Class<?>[] argTypes = new Class<?>[loopArgs]; 67 Arrays.fill(argTypes, int.class); 68 MethodHandle init = MethodHandles.dropArguments(INIT, 0, argTypes); 69 70 // build clauses 71 MethodHandle[][] clauses = new MethodHandle[nClauses][]; 72 MethodHandle[] clause = {init, STEP, PRED_T, FINI}; 73 MethodHandle[] fclause = {init, STEP, PRED_F, FINI}; 74 Arrays.fill(clauses, clause); 75 clauses[nClauses - 1] = fclause; // make the last clause terminate the loop 76 77 try { 78 MethodHandle loop = MethodHandles.loop(clauses); 79 if (excessive) { 80 throw new AssertionError("loop construction should have failed"); 81 } else { 82 int r; 83 if (loopArgs == 0) { 84 r = (int) loop.invoke(); 85 } else { 86 Object[] args = new Object[loopArgs]; 87 Arrays.fill(args, 0); 88 r = (int) loop.invokeWithArguments(args); 89 } 90 System.out.println("SUCCEEDED (OK) -> " + r); 91 } 92 } catch (IllegalArgumentException iae) { 93 if (excessive) { 94 System.out.println("FAILED (OK)"); 95 } else { 96 iae.printStackTrace(System.out); 97 throw new AssertionError("loop construction should not have failed (see above)"); 98 } 99 } catch (Throwable t) { 100 t.printStackTrace(System.out); 101 throw new AssertionError("unexpected failure (see above)"); 102 } 103 } 104 105 }