Path: blob/master/test/hotspot/jtreg/compiler/intrinsics/string/TestStringIntrinsicRangeChecks.java
41153 views
/*1* Copyright (c) 2016, 2018, 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.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 815560826* @summary Verifies that string intrinsics throw array out of bounds exceptions.27* @library /compiler/patches /test/lib28* @build java.base/java.lang.Helper29* @run main/othervm -Xbatch -XX:CompileThreshold=100 compiler.intrinsics.string.TestStringIntrinsicRangeChecks30*/31package compiler.intrinsics.string;3233import java.lang.Helper;34import java.lang.reflect.InvocationTargetException;35import java.lang.reflect.Method;3637public class TestStringIntrinsicRangeChecks {38// Prepare test arrays39private static int SIZE = 16;40private static byte[] byteArray = new byte[SIZE];41private static char[] charArray = new char[SIZE];4243public static void check(Method m, boolean shouldThrow, Object... args) throws Exception {44// Prepare error message45String message = m.getName() + "(";46for (int i = 0; i < args.length; ++i) {47message += args[i];48message += (i+1 < args.length) ? ", " : ")";49}5051try {52m.invoke(null, args);53} catch (InvocationTargetException e) {54// Get actual exception55Throwable t = e.getTargetException();56if (!shouldThrow) {57throw new RuntimeException("Unexpected exception thrown for " + message, e);58}59if (t instanceof StringIndexOutOfBoundsException ||60t instanceof ArrayIndexOutOfBoundsException) {61// Expected exception. Make sure that the exception was not thrown in UTF16.putChar/getChar62// because the corresponding intrinsics are unchecked and the Java code should do all the checks.63StackTraceElement[] stack = t.getStackTrace();64if (stack.length != 0) {65String methodName = stack[0].getMethodName();66if (methodName.equals("putChar") || methodName.equals("getChar")) {67throw new RuntimeException("Exception thrown in " + methodName + " for " + message, t);68}69}70}71return;72}73if (shouldThrow) {74throw new RuntimeException("No exception thrown for " + message);75}76}7778public static void main(String[] args) throws Exception {79// Get intrinsified String API methods80Method compressByte = Helper.class.getMethod("compressByte", byte[].class, int.class, int.class, int.class, int.class);81Method compressChar = Helper.class.getMethod("compressChar", char[].class, int.class, int.class, int.class, int.class);82Method inflateByte = Helper.class.getMethod("inflateByte", byte[].class, int.class, int.class, int.class, int.class);83Method inflateChar = Helper.class.getMethod("inflateChar", byte[].class, int.class, int.class, int.class, int.class);84Method toBytes = Helper.class.getMethod("toBytes", char[].class, int.class, int.class);85Method getChars = Helper.class.getMethod("getChars", byte[].class, int.class, int.class, int.class, int.class);8687// Check different combinations of arguments (source/destination offset and length)88for (int srcOff = 0; srcOff < SIZE; ++srcOff) {89for (int dstOff = 0; dstOff < SIZE; ++dstOff) {90for (int len = 0; len < SIZE; ++len) {91// Check for potential overlows in source or destination array92boolean srcOverflow = (srcOff + len) > SIZE;93boolean srcOverflowB = (2*srcOff + 2*len) > SIZE;94boolean dstOverflow = (dstOff + len) > SIZE;95boolean dstOverflowB = (2*dstOff + 2*len) > SIZE;96boolean getCharsOver = (srcOff < len) && ((2*(len-1) >= SIZE) || ((dstOff + len - srcOff) > SIZE));97// Check if an exception is thrown and bail out if result is inconsistent with above98// assumptions (for example, an exception was not thrown although an overflow happened).99check(compressByte, srcOverflowB || dstOverflow, byteArray, srcOff, SIZE, dstOff, len);100check(compressChar, srcOverflow || dstOverflow, charArray, srcOff, SIZE, dstOff, len);101check(inflateByte, srcOverflow || dstOverflowB, byteArray, srcOff, SIZE, dstOff, len);102check(inflateChar, srcOverflow || dstOverflow, byteArray, srcOff, SIZE, dstOff, len);103check(toBytes, srcOverflow, charArray, srcOff, len);104check(getChars, getCharsOver, byteArray, srcOff, len, SIZE, dstOff);105}106}107}108}109}110111112