Path: blob/master/test/hotspot/jtreg/compiler/codegen/Test6942326.java
41149 views
/*1* Copyright (c) 2011, 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 694232626* @summary x86 code in string_indexof() could read beyond reserved heap space27*28* @run main/othervm/timeout=300 -Xmx128m -Xbatch -XX:+IgnoreUnrecognizedVMOptions29* -XX:CompileCommand=exclude,compiler.codegen.Test6942326::main30* -XX:CompileCommand=exclude,compiler.codegen.Test6942326::test_varsub_indexof31* -XX:CompileCommand=exclude,compiler.codegen.Test6942326::test_varstr_indexof32* -XX:CompileCommand=exclude,compiler.codegen.Test6942326::test_missub_indexof33* -XX:CompileCommand=exclude,compiler.codegen.Test6942326::test_consub_indexof34* -XX:CompileCommand=exclude,compiler.codegen.Test6942326::test_conmis_indexof35* -XX:CompileCommand=exclude,compiler.codegen.Test6942326::test_subcon36* compiler.codegen.Test694232637*/3839package compiler.codegen;4041public class Test6942326 {4243static String[] strings = new String[1024];44private static final int ITERATIONS = 100000;4546public static void main(String[] args) {4748long start_total = System.currentTimeMillis();4950// search variable size substring in string (33 chars).51String a = " 1111111111111xx1111111111111xx11y"; // +1 to execute a.substring(1) first52String b = "1111111111111xx1111111111111xx11y";53test_varsub_indexof(a, b);5455// search variable size substring in string (32 chars).56a = " 1111111111111xx1111111111111xx1y";57b = "1111111111111xx1111111111111xx1y";58test_varsub_indexof(a, b);5960// search variable size substring in string (17 chars).61a = " 1111111111111xx1y";62b = "1111111111111xx1y";63test_varsub_indexof(a, b);6465// search variable size substring in string (16 chars).66a = " 111111111111xx1y";67b = "111111111111xx1y";68test_varsub_indexof(a, b);6970// search variable size substring in string (8 chars).71a = " 1111xx1y";72b = "1111xx1y";73test_varsub_indexof(a, b);7475// search variable size substring in string (7 chars).76a = " 111xx1y";77b = "111xx1y";78test_varsub_indexof(a, b);79808182// search substring (17 chars) in variable size string.83a = "1111111111111xx1x";84b = " 1111111111111xx1111111111111xx1x"; // +1 to execute b.substring(1) first85test_varstr_indexof(a, b);8687// search substring (16 chars) in variable size string.88a = "111111111111xx1x";89b = " 1111111111111xx1111111111111xx1x";90test_varstr_indexof(a, b);9192// search substring (9 chars) in variable size string.93a = "11111xx1x";94b = " 1111111111111xx1111111111111xx1x";95test_varstr_indexof(a, b);9697// search substring (8 chars) in variable size string.98a = "1111xx1x";99b = " 1111111111111xx1111111111111xx1x";100test_varstr_indexof(a, b);101102// search substring (4 chars) in variable size string.103a = "xx1x";104b = " 1111111111111xx1111111111111xx1x";105test_varstr_indexof(a, b);106107// search substring (3 chars) in variable size string.108a = "x1x";109b = " 1111111111111xx1111111111111xx1x";110test_varstr_indexof(a, b);111112// search substring (2 chars) in variable size string.113a = "1y";114b = " 1111111111111xx1111111111111xx1y";115test_varstr_indexof(a, b);116117118119// search non matching variable size substring in string (33 chars).120a = " 1111111111111xx1111111111111xx11z"; // +1 to execute a.substring(1) first121b = "1111111111111xx1111111111111xx11y";122test_missub_indexof(a, b);123124// search non matching variable size substring in string (32 chars).125a = " 1111111111111xx1111111111111xx1z";126b = "1111111111111xx1111111111111xx1y";127test_missub_indexof(a, b);128129// search non matching variable size substring in string (17 chars).130a = " 1111111111111xx1z";131b = "1111111111111xx1y";132test_missub_indexof(a, b);133134// search non matching variable size substring in string (16 chars).135a = " 111111111111xx1z";136b = "111111111111xx1y";137test_missub_indexof(a, b);138139// search non matching variable size substring in string (8 chars).140a = " 1111xx1z";141b = "1111xx1y";142test_missub_indexof(a, b);143144// search non matching variable size substring in string (7 chars).145a = " 111xx1z";146b = "111xx1y";147test_missub_indexof(a, b);148149150151// Testing constant substring search in variable size string.152153// search constant substring (17 chars).154b = " 1111111111111xx1111111111111xx1x"; // +1 to execute b.substring(1) first155TestCon tc = new TestCon17();156test_consub_indexof(tc, b);157158// search constant substring (16 chars).159b = " 1111111111111xx1111111111111xx1x";160tc = new TestCon16();161test_consub_indexof(tc, b);162163// search constant substring (9 chars).164b = " 1111111111111xx1111111111111xx1x";165tc = new TestCon9();166test_consub_indexof(tc, b);167168// search constant substring (8 chars).169b = " 1111111111111xx1111111111111xx1x";170tc = new TestCon8();171test_consub_indexof(tc, b);172173// search constant substring (4 chars).174b = " 1111111111111xx1111111111111xx1x";175tc = new TestCon4();176test_consub_indexof(tc, b);177178// search constant substring (3 chars).179b = " 1111111111111xx1111111111111xx1x";180tc = new TestCon3();181test_consub_indexof(tc, b);182183// search constant substring (2 chars).184b = " 1111111111111xx1111111111111xx1y";185tc = new TestCon2();186test_consub_indexof(tc, b);187188// search constant substring (1 chars).189b = " 1111111111111xx1111111111111xx1y";190tc = new TestCon1();191test_consub_indexof(tc, b);192193194// search non matching constant substring (17 chars).195b = " 1111111111111xx1111111111111xx1z"; // +1 to execute b.substring(1) first196tc = new TestCon17();197test_conmis_indexof(tc, b);198199// search non matching constant substring (16 chars).200b = " 1111111111111xx1111111111111xx1z";201tc = new TestCon16();202test_conmis_indexof(tc, b);203204// search non matching constant substring (9 chars).205b = " 1111111111111xx1111111111111xx1z";206tc = new TestCon9();207test_conmis_indexof(tc, b);208209// search non matching constant substring (8 chars).210b = " 1111111111111xx1111111111111xx1z";211tc = new TestCon8();212test_conmis_indexof(tc, b);213214// search non matching constant substring (4 chars).215b = " 1111111111111xx1111111111111xx1z";216tc = new TestCon4();217test_conmis_indexof(tc, b);218219// search non matching constant substring (3 chars).220b = " 1111111111111xx1111111111111xx1z";221tc = new TestCon3();222test_conmis_indexof(tc, b);223224// search non matching constant substring (2 chars).225b = " 1111111111111xx1111111111111xx1z";226tc = new TestCon2();227test_conmis_indexof(tc, b);228229// search non matching constant substring (1 chars).230b = " 1111111111111xx1111111111111xx1z";231tc = new TestCon1();232test_conmis_indexof(tc, b);233234long end_total = System.currentTimeMillis();235System.out.println("End run time: " + (end_total - start_total));236237}238239public static long test_init(String a, String b) {240for (int i = 0; i < 512; i++) {241strings[i * 2] = new String(b.toCharArray());242strings[i * 2 + 1] = new String(a.toCharArray());243}244System.out.print(a.length() + " " + b.length() + " ");245return System.currentTimeMillis();246}247248public static void test_end(String a, String b, int v, int expected, long start) {249long end = System.currentTimeMillis();250int res = (v/ITERATIONS);251System.out.print(" " + res);252System.out.println(" time:" + (end - start));253if (res != expected) {254System.out.println("wrong indexOf result: " + res + ", expected " + expected);255System.out.println("\"" + b + "\".indexOf(\"" + a + "\")");256System.exit(97);257}258}259260public static int test_subvar() {261int s = 0;262int v = 0;263for (int i = 0; i < ITERATIONS; i++) {264v += strings[s].indexOf(strings[s + 1]);265s += 2;266if (s >= strings.length) s = 0;267}268return v;269}270271public static void test_varsub_indexof(String a, String b) {272System.out.println("Start search variable size substring in string (" + b.length() + " chars)");273long start_it = System.currentTimeMillis();274int limit = 1; // last a.length() == 1275while (a.length() > limit) {276a = a.substring(1);277long start = test_init(a, b);278int v = test_subvar();279test_end(a, b, v, (b.length() - a.length()), start);280}281long end_it = System.currentTimeMillis();282System.out.println("End search variable size substring in string (" + b.length() + " chars), time: " + (end_it - start_it));283}284285public static void test_varstr_indexof(String a, String b) {286System.out.println("Start search substring (" + a.length() + " chars) in variable size string");287long start_it = System.currentTimeMillis();288int limit = a.length();289while (b.length() > limit) {290b = b.substring(1);291long start = test_init(a, b);292int v = test_subvar();293test_end(a, b, v, (b.length() - a.length()), start);294}295long end_it = System.currentTimeMillis();296System.out.println("End search substring (" + a.length() + " chars) in variable size string, time: " + (end_it - start_it));297}298299public static void test_missub_indexof(String a, String b) {300System.out.println("Start search non matching variable size substring in string (" + b.length() + " chars)");301long start_it = System.currentTimeMillis();302int limit = 1; // last a.length() == 1303while (a.length() > limit) {304a = a.substring(1);305long start = test_init(a, b);306int v = test_subvar();307test_end(a, b, v, (-1), start);308}309long end_it = System.currentTimeMillis();310System.out.println("End search non matching variable size substring in string (" + b.length() + " chars), time: " + (end_it - start_it));311}312313314315public static void test_consub_indexof(TestCon tc, String b) {316System.out.println("Start search constant substring (" + tc.constr().length() + " chars)");317long start_it = System.currentTimeMillis();318int limit = tc.constr().length();319while (b.length() > limit) {320b = b.substring(1);321long start = test_init(tc.constr(), b);322int v = test_subcon(tc);323test_end(tc.constr(), b, v, (b.length() - tc.constr().length()), start);324}325long end_it = System.currentTimeMillis();326System.out.println("End search constant substring (" + tc.constr().length() + " chars), time: " + (end_it - start_it));327}328329public static void test_conmis_indexof(TestCon tc, String b) {330System.out.println("Start search non matching constant substring (" + tc.constr().length() + " chars)");331long start_it = System.currentTimeMillis();332int limit = tc.constr().length();333while (b.length() > limit) {334b = b.substring(1);335long start = test_init(tc.constr(), b);336int v = test_subcon(tc);337test_end(tc.constr(), b, v, (-1), start);338}339long end_it = System.currentTimeMillis();340System.out.println("End search non matching constant substring (" + tc.constr().length() + " chars), time: " + (end_it - start_it));341}342343public static int test_subcon(TestCon tc) {344int s = 0;345int v = 0;346for (int i = 0; i < ITERATIONS; i++) {347v += tc.indexOf(strings[s]);348s += 2;349if (s >= strings.length) s = 0;350}351return v;352}353354private interface TestCon {355public String constr();356public int indexOf(String str);357}358359// search constant substring (17 chars).360private final static class TestCon17 implements TestCon {361private static final String constr = "1111111111111xx1x";362public String constr() { return constr; }363public int indexOf(String str) { return str.indexOf(constr); }364}365366// search constant substring (16 chars).367private final static class TestCon16 implements TestCon {368private static final String constr = "111111111111xx1x";369public String constr() { return constr; }370public int indexOf(String str) { return str.indexOf(constr); }371}372373// search constant substring (9 chars).374private final static class TestCon9 implements TestCon {375private static final String constr = "11111xx1x";376public String constr() { return constr; }377public int indexOf(String str) { return str.indexOf(constr); }378}379380// search constant substring (8 chars).381private final static class TestCon8 implements TestCon {382private static final String constr = "1111xx1x";383public String constr() { return constr; }384public int indexOf(String str) { return str.indexOf(constr); }385}386387// search constant substring (4 chars).388private final static class TestCon4 implements TestCon {389private static final String constr = "xx1x";390public String constr() { return constr; }391public int indexOf(String str) { return str.indexOf(constr); }392}393394// search constant substring (3 chars).395private final static class TestCon3 implements TestCon {396private static final String constr = "x1x";397public String constr() { return constr; }398public int indexOf(String str) { return str.indexOf(constr); }399}400401// search constant substring (2 chars).402private final static class TestCon2 implements TestCon {403private static final String constr = "1y";404public String constr() { return constr; }405public int indexOf(String str) { return str.indexOf(constr); }406}407408409// search constant substring (1 chars).410private final static class TestCon1 implements TestCon {411private static final String constr = "y";412public String constr() { return constr; }413public int indexOf(String str) { return str.indexOf(constr); }414}415}416417418