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 8058744
27 * @summary Invalid pattern-matching of address computations in raw unsafe
28 * @library /test/lib
29 * @modules java.base/jdk.internal.misc
30 * java.management
31 * @run main/othervm -Xbatch compiler.unsafe.UnsafeRaw
32 */
33
34 package compiler.unsafe;
35
36 import jdk.internal.misc.Unsafe;
37 import jdk.test.lib.Utils;
38 import jdk.test.lib.unsafe.UnsafeHelper;
39
40 import java.util.Random;
41
42 public class UnsafeRaw {
43 public static class Tests {
44 public static int int_index(Unsafe unsafe, long base, int index) throws Exception {
45 return unsafe.getInt(base + (index << 2));
46 }
47 public static int long_index(Unsafe unsafe, long base, long index) throws Exception {
48 return unsafe.getInt(base + (index << 2));
49 }
50 public static int int_index_back_ashift(Unsafe unsafe, long base, int index) throws Exception {
51 return unsafe.getInt(base + (index >> 2));
52 }
53 public static int int_index_back_lshift(Unsafe unsafe, long base, int index) throws Exception {
54 return unsafe.getInt(base + (index >>> 2));
55 }
56 public static int long_index_back_ashift(Unsafe unsafe, long base, long index) throws Exception {
57 return unsafe.getInt(base + (index >> 2));
58 }
65 }
66 public static int long_const_1234567890abcdef_index(Unsafe unsafe, long base) throws Exception {
67 long idx5 = 0x1234567890abcdefL;
68 return unsafe.getInt(base + idx5);
69 }
70 public static int int_index_mul(Unsafe unsafe, long base, int index) throws Exception {
71 return unsafe.getInt(base + (index * 4));
72 }
73 public static int long_index_mul(Unsafe unsafe, long base, long index) throws Exception {
74 return unsafe.getInt(base + (index * 4));
75 }
76 public static int int_index_mul_scale_16(Unsafe unsafe, long base, int index) throws Exception {
77 return unsafe.getInt(base + (index * 16));
78 }
79 public static int long_index_mul_scale_16(Unsafe unsafe, long base, long index) throws Exception {
80 return unsafe.getInt(base + (index * 16));
81 }
82 }
83
84 public static void main(String[] args) throws Exception {
85 Unsafe unsafe = UnsafeHelper.getUnsafe();
86 final int array_size = 128;
87 final int element_size = 4;
88 final int magic = 0x12345678;
89
90 Random rnd = Utils.getRandomInstance();
91
92 long array = unsafe.allocateMemory(array_size * element_size); // 128 ints
93 long addr = array + array_size * element_size / 2; // something in the middle to work with
94 unsafe.putInt(addr, magic);
95 for (int j = 0; j < 100000; j++) {
96 if (Tests.int_index(unsafe, addr, 0) != magic) throw new Exception();
97 if (Tests.long_index(unsafe, addr, 0) != magic) throw new Exception();
98 if (Tests.int_index_mul(unsafe, addr, 0) != magic) throw new Exception();
99 if (Tests.long_index_mul(unsafe, addr, 0) != magic) throw new Exception();
100 {
101 long idx1 = rnd.nextLong();
102 long addr1 = addr - (idx1 << 2);
103 if (Tests.long_index(unsafe, addr1, idx1) != magic) throw new Exception();
104 }
105 {
|
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 8058744
27 * @summary Invalid pattern-matching of address computations in raw unsafe
28 * @library /test/lib
29 * @modules java.base/jdk.internal.misc
30 * java.management
31 * @run main/othervm -Xbatch compiler.unsafe.UnsafeRaw
32 */
33
34 package compiler.unsafe;
35
36 import jdk.internal.misc.Unsafe;
37 import jdk.test.lib.Utils;
38
39 import java.util.Random;
40
41 public class UnsafeRaw {
42 public static class Tests {
43 public static int int_index(Unsafe unsafe, long base, int index) throws Exception {
44 return unsafe.getInt(base + (index << 2));
45 }
46 public static int long_index(Unsafe unsafe, long base, long index) throws Exception {
47 return unsafe.getInt(base + (index << 2));
48 }
49 public static int int_index_back_ashift(Unsafe unsafe, long base, int index) throws Exception {
50 return unsafe.getInt(base + (index >> 2));
51 }
52 public static int int_index_back_lshift(Unsafe unsafe, long base, int index) throws Exception {
53 return unsafe.getInt(base + (index >>> 2));
54 }
55 public static int long_index_back_ashift(Unsafe unsafe, long base, long index) throws Exception {
56 return unsafe.getInt(base + (index >> 2));
57 }
64 }
65 public static int long_const_1234567890abcdef_index(Unsafe unsafe, long base) throws Exception {
66 long idx5 = 0x1234567890abcdefL;
67 return unsafe.getInt(base + idx5);
68 }
69 public static int int_index_mul(Unsafe unsafe, long base, int index) throws Exception {
70 return unsafe.getInt(base + (index * 4));
71 }
72 public static int long_index_mul(Unsafe unsafe, long base, long index) throws Exception {
73 return unsafe.getInt(base + (index * 4));
74 }
75 public static int int_index_mul_scale_16(Unsafe unsafe, long base, int index) throws Exception {
76 return unsafe.getInt(base + (index * 16));
77 }
78 public static int long_index_mul_scale_16(Unsafe unsafe, long base, long index) throws Exception {
79 return unsafe.getInt(base + (index * 16));
80 }
81 }
82
83 public static void main(String[] args) throws Exception {
84 Unsafe unsafe = Unsafe.getUnsafe();
85 final int array_size = 128;
86 final int element_size = 4;
87 final int magic = 0x12345678;
88
89 Random rnd = Utils.getRandomInstance();
90
91 long array = unsafe.allocateMemory(array_size * element_size); // 128 ints
92 long addr = array + array_size * element_size / 2; // something in the middle to work with
93 unsafe.putInt(addr, magic);
94 for (int j = 0; j < 100000; j++) {
95 if (Tests.int_index(unsafe, addr, 0) != magic) throw new Exception();
96 if (Tests.long_index(unsafe, addr, 0) != magic) throw new Exception();
97 if (Tests.int_index_mul(unsafe, addr, 0) != magic) throw new Exception();
98 if (Tests.long_index_mul(unsafe, addr, 0) != magic) throw new Exception();
99 {
100 long idx1 = rnd.nextLong();
101 long addr1 = addr - (idx1 << 2);
102 if (Tests.long_index(unsafe, addr1, idx1) != magic) throw new Exception();
103 }
104 {
|