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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 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 jdk.internal.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 int testFixedOffsetHeader17(Object o) { 107 return UNSAFE.getIntUnaligned(o, 17); 108 } 109 110 static Object testFixedBase(long off) { 111 return UNSAFE.getObject(INSTANCE, off); 112 } 113 114 static Object testOpaque(Object o, long off) { 115 return UNSAFE.getObject(o, off); 116 } 117 118 static int testFixedOffsetHeaderArray0(Object[] arr) { 119 return UNSAFE.getInt(arr, 0); 120 } 121 122 static int testFixedOffsetHeaderArray4(Object[] arr) { 123 return UNSAFE.getInt(arr, 4); 124 } 125 126 static int testFixedOffsetHeaderArray8(Object[] arr) { 127 return UNSAFE.getInt(arr, 8); 128 } 129 130 static int testFixedOffsetHeaderArray12(Object[] arr) { 131 return UNSAFE.getInt(arr, 12); 132 } 133 134 static int testFixedOffsetHeaderArray16(Object[] arr) { 135 return UNSAFE.getInt(arr, 16); 136 } 137 138 static int testFixedOffsetHeaderArray17(Object[] arr) { 139 return UNSAFE.getIntUnaligned(arr, 17); 140 } 141 142 static Object testFixedOffsetArray(Object[] arr) { 143 return UNSAFE.getObject(arr, E_OFFSET); 144 } 145 146 static Object testFixedBaseArray(long off) { 147 return UNSAFE.getObject(ARRAY, off); 148 } 149 150 static Object testOpaqueArray(Object[] o, long off) { 151 return UNSAFE.getObject(o, off); 152 } 153 154 static final long ADDR = UNSAFE.allocateMemory(10); 155 static boolean flag; 156 157 static int testMixedAccess() { 158 flag = !flag; 159 Object o = (flag ? INSTANCE : null); 160 long off = (flag ? F_OFFSET : ADDR); 161 return UNSAFE.getInt(o, off); 162 } 163 164 public static void main(String[] args) { 165 for (int i = 0; i < 20_000; i++) { 166 // Instance 167 testFixedOffsetField(INSTANCE); 168 testFixedOffsetHeader0(INSTANCE); 169 testFixedOffsetHeader4(INSTANCE); 170 testFixedOffsetHeader8(INSTANCE); 171 testFixedOffsetHeader12(INSTANCE); 172 testFixedOffsetHeader16(INSTANCE); 173 testFixedOffsetHeader17(INSTANCE); 174 testFixedBase(F_OFFSET); 175 testOpaque(INSTANCE, F_OFFSET); 176 testMixedAccess(); 177 178 // Array 179 testFixedOffsetHeaderArray0(ARRAY); 180 testFixedOffsetHeaderArray4(ARRAY); 181 testFixedOffsetHeaderArray8(ARRAY); 182 testFixedOffsetHeaderArray12(ARRAY); 183 testFixedOffsetHeaderArray16(ARRAY); 184 testFixedOffsetHeaderArray17(ARRAY); 185 testFixedOffsetArray(ARRAY); 186 testFixedBaseArray(E_OFFSET); 187 testOpaqueArray(ARRAY, E_OFFSET); 188 } 189 System.out.println("TEST PASSED"); 190 } 191 }