UnsafeAccess.java revision 12827:5242609b8088
1/*
2 * Copyright (c) 2016, 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 * @test
25 * @bug 8134918
26 * @modules java.base/jdk.internal.misc
27 *
28 * @run main/bootclasspath/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:TypeProfileLevel=222 -XX:+UseTypeSpeculation -Xbatch
29 *                                 -XX:CompileCommand=dontinline,compiler.profiling.UnsafeAccess::test*
30 *                                 compiler.profiling.UnsafeAccess
31 */
32
33package compiler.profiling;
34
35import jdk.internal.misc.Unsafe;
36
37public class UnsafeAccess {
38    private static final Unsafe U = Unsafe.getUnsafe();
39
40    static Class cls = Object.class;
41    static long off = U.ARRAY_OBJECT_BASE_OFFSET;
42
43    static Object testUnsafeAccess(Object o, boolean isObjArray) {
44        if (o != null && cls.isInstance(o)) { // speculates "o" type to int[]
45            return helperUnsafeAccess(o, isObjArray);
46        }
47        return null;
48    }
49
50    static Object helperUnsafeAccess(Object o, boolean isObjArray) {
51        if (isObjArray) {
52            U.putObject(o, off, new Object());
53        }
54        return o;
55    }
56
57    static Object testUnsafeLoadStore(Object o, boolean isObjArray) {
58        if (o != null && cls.isInstance(o)) { // speculates "o" type to int[]
59            return helperUnsafeLoadStore(o, isObjArray);
60        }
61        return null;
62    }
63
64    static Object helperUnsafeLoadStore(Object o, boolean isObjArray) {
65        if (isObjArray) {
66            Object o1 = U.getObject(o, off);
67            U.compareAndSetObject(o, off, o1, new Object());
68        }
69        return o;
70    }
71
72    public static void main(String[] args) {
73        Object[] objArray = new Object[10];
74        int[]    intArray = new    int[10];
75
76        for (int i = 0; i < 20_000; i++) {
77            helperUnsafeAccess(objArray, true);
78        }
79        for (int i = 0; i < 20_000; i++) {
80            testUnsafeAccess(intArray, false);
81        }
82
83        for (int i = 0; i < 20_000; i++) {
84            helperUnsafeLoadStore(objArray, true);
85        }
86        for (int i = 0; i < 20_000; i++) {
87            testUnsafeLoadStore(intArray, false);
88        }
89
90        System.out.println("TEST PASSED");
91    }
92}
93