JdkInternalMiscUnsafeUnalignedAccess.java revision 12290:8953c0318163
1/*
2 * Copyright (c) 2016 SAP SE. 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 8158260
27 * @summary Test unaligned Unsafe accesses
28 * @modules java.base/jdk.internal.misc:+open
29 *
30 * @run main/othervm -Diters=20000 -XX:-UseOnStackReplacement -XX:-BackgroundCompilation
31 *      compiler.unsafe.JdkInternalMiscUnsafeUnalignedAccess
32 * @author volker.simonis@gmail.com
33 */
34
35package compiler.unsafe;
36
37import jdk.internal.misc.Unsafe;
38
39import java.lang.reflect.Field;
40import java.nio.ByteOrder;
41
42public class JdkInternalMiscUnsafeUnalignedAccess {
43    static final int ITERS = Integer.getInteger("iters", 20_000);
44    private static final boolean BIG_ENDIAN = ByteOrder.nativeOrder().equals(ByteOrder.BIG_ENDIAN);
45    private static final Unsafe UNSAFE;
46    private static final int SIZE = 1024;
47    private static long memory;
48
49    static {
50        try {
51            Field unsafeField = Unsafe.class.getDeclaredField("theUnsafe");
52            unsafeField.setAccessible(true);
53            UNSAFE = (Unsafe) unsafeField.get(null);
54        }
55        catch (Exception e) {
56            throw new RuntimeException("Unable to get Unsafe instance.", e);
57        }
58    }
59
60    static int getInt_0() {
61        return UNSAFE.getInt(memory + 0);
62    }
63    static int getInt_1() {
64        return UNSAFE.getInt(memory + 1);
65    }
66    static int getInt_4() {
67        return UNSAFE.getInt(memory + 4);
68    }
69    static int getInt_17() {
70        return UNSAFE.getInt(memory + 17);
71    }
72
73    static long getIntAsLong_0() {
74        return UNSAFE.getInt(memory + 0);
75    }
76    static long getIntAsLong_1() {
77        return UNSAFE.getInt(memory + 1);
78    }
79    static long getIntAsLong_4() {
80        return UNSAFE.getInt(memory + 4);
81    }
82    static long getIntAsLong_17() {
83        return UNSAFE.getInt(memory + 17);
84    }
85
86    static long getLong_0() {
87        return UNSAFE.getLong(memory + 0);
88    }
89    static long getLong_1() {
90        return UNSAFE.getLong(memory + 1);
91    }
92    static long getLong_4() {
93        return UNSAFE.getLong(memory + 4);
94    }
95    static long getLong_8() {
96        return UNSAFE.getLong(memory + 8);
97    }
98    static long getLong_17() {
99        return UNSAFE.getLong(memory + 17);
100    }
101
102    static void putInt_0(int i) {
103        UNSAFE.putInt(memory + 0, i);
104    }
105    static void putInt_1(int i) {
106        UNSAFE.putInt(memory + 1, i);
107    }
108    static void putInt_4(int i) {
109        UNSAFE.putInt(memory + 4, i);
110    }
111    static void putInt_17(int i) {
112        UNSAFE.putInt(memory + 17, i);
113    }
114
115    static void putLong_0(long l) {
116        UNSAFE.putLong(memory + 0, l);
117    }
118    static void putLong_1(long l) {
119        UNSAFE.putLong(memory + 1, l);
120    }
121    static void putLong_4(long l) {
122        UNSAFE.putLong(memory + 4, l);
123    }
124    static void putLong_8(long l) {
125        UNSAFE.putLong(memory + 8, l);
126    }
127    static void putLong_17(long l) {
128        UNSAFE.putLong(memory + 17, l);
129    }
130
131    public static void main(String[] args) throws Exception {
132
133        if (!UNSAFE.unalignedAccess()) {
134            System.out.println("Platform is not supporting unaligned access - nothing to test.");
135            return;
136        }
137
138        memory = UNSAFE.allocateMemory(SIZE);
139
140        UNSAFE.putInt(memory +  0, 0x00112233);
141        UNSAFE.putInt(memory +  4, 0x44556677);
142        UNSAFE.putInt(memory +  8, 0x8899aabb);
143        UNSAFE.putInt(memory + 12, 0xccddeeff);
144        UNSAFE.putInt(memory + 16, 0x01234567);
145        UNSAFE.putInt(memory + 20, 0x89abcdef);
146        UNSAFE.putInt(memory + 24, 0x01234567);
147
148        // Unsafe.getInt()
149        int res;
150        for (int i = 0; i < ITERS; i++) {
151            res = getInt_0();
152            if (res != 0x00112233) {
153                throw new Exception(res + " != 0x00112233");
154            }
155        }
156
157        for (int i = 0; i < ITERS; i++) {
158            res = getInt_1();
159            if (res != (BIG_ENDIAN ? 0x11223344 : 0x77001122)) {
160                throw new Exception(res + " != " + (BIG_ENDIAN ? 0x11223344 : 0x77001122));
161            }
162        }
163
164        for (int i = 0; i < ITERS; i++) {
165            res = getInt_4();
166            if (res != 0x44556677) {
167                throw new Exception(res + " != 0x44556677");
168            }
169        }
170
171        for (int i = 0; i < ITERS; i++) {
172            res = getInt_17();
173            if (res != (BIG_ENDIAN ? 0x23456789 : 0xef012345)) {
174                throw new Exception(res + " != " + (BIG_ENDIAN ? 0x23456789 : 0xef012345));
175            }
176        }
177
178        // (long)Unsafe.getInt()
179        long lres;
180        for (int i = 0; i < ITERS; i++) {
181            lres = getIntAsLong_0();
182            if (lres != (long)0x00112233) {
183                throw new Exception(lres + " != 0x00112233");
184            }
185        }
186
187        for (int i = 0; i < ITERS; i++) {
188            lres = getIntAsLong_1();
189            if (lres != (BIG_ENDIAN ? (long)0x11223344 : (long)0x77001122)) {
190                throw new Exception(lres + " != " + (BIG_ENDIAN ? (long)0x11223344 : (long)0x77001122));
191            }
192        }
193
194        for (int i = 0; i < ITERS; i++) {
195            lres = getIntAsLong_4();
196            if (lres != (long)0x44556677) {
197                throw new Exception(lres + " != 0x44556677");
198            }
199        }
200
201        for (int i = 0; i < ITERS; i++) {
202            lres = getIntAsLong_17();
203            if (lres != (BIG_ENDIAN ? (long)0x23456789 : (long)0xef012345)) {
204                throw new Exception(lres + " != " + (BIG_ENDIAN ? (long)0x23456789 : (long)0xef012345));
205            }
206        }
207
208        // Unsafe.getLong()
209        for (int i = 0; i < ITERS; i++) {
210            lres = getLong_0();
211            if (lres != (BIG_ENDIAN ? 0x0011223344556677L : 0x4455667700112233L)) {
212                throw new Exception(lres + " != " + (BIG_ENDIAN ? 0x0011223344556677L : 0x4455667700112233L));
213            }
214        }
215
216        for (int i = 0; i < ITERS; i++) {
217            lres = getLong_1();
218            if (lres != (BIG_ENDIAN ? 0x1122334455667788L : 0xbb44556677001122L)) {
219                throw new Exception(lres + " != " + (BIG_ENDIAN ? 0x1122334455667788L : 0xbb44556677001122L));
220            }
221        }
222
223        for (int i = 0; i < ITERS; i++) {
224            lres = getLong_4();
225            if (lres != (BIG_ENDIAN ? 0x445566778899aabbL : 0x8899aabb44556677L)) {
226                throw new Exception(lres + " != " + (BIG_ENDIAN ? 0x445566778899aabbL : 0x8899aabb44556677L));
227            }
228        }
229
230        for (int i = 0; i < ITERS; i++) {
231            lres = getLong_8();
232            if (lres != (BIG_ENDIAN ? 0x8899aabbccddeeffL : 0xccddeeff8899aabbL)) {
233                throw new Exception(lres + " != " + (BIG_ENDIAN ? 0x8899aabbccddeeffL : 0xccddeeff8899aabbL));
234            }
235        }
236
237        for (int i = 0; i < ITERS; i++) {
238            lres = getLong_17();
239            if (lres != (BIG_ENDIAN ? 0x23456789abcdef01L : 0x6789abcdef012345L)) {
240                throw new Exception(lres + " != " + (BIG_ENDIAN ? 0x23456789abcdef01L : 0x6789abcdef012345L));
241            }
242        }
243
244        // Unsafe.putInt()
245        for (int i = 0; i < ITERS; i++) {
246            putInt_0(0x00112233);
247            res = getInt_0();
248            if (res != 0x00112233) {
249                throw new Exception(res + " != 0x00112233");
250            }
251        }
252
253        for (int i = 0; i < ITERS; i++) {
254            putInt_1(BIG_ENDIAN ? 0x11223344 : 0x77001122);
255            res = getInt_1();
256            if (res != (BIG_ENDIAN ? 0x11223344 : 0x77001122)) {
257                throw new Exception(res + " != " + (BIG_ENDIAN ? 0x11223344 : 0x77001122));
258            }
259        }
260
261        for (int i = 0; i < ITERS; i++) {
262            putInt_4(0x44556677);
263            res = getInt_4();
264            if (res != 0x44556677) {
265                throw new Exception(res + " != 0x44556677");
266            }
267        }
268
269        for (int i = 0; i < ITERS; i++) {
270            putInt_17(BIG_ENDIAN ? 0x23456789 : 0xef012345);
271            res = getInt_17();
272            if (res != (BIG_ENDIAN ? 0x23456789 : 0xef012345)) {
273                throw new Exception(res + " != " + (BIG_ENDIAN ? 0x23456789 : 0xef012345));
274            }
275        }
276
277
278        // Unsafe.putLong()
279        for (int i = 0; i < ITERS; i++) {
280            putLong_0(BIG_ENDIAN ? 0x0011223344556677L : 0x4455667700112233L);
281            lres = getLong_0();
282            if (lres != (BIG_ENDIAN ? 0x0011223344556677L : 0x4455667700112233L)) {
283                throw new Exception(lres + " != " + (BIG_ENDIAN ? 0x0011223344556677L : 0x4455667700112233L));
284            }
285        }
286
287        for (int i = 0; i < ITERS; i++) {
288            putLong_1(BIG_ENDIAN ? 0x1122334455667788L : 0xbb44556677001122L);
289            lres = getLong_1();
290            if (lres != (BIG_ENDIAN ? 0x1122334455667788L : 0xbb44556677001122L)) {
291                throw new Exception(lres + " != " + (BIG_ENDIAN ? 0x1122334455667788L : 0xbb44556677001122L));
292            }
293        }
294
295        for (int i = 0; i < ITERS; i++) {
296            putLong_4(BIG_ENDIAN ? 0x445566778899aabbL : 0x8899aabb44556677L);
297            lres = getLong_4();
298            if (lres != (BIG_ENDIAN ? 0x445566778899aabbL : 0x8899aabb44556677L)) {
299                throw new Exception(lres + " != " + (BIG_ENDIAN ? 0x445566778899aabbL : 0x8899aabb44556677L));
300            }
301        }
302
303        for (int i = 0; i < ITERS; i++) {
304            putLong_8(BIG_ENDIAN ? 0x8899aabbccddeeffL : 0xccddeeff8899aabbL);
305            lres = getLong_8();
306            if (lres != (BIG_ENDIAN ? 0x8899aabbccddeeffL : 0xccddeeff8899aabbL)) {
307                throw new Exception(lres + " != " + (BIG_ENDIAN ? 0x8899aabbccddeeffL : 0xccddeeff8899aabbL));
308            }
309        }
310
311        for (int i = 0; i < ITERS; i++) {
312            putLong_17(BIG_ENDIAN ? 0x23456789abcdef01L : 0x6789abcdef012345L);
313            lres = getLong_17();
314            if (lres != (BIG_ENDIAN ? 0x23456789abcdef01L : 0x6789abcdef012345L)) {
315                throw new Exception(lres + " != " + (BIG_ENDIAN ? 0x23456789abcdef01L : 0x6789abcdef012345L));
316            }
317        }
318    }
319
320}
321