Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/hotspot/jtreg/compiler/arraycopy/TestObjectArrayClone.java
64474 views
1
/*
2
* Copyright (c) 2016, 2021, 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 8155643 8268125 8270461 8270098
27
* @summary Test Object.clone() intrinsic.
28
* @modules java.base/java.lang:+open
29
*
30
* @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-ReduceInitialCardMarks
31
* -XX:CompileCommand=compileonly,compiler.arraycopy.TestObjectArrayClone::testClone*
32
* -XX:CompileCommand=compileonly,jdk.internal.reflect.GeneratedMethodAccessor*::invoke
33
* compiler.arraycopy.TestObjectArrayClone
34
* @run main/othervm -XX:CompileCommand=compileonly,compiler.arraycopy.TestObjectArrayClone::testClone*
35
* -XX:CompileCommand=compileonly,jdk.internal.reflect.GeneratedMethodAccessor*::invoke
36
* compiler.arraycopy.TestObjectArrayClone
37
* @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-UseCompressedClassPointers -Xmx128m
38
* -XX:CompileCommand=compileonly,compiler.arraycopy.TestObjectArrayClone::testClone*
39
* -XX:CompileCommand=compileonly,jdk.internal.reflect.GeneratedMethodAccessor*::invoke
40
* compiler.arraycopy.TestObjectArrayClone
41
* @run main/othervm -Xbatch -XX:-UseTypeProfile
42
* -XX:CompileCommand=compileonly,compiler.arraycopy.TestObjectArrayClone::testClone*
43
* -XX:CompileCommand=compileonly,jdk.internal.reflect.GeneratedMethodAccessor*::invoke
44
* compiler.arraycopy.TestObjectArrayClone
45
*/
46
47
package compiler.arraycopy;
48
49
import java.lang.invoke.*;
50
import java.lang.reflect.InvocationTargetException;
51
import java.lang.reflect.Method;
52
53
class Payload implements Cloneable {
54
boolean b;
55
int i;
56
char c;
57
String str;
58
short s;
59
int i2;
60
61
public Payload(boolean b, int i, char c, String str, short s, int i2) {
62
super();
63
this.b = b;
64
this.i = i;
65
this.c = c;
66
this.str = str;
67
this.s = s;
68
this.i2 = i2;
69
}
70
71
public Payload clonep() {
72
try {
73
return (Payload) super.clone();
74
} catch (CloneNotSupportedException e) {
75
return null;
76
}
77
}
78
}
79
80
class Payload2 implements Cloneable {
81
boolean b;
82
int i;
83
char c;
84
String str;
85
short s;
86
int i2;
87
boolean b2;
88
int i3;
89
char c2;
90
String str2;
91
short s2;
92
int i4;
93
94
public Payload2(boolean b, int i, char c, String str, short s, int i2, boolean b2, int i3, char c2, String str2,
95
short s2, int i4) {
96
super();
97
this.b = b;
98
this.i = i;
99
this.c = c;
100
this.str = str;
101
this.s = s;
102
this.i2 = i2;
103
this.b2 = b2;
104
this.i3 = i3;
105
this.c2 = c2;
106
this.str2 = str2;
107
this.s2 = s2;
108
this.i4 = i4;
109
}
110
111
public Payload2 clonep() {
112
try {
113
return (Payload2) super.clone();
114
} catch(CloneNotSupportedException e) {
115
return null;
116
}
117
}
118
}
119
120
public class TestObjectArrayClone {
121
122
public static String[] escape_arr;
123
124
public static String str1 = new String("1");
125
public static String str2 = new String("2");
126
public static String str3 = new String("3");
127
public static String str4 = new String("4");
128
public static String str5 = new String("5");
129
130
public static String[] testCloneObjectArray(String[] arr) {
131
return arr.clone();
132
}
133
134
public static String[] testCloneObjectArrayCopy(String[] arr) {
135
String[] arr2 = new String[arr.length];
136
System.arraycopy(arr, 0, arr2, 0, arr.length);
137
return arr2;
138
}
139
140
public static String[] testCloneShortObjectArray() {
141
String[] arr = new String[5];
142
arr[0] = str1;
143
arr[1] = str2;
144
arr[2] = str3;
145
arr[3] = str4;
146
arr[4] = str5;
147
escape_arr = arr;
148
return arr.clone();
149
}
150
151
public static String[] testCloneShortObjectArray2(Method clone) throws Exception {
152
String[] arr = new String[5];
153
arr[0] = str1;
154
arr[1] = str2;
155
arr[2] = str3;
156
arr[3] = str4;
157
arr[4] = str5;
158
escape_arr = arr;
159
return (String[]) testCloneObject(clone, arr);
160
}
161
162
public static String[] testCloneShortObjectArrayCopy() {
163
String[] arr = new String[5];
164
arr[0] = str1;
165
arr[1] = str2;
166
arr[2] = str3;
167
arr[3] = str4;
168
arr[4] = str5;
169
escape_arr = arr;
170
String[] arr2 = new String[arr.length];
171
System.arraycopy(arr, 0, arr2, 0, arr.length);
172
return arr2;
173
}
174
175
public static int[] testClonePrimitiveArray(int[] arr) {
176
return arr.clone();
177
}
178
179
public static Object testCloneOop(Payload p) {
180
return p.clonep();
181
}
182
183
public static Object testCloneOop2(Payload2 p) {
184
return p.clonep();
185
}
186
187
public static Object testCloneObject(Method clone, Object obj) throws Exception {
188
return clone.invoke(obj);
189
}
190
191
public static void main(String[] args) throws Exception {
192
Method clone = Object.class.getDeclaredMethod("clone");
193
clone.setAccessible(true);
194
195
String[] arr1 = new String[42];
196
for (int j = 0; j < arr1.length; j++) {
197
arr1[j] = new String(Integer.toString(j));
198
}
199
200
for (int i = 0; i < 50_000; i++) {
201
String[] arr2 = testCloneObjectArray(arr1);
202
verifyStr(arr1, arr2);
203
String[] arr3 = testCloneObjectArray(arr1);
204
verifyStr(arr1, arr3);
205
String[] arr4 = testCloneObjectArray(arr1);
206
verifyStr(arr1, arr4);
207
verifyStr(arr1, arr3);
208
verifyStr(arr1, arr2);
209
}
210
211
for (int i = 0; i < 50_000; i++) {
212
for (int j = 0; j < arr1.length; j++) {
213
arr1[j] = new String(Integer.toString(j));
214
}
215
String[] arr2 = (String[]) testCloneObject(clone, arr1);
216
verifyStr(arr1, arr2);
217
String[] arr3 = (String[]) testCloneObject(clone, arr1);
218
verifyStr(arr1, arr3);
219
String[] arr4 = (String[]) testCloneObject(clone, arr1);
220
verifyStr(arr1, arr4);
221
verifyStr(arr1, arr3);
222
verifyStr(arr1, arr2);
223
}
224
225
for (int i = 0; i < 50_000; i++) {
226
String[] value = testCloneShortObjectArray();
227
verifyStr(value, escape_arr);
228
String[] value2 = testCloneShortObjectArray();
229
verifyStr(value2, escape_arr);
230
String[] value3 = testCloneShortObjectArray();
231
verifyStr(value3, escape_arr);
232
String[] value4 = testCloneShortObjectArray2(clone);
233
verifyStr(value4, escape_arr);
234
verifyStr(value, value4);
235
verifyStr(value, value3);
236
verifyStr(value, value2);
237
}
238
239
for (int i = 0; i < 50_000; i++) {
240
String[] arr2 = testCloneObjectArrayCopy(arr1);
241
verifyStr(arr1, arr2);
242
String[] arr3 = testCloneObjectArrayCopy(arr1);
243
verifyStr(arr1, arr3);
244
String[] arr4 = testCloneObjectArrayCopy(arr1);
245
verifyStr(arr1, arr4);
246
verifyStr(arr1, arr3);
247
verifyStr(arr1, arr2);
248
}
249
250
for (int i = 0; i < 50_000; i++) {
251
String[] value = testCloneShortObjectArrayCopy();
252
verifyStr(value, escape_arr);
253
String[] value2 = testCloneShortObjectArrayCopy();
254
verifyStr(value2, escape_arr);
255
String[] value3 = testCloneShortObjectArrayCopy();
256
verifyStr(value3, escape_arr);
257
verifyStr(value, value3);
258
verifyStr(value, value2);
259
}
260
261
int[] arr2 = new int[42];
262
for (int i = 0; i < arr2.length; i++) {
263
arr2[i] = i;
264
}
265
for (int i = 0; i < 50_000; i++) {
266
int[] res1 = testClonePrimitiveArray(arr2);
267
int[] res2 = (int[])testCloneObject(clone, arr2);
268
for (int j = 0; j < arr2.length; j++) {
269
if (res1[j] != j) {
270
throw new RuntimeException("Unexpected result: " + res1[j] + " != " + j);
271
}
272
if (res2[j] != j) {
273
throw new RuntimeException("Unexpected result: " + res2[j] + " != " + j);
274
}
275
}
276
}
277
278
Payload ref = new Payload(false, -1, 'c', str1, (short) 5, -1);
279
for (int i = 0; i < 50_000; i++) {
280
Payload p1 = (Payload) testCloneOop(ref);
281
verifyPayload(ref, p1);
282
Payload p2 = (Payload) testCloneOop(ref);
283
verifyPayload(ref, p2);
284
Payload p3 = (Payload) testCloneOop(ref);
285
verifyPayload(ref, p3);
286
verifyPayload(p2, p3);
287
verifyPayload(p1, p3);
288
}
289
290
for (int i = 0; i < 50_000; i++) {
291
Payload p1 = (Payload) testCloneObject(clone, ref);
292
verifyPayload(ref, p1);
293
Payload p2 = (Payload) testCloneObject(clone, ref);
294
verifyPayload(ref, p2);
295
Payload p3 = (Payload) testCloneObject(clone, ref);
296
verifyPayload(ref, p3);
297
verifyPayload(p2, p3);
298
verifyPayload(p1, p3);
299
}
300
301
Payload2 ref2 = new Payload2(false, -1, 'c', str1, (short) 5, -1, false, 0, 'k', str2, (short)-1, 0);
302
for (int i = 0; i < 50_000; i++) {
303
Payload2 p1 = (Payload2) testCloneOop2(ref2);
304
verifyPayload2(ref2, p1);
305
Payload2 p2 = (Payload2) testCloneOop2(ref2);
306
verifyPayload2(ref2, p2);
307
Payload2 p3 = (Payload2) testCloneOop2(ref2);
308
verifyPayload2(ref2, p3);
309
verifyPayload2(p2, p3);
310
verifyPayload2(p1, p3);
311
}
312
313
for (int i = 0; i < 50_000; i++) {
314
Payload2 p1 = (Payload2) testCloneObject(clone, ref2);
315
verifyPayload2(ref2, p1);
316
Payload2 p2 = (Payload2) testCloneObject(clone, ref2);
317
verifyPayload2(ref2, p2);
318
Payload2 p3 = (Payload2) testCloneObject(clone, ref2);
319
verifyPayload2(ref2, p3);
320
verifyPayload2(p2, p3);
321
verifyPayload2(p1, p3);
322
}
323
}
324
325
public static void verifyPayload(Payload p1, Payload p2) {
326
if (p1.b != p2.b) {
327
throw new RuntimeException("b is wrong");
328
}
329
if (p1.c != p2.c) {
330
throw new RuntimeException("c is wrong");
331
}
332
if (p1.i != p2.i) {
333
throw new RuntimeException("i is wrong");
334
}
335
if (p1.s != p2.s) {
336
throw new RuntimeException("s is wrong");
337
}
338
if (p1.i2 != p2.i2) {
339
throw new RuntimeException("i2 is wrong");
340
}
341
if (p1.str != p2.str) {
342
throw new RuntimeException("str is wrong");
343
}
344
if (!p1.str.equals(p2.str)) {
345
throw new RuntimeException("str content is wrong");
346
}
347
}
348
349
public static void verifyPayload2(Payload2 p1, Payload2 p2) {
350
if (p1.b != p2.b) {
351
throw new RuntimeException("b is wrong");
352
}
353
if (p1.c != p2.c) {
354
throw new RuntimeException("c is wrong");
355
}
356
if (p1.i != p2.i) {
357
throw new RuntimeException("i is wrong");
358
}
359
if (p1.s != p2.s) {
360
throw new RuntimeException("s is wrong");
361
}
362
if (p1.i2 != p2.i2) {
363
throw new RuntimeException("i2 is wrong");
364
}
365
if (p1.str != p2.str) {
366
throw new RuntimeException("str is wrong");
367
}
368
if (!p1.str.equals(p2.str)) {
369
throw new RuntimeException("str content is wrong");
370
}
371
if (p1.b2 != p2.b2) {
372
throw new RuntimeException("b is wrong");
373
}
374
if (p1.c2 != p2.c2) {
375
throw new RuntimeException("c is wrong");
376
}
377
if (p1.i3 != p2.i3) {
378
throw new RuntimeException("i is wrong");
379
}
380
if (p1.s2 != p2.s2) {
381
throw new RuntimeException("s is wrong");
382
}
383
if (p1.i4 != p2.i4) {
384
throw new RuntimeException("i2 is wrong");
385
}
386
if (p1.str2 != p2.str2) {
387
throw new RuntimeException("str is wrong");
388
}
389
if (!p1.str2.equals(p2.str2)) {
390
throw new RuntimeException("str content is wrong");
391
}
392
}
393
394
public static void verifyStr(String[] arr1, String[] arr2) {
395
if (arr1 == arr2) {
396
throw new RuntimeException("Must not be the same");
397
}
398
if (arr1.length != arr2.length) {
399
throw new RuntimeException("Must have the same length");
400
}
401
for (int i = 0; i < arr1.length; i++) {
402
if (arr1[i] != arr2[i]) {
403
throw new RuntimeException("Fail cloned element not the same: " + i);
404
}
405
if (!arr1[i].equals(arr2[i])) {
406
throw new RuntimeException("Fail cloned element content not the same");
407
}
408
}
409
}
410
}
411
412
413