8203881: Print erroneous size in NegativeArraySizeException

Reviewed-by: dholmes, stuefe
This commit is contained in:
Goetz Lindenmaier 2018-05-30 11:39:26 +02:00
parent 6a48db9cf6
commit ff2c3d4dbe
6 changed files with 315 additions and 7 deletions

View File

@ -130,7 +130,7 @@ bool ArrayKlass::compute_is_subtype_of(Klass* k) {
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");

View File

@ -975,7 +975,9 @@ bool InstanceKlass::is_same_or_direct_interface(Klass *k) const {
}
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();

View File

@ -181,7 +181,7 @@ objArrayOop ObjArrayKlass::allocate(int length, TRAPS) {
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 @@ oop ObjArrayKlass::multi_allocate(int rank, jint* sizes, TRAPS) {
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));
}
}
}

View File

@ -116,7 +116,7 @@ typeArrayOop TypeArrayKlass::allocate_common(int length, bool do_zero, TRAPS) {
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));
}
}

View File

@ -337,7 +337,7 @@ arrayOop Reflection::reflect_new_array(oop element_mirror, jint length, TRAPS) {
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 @@ arrayOop Reflection::reflect_new_multi_array(oop element_mirror, typeArrayOop di
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;
}

View File

@ -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.");
}
}