Path: blob/master/test/jdk/java/lang/invoke/LoopCombinatorLongSignatureTest.java
41149 views
/*1* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/* @test26* @bug 816071727* @run main/othervm -ea -esa -Djava.lang.invoke.MethodHandle.COMPILE_THRESHOLD=-1 test.java.lang.invoke.LoopCombinatorLongSignatureTest28* @run main/othervm -ea -esa test.java.lang.invoke.LoopCombinatorLongSignatureTest29*/3031package test.java.lang.invoke;3233import java.lang.invoke.MethodHandle;34import java.lang.invoke.MethodHandles;35import java.util.Arrays;3637/**38* If a loop with an excessive amount of clauses is created, so that the number of parameters to the resulting loop39* handle exceeds the allowed maximum, an IAE must be signalled. The test is run first in LambdaForm interpretation mode40* and then in default mode, wherein bytecode generation falls back to LFI mode due to excessively long methods.41* <p>42* By default, the test run only checks whether loop handle construction succeeds and fails. If executing the generated43* loops is desired, this should be indicated by setting the {@code java.lang.invoke.LoopCombinatorLongSignatureTest.RUN}44* environment variable to {@code true}. This is disabled by default as it considerably increases the time needed to run45* the test.46*/47public class LoopCombinatorLongSignatureTest {4849static final MethodHandle INIT = MethodHandles.constant(int.class, 0);50static final MethodHandle STEP = MethodHandles.identity(int.class);51static final MethodHandle PRED_F = MethodHandles.constant(boolean.class, false);52static final MethodHandle PRED_T = MethodHandles.constant(boolean.class, true);53static final MethodHandle FINI = MethodHandles.identity(int.class);5455static final int ARG_LIMIT = 254; // for internal reasons, this is the maximum allowed number of arguments5657public static void main(String[] args) {58boolean run = Boolean.parseBoolean(59System.getProperty("java.lang.invoke.LoopCombinatorLongSignatureTest.RUN", "false"));60for (int loopArgs = 0; loopArgs < 2; ++loopArgs) {61testLongSignature(loopArgs, false, run);62testLongSignature(loopArgs, true, run);63}64}6566static void testLongSignature(int loopArgs, boolean excessive, boolean run) {67int nClauses = ARG_LIMIT - loopArgs + (excessive ? 1 : 0);6869System.out.print((excessive ? "(EXCESSIVE)" : "(LONG )") + " arguments: " + loopArgs + ", clauses: " + nClauses + " -> ");7071// extend init to denote what arguments the loop should accept72Class<?>[] argTypes = new Class<?>[loopArgs];73Arrays.fill(argTypes, int.class);74MethodHandle init = MethodHandles.dropArguments(INIT, 0, argTypes);7576// build clauses77MethodHandle[][] clauses = new MethodHandle[nClauses][];78MethodHandle[] clause = {init, STEP, PRED_T, FINI};79MethodHandle[] fclause = {init, STEP, PRED_F, FINI};80Arrays.fill(clauses, clause);81clauses[nClauses - 1] = fclause; // make the last clause terminate the loop8283try {84MethodHandle loop = MethodHandles.loop(clauses);85if (excessive) {86throw new AssertionError("loop construction should have failed");87} else if (run) {88int r;89if (loopArgs == 0) {90r = (int) loop.invoke();91} else {92Object[] args = new Object[loopArgs];93Arrays.fill(args, 0);94r = (int) loop.invokeWithArguments(args);95}96System.out.println("SUCCEEDED (OK) -> " + r);97} else {98System.out.println("SUCCEEDED (OK)");99}100} catch (IllegalArgumentException iae) {101if (excessive) {102System.out.println("FAILED (OK)");103} else {104iae.printStackTrace(System.out);105throw new AssertionError("loop construction should not have failed (see above)");106}107} catch (Throwable t) {108t.printStackTrace(System.out);109throw new AssertionError("unexpected failure (see above)");110}111}112113}114115116