dbuck@8879: /* dbuck@8879: * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. dbuck@8879: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. dbuck@8879: * dbuck@8879: * This code is free software; you can redistribute it and/or modify it dbuck@8879: * under the terms of the GNU General Public License version 2 only, as dbuck@8879: * published by the Free Software Foundation. dbuck@8879: * dbuck@8879: * This code is distributed in the hope that it will be useful, but WITHOUT dbuck@8879: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or dbuck@8879: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License dbuck@8879: * version 2 for more details (a copy is included in the LICENSE file that dbuck@8879: * accompanied this code). dbuck@8879: * dbuck@8879: * You should have received a copy of the GNU General Public License version dbuck@8879: * 2 along with this work; if not, write to the Free Software Foundation, dbuck@8879: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. dbuck@8879: * dbuck@8879: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA dbuck@8879: * or visit www.oracle.com if you need additional information or have any dbuck@8879: * questions. dbuck@8879: */ dbuck@8879: dbuck@8879: /* dbuck@8879: * @test dbuck@8879: * @bug 8178047 dbuck@8879: * @run main/othervm -XX:CompileCommand=exclude,*.main -XX:-TieredCompilation -XX:-BackgroundCompilation compiler.unsafe.TestRawAliasing dbuck@8879: */ dbuck@8879: dbuck@8879: package compiler.unsafe; dbuck@8879: dbuck@8879: import java.lang.reflect.Field; dbuck@8879: dbuck@8879: public class TestRawAliasing { dbuck@8879: static private final sun.misc.Unsafe UNSAFE; dbuck@8879: static { dbuck@8879: try { dbuck@8879: Field f = sun.misc.Unsafe.class.getDeclaredField("theUnsafe"); dbuck@8879: f.setAccessible(true); dbuck@8879: UNSAFE = (sun.misc.Unsafe) f.get(null); dbuck@8879: } catch (Exception e) { dbuck@8879: throw new RuntimeException("Unable to get Unsafe instance.", e); dbuck@8879: } dbuck@8879: } dbuck@8879: dbuck@8879: static private final int OFFSET_X = 50; dbuck@8879: static private final int OFFSET_Y = 100; dbuck@8879: dbuck@8879: private static int test(long base_plus_offset_x, long base_plus_offset_y, int magic_value) { dbuck@8879: // write 0 to a location dbuck@8879: UNSAFE.putByte(base_plus_offset_x - OFFSET_X, (byte)0); dbuck@8879: // write unfoldable value to really the same location with another base dbuck@8879: UNSAFE.putByte(base_plus_offset_y - OFFSET_Y, (byte)magic_value); dbuck@8879: // read the value back, should be equal to "unfoldable_value" dbuck@8879: return UNSAFE.getByte(base_plus_offset_x - OFFSET_X); dbuck@8879: } dbuck@8879: dbuck@8879: private static final int OFF_HEAP_AREA_SIZE = 128; dbuck@8879: private static final byte MAGIC = 123; dbuck@8879: dbuck@8879: // main is excluded from compilation since we don't want the test method to inline and make base values fold dbuck@8879: public static void main(String... args) { dbuck@8879: long base = UNSAFE.allocateMemory(OFF_HEAP_AREA_SIZE); dbuck@8879: for (int i = 0; i < 100_000; i++) { dbuck@8879: if (test(base + OFFSET_X, base + OFFSET_Y, MAGIC) != MAGIC) { dbuck@8879: throw new RuntimeException("Unexpected magic value"); dbuck@8879: } dbuck@8879: } dbuck@8879: } dbuck@8879: }