Path: blob/master/test/hotspot/jtreg/compiler/intrinsics/string/TestStringUTF16IntrinsicRangeChecks.java
41153 views
/*1* Copyright (c) 2017, 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 815816826* @summary Verifies that callers of StringUTF16 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 -XX:+UnlockDiagnosticVMOptions -XX:DisableIntrinsic=_getCharStringU,_putCharStringU compiler.intrinsics.string.TestStringUTF16IntrinsicRangeChecks30* @run main/othervm -Xbatch -XX:CompileThreshold=100 -esa -ea -XX:+UnlockDiagnosticVMOptions -XX:DisableIntrinsic=_getCharStringU,_putCharStringU compiler.intrinsics.string.TestStringUTF16IntrinsicRangeChecks31*/32package compiler.intrinsics.string;3334import java.lang.reflect.Field;35import java.util.Arrays;3637public class TestStringUTF16IntrinsicRangeChecks {3839public static void main(String[] args) throws Exception {40byte[] val = new byte[2];41byte[] b4 = new byte[4];42char[] c4 = new char[4];43String s4 = new String(c4);44byte[] valHigh = new byte[2];45byte[] valLow = new byte[2];46Helper.putCharSB(valHigh, 0, Character.MIN_HIGH_SURROGATE);47Helper.putCharSB(valLow, 0, Character.MIN_LOW_SURROGATE);4849for (int i = 0; i < 1000; ++i) {50getChars((int)1234, -5, -5 + 4, val);51getChars((int)1234, -1, -1 + 4, val);52getChars((int)1234, 0, 0 + 4, val);53getChars((int)1234, 1, 1 + 4, val);5455getChars((long)1234, -5, -5 + 4, val);56getChars((long)1234, -1, -1 + 4, val);57getChars((long)1234, 0, 0 + 4, val);58getChars((long)1234, 1, 1 + 4, val);5960byte[] val2 = Arrays.copyOf(val, val.length);61putCharSB(val2, -1, '!');62putCharSB(val2, 1, '!');6364byte[] val4 = Arrays.copyOf(b4, b4.length);65char[] c2 = new char[2];66String s2 = new String(c2);6768putCharsSB(val4, -3, c2, 0, 2);69putCharsSB(val4, -1, c2, 0, 2);70putCharsSB(val4, 0, c4, 0, 4);71putCharsSB(val4, 1, c2, 0, 2);72putCharsSB(val4, -3, s2, 0, 2);73putCharsSB(val4, -1, s2, 0, 2);74putCharsSB(val4, 0, s4, 0, 4);75putCharsSB(val4, 1, s2, 0, 2);7677codePointAtSB(valHigh, -1, 1);78codePointAtSB(valHigh, -1, 2);79codePointAtSB(valHigh, 0, 2);80codePointAtSB(valHigh, 1, 2);8182codePointBeforeSB(valLow, 0);83codePointBeforeSB(valLow, -1);84codePointBeforeSB(valLow, 2);8586if (Helper.codePointCountSB(valHigh, 0, 1) != 1) {87throw new AssertionError("codePointCountSB");88}89if (Helper.codePointCountSB(valLow, 0, 1) != 1) {90throw new AssertionError("codePointCountSB");91}92codePointCountSB(valHigh, -1, 0);93codePointCountSB(valHigh, -1, 2);94codePointCountSB(valHigh, 0, 2);9596charAt(val, -1);97charAt(val, 1);9899contentEquals(b4, val, -1);100contentEquals(b4, val, 2);101contentEquals(val, s4, 2);102contentEquals(val, s4, -1);103104StringBuilder sb = new StringBuilder();105sb.append((String)null).append(true).append(false);106if (!sb.toString().equals("nulltruefalse")) {107throw new AssertionError("append");108}109110putCharsAt(val2, -1, '1', '2', '3', '4');111putCharsAt(val2, 0, '1', '2', '3', '4');112putCharsAt(val2, 2, '1', '2', '3', '4');113putCharsAt(val2, -1, '1', '2', '3', '4', '5');114putCharsAt(val2, 0, '1', '2', '3', '4', '5');115putCharsAt(val2, 2, '1', '2', '3', '4', '5');116117reverse(valHigh, -1);118reverse(valHigh, 2);119reverse(valLow, -1);120reverse(valLow, 2);121122byte[] d4 = new byte[4];123inflate(b4, 0, d4, -1, 2);124inflate(b4, 0, d4, 3, 2);125inflate(b4, 0, d4, 4, 1);126127byte[] b0 = new byte[0];128byte[] b1 = new byte[1];129byte[] b2 = new byte[2];130byte[] t1 = new byte[] {1};131byte[] t2 = new byte[] {1, 2};132byte[] t4 = new byte[] {1, 2, 3, 4};133indexOf(b1, 1, t2, 1, 0);134indexOf(b2, 1, t1, 1, 0);135indexOf(b2, 2, t2, 1, 0);136indexOf(b2, 1, t2, 2, 0);137indexOf(b2, -1, t2, 1, 0);138indexOf(b2, 1, t2, -1, 0);139indexOf(b2, 1, t2, 1, 1);140141indexOfLatin1(b1, 1, t1, 1, 0);142indexOfLatin1(b2, 2, t1, 1, 0);143indexOfLatin1(b2, 1, b0, 1, 0);144indexOfLatin1(b2, 1, t1, 2, 0);145indexOfLatin1(b2, -1, t1, 1, 0);146indexOfLatin1(b2, 2, t1, 1, 0);147indexOfLatin1(b2, 1, t1, -1, 0);148indexOfLatin1(b2, 1, t1, 2, 0);149150lastIndexOf(b1, t2, 1, 0);151lastIndexOf(b2, t4, 2, 0);152lastIndexOf(b2, t2, 1, 0);153lastIndexOf(b2, t2, 1, 1);154155lastIndexOfLatin1(b1, t1, 1, 0);156lastIndexOfLatin1(b2, t2, 2, 0);157lastIndexOfLatin1(b2, t1, 1, 0);158lastIndexOfLatin1(b2, t1, 1, 1);159}160}161162static void getChars(int i, int begin, int end, byte[] value) {163try {164Helper.getChars(i, begin, end, value);165throw new AssertionError("getChars");166} catch (IndexOutOfBoundsException io) {167}168}169170static void getChars(long l, int begin, int end, byte[] value) {171try {172Helper.getChars(l, begin, end, value);173throw new AssertionError("getChars");174} catch (IndexOutOfBoundsException io) {175}176}177178static void putCharSB(byte[] val, int index, int c) {179try {180Helper.putCharSB(val, index, c);181throw new AssertionError("putCharSB");182} catch (IndexOutOfBoundsException io) {183}184}185186static void putCharsSB(byte[] val, int index, char[] ca, int off, int end) {187try {188Helper.putCharsSB(val, index, ca, off, end);189throw new AssertionError("putCharsSB");190} catch (IndexOutOfBoundsException io) {191}192}193194static void putCharsSB(byte[] val, int index, CharSequence s, int off, int end) {195try {196Helper.putCharsSB(val, index, s, off, end);197throw new AssertionError("putCharsSB");198} catch (IndexOutOfBoundsException io) {199}200}201202static void codePointAtSB(byte[] val, int index, int end) {203try {204Helper.codePointAtSB(val, index, end);205throw new AssertionError("codePointAtSB");206} catch (IndexOutOfBoundsException io) {207}208}209210static void codePointBeforeSB(byte[] val, int index) {211try {212Helper.codePointBeforeSB(val, index);213throw new AssertionError("codePointBeforeSB");214} catch (IndexOutOfBoundsException io) {215}216}217218static void codePointCountSB(byte[] val, int beginIndex, int endIndex) {219try {220Helper.codePointCountSB(val, beginIndex, endIndex);221throw new AssertionError("codePointCountSB");222} catch (IndexOutOfBoundsException io) {223}224}225226static void charAt(byte[] v, int index) {227try {228Helper.charAt(v, index);229throw new AssertionError("charAt");230} catch (IndexOutOfBoundsException io) {231}232}233234static void contentEquals(byte[] v1, byte[] v2, int len) {235try {236Helper.contentEquals(v1, v2, len);237throw new AssertionError("contentEquals");238} catch (IndexOutOfBoundsException io) {239}240}241242static void contentEquals(byte[] v, CharSequence cs, int len) {243try {244Helper.contentEquals(v, cs, len);245throw new AssertionError("contentEquals");246} catch (IndexOutOfBoundsException io) {247}248}249250static void putCharsAt(byte[] v, int i, char c1, char c2, char c3, char c4) {251try {252Helper.putCharsAt(v, i, c1, c2, c3, c4);253throw new AssertionError("putCharsAt");254} catch (IndexOutOfBoundsException io) {255}256}257258static void putCharsAt(byte[] v, int i, char c1, char c2, char c3, char c4, char c5) {259try {260Helper.putCharsAt(v, i, c1, c2, c3, c4, c5);261throw new AssertionError("putCharsAt");262} catch (IndexOutOfBoundsException io) {263}264}265266static void reverse(byte[] v, int len) {267try {268Helper.reverse(v, len);269throw new AssertionError("reverse");270} catch (IndexOutOfBoundsException io) {271}272}273274static void inflate(byte[] v1, int o1, byte[] v2, int o2, int len) {275try {276Helper.inflate(v1, o1, v2, o2, len);277throw new AssertionError("inflate");278} catch (IndexOutOfBoundsException io) {279}280}281282static void indexOf(byte[] v1, int l1, byte[] v2, int l2, int from) {283try {284if (Helper.indexOf(v1, l1, v2, l2, from) != -1) {285throw new AssertionError("indexOf");286}287} catch (IndexOutOfBoundsException io) {288}289}290291static void lastIndexOf(byte[] v1, byte[] v2, int l2, int from) {292try {293if (Helper.lastIndexOf(v1, v2, l2, from) != -1) {294throw new AssertionError("lastIndexOf");295}296} catch (IndexOutOfBoundsException io) {297}298}299300static void indexOfLatin1(byte[] v1, int l1, byte[] v2, int l2, int from) {301try {302if (Helper.indexOfLatin1(v1, l1, v2, l2, from) != -1) {303throw new AssertionError("indexOfLatin1");304}305} catch (IndexOutOfBoundsException io) {306}307}308309static void lastIndexOfLatin1(byte[] v1, byte[] v2, int l2, int from) {310try {311if (Helper.lastIndexOfLatin1(v1, v2, l2, from) != -1) {312throw new AssertionError("lastIndexOfLatin1");313}314} catch (IndexOutOfBoundsException io) {315}316}317}318319320