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