Path: blob/master/test/hotspot/jtreg/compiler/intrinsics/unsafe/TestUnsafeUnalignedMismatchedAccesses.java
41153 views
/*1* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 813647326* @summary Mismatched stores on same slice possible with Unsafe.Put*Unaligned methods27* @modules java.base/jdk.internal.misc:+open28*29* @run main/othervm -XX:-UseOnStackReplacement -XX:-BackgroundCompilation30* compiler.intrinsics.unsafe.TestUnsafeUnalignedMismatchedAccesses31* @run main/othervm -XX:-UseOnStackReplacement -XX:-BackgroundCompilation32* -XX:+UnlockDiagnosticVMOptions -XX:-UseUnalignedAccesses33* compiler.intrinsics.unsafe.TestUnsafeUnalignedMismatchedAccesses34*/3536package compiler.intrinsics.unsafe;3738import jdk.internal.misc.Unsafe;3940import java.lang.reflect.Field;4142public class TestUnsafeUnalignedMismatchedAccesses {4344private static final Unsafe UNSAFE;4546static {47try {48Field unsafeField = Unsafe.class.getDeclaredField("theUnsafe");49unsafeField.setAccessible(true);50UNSAFE = (Unsafe) unsafeField.get(null);51}52catch (Exception e) {53throw new AssertionError(e);54}55}5657static void test1(byte[] array) {58array[0] = 0;59UNSAFE.putIntUnaligned(array, UNSAFE.ARRAY_BYTE_BASE_OFFSET, 0);60array[0] = 0;61}6263static void test2(byte[] array) {64array[0] = 0;65UNSAFE.putIntUnaligned(array, UNSAFE.ARRAY_BYTE_BASE_OFFSET+1, 0);66array[0] = 0;67}6869static void test3(byte[] array) {70array[0] = 0;71UNSAFE.putIntUnaligned(array, UNSAFE.ARRAY_BYTE_BASE_OFFSET+2, 0);72array[0] = 0;73}7475static void test4(byte[] array) {76array[0] = 0;77UNSAFE.putIntUnaligned(array, UNSAFE.ARRAY_BYTE_BASE_OFFSET+3, 0);78array[0] = 0;79}8081static void test5(byte[] array) {82array[0] = 0;83UNSAFE.putInt(array, UNSAFE.ARRAY_BYTE_BASE_OFFSET, 0);84array[0] = 0;85}8687// unaligned access and non escaping allocation88static void test6() {89byte[] array = new byte[10];90UNSAFE.putIntUnaligned(array, UNSAFE.ARRAY_BYTE_BASE_OFFSET+1, -1);91array[0] = 0;92}9394// unaligned access and non escaping allocation95static int test7() {96byte[] array = new byte[10];97UNSAFE.putIntUnaligned(array, UNSAFE.ARRAY_BYTE_BASE_OFFSET+1, -1);98array[0] = 0;99array[2] = 0;100return array[0] + array[1] + array[2] + array[3] + array[4];101}102103// unaligned access with vectorization104static void test8(int[] src1, int[] src2, int[] dst) {105for (int i = 0; i < dst.length-1; i++) {106int res = src1[i] + src2[i];107UNSAFE.putIntUnaligned(dst, UNSAFE.ARRAY_INT_BASE_OFFSET + i*4+1, res);108}109}110111static public void main(String[] args) throws Exception {112byte[] byte_array = new byte[100];113int[] int_array = new int[100];114Object[] obj_array = new Object[100];115TestUnsafeUnalignedMismatchedAccesses test = new TestUnsafeUnalignedMismatchedAccesses();116for (int i = 0; i < 20000; i++) {117test1(byte_array);118test2(byte_array);119test3(byte_array);120test4(byte_array);121test5(byte_array);122test6();123test7();124test8(int_array, int_array, int_array);125}126}127}128129130