shshahma@8655: /* shshahma@8655: * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. shshahma@8655: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. shshahma@8655: * shshahma@8655: * This code is free software; you can redistribute it and/or modify it shshahma@8655: * under the terms of the GNU General Public License version 2 only, as shshahma@8655: * published by the Free Software Foundation. Oracle designates this shshahma@8655: * particular file as subject to the "Classpath" exception as provided shshahma@8655: * by Oracle in the LICENSE file that accompanied this code. shshahma@8655: * shshahma@8655: * This code is distributed in the hope that it will be useful, but WITHOUT shshahma@8655: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or shshahma@8655: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License shshahma@8655: * version 2 for more details (a copy is included in the LICENSE file that shshahma@8655: * accompanied this code). shshahma@8655: * shshahma@8655: * You should have received a copy of the GNU General Public License version shshahma@8655: * 2 along with this work; if not, write to the Free Software Foundation, shshahma@8655: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. shshahma@8655: * shshahma@8655: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA shshahma@8655: * or visit www.oracle.com if you need additional information or have any shshahma@8655: * questions. shshahma@8655: */ shshahma@8655: shshahma@8655: /* shshahma@8655: * @test shshahma@8655: * @bug 8155781 shshahma@8655: * @modules java.base/jdk.internal.misc shshahma@8655: * shshahma@8655: * @run main/bootclasspath/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockDiagnosticVMOptions shshahma@8655: * -XX:-TieredCompilation -Xbatch shshahma@8655: * -XX:CompileCommand=dontinline,compiler.unsafe.OpaqueAccesses::test* shshahma@8655: * compiler.unsafe.OpaqueAccesses shshahma@8655: */ shshahma@8655: package compiler.unsafe; shshahma@8655: shshahma@8655: import sun.misc.Unsafe; shshahma@8655: shshahma@8655: import java.lang.reflect.Field; shshahma@8655: shshahma@8655: public class OpaqueAccesses { shshahma@8655: private static final Unsafe UNSAFE = Unsafe.getUnsafe(); shshahma@8655: shshahma@8655: private static final Object INSTANCE = new OpaqueAccesses(); shshahma@8655: shshahma@8655: private static final Object[] ARRAY = new Object[10]; shshahma@8655: shshahma@8655: private static final long F_OFFSET; shshahma@8655: private static final long E_OFFSET; shshahma@8655: shshahma@8655: static { shshahma@8655: try { shshahma@8655: Field field = OpaqueAccesses.class.getDeclaredField("f"); shshahma@8655: F_OFFSET = UNSAFE.objectFieldOffset(field); shshahma@8655: shshahma@8655: E_OFFSET = UNSAFE.arrayBaseOffset(ARRAY.getClass()); shshahma@8655: } catch (NoSuchFieldException e) { shshahma@8655: throw new Error(e); shshahma@8655: } shshahma@8655: } shshahma@8655: shshahma@8655: private Object f = new Object(); shshahma@8655: shshahma@8655: static Object testFixedOffsetField(Object o) { shshahma@8655: return UNSAFE.getObject(o, F_OFFSET); shshahma@8655: } shshahma@8655: shshahma@8655: static int testFixedOffsetHeader0(Object o) { shshahma@8655: return UNSAFE.getInt(o, 0); shshahma@8655: } shshahma@8655: shshahma@8655: static int testFixedOffsetHeader4(Object o) { shshahma@8655: return UNSAFE.getInt(o, 4); shshahma@8655: } shshahma@8655: shshahma@8655: static Object testFixedBase(long off) { shshahma@8655: return UNSAFE.getObject(INSTANCE, off); shshahma@8655: } shshahma@8655: shshahma@8655: static Object testOpaque(Object o, long off) { shshahma@8655: return UNSAFE.getObject(o, off); shshahma@8655: } shshahma@8655: shshahma@8655: static int testFixedOffsetHeaderArray0(Object[] arr) { shshahma@8655: return UNSAFE.getInt(arr, 0); shshahma@8655: } shshahma@8655: shshahma@8655: static int testFixedOffsetHeaderArray4(Object[] arr) { shshahma@8655: return UNSAFE.getInt(arr, 4); shshahma@8655: } shshahma@8655: shshahma@8655: static Object testFixedOffsetArray(Object[] arr) { shshahma@8655: return UNSAFE.getObject(arr, E_OFFSET); shshahma@8655: } shshahma@8655: shshahma@8655: static Object testFixedBaseArray(long off) { shshahma@8655: return UNSAFE.getObject(ARRAY, off); shshahma@8655: } shshahma@8655: shshahma@8655: static Object testOpaqueArray(Object[] o, long off) { shshahma@8655: return UNSAFE.getObject(o, off); shshahma@8655: } shshahma@8655: shshahma@8655: static final long ADDR = UNSAFE.allocateMemory(10); shshahma@8655: static boolean flag; shshahma@8655: shshahma@8655: static int testMixedAccess() { shshahma@8655: flag = !flag; shshahma@8655: Object o = (flag ? INSTANCE : null); shshahma@8655: long off = (flag ? F_OFFSET : ADDR); shshahma@8655: return UNSAFE.getInt(o, off); shshahma@8655: } shshahma@8655: shshahma@8655: public static void main(String[] args) { shshahma@8655: for (int i = 0; i < 20_000; i++) { shshahma@8655: // Instance shshahma@8655: testFixedOffsetField(INSTANCE); shshahma@8655: testFixedOffsetHeader0(INSTANCE); shshahma@8655: testFixedOffsetHeader4(INSTANCE); shshahma@8655: testFixedBase(F_OFFSET); shshahma@8655: testOpaque(INSTANCE, F_OFFSET); shshahma@8655: testMixedAccess(); shshahma@8655: shshahma@8655: // Array shshahma@8655: testFixedOffsetHeaderArray0(ARRAY); shshahma@8655: testFixedOffsetHeaderArray4(ARRAY); shshahma@8655: testFixedOffsetArray(ARRAY); shshahma@8655: testFixedBaseArray(E_OFFSET); shshahma@8655: testOpaqueArray(ARRAY, E_OFFSET); shshahma@8655: } shshahma@8655: System.out.println("TEST PASSED"); shshahma@8655: } shshahma@8655: }