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