hotspot/test/compiler/intrinsics/string/TestStringIntrinsicRangeChecks.java
author hseigel
Wed, 01 Mar 2017 08:00:02 -0500
changeset 46194 5596e6f63072
parent 40631 ed82623d7831
permissions -rw-r--r--
8172307: Remove ununsed JVM API JVM_GetModuleByPackageName() Summary: Remove get_module_by_package_name() etc., and unneeded test. Reviewed-by: sspitsyn, gtriantafill
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
38683
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
     1
/*
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
     4
 *
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    10
 *
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    15
 * accompanied this code).
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    16
 *
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    20
 *
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    23
 * questions.
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    24
 */
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    25
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    26
/*
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    27
 * @test
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    28
 * @bug 8155608
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    29
 * @summary Verifies that string intrinsics throw array out of bounds exceptions.
40631
ed82623d7831 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 40059
diff changeset
    30
 * @library /compiler/patches /test/lib
38683
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    31
 * @build java.base/java.lang.Helper
40045
4273f3ba95f7 8159129: TestStringIntrinsicRangeChecks fails w/ No exception thrown for compressByte/inflateByte
thartmann
parents: 38683
diff changeset
    32
 * @run main/othervm -Xbatch -XX:CompileThreshold=100 -XX:-TieredCompilation compiler.intrinsics.string.TestStringIntrinsicRangeChecks
38683
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    33
 */
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    34
package compiler.intrinsics.string;
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    35
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    36
import java.lang.Helper;
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 40045
diff changeset
    37
import java.lang.reflect.InvocationTargetException;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 40045
diff changeset
    38
import java.lang.reflect.Method;
38683
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    39
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    40
public class TestStringIntrinsicRangeChecks {
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    41
    // Prepare test arrays
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    42
    private static int SIZE = 16;
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    43
    private static byte[] byteArray = new byte[SIZE];
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    44
    private static char[] charArray = new char[SIZE];
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    45
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    46
    public static void check(Method m, boolean shouldThrow, Object... args) throws Exception {
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    47
        // Prepare error message
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    48
        String message = m.getName() + "(";
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    49
        for (int i = 0; i < args.length; ++i) {
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    50
            message += args[i];
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    51
            message += (i+1 < args.length) ? ", " : ")";
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    52
        }
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    53
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    54
        try {
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    55
            m.invoke(null, args);
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    56
        } catch (InvocationTargetException e) {
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    57
            // Get actual exception
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    58
            Throwable t = e.getTargetException();
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    59
            if (!shouldThrow) {
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    60
                throw new RuntimeException("Unexpected exception thrown for " + message, e);
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    61
            }
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    62
            if (t instanceof StringIndexOutOfBoundsException ||
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    63
                t instanceof ArrayIndexOutOfBoundsException) {
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    64
                // Expected exception. Make sure that the exception was not thrown in UTF16.putChar/getChar
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    65
                // because the corresponding intrinsics are unchecked and the Java code should do all the checks.
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    66
                StackTraceElement[] stack = t.getStackTrace();
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    67
                if (stack.length != 0) {
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    68
                    String methodName = stack[0].getMethodName();
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    69
                    if (methodName.equals("putChar") || methodName.equals("getChar")) {
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    70
                        throw new RuntimeException("Exception thrown in " + methodName + " for " + message, t);
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    71
                    }
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    72
                }
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    73
            }
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    74
            return;
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    75
        }
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    76
        if (shouldThrow) {
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    77
            throw new RuntimeException("No exception thrown for " + message);
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    78
        }
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    79
    }
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    80
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    81
    public static void main(String[] args) throws Exception {
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    82
        // Get intrinsified String API methods
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    83
        Method compressByte = Helper.class.getMethod("compressByte", byte[].class, int.class, int.class, int.class, int.class);
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    84
        Method compressChar = Helper.class.getMethod("compressChar", char[].class, int.class, int.class, int.class, int.class);
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    85
        Method inflateByte  = Helper.class.getMethod("inflateByte",  byte[].class, int.class, int.class, int.class, int.class);
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    86
        Method inflateChar  = Helper.class.getMethod("inflateChar",  byte[].class, int.class, int.class, int.class, int.class);
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    87
        Method toBytes      = Helper.class.getMethod("toBytes",      char[].class, int.class, int.class);
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    88
        Method getChars     = Helper.class.getMethod("getChars",     byte[].class, int.class, int.class, int.class, int.class);
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    89
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    90
        // Check different combinations of arguments (source/destination offset and length)
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    91
        for (int srcOff = 0; srcOff < SIZE; ++srcOff) {
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    92
            for (int dstOff = 0; dstOff < SIZE; ++dstOff) {
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    93
                for (int len = 0; len < SIZE; ++len) {
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    94
                    // Check for potential overlows in source or destination array
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    95
                    boolean srcOverflow  = (srcOff + len) > SIZE;
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    96
                    boolean srcOverflowB = (2*srcOff + 2*len) > SIZE;
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    97
                    boolean dstOverflow  = (dstOff + len) > SIZE;
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    98
                    boolean dstOverflowB = (2*dstOff + 2*len) > SIZE;
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
    99
                    boolean getCharsOver = (srcOff < len) && ((2*(len-1) >= SIZE) || ((dstOff + len - srcOff) > SIZE));
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
   100
                    // Check if an exception is thrown and bail out if result is inconsistent with above
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
   101
                    // assumptions (for example, an exception was not thrown although an overflow happened).
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
   102
                    check(compressByte, srcOverflowB || dstOverflow,  byteArray, srcOff, SIZE, dstOff, len);
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
   103
                    check(compressChar, srcOverflow  || dstOverflow,  charArray, srcOff, SIZE, dstOff, len);
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
   104
                    check(inflateByte,  srcOverflow  || dstOverflowB, byteArray, srcOff, SIZE, dstOff, len);
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
   105
                    check(inflateChar,  srcOverflow  || dstOverflow,  byteArray, srcOff, SIZE, dstOff, len);
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
   106
                    check(toBytes,      srcOverflow,                  charArray, srcOff, len);
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
   107
                    check(getChars,     getCharsOver,                 byteArray, srcOff, len, SIZE, dstOff);
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
   108
                }
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
   109
            }
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
   110
        }
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
   111
    }
e8a871cf6d09 8155608: String intrinsic range checks are not strict enough
thartmann
parents:
diff changeset
   112
}