File: TestArrayCopyIntrinsicWithUCT.java

package info (click to toggle)
openjdk-11 11.0.28%2B6-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 781,124 kB
  • sloc: java: 5,208,481; xml: 1,192,267; cpp: 1,138,346; ansic: 461,925; javascript: 162,416; sh: 16,738; objc: 13,729; python: 4,757; asm: 3,570; makefile: 2,965; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (312 lines) | stat: -rw-r--r-- 12,269 bytes parent folder | download | duplicates (16)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
 * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 8297730
 * @summary Test taking UCT between array allocation and array copy to report correct exception.
 * @library /test/lib
 * @run main/othervm -Xcomp -XX:-TieredCompilation
 *                   -XX:CompileCommand=compileonly,compiler.arraycopy.TestArrayCopyIntrinsicWithUCT::test*
 *                   compiler.arraycopy.TestArrayCopyIntrinsicWithUCT
 */

package compiler.arraycopy;

import jdk.test.lib.Asserts;

import java.util.function.Function;
import java.util.function.Supplier;

public class TestArrayCopyIntrinsicWithUCT {
    static int zero = 0;
    static int zero2 = 0;
    static int minusOne = -1;
    static int iFld;
    static int iFld2;
    static boolean flag;
    static byte[] byArrNull = null;
    static A aFld = null;

    static public void main(String[] args) {
        System.out.println("Start"); // Ensure loaded.
        new A(); // Ensure loaded
        runNegativeSize(TestArrayCopyIntrinsicWithUCT::testNegativeSize);
        runNegativeSize(TestArrayCopyIntrinsicWithUCT::testNegativeSize2);
        runNegativeSize(TestArrayCopyIntrinsicWithUCT::testNegativeFldSize);
        runNegativeSize(TestArrayCopyIntrinsicWithUCT::testNegativeFldSize2);
        runNegativeSize(TestArrayCopyIntrinsicWithUCT::testNegativeSizeStore);
        runNegativeSize(TestArrayCopyIntrinsicWithUCT::testNegativeSizeStore2);
        runNegativeSize(TestArrayCopyIntrinsicWithUCT::testNegativeSizeDivZero);
        runNegativeSize(TestArrayCopyIntrinsicWithUCT::testNegativeSizeDivZero2);
        runNegativeSize(TestArrayCopyIntrinsicWithUCT::testNegativeSizeDivZero3);
        runNegativeSize(TestArrayCopyIntrinsicWithUCT::testNegativeSizeDivZero4);
        runNegativeSize(TestArrayCopyIntrinsicWithUCT::testNegativeSizeDivZero5);
        runNegativeSize(TestArrayCopyIntrinsicWithUCT::testNegativeSizeDivZero6);
        runNegativeSize(TestArrayCopyIntrinsicWithUCT::testNegativeSizeDivZero7);
        runNegativeSize(TestArrayCopyIntrinsicWithUCT::testNegativeSizeDivZeroFld);
        runNegativeSize(TestArrayCopyIntrinsicWithUCT::testNegativeSizeDivZeroFld2);
        runNegativeSize(TestArrayCopyIntrinsicWithUCT::testNegativeSizeDivZeroFld3);
        runNegativeSize(TestArrayCopyIntrinsicWithUCT::testNegativeSizeDivZeroFld4);
        runNegativeSize(TestArrayCopyIntrinsicWithUCT::testNegativeSizeDivZeroFld5);
        runNegativeSize(TestArrayCopyIntrinsicWithUCT::testNegativeSizeDivZeroFld6);
        runNegativeSize(TestArrayCopyIntrinsicWithUCT::testNegativeSizeDivZeroFld7);
        runNegativeSize(TestArrayCopyIntrinsicWithUCT::testNegativeSizeDivZeroNullPointer);
        runNegativeSize(TestArrayCopyIntrinsicWithUCT::testNegativeSizeComplex);
        runNegativeSize(TestArrayCopyIntrinsicWithUCT::testNegativeControlFlowNotAllowed);
        flag = false;
        runNegativeSizeHalf();
        runNegativeSizeHalf();
    }

    static void runNegativeSize(Supplier<byte[]> testMethod) {
        try {
            testMethod.get();
            Asserts.fail("should throw exception");
        } catch (NegativeArraySizeException e) {
            // Expected
        }
    }

    static void runNegativeSize(Function<byte[], byte[]> testMethod) {
        try {
            testMethod.apply(null);
            Asserts.fail("should throw exception");
        } catch (NegativeArraySizeException e) {
            // Expected
        }
    }

    static byte[] testNegativeSize(byte[] byArr) {
        byte[] b = new byte[-1]; // throws NegativeArraySizeException
        int len = byArr.length; // null check trap would fail
        System.arraycopy(byArr, 0, b, 0, len);
        return b;
    }

    static byte[] testNegativeSize2() {
        byte[] byArr = new byte[8];
        byte[] b = new byte[-1]; // throws NegativeArraySizeException
        int len = byArrNull.length; // null check trap would fail
        System.arraycopy(byArr, 0, b, 0, len);
        return b;
    }

    static byte[] testNegativeFldSize(byte[] byArr) {
        byte[] b = new byte[minusOne]; // throws NegativeArraySizeException
        int len = byArr.length; // null check trap would fail
        System.arraycopy(byArr, 0, b, 0, len);
        return b;
    }
    static byte[] testNegativeFldSize2() {
        byte[] byArr = new byte[8];
        byte[] b = new byte[minusOne]; // throws NegativeArraySizeException
        int len = byArrNull.length; // null check trap would fail
        System.arraycopy(byArr, 0, b, 0, len);
        return b;
    }

    static byte[] testNegativeSizeStore(byte[] byArr) {
        byte[] b = new byte[minusOne]; // throws NegativeArraySizeException
        iFld++; // Since we have a store here, we do not move the allocation down
        int len = byArr.length; // null check trap would fail
        System.arraycopy(byArr, 0, b, 0, len);
        return b;
    }

    static byte[] testNegativeSizeStore2() {
        byte[] byArr = new byte[8];
        byte[] b = new byte[minusOne]; // throws NegativeArraySizeException
        iFld++; // Since we have a store here, we do not move the allocation down
        int len = byArrNull.length; // null check trap would fail
        System.arraycopy(byArr, 0, b, 0, len);
        return b;
    }

    static byte[] testNegativeSizeDivZero(byte[] byArr) {
        byte[] b = new byte[-1]; // throws NegativeArraySizeException
        int len = 8 / zero; // div by zero trap would fail
        System.arraycopy(byArr, 0, b, 0, len);
        return b;
    }

    static byte[] testNegativeSizeDivZero2() {
        byte[] byArr = new byte[8];
        byte[] b = new byte[-1]; // throws NegativeArraySizeException
        int len = 8 / zero; // div by zero trap would fail
        System.arraycopy(byArr, 0, b, 0, len);
        return b;
    }

    static byte[] testNegativeSizeDivZero3(byte[] byArr) {
        byte[] b = new byte[-1]; // throws NegativeArraySizeException
        int len = 8 / zero; // div by zero trap would fail
        System.arraycopy(byArr, 0, b, 0, iFld2);
        iFld = len;
        return b;
    }

    static byte[] testNegativeSizeDivZero4(byte[] byArr) {
        byte[] b = new byte[-1]; // throws NegativeArraySizeException
        int len = 8 / zero / zero2; // 2 div by zero traps would fail
        System.arraycopy(byArr, 0, b, 0, iFld2);
        iFld = len;
        return b;
    }

    static byte[] testNegativeSizeDivZero5(byte[] byArr) {
        byte[] b = new byte[minusOne]; // throws NegativeArraySizeException
        int len = 8 / zero / zero2; // 2 div by zero traps would fail
        System.arraycopy(byArr, 0, b, 0, iFld2);
        iFld = len;
        return b;
    }

    static byte[] testNegativeSizeDivZero6(byte[] byArr) {
        byte[] b = new byte[minusOne]; // throws NegativeArraySizeException
        int len = 8 / zero / zero2; // 2 div by zero traps would fail
        System.arraycopy(byArr, 0, b, 0, 8);
        iFld = len;
        return b;
    }

    static byte[] testNegativeSizeDivZero7(byte[] byArr) {
        byte[] b = new byte[-1]; // throws NegativeArraySizeException
        int len = 8 / zero / zero2; // 2 div by zero traps would fail
        System.arraycopy(byArr, 0, b, 0, 8);
        iFld = len;
        return b;
    }

    static byte[] testNegativeSizeDivZeroFld(byte[] byArr) {
        byte[] b = new byte[-1]; // throws NegativeArraySizeException
        int len = minusOne / zero; // div by zero trap would fail
        System.arraycopy(byArr, 0, b, 0, len);
        return b;
    }

    static byte[] testNegativeSizeDivZeroFld2() {
        byte[] byArr = new byte[8];
        byte[] b = new byte[-1]; // throws NegativeArraySizeException
        int len = minusOne / zero; // div by zero trap would fail
        System.arraycopy(byArr, 0, b, 0, len);
        return b;
    }

    static byte[] testNegativeSizeDivZeroFld3(byte[] byArr) {
        byte[] b = new byte[-1]; // throws NegativeArraySizeException
        int len = minusOne / zero; // div by zero trap would fail
        System.arraycopy(byArr, 0, b, 0, iFld2);
        iFld = len;
        return b;
    }

    static byte[] testNegativeSizeDivZeroFld4(byte[] byArr) {
        byte[] b = new byte[-1]; // throws NegativeArraySizeException
        int len = minusOne / zero / zero2; // div by zero trap would fail
        System.arraycopy(byArr, 0, b, 0, iFld2);
        iFld = len;
        return b;
    }

    static byte[] testNegativeSizeDivZeroFld5(byte[] byArr) {
        byte[] b = new byte[minusOne]; // throws NegativeArraySizeException
        int len = minusOne / zero / zero2; // div by zero trap would fail
        System.arraycopy(byArr, 0, b, 0, iFld2);
        iFld = len;
        return b;
    }

    static byte[] testNegativeSizeDivZeroFld6(byte[] byArr) {
        byte[] b = new byte[minusOne]; // throws NegativeArraySizeException
        int len = minusOne / zero / zero2; // div by zero trap would fail
        System.arraycopy(byArr, 0, b, 0, 8);
        iFld = len;
        return b;
    }

    static byte[] testNegativeSizeDivZeroFld7(byte[] byArr) {
        byte[] b = new byte[-1]; // throws NegativeArraySizeException
        int len = minusOne / zero / zero2; // div by zero trap would fail
        System.arraycopy(byArr, 0, b, 0, 8);
        iFld = len;
        return b;
    }

    static byte[] testNegativeSizeDivZeroNullPointer(byte[] byArr) {
        byte[] b = new byte[minusOne]; // throws NegativeArraySizeException
        int x = minusOne / zero / zero2; // div by zero trap would fail
        int len = byArr.length;
        System.arraycopy(byArr, 0, b, 0, len);
        iFld = x;
        return b;
    }

    static byte[] testNegativeSizeComplex(byte[] byArr) {
        byte[] b = new byte[minusOne]; // throws NegativeArraySizeException
        int x = minusOne / zero; // div by zero trap would fail
        int y = aFld.i;
        int len = byArr.length;
        x = x + aFld.i2 / zero2;
        System.arraycopy(byArr, 0, b, 0, x);
        iFld = x + y;
        return b;
    }

    // Optimization not applied because of additional control flow that is not considered safe.
    static byte[] testNegativeControlFlowNotAllowed(byte[] byArr) {
        int x = 23;
        byte[] b = new byte[minusOne]; // throws NegativeArraySizeException
        if (flag) {
            x = 34;
        }
        int len = x / zero;
        System.arraycopy(byArr, 0, b, 0, 8);
        iFld = len;
        return b;
    }

    static void runNegativeSizeHalf() {
        try {
            testNegativeSizeHalf(null);
            Asserts.fail("should throw exception");
        } catch (NegativeArraySizeException e) {
            Asserts.assertTrue(flag, "wrongly caught NegativeArraySizeException");
        } catch (NullPointerException e) {
            Asserts.assertFalse(flag, "wrongly caught NullPointerException");
        }
        flag = !flag;
    }

    static byte[] testNegativeSizeHalf(byte[] byArr) {
        int size = flag ? -1 : 1;
        byte[] b = new byte[size]; // throws NegativeArraySizeException if size == -1
        int len = byArr.length; // throws NullPointerException if size == 1
        System.arraycopy(byArr, 0, b, 0, len);
        return b;
    }
}

class A {
    int i, i2;
}