20492
|
1 /*
|
|
2 * Copyright (c) 2014, 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 8058744
|
|
27 * @summary Invalid pattern-matching of address computations in raw unsafe
|
|
28 * @library /testlibrary
|
|
29 * @run main/othervm -Xbatch UnsafeRaw
|
|
30 */
|
|
31
|
|
32 import com.oracle.java.testlibrary.Utils;
|
|
33 import java.util.Random;
|
|
34
|
|
35 public class UnsafeRaw {
|
|
36 public static class Tests {
|
|
37 public static int int_index(sun.misc.Unsafe unsafe, long base, int index) throws Exception {
|
|
38 return unsafe.getInt(base + (index << 2));
|
|
39 }
|
|
40 public static int long_index(sun.misc.Unsafe unsafe, long base, long index) throws Exception {
|
|
41 return unsafe.getInt(base + (index << 2));
|
|
42 }
|
|
43 public static int int_index_back_ashift(sun.misc.Unsafe unsafe, long base, int index) throws Exception {
|
|
44 return unsafe.getInt(base + (index >> 2));
|
|
45 }
|
|
46 public static int int_index_back_lshift(sun.misc.Unsafe unsafe, long base, int index) throws Exception {
|
|
47 return unsafe.getInt(base + (index >>> 2));
|
|
48 }
|
|
49 public static int long_index_back_ashift(sun.misc.Unsafe unsafe, long base, long index) throws Exception {
|
|
50 return unsafe.getInt(base + (index >> 2));
|
|
51 }
|
|
52 public static int long_index_back_lshift(sun.misc.Unsafe unsafe, long base, long index) throws Exception {
|
|
53 return unsafe.getInt(base + (index >>> 2));
|
|
54 }
|
|
55 public static int int_const_12345678_index(sun.misc.Unsafe unsafe, long base) throws Exception {
|
|
56 int idx4 = 0x12345678;
|
|
57 return unsafe.getInt(base + idx4);
|
|
58 }
|
|
59 public static int long_const_1234567890abcdef_index(sun.misc.Unsafe unsafe, long base) throws Exception {
|
|
60 long idx5 = 0x1234567890abcdefL;
|
|
61 return unsafe.getInt(base + idx5);
|
|
62 }
|
|
63 public static int int_index_mul(sun.misc.Unsafe unsafe, long base, int index) throws Exception {
|
|
64 return unsafe.getInt(base + (index * 4));
|
|
65 }
|
|
66 public static int long_index_mul(sun.misc.Unsafe unsafe, long base, long index) throws Exception {
|
|
67 return unsafe.getInt(base + (index * 4));
|
|
68 }
|
|
69 public static int int_index_mul_scale_16(sun.misc.Unsafe unsafe, long base, int index) throws Exception {
|
|
70 return unsafe.getInt(base + (index * 16));
|
|
71 }
|
|
72 public static int long_index_mul_scale_16(sun.misc.Unsafe unsafe, long base, long index) throws Exception {
|
|
73 return unsafe.getInt(base + (index * 16));
|
|
74 }
|
|
75 }
|
|
76
|
|
77 public static void main(String[] args) throws Exception {
|
|
78 sun.misc.Unsafe unsafe = Utils.getUnsafe();
|
|
79 final int array_size = 128;
|
|
80 final int element_size = 4;
|
|
81 final int magic = 0x12345678;
|
|
82
|
|
83 Random rnd = new Random();
|
|
84
|
|
85 long array = unsafe.allocateMemory(array_size * element_size); // 128 ints
|
|
86 long addr = array + array_size * element_size / 2; // something in the middle to work with
|
|
87 unsafe.putInt(addr, magic);
|
|
88 for (int j = 0; j < 100000; j++) {
|
|
89 if (Tests.int_index(unsafe, addr, 0) != magic) throw new Exception();
|
|
90 if (Tests.long_index(unsafe, addr, 0) != magic) throw new Exception();
|
|
91 if (Tests.int_index_mul(unsafe, addr, 0) != magic) throw new Exception();
|
|
92 if (Tests.long_index_mul(unsafe, addr, 0) != magic) throw new Exception();
|
|
93 {
|
|
94 long idx1 = rnd.nextLong();
|
|
95 long addr1 = addr - (idx1 << 2);
|
|
96 if (Tests.long_index(unsafe, addr1, idx1) != magic) throw new Exception();
|
|
97 }
|
|
98 {
|
|
99 long idx2 = rnd.nextLong();
|
|
100 long addr2 = addr - (idx2 >> 2);
|
|
101 if (Tests.long_index_back_ashift(unsafe, addr2, idx2) != magic) throw new Exception();
|
|
102 }
|
|
103 {
|
|
104 long idx3 = rnd.nextLong();
|
|
105 long addr3 = addr - (idx3 >>> 2);
|
|
106 if (Tests.long_index_back_lshift(unsafe, addr3, idx3) != magic) throw new Exception();
|
|
107 }
|
|
108 {
|
|
109 long idx4 = 0x12345678;
|
|
110 long addr4 = addr - idx4;
|
|
111 if (Tests.int_const_12345678_index(unsafe, addr4) != magic) throw new Exception();
|
|
112 }
|
|
113 {
|
|
114 long idx5 = 0x1234567890abcdefL;
|
|
115 long addr5 = addr - idx5;
|
|
116 if (Tests.long_const_1234567890abcdef_index(unsafe, addr5) != magic) throw new Exception();
|
|
117 }
|
|
118 {
|
|
119 int idx6 = rnd.nextInt();
|
|
120 long addr6 = addr - (idx6 >> 2);
|
|
121 if (Tests.int_index_back_ashift(unsafe, addr6, idx6) != magic) throw new Exception();
|
|
122 }
|
|
123 {
|
|
124 int idx7 = rnd.nextInt();
|
|
125 long addr7 = addr - (idx7 >>> 2);
|
|
126 if (Tests.int_index_back_lshift(unsafe, addr7, idx7) != magic) throw new Exception();
|
|
127 }
|
|
128 {
|
|
129 int idx8 = rnd.nextInt();
|
|
130 long addr8 = addr - (idx8 * 16);
|
|
131 if (Tests.int_index_mul_scale_16(unsafe, addr8, idx8) != magic) throw new Exception();
|
|
132 }
|
|
133 {
|
|
134 long idx9 = rnd.nextLong();
|
|
135 long addr9 = addr - (idx9 * 16);
|
|
136 if (Tests.long_index_mul_scale_16(unsafe, addr9, idx9) != magic) throw new Exception();
|
|
137 }
|
|
138 }
|
|
139 }
|
|
140 }
|