Path: blob/master/test/hotspot/jtreg/compiler/escapeAnalysis/TestArrayCopy.java
41149 views
/*1* Copyright (c) 2016 SAP SE 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 815961126* @summary The elimination of System.arraycopy by EscapeAnalysis prevents27* an IndexOutOfBoundsException from being thrown if the arraycopy28* is called with a negative length argument.29* @modules java.base/jdk.internal.misc30* @library /testlibrary /test/lib31* @build sun.hotspot.WhiteBox32* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox33*34* @run main/othervm/timeout=30035* -Xbootclasspath/a:.36* -XX:+UnlockDiagnosticVMOptions37* -XX:+WhiteBoxAPI38* -XX:-UseOnStackReplacement39* compiler.escapeAnalysis.TestArrayCopy40*41* @author Volker Simonis42*/4344package compiler.escapeAnalysis;4546import sun.hotspot.WhiteBox;47import java.lang.reflect.Method;4849public class TestArrayCopy {5051private static final WhiteBox WB = WhiteBox.getWhiteBox();52// DST_LEN Must be const, otherwise EliminateAllocations won't work53static final int DST_LEN = 4;54static final int SRC_LEN = 8;5556public static boolean do_test1(Object src, int src_pos, int dst_pos, int cpy_len) {57try {58System.arraycopy(src, src_pos, new Object[DST_LEN], dst_pos, cpy_len);59return false;60} catch (IndexOutOfBoundsException e) {61return true;62}63}6465public static int do_test2(Object src, int src_pos, int dst_pos, int cpy_len) {66try {67System.arraycopy(src, src_pos, new Object[DST_LEN], dst_pos, cpy_len);68return 0;69} catch (IndexOutOfBoundsException e) {70return 1;71} catch (ArrayStoreException e) {72return 2;73}74}7576static final int COUNT = 100_000;77static final int[] src_pos = { 0, -1, -1, 0, 0, 0, 1, 1, 1, 1, 1 };78static final int[] dst_pos = { 0, -1, 0, -1, 0, 1, 0, 1, 1, 1, 1 };79static final int[] cpy_len = { 0, 0, 0, 0, -1, -1, -1, -1, 8, 4, 2 };8081public static void main(String args[]) throws Exception {82int length = args.length > 0 ? Integer.parseInt(args[0]) : -1;83int[] int_arr = new int[SRC_LEN];84Object[] obj_arr = new Object[SRC_LEN];8586Method test1 = TestArrayCopy.class.getMethod("do_test1", Object.class, int.class, int.class, int.class);87Method test2 = TestArrayCopy.class.getMethod("do_test2", Object.class, int.class, int.class, int.class);8889for (int i = 0; i < src_pos.length; i++) {90int sp = src_pos[i];91int dp = dst_pos[i];92int cl = cpy_len[i];93String version1 = String.format("System.arraycopy(Object[8], %d, new Object[%d], %d, %d)", sp, DST_LEN, dp, cl);94String version2 = String.format("System.arraycopy(int[8], %d, new Object[%d], %d, %d)", sp, DST_LEN, dp, cl);95System.out.format("Testing " + version1 + "\nand " + version2).flush();96for (int x = 0; x < COUNT; x++) {97if (!do_test1(obj_arr, sp, dp, cl) &&98(sp < 0 || dp < 0 || cl < 0 || (sp + cl >= SRC_LEN) || (dp + cl >= DST_LEN))) {99throw new RuntimeException("Expected IndexOutOfBoundsException for " + version1);100}101int res = do_test2(int_arr, sp, dp, cl);102if (res == 0 || res == 1) {103throw new RuntimeException("Expected ArrayStoreException for " + version2);104}105}106WB.deoptimizeMethod(test1);107WB.clearMethodState(test1);108WB.deoptimizeMethod(test2);109WB.clearMethodState(test2);110}111112}113}114115116