Path: blob/master/test/hotspot/jtreg/compiler/arraycopy/TestACSameSrcDst.java
41149 views
/*1* Copyright (c) 2017, Red Hat, Inc. 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 817967826* @summary ArrayCopy with same src and dst can cause incorrect execution or compiler crash27*28* @run main/othervm -XX:CompileCommand=compileonly,TestACSameSrcDst::test* TestACSameSrcDst29*30*/3132public class TestACSameSrcDst {3334static int test1(int[] src, int[] dst) {35System.arraycopy(src, 5, dst, 0, 10);36// this shouldn't be transformed to src[5] because the copy37// can modify src[5] if src and dst are the same.38return dst[0];39}4041static int test2(int[] src) {42System.arraycopy(src, 0, src, 0, 10);43// same source and destination. If load from destination is44// transformed to load of source, the compiler performs that45// optimization in an infinite loop.46return src[0];47}4849static int test3() {50int[] src = new int[15];51src[5] = 0x42;52System.arraycopy(src, 5, src, 0, 10);53// That load can't bypass the arraycopy54return src[0];55}5657static int test4() {58int[] src = new int[15];59System.arraycopy(src, 0, src, 5, 10);60return src[0];61}6263// The dst[0] load can't bypass the arraycopy. After ArrayCopyNode64// is expanded, C2 looks for a stub call on the control paths of65// the array copy subgraph to decide whether the load's memory66// input can bypass the arraycopy. This test verifies the case of67// a source array that's not declared as an array.68static int test5(Object src, int l, boolean flag) {69int[] dst = new int[10];70if (flag) {71dst[0] = 0x42;72System.arraycopy(src, 0, dst, 0, l);73return dst[0];74}75return 0;76}7778public static void main(String[] args) {79int[] array = new int[15];80for (int i = 0; i < 20000; i++) {81int res;82for (int j = 0; j < array.length; j++) {83array[j] = j;84}85int expected = array[5];86res = test1(array, array);87if (res != expected) {88throw new RuntimeException("bad result: " + res + " != " + expected);89}90test2(array);91res = test3();92if (res != 0x42) {93throw new RuntimeException("bad result: " + res + " != " + 0x42);94}95test4();96for (int j = 0; j < array.length; j++) {97array[j] = j;98}99res = test5(array, 10, (i%2) == 0);100if (res != 0) {101throw new RuntimeException("bad result: " + res + " != " + 0);102}103}104}105}106107108