1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/compiler/6942326/Test.java Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,409 @@ 1.4 +/* 1.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +/** 1.29 + * @test 1.30 + * @bug 6942326 1.31 + * @summary x86 code in string_indexof() could read beyond reserved heap space 1.32 + * 1.33 + * @run main/othervm/timeout=300 -Xmx32m -Xbatch -XX:+IgnoreUnrecognizedVMOptions -XX:CompileCommand=exclude,Test,main -XX:CompileCommand=exclude,Test,test_varsub_indexof -XX:CompileCommand=exclude,Test,test_varstr_indexof -XX:CompileCommand=exclude,Test,test_missub_indexof -XX:CompileCommand=exclude,Test,test_consub_indexof -XX:CompileCommand=exclude,Test,test_conmis_indexof -XX:CompileCommand=exclude,Test,test_subcon Test 1.34 + * 1.35 + */ 1.36 + 1.37 +public class Test { 1.38 + 1.39 + static String[] strings = new String[1024]; 1.40 + private static final int ITERATIONS = 100000; 1.41 + 1.42 + public static void main(String[] args) { 1.43 + 1.44 + long start_total = System.currentTimeMillis(); 1.45 + 1.46 + // search variable size substring in string (33 chars). 1.47 + String a = " 1111111111111xx1111111111111xx11y"; // +1 to execute a.substring(1) first 1.48 + String b = "1111111111111xx1111111111111xx11y"; 1.49 + test_varsub_indexof(a, b); 1.50 + 1.51 + // search variable size substring in string (32 chars). 1.52 + a = " 1111111111111xx1111111111111xx1y"; 1.53 + b = "1111111111111xx1111111111111xx1y"; 1.54 + test_varsub_indexof(a, b); 1.55 + 1.56 + // search variable size substring in string (17 chars). 1.57 + a = " 1111111111111xx1y"; 1.58 + b = "1111111111111xx1y"; 1.59 + test_varsub_indexof(a, b); 1.60 + 1.61 + // search variable size substring in string (16 chars). 1.62 + a = " 111111111111xx1y"; 1.63 + b = "111111111111xx1y"; 1.64 + test_varsub_indexof(a, b); 1.65 + 1.66 + // search variable size substring in string (8 chars). 1.67 + a = " 1111xx1y"; 1.68 + b = "1111xx1y"; 1.69 + test_varsub_indexof(a, b); 1.70 + 1.71 + // search variable size substring in string (7 chars). 1.72 + a = " 111xx1y"; 1.73 + b = "111xx1y"; 1.74 + test_varsub_indexof(a, b); 1.75 + 1.76 + 1.77 + 1.78 + // search substring (17 chars) in variable size string. 1.79 + a = "1111111111111xx1x"; 1.80 + b = " 1111111111111xx1111111111111xx1x"; // +1 to execute b.substring(1) first 1.81 + test_varstr_indexof(a, b); 1.82 + 1.83 + // search substring (16 chars) in variable size string. 1.84 + a = "111111111111xx1x"; 1.85 + b = " 1111111111111xx1111111111111xx1x"; 1.86 + test_varstr_indexof(a, b); 1.87 + 1.88 + // search substring (9 chars) in variable size string. 1.89 + a = "11111xx1x"; 1.90 + b = " 1111111111111xx1111111111111xx1x"; 1.91 + test_varstr_indexof(a, b); 1.92 + 1.93 + // search substring (8 chars) in variable size string. 1.94 + a = "1111xx1x"; 1.95 + b = " 1111111111111xx1111111111111xx1x"; 1.96 + test_varstr_indexof(a, b); 1.97 + 1.98 + // search substring (4 chars) in variable size string. 1.99 + a = "xx1x"; 1.100 + b = " 1111111111111xx1111111111111xx1x"; 1.101 + test_varstr_indexof(a, b); 1.102 + 1.103 + // search substring (3 chars) in variable size string. 1.104 + a = "x1x"; 1.105 + b = " 1111111111111xx1111111111111xx1x"; 1.106 + test_varstr_indexof(a, b); 1.107 + 1.108 + // search substring (2 chars) in variable size string. 1.109 + a = "1y"; 1.110 + b = " 1111111111111xx1111111111111xx1y"; 1.111 + test_varstr_indexof(a, b); 1.112 + 1.113 + 1.114 + 1.115 + // search non matching variable size substring in string (33 chars). 1.116 + a = " 1111111111111xx1111111111111xx11z"; // +1 to execute a.substring(1) first 1.117 + b = "1111111111111xx1111111111111xx11y"; 1.118 + test_missub_indexof(a, b); 1.119 + 1.120 + // search non matching variable size substring in string (32 chars). 1.121 + a = " 1111111111111xx1111111111111xx1z"; 1.122 + b = "1111111111111xx1111111111111xx1y"; 1.123 + test_missub_indexof(a, b); 1.124 + 1.125 + // search non matching variable size substring in string (17 chars). 1.126 + a = " 1111111111111xx1z"; 1.127 + b = "1111111111111xx1y"; 1.128 + test_missub_indexof(a, b); 1.129 + 1.130 + // search non matching variable size substring in string (16 chars). 1.131 + a = " 111111111111xx1z"; 1.132 + b = "111111111111xx1y"; 1.133 + test_missub_indexof(a, b); 1.134 + 1.135 + // search non matching variable size substring in string (8 chars). 1.136 + a = " 1111xx1z"; 1.137 + b = "1111xx1y"; 1.138 + test_missub_indexof(a, b); 1.139 + 1.140 + // search non matching variable size substring in string (7 chars). 1.141 + a = " 111xx1z"; 1.142 + b = "111xx1y"; 1.143 + test_missub_indexof(a, b); 1.144 + 1.145 + 1.146 + 1.147 + // Testing constant substring search in variable size string. 1.148 + 1.149 + // search constant substring (17 chars). 1.150 + b = " 1111111111111xx1111111111111xx1x"; // +1 to execute b.substring(1) first 1.151 + TestCon tc = new TestCon17(); 1.152 + test_consub_indexof(tc, b); 1.153 + 1.154 + // search constant substring (16 chars). 1.155 + b = " 1111111111111xx1111111111111xx1x"; 1.156 + tc = new TestCon16(); 1.157 + test_consub_indexof(tc, b); 1.158 + 1.159 + // search constant substring (9 chars). 1.160 + b = " 1111111111111xx1111111111111xx1x"; 1.161 + tc = new TestCon9(); 1.162 + test_consub_indexof(tc, b); 1.163 + 1.164 + // search constant substring (8 chars). 1.165 + b = " 1111111111111xx1111111111111xx1x"; 1.166 + tc = new TestCon8(); 1.167 + test_consub_indexof(tc, b); 1.168 + 1.169 + // search constant substring (4 chars). 1.170 + b = " 1111111111111xx1111111111111xx1x"; 1.171 + tc = new TestCon4(); 1.172 + test_consub_indexof(tc, b); 1.173 + 1.174 + // search constant substring (3 chars). 1.175 + b = " 1111111111111xx1111111111111xx1x"; 1.176 + tc = new TestCon3(); 1.177 + test_consub_indexof(tc, b); 1.178 + 1.179 + // search constant substring (2 chars). 1.180 + b = " 1111111111111xx1111111111111xx1y"; 1.181 + tc = new TestCon2(); 1.182 + test_consub_indexof(tc, b); 1.183 + 1.184 + // search constant substring (1 chars). 1.185 + b = " 1111111111111xx1111111111111xx1y"; 1.186 + tc = new TestCon1(); 1.187 + test_consub_indexof(tc, b); 1.188 + 1.189 + 1.190 + // search non matching constant substring (17 chars). 1.191 + b = " 1111111111111xx1111111111111xx1z"; // +1 to execute b.substring(1) first 1.192 + tc = new TestCon17(); 1.193 + test_conmis_indexof(tc, b); 1.194 + 1.195 + // search non matching constant substring (16 chars). 1.196 + b = " 1111111111111xx1111111111111xx1z"; 1.197 + tc = new TestCon16(); 1.198 + test_conmis_indexof(tc, b); 1.199 + 1.200 + // search non matching constant substring (9 chars). 1.201 + b = " 1111111111111xx1111111111111xx1z"; 1.202 + tc = new TestCon9(); 1.203 + test_conmis_indexof(tc, b); 1.204 + 1.205 + // search non matching constant substring (8 chars). 1.206 + b = " 1111111111111xx1111111111111xx1z"; 1.207 + tc = new TestCon8(); 1.208 + test_conmis_indexof(tc, b); 1.209 + 1.210 + // search non matching constant substring (4 chars). 1.211 + b = " 1111111111111xx1111111111111xx1z"; 1.212 + tc = new TestCon4(); 1.213 + test_conmis_indexof(tc, b); 1.214 + 1.215 + // search non matching constant substring (3 chars). 1.216 + b = " 1111111111111xx1111111111111xx1z"; 1.217 + tc = new TestCon3(); 1.218 + test_conmis_indexof(tc, b); 1.219 + 1.220 + // search non matching constant substring (2 chars). 1.221 + b = " 1111111111111xx1111111111111xx1z"; 1.222 + tc = new TestCon2(); 1.223 + test_conmis_indexof(tc, b); 1.224 + 1.225 + // search non matching constant substring (1 chars). 1.226 + b = " 1111111111111xx1111111111111xx1z"; 1.227 + tc = new TestCon1(); 1.228 + test_conmis_indexof(tc, b); 1.229 + 1.230 + long end_total = System.currentTimeMillis(); 1.231 + System.out.println("End run time: " + (end_total - start_total)); 1.232 + 1.233 + } 1.234 + 1.235 + public static long test_init(String a, String b) { 1.236 + for (int i = 0; i < 512; i++) { 1.237 + strings[i * 2] = new String(b.toCharArray()); 1.238 + strings[i * 2 + 1] = new String(a.toCharArray()); 1.239 + } 1.240 + System.out.print(a.length() + " " + b.length() + " "); 1.241 + return System.currentTimeMillis(); 1.242 + } 1.243 + 1.244 + public static void test_end(String a, String b, int v, int expected, long start) { 1.245 + long end = System.currentTimeMillis(); 1.246 + int res = (v/ITERATIONS); 1.247 + System.out.print(" " + res); 1.248 + System.out.println(" time:" + (end - start)); 1.249 + if (res != expected) { 1.250 + System.out.println("wrong indexOf result: " + res + ", expected " + expected); 1.251 + System.out.println("\"" + b + "\".indexOf(\"" + a + "\")"); 1.252 + System.exit(97); 1.253 + } 1.254 + } 1.255 + 1.256 + public static int test_subvar() { 1.257 + int s = 0; 1.258 + int v = 0; 1.259 + for (int i = 0; i < ITERATIONS; i++) { 1.260 + v += strings[s].indexOf(strings[s + 1]); 1.261 + s += 2; 1.262 + if (s >= strings.length) s = 0; 1.263 + } 1.264 + return v; 1.265 + } 1.266 + 1.267 + public static void test_varsub_indexof(String a, String b) { 1.268 + System.out.println("Start search variable size substring in string (" + b.length() + " chars)"); 1.269 + long start_it = System.currentTimeMillis(); 1.270 + int limit = 1; // last a.length() == 1 1.271 + while (a.length() > limit) { 1.272 + a = a.substring(1); 1.273 + long start = test_init(a, b); 1.274 + int v = test_subvar(); 1.275 + test_end(a, b, v, (b.length() - a.length()), start); 1.276 + } 1.277 + long end_it = System.currentTimeMillis(); 1.278 + System.out.println("End search variable size substring in string (" + b.length() + " chars), time: " + (end_it - start_it)); 1.279 + } 1.280 + 1.281 + public static void test_varstr_indexof(String a, String b) { 1.282 + System.out.println("Start search substring (" + a.length() + " chars) in variable size string"); 1.283 + long start_it = System.currentTimeMillis(); 1.284 + int limit = a.length(); 1.285 + while (b.length() > limit) { 1.286 + b = b.substring(1); 1.287 + long start = test_init(a, b); 1.288 + int v = test_subvar(); 1.289 + test_end(a, b, v, (b.length() - a.length()), start); 1.290 + } 1.291 + long end_it = System.currentTimeMillis(); 1.292 + System.out.println("End search substring (" + a.length() + " chars) in variable size string, time: " + (end_it - start_it)); 1.293 + } 1.294 + 1.295 + public static void test_missub_indexof(String a, String b) { 1.296 + System.out.println("Start search non matching variable size substring in string (" + b.length() + " chars)"); 1.297 + long start_it = System.currentTimeMillis(); 1.298 + int limit = 1; // last a.length() == 1 1.299 + while (a.length() > limit) { 1.300 + a = a.substring(1); 1.301 + long start = test_init(a, b); 1.302 + int v = test_subvar(); 1.303 + test_end(a, b, v, (-1), start); 1.304 + } 1.305 + long end_it = System.currentTimeMillis(); 1.306 + System.out.println("End search non matching variable size substring in string (" + b.length() + " chars), time: " + (end_it - start_it)); 1.307 + } 1.308 + 1.309 + 1.310 + 1.311 + public static void test_consub_indexof(TestCon tc, String b) { 1.312 + System.out.println("Start search constant substring (" + tc.constr().length() + " chars)"); 1.313 + long start_it = System.currentTimeMillis(); 1.314 + int limit = tc.constr().length(); 1.315 + while (b.length() > limit) { 1.316 + b = b.substring(1); 1.317 + long start = test_init(tc.constr(), b); 1.318 + int v = test_subcon(tc); 1.319 + test_end(tc.constr(), b, v, (b.length() - tc.constr().length()), start); 1.320 + } 1.321 + long end_it = System.currentTimeMillis(); 1.322 + System.out.println("End search constant substring (" + tc.constr().length() + " chars), time: " + (end_it - start_it)); 1.323 + } 1.324 + 1.325 + public static void test_conmis_indexof(TestCon tc, String b) { 1.326 + System.out.println("Start search non matching constant substring (" + tc.constr().length() + " chars)"); 1.327 + long start_it = System.currentTimeMillis(); 1.328 + int limit = tc.constr().length(); 1.329 + while (b.length() > limit) { 1.330 + b = b.substring(1); 1.331 + long start = test_init(tc.constr(), b); 1.332 + int v = test_subcon(tc); 1.333 + test_end(tc.constr(), b, v, (-1), start); 1.334 + } 1.335 + long end_it = System.currentTimeMillis(); 1.336 + System.out.println("End search non matching constant substring (" + tc.constr().length() + " chars), time: " + (end_it - start_it)); 1.337 + } 1.338 + 1.339 + public static int test_subcon(TestCon tc) { 1.340 + int s = 0; 1.341 + int v = 0; 1.342 + for (int i = 0; i < ITERATIONS; i++) { 1.343 + v += tc.indexOf(strings[s]); 1.344 + s += 2; 1.345 + if (s >= strings.length) s = 0; 1.346 + } 1.347 + return v; 1.348 + } 1.349 + 1.350 + private interface TestCon { 1.351 + public String constr(); 1.352 + public int indexOf(String str); 1.353 + } 1.354 + 1.355 + // search constant substring (17 chars). 1.356 + private final static class TestCon17 implements TestCon { 1.357 + private static final String constr = "1111111111111xx1x"; 1.358 + public String constr() { return constr; } 1.359 + public int indexOf(String str) { return str.indexOf(constr); } 1.360 + } 1.361 + 1.362 + // search constant substring (16 chars). 1.363 + private final static class TestCon16 implements TestCon { 1.364 + private static final String constr = "111111111111xx1x"; 1.365 + public String constr() { return constr; } 1.366 + public int indexOf(String str) { return str.indexOf(constr); } 1.367 + } 1.368 + 1.369 + // search constant substring (9 chars). 1.370 + private final static class TestCon9 implements TestCon { 1.371 + private static final String constr = "11111xx1x"; 1.372 + public String constr() { return constr; } 1.373 + public int indexOf(String str) { return str.indexOf(constr); } 1.374 + } 1.375 + 1.376 + // search constant substring (8 chars). 1.377 + private final static class TestCon8 implements TestCon { 1.378 + private static final String constr = "1111xx1x"; 1.379 + public String constr() { return constr; } 1.380 + public int indexOf(String str) { return str.indexOf(constr); } 1.381 + } 1.382 + 1.383 + // search constant substring (4 chars). 1.384 + private final static class TestCon4 implements TestCon { 1.385 + private static final String constr = "xx1x"; 1.386 + public String constr() { return constr; } 1.387 + public int indexOf(String str) { return str.indexOf(constr); } 1.388 + } 1.389 + 1.390 + // search constant substring (3 chars). 1.391 + private final static class TestCon3 implements TestCon { 1.392 + private static final String constr = "x1x"; 1.393 + public String constr() { return constr; } 1.394 + public int indexOf(String str) { return str.indexOf(constr); } 1.395 + } 1.396 + 1.397 + // search constant substring (2 chars). 1.398 + private final static class TestCon2 implements TestCon { 1.399 + private static final String constr = "1y"; 1.400 + public String constr() { return constr; } 1.401 + public int indexOf(String str) { return str.indexOf(constr); } 1.402 + } 1.403 + 1.404 + 1.405 + // search constant substring (1 chars). 1.406 + private final static class TestCon1 implements TestCon { 1.407 + private static final String constr = "y"; 1.408 + public String constr() { return constr; } 1.409 + public int indexOf(String str) { return str.indexOf(constr); } 1.410 + } 1.411 +} 1.412 +