test/compiler/c2/Test8202414.java

changeset 9686
025ce746a942
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/compiler/c2/Test8202414.java	Wed May 01 22:02:48 2019 +0530
     1.3 @@ -0,0 +1,117 @@
     1.4 +/*
     1.5 + * Copyright (c) 2019, Huawei Technologies Co. Ltd. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/**
    1.28 + * @test
    1.29 + * @bug 8202414
    1.30 + * @summary Unsafe write after primitive array creation may result in array length change
    1.31 + * @requires (os.arch != "sparc") & (os.arch != "sparcv9")
    1.32 + * @run main/othervm compiler.c2.Test8202414
    1.33 + */
    1.34 +
    1.35 +package compiler.c2;
    1.36 +
    1.37 +import sun.misc.Unsafe;
    1.38 +import java.lang.reflect.Field;
    1.39 +import java.security.AccessController;
    1.40 +import java.security.PrivilegedAction;
    1.41 +
    1.42 +public class Test8202414 {
    1.43 +
    1.44 +    public static void main(String[] args) {
    1.45 +        System.err.close();
    1.46 +        int count = 0;
    1.47 +        while (count++ < 120000) {
    1.48 +          test();
    1.49 +        }
    1.50 +    }
    1.51 +
    1.52 +    public static void test() {
    1.53 +        byte[] newBufb = serByte(397);
    1.54 +        short[] newBufs = serShort(397);
    1.55 +        int[] newBufi = serInt(397);
    1.56 +        long[] newBufl = serLong(397);
    1.57 +        if (newBufb.length != 397 || newBufs.length != 397
    1.58 +            || newBufi.length != 397 || newBufl.length != 397) {
    1.59 +            System.out.println("array length internal error");
    1.60 +            throw new RuntimeException("Test failed");
    1.61 +        }
    1.62 +
    1.63 +    }
    1.64 +
    1.65 +    public static byte[] serByte(int bufLen) {
    1.66 +        byte[] buf = new byte[bufLen];
    1.67 +        THE_UNSAFE.putByte(buf, BYTE_ARRAY_BASE_OFFSET + 1, (byte) buf.length);
    1.68 +        System.err.println("ref " + buf);
    1.69 +        return buf;
    1.70 +    }
    1.71 +
    1.72 +    public static short[] serShort(int bufLen) {
    1.73 +        short[] buf = new short[bufLen];
    1.74 +        THE_UNSAFE.putShort(buf, SHORT_ARRAY_BASE_OFFSET + 1, (short) buf.length);
    1.75 +        System.err.println("ref " + buf);
    1.76 +        return buf;
    1.77 +    }
    1.78 +
    1.79 +    public static int[] serInt(int bufLen) {
    1.80 +        int[] buf = new int[bufLen];
    1.81 +        THE_UNSAFE.putInt(buf, INT_ARRAY_BASE_OFFSET + 1, buf.length);
    1.82 +        System.err.println("ref " + buf);
    1.83 +        return buf;
    1.84 +    }
    1.85 +
    1.86 +    public static long[] serLong(int bufLen) {
    1.87 +        long[] buf = new long[bufLen];
    1.88 +        THE_UNSAFE.putLong(buf, LONG_ARRAY_BASE_OFFSET + 1, buf.length);
    1.89 +        System.err.println("ref " + buf);
    1.90 +        return buf;
    1.91 +    }
    1.92 +
    1.93 +    /* Unsafe fields and initialization
    1.94 +     */
    1.95 +    static final Unsafe THE_UNSAFE;
    1.96 +    static final long BYTE_ARRAY_BASE_OFFSET;
    1.97 +    static final long SHORT_ARRAY_BASE_OFFSET;
    1.98 +    static final long INT_ARRAY_BASE_OFFSET;
    1.99 +    static final long LONG_ARRAY_BASE_OFFSET;
   1.100 +    static {
   1.101 +        THE_UNSAFE = (Unsafe) AccessController.doPrivileged (
   1.102 +            new PrivilegedAction<Object>() {
   1.103 +                @Override
   1.104 +                public Object run() {
   1.105 +                    try {
   1.106 +                        Field f = Unsafe.class.getDeclaredField("theUnsafe");
   1.107 +                        f.setAccessible(true);
   1.108 +                        return f.get(null);
   1.109 +                    } catch (NoSuchFieldException | IllegalAccessException e) {
   1.110 +                        throw new Error();
   1.111 +                    }
   1.112 +                }
   1.113 +            }
   1.114 +        );
   1.115 +        BYTE_ARRAY_BASE_OFFSET = THE_UNSAFE.arrayBaseOffset(byte[].class);
   1.116 +        SHORT_ARRAY_BASE_OFFSET = THE_UNSAFE.arrayBaseOffset(short[].class);
   1.117 +        INT_ARRAY_BASE_OFFSET = THE_UNSAFE.arrayBaseOffset(int[].class);
   1.118 +        LONG_ARRAY_BASE_OFFSET = THE_UNSAFE.arrayBaseOffset(long[].class);
   1.119 +    }
   1.120 +}

mercurial