1/* 2 * Copyright (c) 2013, Oracle and/or its affiliates. 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 * @run main BadClassFiles 27 * @summary The reflection API should throw the correct exceptions. 28 */ 29import java.lang.Class; 30import java.lang.reflect.Method; 31import java.lang.reflect.Parameter; 32import java.lang.reflect.MalformedParametersException; 33import java.lang.ClassLoader; 34import java.lang.ClassNotFoundException; 35import java.io.File; 36import java.io.FileOutputStream; 37import java.io.IOException; 38 39public class BadClassFiles { 40 private int errors = 0; 41 42 43 /* Class files were created by compiling the following source and 44 * then editing it: 45 * 46 * public class EmptyName { 47 * public void m(int a, int b) {} 48 * } 49 * 50 */ 51 private static final byte[] EmptyName_bytes = { 52 -54,-2,-70,-66,0,0,0,52, 53 0,18,10,0,3,0,15,7, 54 0,16,7,0,17,1,0,6, 55 60,105,110,105,116,62,1,0, 56 3,40,41,86,1,0,4,67, 57 111,100,101,1,0,15,76,105, 58 110,101,78,117,109,98,101,114, 59 84,97,98,108,101,1,0,1, 60 109,1,0,5,40,73,73,41, 61 86,1,0,16,77,101,116,104, 62 111,100,80,97,114,97,109,101, 63 116,101,114,115,1,0,0,1, 64 0,1,98,1,0,10,83,111, 65 117,114,99,101,70,105,108,101, 66 1,0,14,69,109,112,116,121, 67 78,97,109,101,46,106,97,118, 68 97,12,0,4,0,5,1,0, 69 9,69,109,112,116,121,78,97, 70 109,101,1,0,16,106,97,118, 71 97,47,108,97,110,103,47,79, 72 98,106,101,99,116,0,33,0, 73 2,0,3,0,0,0,0,0, 74 2,0,1,0,4,0,5,0, 75 1,0,6,0,0,0,29,0, 76 1,0,1,0,0,0,5,42, 77 -73,0,1,-79,0,0,0,1, 78 0,7,0,0,0,6,0,1, 79 0,0,0,1,0,1,0,8, 80 0,9,0,2,0,6,0,0, 81 0,25,0,0,0,3,0,0, 82 0,1,-79,0,0,0,1,0, 83 7,0,0,0,6,0,1,0, 84 0,0,2, 85 // Method Parameters attribute here 86 0,10, 87 // attribute_length 88 0,0,0,9, 89 // parameter_count 90 2, 91 // first parameter name 92 0,11, 93 // first parameter modifiers 94 0,0, 95 // second parameter name 96 0,12, 97 // second parameter modifiers 98 0,0, 99 // end attribute 100 0,1,0,13,0,0,0,2,0,14 101 }; 102 103 private static final byte[] BadModifiers_bytes = { 104 -54,-2,-70,-66,0,0,0,52, 105 0,18,10,0,3,0,15,7, 106 0,16,7,0,17,1,0,6, 107 60,105,110,105,116,62,1,0, 108 3,40,41,86,1,0,4,67, 109 111,100,101,1,0,15,76,105, 110 110,101,78,117,109,98,101,114, 111 84,97,98,108,101,1,0,1, 112 109,1,0,5,40,73,73,41, 113 86,1,0,16,77,101,116,104, 114 111,100,80,97,114,97,109,101, 115 116,101,114,115,1,0,1,97, 116 1,0,1,98,1,0,10,83, 117 111,117,114,99,101,70,105,108, 118 101,1,0,17,66,97,100,77, 119 111,100,105,102,105,101,114,115, 120 46,106,97,118,97,12,0,4, 121 0,5,1,0,12,66,97,100, 122 77,111,100,105,102,105,101,114, 123 115,1,0,16,106,97,118,97, 124 47,108,97,110,103,47,79,98, 125 106,101,99,116,0,33,0,2, 126 0,3,0,0,0,0,0,2, 127 0,1,0,4,0,5,0,1, 128 0,6,0,0,0,29,0,1, 129 0,1,0,0,0,5,42,-73, 130 0,1,-79,0,0,0,1,0, 131 7,0,0,0,6,0,1,0, 132 0,0,1,0,1,0,8,0, 133 9,0,2,0,6,0,0,0, 134 25,0,0,0,3,0,0,0, 135 1,-79,0,0,0,1,0,7, 136 0,0,0,6,0,1,0,0, 137 0,2, 138 // Method Parameters attribute here 139 0,10, 140 // attribute_length 141 0,0,0,9, 142 // parameter_count 143 2, 144 // first parameter name 145 0,11, 146 // first parameter modifiers 147 51,51, 148 // second parameter name 149 0,12, 150 // second parameter modifiers 151 0,0, 152 // end attribute 153 0,1,0,13,0,0,0,2,0,14 154 }; 155 156 private static final byte[] BadNameIndex_bytes = { 157 -54,-2,-70,-66,0,0,0,52, 158 0,18,10,0,3,0,15,7, 159 0,16,7,0,17,1,0,6, 160 60,105,110,105,116,62,1,0, 161 3,40,41,86,1,0,4,67, 162 111,100,101,1,0,15,76,105, 163 110,101,78,117,109,98,101,114, 164 84,97,98,108,101,1,0,1, 165 109,1,0,5,40,73,73,41, 166 86,1,0,16,77,101,116,104, 167 111,100,80,97,114,97,109,101, 168 116,101,114,115,1,0,1,97, 169 1,0,1,98,1,0,10,83, 170 111,117,114,99,101,70,105,108, 171 101,1,0,17,66,97,100,78, 172 97,109,101,73,110,100,101,120, 173 46,106,97,118,97,12,0,4, 174 0,5,1,0,12,66,97,100, 175 78,97,109,101,73,110,100,101, 176 120,1,0,16,106,97,118,97, 177 47,108,97,110,103,47,79,98, 178 106,101,99,116,0,33,0,2, 179 0,3,0,0,0,0,0,2, 180 0,1,0,4,0,5,0,1, 181 0,6,0,0,0,29,0,1, 182 0,1,0,0,0,5,42,-73, 183 0,1,-79,0,0,0,1,0, 184 7,0,0,0,6,0,1,0, 185 0,0,1,0,1,0,8,0, 186 9,0,2,0,6,0,0,0, 187 25,0,0,0,3,0,0,0, 188 1,-79,0,0,0,1,0,7, 189 0,0,0,6,0,1,0,0, 190 0,2, 191 // Method Parameters attribute here 192 0,10, 193 // attribute_length 194 0,0,0,9, 195 // parameter_count 196 2, 197 // first parameter name 198 0,1, 199 // first parameter modifiers 200 0,0, 201 // second parameter name 202 0,12, 203 // second parameter modifiers 204 0,0, 205 // end attribute 206 0,1,0,13,0,0,0,2,0,14 207 }; 208 209 private static final byte[] NameIndexOutOfBounds_bytes = { 210 -54,-2,-70,-66,0,0,0,52, 211 0,18,10,0,3,0,15,7, 212 0,16,7,0,17,1,0,6, 213 60,105,110,105,116,62,1,0, 214 3,40,41,86,1,0,4,67, 215 111,100,101,1,0,15,76,105, 216 110,101,78,117,109,98,101,114, 217 84,97,98,108,101,1,0,1, 218 109,1,0,5,40,73,73,41, 219 86,1,0,16,77,101,116,104, 220 111,100,80,97,114,97,109,101, 221 116,101,114,115,1,0,1,97, 222 1,0,1,98,1,0,10,83, 223 111,117,114,99,101,70,105,108, 224 101,1,0,25,78,97,109,101, 225 73,110,100,101,120,79,117,116, 226 79,102,66,111,117,110,100,115, 227 46,106,97,118,97,12,0,4, 228 0,5,1,0,20,78,97,109, 229 101,73,110,100,101,120,79,117, 230 116,79,102,66,111,117,110,100, 231 115,1,0,16,106,97,118,97, 232 47,108,97,110,103,47,79,98, 233 106,101,99,116,0,33,0,2, 234 0,3,0,0,0,0,0,2, 235 0,1,0,4,0,5,0,1, 236 0,6,0,0,0,29,0,1, 237 0,1,0,0,0,5,42,-73, 238 0,1,-79,0,0,0,1,0, 239 7,0,0,0,6,0,1,0, 240 0,0,1,0,1,0,8,0, 241 9,0,2,0,6,0,0,0, 242 25,0,0,0,3,0,0,0, 243 1,-79,0,0,0,1,0,7, 244 0,0,0,6,0,1,0,0, 245 0,2, 246 // Method Parameters attribute here 247 0,10, 248 // attribute_length 249 0,0,0,9, 250 // parameter_count 251 2, 252 // first parameter name 253 0,-1, 254 // first parameter modifiers 255 0,0, 256 // second parameter name 257 0,12, 258 // second parameter modifiers 259 0,0, 260 // end attribute 261 0,1,0,13,0,0,0,2,0,14 262 }; 263 264 private static final byte[] ExtraParams_bytes = { 265 -54,-2,-70,-66,0,0,0,52, 266 0,18,10,0,3,0,15,7, 267 0,16,7,0,17,1,0,6, 268 60,105,110,105,116,62,1,0, 269 3,40,41,86,1,0,4,67, 270 111,100,101,1,0,15,76,105, 271 110,101,78,117,109,98,101,114, 272 84,97,98,108,101,1,0,1, 273 109,1,0,5,40,73,73,41, 274 86,1,0,16,77,101,116,104, 275 111,100,80,97,114,97,109,101, 276 116,101,114,115,1,0,1,97, 277 1,0,1,98,1,0,10,83, 278 111,117,114,99,101,70,105,108, 279 101,1,0,16,69,120,116,114, 280 97,80,97,114,97,109,115,46, 281 106,97,118,97,12,0,4,0, 282 5,1,0,11,69,120,116,114, 283 97,80,97,114,97,109,115,1, 284 0,16,106,97,118,97,47,108, 285 97,110,103,47,79,98,106,101, 286 99,116,0,33,0,2,0,3, 287 0,0,0,0,0,2,0,1, 288 0,4,0,5,0,1,0,6, 289 0,0,0,29,0,1,0,1, 290 0,0,0,5,42,-73,0,1, 291 -79,0,0,0,1,0,7,0, 292 0,0,6,0,1,0,0,0, 293 1,0,1,0,8,0,9,0, 294 2,0,6,0,0,0,25,0, 295 0,0,3,0,0,0,1,-79, 296 0,0,0,1,0,7,0,0, 297 0,6,0,1,0,0,0,2, 298 // Method Parameters attribute here 299 0,10, 300 // attribute_length 301 0,0,0,13, 302 // parameter_count 303 3, 304 // first parameter name 305 0,11, 306 // first parameter modifiers 307 0,0, 308 // second parameter name 309 0,12, 310 // second parameter modifiers 311 0,0, 312 // third parameter name 313 0,11, 314 // third parameter modifiers 315 0,0, 316 // end attribute 317 0,1,0,13,0,0,0,2,0,14 318 }; 319 320 private static final byte[] BadName1_bytes = { 321 -54,-2,-70,-66,0,0,0,52, 322 0,18,10,0,3,0,15,7, 323 0,16,7,0,17,1,0,6, 324 60,105,110,105,116,62,1,0, 325 3,40,41,86,1,0,4,67, 326 111,100,101,1,0,15,76,105, 327 110,101,78,117,109,98,101,114, 328 84,97,98,108,101,1,0,1, 329 109,1,0,5,40,73,73,41, 330 86,1,0,16,77,101,116,104, 331 111,100,80,97,114,97,109,101, 332 116,101,114,115,1,0,1,46, 333 1,0,1,98,1,0,10,83, 334 111,117,114,99,101,70,105,108, 335 101,1,0,13,66,97,100,78, 336 97,109,101,49,46,106,97,118, 337 97,12,0,4,0,5,1,0, 338 8,66,97,100,78,97,109,101, 339 49,1,0,16,106,97,118,97, 340 47,108,97,110,103,47,79,98, 341 106,101,99,116,0,33,0,2, 342 0,3,0,0,0,0,0,2, 343 0,1,0,4,0,5,0,1, 344 0,6,0,0,0,29,0,1, 345 0,1,0,0,0,5,42,-73, 346 0,1,-79,0,0,0,1,0, 347 7,0,0,0,6,0,1,0, 348 0,0,1,0,1,0,8,0, 349 9,0,2,0,6,0,0,0, 350 25,0,0,0,3,0,0,0, 351 1,-79,0,0,0,1,0,7, 352 0,0,0,6,0,1,0,0, 353 0,2, 354 // Method Parameters attribute here 355 0,10, 356 // attribute_length 357 0,0,0,9, 358 // parameter_count 359 2, 360 // first parameter name 361 0,11, 362 // first parameter modifiers 363 0,0, 364 // second parameter name 365 0,12, 366 // second parameter modifiers 367 0,0, 368 // end attribute 369 0,1,0,13,0,0,0,2,0,14 370 }; 371 372 private static final byte[] BadName2_bytes = { 373 -54,-2,-70,-66,0,0,0,52, 374 0,18,10,0,3,0,15,7, 375 0,16,7,0,17,1,0,6, 376 60,105,110,105,116,62,1,0, 377 3,40,41,86,1,0,4,67, 378 111,100,101,1,0,15,76,105, 379 110,101,78,117,109,98,101,114, 380 84,97,98,108,101,1,0,1, 381 109,1,0,5,40,73,73,41, 382 86,1,0,16,77,101,116,104, 383 111,100,80,97,114,97,109,101, 384 116,101,114,115,1,0,1,91, 385 1,0,1,98,1,0,10,83, 386 111,117,114,99,101,70,105,108, 387 101,1,0,13,66,97,100,78, 388 97,109,101,50,46,106,97,118, 389 97,12,0,4,0,5,1,0, 390 8,66,97,100,78,97,109,101, 391 50,1,0,16,106,97,118,97, 392 47,108,97,110,103,47,79,98, 393 106,101,99,116,0,33,0,2, 394 0,3,0,0,0,0,0,2, 395 0,1,0,4,0,5,0,1, 396 0,6,0,0,0,29,0,1, 397 0,1,0,0,0,5,42,-73, 398 0,1,-79,0,0,0,1,0, 399 7,0,0,0,6,0,1,0, 400 0,0,1,0,1,0,8,0, 401 9,0,2,0,6,0,0,0, 402 25,0,0,0,3,0,0,0, 403 1,-79,0,0,0,1,0,7, 404 0,0,0,6,0,1,0,0, 405 0,2, 406 // Method Parameters attribute here 407 0,10, 408 // attribute_length 409 0,0,0,9, 410 // parameter_count 411 2, 412 // first parameter name 413 0,11, 414 // first parameter modifiers 415 0,0, 416 // second parameter name 417 0,12, 418 // second parameter modifiers 419 0,0, 420 // end attribute 421 0,1,0,13,0,0,0,2,0,14 422 }; 423 424 private static final byte[] BadName3_bytes = { 425 -54,-2,-70,-66,0,0,0,52, 426 0,18,10,0,3,0,15,7, 427 0,16,7,0,17,1,0,6, 428 60,105,110,105,116,62,1,0, 429 3,40,41,86,1,0,4,67, 430 111,100,101,1,0,15,76,105, 431 110,101,78,117,109,98,101,114, 432 84,97,98,108,101,1,0,1, 433 109,1,0,5,40,73,73,41, 434 86,1,0,16,77,101,116,104, 435 111,100,80,97,114,97,109,101, 436 116,101,114,115,1,0,1,59, 437 1,0,1,98,1,0,10,83, 438 111,117,114,99,101,70,105,108, 439 101,1,0,13,66,97,100,78, 440 97,109,101,51,46,106,97,118, 441 97,12,0,4,0,5,1,0, 442 8,66,97,100,78,97,109,101, 443 51,1,0,16,106,97,118,97, 444 47,108,97,110,103,47,79,98, 445 106,101,99,116,0,33,0,2, 446 0,3,0,0,0,0,0,2, 447 0,1,0,4,0,5,0,1, 448 0,6,0,0,0,29,0,1, 449 0,1,0,0,0,5,42,-73, 450 0,1,-79,0,0,0,1,0, 451 7,0,0,0,6,0,1,0, 452 0,0,1,0,1,0,8,0, 453 9,0,2,0,6,0,0,0, 454 25,0,0,0,3,0,0,0, 455 1,-79,0,0,0,1,0,7, 456 0,0,0,6,0,1,0,0, 457 0,2, 458 // Method Parameters attribute here 459 0,10, 460 // attribute_length 461 0,0,0,9, 462 // parameter_count 463 2, 464 // first parameter name 465 0,11, 466 // first parameter modifiers 467 0,0, 468 // second parameter name 469 0,12, 470 // second parameter modifiers 471 0,0, 472 // end attribute 473 0,1,0,13,0,0,0,2,0,14 474 }; 475 476 private static final byte[] BadName4_bytes = { 477 -54,-2,-70,-66,0,0,0,52, 478 0,18,10,0,3,0,15,7, 479 0,16,7,0,17,1,0,6, 480 60,105,110,105,116,62,1,0, 481 3,40,41,86,1,0,4,67, 482 111,100,101,1,0,15,76,105, 483 110,101,78,117,109,98,101,114, 484 84,97,98,108,101,1,0,1, 485 109,1,0,5,40,73,73,41, 486 86,1,0,16,77,101,116,104, 487 111,100,80,97,114,97,109,101, 488 116,101,114,115,1,0,1,47, 489 1,0,1,98,1,0,10,83, 490 111,117,114,99,101,70,105,108, 491 101,1,0,13,66,97,100,78, 492 97,109,101,52,46,106,97,118, 493 97,12,0,4,0,5,1,0, 494 8,66,97,100,78,97,109,101, 495 52,1,0,16,106,97,118,97, 496 47,108,97,110,103,47,79,98, 497 106,101,99,116,0,33,0,2, 498 0,3,0,0,0,0,0,2, 499 0,1,0,4,0,5,0,1, 500 0,6,0,0,0,29,0,1, 501 0,1,0,0,0,5,42,-73, 502 0,1,-79,0,0,0,1,0, 503 7,0,0,0,6,0,1,0, 504 0,0,1,0,1,0,8,0, 505 9,0,2,0,6,0,0,0, 506 25,0,0,0,3,0,0,0, 507 1,-79,0,0,0,1,0,7, 508 0,0,0,6,0,1,0,0, 509 0,2, 510 // Method Parameters attribute here 511 0,10, 512 // attribute_length 513 0,0,0,9, 514 // parameter_count 515 2, 516 // first parameter name 517 0,11, 518 // first parameter modifiers 519 0,0, 520 // second parameter name 521 0,12, 522 // second parameter modifiers 523 0,0, 524 // end attribute 525 0,1,0,13,0,0,0,2,0,14 526 }; 527 528 private static final byte[] BadParams_bytes = { 529 -54,-2,-70,-66,0,0,0, 530 52,0,18,10,0,3,0,15, 531 7,0,16,7,0,17,1,0,6, 532 60,105,110,105,116,62, 533 1,0,3,40,41,86,1,0,4, 534 67,111,100,101,1,0,15, 535 76,105,110,101,78,117, 536 109,98,101,114,84,97, 537 98,108,101,1,0,1,109, 538 1,0,5,40,73,73,41,86, 539 1,0,16,77,101,116,104, 540 111,100,80,97,114,97, 541 109,101,116,101,114,115, 542 1,0,1,97,1,0,1,98,1, 543 0,10,83,111,117,114, 544 99,101,70,105,108,101, 545 1,0,14,66,97,100,80,97, 546 114,97,109,115,46,106, 547 97,118,97,12,0,4,0,5, 548 1,0,9,66,97,100,80,97, 549 114,97,109,115,1,0,16, 550 106,97,118,97,47,108,97, 551 110,103,47,79,98,106, 552 101,99,116,0,33,0, 553 2,0,3,0,0,0,0,0,2, 554 0,1,0,4,0,5,0,1,0, 555 6,0,0,0,29,0,1,0,1, 556 0,0,0,5,42,-73,0,1, 557 -79,0,0,0,1,0,7,0,0, 558 0,6,0,1,0,0,0,1,0,1, 559 0,8,0,9,0,2,0,6,0,0, 560 0,25,0,0,0,3,0,0,0,1, 561 -79,0,0,0,1,0,7,0,0, 562 0,6,0,1,0,0,0,2, 563 // Method Parameters attribute here 564 0,10, 565 // attribute_length 566 0,0,0,1, 567 // parameter_count 568 0, 569 // end attribute 570 0,1,0,13,0,0,0,2,0,14 571 }; 572 573 private static class InMemoryClassLoader extends ClassLoader { 574 public Class<?> defineClass(String name, byte[] b) { 575 return defineClass(name, b, 0, b.length); 576 } 577 }; 578 579 private static final InMemoryClassLoader loader = new InMemoryClassLoader(); 580 581 private final Class<?>[] classes; 582 583 private BadClassFiles() throws ClassNotFoundException { 584 classes = new Class<?>[] { 585 loader.defineClass("EmptyName", EmptyName_bytes), 586 loader.defineClass("BadModifiers", BadModifiers_bytes), 587 loader.defineClass("BadNameIndex", BadNameIndex_bytes), 588 loader.defineClass("NameIndexOutOfBounds", NameIndexOutOfBounds_bytes), 589 loader.defineClass("ExtraParams", ExtraParams_bytes), 590 loader.defineClass("BadParams", BadParams_bytes), 591 // Name with . 592 loader.defineClass("BadName1", BadName1_bytes), 593 // Name with [ 594 loader.defineClass("BadName2", BadName2_bytes), 595 // Name with ; 596 loader.defineClass("BadName3", BadName3_bytes), 597 // Name with / 598 loader.defineClass("BadName4", BadName4_bytes) 599 }; 600 } 601 602 public static void main(String... args) 603 throws NoSuchMethodException, IOException, ClassNotFoundException { 604 new BadClassFiles().run(); 605 } 606 607 public void assertBadParameters(Class<?> cls) throws NoSuchMethodException { 608 try { 609 System.err.println("Trying " + cls); 610 final Method method = cls.getMethod("m", int.class, int.class); 611 final Parameter[] params = method.getParameters(); 612 System.err.println("Name " + params[0].getName()); 613 System.err.println("Did not see expected exception"); 614 errors++; 615 } catch(MalformedParametersException e) { 616 System.err.println("Expected exception seen"); 617 } 618 } 619 620 public void run() throws NoSuchMethodException { 621 for (Class<?> cls : classes) 622 assertBadParameters(cls); 623 624 if (errors != 0) 625 throw new RuntimeException(errors + " errors in test"); 626 } 627} 628