test/hotspot/jtreg/compiler/jsr292/LongReferenceCastingTest.java
author prr
Thu, 26 Jul 2018 10:00:40 -0700
changeset 51251 0a7a0a6dfa22
parent 47216 71c04702a3d5
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
35846
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
     1
/*
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
     4
 *
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
     7
 * published by the Free Software Foundation.
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
     8
 *
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    13
 * accompanied this code).
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    14
 *
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    18
 *
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    21
 * questions.
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    22
 */
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    23
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    24
/**
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    25
 * @test
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    26
 * @bug 8148752
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    27
 * @summary Test correct casting of MH arguments during inlining.
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35846
diff changeset
    28
 *
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35846
diff changeset
    29
 * @run main compiler.jsr292.LongReferenceCastingTest
35846
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    30
 */
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35846
diff changeset
    31
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35846
diff changeset
    32
package compiler.jsr292;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35846
diff changeset
    33
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35846
diff changeset
    34
import java.lang.invoke.MethodHandle;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35846
diff changeset
    35
import java.lang.invoke.MethodHandles;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35846
diff changeset
    36
import java.lang.invoke.MethodType;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 35846
diff changeset
    37
35846
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    38
public class LongReferenceCastingTest {
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    39
    static final String MY_STRING = "myString";
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    40
    static final MethodHandle MH;
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    41
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    42
    static {
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    43
        try {
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    44
            MethodHandles.Lookup lookup = MethodHandles.lookup();
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    45
            MethodType mt = MethodType.methodType(String.class, long.class, Object.class, String.class);
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    46
            MH = lookup.findVirtual(LongReferenceCastingTest.class, "myMethod", mt);
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    47
        } catch (Exception e) {
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    48
            throw new Error(e);
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    49
        }
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    50
    }
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    51
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    52
    public String myMethod(long l, Object o, String s) {
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    53
        // The long argument occupies two stack slots, causing C2 to treat it as
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    54
        // two arguments and casting the fist one two long and the second one to Object.
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    55
        // As a result, Object o is casted to String and the o.toString() call is
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    56
        // inlined as String::toString(). We fail at runtime because 'o' is not a String.
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    57
        return o.toString();
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    58
    }
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    59
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    60
    public String toString() {
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    61
        return MY_STRING;
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    62
    }
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    63
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    64
    public static void main(String[] args) throws Exception {
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    65
        LongReferenceCastingTest test = new LongReferenceCastingTest();
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    66
        try {
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    67
            for (int i = 0; i < 20_000; ++i) {
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    68
                if (!test.invoke().equals(MY_STRING)) {
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    69
                    throw new RuntimeException("Invalid string");
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    70
                }
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    71
            }
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    72
        } catch (Throwable t) {
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    73
            throw new RuntimeException("Test failed", t);
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    74
        }
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    75
    }
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    76
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    77
    public String invoke() throws Throwable {
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    78
        return (String) MH.invokeExact(this, 0L, (Object)this, MY_STRING);
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    79
    }
f5a8b47778ef 8148752: Compiled StringBuilder code throws StringIndexOutOfBoundsException
thartmann
parents:
diff changeset
    80
}