|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, 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 8317121 |
| 27 | + * @summary Test masked vectors and unsafe access to memory modified by arraycopy |
| 28 | + * @requires vm.compiler2.enabled |
| 29 | + * @modules java.base/jdk.internal.misc |
| 30 | + * @library /test/lib / |
| 31 | + * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockDiagnosticVMOptions -XX:-TieredCompilation -Xbatch -XX:CompileCommand=quiet -XX:CompileCommand=compileonly,TestMaskedVectors::test* -XX:+StressLCM -XX:+StressGCM -XX:StressSeed=2210259638 TestMaskedVectors |
| 32 | + * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockDiagnosticVMOptions -XX:-TieredCompilation -Xbatch -XX:CompileCommand=quiet -XX:CompileCommand=compileonly,TestMaskedVectors::test* -XX:+StressLCM -XX:+StressGCM TestMaskedVectors |
| 33 | + */ |
| 34 | + |
| 35 | +import java.lang.reflect.*; |
| 36 | +import java.util.*; |
| 37 | + |
| 38 | +import jdk.internal.misc.Unsafe; |
| 39 | + |
| 40 | +public class TestMaskedVectors { |
| 41 | + |
| 42 | + private static Unsafe UNSAFE = Unsafe.getUnsafe(); |
| 43 | + private static final long BASE_OFFSET = UNSAFE.arrayBaseOffset(byte[].class); |
| 44 | + |
| 45 | + static void testLoadVectorMasked(byte[] src, byte[] dst, int len) { |
| 46 | + byte[] tmp = new byte[64]; |
| 47 | + |
| 48 | + // (3) The LoadVectorMasked is found to be dependent on below arraycopy and |
| 49 | + // therefore scheduled just below it. As a result, the LoadVectorMasked misses the |
| 50 | + // updated elements at index 16..48 and dst will contain incorrect values. |
| 51 | + System.arraycopy(src, 0, tmp, 0, 16); |
| 52 | + |
| 53 | + // (2) The LoadVectorMasked is incorrectly found to be independent of this arraycopy |
| 54 | + // because the LoadVectorMasked has offset 0 whereas the arraycopy writes offset >= 16. |
| 55 | + // The problem is that MemNode::find_previous_store() -> LoadNode::find_previous_arraycopy() |
| 56 | + // -> ArrayCopyNode::modifies does not account for the size of the load. |
| 57 | + System.arraycopy(src, 0, tmp, 16, 48); |
| 58 | + |
| 59 | + // (1) The following arraycopy is expanded into a LoadVectorMasked and a |
| 60 | + // StoreVectorMasked in PhaseMacroExpand::generate_partial_inlining_block(). |
| 61 | + System.arraycopy(tmp, 0, dst, 0, len); |
| 62 | + } |
| 63 | + |
| 64 | + static long testUnsafeGetLong(byte[] src) { |
| 65 | + byte[] tmp = new byte[16]; |
| 66 | + |
| 67 | + // (3) The unsafe load is found to be dependent on below arraycopy and |
| 68 | + // therefore scheduled just below it. As a result, the unsafe load misses the |
| 69 | + // updated elements at index 1..16 and therefore returns an incorrect result. |
| 70 | + System.arraycopy(src, 0, tmp, 0, 16); |
| 71 | + |
| 72 | + // (2) The unsafe load is incorrectly found to be independent of this arraycopy |
| 73 | + // because the load has offset 0 in 'tmp' whereas the arraycopy writes offsets >= 1. |
| 74 | + // The problem is that MemNode::find_previous_store() -> LoadNode::find_previous_arraycopy() |
| 75 | + // -> ArrayCopyNode::modifies does not account for the size of the load. |
| 76 | + System.arraycopy(src, 0, tmp, 1, 15); |
| 77 | + |
| 78 | + // (1) Below unsafe load reads the first 8 (byte) array elements. |
| 79 | + return UNSAFE.getLong(tmp, BASE_OFFSET); |
| 80 | + } |
| 81 | + |
| 82 | + public static void main(String[] args) { |
| 83 | + // Initialize src array with increasing byte values |
| 84 | + byte[] src = new byte[64]; |
| 85 | + for (byte i = 0; i < src.length; ++i) { |
| 86 | + src[i] = (byte)i; |
| 87 | + } |
| 88 | + |
| 89 | + // Compute expected outputs once |
| 90 | + byte[] golden1 = new byte[64]; |
| 91 | + testLoadVectorMasked(src, golden1, 64); |
| 92 | + |
| 93 | + long golden2 = testUnsafeGetLong(src); |
| 94 | + |
| 95 | + // Trigger compilation of test methods and verify the results |
| 96 | + for (int i = 0; i < 50_000; ++i) { |
| 97 | + int len = i % 32; |
| 98 | + byte[] dst = new byte[len]; |
| 99 | + testLoadVectorMasked(src, dst, len); |
| 100 | + |
| 101 | + boolean error = false; |
| 102 | + for (int j = 0; j < dst.length; ++j) { |
| 103 | + if (dst[j] != golden1[j]) { |
| 104 | + System.out.println("Incorrect value of element " + j + ": Expected " + golden1[j] + " but got " + dst[j]); |
| 105 | + error = true; |
| 106 | + } |
| 107 | + } |
| 108 | + if (error) { |
| 109 | + throw new RuntimeException("Test LoadVectorMasked failed"); |
| 110 | + } |
| 111 | + |
| 112 | + long res = testUnsafeGetLong(src); |
| 113 | + if (res != golden2) { |
| 114 | + throw new RuntimeException("Incorrect result in test UnsafeGetLong: Expected " + golden2 + " but got " + res); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments