1 /*
   2  * Copyright (c) 2016, SAP SE 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 8159611
  27  * @summary The elimination of System.arraycopy by EscapeAnalysis prevents
  28  *          an IndexOutOfBoundsException from being thrown if the arraycopy
  29  *          is called with a negative length argument.
  30  * @modules java.base/jdk.internal.misc
  31  * @library /testlibrary /test/lib
  32  * @build sun.hotspot.WhiteBox
  33  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  34  *                                sun.hotspot.WhiteBox$WhiteBoxPermission
  35  *
  36  * @run main/othervm
  37  *        -Xbootclasspath/a:.
  38  *        -XX:+UnlockDiagnosticVMOptions
  39  *        -XX:+WhiteBoxAPI
  40  *        -XX:-UseOnStackReplacement
  41  *        compiler.escapeAnalysis.TestArrayCopy
  42  *
  43  * @author Volker Simonis
  44  */
  45 
  46 package compiler.escapeAnalysis;
  47 
  48 import sun.hotspot.WhiteBox;
  49 import java.lang.reflect.Method;
  50 
  51 public class TestArrayCopy {
  52 
  53     private static final WhiteBox WB = WhiteBox.getWhiteBox();
  54     // DST_LEN Must be const, otherwise EliminateAllocations won't work
  55     static final int DST_LEN = 4;
  56     static final int SRC_LEN = 8;
  57 
  58     public static boolean do_test1(Object src, int src_pos, int dst_pos, int cpy_len) {
  59         try {
  60             System.arraycopy(src, src_pos, new Object[DST_LEN], dst_pos, cpy_len);
  61             return false;
  62         } catch (IndexOutOfBoundsException e) {
  63             return true;
  64         }
  65     }
  66 
  67     public static int do_test2(Object src, int src_pos, int dst_pos, int cpy_len) {
  68         try {
  69             System.arraycopy(src, src_pos, new Object[DST_LEN], dst_pos, cpy_len);
  70             return 0;
  71         } catch (IndexOutOfBoundsException e) {
  72             return 1;
  73         } catch (ArrayStoreException e) {
  74             return 2;
  75         }
  76     }
  77 
  78     static final int COUNT = 100_000;
  79     static final int[] src_pos = { 0, -1, -1,  0,  0,  0,  1,  1,  1,  1, 1 };
  80     static final int[] dst_pos = { 0, -1,  0, -1,  0,  1,  0,  1,  1,  1, 1 };
  81     static final int[] cpy_len = { 0,  0,  0,  0, -1, -1, -1, -1,  8,  4, 2 };
  82 
  83     public static void main(String args[]) throws Exception {
  84         int length = args.length > 0 ? Integer.parseInt(args[0]) : -1;
  85         int[] int_arr = new int[SRC_LEN];
  86         Object[] obj_arr = new Object[SRC_LEN];
  87 
  88         Method test1 = TestArrayCopy.class.getMethod("do_test1", Object.class, int.class, int.class, int.class);
  89         Method test2 = TestArrayCopy.class.getMethod("do_test2", Object.class, int.class, int.class, int.class);
  90 
  91         for (int i = 0; i < src_pos.length; i++) {
  92             int sp = src_pos[i];
  93             int dp = dst_pos[i];
  94             int cl = cpy_len[i];
  95             String version1 = String.format("System.arraycopy(Object[8], %d, new Object[%d], %d, %d)", sp, DST_LEN, dp, cl);
  96             String version2 = String.format("System.arraycopy(int[8], %d, new Object[%d], %d, %d)", sp, DST_LEN, dp, cl);
  97             System.out.format("Testing " + version1 + "\nand " + version2).flush();
  98             for (int x = 0; x < COUNT; x++) {
  99                 if (!do_test1(obj_arr, sp, dp, cl) &&
 100                     (sp < 0 || dp < 0 || cl < 0 || (sp + cl >= SRC_LEN) || (dp + cl >= DST_LEN))) {
 101                     throw new RuntimeException("Expected IndexOutOfBoundsException for " + version1);
 102                 }
 103                 int res = do_test2(int_arr, sp, dp, cl);
 104                 if (res == 0 || res == 1) {
 105                     throw new RuntimeException("Expected ArrayStoreException for " + version2);
 106                 }
 107             }
 108             WB.deoptimizeMethod(test1);
 109             WB.clearMethodState(test1);
 110             WB.deoptimizeMethod(test2);
 111             WB.clearMethodState(test2);
 112         }
 113 
 114     }
 115 }