13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 /*
27 * @test
28 * @bug 8155781
29 * @modules java.base/jdk.internal.misc
30 *
31 * @run main/bootclasspath/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockDiagnosticVMOptions
32 * -XX:-TieredCompilation -Xbatch
33 * -XX:CompileCommand=dontinline,compiler.unsafe.OpaqueAccesses::test*
34 * compiler.unsafe.OpaqueAccesses
35 */
36 package compiler.unsafe;
37
38 import jdk.internal.misc.Unsafe;
39
40 import java.lang.reflect.Field;
41
42 public class OpaqueAccesses {
43 private static final Unsafe UNSAFE = Unsafe.getUnsafe();
44
45 private static final Object INSTANCE = new OpaqueAccesses();
46
47 private static final Object[] ARRAY = new Object[10];
48
49 private static final long F_OFFSET;
50 private static final long E_OFFSET;
51
52 static {
53 try {
54 Field field = OpaqueAccesses.class.getDeclaredField("f");
55 F_OFFSET = UNSAFE.objectFieldOffset(field);
56
57 E_OFFSET = UNSAFE.arrayBaseOffset(ARRAY.getClass());
58 } catch (NoSuchFieldException e) {
59 throw new Error(e);
60 }
61 }
62
63 private Object f = new Object();
64
65 static Object testFixedOffsetField(Object o) {
66 return UNSAFE.getObject(o, F_OFFSET);
67 }
68
69 static int testFixedOffsetHeader0(Object o) {
70 return UNSAFE.getInt(o, 0);
71 }
72
73 static int testFixedOffsetHeader4(Object o) {
74 return UNSAFE.getInt(o, 4);
75 }
76
77 static Object testFixedBase(long off) {
78 return UNSAFE.getObject(INSTANCE, off);
79 }
80
81 static Object testOpaque(Object o, long off) {
82 return UNSAFE.getObject(o, off);
83 }
84
85 static int testFixedOffsetHeaderArray0(Object[] arr) {
86 return UNSAFE.getInt(arr, 0);
87 }
88
89 static int testFixedOffsetHeaderArray4(Object[] arr) {
90 return UNSAFE.getInt(arr, 4);
91 }
92
93 static Object testFixedOffsetArray(Object[] arr) {
94 return UNSAFE.getObject(arr, E_OFFSET);
95 }
96
97 static Object testFixedBaseArray(long off) {
98 return UNSAFE.getObject(ARRAY, off);
99 }
100
101 static Object testOpaqueArray(Object[] o, long off) {
102 return UNSAFE.getObject(o, off);
103 }
104
105 static final long ADDR = UNSAFE.allocateMemory(10);
106 static boolean flag;
107
108 static int testMixedAccess() {
109 flag = !flag;
110 Object o = (flag ? INSTANCE : null);
111 long off = (flag ? F_OFFSET : ADDR);
112 return UNSAFE.getInt(o, off);
113 }
114
115 public static void main(String[] args) {
116 for (int i = 0; i < 20_000; i++) {
117 // Instance
118 testFixedOffsetField(INSTANCE);
119 testFixedOffsetHeader0(INSTANCE);
120 testFixedOffsetHeader4(INSTANCE);
121 testFixedBase(F_OFFSET);
122 testOpaque(INSTANCE, F_OFFSET);
123 testMixedAccess();
124
125 // Array
126 testFixedOffsetHeaderArray0(ARRAY);
127 testFixedOffsetHeaderArray4(ARRAY);
128 testFixedOffsetArray(ARRAY);
129 testFixedBaseArray(E_OFFSET);
130 testOpaqueArray(ARRAY, E_OFFSET);
131 }
132 System.out.println("TEST PASSED");
133 }
134 }
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 /*
27 * @test
28 * @bug 8155781
29 * @modules java.base/jdk.internal.misc
30 *
31 * @run main/bootclasspath/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockDiagnosticVMOptions
32 * -XX:-TieredCompilation -Xbatch
33 * -XX:+UseCompressedOops -XX:+UseCompressedClassPointers
34 * -XX:CompileCommand=dontinline,compiler.unsafe.OpaqueAccesses::test*
35 * compiler.unsafe.OpaqueAccesses
36 * @run main/bootclasspath/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockDiagnosticVMOptions
37 * -XX:-TieredCompilation -Xbatch
38 * -XX:+UseCompressedOops -XX:-UseCompressedClassPointers
39 * -XX:CompileCommand=dontinline,compiler.unsafe.OpaqueAccesses::test*
40 * compiler.unsafe.OpaqueAccesses
41 * @run main/bootclasspath/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockDiagnosticVMOptions
42 * -XX:-TieredCompilation -Xbatch
43 * -XX:-UseCompressedOops -XX:+UseCompressedClassPointers
44 * -XX:CompileCommand=dontinline,compiler.unsafe.OpaqueAccesses::test*
45 * compiler.unsafe.OpaqueAccesses
46 * @run main/bootclasspath/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockDiagnosticVMOptions
47 * -XX:-TieredCompilation -Xbatch
48 * -XX:-UseCompressedOops -XX:-UseCompressedClassPointers
49 * -XX:CompileCommand=dontinline,compiler.unsafe.OpaqueAccesses::test*
50 * compiler.unsafe.OpaqueAccesses
51 */
52 package compiler.unsafe;
53
54 import sun.misc.Unsafe;
55
56 import java.lang.reflect.Field;
57
58 public class OpaqueAccesses {
59 private static final Unsafe UNSAFE = Unsafe.getUnsafe();
60
61 private static final Object INSTANCE = new OpaqueAccesses();
62
63 private static final Object[] ARRAY = new Object[10];
64
65 private static final long F_OFFSET;
66 private static final long E_OFFSET;
67
68 static {
69 try {
70 Field field = OpaqueAccesses.class.getDeclaredField("f");
71 F_OFFSET = UNSAFE.objectFieldOffset(field);
72
73 E_OFFSET = UNSAFE.arrayBaseOffset(ARRAY.getClass());
74 } catch (NoSuchFieldException e) {
75 throw new Error(e);
76 }
77 }
78
79 private Object f = new Object();
80 private long l1, l2;
81
82 static Object testFixedOffsetField(Object o) {
83 return UNSAFE.getObject(o, F_OFFSET);
84 }
85
86 static int testFixedOffsetHeader0(Object o) {
87 return UNSAFE.getInt(o, 0);
88 }
89
90 static int testFixedOffsetHeader4(Object o) {
91 return UNSAFE.getInt(o, 4);
92 }
93
94 static int testFixedOffsetHeader8(Object o) {
95 return UNSAFE.getInt(o, 8);
96 }
97
98 static int testFixedOffsetHeader12(Object o) {
99 return UNSAFE.getInt(o, 12);
100 }
101
102 static int testFixedOffsetHeader16(Object o) {
103 return UNSAFE.getInt(o, 16);
104 }
105
106 static Object testFixedBase(long off) {
107 return UNSAFE.getObject(INSTANCE, off);
108 }
109
110 static Object testOpaque(Object o, long off) {
111 return UNSAFE.getObject(o, off);
112 }
113
114 static int testFixedOffsetHeaderArray0(Object[] arr) {
115 return UNSAFE.getInt(arr, 0);
116 }
117
118 static int testFixedOffsetHeaderArray4(Object[] arr) {
119 return UNSAFE.getInt(arr, 4);
120 }
121
122 static int testFixedOffsetHeaderArray8(Object[] arr) {
123 return UNSAFE.getInt(arr, 8);
124 }
125
126 static int testFixedOffsetHeaderArray12(Object[] arr) {
127 return UNSAFE.getInt(arr, 12);
128 }
129
130 static int testFixedOffsetHeaderArray16(Object[] arr) {
131 return UNSAFE.getInt(arr, 16);
132 }
133
134 static Object testFixedOffsetArray(Object[] arr) {
135 return UNSAFE.getObject(arr, E_OFFSET);
136 }
137
138 static Object testFixedBaseArray(long off) {
139 return UNSAFE.getObject(ARRAY, off);
140 }
141
142 static Object testOpaqueArray(Object[] o, long off) {
143 return UNSAFE.getObject(o, off);
144 }
145
146 static final long ADDR = UNSAFE.allocateMemory(10);
147 static boolean flag;
148
149 static int testMixedAccess() {
150 flag = !flag;
151 Object o = (flag ? INSTANCE : null);
152 long off = (flag ? F_OFFSET : ADDR);
153 return UNSAFE.getInt(o, off);
154 }
155
156 public static void main(String[] args) {
157 for (int i = 0; i < 20_000; i++) {
158 // Instance
159 testFixedOffsetField(INSTANCE);
160 testFixedOffsetHeader0(INSTANCE);
161 testFixedOffsetHeader4(INSTANCE);
162 testFixedOffsetHeader8(INSTANCE);
163 testFixedOffsetHeader12(INSTANCE);
164 testFixedOffsetHeader16(INSTANCE);
165 testFixedBase(F_OFFSET);
166 testOpaque(INSTANCE, F_OFFSET);
167 testMixedAccess();
168
169 // Array
170 testFixedOffsetHeaderArray0(ARRAY);
171 testFixedOffsetHeaderArray4(ARRAY);
172 testFixedOffsetHeaderArray8(ARRAY);
173 testFixedOffsetHeaderArray12(ARRAY);
174 testFixedOffsetHeaderArray16(ARRAY);
175 testFixedOffsetArray(ARRAY);
176 testFixedBaseArray(E_OFFSET);
177 testOpaqueArray(ARRAY, E_OFFSET);
178 }
179 System.out.println("TEST PASSED");
180 }
181 }
|