test/compiler/unsafe/OpaqueAccesses.java

changeset 8655
0de3b29d549d
child 8656
7ca49bca3c2a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/compiler/unsafe/OpaqueAccesses.java	Wed Nov 23 23:10:03 2016 -0800
     1.3 @@ -0,0 +1,134 @@
     1.4 +/*
     1.5 + * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +/*
    1.30 + * @test
    1.31 + * @bug 8155781
    1.32 + * @modules java.base/jdk.internal.misc
    1.33 + *
    1.34 + * @run main/bootclasspath/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockDiagnosticVMOptions
    1.35 + *                                 -XX:-TieredCompilation -Xbatch
    1.36 + *                                 -XX:CompileCommand=dontinline,compiler.unsafe.OpaqueAccesses::test*
    1.37 + *                                 compiler.unsafe.OpaqueAccesses
    1.38 + */
    1.39 +package compiler.unsafe;
    1.40 +
    1.41 +import sun.misc.Unsafe;
    1.42 +
    1.43 +import java.lang.reflect.Field;
    1.44 +
    1.45 +public class OpaqueAccesses {
    1.46 +    private static final Unsafe UNSAFE = Unsafe.getUnsafe();
    1.47 +
    1.48 +    private static final Object INSTANCE = new OpaqueAccesses();
    1.49 +
    1.50 +    private static final Object[] ARRAY = new Object[10];
    1.51 +
    1.52 +    private static final long F_OFFSET;
    1.53 +    private static final long E_OFFSET;
    1.54 +
    1.55 +    static {
    1.56 +        try {
    1.57 +            Field field = OpaqueAccesses.class.getDeclaredField("f");
    1.58 +            F_OFFSET = UNSAFE.objectFieldOffset(field);
    1.59 +
    1.60 +            E_OFFSET = UNSAFE.arrayBaseOffset(ARRAY.getClass());
    1.61 +        } catch (NoSuchFieldException e) {
    1.62 +            throw new Error(e);
    1.63 +        }
    1.64 +    }
    1.65 +
    1.66 +    private Object f = new Object();
    1.67 +
    1.68 +    static Object testFixedOffsetField(Object o) {
    1.69 +        return UNSAFE.getObject(o, F_OFFSET);
    1.70 +    }
    1.71 +
    1.72 +    static int testFixedOffsetHeader0(Object o) {
    1.73 +        return UNSAFE.getInt(o, 0);
    1.74 +    }
    1.75 +
    1.76 +    static int testFixedOffsetHeader4(Object o) {
    1.77 +        return UNSAFE.getInt(o, 4);
    1.78 +    }
    1.79 +
    1.80 +    static Object testFixedBase(long off) {
    1.81 +        return UNSAFE.getObject(INSTANCE, off);
    1.82 +    }
    1.83 +
    1.84 +    static Object testOpaque(Object o, long off) {
    1.85 +        return UNSAFE.getObject(o, off);
    1.86 +    }
    1.87 +
    1.88 +    static int testFixedOffsetHeaderArray0(Object[] arr) {
    1.89 +        return UNSAFE.getInt(arr, 0);
    1.90 +    }
    1.91 +
    1.92 +    static int testFixedOffsetHeaderArray4(Object[] arr) {
    1.93 +        return UNSAFE.getInt(arr, 4);
    1.94 +    }
    1.95 +
    1.96 +    static Object testFixedOffsetArray(Object[] arr) {
    1.97 +        return UNSAFE.getObject(arr, E_OFFSET);
    1.98 +    }
    1.99 +
   1.100 +    static Object testFixedBaseArray(long off) {
   1.101 +        return UNSAFE.getObject(ARRAY, off);
   1.102 +    }
   1.103 +
   1.104 +    static Object testOpaqueArray(Object[] o, long off) {
   1.105 +        return UNSAFE.getObject(o, off);
   1.106 +    }
   1.107 +
   1.108 +    static final long ADDR = UNSAFE.allocateMemory(10);
   1.109 +    static boolean flag;
   1.110 +
   1.111 +    static int testMixedAccess() {
   1.112 +        flag = !flag;
   1.113 +        Object o = (flag ? INSTANCE : null);
   1.114 +        long off = (flag ? F_OFFSET : ADDR);
   1.115 +        return UNSAFE.getInt(o, off);
   1.116 +    }
   1.117 +
   1.118 +    public static void main(String[] args) {
   1.119 +        for (int i = 0; i < 20_000; i++) {
   1.120 +            // Instance
   1.121 +            testFixedOffsetField(INSTANCE);
   1.122 +            testFixedOffsetHeader0(INSTANCE);
   1.123 +            testFixedOffsetHeader4(INSTANCE);
   1.124 +            testFixedBase(F_OFFSET);
   1.125 +            testOpaque(INSTANCE, F_OFFSET);
   1.126 +            testMixedAccess();
   1.127 +
   1.128 +            // Array
   1.129 +            testFixedOffsetHeaderArray0(ARRAY);
   1.130 +            testFixedOffsetHeaderArray4(ARRAY);
   1.131 +            testFixedOffsetArray(ARRAY);
   1.132 +            testFixedBaseArray(E_OFFSET);
   1.133 +            testOpaqueArray(ARRAY, E_OFFSET);
   1.134 +        }
   1.135 +        System.out.println("TEST PASSED");
   1.136 +    }
   1.137 +}

mercurial