|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * Copyright (c) 2024, Arm Limited. All rights reserved. |
| 4 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 5 | + * |
| 6 | + * This code is free software; you can redistribute it and/or modify it |
| 7 | + * under the terms of the GNU General Public License version 2 only, as |
| 8 | + * published by the Free Software Foundation. |
| 9 | + * |
| 10 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 11 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 14 | + * accompanied this code). |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License version |
| 17 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | + * |
| 20 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 21 | + * or visit www.oracle.com if you need additional information or have any |
| 22 | + * questions. |
| 23 | + */ |
| 24 | + |
| 25 | +package compiler.c2; |
| 26 | + |
| 27 | +import jdk.internal.misc.Unsafe; |
| 28 | +import jdk.test.lib.Asserts; |
| 29 | + |
| 30 | +/** |
| 31 | + * @test TestUnalignedAccess |
| 32 | + * @summary AArch64: C2 compilation hits offset_ok_for_immed: assert "c2 compiler bug". |
| 33 | + * @bug 8319690 |
| 34 | + * @library /test/lib |
| 35 | + * @modules java.base/jdk.internal.misc |
| 36 | + * @run main/othervm compiler.c2.TestUnalignedAccess |
| 37 | + * @run main/othervm -Xcomp -XX:-TieredCompilation -Xmx1g |
| 38 | + * -XX:CompileCommand=compileonly,compiler.c2.TestUnalignedAccess*::<clinit> |
| 39 | + * compiler.c2.TestUnalignedAccess |
| 40 | + */ |
| 41 | + |
| 42 | +public class TestUnalignedAccess { |
| 43 | + |
| 44 | + public static final int LEN = 2040; |
| 45 | + |
| 46 | + static final Unsafe UNSAFE = Unsafe.getUnsafe(); |
| 47 | + static void sink(int x) {} |
| 48 | + |
| 49 | + public static long lseed = 1; |
| 50 | + public static int iseed = 2; |
| 51 | + public static short sseed = 3; |
| 52 | + public static byte bseed = 4; |
| 53 | + public static long lres = lseed; |
| 54 | + public static int ires = iseed; |
| 55 | + public static short sres = sseed; |
| 56 | + public static byte bres = bseed; |
| 57 | + |
| 58 | + public static class TestLong { |
| 59 | + |
| 60 | + private static final byte[] BYTES = new byte[LEN]; |
| 61 | + private static final long rawdata = 0xbeef; |
| 62 | + private static final long data; |
| 63 | + |
| 64 | + static { |
| 65 | + sink(2); |
| 66 | + // Signed immediate byte offset: range -256 to 255 |
| 67 | + // Positive immediate byte offset: a multiple of 8 in the range 0 to 32760 |
| 68 | + // Other immediate byte offsets can't be encoded in the instruction field. |
| 69 | + |
| 70 | + // 1030 can't be encoded as "base + offset" mode into the instruction field. |
| 71 | + UNSAFE.putLongUnaligned(BYTES, 1030, rawdata); |
| 72 | + lres += UNSAFE.getLongUnaligned(BYTES, 1030); |
| 73 | + // 127 can be encoded into simm9 field. |
| 74 | + UNSAFE.putLongUnaligned(BYTES, 127, lres); |
| 75 | + lres += UNSAFE.getLongUnaligned(BYTES, 127); |
| 76 | + // 1096 can be encoded into uimm12 field. |
| 77 | + UNSAFE.putLongUnaligned(BYTES, 1096, lres); |
| 78 | + data = UNSAFE.getLongUnaligned(BYTES, 1096); |
| 79 | + } |
| 80 | + |
| 81 | + } |
| 82 | + |
| 83 | + public static class TestInt { |
| 84 | + |
| 85 | + private static final byte[] BYTES = new byte[LEN]; |
| 86 | + private static final int rawdata = 0xbeef; |
| 87 | + private static final int data; |
| 88 | + static { |
| 89 | + sink(2); |
| 90 | + // Signed immediate byte offset: range -256 to 255 |
| 91 | + // Positive immediate byte offset, a multiple of 4 in the range 0 to 16380 |
| 92 | + // Other immediate byte offsets can't be encoded in the instruction field. |
| 93 | + |
| 94 | + // 274 can't be encoded as "base + offset" mode into the instruction field. |
| 95 | + UNSAFE.putIntUnaligned(BYTES, 274, rawdata); |
| 96 | + ires += UNSAFE.getIntUnaligned(BYTES, 274); |
| 97 | + // 255 can be encoded into simm9 field. |
| 98 | + UNSAFE.putIntUnaligned(BYTES, 255, ires); |
| 99 | + ires += UNSAFE.getIntUnaligned(BYTES, 255); |
| 100 | + // 528 can be encoded into uimm12 field. |
| 101 | + UNSAFE.putIntUnaligned(BYTES, 528, ires); |
| 102 | + data = UNSAFE.getIntUnaligned(BYTES, 528); |
| 103 | + } |
| 104 | + |
| 105 | + } |
| 106 | + |
| 107 | + public static class TestShort { |
| 108 | + |
| 109 | + private static final byte[] BYTES = new byte[LEN]; |
| 110 | + private static final short rawdata = (short)0xbeef; |
| 111 | + private static final short data; |
| 112 | + static { |
| 113 | + sink(2); |
| 114 | + // Signed immediate byte offset: range -256 to 255 |
| 115 | + // Positive immediate byte offset: a multiple of 2 in the range 0 to 8190 |
| 116 | + // Other immediate byte offsets can't be encoded in the instruction field. |
| 117 | + |
| 118 | + // 257 can't be encoded as "base + offset" mode into the instruction field. |
| 119 | + UNSAFE.putShortUnaligned(BYTES, 257, rawdata); |
| 120 | + sres = (short) (sres + UNSAFE.getShortUnaligned(BYTES, 257)); |
| 121 | + // 253 can be encoded into simm9 field. |
| 122 | + UNSAFE.putShortUnaligned(BYTES, 253, sres); |
| 123 | + sres = (short) (sres + UNSAFE.getShortUnaligned(BYTES, 253)); |
| 124 | + // 272 can be encoded into uimm12 field. |
| 125 | + UNSAFE.putShortUnaligned(BYTES, 272, sres); |
| 126 | + data = UNSAFE.getShortUnaligned(BYTES, 272); |
| 127 | + } |
| 128 | + |
| 129 | + } |
| 130 | + |
| 131 | + public static class TestByte { |
| 132 | + |
| 133 | + private static final byte[] BYTES = new byte[LEN]; |
| 134 | + private static final byte rawdata = (byte)0x3f; |
| 135 | + private static final byte data; |
| 136 | + static { |
| 137 | + sink(2); |
| 138 | + // Signed immediate byte offset: range -256 to 255 |
| 139 | + // Positive immediate byte offset: range 0 to 4095 |
| 140 | + // Other immediate byte offsets can't be encoded in the instruction field. |
| 141 | + |
| 142 | + // 272 can be encoded into simm9 field. |
| 143 | + UNSAFE.putByte(BYTES, 272, rawdata); |
| 144 | + bres = (byte) (bres + UNSAFE.getByte(BYTES, 272)); |
| 145 | + // 53 can be encoded into simm9 field. |
| 146 | + UNSAFE.putByte(BYTES, 53, bres); |
| 147 | + bres = (byte) (bres + UNSAFE.getByte(BYTES, 53)); |
| 148 | + // 1027 can be encoded into uimm12 field. |
| 149 | + UNSAFE.putByte(BYTES, 1027, bres); |
| 150 | + data = UNSAFE.getByte(BYTES, 1027); |
| 151 | + } |
| 152 | + |
| 153 | + } |
| 154 | + |
| 155 | + static void test() { |
| 156 | + TestLong ta = new TestLong(); |
| 157 | + Asserts.assertEquals(ta.data, (ta.rawdata + lseed) * 2, "putUnaligned long failed!"); |
| 158 | + |
| 159 | + TestInt tb = new TestInt(); |
| 160 | + Asserts.assertEquals(tb.data, (tb.rawdata + iseed) * 2, "putUnaligned int failed!"); |
| 161 | + |
| 162 | + TestShort tc = new TestShort(); |
| 163 | + Asserts.assertEquals(tc.data, (short) (((short) (tc.rawdata + sseed)) * 2), "putUnaligned short failed!"); |
| 164 | + |
| 165 | + TestByte td = new TestByte(); |
| 166 | + Asserts.assertEquals(td.data, (byte) (((byte) (td.rawdata + bseed)) * 2), "put byte failed!"); |
| 167 | + } |
| 168 | + |
| 169 | + public static void main(String[] strArr) { |
| 170 | + test(); |
| 171 | + } |
| 172 | +} |
0 commit comments