Path: blob/master/test/hotspot/jtreg/compiler/arraycopy/TestArrayCopyNoInit.java
41149 views
/*1* Copyright (c) 2014, Oracle 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 806470326* @summary Deoptimization between array allocation and arraycopy may result in non initialized array27*28* @run main/othervm -XX:-BackgroundCompilation -XX:-UseOnStackReplacement -XX:TypeProfileLevel=02029* compiler.arraycopy.TestArrayCopyNoInit30*/3132package compiler.arraycopy;3334public class TestArrayCopyNoInit {3536static int[] m1(int[] src) {37int[] dest = new int[10];38try {39System.arraycopy(src, 0, dest, 0, 10);40} catch (NullPointerException npe) {41}42return dest;43}4445static int[] m2(Object src, boolean flag) {46Class tmp = src.getClass();47if (flag) {48return null;49}50int[] dest = new int[10];51try {52System.arraycopy(src, 0, dest, 0, 10);53} catch (ArrayStoreException npe) {54}55return dest;56}5758static int[] m3(int[] src, int src_offset) {59int tmp = src[0];60int[] dest = new int[10];61try {62System.arraycopy(src, src_offset, dest, 0, 10);63} catch (IndexOutOfBoundsException npe) {64}65return dest;66}6768static int[] m4(int[] src, int length) {69int tmp = src[0];70int[] dest = new int[10];71try {72System.arraycopy(src, 0, dest, 0, length);73} catch (IndexOutOfBoundsException npe) {74}75return dest;76}7778static TestArrayCopyNoInit[] m5(Object[] src) {79Object tmp = src[0];80TestArrayCopyNoInit[] dest = new TestArrayCopyNoInit[10];81System.arraycopy(src, 0, dest, 0, 10);82return dest;83}8485static class A {86}8788static class B extends A {89}9091static class C extends B {92}9394static class D extends C {95}9697static class E extends D {98}99100static class F extends E {101}102103static class G extends F {104}105106static class H extends G {107}108109static class I extends H {110}111112static H[] m6(Object[] src) {113Object tmp = src[0];114H[] dest = new H[10];115System.arraycopy(src, 0, dest, 0, 10);116return dest;117}118119static Object m7_src(Object src) {120return src;121}122123static int[] m7(Object src, boolean flag) {124Class tmp = src.getClass();125if (flag) {126return null;127}128src = m7_src(src);129int[] dest = new int[10];130try {131System.arraycopy(src, 0, dest, 0, 10);132} catch (ArrayStoreException npe) {133}134return dest;135}136137static public void main(String[] args) {138boolean success = true;139int[] src = new int[10];140TestArrayCopyNoInit[] src2 = new TestArrayCopyNoInit[10];141int[] res = null;142TestArrayCopyNoInit[] res2 = null;143Object src_obj = new Object();144145for (int i = 0; i < 20000; i++) {146m1(src);147}148149res = m1(null);150for (int i = 0; i < res.length; i++) {151if (res[i] != 0) {152success = false;153System.out.println("Uninitialized array following NPE");154break;155}156}157158for (int i = 0; i < 20000; i++) {159if ((i%2) == 0) {160m2(src, false);161} else {162m2(src_obj, true);163}164}165res = m2(src_obj, false);166for (int i = 0; i < res.length; i++) {167if (res[i] != 0) {168success = false;169System.out.println("Uninitialized array following failed array check");170break;171}172}173174for (int i = 0; i < 20000; i++) {175m3(src, 0);176}177res = m3(src, -1);178for (int i = 0; i < res.length; i++) {179if (res[i] != 0) {180success = false;181System.out.println("Uninitialized array following failed src offset check");182break;183}184}185186for (int i = 0; i < 20000; i++) {187m4(src, 0);188}189res = m4(src, -1);190for (int i = 0; i < res.length; i++) {191if (res[i] != 0) {192success = false;193System.out.println("Uninitialized array following failed length check");194break;195}196}197198for (int i = 0; i < 20000; i++) {199m5(src2);200}201res2 = m5(new Object[10]);202for (int i = 0; i < res2.length; i++) {203if (res2[i] != null) {204success = false;205System.out.println("Uninitialized array following failed type check");206break;207}208}209210H[] src3 = new H[10];211I b = new I();212for (int i = 0; i < 20000; i++) {213m6(src3);214}215H[] res3 = m6(new Object[10]);216for (int i = 0; i < res3.length; i++) {217if (res3[i] != null) {218success = false;219System.out.println("Uninitialized array following failed full type check");220break;221}222}223224for (int i = 0; i < 20000; i++) {225if ((i%2) == 0) {226m7(src, false);227} else {228m7(src_obj, true);229}230}231res = m7(src_obj, false);232for (int i = 0; i < res.length; i++) {233if (res[i] != 0) {234success = false;235System.out.println("Uninitialized array following failed type check with return value profiling");236break;237}238}239240if (!success) {241throw new RuntimeException("Some tests failed");242}243}244}245246247