1 /*
   2  * Copyright (c) 2014, 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 8064703
  27  * @summary Deoptimization between array allocation and arraycopy may result in non initialized array
  28  * @run main/othervm -XX:-BackgroundCompilation -XX:-UseOnStackReplacement -XX:TypeProfileLevel=020 TestArrayCopyNoInit
  29  *
  30  */
  31 
  32 public class TestArrayCopyNoInit {
  33 
  34     static int[] m1(int[] src) {
  35         int[] dest = new int[10];
  36         try {
  37             System.arraycopy(src, 0, dest, 0, 10);
  38         } catch (NullPointerException npe) {
  39         }
  40         return dest;
  41     }
  42 
  43     static int[] m2(Object src, boolean flag) {
  44         Class tmp = src.getClass();
  45         if (flag) {
  46             return null;
  47         }
  48         int[] dest = new int[10];
  49         try {
  50             System.arraycopy(src, 0, dest, 0, 10);
  51         } catch (ArrayStoreException npe) {
  52         }
  53         return dest;
  54     }
  55 
  56     static int[] m3(int[] src, int src_offset) {
  57         int tmp = src[0];
  58         int[] dest = new int[10];
  59         try {
  60             System.arraycopy(src, src_offset, dest, 0, 10);
  61         } catch (IndexOutOfBoundsException npe) {
  62         }
  63         return dest;
  64     }
  65 
  66     static int[] m4(int[] src, int length) {
  67         int tmp = src[0];
  68         int[] dest = new int[10];
  69         try {
  70             System.arraycopy(src, 0, dest, 0, length);
  71         } catch (IndexOutOfBoundsException npe) {
  72         }
  73         return dest;
  74     }
  75 
  76     static TestArrayCopyNoInit[] m5(Object[] src) {
  77         Object tmp = src[0];
  78         TestArrayCopyNoInit[] dest = new TestArrayCopyNoInit[10];
  79         System.arraycopy(src, 0, dest, 0, 0);
  80         return dest;
  81     }
  82 
  83     static class A {
  84     }
  85 
  86     static class B extends A {
  87     }
  88 
  89     static class C extends B {
  90     }
  91 
  92     static class D extends C {
  93     }
  94 
  95     static class E extends D {
  96     }
  97 
  98     static class F extends E {
  99     }
 100 
 101     static class G extends F {
 102     }
 103 
 104     static class H extends G {
 105     }
 106 
 107     static class I extends H {
 108     }
 109 
 110     static H[] m6(Object[] src) {
 111         Object tmp = src[0];
 112         H[] dest = new H[10];
 113         System.arraycopy(src, 0, dest, 0, 0);
 114         return dest;
 115     }
 116 
 117     static Object m7_src(Object src) {
 118         return src;
 119     }
 120 
 121     static int[] m7(Object src, boolean flag) {
 122         Class tmp = src.getClass();
 123         if (flag) {
 124             return null;
 125         }
 126         src = m7_src(src);
 127         int[] dest = new int[10];
 128         try {
 129             System.arraycopy(src, 0, dest, 0, 10);
 130         } catch (ArrayStoreException npe) {
 131         }
 132         return dest;
 133     }
 134 
 135     static public void main(String[] args) {
 136         boolean success = true;
 137         int[] src = new int[10];
 138         TestArrayCopyNoInit[] src2 = new TestArrayCopyNoInit[10];
 139         int[] res = null;
 140         TestArrayCopyNoInit[] res2 = null;
 141         Object src_obj = new Object();
 142 
 143         for (int i = 0; i < 20000; i++) {
 144             m1(src);
 145         }
 146 
 147         res = m1(null);
 148         for (int i = 0; i < res.length; i++) {
 149             if (res[i] != 0) {
 150                 success = false;
 151                 System.out.println("Uninitialized array following NPE");
 152                 break;
 153             }
 154         }
 155 
 156         for (int i = 0; i < 20000; i++) {
 157             if ((i%2) == 0) {
 158                 m2(src, false);
 159             } else {
 160                 m2(src_obj, true);
 161             }
 162         }
 163         res = m2(src_obj, false);
 164         for (int i = 0; i < res.length; i++) {
 165             if (res[i] != 0) {
 166                 success = false;
 167                 System.out.println("Uninitialized array following failed array check");
 168                 break;
 169             }
 170         }
 171 
 172         for (int i = 0; i < 20000; i++) {
 173             m3(src, 0);
 174         }
 175         res = m3(src, -1);
 176         for (int i = 0; i < res.length; i++) {
 177             if (res[i] != 0) {
 178                 success = false;
 179                 System.out.println("Uninitialized array following failed src offset check");
 180                 break;
 181             }
 182         }
 183 
 184         for (int i = 0; i < 20000; i++) {
 185             m4(src, 0);
 186         }
 187         res = m4(src, -1);
 188         for (int i = 0; i < res.length; i++) {
 189             if (res[i] != 0) {
 190                 success = false;
 191                 System.out.println("Uninitialized array following failed length check");
 192                 break;
 193             }
 194         }
 195 
 196         for (int i = 0; i < 20000; i++) {
 197             m5(src2);
 198         }
 199         res2 = m5(new Object[10]);
 200         for (int i = 0; i < res2.length; i++) {
 201             if (res2[i] != null) {
 202                 success = false;
 203                 System.out.println("Uninitialized array following failed type check");
 204                 break;
 205             }
 206         }
 207 
 208         H[] src3 = new H[10];
 209         I b = new I();
 210         for (int i = 0; i < 20000; i++) {
 211             m6(src3);
 212         }
 213         H[] res3 = m6(new Object[10]);
 214         for (int i = 0; i < res3.length; i++) {
 215             if (res3[i] != null) {
 216                 success = false;
 217                 System.out.println("Uninitialized array following failed full type check");
 218                 break;
 219             }
 220         }
 221 
 222         for (int i = 0; i < 20000; i++) {
 223             if ((i%2) == 0) {
 224                 m7(src, false);
 225             } else {
 226                 m7(src_obj, true);
 227             }
 228         }
 229         res = m7(src_obj, false);
 230         for (int i = 0; i < res.length; i++) {
 231             if (res[i] != 0) {
 232                 success = false;
 233                 System.out.println("Uninitialized array following failed type check with return value profiling");
 234                 break;
 235             }
 236         }
 237 
 238         if (!success) {
 239             throw new RuntimeException("Some tests failed");
 240         }
 241     }
 242 }