test/compiler/unsafe/OpaqueAccesses.java

Wed, 23 Nov 2016 23:10:03 -0800

author
shshahma
date
Wed, 23 Nov 2016 23:10:03 -0800
changeset 8655
0de3b29d549d
child 8656
7ca49bca3c2a
permissions
-rw-r--r--

8155781: C2: opaque unsafe access triggers an assert
Reviewed-by: kvn, thartmann

shshahma@8655 1 /*
shshahma@8655 2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
shshahma@8655 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
shshahma@8655 4 *
shshahma@8655 5 * This code is free software; you can redistribute it and/or modify it
shshahma@8655 6 * under the terms of the GNU General Public License version 2 only, as
shshahma@8655 7 * published by the Free Software Foundation. Oracle designates this
shshahma@8655 8 * particular file as subject to the "Classpath" exception as provided
shshahma@8655 9 * by Oracle in the LICENSE file that accompanied this code.
shshahma@8655 10 *
shshahma@8655 11 * This code is distributed in the hope that it will be useful, but WITHOUT
shshahma@8655 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
shshahma@8655 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
shshahma@8655 14 * version 2 for more details (a copy is included in the LICENSE file that
shshahma@8655 15 * accompanied this code).
shshahma@8655 16 *
shshahma@8655 17 * You should have received a copy of the GNU General Public License version
shshahma@8655 18 * 2 along with this work; if not, write to the Free Software Foundation,
shshahma@8655 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
shshahma@8655 20 *
shshahma@8655 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
shshahma@8655 22 * or visit www.oracle.com if you need additional information or have any
shshahma@8655 23 * questions.
shshahma@8655 24 */
shshahma@8655 25
shshahma@8655 26 /*
shshahma@8655 27 * @test
shshahma@8655 28 * @bug 8155781
shshahma@8655 29 * @modules java.base/jdk.internal.misc
shshahma@8655 30 *
shshahma@8655 31 * @run main/bootclasspath/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockDiagnosticVMOptions
shshahma@8655 32 * -XX:-TieredCompilation -Xbatch
shshahma@8655 33 * -XX:CompileCommand=dontinline,compiler.unsafe.OpaqueAccesses::test*
shshahma@8655 34 * compiler.unsafe.OpaqueAccesses
shshahma@8655 35 */
shshahma@8655 36 package compiler.unsafe;
shshahma@8655 37
shshahma@8655 38 import sun.misc.Unsafe;
shshahma@8655 39
shshahma@8655 40 import java.lang.reflect.Field;
shshahma@8655 41
shshahma@8655 42 public class OpaqueAccesses {
shshahma@8655 43 private static final Unsafe UNSAFE = Unsafe.getUnsafe();
shshahma@8655 44
shshahma@8655 45 private static final Object INSTANCE = new OpaqueAccesses();
shshahma@8655 46
shshahma@8655 47 private static final Object[] ARRAY = new Object[10];
shshahma@8655 48
shshahma@8655 49 private static final long F_OFFSET;
shshahma@8655 50 private static final long E_OFFSET;
shshahma@8655 51
shshahma@8655 52 static {
shshahma@8655 53 try {
shshahma@8655 54 Field field = OpaqueAccesses.class.getDeclaredField("f");
shshahma@8655 55 F_OFFSET = UNSAFE.objectFieldOffset(field);
shshahma@8655 56
shshahma@8655 57 E_OFFSET = UNSAFE.arrayBaseOffset(ARRAY.getClass());
shshahma@8655 58 } catch (NoSuchFieldException e) {
shshahma@8655 59 throw new Error(e);
shshahma@8655 60 }
shshahma@8655 61 }
shshahma@8655 62
shshahma@8655 63 private Object f = new Object();
shshahma@8655 64
shshahma@8655 65 static Object testFixedOffsetField(Object o) {
shshahma@8655 66 return UNSAFE.getObject(o, F_OFFSET);
shshahma@8655 67 }
shshahma@8655 68
shshahma@8655 69 static int testFixedOffsetHeader0(Object o) {
shshahma@8655 70 return UNSAFE.getInt(o, 0);
shshahma@8655 71 }
shshahma@8655 72
shshahma@8655 73 static int testFixedOffsetHeader4(Object o) {
shshahma@8655 74 return UNSAFE.getInt(o, 4);
shshahma@8655 75 }
shshahma@8655 76
shshahma@8655 77 static Object testFixedBase(long off) {
shshahma@8655 78 return UNSAFE.getObject(INSTANCE, off);
shshahma@8655 79 }
shshahma@8655 80
shshahma@8655 81 static Object testOpaque(Object o, long off) {
shshahma@8655 82 return UNSAFE.getObject(o, off);
shshahma@8655 83 }
shshahma@8655 84
shshahma@8655 85 static int testFixedOffsetHeaderArray0(Object[] arr) {
shshahma@8655 86 return UNSAFE.getInt(arr, 0);
shshahma@8655 87 }
shshahma@8655 88
shshahma@8655 89 static int testFixedOffsetHeaderArray4(Object[] arr) {
shshahma@8655 90 return UNSAFE.getInt(arr, 4);
shshahma@8655 91 }
shshahma@8655 92
shshahma@8655 93 static Object testFixedOffsetArray(Object[] arr) {
shshahma@8655 94 return UNSAFE.getObject(arr, E_OFFSET);
shshahma@8655 95 }
shshahma@8655 96
shshahma@8655 97 static Object testFixedBaseArray(long off) {
shshahma@8655 98 return UNSAFE.getObject(ARRAY, off);
shshahma@8655 99 }
shshahma@8655 100
shshahma@8655 101 static Object testOpaqueArray(Object[] o, long off) {
shshahma@8655 102 return UNSAFE.getObject(o, off);
shshahma@8655 103 }
shshahma@8655 104
shshahma@8655 105 static final long ADDR = UNSAFE.allocateMemory(10);
shshahma@8655 106 static boolean flag;
shshahma@8655 107
shshahma@8655 108 static int testMixedAccess() {
shshahma@8655 109 flag = !flag;
shshahma@8655 110 Object o = (flag ? INSTANCE : null);
shshahma@8655 111 long off = (flag ? F_OFFSET : ADDR);
shshahma@8655 112 return UNSAFE.getInt(o, off);
shshahma@8655 113 }
shshahma@8655 114
shshahma@8655 115 public static void main(String[] args) {
shshahma@8655 116 for (int i = 0; i < 20_000; i++) {
shshahma@8655 117 // Instance
shshahma@8655 118 testFixedOffsetField(INSTANCE);
shshahma@8655 119 testFixedOffsetHeader0(INSTANCE);
shshahma@8655 120 testFixedOffsetHeader4(INSTANCE);
shshahma@8655 121 testFixedBase(F_OFFSET);
shshahma@8655 122 testOpaque(INSTANCE, F_OFFSET);
shshahma@8655 123 testMixedAccess();
shshahma@8655 124
shshahma@8655 125 // Array
shshahma@8655 126 testFixedOffsetHeaderArray0(ARRAY);
shshahma@8655 127 testFixedOffsetHeaderArray4(ARRAY);
shshahma@8655 128 testFixedOffsetArray(ARRAY);
shshahma@8655 129 testFixedBaseArray(E_OFFSET);
shshahma@8655 130 testOpaqueArray(ARRAY, E_OFFSET);
shshahma@8655 131 }
shshahma@8655 132 System.out.println("TEST PASSED");
shshahma@8655 133 }
shshahma@8655 134 }

mercurial