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