1 /*
   2  * Copyright (c) 2013, 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 8023472
  28  * @summary C2 optimization breaks with G1
  29  *
  30  * @run main/othervm -Xbatch -XX:+IgnoreUnrecognizedVMOptions -XX:-TieredCompilation -Dcount=100000 G1CrashTest
  31  *
  32  * @author pbiswal@palantir.com
  33  */
  34 
  35 public class G1CrashTest {
  36     static Object[] set = new Object[11];
  37 
  38     public static void main(String[] args) throws InterruptedException {
  39         for (int j = 0; j < Integer.getInteger("count"); j++) {
  40             Object key = new Object();
  41             insertKey(key);
  42             if (j > set.length / 2) {
  43                 Object[] oldKeys = set;
  44                 set = new Object[2 * set.length - 1];
  45                 for (Object o : oldKeys) {
  46                     if (o != null)
  47                         insertKey(o);
  48                 }
  49             }
  50         }
  51     }
  52 
  53     static void insertKey(Object key) {
  54         int hash = key.hashCode() & 0x7fffffff;
  55         int index = hash % set.length;
  56         Object cur = set[index];
  57         if (cur == null)
  58             set[index] = key;
  59         else
  60             insertKeyRehash(key, index, hash, cur);
  61     }
  62 
  63     static void insertKeyRehash(Object key, int index, int hash, Object cur) {
  64         int loopIndex = index;
  65         int firstRemoved = -1;
  66         do {
  67             if (cur == "dead")
  68                 firstRemoved = 1;
  69             index--;
  70             if (index < 0)
  71                 index += set.length;
  72             cur = set[index];
  73             if (cur == null) {
  74                 if (firstRemoved != -1)
  75                     set[firstRemoved] = "dead";
  76                 else
  77                     set[index] = key;
  78                 return;
  79             }
  80         } while (index != loopIndex);
  81         if (firstRemoved != -1)
  82             set[firstRemoved] = null;
  83     }
  84 }