File: TestUnalignedAccess.java

package info (click to toggle)
openjdk-21 21.0.9%2B10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 821,844 kB
  • sloc: java: 5,620,360; xml: 1,643,607; cpp: 1,297,809; ansic: 421,231; asm: 404,850; objc: 21,018; sh: 15,299; javascript: 11,217; python: 6,895; makefile: 2,367; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (172 lines) | stat: -rw-r--r-- 6,780 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
 * Copyright (c) 2024, Arm Limited. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package compiler.c2;

import jdk.internal.misc.Unsafe;
import jdk.test.lib.Asserts;

/**
 * @test TestUnalignedAccess
 * @summary AArch64: C2 compilation hits offset_ok_for_immed: assert "c2 compiler bug".
 * @bug 8319690
 * @library /test/lib
 * @modules java.base/jdk.internal.misc
 * @run main/othervm compiler.c2.TestUnalignedAccess
 * @run main/othervm -Xcomp -XX:-TieredCompilation -Xmx1g
 *                   -XX:CompileCommand=compileonly,compiler.c2.TestUnalignedAccess*::<clinit>
 *                   compiler.c2.TestUnalignedAccess
 */

public class TestUnalignedAccess {

    public static final int LEN = 2040;

    static final Unsafe UNSAFE = Unsafe.getUnsafe();
    static void sink(int x) {}

    public static long lseed = 1;
    public static int iseed = 2;
    public static short sseed = 3;
    public static byte bseed = 4;
    public static long lres = lseed;
    public static int ires = iseed;
    public static short sres = sseed;
    public static byte bres = bseed;

    public static class TestLong {

        private static final byte[] BYTES = new byte[LEN];
        private static final long rawdata = 0xbeef;
        private static final long data;

        static {
            sink(2);
            // Signed immediate byte offset: range -256 to 255
            // Positive immediate byte offset: a multiple of 8 in the range 0 to 32760
            // Other immediate byte offsets can't be encoded in the instruction field.

            // 1030 can't be encoded as "base + offset" mode into the instruction field.
            UNSAFE.putLongUnaligned(BYTES, 1030, rawdata);
            lres += UNSAFE.getLongUnaligned(BYTES, 1030);
            // 127 can be encoded into simm9 field.
            UNSAFE.putLongUnaligned(BYTES, 127, lres);
            lres += UNSAFE.getLongUnaligned(BYTES, 127);
            // 1096 can be encoded into uimm12 field.
            UNSAFE.putLongUnaligned(BYTES, 1096, lres);
            data = UNSAFE.getLongUnaligned(BYTES, 1096);
        }

    }

    public static class TestInt {

        private static final byte[] BYTES = new byte[LEN];
        private static final int rawdata = 0xbeef;
        private static final int data;
        static {
            sink(2);
            // Signed immediate byte offset: range -256 to 255
            // Positive immediate byte offset, a multiple of 4 in the range 0 to 16380
            // Other immediate byte offsets can't be encoded in the instruction field.

            // 274 can't be encoded as "base + offset" mode into the instruction field.
            UNSAFE.putIntUnaligned(BYTES, 274, rawdata);
            ires += UNSAFE.getIntUnaligned(BYTES, 274);
            // 255 can be encoded into simm9 field.
            UNSAFE.putIntUnaligned(BYTES, 255, ires);
            ires += UNSAFE.getIntUnaligned(BYTES, 255);
            // 528 can be encoded into uimm12 field.
            UNSAFE.putIntUnaligned(BYTES, 528, ires);
            data = UNSAFE.getIntUnaligned(BYTES, 528);
        }

    }

    public static class TestShort {

        private static final byte[] BYTES = new byte[LEN];
        private static final short rawdata = (short)0xbeef;
        private static final short data;
        static {
            sink(2);
            // Signed immediate byte offset: range -256 to 255
            // Positive immediate byte offset: a multiple of 2 in the range 0 to 8190
            // Other immediate byte offsets can't be encoded in the instruction field.

            // 257 can't be encoded as "base + offset" mode into the instruction field.
            UNSAFE.putShortUnaligned(BYTES, 257, rawdata);
            sres = (short) (sres + UNSAFE.getShortUnaligned(BYTES, 257));
            // 253 can be encoded into simm9 field.
            UNSAFE.putShortUnaligned(BYTES, 253, sres);
            sres = (short) (sres + UNSAFE.getShortUnaligned(BYTES, 253));
            // 272 can be encoded into uimm12 field.
            UNSAFE.putShortUnaligned(BYTES, 272, sres);
            data = UNSAFE.getShortUnaligned(BYTES, 272);
        }

    }

    public static class TestByte {

        private static final byte[] BYTES = new byte[LEN];
        private static final byte rawdata = (byte)0x3f;
        private static final byte data;
        static {
            sink(2);
            // Signed immediate byte offset: range -256 to 255
            // Positive immediate byte offset: range 0 to 4095
            // Other immediate byte offsets can't be encoded in the instruction field.

            // 272 can be encoded into simm9 field.
            UNSAFE.putByte(BYTES, 272, rawdata);
            bres = (byte) (bres + UNSAFE.getByte(BYTES, 272));
            // 53 can be encoded into simm9 field.
            UNSAFE.putByte(BYTES, 53, bres);
            bres = (byte) (bres + UNSAFE.getByte(BYTES, 53));
            // 1027 can be encoded into uimm12 field.
            UNSAFE.putByte(BYTES, 1027, bres);
            data = UNSAFE.getByte(BYTES, 1027);
        }

    }

    static void test() {
        TestLong ta = new TestLong();
        Asserts.assertEquals(ta.data, (ta.rawdata + lseed) * 2, "putUnaligned long failed!");

        TestInt tb = new TestInt();
        Asserts.assertEquals(tb.data, (tb.rawdata + iseed) * 2, "putUnaligned int failed!");

        TestShort tc = new TestShort();
        Asserts.assertEquals(tc.data, (short) (((short) (tc.rawdata + sseed)) * 2), "putUnaligned short failed!");

        TestByte td = new TestByte();
        Asserts.assertEquals(td.data, (byte) (((byte) (td.rawdata + bseed)) * 2), "put byte failed!");
    }

    public static void main(String[] strArr) {
        test();
    }
}