test/hotspot/jtreg/compiler/unsafe/TestRawAliasing.java
author iignatyev
Mon, 18 Feb 2019 09:31:07 -0800
changeset 53791 bec6c8739833
parent 47216 71c04702a3d5
permissions -rw-r--r--
8219157: vm/mlvm/mixed/stress/java/findDeadlock should be problem-listed only on mac Reviewed-by: thartmann
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
44739
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
     1
/*
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
     4
 *
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
     7
 * published by the Free Software Foundation.
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
     8
 *
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    13
 * accompanied this code).
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    14
 *
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    18
 *
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    21
 * questions.
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    22
 */
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    23
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    24
/*
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    25
 * @test
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    26
 * @bug 8178047
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    27
 * @run main/othervm -XX:CompileCommand=exclude,*.main -XX:-TieredCompilation -XX:-BackgroundCompilation compiler.unsafe.TestRawAliasing
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    28
 * @modules java.base/jdk.internal.misc:+open
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    29
 */
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    30
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    31
package compiler.unsafe;
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    32
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    33
import java.lang.reflect.Field;
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    34
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    35
public class TestRawAliasing {
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    36
    static private final jdk.internal.misc.Unsafe UNSAFE;
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    37
    static {
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    38
        try {
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    39
            Field f = jdk.internal.misc.Unsafe.class.getDeclaredField("theUnsafe");
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    40
            f.setAccessible(true);
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    41
            UNSAFE = (jdk.internal.misc.Unsafe) f.get(null);
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    42
        } catch (Exception e) {
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    43
            throw new RuntimeException("Unable to get Unsafe instance.", e);
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    44
        }
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    45
    }
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    46
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    47
    static private final int OFFSET_X = 50;
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    48
    static private final int OFFSET_Y = 100;
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    49
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    50
    private static int test(long base_plus_offset_x, long base_plus_offset_y, int magic_value) {
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    51
        // write 0 to a location
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    52
        UNSAFE.putByte(base_plus_offset_x - OFFSET_X, (byte)0);
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    53
        // write unfoldable value to really the same location with another base
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    54
        UNSAFE.putByte(base_plus_offset_y - OFFSET_Y, (byte)magic_value);
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    55
        // read the value back, should be equal to "unfoldable_value"
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    56
        return UNSAFE.getByte(base_plus_offset_x - OFFSET_X);
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    57
    }
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    58
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    59
    private static final int OFF_HEAP_AREA_SIZE = 128;
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    60
    private static final byte MAGIC = 123;
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    61
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    62
    // main is excluded from compilation since we don't want the test method to inline and make base values fold
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    63
    public static void main(String... args) {
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    64
        long base = UNSAFE.allocateMemory(OFF_HEAP_AREA_SIZE);
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    65
        for (int i = 0; i < 100_000; i++) {
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    66
            if (test(base + OFFSET_X, base + OFFSET_Y, MAGIC) != MAGIC) {
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    67
                throw new RuntimeException("Unexpected magic value");
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    68
            }
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    69
        }
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    70
    }
3af2c3740d7b 8178047: Aliasing problem with raw memory accesses
iveresov
parents:
diff changeset
    71
}