1 /*
   2  * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 /**
  26  * @test
  27  * @bug 6942326
  28  * @summary x86 code in string_indexof() could read beyond reserved heap space
  29  *
  30  * @run main/othervm/timeout=300 -Xmx32m -Xbatch -XX:+IgnoreUnrecognizedVMOptions
  31  *      -XX:CompileCommand=exclude,compiler.codegen.Test6942326::main
  32  *      -XX:CompileCommand=exclude,compiler.codegen.Test6942326::test_varsub_indexof
  33  *      -XX:CompileCommand=exclude,compiler.codegen.Test6942326::test_varstr_indexof
  34  *      -XX:CompileCommand=exclude,compiler.codegen.Test6942326::test_missub_indexof
  35  *      -XX:CompileCommand=exclude,compiler.codegen.Test6942326::test_consub_indexof
  36  *      -XX:CompileCommand=exclude,compiler.codegen.Test6942326::test_conmis_indexof
  37  *      -XX:CompileCommand=exclude,compiler.codegen.Test6942326::test_subcon
  38  *      compiler.codegen.Test6942326
  39  */
  40 
  41 package compiler.codegen;
  42 
  43 public class Test6942326 {
  44 
  45     static String[] strings = new String[1024];
  46     private static final int ITERATIONS = 100000;
  47 
  48     public static void main(String[] args) {
  49 
  50         long start_total = System.currentTimeMillis();
  51 
  52         // search variable size substring in string (33 chars).
  53         String a = " 1111111111111xx1111111111111xx11y"; // +1 to execute a.substring(1) first
  54         String b =  "1111111111111xx1111111111111xx11y";
  55         test_varsub_indexof(a, b);
  56 
  57         // search variable size substring in string (32 chars).
  58         a = " 1111111111111xx1111111111111xx1y";
  59         b =  "1111111111111xx1111111111111xx1y";
  60         test_varsub_indexof(a, b);
  61 
  62         // search variable size substring in string (17 chars).
  63         a = " 1111111111111xx1y";
  64         b =  "1111111111111xx1y";
  65         test_varsub_indexof(a, b);
  66 
  67         // search variable size substring in string (16 chars).
  68         a = " 111111111111xx1y";
  69         b =  "111111111111xx1y";
  70         test_varsub_indexof(a, b);
  71 
  72         // search variable size substring in string (8 chars).
  73         a = " 1111xx1y";
  74         b =  "1111xx1y";
  75         test_varsub_indexof(a, b);
  76 
  77         // search variable size substring in string (7 chars).
  78         a = " 111xx1y";
  79         b =  "111xx1y";
  80         test_varsub_indexof(a, b);
  81 
  82 
  83 
  84         // search substring (17 chars) in variable size string.
  85         a =                 "1111111111111xx1x";
  86         b = " 1111111111111xx1111111111111xx1x"; // +1 to execute b.substring(1) first
  87         test_varstr_indexof(a, b);
  88 
  89         // search substring (16 chars) in variable size string.
  90         a =                  "111111111111xx1x";
  91         b = " 1111111111111xx1111111111111xx1x";
  92         test_varstr_indexof(a, b);
  93 
  94         // search substring (9 chars) in variable size string.
  95         a =                         "11111xx1x";
  96         b = " 1111111111111xx1111111111111xx1x";
  97         test_varstr_indexof(a, b);
  98 
  99         // search substring (8 chars) in variable size string.
 100         a =                          "1111xx1x";
 101         b = " 1111111111111xx1111111111111xx1x";
 102         test_varstr_indexof(a, b);
 103 
 104         // search substring (4 chars) in variable size string.
 105         a =                              "xx1x";
 106         b = " 1111111111111xx1111111111111xx1x";
 107         test_varstr_indexof(a, b);
 108 
 109         // search substring (3 chars) in variable size string.
 110         a =                               "x1x";
 111         b = " 1111111111111xx1111111111111xx1x";
 112         test_varstr_indexof(a, b);
 113 
 114         // search substring (2 chars) in variable size string.
 115         a =                                "1y";
 116         b = " 1111111111111xx1111111111111xx1y";
 117         test_varstr_indexof(a, b);
 118 
 119 
 120 
 121         // search non matching variable size substring in string (33 chars).
 122         a = " 1111111111111xx1111111111111xx11z"; // +1 to execute a.substring(1) first
 123         b =  "1111111111111xx1111111111111xx11y";
 124         test_missub_indexof(a, b);
 125 
 126         // search non matching variable size substring in string (32 chars).
 127         a = " 1111111111111xx1111111111111xx1z";
 128         b =  "1111111111111xx1111111111111xx1y";
 129         test_missub_indexof(a, b);
 130 
 131         // search non matching variable size substring in string (17 chars).
 132         a = " 1111111111111xx1z";
 133         b =  "1111111111111xx1y";
 134         test_missub_indexof(a, b);
 135 
 136         // search non matching variable size substring in string (16 chars).
 137         a = " 111111111111xx1z";
 138         b =  "111111111111xx1y";
 139         test_missub_indexof(a, b);
 140 
 141         // search non matching variable size substring in string (8 chars).
 142         a = " 1111xx1z";
 143         b =  "1111xx1y";
 144         test_missub_indexof(a, b);
 145 
 146         // search non matching variable size substring in string (7 chars).
 147         a = " 111xx1z";
 148         b =  "111xx1y";
 149         test_missub_indexof(a, b);
 150 
 151 
 152 
 153         // Testing constant substring search in variable size string.
 154 
 155         // search constant substring (17 chars).
 156         b = " 1111111111111xx1111111111111xx1x"; // +1 to execute b.substring(1) first
 157         TestCon tc = new TestCon17();
 158         test_consub_indexof(tc, b);
 159 
 160         // search constant substring (16 chars).
 161         b = " 1111111111111xx1111111111111xx1x";
 162         tc = new TestCon16();
 163         test_consub_indexof(tc, b);
 164 
 165         // search constant substring (9 chars).
 166         b = " 1111111111111xx1111111111111xx1x";
 167         tc = new TestCon9();
 168         test_consub_indexof(tc, b);
 169 
 170         // search constant substring (8 chars).
 171         b = " 1111111111111xx1111111111111xx1x";
 172         tc = new TestCon8();
 173         test_consub_indexof(tc, b);
 174 
 175         // search constant substring (4 chars).
 176         b = " 1111111111111xx1111111111111xx1x";
 177         tc = new TestCon4();
 178         test_consub_indexof(tc, b);
 179 
 180         // search constant substring (3 chars).
 181         b = " 1111111111111xx1111111111111xx1x";
 182         tc = new TestCon3();
 183         test_consub_indexof(tc, b);
 184 
 185         // search constant substring (2 chars).
 186         b = " 1111111111111xx1111111111111xx1y";
 187         tc = new TestCon2();
 188         test_consub_indexof(tc, b);
 189 
 190         // search constant substring (1 chars).
 191         b = " 1111111111111xx1111111111111xx1y";
 192         tc = new TestCon1();
 193         test_consub_indexof(tc, b);
 194 
 195 
 196         // search non matching constant substring (17 chars).
 197         b = " 1111111111111xx1111111111111xx1z"; // +1 to execute b.substring(1) first
 198         tc = new TestCon17();
 199         test_conmis_indexof(tc, b);
 200 
 201         // search non matching constant substring (16 chars).
 202         b = " 1111111111111xx1111111111111xx1z";
 203         tc = new TestCon16();
 204         test_conmis_indexof(tc, b);
 205 
 206         // search non matching constant substring (9 chars).
 207         b = " 1111111111111xx1111111111111xx1z";
 208         tc = new TestCon9();
 209         test_conmis_indexof(tc, b);
 210 
 211         // search non matching constant substring (8 chars).
 212         b = " 1111111111111xx1111111111111xx1z";
 213         tc = new TestCon8();
 214         test_conmis_indexof(tc, b);
 215 
 216         // search non matching constant substring (4 chars).
 217         b = " 1111111111111xx1111111111111xx1z";
 218         tc = new TestCon4();
 219         test_conmis_indexof(tc, b);
 220 
 221         // search non matching constant substring (3 chars).
 222         b = " 1111111111111xx1111111111111xx1z";
 223         tc = new TestCon3();
 224         test_conmis_indexof(tc, b);
 225 
 226         // search non matching constant substring (2 chars).
 227         b = " 1111111111111xx1111111111111xx1z";
 228         tc = new TestCon2();
 229         test_conmis_indexof(tc, b);
 230 
 231         // search non matching constant substring (1 chars).
 232         b = " 1111111111111xx1111111111111xx1z";
 233         tc = new TestCon1();
 234         test_conmis_indexof(tc, b);
 235 
 236         long end_total = System.currentTimeMillis();
 237         System.out.println("End run time: " + (end_total - start_total));
 238 
 239     }
 240 
 241     public static long test_init(String a, String b) {
 242         for (int i = 0; i < 512; i++) {
 243             strings[i * 2] = new String(b.toCharArray());
 244             strings[i * 2 + 1] = new String(a.toCharArray());
 245         }
 246         System.out.print(a.length() + " " + b.length() + " ");
 247         return System.currentTimeMillis();
 248     }
 249 
 250     public static void test_end(String a, String b, int v, int expected, long start) {
 251         long end = System.currentTimeMillis();
 252         int res = (v/ITERATIONS);
 253         System.out.print(" " + res);
 254         System.out.println(" time:" + (end - start));
 255         if (res != expected) {
 256             System.out.println("wrong indexOf result: " + res + ", expected " + expected);
 257             System.out.println("\"" + b + "\".indexOf(\"" + a + "\")");
 258             System.exit(97);
 259         }
 260     }
 261 
 262     public static int test_subvar() {
 263         int s = 0;
 264         int v = 0;
 265         for (int i = 0; i < ITERATIONS; i++) {
 266             v += strings[s].indexOf(strings[s + 1]);
 267             s += 2;
 268             if (s >= strings.length) s = 0;
 269         }
 270         return v;
 271     }
 272 
 273     public static void test_varsub_indexof(String a, String b) {
 274         System.out.println("Start search variable size substring in string (" + b.length() + " chars)");
 275         long start_it = System.currentTimeMillis();
 276         int limit = 1; // last a.length() == 1
 277         while (a.length() > limit) {
 278             a = a.substring(1);
 279             long start = test_init(a, b);
 280             int v = test_subvar();
 281             test_end(a, b, v, (b.length() - a.length()), start);
 282         }
 283         long end_it = System.currentTimeMillis();
 284         System.out.println("End search variable size substring in string (" + b.length() + " chars), time: " + (end_it - start_it));
 285     }
 286 
 287     public static void test_varstr_indexof(String a, String b) {
 288         System.out.println("Start search substring (" + a.length() + " chars) in variable size string");
 289         long start_it = System.currentTimeMillis();
 290         int limit = a.length();
 291         while (b.length() > limit) {
 292             b = b.substring(1);
 293             long start = test_init(a, b);
 294             int v = test_subvar();
 295             test_end(a, b, v, (b.length() - a.length()), start);
 296         }
 297         long end_it = System.currentTimeMillis();
 298         System.out.println("End search substring (" + a.length() + " chars) in variable size string, time: " + (end_it - start_it));
 299     }
 300 
 301     public static void test_missub_indexof(String a, String b) {
 302         System.out.println("Start search non matching variable size substring in string (" + b.length() + " chars)");
 303         long start_it = System.currentTimeMillis();
 304         int limit = 1; // last a.length() == 1
 305         while (a.length() > limit) {
 306             a = a.substring(1);
 307             long start = test_init(a, b);
 308             int v = test_subvar();
 309             test_end(a, b, v, (-1), start);
 310         }
 311         long end_it = System.currentTimeMillis();
 312         System.out.println("End search non matching variable size substring in string (" + b.length() + " chars), time: " + (end_it - start_it));
 313     }
 314 
 315 
 316 
 317     public static void test_consub_indexof(TestCon tc, String b) {
 318         System.out.println("Start search constant substring (" + tc.constr().length() + " chars)");
 319         long start_it = System.currentTimeMillis();
 320         int limit = tc.constr().length();
 321         while (b.length() > limit) {
 322             b = b.substring(1);
 323             long start = test_init(tc.constr(), b);
 324             int v = test_subcon(tc);
 325             test_end(tc.constr(), b, v, (b.length() - tc.constr().length()), start);
 326         }
 327         long end_it = System.currentTimeMillis();
 328         System.out.println("End search constant substring (" + tc.constr().length() + " chars), time: " + (end_it - start_it));
 329     }
 330 
 331     public static void test_conmis_indexof(TestCon tc, String b) {
 332         System.out.println("Start search non matching constant substring (" + tc.constr().length() + " chars)");
 333         long start_it = System.currentTimeMillis();
 334         int limit = tc.constr().length();
 335         while (b.length() > limit) {
 336             b = b.substring(1);
 337             long start = test_init(tc.constr(), b);
 338             int v = test_subcon(tc);
 339             test_end(tc.constr(), b, v, (-1), start);
 340         }
 341         long end_it = System.currentTimeMillis();
 342         System.out.println("End search non matching constant substring (" + tc.constr().length() + " chars), time: " + (end_it - start_it));
 343     }
 344 
 345     public static int test_subcon(TestCon tc) {
 346         int s = 0;
 347         int v = 0;
 348         for (int i = 0; i < ITERATIONS; i++) {
 349             v += tc.indexOf(strings[s]);
 350             s += 2;
 351             if (s >= strings.length) s = 0;
 352         }
 353         return v;
 354     }
 355 
 356     private interface TestCon {
 357         public String constr();
 358         public int indexOf(String str);
 359     }
 360 
 361     // search constant substring (17 chars).
 362     private final static class TestCon17 implements TestCon {
 363         private static final String constr = "1111111111111xx1x";
 364         public String constr() { return constr; }
 365         public int indexOf(String str) { return str.indexOf(constr); }
 366     }
 367 
 368     // search constant substring (16 chars).
 369     private final static class TestCon16 implements TestCon {
 370         private static final String constr = "111111111111xx1x";
 371         public String constr() { return constr; }
 372         public int indexOf(String str) { return str.indexOf(constr); }
 373     }
 374 
 375     // search constant substring (9 chars).
 376     private final static class TestCon9 implements TestCon {
 377         private static final String constr = "11111xx1x";
 378         public String constr() { return constr; }
 379         public int indexOf(String str) { return str.indexOf(constr); }
 380     }
 381 
 382     // search constant substring (8 chars).
 383     private final static class TestCon8 implements TestCon {
 384         private static final String constr = "1111xx1x";
 385         public String constr() { return constr; }
 386         public int indexOf(String str) { return str.indexOf(constr); }
 387     }
 388 
 389     // search constant substring (4 chars).
 390     private final static class TestCon4 implements TestCon {
 391         private static final String constr = "xx1x";
 392         public String constr() { return constr; }
 393         public int indexOf(String str) { return str.indexOf(constr); }
 394     }
 395 
 396     // search constant substring (3 chars).
 397     private final static class TestCon3 implements TestCon {
 398         private static final String constr = "x1x";
 399         public String constr() { return constr; }
 400         public int indexOf(String str) { return str.indexOf(constr); }
 401     }
 402 
 403     // search constant substring (2 chars).
 404     private final static class TestCon2 implements TestCon {
 405         private static final String constr = "1y";
 406         public String constr() { return constr; }
 407         public int indexOf(String str) { return str.indexOf(constr); }
 408     }
 409 
 410 
 411     // search constant substring (1 chars).
 412     private final static class TestCon1 implements TestCon {
 413         private static final String constr = "y";
 414         public String constr() { return constr; }
 415         public int indexOf(String str) { return str.indexOf(constr); }
 416     }
 417 }
 418