/* * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2018 SAP SE. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /** * @test * @summary Test extended ArrayIndexOutOfBoundsException message for * class files generated without debug information. The message lists * information about the array and the indexes involved. * @compile ArrayIndexOutOfBoundsExceptionTest.java * @run testng ArrayIndexOutOfBoundsExceptionTest * @run testng/othervm -Xcomp -XX:-TieredCompilation ArrayIndexOutOfBoundsExceptionTest * @run testng/othervm -Xcomp -XX:TieredStopAtLevel=1 ArrayIndexOutOfBoundsExceptionTest * @author Ann-Kathrin Wasle */ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import org.testng.annotations.Test; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNull; import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertTrue; import static org.testng.Assert.fail; /** * Tests the detailed messages for the ArrayIndexOutOfBoundsException. */ public class ArrayIndexOutOfBoundsExceptionTest { // Some fields used in the test. static int[] staticArray = new int[0]; static long[][] staticLongArray = new long[0][0]; DoubleArrayGen dag; ArrayList names = new ArrayList<>(); ArrayList curr; static boolean hasDebugInfo = true; static boolean noBetterUnknownLocals = true; static { try { hasDebugInfo = System.getProperty("hasDebugInfo") != null; noBetterUnknownLocals = System.getProperty("noBetterUnknownLocals") != null; } catch (Throwable t) { throw new RuntimeException(t); } } public static void main(String[] args) { ArrayIndexOutOfBoundsExceptionTest t = new ArrayIndexOutOfBoundsExceptionTest(); try { t.testCreationViaNew(); t.testCreationViaReflection(); t.testCreationViaSerialization(); t.testLoadedFromLocalVariable1(); t.testLoadedFromLocalVariable2(); t.testLoadedFromLocalVariable3(); t.testLoadedFromLocalVariable4(); t.testLoadedFromLocalVariable5(); t.testLoadedFromLocalVariable6(); t.testLoadedFromLocalVariable7(); t.testLoadedFromLocalVariable8(); t.testLoadedFromLocalVariable9(); t.testLoadedFromLocalVariable10(); t.testLoadedFromLocalVariable11(); t.testLoadedFromMethod1(); t.testLoadedFromMethod2(); t.testLoadedFromMethod3(); t.testLoadedFromMethod4(); t.testLoadedFromStaticField1(); t.testLoadedFromStaticField2(); t.testLoadedFromStaticField3(); t.testLoadedFromStaticField4(); t.testWorkWithCompiler(); t.testMissingLocalVariableTable(); t.testAIOOBMessages(); } catch (Exception e) {} } /** * */ @Test public void testCreationViaNew() { assertNull(new ArrayIndexOutOfBoundsException().getMessage()); } /** * @throws Exception */ @Test public void testCreationViaReflection() throws Exception { Exception ex = ArrayIndexOutOfBoundsException.class.newInstance(); assertNull(ex.getMessage()); } /** * @throws Exception */ @Test public void testCreationViaSerialization() throws Exception { Object o = new ArrayIndexOutOfBoundsException(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(o); ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bis); Exception ex = (Exception) ois.readObject(); assertNull(ex.getMessage()); } /** * */ @Test public void testLoadedFromLocalVariable1() { Object[] a = new Object[5]; try { a[10].hashCode(); fail(); } catch (ArrayIndexOutOfBoundsException e) { e.printStackTrace(); assertNotNull(e.getMessage()); } } /** * */ @Test public void testLoadedFromLocalVariable2() { Object[] a = new Object[7]; try { a[-7] = null; fail(); } catch (ArrayIndexOutOfBoundsException e) { e.printStackTrace(); assertNotNull(e.getMessage()); } } /** * */ @Test public void testLoadedFromLocalVariable3() { byte[] a = new byte[0]; try { assertTrue(a[99] == 5); fail(); } catch (ArrayIndexOutOfBoundsException e) { e.printStackTrace(); assertNotNull(e.getMessage()); } } /** * */ @Test public void testLoadedFromLocalVariable4() { char[] a = new char[0]; try { a[0] = 0; fail(); } catch (ArrayIndexOutOfBoundsException e) { e.printStackTrace(); assertNotNull(e.getMessage()); } } /** * */ @Test public void testLoadedFromLocalVariable5() { double[] a = new double[0]; try { assertTrue(a[0] == 0); fail(); } catch (ArrayIndexOutOfBoundsException e) { e.printStackTrace(); assertNotNull(e.getMessage()); } } /** * */ @Test public void testLoadedFromLocalVariable6() { float[] a = new float[0]; try { a[0] = 0; fail(); } catch (ArrayIndexOutOfBoundsException e) { e.printStackTrace(); assertNotNull(e.getMessage()); } } /** * */ @Test public void testLoadedFromLocalVariable7() { int[] a = new int[0]; try { assertTrue(a[0] == 0); fail(); } catch (ArrayIndexOutOfBoundsException e) { e.printStackTrace(); assertNotNull(e.getMessage()); } } /** * */ @Test public void testLoadedFromLocalVariable8() { long[] a = new long[0]; try { a[0] = 0; fail(); } catch (ArrayIndexOutOfBoundsException e) { e.printStackTrace(); assertNotNull(e.getMessage()); } } /** * */ @Test public void testLoadedFromLocalVariable9() { short[] a = new short[5]; try { assertTrue(a[10] == 0); fail(); } catch (ArrayIndexOutOfBoundsException e) { e.printStackTrace(); assertNotNull(e.getMessage()); } } /** * */ @Test public void testLoadedFromLocalVariable10() { int[][][] a = new int[1][0][0]; try { assertTrue(a[0][1][2] == 0); fail(); } catch (ArrayIndexOutOfBoundsException e) { e.printStackTrace(); assertNotNull(e.getMessage()); } } /** * */ @Test public void testLoadedFromLocalVariable11() { int[][][] a = new int[1][0][0]; try { a[0][1][2] = 0; fail(); } catch (ArrayIndexOutOfBoundsException e) { e.printStackTrace(); assertNotNull(e.getMessage()); } } /** * */ @Test public void testLoadedFromMethod1() { try { assertTrue((ArrayGenerator.arrayReturner(false))[0] == null); fail(); } catch (ArrayIndexOutOfBoundsException e) { e.printStackTrace(); assertNotNull(e.getMessage()); } } /** * */ @Test public void testLoadedFromMethod2() { try { assertTrue( ((new ArrayGenerator().returnMyArray(1, 1, (short) 1)))[0] == null); fail(); } catch (ArrayIndexOutOfBoundsException e) { e.printStackTrace(); assertNotNull(e.getMessage()); } } /** * */ @Test public void testLoadedFromMethod3() { try { assertTrue((returnArray(null, null, 1f))[0] == null); fail(); } catch (ArrayIndexOutOfBoundsException e) { e.printStackTrace(); assertNotNull(e.getMessage()); } } /** * */ @Test public void testLoadedFromMethod4() { ImplTestLoadedFromMethod4(new DoubleArrayGenImpl()); } /** * @param gen */ public void ImplTestLoadedFromMethod4(DoubleArrayGen gen) { try { (gen.getArray())[0] = 1.0; fail(); } catch (ArrayIndexOutOfBoundsException e) { e.printStackTrace(); assertNotNull(e.getMessage()); } } /** * */ @Test public void testLoadedFromStaticField1() { try { assertTrue(staticArray[0] == 1); fail(); } catch (ArrayIndexOutOfBoundsException e) { e.printStackTrace(); assertNotNull(e.getMessage()); } } /** * */ @Test public void testLoadedFromStaticField2() { try { staticArray[0] = 2; fail(); } catch (ArrayIndexOutOfBoundsException e) { e.printStackTrace(); assertNotNull(e.getMessage()); } } /** * */ @Test public void testLoadedFromStaticField3() { try { assertTrue(staticLongArray[0][0] == 1L); } catch (ArrayIndexOutOfBoundsException e) { e.printStackTrace(); assertNotNull(e.getMessage()); } } /** * */ @Test public void testLoadedFromStaticField4() { try { staticLongArray[0][0] = 2L; fail(); } catch (ArrayIndexOutOfBoundsException e) { e.printStackTrace(); assertNotNull(e.getMessage()); } } /** * */ @Test public void testWorkWithCompiler() { int[] a = {0, 1, 2}; int i1 = 2; int i2 = 3; for (int i = 0; i < 10000000; i++) { // One 0 less is enough for d64. testImplWorkWithCompiler(a, i1); } testImplWorkWithCompiler(a, i2); } /** * @param a * @param i */ public void testImplWorkWithCompiler(int[] a, int i) { try { assertTrue(a[i] == 2); } catch (ArrayIndexOutOfBoundsException e) { e.printStackTrace(); assertNotNull(e.getMessage()); } } private Object[] returnArray(String[][] dummy1, int[][][] dummy2, float dummy3) { return new Object[0]; } /** * */ public static interface DoubleArrayGen { /** * @return double Array */ public double[] getArray(); } /** * */ public static class DoubleArrayGenImpl implements DoubleArrayGen { @Override public double[] getArray() { return new double[0]; } } /** * */ public static class ArrayGenerator { /** * @param dummy1 * @return Object Array */ public static Object[] arrayReturner(boolean dummy1) { return new Object[0]; } /** * @param dummy1 * @param dummy2 * @param dummy3 * @return Object Array */ public Object[] returnMyArray(double dummy1, long dummy2, short dummy3) { return new Object[0]; } } /** * */ @Test public void testMissingLocalVariableTable() { doTestMissingLocalVariableTable(names); System.out.println("Names"); for (int i = 0; i < names.size(); ++i) { System.out.println(names.get(i)); } String[] expectedHasDebugInfoGoodNames = new String[] { "trying to access index 0 of an array with length 0", "while trying to load from index -1 of an object array with length 10, " + "loaded from local variable 'o1'", "while trying to store to index 1 of a byte (or boolean) array with length 0, " + "loaded from local variable 'z1'", "while trying to load from index 7 of an object array with length 5, " + "loaded from local variable 'dd1'" }; String[] expectedNoDebugInfoGoodNames = new String[] { "trying to access index 0 of an array with length 0", "trying to access index -1 of an array with length 10", "trying to access index 1 of an array with length 0", "trying to access index 7 of an array with length 5" }; String[] expectedNoDebugInfoBadNames = new String[] { "trying to access index 0 of an array with length 0", "trying to access index -1 of an array with length 10", "trying to access index 1 of an array with length 0", "trying to access index 7 of an array with length 5" }; String[] expectedNames; if (hasDebugInfo) { expectedNames = expectedHasDebugInfoGoodNames; } else { if (noBetterUnknownLocals) { expectedNames = expectedNoDebugInfoBadNames; } else { expectedNames = expectedNoDebugInfoGoodNames; } } assertEquals(expectedNames.length, names.size()); for (int i = 0; i < expectedNames.length; ++i) { assertEquals(names.get(i), expectedNames[i]); } } private void doTestMissingLocalVariableTable(ArrayList names) { curr = names; doTestMissingLocalVariableTable1(); doTestMissingLocalVariableTable2(new Object[10], new boolean[0], new double[5][1]); } private void doTestMissingLocalVariableTable1() { try { staticArray[0] = 0; fail(); } catch (ArrayIndexOutOfBoundsException e) { curr.add(e.getMessage()); } } private void doTestMissingLocalVariableTable2(Object[] o1, boolean z1[], double[][] dd1) { try { o1[-1].hashCode(); fail(); } catch (ArrayIndexOutOfBoundsException e) { curr.add(e.getMessage()); } try { z1[1] = true; fail(); } catch (ArrayIndexOutOfBoundsException e) { curr.add(e.getMessage()); } try { assertTrue(dd1[7][1] == 0); fail(); } catch (ArrayIndexOutOfBoundsException e) { curr.add(e.getMessage()); } } /** * */ @Test public void testAIOOBMessages() { boolean[] za1 = new boolean[0]; byte[] ba1 = new byte[0]; short[] sa1 = new short[0]; char[] ca1 = new char[0]; int[] ia1 = new int[0]; long[] la1 = new long[0]; float[] fa1 = new float[0]; double[] da1 = new double[0]; Object[] oa1 = new Object[10]; Object[] oa2 = new Object[5]; try { System.out.println(za1[-5]); fail(); } catch (ArrayIndexOutOfBoundsException e) { assertEquals(e.getMessage(), "trying to access index -5 of an array with length 0"); } try { System.out.println(ba1[0]); fail(); } catch (ArrayIndexOutOfBoundsException e) { assertEquals(e.getMessage(), "trying to access index 0 of an array with length 0"); } try { System.out.println(sa1[0]); fail(); } catch (ArrayIndexOutOfBoundsException e) { assertEquals(e.getMessage(), "trying to access index 0 of an array with length 0"); } try { System.out.println(ca1[0]); fail(); } catch (ArrayIndexOutOfBoundsException e) { assertEquals(e.getMessage(), "trying to access index 0 of an array with length 0"); } try { System.out.println(ia1[0]); fail(); } catch (ArrayIndexOutOfBoundsException e) { assertEquals(e.getMessage(), "trying to access index 0 of an array with length 0"); } try { System.out.println(la1[0]); fail(); } catch (ArrayIndexOutOfBoundsException e) { assertEquals(e.getMessage(), "trying to access index 0 of an array with length 0"); } try { System.out.println(fa1[0]); fail(); } catch (ArrayIndexOutOfBoundsException e) { assertEquals(e.getMessage(), "trying to access index 0 of an array with length 0"); } try { System.out.println(da1[0]); fail(); } catch (ArrayIndexOutOfBoundsException e) { assertEquals(e.getMessage(), "trying to access index 0 of an array with length 0"); } try { System.out.println(oa1[12]); fail(); } catch (ArrayIndexOutOfBoundsException e) { assertEquals(e.getMessage(), "trying to access index 12 of an array with length 10"); } try { System.out.println(za1[0] = false); fail(); } catch (ArrayIndexOutOfBoundsException e) { assertEquals(e.getMessage(), "trying to access index 0 of an array with length 0"); } try { System.out.println(ba1[0] = 0); fail(); } catch (ArrayIndexOutOfBoundsException e) { assertEquals(e.getMessage(), "trying to access index 0 of an array with length 0"); } try { System.out.println(sa1[0] = 0); fail(); } catch (ArrayIndexOutOfBoundsException e) { assertEquals(e.getMessage(), "trying to access index 0 of an array with length 0"); } try { System.out.println(ca1[0] = 0); fail(); } catch (ArrayIndexOutOfBoundsException e) { assertEquals(e.getMessage(), "trying to access index 0 of an array with length 0"); } try { System.out.println(ia1[0] = 0); fail(); } catch (ArrayIndexOutOfBoundsException e) { assertEquals(e.getMessage(), "trying to access index 0 of an array with length 0"); } try { System.out.println(la1[0] = 0); fail(); } catch (ArrayIndexOutOfBoundsException e) { assertEquals(e.getMessage(), "trying to access index 0 of an array with length 0"); } try { System.out.println(fa1[0] = 0); fail(); } catch (ArrayIndexOutOfBoundsException e) { assertEquals(e.getMessage(), "trying to access index 0 of an array with length 0"); } try { System.out.println(da1[0] = 0); fail(); } catch (ArrayIndexOutOfBoundsException e) { assertEquals(e.getMessage(), "trying to access index 0 of an array with length 0"); } try { System.out.println(oa1[-2] = null); fail(); } catch (ArrayIndexOutOfBoundsException e) { assertEquals(e.getMessage(), "trying to access index -2 of an array with length 10"); } try { assertTrue((ArrayGenerator.arrayReturner(false))[0] == null); fail(); } catch (ArrayIndexOutOfBoundsException e) { assertEquals(e.getMessage(), "trying to access index 0 of an array with length 0"); } try { staticArray[0] = 2; fail(); } catch (ArrayIndexOutOfBoundsException e) { assertEquals(e.getMessage(), "trying to access index 0 of an array with length 0"); } try { System.arraycopy(oa1, 8, oa2, 0, 5); fail(); } catch (ArrayIndexOutOfBoundsException e) { assertEquals(e.getMessage(), "while trying to copy from index 13 of an object array with length 10"); } } }