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 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 }