test/compiler/unsafe/TestRawAliasing.java

Tue, 25 Jul 2017 10:10:41 -0400

author
dbuck
date
Tue, 25 Jul 2017 10:10:41 -0400
changeset 8879
6bc9abf210fd
permissions
-rw-r--r--

8178047: Aliasing problem with raw memory accesses
Summary: Require equal bases when unaliasing offsets for raw accesses
Reviewed-by: thartmann, kvn

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

mercurial