|
| 1 | +/* |
| 2 | + * Copyright (C) 2020 THL A29 Limited, a Tencent company. 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 8250825 |
| 27 | + * @summary "assert(field != __null) failed: missing field" in TypeOopPtr::TypeOopPt(...) with misaligned unsafe accesses |
| 28 | + * @library /test/lib |
| 29 | + * @modules java.base/jdk.internal.misc |
| 30 | + * @run main/othervm -XX:-TieredCompilation -Xcomp |
| 31 | + * -XX:CompileCommand=compileonly,TestMisalignedUnsafeAccess::test* TestMisalignedUnsafeAccess |
| 32 | + */ |
| 33 | + |
| 34 | +import java.lang.reflect.Field; |
| 35 | +import sun.misc.Unsafe; |
| 36 | +import jdk.test.lib.Asserts; |
| 37 | + |
| 38 | +public class TestMisalignedUnsafeAccess { |
| 39 | + |
| 40 | + private static final Unsafe UNSAFE; |
| 41 | + static { |
| 42 | + Field f = null; |
| 43 | + try { |
| 44 | + f = Unsafe.class.getDeclaredField("theUnsafe"); |
| 45 | + f.setAccessible(true); |
| 46 | + UNSAFE = (Unsafe) f.get(null); |
| 47 | + } catch (ReflectiveOperationException e) { |
| 48 | + throw new Error(e); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private static short onHeapStaticMemory; // For static field testing |
| 53 | + private static final Object onHeapStaticMemoryBase; |
| 54 | + private static final long onHeapStaticMemoryOffset; |
| 55 | + |
| 56 | + private short onHeapInstanceMemory; // For instance field testing |
| 57 | + private static final long onHeapInstanceMemoryOffset; |
| 58 | + |
| 59 | + static { |
| 60 | + try { |
| 61 | + Field staticField = TestMisalignedUnsafeAccess.class.getDeclaredField("onHeapStaticMemory"); |
| 62 | + onHeapStaticMemoryBase = UNSAFE.staticFieldBase(staticField); |
| 63 | + onHeapStaticMemoryOffset = UNSAFE.staticFieldOffset(staticField); |
| 64 | + |
| 65 | + Field instanceField = TestMisalignedUnsafeAccess.class.getDeclaredField("onHeapInstanceMemory"); |
| 66 | + onHeapInstanceMemoryOffset = UNSAFE.objectFieldOffset(instanceField); |
| 67 | + } catch (Exception e) { |
| 68 | + throw new RuntimeException(e); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + public static void testStaticField() { |
| 73 | + byte b1 = 0x01; |
| 74 | + byte b2 = 0x02; |
| 75 | + |
| 76 | + UNSAFE.putByte(onHeapStaticMemoryBase, onHeapStaticMemoryOffset, b1); |
| 77 | + UNSAFE.putByte(onHeapStaticMemoryBase, onHeapStaticMemoryOffset + 1, b2); |
| 78 | + |
| 79 | + Asserts.assertEquals(b1, UNSAFE.getByte(onHeapStaticMemoryBase, onHeapStaticMemoryOffset)); |
| 80 | + Asserts.assertEquals(b2, UNSAFE.getByte(onHeapStaticMemoryBase, onHeapStaticMemoryOffset + 1)); |
| 81 | + } |
| 82 | + |
| 83 | + public static void testInstanceField() { |
| 84 | + byte b1 = 0x03; |
| 85 | + byte b2 = 0x04; |
| 86 | + TestMisalignedUnsafeAccess obj = new TestMisalignedUnsafeAccess(); |
| 87 | + |
| 88 | + UNSAFE.putByte(obj, onHeapInstanceMemoryOffset, b1); |
| 89 | + UNSAFE.putByte(obj, onHeapInstanceMemoryOffset + 1, b2); |
| 90 | + |
| 91 | + Asserts.assertEquals(b1, UNSAFE.getByte(obj, onHeapInstanceMemoryOffset)); |
| 92 | + Asserts.assertEquals(b2, UNSAFE.getByte(obj, onHeapInstanceMemoryOffset + 1)); |
| 93 | + } |
| 94 | + |
| 95 | + public static void main(String[] args) { |
| 96 | + testStaticField(); |
| 97 | + testInstanceField(); |
| 98 | + } |
| 99 | +} |
0 commit comments