Path: blob/master/test/hotspot/jtreg/compiler/arraycopy/TestObjectArrayClone.java
64474 views
/*1* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 8155643 8268125 8270461 827009826* @summary Test Object.clone() intrinsic.27* @modules java.base/java.lang:+open28*29* @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-ReduceInitialCardMarks30* -XX:CompileCommand=compileonly,compiler.arraycopy.TestObjectArrayClone::testClone*31* -XX:CompileCommand=compileonly,jdk.internal.reflect.GeneratedMethodAccessor*::invoke32* compiler.arraycopy.TestObjectArrayClone33* @run main/othervm -XX:CompileCommand=compileonly,compiler.arraycopy.TestObjectArrayClone::testClone*34* -XX:CompileCommand=compileonly,jdk.internal.reflect.GeneratedMethodAccessor*::invoke35* compiler.arraycopy.TestObjectArrayClone36* @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-UseCompressedClassPointers -Xmx128m37* -XX:CompileCommand=compileonly,compiler.arraycopy.TestObjectArrayClone::testClone*38* -XX:CompileCommand=compileonly,jdk.internal.reflect.GeneratedMethodAccessor*::invoke39* compiler.arraycopy.TestObjectArrayClone40* @run main/othervm -Xbatch -XX:-UseTypeProfile41* -XX:CompileCommand=compileonly,compiler.arraycopy.TestObjectArrayClone::testClone*42* -XX:CompileCommand=compileonly,jdk.internal.reflect.GeneratedMethodAccessor*::invoke43* compiler.arraycopy.TestObjectArrayClone44*/4546package compiler.arraycopy;4748import java.lang.invoke.*;49import java.lang.reflect.InvocationTargetException;50import java.lang.reflect.Method;5152class Payload implements Cloneable {53boolean b;54int i;55char c;56String str;57short s;58int i2;5960public Payload(boolean b, int i, char c, String str, short s, int i2) {61super();62this.b = b;63this.i = i;64this.c = c;65this.str = str;66this.s = s;67this.i2 = i2;68}6970public Payload clonep() {71try {72return (Payload) super.clone();73} catch (CloneNotSupportedException e) {74return null;75}76}77}7879class Payload2 implements Cloneable {80boolean b;81int i;82char c;83String str;84short s;85int i2;86boolean b2;87int i3;88char c2;89String str2;90short s2;91int i4;9293public Payload2(boolean b, int i, char c, String str, short s, int i2, boolean b2, int i3, char c2, String str2,94short s2, int i4) {95super();96this.b = b;97this.i = i;98this.c = c;99this.str = str;100this.s = s;101this.i2 = i2;102this.b2 = b2;103this.i3 = i3;104this.c2 = c2;105this.str2 = str2;106this.s2 = s2;107this.i4 = i4;108}109110public Payload2 clonep() {111try {112return (Payload2) super.clone();113} catch(CloneNotSupportedException e) {114return null;115}116}117}118119public class TestObjectArrayClone {120121public static String[] escape_arr;122123public static String str1 = new String("1");124public static String str2 = new String("2");125public static String str3 = new String("3");126public static String str4 = new String("4");127public static String str5 = new String("5");128129public static String[] testCloneObjectArray(String[] arr) {130return arr.clone();131}132133public static String[] testCloneObjectArrayCopy(String[] arr) {134String[] arr2 = new String[arr.length];135System.arraycopy(arr, 0, arr2, 0, arr.length);136return arr2;137}138139public static String[] testCloneShortObjectArray() {140String[] arr = new String[5];141arr[0] = str1;142arr[1] = str2;143arr[2] = str3;144arr[3] = str4;145arr[4] = str5;146escape_arr = arr;147return arr.clone();148}149150public static String[] testCloneShortObjectArray2(Method clone) throws Exception {151String[] arr = new String[5];152arr[0] = str1;153arr[1] = str2;154arr[2] = str3;155arr[3] = str4;156arr[4] = str5;157escape_arr = arr;158return (String[]) testCloneObject(clone, arr);159}160161public static String[] testCloneShortObjectArrayCopy() {162String[] arr = new String[5];163arr[0] = str1;164arr[1] = str2;165arr[2] = str3;166arr[3] = str4;167arr[4] = str5;168escape_arr = arr;169String[] arr2 = new String[arr.length];170System.arraycopy(arr, 0, arr2, 0, arr.length);171return arr2;172}173174public static int[] testClonePrimitiveArray(int[] arr) {175return arr.clone();176}177178public static Object testCloneOop(Payload p) {179return p.clonep();180}181182public static Object testCloneOop2(Payload2 p) {183return p.clonep();184}185186public static Object testCloneObject(Method clone, Object obj) throws Exception {187return clone.invoke(obj);188}189190public static void main(String[] args) throws Exception {191Method clone = Object.class.getDeclaredMethod("clone");192clone.setAccessible(true);193194String[] arr1 = new String[42];195for (int j = 0; j < arr1.length; j++) {196arr1[j] = new String(Integer.toString(j));197}198199for (int i = 0; i < 50_000; i++) {200String[] arr2 = testCloneObjectArray(arr1);201verifyStr(arr1, arr2);202String[] arr3 = testCloneObjectArray(arr1);203verifyStr(arr1, arr3);204String[] arr4 = testCloneObjectArray(arr1);205verifyStr(arr1, arr4);206verifyStr(arr1, arr3);207verifyStr(arr1, arr2);208}209210for (int i = 0; i < 50_000; i++) {211for (int j = 0; j < arr1.length; j++) {212arr1[j] = new String(Integer.toString(j));213}214String[] arr2 = (String[]) testCloneObject(clone, arr1);215verifyStr(arr1, arr2);216String[] arr3 = (String[]) testCloneObject(clone, arr1);217verifyStr(arr1, arr3);218String[] arr4 = (String[]) testCloneObject(clone, arr1);219verifyStr(arr1, arr4);220verifyStr(arr1, arr3);221verifyStr(arr1, arr2);222}223224for (int i = 0; i < 50_000; i++) {225String[] value = testCloneShortObjectArray();226verifyStr(value, escape_arr);227String[] value2 = testCloneShortObjectArray();228verifyStr(value2, escape_arr);229String[] value3 = testCloneShortObjectArray();230verifyStr(value3, escape_arr);231String[] value4 = testCloneShortObjectArray2(clone);232verifyStr(value4, escape_arr);233verifyStr(value, value4);234verifyStr(value, value3);235verifyStr(value, value2);236}237238for (int i = 0; i < 50_000; i++) {239String[] arr2 = testCloneObjectArrayCopy(arr1);240verifyStr(arr1, arr2);241String[] arr3 = testCloneObjectArrayCopy(arr1);242verifyStr(arr1, arr3);243String[] arr4 = testCloneObjectArrayCopy(arr1);244verifyStr(arr1, arr4);245verifyStr(arr1, arr3);246verifyStr(arr1, arr2);247}248249for (int i = 0; i < 50_000; i++) {250String[] value = testCloneShortObjectArrayCopy();251verifyStr(value, escape_arr);252String[] value2 = testCloneShortObjectArrayCopy();253verifyStr(value2, escape_arr);254String[] value3 = testCloneShortObjectArrayCopy();255verifyStr(value3, escape_arr);256verifyStr(value, value3);257verifyStr(value, value2);258}259260int[] arr2 = new int[42];261for (int i = 0; i < arr2.length; i++) {262arr2[i] = i;263}264for (int i = 0; i < 50_000; i++) {265int[] res1 = testClonePrimitiveArray(arr2);266int[] res2 = (int[])testCloneObject(clone, arr2);267for (int j = 0; j < arr2.length; j++) {268if (res1[j] != j) {269throw new RuntimeException("Unexpected result: " + res1[j] + " != " + j);270}271if (res2[j] != j) {272throw new RuntimeException("Unexpected result: " + res2[j] + " != " + j);273}274}275}276277Payload ref = new Payload(false, -1, 'c', str1, (short) 5, -1);278for (int i = 0; i < 50_000; i++) {279Payload p1 = (Payload) testCloneOop(ref);280verifyPayload(ref, p1);281Payload p2 = (Payload) testCloneOop(ref);282verifyPayload(ref, p2);283Payload p3 = (Payload) testCloneOop(ref);284verifyPayload(ref, p3);285verifyPayload(p2, p3);286verifyPayload(p1, p3);287}288289for (int i = 0; i < 50_000; i++) {290Payload p1 = (Payload) testCloneObject(clone, ref);291verifyPayload(ref, p1);292Payload p2 = (Payload) testCloneObject(clone, ref);293verifyPayload(ref, p2);294Payload p3 = (Payload) testCloneObject(clone, ref);295verifyPayload(ref, p3);296verifyPayload(p2, p3);297verifyPayload(p1, p3);298}299300Payload2 ref2 = new Payload2(false, -1, 'c', str1, (short) 5, -1, false, 0, 'k', str2, (short)-1, 0);301for (int i = 0; i < 50_000; i++) {302Payload2 p1 = (Payload2) testCloneOop2(ref2);303verifyPayload2(ref2, p1);304Payload2 p2 = (Payload2) testCloneOop2(ref2);305verifyPayload2(ref2, p2);306Payload2 p3 = (Payload2) testCloneOop2(ref2);307verifyPayload2(ref2, p3);308verifyPayload2(p2, p3);309verifyPayload2(p1, p3);310}311312for (int i = 0; i < 50_000; i++) {313Payload2 p1 = (Payload2) testCloneObject(clone, ref2);314verifyPayload2(ref2, p1);315Payload2 p2 = (Payload2) testCloneObject(clone, ref2);316verifyPayload2(ref2, p2);317Payload2 p3 = (Payload2) testCloneObject(clone, ref2);318verifyPayload2(ref2, p3);319verifyPayload2(p2, p3);320verifyPayload2(p1, p3);321}322}323324public static void verifyPayload(Payload p1, Payload p2) {325if (p1.b != p2.b) {326throw new RuntimeException("b is wrong");327}328if (p1.c != p2.c) {329throw new RuntimeException("c is wrong");330}331if (p1.i != p2.i) {332throw new RuntimeException("i is wrong");333}334if (p1.s != p2.s) {335throw new RuntimeException("s is wrong");336}337if (p1.i2 != p2.i2) {338throw new RuntimeException("i2 is wrong");339}340if (p1.str != p2.str) {341throw new RuntimeException("str is wrong");342}343if (!p1.str.equals(p2.str)) {344throw new RuntimeException("str content is wrong");345}346}347348public static void verifyPayload2(Payload2 p1, Payload2 p2) {349if (p1.b != p2.b) {350throw new RuntimeException("b is wrong");351}352if (p1.c != p2.c) {353throw new RuntimeException("c is wrong");354}355if (p1.i != p2.i) {356throw new RuntimeException("i is wrong");357}358if (p1.s != p2.s) {359throw new RuntimeException("s is wrong");360}361if (p1.i2 != p2.i2) {362throw new RuntimeException("i2 is wrong");363}364if (p1.str != p2.str) {365throw new RuntimeException("str is wrong");366}367if (!p1.str.equals(p2.str)) {368throw new RuntimeException("str content is wrong");369}370if (p1.b2 != p2.b2) {371throw new RuntimeException("b is wrong");372}373if (p1.c2 != p2.c2) {374throw new RuntimeException("c is wrong");375}376if (p1.i3 != p2.i3) {377throw new RuntimeException("i is wrong");378}379if (p1.s2 != p2.s2) {380throw new RuntimeException("s is wrong");381}382if (p1.i4 != p2.i4) {383throw new RuntimeException("i2 is wrong");384}385if (p1.str2 != p2.str2) {386throw new RuntimeException("str is wrong");387}388if (!p1.str2.equals(p2.str2)) {389throw new RuntimeException("str content is wrong");390}391}392393public static void verifyStr(String[] arr1, String[] arr2) {394if (arr1 == arr2) {395throw new RuntimeException("Must not be the same");396}397if (arr1.length != arr2.length) {398throw new RuntimeException("Must have the same length");399}400for (int i = 0; i < arr1.length; i++) {401if (arr1[i] != arr2[i]) {402throw new RuntimeException("Fail cloned element not the same: " + i);403}404if (!arr1[i].equals(arr2[i])) {405throw new RuntimeException("Fail cloned element content not the same");406}407}408}409}410411412413