1 /*
2  * Copyright (c) 2019, 2020, 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 8202414
27  * @summary Unsafe write after primitive array creation may result in array length change
28  * @modules java.base/jdk.internal.misc
29  * @library /test/lib
30  * @run main/othervm compiler.c2.Test8202414
31  */
32 
33 package compiler.c2;
34 
35 import sun.misc.Unsafe;
36 import java.lang.reflect.Field;
37 import java.security.AccessController;
38 import java.security.PrivilegedAction;
39 import jtreg.SkippedException;
40 
41 public class Test8202414 {
42 
main(String[] args)43     public static void main(String[] args) {
44         // Some CPUs (for example, ARM) does not support unaligned
45         // memory accesses. This test may cause JVM crash due to
46         // alignment check failure on such CPUs.
47         if (!jdk.internal.misc.Unsafe.getUnsafe().unalignedAccess()) {
48           throw new SkippedException(
49             "Platform is missing unaligned memory accesses support.");
50         }
51         System.err.close();
52         int count = 0;
53         while (count++ < 120000) {
54           test();
55         }
56     }
57 
test()58     public static void test() {
59         byte[] newBufb = serByte(397);
60         short[] newBufs = serShort(397);
61         int[] newBufi = serInt(397);
62         long[] newBufl = serLong(397);
63         if (newBufb.length != 397 || newBufs.length != 397
64             || newBufi.length != 397 || newBufl.length != 397) {
65             System.out.println("array length internal error");
66             throw new RuntimeException("Test failed");
67         }
68 
69     }
70 
serByte(int bufLen)71     public static byte[] serByte(int bufLen) {
72         byte[] buf = new byte[bufLen];
73         THE_UNSAFE.putByte(buf, BYTE_ARRAY_BASE_OFFSET + 1, (byte) buf.length);
74         System.err.println("ref " + buf);
75         return buf;
76     }
77 
serShort(int bufLen)78     public static short[] serShort(int bufLen) {
79         short[] buf = new short[bufLen];
80         THE_UNSAFE.putShort(buf, SHORT_ARRAY_BASE_OFFSET + 1, (short) buf.length);
81         System.err.println("ref " + buf);
82         return buf;
83     }
84 
serInt(int bufLen)85     public static int[] serInt(int bufLen) {
86         int[] buf = new int[bufLen];
87         THE_UNSAFE.putInt(buf, INT_ARRAY_BASE_OFFSET + 1, buf.length);
88         System.err.println("ref " + buf);
89         return buf;
90     }
91 
serLong(int bufLen)92     public static long[] serLong(int bufLen) {
93         long[] buf = new long[bufLen];
94         THE_UNSAFE.putLong(buf, LONG_ARRAY_BASE_OFFSET + 1, buf.length);
95         System.err.println("ref " + buf);
96         return buf;
97     }
98 
99     /* Unsafe fields and initialization
100      */
101     static final Unsafe THE_UNSAFE;
102     static final long BYTE_ARRAY_BASE_OFFSET;
103     static final long SHORT_ARRAY_BASE_OFFSET;
104     static final long INT_ARRAY_BASE_OFFSET;
105     static final long LONG_ARRAY_BASE_OFFSET;
106     static {
107         THE_UNSAFE = (Unsafe) AccessController.doPrivileged (
108             new PrivilegedAction<Object>() {
109                 @Override
110                 public Object run() {
111                     try {
112                         Field f = Unsafe.class.getDeclaredField("theUnsafe");
113                         f.setAccessible(true);
114                         return f.get(null);
115                     } catch (NoSuchFieldException | IllegalAccessException e) {
116                         throw new Error();
117                     }
118                 }
119             }
120         );
121         BYTE_ARRAY_BASE_OFFSET = THE_UNSAFE.arrayBaseOffset(byte[].class);
122         SHORT_ARRAY_BASE_OFFSET = THE_UNSAFE.arrayBaseOffset(short[].class);
123         INT_ARRAY_BASE_OFFSET = THE_UNSAFE.arrayBaseOffset(int[].class);
124         LONG_ARRAY_BASE_OFFSET = THE_UNSAFE.arrayBaseOffset(long[].class);
125     }
126 }
127