8203881: Print erroneous size in NegativeArraySizeException
authorgoetz
Wed, 30 May 2018 11:39:26 +0200
changeset 50304 d5331b94f821
parent 50303 7164c3bb55df
child 50305 02934b0d661b
8203881: Print erroneous size in NegativeArraySizeException Reviewed-by: dholmes, stuefe
src/hotspot/share/oops/arrayKlass.cpp
src/hotspot/share/oops/instanceKlass.cpp
src/hotspot/share/oops/objArrayKlass.cpp
src/hotspot/share/oops/typeArrayKlass.cpp
src/hotspot/share/runtime/reflection.cpp
test/hotspot/jtreg/runtime/exceptionMsgs/NegativeArraySizeException/NegativeArraySizeExceptionTest.java
--- a/src/hotspot/share/oops/arrayKlass.cpp	Wed May 30 16:36:35 2018 +0530
+++ b/src/hotspot/share/oops/arrayKlass.cpp	Wed May 30 11:39:26 2018 +0200
@@ -130,7 +130,7 @@
 
 objArrayOop ArrayKlass::allocate_arrayArray(int n, int length, TRAPS) {
   if (length < 0) {
-    THROW_0(vmSymbols::java_lang_NegativeArraySizeException());
+    THROW_MSG_0(vmSymbols::java_lang_NegativeArraySizeException(), err_msg("%d", length));
   }
   if (length > arrayOopDesc::max_array_length(T_ARRAY)) {
     report_java_out_of_memory("Requested array size exceeds VM limit");
--- a/src/hotspot/share/oops/instanceKlass.cpp	Wed May 30 16:36:35 2018 +0530
+++ b/src/hotspot/share/oops/instanceKlass.cpp	Wed May 30 11:39:26 2018 +0200
@@ -975,7 +975,9 @@
 }
 
 objArrayOop InstanceKlass::allocate_objArray(int n, int length, TRAPS) {
-  if (length < 0) THROW_0(vmSymbols::java_lang_NegativeArraySizeException());
+  if (length < 0)  {
+    THROW_MSG_0(vmSymbols::java_lang_NegativeArraySizeException(), err_msg("%d", length));
+  }
   if (length > arrayOopDesc::max_array_length(T_OBJECT)) {
     report_java_out_of_memory("Requested array size exceeds VM limit");
     JvmtiExport::post_array_size_exhausted();
--- a/src/hotspot/share/oops/objArrayKlass.cpp	Wed May 30 16:36:35 2018 +0530
+++ b/src/hotspot/share/oops/objArrayKlass.cpp	Wed May 30 11:39:26 2018 +0200
@@ -181,7 +181,7 @@
       THROW_OOP_0(Universe::out_of_memory_error_array_size());
     }
   } else {
-    THROW_0(vmSymbols::java_lang_NegativeArraySizeException());
+    THROW_MSG_0(vmSymbols::java_lang_NegativeArraySizeException(), err_msg("%d", length));
   }
 }
 
@@ -209,7 +209,7 @@
       for (int i = 0; i < rank - 1; ++i) {
         sizes += 1;
         if (*sizes < 0) {
-          THROW_0(vmSymbols::java_lang_NegativeArraySizeException());
+          THROW_MSG_0(vmSymbols::java_lang_NegativeArraySizeException(), err_msg("%d", *sizes));
         }
       }
     }
--- a/src/hotspot/share/oops/typeArrayKlass.cpp	Wed May 30 16:36:35 2018 +0530
+++ b/src/hotspot/share/oops/typeArrayKlass.cpp	Wed May 30 11:39:26 2018 +0200
@@ -116,7 +116,7 @@
       THROW_OOP_0(Universe::out_of_memory_error_array_size());
     }
   } else {
-    THROW_0(vmSymbols::java_lang_NegativeArraySizeException());
+    THROW_MSG_0(vmSymbols::java_lang_NegativeArraySizeException(), err_msg("%d", length));
   }
 }
 
--- a/src/hotspot/share/runtime/reflection.cpp	Wed May 30 16:36:35 2018 +0530
+++ b/src/hotspot/share/runtime/reflection.cpp	Wed May 30 11:39:26 2018 +0200
@@ -337,7 +337,7 @@
     THROW_0(vmSymbols::java_lang_NullPointerException());
   }
   if (length < 0) {
-    THROW_0(vmSymbols::java_lang_NegativeArraySizeException());
+    THROW_MSG_0(vmSymbols::java_lang_NegativeArraySizeException(), err_msg("%d", length));
   }
   if (java_lang_Class::is_primitive(element_mirror)) {
     Klass* tak = basic_type_mirror_to_arrayklass(element_mirror, CHECK_NULL);
@@ -369,7 +369,7 @@
   for (int i = 0; i < len; i++) {
     int d = dim_array->int_at(i);
     if (d < 0) {
-      THROW_0(vmSymbols::java_lang_NegativeArraySizeException());
+      THROW_MSG_0(vmSymbols::java_lang_NegativeArraySizeException(), err_msg("%d", d));
     }
     dimensions[i] = d;
   }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/hotspot/jtreg/runtime/exceptionMsgs/NegativeArraySizeException/NegativeArraySizeExceptionTest.java	Wed May 30 11:39:26 2018 +0200
@@ -0,0 +1,306 @@
+/*
+ * 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 that NegativeArraySizeException reports the wrong size.
+ * @library /test/lib
+ * @compile NegativeArraySizeExceptionTest.java
+ * @run main NegativeArraySizeExceptionTest
+ */
+
+import java.lang.reflect.Array;
+
+import jdk.test.lib.Asserts;
+
+public class NegativeArraySizeExceptionTest {
+
+    private static void fail () throws Exception {
+        throw new RuntimeException("Array allocation with negative size expected to fail!");
+    }
+
+    public static void main(String[] args) throws Exception {
+        int minusOne = -1;
+        Object r = null;
+
+        // Tests for exception thrown in arrayKlass.cp, ArrayKlass::allocate_arrayArray().
+
+        try {
+            r = new byte[minusOne][];
+            fail();
+        } catch (NegativeArraySizeException e) {
+            Asserts.assertEQ("-1", e.getMessage());
+        }
+
+        try {
+            r = new Object[Integer.MIN_VALUE][];
+            fail();
+        } catch (NegativeArraySizeException e) {
+            Asserts.assertEQ("-2147483648", e.getMessage());
+        }
+
+        // Tests for exception thrown in typeArrayKlass.cpp, TypeArrayKlass::allocate_common().
+
+        try {
+            r = new byte[minusOne];
+            fail();
+        } catch (NegativeArraySizeException e) {
+            Asserts.assertEQ("-1", e.getMessage());
+        }
+
+        try {
+            r = new byte[Integer.MIN_VALUE];
+            fail();
+        } catch (NegativeArraySizeException e) {
+            Asserts.assertEQ("-2147483648", e.getMessage());
+        }
+
+        // Tests for exception thrown in instanceKlass.cpp, InstanceKlass::allocate_objArray().
+
+        try {
+            r = new Object[minusOne];
+            fail();
+        } catch (NegativeArraySizeException e) {
+            Asserts.assertEQ("-1", e.getMessage());
+        }
+
+        try {
+            r = new Object[Integer.MIN_VALUE];
+            fail();
+        } catch (NegativeArraySizeException e) {
+            Asserts.assertEQ("-2147483648", e.getMessage());
+        }
+
+        // Tests for exception thrown in typeArrayKlass.cpp, TypeArrayKlass::allocate_common().
+        // Innermost primitive array of multidimensional array has wrong size.
+
+        try {
+            r = new byte[3][minusOne];
+            fail();
+        } catch (NegativeArraySizeException e) {
+            Asserts.assertEQ("-1", e.getMessage());
+        }
+
+        try {
+            r = new byte[3][Integer.MIN_VALUE];
+            fail();
+        } catch (NegativeArraySizeException e) {
+            Asserts.assertEQ("-2147483648", e.getMessage());
+        }
+
+        // Tests for exception thrown in objArrayKlass.cpp, ObjArrayKlass::allocate().
+        // Innermost object array of multidimensional array has wrong size.
+
+        try {
+            r = new Object[3][minusOne];
+            fail();
+        } catch (NegativeArraySizeException e) {
+            Asserts.assertEQ("-1", e.getMessage());
+        }
+
+        try {
+            r = new Object[3][Integer.MIN_VALUE];
+            fail();
+        } catch (NegativeArraySizeException e) {
+            Asserts.assertEQ("-2147483648", e.getMessage());
+        }
+
+        // Tests for exception thrown in
+        // Innermost primitive array of multidimensional array has wrong size.
+        // Outer array has size 0.
+
+        try {
+            r = new byte[0][minusOne];
+            fail();
+        } catch (NegativeArraySizeException e) {
+            Asserts.assertEQ("-1", e.getMessage());
+        }
+
+        try {
+            r = new byte[0][Integer.MIN_VALUE];
+            fail();
+        } catch (NegativeArraySizeException e) {
+            Asserts.assertEQ("-2147483648", e.getMessage());
+        }
+
+        // Tests for exception thrown in
+        // Innermost object array of multidimensional array has wrong size.
+        // Outer array has size 0.
+
+        try {
+            r = new Object[0][minusOne];
+            fail();
+        } catch (NegativeArraySizeException e) {
+            Asserts.assertEQ("-1", e.getMessage());
+        }
+
+        try {
+            r = new Object[0][Integer.MIN_VALUE];
+            fail();
+        } catch (NegativeArraySizeException e) {
+            Asserts.assertEQ("-2147483648", e.getMessage());
+        }
+
+        // Tests for exception thrown in objArrayKlass.cpp, ObjArrayKlass::allocate().
+        // Outer array of multidimensional array has wrong size, inner array
+        // has primitive type.
+
+        try {
+            r = new byte[minusOne][3];
+            fail();
+        } catch (NegativeArraySizeException e) {
+            Asserts.assertEQ("-1", e.getMessage());
+        }
+
+        try {
+            r = new byte[Integer.MIN_VALUE][3];
+            fail();
+        } catch (NegativeArraySizeException e) {
+            Asserts.assertEQ("-2147483648", e.getMessage());
+        }
+
+        // Tests for exception thrown in objArrayKlass.cpp, ObjArrayKlass::allocate().
+        // Outer array of multidimensional array has wrong size, inner array
+        // has object type.
+
+        try {
+            r = new Object[minusOne][3];
+            fail();
+        } catch (NegativeArraySizeException e) {
+            Asserts.assertEQ("-1", e.getMessage());
+        }
+
+        try {
+            r = new Object[Integer.MIN_VALUE][3];
+            fail();
+        } catch (NegativeArraySizeException e) {
+            Asserts.assertEQ("-2147483648", e.getMessage());
+        }
+
+        // Tests for exception thrown in reflection.cpp, Reflection::reflect_new_array().
+
+        try {
+            Array.newInstance(byte.class, minusOne);
+            fail();
+        } catch (NegativeArraySizeException e) {
+            Asserts.assertEQ("-1", e.getMessage());
+        }
+
+        try {
+            Array.newInstance(byte.class, Integer.MIN_VALUE);
+            fail();
+        } catch (NegativeArraySizeException e) {
+            Asserts.assertEQ("-2147483648", e.getMessage());
+        }
+
+        try {
+            Array.newInstance(NegativeArraySizeException.class, minusOne);
+            fail();
+        } catch (NegativeArraySizeException e) {
+            Asserts.assertEQ("-1", e.getMessage());
+        }
+
+        try {
+            Array.newInstance(NegativeArraySizeException.class, Integer.MIN_VALUE);
+            fail();
+        } catch (NegativeArraySizeException e) {
+            Asserts.assertEQ("-2147483648", e.getMessage());
+        }
+
+
+        // Tests for exception thrown in reflection.cpp, Reflection::reflect_new_multi_array().
+
+        try {
+            Array.newInstance(byte.class, new int[] {3, minusOne});
+            fail();
+        } catch (NegativeArraySizeException e) {
+            Asserts.assertEQ("-1", e.getMessage());
+        }
+
+        try {
+            Array.newInstance(byte.class, new int[] {3, Integer.MIN_VALUE});
+            fail();
+        } catch (NegativeArraySizeException e) {
+            Asserts.assertEQ("-2147483648", e.getMessage());
+        }
+
+        try {
+            Array.newInstance(byte.class, new int[] {0, minusOne});
+            fail();
+        } catch (NegativeArraySizeException e) {
+            Asserts.assertEQ("-1", e.getMessage());
+        }
+
+        try {
+            Array.newInstance(byte.class, new int[] {0, Integer.MIN_VALUE});
+            fail();
+        } catch (NegativeArraySizeException e) {
+            Asserts.assertEQ("-2147483648", e.getMessage());
+        }
+
+        try {
+            Array.newInstance(NegativeArraySizeException.class, new int[] {3, minusOne});
+            fail();
+        } catch (NegativeArraySizeException e) {
+            Asserts.assertEQ("-1", e.getMessage());
+        }
+
+        try {
+            Array.newInstance(NegativeArraySizeException.class, new int[] {3, Integer.MIN_VALUE});
+            fail();
+        } catch (NegativeArraySizeException e) {
+            Asserts.assertEQ("-2147483648", e.getMessage());
+        }
+
+        try {
+            Array.newInstance(byte.class, new int[] {minusOne, 3});
+            fail();
+        } catch (NegativeArraySizeException e) {
+            Asserts.assertEQ("-1", e.getMessage());
+        }
+
+        try {
+            Array.newInstance(byte.class, new int[] {Integer.MIN_VALUE, 3});
+            fail();
+        } catch (NegativeArraySizeException e) {
+            Asserts.assertEQ("-2147483648", e.getMessage());
+        }
+
+        try {
+            Array.newInstance(NegativeArraySizeException.class, new int[] {minusOne, 3});
+            fail();
+        } catch (NegativeArraySizeException e) {
+            Asserts.assertEQ("-1", e.getMessage());
+        }
+
+        try {
+            Array.newInstance(NegativeArraySizeException.class, new int[] {Integer.MIN_VALUE, 3});
+            fail();
+        } catch (NegativeArraySizeException e) {
+            Asserts.assertEQ("-2147483648", e.getMessage());
+        }
+
+        Asserts.assertEQ(r, null, "Expected all tries to allocate negative array to fail.");
+    }
+}