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