test/compiler/unsafe/TestRawAliasing.java

changeset 8879
6bc9abf210fd
equal deleted inserted replaced
8878:d3cc20285653 8879:6bc9abf210fd
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 */
29
30 package compiler.unsafe;
31
32 import java.lang.reflect.Field;
33
34 public class TestRawAliasing {
35 static private final sun.misc.Unsafe UNSAFE;
36 static {
37 try {
38 Field f = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
39 f.setAccessible(true);
40 UNSAFE = (sun.misc.Unsafe) f.get(null);
41 } catch (Exception e) {
42 throw new RuntimeException("Unable to get Unsafe instance.", e);
43 }
44 }
45
46 static private final int OFFSET_X = 50;
47 static private final int OFFSET_Y = 100;
48
49 private static int test(long base_plus_offset_x, long base_plus_offset_y, int magic_value) {
50 // write 0 to a location
51 UNSAFE.putByte(base_plus_offset_x - OFFSET_X, (byte)0);
52 // write unfoldable value to really the same location with another base
53 UNSAFE.putByte(base_plus_offset_y - OFFSET_Y, (byte)magic_value);
54 // read the value back, should be equal to "unfoldable_value"
55 return UNSAFE.getByte(base_plus_offset_x - OFFSET_X);
56 }
57
58 private static final int OFF_HEAP_AREA_SIZE = 128;
59 private static final byte MAGIC = 123;
60
61 // main is excluded from compilation since we don't want the test method to inline and make base values fold
62 public static void main(String... args) {
63 long base = UNSAFE.allocateMemory(OFF_HEAP_AREA_SIZE);
64 for (int i = 0; i < 100_000; i++) {
65 if (test(base + OFFSET_X, base + OFFSET_Y, MAGIC) != MAGIC) {
66 throw new RuntimeException("Unexpected magic value");
67 }
68 }
69 }
70 }

mercurial