|
1 /* |
|
2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
|
3 * Copyright (c) 2018 SAP SE. All rights reserved. |
|
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
5 * |
|
6 * This code is free software; you can redistribute it and/or modify it |
|
7 * under the terms of the GNU General Public License version 2 only, as |
|
8 * published by the Free Software Foundation. |
|
9 * |
|
10 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 * version 2 for more details (a copy is included in the LICENSE file that |
|
14 * accompanied this code). |
|
15 * |
|
16 * You should have received a copy of the GNU General Public License version |
|
17 * 2 along with this work; if not, write to the Free Software Foundation, |
|
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
19 * |
|
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
21 * or visit www.oracle.com if you need additional information or have any |
|
22 * questions. |
|
23 */ |
|
24 |
|
25 /** |
|
26 * @test |
|
27 * @summary Test that NegativeArraySizeException reports the wrong size. |
|
28 * @library /test/lib |
|
29 * @compile NegativeArraySizeExceptionTest.java |
|
30 * @run main NegativeArraySizeExceptionTest |
|
31 */ |
|
32 |
|
33 import java.lang.reflect.Array; |
|
34 |
|
35 import jdk.test.lib.Asserts; |
|
36 |
|
37 public class NegativeArraySizeExceptionTest { |
|
38 |
|
39 private static void fail () throws Exception { |
|
40 throw new RuntimeException("Array allocation with negative size expected to fail!"); |
|
41 } |
|
42 |
|
43 public static void main(String[] args) throws Exception { |
|
44 int minusOne = -1; |
|
45 Object r = null; |
|
46 |
|
47 // Tests for exception thrown in arrayKlass.cp, ArrayKlass::allocate_arrayArray(). |
|
48 |
|
49 try { |
|
50 r = new byte[minusOne][]; |
|
51 fail(); |
|
52 } catch (NegativeArraySizeException e) { |
|
53 Asserts.assertEQ("-1", e.getMessage()); |
|
54 } |
|
55 |
|
56 try { |
|
57 r = new Object[Integer.MIN_VALUE][]; |
|
58 fail(); |
|
59 } catch (NegativeArraySizeException e) { |
|
60 Asserts.assertEQ("-2147483648", e.getMessage()); |
|
61 } |
|
62 |
|
63 // Tests for exception thrown in typeArrayKlass.cpp, TypeArrayKlass::allocate_common(). |
|
64 |
|
65 try { |
|
66 r = new byte[minusOne]; |
|
67 fail(); |
|
68 } catch (NegativeArraySizeException e) { |
|
69 Asserts.assertEQ("-1", e.getMessage()); |
|
70 } |
|
71 |
|
72 try { |
|
73 r = new byte[Integer.MIN_VALUE]; |
|
74 fail(); |
|
75 } catch (NegativeArraySizeException e) { |
|
76 Asserts.assertEQ("-2147483648", e.getMessage()); |
|
77 } |
|
78 |
|
79 // Tests for exception thrown in instanceKlass.cpp, InstanceKlass::allocate_objArray(). |
|
80 |
|
81 try { |
|
82 r = new Object[minusOne]; |
|
83 fail(); |
|
84 } catch (NegativeArraySizeException e) { |
|
85 Asserts.assertEQ("-1", e.getMessage()); |
|
86 } |
|
87 |
|
88 try { |
|
89 r = new Object[Integer.MIN_VALUE]; |
|
90 fail(); |
|
91 } catch (NegativeArraySizeException e) { |
|
92 Asserts.assertEQ("-2147483648", e.getMessage()); |
|
93 } |
|
94 |
|
95 // Tests for exception thrown in typeArrayKlass.cpp, TypeArrayKlass::allocate_common(). |
|
96 // Innermost primitive array of multidimensional array has wrong size. |
|
97 |
|
98 try { |
|
99 r = new byte[3][minusOne]; |
|
100 fail(); |
|
101 } catch (NegativeArraySizeException e) { |
|
102 Asserts.assertEQ("-1", e.getMessage()); |
|
103 } |
|
104 |
|
105 try { |
|
106 r = new byte[3][Integer.MIN_VALUE]; |
|
107 fail(); |
|
108 } catch (NegativeArraySizeException e) { |
|
109 Asserts.assertEQ("-2147483648", e.getMessage()); |
|
110 } |
|
111 |
|
112 // Tests for exception thrown in objArrayKlass.cpp, ObjArrayKlass::allocate(). |
|
113 // Innermost object array of multidimensional array has wrong size. |
|
114 |
|
115 try { |
|
116 r = new Object[3][minusOne]; |
|
117 fail(); |
|
118 } catch (NegativeArraySizeException e) { |
|
119 Asserts.assertEQ("-1", e.getMessage()); |
|
120 } |
|
121 |
|
122 try { |
|
123 r = new Object[3][Integer.MIN_VALUE]; |
|
124 fail(); |
|
125 } catch (NegativeArraySizeException e) { |
|
126 Asserts.assertEQ("-2147483648", e.getMessage()); |
|
127 } |
|
128 |
|
129 // Tests for exception thrown in |
|
130 // Innermost primitive array of multidimensional array has wrong size. |
|
131 // Outer array has size 0. |
|
132 |
|
133 try { |
|
134 r = new byte[0][minusOne]; |
|
135 fail(); |
|
136 } catch (NegativeArraySizeException e) { |
|
137 Asserts.assertEQ("-1", e.getMessage()); |
|
138 } |
|
139 |
|
140 try { |
|
141 r = new byte[0][Integer.MIN_VALUE]; |
|
142 fail(); |
|
143 } catch (NegativeArraySizeException e) { |
|
144 Asserts.assertEQ("-2147483648", e.getMessage()); |
|
145 } |
|
146 |
|
147 // Tests for exception thrown in |
|
148 // Innermost object array of multidimensional array has wrong size. |
|
149 // Outer array has size 0. |
|
150 |
|
151 try { |
|
152 r = new Object[0][minusOne]; |
|
153 fail(); |
|
154 } catch (NegativeArraySizeException e) { |
|
155 Asserts.assertEQ("-1", e.getMessage()); |
|
156 } |
|
157 |
|
158 try { |
|
159 r = new Object[0][Integer.MIN_VALUE]; |
|
160 fail(); |
|
161 } catch (NegativeArraySizeException e) { |
|
162 Asserts.assertEQ("-2147483648", e.getMessage()); |
|
163 } |
|
164 |
|
165 // Tests for exception thrown in objArrayKlass.cpp, ObjArrayKlass::allocate(). |
|
166 // Outer array of multidimensional array has wrong size, inner array |
|
167 // has primitive type. |
|
168 |
|
169 try { |
|
170 r = new byte[minusOne][3]; |
|
171 fail(); |
|
172 } catch (NegativeArraySizeException e) { |
|
173 Asserts.assertEQ("-1", e.getMessage()); |
|
174 } |
|
175 |
|
176 try { |
|
177 r = new byte[Integer.MIN_VALUE][3]; |
|
178 fail(); |
|
179 } catch (NegativeArraySizeException e) { |
|
180 Asserts.assertEQ("-2147483648", e.getMessage()); |
|
181 } |
|
182 |
|
183 // Tests for exception thrown in objArrayKlass.cpp, ObjArrayKlass::allocate(). |
|
184 // Outer array of multidimensional array has wrong size, inner array |
|
185 // has object type. |
|
186 |
|
187 try { |
|
188 r = new Object[minusOne][3]; |
|
189 fail(); |
|
190 } catch (NegativeArraySizeException e) { |
|
191 Asserts.assertEQ("-1", e.getMessage()); |
|
192 } |
|
193 |
|
194 try { |
|
195 r = new Object[Integer.MIN_VALUE][3]; |
|
196 fail(); |
|
197 } catch (NegativeArraySizeException e) { |
|
198 Asserts.assertEQ("-2147483648", e.getMessage()); |
|
199 } |
|
200 |
|
201 // Tests for exception thrown in reflection.cpp, Reflection::reflect_new_array(). |
|
202 |
|
203 try { |
|
204 Array.newInstance(byte.class, minusOne); |
|
205 fail(); |
|
206 } catch (NegativeArraySizeException e) { |
|
207 Asserts.assertEQ("-1", e.getMessage()); |
|
208 } |
|
209 |
|
210 try { |
|
211 Array.newInstance(byte.class, Integer.MIN_VALUE); |
|
212 fail(); |
|
213 } catch (NegativeArraySizeException e) { |
|
214 Asserts.assertEQ("-2147483648", e.getMessage()); |
|
215 } |
|
216 |
|
217 try { |
|
218 Array.newInstance(NegativeArraySizeException.class, minusOne); |
|
219 fail(); |
|
220 } catch (NegativeArraySizeException e) { |
|
221 Asserts.assertEQ("-1", e.getMessage()); |
|
222 } |
|
223 |
|
224 try { |
|
225 Array.newInstance(NegativeArraySizeException.class, Integer.MIN_VALUE); |
|
226 fail(); |
|
227 } catch (NegativeArraySizeException e) { |
|
228 Asserts.assertEQ("-2147483648", e.getMessage()); |
|
229 } |
|
230 |
|
231 |
|
232 // Tests for exception thrown in reflection.cpp, Reflection::reflect_new_multi_array(). |
|
233 |
|
234 try { |
|
235 Array.newInstance(byte.class, new int[] {3, minusOne}); |
|
236 fail(); |
|
237 } catch (NegativeArraySizeException e) { |
|
238 Asserts.assertEQ("-1", e.getMessage()); |
|
239 } |
|
240 |
|
241 try { |
|
242 Array.newInstance(byte.class, new int[] {3, Integer.MIN_VALUE}); |
|
243 fail(); |
|
244 } catch (NegativeArraySizeException e) { |
|
245 Asserts.assertEQ("-2147483648", e.getMessage()); |
|
246 } |
|
247 |
|
248 try { |
|
249 Array.newInstance(byte.class, new int[] {0, minusOne}); |
|
250 fail(); |
|
251 } catch (NegativeArraySizeException e) { |
|
252 Asserts.assertEQ("-1", e.getMessage()); |
|
253 } |
|
254 |
|
255 try { |
|
256 Array.newInstance(byte.class, new int[] {0, Integer.MIN_VALUE}); |
|
257 fail(); |
|
258 } catch (NegativeArraySizeException e) { |
|
259 Asserts.assertEQ("-2147483648", e.getMessage()); |
|
260 } |
|
261 |
|
262 try { |
|
263 Array.newInstance(NegativeArraySizeException.class, new int[] {3, minusOne}); |
|
264 fail(); |
|
265 } catch (NegativeArraySizeException e) { |
|
266 Asserts.assertEQ("-1", e.getMessage()); |
|
267 } |
|
268 |
|
269 try { |
|
270 Array.newInstance(NegativeArraySizeException.class, new int[] {3, Integer.MIN_VALUE}); |
|
271 fail(); |
|
272 } catch (NegativeArraySizeException e) { |
|
273 Asserts.assertEQ("-2147483648", e.getMessage()); |
|
274 } |
|
275 |
|
276 try { |
|
277 Array.newInstance(byte.class, new int[] {minusOne, 3}); |
|
278 fail(); |
|
279 } catch (NegativeArraySizeException e) { |
|
280 Asserts.assertEQ("-1", e.getMessage()); |
|
281 } |
|
282 |
|
283 try { |
|
284 Array.newInstance(byte.class, new int[] {Integer.MIN_VALUE, 3}); |
|
285 fail(); |
|
286 } catch (NegativeArraySizeException e) { |
|
287 Asserts.assertEQ("-2147483648", e.getMessage()); |
|
288 } |
|
289 |
|
290 try { |
|
291 Array.newInstance(NegativeArraySizeException.class, new int[] {minusOne, 3}); |
|
292 fail(); |
|
293 } catch (NegativeArraySizeException e) { |
|
294 Asserts.assertEQ("-1", e.getMessage()); |
|
295 } |
|
296 |
|
297 try { |
|
298 Array.newInstance(NegativeArraySizeException.class, new int[] {Integer.MIN_VALUE, 3}); |
|
299 fail(); |
|
300 } catch (NegativeArraySizeException e) { |
|
301 Asserts.assertEQ("-2147483648", e.getMessage()); |
|
302 } |
|
303 |
|
304 Asserts.assertEQ(r, null, "Expected all tries to allocate negative array to fail."); |
|
305 } |
|
306 } |