1/*
2 * Copyright (c) 2015, 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
26 * @bug 8077504
27 * @summary Unsafe load can loose control dependency and cause crash
28 * @modules java.base/jdk.internal.misc:+open
29 *
30 * @run main/othervm -XX:-BackgroundCompilation -XX:-UseOnStackReplacement
31 *                   compiler.unsafe.TestUnsafeLoadControl
32 */
33
34package compiler.unsafe;
35
36import jdk.internal.misc.Unsafe;
37
38import java.lang.reflect.Field;
39
40public class TestUnsafeLoadControl {
41
42    private static final Unsafe UNSAFE;
43
44    static {
45        try {
46            Field unsafeField = Unsafe.class.getDeclaredField("theUnsafe");
47            unsafeField.setAccessible(true);
48            UNSAFE = (Unsafe) unsafeField.get(null);
49        } catch(Exception e) {
50            throw new RuntimeException(e);
51        }
52    }
53
54    static int val;
55    static void test1(int[] a, boolean[] flags, boolean flag, long j) {
56        for (int i = 0; i < 10; i++) {
57            if (flags[i]) {
58                if (flag) {
59                    long address = (j << 2) + UNSAFE.ARRAY_INT_BASE_OFFSET;
60                    int v = UNSAFE.getInt(a, address);
61                    val = v;
62                }
63            }
64        }
65    }
66
67    static int test2(int[] a, boolean[] flags, boolean flag, long j) {
68        int sum = 0;
69        for (int i = 0; i < 10; i++) {
70            if (flags[i]) {
71                if (flag) {
72                    long address = (j << 2) + UNSAFE.ARRAY_INT_BASE_OFFSET;
73                    int v = UNSAFE.getInt(a, address);
74                    if (v == 0) {
75                        sum++;
76                    }
77                }
78            }
79        }
80        return sum;
81    }
82
83    static public void main(String[] args) {
84        boolean[] flags = new boolean[10];
85        for (int i = 0; i < flags.length; i++) {
86            flags[i] = true;
87        }
88        int[] array = new int[10];
89        for (int i = 0; i < 20000; i++) {
90            test1(array, flags, true, 0);
91        }
92        for (int i = 0; i < flags.length; i++) {
93            flags[i] = false;
94        }
95        test1(array, flags, true, Long.MAX_VALUE/4);
96
97        for (int i = 0; i < flags.length; i++) {
98            flags[i] = true;
99        }
100        for (int i = 0; i < 20000; i++) {
101            test2(array, flags, true, 0);
102        }
103        for (int i = 0; i < flags.length; i++) {
104            flags[i] = false;
105        }
106        test2(array, flags, true, Long.MAX_VALUE/4);
107    }
108}
109