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 -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
  31  *
  32  */
  33 
  34 public class Test {
  35 
  36     static String[] strings = new String[1024];
  37     private static final int ITERATIONS = 100000;
  38 
  39     public static void main(String[] args) {
  40 
  41         long start_total = System.currentTimeMillis();
  42 
  43         // search variable size substring in string (33 chars).
  44         String a = " 1111111111111xx1111111111111xx11y"; // +1 to execute a.substring(1) first
  45         String b =  "1111111111111xx1111111111111xx11y";
  46         test_varsub_indexof(a, b);
  47 
  48         // search variable size substring in string (32 chars).
  49         a = " 1111111111111xx1111111111111xx1y";
  50         b =  "1111111111111xx1111111111111xx1y";
  51         test_varsub_indexof(a, b);
  52 
  53         // search variable size substring in string (17 chars).
  54         a = " 1111111111111xx1y";
  55         b =  "1111111111111xx1y";
  56         test_varsub_indexof(a, b);
  57 
  58         // search variable size substring in string (16 chars).
  59         a = " 111111111111xx1y";
  60         b =  "111111111111xx1y";
  61         test_varsub_indexof(a, b);
  62 
  63         // search variable size substring in string (8 chars).
  64         a = " 1111xx1y";
  65         b =  "1111xx1y";
  66         test_varsub_indexof(a, b);
  67 
  68         // search variable size substring in string (7 chars).
  69         a = " 111xx1y";
  70         b =  "111xx1y";
  71         test_varsub_indexof(a, b);
  72 
  73 
  74 
  75         // search substring (17 chars) in variable size string.
  76         a =                 "1111111111111xx1x";
  77         b = " 1111111111111xx1111111111111xx1x"; // +1 to execute b.substring(1) first
  78         test_varstr_indexof(a, b);
  79 
  80         // search substring (16 chars) in variable size string.
  81         a =                  "111111111111xx1x";
  82         b = " 1111111111111xx1111111111111xx1x";
  83         test_varstr_indexof(a, b);
  84 
  85         // search substring (9 chars) in variable size string.
  86         a =                         "11111xx1x";
  87         b = " 1111111111111xx1111111111111xx1x";
  88         test_varstr_indexof(a, b);
  89 
  90         // search substring (8 chars) in variable size string.
  91         a =                          "1111xx1x";
  92         b = " 1111111111111xx1111111111111xx1x";
  93         test_varstr_indexof(a, b);
  94 
  95         // search substring (4 chars) in variable size string.
  96         a =                              "xx1x";
  97         b = " 1111111111111xx1111111111111xx1x";
  98         test_varstr_indexof(a, b);
  99 
 100         // search substring (3 chars) in variable size string.
 101         a =                               "x1x";
 102         b = " 1111111111111xx1111111111111xx1x";
 103         test_varstr_indexof(a, b);
 104 
 105         // search substring (2 chars) in variable size string.
 106         a =                                "1y";
 107         b = " 1111111111111xx1111111111111xx1y";
 108         test_varstr_indexof(a, b);
 109 
 110 
 111 
 112         // search non matching variable size substring in string (33 chars).
 113         a = " 1111111111111xx1111111111111xx11z"; // +1 to execute a.substring(1) first
 114         b =  "1111111111111xx1111111111111xx11y";
 115         test_missub_indexof(a, b);
 116 
 117         // search non matching variable size substring in string (32 chars).
 118         a = " 1111111111111xx1111111111111xx1z";
 119         b =  "1111111111111xx1111111111111xx1y";
 120         test_missub_indexof(a, b);
 121 
 122         // search non matching variable size substring in string (17 chars).
 123         a = " 1111111111111xx1z";
 124         b =  "1111111111111xx1y";
 125         test_missub_indexof(a, b);
 126 
 127         // search non matching variable size substring in string (16 chars).
 128         a = " 111111111111xx1z";
 129         b =  "111111111111xx1y";
 130         test_missub_indexof(a, b);
 131 
 132         // search non matching variable size substring in string (8 chars).
 133         a = " 1111xx1z";
 134         b =  "1111xx1y";
 135         test_missub_indexof(a, b);
 136 
 137         // search non matching variable size substring in string (7 chars).
 138         a = " 111xx1z";
 139         b =  "111xx1y";
 140         test_missub_indexof(a, b);
 141 
 142 
 143 
 144         // Testing constant substring search in variable size string.
 145 
 146         // search constant substring (17 chars).
 147         b = " 1111111111111xx1111111111111xx1x"; // +1 to execute b.substring(1) first
 148         TestCon tc = new TestCon17();
 149         test_consub_indexof(tc, b);
 150 
 151         // search constant substring (16 chars).
 152         b = " 1111111111111xx1111111111111xx1x";
 153         tc = new TestCon16();
 154         test_consub_indexof(tc, b);
 155 
 156         // search constant substring (9 chars).
 157         b = " 1111111111111xx1111111111111xx1x";
 158         tc = new TestCon9();
 159         test_consub_indexof(tc, b);
 160 
 161         // search constant substring (8 chars).
 162         b = " 1111111111111xx1111111111111xx1x";
 163         tc = new TestCon8();
 164         test_consub_indexof(tc, b);
 165 
 166         // search constant substring (4 chars).
 167         b = " 1111111111111xx1111111111111xx1x";
 168         tc = new TestCon4();
 169         test_consub_indexof(tc, b);
 170 
 171         // search constant substring (3 chars).
 172         b = " 1111111111111xx1111111111111xx1x";
 173         tc = new TestCon3();
 174         test_consub_indexof(tc, b);
 175 
 176         // search constant substring (2 chars).
 177         b = " 1111111111111xx1111111111111xx1y";
 178         tc = new TestCon2();
 179         test_consub_indexof(tc, b);
 180 
 181         // search constant substring (1 chars).
 182         b = " 1111111111111xx1111111111111xx1y";
 183         tc = new TestCon1();
 184         test_consub_indexof(tc, b);
 185 
 186 
 187         // search non matching constant substring (17 chars).
 188         b = " 1111111111111xx1111111111111xx1z"; // +1 to execute b.substring(1) first
 189         tc = new TestCon17();
 190         test_conmis_indexof(tc, b);
 191 
 192         // search non matching constant substring (16 chars).
 193         b = " 1111111111111xx1111111111111xx1z";
 194         tc = new TestCon16();
 195         test_conmis_indexof(tc, b);
 196 
 197         // search non matching constant substring (9 chars).
 198         b = " 1111111111111xx1111111111111xx1z";
 199         tc = new TestCon9();
 200         test_conmis_indexof(tc, b);
 201 
 202         // search non matching constant substring (8 chars).
 203         b = " 1111111111111xx1111111111111xx1z";
 204         tc = new TestCon8();
 205         test_conmis_indexof(tc, b);
 206 
 207         // search non matching constant substring (4 chars).
 208         b = " 1111111111111xx1111111111111xx1z";
 209         tc = new TestCon4();
 210         test_conmis_indexof(tc, b);
 211 
 212         // search non matching constant substring (3 chars).
 213         b = " 1111111111111xx1111111111111xx1z";
 214         tc = new TestCon3();
 215         test_conmis_indexof(tc, b);
 216 
 217         // search non matching constant substring (2 chars).
 218         b = " 1111111111111xx1111111111111xx1z";
 219         tc = new TestCon2();
 220         test_conmis_indexof(tc, b);
 221 
 222         // search non matching constant substring (1 chars).
 223         b = " 1111111111111xx1111111111111xx1z";
 224         tc = new TestCon1();
 225         test_conmis_indexof(tc, b);
 226 
 227         long end_total = System.currentTimeMillis();
 228         System.out.println("End run time: " + (end_total - start_total));
 229 
 230     }
 231 
 232     public static long test_init(String a, String b) {
 233         for (int i = 0; i < 512; i++) {
 234             strings[i * 2] = new String(b.toCharArray());
 235             strings[i * 2 + 1] = new String(a.toCharArray());
 236         }
 237         System.out.print(a.length() + " " + b.length() + " ");
 238         return System.currentTimeMillis();
 239     }
 240 
 241     public static void test_end(String a, String b, int v, int expected, long start) {
 242         long end = System.currentTimeMillis();
 243         int res = (v/ITERATIONS);
 244         System.out.print(" " + res);
 245         System.out.println(" time:" + (end - start));
 246         if (res != expected) {
 247             System.out.println("wrong indexOf result: " + res + ", expected " + expected);
 248             System.out.println("\"" + b + "\".indexOf(\"" + a + "\")");
 249             System.exit(97);
 250         }
 251     }
 252 
 253     public static int test_subvar() {
 254         int s = 0;
 255         int v = 0;
 256         for (int i = 0; i < ITERATIONS; i++) {
 257             v += strings[s].indexOf(strings[s + 1]);
 258             s += 2;
 259             if (s >= strings.length) s = 0;
 260         }
 261         return v;
 262     }
 263 
 264     public static void test_varsub_indexof(String a, String b) {
 265         System.out.println("Start search variable size substring in string (" + b.length() + " chars)");
 266         long start_it = System.currentTimeMillis();
 267         int limit = 1; // last a.length() == 1
 268         while (a.length() > limit) {
 269             a = a.substring(1);
 270             long start = test_init(a, b);
 271             int v = test_subvar();
 272             test_end(a, b, v, (b.length() - a.length()), start);
 273         }
 274         long end_it = System.currentTimeMillis();
 275         System.out.println("End search variable size substring in string (" + b.length() + " chars), time: " + (end_it - start_it));
 276     }
 277 
 278     public static void test_varstr_indexof(String a, String b) {
 279         System.out.println("Start search substring (" + a.length() + " chars) in variable size string");
 280         long start_it = System.currentTimeMillis();
 281         int limit = a.length();
 282         while (b.length() > limit) {
 283             b = b.substring(1);
 284             long start = test_init(a, b);
 285             int v = test_subvar();
 286             test_end(a, b, v, (b.length() - a.length()), start);
 287         }
 288         long end_it = System.currentTimeMillis();
 289         System.out.println("End search substring (" + a.length() + " chars) in variable size string, time: " + (end_it - start_it));
 290     }
 291 
 292     public static void test_missub_indexof(String a, String b) {
 293         System.out.println("Start search non matching variable size substring in string (" + b.length() + " chars)");
 294         long start_it = System.currentTimeMillis();
 295         int limit = 1; // last a.length() == 1
 296         while (a.length() > limit) {
 297             a = a.substring(1);
 298             long start = test_init(a, b);
 299             int v = test_subvar();
 300             test_end(a, b, v, (-1), start);
 301         }
 302         long end_it = System.currentTimeMillis();
 303         System.out.println("End search non matching variable size substring in string (" + b.length() + " chars), time: " + (end_it - start_it));
 304     }
 305 
 306 
 307 
 308     public static void test_consub_indexof(TestCon tc, String b) {
 309         System.out.println("Start search constant substring (" + tc.constr().length() + " chars)");
 310         long start_it = System.currentTimeMillis();
 311         int limit = tc.constr().length();
 312         while (b.length() > limit) {
 313             b = b.substring(1);
 314             long start = test_init(tc.constr(), b);
 315             int v = test_subcon(tc);
 316             test_end(tc.constr(), b, v, (b.length() - tc.constr().length()), start);
 317         }
 318         long end_it = System.currentTimeMillis();
 319         System.out.println("End search constant substring (" + tc.constr().length() + " chars), time: " + (end_it - start_it));
 320     }
 321 
 322     public static void test_conmis_indexof(TestCon tc, String b) {
 323         System.out.println("Start search non matching constant substring (" + tc.constr().length() + " chars)");
 324         long start_it = System.currentTimeMillis();
 325         int limit = tc.constr().length();
 326         while (b.length() > limit) {
 327             b = b.substring(1);
 328             long start = test_init(tc.constr(), b);
 329             int v = test_subcon(tc);
 330             test_end(tc.constr(), b, v, (-1), start);
 331         }
 332         long end_it = System.currentTimeMillis();
 333         System.out.println("End search non matching constant substring (" + tc.constr().length() + " chars), time: " + (end_it - start_it));
 334     }
 335 
 336     public static int test_subcon(TestCon tc) {
 337         int s = 0;
 338         int v = 0;
 339         for (int i = 0; i < ITERATIONS; i++) {
 340             v += tc.indexOf(strings[s]);
 341             s += 2;
 342             if (s >= strings.length) s = 0;
 343         }
 344         return v;
 345     }
 346 
 347     private interface TestCon {
 348         public String constr();
 349         public int indexOf(String str);
 350     }
 351 
 352     // search constant substring (17 chars).
 353     private final static class TestCon17 implements TestCon {
 354         private static final String constr = "1111111111111xx1x";
 355         public String constr() { return constr; }
 356         public int indexOf(String str) { return str.indexOf(constr); }
 357     }
 358 
 359     // search constant substring (16 chars).
 360     private final static class TestCon16 implements TestCon {
 361         private static final String constr = "111111111111xx1x";
 362         public String constr() { return constr; }
 363         public int indexOf(String str) { return str.indexOf(constr); }
 364     }
 365 
 366     // search constant substring (9 chars).
 367     private final static class TestCon9 implements TestCon {
 368         private static final String constr = "11111xx1x";
 369         public String constr() { return constr; }
 370         public int indexOf(String str) { return str.indexOf(constr); }
 371     }
 372 
 373     // search constant substring (8 chars).
 374     private final static class TestCon8 implements TestCon {
 375         private static final String constr = "1111xx1x";
 376         public String constr() { return constr; }
 377         public int indexOf(String str) { return str.indexOf(constr); }
 378     }
 379 
 380     // search constant substring (4 chars).
 381     private final static class TestCon4 implements TestCon {
 382         private static final String constr = "xx1x";
 383         public String constr() { return constr; }
 384         public int indexOf(String str) { return str.indexOf(constr); }
 385     }
 386 
 387     // search constant substring (3 chars).
 388     private final static class TestCon3 implements TestCon {
 389         private static final String constr = "x1x";
 390         public String constr() { return constr; }
 391         public int indexOf(String str) { return str.indexOf(constr); }
 392     }
 393 
 394     // search constant substring (2 chars).
 395     private final static class TestCon2 implements TestCon {
 396         private static final String constr = "1y";
 397         public String constr() { return constr; }
 398         public int indexOf(String str) { return str.indexOf(constr); }
 399     }
 400 
 401 
 402     // search constant substring (1 chars).
 403     private final static class TestCon1 implements TestCon {
 404         private static final String constr = "y";
 405         public String constr() { return constr; }
 406         public int indexOf(String str) { return str.indexOf(constr); }
 407     }
 408 }
 409