1 /*
  2  * Copyright (c) 2016, 2018, 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 8155781
 27  * @modules java.base/jdk.internal.misc
 28  *
 29  * @run main/bootclasspath/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockDiagnosticVMOptions
 30  *                                 -XX:-TieredCompilation -Xbatch
 31  *                                 -XX:+UseCompressedOops -XX:+UseCompressedClassPointers
 32  *                                 -XX:CompileCommand=dontinline,compiler.unsafe.OpaqueAccesses::test*
 33  *                                 compiler.unsafe.OpaqueAccesses
 34  * @run main/bootclasspath/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockDiagnosticVMOptions
 35  *                                 -XX:-TieredCompilation -Xbatch





 36  *                                 -XX:-UseCompressedOops -XX:+UseCompressedClassPointers
 37  *                                 -XX:CompileCommand=dontinline,compiler.unsafe.OpaqueAccesses::test*





 38  *                                 compiler.unsafe.OpaqueAccesses
 39  */
 40 package compiler.unsafe;
 41 
 42 import jdk.internal.misc.Unsafe;
 43 
 44 import java.lang.reflect.Field;
 45 
 46 public class OpaqueAccesses {
 47     private static final Unsafe UNSAFE = Unsafe.getUnsafe();
 48 
 49     private static final Object INSTANCE = new OpaqueAccesses();
 50 
 51     private static final Object[] ARRAY = new Object[10];
 52 
 53     private static final long F_OFFSET;
 54     private static final long E_OFFSET;
 55 
 56     static {
 57         try {
 58             Field field = OpaqueAccesses.class.getDeclaredField("f");
 59             F_OFFSET = UNSAFE.objectFieldOffset(field);
 60 
 61             E_OFFSET = UNSAFE.arrayBaseOffset(ARRAY.getClass());
 62         } catch (NoSuchFieldException e) {
 63             throw new Error(e);
 64         }
 65     }
 66 
 67     private Object f = new Object();
 68     private long l1, l2;
 69 
 70     static Object testFixedOffsetField(Object o) {
 71         return UNSAFE.getReference(o, F_OFFSET);
 72     }
 73 
 74     static int testFixedOffsetHeader0(Object o) {
 75         return UNSAFE.getInt(o, 0);
 76     }
 77 
 78     static int testFixedOffsetHeader4(Object o) {
 79         return UNSAFE.getInt(o, 4);
 80     }
 81 
 82     static int testFixedOffsetHeader8(Object o) {
 83         return UNSAFE.getInt(o, 8);
 84     }
 85 
 86     static int testFixedOffsetHeader12(Object o) {
 87         return UNSAFE.getInt(o, 12);
 88     }
 89 
 90     static int testFixedOffsetHeader16(Object o) {
 91         return UNSAFE.getInt(o, 16);
 92     }
 93 
 94     static int testFixedOffsetHeader17(Object o) {
 95         return UNSAFE.getIntUnaligned(o, 17);
 96     }
 97 
 98     static Object testFixedBase(long off) {
 99         return UNSAFE.getReference(INSTANCE, off);
100     }
101 
102     static Object testOpaque(Object o, long off) {
103         return UNSAFE.getReference(o, off);
104     }
105 
106     static int testFixedOffsetHeaderArray0(Object[] arr) {
107         return UNSAFE.getInt(arr, 0);
108     }
109 
110     static int testFixedOffsetHeaderArray4(Object[] arr) {
111         return UNSAFE.getInt(arr, 4);
112     }
113 
114     static int testFixedOffsetHeaderArray8(Object[] arr) {
115         return UNSAFE.getInt(arr, 8);
116     }
117 
118     static int testFixedOffsetHeaderArray12(Object[] arr) {
119         return UNSAFE.getInt(arr, 12);
120     }
121 
122     static int testFixedOffsetHeaderArray16(Object[] arr) {
123         return UNSAFE.getInt(arr, 16);
124     }
125 
126     static int testFixedOffsetHeaderArray17(Object[] arr) {
127         return UNSAFE.getIntUnaligned(arr, 17);
128     }
129 
130     static Object testFixedOffsetArray(Object[] arr) {
131         return UNSAFE.getReference(arr, E_OFFSET);
132     }
133 
134     static Object testFixedBaseArray(long off) {
135         return UNSAFE.getReference(ARRAY, off);
136     }
137 
138     static Object testOpaqueArray(Object[] o, long off) {
139         return UNSAFE.getReference(o, off);
140     }
141 
142     static final long ADDR = UNSAFE.allocateMemory(10);
143     static boolean flag;
144 
145     static int testMixedAccess() {
146         flag = !flag;
147         Object o = (flag ? INSTANCE : null);
148         long off = (flag ? F_OFFSET : ADDR);
149         return UNSAFE.getInt(o, off);
150     }
151 
152     public static void main(String[] args) {
153         for (int i = 0; i < 20_000; i++) {
154             // Instance
155             testFixedOffsetField(INSTANCE);
156             testFixedOffsetHeader0(INSTANCE);
157             testFixedOffsetHeader4(INSTANCE);
158             testFixedOffsetHeader8(INSTANCE);
159             testFixedOffsetHeader12(INSTANCE);
160             testFixedOffsetHeader16(INSTANCE);
161             testFixedOffsetHeader17(INSTANCE);
162             testFixedBase(F_OFFSET);
163             testOpaque(INSTANCE, F_OFFSET);
164             testMixedAccess();
165 
166             // Array
167             testFixedOffsetHeaderArray0(ARRAY);
168             testFixedOffsetHeaderArray4(ARRAY);
169             testFixedOffsetHeaderArray8(ARRAY);
170             testFixedOffsetHeaderArray12(ARRAY);
171             testFixedOffsetHeaderArray16(ARRAY);
172             testFixedOffsetHeaderArray17(ARRAY);
173             testFixedOffsetArray(ARRAY);
174             testFixedBaseArray(E_OFFSET);
175             testOpaqueArray(ARRAY, E_OFFSET);
176         }
177         System.out.println("TEST PASSED");
178     }
179 }
--- EOF ---