|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, 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 8302850 |
| 27 | + * @summary Tests that an array clone call that has been compiled with C1 |
| 28 | + * handles null values correctly. |
| 29 | + * @run main/othervm -XX:-UseOnStackReplacement -XX:-BackgroundCompilation -XX:TieredStopAtLevel=1 |
| 30 | + * -XX:CompileOnly=compiler.c1.TestNullArrayClone::testClone* -XX:+UnlockExperimentalVMOptions |
| 31 | + * compiler.c1.TestNullArrayClone |
| 32 | + */ |
| 33 | +package compiler.c1; |
| 34 | + |
| 35 | +import java.util.concurrent.ThreadLocalRandom; |
| 36 | + |
| 37 | +public class TestNullArrayClone { |
| 38 | + static final int ITER = 2000; // ~ Tier3CompileThreshold |
| 39 | + static final int ARRAY_SIZE = 999; |
| 40 | + |
| 41 | + public static void main(String[] args) { |
| 42 | + testInts(); |
| 43 | + testLongs(); |
| 44 | + testBytes(); |
| 45 | + } |
| 46 | + |
| 47 | + private static void testInts() { |
| 48 | + final int[] arr = new int[ARRAY_SIZE]; |
| 49 | + for (int i = 0; i < arr.length; i++) { |
| 50 | + arr[i] = ThreadLocalRandom.current().nextInt(); |
| 51 | + } |
| 52 | + |
| 53 | + for (int i = 0; i < ITER; i++) { |
| 54 | + int[] result = testClonePrimitiveInt(arr); |
| 55 | + if (result.length != arr.length) { |
| 56 | + throw new RuntimeException("Unexpected clone length: source array length " + arr.length + " != clone array length " + result.length); |
| 57 | + } |
| 58 | + for (int j = 0; j < arr.length; j++) { |
| 59 | + if (result[j] != arr[j]) { |
| 60 | + throw new RuntimeException("Unexpected result: " + result[j] + " != " + j); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + try { |
| 66 | + testClonePrimitiveInt(null); |
| 67 | + throw new RuntimeException("Expected NullPointerException to be thrown"); |
| 68 | + } catch (NullPointerException e) { |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private static void testLongs() { |
| 73 | + final long[] arr = new long[ARRAY_SIZE]; |
| 74 | + for (int i = 0; i < arr.length; i++) { |
| 75 | + arr[i] = ThreadLocalRandom.current().nextLong(); |
| 76 | + } |
| 77 | + |
| 78 | + for (int i = 0; i < ITER; i++) { |
| 79 | + long[] result = testClonePrimitiveLong(arr); |
| 80 | + if (result.length != arr.length) { |
| 81 | + throw new RuntimeException("Unexpected clone length: source array length " + arr.length + " != clone array length " + result.length); |
| 82 | + } |
| 83 | + for (int j = 0; j < arr.length; j++) { |
| 84 | + if (result[j] != arr[j]) { |
| 85 | + throw new RuntimeException("Unexpected result: " + result[j] + " != " + j); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + try { |
| 91 | + testClonePrimitiveLong(null); |
| 92 | + throw new RuntimeException("Expected NullPointerException to be thrown"); |
| 93 | + } catch (NullPointerException e) { |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private static void testBytes() { |
| 98 | + final byte[] arr = new byte[ARRAY_SIZE]; |
| 99 | + for (int i = 0; i < arr.length; i++) { |
| 100 | + arr[i] = (byte) ThreadLocalRandom.current().nextInt(); |
| 101 | + } |
| 102 | + |
| 103 | + for (int i = 0; i < ITER; i++) { |
| 104 | + byte[] result = testClonePrimitiveBytes(arr); |
| 105 | + if (result.length != arr.length) { |
| 106 | + throw new RuntimeException("Unexpected clone length: source array length " + arr.length + " != clone array length " + result.length); |
| 107 | + } |
| 108 | + for (int j = 0; j < arr.length; j++) { |
| 109 | + if (result[j] != arr[j]) { |
| 110 | + throw new RuntimeException("Unexpected result: " + result[j] + " != " + j); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + try { |
| 116 | + testClonePrimitiveBytes(null); |
| 117 | + throw new RuntimeException("Expected NullPointerException to be thrown"); |
| 118 | + } catch (NullPointerException e) { |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + static int[] testClonePrimitiveInt(int[] ints) { |
| 123 | + return ints.clone(); |
| 124 | + } |
| 125 | + |
| 126 | + static long[] testClonePrimitiveLong(long[] longs) { |
| 127 | + return longs.clone(); |
| 128 | + } |
| 129 | + |
| 130 | + static byte[] testClonePrimitiveBytes(byte[] bytes) { |
| 131 | + return bytes.clone(); |
| 132 | + } |
| 133 | +} |
0 commit comments