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