Path: blob/master/test/hotspot/jtreg/compiler/rangechecks/RangeCheckEliminationScaleNotOne.java
41149 views
/*1* Copyright (c) 2018, Red Hat, Inc. 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 821526526* @summary C2: range check elimination may allow illegal out of bound access27*28* @run main/othervm -XX:-TieredCompilation -XX:-BackgroundCompilation -XX:-UseOnStackReplacement -XX:-UseLoopPredicate RangeCheckEliminationScaleNotOne29*30*/3132import java.util.Arrays;3334public class RangeCheckEliminationScaleNotOne {35public static void main(String[] args) {36{37int[] array = new int[199];38boolean[] flags = new boolean[100];39Arrays.fill(flags, true);40flags[0] = false;41flags[1] = false;42for (int i = 0; i < 20_000; i++) {43test1(100, array, 0, flags);44}45boolean ex = false;46try {47test1(100, array, -5, flags);48} catch (ArrayIndexOutOfBoundsException aie) {49ex = true;50}51if (!ex) {52throw new RuntimeException("no AIOOB exception");53}54}5556{57int[] array = new int[199];58boolean[] flags = new boolean[100];59Arrays.fill(flags, true);60flags[0] = false;61flags[1] = false;62for (int i = 0; i < 20_000; i++) {63test2(100, array, 198, flags);64}65boolean ex = false;66try {67test2(100, array, 203, flags);68} catch (ArrayIndexOutOfBoundsException aie) {69ex = true;70}71if (!ex) {72throw new RuntimeException("no AIOOB exception");73}74}75}7677private static int test1(int stop, int[] array, int offset, boolean[] flags) {78if (array == null) {}79int res = 0;80for (int i = 0; i < stop; i++) {81if (flags[i]) {82res += array[2 * i + offset];83}84}85return res;86}878889private static int test2(int stop, int[] array, int offset, boolean[] flags) {90if (array == null) {}91int res = 0;92for (int i = 0; i < stop; i++) {93if (flags[i]) {94res += array[-2 * i + offset];95}96}97return res;98}99}100101102