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:+UnlockDiagnosticVMOptions -XX:-TieredCompilation -Xbatch 32 * -XX:+PrintCompilation -XX:+PrintInlining 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 long F_OFFSET; 48 static { 49 try { 50 Field field = OpaqueAccesses.class.getDeclaredField("f"); 51 F_OFFSET = UNSAFE.objectFieldOffset(field); 52 } catch (NoSuchFieldException e) { 53 throw new Error(e); 54 } 55 } 56 57 private Object f = new Object(); 58 59 static Object testFixedOffsetField(Object o) { 60 return UNSAFE.getObject(o, F_OFFSET); 61 } 62 63 static int testFixedOffsetHeader(Object o) { 64 return UNSAFE.getInt(o, 4); 65 } 66 67 static Object testFixedBase(long off) { 68 return UNSAFE.getObject(INSTANCE, off); 69 } 70 71 static Object testOpaque(Object o, long off) { 72 return UNSAFE.getObject(o, off); 73 } 74 75 static final long ADDR = UNSAFE.allocateMemory(10); 76 static boolean flag; 77 78 static int testMixedAccess() { 79 flag = !flag; 80 Object o = (flag ? INSTANCE : null); 81 long off = (flag ? F_OFFSET : ADDR); 82 return UNSAFE.getInt(o, off); 83 } 84 85 public static void main(String[] args) { 86 for (int i = 0; i < 20_000; i++) { 87 testFixedOffsetField(INSTANCE); 88 testFixedOffsetHeader(INSTANCE); 89 testFixedBase(F_OFFSET); 90 testOpaque(INSTANCE, F_OFFSET); 91 testMixedAccess(); 92 } 93 System.out.println("TEST PASSED"); 94 } 95 }