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