1/*
2 * Copyright (c) 2017, 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 * @test ArraysNewInstanceBug
26 * @bug 8182397
27 * @summary race in setting array_klass field for component mirror with mirror update for klass
28 * @modules java.base/jdk.internal.misc
29 * @run main/othervm -Xcomp ArraysNewInstanceBug
30 */
31
32// This test crashes in compiled code with race, because the compiler generates code that assumes this ordering.
33import java.lang.reflect.Array;
34import java.net.URL;
35import java.net.URLClassLoader;
36
37public class ArraysNewInstanceBug implements Runnable {
38    static Class<?>[] classes;
39
40    int start;
41
42    ArraysNewInstanceBug(int start) {
43        this.start = start;
44    }
45
46    String[] result;
47
48    public void run() {
49        result = new String[classes.length];
50        System.err.print('.');
51        for (int i = start; i < classes.length; i++) {
52            result[i] = Array.newInstance(classes[i], 0).getClass().getName();
53        }
54    }
55
56    public static void main(String[] args) throws Throwable {
57        Class<?> c = ArraysNewInstanceBug.class;
58        ClassLoader apploader =  c.getClassLoader();
59        for (int iter = 0; iter < 10 ; iter++) {  // 10 is enough to get it to crash on my machine.
60            System.err.print('[');
61            classes = new Class<?>[1000];
62            String urlpath = "file://" + System.getProperty("test.classes") + "/";
63            for (int i = 0; i < classes.length; i++) {
64                ClassLoader loader = new URLClassLoader(new URL[] { new URL(urlpath) }, apploader.getParent());
65                classes[i] = loader.loadClass(c.getSimpleName());
66            }
67            System.err.print(']');
68            System.err.print('(');
69            int threadCount = 64;
70            Thread[] threads = new Thread[threadCount];
71            for (int i = 0; i < threads.length; i++) {
72                threads[i] = new Thread(new ArraysNewInstanceBug(i));
73            }
74            for (int i = 0; i < threads.length; i++) {
75                threads[i].start();
76            }
77            for (int i = 0; i < threads.length; i++) {
78                threads[i].join();
79            }
80            System.err.print(')');
81        }
82    }
83}
84