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